blob: f47ccf1f39f64a818c48ce53bb94d26ef4eac08c [file] [log] [blame]
Abhilash S.L7f17e402019-03-15 17:40:41 +05301/*
2 * Copyright 2019-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070017//Package resourcemanager provides the utility for managing resources
manikkaraj kbf256be2019-03-25 00:13:48 +053018package resourcemanager
Abhilash S.L7f17e402019-03-15 17:40:41 +053019
20import (
npujarec5762e2020-01-01 14:08:48 +053021 "context"
Girish Gowdru0c588b22019-04-23 23:24:56 -040022 "encoding/json"
23 "errors"
24 "fmt"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070025 "strings"
Girish Gowdra38d533d2020-03-30 20:38:51 -070026 "sync"
Neha Sharmacc656962020-04-14 14:26:11 +000027 "time"
Abhilash S.L7f17e402019-03-15 17:40:41 +053028
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"
Gamze Abakafee36392019-10-03 11:17:24 +000046 //MeterIDPathSuffix - <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
47 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
56 //FlowIDsForGemPathPrefix format: flowids_for_gem/<intfid>
57 FlowIDsForGemPathPrefix = "flowids_per_gem/{%d}"
58 //FlowIDsForGem flowids_for_gem/<intfid>/<gemport-id>
59 FlowIDsForGem = FlowIDsForGemPathPrefix + "/{%d}"
60
Esin Karamanccb714b2019-11-29 15:02:06 +000061 //McastQueuesForIntf multicast queues for pon interfaces
62 McastQueuesForIntf = "mcast_qs_for_int"
63 //FlowGroup flow_groups/<flow_group_id>
64 // 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}"
67 //FlowGroupCached flow_groups_cached/<flow_group_id>
68 // 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
76 //FlowIDPath - Path on the KV store for storing list of Flow IDs for a given subscriber
77 //Format: BasePathKvStore/<(pon_intf_id, onu_id, uni_id)>/flow_ids
78 FlowIDPath = "{%s}/flow_ids"
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -070079
Girish Gowdra950326e2021-11-05 12:43:24 -070080 //OnuGemInfoPathPathPrefix format: onu_gem_info/<intfid>
81 OnuGemInfoPathPathPrefix = "onu_gem_info/{%d}"
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -070082 //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}"
salmansiddiqui7ac62132019-08-22 03:58:50 +000085)
Abhilash S.L7f17e402019-03-15 17:40:41 +053086
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070087// FlowInfo holds the flow information
Abhilash S.L8ee90712019-04-29 16:24:22 +053088type FlowInfo struct {
Girish Gowdraa09aeab2020-09-14 16:30:52 -070089 Flow *openolt.Flow
90 IsSymmtricFlow bool
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053091}
92
93// OnuGemInfo holds onu information along with gem port list and uni port list
94type OnuGemInfo struct {
95 OnuID uint32
96 SerialNumber string
97 IntfID uint32
98 GemPorts []uint32
99 UniPorts []uint32
100}
101
102// PacketInInfoKey is the key for packet in gemport
103type PacketInInfoKey struct {
104 IntfID uint32
105 OnuID uint32
106 LogicalPort uint32
Esin Karaman7fb80c22020-07-16 14:23:33 +0000107 VlanID uint16
108 Priority uint8
Abhilash S.L8ee90712019-04-29 16:24:22 +0530109}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700110
Esin Karamanccb714b2019-11-29 15:02:06 +0000111// GroupInfo holds group information
112type GroupInfo struct {
113 GroupID uint32
114 OutPorts []uint32
115}
116
Girish Gowdraa482f272021-03-24 23:04:19 -0700117// MeterInfo store meter information at path <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
118type MeterInfo struct {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700119 RefCnt uint8 // number of flow references for this meter. When RefCnt is 0, the MeterInfo should be deleted.
120 MeterID uint32
Girish Gowdraa482f272021-03-24 23:04:19 -0700121}
122
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700123// OpenOltResourceMgr holds resource related information as provided below for each field
Abhilash S.L7f17e402019-03-15 17:40:41 +0530124type OpenOltResourceMgr struct {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700125 PonIntfID uint32
Neha Sharma3f221ae2020-04-29 19:02:12 +0000126 DeviceID string // OLT device id
127 Address string // Host and port of the kv store to connect to
128 Args string // args
129 KVStore *db.Backend // backend kv store connection handle
130 DeviceType string
131 DevInfo *openolt.DeviceInfo // device information
Girish Gowdru0c588b22019-04-23 23:24:56 -0400132 // array of pon resource managers per interface technology
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700133 PonRsrMgr *ponrmgr.PONResourceManager
Girish Gowdra38d533d2020-03-30 20:38:51 -0700134
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700135 // Local maps used for write-through-cache - start
136 flowIDsForOnu map[string][]uint64
137 flowIDsForOnuLock sync.RWMutex
138
139 allocIDsForOnu map[string][]uint32
140 allocIDsForOnuLock sync.RWMutex
141
142 gemPortIDsForOnu map[string][]uint32
143 gemPortIDsForOnuLock sync.RWMutex
144
145 techProfileIDsForOnu map[string][]uint32
146 techProfileIDsForOnuLock sync.RWMutex
147
148 meterInfoForOnu map[string]*MeterInfo
149 meterInfoForOnuLock sync.RWMutex
150
151 onuGemInfo map[string]*OnuGemInfo
152 onuGemInfoLock sync.RWMutex
153
154 gemPortForPacketInInfo map[string]uint32
155 gemPortForPacketInInfoLock sync.RWMutex
156
157 flowIDsForGem map[uint32][]uint64
158 flowIDsForGemLock sync.RWMutex
159
160 mcastQueueForIntf map[uint32][]uint32
161 mcastQueueForIntfLock sync.RWMutex
162 mcastQueueForIntfLoadedFromKvStore bool
163
164 groupInfo map[string]*GroupInfo
165 groupInfoLock sync.RWMutex
166 // Local maps used for write-through-cache - end
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700167
Girish Gowdra76a1b092021-07-28 10:07:04 -0700168 TechprofileRef tp.TechProfileIf
Abhilash S.L7f17e402019-03-15 17:40:41 +0530169}
170
Neha Sharma96b7bf22020-06-15 10:37:32 +0000171func newKVClient(ctx context.Context, storeType string, address string, timeout time.Duration) (kvstore.Client, error) {
172 logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400173 switch storeType {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400174 case "etcd":
Neha Sharma96b7bf22020-06-15 10:37:32 +0000175 return kvstore.NewEtcdClient(ctx, address, timeout, log.FatalLevel)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400176 }
177 return nil, errors.New("unsupported-kv-store")
Abhilash S.L7f17e402019-03-15 17:40:41 +0530178}
179
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700180// SetKVClient sets the KV client and return a kv backend
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800181func SetKVClient(ctx context.Context, backend string, addr string, DeviceID string, basePathKvStore string) *db.Backend {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400182 // TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
183 // issue between kv store and backend , core is not calling NewBackend directly
Neha Sharma96b7bf22020-06-15 10:37:32 +0000184 kvClient, err := newKVClient(ctx, backend, addr, KvstoreTimeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400185 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000186 logger.Fatalw(ctx, "Failed to init KV client\n", log.Fields{"err": err})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400187 return nil
188 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700189 // return db.NewBackend(ctx, backend, addr, KvstoreTimeout, fmt.Sprintf(BasePathKvStore, basePathKvStore, DeviceID))
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700190
sbarbaria8910ba2019-11-05 10:12:23 -0500191 kvbackend := &db.Backend{
Girish Gowdru0c588b22019-04-23 23:24:56 -0400192 Client: kvClient,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700193 StoreType: backend,
Neha Sharma3f221ae2020-04-29 19:02:12 +0000194 Address: addr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700195 Timeout: KvstoreTimeout,
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800196 PathPrefix: fmt.Sprintf(BasePathKvStore, basePathKvStore, DeviceID)}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530197
Girish Gowdru0c588b22019-04-23 23:24:56 -0400198 return kvbackend
Abhilash S.L7f17e402019-03-15 17:40:41 +0530199}
200
Gamze Abakafee36392019-10-03 11:17:24 +0000201// NewResourceMgr init a New resource manager instance which in turn instantiates pon resource manager
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700202// instances according to technology. Initializes the default resource ranges for all
203// the resources.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700204func 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 -0400205 var ResourceMgr OpenOltResourceMgr
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700206 logger.Debugf(ctx, "Init new resource manager , ponIf: %v, address: %s, device-id: %s", PonIntfID, KVStoreAddress, deviceID)
207 ResourceMgr.PonIntfID = PonIntfID
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700208 ResourceMgr.DeviceID = deviceID
Neha Sharma3f221ae2020-04-29 19:02:12 +0000209 ResourceMgr.Address = KVStoreAddress
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700210 ResourceMgr.DeviceType = deviceType
211 ResourceMgr.DevInfo = devInfo
Abhilash S.L7f17e402019-03-15 17:40:41 +0530212
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700213 Backend := kvStoreType
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800214 ResourceMgr.KVStore = SetKVClient(ctx, Backend, ResourceMgr.Address, deviceID, basePathKvStore)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400215 if ResourceMgr.KVStore == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000216 logger.Error(ctx, "Failed to setup KV store")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400217 }
Girish Gowdra38d533d2020-03-30 20:38:51 -0700218
Girish Gowdru0c588b22019-04-23 23:24:56 -0400219 // TODO self.args = registry('main').get_args()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530220
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700221 // Create a separate Resource Manager instance for each range. This assumes that
Girish Gowdru0c588b22019-04-23 23:24:56 -0400222 // each technology is represented by only a single range
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700223 for _, TechRange := range devInfo.Ranges {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700224 for _, intfID := range TechRange.IntfIds {
225 if intfID == PonIntfID {
226 technology := TechRange.Technology
227 logger.Debugf(ctx, "Device info technology %s, intf-id %v", technology, PonIntfID)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000228
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700229 rsrMgr, err := ponrmgr.NewPONResourceManager(ctx, technology, deviceType, deviceID,
230 Backend, ResourceMgr.Address, basePathKvStore)
231 if err != nil {
232 logger.Errorf(ctx, "Failed to create pon resource manager instance for technology %s", technology)
233 return nil
234 }
235 ResourceMgr.PonRsrMgr = rsrMgr
236 // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
237 InitializeDeviceResourceRangeAndPool(ctx, rsrMgr, TechRange, devInfo)
238 if err := ResourceMgr.PonRsrMgr.InitDeviceResourcePoolForIntf(ctx, intfID); err != nil {
239 logger.Fatal(ctx, "failed-to-initialize-device-resource-pool-intf-id-%v-device-id", ResourceMgr.PonIntfID, ResourceMgr.DeviceID)
240 return nil
241 }
242 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400243 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400244 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700245
246 ResourceMgr.InitLocalCache()
247
Neha Sharma96b7bf22020-06-15 10:37:32 +0000248 logger.Info(ctx, "Initialization of resource manager success!")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400249 return &ResourceMgr
Abhilash S.L7f17e402019-03-15 17:40:41 +0530250}
251
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700252//InitLocalCache initializes local maps used for write-through-cache
253func (rsrcMgr *OpenOltResourceMgr) InitLocalCache() {
254 rsrcMgr.flowIDsForOnu = make(map[string][]uint64)
255 rsrcMgr.allocIDsForOnu = make(map[string][]uint32)
256 rsrcMgr.gemPortIDsForOnu = make(map[string][]uint32)
257 rsrcMgr.techProfileIDsForOnu = make(map[string][]uint32)
258 rsrcMgr.meterInfoForOnu = make(map[string]*MeterInfo)
259 rsrcMgr.onuGemInfo = make(map[string]*OnuGemInfo)
260 rsrcMgr.gemPortForPacketInInfo = make(map[string]uint32)
261 rsrcMgr.flowIDsForGem = make(map[uint32][]uint64)
262 rsrcMgr.mcastQueueForIntf = make(map[uint32][]uint32)
263 rsrcMgr.groupInfo = make(map[string]*GroupInfo)
264}
265
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700266// InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply
267// device specific information. If KV doesn't exist
268// or is broader than the device, the device's information will
269// dictate the range limits
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700270func InitializeDeviceResourceRangeAndPool(ctx context.Context, ponRMgr *ponrmgr.PONResourceManager,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700271 techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700272 // var ONUIDShared, AllocIDShared, GEMPortIDShared openolt.DeviceInfo_DeviceResourceRanges_Pool_SharingType
273 var ONUIDStart, ONUIDEnd, AllocIDStart, AllocIDEnd, GEMPortIDStart, GEMPortIDEnd uint32
274 var ONUIDShared, AllocIDShared, GEMPortIDShared, FlowIDShared uint32
275
276 // The below variables are just dummy and needed to pass as arguments to InitDefaultPONResourceRanges function.
277 // The openolt adapter does not need flowIDs to be managed as it is managed on the OLT device
278 // The UNI IDs are dynamically generated by openonu adapter for every discovered UNI.
279 var flowIDDummyStart, flowIDDummyEnd uint32 = 1, 2
280 var uniIDDummyStart, uniIDDummyEnd uint32 = 0, 1
Abhilash S.L7f17e402019-03-15 17:40:41 +0530281
Girish Gowdru0c588b22019-04-23 23:24:56 -0400282 // init the resource range pool according to the sharing type
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700283 logger.Debugw(ctx, "Device info init", log.Fields{"technology": techRange.Technology,
284 "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd,
285 "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd,
286 "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd,
287 "intf_ids": techRange.IntfIds,
288 })
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700289 for _, RangePool := range techRange.Pools {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700290 // FIXME: Remove hardcoding
Girish Gowdru0c588b22019-04-23 23:24:56 -0400291 if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID {
292 ONUIDStart = RangePool.Start
293 ONUIDEnd = RangePool.End
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700294 ONUIDShared = uint32(RangePool.Sharing)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400295 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID {
296 AllocIDStart = RangePool.Start
297 AllocIDEnd = RangePool.End
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700298 AllocIDShared = uint32(RangePool.Sharing)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400299 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID {
300 GEMPortIDStart = RangePool.Start
301 GEMPortIDEnd = RangePool.End
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700302 GEMPortIDShared = uint32(RangePool.Sharing)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400303 }
304 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530305
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700306 ponRMgr.InitDefaultPONResourceRanges(ctx, ONUIDStart, ONUIDEnd, ONUIDShared,
307 AllocIDStart, AllocIDEnd, AllocIDShared,
308 GEMPortIDStart, GEMPortIDEnd, GEMPortIDShared,
309 flowIDDummyStart, flowIDDummyEnd, FlowIDShared, uniIDDummyStart, uniIDDummyEnd,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700310 devInfo.PonPorts, techRange.IntfIds)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530311
Abhilash S.L7f17e402019-03-15 17:40:41 +0530312}
313
Devmalya Paul495b94a2019-08-27 19:42:00 -0400314// Delete clears used resources for the particular olt device being deleted
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700315func (rsrcMgr *OpenOltResourceMgr) Delete(ctx context.Context, intfID uint32) error {
316 if err := rsrcMgr.PonRsrMgr.ClearDeviceResourcePoolForIntf(ctx, intfID); err != nil {
317 logger.Debug(ctx, "Failed to clear device resource pool")
318 return err
Devmalya Paul495b94a2019-08-27 19:42:00 -0400319 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000320 logger.Debug(ctx, "Cleared device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400321 return nil
322}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530323
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700324// GetONUID returns the available onuID for the given pon-port
325func (rsrcMgr *OpenOltResourceMgr) GetONUID(ctx context.Context, PonIntfID uint32) (uint32, error) {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400326 // Get ONU id for a provided pon interface ID.
Girish Gowdra76a1b092021-07-28 10:07:04 -0700327 onuID, err := rsrcMgr.TechprofileRef.GetResourceID(ctx, PonIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400328 ponrmgr.ONU_ID, 1)
329 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000330 logger.Errorf(ctx, "Failed to get resource for interface %d for type %s",
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700331 PonIntfID, ponrmgr.ONU_ID)
cbabuabf02352019-10-15 13:14:56 +0200332 return 0, err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400333 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700334 if len(onuID) > 0 {
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700335 return onuID[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400336 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530337
Girish Gowdra950326e2021-11-05 12:43:24 -0700338 return 0, fmt.Errorf("no-onu-id-allocated")
Abhilash S.L8ee90712019-04-29 16:24:22 +0530339}
340
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700341// GetCurrentFlowIDsForOnu fetches flow ID from the resource manager
342// Note: For flows which trap from the NNI and not really associated with any particular
343// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700344func (rsrcMgr *OpenOltResourceMgr) GetCurrentFlowIDsForOnu(ctx context.Context, PonIntfID uint32, onuID int32, uniID int32) ([]uint64, error) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700345
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700346 subs := fmt.Sprintf("%d,%d,%d", PonIntfID, onuID, uniID)
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700347 path := fmt.Sprintf(FlowIDPath, subs)
348
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700349 // fetch from cache
350 rsrcMgr.flowIDsForOnuLock.RLock()
351 flowIDsForOnu, ok := rsrcMgr.flowIDsForOnu[path]
352 rsrcMgr.flowIDsForOnuLock.RUnlock()
353
354 if ok {
355 return flowIDsForOnu, nil
356 }
357
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700358 var data []uint64
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700359 value, err := rsrcMgr.KVStore.Get(ctx, path)
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700360 if err == nil {
361 if value != nil {
362 Val, _ := toByte(value.Value)
363 if err = json.Unmarshal(Val, &data); err != nil {
364 logger.Error(ctx, "Failed to unmarshal")
365 return nil, err
366 }
367 }
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700368 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700369 // update cache
370 rsrcMgr.flowIDsForOnuLock.Lock()
371 rsrcMgr.flowIDsForOnu[path] = data
372 rsrcMgr.flowIDsForOnuLock.Unlock()
373
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700374 return data, nil
Abhilash S.L8ee90712019-04-29 16:24:22 +0530375}
376
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700377// UpdateAllocIdsForOnu updates alloc ids in kv store for a given pon interface id, onu id and uni id
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700378func (rsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ctx context.Context, ponPort uint32, onuID uint32, uniID uint32, allocIDs []uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530379
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700380 intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
381 // update cache
382 rsrcMgr.allocIDsForOnuLock.Lock()
383 rsrcMgr.allocIDsForOnu[intfOnuIDuniID] = allocIDs
384 rsrcMgr.allocIDsForOnuLock.Unlock()
385
386 // Note: in case the write to DB fails there could be inconsistent data between cache and db.
387 // Although this is highly unlikely with DB retries in place, this is something we have to deal with in the next release
388 return rsrcMgr.PonRsrMgr.UpdateAllocIdsForOnu(ctx, intfOnuIDuniID,
389 allocIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530390}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700391
392// GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700393func (rsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(ctx context.Context, intfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700394 uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530395
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700396 intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530397
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700398 // fetch from cache
399 rsrcMgr.gemPortIDsForOnuLock.RLock()
400 gemIDs, ok := rsrcMgr.gemPortIDsForOnu[intfOnuIDuniID]
401 rsrcMgr.gemPortIDsForOnuLock.RUnlock()
402 if ok {
403 return gemIDs
404 }
405 /* Get gem ports for given pon interface , onu id and uni id. */
406 gemIDs = rsrcMgr.PonRsrMgr.GetCurrentGEMPortIDsForOnu(ctx, intfOnuIDuniID)
407
408 // update cache
409 rsrcMgr.gemPortIDsForOnuLock.Lock()
410 rsrcMgr.gemPortIDsForOnu[intfOnuIDuniID] = gemIDs
411 rsrcMgr.gemPortIDsForOnuLock.Unlock()
412
413 return gemIDs
Abhilash S.L7f17e402019-03-15 17:40:41 +0530414}
415
Gamze Abakafee36392019-10-03 11:17:24 +0000416// GetCurrentAllocIDsForOnu returns alloc ids for given pon interface and onu id
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700417func (rsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDsForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530418
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700419 intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
420 // fetch from cache
421 rsrcMgr.allocIDsForOnuLock.RLock()
422 allocIDs, ok := rsrcMgr.allocIDsForOnu[intfOnuIDuniID]
423 rsrcMgr.allocIDsForOnuLock.RUnlock()
424 if ok {
425 return allocIDs
Girish Gowdru0c588b22019-04-23 23:24:56 -0400426 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700427 allocIDs = rsrcMgr.PonRsrMgr.GetCurrentAllocIDForOnu(ctx, intfOnuIDuniID)
428
429 // update cache
430 rsrcMgr.allocIDsForOnuLock.Lock()
431 rsrcMgr.allocIDsForOnu[intfOnuIDuniID] = allocIDs
432 rsrcMgr.allocIDsForOnuLock.Unlock()
433
434 return allocIDs
Gamze Abakafee36392019-10-03 11:17:24 +0000435}
436
437// RemoveAllocIDForOnu removes the alloc id for given pon interface, onu id, uni id and alloc id
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700438func (rsrcMgr *OpenOltResourceMgr) RemoveAllocIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, allocID uint32) {
439 allocIDs := rsrcMgr.GetCurrentAllocIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000440 for i := 0; i < len(allocIDs); i++ {
441 if allocIDs[i] == allocID {
442 allocIDs = append(allocIDs[:i], allocIDs[i+1:]...)
443 break
444 }
445 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700446 err := rsrcMgr.UpdateAllocIdsForOnu(ctx, intfID, onuID, uniID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000447 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700448 logger.Errorf(ctx, "Failed to Remove Alloc Id For Onu. intfID %d onuID %d uniID %d allocID %d",
Gamze Abakafee36392019-10-03 11:17:24 +0000449 intfID, onuID, uniID, allocID)
450 }
451}
452
453// RemoveGemPortIDForOnu removes the gem port id for given pon interface, onu id, uni id and gem port id
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700454func (rsrcMgr *OpenOltResourceMgr) RemoveGemPortIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, gemPortID uint32) {
455 gemPortIDs := rsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000456 for i := 0; i < len(gemPortIDs); i++ {
457 if gemPortIDs[i] == gemPortID {
458 gemPortIDs = append(gemPortIDs[:i], gemPortIDs[i+1:]...)
459 break
460 }
461 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700462 err := rsrcMgr.UpdateGEMPortIDsForOnu(ctx, intfID, onuID, uniID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000463 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700464 logger.Errorf(ctx, "Failed to Remove Gem Id For Onu. intfID %d onuID %d uniID %d gemPortId %d",
Gamze Abakafee36392019-10-03 11:17:24 +0000465 intfID, onuID, uniID, gemPortID)
466 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530467}
468
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700469// UpdateGEMPortIDsForOnu updates gemport ids on to the kv store for a given pon port, onu id and uni id
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700470func (rsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ctx context.Context, ponPort uint32, onuID uint32,
471 uniID uint32, gemIDs []uint32) error {
472 intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
473 // update cache
474 rsrcMgr.gemPortIDsForOnuLock.Lock()
475 rsrcMgr.gemPortIDsForOnu[intfOnuIDuniID] = gemIDs
476 rsrcMgr.gemPortIDsForOnuLock.Unlock()
477
478 // Note: in case the write to DB fails there could be inconsistent data between cache and db.
479 // Although this is highly unlikely with DB retries in place, this is something we have to deal with in the next release
480 return rsrcMgr.PonRsrMgr.UpdateGEMPortIDsForOnu(ctx, intfOnuIDuniID,
481 gemIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530482
483}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700484
485// FreeonuID releases(make free) onu id for a particular pon-port
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700486func (rsrcMgr *OpenOltResourceMgr) FreeonuID(ctx context.Context, intfID uint32, onuID []uint32) {
Girish Gowdrab8f1b5a2021-06-27 20:42:40 -0700487 if len(onuID) == 0 {
488 logger.Info(ctx, "onu id slice is nil, nothing to free")
489 return
490 }
Girish Gowdra76a1b092021-07-28 10:07:04 -0700491 if err := rsrcMgr.TechprofileRef.FreeResourceID(ctx, intfID, ponrmgr.ONU_ID, onuID); err != nil {
Matteo Scandolo84585372021-03-18 14:21:22 -0700492 logger.Errorw(ctx, "error-while-freeing-onu-id", log.Fields{
493 "intf-id": intfID,
494 "onu-id": onuID,
495 "err": err.Error(),
496 })
Girish Gowdrab8f1b5a2021-06-27 20:42:40 -0700497 } else {
498 logger.Infow(ctx, "freed onu id", log.Fields{"intfID": intfID, "onuID": onuID})
Matteo Scandolo84585372021-03-18 14:21:22 -0700499 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530500}
501
Gamze Abakafee36392019-10-03 11:17:24 +0000502// FreeAllocID frees AllocID on the PON resource pool and also frees the allocID association
503// for the given OLT device.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700504// The caller should ensure that this is a blocking call and this operation is serialized for
505// the ONU so as not cause resource corruption since there are no mutexes used here.
506func (rsrcMgr *OpenOltResourceMgr) FreeAllocID(ctx context.Context, intfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000507 uniID uint32, allocID uint32) {
Girish Gowdrab77ded92020-04-08 11:45:05 -0700508
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700509 rsrcMgr.RemoveAllocIDForOnu(ctx, intfID, onuID, uniID, allocID)
Gamze Abakafee36392019-10-03 11:17:24 +0000510 allocIDs := make([]uint32, 0)
511 allocIDs = append(allocIDs, allocID)
Girish Gowdra76a1b092021-07-28 10:07:04 -0700512 if err := rsrcMgr.TechprofileRef.FreeResourceID(ctx, intfID, ponrmgr.ALLOC_ID, allocIDs); err != nil {
Matteo Scandolo84585372021-03-18 14:21:22 -0700513 logger.Errorw(ctx, "error-while-freeing-alloc-id", log.Fields{
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700514 "intf-id": intfID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700515 "onu-id": onuID,
516 "err": err.Error(),
517 })
518 }
Gamze Abakafee36392019-10-03 11:17:24 +0000519}
520
521// FreeGemPortID frees GemPortID on the PON resource pool and also frees the gemPortID association
522// for the given OLT device.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700523// The caller should ensure that this is a blocking call and this operation is serialized for
524// the ONU so as not cause resource corruption since there are no mutexes used here.
525func (rsrcMgr *OpenOltResourceMgr) FreeGemPortID(ctx context.Context, intfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000526 uniID uint32, gemPortID uint32) {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700527 rsrcMgr.RemoveGemPortIDForOnu(ctx, intfID, onuID, uniID, gemPortID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700528
Gamze Abakafee36392019-10-03 11:17:24 +0000529 gemPortIDs := make([]uint32, 0)
530 gemPortIDs = append(gemPortIDs, gemPortID)
Girish Gowdra76a1b092021-07-28 10:07:04 -0700531 if err := rsrcMgr.TechprofileRef.FreeResourceID(ctx, intfID, ponrmgr.GEMPORT_ID, gemPortIDs); err != nil {
Matteo Scandolo84585372021-03-18 14:21:22 -0700532 logger.Errorw(ctx, "error-while-freeing-gem-port-id", log.Fields{
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700533 "intf-id": intfID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700534 "onu-id": onuID,
535 "err": err.Error(),
536 })
537 }
Gamze Abakafee36392019-10-03 11:17:24 +0000538}
539
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700540// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id
541func (rsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530542
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700543 intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530544
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700545 AllocIDs := rsrcMgr.PonRsrMgr.GetCurrentAllocIDForOnu(ctx, intfOnuIDuniID)
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700546
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700547 rsrcMgr.allocIDsForOnuLock.Lock()
548 delete(rsrcMgr.allocIDsForOnu, intfOnuIDuniID)
549 rsrcMgr.allocIDsForOnuLock.Unlock()
550
Girish Gowdra76a1b092021-07-28 10:07:04 -0700551 if err := rsrcMgr.TechprofileRef.FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400552 ponrmgr.ALLOC_ID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700553 AllocIDs); err != nil {
554 logger.Errorw(ctx, "error-while-freeing-all-alloc-ids-for-onu", log.Fields{
555 "intf-id": intfID,
556 "onu-id": onuID,
557 "err": err.Error(),
558 })
559 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530560
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700561 GEMPortIDs := rsrcMgr.PonRsrMgr.GetCurrentGEMPortIDsForOnu(ctx, intfOnuIDuniID)
562
563 rsrcMgr.gemPortIDsForOnuLock.Lock()
564 delete(rsrcMgr.gemPortIDsForOnu, intfOnuIDuniID)
565 rsrcMgr.gemPortIDsForOnuLock.Unlock()
566
Girish Gowdra76a1b092021-07-28 10:07:04 -0700567 if err := rsrcMgr.TechprofileRef.FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400568 ponrmgr.GEMPORT_ID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700569 GEMPortIDs); err != nil {
570 logger.Errorw(ctx, "error-while-freeing-all-gem-port-ids-for-onu", log.Fields{
571 "intf-id": intfID,
572 "onu-id": onuID,
573 "err": err.Error(),
574 })
575 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530576
Girish Gowdru0c588b22019-04-23 23:24:56 -0400577 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700578 rsrcMgr.PonRsrMgr.RemoveResourceMap(ctx, intfOnuIDuniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530579}
580
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700581// IsFlowOnKvStore checks if the given flowID is present on the kv store
582// Returns true if the flowID is found, otherwise it returns false
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700583func (rsrcMgr *OpenOltResourceMgr) IsFlowOnKvStore(ctx context.Context, intfID uint32, onuID int32, uniID int32,
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700584 flowID uint64) bool {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530585
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700586 FlowIDs, err := rsrcMgr.GetCurrentFlowIDsForOnu(ctx, intfID, onuID, uniID)
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700587 if err != nil {
588 // error logged in the called function
589 return false
590 }
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400591 if FlowIDs != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700592 logger.Debugw(ctx, "Found flowId(s) for this ONU", log.Fields{"pon": intfID, "onuID": onuID, "uniID": uniID})
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700593 for _, id := range FlowIDs {
594 if flowID == id {
595 return true
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400596 }
597 }
598 }
599 return false
600}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400601
salmansiddiqui7ac62132019-08-22 03:58:50 +0000602// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700603// This path is formed as the following: {intfID, onuID, uniID}/tp_id
604func (rsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) []uint32 {
605 Path := fmt.Sprintf(tpIDPathSuffix, intfID, onuID, uniID)
606 // fetch from cache
607 rsrcMgr.techProfileIDsForOnuLock.RLock()
608 tpIDs, ok := rsrcMgr.techProfileIDsForOnu[Path]
609 rsrcMgr.techProfileIDsForOnuLock.RUnlock()
610 if ok {
611 return tpIDs
612 }
613 Value, err := rsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400614 if err == nil {
615 if Value != nil {
616 Val, err := kvstore.ToByte(Value.Value)
617 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700618 logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"err": err})
619 return tpIDs
Manikkaraj kb1d51442019-07-23 10:41:02 -0400620 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700621 if err = json.Unmarshal(Val, &tpIDs); err != nil {
622 logger.Error(ctx, "Failed to unmarshal", log.Fields{"err": err})
623 return tpIDs
Manikkaraj kb1d51442019-07-23 10:41:02 -0400624 }
625 }
626 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000627 logger.Errorf(ctx, "Failed to get TP id from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400628 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700629 logger.Debugf(ctx, "Getting TP id %d from path %s", tpIDs, Path)
630
631 // update cache
632 rsrcMgr.techProfileIDsForOnuLock.Lock()
633 rsrcMgr.techProfileIDsForOnu[Path] = tpIDs
634 rsrcMgr.techProfileIDsForOnuLock.Unlock()
635
636 return tpIDs
Manikkaraj kb1d51442019-07-23 10:41:02 -0400637
638}
639
Gamze Abakafee36392019-10-03 11:17:24 +0000640// RemoveTechProfileIDsForOnu deletes all tech profile ids from the KV-Store for the given onu based on the path
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700641// This path is formed as the following: {intfID, onuID, uniID}/tp_id
642func (rsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDsForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) error {
643 intfOnuUniID := fmt.Sprintf(tpIDPathSuffix, intfID, onuID, uniID)
644 // update cache
645 rsrcMgr.techProfileIDsForOnuLock.Lock()
646 delete(rsrcMgr.techProfileIDsForOnu, intfOnuUniID)
647 rsrcMgr.techProfileIDsForOnuLock.Unlock()
648
649 if err := rsrcMgr.KVStore.Delete(ctx, intfOnuUniID); err != nil {
650 logger.Errorw(ctx, "Failed to delete techprofile id resource in KV store", log.Fields{"path": intfOnuUniID})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400651 return err
652 }
653 return nil
654}
655
Gamze Abakafee36392019-10-03 11:17:24 +0000656// RemoveTechProfileIDForOnu deletes a specific tech profile id from the KV-Store for the given onu based on the path
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700657// This path is formed as the following: {intfID, onuID, uniID}/tp_id
658func (rsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, tpID uint32) error {
659 tpIDList := rsrcMgr.GetTechProfileIDForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000660 for i, tpIDInList := range tpIDList {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700661 if tpIDInList == tpID {
Gamze Abakafee36392019-10-03 11:17:24 +0000662 tpIDList = append(tpIDList[:i], tpIDList[i+1:]...)
663 }
664 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700665 intfOnuUniID := fmt.Sprintf(tpIDPathSuffix, intfID, onuID, uniID)
666 // update cache
667 rsrcMgr.techProfileIDsForOnuLock.Lock()
668 rsrcMgr.techProfileIDsForOnu[intfOnuUniID] = tpIDList
669 rsrcMgr.techProfileIDsForOnuLock.Unlock()
670
Gamze Abakafee36392019-10-03 11:17:24 +0000671 Value, err := json.Marshal(tpIDList)
672 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000673 logger.Error(ctx, "failed to Marshal")
Gamze Abakafee36392019-10-03 11:17:24 +0000674 return err
675 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700676 if err = rsrcMgr.KVStore.Put(ctx, intfOnuUniID, Value); err != nil {
677 logger.Errorf(ctx, "Failed to update resource %s", intfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000678 return err
679 }
680 return err
681}
682
683// UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700684// This path is formed as the following: {intfID, onuID, uniID}/tp_id
685func (rsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(ctx context.Context, intfID uint32, onuID uint32,
686 uniID uint32, tpID uint32) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400687 var Value []byte
688 var err error
689
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700690 intfOnuUniID := fmt.Sprintf(tpIDPathSuffix, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000691
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700692 tpIDList := rsrcMgr.GetTechProfileIDForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000693 for _, value := range tpIDList {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700694 if value == tpID {
695 logger.Debugf(ctx, "tpID %d is already in tpIdList for the path %s", tpID, intfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000696 return err
697 }
698 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700699 logger.Debugf(ctx, "updating tp id %d on path %s", tpID, intfOnuUniID)
700 tpIDList = append(tpIDList, tpID)
701
702 // update cache
703 rsrcMgr.techProfileIDsForOnuLock.Lock()
704 rsrcMgr.techProfileIDsForOnu[intfOnuUniID] = tpIDList
705 rsrcMgr.techProfileIDsForOnuLock.Unlock()
706
Gamze Abakafee36392019-10-03 11:17:24 +0000707 Value, err = json.Marshal(tpIDList)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400708 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000709 logger.Error(ctx, "failed to Marshal")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400710 return err
711 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700712 if err = rsrcMgr.KVStore.Put(ctx, intfOnuUniID, Value); err != nil {
713 logger.Errorf(ctx, "Failed to update resource %s", intfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400714 return err
715 }
716 return err
717}
718
Girish Gowdraa482f272021-03-24 23:04:19 -0700719// StoreMeterInfoForOnu updates the meter id in the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000720// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700721func (rsrcMgr *OpenOltResourceMgr) StoreMeterInfoForOnu(ctx context.Context, Direction string, intfID uint32, onuID uint32,
722 uniID uint32, tpID uint32, meterInfo *MeterInfo) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400723 var Value []byte
724 var err error
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700725 intfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, intfID, onuID, uniID, tpID, Direction)
726
727 // update cache
728 rsrcMgr.meterInfoForOnuLock.Lock()
729 rsrcMgr.meterInfoForOnu[intfOnuUniID] = meterInfo
730 rsrcMgr.meterInfoForOnuLock.Unlock()
731
Girish Gowdraa482f272021-03-24 23:04:19 -0700732 Value, err = json.Marshal(*meterInfo)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400733 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000734 logger.Error(ctx, "failed to Marshal meter config")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400735 return err
736 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700737 if err = rsrcMgr.KVStore.Put(ctx, intfOnuUniID, Value); err != nil {
738 logger.Errorf(ctx, "Failed to store meter into KV store %s", intfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400739 return err
740 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700741 logger.Debugw(ctx, "meter info updated successfully", log.Fields{"path": intfOnuUniID, "meter-info": meterInfo})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400742 return err
743}
744
Girish Gowdraa482f272021-03-24 23:04:19 -0700745// GetMeterInfoForOnu fetches the meter id from the kv store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000746// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700747func (rsrcMgr *OpenOltResourceMgr) GetMeterInfoForOnu(ctx context.Context, Direction string, intfID uint32, onuID uint32,
748 uniID uint32, tpID uint32) (*MeterInfo, error) {
749 Path := fmt.Sprintf(MeterIDPathSuffix, intfID, onuID, uniID, tpID, Direction)
750
751 // get from cache
752 rsrcMgr.meterInfoForOnuLock.RLock()
753 val, ok := rsrcMgr.meterInfoForOnu[Path]
754 rsrcMgr.meterInfoForOnuLock.RUnlock()
755 if ok {
756 return val, nil
757 }
758
Girish Gowdraa482f272021-03-24 23:04:19 -0700759 var meterInfo MeterInfo
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700760 Value, err := rsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400761 if err == nil {
762 if Value != nil {
Girish Gowdraa482f272021-03-24 23:04:19 -0700763 logger.Debug(ctx, "Found meter info in KV store", log.Fields{"Direction": Direction})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000764 Val, er := kvstore.ToByte(Value.Value)
765 if er != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700766 logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"err": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000767 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400768 }
Girish Gowdraa482f272021-03-24 23:04:19 -0700769 if er = json.Unmarshal(Val, &meterInfo); er != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700770 logger.Error(ctx, "Failed to unmarshal meter info", log.Fields{"err": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000771 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400772 }
773 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000774 logger.Debug(ctx, "meter-does-not-exists-in-KVStore")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400775 return nil, err
776 }
777 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000778 logger.Errorf(ctx, "Failed to get Meter config from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400779
780 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700781 // update cache
782 rsrcMgr.meterInfoForOnuLock.Lock()
783 rsrcMgr.meterInfoForOnu[Path] = &meterInfo
784 rsrcMgr.meterInfoForOnuLock.Unlock()
785
Girish Gowdraa482f272021-03-24 23:04:19 -0700786 return &meterInfo, err
Manikkaraj kb1d51442019-07-23 10:41:02 -0400787}
788
Girish Gowdraa482f272021-03-24 23:04:19 -0700789// HandleMeterInfoRefCntUpdate increments or decrements the reference counter for a given meter.
790// When reference count becomes 0, it clears the meter information from the kv store
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700791func (rsrcMgr *OpenOltResourceMgr) HandleMeterInfoRefCntUpdate(ctx context.Context, Direction string,
792 intfID uint32, onuID uint32, uniID uint32, tpID uint32, increment bool) error {
793 meterInfo, err := rsrcMgr.GetMeterInfoForOnu(ctx, Direction, intfID, onuID, uniID, tpID)
Girish Gowdra82c80982021-03-26 16:22:02 -0700794 if err != nil {
795 return err
796 } else if meterInfo == nil {
797 // If we are increasing the reference count, we expect the meter information to be present on KV store.
798 // But if decrementing the reference count, the meter is possibly already cleared from KV store. Just log warn but do not return error.
799 if increment {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700800 logger.Errorf(ctx, "error-fetching-meter-info-for-intf-%d-onu-%d-uni-%d-tp-id-%d-direction-%s", intfID, onuID, uniID, tpID, Direction)
801 return fmt.Errorf("error-fetching-meter-info-for-intf-%d-onu-%d-uni-%d-tp-id-%d-direction-%s", intfID, onuID, uniID, tpID, Direction)
Girish Gowdra82c80982021-03-26 16:22:02 -0700802 }
803 logger.Warnw(ctx, "meter is already cleared",
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700804 log.Fields{"intfID": intfID, "onuID": onuID, "uniID": uniID, "direction": Direction, "increment": increment})
Girish Gowdra82c80982021-03-26 16:22:02 -0700805 return nil
Girish Gowdraa482f272021-03-24 23:04:19 -0700806 }
Girish Gowdra82c80982021-03-26 16:22:02 -0700807
Girish Gowdraa482f272021-03-24 23:04:19 -0700808 if increment {
809 meterInfo.RefCnt++
810 } else {
811 meterInfo.RefCnt--
Girish Gowdra82c80982021-03-26 16:22:02 -0700812 // If RefCnt become 0 clear the meter information from the DB.
Girish Gowdraa482f272021-03-24 23:04:19 -0700813 if meterInfo.RefCnt == 0 {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700814 if err := rsrcMgr.RemoveMeterInfoForOnu(ctx, Direction, intfID, onuID, uniID, tpID); err != nil {
Girish Gowdraa482f272021-03-24 23:04:19 -0700815 return err
816 }
817 return nil
818 }
819 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700820 if err := rsrcMgr.StoreMeterInfoForOnu(ctx, Direction, intfID, onuID, uniID, tpID, meterInfo); err != nil {
Girish Gowdraa482f272021-03-24 23:04:19 -0700821 return err
822 }
823 return nil
824}
825
826// RemoveMeterInfoForOnu deletes the meter id from the kV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000827// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700828func (rsrcMgr *OpenOltResourceMgr) RemoveMeterInfoForOnu(ctx context.Context, Direction string, intfID uint32, onuID uint32,
829 uniID uint32, tpID uint32) error {
830 Path := fmt.Sprintf(MeterIDPathSuffix, intfID, onuID, uniID, tpID, Direction)
831
832 // update cache
833 rsrcMgr.meterInfoForOnuLock.Lock()
834 delete(rsrcMgr.meterInfoForOnu, Path)
835 rsrcMgr.meterInfoForOnuLock.Unlock()
836
837 if err := rsrcMgr.KVStore.Delete(ctx, Path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000838 logger.Errorf(ctx, "Failed to delete meter id %s from kvstore ", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400839 return err
840 }
841 return nil
842}
salmansiddiqui7ac62132019-08-22 03:58:50 +0000843
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700844//AddGemToOnuGemInfo adds gemport to onugem info kvstore and also local cache
845func (rsrcMgr *OpenOltResourceMgr) AddGemToOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, gemPort uint32) error {
846 onugem, err := rsrcMgr.GetOnuGemInfo(ctx, intfID, onuID)
847 if err != nil || onugem == nil || onugem.SerialNumber == "" {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000848 logger.Errorf(ctx, "failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530849 return err
850 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700851 if onugem.OnuID == onuID {
852 for _, gem := range onugem.GemPorts {
853 if gem == gemPort {
854 logger.Debugw(ctx, "Gem already present in onugem info, skpping addition", log.Fields{"gem": gem})
855 return nil
856 }
857 }
858 logger.Debugw(ctx, "Added gem to onugem info", log.Fields{"gem": gemPort})
859 onugem.GemPorts = append(onugem.GemPorts, gemPort)
860 } else {
861 logger.Errorw(ctx, "onu id in OnuGemInfo does not match", log.Fields{"onuID": onuID, "ponIf": intfID, "onuGemInfoOnuID": onugem.OnuID})
862 return fmt.Errorf("onu-id-in-OnuGemInfo-does-not-match-%v", onuID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530863 }
864
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700865 err = rsrcMgr.AddOnuGemInfo(ctx, intfID, onuID, *onugem)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530866 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000867 logger.Error(ctx, "Failed to add onugem to kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530868 return err
869 }
870 return err
871}
872
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700873//RemoveGemFromOnuGemInfo removes gemport from onugem info on kvstore and also local cache
874func (rsrcMgr *OpenOltResourceMgr) RemoveGemFromOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, gemPort uint32) error {
875 onugem, err := rsrcMgr.GetOnuGemInfo(ctx, intfID, onuID)
876 if err != nil || onugem == nil || onugem.SerialNumber == "" {
877 logger.Errorf(ctx, "failed to get onuifo for intfid %d", intfID)
878 return err
879 }
880 updated := false
881 if onugem.OnuID == onuID {
882 for i, gem := range onugem.GemPorts {
883 if gem == gemPort {
884 logger.Debugw(ctx, "Gem found, removing from onu gem info", log.Fields{"gem": gem})
885 onugem.GemPorts = append(onugem.GemPorts[:i], onugem.GemPorts[i+1:]...)
886 updated = true
887 break
888 }
889 }
890 } else {
891 logger.Errorw(ctx, "onu id in OnuGemInfo does not match", log.Fields{"onuID": onuID, "ponIf": intfID, "onuGemInfoOnuID": onugem.OnuID})
892 return fmt.Errorf("onu-id-in-OnuGemInfo-does-not-match-%v", onuID)
893 }
894 if updated {
895 err = rsrcMgr.AddOnuGemInfo(ctx, intfID, onuID, *onugem)
896 if err != nil {
897 logger.Error(ctx, "Failed to add onugem to kv store")
898 return err
899 }
900 } else {
901 logger.Debugw(ctx, "Gem port not found in onu gem info", log.Fields{"gem": gemPort})
902 }
903 return nil
904}
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530905
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700906//GetOnuGemInfo gets onu gem info from the kvstore per interface
907func (rsrcMgr *OpenOltResourceMgr) GetOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32) (*OnuGemInfo, error) {
908 var err error
909 var Val []byte
910 var onugem OnuGemInfo
911
912 path := fmt.Sprintf(OnuGemInfoPath, intfID, onuID)
913
914 rsrcMgr.onuGemInfoLock.RLock()
915 val, ok := rsrcMgr.onuGemInfo[path]
916 rsrcMgr.onuGemInfoLock.RUnlock()
917 if ok {
918 return val, nil
919 }
920 value, err := rsrcMgr.KVStore.Get(ctx, path)
921 if err != nil {
922 logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path})
923 return nil, err
924 } else if value == nil {
925 logger.Debug(ctx, "No onuinfo for path", log.Fields{"path": path})
926 return nil, nil // returning nil as this could happen if there are no onus for the interface yet
927 }
928 if Val, err = kvstore.ToByte(value.Value); err != nil {
929 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530930 return nil, err
931 }
932
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700933 if err = json.Unmarshal(Val, &onugem); err != nil {
934 logger.Error(ctx, "Failed to unmarshall")
935 return nil, err
936 }
937 logger.Debugw(ctx, "found onugem info from path", log.Fields{"path": path, "onuGemInfo": onugem})
938 rsrcMgr.onuGemInfoLock.Lock()
939 rsrcMgr.onuGemInfo[path] = &onugem
940 rsrcMgr.onuGemInfoLock.Unlock()
941
942 return &onugem, nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530943}
944
Chaitrashree G S1a55b882020-02-04 17:35:35 -0500945// AddOnuGemInfo adds onu info on to the kvstore per interface
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700946func (rsrcMgr *OpenOltResourceMgr) AddOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, onuGem OnuGemInfo) error {
947
948 var Value []byte
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530949 var err error
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700950 Path := fmt.Sprintf(OnuGemInfoPath, intfID, onuID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530951
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700952 rsrcMgr.onuGemInfoLock.Lock()
953 rsrcMgr.onuGemInfo[Path] = &onuGem
954 rsrcMgr.onuGemInfoLock.Unlock()
955
956 Value, err = json.Marshal(onuGem)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530957 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700958 logger.Error(ctx, "failed to Marshal")
959 return err
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530960 }
961
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700962 if err = rsrcMgr.KVStore.Put(ctx, Path, Value); err != nil {
963 logger.Errorf(ctx, "Failed to update resource %s", Path)
964 return err
965 }
Girish Gowdrabcf98af2021-07-01 08:24:42 -0700966 logger.Debugw(ctx, "added onu gem info to store", log.Fields{"onuGemInfo": onuGem})
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700967 return err
968}
969
970// DelOnuGemInfo deletes the onugem info from kvstore per ONU
971func (rsrcMgr *OpenOltResourceMgr) DelOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32) error {
972 path := fmt.Sprintf(OnuGemInfoPath, intfID, onuID)
973 rsrcMgr.onuGemInfoLock.Lock()
974 logger.Debugw(ctx, "removing onu gem info", log.Fields{"onuGemInfo": rsrcMgr.onuGemInfo[path]})
975 delete(rsrcMgr.onuGemInfo, path)
976 rsrcMgr.onuGemInfoLock.Unlock()
977
978 if err := rsrcMgr.KVStore.Delete(ctx, path); err != nil {
979 logger.Errorf(ctx, "failed to remove resource %s", path)
980 return err
981 }
Andrea Campanellab83b39d2020-03-30 11:41:16 +0200982 return nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530983}
984
Girish Gowdra950326e2021-11-05 12:43:24 -0700985//DeleteAllOnuGemInfoForIntf deletes all the all onu gem info on the given pon interface
986func (rsrcMgr *OpenOltResourceMgr) DeleteAllOnuGemInfoForIntf(ctx context.Context, intfID uint32) error {
987
988 path := fmt.Sprintf(OnuGemInfoPathPathPrefix, intfID)
989
990 logger.Debugw(ctx, "delete-all-onu-gem-info-for-pon-intf", log.Fields{"intfID": intfID})
991 if err := rsrcMgr.KVStore.DeleteWithPrefix(ctx, path); err != nil {
992 logger.Errorf(ctx, "failed-to-remove-resource-%s", path)
993 return err
994 }
995
996 // Reset cache. Normally not necessary as the entire device is getting deleted when this API is invoked.
997 rsrcMgr.onuGemInfoLock.Lock()
998 rsrcMgr.onuGemInfo = make(map[string]*OnuGemInfo)
999 rsrcMgr.onuGemInfoLock.Unlock()
1000 return nil
1001}
1002
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301003// AddUniPortToOnuInfo adds uni port to the onuinfo kvstore. check if the uni is already present if not update the kv store.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001004func (rsrcMgr *OpenOltResourceMgr) AddUniPortToOnuInfo(ctx context.Context, intfID uint32, onuID uint32, portNo uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301005
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001006 onugem, err := rsrcMgr.GetOnuGemInfo(ctx, intfID, onuID)
1007 if err != nil || onugem == nil || onugem.SerialNumber == "" {
1008 logger.Warnf(ctx, "failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301009 return
1010 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001011
1012 if onugem.OnuID == onuID {
1013 for _, uni := range onugem.UniPorts {
1014 if uni == portNo {
1015 logger.Debugw(ctx, "uni already present in onugem info", log.Fields{"uni": portNo})
1016 return
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301017 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301018 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001019 onugem.UniPorts = append(onugem.UniPorts, portNo)
1020 } else {
1021 logger.Warnw(ctx, "onu id mismatch in onu gem info", log.Fields{"intfID": intfID, "onuID": onuID})
1022 return
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301023 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001024 err = rsrcMgr.AddOnuGemInfo(ctx, intfID, onuID, *onugem)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301025 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001026 logger.Errorw(ctx, "Failed to add uni port in onugem to kv store", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301027 return
1028 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301029}
1030
Esin Karaman7fb80c22020-07-16 14:23:33 +00001031//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 -07001032func (rsrcMgr *OpenOltResourceMgr) UpdateGemPortForPktIn(ctx context.Context, pktIn PacketInInfoKey, gemPort uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301033
Girish Gowdra950326e2021-11-05 12:43:24 -07001034 path := fmt.Sprintf(OnuPacketInPath, pktIn.IntfID, pktIn.OnuID, pktIn.LogicalPort, pktIn.VlanID, pktIn.Priority)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001035 // update cache
1036 rsrcMgr.gemPortForPacketInInfoLock.Lock()
1037 rsrcMgr.gemPortForPacketInInfo[path] = gemPort
1038 rsrcMgr.gemPortForPacketInInfoLock.Unlock()
1039
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301040 Value, err := json.Marshal(gemPort)
1041 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001042 logger.Error(ctx, "Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301043 return
1044 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001045 if err = rsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001046 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"path": path, "value": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301047 return
1048 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001049 logger.Debugw(ctx, "added gem packet in successfully", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301050}
1051
Esin Karaman7fb80c22020-07-16 14:23:33 +00001052// 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 -07001053func (rsrcMgr *OpenOltResourceMgr) GetGemPortFromOnuPktIn(ctx context.Context, packetInInfoKey PacketInInfoKey) (uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301054
1055 var Val []byte
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301056
Girish Gowdra950326e2021-11-05 12:43:24 -07001057 path := fmt.Sprintf(OnuPacketInPath, packetInInfoKey.IntfID, packetInInfoKey.OnuID, packetInInfoKey.LogicalPort,
Esin Karaman7fb80c22020-07-16 14:23:33 +00001058 packetInInfoKey.VlanID, packetInInfoKey.Priority)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001059 // get from cache
1060 rsrcMgr.gemPortForPacketInInfoLock.RLock()
1061 gemPort, ok := rsrcMgr.gemPortForPacketInInfo[path]
1062 rsrcMgr.gemPortForPacketInInfoLock.RUnlock()
1063 if ok {
1064 logger.Debugw(ctx, "found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
1065 return gemPort, nil
1066 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301067
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001068 value, err := rsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301069 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001070 logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301071 return uint32(0), err
1072 } else if value == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001073 logger.Debugw(ctx, "No pkt in gem found", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301074 return uint32(0), nil
1075 }
1076
1077 if Val, err = kvstore.ToByte(value.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001078 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301079 return uint32(0), err
1080 }
1081 if err = json.Unmarshal(Val, &gemPort); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001082 logger.Error(ctx, "Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301083 return uint32(0), err
1084 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001085 logger.Debugw(ctx, "found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001086 // update cache
1087 rsrcMgr.gemPortForPacketInInfoLock.Lock()
1088 rsrcMgr.gemPortForPacketInInfo[path] = gemPort
1089 rsrcMgr.gemPortForPacketInInfoLock.Unlock()
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301090
1091 return gemPort, nil
1092}
1093
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001094//DeletePacketInGemPortForOnu deletes the packet-in gemport for ONU
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001095func (rsrcMgr *OpenOltResourceMgr) DeletePacketInGemPortForOnu(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) error {
Girish Gowdra950326e2021-11-05 12:43:24 -07001096 path := fmt.Sprintf(OnuPacketInPathPrefix, intfID, onuID, logicalPort)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001097 value, err := rsrcMgr.KVStore.List(ctx, path)
Esin Karaman7fb80c22020-07-16 14:23:33 +00001098 if err != nil {
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001099 logger.Errorf(ctx, "failed-to-read-value-from-path-%s", path)
1100 return errors.New("failed-to-read-value-from-path-" + path)
Esin Karaman7fb80c22020-07-16 14:23:33 +00001101 }
Esin Karaman7fb80c22020-07-16 14:23:33 +00001102
1103 //remove them one by one
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001104 for key := range value {
serkant.uluderya4f5ed592020-12-17 20:57:59 +03001105 // Remove the PathPrefix from the path on KV key.
Girish Gowdra950326e2021-11-05 12:43:24 -07001106 // gemPortForPacketInInfo cache uses OnuPacketInPath as the key
serkant.uluderya4f5ed592020-12-17 20:57:59 +03001107 stringToBeReplaced := rsrcMgr.KVStore.PathPrefix + "/"
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001108 replacedWith := ""
1109 key = strings.Replace(key, stringToBeReplaced, replacedWith, 1)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001110 // update cache
1111 rsrcMgr.gemPortForPacketInInfoLock.Lock()
1112 delete(rsrcMgr.gemPortForPacketInInfo, key)
1113 rsrcMgr.gemPortForPacketInInfoLock.Unlock()
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001114
serkant.uluderya4f5ed592020-12-17 20:57:59 +03001115 logger.Debugw(ctx, "removed-key-from-packetin-gem-port-cache", log.Fields{"key": key, "cache-len": len(rsrcMgr.gemPortForPacketInInfo)})
1116 }
1117
1118 logger.Debugw(ctx, "delete-packetin-gem-port", log.Fields{"realPath": path})
1119 if err := rsrcMgr.KVStore.DeleteWithPrefix(ctx, path); err != nil {
1120 logger.Errorf(ctx, "failed-to-remove-resource-%s", path)
1121 return err
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301122 }
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001123
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301124 return nil
1125}
1126
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001127//GetFlowIDsForGem gets the list of FlowIDs for the given gemport
1128func (rsrcMgr *OpenOltResourceMgr) GetFlowIDsForGem(ctx context.Context, intf uint32, gem uint32) ([]uint64, error) {
1129 path := fmt.Sprintf(FlowIDsForGem, intf, gem)
1130
1131 // get from cache
1132 rsrcMgr.flowIDsForGemLock.RLock()
1133 flowIDs, ok := rsrcMgr.flowIDsForGem[gem]
1134 rsrcMgr.flowIDsForGemLock.RUnlock()
1135 if ok {
1136 return flowIDs, nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301137 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301138
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001139 value, err := rsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301140 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001141 logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path})
1142 return nil, err
1143 } else if value == nil {
1144 logger.Debug(ctx, "no flow-ids found", log.Fields{"path": path})
1145 return nil, nil
1146 }
1147 Val, err := kvstore.ToByte(value.Value)
1148 if err != nil {
1149 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301150 return nil, err
1151 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301152
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001153 if err = json.Unmarshal(Val, &flowIDs); err != nil {
1154 logger.Error(ctx, "Failed to unmarshall")
1155 return nil, err
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301156 }
1157
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001158 // update cache
1159 rsrcMgr.flowIDsForGemLock.Lock()
1160 rsrcMgr.flowIDsForGem[gem] = flowIDs
1161 rsrcMgr.flowIDsForGemLock.Unlock()
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301162
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001163 return flowIDs, nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301164}
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301165
1166//UpdateFlowIDsForGem updates flow id per gemport
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001167func (rsrcMgr *OpenOltResourceMgr) UpdateFlowIDsForGem(ctx context.Context, intf uint32, gem uint32, flowIDs []uint64) error {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301168 var val []byte
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001169 path := fmt.Sprintf(FlowIDsForGem, intf, gem)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301170
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001171 // update cache
1172 rsrcMgr.flowIDsForGemLock.Lock()
1173 rsrcMgr.flowIDsForGem[gem] = flowIDs
1174 rsrcMgr.flowIDsForGemLock.Unlock()
1175
1176 if flowIDs == nil {
Girish Gowdra950326e2021-11-05 12:43:24 -07001177 if err := rsrcMgr.KVStore.Delete(ctx, path); err != nil {
1178 logger.Errorw(ctx, "Failed to delete from kvstore", log.Fields{"err": err, "path": path})
1179 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001180 return nil
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301181 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001182 val, err := json.Marshal(flowIDs)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301183 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001184 logger.Error(ctx, "Failed to marshal data", log.Fields{"err": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301185 return err
1186 }
Girish Gowdrab77ded92020-04-08 11:45:05 -07001187
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001188 if err = rsrcMgr.KVStore.Put(ctx, path, val); err != nil {
1189 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"err": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301190 return err
1191 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001192 logger.Debugw(ctx, "added flowid list for gem to kv successfully", log.Fields{"path": path, "flowidlist": flowIDs})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301193 return nil
1194}
1195
1196//DeleteFlowIDsForGem deletes the flowID list entry per gem from kvstore.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001197func (rsrcMgr *OpenOltResourceMgr) DeleteFlowIDsForGem(ctx context.Context, intf uint32, gem uint32) {
1198 path := fmt.Sprintf(FlowIDsForGem, intf, gem)
1199 // update cache
1200 rsrcMgr.flowIDsForGemLock.Lock()
1201 delete(rsrcMgr.flowIDsForGem, gem)
1202 rsrcMgr.flowIDsForGemLock.Unlock()
1203 if err := rsrcMgr.KVStore.Delete(ctx, path); err != nil {
1204 logger.Errorw(ctx, "Failed to delete from kvstore", log.Fields{"err": err, "path": path})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301205 }
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301206}
Esin Karamanccb714b2019-11-29 15:02:06 +00001207
Girish Gowdra950326e2021-11-05 12:43:24 -07001208//DeleteAllFlowIDsForGemForIntf deletes all the flow ids associated for all the gems on the given pon interface
1209func (rsrcMgr *OpenOltResourceMgr) DeleteAllFlowIDsForGemForIntf(ctx context.Context, intfID uint32) error {
1210
1211 path := fmt.Sprintf(FlowIDsForGemPathPrefix, intfID)
1212
1213 logger.Debugw(ctx, "delete-flow-ids-for-gem-for-pon-intf", log.Fields{"intfID": intfID})
1214 if err := rsrcMgr.KVStore.DeleteWithPrefix(ctx, path); err != nil {
1215 logger.Errorf(ctx, "failed-to-remove-resource-%s", path)
1216 return err
1217 }
1218
1219 // Reset cache. Normally not necessary as the entire device is getting deleted when this API is invoked.
1220 rsrcMgr.flowIDsForGemLock.Lock()
1221 rsrcMgr.flowIDsForGem = make(map[uint32][]uint64)
1222 rsrcMgr.flowIDsForGemLock.Unlock()
1223 return nil
1224}
1225
Esin Karamanccb714b2019-11-29 15:02:06 +00001226//GetMcastQueuePerInterfaceMap gets multicast queue info per pon interface
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001227func (rsrcMgr *OpenOltResourceMgr) GetMcastQueuePerInterfaceMap(ctx context.Context) (map[uint32][]uint32, error) {
Kent Hagermane6ff1012020-07-14 15:07:53 -04001228 path := McastQueuesForIntf
Esin Karamanccb714b2019-11-29 15:02:06 +00001229 var val []byte
1230
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001231 rsrcMgr.mcastQueueForIntfLock.RLock()
1232 if rsrcMgr.mcastQueueForIntfLoadedFromKvStore {
1233 rsrcMgr.mcastQueueForIntfLock.RUnlock()
1234 return rsrcMgr.mcastQueueForIntf, nil
1235 }
1236 rsrcMgr.mcastQueueForIntfLock.RUnlock()
1237
1238 kvPair, err := rsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001239 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001240 logger.Error(ctx, "failed to get data from kv store")
Esin Karamanccb714b2019-11-29 15:02:06 +00001241 return nil, err
1242 }
1243 if kvPair != nil && kvPair.Value != nil {
1244 if val, err = kvstore.ToByte(kvPair.Value); err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001245 logger.Error(ctx, "Failed to convert to byte array ", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001246 return nil, err
1247 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001248 rsrcMgr.mcastQueueForIntfLock.Lock()
1249 defer rsrcMgr.mcastQueueForIntfLock.Unlock()
1250 if err = json.Unmarshal(val, &rsrcMgr.mcastQueueForIntf); err != nil {
1251 logger.Error(ctx, "Failed to unmarshall ", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001252 return nil, err
1253 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001254 rsrcMgr.mcastQueueForIntfLoadedFromKvStore = true
Esin Karamanccb714b2019-11-29 15:02:06 +00001255 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001256 return rsrcMgr.mcastQueueForIntf, nil
Esin Karamanccb714b2019-11-29 15:02:06 +00001257}
1258
1259//AddMcastQueueForIntf adds multicast queue for pon interface
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001260func (rsrcMgr *OpenOltResourceMgr) AddMcastQueueForIntf(ctx context.Context, intf uint32, gem uint32, servicePriority uint32) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001261 var val []byte
Kent Hagermane6ff1012020-07-14 15:07:53 -04001262 path := McastQueuesForIntf
Esin Karamanccb714b2019-11-29 15:02:06 +00001263
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001264 // Load local cache from kv store the first time
1265 rsrcMgr.mcastQueueForIntfLock.RLock()
1266 if !rsrcMgr.mcastQueueForIntfLoadedFromKvStore {
1267 rsrcMgr.mcastQueueForIntfLock.RUnlock()
1268 _, err := rsrcMgr.GetMcastQueuePerInterfaceMap(ctx)
1269 if err != nil {
1270 logger.Errorw(ctx, "Failed to get multicast queue info for interface", log.Fields{"err": err, "intf": intf})
1271 return err
1272 }
1273 } else {
1274 rsrcMgr.mcastQueueForIntfLock.RUnlock()
1275 }
1276
1277 // Update KV store
1278 rsrcMgr.mcastQueueForIntfLock.Lock()
1279 rsrcMgr.mcastQueueForIntf[intf] = []uint32{gem, servicePriority}
1280 val, err := json.Marshal(rsrcMgr.mcastQueueForIntf)
Esin Karamanccb714b2019-11-29 15:02:06 +00001281 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001282 rsrcMgr.mcastQueueForIntfLock.Unlock()
1283 logger.Errorw(ctx, "Failed to marshal data", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001284 return err
1285 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001286 rsrcMgr.mcastQueueForIntfLock.Unlock()
1287
1288 if err = rsrcMgr.KVStore.Put(ctx, path, val); err != nil {
1289 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"err": err, "path": path, "value": val})
Esin Karamanccb714b2019-11-29 15:02:06 +00001290 return err
1291 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001292 logger.Debugw(ctx, "added multicast queue info to KV store successfully", log.Fields{"path": path, "interfaceId": intf, "gem": gem, "svcPrior": servicePriority})
Esin Karamanccb714b2019-11-29 15:02:06 +00001293 return nil
1294}
1295
1296//AddFlowGroupToKVStore adds flow group into KV store
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001297func (rsrcMgr *OpenOltResourceMgr) AddFlowGroupToKVStore(ctx context.Context, groupEntry *ofp.OfpGroupEntry, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001298 var Value []byte
1299 var err error
1300 var path string
1301 if cached {
1302 path = fmt.Sprintf(FlowGroupCached, groupEntry.Desc.GroupId)
1303 } else {
1304 path = fmt.Sprintf(FlowGroup, groupEntry.Desc.GroupId)
1305 }
1306 //build group info object
1307 var outPorts []uint32
1308 for _, ofBucket := range groupEntry.Desc.Buckets {
1309 for _, ofAction := range ofBucket.Actions {
1310 if ofAction.Type == ofp.OfpActionType_OFPAT_OUTPUT {
1311 outPorts = append(outPorts, ofAction.GetOutput().Port)
1312 }
1313 }
1314 }
1315 groupInfo := GroupInfo{
1316 GroupID: groupEntry.Desc.GroupId,
1317 OutPorts: outPorts,
1318 }
1319
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001320 rsrcMgr.groupInfoLock.Lock()
1321 rsrcMgr.groupInfo[path] = &groupInfo
1322 rsrcMgr.groupInfoLock.Unlock()
1323
Esin Karamanccb714b2019-11-29 15:02:06 +00001324 Value, err = json.Marshal(groupInfo)
1325
1326 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001327 logger.Error(ctx, "failed to Marshal flow group object")
Esin Karamanccb714b2019-11-29 15:02:06 +00001328 return err
1329 }
1330
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001331 if err = rsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001332 logger.Errorf(ctx, "Failed to update resource %s", path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001333 return err
1334 }
1335 return nil
1336}
1337
1338//RemoveFlowGroupFromKVStore removes flow group from KV store
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001339func (rsrcMgr *OpenOltResourceMgr) RemoveFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001340 var path string
1341 if cached {
1342 path = fmt.Sprintf(FlowGroupCached, groupID)
1343 } else {
1344 path = fmt.Sprintf(FlowGroup, groupID)
1345 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001346 rsrcMgr.groupInfoLock.Lock()
1347 delete(rsrcMgr.groupInfo, path)
1348 rsrcMgr.groupInfoLock.Unlock()
1349
1350 if err := rsrcMgr.KVStore.Delete(ctx, path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001351 logger.Errorf(ctx, "Failed to remove resource %s due to %s", path, err)
Esin Karamand519bbf2020-07-01 11:16:03 +00001352 return err
Esin Karamanccb714b2019-11-29 15:02:06 +00001353 }
Esin Karamand519bbf2020-07-01 11:16:03 +00001354 return nil
Esin Karamanccb714b2019-11-29 15:02:06 +00001355}
1356
1357//GetFlowGroupFromKVStore fetches flow group from the KV store. Returns (false, {} error) if any problem occurs during
1358//fetching the data. Returns (true, groupInfo, nil) if the group is fetched successfully.
1359// Returns (false, {}, nil) if the group does not exists in the KV store.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001360func (rsrcMgr *OpenOltResourceMgr) GetFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) (bool, GroupInfo, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001361 var groupInfo GroupInfo
1362 var path string
1363 if cached {
1364 path = fmt.Sprintf(FlowGroupCached, groupID)
1365 } else {
1366 path = fmt.Sprintf(FlowGroup, groupID)
1367 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001368
1369 // read from cache
1370 rsrcMgr.groupInfoLock.RLock()
1371 gi, ok := rsrcMgr.groupInfo[path]
1372 rsrcMgr.groupInfoLock.RUnlock()
1373 if ok {
1374 return true, *gi, nil
1375 }
1376
1377 kvPair, err := rsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001378 if err != nil {
1379 return false, groupInfo, err
1380 }
1381 if kvPair != nil && kvPair.Value != nil {
1382 Val, err := kvstore.ToByte(kvPair.Value)
1383 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001384 logger.Errorw(ctx, "Failed to convert flow group into byte array", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001385 return false, groupInfo, err
1386 }
1387 if err = json.Unmarshal(Val, &groupInfo); err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001388 logger.Errorw(ctx, "Failed to unmarshal", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001389 return false, groupInfo, err
1390 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001391 // update cache
1392 rsrcMgr.groupInfoLock.Lock()
1393 rsrcMgr.groupInfo[path] = &groupInfo
1394 rsrcMgr.groupInfoLock.Unlock()
1395
Esin Karamanccb714b2019-11-29 15:02:06 +00001396 return true, groupInfo, nil
1397 }
1398 return false, groupInfo, nil
1399}
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001400
1401// toByte converts an interface value to a []byte. The interface should either be of
1402// a string type or []byte. Otherwise, an error is returned.
1403func toByte(value interface{}) ([]byte, error) {
1404 switch t := value.(type) {
1405 case []byte:
1406 return value.([]byte), nil
1407 case string:
1408 return []byte(value.(string)), nil
1409 default:
1410 return nil, fmt.Errorf("unexpected-type-%T", t)
1411 }
1412}