blob: c256a3d241b4d04efb90000410f2b61129792ef1 [file] [log] [blame]
Abhilash S.L7f17e402019-03-15 17:40:41 +05301/*
2 * Copyright 2019-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070017//Package resourcemanager provides the utility for managing resources
manikkaraj kbf256be2019-03-25 00:13:48 +053018package resourcemanager
Abhilash S.L7f17e402019-03-15 17:40:41 +053019
20import (
npujarec5762e2020-01-01 14:08:48 +053021 "context"
Girish Gowdru0c588b22019-04-23 23:24:56 -040022 "encoding/json"
23 "errors"
24 "fmt"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070025 "strings"
Girish Gowdra38d533d2020-03-30 20:38:51 -070026 "sync"
Neha Sharmacc656962020-04-14 14:26:11 +000027 "time"
Abhilash S.L7f17e402019-03-15 17:40:41 +053028
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -070029 "github.com/opencord/voltha-lib-go/v5/pkg/db"
30 "github.com/opencord/voltha-lib-go/v5/pkg/db/kvstore"
31 "github.com/opencord/voltha-lib-go/v5/pkg/log"
32 ponrmgr "github.com/opencord/voltha-lib-go/v5/pkg/ponresourcemanager"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070033 ofp "github.com/opencord/voltha-protos/v4/go/openflow_13"
34 "github.com/opencord/voltha-protos/v4/go/openolt"
Abhilash S.L7f17e402019-03-15 17:40:41 +053035)
36
salmansiddiqui7ac62132019-08-22 03:58:50 +000037const (
38 // KvstoreTimeout specifies the time out for KV Store Connection
Neha Sharmacc656962020-04-14 14:26:11 +000039 KvstoreTimeout = 5 * time.Second
Matteo Scandolodfa7a972020-11-06 13:03:40 -080040 // BasePathKvStore - <pathPrefix>/openolt/<device_id>
41 BasePathKvStore = "%s/openolt/{%s}"
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -070042 // tpIDPathSuffix - <(pon_id, onu_id, uni_id)>/tp_id
43 tpIDPathSuffix = "{%d,%d,%d}/tp_id"
Gamze Abakafee36392019-10-03 11:17:24 +000044 //MeterIDPathSuffix - <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
45 MeterIDPathSuffix = "{%d,%d,%d}/{%d}/meter_id/{%s}"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070046 // OnuPacketINPathPrefix - path prefix where ONU packet-in vlanID/PCP is stored
47 //format: onu_packetin/{<intfid>,<onuid>,<logicalport>}
48 OnuPacketINPathPrefix = "onu_packetin/{%d,%d,%d}"
49 // OnuPacketINPath path on the kvstore to store packetin gemport,which will be used for packetin, packetout
50 //format: onu_packetin/{<intfid>,<onuid>,<logicalport>}/{<vlanId>,<priority>}
51 OnuPacketINPath = OnuPacketINPathPrefix + "/{%d,%d}"
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -070052 //FlowIDsForGem flowids_per_gem/<intfid>/<gemport-id>
53 FlowIDsForGem = "flowids_per_gem/{%d}/{%d}"
Esin Karamanccb714b2019-11-29 15:02:06 +000054 //McastQueuesForIntf multicast queues for pon interfaces
55 McastQueuesForIntf = "mcast_qs_for_int"
56 //FlowGroup flow_groups/<flow_group_id>
57 // A group is stored under this path on the KV store after it has been installed to the device.
58 // It should also be deleted after it has been removed from the device accordingly.
59 FlowGroup = "flow_groups/{%d}"
60 //FlowGroupCached flow_groups_cached/<flow_group_id>
61 // When a group add request received, we create the group without setting any members to it since we cannot
62 // set any members to a group until it is associated with a multicast flow. It is a BAL limitation.
63 // When the related multicast flow has been created we perform set members operation for the group.
64 // That is why we need to keep the members of a group until the multicast flow creation request comes.
65 // We preserve the groups under "FlowGroupsCached" directory in the KV store temporarily. Having set members,
66 // we remove the group from the cached group store.
67 FlowGroupCached = "flow_groups_cached/{%d}"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070068
69 //FlowIDPath - Path on the KV store for storing list of Flow IDs for a given subscriber
70 //Format: BasePathKvStore/<(pon_intf_id, onu_id, uni_id)>/flow_ids
71 FlowIDPath = "{%s}/flow_ids"
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -070072
73 //OnuGemInfoPath is path on the kvstore to store onugem info map
74 //format: <device-id>/onu_gem_info/<intfid>
75 OnuGemInfoPath = "onu_gem_info/{%d}/{%d}" // onu_gem/<intfid>/<onuID>
salmansiddiqui7ac62132019-08-22 03:58:50 +000076)
Abhilash S.L7f17e402019-03-15 17:40:41 +053077
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070078// FlowInfo holds the flow information
Abhilash S.L8ee90712019-04-29 16:24:22 +053079type FlowInfo struct {
Girish Gowdraa09aeab2020-09-14 16:30:52 -070080 Flow *openolt.Flow
81 IsSymmtricFlow bool
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053082}
83
84// OnuGemInfo holds onu information along with gem port list and uni port list
85type OnuGemInfo struct {
86 OnuID uint32
87 SerialNumber string
88 IntfID uint32
89 GemPorts []uint32
90 UniPorts []uint32
91}
92
93// PacketInInfoKey is the key for packet in gemport
94type PacketInInfoKey struct {
95 IntfID uint32
96 OnuID uint32
97 LogicalPort uint32
Esin Karaman7fb80c22020-07-16 14:23:33 +000098 VlanID uint16
99 Priority uint8
Abhilash S.L8ee90712019-04-29 16:24:22 +0530100}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700101
Esin Karamanccb714b2019-11-29 15:02:06 +0000102// GroupInfo holds group information
103type GroupInfo struct {
104 GroupID uint32
105 OutPorts []uint32
106}
107
Girish Gowdraa482f272021-03-24 23:04:19 -0700108// MeterInfo store meter information at path <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
109type MeterInfo struct {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700110 RefCnt uint8 // number of flow references for this meter. When RefCnt is 0, the MeterInfo should be deleted.
111 MeterID uint32
Girish Gowdraa482f272021-03-24 23:04:19 -0700112}
113
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700114// OpenOltResourceMgr holds resource related information as provided below for each field
Abhilash S.L7f17e402019-03-15 17:40:41 +0530115type OpenOltResourceMgr struct {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700116 PonIntfID uint32
Neha Sharma3f221ae2020-04-29 19:02:12 +0000117 DeviceID string // OLT device id
118 Address string // Host and port of the kv store to connect to
119 Args string // args
120 KVStore *db.Backend // backend kv store connection handle
121 DeviceType string
122 DevInfo *openolt.DeviceInfo // device information
Girish Gowdru0c588b22019-04-23 23:24:56 -0400123 // array of pon resource managers per interface technology
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700124 PonRsrMgr *ponrmgr.PONResourceManager
Girish Gowdra38d533d2020-03-30 20:38:51 -0700125
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700126 // Local maps used for write-through-cache - start
127 flowIDsForOnu map[string][]uint64
128 flowIDsForOnuLock sync.RWMutex
129
130 allocIDsForOnu map[string][]uint32
131 allocIDsForOnuLock sync.RWMutex
132
133 gemPortIDsForOnu map[string][]uint32
134 gemPortIDsForOnuLock sync.RWMutex
135
136 techProfileIDsForOnu map[string][]uint32
137 techProfileIDsForOnuLock sync.RWMutex
138
139 meterInfoForOnu map[string]*MeterInfo
140 meterInfoForOnuLock sync.RWMutex
141
142 onuGemInfo map[string]*OnuGemInfo
143 onuGemInfoLock sync.RWMutex
144
145 gemPortForPacketInInfo map[string]uint32
146 gemPortForPacketInInfoLock sync.RWMutex
147
148 flowIDsForGem map[uint32][]uint64
149 flowIDsForGemLock sync.RWMutex
150
151 mcastQueueForIntf map[uint32][]uint32
152 mcastQueueForIntfLock sync.RWMutex
153 mcastQueueForIntfLoadedFromKvStore bool
154
155 groupInfo map[string]*GroupInfo
156 groupInfoLock sync.RWMutex
157 // Local maps used for write-through-cache - end
Abhilash S.L7f17e402019-03-15 17:40:41 +0530158}
159
Neha Sharma96b7bf22020-06-15 10:37:32 +0000160func newKVClient(ctx context.Context, storeType string, address string, timeout time.Duration) (kvstore.Client, error) {
161 logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400162 switch storeType {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400163 case "etcd":
Neha Sharma96b7bf22020-06-15 10:37:32 +0000164 return kvstore.NewEtcdClient(ctx, address, timeout, log.FatalLevel)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400165 }
166 return nil, errors.New("unsupported-kv-store")
Abhilash S.L7f17e402019-03-15 17:40:41 +0530167}
168
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700169// SetKVClient sets the KV client and return a kv backend
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800170func SetKVClient(ctx context.Context, backend string, addr string, DeviceID string, basePathKvStore string) *db.Backend {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400171 // TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
172 // issue between kv store and backend , core is not calling NewBackend directly
Neha Sharma96b7bf22020-06-15 10:37:32 +0000173 kvClient, err := newKVClient(ctx, backend, addr, KvstoreTimeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400174 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000175 logger.Fatalw(ctx, "Failed to init KV client\n", log.Fields{"err": err})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400176 return nil
177 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700178 // return db.NewBackend(ctx, backend, addr, KvstoreTimeout, fmt.Sprintf(BasePathKvStore, basePathKvStore, DeviceID))
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700179
sbarbaria8910ba2019-11-05 10:12:23 -0500180 kvbackend := &db.Backend{
Girish Gowdru0c588b22019-04-23 23:24:56 -0400181 Client: kvClient,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700182 StoreType: backend,
Neha Sharma3f221ae2020-04-29 19:02:12 +0000183 Address: addr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700184 Timeout: KvstoreTimeout,
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800185 PathPrefix: fmt.Sprintf(BasePathKvStore, basePathKvStore, DeviceID)}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530186
Girish Gowdru0c588b22019-04-23 23:24:56 -0400187 return kvbackend
Abhilash S.L7f17e402019-03-15 17:40:41 +0530188}
189
Gamze Abakafee36392019-10-03 11:17:24 +0000190// NewResourceMgr init a New resource manager instance which in turn instantiates pon resource manager
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700191// instances according to technology. Initializes the default resource ranges for all
192// the resources.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700193func 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 -0400194 var ResourceMgr OpenOltResourceMgr
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700195 logger.Debugf(ctx, "Init new resource manager , ponIf: %v, address: %s, device-id: %s", PonIntfID, KVStoreAddress, deviceID)
196 ResourceMgr.PonIntfID = PonIntfID
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700197 ResourceMgr.DeviceID = deviceID
Neha Sharma3f221ae2020-04-29 19:02:12 +0000198 ResourceMgr.Address = KVStoreAddress
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700199 ResourceMgr.DeviceType = deviceType
200 ResourceMgr.DevInfo = devInfo
Abhilash S.L7f17e402019-03-15 17:40:41 +0530201
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700202 Backend := kvStoreType
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800203 ResourceMgr.KVStore = SetKVClient(ctx, Backend, ResourceMgr.Address, deviceID, basePathKvStore)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400204 if ResourceMgr.KVStore == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000205 logger.Error(ctx, "Failed to setup KV store")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400206 }
Girish Gowdra38d533d2020-03-30 20:38:51 -0700207
Girish Gowdru0c588b22019-04-23 23:24:56 -0400208 // TODO self.args = registry('main').get_args()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530209
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700210 // Create a separate Resource Manager instance for each range. This assumes that
Girish Gowdru0c588b22019-04-23 23:24:56 -0400211 // each technology is represented by only a single range
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700212 for _, TechRange := range devInfo.Ranges {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700213 for _, intfID := range TechRange.IntfIds {
214 if intfID == PonIntfID {
215 technology := TechRange.Technology
216 logger.Debugf(ctx, "Device info technology %s, intf-id %v", technology, PonIntfID)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000217
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700218 rsrMgr, err := ponrmgr.NewPONResourceManager(ctx, technology, deviceType, deviceID,
219 Backend, ResourceMgr.Address, basePathKvStore)
220 if err != nil {
221 logger.Errorf(ctx, "Failed to create pon resource manager instance for technology %s", technology)
222 return nil
223 }
224 ResourceMgr.PonRsrMgr = rsrMgr
225 // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
226 InitializeDeviceResourceRangeAndPool(ctx, rsrMgr, TechRange, devInfo)
227 if err := ResourceMgr.PonRsrMgr.InitDeviceResourcePoolForIntf(ctx, intfID); err != nil {
228 logger.Fatal(ctx, "failed-to-initialize-device-resource-pool-intf-id-%v-device-id", ResourceMgr.PonIntfID, ResourceMgr.DeviceID)
229 return nil
230 }
231 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400232 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400233 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700234
235 ResourceMgr.InitLocalCache()
236
Neha Sharma96b7bf22020-06-15 10:37:32 +0000237 logger.Info(ctx, "Initialization of resource manager success!")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400238 return &ResourceMgr
Abhilash S.L7f17e402019-03-15 17:40:41 +0530239}
240
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700241//InitLocalCache initializes local maps used for write-through-cache
242func (rsrcMgr *OpenOltResourceMgr) InitLocalCache() {
243 rsrcMgr.flowIDsForOnu = make(map[string][]uint64)
244 rsrcMgr.allocIDsForOnu = make(map[string][]uint32)
245 rsrcMgr.gemPortIDsForOnu = make(map[string][]uint32)
246 rsrcMgr.techProfileIDsForOnu = make(map[string][]uint32)
247 rsrcMgr.meterInfoForOnu = make(map[string]*MeterInfo)
248 rsrcMgr.onuGemInfo = make(map[string]*OnuGemInfo)
249 rsrcMgr.gemPortForPacketInInfo = make(map[string]uint32)
250 rsrcMgr.flowIDsForGem = make(map[uint32][]uint64)
251 rsrcMgr.mcastQueueForIntf = make(map[uint32][]uint32)
252 rsrcMgr.groupInfo = make(map[string]*GroupInfo)
253}
254
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700255// InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply
256// device specific information. If KV doesn't exist
257// or is broader than the device, the device's information will
258// dictate the range limits
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700259func InitializeDeviceResourceRangeAndPool(ctx context.Context, ponRMgr *ponrmgr.PONResourceManager,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700260 techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700261 // var ONUIDShared, AllocIDShared, GEMPortIDShared openolt.DeviceInfo_DeviceResourceRanges_Pool_SharingType
262 var ONUIDStart, ONUIDEnd, AllocIDStart, AllocIDEnd, GEMPortIDStart, GEMPortIDEnd uint32
263 var ONUIDShared, AllocIDShared, GEMPortIDShared, FlowIDShared uint32
264
265 // The below variables are just dummy and needed to pass as arguments to InitDefaultPONResourceRanges function.
266 // The openolt adapter does not need flowIDs to be managed as it is managed on the OLT device
267 // The UNI IDs are dynamically generated by openonu adapter for every discovered UNI.
268 var flowIDDummyStart, flowIDDummyEnd uint32 = 1, 2
269 var uniIDDummyStart, uniIDDummyEnd uint32 = 0, 1
Abhilash S.L7f17e402019-03-15 17:40:41 +0530270
Girish Gowdru0c588b22019-04-23 23:24:56 -0400271 // init the resource range pool according to the sharing type
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700272 logger.Debugw(ctx, "Device info init", log.Fields{"technology": techRange.Technology,
273 "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd,
274 "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd,
275 "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd,
276 "intf_ids": techRange.IntfIds,
277 })
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700278 for _, RangePool := range techRange.Pools {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700279 // FIXME: Remove hardcoding
Girish Gowdru0c588b22019-04-23 23:24:56 -0400280 if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID {
281 ONUIDStart = RangePool.Start
282 ONUIDEnd = RangePool.End
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700283 ONUIDShared = uint32(RangePool.Sharing)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400284 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID {
285 AllocIDStart = RangePool.Start
286 AllocIDEnd = RangePool.End
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700287 AllocIDShared = uint32(RangePool.Sharing)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400288 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID {
289 GEMPortIDStart = RangePool.Start
290 GEMPortIDEnd = RangePool.End
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700291 GEMPortIDShared = uint32(RangePool.Sharing)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400292 }
293 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530294
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700295 ponRMgr.InitDefaultPONResourceRanges(ctx, ONUIDStart, ONUIDEnd, ONUIDShared,
296 AllocIDStart, AllocIDEnd, AllocIDShared,
297 GEMPortIDStart, GEMPortIDEnd, GEMPortIDShared,
298 flowIDDummyStart, flowIDDummyEnd, FlowIDShared, uniIDDummyStart, uniIDDummyEnd,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700299 devInfo.PonPorts, techRange.IntfIds)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530300
Abhilash S.L7f17e402019-03-15 17:40:41 +0530301}
302
Devmalya Paul495b94a2019-08-27 19:42:00 -0400303// Delete clears used resources for the particular olt device being deleted
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700304func (rsrcMgr *OpenOltResourceMgr) Delete(ctx context.Context, intfID uint32) error {
305 if err := rsrcMgr.PonRsrMgr.ClearDeviceResourcePoolForIntf(ctx, intfID); err != nil {
306 logger.Debug(ctx, "Failed to clear device resource pool")
307 return err
Devmalya Paul495b94a2019-08-27 19:42:00 -0400308 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000309 logger.Debug(ctx, "Cleared device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400310 return nil
311}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530312
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700313// GetONUID returns the available onuID for the given pon-port
314func (rsrcMgr *OpenOltResourceMgr) GetONUID(ctx context.Context, PonIntfID uint32) (uint32, error) {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400315 // Get ONU id for a provided pon interface ID.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700316 onuID, err := rsrcMgr.PonRsrMgr.TechProfileMgr.GetResourceID(ctx, PonIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400317 ponrmgr.ONU_ID, 1)
318 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000319 logger.Errorf(ctx, "Failed to get resource for interface %d for type %s",
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700320 PonIntfID, ponrmgr.ONU_ID)
cbabuabf02352019-10-15 13:14:56 +0200321 return 0, err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400322 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700323 if len(onuID) > 0 {
324 rsrcMgr.PonRsrMgr.InitResourceMap(ctx, fmt.Sprintf("%d,%d", PonIntfID, onuID[0]))
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700325 return onuID[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400326 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530327
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700328 return 0, err // return onuID 0 on error
Abhilash S.L8ee90712019-04-29 16:24:22 +0530329}
330
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700331// GetCurrentFlowIDsForOnu fetches flow ID from the resource manager
332// Note: For flows which trap from the NNI and not really associated with any particular
333// 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 -0700334func (rsrcMgr *OpenOltResourceMgr) GetCurrentFlowIDsForOnu(ctx context.Context, PonIntfID uint32, onuID int32, uniID int32) ([]uint64, error) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700335
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700336 subs := fmt.Sprintf("%d,%d,%d", PonIntfID, onuID, uniID)
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700337 path := fmt.Sprintf(FlowIDPath, subs)
338
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700339 // fetch from cache
340 rsrcMgr.flowIDsForOnuLock.RLock()
341 flowIDsForOnu, ok := rsrcMgr.flowIDsForOnu[path]
342 rsrcMgr.flowIDsForOnuLock.RUnlock()
343
344 if ok {
345 return flowIDsForOnu, nil
346 }
347
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700348 var data []uint64
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700349 value, err := rsrcMgr.KVStore.Get(ctx, path)
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700350 if err == nil {
351 if value != nil {
352 Val, _ := toByte(value.Value)
353 if err = json.Unmarshal(Val, &data); err != nil {
354 logger.Error(ctx, "Failed to unmarshal")
355 return nil, err
356 }
357 }
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700358 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700359 // update cache
360 rsrcMgr.flowIDsForOnuLock.Lock()
361 rsrcMgr.flowIDsForOnu[path] = data
362 rsrcMgr.flowIDsForOnuLock.Unlock()
363
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700364 return data, nil
Abhilash S.L8ee90712019-04-29 16:24:22 +0530365}
366
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700367// 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 -0700368func (rsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ctx context.Context, ponPort uint32, onuID uint32, uniID uint32, allocIDs []uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530369
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700370 intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
371 // update cache
372 rsrcMgr.allocIDsForOnuLock.Lock()
373 rsrcMgr.allocIDsForOnu[intfOnuIDuniID] = allocIDs
374 rsrcMgr.allocIDsForOnuLock.Unlock()
375
376 // Note: in case the write to DB fails there could be inconsistent data between cache and db.
377 // Although this is highly unlikely with DB retries in place, this is something we have to deal with in the next release
378 return rsrcMgr.PonRsrMgr.UpdateAllocIdsForOnu(ctx, intfOnuIDuniID,
379 allocIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530380}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700381
382// GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700383func (rsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(ctx context.Context, intfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700384 uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530385
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700386 intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530387
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700388 // fetch from cache
389 rsrcMgr.gemPortIDsForOnuLock.RLock()
390 gemIDs, ok := rsrcMgr.gemPortIDsForOnu[intfOnuIDuniID]
391 rsrcMgr.gemPortIDsForOnuLock.RUnlock()
392 if ok {
393 return gemIDs
394 }
395 /* Get gem ports for given pon interface , onu id and uni id. */
396 gemIDs = rsrcMgr.PonRsrMgr.GetCurrentGEMPortIDsForOnu(ctx, intfOnuIDuniID)
397
398 // update cache
399 rsrcMgr.gemPortIDsForOnuLock.Lock()
400 rsrcMgr.gemPortIDsForOnu[intfOnuIDuniID] = gemIDs
401 rsrcMgr.gemPortIDsForOnuLock.Unlock()
402
403 return gemIDs
Abhilash S.L7f17e402019-03-15 17:40:41 +0530404}
405
Gamze Abakafee36392019-10-03 11:17:24 +0000406// GetCurrentAllocIDsForOnu returns alloc ids for given pon interface and onu id
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700407func (rsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDsForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530408
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700409 intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
410 // fetch from cache
411 rsrcMgr.allocIDsForOnuLock.RLock()
412 allocIDs, ok := rsrcMgr.allocIDsForOnu[intfOnuIDuniID]
413 rsrcMgr.allocIDsForOnuLock.RUnlock()
414 if ok {
415 return allocIDs
Girish Gowdru0c588b22019-04-23 23:24:56 -0400416 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700417 allocIDs = rsrcMgr.PonRsrMgr.GetCurrentAllocIDForOnu(ctx, intfOnuIDuniID)
418
419 // update cache
420 rsrcMgr.allocIDsForOnuLock.Lock()
421 rsrcMgr.allocIDsForOnu[intfOnuIDuniID] = allocIDs
422 rsrcMgr.allocIDsForOnuLock.Unlock()
423
424 return allocIDs
Gamze Abakafee36392019-10-03 11:17:24 +0000425}
426
427// RemoveAllocIDForOnu removes the alloc id for given pon interface, onu id, uni id and alloc id
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700428func (rsrcMgr *OpenOltResourceMgr) RemoveAllocIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, allocID uint32) {
429 allocIDs := rsrcMgr.GetCurrentAllocIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000430 for i := 0; i < len(allocIDs); i++ {
431 if allocIDs[i] == allocID {
432 allocIDs = append(allocIDs[:i], allocIDs[i+1:]...)
433 break
434 }
435 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700436 err := rsrcMgr.UpdateAllocIdsForOnu(ctx, intfID, onuID, uniID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000437 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700438 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 +0000439 intfID, onuID, uniID, allocID)
440 }
441}
442
443// 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 -0700444func (rsrcMgr *OpenOltResourceMgr) RemoveGemPortIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, gemPortID uint32) {
445 gemPortIDs := rsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000446 for i := 0; i < len(gemPortIDs); i++ {
447 if gemPortIDs[i] == gemPortID {
448 gemPortIDs = append(gemPortIDs[:i], gemPortIDs[i+1:]...)
449 break
450 }
451 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700452 err := rsrcMgr.UpdateGEMPortIDsForOnu(ctx, intfID, onuID, uniID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000453 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700454 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 +0000455 intfID, onuID, uniID, gemPortID)
456 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530457}
458
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700459// 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 -0700460func (rsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ctx context.Context, ponPort uint32, onuID uint32,
461 uniID uint32, gemIDs []uint32) error {
462 intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
463 // update cache
464 rsrcMgr.gemPortIDsForOnuLock.Lock()
465 rsrcMgr.gemPortIDsForOnu[intfOnuIDuniID] = gemIDs
466 rsrcMgr.gemPortIDsForOnuLock.Unlock()
467
468 // Note: in case the write to DB fails there could be inconsistent data between cache and db.
469 // Although this is highly unlikely with DB retries in place, this is something we have to deal with in the next release
470 return rsrcMgr.PonRsrMgr.UpdateGEMPortIDsForOnu(ctx, intfOnuIDuniID,
471 gemIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530472
473}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700474
475// FreeonuID releases(make free) onu id for a particular pon-port
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700476func (rsrcMgr *OpenOltResourceMgr) FreeonuID(ctx context.Context, intfID uint32, onuID []uint32) {
Girish Gowdrab8f1b5a2021-06-27 20:42:40 -0700477 if len(onuID) == 0 {
478 logger.Info(ctx, "onu id slice is nil, nothing to free")
479 return
480 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700481 if err := rsrcMgr.PonRsrMgr.TechProfileMgr.FreeResourceID(ctx, intfID, ponrmgr.ONU_ID, onuID); err != nil {
Matteo Scandolo84585372021-03-18 14:21:22 -0700482 logger.Errorw(ctx, "error-while-freeing-onu-id", log.Fields{
483 "intf-id": intfID,
484 "onu-id": onuID,
485 "err": err.Error(),
486 })
Girish Gowdrab8f1b5a2021-06-27 20:42:40 -0700487 } else {
488 logger.Infow(ctx, "freed onu id", log.Fields{"intfID": intfID, "onuID": onuID})
Matteo Scandolo84585372021-03-18 14:21:22 -0700489 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530490
Girish Gowdru0c588b22019-04-23 23:24:56 -0400491 /* Free onu id for a particular interface.*/
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700492 var IntfonuID string
493 for _, onu := range onuID {
494 IntfonuID = fmt.Sprintf("%d,%d", intfID, onu)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700495 rsrcMgr.PonRsrMgr.RemoveResourceMap(ctx, IntfonuID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400496 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530497}
498
Gamze Abakafee36392019-10-03 11:17:24 +0000499// FreeAllocID frees AllocID on the PON resource pool and also frees the allocID association
500// for the given OLT device.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700501// The caller should ensure that this is a blocking call and this operation is serialized for
502// the ONU so as not cause resource corruption since there are no mutexes used here.
503func (rsrcMgr *OpenOltResourceMgr) FreeAllocID(ctx context.Context, intfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000504 uniID uint32, allocID uint32) {
Girish Gowdrab77ded92020-04-08 11:45:05 -0700505
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700506 rsrcMgr.RemoveAllocIDForOnu(ctx, intfID, onuID, uniID, allocID)
Gamze Abakafee36392019-10-03 11:17:24 +0000507 allocIDs := make([]uint32, 0)
508 allocIDs = append(allocIDs, allocID)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700509 if err := rsrcMgr.PonRsrMgr.TechProfileMgr.FreeResourceID(ctx, intfID, ponrmgr.ALLOC_ID, allocIDs); err != nil {
Matteo Scandolo84585372021-03-18 14:21:22 -0700510 logger.Errorw(ctx, "error-while-freeing-alloc-id", log.Fields{
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700511 "intf-id": intfID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700512 "onu-id": onuID,
513 "err": err.Error(),
514 })
515 }
Gamze Abakafee36392019-10-03 11:17:24 +0000516}
517
518// FreeGemPortID frees GemPortID on the PON resource pool and also frees the gemPortID association
519// for the given OLT device.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700520// The caller should ensure that this is a blocking call and this operation is serialized for
521// the ONU so as not cause resource corruption since there are no mutexes used here.
522func (rsrcMgr *OpenOltResourceMgr) FreeGemPortID(ctx context.Context, intfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000523 uniID uint32, gemPortID uint32) {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700524 rsrcMgr.RemoveGemPortIDForOnu(ctx, intfID, onuID, uniID, gemPortID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700525
Gamze Abakafee36392019-10-03 11:17:24 +0000526 gemPortIDs := make([]uint32, 0)
527 gemPortIDs = append(gemPortIDs, gemPortID)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700528 if err := rsrcMgr.PonRsrMgr.TechProfileMgr.FreeResourceID(ctx, intfID, ponrmgr.GEMPORT_ID, gemPortIDs); err != nil {
Matteo Scandolo84585372021-03-18 14:21:22 -0700529 logger.Errorw(ctx, "error-while-freeing-gem-port-id", log.Fields{
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700530 "intf-id": intfID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700531 "onu-id": onuID,
532 "err": err.Error(),
533 })
534 }
Gamze Abakafee36392019-10-03 11:17:24 +0000535}
536
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700537// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id
538func (rsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530539
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700540 intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530541
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700542 AllocIDs := rsrcMgr.PonRsrMgr.GetCurrentAllocIDForOnu(ctx, intfOnuIDuniID)
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700543
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700544 rsrcMgr.allocIDsForOnuLock.Lock()
545 delete(rsrcMgr.allocIDsForOnu, intfOnuIDuniID)
546 rsrcMgr.allocIDsForOnuLock.Unlock()
547
548 if err := rsrcMgr.PonRsrMgr.TechProfileMgr.FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400549 ponrmgr.ALLOC_ID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700550 AllocIDs); err != nil {
551 logger.Errorw(ctx, "error-while-freeing-all-alloc-ids-for-onu", log.Fields{
552 "intf-id": intfID,
553 "onu-id": onuID,
554 "err": err.Error(),
555 })
556 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530557
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700558 GEMPortIDs := rsrcMgr.PonRsrMgr.GetCurrentGEMPortIDsForOnu(ctx, intfOnuIDuniID)
559
560 rsrcMgr.gemPortIDsForOnuLock.Lock()
561 delete(rsrcMgr.gemPortIDsForOnu, intfOnuIDuniID)
562 rsrcMgr.gemPortIDsForOnuLock.Unlock()
563
564 if err := rsrcMgr.PonRsrMgr.TechProfileMgr.FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400565 ponrmgr.GEMPORT_ID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700566 GEMPortIDs); err != nil {
567 logger.Errorw(ctx, "error-while-freeing-all-gem-port-ids-for-onu", log.Fields{
568 "intf-id": intfID,
569 "onu-id": onuID,
570 "err": err.Error(),
571 })
572 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530573
Girish Gowdru0c588b22019-04-23 23:24:56 -0400574 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700575 rsrcMgr.PonRsrMgr.RemoveResourceMap(ctx, intfOnuIDuniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530576}
577
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700578// IsFlowOnKvStore checks if the given flowID is present on the kv store
579// Returns true if the flowID is found, otherwise it returns false
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700580func (rsrcMgr *OpenOltResourceMgr) IsFlowOnKvStore(ctx context.Context, intfID uint32, onuID int32, uniID int32,
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700581 flowID uint64) bool {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530582
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700583 FlowIDs, err := rsrcMgr.GetCurrentFlowIDsForOnu(ctx, intfID, onuID, uniID)
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700584 if err != nil {
585 // error logged in the called function
586 return false
587 }
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400588 if FlowIDs != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700589 logger.Debugw(ctx, "Found flowId(s) for this ONU", log.Fields{"pon": intfID, "onuID": onuID, "uniID": uniID})
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700590 for _, id := range FlowIDs {
591 if flowID == id {
592 return true
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400593 }
594 }
595 }
596 return false
597}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400598
salmansiddiqui7ac62132019-08-22 03:58:50 +0000599// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700600// This path is formed as the following: {intfID, onuID, uniID}/tp_id
601func (rsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) []uint32 {
602 Path := fmt.Sprintf(tpIDPathSuffix, intfID, onuID, uniID)
603 // fetch from cache
604 rsrcMgr.techProfileIDsForOnuLock.RLock()
605 tpIDs, ok := rsrcMgr.techProfileIDsForOnu[Path]
606 rsrcMgr.techProfileIDsForOnuLock.RUnlock()
607 if ok {
608 return tpIDs
609 }
610 Value, err := rsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400611 if err == nil {
612 if Value != nil {
613 Val, err := kvstore.ToByte(Value.Value)
614 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700615 logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"err": err})
616 return tpIDs
Manikkaraj kb1d51442019-07-23 10:41:02 -0400617 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700618 if err = json.Unmarshal(Val, &tpIDs); err != nil {
619 logger.Error(ctx, "Failed to unmarshal", log.Fields{"err": err})
620 return tpIDs
Manikkaraj kb1d51442019-07-23 10:41:02 -0400621 }
622 }
623 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000624 logger.Errorf(ctx, "Failed to get TP id from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400625 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700626 logger.Debugf(ctx, "Getting TP id %d from path %s", tpIDs, Path)
627
628 // update cache
629 rsrcMgr.techProfileIDsForOnuLock.Lock()
630 rsrcMgr.techProfileIDsForOnu[Path] = tpIDs
631 rsrcMgr.techProfileIDsForOnuLock.Unlock()
632
633 return tpIDs
Manikkaraj kb1d51442019-07-23 10:41:02 -0400634
635}
636
Gamze Abakafee36392019-10-03 11:17:24 +0000637// 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 -0700638// This path is formed as the following: {intfID, onuID, uniID}/tp_id
639func (rsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDsForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) error {
640 intfOnuUniID := fmt.Sprintf(tpIDPathSuffix, intfID, onuID, uniID)
641 // update cache
642 rsrcMgr.techProfileIDsForOnuLock.Lock()
643 delete(rsrcMgr.techProfileIDsForOnu, intfOnuUniID)
644 rsrcMgr.techProfileIDsForOnuLock.Unlock()
645
646 if err := rsrcMgr.KVStore.Delete(ctx, intfOnuUniID); err != nil {
647 logger.Errorw(ctx, "Failed to delete techprofile id resource in KV store", log.Fields{"path": intfOnuUniID})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400648 return err
649 }
650 return nil
651}
652
Gamze Abakafee36392019-10-03 11:17:24 +0000653// 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 -0700654// This path is formed as the following: {intfID, onuID, uniID}/tp_id
655func (rsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, tpID uint32) error {
656 tpIDList := rsrcMgr.GetTechProfileIDForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000657 for i, tpIDInList := range tpIDList {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700658 if tpIDInList == tpID {
Gamze Abakafee36392019-10-03 11:17:24 +0000659 tpIDList = append(tpIDList[:i], tpIDList[i+1:]...)
660 }
661 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700662 intfOnuUniID := fmt.Sprintf(tpIDPathSuffix, intfID, onuID, uniID)
663 // update cache
664 rsrcMgr.techProfileIDsForOnuLock.Lock()
665 rsrcMgr.techProfileIDsForOnu[intfOnuUniID] = tpIDList
666 rsrcMgr.techProfileIDsForOnuLock.Unlock()
667
Gamze Abakafee36392019-10-03 11:17:24 +0000668 Value, err := json.Marshal(tpIDList)
669 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000670 logger.Error(ctx, "failed to Marshal")
Gamze Abakafee36392019-10-03 11:17:24 +0000671 return err
672 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700673 if err = rsrcMgr.KVStore.Put(ctx, intfOnuUniID, Value); err != nil {
674 logger.Errorf(ctx, "Failed to update resource %s", intfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000675 return err
676 }
677 return err
678}
679
680// UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700681// This path is formed as the following: {intfID, onuID, uniID}/tp_id
682func (rsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(ctx context.Context, intfID uint32, onuID uint32,
683 uniID uint32, tpID uint32) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400684 var Value []byte
685 var err error
686
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700687 intfOnuUniID := fmt.Sprintf(tpIDPathSuffix, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000688
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700689 tpIDList := rsrcMgr.GetTechProfileIDForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000690 for _, value := range tpIDList {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700691 if value == tpID {
692 logger.Debugf(ctx, "tpID %d is already in tpIdList for the path %s", tpID, intfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000693 return err
694 }
695 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700696 logger.Debugf(ctx, "updating tp id %d on path %s", tpID, intfOnuUniID)
697 tpIDList = append(tpIDList, tpID)
698
699 // update cache
700 rsrcMgr.techProfileIDsForOnuLock.Lock()
701 rsrcMgr.techProfileIDsForOnu[intfOnuUniID] = tpIDList
702 rsrcMgr.techProfileIDsForOnuLock.Unlock()
703
Gamze Abakafee36392019-10-03 11:17:24 +0000704 Value, err = json.Marshal(tpIDList)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400705 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000706 logger.Error(ctx, "failed to Marshal")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400707 return err
708 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700709 if err = rsrcMgr.KVStore.Put(ctx, intfOnuUniID, Value); err != nil {
710 logger.Errorf(ctx, "Failed to update resource %s", intfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400711 return err
712 }
713 return err
714}
715
Girish Gowdraa482f272021-03-24 23:04:19 -0700716// StoreMeterInfoForOnu updates the meter id in the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000717// 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 -0700718func (rsrcMgr *OpenOltResourceMgr) StoreMeterInfoForOnu(ctx context.Context, Direction string, intfID uint32, onuID uint32,
719 uniID uint32, tpID uint32, meterInfo *MeterInfo) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400720 var Value []byte
721 var err error
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700722 intfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, intfID, onuID, uniID, tpID, Direction)
723
724 // update cache
725 rsrcMgr.meterInfoForOnuLock.Lock()
726 rsrcMgr.meterInfoForOnu[intfOnuUniID] = meterInfo
727 rsrcMgr.meterInfoForOnuLock.Unlock()
728
Girish Gowdraa482f272021-03-24 23:04:19 -0700729 Value, err = json.Marshal(*meterInfo)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400730 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000731 logger.Error(ctx, "failed to Marshal meter config")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400732 return err
733 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700734 if err = rsrcMgr.KVStore.Put(ctx, intfOnuUniID, Value); err != nil {
735 logger.Errorf(ctx, "Failed to store meter into KV store %s", intfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400736 return err
737 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700738 logger.Debugw(ctx, "meter info updated successfully", log.Fields{"path": intfOnuUniID, "meter-info": meterInfo})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400739 return err
740}
741
Girish Gowdraa482f272021-03-24 23:04:19 -0700742// GetMeterInfoForOnu fetches the meter id from the kv store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000743// 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 -0700744func (rsrcMgr *OpenOltResourceMgr) GetMeterInfoForOnu(ctx context.Context, Direction string, intfID uint32, onuID uint32,
745 uniID uint32, tpID uint32) (*MeterInfo, error) {
746 Path := fmt.Sprintf(MeterIDPathSuffix, intfID, onuID, uniID, tpID, Direction)
747
748 // get from cache
749 rsrcMgr.meterInfoForOnuLock.RLock()
750 val, ok := rsrcMgr.meterInfoForOnu[Path]
751 rsrcMgr.meterInfoForOnuLock.RUnlock()
752 if ok {
753 return val, nil
754 }
755
Girish Gowdraa482f272021-03-24 23:04:19 -0700756 var meterInfo MeterInfo
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700757 Value, err := rsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400758 if err == nil {
759 if Value != nil {
Girish Gowdraa482f272021-03-24 23:04:19 -0700760 logger.Debug(ctx, "Found meter info in KV store", log.Fields{"Direction": Direction})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000761 Val, er := kvstore.ToByte(Value.Value)
762 if er != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700763 logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"err": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000764 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400765 }
Girish Gowdraa482f272021-03-24 23:04:19 -0700766 if er = json.Unmarshal(Val, &meterInfo); er != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700767 logger.Error(ctx, "Failed to unmarshal meter info", log.Fields{"err": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000768 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400769 }
770 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000771 logger.Debug(ctx, "meter-does-not-exists-in-KVStore")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400772 return nil, err
773 }
774 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000775 logger.Errorf(ctx, "Failed to get Meter config from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400776
777 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700778 // update cache
779 rsrcMgr.meterInfoForOnuLock.Lock()
780 rsrcMgr.meterInfoForOnu[Path] = &meterInfo
781 rsrcMgr.meterInfoForOnuLock.Unlock()
782
Girish Gowdraa482f272021-03-24 23:04:19 -0700783 return &meterInfo, err
Manikkaraj kb1d51442019-07-23 10:41:02 -0400784}
785
Girish Gowdraa482f272021-03-24 23:04:19 -0700786// HandleMeterInfoRefCntUpdate increments or decrements the reference counter for a given meter.
787// When reference count becomes 0, it clears the meter information from the kv store
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700788func (rsrcMgr *OpenOltResourceMgr) HandleMeterInfoRefCntUpdate(ctx context.Context, Direction string,
789 intfID uint32, onuID uint32, uniID uint32, tpID uint32, increment bool) error {
790 meterInfo, err := rsrcMgr.GetMeterInfoForOnu(ctx, Direction, intfID, onuID, uniID, tpID)
Girish Gowdra82c80982021-03-26 16:22:02 -0700791 if err != nil {
792 return err
793 } else if meterInfo == nil {
794 // If we are increasing the reference count, we expect the meter information to be present on KV store.
795 // But if decrementing the reference count, the meter is possibly already cleared from KV store. Just log warn but do not return error.
796 if increment {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700797 logger.Errorf(ctx, "error-fetching-meter-info-for-intf-%d-onu-%d-uni-%d-tp-id-%d-direction-%s", intfID, onuID, uniID, tpID, Direction)
798 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 -0700799 }
800 logger.Warnw(ctx, "meter is already cleared",
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700801 log.Fields{"intfID": intfID, "onuID": onuID, "uniID": uniID, "direction": Direction, "increment": increment})
Girish Gowdra82c80982021-03-26 16:22:02 -0700802 return nil
Girish Gowdraa482f272021-03-24 23:04:19 -0700803 }
Girish Gowdra82c80982021-03-26 16:22:02 -0700804
Girish Gowdraa482f272021-03-24 23:04:19 -0700805 if increment {
806 meterInfo.RefCnt++
807 } else {
808 meterInfo.RefCnt--
Girish Gowdra82c80982021-03-26 16:22:02 -0700809 // If RefCnt become 0 clear the meter information from the DB.
Girish Gowdraa482f272021-03-24 23:04:19 -0700810 if meterInfo.RefCnt == 0 {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700811 if err := rsrcMgr.RemoveMeterInfoForOnu(ctx, Direction, intfID, onuID, uniID, tpID); err != nil {
Girish Gowdraa482f272021-03-24 23:04:19 -0700812 return err
813 }
814 return nil
815 }
816 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700817 if err := rsrcMgr.StoreMeterInfoForOnu(ctx, Direction, intfID, onuID, uniID, tpID, meterInfo); err != nil {
Girish Gowdraa482f272021-03-24 23:04:19 -0700818 return err
819 }
820 return nil
821}
822
823// RemoveMeterInfoForOnu deletes the meter id from the kV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000824// 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 -0700825func (rsrcMgr *OpenOltResourceMgr) RemoveMeterInfoForOnu(ctx context.Context, Direction string, intfID uint32, onuID uint32,
826 uniID uint32, tpID uint32) error {
827 Path := fmt.Sprintf(MeterIDPathSuffix, intfID, onuID, uniID, tpID, Direction)
828
829 // update cache
830 rsrcMgr.meterInfoForOnuLock.Lock()
831 delete(rsrcMgr.meterInfoForOnu, Path)
832 rsrcMgr.meterInfoForOnuLock.Unlock()
833
834 if err := rsrcMgr.KVStore.Delete(ctx, Path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000835 logger.Errorf(ctx, "Failed to delete meter id %s from kvstore ", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400836 return err
837 }
838 return nil
839}
salmansiddiqui7ac62132019-08-22 03:58:50 +0000840
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700841//AddGemToOnuGemInfo adds gemport to onugem info kvstore and also local cache
842func (rsrcMgr *OpenOltResourceMgr) AddGemToOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, gemPort uint32) error {
843 onugem, err := rsrcMgr.GetOnuGemInfo(ctx, intfID, onuID)
844 if err != nil || onugem == nil || onugem.SerialNumber == "" {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000845 logger.Errorf(ctx, "failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530846 return err
847 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700848 if onugem.OnuID == onuID {
849 for _, gem := range onugem.GemPorts {
850 if gem == gemPort {
851 logger.Debugw(ctx, "Gem already present in onugem info, skpping addition", log.Fields{"gem": gem})
852 return nil
853 }
854 }
855 logger.Debugw(ctx, "Added gem to onugem info", log.Fields{"gem": gemPort})
856 onugem.GemPorts = append(onugem.GemPorts, gemPort)
857 } else {
858 logger.Errorw(ctx, "onu id in OnuGemInfo does not match", log.Fields{"onuID": onuID, "ponIf": intfID, "onuGemInfoOnuID": onugem.OnuID})
859 return fmt.Errorf("onu-id-in-OnuGemInfo-does-not-match-%v", onuID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530860 }
861
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700862 err = rsrcMgr.AddOnuGemInfo(ctx, intfID, onuID, *onugem)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530863 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000864 logger.Error(ctx, "Failed to add onugem to kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530865 return err
866 }
867 return err
868}
869
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700870//RemoveGemFromOnuGemInfo removes gemport from onugem info on kvstore and also local cache
871func (rsrcMgr *OpenOltResourceMgr) RemoveGemFromOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, gemPort uint32) error {
872 onugem, err := rsrcMgr.GetOnuGemInfo(ctx, intfID, onuID)
873 if err != nil || onugem == nil || onugem.SerialNumber == "" {
874 logger.Errorf(ctx, "failed to get onuifo for intfid %d", intfID)
875 return err
876 }
877 updated := false
878 if onugem.OnuID == onuID {
879 for i, gem := range onugem.GemPorts {
880 if gem == gemPort {
881 logger.Debugw(ctx, "Gem found, removing from onu gem info", log.Fields{"gem": gem})
882 onugem.GemPorts = append(onugem.GemPorts[:i], onugem.GemPorts[i+1:]...)
883 updated = true
884 break
885 }
886 }
887 } else {
888 logger.Errorw(ctx, "onu id in OnuGemInfo does not match", log.Fields{"onuID": onuID, "ponIf": intfID, "onuGemInfoOnuID": onugem.OnuID})
889 return fmt.Errorf("onu-id-in-OnuGemInfo-does-not-match-%v", onuID)
890 }
891 if updated {
892 err = rsrcMgr.AddOnuGemInfo(ctx, intfID, onuID, *onugem)
893 if err != nil {
894 logger.Error(ctx, "Failed to add onugem to kv store")
895 return err
896 }
897 } else {
898 logger.Debugw(ctx, "Gem port not found in onu gem info", log.Fields{"gem": gemPort})
899 }
900 return nil
901}
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530902
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700903//GetOnuGemInfo gets onu gem info from the kvstore per interface
904func (rsrcMgr *OpenOltResourceMgr) GetOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32) (*OnuGemInfo, error) {
905 var err error
906 var Val []byte
907 var onugem OnuGemInfo
908
909 path := fmt.Sprintf(OnuGemInfoPath, intfID, onuID)
910
911 rsrcMgr.onuGemInfoLock.RLock()
912 val, ok := rsrcMgr.onuGemInfo[path]
913 rsrcMgr.onuGemInfoLock.RUnlock()
914 if ok {
915 return val, nil
916 }
917 value, err := rsrcMgr.KVStore.Get(ctx, path)
918 if err != nil {
919 logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path})
920 return nil, err
921 } else if value == nil {
922 logger.Debug(ctx, "No onuinfo for path", log.Fields{"path": path})
923 return nil, nil // returning nil as this could happen if there are no onus for the interface yet
924 }
925 if Val, err = kvstore.ToByte(value.Value); err != nil {
926 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530927 return nil, err
928 }
929
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700930 if err = json.Unmarshal(Val, &onugem); err != nil {
931 logger.Error(ctx, "Failed to unmarshall")
932 return nil, err
933 }
934 logger.Debugw(ctx, "found onugem info from path", log.Fields{"path": path, "onuGemInfo": onugem})
935 rsrcMgr.onuGemInfoLock.Lock()
936 rsrcMgr.onuGemInfo[path] = &onugem
937 rsrcMgr.onuGemInfoLock.Unlock()
938
939 return &onugem, nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530940}
941
Chaitrashree G S1a55b882020-02-04 17:35:35 -0500942// AddOnuGemInfo adds onu info on to the kvstore per interface
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700943func (rsrcMgr *OpenOltResourceMgr) AddOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, onuGem OnuGemInfo) error {
944
945 var Value []byte
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530946 var err error
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700947 Path := fmt.Sprintf(OnuGemInfoPath, intfID, onuID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530948
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700949 rsrcMgr.onuGemInfoLock.Lock()
950 rsrcMgr.onuGemInfo[Path] = &onuGem
951 rsrcMgr.onuGemInfoLock.Unlock()
952
953 Value, err = json.Marshal(onuGem)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530954 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700955 logger.Error(ctx, "failed to Marshal")
956 return err
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530957 }
958
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700959 if err = rsrcMgr.KVStore.Put(ctx, Path, Value); err != nil {
960 logger.Errorf(ctx, "Failed to update resource %s", Path)
961 return err
962 }
963 logger.Debugw(ctx, "added onu gem info", log.Fields{"onuGemInfo": onuGem})
964 return err
965}
966
967// DelOnuGemInfo deletes the onugem info from kvstore per ONU
968func (rsrcMgr *OpenOltResourceMgr) DelOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32) error {
969 path := fmt.Sprintf(OnuGemInfoPath, intfID, onuID)
970 rsrcMgr.onuGemInfoLock.Lock()
971 logger.Debugw(ctx, "removing onu gem info", log.Fields{"onuGemInfo": rsrcMgr.onuGemInfo[path]})
972 delete(rsrcMgr.onuGemInfo, path)
973 rsrcMgr.onuGemInfoLock.Unlock()
974
975 if err := rsrcMgr.KVStore.Delete(ctx, path); err != nil {
976 logger.Errorf(ctx, "failed to remove resource %s", path)
977 return err
978 }
Andrea Campanellab83b39d2020-03-30 11:41:16 +0200979 return nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530980}
981
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530982// 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 -0700983func (rsrcMgr *OpenOltResourceMgr) AddUniPortToOnuInfo(ctx context.Context, intfID uint32, onuID uint32, portNo uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530984
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700985 onugem, err := rsrcMgr.GetOnuGemInfo(ctx, intfID, onuID)
986 if err != nil || onugem == nil || onugem.SerialNumber == "" {
987 logger.Warnf(ctx, "failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530988 return
989 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700990
991 if onugem.OnuID == onuID {
992 for _, uni := range onugem.UniPorts {
993 if uni == portNo {
994 logger.Debugw(ctx, "uni already present in onugem info", log.Fields{"uni": portNo})
995 return
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530996 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530997 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700998 onugem.UniPorts = append(onugem.UniPorts, portNo)
999 } else {
1000 logger.Warnw(ctx, "onu id mismatch in onu gem info", log.Fields{"intfID": intfID, "onuID": onuID})
1001 return
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301002 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001003 err = rsrcMgr.AddOnuGemInfo(ctx, intfID, onuID, *onugem)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301004 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001005 logger.Errorw(ctx, "Failed to add uni port in onugem to kv store", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301006 return
1007 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301008}
1009
Esin Karaman7fb80c22020-07-16 14:23:33 +00001010//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 -07001011func (rsrcMgr *OpenOltResourceMgr) UpdateGemPortForPktIn(ctx context.Context, pktIn PacketInInfoKey, gemPort uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301012
Esin Karaman7fb80c22020-07-16 14:23:33 +00001013 path := fmt.Sprintf(OnuPacketINPath, pktIn.IntfID, pktIn.OnuID, pktIn.LogicalPort, pktIn.VlanID, pktIn.Priority)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001014 // update cache
1015 rsrcMgr.gemPortForPacketInInfoLock.Lock()
1016 rsrcMgr.gemPortForPacketInInfo[path] = gemPort
1017 rsrcMgr.gemPortForPacketInInfoLock.Unlock()
1018
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301019 Value, err := json.Marshal(gemPort)
1020 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001021 logger.Error(ctx, "Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301022 return
1023 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001024 if err = rsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001025 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"path": path, "value": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301026 return
1027 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001028 logger.Debugw(ctx, "added gem packet in successfully", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301029}
1030
Esin Karaman7fb80c22020-07-16 14:23:33 +00001031// 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 -07001032func (rsrcMgr *OpenOltResourceMgr) GetGemPortFromOnuPktIn(ctx context.Context, packetInInfoKey PacketInInfoKey) (uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301033
1034 var Val []byte
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301035
Esin Karaman7fb80c22020-07-16 14:23:33 +00001036 path := fmt.Sprintf(OnuPacketINPath, packetInInfoKey.IntfID, packetInInfoKey.OnuID, packetInInfoKey.LogicalPort,
1037 packetInInfoKey.VlanID, packetInInfoKey.Priority)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001038 // get from cache
1039 rsrcMgr.gemPortForPacketInInfoLock.RLock()
1040 gemPort, ok := rsrcMgr.gemPortForPacketInInfo[path]
1041 rsrcMgr.gemPortForPacketInInfoLock.RUnlock()
1042 if ok {
1043 logger.Debugw(ctx, "found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
1044 return gemPort, nil
1045 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301046
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001047 value, err := rsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301048 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001049 logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301050 return uint32(0), err
1051 } else if value == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001052 logger.Debugw(ctx, "No pkt in gem found", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301053 return uint32(0), nil
1054 }
1055
1056 if Val, err = kvstore.ToByte(value.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001057 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301058 return uint32(0), err
1059 }
1060 if err = json.Unmarshal(Val, &gemPort); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001061 logger.Error(ctx, "Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301062 return uint32(0), err
1063 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001064 logger.Debugw(ctx, "found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001065 // update cache
1066 rsrcMgr.gemPortForPacketInInfoLock.Lock()
1067 rsrcMgr.gemPortForPacketInInfo[path] = gemPort
1068 rsrcMgr.gemPortForPacketInInfoLock.Unlock()
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301069
1070 return gemPort, nil
1071}
1072
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001073//DeletePacketInGemPortForOnu deletes the packet-in gemport for ONU
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001074func (rsrcMgr *OpenOltResourceMgr) DeletePacketInGemPortForOnu(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301075
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001076 path := fmt.Sprintf(OnuPacketINPathPrefix, intfID, onuID, logicalPort)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001077
1078 value, err := rsrcMgr.KVStore.List(ctx, path)
Esin Karaman7fb80c22020-07-16 14:23:33 +00001079 if err != nil {
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001080 logger.Errorf(ctx, "failed-to-read-value-from-path-%s", path)
1081 return errors.New("failed-to-read-value-from-path-" + path)
Esin Karaman7fb80c22020-07-16 14:23:33 +00001082 }
Esin Karaman7fb80c22020-07-16 14:23:33 +00001083
1084 //remove them one by one
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001085 for key := range value {
1086 // Formulate the right key path suffix ti be delete
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001087 stringToBeReplaced := fmt.Sprintf(BasePathKvStore, rsrcMgr.KVStore.PathPrefix, rsrcMgr.DeviceID) + "/"
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001088 replacedWith := ""
1089 key = strings.Replace(key, stringToBeReplaced, replacedWith, 1)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001090 // update cache
1091 rsrcMgr.gemPortForPacketInInfoLock.Lock()
1092 delete(rsrcMgr.gemPortForPacketInInfo, key)
1093 rsrcMgr.gemPortForPacketInInfoLock.Unlock()
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001094
1095 logger.Debugf(ctx, "removing-key-%s", key)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001096 if err := rsrcMgr.KVStore.Delete(ctx, key); err != nil {
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001097 logger.Errorf(ctx, "failed-to-remove-resource-%s", key)
Esin Karaman7fb80c22020-07-16 14:23:33 +00001098 return err
1099 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301100 }
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001101
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301102 return nil
1103}
1104
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001105//GetFlowIDsForGem gets the list of FlowIDs for the given gemport
1106func (rsrcMgr *OpenOltResourceMgr) GetFlowIDsForGem(ctx context.Context, intf uint32, gem uint32) ([]uint64, error) {
1107 path := fmt.Sprintf(FlowIDsForGem, intf, gem)
1108
1109 // get from cache
1110 rsrcMgr.flowIDsForGemLock.RLock()
1111 flowIDs, ok := rsrcMgr.flowIDsForGem[gem]
1112 rsrcMgr.flowIDsForGemLock.RUnlock()
1113 if ok {
1114 return flowIDs, nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301115 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301116
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001117 value, err := rsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301118 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001119 logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path})
1120 return nil, err
1121 } else if value == nil {
1122 logger.Debug(ctx, "no flow-ids found", log.Fields{"path": path})
1123 return nil, nil
1124 }
1125 Val, err := kvstore.ToByte(value.Value)
1126 if err != nil {
1127 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301128 return nil, err
1129 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301130
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001131 if err = json.Unmarshal(Val, &flowIDs); err != nil {
1132 logger.Error(ctx, "Failed to unmarshall")
1133 return nil, err
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301134 }
1135
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001136 // update cache
1137 rsrcMgr.flowIDsForGemLock.Lock()
1138 rsrcMgr.flowIDsForGem[gem] = flowIDs
1139 rsrcMgr.flowIDsForGemLock.Unlock()
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301140
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001141 return flowIDs, nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301142}
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301143
1144//UpdateFlowIDsForGem updates flow id per gemport
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001145func (rsrcMgr *OpenOltResourceMgr) UpdateFlowIDsForGem(ctx context.Context, intf uint32, gem uint32, flowIDs []uint64) error {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301146 var val []byte
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001147 path := fmt.Sprintf(FlowIDsForGem, intf, gem)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301148
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001149 // update cache
1150 rsrcMgr.flowIDsForGemLock.Lock()
1151 rsrcMgr.flowIDsForGem[gem] = flowIDs
1152 rsrcMgr.flowIDsForGemLock.Unlock()
1153
1154 if flowIDs == nil {
1155 return nil
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301156 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001157 val, err := json.Marshal(flowIDs)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301158 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001159 logger.Error(ctx, "Failed to marshal data", log.Fields{"err": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301160 return err
1161 }
Girish Gowdrab77ded92020-04-08 11:45:05 -07001162
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001163 if err = rsrcMgr.KVStore.Put(ctx, path, val); err != nil {
1164 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"err": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301165 return err
1166 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001167 logger.Debugw(ctx, "added flowid list for gem to kv successfully", log.Fields{"path": path, "flowidlist": flowIDs})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301168 return nil
1169}
1170
1171//DeleteFlowIDsForGem deletes the flowID list entry per gem from kvstore.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001172func (rsrcMgr *OpenOltResourceMgr) DeleteFlowIDsForGem(ctx context.Context, intf uint32, gem uint32) {
1173 path := fmt.Sprintf(FlowIDsForGem, intf, gem)
1174 // update cache
1175 rsrcMgr.flowIDsForGemLock.Lock()
1176 delete(rsrcMgr.flowIDsForGem, gem)
1177 rsrcMgr.flowIDsForGemLock.Unlock()
1178 if err := rsrcMgr.KVStore.Delete(ctx, path); err != nil {
1179 logger.Errorw(ctx, "Failed to delete from kvstore", log.Fields{"err": err, "path": path})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301180 }
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301181}
Esin Karamanccb714b2019-11-29 15:02:06 +00001182
1183//GetMcastQueuePerInterfaceMap gets multicast queue info per pon interface
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001184func (rsrcMgr *OpenOltResourceMgr) GetMcastQueuePerInterfaceMap(ctx context.Context) (map[uint32][]uint32, error) {
Kent Hagermane6ff1012020-07-14 15:07:53 -04001185 path := McastQueuesForIntf
Esin Karamanccb714b2019-11-29 15:02:06 +00001186 var val []byte
1187
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001188 rsrcMgr.mcastQueueForIntfLock.RLock()
1189 if rsrcMgr.mcastQueueForIntfLoadedFromKvStore {
1190 rsrcMgr.mcastQueueForIntfLock.RUnlock()
1191 return rsrcMgr.mcastQueueForIntf, nil
1192 }
1193 rsrcMgr.mcastQueueForIntfLock.RUnlock()
1194
1195 kvPair, err := rsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001196 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001197 logger.Error(ctx, "failed to get data from kv store")
Esin Karamanccb714b2019-11-29 15:02:06 +00001198 return nil, err
1199 }
1200 if kvPair != nil && kvPair.Value != nil {
1201 if val, err = kvstore.ToByte(kvPair.Value); err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001202 logger.Error(ctx, "Failed to convert to byte array ", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001203 return nil, err
1204 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001205 rsrcMgr.mcastQueueForIntfLock.Lock()
1206 defer rsrcMgr.mcastQueueForIntfLock.Unlock()
1207 if err = json.Unmarshal(val, &rsrcMgr.mcastQueueForIntf); err != nil {
1208 logger.Error(ctx, "Failed to unmarshall ", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001209 return nil, err
1210 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001211 rsrcMgr.mcastQueueForIntfLoadedFromKvStore = true
Esin Karamanccb714b2019-11-29 15:02:06 +00001212 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001213 return rsrcMgr.mcastQueueForIntf, nil
Esin Karamanccb714b2019-11-29 15:02:06 +00001214}
1215
1216//AddMcastQueueForIntf adds multicast queue for pon interface
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001217func (rsrcMgr *OpenOltResourceMgr) AddMcastQueueForIntf(ctx context.Context, intf uint32, gem uint32, servicePriority uint32) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001218 var val []byte
Kent Hagermane6ff1012020-07-14 15:07:53 -04001219 path := McastQueuesForIntf
Esin Karamanccb714b2019-11-29 15:02:06 +00001220
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001221 // Load local cache from kv store the first time
1222 rsrcMgr.mcastQueueForIntfLock.RLock()
1223 if !rsrcMgr.mcastQueueForIntfLoadedFromKvStore {
1224 rsrcMgr.mcastQueueForIntfLock.RUnlock()
1225 _, err := rsrcMgr.GetMcastQueuePerInterfaceMap(ctx)
1226 if err != nil {
1227 logger.Errorw(ctx, "Failed to get multicast queue info for interface", log.Fields{"err": err, "intf": intf})
1228 return err
1229 }
1230 } else {
1231 rsrcMgr.mcastQueueForIntfLock.RUnlock()
1232 }
1233
1234 // Update KV store
1235 rsrcMgr.mcastQueueForIntfLock.Lock()
1236 rsrcMgr.mcastQueueForIntf[intf] = []uint32{gem, servicePriority}
1237 val, err := json.Marshal(rsrcMgr.mcastQueueForIntf)
Esin Karamanccb714b2019-11-29 15:02:06 +00001238 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001239 rsrcMgr.mcastQueueForIntfLock.Unlock()
1240 logger.Errorw(ctx, "Failed to marshal data", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001241 return err
1242 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001243 rsrcMgr.mcastQueueForIntfLock.Unlock()
1244
1245 if err = rsrcMgr.KVStore.Put(ctx, path, val); err != nil {
1246 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"err": err, "path": path, "value": val})
Esin Karamanccb714b2019-11-29 15:02:06 +00001247 return err
1248 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001249 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 +00001250 return nil
1251}
1252
1253//AddFlowGroupToKVStore adds flow group into KV store
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001254func (rsrcMgr *OpenOltResourceMgr) AddFlowGroupToKVStore(ctx context.Context, groupEntry *ofp.OfpGroupEntry, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001255 var Value []byte
1256 var err error
1257 var path string
1258 if cached {
1259 path = fmt.Sprintf(FlowGroupCached, groupEntry.Desc.GroupId)
1260 } else {
1261 path = fmt.Sprintf(FlowGroup, groupEntry.Desc.GroupId)
1262 }
1263 //build group info object
1264 var outPorts []uint32
1265 for _, ofBucket := range groupEntry.Desc.Buckets {
1266 for _, ofAction := range ofBucket.Actions {
1267 if ofAction.Type == ofp.OfpActionType_OFPAT_OUTPUT {
1268 outPorts = append(outPorts, ofAction.GetOutput().Port)
1269 }
1270 }
1271 }
1272 groupInfo := GroupInfo{
1273 GroupID: groupEntry.Desc.GroupId,
1274 OutPorts: outPorts,
1275 }
1276
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001277 rsrcMgr.groupInfoLock.Lock()
1278 rsrcMgr.groupInfo[path] = &groupInfo
1279 rsrcMgr.groupInfoLock.Unlock()
1280
Esin Karamanccb714b2019-11-29 15:02:06 +00001281 Value, err = json.Marshal(groupInfo)
1282
1283 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001284 logger.Error(ctx, "failed to Marshal flow group object")
Esin Karamanccb714b2019-11-29 15:02:06 +00001285 return err
1286 }
1287
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001288 if err = rsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001289 logger.Errorf(ctx, "Failed to update resource %s", path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001290 return err
1291 }
1292 return nil
1293}
1294
1295//RemoveFlowGroupFromKVStore removes flow group from KV store
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001296func (rsrcMgr *OpenOltResourceMgr) RemoveFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001297 var path string
1298 if cached {
1299 path = fmt.Sprintf(FlowGroupCached, groupID)
1300 } else {
1301 path = fmt.Sprintf(FlowGroup, groupID)
1302 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001303 rsrcMgr.groupInfoLock.Lock()
1304 delete(rsrcMgr.groupInfo, path)
1305 rsrcMgr.groupInfoLock.Unlock()
1306
1307 if err := rsrcMgr.KVStore.Delete(ctx, path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001308 logger.Errorf(ctx, "Failed to remove resource %s due to %s", path, err)
Esin Karamand519bbf2020-07-01 11:16:03 +00001309 return err
Esin Karamanccb714b2019-11-29 15:02:06 +00001310 }
Esin Karamand519bbf2020-07-01 11:16:03 +00001311 return nil
Esin Karamanccb714b2019-11-29 15:02:06 +00001312}
1313
1314//GetFlowGroupFromKVStore fetches flow group from the KV store. Returns (false, {} error) if any problem occurs during
1315//fetching the data. Returns (true, groupInfo, nil) if the group is fetched successfully.
1316// Returns (false, {}, nil) if the group does not exists in the KV store.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001317func (rsrcMgr *OpenOltResourceMgr) GetFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) (bool, GroupInfo, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001318 var groupInfo GroupInfo
1319 var path string
1320 if cached {
1321 path = fmt.Sprintf(FlowGroupCached, groupID)
1322 } else {
1323 path = fmt.Sprintf(FlowGroup, groupID)
1324 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001325
1326 // read from cache
1327 rsrcMgr.groupInfoLock.RLock()
1328 gi, ok := rsrcMgr.groupInfo[path]
1329 rsrcMgr.groupInfoLock.RUnlock()
1330 if ok {
1331 return true, *gi, nil
1332 }
1333
1334 kvPair, err := rsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001335 if err != nil {
1336 return false, groupInfo, err
1337 }
1338 if kvPair != nil && kvPair.Value != nil {
1339 Val, err := kvstore.ToByte(kvPair.Value)
1340 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001341 logger.Errorw(ctx, "Failed to convert flow group into byte array", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001342 return false, groupInfo, err
1343 }
1344 if err = json.Unmarshal(Val, &groupInfo); err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001345 logger.Errorw(ctx, "Failed to unmarshal", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001346 return false, groupInfo, err
1347 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001348 // update cache
1349 rsrcMgr.groupInfoLock.Lock()
1350 rsrcMgr.groupInfo[path] = &groupInfo
1351 rsrcMgr.groupInfoLock.Unlock()
1352
Esin Karamanccb714b2019-11-29 15:02:06 +00001353 return true, groupInfo, nil
1354 }
1355 return false, groupInfo, nil
1356}
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001357
1358// toByte converts an interface value to a []byte. The interface should either be of
1359// a string type or []byte. Otherwise, an error is returned.
1360func toByte(value interface{}) ([]byte, error) {
1361 switch t := value.(type) {
1362 case []byte:
1363 return value.([]byte), nil
1364 case string:
1365 return []byte(value.(string)), nil
1366 default:
1367 return nil, fmt.Errorf("unexpected-type-%T", t)
1368 }
1369}