blob: 737f6944ac83958af2d0097bf0b43c1508e82ab6 [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 Gowdru6a80bbd2019-07-02 07:36:09 -0700477
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700478 if err := rsrcMgr.PonRsrMgr.TechProfileMgr.FreeResourceID(ctx, intfID, ponrmgr.ONU_ID, onuID); err != nil {
Matteo Scandolo84585372021-03-18 14:21:22 -0700479 logger.Errorw(ctx, "error-while-freeing-onu-id", log.Fields{
480 "intf-id": intfID,
481 "onu-id": onuID,
482 "err": err.Error(),
483 })
484 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530485
Girish Gowdru0c588b22019-04-23 23:24:56 -0400486 /* Free onu id for a particular interface.*/
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700487 var IntfonuID string
488 for _, onu := range onuID {
489 IntfonuID = fmt.Sprintf("%d,%d", intfID, onu)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700490 rsrcMgr.PonRsrMgr.RemoveResourceMap(ctx, IntfonuID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400491 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530492}
493
Gamze Abakafee36392019-10-03 11:17:24 +0000494// FreeAllocID frees AllocID on the PON resource pool and also frees the allocID association
495// for the given OLT device.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700496// The caller should ensure that this is a blocking call and this operation is serialized for
497// the ONU so as not cause resource corruption since there are no mutexes used here.
498func (rsrcMgr *OpenOltResourceMgr) FreeAllocID(ctx context.Context, intfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000499 uniID uint32, allocID uint32) {
Girish Gowdrab77ded92020-04-08 11:45:05 -0700500
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700501 rsrcMgr.RemoveAllocIDForOnu(ctx, intfID, onuID, uniID, allocID)
Gamze Abakafee36392019-10-03 11:17:24 +0000502 allocIDs := make([]uint32, 0)
503 allocIDs = append(allocIDs, allocID)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700504 if err := rsrcMgr.PonRsrMgr.TechProfileMgr.FreeResourceID(ctx, intfID, ponrmgr.ALLOC_ID, allocIDs); err != nil {
Matteo Scandolo84585372021-03-18 14:21:22 -0700505 logger.Errorw(ctx, "error-while-freeing-alloc-id", log.Fields{
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700506 "intf-id": intfID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700507 "onu-id": onuID,
508 "err": err.Error(),
509 })
510 }
Gamze Abakafee36392019-10-03 11:17:24 +0000511}
512
513// FreeGemPortID frees GemPortID on the PON resource pool and also frees the gemPortID association
514// for the given OLT device.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700515// The caller should ensure that this is a blocking call and this operation is serialized for
516// the ONU so as not cause resource corruption since there are no mutexes used here.
517func (rsrcMgr *OpenOltResourceMgr) FreeGemPortID(ctx context.Context, intfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000518 uniID uint32, gemPortID uint32) {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700519 rsrcMgr.RemoveGemPortIDForOnu(ctx, intfID, onuID, uniID, gemPortID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700520
Gamze Abakafee36392019-10-03 11:17:24 +0000521 gemPortIDs := make([]uint32, 0)
522 gemPortIDs = append(gemPortIDs, gemPortID)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700523 if err := rsrcMgr.PonRsrMgr.TechProfileMgr.FreeResourceID(ctx, intfID, ponrmgr.GEMPORT_ID, gemPortIDs); err != nil {
Matteo Scandolo84585372021-03-18 14:21:22 -0700524 logger.Errorw(ctx, "error-while-freeing-gem-port-id", log.Fields{
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700525 "intf-id": intfID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700526 "onu-id": onuID,
527 "err": err.Error(),
528 })
529 }
Gamze Abakafee36392019-10-03 11:17:24 +0000530}
531
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700532// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id
533func (rsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530534
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700535 intfOnuIDuniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530536
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700537 AllocIDs := rsrcMgr.PonRsrMgr.GetCurrentAllocIDForOnu(ctx, intfOnuIDuniID)
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700538
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700539 rsrcMgr.allocIDsForOnuLock.Lock()
540 delete(rsrcMgr.allocIDsForOnu, intfOnuIDuniID)
541 rsrcMgr.allocIDsForOnuLock.Unlock()
542
543 if err := rsrcMgr.PonRsrMgr.TechProfileMgr.FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400544 ponrmgr.ALLOC_ID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700545 AllocIDs); err != nil {
546 logger.Errorw(ctx, "error-while-freeing-all-alloc-ids-for-onu", log.Fields{
547 "intf-id": intfID,
548 "onu-id": onuID,
549 "err": err.Error(),
550 })
551 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530552
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700553 GEMPortIDs := rsrcMgr.PonRsrMgr.GetCurrentGEMPortIDsForOnu(ctx, intfOnuIDuniID)
554
555 rsrcMgr.gemPortIDsForOnuLock.Lock()
556 delete(rsrcMgr.gemPortIDsForOnu, intfOnuIDuniID)
557 rsrcMgr.gemPortIDsForOnuLock.Unlock()
558
559 if err := rsrcMgr.PonRsrMgr.TechProfileMgr.FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400560 ponrmgr.GEMPORT_ID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700561 GEMPortIDs); err != nil {
562 logger.Errorw(ctx, "error-while-freeing-all-gem-port-ids-for-onu", log.Fields{
563 "intf-id": intfID,
564 "onu-id": onuID,
565 "err": err.Error(),
566 })
567 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530568
Girish Gowdru0c588b22019-04-23 23:24:56 -0400569 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700570 rsrcMgr.PonRsrMgr.RemoveResourceMap(ctx, intfOnuIDuniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530571}
572
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700573// IsFlowOnKvStore checks if the given flowID is present on the kv store
574// Returns true if the flowID is found, otherwise it returns false
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700575func (rsrcMgr *OpenOltResourceMgr) IsFlowOnKvStore(ctx context.Context, intfID uint32, onuID int32, uniID int32,
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700576 flowID uint64) bool {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530577
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700578 FlowIDs, err := rsrcMgr.GetCurrentFlowIDsForOnu(ctx, intfID, onuID, uniID)
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700579 if err != nil {
580 // error logged in the called function
581 return false
582 }
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400583 if FlowIDs != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700584 logger.Debugw(ctx, "Found flowId(s) for this ONU", log.Fields{"pon": intfID, "onuID": onuID, "uniID": uniID})
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700585 for _, id := range FlowIDs {
586 if flowID == id {
587 return true
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400588 }
589 }
590 }
591 return false
592}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400593
salmansiddiqui7ac62132019-08-22 03:58:50 +0000594// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700595// This path is formed as the following: {intfID, onuID, uniID}/tp_id
596func (rsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) []uint32 {
597 Path := fmt.Sprintf(tpIDPathSuffix, intfID, onuID, uniID)
598 // fetch from cache
599 rsrcMgr.techProfileIDsForOnuLock.RLock()
600 tpIDs, ok := rsrcMgr.techProfileIDsForOnu[Path]
601 rsrcMgr.techProfileIDsForOnuLock.RUnlock()
602 if ok {
603 return tpIDs
604 }
605 Value, err := rsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400606 if err == nil {
607 if Value != nil {
608 Val, err := kvstore.ToByte(Value.Value)
609 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700610 logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"err": err})
611 return tpIDs
Manikkaraj kb1d51442019-07-23 10:41:02 -0400612 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700613 if err = json.Unmarshal(Val, &tpIDs); err != nil {
614 logger.Error(ctx, "Failed to unmarshal", log.Fields{"err": err})
615 return tpIDs
Manikkaraj kb1d51442019-07-23 10:41:02 -0400616 }
617 }
618 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000619 logger.Errorf(ctx, "Failed to get TP id from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400620 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700621 logger.Debugf(ctx, "Getting TP id %d from path %s", tpIDs, Path)
622
623 // update cache
624 rsrcMgr.techProfileIDsForOnuLock.Lock()
625 rsrcMgr.techProfileIDsForOnu[Path] = tpIDs
626 rsrcMgr.techProfileIDsForOnuLock.Unlock()
627
628 return tpIDs
Manikkaraj kb1d51442019-07-23 10:41:02 -0400629
630}
631
Gamze Abakafee36392019-10-03 11:17:24 +0000632// 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 -0700633// This path is formed as the following: {intfID, onuID, uniID}/tp_id
634func (rsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDsForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) error {
635 intfOnuUniID := fmt.Sprintf(tpIDPathSuffix, intfID, onuID, uniID)
636 // update cache
637 rsrcMgr.techProfileIDsForOnuLock.Lock()
638 delete(rsrcMgr.techProfileIDsForOnu, intfOnuUniID)
639 rsrcMgr.techProfileIDsForOnuLock.Unlock()
640
641 if err := rsrcMgr.KVStore.Delete(ctx, intfOnuUniID); err != nil {
642 logger.Errorw(ctx, "Failed to delete techprofile id resource in KV store", log.Fields{"path": intfOnuUniID})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400643 return err
644 }
645 return nil
646}
647
Gamze Abakafee36392019-10-03 11:17:24 +0000648// 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 -0700649// This path is formed as the following: {intfID, onuID, uniID}/tp_id
650func (rsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, tpID uint32) error {
651 tpIDList := rsrcMgr.GetTechProfileIDForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000652 for i, tpIDInList := range tpIDList {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700653 if tpIDInList == tpID {
Gamze Abakafee36392019-10-03 11:17:24 +0000654 tpIDList = append(tpIDList[:i], tpIDList[i+1:]...)
655 }
656 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700657 intfOnuUniID := fmt.Sprintf(tpIDPathSuffix, intfID, onuID, uniID)
658 // update cache
659 rsrcMgr.techProfileIDsForOnuLock.Lock()
660 rsrcMgr.techProfileIDsForOnu[intfOnuUniID] = tpIDList
661 rsrcMgr.techProfileIDsForOnuLock.Unlock()
662
Gamze Abakafee36392019-10-03 11:17:24 +0000663 Value, err := json.Marshal(tpIDList)
664 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000665 logger.Error(ctx, "failed to Marshal")
Gamze Abakafee36392019-10-03 11:17:24 +0000666 return err
667 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700668 if err = rsrcMgr.KVStore.Put(ctx, intfOnuUniID, Value); err != nil {
669 logger.Errorf(ctx, "Failed to update resource %s", intfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000670 return err
671 }
672 return err
673}
674
675// UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700676// This path is formed as the following: {intfID, onuID, uniID}/tp_id
677func (rsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(ctx context.Context, intfID uint32, onuID uint32,
678 uniID uint32, tpID uint32) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400679 var Value []byte
680 var err error
681
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700682 intfOnuUniID := fmt.Sprintf(tpIDPathSuffix, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000683
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700684 tpIDList := rsrcMgr.GetTechProfileIDForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000685 for _, value := range tpIDList {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700686 if value == tpID {
687 logger.Debugf(ctx, "tpID %d is already in tpIdList for the path %s", tpID, intfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000688 return err
689 }
690 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700691 logger.Debugf(ctx, "updating tp id %d on path %s", tpID, intfOnuUniID)
692 tpIDList = append(tpIDList, tpID)
693
694 // update cache
695 rsrcMgr.techProfileIDsForOnuLock.Lock()
696 rsrcMgr.techProfileIDsForOnu[intfOnuUniID] = tpIDList
697 rsrcMgr.techProfileIDsForOnuLock.Unlock()
698
Gamze Abakafee36392019-10-03 11:17:24 +0000699 Value, err = json.Marshal(tpIDList)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400700 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000701 logger.Error(ctx, "failed to Marshal")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400702 return err
703 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700704 if err = rsrcMgr.KVStore.Put(ctx, intfOnuUniID, Value); err != nil {
705 logger.Errorf(ctx, "Failed to update resource %s", intfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400706 return err
707 }
708 return err
709}
710
Girish Gowdraa482f272021-03-24 23:04:19 -0700711// StoreMeterInfoForOnu updates the meter id in the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000712// 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 -0700713func (rsrcMgr *OpenOltResourceMgr) StoreMeterInfoForOnu(ctx context.Context, Direction string, intfID uint32, onuID uint32,
714 uniID uint32, tpID uint32, meterInfo *MeterInfo) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400715 var Value []byte
716 var err error
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700717 intfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, intfID, onuID, uniID, tpID, Direction)
718
719 // update cache
720 rsrcMgr.meterInfoForOnuLock.Lock()
721 rsrcMgr.meterInfoForOnu[intfOnuUniID] = meterInfo
722 rsrcMgr.meterInfoForOnuLock.Unlock()
723
Girish Gowdraa482f272021-03-24 23:04:19 -0700724 Value, err = json.Marshal(*meterInfo)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400725 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000726 logger.Error(ctx, "failed to Marshal meter config")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400727 return err
728 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700729 if err = rsrcMgr.KVStore.Put(ctx, intfOnuUniID, Value); err != nil {
730 logger.Errorf(ctx, "Failed to store meter into KV store %s", intfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400731 return err
732 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700733 logger.Debugw(ctx, "meter info updated successfully", log.Fields{"path": intfOnuUniID, "meter-info": meterInfo})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400734 return err
735}
736
Girish Gowdraa482f272021-03-24 23:04:19 -0700737// GetMeterInfoForOnu fetches the meter id from the kv store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000738// 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 -0700739func (rsrcMgr *OpenOltResourceMgr) GetMeterInfoForOnu(ctx context.Context, Direction string, intfID uint32, onuID uint32,
740 uniID uint32, tpID uint32) (*MeterInfo, error) {
741 Path := fmt.Sprintf(MeterIDPathSuffix, intfID, onuID, uniID, tpID, Direction)
742
743 // get from cache
744 rsrcMgr.meterInfoForOnuLock.RLock()
745 val, ok := rsrcMgr.meterInfoForOnu[Path]
746 rsrcMgr.meterInfoForOnuLock.RUnlock()
747 if ok {
748 return val, nil
749 }
750
Girish Gowdraa482f272021-03-24 23:04:19 -0700751 var meterInfo MeterInfo
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700752 Value, err := rsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400753 if err == nil {
754 if Value != nil {
Girish Gowdraa482f272021-03-24 23:04:19 -0700755 logger.Debug(ctx, "Found meter info in KV store", log.Fields{"Direction": Direction})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000756 Val, er := kvstore.ToByte(Value.Value)
757 if er != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700758 logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"err": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000759 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400760 }
Girish Gowdraa482f272021-03-24 23:04:19 -0700761 if er = json.Unmarshal(Val, &meterInfo); er != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700762 logger.Error(ctx, "Failed to unmarshal meter info", log.Fields{"err": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000763 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400764 }
765 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000766 logger.Debug(ctx, "meter-does-not-exists-in-KVStore")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400767 return nil, err
768 }
769 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000770 logger.Errorf(ctx, "Failed to get Meter config from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400771
772 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700773 // update cache
774 rsrcMgr.meterInfoForOnuLock.Lock()
775 rsrcMgr.meterInfoForOnu[Path] = &meterInfo
776 rsrcMgr.meterInfoForOnuLock.Unlock()
777
Girish Gowdraa482f272021-03-24 23:04:19 -0700778 return &meterInfo, err
Manikkaraj kb1d51442019-07-23 10:41:02 -0400779}
780
Girish Gowdraa482f272021-03-24 23:04:19 -0700781// HandleMeterInfoRefCntUpdate increments or decrements the reference counter for a given meter.
782// When reference count becomes 0, it clears the meter information from the kv store
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700783func (rsrcMgr *OpenOltResourceMgr) HandleMeterInfoRefCntUpdate(ctx context.Context, Direction string,
784 intfID uint32, onuID uint32, uniID uint32, tpID uint32, increment bool) error {
785 meterInfo, err := rsrcMgr.GetMeterInfoForOnu(ctx, Direction, intfID, onuID, uniID, tpID)
Girish Gowdra82c80982021-03-26 16:22:02 -0700786 if err != nil {
787 return err
788 } else if meterInfo == nil {
789 // If we are increasing the reference count, we expect the meter information to be present on KV store.
790 // But if decrementing the reference count, the meter is possibly already cleared from KV store. Just log warn but do not return error.
791 if increment {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700792 logger.Errorf(ctx, "error-fetching-meter-info-for-intf-%d-onu-%d-uni-%d-tp-id-%d-direction-%s", intfID, onuID, uniID, tpID, Direction)
793 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 -0700794 }
795 logger.Warnw(ctx, "meter is already cleared",
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700796 log.Fields{"intfID": intfID, "onuID": onuID, "uniID": uniID, "direction": Direction, "increment": increment})
Girish Gowdra82c80982021-03-26 16:22:02 -0700797 return nil
Girish Gowdraa482f272021-03-24 23:04:19 -0700798 }
Girish Gowdra82c80982021-03-26 16:22:02 -0700799
Girish Gowdraa482f272021-03-24 23:04:19 -0700800 if increment {
801 meterInfo.RefCnt++
802 } else {
803 meterInfo.RefCnt--
Girish Gowdra82c80982021-03-26 16:22:02 -0700804 // If RefCnt become 0 clear the meter information from the DB.
Girish Gowdraa482f272021-03-24 23:04:19 -0700805 if meterInfo.RefCnt == 0 {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700806 if err := rsrcMgr.RemoveMeterInfoForOnu(ctx, Direction, intfID, onuID, uniID, tpID); err != nil {
Girish Gowdraa482f272021-03-24 23:04:19 -0700807 return err
808 }
809 return nil
810 }
811 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700812 if err := rsrcMgr.StoreMeterInfoForOnu(ctx, Direction, intfID, onuID, uniID, tpID, meterInfo); err != nil {
Girish Gowdraa482f272021-03-24 23:04:19 -0700813 return err
814 }
815 return nil
816}
817
818// RemoveMeterInfoForOnu deletes the meter id from the kV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000819// 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 -0700820func (rsrcMgr *OpenOltResourceMgr) RemoveMeterInfoForOnu(ctx context.Context, Direction string, intfID uint32, onuID uint32,
821 uniID uint32, tpID uint32) error {
822 Path := fmt.Sprintf(MeterIDPathSuffix, intfID, onuID, uniID, tpID, Direction)
823
824 // update cache
825 rsrcMgr.meterInfoForOnuLock.Lock()
826 delete(rsrcMgr.meterInfoForOnu, Path)
827 rsrcMgr.meterInfoForOnuLock.Unlock()
828
829 if err := rsrcMgr.KVStore.Delete(ctx, Path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000830 logger.Errorf(ctx, "Failed to delete meter id %s from kvstore ", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400831 return err
832 }
833 return nil
834}
salmansiddiqui7ac62132019-08-22 03:58:50 +0000835
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700836//AddGemToOnuGemInfo adds gemport to onugem info kvstore and also local cache
837func (rsrcMgr *OpenOltResourceMgr) AddGemToOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, gemPort uint32) error {
838 onugem, err := rsrcMgr.GetOnuGemInfo(ctx, intfID, onuID)
839 if err != nil || onugem == nil || onugem.SerialNumber == "" {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000840 logger.Errorf(ctx, "failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530841 return err
842 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700843 if onugem.OnuID == onuID {
844 for _, gem := range onugem.GemPorts {
845 if gem == gemPort {
846 logger.Debugw(ctx, "Gem already present in onugem info, skpping addition", log.Fields{"gem": gem})
847 return nil
848 }
849 }
850 logger.Debugw(ctx, "Added gem to onugem info", log.Fields{"gem": gemPort})
851 onugem.GemPorts = append(onugem.GemPorts, gemPort)
852 } else {
853 logger.Errorw(ctx, "onu id in OnuGemInfo does not match", log.Fields{"onuID": onuID, "ponIf": intfID, "onuGemInfoOnuID": onugem.OnuID})
854 return fmt.Errorf("onu-id-in-OnuGemInfo-does-not-match-%v", onuID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530855 }
856
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700857 err = rsrcMgr.AddOnuGemInfo(ctx, intfID, onuID, *onugem)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530858 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000859 logger.Error(ctx, "Failed to add onugem to kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530860 return err
861 }
862 return err
863}
864
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700865//RemoveGemFromOnuGemInfo removes gemport from onugem info on kvstore and also local cache
866func (rsrcMgr *OpenOltResourceMgr) RemoveGemFromOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, gemPort uint32) error {
867 onugem, err := rsrcMgr.GetOnuGemInfo(ctx, intfID, onuID)
868 if err != nil || onugem == nil || onugem.SerialNumber == "" {
869 logger.Errorf(ctx, "failed to get onuifo for intfid %d", intfID)
870 return err
871 }
872 updated := false
873 if onugem.OnuID == onuID {
874 for i, gem := range onugem.GemPorts {
875 if gem == gemPort {
876 logger.Debugw(ctx, "Gem found, removing from onu gem info", log.Fields{"gem": gem})
877 onugem.GemPorts = append(onugem.GemPorts[:i], onugem.GemPorts[i+1:]...)
878 updated = true
879 break
880 }
881 }
882 } else {
883 logger.Errorw(ctx, "onu id in OnuGemInfo does not match", log.Fields{"onuID": onuID, "ponIf": intfID, "onuGemInfoOnuID": onugem.OnuID})
884 return fmt.Errorf("onu-id-in-OnuGemInfo-does-not-match-%v", onuID)
885 }
886 if updated {
887 err = rsrcMgr.AddOnuGemInfo(ctx, intfID, onuID, *onugem)
888 if err != nil {
889 logger.Error(ctx, "Failed to add onugem to kv store")
890 return err
891 }
892 } else {
893 logger.Debugw(ctx, "Gem port not found in onu gem info", log.Fields{"gem": gemPort})
894 }
895 return nil
896}
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530897
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700898//GetOnuGemInfo gets onu gem info from the kvstore per interface
899func (rsrcMgr *OpenOltResourceMgr) GetOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32) (*OnuGemInfo, error) {
900 var err error
901 var Val []byte
902 var onugem OnuGemInfo
903
904 path := fmt.Sprintf(OnuGemInfoPath, intfID, onuID)
905
906 rsrcMgr.onuGemInfoLock.RLock()
907 val, ok := rsrcMgr.onuGemInfo[path]
908 rsrcMgr.onuGemInfoLock.RUnlock()
909 if ok {
910 return val, nil
911 }
912 value, err := rsrcMgr.KVStore.Get(ctx, path)
913 if err != nil {
914 logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path})
915 return nil, err
916 } else if value == nil {
917 logger.Debug(ctx, "No onuinfo for path", log.Fields{"path": path})
918 return nil, nil // returning nil as this could happen if there are no onus for the interface yet
919 }
920 if Val, err = kvstore.ToByte(value.Value); err != nil {
921 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530922 return nil, err
923 }
924
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700925 if err = json.Unmarshal(Val, &onugem); err != nil {
926 logger.Error(ctx, "Failed to unmarshall")
927 return nil, err
928 }
929 logger.Debugw(ctx, "found onugem info from path", log.Fields{"path": path, "onuGemInfo": onugem})
930 rsrcMgr.onuGemInfoLock.Lock()
931 rsrcMgr.onuGemInfo[path] = &onugem
932 rsrcMgr.onuGemInfoLock.Unlock()
933
934 return &onugem, nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530935}
936
Chaitrashree G S1a55b882020-02-04 17:35:35 -0500937// AddOnuGemInfo adds onu info on to the kvstore per interface
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700938func (rsrcMgr *OpenOltResourceMgr) AddOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, onuGem OnuGemInfo) error {
939
940 var Value []byte
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530941 var err error
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700942 Path := fmt.Sprintf(OnuGemInfoPath, intfID, onuID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530943
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700944 rsrcMgr.onuGemInfoLock.Lock()
945 rsrcMgr.onuGemInfo[Path] = &onuGem
946 rsrcMgr.onuGemInfoLock.Unlock()
947
948 Value, err = json.Marshal(onuGem)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530949 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700950 logger.Error(ctx, "failed to Marshal")
951 return err
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530952 }
953
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700954 if err = rsrcMgr.KVStore.Put(ctx, Path, Value); err != nil {
955 logger.Errorf(ctx, "Failed to update resource %s", Path)
956 return err
957 }
958 logger.Debugw(ctx, "added onu gem info", log.Fields{"onuGemInfo": onuGem})
959 return err
960}
961
962// DelOnuGemInfo deletes the onugem info from kvstore per ONU
963func (rsrcMgr *OpenOltResourceMgr) DelOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32) error {
964 path := fmt.Sprintf(OnuGemInfoPath, intfID, onuID)
965 rsrcMgr.onuGemInfoLock.Lock()
966 logger.Debugw(ctx, "removing onu gem info", log.Fields{"onuGemInfo": rsrcMgr.onuGemInfo[path]})
967 delete(rsrcMgr.onuGemInfo, path)
968 rsrcMgr.onuGemInfoLock.Unlock()
969
970 if err := rsrcMgr.KVStore.Delete(ctx, path); err != nil {
971 logger.Errorf(ctx, "failed to remove resource %s", path)
972 return err
973 }
Andrea Campanellab83b39d2020-03-30 11:41:16 +0200974 return nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530975}
976
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530977// 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 -0700978func (rsrcMgr *OpenOltResourceMgr) AddUniPortToOnuInfo(ctx context.Context, intfID uint32, onuID uint32, portNo uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530979
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700980 onugem, err := rsrcMgr.GetOnuGemInfo(ctx, intfID, onuID)
981 if err != nil || onugem == nil || onugem.SerialNumber == "" {
982 logger.Warnf(ctx, "failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530983 return
984 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700985
986 if onugem.OnuID == onuID {
987 for _, uni := range onugem.UniPorts {
988 if uni == portNo {
989 logger.Debugw(ctx, "uni already present in onugem info", log.Fields{"uni": portNo})
990 return
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530991 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530992 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700993 onugem.UniPorts = append(onugem.UniPorts, portNo)
994 } else {
995 logger.Warnw(ctx, "onu id mismatch in onu gem info", log.Fields{"intfID": intfID, "onuID": onuID})
996 return
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530997 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700998 err = rsrcMgr.AddOnuGemInfo(ctx, intfID, onuID, *onugem)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530999 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001000 logger.Errorw(ctx, "Failed to add uni port in onugem to kv store", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301001 return
1002 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301003}
1004
Esin Karaman7fb80c22020-07-16 14:23:33 +00001005//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 -07001006func (rsrcMgr *OpenOltResourceMgr) UpdateGemPortForPktIn(ctx context.Context, pktIn PacketInInfoKey, gemPort uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301007
Esin Karaman7fb80c22020-07-16 14:23:33 +00001008 path := fmt.Sprintf(OnuPacketINPath, pktIn.IntfID, pktIn.OnuID, pktIn.LogicalPort, pktIn.VlanID, pktIn.Priority)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001009 // update cache
1010 rsrcMgr.gemPortForPacketInInfoLock.Lock()
1011 rsrcMgr.gemPortForPacketInInfo[path] = gemPort
1012 rsrcMgr.gemPortForPacketInInfoLock.Unlock()
1013
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301014 Value, err := json.Marshal(gemPort)
1015 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001016 logger.Error(ctx, "Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301017 return
1018 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001019 if err = rsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001020 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"path": path, "value": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301021 return
1022 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001023 logger.Debugw(ctx, "added gem packet in successfully", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301024}
1025
Esin Karaman7fb80c22020-07-16 14:23:33 +00001026// 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 -07001027func (rsrcMgr *OpenOltResourceMgr) GetGemPortFromOnuPktIn(ctx context.Context, packetInInfoKey PacketInInfoKey) (uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301028
1029 var Val []byte
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301030
Esin Karaman7fb80c22020-07-16 14:23:33 +00001031 path := fmt.Sprintf(OnuPacketINPath, packetInInfoKey.IntfID, packetInInfoKey.OnuID, packetInInfoKey.LogicalPort,
1032 packetInInfoKey.VlanID, packetInInfoKey.Priority)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001033 // get from cache
1034 rsrcMgr.gemPortForPacketInInfoLock.RLock()
1035 gemPort, ok := rsrcMgr.gemPortForPacketInInfo[path]
1036 rsrcMgr.gemPortForPacketInInfoLock.RUnlock()
1037 if ok {
1038 logger.Debugw(ctx, "found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
1039 return gemPort, nil
1040 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301041
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001042 value, err := rsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301043 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001044 logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301045 return uint32(0), err
1046 } else if value == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001047 logger.Debugw(ctx, "No pkt in gem found", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301048 return uint32(0), nil
1049 }
1050
1051 if Val, err = kvstore.ToByte(value.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001052 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301053 return uint32(0), err
1054 }
1055 if err = json.Unmarshal(Val, &gemPort); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001056 logger.Error(ctx, "Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301057 return uint32(0), err
1058 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001059 logger.Debugw(ctx, "found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001060 // update cache
1061 rsrcMgr.gemPortForPacketInInfoLock.Lock()
1062 rsrcMgr.gemPortForPacketInInfo[path] = gemPort
1063 rsrcMgr.gemPortForPacketInInfoLock.Unlock()
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301064
1065 return gemPort, nil
1066}
1067
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001068//DeletePacketInGemPortForOnu deletes the packet-in gemport for ONU
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001069func (rsrcMgr *OpenOltResourceMgr) DeletePacketInGemPortForOnu(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301070
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001071 path := fmt.Sprintf(OnuPacketINPathPrefix, intfID, onuID, logicalPort)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001072
1073 value, err := rsrcMgr.KVStore.List(ctx, path)
Esin Karaman7fb80c22020-07-16 14:23:33 +00001074 if err != nil {
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001075 logger.Errorf(ctx, "failed-to-read-value-from-path-%s", path)
1076 return errors.New("failed-to-read-value-from-path-" + path)
Esin Karaman7fb80c22020-07-16 14:23:33 +00001077 }
Esin Karaman7fb80c22020-07-16 14:23:33 +00001078
1079 //remove them one by one
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001080 for key := range value {
1081 // Formulate the right key path suffix ti be delete
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001082 stringToBeReplaced := fmt.Sprintf(BasePathKvStore, rsrcMgr.KVStore.PathPrefix, rsrcMgr.DeviceID) + "/"
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
1090 logger.Debugf(ctx, "removing-key-%s", key)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001091 if err := rsrcMgr.KVStore.Delete(ctx, key); err != nil {
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001092 logger.Errorf(ctx, "failed-to-remove-resource-%s", key)
Esin Karaman7fb80c22020-07-16 14:23:33 +00001093 return err
1094 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301095 }
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001096
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301097 return nil
1098}
1099
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001100//GetFlowIDsForGem gets the list of FlowIDs for the given gemport
1101func (rsrcMgr *OpenOltResourceMgr) GetFlowIDsForGem(ctx context.Context, intf uint32, gem uint32) ([]uint64, error) {
1102 path := fmt.Sprintf(FlowIDsForGem, intf, gem)
1103
1104 // get from cache
1105 rsrcMgr.flowIDsForGemLock.RLock()
1106 flowIDs, ok := rsrcMgr.flowIDsForGem[gem]
1107 rsrcMgr.flowIDsForGemLock.RUnlock()
1108 if ok {
1109 return flowIDs, nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301110 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301111
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001112 value, err := rsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301113 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001114 logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path})
1115 return nil, err
1116 } else if value == nil {
1117 logger.Debug(ctx, "no flow-ids found", log.Fields{"path": path})
1118 return nil, nil
1119 }
1120 Val, err := kvstore.ToByte(value.Value)
1121 if err != nil {
1122 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301123 return nil, err
1124 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301125
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001126 if err = json.Unmarshal(Val, &flowIDs); err != nil {
1127 logger.Error(ctx, "Failed to unmarshall")
1128 return nil, err
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301129 }
1130
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001131 // update cache
1132 rsrcMgr.flowIDsForGemLock.Lock()
1133 rsrcMgr.flowIDsForGem[gem] = flowIDs
1134 rsrcMgr.flowIDsForGemLock.Unlock()
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301135
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001136 return flowIDs, nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301137}
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301138
1139//UpdateFlowIDsForGem updates flow id per gemport
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001140func (rsrcMgr *OpenOltResourceMgr) UpdateFlowIDsForGem(ctx context.Context, intf uint32, gem uint32, flowIDs []uint64) error {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301141 var val []byte
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001142 path := fmt.Sprintf(FlowIDsForGem, intf, gem)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301143
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001144 // update cache
1145 rsrcMgr.flowIDsForGemLock.Lock()
1146 rsrcMgr.flowIDsForGem[gem] = flowIDs
1147 rsrcMgr.flowIDsForGemLock.Unlock()
1148
1149 if flowIDs == nil {
1150 return nil
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301151 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001152 val, err := json.Marshal(flowIDs)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301153 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001154 logger.Error(ctx, "Failed to marshal data", log.Fields{"err": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301155 return err
1156 }
Girish Gowdrab77ded92020-04-08 11:45:05 -07001157
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001158 if err = rsrcMgr.KVStore.Put(ctx, path, val); err != nil {
1159 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"err": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301160 return err
1161 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001162 logger.Debugw(ctx, "added flowid list for gem to kv successfully", log.Fields{"path": path, "flowidlist": flowIDs})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301163 return nil
1164}
1165
1166//DeleteFlowIDsForGem deletes the flowID list entry per gem from kvstore.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001167func (rsrcMgr *OpenOltResourceMgr) DeleteFlowIDsForGem(ctx context.Context, intf uint32, gem uint32) {
1168 path := fmt.Sprintf(FlowIDsForGem, intf, gem)
1169 // update cache
1170 rsrcMgr.flowIDsForGemLock.Lock()
1171 delete(rsrcMgr.flowIDsForGem, gem)
1172 rsrcMgr.flowIDsForGemLock.Unlock()
1173 if err := rsrcMgr.KVStore.Delete(ctx, path); err != nil {
1174 logger.Errorw(ctx, "Failed to delete from kvstore", log.Fields{"err": err, "path": path})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301175 }
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301176}
Esin Karamanccb714b2019-11-29 15:02:06 +00001177
1178//GetMcastQueuePerInterfaceMap gets multicast queue info per pon interface
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001179func (rsrcMgr *OpenOltResourceMgr) GetMcastQueuePerInterfaceMap(ctx context.Context) (map[uint32][]uint32, error) {
Kent Hagermane6ff1012020-07-14 15:07:53 -04001180 path := McastQueuesForIntf
Esin Karamanccb714b2019-11-29 15:02:06 +00001181 var val []byte
1182
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001183 rsrcMgr.mcastQueueForIntfLock.RLock()
1184 if rsrcMgr.mcastQueueForIntfLoadedFromKvStore {
1185 rsrcMgr.mcastQueueForIntfLock.RUnlock()
1186 return rsrcMgr.mcastQueueForIntf, nil
1187 }
1188 rsrcMgr.mcastQueueForIntfLock.RUnlock()
1189
1190 kvPair, err := rsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001191 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001192 logger.Error(ctx, "failed to get data from kv store")
Esin Karamanccb714b2019-11-29 15:02:06 +00001193 return nil, err
1194 }
1195 if kvPair != nil && kvPair.Value != nil {
1196 if val, err = kvstore.ToByte(kvPair.Value); err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001197 logger.Error(ctx, "Failed to convert to byte array ", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001198 return nil, err
1199 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001200 rsrcMgr.mcastQueueForIntfLock.Lock()
1201 defer rsrcMgr.mcastQueueForIntfLock.Unlock()
1202 if err = json.Unmarshal(val, &rsrcMgr.mcastQueueForIntf); err != nil {
1203 logger.Error(ctx, "Failed to unmarshall ", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001204 return nil, err
1205 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001206 rsrcMgr.mcastQueueForIntfLoadedFromKvStore = true
Esin Karamanccb714b2019-11-29 15:02:06 +00001207 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001208 return rsrcMgr.mcastQueueForIntf, nil
Esin Karamanccb714b2019-11-29 15:02:06 +00001209}
1210
1211//AddMcastQueueForIntf adds multicast queue for pon interface
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001212func (rsrcMgr *OpenOltResourceMgr) AddMcastQueueForIntf(ctx context.Context, intf uint32, gem uint32, servicePriority uint32) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001213 var val []byte
Kent Hagermane6ff1012020-07-14 15:07:53 -04001214 path := McastQueuesForIntf
Esin Karamanccb714b2019-11-29 15:02:06 +00001215
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001216 // Load local cache from kv store the first time
1217 rsrcMgr.mcastQueueForIntfLock.RLock()
1218 if !rsrcMgr.mcastQueueForIntfLoadedFromKvStore {
1219 rsrcMgr.mcastQueueForIntfLock.RUnlock()
1220 _, err := rsrcMgr.GetMcastQueuePerInterfaceMap(ctx)
1221 if err != nil {
1222 logger.Errorw(ctx, "Failed to get multicast queue info for interface", log.Fields{"err": err, "intf": intf})
1223 return err
1224 }
1225 } else {
1226 rsrcMgr.mcastQueueForIntfLock.RUnlock()
1227 }
1228
1229 // Update KV store
1230 rsrcMgr.mcastQueueForIntfLock.Lock()
1231 rsrcMgr.mcastQueueForIntf[intf] = []uint32{gem, servicePriority}
1232 val, err := json.Marshal(rsrcMgr.mcastQueueForIntf)
Esin Karamanccb714b2019-11-29 15:02:06 +00001233 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001234 rsrcMgr.mcastQueueForIntfLock.Unlock()
1235 logger.Errorw(ctx, "Failed to marshal data", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001236 return err
1237 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001238 rsrcMgr.mcastQueueForIntfLock.Unlock()
1239
1240 if err = rsrcMgr.KVStore.Put(ctx, path, val); err != nil {
1241 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"err": err, "path": path, "value": val})
Esin Karamanccb714b2019-11-29 15:02:06 +00001242 return err
1243 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001244 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 +00001245 return nil
1246}
1247
1248//AddFlowGroupToKVStore adds flow group into KV store
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001249func (rsrcMgr *OpenOltResourceMgr) AddFlowGroupToKVStore(ctx context.Context, groupEntry *ofp.OfpGroupEntry, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001250 var Value []byte
1251 var err error
1252 var path string
1253 if cached {
1254 path = fmt.Sprintf(FlowGroupCached, groupEntry.Desc.GroupId)
1255 } else {
1256 path = fmt.Sprintf(FlowGroup, groupEntry.Desc.GroupId)
1257 }
1258 //build group info object
1259 var outPorts []uint32
1260 for _, ofBucket := range groupEntry.Desc.Buckets {
1261 for _, ofAction := range ofBucket.Actions {
1262 if ofAction.Type == ofp.OfpActionType_OFPAT_OUTPUT {
1263 outPorts = append(outPorts, ofAction.GetOutput().Port)
1264 }
1265 }
1266 }
1267 groupInfo := GroupInfo{
1268 GroupID: groupEntry.Desc.GroupId,
1269 OutPorts: outPorts,
1270 }
1271
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001272 rsrcMgr.groupInfoLock.Lock()
1273 rsrcMgr.groupInfo[path] = &groupInfo
1274 rsrcMgr.groupInfoLock.Unlock()
1275
Esin Karamanccb714b2019-11-29 15:02:06 +00001276 Value, err = json.Marshal(groupInfo)
1277
1278 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001279 logger.Error(ctx, "failed to Marshal flow group object")
Esin Karamanccb714b2019-11-29 15:02:06 +00001280 return err
1281 }
1282
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001283 if err = rsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001284 logger.Errorf(ctx, "Failed to update resource %s", path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001285 return err
1286 }
1287 return nil
1288}
1289
1290//RemoveFlowGroupFromKVStore removes flow group from KV store
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001291func (rsrcMgr *OpenOltResourceMgr) RemoveFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001292 var path string
1293 if cached {
1294 path = fmt.Sprintf(FlowGroupCached, groupID)
1295 } else {
1296 path = fmt.Sprintf(FlowGroup, groupID)
1297 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001298 rsrcMgr.groupInfoLock.Lock()
1299 delete(rsrcMgr.groupInfo, path)
1300 rsrcMgr.groupInfoLock.Unlock()
1301
1302 if err := rsrcMgr.KVStore.Delete(ctx, path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001303 logger.Errorf(ctx, "Failed to remove resource %s due to %s", path, err)
Esin Karamand519bbf2020-07-01 11:16:03 +00001304 return err
Esin Karamanccb714b2019-11-29 15:02:06 +00001305 }
Esin Karamand519bbf2020-07-01 11:16:03 +00001306 return nil
Esin Karamanccb714b2019-11-29 15:02:06 +00001307}
1308
1309//GetFlowGroupFromKVStore fetches flow group from the KV store. Returns (false, {} error) if any problem occurs during
1310//fetching the data. Returns (true, groupInfo, nil) if the group is fetched successfully.
1311// Returns (false, {}, nil) if the group does not exists in the KV store.
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001312func (rsrcMgr *OpenOltResourceMgr) GetFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) (bool, GroupInfo, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001313 var groupInfo GroupInfo
1314 var path string
1315 if cached {
1316 path = fmt.Sprintf(FlowGroupCached, groupID)
1317 } else {
1318 path = fmt.Sprintf(FlowGroup, groupID)
1319 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001320
1321 // read from cache
1322 rsrcMgr.groupInfoLock.RLock()
1323 gi, ok := rsrcMgr.groupInfo[path]
1324 rsrcMgr.groupInfoLock.RUnlock()
1325 if ok {
1326 return true, *gi, nil
1327 }
1328
1329 kvPair, err := rsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001330 if err != nil {
1331 return false, groupInfo, err
1332 }
1333 if kvPair != nil && kvPair.Value != nil {
1334 Val, err := kvstore.ToByte(kvPair.Value)
1335 if err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001336 logger.Errorw(ctx, "Failed to convert flow group into byte array", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001337 return false, groupInfo, err
1338 }
1339 if err = json.Unmarshal(Val, &groupInfo); err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001340 logger.Errorw(ctx, "Failed to unmarshal", log.Fields{"err": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001341 return false, groupInfo, err
1342 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -07001343 // update cache
1344 rsrcMgr.groupInfoLock.Lock()
1345 rsrcMgr.groupInfo[path] = &groupInfo
1346 rsrcMgr.groupInfoLock.Unlock()
1347
Esin Karamanccb714b2019-11-29 15:02:06 +00001348 return true, groupInfo, nil
1349 }
1350 return false, groupInfo, nil
1351}
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001352
1353// toByte converts an interface value to a []byte. The interface should either be of
1354// a string type or []byte. Otherwise, an error is returned.
1355func toByte(value interface{}) ([]byte, error) {
1356 switch t := value.(type) {
1357 case []byte:
1358 return value.([]byte), nil
1359 case string:
1360 return []byte(value.(string)), nil
1361 default:
1362 return nil, fmt.Errorf("unexpected-type-%T", t)
1363 }
1364}