blob: 830732c457817ed63af1ce1c603cbe86b3ff3cfd [file] [log] [blame]
Abhilash S.L7f17e402019-03-15 17:40:41 +05301/*
2 * Copyright 2019-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070017//Package resourcemanager provides the utility for managing resources
manikkaraj kbf256be2019-03-25 00:13:48 +053018package resourcemanager
Abhilash S.L7f17e402019-03-15 17:40:41 +053019
20import (
npujarec5762e2020-01-01 14:08:48 +053021 "context"
Girish Gowdru0c588b22019-04-23 23:24:56 -040022 "encoding/json"
23 "errors"
24 "fmt"
Girish Gowdra76a1b092021-07-28 10:07:04 -070025 tp "github.com/opencord/voltha-lib-go/v6/pkg/techprofile"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070026 "strings"
Girish Gowdra38d533d2020-03-30 20:38:51 -070027 "sync"
Neha Sharmacc656962020-04-14 14:26:11 +000028 "time"
Abhilash S.L7f17e402019-03-15 17:40:41 +053029
Girish Gowdra4c3d4602021-07-22 16:33:37 -070030 "github.com/opencord/voltha-lib-go/v6/pkg/db"
31 "github.com/opencord/voltha-lib-go/v6/pkg/db/kvstore"
32 "github.com/opencord/voltha-lib-go/v6/pkg/log"
33 ponrmgr "github.com/opencord/voltha-lib-go/v6/pkg/ponresourcemanager"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070034 ofp "github.com/opencord/voltha-protos/v4/go/openflow_13"
35 "github.com/opencord/voltha-protos/v4/go/openolt"
Abhilash S.L7f17e402019-03-15 17:40:41 +053036)
37
salmansiddiqui7ac62132019-08-22 03:58:50 +000038const (
39 // KvstoreTimeout specifies the time out for KV Store Connection
Neha Sharmacc656962020-04-14 14:26:11 +000040 KvstoreTimeout = 5 * time.Second
Matteo Scandolodfa7a972020-11-06 13:03:40 -080041 // BasePathKvStore - <pathPrefix>/openolt/<device_id>
42 BasePathKvStore = "%s/openolt/{%s}"
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -070043 // tpIDPathSuffix - <(pon_id, onu_id, uni_id)>/tp_id
44 tpIDPathSuffix = "{%d,%d,%d}/tp_id"
Gamze Abakafee36392019-10-03 11:17:24 +000045 //MeterIDPathSuffix - <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
46 MeterIDPathSuffix = "{%d,%d,%d}/{%d}/meter_id/{%s}"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070047 // OnuPacketINPathPrefix - path prefix where ONU packet-in vlanID/PCP is stored
48 //format: onu_packetin/{<intfid>,<onuid>,<logicalport>}
49 OnuPacketINPathPrefix = "onu_packetin/{%d,%d,%d}"
50 // OnuPacketINPath path on the kvstore to store packetin gemport,which will be used for packetin, packetout
51 //format: onu_packetin/{<intfid>,<onuid>,<logicalport>}/{<vlanId>,<priority>}
52 OnuPacketINPath = OnuPacketINPathPrefix + "/{%d,%d}"
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -070053 //FlowIDsForGem flowids_per_gem/<intfid>/<gemport-id>
54 FlowIDsForGem = "flowids_per_gem/{%d}/{%d}"
Esin Karamanccb714b2019-11-29 15:02:06 +000055 //McastQueuesForIntf multicast queues for pon interfaces
56 McastQueuesForIntf = "mcast_qs_for_int"
57 //FlowGroup flow_groups/<flow_group_id>
58 // A group is stored under this path on the KV store after it has been installed to the device.
59 // It should also be deleted after it has been removed from the device accordingly.
60 FlowGroup = "flow_groups/{%d}"
61 //FlowGroupCached flow_groups_cached/<flow_group_id>
62 // When a group add request received, we create the group without setting any members to it since we cannot
63 // set any members to a group until it is associated with a multicast flow. It is a BAL limitation.
64 // When the related multicast flow has been created we perform set members operation for the group.
65 // That is why we need to keep the members of a group until the multicast flow creation request comes.
66 // We preserve the groups under "FlowGroupsCached" directory in the KV store temporarily. Having set members,
67 // we remove the group from the cached group store.
68 FlowGroupCached = "flow_groups_cached/{%d}"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070069
70 //FlowIDPath - Path on the KV store for storing list of Flow IDs for a given subscriber
71 //Format: BasePathKvStore/<(pon_intf_id, onu_id, uni_id)>/flow_ids
72 FlowIDPath = "{%s}/flow_ids"
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -070073
74 //OnuGemInfoPath is path on the kvstore to store onugem info map
75 //format: <device-id>/onu_gem_info/<intfid>
76 OnuGemInfoPath = "onu_gem_info/{%d}/{%d}" // onu_gem/<intfid>/<onuID>
salmansiddiqui7ac62132019-08-22 03:58:50 +000077)
Abhilash S.L7f17e402019-03-15 17:40:41 +053078
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070079// FlowInfo holds the flow information
Abhilash S.L8ee90712019-04-29 16:24:22 +053080type FlowInfo struct {
Girish Gowdraa09aeab2020-09-14 16:30:52 -070081 Flow *openolt.Flow
82 IsSymmtricFlow bool
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053083}
84
85// OnuGemInfo holds onu information along with gem port list and uni port list
86type OnuGemInfo struct {
87 OnuID uint32
88 SerialNumber string
89 IntfID uint32
90 GemPorts []uint32
91 UniPorts []uint32
92}
93
94// PacketInInfoKey is the key for packet in gemport
95type PacketInInfoKey struct {
96 IntfID uint32
97 OnuID uint32
98 LogicalPort uint32
Esin Karaman7fb80c22020-07-16 14:23:33 +000099 VlanID uint16
100 Priority uint8
Abhilash S.L8ee90712019-04-29 16:24:22 +0530101}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700102
Esin Karamanccb714b2019-11-29 15:02:06 +0000103// GroupInfo holds group information
104type GroupInfo struct {
105 GroupID uint32
106 OutPorts []uint32
107}
108
Girish Gowdraa482f272021-03-24 23:04:19 -0700109// MeterInfo store meter information at path <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
110type MeterInfo struct {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700111 RefCnt uint8 // number of flow references for this meter. When RefCnt is 0, the MeterInfo should be deleted.
112 MeterID uint32
Girish Gowdraa482f272021-03-24 23:04:19 -0700113}
114
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700115// OpenOltResourceMgr holds resource related information as provided below for each field
Abhilash S.L7f17e402019-03-15 17:40:41 +0530116type OpenOltResourceMgr struct {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700117 PonIntfID uint32
Neha Sharma3f221ae2020-04-29 19:02:12 +0000118 DeviceID string // OLT device id
119 Address string // Host and port of the kv store to connect to
120 Args string // args
121 KVStore *db.Backend // backend kv store connection handle
122 DeviceType string
123 DevInfo *openolt.DeviceInfo // device information
Girish Gowdru0c588b22019-04-23 23:24:56 -0400124 // array of pon resource managers per interface technology
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700125 PonRsrMgr *ponrmgr.PONResourceManager
Girish Gowdra38d533d2020-03-30 20:38:51 -0700126
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700127 // Local maps used for write-through-cache - start
128 flowIDsForOnu map[string][]uint64
129 flowIDsForOnuLock sync.RWMutex
130
131 allocIDsForOnu map[string][]uint32
132 allocIDsForOnuLock sync.RWMutex
133
134 gemPortIDsForOnu map[string][]uint32
135 gemPortIDsForOnuLock sync.RWMutex
136
137 techProfileIDsForOnu map[string][]uint32
138 techProfileIDsForOnuLock sync.RWMutex
139
140 meterInfoForOnu map[string]*MeterInfo
141 meterInfoForOnuLock sync.RWMutex
142
143 onuGemInfo map[string]*OnuGemInfo
144 onuGemInfoLock sync.RWMutex
145
146 gemPortForPacketInInfo map[string]uint32
147 gemPortForPacketInInfoLock sync.RWMutex
148
149 flowIDsForGem map[uint32][]uint64
150 flowIDsForGemLock sync.RWMutex
151
152 mcastQueueForIntf map[uint32][]uint32
153 mcastQueueForIntfLock sync.RWMutex
154 mcastQueueForIntfLoadedFromKvStore bool
155
156 groupInfo map[string]*GroupInfo
157 groupInfoLock sync.RWMutex
158 // Local maps used for write-through-cache - end
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700159
Girish Gowdra76a1b092021-07-28 10:07:04 -0700160 TechprofileRef tp.TechProfileIf
Abhilash S.L7f17e402019-03-15 17:40:41 +0530161}
162
Neha Sharma96b7bf22020-06-15 10:37:32 +0000163func newKVClient(ctx context.Context, storeType string, address string, timeout time.Duration) (kvstore.Client, error) {
164 logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400165 switch storeType {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400166 case "etcd":
Neha Sharma96b7bf22020-06-15 10:37:32 +0000167 return kvstore.NewEtcdClient(ctx, address, timeout, log.FatalLevel)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400168 }
169 return nil, errors.New("unsupported-kv-store")
Abhilash S.L7f17e402019-03-15 17:40:41 +0530170}
171
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700172// SetKVClient sets the KV client and return a kv backend
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800173func SetKVClient(ctx context.Context, backend string, addr string, DeviceID string, basePathKvStore string) *db.Backend {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400174 // TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
175 // issue between kv store and backend , core is not calling NewBackend directly
Neha Sharma96b7bf22020-06-15 10:37:32 +0000176 kvClient, err := newKVClient(ctx, backend, addr, KvstoreTimeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400177 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000178 logger.Fatalw(ctx, "Failed to init KV client\n", log.Fields{"err": err})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400179 return nil
180 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700181 // return db.NewBackend(ctx, backend, addr, KvstoreTimeout, fmt.Sprintf(BasePathKvStore, basePathKvStore, DeviceID))
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700182
sbarbaria8910ba2019-11-05 10:12:23 -0500183 kvbackend := &db.Backend{
Girish Gowdru0c588b22019-04-23 23:24:56 -0400184 Client: kvClient,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700185 StoreType: backend,
Neha Sharma3f221ae2020-04-29 19:02:12 +0000186 Address: addr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700187 Timeout: KvstoreTimeout,
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800188 PathPrefix: fmt.Sprintf(BasePathKvStore, basePathKvStore, DeviceID)}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530189
Girish Gowdru0c588b22019-04-23 23:24:56 -0400190 return kvbackend
Abhilash S.L7f17e402019-03-15 17:40:41 +0530191}
192
Gamze Abakafee36392019-10-03 11:17:24 +0000193// NewResourceMgr init a New resource manager instance which in turn instantiates pon resource manager
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700194// instances according to technology. Initializes the default resource ranges for all
195// the resources.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700196func NewResourceMgr(ctx context.Context, PonIntfID uint32, deviceID string, KVStoreAddress string, kvStoreType string, deviceType string, devInfo *openolt.DeviceInfo, basePathKvStore string) *OpenOltResourceMgr {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400197 var ResourceMgr OpenOltResourceMgr
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700198 logger.Debugf(ctx, "Init new resource manager , ponIf: %v, address: %s, device-id: %s", PonIntfID, KVStoreAddress, deviceID)
199 ResourceMgr.PonIntfID = PonIntfID
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700200 ResourceMgr.DeviceID = deviceID
Neha Sharma3f221ae2020-04-29 19:02:12 +0000201 ResourceMgr.Address = KVStoreAddress
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700202 ResourceMgr.DeviceType = deviceType
203 ResourceMgr.DevInfo = devInfo
Abhilash S.L7f17e402019-03-15 17:40:41 +0530204
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700205 Backend := kvStoreType
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800206 ResourceMgr.KVStore = SetKVClient(ctx, Backend, ResourceMgr.Address, deviceID, basePathKvStore)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400207 if ResourceMgr.KVStore == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000208 logger.Error(ctx, "Failed to setup KV store")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400209 }
Girish Gowdra38d533d2020-03-30 20:38:51 -0700210
Girish Gowdru0c588b22019-04-23 23:24:56 -0400211 // TODO self.args = registry('main').get_args()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530212
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700213 // Create a separate Resource Manager instance for each range. This assumes that
Girish Gowdru0c588b22019-04-23 23:24:56 -0400214 // each technology is represented by only a single range
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700215 for _, TechRange := range devInfo.Ranges {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700216 for _, intfID := range TechRange.IntfIds {
217 if intfID == PonIntfID {
218 technology := TechRange.Technology
219 logger.Debugf(ctx, "Device info technology %s, intf-id %v", technology, PonIntfID)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000220
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700221 rsrMgr, err := ponrmgr.NewPONResourceManager(ctx, technology, deviceType, deviceID,
222 Backend, ResourceMgr.Address, basePathKvStore)
223 if err != nil {
224 logger.Errorf(ctx, "Failed to create pon resource manager instance for technology %s", technology)
225 return nil
226 }
227 ResourceMgr.PonRsrMgr = rsrMgr
228 // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
229 InitializeDeviceResourceRangeAndPool(ctx, rsrMgr, TechRange, devInfo)
230 if err := ResourceMgr.PonRsrMgr.InitDeviceResourcePoolForIntf(ctx, intfID); err != nil {
231 logger.Fatal(ctx, "failed-to-initialize-device-resource-pool-intf-id-%v-device-id", ResourceMgr.PonIntfID, ResourceMgr.DeviceID)
232 return nil
233 }
234 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400235 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400236 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700237
238 ResourceMgr.InitLocalCache()
239
Neha Sharma96b7bf22020-06-15 10:37:32 +0000240 logger.Info(ctx, "Initialization of resource manager success!")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400241 return &ResourceMgr
Abhilash S.L7f17e402019-03-15 17:40:41 +0530242}
243
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700244//InitLocalCache initializes local maps used for write-through-cache
245func (rsrcMgr *OpenOltResourceMgr) InitLocalCache() {
246 rsrcMgr.flowIDsForOnu = make(map[string][]uint64)
247 rsrcMgr.allocIDsForOnu = make(map[string][]uint32)
248 rsrcMgr.gemPortIDsForOnu = make(map[string][]uint32)
249 rsrcMgr.techProfileIDsForOnu = make(map[string][]uint32)
250 rsrcMgr.meterInfoForOnu = make(map[string]*MeterInfo)
251 rsrcMgr.onuGemInfo = make(map[string]*OnuGemInfo)
252 rsrcMgr.gemPortForPacketInInfo = make(map[string]uint32)
253 rsrcMgr.flowIDsForGem = make(map[uint32][]uint64)
254 rsrcMgr.mcastQueueForIntf = make(map[uint32][]uint32)
255 rsrcMgr.groupInfo = make(map[string]*GroupInfo)
256}
257
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700258// InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply
259// device specific information. If KV doesn't exist
260// or is broader than the device, the device's information will
261// dictate the range limits
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700262func InitializeDeviceResourceRangeAndPool(ctx context.Context, ponRMgr *ponrmgr.PONResourceManager,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700263 techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700264 // var ONUIDShared, AllocIDShared, GEMPortIDShared openolt.DeviceInfo_DeviceResourceRanges_Pool_SharingType
265 var ONUIDStart, ONUIDEnd, AllocIDStart, AllocIDEnd, GEMPortIDStart, GEMPortIDEnd uint32
266 var ONUIDShared, AllocIDShared, GEMPortIDShared, FlowIDShared uint32
267
268 // The below variables are just dummy and needed to pass as arguments to InitDefaultPONResourceRanges function.
269 // The openolt adapter does not need flowIDs to be managed as it is managed on the OLT device
270 // The UNI IDs are dynamically generated by openonu adapter for every discovered UNI.
271 var flowIDDummyStart, flowIDDummyEnd uint32 = 1, 2
272 var uniIDDummyStart, uniIDDummyEnd uint32 = 0, 1
Abhilash S.L7f17e402019-03-15 17:40:41 +0530273
Girish Gowdru0c588b22019-04-23 23:24:56 -0400274 // init the resource range pool according to the sharing type
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700275 logger.Debugw(ctx, "Device info init", log.Fields{"technology": techRange.Technology,
276 "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd,
277 "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd,
278 "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd,
279 "intf_ids": techRange.IntfIds,
280 })
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700281 for _, RangePool := range techRange.Pools {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700282 // FIXME: Remove hardcoding
Girish Gowdru0c588b22019-04-23 23:24:56 -0400283 if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID {
284 ONUIDStart = RangePool.Start
285 ONUIDEnd = RangePool.End
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700286 ONUIDShared = uint32(RangePool.Sharing)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400287 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID {
288 AllocIDStart = RangePool.Start
289 AllocIDEnd = RangePool.End
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700290 AllocIDShared = uint32(RangePool.Sharing)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400291 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID {
292 GEMPortIDStart = RangePool.Start
293 GEMPortIDEnd = RangePool.End
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700294 GEMPortIDShared = uint32(RangePool.Sharing)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400295 }
296 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530297
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700298 ponRMgr.InitDefaultPONResourceRanges(ctx, ONUIDStart, ONUIDEnd, ONUIDShared,
299 AllocIDStart, AllocIDEnd, AllocIDShared,
300 GEMPortIDStart, GEMPortIDEnd, GEMPortIDShared,
301 flowIDDummyStart, flowIDDummyEnd, FlowIDShared, uniIDDummyStart, uniIDDummyEnd,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700302 devInfo.PonPorts, techRange.IntfIds)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530303
Abhilash S.L7f17e402019-03-15 17:40:41 +0530304}
305
Devmalya Paul495b94a2019-08-27 19:42:00 -0400306// Delete clears used resources for the particular olt device being deleted
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700307func (rsrcMgr *OpenOltResourceMgr) Delete(ctx context.Context, intfID uint32) error {
308 if err := rsrcMgr.PonRsrMgr.ClearDeviceResourcePoolForIntf(ctx, intfID); err != nil {
309 logger.Debug(ctx, "Failed to clear device resource pool")
310 return err
Devmalya Paul495b94a2019-08-27 19:42:00 -0400311 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000312 logger.Debug(ctx, "Cleared device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400313 return nil
314}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530315
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700316// GetONUID returns the available onuID for the given pon-port
317func (rsrcMgr *OpenOltResourceMgr) GetONUID(ctx context.Context, PonIntfID uint32) (uint32, error) {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400318 // Get ONU id for a provided pon interface ID.
Girish Gowdra76a1b092021-07-28 10:07:04 -0700319 onuID, err := rsrcMgr.TechprofileRef.GetResourceID(ctx, PonIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400320 ponrmgr.ONU_ID, 1)
321 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000322 logger.Errorf(ctx, "Failed to get resource for interface %d for type %s",
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700323 PonIntfID, ponrmgr.ONU_ID)
cbabuabf02352019-10-15 13:14:56 +0200324 return 0, err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400325 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700326 if len(onuID) > 0 {
327 rsrcMgr.PonRsrMgr.InitResourceMap(ctx, fmt.Sprintf("%d,%d", PonIntfID, onuID[0]))
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700328 return onuID[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400329 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530330
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700331 return 0, err // return onuID 0 on error
Abhilash S.L8ee90712019-04-29 16:24:22 +0530332}
333
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700334// GetCurrentFlowIDsForOnu fetches flow ID from the resource manager
335// Note: For flows which trap from the NNI and not really associated with any particular
336// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700337func (rsrcMgr *OpenOltResourceMgr) GetCurrentFlowIDsForOnu(ctx context.Context, PonIntfID uint32, onuID int32, uniID int32) ([]uint64, error) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700338
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700339 subs := fmt.Sprintf("%d,%d,%d", PonIntfID, onuID, uniID)
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700340 path := fmt.Sprintf(FlowIDPath, subs)
341
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700342 // fetch from cache
343 rsrcMgr.flowIDsForOnuLock.RLock()
344 flowIDsForOnu, ok := rsrcMgr.flowIDsForOnu[path]
345 rsrcMgr.flowIDsForOnuLock.RUnlock()
346
347 if ok {
348 return flowIDsForOnu, nil
349 }
350
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700351 var data []uint64
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700352 value, err := rsrcMgr.KVStore.Get(ctx, path)
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700353 if err == nil {
354 if value != nil {
355 Val, _ := toByte(value.Value)
356 if err = json.Unmarshal(Val, &data); err != nil {
357 logger.Error(ctx, "Failed to unmarshal")
358 return nil, err
359 }
360 }
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700361 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700362 // update cache
363 rsrcMgr.flowIDsForOnuLock.Lock()
364 rsrcMgr.flowIDsForOnu[path] = data
365 rsrcMgr.flowIDsForOnuLock.Unlock()
366
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700367 return data, nil
Abhilash S.L8ee90712019-04-29 16:24:22 +0530368}
369
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700370// UpdateAllocIdsForOnu updates alloc ids in kv store for a given pon interface id, onu id and uni id
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700371func (rsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ctx context.Context, ponPort uint32, onuID uint32, uniID uint32, allocIDs []uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530372
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700373 intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
374 // update cache
375 rsrcMgr.allocIDsForOnuLock.Lock()
376 rsrcMgr.allocIDsForOnu[intfOnuIDuniID] = allocIDs
377 rsrcMgr.allocIDsForOnuLock.Unlock()
378
379 // Note: in case the write to DB fails there could be inconsistent data between cache and db.
380 // Although this is highly unlikely with DB retries in place, this is something we have to deal with in the next release
381 return rsrcMgr.PonRsrMgr.UpdateAllocIdsForOnu(ctx, intfOnuIDuniID,
382 allocIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530383}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700384
385// GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700386func (rsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(ctx context.Context, intfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700387 uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530388
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700389 intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530390
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700391 // fetch from cache
392 rsrcMgr.gemPortIDsForOnuLock.RLock()
393 gemIDs, ok := rsrcMgr.gemPortIDsForOnu[intfOnuIDuniID]
394 rsrcMgr.gemPortIDsForOnuLock.RUnlock()
395 if ok {
396 return gemIDs
397 }
398 /* Get gem ports for given pon interface , onu id and uni id. */
399 gemIDs = rsrcMgr.PonRsrMgr.GetCurrentGEMPortIDsForOnu(ctx, intfOnuIDuniID)
400
401 // update cache
402 rsrcMgr.gemPortIDsForOnuLock.Lock()
403 rsrcMgr.gemPortIDsForOnu[intfOnuIDuniID] = gemIDs
404 rsrcMgr.gemPortIDsForOnuLock.Unlock()
405
406 return gemIDs
Abhilash S.L7f17e402019-03-15 17:40:41 +0530407}
408
Gamze Abakafee36392019-10-03 11:17:24 +0000409// GetCurrentAllocIDsForOnu returns alloc ids for given pon interface and onu id
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700410func (rsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDsForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530411
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700412 intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
413 // fetch from cache
414 rsrcMgr.allocIDsForOnuLock.RLock()
415 allocIDs, ok := rsrcMgr.allocIDsForOnu[intfOnuIDuniID]
416 rsrcMgr.allocIDsForOnuLock.RUnlock()
417 if ok {
418 return allocIDs
Girish Gowdru0c588b22019-04-23 23:24:56 -0400419 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700420 allocIDs = rsrcMgr.PonRsrMgr.GetCurrentAllocIDForOnu(ctx, intfOnuIDuniID)
421
422 // update cache
423 rsrcMgr.allocIDsForOnuLock.Lock()
424 rsrcMgr.allocIDsForOnu[intfOnuIDuniID] = allocIDs
425 rsrcMgr.allocIDsForOnuLock.Unlock()
426
427 return allocIDs
Gamze Abakafee36392019-10-03 11:17:24 +0000428}
429
430// RemoveAllocIDForOnu removes the alloc id for given pon interface, onu id, uni id and alloc id
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700431func (rsrcMgr *OpenOltResourceMgr) RemoveAllocIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, allocID uint32) {
432 allocIDs := rsrcMgr.GetCurrentAllocIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000433 for i := 0; i < len(allocIDs); i++ {
434 if allocIDs[i] == allocID {
435 allocIDs = append(allocIDs[:i], allocIDs[i+1:]...)
436 break
437 }
438 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700439 err := rsrcMgr.UpdateAllocIdsForOnu(ctx, intfID, onuID, uniID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000440 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700441 logger.Errorf(ctx, "Failed to Remove Alloc Id For Onu. intfID %d onuID %d uniID %d allocID %d",
Gamze Abakafee36392019-10-03 11:17:24 +0000442 intfID, onuID, uniID, allocID)
443 }
444}
445
446// RemoveGemPortIDForOnu removes the gem port id for given pon interface, onu id, uni id and gem port id
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700447func (rsrcMgr *OpenOltResourceMgr) RemoveGemPortIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, gemPortID uint32) {
448 gemPortIDs := rsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000449 for i := 0; i < len(gemPortIDs); i++ {
450 if gemPortIDs[i] == gemPortID {
451 gemPortIDs = append(gemPortIDs[:i], gemPortIDs[i+1:]...)
452 break
453 }
454 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700455 err := rsrcMgr.UpdateGEMPortIDsForOnu(ctx, intfID, onuID, uniID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000456 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700457 logger.Errorf(ctx, "Failed to Remove Gem Id For Onu. intfID %d onuID %d uniID %d gemPortId %d",
Gamze Abakafee36392019-10-03 11:17:24 +0000458 intfID, onuID, uniID, gemPortID)
459 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530460}
461
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700462// UpdateGEMPortIDsForOnu updates gemport ids on to the kv store for a given pon port, onu id and uni id
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700463func (rsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ctx context.Context, ponPort uint32, onuID uint32,
464 uniID uint32, gemIDs []uint32) error {
465 intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
466 // update cache
467 rsrcMgr.gemPortIDsForOnuLock.Lock()
468 rsrcMgr.gemPortIDsForOnu[intfOnuIDuniID] = gemIDs
469 rsrcMgr.gemPortIDsForOnuLock.Unlock()
470
471 // Note: in case the write to DB fails there could be inconsistent data between cache and db.
472 // Although this is highly unlikely with DB retries in place, this is something we have to deal with in the next release
473 return rsrcMgr.PonRsrMgr.UpdateGEMPortIDsForOnu(ctx, intfOnuIDuniID,
474 gemIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530475
476}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700477
478// FreeonuID releases(make free) onu id for a particular pon-port
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700479func (rsrcMgr *OpenOltResourceMgr) FreeonuID(ctx context.Context, intfID uint32, onuID []uint32) {
Girish Gowdrab8f1b5a2021-06-27 20:42:40 -0700480 if len(onuID) == 0 {
481 logger.Info(ctx, "onu id slice is nil, nothing to free")
482 return
483 }
Girish Gowdra76a1b092021-07-28 10:07:04 -0700484 if err := rsrcMgr.TechprofileRef.FreeResourceID(ctx, intfID, ponrmgr.ONU_ID, onuID); err != nil {
Matteo Scandolo84585372021-03-18 14:21:22 -0700485 logger.Errorw(ctx, "error-while-freeing-onu-id", log.Fields{
486 "intf-id": intfID,
487 "onu-id": onuID,
488 "err": err.Error(),
489 })
Girish Gowdrab8f1b5a2021-06-27 20:42:40 -0700490 } else {
491 logger.Infow(ctx, "freed onu id", log.Fields{"intfID": intfID, "onuID": onuID})
Matteo Scandolo84585372021-03-18 14:21:22 -0700492 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530493
Girish Gowdru0c588b22019-04-23 23:24:56 -0400494 /* Free onu id for a particular interface.*/
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700495 var IntfonuID string
496 for _, onu := range onuID {
497 IntfonuID = fmt.Sprintf("%d,%d", intfID, onu)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700498 rsrcMgr.PonRsrMgr.RemoveResourceMap(ctx, IntfonuID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400499 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530500}
501
Gamze Abakafee36392019-10-03 11:17:24 +0000502// FreeAllocID frees AllocID on the PON resource pool and also frees the allocID association
503// for the given OLT device.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700504// The caller should ensure that this is a blocking call and this operation is serialized for
505// the ONU so as not cause resource corruption since there are no mutexes used here.
506func (rsrcMgr *OpenOltResourceMgr) FreeAllocID(ctx context.Context, intfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000507 uniID uint32, allocID uint32) {
Girish Gowdrab77ded92020-04-08 11:45:05 -0700508
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700509 rsrcMgr.RemoveAllocIDForOnu(ctx, intfID, onuID, uniID, allocID)
Gamze Abakafee36392019-10-03 11:17:24 +0000510 allocIDs := make([]uint32, 0)
511 allocIDs = append(allocIDs, allocID)
Girish Gowdra76a1b092021-07-28 10:07:04 -0700512 if err := rsrcMgr.TechprofileRef.FreeResourceID(ctx, intfID, ponrmgr.ALLOC_ID, allocIDs); err != nil {
Matteo Scandolo84585372021-03-18 14:21:22 -0700513 logger.Errorw(ctx, "error-while-freeing-alloc-id", log.Fields{
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700514 "intf-id": intfID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700515 "onu-id": onuID,
516 "err": err.Error(),
517 })
518 }
Gamze Abakafee36392019-10-03 11:17:24 +0000519}
520
521// FreeGemPortID frees GemPortID on the PON resource pool and also frees the gemPortID association
522// for the given OLT device.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700523// The caller should ensure that this is a blocking call and this operation is serialized for
524// the ONU so as not cause resource corruption since there are no mutexes used here.
525func (rsrcMgr *OpenOltResourceMgr) FreeGemPortID(ctx context.Context, intfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000526 uniID uint32, gemPortID uint32) {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700527 rsrcMgr.RemoveGemPortIDForOnu(ctx, intfID, onuID, uniID, gemPortID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700528
Gamze Abakafee36392019-10-03 11:17:24 +0000529 gemPortIDs := make([]uint32, 0)
530 gemPortIDs = append(gemPortIDs, gemPortID)
Girish Gowdra76a1b092021-07-28 10:07:04 -0700531 if err := rsrcMgr.TechprofileRef.FreeResourceID(ctx, intfID, ponrmgr.GEMPORT_ID, gemPortIDs); err != nil {
Matteo Scandolo84585372021-03-18 14:21:22 -0700532 logger.Errorw(ctx, "error-while-freeing-gem-port-id", log.Fields{
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700533 "intf-id": intfID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700534 "onu-id": onuID,
535 "err": err.Error(),
536 })
537 }
Gamze Abakafee36392019-10-03 11:17:24 +0000538}
539
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700540// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id
541func (rsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530542
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700543 intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530544
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700545 AllocIDs := rsrcMgr.PonRsrMgr.GetCurrentAllocIDForOnu(ctx, intfOnuIDuniID)
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700546
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700547 rsrcMgr.allocIDsForOnuLock.Lock()
548 delete(rsrcMgr.allocIDsForOnu, intfOnuIDuniID)
549 rsrcMgr.allocIDsForOnuLock.Unlock()
550
Girish Gowdra76a1b092021-07-28 10:07:04 -0700551 if err := rsrcMgr.TechprofileRef.FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400552 ponrmgr.ALLOC_ID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700553 AllocIDs); err != nil {
554 logger.Errorw(ctx, "error-while-freeing-all-alloc-ids-for-onu", log.Fields{
555 "intf-id": intfID,
556 "onu-id": onuID,
557 "err": err.Error(),
558 })
559 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530560
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700561 GEMPortIDs := rsrcMgr.PonRsrMgr.GetCurrentGEMPortIDsForOnu(ctx, intfOnuIDuniID)
562
563 rsrcMgr.gemPortIDsForOnuLock.Lock()
564 delete(rsrcMgr.gemPortIDsForOnu, intfOnuIDuniID)
565 rsrcMgr.gemPortIDsForOnuLock.Unlock()
566
Girish Gowdra76a1b092021-07-28 10:07:04 -0700567 if err := rsrcMgr.TechprofileRef.FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400568 ponrmgr.GEMPORT_ID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700569 GEMPortIDs); err != nil {
570 logger.Errorw(ctx, "error-while-freeing-all-gem-port-ids-for-onu", log.Fields{
571 "intf-id": intfID,
572 "onu-id": onuID,
573 "err": err.Error(),
574 })
575 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530576
Girish Gowdru0c588b22019-04-23 23:24:56 -0400577 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700578 rsrcMgr.PonRsrMgr.RemoveResourceMap(ctx, intfOnuIDuniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530579}
580
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700581// IsFlowOnKvStore checks if the given flowID is present on the kv store
582// Returns true if the flowID is found, otherwise it returns false
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700583func (rsrcMgr *OpenOltResourceMgr) IsFlowOnKvStore(ctx context.Context, intfID uint32, onuID int32, uniID int32,
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700584 flowID uint64) bool {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530585
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700586 FlowIDs, err := rsrcMgr.GetCurrentFlowIDsForOnu(ctx, intfID, onuID, uniID)
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700587 if err != nil {
588 // error logged in the called function
589 return false
590 }
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400591 if FlowIDs != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700592 logger.Debugw(ctx, "Found flowId(s) for this ONU", log.Fields{"pon": intfID, "onuID": onuID, "uniID": uniID})
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700593 for _, id := range FlowIDs {
594 if flowID == id {
595 return true
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400596 }
597 }
598 }
599 return false
600}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400601
salmansiddiqui7ac62132019-08-22 03:58:50 +0000602// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700603// This path is formed as the following: {intfID, onuID, uniID}/tp_id
604func (rsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) []uint32 {
605 Path := fmt.Sprintf(tpIDPathSuffix, intfID, onuID, uniID)
606 // fetch from cache
607 rsrcMgr.techProfileIDsForOnuLock.RLock()
608 tpIDs, ok := rsrcMgr.techProfileIDsForOnu[Path]
609 rsrcMgr.techProfileIDsForOnuLock.RUnlock()
610 if ok {
611 return tpIDs
612 }
613 Value, err := rsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400614 if err == nil {
615 if Value != nil {
616 Val, err := kvstore.ToByte(Value.Value)
617 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700618 logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"err": err})
619 return tpIDs
Manikkaraj kb1d51442019-07-23 10:41:02 -0400620 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700621 if err = json.Unmarshal(Val, &tpIDs); err != nil {
622 logger.Error(ctx, "Failed to unmarshal", log.Fields{"err": err})
623 return tpIDs
Manikkaraj kb1d51442019-07-23 10:41:02 -0400624 }
625 }
626 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000627 logger.Errorf(ctx, "Failed to get TP id from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400628 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700629 logger.Debugf(ctx, "Getting TP id %d from path %s", tpIDs, Path)
630
631 // update cache
632 rsrcMgr.techProfileIDsForOnuLock.Lock()
633 rsrcMgr.techProfileIDsForOnu[Path] = tpIDs
634 rsrcMgr.techProfileIDsForOnuLock.Unlock()
635
636 return tpIDs
Manikkaraj kb1d51442019-07-23 10:41:02 -0400637
638}
639
Gamze Abakafee36392019-10-03 11:17:24 +0000640// RemoveTechProfileIDsForOnu deletes all tech profile ids from the KV-Store for the given onu based on the path
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700641// This path is formed as the following: {intfID, onuID, uniID}/tp_id
642func (rsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDsForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) error {
643 intfOnuUniID := fmt.Sprintf(tpIDPathSuffix, intfID, onuID, uniID)
644 // update cache
645 rsrcMgr.techProfileIDsForOnuLock.Lock()
646 delete(rsrcMgr.techProfileIDsForOnu, intfOnuUniID)
647 rsrcMgr.techProfileIDsForOnuLock.Unlock()
648
649 if err := rsrcMgr.KVStore.Delete(ctx, intfOnuUniID); err != nil {
650 logger.Errorw(ctx, "Failed to delete techprofile id resource in KV store", log.Fields{"path": intfOnuUniID})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400651 return err
652 }
653 return nil
654}
655
Gamze Abakafee36392019-10-03 11:17:24 +0000656// RemoveTechProfileIDForOnu deletes a specific tech profile id from the KV-Store for the given onu based on the path
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700657// This path is formed as the following: {intfID, onuID, uniID}/tp_id
658func (rsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, tpID uint32) error {
659 tpIDList := rsrcMgr.GetTechProfileIDForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000660 for i, tpIDInList := range tpIDList {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700661 if tpIDInList == tpID {
Gamze Abakafee36392019-10-03 11:17:24 +0000662 tpIDList = append(tpIDList[:i], tpIDList[i+1:]...)
663 }
664 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700665 intfOnuUniID := fmt.Sprintf(tpIDPathSuffix, intfID, onuID, uniID)
666 // update cache
667 rsrcMgr.techProfileIDsForOnuLock.Lock()
668 rsrcMgr.techProfileIDsForOnu[intfOnuUniID] = tpIDList
669 rsrcMgr.techProfileIDsForOnuLock.Unlock()
670
Gamze Abakafee36392019-10-03 11:17:24 +0000671 Value, err := json.Marshal(tpIDList)
672 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000673 logger.Error(ctx, "failed to Marshal")
Gamze Abakafee36392019-10-03 11:17:24 +0000674 return err
675 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700676 if err = rsrcMgr.KVStore.Put(ctx, intfOnuUniID, Value); err != nil {
677 logger.Errorf(ctx, "Failed to update resource %s", intfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000678 return err
679 }
680 return err
681}
682
683// UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700684// This path is formed as the following: {intfID, onuID, uniID}/tp_id
685func (rsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(ctx context.Context, intfID uint32, onuID uint32,
686 uniID uint32, tpID uint32) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400687 var Value []byte
688 var err error
689
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700690 intfOnuUniID := fmt.Sprintf(tpIDPathSuffix, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000691
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700692 tpIDList := rsrcMgr.GetTechProfileIDForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000693 for _, value := range tpIDList {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700694 if value == tpID {
695 logger.Debugf(ctx, "tpID %d is already in tpIdList for the path %s", tpID, intfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000696 return err
697 }
698 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700699 logger.Debugf(ctx, "updating tp id %d on path %s", tpID, intfOnuUniID)
700 tpIDList = append(tpIDList, tpID)
701
702 // update cache
703 rsrcMgr.techProfileIDsForOnuLock.Lock()
704 rsrcMgr.techProfileIDsForOnu[intfOnuUniID] = tpIDList
705 rsrcMgr.techProfileIDsForOnuLock.Unlock()
706
Gamze Abakafee36392019-10-03 11:17:24 +0000707 Value, err = json.Marshal(tpIDList)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400708 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000709 logger.Error(ctx, "failed to Marshal")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400710 return err
711 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700712 if err = rsrcMgr.KVStore.Put(ctx, intfOnuUniID, Value); err != nil {
713 logger.Errorf(ctx, "Failed to update resource %s", intfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400714 return err
715 }
716 return err
717}
718
Girish Gowdraa482f272021-03-24 23:04:19 -0700719// StoreMeterInfoForOnu updates the meter id in the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000720// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700721func (rsrcMgr *OpenOltResourceMgr) StoreMeterInfoForOnu(ctx context.Context, Direction string, intfID uint32, onuID uint32,
722 uniID uint32, tpID uint32, meterInfo *MeterInfo) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400723 var Value []byte
724 var err error
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700725 intfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, intfID, onuID, uniID, tpID, Direction)
726
727 // update cache
728 rsrcMgr.meterInfoForOnuLock.Lock()
729 rsrcMgr.meterInfoForOnu[intfOnuUniID] = meterInfo
730 rsrcMgr.meterInfoForOnuLock.Unlock()
731
Girish Gowdraa482f272021-03-24 23:04:19 -0700732 Value, err = json.Marshal(*meterInfo)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400733 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000734 logger.Error(ctx, "failed to Marshal meter config")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400735 return err
736 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700737 if err = rsrcMgr.KVStore.Put(ctx, intfOnuUniID, Value); err != nil {
738 logger.Errorf(ctx, "Failed to store meter into KV store %s", intfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400739 return err
740 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700741 logger.Debugw(ctx, "meter info updated successfully", log.Fields{"path": intfOnuUniID, "meter-info": meterInfo})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400742 return err
743}
744
Girish Gowdraa482f272021-03-24 23:04:19 -0700745// GetMeterInfoForOnu fetches the meter id from the kv store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000746// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700747func (rsrcMgr *OpenOltResourceMgr) GetMeterInfoForOnu(ctx context.Context, Direction string, intfID uint32, onuID uint32,
748 uniID uint32, tpID uint32) (*MeterInfo, error) {
749 Path := fmt.Sprintf(MeterIDPathSuffix, intfID, onuID, uniID, tpID, Direction)
750
751 // get from cache
752 rsrcMgr.meterInfoForOnuLock.RLock()
753 val, ok := rsrcMgr.meterInfoForOnu[Path]
754 rsrcMgr.meterInfoForOnuLock.RUnlock()
755 if ok {
756 return val, nil
757 }
758
Girish Gowdraa482f272021-03-24 23:04:19 -0700759 var meterInfo MeterInfo
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700760 Value, err := rsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400761 if err == nil {
762 if Value != nil {
Girish Gowdraa482f272021-03-24 23:04:19 -0700763 logger.Debug(ctx, "Found meter info in KV store", log.Fields{"Direction": Direction})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000764 Val, er := kvstore.ToByte(Value.Value)
765 if er != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700766 logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"err": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000767 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400768 }
Girish Gowdraa482f272021-03-24 23:04:19 -0700769 if er = json.Unmarshal(Val, &meterInfo); er != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700770 logger.Error(ctx, "Failed to unmarshal meter info", log.Fields{"err": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000771 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400772 }
773 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000774 logger.Debug(ctx, "meter-does-not-exists-in-KVStore")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400775 return nil, err
776 }
777 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000778 logger.Errorf(ctx, "Failed to get Meter config from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400779
780 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700781 // update cache
782 rsrcMgr.meterInfoForOnuLock.Lock()
783 rsrcMgr.meterInfoForOnu[Path] = &meterInfo
784 rsrcMgr.meterInfoForOnuLock.Unlock()
785
Girish Gowdraa482f272021-03-24 23:04:19 -0700786 return &meterInfo, err
Manikkaraj kb1d51442019-07-23 10:41:02 -0400787}
788
Girish Gowdraa482f272021-03-24 23:04:19 -0700789// HandleMeterInfoRefCntUpdate increments or decrements the reference counter for a given meter.
790// When reference count becomes 0, it clears the meter information from the kv store
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700791func (rsrcMgr *OpenOltResourceMgr) HandleMeterInfoRefCntUpdate(ctx context.Context, Direction string,
792 intfID uint32, onuID uint32, uniID uint32, tpID uint32, increment bool) error {
793 meterInfo, err := rsrcMgr.GetMeterInfoForOnu(ctx, Direction, intfID, onuID, uniID, tpID)
Girish Gowdra82c80982021-03-26 16:22:02 -0700794 if err != nil {
795 return err
796 } else if meterInfo == nil {
797 // If we are increasing the reference count, we expect the meter information to be present on KV store.
798 // But if decrementing the reference count, the meter is possibly already cleared from KV store. Just log warn but do not return error.
799 if increment {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700800 logger.Errorf(ctx, "error-fetching-meter-info-for-intf-%d-onu-%d-uni-%d-tp-id-%d-direction-%s", intfID, onuID, uniID, tpID, Direction)
801 return fmt.Errorf("error-fetching-meter-info-for-intf-%d-onu-%d-uni-%d-tp-id-%d-direction-%s", intfID, onuID, uniID, tpID, Direction)
Girish Gowdra82c80982021-03-26 16:22:02 -0700802 }
803 logger.Warnw(ctx, "meter is already cleared",
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700804 log.Fields{"intfID": intfID, "onuID": onuID, "uniID": uniID, "direction": Direction, "increment": increment})
Girish Gowdra82c80982021-03-26 16:22:02 -0700805 return nil
Girish Gowdraa482f272021-03-24 23:04:19 -0700806 }
Girish Gowdra82c80982021-03-26 16:22:02 -0700807
Girish Gowdraa482f272021-03-24 23:04:19 -0700808 if increment {
809 meterInfo.RefCnt++
810 } else {
811 meterInfo.RefCnt--
Girish Gowdra82c80982021-03-26 16:22:02 -0700812 // If RefCnt become 0 clear the meter information from the DB.
Girish Gowdraa482f272021-03-24 23:04:19 -0700813 if meterInfo.RefCnt == 0 {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700814 if err := rsrcMgr.RemoveMeterInfoForOnu(ctx, Direction, intfID, onuID, uniID, tpID); err != nil {
Girish Gowdraa482f272021-03-24 23:04:19 -0700815 return err
816 }
817 return nil
818 }
819 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700820 if err := rsrcMgr.StoreMeterInfoForOnu(ctx, Direction, intfID, onuID, uniID, tpID, meterInfo); err != nil {
Girish Gowdraa482f272021-03-24 23:04:19 -0700821 return err
822 }
823 return nil
824}
825
826// RemoveMeterInfoForOnu deletes the meter id from the kV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000827// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700828func (rsrcMgr *OpenOltResourceMgr) RemoveMeterInfoForOnu(ctx context.Context, Direction string, intfID uint32, onuID uint32,
829 uniID uint32, tpID uint32) error {
830 Path := fmt.Sprintf(MeterIDPathSuffix, intfID, onuID, uniID, tpID, Direction)
831
832 // update cache
833 rsrcMgr.meterInfoForOnuLock.Lock()
834 delete(rsrcMgr.meterInfoForOnu, Path)
835 rsrcMgr.meterInfoForOnuLock.Unlock()
836
837 if err := rsrcMgr.KVStore.Delete(ctx, Path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000838 logger.Errorf(ctx, "Failed to delete meter id %s from kvstore ", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400839 return err
840 }
841 return nil
842}
salmansiddiqui7ac62132019-08-22 03:58:50 +0000843
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700844//AddGemToOnuGemInfo adds gemport to onugem info kvstore and also local cache
845func (rsrcMgr *OpenOltResourceMgr) AddGemToOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, gemPort uint32) error {
846 onugem, err := rsrcMgr.GetOnuGemInfo(ctx, intfID, onuID)
847 if err != nil || onugem == nil || onugem.SerialNumber == "" {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000848 logger.Errorf(ctx, "failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530849 return err
850 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700851 if onugem.OnuID == onuID {
852 for _, gem := range onugem.GemPorts {
853 if gem == gemPort {
854 logger.Debugw(ctx, "Gem already present in onugem info, skpping addition", log.Fields{"gem": gem})
855 return nil
856 }
857 }
858 logger.Debugw(ctx, "Added gem to onugem info", log.Fields{"gem": gemPort})
859 onugem.GemPorts = append(onugem.GemPorts, gemPort)
860 } else {
861 logger.Errorw(ctx, "onu id in OnuGemInfo does not match", log.Fields{"onuID": onuID, "ponIf": intfID, "onuGemInfoOnuID": onugem.OnuID})
862 return fmt.Errorf("onu-id-in-OnuGemInfo-does-not-match-%v", onuID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530863 }
864
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700865 err = rsrcMgr.AddOnuGemInfo(ctx, intfID, onuID, *onugem)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530866 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000867 logger.Error(ctx, "Failed to add onugem to kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530868 return err
869 }
870 return err
871}
872
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700873//RemoveGemFromOnuGemInfo removes gemport from onugem info on kvstore and also local cache
874func (rsrcMgr *OpenOltResourceMgr) RemoveGemFromOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, gemPort uint32) error {
875 onugem, err := rsrcMgr.GetOnuGemInfo(ctx, intfID, onuID)
876 if err != nil || onugem == nil || onugem.SerialNumber == "" {
877 logger.Errorf(ctx, "failed to get onuifo for intfid %d", intfID)
878 return err
879 }
880 updated := false
881 if onugem.OnuID == onuID {
882 for i, gem := range onugem.GemPorts {
883 if gem == gemPort {
884 logger.Debugw(ctx, "Gem found, removing from onu gem info", log.Fields{"gem": gem})
885 onugem.GemPorts = append(onugem.GemPorts[:i], onugem.GemPorts[i+1:]...)
886 updated = true
887 break
888 }
889 }
890 } else {
891 logger.Errorw(ctx, "onu id in OnuGemInfo does not match", log.Fields{"onuID": onuID, "ponIf": intfID, "onuGemInfoOnuID": onugem.OnuID})
892 return fmt.Errorf("onu-id-in-OnuGemInfo-does-not-match-%v", onuID)
893 }
894 if updated {
895 err = rsrcMgr.AddOnuGemInfo(ctx, intfID, onuID, *onugem)
896 if err != nil {
897 logger.Error(ctx, "Failed to add onugem to kv store")
898 return err
899 }
900 } else {
901 logger.Debugw(ctx, "Gem port not found in onu gem info", log.Fields{"gem": gemPort})
902 }
903 return nil
904}
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530905
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700906//GetOnuGemInfo gets onu gem info from the kvstore per interface
907func (rsrcMgr *OpenOltResourceMgr) GetOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32) (*OnuGemInfo, error) {
908 var err error
909 var Val []byte
910 var onugem OnuGemInfo
911
912 path := fmt.Sprintf(OnuGemInfoPath, intfID, onuID)
913
914 rsrcMgr.onuGemInfoLock.RLock()
915 val, ok := rsrcMgr.onuGemInfo[path]
916 rsrcMgr.onuGemInfoLock.RUnlock()
917 if ok {
918 return val, nil
919 }
920 value, err := rsrcMgr.KVStore.Get(ctx, path)
921 if err != nil {
922 logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path})
923 return nil, err
924 } else if value == nil {
925 logger.Debug(ctx, "No onuinfo for path", log.Fields{"path": path})
926 return nil, nil // returning nil as this could happen if there are no onus for the interface yet
927 }
928 if Val, err = kvstore.ToByte(value.Value); err != nil {
929 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530930 return nil, err
931 }
932
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700933 if err = json.Unmarshal(Val, &onugem); err != nil {
934 logger.Error(ctx, "Failed to unmarshall")
935 return nil, err
936 }
937 logger.Debugw(ctx, "found onugem info from path", log.Fields{"path": path, "onuGemInfo": onugem})
938 rsrcMgr.onuGemInfoLock.Lock()
939 rsrcMgr.onuGemInfo[path] = &onugem
940 rsrcMgr.onuGemInfoLock.Unlock()
941
942 return &onugem, nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530943}
944
Chaitrashree G S1a55b882020-02-04 17:35:35 -0500945// AddOnuGemInfo adds onu info on to the kvstore per interface
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700946func (rsrcMgr *OpenOltResourceMgr) AddOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, onuGem OnuGemInfo) error {
947
948 var Value []byte
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530949 var err error
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700950 Path := fmt.Sprintf(OnuGemInfoPath, intfID, onuID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530951
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700952 rsrcMgr.onuGemInfoLock.Lock()
953 rsrcMgr.onuGemInfo[Path] = &onuGem
954 rsrcMgr.onuGemInfoLock.Unlock()
955
956 Value, err = json.Marshal(onuGem)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530957 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700958 logger.Error(ctx, "failed to Marshal")
959 return err
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530960 }
961
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700962 if err = rsrcMgr.KVStore.Put(ctx, Path, Value); err != nil {
963 logger.Errorf(ctx, "Failed to update resource %s", Path)
964 return err
965 }
Girish Gowdrabcf98af2021-07-01 08:24:42 -0700966 logger.Debugw(ctx, "added onu gem info to store", log.Fields{"onuGemInfo": onuGem})
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700967 return err
968}
969
970// DelOnuGemInfo deletes the onugem info from kvstore per ONU
971func (rsrcMgr *OpenOltResourceMgr) DelOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32) error {
972 path := fmt.Sprintf(OnuGemInfoPath, intfID, onuID)
973 rsrcMgr.onuGemInfoLock.Lock()
974 logger.Debugw(ctx, "removing onu gem info", log.Fields{"onuGemInfo": rsrcMgr.onuGemInfo[path]})
975 delete(rsrcMgr.onuGemInfo, path)
976 rsrcMgr.onuGemInfoLock.Unlock()
977
978 if err := rsrcMgr.KVStore.Delete(ctx, path); err != nil {
979 logger.Errorf(ctx, "failed to remove resource %s", path)
980 return err
981 }
Andrea Campanellab83b39d2020-03-30 11:41:16 +0200982 return nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530983}
984
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530985// AddUniPortToOnuInfo adds uni port to the onuinfo kvstore. check if the uni is already present if not update the kv store.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700986func (rsrcMgr *OpenOltResourceMgr) AddUniPortToOnuInfo(ctx context.Context, intfID uint32, onuID uint32, portNo uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530987
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700988 onugem, err := rsrcMgr.GetOnuGemInfo(ctx, intfID, onuID)
989 if err != nil || onugem == nil || onugem.SerialNumber == "" {
990 logger.Warnf(ctx, "failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530991 return
992 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700993
994 if onugem.OnuID == onuID {
995 for _, uni := range onugem.UniPorts {
996 if uni == portNo {
997 logger.Debugw(ctx, "uni already present in onugem info", log.Fields{"uni": portNo})
998 return
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530999 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301000 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001001 onugem.UniPorts = append(onugem.UniPorts, portNo)
1002 } else {
1003 logger.Warnw(ctx, "onu id mismatch in onu gem info", log.Fields{"intfID": intfID, "onuID": onuID})
1004 return
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301005 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001006 err = rsrcMgr.AddOnuGemInfo(ctx, intfID, onuID, *onugem)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301007 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001008 logger.Errorw(ctx, "Failed to add uni port in onugem to kv store", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301009 return
1010 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301011}
1012
Esin Karaman7fb80c22020-07-16 14:23:33 +00001013//UpdateGemPortForPktIn updates gemport for pkt in path to kvstore, path being intfid, onuid, portno, vlan id, priority bit
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001014func (rsrcMgr *OpenOltResourceMgr) UpdateGemPortForPktIn(ctx context.Context, pktIn PacketInInfoKey, gemPort uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301015
Esin Karaman7fb80c22020-07-16 14:23:33 +00001016 path := fmt.Sprintf(OnuPacketINPath, pktIn.IntfID, pktIn.OnuID, pktIn.LogicalPort, pktIn.VlanID, pktIn.Priority)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001017 // update cache
1018 rsrcMgr.gemPortForPacketInInfoLock.Lock()
1019 rsrcMgr.gemPortForPacketInInfo[path] = gemPort
1020 rsrcMgr.gemPortForPacketInInfoLock.Unlock()
1021
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301022 Value, err := json.Marshal(gemPort)
1023 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001024 logger.Error(ctx, "Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301025 return
1026 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001027 if err = rsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001028 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"path": path, "value": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301029 return
1030 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001031 logger.Debugw(ctx, "added gem packet in successfully", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301032}
1033
Esin Karaman7fb80c22020-07-16 14:23:33 +00001034// GetGemPortFromOnuPktIn gets the gem port from onu pkt in path, path being intfid, onuid, portno, vlan id, priority bit
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001035func (rsrcMgr *OpenOltResourceMgr) GetGemPortFromOnuPktIn(ctx context.Context, packetInInfoKey PacketInInfoKey) (uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301036
1037 var Val []byte
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301038
Esin Karaman7fb80c22020-07-16 14:23:33 +00001039 path := fmt.Sprintf(OnuPacketINPath, packetInInfoKey.IntfID, packetInInfoKey.OnuID, packetInInfoKey.LogicalPort,
1040 packetInInfoKey.VlanID, packetInInfoKey.Priority)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001041 // get from cache
1042 rsrcMgr.gemPortForPacketInInfoLock.RLock()
1043 gemPort, ok := rsrcMgr.gemPortForPacketInInfo[path]
1044 rsrcMgr.gemPortForPacketInInfoLock.RUnlock()
1045 if ok {
1046 logger.Debugw(ctx, "found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
1047 return gemPort, nil
1048 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301049
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001050 value, err := rsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301051 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001052 logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301053 return uint32(0), err
1054 } else if value == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001055 logger.Debugw(ctx, "No pkt in gem found", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301056 return uint32(0), nil
1057 }
1058
1059 if Val, err = kvstore.ToByte(value.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001060 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301061 return uint32(0), err
1062 }
1063 if err = json.Unmarshal(Val, &gemPort); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001064 logger.Error(ctx, "Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301065 return uint32(0), err
1066 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001067 logger.Debugw(ctx, "found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001068 // update cache
1069 rsrcMgr.gemPortForPacketInInfoLock.Lock()
1070 rsrcMgr.gemPortForPacketInInfo[path] = gemPort
1071 rsrcMgr.gemPortForPacketInInfoLock.Unlock()
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301072
1073 return gemPort, nil
1074}
1075
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001076//DeletePacketInGemPortForOnu deletes the packet-in gemport for ONU
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001077func (rsrcMgr *OpenOltResourceMgr) DeletePacketInGemPortForOnu(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301078
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001079 path := fmt.Sprintf(OnuPacketINPathPrefix, intfID, onuID, logicalPort)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001080
1081 value, err := rsrcMgr.KVStore.List(ctx, path)
Esin Karaman7fb80c22020-07-16 14:23:33 +00001082 if err != nil {
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001083 logger.Errorf(ctx, "failed-to-read-value-from-path-%s", path)
1084 return errors.New("failed-to-read-value-from-path-" + path)
Esin Karaman7fb80c22020-07-16 14:23:33 +00001085 }
Esin Karaman7fb80c22020-07-16 14:23:33 +00001086
1087 //remove them one by one
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001088 for key := range value {
1089 // Formulate the right key path suffix ti be delete
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001090 stringToBeReplaced := fmt.Sprintf(BasePathKvStore, rsrcMgr.KVStore.PathPrefix, rsrcMgr.DeviceID) + "/"
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001091 replacedWith := ""
1092 key = strings.Replace(key, stringToBeReplaced, replacedWith, 1)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001093 // update cache
1094 rsrcMgr.gemPortForPacketInInfoLock.Lock()
1095 delete(rsrcMgr.gemPortForPacketInInfo, key)
1096 rsrcMgr.gemPortForPacketInInfoLock.Unlock()
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001097
1098 logger.Debugf(ctx, "removing-key-%s", key)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001099 if err := rsrcMgr.KVStore.Delete(ctx, key); err != nil {
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001100 logger.Errorf(ctx, "failed-to-remove-resource-%s", key)
Esin Karaman7fb80c22020-07-16 14:23:33 +00001101 return err
1102 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301103 }
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001104
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301105 return nil
1106}
1107
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001108//GetFlowIDsForGem gets the list of FlowIDs for the given gemport
1109func (rsrcMgr *OpenOltResourceMgr) GetFlowIDsForGem(ctx context.Context, intf uint32, gem uint32) ([]uint64, error) {
1110 path := fmt.Sprintf(FlowIDsForGem, intf, gem)
1111
1112 // get from cache
1113 rsrcMgr.flowIDsForGemLock.RLock()
1114 flowIDs, ok := rsrcMgr.flowIDsForGem[gem]
1115 rsrcMgr.flowIDsForGemLock.RUnlock()
1116 if ok {
1117 return flowIDs, nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301118 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301119
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001120 value, err := rsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301121 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001122 logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path})
1123 return nil, err
1124 } else if value == nil {
1125 logger.Debug(ctx, "no flow-ids found", log.Fields{"path": path})
1126 return nil, nil
1127 }
1128 Val, err := kvstore.ToByte(value.Value)
1129 if err != nil {
1130 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301131 return nil, err
1132 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301133
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001134 if err = json.Unmarshal(Val, &flowIDs); err != nil {
1135 logger.Error(ctx, "Failed to unmarshall")
1136 return nil, err
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301137 }
1138
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001139 // update cache
1140 rsrcMgr.flowIDsForGemLock.Lock()
1141 rsrcMgr.flowIDsForGem[gem] = flowIDs
1142 rsrcMgr.flowIDsForGemLock.Unlock()
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301143
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001144 return flowIDs, nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301145}
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301146
1147//UpdateFlowIDsForGem updates flow id per gemport
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001148func (rsrcMgr *OpenOltResourceMgr) UpdateFlowIDsForGem(ctx context.Context, intf uint32, gem uint32, flowIDs []uint64) error {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301149 var val []byte
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001150 path := fmt.Sprintf(FlowIDsForGem, intf, gem)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301151
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001152 // update cache
1153 rsrcMgr.flowIDsForGemLock.Lock()
1154 rsrcMgr.flowIDsForGem[gem] = flowIDs
1155 rsrcMgr.flowIDsForGemLock.Unlock()
1156
1157 if flowIDs == nil {
1158 return nil
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301159 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001160 val, err := json.Marshal(flowIDs)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301161 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001162 logger.Error(ctx, "Failed to marshal data", log.Fields{"err": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301163 return err
1164 }
Girish Gowdrab77ded92020-04-08 11:45:05 -07001165
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001166 if err = rsrcMgr.KVStore.Put(ctx, path, val); err != nil {
1167 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"err": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301168 return err
1169 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001170 logger.Debugw(ctx, "added flowid list for gem to kv successfully", log.Fields{"path": path, "flowidlist": flowIDs})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301171 return nil
1172}
1173
1174//DeleteFlowIDsForGem deletes the flowID list entry per gem from kvstore.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001175func (rsrcMgr *OpenOltResourceMgr) DeleteFlowIDsForGem(ctx context.Context, intf uint32, gem uint32) {
1176 path := fmt.Sprintf(FlowIDsForGem, intf, gem)
1177 // update cache
1178 rsrcMgr.flowIDsForGemLock.Lock()
1179 delete(rsrcMgr.flowIDsForGem, gem)
1180 rsrcMgr.flowIDsForGemLock.Unlock()
1181 if err := rsrcMgr.KVStore.Delete(ctx, path); err != nil {
1182 logger.Errorw(ctx, "Failed to delete from kvstore", log.Fields{"err": err, "path": path})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301183 }
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301184}
Esin Karamanccb714b2019-11-29 15:02:06 +00001185
1186//GetMcastQueuePerInterfaceMap gets multicast queue info per pon interface
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001187func (rsrcMgr *OpenOltResourceMgr) GetMcastQueuePerInterfaceMap(ctx context.Context) (map[uint32][]uint32, error) {
Kent Hagermane6ff1012020-07-14 15:07:53 -04001188 path := McastQueuesForIntf
Esin Karamanccb714b2019-11-29 15:02:06 +00001189 var val []byte
1190
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001191 rsrcMgr.mcastQueueForIntfLock.RLock()
1192 if rsrcMgr.mcastQueueForIntfLoadedFromKvStore {
1193 rsrcMgr.mcastQueueForIntfLock.RUnlock()
1194 return rsrcMgr.mcastQueueForIntf, nil
1195 }
1196 rsrcMgr.mcastQueueForIntfLock.RUnlock()
1197
1198 kvPair, err := rsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001199 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001200 logger.Error(ctx, "failed to get data from kv store")
Esin Karamanccb714b2019-11-29 15:02:06 +00001201 return nil, err
1202 }
1203 if kvPair != nil && kvPair.Value != nil {
1204 if val, err = kvstore.ToByte(kvPair.Value); err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001205 logger.Error(ctx, "Failed to convert to byte array ", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001206 return nil, err
1207 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001208 rsrcMgr.mcastQueueForIntfLock.Lock()
1209 defer rsrcMgr.mcastQueueForIntfLock.Unlock()
1210 if err = json.Unmarshal(val, &rsrcMgr.mcastQueueForIntf); err != nil {
1211 logger.Error(ctx, "Failed to unmarshall ", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001212 return nil, err
1213 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001214 rsrcMgr.mcastQueueForIntfLoadedFromKvStore = true
Esin Karamanccb714b2019-11-29 15:02:06 +00001215 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001216 return rsrcMgr.mcastQueueForIntf, nil
Esin Karamanccb714b2019-11-29 15:02:06 +00001217}
1218
1219//AddMcastQueueForIntf adds multicast queue for pon interface
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001220func (rsrcMgr *OpenOltResourceMgr) AddMcastQueueForIntf(ctx context.Context, intf uint32, gem uint32, servicePriority uint32) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001221 var val []byte
Kent Hagermane6ff1012020-07-14 15:07:53 -04001222 path := McastQueuesForIntf
Esin Karamanccb714b2019-11-29 15:02:06 +00001223
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001224 // Load local cache from kv store the first time
1225 rsrcMgr.mcastQueueForIntfLock.RLock()
1226 if !rsrcMgr.mcastQueueForIntfLoadedFromKvStore {
1227 rsrcMgr.mcastQueueForIntfLock.RUnlock()
1228 _, err := rsrcMgr.GetMcastQueuePerInterfaceMap(ctx)
1229 if err != nil {
1230 logger.Errorw(ctx, "Failed to get multicast queue info for interface", log.Fields{"err": err, "intf": intf})
1231 return err
1232 }
1233 } else {
1234 rsrcMgr.mcastQueueForIntfLock.RUnlock()
1235 }
1236
1237 // Update KV store
1238 rsrcMgr.mcastQueueForIntfLock.Lock()
1239 rsrcMgr.mcastQueueForIntf[intf] = []uint32{gem, servicePriority}
1240 val, err := json.Marshal(rsrcMgr.mcastQueueForIntf)
Esin Karamanccb714b2019-11-29 15:02:06 +00001241 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001242 rsrcMgr.mcastQueueForIntfLock.Unlock()
1243 logger.Errorw(ctx, "Failed to marshal data", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001244 return err
1245 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001246 rsrcMgr.mcastQueueForIntfLock.Unlock()
1247
1248 if err = rsrcMgr.KVStore.Put(ctx, path, val); err != nil {
1249 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"err": err, "path": path, "value": val})
Esin Karamanccb714b2019-11-29 15:02:06 +00001250 return err
1251 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001252 logger.Debugw(ctx, "added multicast queue info to KV store successfully", log.Fields{"path": path, "interfaceId": intf, "gem": gem, "svcPrior": servicePriority})
Esin Karamanccb714b2019-11-29 15:02:06 +00001253 return nil
1254}
1255
1256//AddFlowGroupToKVStore adds flow group into KV store
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001257func (rsrcMgr *OpenOltResourceMgr) AddFlowGroupToKVStore(ctx context.Context, groupEntry *ofp.OfpGroupEntry, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001258 var Value []byte
1259 var err error
1260 var path string
1261 if cached {
1262 path = fmt.Sprintf(FlowGroupCached, groupEntry.Desc.GroupId)
1263 } else {
1264 path = fmt.Sprintf(FlowGroup, groupEntry.Desc.GroupId)
1265 }
1266 //build group info object
1267 var outPorts []uint32
1268 for _, ofBucket := range groupEntry.Desc.Buckets {
1269 for _, ofAction := range ofBucket.Actions {
1270 if ofAction.Type == ofp.OfpActionType_OFPAT_OUTPUT {
1271 outPorts = append(outPorts, ofAction.GetOutput().Port)
1272 }
1273 }
1274 }
1275 groupInfo := GroupInfo{
1276 GroupID: groupEntry.Desc.GroupId,
1277 OutPorts: outPorts,
1278 }
1279
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001280 rsrcMgr.groupInfoLock.Lock()
1281 rsrcMgr.groupInfo[path] = &groupInfo
1282 rsrcMgr.groupInfoLock.Unlock()
1283
Esin Karamanccb714b2019-11-29 15:02:06 +00001284 Value, err = json.Marshal(groupInfo)
1285
1286 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001287 logger.Error(ctx, "failed to Marshal flow group object")
Esin Karamanccb714b2019-11-29 15:02:06 +00001288 return err
1289 }
1290
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001291 if err = rsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001292 logger.Errorf(ctx, "Failed to update resource %s", path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001293 return err
1294 }
1295 return nil
1296}
1297
1298//RemoveFlowGroupFromKVStore removes flow group from KV store
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001299func (rsrcMgr *OpenOltResourceMgr) RemoveFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001300 var path string
1301 if cached {
1302 path = fmt.Sprintf(FlowGroupCached, groupID)
1303 } else {
1304 path = fmt.Sprintf(FlowGroup, groupID)
1305 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001306 rsrcMgr.groupInfoLock.Lock()
1307 delete(rsrcMgr.groupInfo, path)
1308 rsrcMgr.groupInfoLock.Unlock()
1309
1310 if err := rsrcMgr.KVStore.Delete(ctx, path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001311 logger.Errorf(ctx, "Failed to remove resource %s due to %s", path, err)
Esin Karamand519bbf2020-07-01 11:16:03 +00001312 return err
Esin Karamanccb714b2019-11-29 15:02:06 +00001313 }
Esin Karamand519bbf2020-07-01 11:16:03 +00001314 return nil
Esin Karamanccb714b2019-11-29 15:02:06 +00001315}
1316
1317//GetFlowGroupFromKVStore fetches flow group from the KV store. Returns (false, {} error) if any problem occurs during
1318//fetching the data. Returns (true, groupInfo, nil) if the group is fetched successfully.
1319// Returns (false, {}, nil) if the group does not exists in the KV store.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001320func (rsrcMgr *OpenOltResourceMgr) GetFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) (bool, GroupInfo, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001321 var groupInfo GroupInfo
1322 var path string
1323 if cached {
1324 path = fmt.Sprintf(FlowGroupCached, groupID)
1325 } else {
1326 path = fmt.Sprintf(FlowGroup, groupID)
1327 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001328
1329 // read from cache
1330 rsrcMgr.groupInfoLock.RLock()
1331 gi, ok := rsrcMgr.groupInfo[path]
1332 rsrcMgr.groupInfoLock.RUnlock()
1333 if ok {
1334 return true, *gi, nil
1335 }
1336
1337 kvPair, err := rsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001338 if err != nil {
1339 return false, groupInfo, err
1340 }
1341 if kvPair != nil && kvPair.Value != nil {
1342 Val, err := kvstore.ToByte(kvPair.Value)
1343 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001344 logger.Errorw(ctx, "Failed to convert flow group into byte array", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001345 return false, groupInfo, err
1346 }
1347 if err = json.Unmarshal(Val, &groupInfo); err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001348 logger.Errorw(ctx, "Failed to unmarshal", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001349 return false, groupInfo, err
1350 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001351 // update cache
1352 rsrcMgr.groupInfoLock.Lock()
1353 rsrcMgr.groupInfo[path] = &groupInfo
1354 rsrcMgr.groupInfoLock.Unlock()
1355
Esin Karamanccb714b2019-11-29 15:02:06 +00001356 return true, groupInfo, nil
1357 }
1358 return false, groupInfo, nil
1359}
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001360
1361// toByte converts an interface value to a []byte. The interface should either be of
1362// a string type or []byte. Otherwise, an error is returned.
1363func toByte(value interface{}) ([]byte, error) {
1364 switch t := value.(type) {
1365 case []byte:
1366 return value.([]byte), nil
1367 case string:
1368 return []byte(value.(string)), nil
1369 default:
1370 return nil, fmt.Errorf("unexpected-type-%T", t)
1371 }
1372}