blob: 3726ea5a02ac64713c17915371f24c2e0ccf6070 [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 Gowdraa09aeab2020-09-14 16:30:52 -070025 "strings"
Girish Gowdra38d533d2020-03-30 20:38:51 -070026 "sync"
Neha Sharmacc656962020-04-14 14:26:11 +000027 "time"
Abhilash S.L7f17e402019-03-15 17:40:41 +053028
Girish Gowdra4c3d4602021-07-22 16:33:37 -070029 "github.com/opencord/voltha-lib-go/v6/pkg/db"
30 "github.com/opencord/voltha-lib-go/v6/pkg/db/kvstore"
31 "github.com/opencord/voltha-lib-go/v6/pkg/log"
32 ponrmgr "github.com/opencord/voltha-lib-go/v6/pkg/ponresourcemanager"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070033 ofp "github.com/opencord/voltha-protos/v4/go/openflow_13"
34 "github.com/opencord/voltha-protos/v4/go/openolt"
Abhilash S.L7f17e402019-03-15 17:40:41 +053035)
36
salmansiddiqui7ac62132019-08-22 03:58:50 +000037const (
38 // KvstoreTimeout specifies the time out for KV Store Connection
Neha Sharmacc656962020-04-14 14:26:11 +000039 KvstoreTimeout = 5 * time.Second
Matteo Scandolodfa7a972020-11-06 13:03:40 -080040 // BasePathKvStore - <pathPrefix>/openolt/<device_id>
41 BasePathKvStore = "%s/openolt/{%s}"
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -070042 // tpIDPathSuffix - <(pon_id, onu_id, uni_id)>/tp_id
43 tpIDPathSuffix = "{%d,%d,%d}/tp_id"
Gamze Abakafee36392019-10-03 11:17:24 +000044 //MeterIDPathSuffix - <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
45 MeterIDPathSuffix = "{%d,%d,%d}/{%d}/meter_id/{%s}"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070046 // OnuPacketINPathPrefix - path prefix where ONU packet-in vlanID/PCP is stored
47 //format: onu_packetin/{<intfid>,<onuid>,<logicalport>}
48 OnuPacketINPathPrefix = "onu_packetin/{%d,%d,%d}"
49 // OnuPacketINPath path on the kvstore to store packetin gemport,which will be used for packetin, packetout
50 //format: onu_packetin/{<intfid>,<onuid>,<logicalport>}/{<vlanId>,<priority>}
51 OnuPacketINPath = OnuPacketINPathPrefix + "/{%d,%d}"
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -070052 //FlowIDsForGem flowids_per_gem/<intfid>/<gemport-id>
53 FlowIDsForGem = "flowids_per_gem/{%d}/{%d}"
Esin Karamanccb714b2019-11-29 15:02:06 +000054 //McastQueuesForIntf multicast queues for pon interfaces
55 McastQueuesForIntf = "mcast_qs_for_int"
56 //FlowGroup flow_groups/<flow_group_id>
57 // A group is stored under this path on the KV store after it has been installed to the device.
58 // It should also be deleted after it has been removed from the device accordingly.
59 FlowGroup = "flow_groups/{%d}"
60 //FlowGroupCached flow_groups_cached/<flow_group_id>
61 // When a group add request received, we create the group without setting any members to it since we cannot
62 // set any members to a group until it is associated with a multicast flow. It is a BAL limitation.
63 // When the related multicast flow has been created we perform set members operation for the group.
64 // That is why we need to keep the members of a group until the multicast flow creation request comes.
65 // We preserve the groups under "FlowGroupsCached" directory in the KV store temporarily. Having set members,
66 // we remove the group from the cached group store.
67 FlowGroupCached = "flow_groups_cached/{%d}"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070068
69 //FlowIDPath - Path on the KV store for storing list of Flow IDs for a given subscriber
70 //Format: BasePathKvStore/<(pon_intf_id, onu_id, uni_id)>/flow_ids
71 FlowIDPath = "{%s}/flow_ids"
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -070072
73 //OnuGemInfoPath is path on the kvstore to store onugem info map
74 //format: <device-id>/onu_gem_info/<intfid>
75 OnuGemInfoPath = "onu_gem_info/{%d}/{%d}" // onu_gem/<intfid>/<onuID>
salmansiddiqui7ac62132019-08-22 03:58:50 +000076)
Abhilash S.L7f17e402019-03-15 17:40:41 +053077
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070078// FlowInfo holds the flow information
Abhilash S.L8ee90712019-04-29 16:24:22 +053079type FlowInfo struct {
Girish Gowdraa09aeab2020-09-14 16:30:52 -070080 Flow *openolt.Flow
81 IsSymmtricFlow bool
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053082}
83
84// OnuGemInfo holds onu information along with gem port list and uni port list
85type OnuGemInfo struct {
86 OnuID uint32
87 SerialNumber string
88 IntfID uint32
89 GemPorts []uint32
90 UniPorts []uint32
91}
92
93// PacketInInfoKey is the key for packet in gemport
94type PacketInInfoKey struct {
95 IntfID uint32
96 OnuID uint32
97 LogicalPort uint32
Esin Karaman7fb80c22020-07-16 14:23:33 +000098 VlanID uint16
99 Priority uint8
Abhilash S.L8ee90712019-04-29 16:24:22 +0530100}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700101
Esin Karamanccb714b2019-11-29 15:02:06 +0000102// GroupInfo holds group information
103type GroupInfo struct {
104 GroupID uint32
105 OutPorts []uint32
106}
107
Girish Gowdraa482f272021-03-24 23:04:19 -0700108// MeterInfo store meter information at path <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
109type MeterInfo struct {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700110 RefCnt uint8 // number of flow references for this meter. When RefCnt is 0, the MeterInfo should be deleted.
111 MeterID uint32
Girish Gowdraa482f272021-03-24 23:04:19 -0700112}
113
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700114// OpenOltResourceMgr holds resource related information as provided below for each field
Abhilash S.L7f17e402019-03-15 17:40:41 +0530115type OpenOltResourceMgr struct {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700116 PonIntfID uint32
Neha Sharma3f221ae2020-04-29 19:02:12 +0000117 DeviceID string // OLT device id
118 Address string // Host and port of the kv store to connect to
119 Args string // args
120 KVStore *db.Backend // backend kv store connection handle
121 DeviceType string
122 DevInfo *openolt.DeviceInfo // device information
Girish Gowdru0c588b22019-04-23 23:24:56 -0400123 // array of pon resource managers per interface technology
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700124 PonRsrMgr *ponrmgr.PONResourceManager
Girish Gowdra38d533d2020-03-30 20:38:51 -0700125
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700126 // Local maps used for write-through-cache - start
127 flowIDsForOnu map[string][]uint64
128 flowIDsForOnuLock sync.RWMutex
129
130 allocIDsForOnu map[string][]uint32
131 allocIDsForOnuLock sync.RWMutex
132
133 gemPortIDsForOnu map[string][]uint32
134 gemPortIDsForOnuLock sync.RWMutex
135
136 techProfileIDsForOnu map[string][]uint32
137 techProfileIDsForOnuLock sync.RWMutex
138
139 meterInfoForOnu map[string]*MeterInfo
140 meterInfoForOnuLock sync.RWMutex
141
142 onuGemInfo map[string]*OnuGemInfo
143 onuGemInfoLock sync.RWMutex
144
145 gemPortForPacketInInfo map[string]uint32
146 gemPortForPacketInInfoLock sync.RWMutex
147
148 flowIDsForGem map[uint32][]uint64
149 flowIDsForGemLock sync.RWMutex
150
151 mcastQueueForIntf map[uint32][]uint32
152 mcastQueueForIntfLock sync.RWMutex
153 mcastQueueForIntfLoadedFromKvStore bool
154
155 groupInfo map[string]*GroupInfo
156 groupInfoLock sync.RWMutex
157 // Local maps used for write-through-cache - end
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700158
159 OnuIDMgmtLock sync.RWMutex
160 GemPortIDMgmtLock sync.RWMutex
161 AllocIDMgmtLock sync.RWMutex
Abhilash S.L7f17e402019-03-15 17:40:41 +0530162}
163
Neha Sharma96b7bf22020-06-15 10:37:32 +0000164func newKVClient(ctx context.Context, storeType string, address string, timeout time.Duration) (kvstore.Client, error) {
165 logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400166 switch storeType {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400167 case "etcd":
Neha Sharma96b7bf22020-06-15 10:37:32 +0000168 return kvstore.NewEtcdClient(ctx, address, timeout, log.FatalLevel)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400169 }
170 return nil, errors.New("unsupported-kv-store")
Abhilash S.L7f17e402019-03-15 17:40:41 +0530171}
172
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700173// SetKVClient sets the KV client and return a kv backend
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800174func SetKVClient(ctx context.Context, backend string, addr string, DeviceID string, basePathKvStore string) *db.Backend {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400175 // TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
176 // issue between kv store and backend , core is not calling NewBackend directly
Neha Sharma96b7bf22020-06-15 10:37:32 +0000177 kvClient, err := newKVClient(ctx, backend, addr, KvstoreTimeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400178 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000179 logger.Fatalw(ctx, "Failed to init KV client\n", log.Fields{"err": err})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400180 return nil
181 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700182 // return db.NewBackend(ctx, backend, addr, KvstoreTimeout, fmt.Sprintf(BasePathKvStore, basePathKvStore, DeviceID))
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700183
sbarbaria8910ba2019-11-05 10:12:23 -0500184 kvbackend := &db.Backend{
Girish Gowdru0c588b22019-04-23 23:24:56 -0400185 Client: kvClient,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700186 StoreType: backend,
Neha Sharma3f221ae2020-04-29 19:02:12 +0000187 Address: addr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700188 Timeout: KvstoreTimeout,
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800189 PathPrefix: fmt.Sprintf(BasePathKvStore, basePathKvStore, DeviceID)}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530190
Girish Gowdru0c588b22019-04-23 23:24:56 -0400191 return kvbackend
Abhilash S.L7f17e402019-03-15 17:40:41 +0530192}
193
Gamze Abakafee36392019-10-03 11:17:24 +0000194// NewResourceMgr init a New resource manager instance which in turn instantiates pon resource manager
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700195// instances according to technology. Initializes the default resource ranges for all
196// the resources.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700197func 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 -0400198 var ResourceMgr OpenOltResourceMgr
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700199 logger.Debugf(ctx, "Init new resource manager , ponIf: %v, address: %s, device-id: %s", PonIntfID, KVStoreAddress, deviceID)
200 ResourceMgr.PonIntfID = PonIntfID
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700201 ResourceMgr.DeviceID = deviceID
Neha Sharma3f221ae2020-04-29 19:02:12 +0000202 ResourceMgr.Address = KVStoreAddress
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700203 ResourceMgr.DeviceType = deviceType
204 ResourceMgr.DevInfo = devInfo
Abhilash S.L7f17e402019-03-15 17:40:41 +0530205
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700206 Backend := kvStoreType
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800207 ResourceMgr.KVStore = SetKVClient(ctx, Backend, ResourceMgr.Address, deviceID, basePathKvStore)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400208 if ResourceMgr.KVStore == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000209 logger.Error(ctx, "Failed to setup KV store")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400210 }
Girish Gowdra38d533d2020-03-30 20:38:51 -0700211
Girish Gowdru0c588b22019-04-23 23:24:56 -0400212 // TODO self.args = registry('main').get_args()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530213
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700214 // Create a separate Resource Manager instance for each range. This assumes that
Girish Gowdru0c588b22019-04-23 23:24:56 -0400215 // each technology is represented by only a single range
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700216 for _, TechRange := range devInfo.Ranges {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700217 for _, intfID := range TechRange.IntfIds {
218 if intfID == PonIntfID {
219 technology := TechRange.Technology
220 logger.Debugf(ctx, "Device info technology %s, intf-id %v", technology, PonIntfID)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000221
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700222 rsrMgr, err := ponrmgr.NewPONResourceManager(ctx, technology, deviceType, deviceID,
223 Backend, ResourceMgr.Address, basePathKvStore)
224 if err != nil {
225 logger.Errorf(ctx, "Failed to create pon resource manager instance for technology %s", technology)
226 return nil
227 }
228 ResourceMgr.PonRsrMgr = rsrMgr
229 // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
230 InitializeDeviceResourceRangeAndPool(ctx, rsrMgr, TechRange, devInfo)
231 if err := ResourceMgr.PonRsrMgr.InitDeviceResourcePoolForIntf(ctx, intfID); err != nil {
232 logger.Fatal(ctx, "failed-to-initialize-device-resource-pool-intf-id-%v-device-id", ResourceMgr.PonIntfID, ResourceMgr.DeviceID)
233 return nil
234 }
235 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400236 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400237 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700238
239 ResourceMgr.InitLocalCache()
240
Neha Sharma96b7bf22020-06-15 10:37:32 +0000241 logger.Info(ctx, "Initialization of resource manager success!")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400242 return &ResourceMgr
Abhilash S.L7f17e402019-03-15 17:40:41 +0530243}
244
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700245//InitLocalCache initializes local maps used for write-through-cache
246func (rsrcMgr *OpenOltResourceMgr) InitLocalCache() {
247 rsrcMgr.flowIDsForOnu = make(map[string][]uint64)
248 rsrcMgr.allocIDsForOnu = make(map[string][]uint32)
249 rsrcMgr.gemPortIDsForOnu = make(map[string][]uint32)
250 rsrcMgr.techProfileIDsForOnu = make(map[string][]uint32)
251 rsrcMgr.meterInfoForOnu = make(map[string]*MeterInfo)
252 rsrcMgr.onuGemInfo = make(map[string]*OnuGemInfo)
253 rsrcMgr.gemPortForPacketInInfo = make(map[string]uint32)
254 rsrcMgr.flowIDsForGem = make(map[uint32][]uint64)
255 rsrcMgr.mcastQueueForIntf = make(map[uint32][]uint32)
256 rsrcMgr.groupInfo = make(map[string]*GroupInfo)
257}
258
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700259// InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply
260// device specific information. If KV doesn't exist
261// or is broader than the device, the device's information will
262// dictate the range limits
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700263func InitializeDeviceResourceRangeAndPool(ctx context.Context, ponRMgr *ponrmgr.PONResourceManager,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700264 techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700265 // var ONUIDShared, AllocIDShared, GEMPortIDShared openolt.DeviceInfo_DeviceResourceRanges_Pool_SharingType
266 var ONUIDStart, ONUIDEnd, AllocIDStart, AllocIDEnd, GEMPortIDStart, GEMPortIDEnd uint32
267 var ONUIDShared, AllocIDShared, GEMPortIDShared, FlowIDShared uint32
268
269 // The below variables are just dummy and needed to pass as arguments to InitDefaultPONResourceRanges function.
270 // The openolt adapter does not need flowIDs to be managed as it is managed on the OLT device
271 // The UNI IDs are dynamically generated by openonu adapter for every discovered UNI.
272 var flowIDDummyStart, flowIDDummyEnd uint32 = 1, 2
273 var uniIDDummyStart, uniIDDummyEnd uint32 = 0, 1
Abhilash S.L7f17e402019-03-15 17:40:41 +0530274
Girish Gowdru0c588b22019-04-23 23:24:56 -0400275 // init the resource range pool according to the sharing type
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700276 logger.Debugw(ctx, "Device info init", log.Fields{"technology": techRange.Technology,
277 "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd,
278 "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd,
279 "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd,
280 "intf_ids": techRange.IntfIds,
281 })
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700282 for _, RangePool := range techRange.Pools {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700283 // FIXME: Remove hardcoding
Girish Gowdru0c588b22019-04-23 23:24:56 -0400284 if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID {
285 ONUIDStart = RangePool.Start
286 ONUIDEnd = RangePool.End
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700287 ONUIDShared = uint32(RangePool.Sharing)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400288 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID {
289 AllocIDStart = RangePool.Start
290 AllocIDEnd = RangePool.End
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700291 AllocIDShared = uint32(RangePool.Sharing)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400292 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID {
293 GEMPortIDStart = RangePool.Start
294 GEMPortIDEnd = RangePool.End
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700295 GEMPortIDShared = uint32(RangePool.Sharing)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400296 }
297 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530298
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700299 ponRMgr.InitDefaultPONResourceRanges(ctx, ONUIDStart, ONUIDEnd, ONUIDShared,
300 AllocIDStart, AllocIDEnd, AllocIDShared,
301 GEMPortIDStart, GEMPortIDEnd, GEMPortIDShared,
302 flowIDDummyStart, flowIDDummyEnd, FlowIDShared, uniIDDummyStart, uniIDDummyEnd,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700303 devInfo.PonPorts, techRange.IntfIds)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530304
Abhilash S.L7f17e402019-03-15 17:40:41 +0530305}
306
Devmalya Paul495b94a2019-08-27 19:42:00 -0400307// Delete clears used resources for the particular olt device being deleted
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700308func (rsrcMgr *OpenOltResourceMgr) Delete(ctx context.Context, intfID uint32) error {
309 if err := rsrcMgr.PonRsrMgr.ClearDeviceResourcePoolForIntf(ctx, intfID); err != nil {
310 logger.Debug(ctx, "Failed to clear device resource pool")
311 return err
Devmalya Paul495b94a2019-08-27 19:42:00 -0400312 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000313 logger.Debug(ctx, "Cleared device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400314 return nil
315}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530316
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700317// GetONUID returns the available onuID for the given pon-port
318func (rsrcMgr *OpenOltResourceMgr) GetONUID(ctx context.Context, PonIntfID uint32) (uint32, error) {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400319 // Get ONU id for a provided pon interface ID.
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700320 rsrcMgr.OnuIDMgmtLock.Lock()
321 onuID, err := rsrcMgr.PonRsrMgr.GetResourceID(ctx, PonIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400322 ponrmgr.ONU_ID, 1)
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700323 rsrcMgr.OnuIDMgmtLock.Unlock()
Girish Gowdru0c588b22019-04-23 23:24:56 -0400324 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000325 logger.Errorf(ctx, "Failed to get resource for interface %d for type %s",
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700326 PonIntfID, ponrmgr.ONU_ID)
cbabuabf02352019-10-15 13:14:56 +0200327 return 0, err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400328 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700329 if len(onuID) > 0 {
330 rsrcMgr.PonRsrMgr.InitResourceMap(ctx, fmt.Sprintf("%d,%d", PonIntfID, onuID[0]))
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700331 return onuID[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400332 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530333
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700334 return 0, err // return onuID 0 on error
Abhilash S.L8ee90712019-04-29 16:24:22 +0530335}
336
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700337// GetCurrentFlowIDsForOnu fetches flow ID from the resource manager
338// Note: For flows which trap from the NNI and not really associated with any particular
339// 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 -0700340func (rsrcMgr *OpenOltResourceMgr) GetCurrentFlowIDsForOnu(ctx context.Context, PonIntfID uint32, onuID int32, uniID int32) ([]uint64, error) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700341
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700342 subs := fmt.Sprintf("%d,%d,%d", PonIntfID, onuID, uniID)
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700343 path := fmt.Sprintf(FlowIDPath, subs)
344
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700345 // fetch from cache
346 rsrcMgr.flowIDsForOnuLock.RLock()
347 flowIDsForOnu, ok := rsrcMgr.flowIDsForOnu[path]
348 rsrcMgr.flowIDsForOnuLock.RUnlock()
349
350 if ok {
351 return flowIDsForOnu, nil
352 }
353
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700354 var data []uint64
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700355 value, err := rsrcMgr.KVStore.Get(ctx, path)
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700356 if err == nil {
357 if value != nil {
358 Val, _ := toByte(value.Value)
359 if err = json.Unmarshal(Val, &data); err != nil {
360 logger.Error(ctx, "Failed to unmarshal")
361 return nil, err
362 }
363 }
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700364 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700365 // update cache
366 rsrcMgr.flowIDsForOnuLock.Lock()
367 rsrcMgr.flowIDsForOnu[path] = data
368 rsrcMgr.flowIDsForOnuLock.Unlock()
369
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700370 return data, nil
Abhilash S.L8ee90712019-04-29 16:24:22 +0530371}
372
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700373// 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 -0700374func (rsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ctx context.Context, ponPort uint32, onuID uint32, uniID uint32, allocIDs []uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530375
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700376 intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
377 // update cache
378 rsrcMgr.allocIDsForOnuLock.Lock()
379 rsrcMgr.allocIDsForOnu[intfOnuIDuniID] = allocIDs
380 rsrcMgr.allocIDsForOnuLock.Unlock()
381
382 // Note: in case the write to DB fails there could be inconsistent data between cache and db.
383 // Although this is highly unlikely with DB retries in place, this is something we have to deal with in the next release
384 return rsrcMgr.PonRsrMgr.UpdateAllocIdsForOnu(ctx, intfOnuIDuniID,
385 allocIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530386}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700387
388// GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700389func (rsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(ctx context.Context, intfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700390 uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530391
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700392 intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530393
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700394 // fetch from cache
395 rsrcMgr.gemPortIDsForOnuLock.RLock()
396 gemIDs, ok := rsrcMgr.gemPortIDsForOnu[intfOnuIDuniID]
397 rsrcMgr.gemPortIDsForOnuLock.RUnlock()
398 if ok {
399 return gemIDs
400 }
401 /* Get gem ports for given pon interface , onu id and uni id. */
402 gemIDs = rsrcMgr.PonRsrMgr.GetCurrentGEMPortIDsForOnu(ctx, intfOnuIDuniID)
403
404 // update cache
405 rsrcMgr.gemPortIDsForOnuLock.Lock()
406 rsrcMgr.gemPortIDsForOnu[intfOnuIDuniID] = gemIDs
407 rsrcMgr.gemPortIDsForOnuLock.Unlock()
408
409 return gemIDs
Abhilash S.L7f17e402019-03-15 17:40:41 +0530410}
411
Gamze Abakafee36392019-10-03 11:17:24 +0000412// GetCurrentAllocIDsForOnu returns alloc ids for given pon interface and onu id
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700413func (rsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDsForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530414
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700415 intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
416 // fetch from cache
417 rsrcMgr.allocIDsForOnuLock.RLock()
418 allocIDs, ok := rsrcMgr.allocIDsForOnu[intfOnuIDuniID]
419 rsrcMgr.allocIDsForOnuLock.RUnlock()
420 if ok {
421 return allocIDs
Girish Gowdru0c588b22019-04-23 23:24:56 -0400422 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700423 allocIDs = rsrcMgr.PonRsrMgr.GetCurrentAllocIDForOnu(ctx, intfOnuIDuniID)
424
425 // update cache
426 rsrcMgr.allocIDsForOnuLock.Lock()
427 rsrcMgr.allocIDsForOnu[intfOnuIDuniID] = allocIDs
428 rsrcMgr.allocIDsForOnuLock.Unlock()
429
430 return allocIDs
Gamze Abakafee36392019-10-03 11:17:24 +0000431}
432
433// RemoveAllocIDForOnu removes the alloc id for given pon interface, onu id, uni id and alloc id
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700434func (rsrcMgr *OpenOltResourceMgr) RemoveAllocIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, allocID uint32) {
435 allocIDs := rsrcMgr.GetCurrentAllocIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000436 for i := 0; i < len(allocIDs); i++ {
437 if allocIDs[i] == allocID {
438 allocIDs = append(allocIDs[:i], allocIDs[i+1:]...)
439 break
440 }
441 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700442 err := rsrcMgr.UpdateAllocIdsForOnu(ctx, intfID, onuID, uniID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000443 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700444 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 +0000445 intfID, onuID, uniID, allocID)
446 }
447}
448
449// 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 -0700450func (rsrcMgr *OpenOltResourceMgr) RemoveGemPortIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, gemPortID uint32) {
451 gemPortIDs := rsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000452 for i := 0; i < len(gemPortIDs); i++ {
453 if gemPortIDs[i] == gemPortID {
454 gemPortIDs = append(gemPortIDs[:i], gemPortIDs[i+1:]...)
455 break
456 }
457 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700458 err := rsrcMgr.UpdateGEMPortIDsForOnu(ctx, intfID, onuID, uniID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000459 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700460 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 +0000461 intfID, onuID, uniID, gemPortID)
462 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530463}
464
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700465// 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 -0700466func (rsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ctx context.Context, ponPort uint32, onuID uint32,
467 uniID uint32, gemIDs []uint32) error {
468 intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
469 // update cache
470 rsrcMgr.gemPortIDsForOnuLock.Lock()
471 rsrcMgr.gemPortIDsForOnu[intfOnuIDuniID] = gemIDs
472 rsrcMgr.gemPortIDsForOnuLock.Unlock()
473
474 // Note: in case the write to DB fails there could be inconsistent data between cache and db.
475 // Although this is highly unlikely with DB retries in place, this is something we have to deal with in the next release
476 return rsrcMgr.PonRsrMgr.UpdateGEMPortIDsForOnu(ctx, intfOnuIDuniID,
477 gemIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530478
479}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700480
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700481// FreeResourceID frees PON resource ID like ONU ID, Alloc ID or GEM Port ID
482func (rsrcMgr *OpenOltResourceMgr) FreeResourceID(ctx context.Context, intfID uint32, resourceType string, ReleaseContent []uint32) error {
483 logger.Debugw(ctx, "freeing-resource-id", log.Fields{
484 "intf-id": intfID,
485 "resource-type": resourceType,
486 "release-content": ReleaseContent,
487 })
488 var err error
489 switch resourceType {
490 case rsrcMgr.PonRsrMgr.GetResourceTypeAllocID():
491 rsrcMgr.AllocIDMgmtLock.Lock()
492 err = rsrcMgr.PonRsrMgr.FreeResourceID(ctx, intfID, resourceType, ReleaseContent)
493 rsrcMgr.AllocIDMgmtLock.Unlock()
494 case rsrcMgr.PonRsrMgr.GetResourceTypeGemPortID():
495 rsrcMgr.GemPortIDMgmtLock.Lock()
496 err = rsrcMgr.PonRsrMgr.FreeResourceID(ctx, intfID, resourceType, ReleaseContent)
497 rsrcMgr.GemPortIDMgmtLock.Unlock()
498 case rsrcMgr.PonRsrMgr.GetResourceTypeOnuID():
499 rsrcMgr.OnuIDMgmtLock.Lock()
500 err = rsrcMgr.PonRsrMgr.FreeResourceID(ctx, intfID, resourceType, ReleaseContent)
501 rsrcMgr.OnuIDMgmtLock.Unlock()
502 default:
503 return fmt.Errorf("resourceType %s not supported", resourceType)
504 }
505 if err != nil {
506 return err
507 }
508 return nil
509}
510
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700511// FreeonuID releases(make free) onu id for a particular pon-port
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700512func (rsrcMgr *OpenOltResourceMgr) FreeonuID(ctx context.Context, intfID uint32, onuID []uint32) {
Girish Gowdrab8f1b5a2021-06-27 20:42:40 -0700513 if len(onuID) == 0 {
514 logger.Info(ctx, "onu id slice is nil, nothing to free")
515 return
516 }
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700517 if err := rsrcMgr.FreeResourceID(ctx, intfID, ponrmgr.ONU_ID, onuID); err != nil {
Matteo Scandolo84585372021-03-18 14:21:22 -0700518 logger.Errorw(ctx, "error-while-freeing-onu-id", log.Fields{
519 "intf-id": intfID,
520 "onu-id": onuID,
521 "err": err.Error(),
522 })
Girish Gowdrab8f1b5a2021-06-27 20:42:40 -0700523 } else {
524 logger.Infow(ctx, "freed onu id", log.Fields{"intfID": intfID, "onuID": onuID})
Matteo Scandolo84585372021-03-18 14:21:22 -0700525 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530526
Girish Gowdru0c588b22019-04-23 23:24:56 -0400527 /* Free onu id for a particular interface.*/
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700528 var IntfonuID string
529 for _, onu := range onuID {
530 IntfonuID = fmt.Sprintf("%d,%d", intfID, onu)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700531 rsrcMgr.PonRsrMgr.RemoveResourceMap(ctx, IntfonuID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400532 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530533}
534
Gamze Abakafee36392019-10-03 11:17:24 +0000535// FreeAllocID frees AllocID on the PON resource pool and also frees the allocID association
536// for the given OLT device.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700537// The caller should ensure that this is a blocking call and this operation is serialized for
538// the ONU so as not cause resource corruption since there are no mutexes used here.
539func (rsrcMgr *OpenOltResourceMgr) FreeAllocID(ctx context.Context, intfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000540 uniID uint32, allocID uint32) {
Girish Gowdrab77ded92020-04-08 11:45:05 -0700541
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700542 rsrcMgr.RemoveAllocIDForOnu(ctx, intfID, onuID, uniID, allocID)
Gamze Abakafee36392019-10-03 11:17:24 +0000543 allocIDs := make([]uint32, 0)
544 allocIDs = append(allocIDs, allocID)
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700545 if err := rsrcMgr.FreeResourceID(ctx, intfID, ponrmgr.ALLOC_ID, allocIDs); err != nil {
Matteo Scandolo84585372021-03-18 14:21:22 -0700546 logger.Errorw(ctx, "error-while-freeing-alloc-id", log.Fields{
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700547 "intf-id": intfID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700548 "onu-id": onuID,
549 "err": err.Error(),
550 })
551 }
Gamze Abakafee36392019-10-03 11:17:24 +0000552}
553
554// FreeGemPortID frees GemPortID on the PON resource pool and also frees the gemPortID association
555// for the given OLT device.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700556// The caller should ensure that this is a blocking call and this operation is serialized for
557// the ONU so as not cause resource corruption since there are no mutexes used here.
558func (rsrcMgr *OpenOltResourceMgr) FreeGemPortID(ctx context.Context, intfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000559 uniID uint32, gemPortID uint32) {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700560 rsrcMgr.RemoveGemPortIDForOnu(ctx, intfID, onuID, uniID, gemPortID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700561
Gamze Abakafee36392019-10-03 11:17:24 +0000562 gemPortIDs := make([]uint32, 0)
563 gemPortIDs = append(gemPortIDs, gemPortID)
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700564 if err := rsrcMgr.FreeResourceID(ctx, intfID, ponrmgr.GEMPORT_ID, gemPortIDs); err != nil {
Matteo Scandolo84585372021-03-18 14:21:22 -0700565 logger.Errorw(ctx, "error-while-freeing-gem-port-id", log.Fields{
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700566 "intf-id": intfID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700567 "onu-id": onuID,
568 "err": err.Error(),
569 })
570 }
Gamze Abakafee36392019-10-03 11:17:24 +0000571}
572
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700573// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id
574func (rsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530575
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700576 intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530577
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700578 AllocIDs := rsrcMgr.PonRsrMgr.GetCurrentAllocIDForOnu(ctx, intfOnuIDuniID)
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700579
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700580 rsrcMgr.allocIDsForOnuLock.Lock()
581 delete(rsrcMgr.allocIDsForOnu, intfOnuIDuniID)
582 rsrcMgr.allocIDsForOnuLock.Unlock()
583
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700584 if err := rsrcMgr.FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400585 ponrmgr.ALLOC_ID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700586 AllocIDs); err != nil {
587 logger.Errorw(ctx, "error-while-freeing-all-alloc-ids-for-onu", log.Fields{
588 "intf-id": intfID,
589 "onu-id": onuID,
590 "err": err.Error(),
591 })
592 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530593
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700594 GEMPortIDs := rsrcMgr.PonRsrMgr.GetCurrentGEMPortIDsForOnu(ctx, intfOnuIDuniID)
595
596 rsrcMgr.gemPortIDsForOnuLock.Lock()
597 delete(rsrcMgr.gemPortIDsForOnu, intfOnuIDuniID)
598 rsrcMgr.gemPortIDsForOnuLock.Unlock()
599
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700600 if err := rsrcMgr.FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400601 ponrmgr.GEMPORT_ID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700602 GEMPortIDs); err != nil {
603 logger.Errorw(ctx, "error-while-freeing-all-gem-port-ids-for-onu", log.Fields{
604 "intf-id": intfID,
605 "onu-id": onuID,
606 "err": err.Error(),
607 })
608 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530609
Girish Gowdru0c588b22019-04-23 23:24:56 -0400610 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700611 rsrcMgr.PonRsrMgr.RemoveResourceMap(ctx, intfOnuIDuniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530612}
613
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700614// IsFlowOnKvStore checks if the given flowID is present on the kv store
615// Returns true if the flowID is found, otherwise it returns false
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700616func (rsrcMgr *OpenOltResourceMgr) IsFlowOnKvStore(ctx context.Context, intfID uint32, onuID int32, uniID int32,
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700617 flowID uint64) bool {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530618
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700619 FlowIDs, err := rsrcMgr.GetCurrentFlowIDsForOnu(ctx, intfID, onuID, uniID)
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700620 if err != nil {
621 // error logged in the called function
622 return false
623 }
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400624 if FlowIDs != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700625 logger.Debugw(ctx, "Found flowId(s) for this ONU", log.Fields{"pon": intfID, "onuID": onuID, "uniID": uniID})
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700626 for _, id := range FlowIDs {
627 if flowID == id {
628 return true
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400629 }
630 }
631 }
632 return false
633}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400634
salmansiddiqui7ac62132019-08-22 03:58:50 +0000635// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700636// This path is formed as the following: {intfID, onuID, uniID}/tp_id
637func (rsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) []uint32 {
638 Path := fmt.Sprintf(tpIDPathSuffix, intfID, onuID, uniID)
639 // fetch from cache
640 rsrcMgr.techProfileIDsForOnuLock.RLock()
641 tpIDs, ok := rsrcMgr.techProfileIDsForOnu[Path]
642 rsrcMgr.techProfileIDsForOnuLock.RUnlock()
643 if ok {
644 return tpIDs
645 }
646 Value, err := rsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400647 if err == nil {
648 if Value != nil {
649 Val, err := kvstore.ToByte(Value.Value)
650 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700651 logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"err": err})
652 return tpIDs
Manikkaraj kb1d51442019-07-23 10:41:02 -0400653 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700654 if err = json.Unmarshal(Val, &tpIDs); err != nil {
655 logger.Error(ctx, "Failed to unmarshal", log.Fields{"err": err})
656 return tpIDs
Manikkaraj kb1d51442019-07-23 10:41:02 -0400657 }
658 }
659 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000660 logger.Errorf(ctx, "Failed to get TP id from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400661 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700662 logger.Debugf(ctx, "Getting TP id %d from path %s", tpIDs, Path)
663
664 // update cache
665 rsrcMgr.techProfileIDsForOnuLock.Lock()
666 rsrcMgr.techProfileIDsForOnu[Path] = tpIDs
667 rsrcMgr.techProfileIDsForOnuLock.Unlock()
668
669 return tpIDs
Manikkaraj kb1d51442019-07-23 10:41:02 -0400670
671}
672
Gamze Abakafee36392019-10-03 11:17:24 +0000673// 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 -0700674// This path is formed as the following: {intfID, onuID, uniID}/tp_id
675func (rsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDsForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) error {
676 intfOnuUniID := fmt.Sprintf(tpIDPathSuffix, intfID, onuID, uniID)
677 // update cache
678 rsrcMgr.techProfileIDsForOnuLock.Lock()
679 delete(rsrcMgr.techProfileIDsForOnu, intfOnuUniID)
680 rsrcMgr.techProfileIDsForOnuLock.Unlock()
681
682 if err := rsrcMgr.KVStore.Delete(ctx, intfOnuUniID); err != nil {
683 logger.Errorw(ctx, "Failed to delete techprofile id resource in KV store", log.Fields{"path": intfOnuUniID})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400684 return err
685 }
686 return nil
687}
688
Gamze Abakafee36392019-10-03 11:17:24 +0000689// 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 -0700690// This path is formed as the following: {intfID, onuID, uniID}/tp_id
691func (rsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, tpID uint32) error {
692 tpIDList := rsrcMgr.GetTechProfileIDForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000693 for i, tpIDInList := range tpIDList {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700694 if tpIDInList == tpID {
Gamze Abakafee36392019-10-03 11:17:24 +0000695 tpIDList = append(tpIDList[:i], tpIDList[i+1:]...)
696 }
697 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700698 intfOnuUniID := fmt.Sprintf(tpIDPathSuffix, intfID, onuID, uniID)
699 // update cache
700 rsrcMgr.techProfileIDsForOnuLock.Lock()
701 rsrcMgr.techProfileIDsForOnu[intfOnuUniID] = tpIDList
702 rsrcMgr.techProfileIDsForOnuLock.Unlock()
703
Gamze Abakafee36392019-10-03 11:17:24 +0000704 Value, err := json.Marshal(tpIDList)
705 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000706 logger.Error(ctx, "failed to Marshal")
Gamze Abakafee36392019-10-03 11:17:24 +0000707 return err
708 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700709 if err = rsrcMgr.KVStore.Put(ctx, intfOnuUniID, Value); err != nil {
710 logger.Errorf(ctx, "Failed to update resource %s", intfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000711 return err
712 }
713 return err
714}
715
716// UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700717// This path is formed as the following: {intfID, onuID, uniID}/tp_id
718func (rsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(ctx context.Context, intfID uint32, onuID uint32,
719 uniID uint32, tpID uint32) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400720 var Value []byte
721 var err error
722
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700723 intfOnuUniID := fmt.Sprintf(tpIDPathSuffix, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000724
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700725 tpIDList := rsrcMgr.GetTechProfileIDForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000726 for _, value := range tpIDList {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700727 if value == tpID {
728 logger.Debugf(ctx, "tpID %d is already in tpIdList for the path %s", tpID, intfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000729 return err
730 }
731 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700732 logger.Debugf(ctx, "updating tp id %d on path %s", tpID, intfOnuUniID)
733 tpIDList = append(tpIDList, tpID)
734
735 // update cache
736 rsrcMgr.techProfileIDsForOnuLock.Lock()
737 rsrcMgr.techProfileIDsForOnu[intfOnuUniID] = tpIDList
738 rsrcMgr.techProfileIDsForOnuLock.Unlock()
739
Gamze Abakafee36392019-10-03 11:17:24 +0000740 Value, err = json.Marshal(tpIDList)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400741 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000742 logger.Error(ctx, "failed to Marshal")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400743 return err
744 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700745 if err = rsrcMgr.KVStore.Put(ctx, intfOnuUniID, Value); err != nil {
746 logger.Errorf(ctx, "Failed to update resource %s", intfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400747 return err
748 }
749 return err
750}
751
Girish Gowdraa482f272021-03-24 23:04:19 -0700752// StoreMeterInfoForOnu updates the meter id in the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000753// 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 -0700754func (rsrcMgr *OpenOltResourceMgr) StoreMeterInfoForOnu(ctx context.Context, Direction string, intfID uint32, onuID uint32,
755 uniID uint32, tpID uint32, meterInfo *MeterInfo) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400756 var Value []byte
757 var err error
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700758 intfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, intfID, onuID, uniID, tpID, Direction)
759
760 // update cache
761 rsrcMgr.meterInfoForOnuLock.Lock()
762 rsrcMgr.meterInfoForOnu[intfOnuUniID] = meterInfo
763 rsrcMgr.meterInfoForOnuLock.Unlock()
764
Girish Gowdraa482f272021-03-24 23:04:19 -0700765 Value, err = json.Marshal(*meterInfo)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400766 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000767 logger.Error(ctx, "failed to Marshal meter config")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400768 return err
769 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700770 if err = rsrcMgr.KVStore.Put(ctx, intfOnuUniID, Value); err != nil {
771 logger.Errorf(ctx, "Failed to store meter into KV store %s", intfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400772 return err
773 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700774 logger.Debugw(ctx, "meter info updated successfully", log.Fields{"path": intfOnuUniID, "meter-info": meterInfo})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400775 return err
776}
777
Girish Gowdraa482f272021-03-24 23:04:19 -0700778// GetMeterInfoForOnu fetches the meter id from the kv store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000779// 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 -0700780func (rsrcMgr *OpenOltResourceMgr) GetMeterInfoForOnu(ctx context.Context, Direction string, intfID uint32, onuID uint32,
781 uniID uint32, tpID uint32) (*MeterInfo, error) {
782 Path := fmt.Sprintf(MeterIDPathSuffix, intfID, onuID, uniID, tpID, Direction)
783
784 // get from cache
785 rsrcMgr.meterInfoForOnuLock.RLock()
786 val, ok := rsrcMgr.meterInfoForOnu[Path]
787 rsrcMgr.meterInfoForOnuLock.RUnlock()
788 if ok {
789 return val, nil
790 }
791
Girish Gowdraa482f272021-03-24 23:04:19 -0700792 var meterInfo MeterInfo
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700793 Value, err := rsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400794 if err == nil {
795 if Value != nil {
Girish Gowdraa482f272021-03-24 23:04:19 -0700796 logger.Debug(ctx, "Found meter info in KV store", log.Fields{"Direction": Direction})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000797 Val, er := kvstore.ToByte(Value.Value)
798 if er != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700799 logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"err": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000800 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400801 }
Girish Gowdraa482f272021-03-24 23:04:19 -0700802 if er = json.Unmarshal(Val, &meterInfo); er != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700803 logger.Error(ctx, "Failed to unmarshal meter info", log.Fields{"err": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000804 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400805 }
806 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000807 logger.Debug(ctx, "meter-does-not-exists-in-KVStore")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400808 return nil, err
809 }
810 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000811 logger.Errorf(ctx, "Failed to get Meter config from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400812
813 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700814 // update cache
815 rsrcMgr.meterInfoForOnuLock.Lock()
816 rsrcMgr.meterInfoForOnu[Path] = &meterInfo
817 rsrcMgr.meterInfoForOnuLock.Unlock()
818
Girish Gowdraa482f272021-03-24 23:04:19 -0700819 return &meterInfo, err
Manikkaraj kb1d51442019-07-23 10:41:02 -0400820}
821
Girish Gowdraa482f272021-03-24 23:04:19 -0700822// HandleMeterInfoRefCntUpdate increments or decrements the reference counter for a given meter.
823// When reference count becomes 0, it clears the meter information from the kv store
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700824func (rsrcMgr *OpenOltResourceMgr) HandleMeterInfoRefCntUpdate(ctx context.Context, Direction string,
825 intfID uint32, onuID uint32, uniID uint32, tpID uint32, increment bool) error {
826 meterInfo, err := rsrcMgr.GetMeterInfoForOnu(ctx, Direction, intfID, onuID, uniID, tpID)
Girish Gowdra82c80982021-03-26 16:22:02 -0700827 if err != nil {
828 return err
829 } else if meterInfo == nil {
830 // If we are increasing the reference count, we expect the meter information to be present on KV store.
831 // But if decrementing the reference count, the meter is possibly already cleared from KV store. Just log warn but do not return error.
832 if increment {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700833 logger.Errorf(ctx, "error-fetching-meter-info-for-intf-%d-onu-%d-uni-%d-tp-id-%d-direction-%s", intfID, onuID, uniID, tpID, Direction)
834 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 -0700835 }
836 logger.Warnw(ctx, "meter is already cleared",
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700837 log.Fields{"intfID": intfID, "onuID": onuID, "uniID": uniID, "direction": Direction, "increment": increment})
Girish Gowdra82c80982021-03-26 16:22:02 -0700838 return nil
Girish Gowdraa482f272021-03-24 23:04:19 -0700839 }
Girish Gowdra82c80982021-03-26 16:22:02 -0700840
Girish Gowdraa482f272021-03-24 23:04:19 -0700841 if increment {
842 meterInfo.RefCnt++
843 } else {
844 meterInfo.RefCnt--
Girish Gowdra82c80982021-03-26 16:22:02 -0700845 // If RefCnt become 0 clear the meter information from the DB.
Girish Gowdraa482f272021-03-24 23:04:19 -0700846 if meterInfo.RefCnt == 0 {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700847 if err := rsrcMgr.RemoveMeterInfoForOnu(ctx, Direction, intfID, onuID, uniID, tpID); err != nil {
Girish Gowdraa482f272021-03-24 23:04:19 -0700848 return err
849 }
850 return nil
851 }
852 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700853 if err := rsrcMgr.StoreMeterInfoForOnu(ctx, Direction, intfID, onuID, uniID, tpID, meterInfo); err != nil {
Girish Gowdraa482f272021-03-24 23:04:19 -0700854 return err
855 }
856 return nil
857}
858
859// RemoveMeterInfoForOnu deletes the meter id from the kV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000860// 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 -0700861func (rsrcMgr *OpenOltResourceMgr) RemoveMeterInfoForOnu(ctx context.Context, Direction string, intfID uint32, onuID uint32,
862 uniID uint32, tpID uint32) error {
863 Path := fmt.Sprintf(MeterIDPathSuffix, intfID, onuID, uniID, tpID, Direction)
864
865 // update cache
866 rsrcMgr.meterInfoForOnuLock.Lock()
867 delete(rsrcMgr.meterInfoForOnu, Path)
868 rsrcMgr.meterInfoForOnuLock.Unlock()
869
870 if err := rsrcMgr.KVStore.Delete(ctx, Path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000871 logger.Errorf(ctx, "Failed to delete meter id %s from kvstore ", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400872 return err
873 }
874 return nil
875}
salmansiddiqui7ac62132019-08-22 03:58:50 +0000876
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700877//AddGemToOnuGemInfo adds gemport to onugem info kvstore and also local cache
878func (rsrcMgr *OpenOltResourceMgr) AddGemToOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, gemPort uint32) error {
879 onugem, err := rsrcMgr.GetOnuGemInfo(ctx, intfID, onuID)
880 if err != nil || onugem == nil || onugem.SerialNumber == "" {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000881 logger.Errorf(ctx, "failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530882 return err
883 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700884 if onugem.OnuID == onuID {
885 for _, gem := range onugem.GemPorts {
886 if gem == gemPort {
887 logger.Debugw(ctx, "Gem already present in onugem info, skpping addition", log.Fields{"gem": gem})
888 return nil
889 }
890 }
891 logger.Debugw(ctx, "Added gem to onugem info", log.Fields{"gem": gemPort})
892 onugem.GemPorts = append(onugem.GemPorts, gemPort)
893 } else {
894 logger.Errorw(ctx, "onu id in OnuGemInfo does not match", log.Fields{"onuID": onuID, "ponIf": intfID, "onuGemInfoOnuID": onugem.OnuID})
895 return fmt.Errorf("onu-id-in-OnuGemInfo-does-not-match-%v", onuID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530896 }
897
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700898 err = rsrcMgr.AddOnuGemInfo(ctx, intfID, onuID, *onugem)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530899 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000900 logger.Error(ctx, "Failed to add onugem to kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530901 return err
902 }
903 return err
904}
905
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700906//RemoveGemFromOnuGemInfo removes gemport from onugem info on kvstore and also local cache
907func (rsrcMgr *OpenOltResourceMgr) RemoveGemFromOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, gemPort uint32) error {
908 onugem, err := rsrcMgr.GetOnuGemInfo(ctx, intfID, onuID)
909 if err != nil || onugem == nil || onugem.SerialNumber == "" {
910 logger.Errorf(ctx, "failed to get onuifo for intfid %d", intfID)
911 return err
912 }
913 updated := false
914 if onugem.OnuID == onuID {
915 for i, gem := range onugem.GemPorts {
916 if gem == gemPort {
917 logger.Debugw(ctx, "Gem found, removing from onu gem info", log.Fields{"gem": gem})
918 onugem.GemPorts = append(onugem.GemPorts[:i], onugem.GemPorts[i+1:]...)
919 updated = true
920 break
921 }
922 }
923 } else {
924 logger.Errorw(ctx, "onu id in OnuGemInfo does not match", log.Fields{"onuID": onuID, "ponIf": intfID, "onuGemInfoOnuID": onugem.OnuID})
925 return fmt.Errorf("onu-id-in-OnuGemInfo-does-not-match-%v", onuID)
926 }
927 if updated {
928 err = rsrcMgr.AddOnuGemInfo(ctx, intfID, onuID, *onugem)
929 if err != nil {
930 logger.Error(ctx, "Failed to add onugem to kv store")
931 return err
932 }
933 } else {
934 logger.Debugw(ctx, "Gem port not found in onu gem info", log.Fields{"gem": gemPort})
935 }
936 return nil
937}
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530938
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700939//GetOnuGemInfo gets onu gem info from the kvstore per interface
940func (rsrcMgr *OpenOltResourceMgr) GetOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32) (*OnuGemInfo, error) {
941 var err error
942 var Val []byte
943 var onugem OnuGemInfo
944
945 path := fmt.Sprintf(OnuGemInfoPath, intfID, onuID)
946
947 rsrcMgr.onuGemInfoLock.RLock()
948 val, ok := rsrcMgr.onuGemInfo[path]
949 rsrcMgr.onuGemInfoLock.RUnlock()
950 if ok {
951 return val, nil
952 }
953 value, err := rsrcMgr.KVStore.Get(ctx, path)
954 if err != nil {
955 logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path})
956 return nil, err
957 } else if value == nil {
958 logger.Debug(ctx, "No onuinfo for path", log.Fields{"path": path})
959 return nil, nil // returning nil as this could happen if there are no onus for the interface yet
960 }
961 if Val, err = kvstore.ToByte(value.Value); err != nil {
962 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530963 return nil, err
964 }
965
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700966 if err = json.Unmarshal(Val, &onugem); err != nil {
967 logger.Error(ctx, "Failed to unmarshall")
968 return nil, err
969 }
970 logger.Debugw(ctx, "found onugem info from path", log.Fields{"path": path, "onuGemInfo": onugem})
971 rsrcMgr.onuGemInfoLock.Lock()
972 rsrcMgr.onuGemInfo[path] = &onugem
973 rsrcMgr.onuGemInfoLock.Unlock()
974
975 return &onugem, nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530976}
977
Chaitrashree G S1a55b882020-02-04 17:35:35 -0500978// AddOnuGemInfo adds onu info on to the kvstore per interface
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700979func (rsrcMgr *OpenOltResourceMgr) AddOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, onuGem OnuGemInfo) error {
980
981 var Value []byte
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530982 var err error
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700983 Path := fmt.Sprintf(OnuGemInfoPath, intfID, onuID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530984
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700985 rsrcMgr.onuGemInfoLock.Lock()
986 rsrcMgr.onuGemInfo[Path] = &onuGem
987 rsrcMgr.onuGemInfoLock.Unlock()
988
989 Value, err = json.Marshal(onuGem)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530990 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700991 logger.Error(ctx, "failed to Marshal")
992 return err
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530993 }
994
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700995 if err = rsrcMgr.KVStore.Put(ctx, Path, Value); err != nil {
996 logger.Errorf(ctx, "Failed to update resource %s", Path)
997 return err
998 }
Girish Gowdrabcf98af2021-07-01 08:24:42 -0700999 logger.Debugw(ctx, "added onu gem info to store", log.Fields{"onuGemInfo": onuGem})
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001000 return err
1001}
1002
1003// DelOnuGemInfo deletes the onugem info from kvstore per ONU
1004func (rsrcMgr *OpenOltResourceMgr) DelOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32) error {
1005 path := fmt.Sprintf(OnuGemInfoPath, intfID, onuID)
1006 rsrcMgr.onuGemInfoLock.Lock()
1007 logger.Debugw(ctx, "removing onu gem info", log.Fields{"onuGemInfo": rsrcMgr.onuGemInfo[path]})
1008 delete(rsrcMgr.onuGemInfo, path)
1009 rsrcMgr.onuGemInfoLock.Unlock()
1010
1011 if err := rsrcMgr.KVStore.Delete(ctx, path); err != nil {
1012 logger.Errorf(ctx, "failed to remove resource %s", path)
1013 return err
1014 }
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001015 return nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301016}
1017
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301018// 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 -07001019func (rsrcMgr *OpenOltResourceMgr) AddUniPortToOnuInfo(ctx context.Context, intfID uint32, onuID uint32, portNo uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301020
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001021 onugem, err := rsrcMgr.GetOnuGemInfo(ctx, intfID, onuID)
1022 if err != nil || onugem == nil || onugem.SerialNumber == "" {
1023 logger.Warnf(ctx, "failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301024 return
1025 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001026
1027 if onugem.OnuID == onuID {
1028 for _, uni := range onugem.UniPorts {
1029 if uni == portNo {
1030 logger.Debugw(ctx, "uni already present in onugem info", log.Fields{"uni": portNo})
1031 return
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301032 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301033 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001034 onugem.UniPorts = append(onugem.UniPorts, portNo)
1035 } else {
1036 logger.Warnw(ctx, "onu id mismatch in onu gem info", log.Fields{"intfID": intfID, "onuID": onuID})
1037 return
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301038 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001039 err = rsrcMgr.AddOnuGemInfo(ctx, intfID, onuID, *onugem)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301040 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001041 logger.Errorw(ctx, "Failed to add uni port in onugem to kv store", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301042 return
1043 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301044}
1045
Esin Karaman7fb80c22020-07-16 14:23:33 +00001046//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 -07001047func (rsrcMgr *OpenOltResourceMgr) UpdateGemPortForPktIn(ctx context.Context, pktIn PacketInInfoKey, gemPort uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301048
Esin Karaman7fb80c22020-07-16 14:23:33 +00001049 path := fmt.Sprintf(OnuPacketINPath, pktIn.IntfID, pktIn.OnuID, pktIn.LogicalPort, pktIn.VlanID, pktIn.Priority)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001050 // update cache
1051 rsrcMgr.gemPortForPacketInInfoLock.Lock()
1052 rsrcMgr.gemPortForPacketInInfo[path] = gemPort
1053 rsrcMgr.gemPortForPacketInInfoLock.Unlock()
1054
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301055 Value, err := json.Marshal(gemPort)
1056 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001057 logger.Error(ctx, "Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301058 return
1059 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001060 if err = rsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001061 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"path": path, "value": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301062 return
1063 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001064 logger.Debugw(ctx, "added gem packet in successfully", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301065}
1066
Esin Karaman7fb80c22020-07-16 14:23:33 +00001067// 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 -07001068func (rsrcMgr *OpenOltResourceMgr) GetGemPortFromOnuPktIn(ctx context.Context, packetInInfoKey PacketInInfoKey) (uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301069
1070 var Val []byte
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301071
Esin Karaman7fb80c22020-07-16 14:23:33 +00001072 path := fmt.Sprintf(OnuPacketINPath, packetInInfoKey.IntfID, packetInInfoKey.OnuID, packetInInfoKey.LogicalPort,
1073 packetInInfoKey.VlanID, packetInInfoKey.Priority)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001074 // get from cache
1075 rsrcMgr.gemPortForPacketInInfoLock.RLock()
1076 gemPort, ok := rsrcMgr.gemPortForPacketInInfo[path]
1077 rsrcMgr.gemPortForPacketInInfoLock.RUnlock()
1078 if ok {
1079 logger.Debugw(ctx, "found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
1080 return gemPort, nil
1081 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301082
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001083 value, err := rsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301084 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001085 logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301086 return uint32(0), err
1087 } else if value == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001088 logger.Debugw(ctx, "No pkt in gem found", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301089 return uint32(0), nil
1090 }
1091
1092 if Val, err = kvstore.ToByte(value.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001093 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301094 return uint32(0), err
1095 }
1096 if err = json.Unmarshal(Val, &gemPort); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001097 logger.Error(ctx, "Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301098 return uint32(0), err
1099 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001100 logger.Debugw(ctx, "found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001101 // update cache
1102 rsrcMgr.gemPortForPacketInInfoLock.Lock()
1103 rsrcMgr.gemPortForPacketInInfo[path] = gemPort
1104 rsrcMgr.gemPortForPacketInInfoLock.Unlock()
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301105
1106 return gemPort, nil
1107}
1108
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001109//DeletePacketInGemPortForOnu deletes the packet-in gemport for ONU
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001110func (rsrcMgr *OpenOltResourceMgr) DeletePacketInGemPortForOnu(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301111
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001112 path := fmt.Sprintf(OnuPacketINPathPrefix, intfID, onuID, logicalPort)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001113
1114 value, err := rsrcMgr.KVStore.List(ctx, path)
Esin Karaman7fb80c22020-07-16 14:23:33 +00001115 if err != nil {
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001116 logger.Errorf(ctx, "failed-to-read-value-from-path-%s", path)
1117 return errors.New("failed-to-read-value-from-path-" + path)
Esin Karaman7fb80c22020-07-16 14:23:33 +00001118 }
Esin Karaman7fb80c22020-07-16 14:23:33 +00001119
1120 //remove them one by one
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001121 for key := range value {
1122 // Formulate the right key path suffix ti be delete
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001123 stringToBeReplaced := fmt.Sprintf(BasePathKvStore, rsrcMgr.KVStore.PathPrefix, rsrcMgr.DeviceID) + "/"
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001124 replacedWith := ""
1125 key = strings.Replace(key, stringToBeReplaced, replacedWith, 1)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001126 // update cache
1127 rsrcMgr.gemPortForPacketInInfoLock.Lock()
1128 delete(rsrcMgr.gemPortForPacketInInfo, key)
1129 rsrcMgr.gemPortForPacketInInfoLock.Unlock()
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001130
1131 logger.Debugf(ctx, "removing-key-%s", key)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001132 if err := rsrcMgr.KVStore.Delete(ctx, key); err != nil {
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001133 logger.Errorf(ctx, "failed-to-remove-resource-%s", key)
Esin Karaman7fb80c22020-07-16 14:23:33 +00001134 return err
1135 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301136 }
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001137
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301138 return nil
1139}
1140
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001141//GetFlowIDsForGem gets the list of FlowIDs for the given gemport
1142func (rsrcMgr *OpenOltResourceMgr) GetFlowIDsForGem(ctx context.Context, intf uint32, gem uint32) ([]uint64, error) {
1143 path := fmt.Sprintf(FlowIDsForGem, intf, gem)
1144
1145 // get from cache
1146 rsrcMgr.flowIDsForGemLock.RLock()
1147 flowIDs, ok := rsrcMgr.flowIDsForGem[gem]
1148 rsrcMgr.flowIDsForGemLock.RUnlock()
1149 if ok {
1150 return flowIDs, nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301151 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301152
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001153 value, err := rsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301154 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001155 logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path})
1156 return nil, err
1157 } else if value == nil {
1158 logger.Debug(ctx, "no flow-ids found", log.Fields{"path": path})
1159 return nil, nil
1160 }
1161 Val, err := kvstore.ToByte(value.Value)
1162 if err != nil {
1163 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301164 return nil, err
1165 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301166
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001167 if err = json.Unmarshal(Val, &flowIDs); err != nil {
1168 logger.Error(ctx, "Failed to unmarshall")
1169 return nil, err
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301170 }
1171
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001172 // update cache
1173 rsrcMgr.flowIDsForGemLock.Lock()
1174 rsrcMgr.flowIDsForGem[gem] = flowIDs
1175 rsrcMgr.flowIDsForGemLock.Unlock()
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301176
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001177 return flowIDs, nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301178}
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301179
1180//UpdateFlowIDsForGem updates flow id per gemport
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001181func (rsrcMgr *OpenOltResourceMgr) UpdateFlowIDsForGem(ctx context.Context, intf uint32, gem uint32, flowIDs []uint64) error {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301182 var val []byte
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001183 path := fmt.Sprintf(FlowIDsForGem, intf, gem)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301184
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001185 // update cache
1186 rsrcMgr.flowIDsForGemLock.Lock()
1187 rsrcMgr.flowIDsForGem[gem] = flowIDs
1188 rsrcMgr.flowIDsForGemLock.Unlock()
1189
1190 if flowIDs == nil {
1191 return nil
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301192 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001193 val, err := json.Marshal(flowIDs)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301194 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001195 logger.Error(ctx, "Failed to marshal data", log.Fields{"err": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301196 return err
1197 }
Girish Gowdrab77ded92020-04-08 11:45:05 -07001198
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001199 if err = rsrcMgr.KVStore.Put(ctx, path, val); err != nil {
1200 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"err": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301201 return err
1202 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001203 logger.Debugw(ctx, "added flowid list for gem to kv successfully", log.Fields{"path": path, "flowidlist": flowIDs})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301204 return nil
1205}
1206
1207//DeleteFlowIDsForGem deletes the flowID list entry per gem from kvstore.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001208func (rsrcMgr *OpenOltResourceMgr) DeleteFlowIDsForGem(ctx context.Context, intf uint32, gem uint32) {
1209 path := fmt.Sprintf(FlowIDsForGem, intf, gem)
1210 // update cache
1211 rsrcMgr.flowIDsForGemLock.Lock()
1212 delete(rsrcMgr.flowIDsForGem, gem)
1213 rsrcMgr.flowIDsForGemLock.Unlock()
1214 if err := rsrcMgr.KVStore.Delete(ctx, path); err != nil {
1215 logger.Errorw(ctx, "Failed to delete from kvstore", log.Fields{"err": err, "path": path})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301216 }
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301217}
Esin Karamanccb714b2019-11-29 15:02:06 +00001218
1219//GetMcastQueuePerInterfaceMap gets multicast queue info per pon interface
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001220func (rsrcMgr *OpenOltResourceMgr) GetMcastQueuePerInterfaceMap(ctx context.Context) (map[uint32][]uint32, error) {
Kent Hagermane6ff1012020-07-14 15:07:53 -04001221 path := McastQueuesForIntf
Esin Karamanccb714b2019-11-29 15:02:06 +00001222 var val []byte
1223
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001224 rsrcMgr.mcastQueueForIntfLock.RLock()
1225 if rsrcMgr.mcastQueueForIntfLoadedFromKvStore {
1226 rsrcMgr.mcastQueueForIntfLock.RUnlock()
1227 return rsrcMgr.mcastQueueForIntf, nil
1228 }
1229 rsrcMgr.mcastQueueForIntfLock.RUnlock()
1230
1231 kvPair, err := rsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001232 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001233 logger.Error(ctx, "failed to get data from kv store")
Esin Karamanccb714b2019-11-29 15:02:06 +00001234 return nil, err
1235 }
1236 if kvPair != nil && kvPair.Value != nil {
1237 if val, err = kvstore.ToByte(kvPair.Value); err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001238 logger.Error(ctx, "Failed to convert to byte array ", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001239 return nil, err
1240 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001241 rsrcMgr.mcastQueueForIntfLock.Lock()
1242 defer rsrcMgr.mcastQueueForIntfLock.Unlock()
1243 if err = json.Unmarshal(val, &rsrcMgr.mcastQueueForIntf); err != nil {
1244 logger.Error(ctx, "Failed to unmarshall ", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001245 return nil, err
1246 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001247 rsrcMgr.mcastQueueForIntfLoadedFromKvStore = true
Esin Karamanccb714b2019-11-29 15:02:06 +00001248 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001249 return rsrcMgr.mcastQueueForIntf, nil
Esin Karamanccb714b2019-11-29 15:02:06 +00001250}
1251
1252//AddMcastQueueForIntf adds multicast queue for pon interface
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001253func (rsrcMgr *OpenOltResourceMgr) AddMcastQueueForIntf(ctx context.Context, intf uint32, gem uint32, servicePriority uint32) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001254 var val []byte
Kent Hagermane6ff1012020-07-14 15:07:53 -04001255 path := McastQueuesForIntf
Esin Karamanccb714b2019-11-29 15:02:06 +00001256
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001257 // Load local cache from kv store the first time
1258 rsrcMgr.mcastQueueForIntfLock.RLock()
1259 if !rsrcMgr.mcastQueueForIntfLoadedFromKvStore {
1260 rsrcMgr.mcastQueueForIntfLock.RUnlock()
1261 _, err := rsrcMgr.GetMcastQueuePerInterfaceMap(ctx)
1262 if err != nil {
1263 logger.Errorw(ctx, "Failed to get multicast queue info for interface", log.Fields{"err": err, "intf": intf})
1264 return err
1265 }
1266 } else {
1267 rsrcMgr.mcastQueueForIntfLock.RUnlock()
1268 }
1269
1270 // Update KV store
1271 rsrcMgr.mcastQueueForIntfLock.Lock()
1272 rsrcMgr.mcastQueueForIntf[intf] = []uint32{gem, servicePriority}
1273 val, err := json.Marshal(rsrcMgr.mcastQueueForIntf)
Esin Karamanccb714b2019-11-29 15:02:06 +00001274 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001275 rsrcMgr.mcastQueueForIntfLock.Unlock()
1276 logger.Errorw(ctx, "Failed to marshal data", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001277 return err
1278 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001279 rsrcMgr.mcastQueueForIntfLock.Unlock()
1280
1281 if err = rsrcMgr.KVStore.Put(ctx, path, val); err != nil {
1282 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"err": err, "path": path, "value": val})
Esin Karamanccb714b2019-11-29 15:02:06 +00001283 return err
1284 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001285 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 +00001286 return nil
1287}
1288
1289//AddFlowGroupToKVStore adds flow group into KV store
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001290func (rsrcMgr *OpenOltResourceMgr) AddFlowGroupToKVStore(ctx context.Context, groupEntry *ofp.OfpGroupEntry, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001291 var Value []byte
1292 var err error
1293 var path string
1294 if cached {
1295 path = fmt.Sprintf(FlowGroupCached, groupEntry.Desc.GroupId)
1296 } else {
1297 path = fmt.Sprintf(FlowGroup, groupEntry.Desc.GroupId)
1298 }
1299 //build group info object
1300 var outPorts []uint32
1301 for _, ofBucket := range groupEntry.Desc.Buckets {
1302 for _, ofAction := range ofBucket.Actions {
1303 if ofAction.Type == ofp.OfpActionType_OFPAT_OUTPUT {
1304 outPorts = append(outPorts, ofAction.GetOutput().Port)
1305 }
1306 }
1307 }
1308 groupInfo := GroupInfo{
1309 GroupID: groupEntry.Desc.GroupId,
1310 OutPorts: outPorts,
1311 }
1312
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001313 rsrcMgr.groupInfoLock.Lock()
1314 rsrcMgr.groupInfo[path] = &groupInfo
1315 rsrcMgr.groupInfoLock.Unlock()
1316
Esin Karamanccb714b2019-11-29 15:02:06 +00001317 Value, err = json.Marshal(groupInfo)
1318
1319 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001320 logger.Error(ctx, "failed to Marshal flow group object")
Esin Karamanccb714b2019-11-29 15:02:06 +00001321 return err
1322 }
1323
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001324 if err = rsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001325 logger.Errorf(ctx, "Failed to update resource %s", path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001326 return err
1327 }
1328 return nil
1329}
1330
1331//RemoveFlowGroupFromKVStore removes flow group from KV store
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001332func (rsrcMgr *OpenOltResourceMgr) RemoveFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001333 var path string
1334 if cached {
1335 path = fmt.Sprintf(FlowGroupCached, groupID)
1336 } else {
1337 path = fmt.Sprintf(FlowGroup, groupID)
1338 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001339 rsrcMgr.groupInfoLock.Lock()
1340 delete(rsrcMgr.groupInfo, path)
1341 rsrcMgr.groupInfoLock.Unlock()
1342
1343 if err := rsrcMgr.KVStore.Delete(ctx, path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001344 logger.Errorf(ctx, "Failed to remove resource %s due to %s", path, err)
Esin Karamand519bbf2020-07-01 11:16:03 +00001345 return err
Esin Karamanccb714b2019-11-29 15:02:06 +00001346 }
Esin Karamand519bbf2020-07-01 11:16:03 +00001347 return nil
Esin Karamanccb714b2019-11-29 15:02:06 +00001348}
1349
1350//GetFlowGroupFromKVStore fetches flow group from the KV store. Returns (false, {} error) if any problem occurs during
1351//fetching the data. Returns (true, groupInfo, nil) if the group is fetched successfully.
1352// Returns (false, {}, nil) if the group does not exists in the KV store.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001353func (rsrcMgr *OpenOltResourceMgr) GetFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) (bool, GroupInfo, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001354 var groupInfo GroupInfo
1355 var path string
1356 if cached {
1357 path = fmt.Sprintf(FlowGroupCached, groupID)
1358 } else {
1359 path = fmt.Sprintf(FlowGroup, groupID)
1360 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001361
1362 // read from cache
1363 rsrcMgr.groupInfoLock.RLock()
1364 gi, ok := rsrcMgr.groupInfo[path]
1365 rsrcMgr.groupInfoLock.RUnlock()
1366 if ok {
1367 return true, *gi, nil
1368 }
1369
1370 kvPair, err := rsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001371 if err != nil {
1372 return false, groupInfo, err
1373 }
1374 if kvPair != nil && kvPair.Value != nil {
1375 Val, err := kvstore.ToByte(kvPair.Value)
1376 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001377 logger.Errorw(ctx, "Failed to convert flow group into byte array", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001378 return false, groupInfo, err
1379 }
1380 if err = json.Unmarshal(Val, &groupInfo); err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001381 logger.Errorw(ctx, "Failed to unmarshal", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001382 return false, groupInfo, err
1383 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001384 // update cache
1385 rsrcMgr.groupInfoLock.Lock()
1386 rsrcMgr.groupInfo[path] = &groupInfo
1387 rsrcMgr.groupInfoLock.Unlock()
1388
Esin Karamanccb714b2019-11-29 15:02:06 +00001389 return true, groupInfo, nil
1390 }
1391 return false, groupInfo, nil
1392}
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001393
1394// toByte converts an interface value to a []byte. The interface should either be of
1395// a string type or []byte. Otherwise, an error is returned.
1396func toByte(value interface{}) ([]byte, error) {
1397 switch t := value.(type) {
1398 case []byte:
1399 return value.([]byte), nil
1400 case string:
1401 return []byte(value.(string)), nil
1402 default:
1403 return nil, fmt.Errorf("unexpected-type-%T", t)
1404 }
1405}