blob: 63e299ba50d2108bb9b7adfcf65ce196f43a4560 [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 Gowdraa09aeab2020-09-14 16:30:52 -070048 // OnuPacketINPathPrefix - path prefix where ONU packet-in vlanID/PCP is stored
49 //format: onu_packetin/{<intfid>,<onuid>,<logicalport>}
50 OnuPacketINPathPrefix = "onu_packetin/{%d,%d,%d}"
51 // OnuPacketINPath path on the kvstore to store packetin gemport,which will be used for packetin, packetout
52 //format: onu_packetin/{<intfid>,<onuid>,<logicalport>}/{<vlanId>,<priority>}
53 OnuPacketINPath = OnuPacketINPathPrefix + "/{%d,%d}"
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -070054 //FlowIDsForGem flowids_per_gem/<intfid>/<gemport-id>
55 FlowIDsForGem = "flowids_per_gem/{%d}/{%d}"
Esin Karamanccb714b2019-11-29 15:02:06 +000056 //McastQueuesForIntf multicast queues for pon interfaces
57 McastQueuesForIntf = "mcast_qs_for_int"
58 //FlowGroup flow_groups/<flow_group_id>
59 // A group is stored under this path on the KV store after it has been installed to the device.
60 // It should also be deleted after it has been removed from the device accordingly.
61 FlowGroup = "flow_groups/{%d}"
62 //FlowGroupCached flow_groups_cached/<flow_group_id>
63 // When a group add request received, we create the group without setting any members to it since we cannot
64 // set any members to a group until it is associated with a multicast flow. It is a BAL limitation.
65 // When the related multicast flow has been created we perform set members operation for the group.
66 // That is why we need to keep the members of a group until the multicast flow creation request comes.
67 // We preserve the groups under "FlowGroupsCached" directory in the KV store temporarily. Having set members,
68 // we remove the group from the cached group store.
69 FlowGroupCached = "flow_groups_cached/{%d}"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070070
71 //FlowIDPath - Path on the KV store for storing list of Flow IDs for a given subscriber
72 //Format: BasePathKvStore/<(pon_intf_id, onu_id, uni_id)>/flow_ids
73 FlowIDPath = "{%s}/flow_ids"
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -070074
75 //OnuGemInfoPath is path on the kvstore to store onugem info map
76 //format: <device-id>/onu_gem_info/<intfid>
77 OnuGemInfoPath = "onu_gem_info/{%d}/{%d}" // onu_gem/<intfid>/<onuID>
salmansiddiqui7ac62132019-08-22 03:58:50 +000078)
Abhilash S.L7f17e402019-03-15 17:40:41 +053079
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070080// FlowInfo holds the flow information
Abhilash S.L8ee90712019-04-29 16:24:22 +053081type FlowInfo struct {
Girish Gowdraa09aeab2020-09-14 16:30:52 -070082 Flow *openolt.Flow
83 IsSymmtricFlow bool
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053084}
85
86// OnuGemInfo holds onu information along with gem port list and uni port list
87type OnuGemInfo struct {
88 OnuID uint32
89 SerialNumber string
90 IntfID uint32
91 GemPorts []uint32
92 UniPorts []uint32
93}
94
95// PacketInInfoKey is the key for packet in gemport
96type PacketInInfoKey struct {
97 IntfID uint32
98 OnuID uint32
99 LogicalPort uint32
Esin Karaman7fb80c22020-07-16 14:23:33 +0000100 VlanID uint16
101 Priority uint8
Abhilash S.L8ee90712019-04-29 16:24:22 +0530102}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700103
Esin Karamanccb714b2019-11-29 15:02:06 +0000104// GroupInfo holds group information
105type GroupInfo struct {
106 GroupID uint32
107 OutPorts []uint32
108}
109
Girish Gowdraa482f272021-03-24 23:04:19 -0700110// MeterInfo store meter information at path <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
111type MeterInfo struct {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700112 RefCnt uint8 // number of flow references for this meter. When RefCnt is 0, the MeterInfo should be deleted.
113 MeterID uint32
Girish Gowdraa482f272021-03-24 23:04:19 -0700114}
115
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700116// OpenOltResourceMgr holds resource related information as provided below for each field
Abhilash S.L7f17e402019-03-15 17:40:41 +0530117type OpenOltResourceMgr struct {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700118 PonIntfID uint32
Neha Sharma3f221ae2020-04-29 19:02:12 +0000119 DeviceID string // OLT device id
120 Address string // Host and port of the kv store to connect to
121 Args string // args
122 KVStore *db.Backend // backend kv store connection handle
123 DeviceType string
124 DevInfo *openolt.DeviceInfo // device information
Girish Gowdru0c588b22019-04-23 23:24:56 -0400125 // array of pon resource managers per interface technology
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700126 PonRsrMgr *ponrmgr.PONResourceManager
Girish Gowdra38d533d2020-03-30 20:38:51 -0700127
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700128 // Local maps used for write-through-cache - start
129 flowIDsForOnu map[string][]uint64
130 flowIDsForOnuLock sync.RWMutex
131
132 allocIDsForOnu map[string][]uint32
133 allocIDsForOnuLock sync.RWMutex
134
135 gemPortIDsForOnu map[string][]uint32
136 gemPortIDsForOnuLock sync.RWMutex
137
138 techProfileIDsForOnu map[string][]uint32
139 techProfileIDsForOnuLock sync.RWMutex
140
141 meterInfoForOnu map[string]*MeterInfo
142 meterInfoForOnuLock sync.RWMutex
143
144 onuGemInfo map[string]*OnuGemInfo
145 onuGemInfoLock sync.RWMutex
146
147 gemPortForPacketInInfo map[string]uint32
148 gemPortForPacketInInfoLock sync.RWMutex
149
150 flowIDsForGem map[uint32][]uint64
151 flowIDsForGemLock sync.RWMutex
152
153 mcastQueueForIntf map[uint32][]uint32
154 mcastQueueForIntfLock sync.RWMutex
155 mcastQueueForIntfLoadedFromKvStore bool
156
157 groupInfo map[string]*GroupInfo
158 groupInfoLock sync.RWMutex
159 // Local maps used for write-through-cache - end
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700160
Girish Gowdra76a1b092021-07-28 10:07:04 -0700161 TechprofileRef tp.TechProfileIf
Abhilash S.L7f17e402019-03-15 17:40:41 +0530162}
163
Neha Sharma96b7bf22020-06-15 10:37:32 +0000164func newKVClient(ctx context.Context, storeType string, address string, timeout time.Duration) (kvstore.Client, error) {
165 logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400166 switch storeType {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400167 case "etcd":
Neha Sharma96b7bf22020-06-15 10:37:32 +0000168 return kvstore.NewEtcdClient(ctx, address, timeout, log.FatalLevel)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400169 }
170 return nil, errors.New("unsupported-kv-store")
Abhilash S.L7f17e402019-03-15 17:40:41 +0530171}
172
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700173// SetKVClient sets the KV client and return a kv backend
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800174func SetKVClient(ctx context.Context, backend string, addr string, DeviceID string, basePathKvStore string) *db.Backend {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400175 // TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
176 // issue between kv store and backend , core is not calling NewBackend directly
Neha Sharma96b7bf22020-06-15 10:37:32 +0000177 kvClient, err := newKVClient(ctx, backend, addr, KvstoreTimeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400178 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000179 logger.Fatalw(ctx, "Failed to init KV client\n", log.Fields{"err": err})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400180 return nil
181 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700182 // return db.NewBackend(ctx, backend, addr, KvstoreTimeout, fmt.Sprintf(BasePathKvStore, basePathKvStore, DeviceID))
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700183
sbarbaria8910ba2019-11-05 10:12:23 -0500184 kvbackend := &db.Backend{
Girish Gowdru0c588b22019-04-23 23:24:56 -0400185 Client: kvClient,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700186 StoreType: backend,
Neha Sharma3f221ae2020-04-29 19:02:12 +0000187 Address: addr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700188 Timeout: KvstoreTimeout,
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800189 PathPrefix: fmt.Sprintf(BasePathKvStore, basePathKvStore, DeviceID)}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530190
Girish Gowdru0c588b22019-04-23 23:24:56 -0400191 return kvbackend
Abhilash S.L7f17e402019-03-15 17:40:41 +0530192}
193
Gamze Abakafee36392019-10-03 11:17:24 +0000194// NewResourceMgr init a New resource manager instance which in turn instantiates pon resource manager
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700195// instances according to technology. Initializes the default resource ranges for all
196// the resources.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700197func NewResourceMgr(ctx context.Context, PonIntfID uint32, deviceID string, KVStoreAddress string, kvStoreType string, deviceType string, devInfo *openolt.DeviceInfo, basePathKvStore string) *OpenOltResourceMgr {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400198 var ResourceMgr OpenOltResourceMgr
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700199 logger.Debugf(ctx, "Init new resource manager , ponIf: %v, address: %s, device-id: %s", PonIntfID, KVStoreAddress, deviceID)
200 ResourceMgr.PonIntfID = PonIntfID
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700201 ResourceMgr.DeviceID = deviceID
Neha Sharma3f221ae2020-04-29 19:02:12 +0000202 ResourceMgr.Address = KVStoreAddress
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700203 ResourceMgr.DeviceType = deviceType
204 ResourceMgr.DevInfo = devInfo
Abhilash S.L7f17e402019-03-15 17:40:41 +0530205
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700206 Backend := kvStoreType
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800207 ResourceMgr.KVStore = SetKVClient(ctx, Backend, ResourceMgr.Address, deviceID, basePathKvStore)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400208 if ResourceMgr.KVStore == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000209 logger.Error(ctx, "Failed to setup KV store")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400210 }
Girish Gowdra38d533d2020-03-30 20:38:51 -0700211
Girish Gowdru0c588b22019-04-23 23:24:56 -0400212 // TODO self.args = registry('main').get_args()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530213
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700214 // Create a separate Resource Manager instance for each range. This assumes that
Girish Gowdru0c588b22019-04-23 23:24:56 -0400215 // each technology is represented by only a single range
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700216 for _, TechRange := range devInfo.Ranges {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700217 for _, intfID := range TechRange.IntfIds {
218 if intfID == PonIntfID {
219 technology := TechRange.Technology
220 logger.Debugf(ctx, "Device info technology %s, intf-id %v", technology, PonIntfID)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000221
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700222 rsrMgr, err := ponrmgr.NewPONResourceManager(ctx, technology, deviceType, deviceID,
223 Backend, ResourceMgr.Address, basePathKvStore)
224 if err != nil {
225 logger.Errorf(ctx, "Failed to create pon resource manager instance for technology %s", technology)
226 return nil
227 }
228 ResourceMgr.PonRsrMgr = rsrMgr
229 // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
230 InitializeDeviceResourceRangeAndPool(ctx, rsrMgr, TechRange, devInfo)
231 if err := ResourceMgr.PonRsrMgr.InitDeviceResourcePoolForIntf(ctx, intfID); err != nil {
232 logger.Fatal(ctx, "failed-to-initialize-device-resource-pool-intf-id-%v-device-id", ResourceMgr.PonIntfID, ResourceMgr.DeviceID)
233 return nil
234 }
235 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400236 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400237 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700238
239 ResourceMgr.InitLocalCache()
240
Neha Sharma96b7bf22020-06-15 10:37:32 +0000241 logger.Info(ctx, "Initialization of resource manager success!")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400242 return &ResourceMgr
Abhilash S.L7f17e402019-03-15 17:40:41 +0530243}
244
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700245//InitLocalCache initializes local maps used for write-through-cache
246func (rsrcMgr *OpenOltResourceMgr) InitLocalCache() {
247 rsrcMgr.flowIDsForOnu = make(map[string][]uint64)
248 rsrcMgr.allocIDsForOnu = make(map[string][]uint32)
249 rsrcMgr.gemPortIDsForOnu = make(map[string][]uint32)
250 rsrcMgr.techProfileIDsForOnu = make(map[string][]uint32)
251 rsrcMgr.meterInfoForOnu = make(map[string]*MeterInfo)
252 rsrcMgr.onuGemInfo = make(map[string]*OnuGemInfo)
253 rsrcMgr.gemPortForPacketInInfo = make(map[string]uint32)
254 rsrcMgr.flowIDsForGem = make(map[uint32][]uint64)
255 rsrcMgr.mcastQueueForIntf = make(map[uint32][]uint32)
256 rsrcMgr.groupInfo = make(map[string]*GroupInfo)
257}
258
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700259// InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply
260// device specific information. If KV doesn't exist
261// or is broader than the device, the device's information will
262// dictate the range limits
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700263func InitializeDeviceResourceRangeAndPool(ctx context.Context, ponRMgr *ponrmgr.PONResourceManager,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700264 techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700265 // var ONUIDShared, AllocIDShared, GEMPortIDShared openolt.DeviceInfo_DeviceResourceRanges_Pool_SharingType
266 var ONUIDStart, ONUIDEnd, AllocIDStart, AllocIDEnd, GEMPortIDStart, GEMPortIDEnd uint32
267 var ONUIDShared, AllocIDShared, GEMPortIDShared, FlowIDShared uint32
268
269 // The below variables are just dummy and needed to pass as arguments to InitDefaultPONResourceRanges function.
270 // The openolt adapter does not need flowIDs to be managed as it is managed on the OLT device
271 // The UNI IDs are dynamically generated by openonu adapter for every discovered UNI.
272 var flowIDDummyStart, flowIDDummyEnd uint32 = 1, 2
273 var uniIDDummyStart, uniIDDummyEnd uint32 = 0, 1
Abhilash S.L7f17e402019-03-15 17:40:41 +0530274
Girish Gowdru0c588b22019-04-23 23:24:56 -0400275 // init the resource range pool according to the sharing type
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700276 logger.Debugw(ctx, "Device info init", log.Fields{"technology": techRange.Technology,
277 "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd,
278 "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd,
279 "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd,
280 "intf_ids": techRange.IntfIds,
281 })
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700282 for _, RangePool := range techRange.Pools {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700283 // FIXME: Remove hardcoding
Girish Gowdru0c588b22019-04-23 23:24:56 -0400284 if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID {
285 ONUIDStart = RangePool.Start
286 ONUIDEnd = RangePool.End
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700287 ONUIDShared = uint32(RangePool.Sharing)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400288 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID {
289 AllocIDStart = RangePool.Start
290 AllocIDEnd = RangePool.End
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700291 AllocIDShared = uint32(RangePool.Sharing)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400292 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID {
293 GEMPortIDStart = RangePool.Start
294 GEMPortIDEnd = RangePool.End
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700295 GEMPortIDShared = uint32(RangePool.Sharing)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400296 }
297 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530298
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700299 ponRMgr.InitDefaultPONResourceRanges(ctx, ONUIDStart, ONUIDEnd, ONUIDShared,
300 AllocIDStart, AllocIDEnd, AllocIDShared,
301 GEMPortIDStart, GEMPortIDEnd, GEMPortIDShared,
302 flowIDDummyStart, flowIDDummyEnd, FlowIDShared, uniIDDummyStart, uniIDDummyEnd,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700303 devInfo.PonPorts, techRange.IntfIds)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530304
Abhilash S.L7f17e402019-03-15 17:40:41 +0530305}
306
Devmalya Paul495b94a2019-08-27 19:42:00 -0400307// Delete clears used resources for the particular olt device being deleted
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700308func (rsrcMgr *OpenOltResourceMgr) Delete(ctx context.Context, intfID uint32) error {
309 if err := rsrcMgr.PonRsrMgr.ClearDeviceResourcePoolForIntf(ctx, intfID); err != nil {
310 logger.Debug(ctx, "Failed to clear device resource pool")
311 return err
Devmalya Paul495b94a2019-08-27 19:42:00 -0400312 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000313 logger.Debug(ctx, "Cleared device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400314 return nil
315}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530316
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700317// GetONUID returns the available onuID for the given pon-port
318func (rsrcMgr *OpenOltResourceMgr) GetONUID(ctx context.Context, PonIntfID uint32) (uint32, error) {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400319 // Get ONU id for a provided pon interface ID.
Girish Gowdra76a1b092021-07-28 10:07:04 -0700320 onuID, err := rsrcMgr.TechprofileRef.GetResourceID(ctx, PonIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400321 ponrmgr.ONU_ID, 1)
322 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000323 logger.Errorf(ctx, "Failed to get resource for interface %d for type %s",
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700324 PonIntfID, ponrmgr.ONU_ID)
cbabuabf02352019-10-15 13:14:56 +0200325 return 0, err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400326 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700327 if len(onuID) > 0 {
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700328 return onuID[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400329 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530330
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700331 return 0, err // return onuID 0 on error
Abhilash S.L8ee90712019-04-29 16:24:22 +0530332}
333
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700334// GetCurrentFlowIDsForOnu fetches flow ID from the resource manager
335// Note: For flows which trap from the NNI and not really associated with any particular
336// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700337func (rsrcMgr *OpenOltResourceMgr) GetCurrentFlowIDsForOnu(ctx context.Context, PonIntfID uint32, onuID int32, uniID int32) ([]uint64, error) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700338
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700339 subs := fmt.Sprintf("%d,%d,%d", PonIntfID, onuID, uniID)
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700340 path := fmt.Sprintf(FlowIDPath, subs)
341
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700342 // fetch from cache
343 rsrcMgr.flowIDsForOnuLock.RLock()
344 flowIDsForOnu, ok := rsrcMgr.flowIDsForOnu[path]
345 rsrcMgr.flowIDsForOnuLock.RUnlock()
346
347 if ok {
348 return flowIDsForOnu, nil
349 }
350
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700351 var data []uint64
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700352 value, err := rsrcMgr.KVStore.Get(ctx, path)
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700353 if err == nil {
354 if value != nil {
355 Val, _ := toByte(value.Value)
356 if err = json.Unmarshal(Val, &data); err != nil {
357 logger.Error(ctx, "Failed to unmarshal")
358 return nil, err
359 }
360 }
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700361 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700362 // update cache
363 rsrcMgr.flowIDsForOnuLock.Lock()
364 rsrcMgr.flowIDsForOnu[path] = data
365 rsrcMgr.flowIDsForOnuLock.Unlock()
366
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700367 return data, nil
Abhilash S.L8ee90712019-04-29 16:24:22 +0530368}
369
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700370// UpdateAllocIdsForOnu updates alloc ids in kv store for a given pon interface id, onu id and uni id
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700371func (rsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ctx context.Context, ponPort uint32, onuID uint32, uniID uint32, allocIDs []uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530372
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700373 intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
374 // update cache
375 rsrcMgr.allocIDsForOnuLock.Lock()
376 rsrcMgr.allocIDsForOnu[intfOnuIDuniID] = allocIDs
377 rsrcMgr.allocIDsForOnuLock.Unlock()
378
379 // Note: in case the write to DB fails there could be inconsistent data between cache and db.
380 // Although this is highly unlikely with DB retries in place, this is something we have to deal with in the next release
381 return rsrcMgr.PonRsrMgr.UpdateAllocIdsForOnu(ctx, intfOnuIDuniID,
382 allocIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530383}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700384
385// GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700386func (rsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(ctx context.Context, intfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700387 uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530388
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700389 intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530390
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700391 // fetch from cache
392 rsrcMgr.gemPortIDsForOnuLock.RLock()
393 gemIDs, ok := rsrcMgr.gemPortIDsForOnu[intfOnuIDuniID]
394 rsrcMgr.gemPortIDsForOnuLock.RUnlock()
395 if ok {
396 return gemIDs
397 }
398 /* Get gem ports for given pon interface , onu id and uni id. */
399 gemIDs = rsrcMgr.PonRsrMgr.GetCurrentGEMPortIDsForOnu(ctx, intfOnuIDuniID)
400
401 // update cache
402 rsrcMgr.gemPortIDsForOnuLock.Lock()
403 rsrcMgr.gemPortIDsForOnu[intfOnuIDuniID] = gemIDs
404 rsrcMgr.gemPortIDsForOnuLock.Unlock()
405
406 return gemIDs
Abhilash S.L7f17e402019-03-15 17:40:41 +0530407}
408
Gamze Abakafee36392019-10-03 11:17:24 +0000409// GetCurrentAllocIDsForOnu returns alloc ids for given pon interface and onu id
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700410func (rsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDsForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530411
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700412 intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
413 // fetch from cache
414 rsrcMgr.allocIDsForOnuLock.RLock()
415 allocIDs, ok := rsrcMgr.allocIDsForOnu[intfOnuIDuniID]
416 rsrcMgr.allocIDsForOnuLock.RUnlock()
417 if ok {
418 return allocIDs
Girish Gowdru0c588b22019-04-23 23:24:56 -0400419 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700420 allocIDs = rsrcMgr.PonRsrMgr.GetCurrentAllocIDForOnu(ctx, intfOnuIDuniID)
421
422 // update cache
423 rsrcMgr.allocIDsForOnuLock.Lock()
424 rsrcMgr.allocIDsForOnu[intfOnuIDuniID] = allocIDs
425 rsrcMgr.allocIDsForOnuLock.Unlock()
426
427 return allocIDs
Gamze Abakafee36392019-10-03 11:17:24 +0000428}
429
430// RemoveAllocIDForOnu removes the alloc id for given pon interface, onu id, uni id and alloc id
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700431func (rsrcMgr *OpenOltResourceMgr) RemoveAllocIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, allocID uint32) {
432 allocIDs := rsrcMgr.GetCurrentAllocIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000433 for i := 0; i < len(allocIDs); i++ {
434 if allocIDs[i] == allocID {
435 allocIDs = append(allocIDs[:i], allocIDs[i+1:]...)
436 break
437 }
438 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700439 err := rsrcMgr.UpdateAllocIdsForOnu(ctx, intfID, onuID, uniID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000440 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700441 logger.Errorf(ctx, "Failed to Remove Alloc Id For Onu. intfID %d onuID %d uniID %d allocID %d",
Gamze Abakafee36392019-10-03 11:17:24 +0000442 intfID, onuID, uniID, allocID)
443 }
444}
445
446// RemoveGemPortIDForOnu removes the gem port id for given pon interface, onu id, uni id and gem port id
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700447func (rsrcMgr *OpenOltResourceMgr) RemoveGemPortIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, gemPortID uint32) {
448 gemPortIDs := rsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000449 for i := 0; i < len(gemPortIDs); i++ {
450 if gemPortIDs[i] == gemPortID {
451 gemPortIDs = append(gemPortIDs[:i], gemPortIDs[i+1:]...)
452 break
453 }
454 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700455 err := rsrcMgr.UpdateGEMPortIDsForOnu(ctx, intfID, onuID, uniID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000456 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700457 logger.Errorf(ctx, "Failed to Remove Gem Id For Onu. intfID %d onuID %d uniID %d gemPortId %d",
Gamze Abakafee36392019-10-03 11:17:24 +0000458 intfID, onuID, uniID, gemPortID)
459 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530460}
461
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700462// UpdateGEMPortIDsForOnu updates gemport ids on to the kv store for a given pon port, onu id and uni id
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700463func (rsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ctx context.Context, ponPort uint32, onuID uint32,
464 uniID uint32, gemIDs []uint32) error {
465 intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
466 // update cache
467 rsrcMgr.gemPortIDsForOnuLock.Lock()
468 rsrcMgr.gemPortIDsForOnu[intfOnuIDuniID] = gemIDs
469 rsrcMgr.gemPortIDsForOnuLock.Unlock()
470
471 // Note: in case the write to DB fails there could be inconsistent data between cache and db.
472 // Although this is highly unlikely with DB retries in place, this is something we have to deal with in the next release
473 return rsrcMgr.PonRsrMgr.UpdateGEMPortIDsForOnu(ctx, intfOnuIDuniID,
474 gemIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530475
476}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700477
478// FreeonuID releases(make free) onu id for a particular pon-port
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700479func (rsrcMgr *OpenOltResourceMgr) FreeonuID(ctx context.Context, intfID uint32, onuID []uint32) {
Girish Gowdrab8f1b5a2021-06-27 20:42:40 -0700480 if len(onuID) == 0 {
481 logger.Info(ctx, "onu id slice is nil, nothing to free")
482 return
483 }
Girish Gowdra76a1b092021-07-28 10:07:04 -0700484 if err := rsrcMgr.TechprofileRef.FreeResourceID(ctx, intfID, ponrmgr.ONU_ID, onuID); err != nil {
Matteo Scandolo84585372021-03-18 14:21:22 -0700485 logger.Errorw(ctx, "error-while-freeing-onu-id", log.Fields{
486 "intf-id": intfID,
487 "onu-id": onuID,
488 "err": err.Error(),
489 })
Girish Gowdrab8f1b5a2021-06-27 20:42:40 -0700490 } else {
491 logger.Infow(ctx, "freed onu id", log.Fields{"intfID": intfID, "onuID": onuID})
Matteo Scandolo84585372021-03-18 14:21:22 -0700492 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530493}
494
Gamze Abakafee36392019-10-03 11:17:24 +0000495// FreeAllocID frees AllocID on the PON resource pool and also frees the allocID association
496// for the given OLT device.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700497// The caller should ensure that this is a blocking call and this operation is serialized for
498// the ONU so as not cause resource corruption since there are no mutexes used here.
499func (rsrcMgr *OpenOltResourceMgr) FreeAllocID(ctx context.Context, intfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000500 uniID uint32, allocID uint32) {
Girish Gowdrab77ded92020-04-08 11:45:05 -0700501
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700502 rsrcMgr.RemoveAllocIDForOnu(ctx, intfID, onuID, uniID, allocID)
Gamze Abakafee36392019-10-03 11:17:24 +0000503 allocIDs := make([]uint32, 0)
504 allocIDs = append(allocIDs, allocID)
Girish Gowdra76a1b092021-07-28 10:07:04 -0700505 if err := rsrcMgr.TechprofileRef.FreeResourceID(ctx, intfID, ponrmgr.ALLOC_ID, allocIDs); err != nil {
Matteo Scandolo84585372021-03-18 14:21:22 -0700506 logger.Errorw(ctx, "error-while-freeing-alloc-id", log.Fields{
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700507 "intf-id": intfID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700508 "onu-id": onuID,
509 "err": err.Error(),
510 })
511 }
Gamze Abakafee36392019-10-03 11:17:24 +0000512}
513
514// FreeGemPortID frees GemPortID on the PON resource pool and also frees the gemPortID association
515// for the given OLT device.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700516// The caller should ensure that this is a blocking call and this operation is serialized for
517// the ONU so as not cause resource corruption since there are no mutexes used here.
518func (rsrcMgr *OpenOltResourceMgr) FreeGemPortID(ctx context.Context, intfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000519 uniID uint32, gemPortID uint32) {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700520 rsrcMgr.RemoveGemPortIDForOnu(ctx, intfID, onuID, uniID, gemPortID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700521
Gamze Abakafee36392019-10-03 11:17:24 +0000522 gemPortIDs := make([]uint32, 0)
523 gemPortIDs = append(gemPortIDs, gemPortID)
Girish Gowdra76a1b092021-07-28 10:07:04 -0700524 if err := rsrcMgr.TechprofileRef.FreeResourceID(ctx, intfID, ponrmgr.GEMPORT_ID, gemPortIDs); err != nil {
Matteo Scandolo84585372021-03-18 14:21:22 -0700525 logger.Errorw(ctx, "error-while-freeing-gem-port-id", log.Fields{
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700526 "intf-id": intfID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700527 "onu-id": onuID,
528 "err": err.Error(),
529 })
530 }
Gamze Abakafee36392019-10-03 11:17:24 +0000531}
532
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700533// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id
534func (rsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530535
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700536 intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530537
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700538 AllocIDs := rsrcMgr.PonRsrMgr.GetCurrentAllocIDForOnu(ctx, intfOnuIDuniID)
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700539
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700540 rsrcMgr.allocIDsForOnuLock.Lock()
541 delete(rsrcMgr.allocIDsForOnu, intfOnuIDuniID)
542 rsrcMgr.allocIDsForOnuLock.Unlock()
543
Girish Gowdra76a1b092021-07-28 10:07:04 -0700544 if err := rsrcMgr.TechprofileRef.FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400545 ponrmgr.ALLOC_ID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700546 AllocIDs); err != nil {
547 logger.Errorw(ctx, "error-while-freeing-all-alloc-ids-for-onu", log.Fields{
548 "intf-id": intfID,
549 "onu-id": onuID,
550 "err": err.Error(),
551 })
552 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530553
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700554 GEMPortIDs := rsrcMgr.PonRsrMgr.GetCurrentGEMPortIDsForOnu(ctx, intfOnuIDuniID)
555
556 rsrcMgr.gemPortIDsForOnuLock.Lock()
557 delete(rsrcMgr.gemPortIDsForOnu, intfOnuIDuniID)
558 rsrcMgr.gemPortIDsForOnuLock.Unlock()
559
Girish Gowdra76a1b092021-07-28 10:07:04 -0700560 if err := rsrcMgr.TechprofileRef.FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400561 ponrmgr.GEMPORT_ID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700562 GEMPortIDs); err != nil {
563 logger.Errorw(ctx, "error-while-freeing-all-gem-port-ids-for-onu", log.Fields{
564 "intf-id": intfID,
565 "onu-id": onuID,
566 "err": err.Error(),
567 })
568 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530569
Girish Gowdru0c588b22019-04-23 23:24:56 -0400570 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700571 rsrcMgr.PonRsrMgr.RemoveResourceMap(ctx, intfOnuIDuniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530572}
573
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700574// IsFlowOnKvStore checks if the given flowID is present on the kv store
575// Returns true if the flowID is found, otherwise it returns false
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700576func (rsrcMgr *OpenOltResourceMgr) IsFlowOnKvStore(ctx context.Context, intfID uint32, onuID int32, uniID int32,
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700577 flowID uint64) bool {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530578
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700579 FlowIDs, err := rsrcMgr.GetCurrentFlowIDsForOnu(ctx, intfID, onuID, uniID)
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700580 if err != nil {
581 // error logged in the called function
582 return false
583 }
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400584 if FlowIDs != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700585 logger.Debugw(ctx, "Found flowId(s) for this ONU", log.Fields{"pon": intfID, "onuID": onuID, "uniID": uniID})
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700586 for _, id := range FlowIDs {
587 if flowID == id {
588 return true
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400589 }
590 }
591 }
592 return false
593}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400594
salmansiddiqui7ac62132019-08-22 03:58:50 +0000595// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700596// This path is formed as the following: {intfID, onuID, uniID}/tp_id
597func (rsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) []uint32 {
598 Path := fmt.Sprintf(tpIDPathSuffix, intfID, onuID, uniID)
599 // fetch from cache
600 rsrcMgr.techProfileIDsForOnuLock.RLock()
601 tpIDs, ok := rsrcMgr.techProfileIDsForOnu[Path]
602 rsrcMgr.techProfileIDsForOnuLock.RUnlock()
603 if ok {
604 return tpIDs
605 }
606 Value, err := rsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400607 if err == nil {
608 if Value != nil {
609 Val, err := kvstore.ToByte(Value.Value)
610 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700611 logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"err": err})
612 return tpIDs
Manikkaraj kb1d51442019-07-23 10:41:02 -0400613 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700614 if err = json.Unmarshal(Val, &tpIDs); err != nil {
615 logger.Error(ctx, "Failed to unmarshal", log.Fields{"err": err})
616 return tpIDs
Manikkaraj kb1d51442019-07-23 10:41:02 -0400617 }
618 }
619 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000620 logger.Errorf(ctx, "Failed to get TP id from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400621 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700622 logger.Debugf(ctx, "Getting TP id %d from path %s", tpIDs, Path)
623
624 // update cache
625 rsrcMgr.techProfileIDsForOnuLock.Lock()
626 rsrcMgr.techProfileIDsForOnu[Path] = tpIDs
627 rsrcMgr.techProfileIDsForOnuLock.Unlock()
628
629 return tpIDs
Manikkaraj kb1d51442019-07-23 10:41:02 -0400630
631}
632
Gamze Abakafee36392019-10-03 11:17:24 +0000633// 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 -0700634// This path is formed as the following: {intfID, onuID, uniID}/tp_id
635func (rsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDsForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) error {
636 intfOnuUniID := fmt.Sprintf(tpIDPathSuffix, intfID, onuID, uniID)
637 // update cache
638 rsrcMgr.techProfileIDsForOnuLock.Lock()
639 delete(rsrcMgr.techProfileIDsForOnu, intfOnuUniID)
640 rsrcMgr.techProfileIDsForOnuLock.Unlock()
641
642 if err := rsrcMgr.KVStore.Delete(ctx, intfOnuUniID); err != nil {
643 logger.Errorw(ctx, "Failed to delete techprofile id resource in KV store", log.Fields{"path": intfOnuUniID})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400644 return err
645 }
646 return nil
647}
648
Gamze Abakafee36392019-10-03 11:17:24 +0000649// 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 -0700650// This path is formed as the following: {intfID, onuID, uniID}/tp_id
651func (rsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, tpID uint32) error {
652 tpIDList := rsrcMgr.GetTechProfileIDForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000653 for i, tpIDInList := range tpIDList {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700654 if tpIDInList == tpID {
Gamze Abakafee36392019-10-03 11:17:24 +0000655 tpIDList = append(tpIDList[:i], tpIDList[i+1:]...)
656 }
657 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700658 intfOnuUniID := fmt.Sprintf(tpIDPathSuffix, intfID, onuID, uniID)
659 // update cache
660 rsrcMgr.techProfileIDsForOnuLock.Lock()
661 rsrcMgr.techProfileIDsForOnu[intfOnuUniID] = tpIDList
662 rsrcMgr.techProfileIDsForOnuLock.Unlock()
663
Gamze Abakafee36392019-10-03 11:17:24 +0000664 Value, err := json.Marshal(tpIDList)
665 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000666 logger.Error(ctx, "failed to Marshal")
Gamze Abakafee36392019-10-03 11:17:24 +0000667 return err
668 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700669 if err = rsrcMgr.KVStore.Put(ctx, intfOnuUniID, Value); err != nil {
670 logger.Errorf(ctx, "Failed to update resource %s", intfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000671 return err
672 }
673 return err
674}
675
676// UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700677// This path is formed as the following: {intfID, onuID, uniID}/tp_id
678func (rsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(ctx context.Context, intfID uint32, onuID uint32,
679 uniID uint32, tpID uint32) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400680 var Value []byte
681 var err error
682
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700683 intfOnuUniID := fmt.Sprintf(tpIDPathSuffix, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000684
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700685 tpIDList := rsrcMgr.GetTechProfileIDForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000686 for _, value := range tpIDList {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700687 if value == tpID {
688 logger.Debugf(ctx, "tpID %d is already in tpIdList for the path %s", tpID, intfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000689 return err
690 }
691 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700692 logger.Debugf(ctx, "updating tp id %d on path %s", tpID, intfOnuUniID)
693 tpIDList = append(tpIDList, tpID)
694
695 // update cache
696 rsrcMgr.techProfileIDsForOnuLock.Lock()
697 rsrcMgr.techProfileIDsForOnu[intfOnuUniID] = tpIDList
698 rsrcMgr.techProfileIDsForOnuLock.Unlock()
699
Gamze Abakafee36392019-10-03 11:17:24 +0000700 Value, err = json.Marshal(tpIDList)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400701 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000702 logger.Error(ctx, "failed to Marshal")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400703 return err
704 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700705 if err = rsrcMgr.KVStore.Put(ctx, intfOnuUniID, Value); err != nil {
706 logger.Errorf(ctx, "Failed to update resource %s", intfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400707 return err
708 }
709 return err
710}
711
Girish Gowdraa482f272021-03-24 23:04:19 -0700712// StoreMeterInfoForOnu updates the meter id in the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000713// 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 -0700714func (rsrcMgr *OpenOltResourceMgr) StoreMeterInfoForOnu(ctx context.Context, Direction string, intfID uint32, onuID uint32,
715 uniID uint32, tpID uint32, meterInfo *MeterInfo) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400716 var Value []byte
717 var err error
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700718 intfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, intfID, onuID, uniID, tpID, Direction)
719
720 // update cache
721 rsrcMgr.meterInfoForOnuLock.Lock()
722 rsrcMgr.meterInfoForOnu[intfOnuUniID] = meterInfo
723 rsrcMgr.meterInfoForOnuLock.Unlock()
724
Girish Gowdraa482f272021-03-24 23:04:19 -0700725 Value, err = json.Marshal(*meterInfo)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400726 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000727 logger.Error(ctx, "failed to Marshal meter config")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400728 return err
729 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700730 if err = rsrcMgr.KVStore.Put(ctx, intfOnuUniID, Value); err != nil {
731 logger.Errorf(ctx, "Failed to store meter into KV store %s", intfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400732 return err
733 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700734 logger.Debugw(ctx, "meter info updated successfully", log.Fields{"path": intfOnuUniID, "meter-info": meterInfo})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400735 return err
736}
737
Girish Gowdraa482f272021-03-24 23:04:19 -0700738// GetMeterInfoForOnu fetches the meter id from the kv store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000739// 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 -0700740func (rsrcMgr *OpenOltResourceMgr) GetMeterInfoForOnu(ctx context.Context, Direction string, intfID uint32, onuID uint32,
741 uniID uint32, tpID uint32) (*MeterInfo, error) {
742 Path := fmt.Sprintf(MeterIDPathSuffix, intfID, onuID, uniID, tpID, Direction)
743
744 // get from cache
745 rsrcMgr.meterInfoForOnuLock.RLock()
746 val, ok := rsrcMgr.meterInfoForOnu[Path]
747 rsrcMgr.meterInfoForOnuLock.RUnlock()
748 if ok {
749 return val, nil
750 }
751
Girish Gowdraa482f272021-03-24 23:04:19 -0700752 var meterInfo MeterInfo
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700753 Value, err := rsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400754 if err == nil {
755 if Value != nil {
Girish Gowdraa482f272021-03-24 23:04:19 -0700756 logger.Debug(ctx, "Found meter info in KV store", log.Fields{"Direction": Direction})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000757 Val, er := kvstore.ToByte(Value.Value)
758 if er != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700759 logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"err": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000760 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400761 }
Girish Gowdraa482f272021-03-24 23:04:19 -0700762 if er = json.Unmarshal(Val, &meterInfo); er != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700763 logger.Error(ctx, "Failed to unmarshal meter info", log.Fields{"err": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000764 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400765 }
766 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000767 logger.Debug(ctx, "meter-does-not-exists-in-KVStore")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400768 return nil, err
769 }
770 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000771 logger.Errorf(ctx, "Failed to get Meter config from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400772
773 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700774 // update cache
775 rsrcMgr.meterInfoForOnuLock.Lock()
776 rsrcMgr.meterInfoForOnu[Path] = &meterInfo
777 rsrcMgr.meterInfoForOnuLock.Unlock()
778
Girish Gowdraa482f272021-03-24 23:04:19 -0700779 return &meterInfo, err
Manikkaraj kb1d51442019-07-23 10:41:02 -0400780}
781
Girish Gowdraa482f272021-03-24 23:04:19 -0700782// HandleMeterInfoRefCntUpdate increments or decrements the reference counter for a given meter.
783// When reference count becomes 0, it clears the meter information from the kv store
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700784func (rsrcMgr *OpenOltResourceMgr) HandleMeterInfoRefCntUpdate(ctx context.Context, Direction string,
785 intfID uint32, onuID uint32, uniID uint32, tpID uint32, increment bool) error {
786 meterInfo, err := rsrcMgr.GetMeterInfoForOnu(ctx, Direction, intfID, onuID, uniID, tpID)
Girish Gowdra82c80982021-03-26 16:22:02 -0700787 if err != nil {
788 return err
789 } else if meterInfo == nil {
790 // If we are increasing the reference count, we expect the meter information to be present on KV store.
791 // But if decrementing the reference count, the meter is possibly already cleared from KV store. Just log warn but do not return error.
792 if increment {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700793 logger.Errorf(ctx, "error-fetching-meter-info-for-intf-%d-onu-%d-uni-%d-tp-id-%d-direction-%s", intfID, onuID, uniID, tpID, Direction)
794 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 -0700795 }
796 logger.Warnw(ctx, "meter is already cleared",
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700797 log.Fields{"intfID": intfID, "onuID": onuID, "uniID": uniID, "direction": Direction, "increment": increment})
Girish Gowdra82c80982021-03-26 16:22:02 -0700798 return nil
Girish Gowdraa482f272021-03-24 23:04:19 -0700799 }
Girish Gowdra82c80982021-03-26 16:22:02 -0700800
Girish Gowdraa482f272021-03-24 23:04:19 -0700801 if increment {
802 meterInfo.RefCnt++
803 } else {
804 meterInfo.RefCnt--
Girish Gowdra82c80982021-03-26 16:22:02 -0700805 // If RefCnt become 0 clear the meter information from the DB.
Girish Gowdraa482f272021-03-24 23:04:19 -0700806 if meterInfo.RefCnt == 0 {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700807 if err := rsrcMgr.RemoveMeterInfoForOnu(ctx, Direction, intfID, onuID, uniID, tpID); err != nil {
Girish Gowdraa482f272021-03-24 23:04:19 -0700808 return err
809 }
810 return nil
811 }
812 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700813 if err := rsrcMgr.StoreMeterInfoForOnu(ctx, Direction, intfID, onuID, uniID, tpID, meterInfo); err != nil {
Girish Gowdraa482f272021-03-24 23:04:19 -0700814 return err
815 }
816 return nil
817}
818
819// RemoveMeterInfoForOnu deletes the meter id from the kV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000820// 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 -0700821func (rsrcMgr *OpenOltResourceMgr) RemoveMeterInfoForOnu(ctx context.Context, Direction string, intfID uint32, onuID uint32,
822 uniID uint32, tpID uint32) error {
823 Path := fmt.Sprintf(MeterIDPathSuffix, intfID, onuID, uniID, tpID, Direction)
824
825 // update cache
826 rsrcMgr.meterInfoForOnuLock.Lock()
827 delete(rsrcMgr.meterInfoForOnu, Path)
828 rsrcMgr.meterInfoForOnuLock.Unlock()
829
830 if err := rsrcMgr.KVStore.Delete(ctx, Path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000831 logger.Errorf(ctx, "Failed to delete meter id %s from kvstore ", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400832 return err
833 }
834 return nil
835}
salmansiddiqui7ac62132019-08-22 03:58:50 +0000836
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700837//AddGemToOnuGemInfo adds gemport to onugem info kvstore and also local cache
838func (rsrcMgr *OpenOltResourceMgr) AddGemToOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, gemPort uint32) error {
839 onugem, err := rsrcMgr.GetOnuGemInfo(ctx, intfID, onuID)
840 if err != nil || onugem == nil || onugem.SerialNumber == "" {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000841 logger.Errorf(ctx, "failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530842 return err
843 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700844 if onugem.OnuID == onuID {
845 for _, gem := range onugem.GemPorts {
846 if gem == gemPort {
847 logger.Debugw(ctx, "Gem already present in onugem info, skpping addition", log.Fields{"gem": gem})
848 return nil
849 }
850 }
851 logger.Debugw(ctx, "Added gem to onugem info", log.Fields{"gem": gemPort})
852 onugem.GemPorts = append(onugem.GemPorts, gemPort)
853 } else {
854 logger.Errorw(ctx, "onu id in OnuGemInfo does not match", log.Fields{"onuID": onuID, "ponIf": intfID, "onuGemInfoOnuID": onugem.OnuID})
855 return fmt.Errorf("onu-id-in-OnuGemInfo-does-not-match-%v", onuID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530856 }
857
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700858 err = rsrcMgr.AddOnuGemInfo(ctx, intfID, onuID, *onugem)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530859 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000860 logger.Error(ctx, "Failed to add onugem to kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530861 return err
862 }
863 return err
864}
865
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700866//RemoveGemFromOnuGemInfo removes gemport from onugem info on kvstore and also local cache
867func (rsrcMgr *OpenOltResourceMgr) RemoveGemFromOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, gemPort uint32) error {
868 onugem, err := rsrcMgr.GetOnuGemInfo(ctx, intfID, onuID)
869 if err != nil || onugem == nil || onugem.SerialNumber == "" {
870 logger.Errorf(ctx, "failed to get onuifo for intfid %d", intfID)
871 return err
872 }
873 updated := false
874 if onugem.OnuID == onuID {
875 for i, gem := range onugem.GemPorts {
876 if gem == gemPort {
877 logger.Debugw(ctx, "Gem found, removing from onu gem info", log.Fields{"gem": gem})
878 onugem.GemPorts = append(onugem.GemPorts[:i], onugem.GemPorts[i+1:]...)
879 updated = true
880 break
881 }
882 }
883 } else {
884 logger.Errorw(ctx, "onu id in OnuGemInfo does not match", log.Fields{"onuID": onuID, "ponIf": intfID, "onuGemInfoOnuID": onugem.OnuID})
885 return fmt.Errorf("onu-id-in-OnuGemInfo-does-not-match-%v", onuID)
886 }
887 if updated {
888 err = rsrcMgr.AddOnuGemInfo(ctx, intfID, onuID, *onugem)
889 if err != nil {
890 logger.Error(ctx, "Failed to add onugem to kv store")
891 return err
892 }
893 } else {
894 logger.Debugw(ctx, "Gem port not found in onu gem info", log.Fields{"gem": gemPort})
895 }
896 return nil
897}
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530898
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700899//GetOnuGemInfo gets onu gem info from the kvstore per interface
900func (rsrcMgr *OpenOltResourceMgr) GetOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32) (*OnuGemInfo, error) {
901 var err error
902 var Val []byte
903 var onugem OnuGemInfo
904
905 path := fmt.Sprintf(OnuGemInfoPath, intfID, onuID)
906
907 rsrcMgr.onuGemInfoLock.RLock()
908 val, ok := rsrcMgr.onuGemInfo[path]
909 rsrcMgr.onuGemInfoLock.RUnlock()
910 if ok {
911 return val, nil
912 }
913 value, err := rsrcMgr.KVStore.Get(ctx, path)
914 if err != nil {
915 logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path})
916 return nil, err
917 } else if value == nil {
918 logger.Debug(ctx, "No onuinfo for path", log.Fields{"path": path})
919 return nil, nil // returning nil as this could happen if there are no onus for the interface yet
920 }
921 if Val, err = kvstore.ToByte(value.Value); err != nil {
922 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530923 return nil, err
924 }
925
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700926 if err = json.Unmarshal(Val, &onugem); err != nil {
927 logger.Error(ctx, "Failed to unmarshall")
928 return nil, err
929 }
930 logger.Debugw(ctx, "found onugem info from path", log.Fields{"path": path, "onuGemInfo": onugem})
931 rsrcMgr.onuGemInfoLock.Lock()
932 rsrcMgr.onuGemInfo[path] = &onugem
933 rsrcMgr.onuGemInfoLock.Unlock()
934
935 return &onugem, nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530936}
937
Chaitrashree G S1a55b882020-02-04 17:35:35 -0500938// AddOnuGemInfo adds onu info on to the kvstore per interface
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700939func (rsrcMgr *OpenOltResourceMgr) AddOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, onuGem OnuGemInfo) error {
940
941 var Value []byte
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530942 var err error
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700943 Path := fmt.Sprintf(OnuGemInfoPath, intfID, onuID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530944
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700945 rsrcMgr.onuGemInfoLock.Lock()
946 rsrcMgr.onuGemInfo[Path] = &onuGem
947 rsrcMgr.onuGemInfoLock.Unlock()
948
949 Value, err = json.Marshal(onuGem)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530950 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700951 logger.Error(ctx, "failed to Marshal")
952 return err
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530953 }
954
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700955 if err = rsrcMgr.KVStore.Put(ctx, Path, Value); err != nil {
956 logger.Errorf(ctx, "Failed to update resource %s", Path)
957 return err
958 }
Girish Gowdrabcf98af2021-07-01 08:24:42 -0700959 logger.Debugw(ctx, "added onu gem info to store", log.Fields{"onuGemInfo": onuGem})
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700960 return err
961}
962
963// DelOnuGemInfo deletes the onugem info from kvstore per ONU
964func (rsrcMgr *OpenOltResourceMgr) DelOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32) error {
965 path := fmt.Sprintf(OnuGemInfoPath, intfID, onuID)
966 rsrcMgr.onuGemInfoLock.Lock()
967 logger.Debugw(ctx, "removing onu gem info", log.Fields{"onuGemInfo": rsrcMgr.onuGemInfo[path]})
968 delete(rsrcMgr.onuGemInfo, path)
969 rsrcMgr.onuGemInfoLock.Unlock()
970
971 if err := rsrcMgr.KVStore.Delete(ctx, path); err != nil {
972 logger.Errorf(ctx, "failed to remove resource %s", path)
973 return err
974 }
Andrea Campanellab83b39d2020-03-30 11:41:16 +0200975 return nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530976}
977
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530978// 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 -0700979func (rsrcMgr *OpenOltResourceMgr) AddUniPortToOnuInfo(ctx context.Context, intfID uint32, onuID uint32, portNo uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530980
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700981 onugem, err := rsrcMgr.GetOnuGemInfo(ctx, intfID, onuID)
982 if err != nil || onugem == nil || onugem.SerialNumber == "" {
983 logger.Warnf(ctx, "failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530984 return
985 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700986
987 if onugem.OnuID == onuID {
988 for _, uni := range onugem.UniPorts {
989 if uni == portNo {
990 logger.Debugw(ctx, "uni already present in onugem info", log.Fields{"uni": portNo})
991 return
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530992 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530993 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700994 onugem.UniPorts = append(onugem.UniPorts, portNo)
995 } else {
996 logger.Warnw(ctx, "onu id mismatch in onu gem info", log.Fields{"intfID": intfID, "onuID": onuID})
997 return
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530998 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700999 err = rsrcMgr.AddOnuGemInfo(ctx, intfID, onuID, *onugem)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301000 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001001 logger.Errorw(ctx, "Failed to add uni port in onugem to kv store", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301002 return
1003 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301004}
1005
Esin Karaman7fb80c22020-07-16 14:23:33 +00001006//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 -07001007func (rsrcMgr *OpenOltResourceMgr) UpdateGemPortForPktIn(ctx context.Context, pktIn PacketInInfoKey, gemPort uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301008
Esin Karaman7fb80c22020-07-16 14:23:33 +00001009 path := fmt.Sprintf(OnuPacketINPath, pktIn.IntfID, pktIn.OnuID, pktIn.LogicalPort, pktIn.VlanID, pktIn.Priority)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001010 // update cache
1011 rsrcMgr.gemPortForPacketInInfoLock.Lock()
1012 rsrcMgr.gemPortForPacketInInfo[path] = gemPort
1013 rsrcMgr.gemPortForPacketInInfoLock.Unlock()
1014
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301015 Value, err := json.Marshal(gemPort)
1016 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001017 logger.Error(ctx, "Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301018 return
1019 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001020 if err = rsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001021 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"path": path, "value": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301022 return
1023 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001024 logger.Debugw(ctx, "added gem packet in successfully", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301025}
1026
Esin Karaman7fb80c22020-07-16 14:23:33 +00001027// 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 -07001028func (rsrcMgr *OpenOltResourceMgr) GetGemPortFromOnuPktIn(ctx context.Context, packetInInfoKey PacketInInfoKey) (uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301029
1030 var Val []byte
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301031
Esin Karaman7fb80c22020-07-16 14:23:33 +00001032 path := fmt.Sprintf(OnuPacketINPath, packetInInfoKey.IntfID, packetInInfoKey.OnuID, packetInInfoKey.LogicalPort,
1033 packetInInfoKey.VlanID, packetInInfoKey.Priority)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001034 // get from cache
1035 rsrcMgr.gemPortForPacketInInfoLock.RLock()
1036 gemPort, ok := rsrcMgr.gemPortForPacketInInfo[path]
1037 rsrcMgr.gemPortForPacketInInfoLock.RUnlock()
1038 if ok {
1039 logger.Debugw(ctx, "found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
1040 return gemPort, nil
1041 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301042
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001043 value, err := rsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301044 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001045 logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301046 return uint32(0), err
1047 } else if value == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001048 logger.Debugw(ctx, "No pkt in gem found", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301049 return uint32(0), nil
1050 }
1051
1052 if Val, err = kvstore.ToByte(value.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001053 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301054 return uint32(0), err
1055 }
1056 if err = json.Unmarshal(Val, &gemPort); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001057 logger.Error(ctx, "Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301058 return uint32(0), err
1059 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001060 logger.Debugw(ctx, "found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001061 // update cache
1062 rsrcMgr.gemPortForPacketInInfoLock.Lock()
1063 rsrcMgr.gemPortForPacketInInfo[path] = gemPort
1064 rsrcMgr.gemPortForPacketInInfoLock.Unlock()
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301065
1066 return gemPort, nil
1067}
1068
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001069//DeletePacketInGemPortForOnu deletes the packet-in gemport for ONU
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001070func (rsrcMgr *OpenOltResourceMgr) DeletePacketInGemPortForOnu(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) error {
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001071 path := fmt.Sprintf(OnuPacketINPathPrefix, intfID, onuID, logicalPort)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001072 value, err := rsrcMgr.KVStore.List(ctx, path)
Esin Karaman7fb80c22020-07-16 14:23:33 +00001073 if err != nil {
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001074 logger.Errorf(ctx, "failed-to-read-value-from-path-%s", path)
1075 return errors.New("failed-to-read-value-from-path-" + path)
Esin Karaman7fb80c22020-07-16 14:23:33 +00001076 }
Esin Karaman7fb80c22020-07-16 14:23:33 +00001077
1078 //remove them one by one
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001079 for key := range value {
serkant.uluderya4f5ed592020-12-17 20:57:59 +03001080 // Remove the PathPrefix from the path on KV key.
1081 // gemPortForPacketInInfo cache uses OnuPacketINPath as the key
1082 stringToBeReplaced := rsrcMgr.KVStore.PathPrefix + "/"
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001083 replacedWith := ""
1084 key = strings.Replace(key, stringToBeReplaced, replacedWith, 1)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001085 // update cache
1086 rsrcMgr.gemPortForPacketInInfoLock.Lock()
1087 delete(rsrcMgr.gemPortForPacketInInfo, key)
1088 rsrcMgr.gemPortForPacketInInfoLock.Unlock()
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001089
serkant.uluderya4f5ed592020-12-17 20:57:59 +03001090 logger.Debugw(ctx, "removed-key-from-packetin-gem-port-cache", log.Fields{"key": key, "cache-len": len(rsrcMgr.gemPortForPacketInInfo)})
1091 }
1092
1093 logger.Debugw(ctx, "delete-packetin-gem-port", log.Fields{"realPath": path})
1094 if err := rsrcMgr.KVStore.DeleteWithPrefix(ctx, path); err != nil {
1095 logger.Errorf(ctx, "failed-to-remove-resource-%s", path)
1096 return err
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301097 }
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001098
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301099 return nil
1100}
1101
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001102//GetFlowIDsForGem gets the list of FlowIDs for the given gemport
1103func (rsrcMgr *OpenOltResourceMgr) GetFlowIDsForGem(ctx context.Context, intf uint32, gem uint32) ([]uint64, error) {
1104 path := fmt.Sprintf(FlowIDsForGem, intf, gem)
1105
1106 // get from cache
1107 rsrcMgr.flowIDsForGemLock.RLock()
1108 flowIDs, ok := rsrcMgr.flowIDsForGem[gem]
1109 rsrcMgr.flowIDsForGemLock.RUnlock()
1110 if ok {
1111 return flowIDs, nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301112 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301113
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001114 value, err := rsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301115 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001116 logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path})
1117 return nil, err
1118 } else if value == nil {
1119 logger.Debug(ctx, "no flow-ids found", log.Fields{"path": path})
1120 return nil, nil
1121 }
1122 Val, err := kvstore.ToByte(value.Value)
1123 if err != nil {
1124 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301125 return nil, err
1126 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301127
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001128 if err = json.Unmarshal(Val, &flowIDs); err != nil {
1129 logger.Error(ctx, "Failed to unmarshall")
1130 return nil, err
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301131 }
1132
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001133 // update cache
1134 rsrcMgr.flowIDsForGemLock.Lock()
1135 rsrcMgr.flowIDsForGem[gem] = flowIDs
1136 rsrcMgr.flowIDsForGemLock.Unlock()
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301137
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001138 return flowIDs, nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301139}
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301140
1141//UpdateFlowIDsForGem updates flow id per gemport
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001142func (rsrcMgr *OpenOltResourceMgr) UpdateFlowIDsForGem(ctx context.Context, intf uint32, gem uint32, flowIDs []uint64) error {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301143 var val []byte
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001144 path := fmt.Sprintf(FlowIDsForGem, intf, gem)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301145
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001146 // update cache
1147 rsrcMgr.flowIDsForGemLock.Lock()
1148 rsrcMgr.flowIDsForGem[gem] = flowIDs
1149 rsrcMgr.flowIDsForGemLock.Unlock()
1150
1151 if flowIDs == nil {
1152 return nil
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301153 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001154 val, err := json.Marshal(flowIDs)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301155 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001156 logger.Error(ctx, "Failed to marshal data", log.Fields{"err": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301157 return err
1158 }
Girish Gowdrab77ded92020-04-08 11:45:05 -07001159
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001160 if err = rsrcMgr.KVStore.Put(ctx, path, val); err != nil {
1161 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"err": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301162 return err
1163 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001164 logger.Debugw(ctx, "added flowid list for gem to kv successfully", log.Fields{"path": path, "flowidlist": flowIDs})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301165 return nil
1166}
1167
1168//DeleteFlowIDsForGem deletes the flowID list entry per gem from kvstore.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001169func (rsrcMgr *OpenOltResourceMgr) DeleteFlowIDsForGem(ctx context.Context, intf uint32, gem uint32) {
1170 path := fmt.Sprintf(FlowIDsForGem, intf, gem)
1171 // update cache
1172 rsrcMgr.flowIDsForGemLock.Lock()
1173 delete(rsrcMgr.flowIDsForGem, gem)
1174 rsrcMgr.flowIDsForGemLock.Unlock()
1175 if err := rsrcMgr.KVStore.Delete(ctx, path); err != nil {
1176 logger.Errorw(ctx, "Failed to delete from kvstore", log.Fields{"err": err, "path": path})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301177 }
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301178}
Esin Karamanccb714b2019-11-29 15:02:06 +00001179
1180//GetMcastQueuePerInterfaceMap gets multicast queue info per pon interface
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001181func (rsrcMgr *OpenOltResourceMgr) GetMcastQueuePerInterfaceMap(ctx context.Context) (map[uint32][]uint32, error) {
Kent Hagermane6ff1012020-07-14 15:07:53 -04001182 path := McastQueuesForIntf
Esin Karamanccb714b2019-11-29 15:02:06 +00001183 var val []byte
1184
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001185 rsrcMgr.mcastQueueForIntfLock.RLock()
1186 if rsrcMgr.mcastQueueForIntfLoadedFromKvStore {
1187 rsrcMgr.mcastQueueForIntfLock.RUnlock()
1188 return rsrcMgr.mcastQueueForIntf, nil
1189 }
1190 rsrcMgr.mcastQueueForIntfLock.RUnlock()
1191
1192 kvPair, err := rsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001193 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001194 logger.Error(ctx, "failed to get data from kv store")
Esin Karamanccb714b2019-11-29 15:02:06 +00001195 return nil, err
1196 }
1197 if kvPair != nil && kvPair.Value != nil {
1198 if val, err = kvstore.ToByte(kvPair.Value); err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001199 logger.Error(ctx, "Failed to convert to byte array ", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001200 return nil, err
1201 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001202 rsrcMgr.mcastQueueForIntfLock.Lock()
1203 defer rsrcMgr.mcastQueueForIntfLock.Unlock()
1204 if err = json.Unmarshal(val, &rsrcMgr.mcastQueueForIntf); err != nil {
1205 logger.Error(ctx, "Failed to unmarshall ", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001206 return nil, err
1207 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001208 rsrcMgr.mcastQueueForIntfLoadedFromKvStore = true
Esin Karamanccb714b2019-11-29 15:02:06 +00001209 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001210 return rsrcMgr.mcastQueueForIntf, nil
Esin Karamanccb714b2019-11-29 15:02:06 +00001211}
1212
1213//AddMcastQueueForIntf adds multicast queue for pon interface
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001214func (rsrcMgr *OpenOltResourceMgr) AddMcastQueueForIntf(ctx context.Context, intf uint32, gem uint32, servicePriority uint32) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001215 var val []byte
Kent Hagermane6ff1012020-07-14 15:07:53 -04001216 path := McastQueuesForIntf
Esin Karamanccb714b2019-11-29 15:02:06 +00001217
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001218 // Load local cache from kv store the first time
1219 rsrcMgr.mcastQueueForIntfLock.RLock()
1220 if !rsrcMgr.mcastQueueForIntfLoadedFromKvStore {
1221 rsrcMgr.mcastQueueForIntfLock.RUnlock()
1222 _, err := rsrcMgr.GetMcastQueuePerInterfaceMap(ctx)
1223 if err != nil {
1224 logger.Errorw(ctx, "Failed to get multicast queue info for interface", log.Fields{"err": err, "intf": intf})
1225 return err
1226 }
1227 } else {
1228 rsrcMgr.mcastQueueForIntfLock.RUnlock()
1229 }
1230
1231 // Update KV store
1232 rsrcMgr.mcastQueueForIntfLock.Lock()
1233 rsrcMgr.mcastQueueForIntf[intf] = []uint32{gem, servicePriority}
1234 val, err := json.Marshal(rsrcMgr.mcastQueueForIntf)
Esin Karamanccb714b2019-11-29 15:02:06 +00001235 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001236 rsrcMgr.mcastQueueForIntfLock.Unlock()
1237 logger.Errorw(ctx, "Failed to marshal data", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001238 return err
1239 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001240 rsrcMgr.mcastQueueForIntfLock.Unlock()
1241
1242 if err = rsrcMgr.KVStore.Put(ctx, path, val); err != nil {
1243 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"err": err, "path": path, "value": val})
Esin Karamanccb714b2019-11-29 15:02:06 +00001244 return err
1245 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001246 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 +00001247 return nil
1248}
1249
1250//AddFlowGroupToKVStore adds flow group into KV store
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001251func (rsrcMgr *OpenOltResourceMgr) AddFlowGroupToKVStore(ctx context.Context, groupEntry *ofp.OfpGroupEntry, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001252 var Value []byte
1253 var err error
1254 var path string
1255 if cached {
1256 path = fmt.Sprintf(FlowGroupCached, groupEntry.Desc.GroupId)
1257 } else {
1258 path = fmt.Sprintf(FlowGroup, groupEntry.Desc.GroupId)
1259 }
1260 //build group info object
1261 var outPorts []uint32
1262 for _, ofBucket := range groupEntry.Desc.Buckets {
1263 for _, ofAction := range ofBucket.Actions {
1264 if ofAction.Type == ofp.OfpActionType_OFPAT_OUTPUT {
1265 outPorts = append(outPorts, ofAction.GetOutput().Port)
1266 }
1267 }
1268 }
1269 groupInfo := GroupInfo{
1270 GroupID: groupEntry.Desc.GroupId,
1271 OutPorts: outPorts,
1272 }
1273
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001274 rsrcMgr.groupInfoLock.Lock()
1275 rsrcMgr.groupInfo[path] = &groupInfo
1276 rsrcMgr.groupInfoLock.Unlock()
1277
Esin Karamanccb714b2019-11-29 15:02:06 +00001278 Value, err = json.Marshal(groupInfo)
1279
1280 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001281 logger.Error(ctx, "failed to Marshal flow group object")
Esin Karamanccb714b2019-11-29 15:02:06 +00001282 return err
1283 }
1284
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001285 if err = rsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001286 logger.Errorf(ctx, "Failed to update resource %s", path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001287 return err
1288 }
1289 return nil
1290}
1291
1292//RemoveFlowGroupFromKVStore removes flow group from KV store
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001293func (rsrcMgr *OpenOltResourceMgr) RemoveFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001294 var path string
1295 if cached {
1296 path = fmt.Sprintf(FlowGroupCached, groupID)
1297 } else {
1298 path = fmt.Sprintf(FlowGroup, groupID)
1299 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001300 rsrcMgr.groupInfoLock.Lock()
1301 delete(rsrcMgr.groupInfo, path)
1302 rsrcMgr.groupInfoLock.Unlock()
1303
1304 if err := rsrcMgr.KVStore.Delete(ctx, path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001305 logger.Errorf(ctx, "Failed to remove resource %s due to %s", path, err)
Esin Karamand519bbf2020-07-01 11:16:03 +00001306 return err
Esin Karamanccb714b2019-11-29 15:02:06 +00001307 }
Esin Karamand519bbf2020-07-01 11:16:03 +00001308 return nil
Esin Karamanccb714b2019-11-29 15:02:06 +00001309}
1310
1311//GetFlowGroupFromKVStore fetches flow group from the KV store. Returns (false, {} error) if any problem occurs during
1312//fetching the data. Returns (true, groupInfo, nil) if the group is fetched successfully.
1313// Returns (false, {}, nil) if the group does not exists in the KV store.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001314func (rsrcMgr *OpenOltResourceMgr) GetFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) (bool, GroupInfo, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001315 var groupInfo GroupInfo
1316 var path string
1317 if cached {
1318 path = fmt.Sprintf(FlowGroupCached, groupID)
1319 } else {
1320 path = fmt.Sprintf(FlowGroup, groupID)
1321 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001322
1323 // read from cache
1324 rsrcMgr.groupInfoLock.RLock()
1325 gi, ok := rsrcMgr.groupInfo[path]
1326 rsrcMgr.groupInfoLock.RUnlock()
1327 if ok {
1328 return true, *gi, nil
1329 }
1330
1331 kvPair, err := rsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001332 if err != nil {
1333 return false, groupInfo, err
1334 }
1335 if kvPair != nil && kvPair.Value != nil {
1336 Val, err := kvstore.ToByte(kvPair.Value)
1337 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001338 logger.Errorw(ctx, "Failed to convert flow group into byte array", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001339 return false, groupInfo, err
1340 }
1341 if err = json.Unmarshal(Val, &groupInfo); err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001342 logger.Errorw(ctx, "Failed to unmarshal", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001343 return false, groupInfo, err
1344 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001345 // update cache
1346 rsrcMgr.groupInfoLock.Lock()
1347 rsrcMgr.groupInfo[path] = &groupInfo
1348 rsrcMgr.groupInfoLock.Unlock()
1349
Esin Karamanccb714b2019-11-29 15:02:06 +00001350 return true, groupInfo, nil
1351 }
1352 return false, groupInfo, nil
1353}
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001354
1355// toByte converts an interface value to a []byte. The interface should either be of
1356// a string type or []byte. Otherwise, an error is returned.
1357func toByte(value interface{}) ([]byte, error) {
1358 switch t := value.(type) {
1359 case []byte:
1360 return value.([]byte), nil
1361 case string:
1362 return []byte(value.(string)), nil
1363 default:
1364 return nil, fmt.Errorf("unexpected-type-%T", t)
1365 }
1366}