blob: bc0524d4f6ecf50a47337af3bb239d9404ceca01 [file] [log] [blame]
Abhilash S.L7f17e402019-03-15 17:40:41 +05301/*
Joey Armstrong11f5a572024-01-12 19:11:32 -05002 * Copyright 2019-2024 Open Networking Foundation (ONF) and the ONF Contributors
Abhilash S.L7f17e402019-03-15 17:40:41 +05303
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
Joey Armstrong3f0e2422023-07-05 18:25:41 -040017// 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
khenaidoo106c61a2021-08-11 18:05:46 -040029 tp "github.com/opencord/voltha-lib-go/v7/pkg/techprofile"
30
31 "github.com/opencord/voltha-lib-go/v7/pkg/db"
32 "github.com/opencord/voltha-lib-go/v7/pkg/db/kvstore"
33 "github.com/opencord/voltha-lib-go/v7/pkg/log"
34 ponrmgr "github.com/opencord/voltha-lib-go/v7/pkg/ponresourcemanager"
35 ofp "github.com/opencord/voltha-protos/v5/go/openflow_13"
36 "github.com/opencord/voltha-protos/v5/go/openolt"
Abhilash S.L7f17e402019-03-15 17:40:41 +053037)
38
salmansiddiqui7ac62132019-08-22 03:58:50 +000039const (
40 // KvstoreTimeout specifies the time out for KV Store Connection
Neha Sharmacc656962020-04-14 14:26:11 +000041 KvstoreTimeout = 5 * time.Second
Matteo Scandolodfa7a972020-11-06 13:03:40 -080042 // BasePathKvStore - <pathPrefix>/openolt/<device_id>
43 BasePathKvStore = "%s/openolt/{%s}"
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -070044 // tpIDPathSuffix - <(pon_id, onu_id, uni_id)>/tp_id
45 tpIDPathSuffix = "{%d,%d,%d}/tp_id"
Akash Kankanala041a2122024-10-16 15:49:22 +053046 // MeterIDPathSuffix - <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
Gamze Abakafee36392019-10-03 11:17:24 +000047 MeterIDPathSuffix = "{%d,%d,%d}/{%d}/meter_id/{%s}"
Girish Gowdra950326e2021-11-05 12:43:24 -070048
49 // OnuPacketInPathPrefix - path prefix where ONU packet-in vlanID/PCP is stored
Girish Gowdraa09aeab2020-09-14 16:30:52 -070050 //format: onu_packetin/{<intfid>,<onuid>,<logicalport>}
Girish Gowdra950326e2021-11-05 12:43:24 -070051 OnuPacketInPathPrefix = "onu_packetin/{%d,%d,%d}"
52 // OnuPacketInPath path on the kvstore to store packetin gemport,which will be used for packetin, packetout
Girish Gowdraa09aeab2020-09-14 16:30:52 -070053 //format: onu_packetin/{<intfid>,<onuid>,<logicalport>}/{<vlanId>,<priority>}
Girish Gowdra950326e2021-11-05 12:43:24 -070054 OnuPacketInPath = OnuPacketInPathPrefix + "/{%d,%d}"
55
Akash Kankanala041a2122024-10-16 15:49:22 +053056 // FlowIDsForGemPathPrefix format: flowids_for_gem/<intfid>
Girish Gowdra950326e2021-11-05 12:43:24 -070057 FlowIDsForGemPathPrefix = "flowids_per_gem/{%d}"
Akash Kankanala041a2122024-10-16 15:49:22 +053058 // FlowIDsForGem flowids_for_gem/<intfid>/<gemport-id>
Girish Gowdra950326e2021-11-05 12:43:24 -070059 FlowIDsForGem = FlowIDsForGemPathPrefix + "/{%d}"
60
Akash Kankanala041a2122024-10-16 15:49:22 +053061 // McastQueuesForIntf multicast queues for pon interfaces
Esin Karamanccb714b2019-11-29 15:02:06 +000062 McastQueuesForIntf = "mcast_qs_for_int"
Akash Kankanala041a2122024-10-16 15:49:22 +053063 // FlowGroup flow_groups/<flow_group_id>
Esin Karamanccb714b2019-11-29 15:02:06 +000064 // A group is stored under this path on the KV store after it has been installed to the device.
65 // It should also be deleted after it has been removed from the device accordingly.
66 FlowGroup = "flow_groups/{%d}"
Akash Kankanala041a2122024-10-16 15:49:22 +053067 // FlowGroupCached flow_groups_cached/<flow_group_id>
Esin Karamanccb714b2019-11-29 15:02:06 +000068 // When a group add request received, we create the group without setting any members to it since we cannot
69 // set any members to a group until it is associated with a multicast flow. It is a BAL limitation.
70 // When the related multicast flow has been created we perform set members operation for the group.
71 // That is why we need to keep the members of a group until the multicast flow creation request comes.
72 // We preserve the groups under "FlowGroupsCached" directory in the KV store temporarily. Having set members,
73 // we remove the group from the cached group store.
74 FlowGroupCached = "flow_groups_cached/{%d}"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070075
Akash Kankanala041a2122024-10-16 15:49:22 +053076 // FlowIDPath - Path on the KV store for storing list of Flow IDs for a given subscriber
Girish Gowdraa09aeab2020-09-14 16:30:52 -070077 //Format: BasePathKvStore/<(pon_intf_id, onu_id, uni_id)>/flow_ids
78 FlowIDPath = "{%s}/flow_ids"
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -070079
Akash Kankanala041a2122024-10-16 15:49:22 +053080 // OnuGemInfoPathPathPrefix format: onu_gem_info/<intfid>
Girish Gowdra950326e2021-11-05 12:43:24 -070081 OnuGemInfoPathPathPrefix = "onu_gem_info/{%d}"
Akash Kankanala041a2122024-10-16 15:49:22 +053082 // OnuGemInfoPath is path on the kvstore to store onugem info map
Girish Gowdra950326e2021-11-05 12:43:24 -070083 //format: onu_gem_info/<intfid>/<onu_id>
84 OnuGemInfoPath = OnuGemInfoPathPathPrefix + "/{%d}"
yasin saplid0566272021-12-21 09:10:30 +000085
86 // NNI uint32 version of -1 which represents the NNI port
87 NNI = 4294967295
salmansiddiqui7ac62132019-08-22 03:58:50 +000088)
Abhilash S.L7f17e402019-03-15 17:40:41 +053089
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070090// FlowInfo holds the flow information
Abhilash S.L8ee90712019-04-29 16:24:22 +053091type FlowInfo struct {
Girish Gowdraa09aeab2020-09-14 16:30:52 -070092 Flow *openolt.Flow
93 IsSymmtricFlow bool
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053094}
95
96// OnuGemInfo holds onu information along with gem port list and uni port list
97type OnuGemInfo struct {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053098 SerialNumber string
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053099 GemPorts []uint32
100 UniPorts []uint32
Akash Kankanala041a2122024-10-16 15:49:22 +0530101 OnuID uint32
102 IntfID uint32
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530103}
104
105// PacketInInfoKey is the key for packet in gemport
106type PacketInInfoKey struct {
107 IntfID uint32
108 OnuID uint32
109 LogicalPort uint32
Esin Karaman7fb80c22020-07-16 14:23:33 +0000110 VlanID uint16
111 Priority uint8
Abhilash S.L8ee90712019-04-29 16:24:22 +0530112}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700113
Esin Karamanccb714b2019-11-29 15:02:06 +0000114// GroupInfo holds group information
115type GroupInfo struct {
Esin Karamanccb714b2019-11-29 15:02:06 +0000116 OutPorts []uint32
Akash Kankanala041a2122024-10-16 15:49:22 +0530117 GroupID uint32
Esin Karamanccb714b2019-11-29 15:02:06 +0000118}
119
Girish Gowdraa482f272021-03-24 23:04:19 -0700120// MeterInfo store meter information at path <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
121type MeterInfo struct {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700122 RefCnt uint8 // number of flow references for this meter. When RefCnt is 0, the MeterInfo should be deleted.
123 MeterID uint32
Girish Gowdraa482f272021-03-24 23:04:19 -0700124}
125
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700126// OpenOltResourceMgr holds resource related information as provided below for each field
Abhilash S.L7f17e402019-03-15 17:40:41 +0530127type OpenOltResourceMgr struct {
Akash Kankanala041a2122024-10-16 15:49:22 +0530128 TechprofileRef tp.TechProfileIf
129 KVStore *db.Backend // backend kv store connection handle
130 DevInfo *openolt.DeviceInfo // device information
Girish Gowdru0c588b22019-04-23 23:24:56 -0400131 // array of pon resource managers per interface technology
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700132 PonRsrMgr *ponrmgr.PONResourceManager
Girish Gowdra38d533d2020-03-30 20:38:51 -0700133
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700134 // Local maps used for write-through-cache - start
Akash Kankanala041a2122024-10-16 15:49:22 +0530135 allocIDsForOnu map[string][]uint32
136
137 gemPortIDsForOnu map[string][]uint32
138
139 techProfileIDsForOnu map[string][]uint32
140
141 meterInfoForOnu map[string]*MeterInfo
142
143 onuGemInfo map[string]*OnuGemInfo
144
145 gemPortForPacketInInfo map[string]uint32
146
147 flowIDsForGem map[uint32][]uint64
148
149 mcastQueueForIntf map[uint32][]uint32
150
151 groupInfo map[string]*GroupInfo
152 DeviceID string // OLT device id
153 Address string // Host and port of the kv store to connect to
154 Args string // args
155 DeviceType string
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700156 allocIDsForOnuLock sync.RWMutex
157
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700158 gemPortIDsForOnuLock sync.RWMutex
159
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700160 techProfileIDsForOnuLock sync.RWMutex
161
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700162 meterInfoForOnuLock sync.RWMutex
163
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700164 onuGemInfoLock sync.RWMutex
165
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700166 gemPortForPacketInInfoLock sync.RWMutex
167
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700168 flowIDsForGemLock sync.RWMutex
169
Akash Kankanala041a2122024-10-16 15:49:22 +0530170 mcastQueueForIntfLock sync.RWMutex
171 groupInfoLock sync.RWMutex
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700172 // Local maps used for write-through-cache - end
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700173
Akash Kankanala041a2122024-10-16 15:49:22 +0530174 PonIntfID uint32
175 mcastQueueForIntfLoadedFromKvStore bool
Abhilash S.L7f17e402019-03-15 17:40:41 +0530176}
177
Neha Sharma96b7bf22020-06-15 10:37:32 +0000178func newKVClient(ctx context.Context, storeType string, address string, timeout time.Duration) (kvstore.Client, error) {
179 logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400180 switch storeType {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400181 case "etcd":
Neha Sharma96b7bf22020-06-15 10:37:32 +0000182 return kvstore.NewEtcdClient(ctx, address, timeout, log.FatalLevel)
Abhay Kumar9bcfeb22024-07-12 09:14:25 +0530183 case "redis":
184 return kvstore.NewRedisClient(address, timeout, false)
185 case "redis-sentinel":
186 return kvstore.NewRedisClient(address, timeout, true)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400187 }
188 return nil, errors.New("unsupported-kv-store")
Abhilash S.L7f17e402019-03-15 17:40:41 +0530189}
190
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700191// SetKVClient sets the KV client and return a kv backend
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800192func SetKVClient(ctx context.Context, backend string, addr string, DeviceID string, basePathKvStore string) *db.Backend {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400193 // TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
194 // issue between kv store and backend , core is not calling NewBackend directly
Neha Sharma96b7bf22020-06-15 10:37:32 +0000195 kvClient, err := newKVClient(ctx, backend, addr, KvstoreTimeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400196 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000197 logger.Fatalw(ctx, "Failed to init KV client\n", log.Fields{"err": err})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400198 return nil
199 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700200 // return db.NewBackend(ctx, backend, addr, KvstoreTimeout, fmt.Sprintf(BasePathKvStore, basePathKvStore, DeviceID))
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700201
sbarbaria8910ba2019-11-05 10:12:23 -0500202 kvbackend := &db.Backend{
Girish Gowdru0c588b22019-04-23 23:24:56 -0400203 Client: kvClient,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700204 StoreType: backend,
Neha Sharma3f221ae2020-04-29 19:02:12 +0000205 Address: addr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700206 Timeout: KvstoreTimeout,
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800207 PathPrefix: fmt.Sprintf(BasePathKvStore, basePathKvStore, DeviceID)}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530208
Girish Gowdru0c588b22019-04-23 23:24:56 -0400209 return kvbackend
Abhilash S.L7f17e402019-03-15 17:40:41 +0530210}
211
Holger Hildebrandt143b5be2023-02-10 08:28:15 +0000212// CloseKVClient closes open KV clients
213func (rsrcMgr *OpenOltResourceMgr) CloseKVClient(ctx context.Context) {
214 if rsrcMgr.KVStore != nil {
215 rsrcMgr.KVStore.Client.Close(ctx)
216 rsrcMgr.KVStore = nil
217 }
218 if rsrcMgr.PonRsrMgr != nil {
219 rsrcMgr.PonRsrMgr.CloseKVClient(ctx)
220 }
221}
222
Gamze Abakafee36392019-10-03 11:17:24 +0000223// NewResourceMgr init a New resource manager instance which in turn instantiates pon resource manager
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700224// instances according to technology. Initializes the default resource ranges for all
225// the resources.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700226func 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 -0400227 var ResourceMgr OpenOltResourceMgr
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700228 logger.Debugf(ctx, "Init new resource manager , ponIf: %v, address: %s, device-id: %s", PonIntfID, KVStoreAddress, deviceID)
229 ResourceMgr.PonIntfID = PonIntfID
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700230 ResourceMgr.DeviceID = deviceID
Neha Sharma3f221ae2020-04-29 19:02:12 +0000231 ResourceMgr.Address = KVStoreAddress
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700232 ResourceMgr.DeviceType = deviceType
233 ResourceMgr.DevInfo = devInfo
Abhilash S.L7f17e402019-03-15 17:40:41 +0530234
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700235 Backend := kvStoreType
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800236 ResourceMgr.KVStore = SetKVClient(ctx, Backend, ResourceMgr.Address, deviceID, basePathKvStore)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400237 if ResourceMgr.KVStore == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000238 logger.Error(ctx, "Failed to setup KV store")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400239 }
Girish Gowdra38d533d2020-03-30 20:38:51 -0700240
Girish Gowdru0c588b22019-04-23 23:24:56 -0400241 // TODO self.args = registry('main').get_args()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530242
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700243 // Create a separate Resource Manager instance for each range. This assumes that
Girish Gowdru0c588b22019-04-23 23:24:56 -0400244 // each technology is represented by only a single range
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700245 for _, TechRange := range devInfo.Ranges {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700246 for _, intfID := range TechRange.IntfIds {
247 if intfID == PonIntfID {
248 technology := TechRange.Technology
249 logger.Debugf(ctx, "Device info technology %s, intf-id %v", technology, PonIntfID)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000250
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700251 rsrMgr, err := ponrmgr.NewPONResourceManager(ctx, technology, deviceType, deviceID,
252 Backend, ResourceMgr.Address, basePathKvStore)
253 if err != nil {
254 logger.Errorf(ctx, "Failed to create pon resource manager instance for technology %s", technology)
255 return nil
256 }
257 ResourceMgr.PonRsrMgr = rsrMgr
258 // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
259 InitializeDeviceResourceRangeAndPool(ctx, rsrMgr, TechRange, devInfo)
260 if err := ResourceMgr.PonRsrMgr.InitDeviceResourcePoolForIntf(ctx, intfID); err != nil {
261 logger.Fatal(ctx, "failed-to-initialize-device-resource-pool-intf-id-%v-device-id", ResourceMgr.PonIntfID, ResourceMgr.DeviceID)
262 return nil
263 }
264 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400265 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400266 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700267
268 ResourceMgr.InitLocalCache()
yasin saplibddc2d72022-02-08 13:10:17 +0000269 if err := ResourceMgr.LoadLocalCacheFromKVStore(ctx); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000270 logger.Error(ctx, "failed-to-load-local-cache-from-kvstore")
271 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000272 logger.Info(ctx, "Initialization of resource manager success!")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400273 return &ResourceMgr
Abhilash S.L7f17e402019-03-15 17:40:41 +0530274}
275
Joey Armstrong3f0e2422023-07-05 18:25:41 -0400276// InitLocalCache initializes local maps used for write-through-cache
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700277func (rsrcMgr *OpenOltResourceMgr) InitLocalCache() {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700278 rsrcMgr.allocIDsForOnu = make(map[string][]uint32)
279 rsrcMgr.gemPortIDsForOnu = make(map[string][]uint32)
280 rsrcMgr.techProfileIDsForOnu = make(map[string][]uint32)
281 rsrcMgr.meterInfoForOnu = make(map[string]*MeterInfo)
282 rsrcMgr.onuGemInfo = make(map[string]*OnuGemInfo)
283 rsrcMgr.gemPortForPacketInInfo = make(map[string]uint32)
284 rsrcMgr.flowIDsForGem = make(map[uint32][]uint64)
285 rsrcMgr.mcastQueueForIntf = make(map[uint32][]uint32)
286 rsrcMgr.groupInfo = make(map[string]*GroupInfo)
287}
288
Joey Armstrong3f0e2422023-07-05 18:25:41 -0400289// LoadLocalCacheFromKVStore loads local maps
yasin saplibddc2d72022-02-08 13:10:17 +0000290func (rsrcMgr *OpenOltResourceMgr) LoadLocalCacheFromKVStore(ctx context.Context) error {
Akash Kankanala041a2122024-10-16 15:49:22 +0530291 // List all the keys for OnuGemInfo
yasin saplibddc2d72022-02-08 13:10:17 +0000292 prefixPath := fmt.Sprintf(OnuGemInfoPathPathPrefix, rsrcMgr.PonIntfID)
yasin sapli9e4c5092022-02-01 13:52:33 +0000293 keys, err := rsrcMgr.KVStore.List(ctx, prefixPath)
294 logger.Debug(ctx, "load-local-cache-from-KV-store-started")
295 if err != nil {
296 logger.Errorf(ctx, "failed-to-list-keys-from-path-%s", prefixPath)
297 return err
298 }
299 for path := range keys {
300 var Val []byte
301 var onugem OnuGemInfo
302 // Get rid of the path prefix
303 stringToBeReplaced := rsrcMgr.KVStore.PathPrefix + "/"
304 replacedWith := ""
305 path = strings.Replace(path, stringToBeReplaced, replacedWith, 1)
306
307 value, err := rsrcMgr.KVStore.Get(ctx, path)
308 if err != nil {
309 logger.Errorw(ctx, "failed-to-get-from-kv-store", log.Fields{"path": path})
310 return err
311 } else if value == nil {
312 logger.Debug(ctx, "no-onugeminfo-for-path", log.Fields{"path": path})
313 continue
314 }
315 if Val, err = kvstore.ToByte(value.Value); err != nil {
316 logger.Error(ctx, "failed-to-covert-to-byte-array")
317 return err
318 }
319 if err = json.Unmarshal(Val, &onugem); err != nil {
320 logger.Error(ctx, "failed-to-unmarshall")
321 return err
322 }
323 logger.Debugw(ctx, "found-onugeminfo-from-path", log.Fields{"path": path, "onuGemInfo": onugem})
324
325 rsrcMgr.onuGemInfoLock.Lock()
326 rsrcMgr.onuGemInfo[path] = &onugem
327 rsrcMgr.onuGemInfoLock.Unlock()
yasin sapli9e4c5092022-02-01 13:52:33 +0000328 }
329 logger.Debug(ctx, "load-local-cache-from-KV-store-finished")
330 return nil
331}
332
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700333// InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply
334// device specific information. If KV doesn't exist
335// or is broader than the device, the device's information will
336// dictate the range limits
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700337func InitializeDeviceResourceRangeAndPool(ctx context.Context, ponRMgr *ponrmgr.PONResourceManager,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700338 techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700339 // var ONUIDShared, AllocIDShared, GEMPortIDShared openolt.DeviceInfo_DeviceResourceRanges_Pool_SharingType
340 var ONUIDStart, ONUIDEnd, AllocIDStart, AllocIDEnd, GEMPortIDStart, GEMPortIDEnd uint32
341 var ONUIDShared, AllocIDShared, GEMPortIDShared, FlowIDShared uint32
342
343 // The below variables are just dummy and needed to pass as arguments to InitDefaultPONResourceRanges function.
344 // The openolt adapter does not need flowIDs to be managed as it is managed on the OLT device
345 // The UNI IDs are dynamically generated by openonu adapter for every discovered UNI.
346 var flowIDDummyStart, flowIDDummyEnd uint32 = 1, 2
347 var uniIDDummyStart, uniIDDummyEnd uint32 = 0, 1
Abhilash S.L7f17e402019-03-15 17:40:41 +0530348
Girish Gowdru0c588b22019-04-23 23:24:56 -0400349 // init the resource range pool according to the sharing type
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700350 logger.Debugw(ctx, "Device info init", log.Fields{"technology": techRange.Technology,
351 "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd,
352 "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd,
353 "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd,
354 "intf_ids": techRange.IntfIds,
355 })
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700356 for _, RangePool := range techRange.Pools {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700357 // FIXME: Remove hardcoding
Girish Gowdru0c588b22019-04-23 23:24:56 -0400358 if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID {
359 ONUIDStart = RangePool.Start
360 ONUIDEnd = RangePool.End
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700361 ONUIDShared = uint32(RangePool.Sharing)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400362 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID {
363 AllocIDStart = RangePool.Start
364 AllocIDEnd = RangePool.End
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700365 AllocIDShared = uint32(RangePool.Sharing)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400366 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID {
367 GEMPortIDStart = RangePool.Start
368 GEMPortIDEnd = RangePool.End
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700369 GEMPortIDShared = uint32(RangePool.Sharing)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400370 }
371 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530372
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700373 ponRMgr.InitDefaultPONResourceRanges(ctx, ONUIDStart, ONUIDEnd, ONUIDShared,
374 AllocIDStart, AllocIDEnd, AllocIDShared,
375 GEMPortIDStart, GEMPortIDEnd, GEMPortIDShared,
376 flowIDDummyStart, flowIDDummyEnd, FlowIDShared, uniIDDummyStart, uniIDDummyEnd,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700377 devInfo.PonPorts, techRange.IntfIds)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530378}
379
Devmalya Paul495b94a2019-08-27 19:42:00 -0400380// Delete clears used resources for the particular olt device being deleted
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700381func (rsrcMgr *OpenOltResourceMgr) Delete(ctx context.Context, intfID uint32) error {
382 if err := rsrcMgr.PonRsrMgr.ClearDeviceResourcePoolForIntf(ctx, intfID); err != nil {
383 logger.Debug(ctx, "Failed to clear device resource pool")
384 return err
Devmalya Paul495b94a2019-08-27 19:42:00 -0400385 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000386 logger.Debug(ctx, "Cleared device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400387 return nil
388}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530389
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700390// GetONUID returns the available onuID for the given pon-port
yasin saplibddc2d72022-02-08 13:10:17 +0000391func (rsrcMgr *OpenOltResourceMgr) GetONUID(ctx context.Context) (uint32, error) {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400392 // Get ONU id for a provided pon interface ID.
yasin saplibddc2d72022-02-08 13:10:17 +0000393 onuID, err := rsrcMgr.TechprofileRef.GetResourceID(ctx, rsrcMgr.PonIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400394 ponrmgr.ONU_ID, 1)
395 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000396 logger.Errorf(ctx, "Failed to get resource for interface %d for type %s",
yasin saplibddc2d72022-02-08 13:10:17 +0000397 rsrcMgr.PonIntfID, ponrmgr.ONU_ID)
cbabuabf02352019-10-15 13:14:56 +0200398 return 0, err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400399 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700400 if len(onuID) > 0 {
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700401 return onuID[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400402 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530403
Girish Gowdra950326e2021-11-05 12:43:24 -0700404 return 0, fmt.Errorf("no-onu-id-allocated")
Abhilash S.L8ee90712019-04-29 16:24:22 +0530405}
406
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700407// UpdateAllocIdsForOnu updates alloc ids in kv store for a given pon interface id, onu id and uni id
yasin saplibddc2d72022-02-08 13:10:17 +0000408func (rsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ctx context.Context, onuID uint32, uniID uint32, allocIDs []uint32) error {
yasin saplibddc2d72022-02-08 13:10:17 +0000409 intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", rsrcMgr.PonIntfID, onuID, uniID)
Gamze Abaka745ccb72021-11-18 11:29:58 +0000410
411 // Note: in case the write to DB fails there could be inconsistent data between cache and db.
412 // Although this is highly unlikely with DB retries in place, this is something we have to deal with in the next release
413 if err := rsrcMgr.PonRsrMgr.UpdateAllocIdsForOnu(ctx, intfOnuIDuniID, allocIDs); err != nil {
414 logger.Errorw(ctx, "Failed to update alloc ids for onu", log.Fields{"err": err})
415 return err
416 }
417
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700418 // update cache
419 rsrcMgr.allocIDsForOnuLock.Lock()
420 rsrcMgr.allocIDsForOnu[intfOnuIDuniID] = allocIDs
421 rsrcMgr.allocIDsForOnuLock.Unlock()
Gamze Abaka745ccb72021-11-18 11:29:58 +0000422 return nil
Abhilash S.L7f17e402019-03-15 17:40:41 +0530423}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700424
425// GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id
Akash Kankanala041a2122024-10-16 15:49:22 +0530426func (rsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(ctx context.Context, onuID uint32, uniID uint32) []uint32 {
yasin saplibddc2d72022-02-08 13:10:17 +0000427 intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", rsrcMgr.PonIntfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530428
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700429 // fetch from cache
430 rsrcMgr.gemPortIDsForOnuLock.RLock()
431 gemIDs, ok := rsrcMgr.gemPortIDsForOnu[intfOnuIDuniID]
432 rsrcMgr.gemPortIDsForOnuLock.RUnlock()
433 if ok {
434 return gemIDs
435 }
436 /* Get gem ports for given pon interface , onu id and uni id. */
437 gemIDs = rsrcMgr.PonRsrMgr.GetCurrentGEMPortIDsForOnu(ctx, intfOnuIDuniID)
438
439 // update cache
440 rsrcMgr.gemPortIDsForOnuLock.Lock()
441 rsrcMgr.gemPortIDsForOnu[intfOnuIDuniID] = gemIDs
442 rsrcMgr.gemPortIDsForOnuLock.Unlock()
443
444 return gemIDs
Abhilash S.L7f17e402019-03-15 17:40:41 +0530445}
446
Gamze Abakafee36392019-10-03 11:17:24 +0000447// GetCurrentAllocIDsForOnu returns alloc ids for given pon interface and onu id
yasin saplibddc2d72022-02-08 13:10:17 +0000448func (rsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDsForOnu(ctx context.Context, onuID uint32, uniID uint32) []uint32 {
yasin saplibddc2d72022-02-08 13:10:17 +0000449 intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", rsrcMgr.PonIntfID, onuID, uniID)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700450 // fetch from cache
451 rsrcMgr.allocIDsForOnuLock.RLock()
452 allocIDs, ok := rsrcMgr.allocIDsForOnu[intfOnuIDuniID]
453 rsrcMgr.allocIDsForOnuLock.RUnlock()
454 if ok {
455 return allocIDs
Girish Gowdru0c588b22019-04-23 23:24:56 -0400456 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700457 allocIDs = rsrcMgr.PonRsrMgr.GetCurrentAllocIDForOnu(ctx, intfOnuIDuniID)
458
459 // update cache
460 rsrcMgr.allocIDsForOnuLock.Lock()
461 rsrcMgr.allocIDsForOnu[intfOnuIDuniID] = allocIDs
462 rsrcMgr.allocIDsForOnuLock.Unlock()
463
464 return allocIDs
Gamze Abakafee36392019-10-03 11:17:24 +0000465}
466
467// RemoveAllocIDForOnu removes the alloc id for given pon interface, onu id, uni id and alloc id
yasin saplibddc2d72022-02-08 13:10:17 +0000468func (rsrcMgr *OpenOltResourceMgr) RemoveAllocIDForOnu(ctx context.Context, onuID uint32, uniID uint32, allocID uint32) {
469 allocIDs := rsrcMgr.GetCurrentAllocIDsForOnu(ctx, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000470 for i := 0; i < len(allocIDs); i++ {
471 if allocIDs[i] == allocID {
472 allocIDs = append(allocIDs[:i], allocIDs[i+1:]...)
473 break
474 }
475 }
yasin saplibddc2d72022-02-08 13:10:17 +0000476 err := rsrcMgr.UpdateAllocIdsForOnu(ctx, onuID, uniID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000477 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700478 logger.Errorf(ctx, "Failed to Remove Alloc Id For Onu. intfID %d onuID %d uniID %d allocID %d",
yasin saplibddc2d72022-02-08 13:10:17 +0000479 rsrcMgr.PonIntfID, onuID, uniID, allocID)
Gamze Abakafee36392019-10-03 11:17:24 +0000480 }
481}
482
483// RemoveGemPortIDForOnu removes the gem port id for given pon interface, onu id, uni id and gem port id
yasin saplibddc2d72022-02-08 13:10:17 +0000484func (rsrcMgr *OpenOltResourceMgr) RemoveGemPortIDForOnu(ctx context.Context, onuID uint32, uniID uint32, gemPortID uint32) {
485 gemPortIDs := rsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000486 for i := 0; i < len(gemPortIDs); i++ {
487 if gemPortIDs[i] == gemPortID {
488 gemPortIDs = append(gemPortIDs[:i], gemPortIDs[i+1:]...)
489 break
490 }
491 }
yasin saplibddc2d72022-02-08 13:10:17 +0000492 err := rsrcMgr.UpdateGEMPortIDsForOnu(ctx, onuID, uniID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000493 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700494 logger.Errorf(ctx, "Failed to Remove Gem Id For Onu. intfID %d onuID %d uniID %d gemPortId %d",
yasin saplibddc2d72022-02-08 13:10:17 +0000495 rsrcMgr.PonIntfID, onuID, uniID, gemPortID)
Gamze Abakafee36392019-10-03 11:17:24 +0000496 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530497}
498
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700499// UpdateGEMPortIDsForOnu updates gemport ids on to the kv store for a given pon port, onu id and uni id
Akash Kankanala041a2122024-10-16 15:49:22 +0530500func (rsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ctx context.Context, onuID uint32, uniID uint32, gemIDs []uint32) error {
yasin saplibddc2d72022-02-08 13:10:17 +0000501 intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", rsrcMgr.PonIntfID, onuID, uniID)
Gamze Abaka745ccb72021-11-18 11:29:58 +0000502
503 if err := rsrcMgr.PonRsrMgr.UpdateGEMPortIDsForOnu(ctx, intfOnuIDuniID, gemIDs); err != nil {
504 logger.Errorw(ctx, "Failed to update gem port ids for onu", log.Fields{"err": err})
505 return err
506 }
507
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700508 // update cache
509 rsrcMgr.gemPortIDsForOnuLock.Lock()
510 rsrcMgr.gemPortIDsForOnu[intfOnuIDuniID] = gemIDs
511 rsrcMgr.gemPortIDsForOnuLock.Unlock()
Gamze Abaka745ccb72021-11-18 11:29:58 +0000512 return nil
Abhilash S.L7f17e402019-03-15 17:40:41 +0530513}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700514
515// FreeonuID releases(make free) onu id for a particular pon-port
yasin saplibddc2d72022-02-08 13:10:17 +0000516func (rsrcMgr *OpenOltResourceMgr) FreeonuID(ctx context.Context, onuID []uint32) {
Girish Gowdrab8f1b5a2021-06-27 20:42:40 -0700517 if len(onuID) == 0 {
518 logger.Info(ctx, "onu id slice is nil, nothing to free")
519 return
520 }
yasin saplibddc2d72022-02-08 13:10:17 +0000521 if err := rsrcMgr.TechprofileRef.FreeResourceID(ctx, rsrcMgr.PonIntfID, ponrmgr.ONU_ID, onuID); err != nil {
Matteo Scandolo84585372021-03-18 14:21:22 -0700522 logger.Errorw(ctx, "error-while-freeing-onu-id", log.Fields{
yasin saplibddc2d72022-02-08 13:10:17 +0000523 "intf-id": rsrcMgr.PonIntfID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700524 "onu-id": onuID,
525 "err": err.Error(),
526 })
Girish Gowdrab8f1b5a2021-06-27 20:42:40 -0700527 } else {
yasin saplibddc2d72022-02-08 13:10:17 +0000528 logger.Infow(ctx, "freed onu id", log.Fields{"intfID": rsrcMgr.PonIntfID, "onuID": onuID})
Matteo Scandolo84585372021-03-18 14:21:22 -0700529 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530530}
531
Gamze Abakafee36392019-10-03 11:17:24 +0000532// FreeAllocID frees AllocID on the PON resource pool and also frees the allocID association
533// for the given OLT device.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700534// The caller should ensure that this is a blocking call and this operation is serialized for
535// the ONU so as not cause resource corruption since there are no mutexes used here.
Girish Gowdraf3728b12022-02-02 21:46:51 -0800536// Setting freeFromResourcePool to false will not clear it from the resource pool but only
537// clear it for the given pon/onu/uni
Akash Kankanala041a2122024-10-16 15:49:22 +0530538func (rsrcMgr *OpenOltResourceMgr) FreeAllocID(ctx context.Context, onuID uint32, uniID uint32, allocID uint32, freeFromResourcePool bool) {
yasin saplibddc2d72022-02-08 13:10:17 +0000539 rsrcMgr.RemoveAllocIDForOnu(ctx, onuID, uniID, allocID)
Girish Gowdraf3728b12022-02-02 21:46:51 -0800540 if freeFromResourcePool {
541 allocIDs := make([]uint32, 0)
542 allocIDs = append(allocIDs, allocID)
yasin saplibddc2d72022-02-08 13:10:17 +0000543 if err := rsrcMgr.TechprofileRef.FreeResourceID(ctx, rsrcMgr.PonIntfID, ponrmgr.ALLOC_ID, allocIDs); err != nil {
Girish Gowdraf3728b12022-02-02 21:46:51 -0800544 logger.Errorw(ctx, "error-while-freeing-alloc-id", log.Fields{
yasin saplibddc2d72022-02-08 13:10:17 +0000545 "intf-id": rsrcMgr.PonIntfID,
Girish Gowdraf3728b12022-02-02 21:46:51 -0800546 "onu-id": onuID,
547 "err": err.Error(),
548 })
549 }
Matteo Scandolo84585372021-03-18 14:21:22 -0700550 }
Gamze Abakafee36392019-10-03 11:17:24 +0000551}
552
553// FreeGemPortID frees GemPortID on the PON resource pool and also frees the gemPortID association
554// for the given OLT device.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700555// The caller should ensure that this is a blocking call and this operation is serialized for
556// the ONU so as not cause resource corruption since there are no mutexes used here.
yasin saplibddc2d72022-02-08 13:10:17 +0000557func (rsrcMgr *OpenOltResourceMgr) FreeGemPortID(ctx context.Context, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000558 uniID uint32, gemPortID uint32) {
yasin saplibddc2d72022-02-08 13:10:17 +0000559 rsrcMgr.RemoveGemPortIDForOnu(ctx, onuID, uniID, gemPortID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700560
Gamze Abakafee36392019-10-03 11:17:24 +0000561 gemPortIDs := make([]uint32, 0)
562 gemPortIDs = append(gemPortIDs, gemPortID)
yasin saplibddc2d72022-02-08 13:10:17 +0000563 if err := rsrcMgr.TechprofileRef.FreeResourceID(ctx, rsrcMgr.PonIntfID, ponrmgr.GEMPORT_ID, gemPortIDs); err != nil {
Matteo Scandolo84585372021-03-18 14:21:22 -0700564 logger.Errorw(ctx, "error-while-freeing-gem-port-id", log.Fields{
yasin saplibddc2d72022-02-08 13:10:17 +0000565 "intf-id": rsrcMgr.PonIntfID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700566 "onu-id": onuID,
567 "err": err.Error(),
568 })
569 }
Gamze Abakafee36392019-10-03 11:17:24 +0000570}
571
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700572// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id
yasin saplibddc2d72022-02-08 13:10:17 +0000573func (rsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(ctx context.Context, onuID uint32, uniID uint32) {
yasin saplibddc2d72022-02-08 13:10:17 +0000574 intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", rsrcMgr.PonIntfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530575
Sridhar Ravindra92c9b0d2025-01-16 00:15:54 +0530576 if rsrcMgr.PonRsrMgr == nil || rsrcMgr.TechprofileRef == nil {
577 logger.Warn(ctx, "PonRsrMgr or TechprofileRef is nil")
578 return
579 }
580
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700581 AllocIDs := rsrcMgr.PonRsrMgr.GetCurrentAllocIDForOnu(ctx, intfOnuIDuniID)
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700582
yasin saplibddc2d72022-02-08 13:10:17 +0000583 if err := rsrcMgr.TechprofileRef.FreeResourceID(ctx, rsrcMgr.PonIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400584 ponrmgr.ALLOC_ID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700585 AllocIDs); err != nil {
586 logger.Errorw(ctx, "error-while-freeing-all-alloc-ids-for-onu", log.Fields{
yasin saplibddc2d72022-02-08 13:10:17 +0000587 "intf-id": rsrcMgr.PonIntfID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700588 "onu-id": onuID,
589 "err": err.Error(),
590 })
591 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530592
Akash Kankanala041a2122024-10-16 15:49:22 +0530593 // update cache
Gamze Abaka745ccb72021-11-18 11:29:58 +0000594 rsrcMgr.allocIDsForOnuLock.Lock()
595 delete(rsrcMgr.allocIDsForOnu, intfOnuIDuniID)
596 rsrcMgr.allocIDsForOnuLock.Unlock()
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700597
Gamze Abaka745ccb72021-11-18 11:29:58 +0000598 GEMPortIDs := rsrcMgr.PonRsrMgr.GetCurrentGEMPortIDsForOnu(ctx, intfOnuIDuniID)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700599
yasin saplibddc2d72022-02-08 13:10:17 +0000600 if err := rsrcMgr.TechprofileRef.FreeResourceID(ctx, rsrcMgr.PonIntfID,
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{
yasin saplibddc2d72022-02-08 13:10:17 +0000604 "intf-id": rsrcMgr.PonIntfID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700605 "onu-id": onuID,
606 "err": err.Error(),
607 })
608 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530609
Gamze Abaka745ccb72021-11-18 11:29:58 +0000610 // update cache
611 rsrcMgr.gemPortIDsForOnuLock.Lock()
612 delete(rsrcMgr.gemPortIDsForOnu, intfOnuIDuniID)
613 rsrcMgr.gemPortIDsForOnuLock.Unlock()
614
Girish Gowdru0c588b22019-04-23 23:24:56 -0400615 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700616 rsrcMgr.PonRsrMgr.RemoveResourceMap(ctx, intfOnuIDuniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530617}
618
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700619// IsFlowOnKvStore checks if the given flowID is present on the kv store
620// Returns true if the flowID is found, otherwise it returns false
yasin saplibddc2d72022-02-08 13:10:17 +0000621func (rsrcMgr *OpenOltResourceMgr) IsFlowOnKvStore(ctx context.Context, onuID int32, flowID uint64) (bool, error) {
yasin saplid0566272021-12-21 09:10:30 +0000622 var anyError error
Abhilash S.L7f17e402019-03-15 17:40:41 +0530623
yasin saplid0566272021-12-21 09:10:30 +0000624 // In case of nni trap flow
625 if onuID == -1 {
yasin saplibddc2d72022-02-08 13:10:17 +0000626 nniTrapflowIDs, err := rsrcMgr.GetFlowIDsForGem(ctx, NNI)
yasin saplid0566272021-12-21 09:10:30 +0000627 if err != nil {
628 logger.Warnw(ctx, "failed-to-get-nni-trap-flowIDs", log.Fields{"err": err})
629 return false, err
630 }
631 for _, id := range nniTrapflowIDs {
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700632 if flowID == id {
yasin saplid0566272021-12-21 09:10:30 +0000633 return true, nil
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400634 }
635 }
636 }
yasin saplid0566272021-12-21 09:10:30 +0000637
yasin saplibddc2d72022-02-08 13:10:17 +0000638 path := fmt.Sprintf(OnuGemInfoPath, rsrcMgr.PonIntfID, onuID)
yasin saplid0566272021-12-21 09:10:30 +0000639 rsrcMgr.onuGemInfoLock.RLock()
640 val, ok := rsrcMgr.onuGemInfo[path]
641 rsrcMgr.onuGemInfoLock.RUnlock()
642
643 if ok {
644 for _, gem := range val.GemPorts {
yasin saplibddc2d72022-02-08 13:10:17 +0000645 flowIDs, err := rsrcMgr.GetFlowIDsForGem(ctx, gem)
yasin saplid0566272021-12-21 09:10:30 +0000646 if err != nil {
647 anyError = err
648 logger.Warnw(ctx, "failed-to-get-flowIDs-for-gem", log.Fields{"err": err, "onuID": onuID, "gem": gem})
649 } else {
650 for _, id := range flowIDs {
651 if flowID == id {
652 return true, nil
653 }
654 }
655 }
656 }
657 }
658 return false, anyError
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400659}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400660
salmansiddiqui7ac62132019-08-22 03:58:50 +0000661// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700662// This path is formed as the following: {intfID, onuID, uniID}/tp_id
yasin saplibddc2d72022-02-08 13:10:17 +0000663func (rsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(ctx context.Context, onuID uint32, uniID uint32) []uint32 {
664 Path := fmt.Sprintf(tpIDPathSuffix, rsrcMgr.PonIntfID, onuID, uniID)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700665 // fetch from cache
666 rsrcMgr.techProfileIDsForOnuLock.RLock()
667 tpIDs, ok := rsrcMgr.techProfileIDsForOnu[Path]
668 rsrcMgr.techProfileIDsForOnuLock.RUnlock()
669 if ok {
670 return tpIDs
671 }
672 Value, err := rsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400673 if err == nil {
674 if Value != nil {
675 Val, err := kvstore.ToByte(Value.Value)
676 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700677 logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"err": err})
678 return tpIDs
Manikkaraj kb1d51442019-07-23 10:41:02 -0400679 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700680 if err = json.Unmarshal(Val, &tpIDs); err != nil {
681 logger.Error(ctx, "Failed to unmarshal", log.Fields{"err": err})
682 return tpIDs
Manikkaraj kb1d51442019-07-23 10:41:02 -0400683 }
684 }
685 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000686 logger.Errorf(ctx, "Failed to get TP id from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400687 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700688 logger.Debugf(ctx, "Getting TP id %d from path %s", tpIDs, Path)
689
690 // update cache
691 rsrcMgr.techProfileIDsForOnuLock.Lock()
692 rsrcMgr.techProfileIDsForOnu[Path] = tpIDs
693 rsrcMgr.techProfileIDsForOnuLock.Unlock()
694
695 return tpIDs
Manikkaraj kb1d51442019-07-23 10:41:02 -0400696}
697
Gamze Abakafee36392019-10-03 11:17:24 +0000698// 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 -0700699// This path is formed as the following: {intfID, onuID, uniID}/tp_id
yasin saplibddc2d72022-02-08 13:10:17 +0000700func (rsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDsForOnu(ctx context.Context, onuID uint32, uniID uint32) error {
701 intfOnuUniID := fmt.Sprintf(tpIDPathSuffix, rsrcMgr.PonIntfID, onuID, uniID)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700702
703 if err := rsrcMgr.KVStore.Delete(ctx, intfOnuUniID); err != nil {
704 logger.Errorw(ctx, "Failed to delete techprofile id resource in KV store", log.Fields{"path": intfOnuUniID})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400705 return err
706 }
Gamze Abaka745ccb72021-11-18 11:29:58 +0000707
708 // update cache
709 rsrcMgr.techProfileIDsForOnuLock.Lock()
710 delete(rsrcMgr.techProfileIDsForOnu, intfOnuUniID)
711 rsrcMgr.techProfileIDsForOnuLock.Unlock()
Manikkaraj kb1d51442019-07-23 10:41:02 -0400712 return nil
713}
714
Gamze Abakafee36392019-10-03 11:17:24 +0000715// 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 -0700716// This path is formed as the following: {intfID, onuID, uniID}/tp_id
yasin saplibddc2d72022-02-08 13:10:17 +0000717func (rsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(ctx context.Context, onuID uint32, uniID uint32, tpID uint32) error {
718 tpIDList := rsrcMgr.GetTechProfileIDForOnu(ctx, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000719 for i, tpIDInList := range tpIDList {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700720 if tpIDInList == tpID {
Gamze Abakafee36392019-10-03 11:17:24 +0000721 tpIDList = append(tpIDList[:i], tpIDList[i+1:]...)
722 }
723 }
yasin saplibddc2d72022-02-08 13:10:17 +0000724 intfOnuUniID := fmt.Sprintf(tpIDPathSuffix, rsrcMgr.PonIntfID, onuID, uniID)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700725
Gamze Abakafee36392019-10-03 11:17:24 +0000726 Value, err := json.Marshal(tpIDList)
727 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000728 logger.Error(ctx, "failed to Marshal")
Gamze Abakafee36392019-10-03 11:17:24 +0000729 return err
730 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700731 if err = rsrcMgr.KVStore.Put(ctx, intfOnuUniID, Value); err != nil {
732 logger.Errorf(ctx, "Failed to update resource %s", intfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000733 return err
734 }
Gamze Abaka745ccb72021-11-18 11:29:58 +0000735
736 // update cache
737 rsrcMgr.techProfileIDsForOnuLock.Lock()
738 rsrcMgr.techProfileIDsForOnu[intfOnuUniID] = tpIDList
739 rsrcMgr.techProfileIDsForOnuLock.Unlock()
Gamze Abakafee36392019-10-03 11:17:24 +0000740 return err
741}
742
743// UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700744// This path is formed as the following: {intfID, onuID, uniID}/tp_id
yasin saplibddc2d72022-02-08 13:10:17 +0000745func (rsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(ctx context.Context, onuID uint32,
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700746 uniID uint32, tpID uint32) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400747 var Value []byte
748 var err error
749
yasin saplibddc2d72022-02-08 13:10:17 +0000750 intfOnuUniID := fmt.Sprintf(tpIDPathSuffix, rsrcMgr.PonIntfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000751
yasin saplibddc2d72022-02-08 13:10:17 +0000752 tpIDList := rsrcMgr.GetTechProfileIDForOnu(ctx, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000753 for _, value := range tpIDList {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700754 if value == tpID {
755 logger.Debugf(ctx, "tpID %d is already in tpIdList for the path %s", tpID, intfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000756 return err
757 }
758 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700759 logger.Debugf(ctx, "updating tp id %d on path %s", tpID, intfOnuUniID)
760 tpIDList = append(tpIDList, tpID)
761
Gamze Abakafee36392019-10-03 11:17:24 +0000762 Value, err = json.Marshal(tpIDList)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400763 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000764 logger.Error(ctx, "failed to Marshal")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400765 return err
766 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700767 if err = rsrcMgr.KVStore.Put(ctx, intfOnuUniID, Value); err != nil {
768 logger.Errorf(ctx, "Failed to update resource %s", intfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400769 return err
770 }
Gamze Abaka745ccb72021-11-18 11:29:58 +0000771
772 // update cache
773 rsrcMgr.techProfileIDsForOnuLock.Lock()
774 rsrcMgr.techProfileIDsForOnu[intfOnuUniID] = tpIDList
775 rsrcMgr.techProfileIDsForOnuLock.Unlock()
Manikkaraj kb1d51442019-07-23 10:41:02 -0400776 return err
777}
778
Girish Gowdraa482f272021-03-24 23:04:19 -0700779// StoreMeterInfoForOnu updates the meter id in the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000780// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
yasin saplibddc2d72022-02-08 13:10:17 +0000781func (rsrcMgr *OpenOltResourceMgr) StoreMeterInfoForOnu(ctx context.Context, Direction string, onuID uint32,
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700782 uniID uint32, tpID uint32, meterInfo *MeterInfo) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400783 var Value []byte
784 var err error
yasin saplibddc2d72022-02-08 13:10:17 +0000785 intfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, rsrcMgr.PonIntfID, onuID, uniID, tpID, Direction)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700786
Girish Gowdraa482f272021-03-24 23:04:19 -0700787 Value, err = json.Marshal(*meterInfo)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400788 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000789 logger.Error(ctx, "failed to Marshal meter config")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400790 return err
791 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700792 if err = rsrcMgr.KVStore.Put(ctx, intfOnuUniID, Value); err != nil {
793 logger.Errorf(ctx, "Failed to store meter into KV store %s", intfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400794 return err
795 }
Gamze Abaka745ccb72021-11-18 11:29:58 +0000796
797 // update cache
798 rsrcMgr.meterInfoForOnuLock.Lock()
799 rsrcMgr.meterInfoForOnu[intfOnuUniID] = meterInfo
800 rsrcMgr.meterInfoForOnuLock.Unlock()
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700801 logger.Debugw(ctx, "meter info updated successfully", log.Fields{"path": intfOnuUniID, "meter-info": meterInfo})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400802 return err
803}
804
Girish Gowdraa482f272021-03-24 23:04:19 -0700805// GetMeterInfoForOnu fetches the meter id from the kv store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000806// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
yasin saplibddc2d72022-02-08 13:10:17 +0000807func (rsrcMgr *OpenOltResourceMgr) GetMeterInfoForOnu(ctx context.Context, Direction string, onuID uint32,
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700808 uniID uint32, tpID uint32) (*MeterInfo, error) {
yasin saplibddc2d72022-02-08 13:10:17 +0000809 Path := fmt.Sprintf(MeterIDPathSuffix, rsrcMgr.PonIntfID, onuID, uniID, tpID, Direction)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700810
811 // get from cache
812 rsrcMgr.meterInfoForOnuLock.RLock()
813 val, ok := rsrcMgr.meterInfoForOnu[Path]
814 rsrcMgr.meterInfoForOnuLock.RUnlock()
815 if ok {
816 return val, nil
817 }
818
Girish Gowdraa482f272021-03-24 23:04:19 -0700819 var meterInfo MeterInfo
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700820 Value, err := rsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400821 if err == nil {
822 if Value != nil {
Girish Gowdraa482f272021-03-24 23:04:19 -0700823 logger.Debug(ctx, "Found meter info in KV store", log.Fields{"Direction": Direction})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000824 Val, er := kvstore.ToByte(Value.Value)
825 if er != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700826 logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"err": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000827 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400828 }
Girish Gowdraa482f272021-03-24 23:04:19 -0700829 if er = json.Unmarshal(Val, &meterInfo); er != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700830 logger.Error(ctx, "Failed to unmarshal meter info", log.Fields{"err": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000831 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400832 }
833 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000834 logger.Debug(ctx, "meter-does-not-exists-in-KVStore")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400835 return nil, err
836 }
837 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000838 logger.Errorf(ctx, "Failed to get Meter config from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400839 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700840 // update cache
841 rsrcMgr.meterInfoForOnuLock.Lock()
842 rsrcMgr.meterInfoForOnu[Path] = &meterInfo
843 rsrcMgr.meterInfoForOnuLock.Unlock()
844
Girish Gowdraa482f272021-03-24 23:04:19 -0700845 return &meterInfo, err
Manikkaraj kb1d51442019-07-23 10:41:02 -0400846}
847
Girish Gowdraa482f272021-03-24 23:04:19 -0700848// HandleMeterInfoRefCntUpdate increments or decrements the reference counter for a given meter.
849// When reference count becomes 0, it clears the meter information from the kv store
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700850func (rsrcMgr *OpenOltResourceMgr) HandleMeterInfoRefCntUpdate(ctx context.Context, Direction string,
yasin saplibddc2d72022-02-08 13:10:17 +0000851 onuID uint32, uniID uint32, tpID uint32, increment bool) error {
852 meterInfo, err := rsrcMgr.GetMeterInfoForOnu(ctx, Direction, onuID, uniID, tpID)
Girish Gowdra82c80982021-03-26 16:22:02 -0700853 if err != nil {
854 return err
855 } else if meterInfo == nil {
856 // If we are increasing the reference count, we expect the meter information to be present on KV store.
857 // But if decrementing the reference count, the meter is possibly already cleared from KV store. Just log warn but do not return error.
858 if increment {
yasin saplibddc2d72022-02-08 13:10:17 +0000859 logger.Errorf(ctx, "error-fetching-meter-info-for-intf-%d-onu-%d-uni-%d-tp-id-%d-direction-%s", rsrcMgr.PonIntfID, onuID, uniID, tpID, Direction)
860 return fmt.Errorf("error-fetching-meter-info-for-intf-%d-onu-%d-uni-%d-tp-id-%d-direction-%s", rsrcMgr.PonIntfID, onuID, uniID, tpID, Direction)
Girish Gowdra82c80982021-03-26 16:22:02 -0700861 }
862 logger.Warnw(ctx, "meter is already cleared",
yasin saplibddc2d72022-02-08 13:10:17 +0000863 log.Fields{"intfID": rsrcMgr.PonIntfID, "onuID": onuID, "uniID": uniID, "direction": Direction, "increment": increment})
Girish Gowdra82c80982021-03-26 16:22:02 -0700864 return nil
Girish Gowdraa482f272021-03-24 23:04:19 -0700865 }
Girish Gowdra82c80982021-03-26 16:22:02 -0700866
Girish Gowdraa482f272021-03-24 23:04:19 -0700867 if increment {
868 meterInfo.RefCnt++
869 } else {
870 meterInfo.RefCnt--
Girish Gowdraa482f272021-03-24 23:04:19 -0700871 }
yasin saplibddc2d72022-02-08 13:10:17 +0000872 if err := rsrcMgr.StoreMeterInfoForOnu(ctx, Direction, onuID, uniID, tpID, meterInfo); err != nil {
Girish Gowdraa482f272021-03-24 23:04:19 -0700873 return err
874 }
875 return nil
876}
877
878// RemoveMeterInfoForOnu deletes the meter id from the kV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000879// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
yasin saplibddc2d72022-02-08 13:10:17 +0000880func (rsrcMgr *OpenOltResourceMgr) RemoveMeterInfoForOnu(ctx context.Context, Direction string, onuID uint32,
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700881 uniID uint32, tpID uint32) error {
yasin saplibddc2d72022-02-08 13:10:17 +0000882 Path := fmt.Sprintf(MeterIDPathSuffix, rsrcMgr.PonIntfID, onuID, uniID, tpID, Direction)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700883
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700884 if err := rsrcMgr.KVStore.Delete(ctx, Path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000885 logger.Errorf(ctx, "Failed to delete meter id %s from kvstore ", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400886 return err
887 }
Gamze Abaka745ccb72021-11-18 11:29:58 +0000888
889 // update cache
890 rsrcMgr.meterInfoForOnuLock.Lock()
891 delete(rsrcMgr.meterInfoForOnu, Path)
892 rsrcMgr.meterInfoForOnuLock.Unlock()
Manikkaraj kb1d51442019-07-23 10:41:02 -0400893 return nil
894}
salmansiddiqui7ac62132019-08-22 03:58:50 +0000895
Joey Armstrong3f0e2422023-07-05 18:25:41 -0400896// AddGemToOnuGemInfo adds gemport to onugem info kvstore and also local cache
yasin saplibddc2d72022-02-08 13:10:17 +0000897func (rsrcMgr *OpenOltResourceMgr) AddGemToOnuGemInfo(ctx context.Context, onuID uint32, gemPort uint32) error {
898 onugem, err := rsrcMgr.GetOnuGemInfo(ctx, onuID)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700899 if err != nil || onugem == nil || onugem.SerialNumber == "" {
yasin saplibddc2d72022-02-08 13:10:17 +0000900 logger.Errorf(ctx, "failed to get onuifo for intfid %d", rsrcMgr.PonIntfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530901 return err
902 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700903 if onugem.OnuID == onuID {
904 for _, gem := range onugem.GemPorts {
905 if gem == gemPort {
906 logger.Debugw(ctx, "Gem already present in onugem info, skpping addition", log.Fields{"gem": gem})
907 return nil
908 }
909 }
910 logger.Debugw(ctx, "Added gem to onugem info", log.Fields{"gem": gemPort})
911 onugem.GemPorts = append(onugem.GemPorts, gemPort)
912 } else {
yasin saplibddc2d72022-02-08 13:10:17 +0000913 logger.Errorw(ctx, "onu id in OnuGemInfo does not match", log.Fields{"onuID": onuID, "ponIf": rsrcMgr.PonIntfID, "onuGemInfoOnuID": onugem.OnuID})
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700914 return fmt.Errorf("onu-id-in-OnuGemInfo-does-not-match-%v", onuID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530915 }
916
yasin saplibddc2d72022-02-08 13:10:17 +0000917 err = rsrcMgr.AddOnuGemInfo(ctx, onuID, *onugem)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530918 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000919 logger.Error(ctx, "Failed to add onugem to kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530920 return err
921 }
922 return err
923}
924
Joey Armstrong3f0e2422023-07-05 18:25:41 -0400925// RemoveGemFromOnuGemInfo removes gemport from onugem info on kvstore and also local cache
yasin saplibddc2d72022-02-08 13:10:17 +0000926func (rsrcMgr *OpenOltResourceMgr) RemoveGemFromOnuGemInfo(ctx context.Context, onuID uint32, gemPort uint32) error {
927 onugem, err := rsrcMgr.GetOnuGemInfo(ctx, onuID)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700928 if err != nil || onugem == nil || onugem.SerialNumber == "" {
yasin saplibddc2d72022-02-08 13:10:17 +0000929 logger.Errorf(ctx, "failed to get onuifo for intfid %d", rsrcMgr.PonIntfID)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700930 return err
931 }
932 updated := false
933 if onugem.OnuID == onuID {
934 for i, gem := range onugem.GemPorts {
935 if gem == gemPort {
936 logger.Debugw(ctx, "Gem found, removing from onu gem info", log.Fields{"gem": gem})
937 onugem.GemPorts = append(onugem.GemPorts[:i], onugem.GemPorts[i+1:]...)
938 updated = true
939 break
940 }
941 }
942 } else {
yasin saplibddc2d72022-02-08 13:10:17 +0000943 logger.Errorw(ctx, "onu id in OnuGemInfo does not match", log.Fields{"onuID": onuID, "ponIf": rsrcMgr.PonIntfID, "onuGemInfoOnuID": onugem.OnuID})
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700944 return fmt.Errorf("onu-id-in-OnuGemInfo-does-not-match-%v", onuID)
945 }
946 if updated {
yasin saplibddc2d72022-02-08 13:10:17 +0000947 err = rsrcMgr.AddOnuGemInfo(ctx, onuID, *onugem)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700948 if err != nil {
949 logger.Error(ctx, "Failed to add onugem to kv store")
950 return err
951 }
952 } else {
953 logger.Debugw(ctx, "Gem port not found in onu gem info", log.Fields{"gem": gemPort})
954 }
955 return nil
956}
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530957
Joey Armstrong3f0e2422023-07-05 18:25:41 -0400958// GetOnuGemInfo gets onu gem info from the kvstore per interface
yasin saplibddc2d72022-02-08 13:10:17 +0000959func (rsrcMgr *OpenOltResourceMgr) GetOnuGemInfo(ctx context.Context, onuID uint32) (*OnuGemInfo, error) {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700960 var err error
961 var Val []byte
962 var onugem OnuGemInfo
963
yasin saplibddc2d72022-02-08 13:10:17 +0000964 path := fmt.Sprintf(OnuGemInfoPath, rsrcMgr.PonIntfID, onuID)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700965
966 rsrcMgr.onuGemInfoLock.RLock()
967 val, ok := rsrcMgr.onuGemInfo[path]
968 rsrcMgr.onuGemInfoLock.RUnlock()
969 if ok {
970 return val, nil
971 }
972 value, err := rsrcMgr.KVStore.Get(ctx, path)
973 if err != nil {
974 logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path})
975 return nil, err
976 } else if value == nil {
977 logger.Debug(ctx, "No onuinfo for path", log.Fields{"path": path})
978 return nil, nil // returning nil as this could happen if there are no onus for the interface yet
979 }
980 if Val, err = kvstore.ToByte(value.Value); err != nil {
981 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530982 return nil, err
983 }
984
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700985 if err = json.Unmarshal(Val, &onugem); err != nil {
986 logger.Error(ctx, "Failed to unmarshall")
987 return nil, err
988 }
989 logger.Debugw(ctx, "found onugem info from path", log.Fields{"path": path, "onuGemInfo": onugem})
990 rsrcMgr.onuGemInfoLock.Lock()
991 rsrcMgr.onuGemInfo[path] = &onugem
992 rsrcMgr.onuGemInfoLock.Unlock()
993
994 return &onugem, nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530995}
996
Joey Armstrong3f0e2422023-07-05 18:25:41 -0400997// AddNewOnuGemInfoToCacheAndKvStore function adds a new onu gem info to cache and kvstore
yasin saplibddc2d72022-02-08 13:10:17 +0000998func (rsrcMgr *OpenOltResourceMgr) AddNewOnuGemInfoToCacheAndKvStore(ctx context.Context, onuID uint32, serialNum string) error {
yasin saplibddc2d72022-02-08 13:10:17 +0000999 Path := fmt.Sprintf(OnuGemInfoPath, rsrcMgr.PonIntfID, onuID)
yasin sapli9e4c5092022-02-01 13:52:33 +00001000
1001 rsrcMgr.onuGemInfoLock.Lock()
1002 _, ok := rsrcMgr.onuGemInfo[Path]
1003 rsrcMgr.onuGemInfoLock.Unlock()
1004
1005 // If the ONU already exists in onuGemInfo list, nothing to do
1006 if ok {
1007 logger.Debugw(ctx, "onu-id-already-exists-in-cache", log.Fields{"onuID": onuID, "serialNum": serialNum})
1008 return nil
1009 }
1010
yasin saplibddc2d72022-02-08 13:10:17 +00001011 onuGemInfo := OnuGemInfo{OnuID: onuID, SerialNumber: serialNum, IntfID: rsrcMgr.PonIntfID}
yasin sapli9e4c5092022-02-01 13:52:33 +00001012
yasin saplibddc2d72022-02-08 13:10:17 +00001013 if err := rsrcMgr.AddOnuGemInfo(ctx, onuID, onuGemInfo); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +00001014 return err
1015 }
1016 logger.Infow(ctx, "added-onuinfo",
1017 log.Fields{
yasin saplibddc2d72022-02-08 13:10:17 +00001018 "intf-id": rsrcMgr.PonIntfID,
yasin sapli9e4c5092022-02-01 13:52:33 +00001019 "onu-id": onuID,
1020 "serial-num": serialNum,
1021 "onu": onuGemInfo,
1022 "device-id": rsrcMgr.DeviceID})
1023 return nil
1024}
1025
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001026// AddOnuGemInfo adds onu info on to the kvstore per interface
yasin saplibddc2d72022-02-08 13:10:17 +00001027func (rsrcMgr *OpenOltResourceMgr) AddOnuGemInfo(ctx context.Context, onuID uint32, onuGem OnuGemInfo) error {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001028 var Value []byte
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301029 var err error
yasin saplibddc2d72022-02-08 13:10:17 +00001030 Path := fmt.Sprintf(OnuGemInfoPath, rsrcMgr.PonIntfID, onuID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301031
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001032 Value, err = json.Marshal(onuGem)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301033 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001034 logger.Error(ctx, "failed to Marshal")
1035 return err
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301036 }
1037
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001038 if err = rsrcMgr.KVStore.Put(ctx, Path, Value); err != nil {
1039 logger.Errorf(ctx, "Failed to update resource %s", Path)
1040 return err
1041 }
yasin sapli9e4c5092022-02-01 13:52:33 +00001042 logger.Debugw(ctx, "added onu gem info to store", log.Fields{"onuGemInfo": onuGem, "Path": Path})
Gamze Abaka745ccb72021-11-18 11:29:58 +00001043
Akash Kankanala041a2122024-10-16 15:49:22 +05301044 // update cache
Gamze Abaka745ccb72021-11-18 11:29:58 +00001045 rsrcMgr.onuGemInfoLock.Lock()
1046 rsrcMgr.onuGemInfo[Path] = &onuGem
1047 rsrcMgr.onuGemInfoLock.Unlock()
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001048 return err
1049}
1050
1051// DelOnuGemInfo deletes the onugem info from kvstore per ONU
yasin saplibddc2d72022-02-08 13:10:17 +00001052func (rsrcMgr *OpenOltResourceMgr) DelOnuGemInfo(ctx context.Context, onuID uint32) error {
1053 path := fmt.Sprintf(OnuGemInfoPath, rsrcMgr.PonIntfID, onuID)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001054
1055 if err := rsrcMgr.KVStore.Delete(ctx, path); err != nil {
1056 logger.Errorf(ctx, "failed to remove resource %s", path)
1057 return err
1058 }
Gamze Abaka745ccb72021-11-18 11:29:58 +00001059
Akash Kankanala041a2122024-10-16 15:49:22 +05301060 // update cache
Gamze Abaka745ccb72021-11-18 11:29:58 +00001061 rsrcMgr.onuGemInfoLock.Lock()
1062 logger.Debugw(ctx, "removing onu gem info", log.Fields{"onuGemInfo": rsrcMgr.onuGemInfo[path]})
1063 delete(rsrcMgr.onuGemInfo, path)
1064 rsrcMgr.onuGemInfoLock.Unlock()
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001065 return nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301066}
1067
Joey Armstrong3f0e2422023-07-05 18:25:41 -04001068// DeleteAllOnuGemInfoForIntf deletes all the all onu gem info on the given pon interface
yasin saplibddc2d72022-02-08 13:10:17 +00001069func (rsrcMgr *OpenOltResourceMgr) DeleteAllOnuGemInfoForIntf(ctx context.Context) error {
yasin saplibddc2d72022-02-08 13:10:17 +00001070 path := fmt.Sprintf(OnuGemInfoPathPathPrefix, rsrcMgr.PonIntfID)
Girish Gowdra950326e2021-11-05 12:43:24 -07001071
yasin saplibddc2d72022-02-08 13:10:17 +00001072 logger.Debugw(ctx, "delete-all-onu-gem-info-for-pon-intf", log.Fields{"intfID": rsrcMgr.PonIntfID})
Girish Gowdra950326e2021-11-05 12:43:24 -07001073 if err := rsrcMgr.KVStore.DeleteWithPrefix(ctx, path); err != nil {
1074 logger.Errorf(ctx, "failed-to-remove-resource-%s", path)
1075 return err
1076 }
1077
1078 // Reset cache. Normally not necessary as the entire device is getting deleted when this API is invoked.
1079 rsrcMgr.onuGemInfoLock.Lock()
1080 rsrcMgr.onuGemInfo = make(map[string]*OnuGemInfo)
1081 rsrcMgr.onuGemInfoLock.Unlock()
1082 return nil
1083}
1084
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301085// AddUniPortToOnuInfo adds uni port to the onuinfo kvstore. check if the uni is already present if not update the kv store.
yasin saplibddc2d72022-02-08 13:10:17 +00001086func (rsrcMgr *OpenOltResourceMgr) AddUniPortToOnuInfo(ctx context.Context, onuID uint32, portNo uint32) {
yasin saplibddc2d72022-02-08 13:10:17 +00001087 onugem, err := rsrcMgr.GetOnuGemInfo(ctx, onuID)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001088 if err != nil || onugem == nil || onugem.SerialNumber == "" {
yasin saplibddc2d72022-02-08 13:10:17 +00001089 logger.Warnf(ctx, "failed to get onuifo for intfid %d", rsrcMgr.PonIntfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301090 return
1091 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001092
1093 if onugem.OnuID == onuID {
1094 for _, uni := range onugem.UniPorts {
1095 if uni == portNo {
1096 logger.Debugw(ctx, "uni already present in onugem info", log.Fields{"uni": portNo})
1097 return
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301098 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301099 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001100 onugem.UniPorts = append(onugem.UniPorts, portNo)
1101 } else {
yasin saplibddc2d72022-02-08 13:10:17 +00001102 logger.Warnw(ctx, "onu id mismatch in onu gem info", log.Fields{"intfID": rsrcMgr.PonIntfID, "onuID": onuID})
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001103 return
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301104 }
yasin saplibddc2d72022-02-08 13:10:17 +00001105 err = rsrcMgr.AddOnuGemInfo(ctx, onuID, *onugem)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301106 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001107 logger.Errorw(ctx, "Failed to add uni port in onugem to kv store", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301108 return
1109 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301110}
1111
Joey Armstrong3f0e2422023-07-05 18:25:41 -04001112// 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 -07001113func (rsrcMgr *OpenOltResourceMgr) UpdateGemPortForPktIn(ctx context.Context, pktIn PacketInInfoKey, gemPort uint32) {
Girish Gowdra950326e2021-11-05 12:43:24 -07001114 path := fmt.Sprintf(OnuPacketInPath, pktIn.IntfID, pktIn.OnuID, pktIn.LogicalPort, pktIn.VlanID, pktIn.Priority)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001115
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301116 Value, err := json.Marshal(gemPort)
1117 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001118 logger.Error(ctx, "Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301119 return
1120 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001121 if err = rsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001122 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"path": path, "value": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301123 return
1124 }
Gamze Abaka745ccb72021-11-18 11:29:58 +00001125
1126 // update cache
1127 rsrcMgr.gemPortForPacketInInfoLock.Lock()
1128 rsrcMgr.gemPortForPacketInInfo[path] = gemPort
1129 rsrcMgr.gemPortForPacketInInfoLock.Unlock()
Neha Sharma96b7bf22020-06-15 10:37:32 +00001130 logger.Debugw(ctx, "added gem packet in successfully", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301131}
1132
Esin Karaman7fb80c22020-07-16 14:23:33 +00001133// 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 -07001134func (rsrcMgr *OpenOltResourceMgr) GetGemPortFromOnuPktIn(ctx context.Context, packetInInfoKey PacketInInfoKey) (uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301135 var Val []byte
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301136
Girish Gowdra950326e2021-11-05 12:43:24 -07001137 path := fmt.Sprintf(OnuPacketInPath, packetInInfoKey.IntfID, packetInInfoKey.OnuID, packetInInfoKey.LogicalPort,
Esin Karaman7fb80c22020-07-16 14:23:33 +00001138 packetInInfoKey.VlanID, packetInInfoKey.Priority)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001139 // get from cache
1140 rsrcMgr.gemPortForPacketInInfoLock.RLock()
1141 gemPort, ok := rsrcMgr.gemPortForPacketInInfo[path]
1142 rsrcMgr.gemPortForPacketInInfoLock.RUnlock()
1143 if ok {
1144 logger.Debugw(ctx, "found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
1145 return gemPort, nil
1146 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301147
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001148 value, err := rsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301149 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001150 logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301151 return uint32(0), err
1152 } else if value == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001153 logger.Debugw(ctx, "No pkt in gem found", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301154 return uint32(0), nil
1155 }
1156
1157 if Val, err = kvstore.ToByte(value.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001158 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301159 return uint32(0), err
1160 }
1161 if err = json.Unmarshal(Val, &gemPort); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001162 logger.Error(ctx, "Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301163 return uint32(0), err
1164 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001165 logger.Debugw(ctx, "found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001166 // update cache
1167 rsrcMgr.gemPortForPacketInInfoLock.Lock()
1168 rsrcMgr.gemPortForPacketInInfo[path] = gemPort
1169 rsrcMgr.gemPortForPacketInInfoLock.Unlock()
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301170
1171 return gemPort, nil
1172}
1173
Joey Armstrong3f0e2422023-07-05 18:25:41 -04001174// DeletePacketInGemPortForOnu deletes the packet-in gemport for ONU
yasin saplibddc2d72022-02-08 13:10:17 +00001175func (rsrcMgr *OpenOltResourceMgr) DeletePacketInGemPortForOnu(ctx context.Context, onuID uint32, logicalPort uint32) error {
1176 path := fmt.Sprintf(OnuPacketInPathPrefix, rsrcMgr.PonIntfID, onuID, logicalPort)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001177 value, err := rsrcMgr.KVStore.List(ctx, path)
Esin Karaman7fb80c22020-07-16 14:23:33 +00001178 if err != nil {
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001179 logger.Errorf(ctx, "failed-to-read-value-from-path-%s", path)
1180 return errors.New("failed-to-read-value-from-path-" + path)
Esin Karaman7fb80c22020-07-16 14:23:33 +00001181 }
Esin Karaman7fb80c22020-07-16 14:23:33 +00001182
Gamze Abaka745ccb72021-11-18 11:29:58 +00001183 logger.Debugw(ctx, "delete-packetin-gem-port", log.Fields{"realPath": path})
1184 if err := rsrcMgr.KVStore.DeleteWithPrefix(ctx, path); err != nil {
1185 logger.Errorf(ctx, "failed-to-remove-resource-%s", path)
1186 return err
1187 }
1188
Akash Kankanala041a2122024-10-16 15:49:22 +05301189 // remove them one by one
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001190 for key := range value {
serkant.uluderya4f5ed592020-12-17 20:57:59 +03001191 // Remove the PathPrefix from the path on KV key.
Girish Gowdra950326e2021-11-05 12:43:24 -07001192 // gemPortForPacketInInfo cache uses OnuPacketInPath as the key
serkant.uluderya4f5ed592020-12-17 20:57:59 +03001193 stringToBeReplaced := rsrcMgr.KVStore.PathPrefix + "/"
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001194 replacedWith := ""
1195 key = strings.Replace(key, stringToBeReplaced, replacedWith, 1)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001196 // update cache
1197 rsrcMgr.gemPortForPacketInInfoLock.Lock()
1198 delete(rsrcMgr.gemPortForPacketInInfo, key)
1199 rsrcMgr.gemPortForPacketInInfoLock.Unlock()
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001200
serkant.uluderya4f5ed592020-12-17 20:57:59 +03001201 logger.Debugw(ctx, "removed-key-from-packetin-gem-port-cache", log.Fields{"key": key, "cache-len": len(rsrcMgr.gemPortForPacketInInfo)})
1202 }
1203
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301204 return nil
1205}
1206
Joey Armstrong3f0e2422023-07-05 18:25:41 -04001207// GetFlowIDsForGem gets the list of FlowIDs for the given gemport
yasin saplibddc2d72022-02-08 13:10:17 +00001208func (rsrcMgr *OpenOltResourceMgr) GetFlowIDsForGem(ctx context.Context, gem uint32) ([]uint64, error) {
1209 path := fmt.Sprintf(FlowIDsForGem, rsrcMgr.PonIntfID, gem)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001210
1211 // get from cache
1212 rsrcMgr.flowIDsForGemLock.RLock()
1213 flowIDs, ok := rsrcMgr.flowIDsForGem[gem]
1214 rsrcMgr.flowIDsForGemLock.RUnlock()
1215 if ok {
1216 return flowIDs, nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301217 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301218
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001219 value, err := rsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301220 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001221 logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path})
1222 return nil, err
yasin saplid0566272021-12-21 09:10:30 +00001223 } else if value == nil || value.Value == nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001224 logger.Debug(ctx, "no flow-ids found", log.Fields{"path": path})
1225 return nil, nil
1226 }
1227 Val, err := kvstore.ToByte(value.Value)
1228 if err != nil {
1229 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301230 return nil, err
1231 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301232
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001233 if err = json.Unmarshal(Val, &flowIDs); err != nil {
1234 logger.Error(ctx, "Failed to unmarshall")
1235 return nil, err
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301236 }
1237
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001238 // update cache
1239 rsrcMgr.flowIDsForGemLock.Lock()
1240 rsrcMgr.flowIDsForGem[gem] = flowIDs
1241 rsrcMgr.flowIDsForGemLock.Unlock()
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301242
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001243 return flowIDs, nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301244}
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301245
yasin sapli9e4c5092022-02-01 13:52:33 +00001246// IsGemPortUsedByAnotherFlow returns true if given gem is used by another flow
Girish Gowdrab4c33302022-03-18 15:07:38 -07001247func (rsrcMgr *OpenOltResourceMgr) IsGemPortUsedByAnotherFlow(ctx context.Context, gemPortID uint32, flowID uint64) (bool, error) {
1248 flowIDList, err := rsrcMgr.GetFlowIDsForGem(ctx, gemPortID)
1249 if err != nil {
1250 return false, err
1251 }
yasin sapli9e4c5092022-02-01 13:52:33 +00001252 for _, id := range flowIDList {
1253 if flowID != id {
Girish Gowdrab4c33302022-03-18 15:07:38 -07001254 return true, nil
yasin sapli9e4c5092022-02-01 13:52:33 +00001255 }
1256 }
Girish Gowdrab4c33302022-03-18 15:07:38 -07001257 return false, nil
yasin sapli9e4c5092022-02-01 13:52:33 +00001258}
1259
1260// RegisterFlowIDForGem updates both cache and KV store for flowIDsForGem map
yasin saplibddc2d72022-02-08 13:10:17 +00001261func (rsrcMgr *OpenOltResourceMgr) RegisterFlowIDForGem(ctx context.Context, gemPortID uint32, flowFromCore *ofp.OfpFlowStats) error {
Girish Gowdrab4c33302022-03-18 15:07:38 -07001262 flowIDs, err := rsrcMgr.GetFlowIDsForGem(ctx, gemPortID)
1263 if err != nil {
1264 return err
yasin sapli9e4c5092022-02-01 13:52:33 +00001265 }
Girish Gowdrab4c33302022-03-18 15:07:38 -07001266
1267 flowIDs = appendUnique64bit(flowIDs, flowFromCore.Id)
yasin sapli9e4c5092022-02-01 13:52:33 +00001268 // update the flowids for a gem to the KVstore
yasin saplibddc2d72022-02-08 13:10:17 +00001269 return rsrcMgr.UpdateFlowIDsForGem(ctx, gemPortID, flowIDs)
yasin sapli9e4c5092022-02-01 13:52:33 +00001270}
1271
Joey Armstrong3f0e2422023-07-05 18:25:41 -04001272// UpdateFlowIDsForGem updates flow id per gemport
yasin saplibddc2d72022-02-08 13:10:17 +00001273func (rsrcMgr *OpenOltResourceMgr) UpdateFlowIDsForGem(ctx context.Context, gem uint32, flowIDs []uint64) error {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301274 var val []byte
yasin saplibddc2d72022-02-08 13:10:17 +00001275 path := fmt.Sprintf(FlowIDsForGem, rsrcMgr.PonIntfID, gem)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301276
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001277 if flowIDs == nil {
Girish Gowdra950326e2021-11-05 12:43:24 -07001278 if err := rsrcMgr.KVStore.Delete(ctx, path); err != nil {
1279 logger.Errorw(ctx, "Failed to delete from kvstore", log.Fields{"err": err, "path": path})
1280 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001281 return nil
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301282 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001283 val, err := json.Marshal(flowIDs)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301284 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001285 logger.Error(ctx, "Failed to marshal data", log.Fields{"err": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301286 return err
1287 }
Girish Gowdrab77ded92020-04-08 11:45:05 -07001288
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001289 if err = rsrcMgr.KVStore.Put(ctx, path, val); err != nil {
1290 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"err": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301291 return err
1292 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001293 logger.Debugw(ctx, "added flowid list for gem to kv successfully", log.Fields{"path": path, "flowidlist": flowIDs})
Gamze Abaka745ccb72021-11-18 11:29:58 +00001294
1295 // update cache
1296 rsrcMgr.flowIDsForGemLock.Lock()
1297 rsrcMgr.flowIDsForGem[gem] = flowIDs
1298 rsrcMgr.flowIDsForGemLock.Unlock()
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301299 return nil
1300}
1301
Joey Armstrong3f0e2422023-07-05 18:25:41 -04001302// DeleteFlowIDsForGem deletes the flowID list entry per gem from kvstore.
yasin saplibddc2d72022-02-08 13:10:17 +00001303func (rsrcMgr *OpenOltResourceMgr) DeleteFlowIDsForGem(ctx context.Context, gem uint32) error {
1304 path := fmt.Sprintf(FlowIDsForGem, rsrcMgr.PonIntfID, gem)
Gamze Abaka745ccb72021-11-18 11:29:58 +00001305 if err := rsrcMgr.KVStore.Delete(ctx, path); err != nil {
1306 logger.Errorw(ctx, "Failed to delete from kvstore", log.Fields{"err": err, "path": path})
1307 return err
1308 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001309 // update cache
1310 rsrcMgr.flowIDsForGemLock.Lock()
1311 delete(rsrcMgr.flowIDsForGem, gem)
1312 rsrcMgr.flowIDsForGemLock.Unlock()
Gamze Abaka745ccb72021-11-18 11:29:58 +00001313 return nil
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301314}
Esin Karamanccb714b2019-11-29 15:02:06 +00001315
Joey Armstrong3f0e2422023-07-05 18:25:41 -04001316// DeleteAllFlowIDsForGemForIntf deletes all the flow ids associated for all the gems on the given pon interface
yasin saplibddc2d72022-02-08 13:10:17 +00001317func (rsrcMgr *OpenOltResourceMgr) DeleteAllFlowIDsForGemForIntf(ctx context.Context) error {
1318 path := fmt.Sprintf(FlowIDsForGemPathPrefix, rsrcMgr.PonIntfID)
Girish Gowdra950326e2021-11-05 12:43:24 -07001319
yasin saplibddc2d72022-02-08 13:10:17 +00001320 logger.Debugw(ctx, "delete-flow-ids-for-gem-for-pon-intf", log.Fields{"intfID": rsrcMgr.PonIntfID})
Girish Gowdra950326e2021-11-05 12:43:24 -07001321 if err := rsrcMgr.KVStore.DeleteWithPrefix(ctx, path); err != nil {
1322 logger.Errorf(ctx, "failed-to-remove-resource-%s", path)
1323 return err
1324 }
1325
1326 // Reset cache. Normally not necessary as the entire device is getting deleted when this API is invoked.
1327 rsrcMgr.flowIDsForGemLock.Lock()
1328 rsrcMgr.flowIDsForGem = make(map[uint32][]uint64)
1329 rsrcMgr.flowIDsForGemLock.Unlock()
1330 return nil
1331}
1332
Joey Armstrong3f0e2422023-07-05 18:25:41 -04001333// GetMcastQueuePerInterfaceMap gets multicast queue info per pon interface
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001334func (rsrcMgr *OpenOltResourceMgr) GetMcastQueuePerInterfaceMap(ctx context.Context) (map[uint32][]uint32, error) {
Kent Hagermane6ff1012020-07-14 15:07:53 -04001335 path := McastQueuesForIntf
Esin Karamanccb714b2019-11-29 15:02:06 +00001336 var val []byte
1337
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001338 rsrcMgr.mcastQueueForIntfLock.RLock()
1339 if rsrcMgr.mcastQueueForIntfLoadedFromKvStore {
1340 rsrcMgr.mcastQueueForIntfLock.RUnlock()
1341 return rsrcMgr.mcastQueueForIntf, nil
1342 }
1343 rsrcMgr.mcastQueueForIntfLock.RUnlock()
1344
1345 kvPair, err := rsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001346 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001347 logger.Error(ctx, "failed to get data from kv store")
Esin Karamanccb714b2019-11-29 15:02:06 +00001348 return nil, err
1349 }
1350 if kvPair != nil && kvPair.Value != nil {
1351 if val, err = kvstore.ToByte(kvPair.Value); err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001352 logger.Error(ctx, "Failed to convert to byte array ", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001353 return nil, err
1354 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001355 rsrcMgr.mcastQueueForIntfLock.Lock()
1356 defer rsrcMgr.mcastQueueForIntfLock.Unlock()
1357 if err = json.Unmarshal(val, &rsrcMgr.mcastQueueForIntf); err != nil {
1358 logger.Error(ctx, "Failed to unmarshall ", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001359 return nil, err
1360 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001361 rsrcMgr.mcastQueueForIntfLoadedFromKvStore = true
Esin Karamanccb714b2019-11-29 15:02:06 +00001362 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001363 return rsrcMgr.mcastQueueForIntf, nil
Esin Karamanccb714b2019-11-29 15:02:06 +00001364}
1365
Joey Armstrong3f0e2422023-07-05 18:25:41 -04001366// AddMcastQueueForIntf adds multicast queue for pon interface
yasin saplibddc2d72022-02-08 13:10:17 +00001367func (rsrcMgr *OpenOltResourceMgr) AddMcastQueueForIntf(ctx context.Context, gem uint32, servicePriority uint32) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001368 var val []byte
Kent Hagermane6ff1012020-07-14 15:07:53 -04001369 path := McastQueuesForIntf
Esin Karamanccb714b2019-11-29 15:02:06 +00001370
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001371 // Load local cache from kv store the first time
1372 rsrcMgr.mcastQueueForIntfLock.RLock()
1373 if !rsrcMgr.mcastQueueForIntfLoadedFromKvStore {
1374 rsrcMgr.mcastQueueForIntfLock.RUnlock()
1375 _, err := rsrcMgr.GetMcastQueuePerInterfaceMap(ctx)
1376 if err != nil {
yasin saplibddc2d72022-02-08 13:10:17 +00001377 logger.Errorw(ctx, "Failed to get multicast queue info for interface", log.Fields{"err": err, "intf": rsrcMgr.PonIntfID})
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001378 return err
1379 }
1380 } else {
1381 rsrcMgr.mcastQueueForIntfLock.RUnlock()
1382 }
1383
1384 // Update KV store
1385 rsrcMgr.mcastQueueForIntfLock.Lock()
yasin saplibddc2d72022-02-08 13:10:17 +00001386 rsrcMgr.mcastQueueForIntf[rsrcMgr.PonIntfID] = []uint32{gem, servicePriority}
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001387 val, err := json.Marshal(rsrcMgr.mcastQueueForIntf)
Esin Karamanccb714b2019-11-29 15:02:06 +00001388 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001389 rsrcMgr.mcastQueueForIntfLock.Unlock()
1390 logger.Errorw(ctx, "Failed to marshal data", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001391 return err
1392 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001393 rsrcMgr.mcastQueueForIntfLock.Unlock()
1394
1395 if err = rsrcMgr.KVStore.Put(ctx, path, val); err != nil {
1396 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"err": err, "path": path, "value": val})
Esin Karamanccb714b2019-11-29 15:02:06 +00001397 return err
1398 }
yasin saplibddc2d72022-02-08 13:10:17 +00001399 logger.Debugw(ctx, "added multicast queue info to KV store successfully", log.Fields{"path": path, "interfaceId": rsrcMgr.PonIntfID, "gem": gem, "svcPrior": servicePriority})
Esin Karamanccb714b2019-11-29 15:02:06 +00001400 return nil
1401}
1402
Joey Armstrong3f0e2422023-07-05 18:25:41 -04001403// DeleteMcastQueueForIntf deletes multicast queue info for the current pon interface from kvstore
Gustavo Silva41af9122022-10-11 11:05:13 -03001404func (rsrcMgr *OpenOltResourceMgr) DeleteMcastQueueForIntf(ctx context.Context) error {
Girish Gowdraf3728b12022-02-02 21:46:51 -08001405 path := McastQueuesForIntf
1406
1407 if err := rsrcMgr.KVStore.Delete(ctx, path); err != nil {
1408 logger.Errorw(ctx, "Failed to delete multicast queue info from kvstore", log.Fields{"err": err, "interfaceId": rsrcMgr.PonIntfID})
Gustavo Silva41af9122022-10-11 11:05:13 -03001409 return err
Girish Gowdraf3728b12022-02-02 21:46:51 -08001410 }
1411 logger.Debugw(ctx, "deleted multicast queue info from KV store successfully", log.Fields{"interfaceId": rsrcMgr.PonIntfID})
Gustavo Silva41af9122022-10-11 11:05:13 -03001412 return nil
Girish Gowdraf3728b12022-02-02 21:46:51 -08001413}
1414
Joey Armstrong3f0e2422023-07-05 18:25:41 -04001415// AddFlowGroupToKVStore adds flow group into KV store
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001416func (rsrcMgr *OpenOltResourceMgr) AddFlowGroupToKVStore(ctx context.Context, groupEntry *ofp.OfpGroupEntry, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001417 var Value []byte
1418 var err error
1419 var path string
1420 if cached {
1421 path = fmt.Sprintf(FlowGroupCached, groupEntry.Desc.GroupId)
1422 } else {
1423 path = fmt.Sprintf(FlowGroup, groupEntry.Desc.GroupId)
1424 }
Akash Kankanala041a2122024-10-16 15:49:22 +05301425 // build group info object
Esin Karamanccb714b2019-11-29 15:02:06 +00001426 var outPorts []uint32
1427 for _, ofBucket := range groupEntry.Desc.Buckets {
1428 for _, ofAction := range ofBucket.Actions {
1429 if ofAction.Type == ofp.OfpActionType_OFPAT_OUTPUT {
1430 outPorts = append(outPorts, ofAction.GetOutput().Port)
1431 }
1432 }
1433 }
1434 groupInfo := GroupInfo{
1435 GroupID: groupEntry.Desc.GroupId,
1436 OutPorts: outPorts,
1437 }
1438
1439 Value, err = json.Marshal(groupInfo)
1440
1441 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001442 logger.Error(ctx, "failed to Marshal flow group object")
Esin Karamanccb714b2019-11-29 15:02:06 +00001443 return err
1444 }
1445
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001446 if err = rsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001447 logger.Errorf(ctx, "Failed to update resource %s", path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001448 return err
1449 }
Gamze Abaka745ccb72021-11-18 11:29:58 +00001450
1451 // update cache
1452 rsrcMgr.groupInfoLock.Lock()
1453 rsrcMgr.groupInfo[path] = &groupInfo
1454 rsrcMgr.groupInfoLock.Unlock()
Esin Karamanccb714b2019-11-29 15:02:06 +00001455 return nil
1456}
1457
Joey Armstrong3f0e2422023-07-05 18:25:41 -04001458// RemoveFlowGroupFromKVStore removes flow group from KV store
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001459func (rsrcMgr *OpenOltResourceMgr) RemoveFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001460 var path string
1461 if cached {
1462 path = fmt.Sprintf(FlowGroupCached, groupID)
1463 } else {
1464 path = fmt.Sprintf(FlowGroup, groupID)
1465 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001466
1467 if err := rsrcMgr.KVStore.Delete(ctx, path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001468 logger.Errorf(ctx, "Failed to remove resource %s due to %s", path, err)
Esin Karamand519bbf2020-07-01 11:16:03 +00001469 return err
Esin Karamanccb714b2019-11-29 15:02:06 +00001470 }
Gamze Abaka745ccb72021-11-18 11:29:58 +00001471
1472 // update cache
1473 rsrcMgr.groupInfoLock.Lock()
1474 delete(rsrcMgr.groupInfo, path)
1475 rsrcMgr.groupInfoLock.Unlock()
Esin Karamand519bbf2020-07-01 11:16:03 +00001476 return nil
Esin Karamanccb714b2019-11-29 15:02:06 +00001477}
1478
Joey Armstrong3f0e2422023-07-05 18:25:41 -04001479// GetFlowGroupFromKVStore fetches flow group from the KV store. Returns (false, {} error) if any problem occurs during
1480// fetching the data. Returns (true, groupInfo, nil) if the group is fetched successfully.
Esin Karamanccb714b2019-11-29 15:02:06 +00001481// Returns (false, {}, nil) if the group does not exists in the KV store.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001482func (rsrcMgr *OpenOltResourceMgr) GetFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) (bool, GroupInfo, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001483 var groupInfo GroupInfo
1484 var path string
1485 if cached {
1486 path = fmt.Sprintf(FlowGroupCached, groupID)
1487 } else {
1488 path = fmt.Sprintf(FlowGroup, groupID)
1489 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001490
1491 // read from cache
1492 rsrcMgr.groupInfoLock.RLock()
1493 gi, ok := rsrcMgr.groupInfo[path]
1494 rsrcMgr.groupInfoLock.RUnlock()
1495 if ok {
1496 return true, *gi, nil
1497 }
1498
1499 kvPair, err := rsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001500 if err != nil {
1501 return false, groupInfo, err
1502 }
1503 if kvPair != nil && kvPair.Value != nil {
1504 Val, err := kvstore.ToByte(kvPair.Value)
1505 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001506 logger.Errorw(ctx, "Failed to convert flow group into byte array", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001507 return false, groupInfo, err
1508 }
1509 if err = json.Unmarshal(Val, &groupInfo); err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001510 logger.Errorw(ctx, "Failed to unmarshal", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001511 return false, groupInfo, err
1512 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001513 // update cache
1514 rsrcMgr.groupInfoLock.Lock()
1515 rsrcMgr.groupInfo[path] = &groupInfo
1516 rsrcMgr.groupInfoLock.Unlock()
1517
Esin Karamanccb714b2019-11-29 15:02:06 +00001518 return true, groupInfo, nil
1519 }
1520 return false, groupInfo, nil
1521}
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001522
yasin sapli9e4c5092022-02-01 13:52:33 +00001523// GetOnuGemInfoList returns all gems in the onuGemInfo map
1524func (rsrcMgr *OpenOltResourceMgr) GetOnuGemInfoList(ctx context.Context) []OnuGemInfo {
1525 var onuGemInfoLst []OnuGemInfo
1526 rsrcMgr.onuGemInfoLock.RLock()
1527 defer rsrcMgr.onuGemInfoLock.RUnlock()
1528 for _, v := range rsrcMgr.onuGemInfo {
1529 onuGemInfoLst = append(onuGemInfoLst, *v)
1530 }
1531 return onuGemInfoLst
1532}
1533
yasin sapli9e4c5092022-02-01 13:52:33 +00001534func appendUnique64bit(slice []uint64, item uint64) []uint64 {
1535 for _, sliceElement := range slice {
1536 if sliceElement == item {
1537 return slice
1538 }
1539 }
1540 return append(slice, item)
1541}