blob: a50131000fd31b42aff5ed352f56b4144777c8fd [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"
25 "strconv"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070026 "strings"
Girish Gowdra38d533d2020-03-30 20:38:51 -070027 "sync"
Neha Sharmacc656962020-04-14 14:26:11 +000028 "time"
Abhilash S.L7f17e402019-03-15 17:40:41 +053029
Kent Hagermane6ff1012020-07-14 15:07:53 -040030 "github.com/opencord/voltha-openolt-adapter/internal/pkg/olterrors"
31
Girish Gowdraa09aeab2020-09-14 16:30:52 -070032 "github.com/opencord/voltha-lib-go/v4/pkg/db"
33 "github.com/opencord/voltha-lib-go/v4/pkg/db/kvstore"
34 "github.com/opencord/voltha-lib-go/v4/pkg/log"
35 ponrmgr "github.com/opencord/voltha-lib-go/v4/pkg/ponresourcemanager"
36 ofp "github.com/opencord/voltha-protos/v4/go/openflow_13"
37 "github.com/opencord/voltha-protos/v4/go/openolt"
Abhilash S.L7f17e402019-03-15 17:40:41 +053038)
39
salmansiddiqui7ac62132019-08-22 03:58:50 +000040const (
41 // KvstoreTimeout specifies the time out for KV Store Connection
Neha Sharmacc656962020-04-14 14:26:11 +000042 KvstoreTimeout = 5 * time.Second
Matteo Scandolodfa7a972020-11-06 13:03:40 -080043 // BasePathKvStore - <pathPrefix>/openolt/<device_id>
44 BasePathKvStore = "%s/openolt/{%s}"
Gamze Abakafee36392019-10-03 11:17:24 +000045 // TpIDPathSuffix - <(pon_id, onu_id, uni_id)>/tp_id
46 TpIDPathSuffix = "{%d,%d,%d}/tp_id"
47 //MeterIDPathSuffix - <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
48 MeterIDPathSuffix = "{%d,%d,%d}/{%d}/meter_id/{%s}"
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053049 //NnniIntfID - nniintfids
50 NnniIntfID = "nniintfids"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070051 // OnuPacketINPathPrefix - path prefix where ONU packet-in vlanID/PCP is stored
52 //format: onu_packetin/{<intfid>,<onuid>,<logicalport>}
53 OnuPacketINPathPrefix = "onu_packetin/{%d,%d,%d}"
54 // OnuPacketINPath path on the kvstore to store packetin gemport,which will be used for packetin, packetout
55 //format: onu_packetin/{<intfid>,<onuid>,<logicalport>}/{<vlanId>,<priority>}
56 OnuPacketINPath = OnuPacketINPathPrefix + "/{%d,%d}"
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +053057 //FlowIDsForGem flowids_per_gem/<intfid>
58 FlowIDsForGem = "flowids_per_gem/{%d}"
Esin Karamanccb714b2019-11-29 15:02:06 +000059 //McastQueuesForIntf multicast queues for pon interfaces
60 McastQueuesForIntf = "mcast_qs_for_int"
61 //FlowGroup flow_groups/<flow_group_id>
62 // A group is stored under this path on the KV store after it has been installed to the device.
63 // It should also be deleted after it has been removed from the device accordingly.
64 FlowGroup = "flow_groups/{%d}"
65 //FlowGroupCached flow_groups_cached/<flow_group_id>
66 // When a group add request received, we create the group without setting any members to it since we cannot
67 // set any members to a group until it is associated with a multicast flow. It is a BAL limitation.
68 // When the related multicast flow has been created we perform set members operation for the group.
69 // That is why we need to keep the members of a group until the multicast flow creation request comes.
70 // We preserve the groups under "FlowGroupsCached" directory in the KV store temporarily. Having set members,
71 // we remove the group from the cached group store.
72 FlowGroupCached = "flow_groups_cached/{%d}"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070073
74 //FlowIDPath - Path on the KV store for storing list of Flow IDs for a given subscriber
75 //Format: BasePathKvStore/<(pon_intf_id, onu_id, uni_id)>/flow_ids
76 FlowIDPath = "{%s}/flow_ids"
77 //FlowIDInfoPath - Used to store more metadata associated with the flow_id
78 //Format: BasePathKvStore/<(pon_intf_id, onu_id, uni_id)>/flow_id_info/<flow_id>
79 FlowIDInfoPath = "{%s}/flow_id_info/{%d}"
salmansiddiqui7ac62132019-08-22 03:58:50 +000080)
Abhilash S.L7f17e402019-03-15 17:40:41 +053081
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070082// FlowInfo holds the flow information
Abhilash S.L8ee90712019-04-29 16:24:22 +053083type FlowInfo struct {
Girish Gowdraa09aeab2020-09-14 16:30:52 -070084 Flow *openolt.Flow
85 IsSymmtricFlow bool
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053086}
87
88// OnuGemInfo holds onu information along with gem port list and uni port list
89type OnuGemInfo struct {
90 OnuID uint32
91 SerialNumber string
92 IntfID uint32
93 GemPorts []uint32
94 UniPorts []uint32
95}
96
97// PacketInInfoKey is the key for packet in gemport
98type PacketInInfoKey struct {
99 IntfID uint32
100 OnuID uint32
101 LogicalPort uint32
Esin Karaman7fb80c22020-07-16 14:23:33 +0000102 VlanID uint16
103 Priority uint8
Abhilash S.L8ee90712019-04-29 16:24:22 +0530104}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700105
Esin Karamanccb714b2019-11-29 15:02:06 +0000106// GroupInfo holds group information
107type GroupInfo struct {
108 GroupID uint32
109 OutPorts []uint32
110}
111
Girish Gowdra3fe4cc52021-03-24 23:04:19 -0700112// MeterInfo store meter information at path <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
113type MeterInfo struct {
114 RefCnt uint8 // number of flow references for this meter. When RefCnt is 0, the MeterInfo should be deleted.
115 MeterConfig ofp.OfpMeterConfig
116}
117
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700118// OpenOltResourceMgr holds resource related information as provided below for each field
Abhilash S.L7f17e402019-03-15 17:40:41 +0530119type OpenOltResourceMgr struct {
Neha Sharma3f221ae2020-04-29 19:02:12 +0000120 DeviceID string // OLT device id
121 Address string // Host and port of the kv store to connect to
122 Args string // args
123 KVStore *db.Backend // backend kv store connection handle
124 DeviceType string
125 DevInfo *openolt.DeviceInfo // device information
Girish Gowdru0c588b22019-04-23 23:24:56 -0400126 // array of pon resource managers per interface technology
127 ResourceMgrs map[uint32]*ponrmgr.PONResourceManager
Girish Gowdra38d533d2020-03-30 20:38:51 -0700128
129 // This protects concurrent gemport_id allocate/delete calls on a per PON port basis
130 GemPortIDMgmtLock []sync.RWMutex
131 // This protects concurrent alloc_id allocate/delete calls on a per PON port basis
132 AllocIDMgmtLock []sync.RWMutex
133 // This protects concurrent onu_id allocate/delete calls on a per PON port basis
134 OnuIDMgmtLock []sync.RWMutex
Abhilash S.L7f17e402019-03-15 17:40:41 +0530135}
136
Neha Sharma96b7bf22020-06-15 10:37:32 +0000137func newKVClient(ctx context.Context, storeType string, address string, timeout time.Duration) (kvstore.Client, error) {
138 logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400139 switch storeType {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400140 case "etcd":
Neha Sharma96b7bf22020-06-15 10:37:32 +0000141 return kvstore.NewEtcdClient(ctx, address, timeout, log.FatalLevel)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400142 }
143 return nil, errors.New("unsupported-kv-store")
Abhilash S.L7f17e402019-03-15 17:40:41 +0530144}
145
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700146// SetKVClient sets the KV client and return a kv backend
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800147func SetKVClient(ctx context.Context, backend string, addr string, DeviceID string, basePathKvStore string) *db.Backend {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400148 // TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
149 // issue between kv store and backend , core is not calling NewBackend directly
Neha Sharma96b7bf22020-06-15 10:37:32 +0000150 kvClient, err := newKVClient(ctx, backend, addr, KvstoreTimeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400151 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000152 logger.Fatalw(ctx, "Failed to init KV client\n", log.Fields{"err": err})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400153 return nil
154 }
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700155
sbarbaria8910ba2019-11-05 10:12:23 -0500156 kvbackend := &db.Backend{
Girish Gowdru0c588b22019-04-23 23:24:56 -0400157 Client: kvClient,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700158 StoreType: backend,
Neha Sharma3f221ae2020-04-29 19:02:12 +0000159 Address: addr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700160 Timeout: KvstoreTimeout,
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800161 PathPrefix: fmt.Sprintf(BasePathKvStore, basePathKvStore, DeviceID)}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530162
Girish Gowdru0c588b22019-04-23 23:24:56 -0400163 return kvbackend
Abhilash S.L7f17e402019-03-15 17:40:41 +0530164}
165
Gamze Abakafee36392019-10-03 11:17:24 +0000166// NewResourceMgr init a New resource manager instance which in turn instantiates pon resource manager
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700167// instances according to technology. Initializes the default resource ranges for all
168// the resources.
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800169func NewResourceMgr(ctx context.Context, deviceID string, KVStoreAddress string, kvStoreType string, deviceType string, devInfo *openolt.DeviceInfo, basePathKvStore string) *OpenOltResourceMgr {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400170 var ResourceMgr OpenOltResourceMgr
divyadesai3af43e12020-08-18 07:10:54 +0000171 logger.Debugf(ctx, "Init new resource manager , address: %s, device-id: %s", KVStoreAddress, deviceID)
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700172 ResourceMgr.DeviceID = deviceID
Neha Sharma3f221ae2020-04-29 19:02:12 +0000173 ResourceMgr.Address = KVStoreAddress
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700174 ResourceMgr.DeviceType = deviceType
175 ResourceMgr.DevInfo = devInfo
Girish Gowdra38d533d2020-03-30 20:38:51 -0700176 NumPONPorts := devInfo.GetPonPorts()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530177
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700178 Backend := kvStoreType
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800179 ResourceMgr.KVStore = SetKVClient(ctx, Backend, ResourceMgr.Address, deviceID, basePathKvStore)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400180 if ResourceMgr.KVStore == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000181 logger.Error(ctx, "Failed to setup KV store")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400182 }
183 Ranges := make(map[string]*openolt.DeviceInfo_DeviceResourceRanges)
184 RsrcMgrsByTech := make(map[string]*ponrmgr.PONResourceManager)
185 ResourceMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530186
Girish Gowdra38d533d2020-03-30 20:38:51 -0700187 ResourceMgr.AllocIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
188 ResourceMgr.GemPortIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
189 ResourceMgr.OnuIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
190
Girish Gowdru0c588b22019-04-23 23:24:56 -0400191 // TODO self.args = registry('main').get_args()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530192
Girish Gowdru0c588b22019-04-23 23:24:56 -0400193 /*
194 If a legacy driver returns protobuf without any ranges,s synthesize one from
Gamze Abakafee36392019-10-03 11:17:24 +0000195 the legacy global per-device information. This, in theory, is temporary until
Girish Gowdru0c588b22019-04-23 23:24:56 -0400196 the legacy drivers are upgrade to support pool ranges.
197 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700198 if devInfo.Ranges == nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400199 var ranges openolt.DeviceInfo_DeviceResourceRanges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700200 ranges.Technology = devInfo.GetTechnology()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530201
Girish Gowdru0c588b22019-04-23 23:24:56 -0400202 var index uint32
203 for index = 0; index < NumPONPorts; index++ {
204 ranges.IntfIds = append(ranges.IntfIds, index)
205 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530206
Abhilash S.L8ee90712019-04-29 16:24:22 +0530207 var Pool openolt.DeviceInfo_DeviceResourceRanges_Pool
Girish Gowdru0c588b22019-04-23 23:24:56 -0400208 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700209 Pool.Start = devInfo.OnuIdStart
210 Pool.End = devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400211 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
cbabuabf02352019-10-15 13:14:56 +0200212 onuPool := Pool
213 ranges.Pools = append(ranges.Pools, &onuPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530214
Girish Gowdru0c588b22019-04-23 23:24:56 -0400215 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700216 Pool.Start = devInfo.AllocIdStart
217 Pool.End = devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400218 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200219 allocPool := Pool
220 ranges.Pools = append(ranges.Pools, &allocPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530221
Girish Gowdru0c588b22019-04-23 23:24:56 -0400222 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700223 Pool.Start = devInfo.GemportIdStart
224 Pool.End = devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400225 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200226 gemPool := Pool
227 ranges.Pools = append(ranges.Pools, &gemPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530228
Girish Gowdru0c588b22019-04-23 23:24:56 -0400229 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700230 Pool.Start = devInfo.FlowIdStart
231 Pool.End = devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400232 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
Abhilash S.L8ee90712019-04-29 16:24:22 +0530233 ranges.Pools = append(ranges.Pools, &Pool)
234 // Add to device info
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700235 devInfo.Ranges = append(devInfo.Ranges, &ranges)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400236 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530237
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700238 // Create a separate Resource Manager instance for each range. This assumes that
Girish Gowdru0c588b22019-04-23 23:24:56 -0400239 // each technology is represented by only a single range
240 var GlobalPONRsrcMgr *ponrmgr.PONResourceManager
241 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700242 for _, TechRange := range devInfo.Ranges {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400243 technology := TechRange.Technology
Neha Sharma96b7bf22020-06-15 10:37:32 +0000244 logger.Debugf(ctx, "Device info technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400245 Ranges[technology] = TechRange
Neha Sharma96b7bf22020-06-15 10:37:32 +0000246
247 RsrcMgrsByTech[technology], err = ponrmgr.NewPONResourceManager(ctx, technology, deviceType, deviceID,
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800248 Backend, ResourceMgr.Address, basePathKvStore)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400249 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000250 logger.Errorf(ctx, "Failed to create pon resource manager instance for technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400251 return nil
252 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700253 // resource_mgrs_by_tech[technology] = resource_mgr
Girish Gowdru0c588b22019-04-23 23:24:56 -0400254 if GlobalPONRsrcMgr == nil {
255 GlobalPONRsrcMgr = RsrcMgrsByTech[technology]
256 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700257 for _, IntfID := range TechRange.IntfIds {
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700258 ResourceMgr.ResourceMgrs[IntfID] = RsrcMgrsByTech[technology]
Girish Gowdru0c588b22019-04-23 23:24:56 -0400259 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700260 // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
npujarec5762e2020-01-01 14:08:48 +0530261 InitializeDeviceResourceRangeAndPool(ctx, RsrcMgrsByTech[technology], GlobalPONRsrcMgr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700262 TechRange, devInfo)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400263 }
264 // After we have initialized resource ranges, initialize the
265 // resource pools accordingly.
266 for _, PONRMgr := range RsrcMgrsByTech {
npujarec5762e2020-01-01 14:08:48 +0530267 _ = PONRMgr.InitDeviceResourcePool(ctx)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400268 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000269 logger.Info(ctx, "Initialization of resource manager success!")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400270 return &ResourceMgr
Abhilash S.L7f17e402019-03-15 17:40:41 +0530271}
272
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700273// InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply
274// device specific information. If KV doesn't exist
275// or is broader than the device, the device's information will
276// dictate the range limits
npujarec5762e2020-01-01 14:08:48 +0530277func InitializeDeviceResourceRangeAndPool(ctx context.Context, ponRMgr *ponrmgr.PONResourceManager, globalPONRMgr *ponrmgr.PONResourceManager,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700278 techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530279
Girish Gowdru0c588b22019-04-23 23:24:56 -0400280 // init the resource range pool according to the sharing type
Abhilash S.L7f17e402019-03-15 17:40:41 +0530281
Neha Sharma96b7bf22020-06-15 10:37:32 +0000282 logger.Debugf(ctx, "Resource range pool init for technology %s", ponRMgr.Technology)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700283 // first load from KV profiles
npujarec5762e2020-01-01 14:08:48 +0530284 status := ponRMgr.InitResourceRangesFromKVStore(ctx)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700285 if !status {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000286 logger.Debugf(ctx, "Failed to load resource ranges from KV store for tech %s", ponRMgr.Technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400287 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530288
Girish Gowdru0c588b22019-04-23 23:24:56 -0400289 /*
290 Then apply device specific information. If KV doesn't exist
Gamze Abakafee36392019-10-03 11:17:24 +0000291 or is broader than the device, the device's information will
Girish Gowdru0c588b22019-04-23 23:24:56 -0400292 dictate the range limits
293 */
Neha Sharma96b7bf22020-06-15 10:37:32 +0000294 logger.Debugw(ctx, "Using device info to init pon resource ranges", log.Fields{"Tech": ponRMgr.Technology})
Abhilash S.L7f17e402019-03-15 17:40:41 +0530295
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700296 ONUIDStart := devInfo.OnuIdStart
297 ONUIDEnd := devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400298 ONUIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
299 ONUIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700300 AllocIDStart := devInfo.AllocIdStart
301 AllocIDEnd := devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400302 AllocIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
303 AllocIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700304 GEMPortIDStart := devInfo.GemportIdStart
305 GEMPortIDEnd := devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400306 GEMPortIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
307 GEMPortIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700308 FlowIDStart := devInfo.FlowIdStart
309 FlowIDEnd := devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400310 FlowIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
311 FlowIDSharedPoolID := uint32(0)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530312
Girish Gowdru0c588b22019-04-23 23:24:56 -0400313 var FirstIntfPoolID uint32
314 var SharedPoolID uint32
Abhilash S.L7f17e402019-03-15 17:40:41 +0530315
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400316 /*
317 * As a zero check is made against SharedPoolID to check whether the resources are shared across all intfs
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700318 * if resources are shared across interfaces then SharedPoolID is given a positive number.
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400319 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700320 for _, FirstIntfPoolID = range techRange.IntfIds {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400321 // skip the intf id 0
322 if FirstIntfPoolID == 0 {
323 continue
324 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400325 break
326 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530327
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700328 for _, RangePool := range techRange.Pools {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400329 if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400330 SharedPoolID = FirstIntfPoolID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400331 } else if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_SAME_TECH {
332 SharedPoolID = FirstIntfPoolID
333 } else {
334 SharedPoolID = 0
335 }
336 if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID {
337 ONUIDStart = RangePool.Start
338 ONUIDEnd = RangePool.End
339 ONUIDShared = RangePool.Sharing
340 ONUIDSharedPoolID = SharedPoolID
341 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID {
342 AllocIDStart = RangePool.Start
343 AllocIDEnd = RangePool.End
344 AllocIDShared = RangePool.Sharing
345 AllocIDSharedPoolID = SharedPoolID
346 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID {
347 GEMPortIDStart = RangePool.Start
348 GEMPortIDEnd = RangePool.End
349 GEMPortIDShared = RangePool.Sharing
350 GEMPortIDSharedPoolID = SharedPoolID
351 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID {
352 FlowIDStart = RangePool.Start
353 FlowIDEnd = RangePool.End
354 FlowIDShared = RangePool.Sharing
355 FlowIDSharedPoolID = SharedPoolID
356 }
357 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530358
Neha Sharma96b7bf22020-06-15 10:37:32 +0000359 logger.Debugw(ctx, "Device info init", log.Fields{"technology": techRange.Technology,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400360 "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd, "onu_id_shared_pool_id": ONUIDSharedPoolID,
361 "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd,
362 "alloc_id_shared_pool_id": AllocIDSharedPoolID,
363 "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd,
364 "gemport_id_shared_pool_id": GEMPortIDSharedPoolID,
365 "flow_id_start": FlowIDStart,
366 "flow_id_end_idx": FlowIDEnd,
367 "flow_id_shared_pool_id": FlowIDSharedPoolID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700368 "intf_ids": techRange.IntfIds,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400369 "uni_id_start": 0,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700370 "uni_id_end_idx": 1, /*MaxUNIIDperONU()*/
371 })
Abhilash S.L7f17e402019-03-15 17:40:41 +0530372
Neha Sharma96b7bf22020-06-15 10:37:32 +0000373 ponRMgr.InitDefaultPONResourceRanges(ctx, ONUIDStart, ONUIDEnd, ONUIDSharedPoolID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400374 AllocIDStart, AllocIDEnd, AllocIDSharedPoolID,
375 GEMPortIDStart, GEMPortIDEnd, GEMPortIDSharedPoolID,
376 FlowIDStart, FlowIDEnd, FlowIDSharedPoolID, 0, 1,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700377 devInfo.PonPorts, techRange.IntfIds)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530378
Girish Gowdru0c588b22019-04-23 23:24:56 -0400379 // For global sharing, make sure to refresh both local and global resource manager instances' range
Abhilash S.L7f17e402019-03-15 17:40:41 +0530380
Girish Gowdru0c588b22019-04-23 23:24:56 -0400381 if ONUIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000382 globalPONRMgr.UpdateRanges(ctx, ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400383 "", 0, nil)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000384 ponRMgr.UpdateRanges(ctx, ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700385 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400386 }
387 if AllocIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000388 globalPONRMgr.UpdateRanges(ctx, ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400389 "", 0, nil)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530390
Neha Sharma96b7bf22020-06-15 10:37:32 +0000391 ponRMgr.UpdateRanges(ctx, ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700392 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400393 }
394 if GEMPortIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000395 globalPONRMgr.UpdateRanges(ctx, ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400396 "", 0, nil)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000397 ponRMgr.UpdateRanges(ctx, ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700398 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400399 }
400 if FlowIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000401 globalPONRMgr.UpdateRanges(ctx, ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400402 "", 0, nil)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000403 ponRMgr.UpdateRanges(ctx, ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700404 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400405 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530406
Girish Gowdru0c588b22019-04-23 23:24:56 -0400407 // Make sure loaded range fits the platform bit encoding ranges
Neha Sharma96b7bf22020-06-15 10:37:32 +0000408 ponRMgr.UpdateRanges(ctx, ponrmgr.UNI_ID_START_IDX, 0, ponrmgr.UNI_ID_END_IDX /* TODO =OpenOltPlatform.MAX_UNIS_PER_ONU-1*/, 1, "", 0, nil)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530409}
410
Devmalya Paul495b94a2019-08-27 19:42:00 -0400411// Delete clears used resources for the particular olt device being deleted
npujarec5762e2020-01-01 14:08:48 +0530412func (RsrcMgr *OpenOltResourceMgr) Delete(ctx context.Context) error {
Devmalya Paul495b94a2019-08-27 19:42:00 -0400413 /* TODO
414 def __del__(self):
415 self.log.info("clearing-device-resource-pool")
416 for key, resource_mgr in self.resource_mgrs.iteritems():
417 resource_mgr.clear_device_resource_pool()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530418
Devmalya Paul495b94a2019-08-27 19:42:00 -0400419 def assert_pon_id_limit(self, pon_intf_id):
420 assert pon_intf_id in self.resource_mgrs
Abhilash S.L7f17e402019-03-15 17:40:41 +0530421
Devmalya Paul495b94a2019-08-27 19:42:00 -0400422 def assert_onu_id_limit(self, pon_intf_id, onu_id):
423 self.assert_pon_id_limit(pon_intf_id)
424 self.resource_mgrs[pon_intf_id].assert_resource_limits(onu_id, PONResourceManager.ONU_ID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530425
Devmalya Paul495b94a2019-08-27 19:42:00 -0400426 @property
427 def max_uni_id_per_onu(self):
428 return 0 #OpenOltPlatform.MAX_UNIS_PER_ONU-1, zero-based indexing Uncomment or override to make default multi-uni
Abhilash S.L7f17e402019-03-15 17:40:41 +0530429
Devmalya Paul495b94a2019-08-27 19:42:00 -0400430 def assert_uni_id_limit(self, pon_intf_id, onu_id, uni_id):
431 self.assert_onu_id_limit(pon_intf_id, onu_id)
432 self.resource_mgrs[pon_intf_id].assert_resource_limits(uni_id, PONResourceManager.UNI_ID)
433 */
434 for _, rsrcMgr := range RsrcMgr.ResourceMgrs {
npujarec5762e2020-01-01 14:08:48 +0530435 if err := rsrcMgr.ClearDeviceResourcePool(ctx); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000436 logger.Debug(ctx, "Failed to clear device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400437 return err
438 }
439 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000440 logger.Debug(ctx, "Cleared device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400441 return nil
442}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530443
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700444// GetONUID returns the available OnuID for the given pon-port
npujarec5762e2020-01-01 14:08:48 +0530445func (RsrcMgr *OpenOltResourceMgr) GetONUID(ctx context.Context, ponIntfID uint32) (uint32, error) {
salmansiddiqui352a45c2019-08-19 10:15:36 +0000446 // Check if Pon Interface ID is present in Resource-manager-map
Girish Gowdrab77ded92020-04-08 11:45:05 -0700447 RsrcMgr.OnuIDMgmtLock[ponIntfID].Lock()
448 defer RsrcMgr.OnuIDMgmtLock[ponIntfID].Unlock()
449
salmansiddiqui352a45c2019-08-19 10:15:36 +0000450 if _, ok := RsrcMgr.ResourceMgrs[ponIntfID]; !ok {
451 err := errors.New("invalid-pon-interface-" + strconv.Itoa(int(ponIntfID)))
452 return 0, err
453 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400454 // Get ONU id for a provided pon interface ID.
Matteo Scandolo84585372021-03-18 14:21:22 -0700455 onuID, err := RsrcMgr.ResourceMgrs[ponIntfID].TechProfileMgr.GetResourceID(ctx, ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400456 ponrmgr.ONU_ID, 1)
457 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000458 logger.Errorf(ctx, "Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700459 ponIntfID, ponrmgr.ONU_ID)
cbabuabf02352019-10-15 13:14:56 +0200460 return 0, err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400461 }
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700462 if onuID != nil {
463 RsrcMgr.ResourceMgrs[ponIntfID].InitResourceMap(ctx, fmt.Sprintf("%d,%d", ponIntfID, onuID[0]))
464 return onuID[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400465 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530466
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700467 return 0, err // return OnuID 0 on error
Abhilash S.L7f17e402019-03-15 17:40:41 +0530468}
469
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700470// GetFlowIDInfo returns the slice of flow info of the given pon-port
471// Note: For flows which trap from the NNI and not really associated with any particular
472// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700473func (RsrcMgr *OpenOltResourceMgr) GetFlowIDInfo(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32, flowID uint64) *FlowInfo {
474 var flowInfo FlowInfo
Abhilash S.L8ee90712019-04-29 16:24:22 +0530475
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700476 subs := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
477 Path := fmt.Sprintf(FlowIDInfoPath, subs, flowID)
478 value, err := RsrcMgr.KVStore.Get(ctx, Path)
479 if err == nil {
480 if value != nil {
481 Val, err := toByte(value.Value)
482 if err != nil {
483 logger.Errorw(ctx, "Failed to convert flowinfo into byte array", log.Fields{"error": err, "subs": subs})
484 return nil
485 }
486 if err = json.Unmarshal(Val, &flowInfo); err != nil {
487 logger.Errorw(ctx, "Failed to unmarshal", log.Fields{"error": err, "subs": subs})
488 return nil
489 }
490 }
491 }
492 if flowInfo.Flow == nil {
493 logger.Debugw(ctx, "No flowInfo found in KV store", log.Fields{"subs": subs})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530494 return nil
495 }
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700496 return &flowInfo
Abhilash S.L8ee90712019-04-29 16:24:22 +0530497}
498
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700499// GetCurrentFlowIDsForOnu fetches flow ID from the resource manager
500// Note: For flows which trap from the NNI and not really associated with any particular
501// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700502func (RsrcMgr *OpenOltResourceMgr) GetCurrentFlowIDsForOnu(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32) ([]uint64, error) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700503
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700504 subs := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
505 path := fmt.Sprintf(FlowIDPath, subs)
506
507 var data []uint64
508 value, err := RsrcMgr.KVStore.Get(ctx, path)
509 if err == nil {
510 if value != nil {
511 Val, _ := toByte(value.Value)
512 if err = json.Unmarshal(Val, &data); err != nil {
513 logger.Error(ctx, "Failed to unmarshal")
514 return nil, err
515 }
516 }
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700517 }
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700518 return data, nil
Abhilash S.L8ee90712019-04-29 16:24:22 +0530519}
520
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700521// UpdateFlowIDInfo updates flow info for the given pon interface, onu id, and uni id
522// Note: For flows which trap from the NNI and not really associated with any particular
523// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700524func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDInfo(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32,
525 flowID uint64, flowData FlowInfo) error {
526
527 subs := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
528 path := fmt.Sprintf(FlowIDInfoPath, subs, flowID)
529
530 var value []byte
531 var err error
532 value, err = json.Marshal(flowData)
533 if err != nil {
534 logger.Errorf(ctx, "failed to Marshal, resource path %s", path)
535 return err
536 }
537
538 if err = RsrcMgr.KVStore.Put(ctx, path, value); err != nil {
539 logger.Errorf(ctx, "Failed to update resource %s", path)
540 }
541
542 // Update the flowID list for the ONU
543 if err = RsrcMgr.UpdateFlowIDForOnu(ctx, ponIntfID, onuID, uniID, flowID, true); err != nil {
544 // If the operation fails, try to remove FlowInfo from the KV store
545 _ = RsrcMgr.KVStore.Delete(ctx, path)
546 return err
547 }
548 return err
Abhilash S.L8ee90712019-04-29 16:24:22 +0530549}
550
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700551// UpdateFlowIDForOnu updates the flow_id list of the ONU (add or remove flow_id from the list)
552func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDForOnu(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32, flowID uint64, add bool) error {
553 /*
554 Update the flow_id list of the ONU (add or remove flow_id from the list)
555 :param pon_intf_onu_id: reference of PON interface id and onu id
556 :param flow_id: flow ID
557 :param add: Boolean flag to indicate whether the flow_id should be
558 added or removed from the list. Defaults to adding the flow.
559 */
560 var Value []byte
561 var err error
562 var retVal bool
563 var idx uint64
564 subs := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
565 path := fmt.Sprintf(FlowIDPath, subs)
566 flowIDs, err := RsrcMgr.GetCurrentFlowIDsForOnu(ctx, ponIntfID, onuID, uniID)
567 if err != nil {
568 // Error logged in the called function
569 return err
570 }
571
572 if add {
573 if retVal, _ = checkForFlowIDInList(flowIDs, flowID); retVal {
574 return nil
575 }
576 flowIDs = append(flowIDs, flowID)
577 } else {
578 if retVal, idx = checkForFlowIDInList(flowIDs, flowID); !retVal {
579 return nil
580 }
581 // delete the index and shift
582 flowIDs = append(flowIDs[:idx], flowIDs[idx+1:]...)
583 }
584 Value, err = json.Marshal(flowIDs)
585 if err != nil {
586 logger.Error(ctx, "Failed to Marshal")
587 return err
588 }
589
590 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
591 logger.Errorf(ctx, "Failed to update resource %s", path)
592 return err
593 }
594 return err
595}
596
597// RemoveFlowIDInfo remove flow info for the given pon interface, onu id, and uni id
598// Note: For flows which trap from the NNI and not really associated with any particular
599// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
600func (RsrcMgr *OpenOltResourceMgr) RemoveFlowIDInfo(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32,
601 flowID uint64) error {
602
603 subs := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
604 path := fmt.Sprintf(FlowIDInfoPath, subs, flowID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530605
Girish Gowdru0c588b22019-04-23 23:24:56 -0400606 var err error
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700607 if err = RsrcMgr.KVStore.Delete(ctx, path); err != nil {
608 logger.Errorf(ctx, "Failed to delete resource %s", path)
609 return err
610 }
Girish Gowdrab77ded92020-04-08 11:45:05 -0700611
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700612 // Update the flowID list for the ONU
613 err = RsrcMgr.UpdateFlowIDForOnu(ctx, ponIntfID, onuID, uniID, flowID, false)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700614
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700615 return err
616}
617
618// RemoveAllFlowsForIntfOnuUniKey removes flow info for the given interface, onu id, and uni id
619func (RsrcMgr *OpenOltResourceMgr) RemoveAllFlowsForIntfOnuUniKey(ctx context.Context, intf uint32, onuID int32, uniID int32) error {
620 flowIDs, err := RsrcMgr.GetCurrentFlowIDsForOnu(ctx, intf, onuID, uniID)
621 if err != nil {
622 // error logged in the called function
623 return err
624 }
625 for _, flID := range flowIDs {
626 if err := RsrcMgr.RemoveFlowIDInfo(ctx, intf, onuID, uniID, flID); err != nil {
627 logger.Errorw(ctx, "failed-to-delete-flow-id-info", log.Fields{"intf": intf, "onuID": onuID, "uniID": uniID, "flowID": flID})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530628 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400629 }
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700630 subs := fmt.Sprintf("%d,%d,%d", intf, onuID, uniID)
631 path := fmt.Sprintf(FlowIDPath, subs)
632 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
633 logger.Errorf(ctx, "Failed to delete resource %s", path)
634 return err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400635 }
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700636 return nil
Abhilash S.L7f17e402019-03-15 17:40:41 +0530637}
638
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700639// GetAllocID return the first Alloc ID for a given pon interface id and onu id and then update the resource map on
640// the KV store with the list of alloc_ids allocated for the pon_intf_onu_id tuple
641// Currently of all the alloc_ids available, it returns the first alloc_id in the list for tha given ONU
npujarec5762e2020-01-01 14:08:48 +0530642func (RsrcMgr *OpenOltResourceMgr) GetAllocID(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530643
Girish Gowdru0c588b22019-04-23 23:24:56 -0400644 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700645 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700646
647 RsrcMgr.AllocIDMgmtLock[intfID].Lock()
648 defer RsrcMgr.AllocIDMgmtLock[intfID].Unlock()
649
npujarec5762e2020-01-01 14:08:48 +0530650 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400651 if AllocID != nil {
652 // Since we support only one alloc_id for the ONU at the moment,
653 // return the first alloc_id in the list, if available, for that
654 // ONU.
Neha Sharma96b7bf22020-06-15 10:37:32 +0000655 logger.Debugw(ctx, "Retrieved alloc ID from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400656 return AllocID[0]
657 }
npujarec5762e2020-01-01 14:08:48 +0530658 AllocID, err = RsrcMgr.ResourceMgrs[intfID].GetResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400659 ponrmgr.ALLOC_ID, 1)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530660
Girish Gowdru0c588b22019-04-23 23:24:56 -0400661 if AllocID == nil || err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000662 logger.Error(ctx, "Failed to allocate alloc id")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400663 return 0
664 }
665 // update the resource map on KV store with the list of alloc_id
666 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530667 err = RsrcMgr.ResourceMgrs[intfID].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID, AllocID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400668 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000669 logger.Error(ctx, "Failed to update Alloc ID")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400670 return 0
671 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000672 logger.Debugw(ctx, "Allocated new Tcont from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400673 return AllocID[0]
Abhilash S.L7f17e402019-03-15 17:40:41 +0530674}
675
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700676// UpdateAllocIdsForOnu updates alloc ids in kv store for a given pon interface id, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530677func (RsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ctx context.Context, ponPort uint32, onuID uint32, uniID uint32, allocID []uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530678
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700679 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530680 return RsrcMgr.ResourceMgrs[ponPort].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700681 allocID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530682}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700683
684// GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530685func (RsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(ctx context.Context, intfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700686 uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530687
Girish Gowdru0c588b22019-04-23 23:24:56 -0400688 /* Get gem ports for given pon interface , onu id and uni id. */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530689
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700690 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530691 return RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530692}
693
Gamze Abakafee36392019-10-03 11:17:24 +0000694// GetCurrentAllocIDsForOnu returns alloc ids for given pon interface and onu id
npujarec5762e2020-01-01 14:08:48 +0530695func (RsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDsForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530696
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700697 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530698 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400699 if AllocID != nil {
Gamze Abakafee36392019-10-03 11:17:24 +0000700 return AllocID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400701 }
Gamze Abakafee36392019-10-03 11:17:24 +0000702 return []uint32{}
703}
704
705// RemoveAllocIDForOnu removes the alloc id for given pon interface, onu id, uni id and alloc id
npujarec5762e2020-01-01 14:08:48 +0530706func (RsrcMgr *OpenOltResourceMgr) RemoveAllocIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, allocID uint32) {
707 allocIDs := RsrcMgr.GetCurrentAllocIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000708 for i := 0; i < len(allocIDs); i++ {
709 if allocIDs[i] == allocID {
710 allocIDs = append(allocIDs[:i], allocIDs[i+1:]...)
711 break
712 }
713 }
npujarec5762e2020-01-01 14:08:48 +0530714 err := RsrcMgr.UpdateAllocIdsForOnu(ctx, intfID, onuID, uniID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000715 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000716 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 +0000717 intfID, onuID, uniID, allocID)
718 }
719}
720
721// RemoveGemPortIDForOnu removes the gem port id for given pon interface, onu id, uni id and gem port id
npujarec5762e2020-01-01 14:08:48 +0530722func (RsrcMgr *OpenOltResourceMgr) RemoveGemPortIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, gemPortID uint32) {
723 gemPortIDs := RsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000724 for i := 0; i < len(gemPortIDs); i++ {
725 if gemPortIDs[i] == gemPortID {
726 gemPortIDs = append(gemPortIDs[:i], gemPortIDs[i+1:]...)
727 break
728 }
729 }
npujarec5762e2020-01-01 14:08:48 +0530730 err := RsrcMgr.UpdateGEMPortIDsForOnu(ctx, intfID, onuID, uniID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000731 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000732 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 +0000733 intfID, onuID, uniID, gemPortID)
734 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530735}
736
Esin Karamandf392e12020-12-16 13:33:09 +0000737//GetUniPortByPonPortGemPortFromKVStore retrieves onu and uni ID associated with the pon and gem ports.
738func (RsrcMgr *OpenOltResourceMgr) GetUniPortByPonPortGemPortFromKVStore(ctx context.Context, PonPort uint32, GemPort uint32) (uint32, uint32, error) {
739 IntfGEMPortPath := fmt.Sprintf("%d,%d", PonPort, GemPort)
740 logger.Debugf(ctx, "Getting ONU and UNI IDs from the path %s", IntfGEMPortPath)
741 var Data []uint32
742 Value, err := RsrcMgr.KVStore.Get(ctx, IntfGEMPortPath)
743 if err == nil {
744 if Value != nil {
745 Val, _ := ponrmgr.ToByte(Value.Value)
746 if err = json.Unmarshal(Val, &Data); err != nil {
747 logger.Errorw(ctx, "Failed to unmarshal", log.Fields{"error": err})
748 return 0, 0, errors.New("failed to unmarshal the data retrieved")
749 }
750 }
751 } else {
752 logger.Errorf(ctx, "Failed to get data from kvstore for %s", IntfGEMPortPath, err)
753 return 0, 0, errors.New("could not get data")
754 }
755 if len(Data) < 2 {
756 return 0, 0, errors.New("invalid data format")
757 }
758 return Data[0], Data[1], nil
759}
760
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700761// UpdateGEMportsPonportToOnuMapOnKVStore updates onu and uni id associated with the gem port to the kv store
762// This stored information is used when packet_indication is received and we need to derive the ONU Id for which
763// the packet arrived based on the pon_intf and gemport available in the packet_indication
npujarec5762e2020-01-01 14:08:48 +0530764func (RsrcMgr *OpenOltResourceMgr) UpdateGEMportsPonportToOnuMapOnKVStore(ctx context.Context, gemPorts []uint32, PonPort uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700765 onuID uint32, uniID uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530766
Girish Gowdru0c588b22019-04-23 23:24:56 -0400767 /* Update onu and uni id associated with the gem port to the kv store. */
768 var IntfGEMPortPath string
Esin Karamandf392e12020-12-16 13:33:09 +0000769 Data := []uint32{onuID, uniID}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700770 for _, GEM := range gemPorts {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400771 IntfGEMPortPath = fmt.Sprintf("%d,%d", PonPort, GEM)
772 Val, err := json.Marshal(Data)
773 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000774 logger.Error(ctx, "failed to Marshal")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400775 return err
776 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700777
npujarec5762e2020-01-01 14:08:48 +0530778 if err = RsrcMgr.KVStore.Put(ctx, IntfGEMPortPath, Val); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000779 logger.Errorf(ctx, "Failed to update resource %s", IntfGEMPortPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400780 return err
781 }
782 }
783 return nil
Abhilash S.L7f17e402019-03-15 17:40:41 +0530784}
785
Gamze Abakafee36392019-10-03 11:17:24 +0000786// RemoveGEMportPonportToOnuMapOnKVStore removes the relationship between the gem port and pon port
npujarec5762e2020-01-01 14:08:48 +0530787func (RsrcMgr *OpenOltResourceMgr) RemoveGEMportPonportToOnuMapOnKVStore(ctx context.Context, GemPort uint32, PonPort uint32) {
Gamze Abakafee36392019-10-03 11:17:24 +0000788 IntfGEMPortPath := fmt.Sprintf("%d,%d", PonPort, GemPort)
npujarec5762e2020-01-01 14:08:48 +0530789 err := RsrcMgr.KVStore.Delete(ctx, IntfGEMPortPath)
Gamze Abakafee36392019-10-03 11:17:24 +0000790 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000791 logger.Errorf(ctx, "Failed to Remove Gem port-Pon port to onu map on kv store. Gem %d PonPort %d", GemPort, PonPort)
Gamze Abakafee36392019-10-03 11:17:24 +0000792 }
793}
794
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700795// GetGEMPortID gets gem port id for a particular pon port, onu id and uni id and then update the resource map on
796// the KV store with the list of gemport_id allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530797func (RsrcMgr *OpenOltResourceMgr) GetGEMPortID(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700798 uniID uint32, NumOfPorts uint32) ([]uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530799
Girish Gowdru0c588b22019-04-23 23:24:56 -0400800 /* Get gem port id for a particular pon port, onu id
801 and uni id.
802 */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530803
Girish Gowdru0c588b22019-04-23 23:24:56 -0400804 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700805 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530806
Girish Gowdrab77ded92020-04-08 11:45:05 -0700807 RsrcMgr.GemPortIDMgmtLock[ponPort].Lock()
808 defer RsrcMgr.GemPortIDMgmtLock[ponPort].Unlock()
809
npujarec5762e2020-01-01 14:08:48 +0530810 GEMPortList := RsrcMgr.ResourceMgrs[ponPort].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400811 if GEMPortList != nil {
812 return GEMPortList, nil
813 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530814
npujarec5762e2020-01-01 14:08:48 +0530815 GEMPortList, err = RsrcMgr.ResourceMgrs[ponPort].GetResourceID(ctx, ponPort,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400816 ponrmgr.GEMPORT_ID, NumOfPorts)
817 if err != nil && GEMPortList == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000818 logger.Errorf(ctx, "Failed to get gem port id for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400819 return nil, err
820 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530821
Girish Gowdru0c588b22019-04-23 23:24:56 -0400822 // update the resource map on KV store with the list of gemport_id
823 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530824 err = RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400825 GEMPortList)
826 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000827 logger.Errorf(ctx, "Failed to update GEM ports to kv store for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400828 return nil, err
829 }
npujarec5762e2020-01-01 14:08:48 +0530830 _ = RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(ctx, GEMPortList, ponPort,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700831 onuID, uniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400832 return GEMPortList, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530833}
834
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700835// UpdateGEMPortIDsForOnu updates gemport ids on to the kv store for a given pon port, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530836func (RsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700837 uniID uint32, GEMPortList []uint32) error {
838 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530839 return RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400840 GEMPortList)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530841
842}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700843
844// FreeonuID releases(make free) onu id for a particular pon-port
npujarec5762e2020-01-01 14:08:48 +0530845func (RsrcMgr *OpenOltResourceMgr) FreeonuID(ctx context.Context, intfID uint32, onuID []uint32) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700846
Girish Gowdra38d533d2020-03-30 20:38:51 -0700847 RsrcMgr.OnuIDMgmtLock[intfID].Lock()
Girish Gowdrab77ded92020-04-08 11:45:05 -0700848 defer RsrcMgr.OnuIDMgmtLock[intfID].Unlock()
849
Matteo Scandolo84585372021-03-18 14:21:22 -0700850 if err := RsrcMgr.ResourceMgrs[intfID].TechProfileMgr.FreeResourceID(ctx, intfID, ponrmgr.ONU_ID, onuID); err != nil {
851 logger.Errorw(ctx, "error-while-freeing-onu-id", log.Fields{
852 "intf-id": intfID,
853 "onu-id": onuID,
854 "err": err.Error(),
855 })
856 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530857
Girish Gowdru0c588b22019-04-23 23:24:56 -0400858 /* Free onu id for a particular interface.*/
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700859 var IntfonuID string
860 for _, onu := range onuID {
861 IntfonuID = fmt.Sprintf("%d,%d", intfID, onu)
npujarec5762e2020-01-01 14:08:48 +0530862 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfonuID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400863 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530864}
865
Gamze Abakafee36392019-10-03 11:17:24 +0000866// FreeAllocID frees AllocID on the PON resource pool and also frees the allocID association
867// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530868func (RsrcMgr *OpenOltResourceMgr) FreeAllocID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000869 uniID uint32, allocID uint32) {
Girish Gowdrab77ded92020-04-08 11:45:05 -0700870 RsrcMgr.AllocIDMgmtLock[IntfID].Lock()
871 defer RsrcMgr.AllocIDMgmtLock[IntfID].Unlock()
872
npujarec5762e2020-01-01 14:08:48 +0530873 RsrcMgr.RemoveAllocIDForOnu(ctx, IntfID, onuID, uniID, allocID)
Gamze Abakafee36392019-10-03 11:17:24 +0000874 allocIDs := make([]uint32, 0)
875 allocIDs = append(allocIDs, allocID)
Matteo Scandolo84585372021-03-18 14:21:22 -0700876 if err := RsrcMgr.ResourceMgrs[IntfID].TechProfileMgr.FreeResourceID(ctx, IntfID, ponrmgr.ALLOC_ID, allocIDs); err != nil {
877 logger.Errorw(ctx, "error-while-freeing-alloc-id", log.Fields{
878 "intf-id": IntfID,
879 "onu-id": onuID,
880 "err": err.Error(),
881 })
882 }
Gamze Abakafee36392019-10-03 11:17:24 +0000883}
884
885// FreeGemPortID frees GemPortID on the PON resource pool and also frees the gemPortID association
886// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530887func (RsrcMgr *OpenOltResourceMgr) FreeGemPortID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000888 uniID uint32, gemPortID uint32) {
Girish Gowdrab77ded92020-04-08 11:45:05 -0700889 RsrcMgr.GemPortIDMgmtLock[IntfID].Lock()
890 defer RsrcMgr.GemPortIDMgmtLock[IntfID].Unlock()
891
npujarec5762e2020-01-01 14:08:48 +0530892 RsrcMgr.RemoveGemPortIDForOnu(ctx, IntfID, onuID, uniID, gemPortID)
Gamze Abakafee36392019-10-03 11:17:24 +0000893 gemPortIDs := make([]uint32, 0)
894 gemPortIDs = append(gemPortIDs, gemPortID)
Matteo Scandolo84585372021-03-18 14:21:22 -0700895 if err := RsrcMgr.ResourceMgrs[IntfID].TechProfileMgr.FreeResourceID(ctx, IntfID, ponrmgr.GEMPORT_ID, gemPortIDs); err != nil {
896 logger.Errorw(ctx, "error-while-freeing-gem-port-id", log.Fields{
897 "intf-id": IntfID,
898 "onu-id": onuID,
899 "err": err.Error(),
900 })
901 }
Gamze Abakafee36392019-10-03 11:17:24 +0000902}
903
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700904// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id, and the clears the
905// resource map and the onuID associated with (pon_intf_id, gemport_id) tuple,
npujarec5762e2020-01-01 14:08:48 +0530906func (RsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530907
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700908 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530909
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700910 RsrcMgr.AllocIDMgmtLock[intfID].Lock()
Girish Gowdrab77ded92020-04-08 11:45:05 -0700911 AllocIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700912
Matteo Scandolo84585372021-03-18 14:21:22 -0700913 if err := RsrcMgr.ResourceMgrs[intfID].TechProfileMgr.FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400914 ponrmgr.ALLOC_ID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700915 AllocIDs); err != nil {
916 logger.Errorw(ctx, "error-while-freeing-all-alloc-ids-for-onu", log.Fields{
917 "intf-id": intfID,
918 "onu-id": onuID,
919 "err": err.Error(),
920 })
921 }
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700922 RsrcMgr.AllocIDMgmtLock[intfID].Unlock()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530923
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700924 RsrcMgr.GemPortIDMgmtLock[intfID].Lock()
npujarec5762e2020-01-01 14:08:48 +0530925 GEMPortIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Matteo Scandolo84585372021-03-18 14:21:22 -0700926 if err := RsrcMgr.ResourceMgrs[intfID].TechProfileMgr.FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400927 ponrmgr.GEMPORT_ID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700928 GEMPortIDs); err != nil {
929 logger.Errorw(ctx, "error-while-freeing-all-gem-port-ids-for-onu", log.Fields{
930 "intf-id": intfID,
931 "onu-id": onuID,
932 "err": err.Error(),
933 })
934 }
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700935 RsrcMgr.GemPortIDMgmtLock[intfID].Unlock()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530936
Girish Gowdru0c588b22019-04-23 23:24:56 -0400937 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
npujarec5762e2020-01-01 14:08:48 +0530938 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400939 // Clear the ONU Id associated with the (pon_intf_id, gemport_id) tuple.
940 for _, GEM := range GEMPortIDs {
npujarec5762e2020-01-01 14:08:48 +0530941 _ = RsrcMgr.KVStore.Delete(ctx, fmt.Sprintf("%d,%d", intfID, GEM))
Girish Gowdru0c588b22019-04-23 23:24:56 -0400942 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530943}
944
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700945// IsFlowOnKvStore checks if the given flowID is present on the kv store
946// Returns true if the flowID is found, otherwise it returns false
947func (RsrcMgr *OpenOltResourceMgr) IsFlowOnKvStore(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32,
948 flowID uint64) bool {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530949
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700950 FlowIDs, err := RsrcMgr.GetCurrentFlowIDsForOnu(ctx, ponIntfID, onuID, uniID)
951 if err != nil {
952 // error logged in the called function
953 return false
954 }
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400955 if FlowIDs != nil {
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700956 logger.Debugw(ctx, "Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "onuID": onuID, "uniID": uniID})
957 for _, id := range FlowIDs {
958 if flowID == id {
959 return true
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400960 }
961 }
962 }
963 return false
964}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400965
salmansiddiqui7ac62132019-08-22 03:58:50 +0000966// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000967// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530968func (RsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) []uint32 {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000969 Path := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000970 var Data []uint32
npujarec5762e2020-01-01 14:08:48 +0530971 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400972 if err == nil {
973 if Value != nil {
974 Val, err := kvstore.ToByte(Value.Value)
975 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000976 logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"error": err})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400977 return Data
978 }
979 if err = json.Unmarshal(Val, &Data); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000980 logger.Error(ctx, "Failed to unmarshal", log.Fields{"error": err})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400981 return Data
982 }
983 }
984 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000985 logger.Errorf(ctx, "Failed to get TP id from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400986 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000987 logger.Debugf(ctx, "Getting TP id %d from path %s", Data, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400988 return Data
989
990}
991
Gamze Abakafee36392019-10-03 11:17:24 +0000992// RemoveTechProfileIDsForOnu deletes all tech profile ids from the KV-Store for the given onu based on the path
993// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530994func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDsForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) error {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000995 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
npujarec5762e2020-01-01 14:08:48 +0530996 if err := RsrcMgr.KVStore.Delete(ctx, IntfOnuUniID); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000997 logger.Errorw(ctx, "Failed to delete techprofile id resource in KV store", log.Fields{"path": IntfOnuUniID})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400998 return err
999 }
1000 return nil
1001}
1002
Gamze Abakafee36392019-10-03 11:17:24 +00001003// RemoveTechProfileIDForOnu deletes a specific tech profile id from the KV-Store for the given onu based on the path
1004// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +05301005func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32, TpID uint32) error {
1006 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +00001007 for i, tpIDInList := range tpIDList {
1008 if tpIDInList == TpID {
1009 tpIDList = append(tpIDList[:i], tpIDList[i+1:]...)
1010 }
1011 }
1012 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
1013 Value, err := json.Marshal(tpIDList)
1014 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001015 logger.Error(ctx, "failed to Marshal")
Gamze Abakafee36392019-10-03 11:17:24 +00001016 return err
1017 }
npujarec5762e2020-01-01 14:08:48 +05301018 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001019 logger.Errorf(ctx, "Failed to update resource %s", IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +00001020 return err
1021 }
1022 return err
1023}
1024
1025// UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
1026// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +05301027func (RsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32,
salmansiddiqui7ac62132019-08-22 03:58:50 +00001028 UniID uint32, TpID uint32) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -04001029 var Value []byte
1030 var err error
1031
salmansiddiqui7ac62132019-08-22 03:58:50 +00001032 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +00001033
npujarec5762e2020-01-01 14:08:48 +05301034 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +00001035 for _, value := range tpIDList {
1036 if value == TpID {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001037 logger.Debugf(ctx, "TpID %d is already in tpIdList for the path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +00001038 return err
1039 }
1040 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001041 logger.Debugf(ctx, "updating tp id %d on path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +00001042 tpIDList = append(tpIDList, TpID)
1043 Value, err = json.Marshal(tpIDList)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001044 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001045 logger.Error(ctx, "failed to Marshal")
Manikkaraj kb1d51442019-07-23 10:41:02 -04001046 return err
1047 }
npujarec5762e2020-01-01 14:08:48 +05301048 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001049 logger.Errorf(ctx, "Failed to update resource %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001050 return err
1051 }
1052 return err
1053}
1054
Girish Gowdra3fe4cc52021-03-24 23:04:19 -07001055// StoreMeterInfoForOnu updates the meter id in the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +00001056// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
Girish Gowdra3fe4cc52021-03-24 23:04:19 -07001057func (RsrcMgr *OpenOltResourceMgr) StoreMeterInfoForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
1058 UniID uint32, TpID uint32, meterInfo *MeterInfo) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -04001059 var Value []byte
1060 var err error
Gamze Abakafee36392019-10-03 11:17:24 +00001061 IntfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Girish Gowdra3fe4cc52021-03-24 23:04:19 -07001062 Value, err = json.Marshal(*meterInfo)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001063 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001064 logger.Error(ctx, "failed to Marshal meter config")
Manikkaraj kb1d51442019-07-23 10:41:02 -04001065 return err
1066 }
npujarec5762e2020-01-01 14:08:48 +05301067 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001068 logger.Errorf(ctx, "Failed to store meter into KV store %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001069 return err
1070 }
Girish Gowdra3fe4cc52021-03-24 23:04:19 -07001071 logger.Debugw(ctx, "meter info updated successfully", log.Fields{"path": IntfOnuUniID, "meter-info": meterInfo})
Manikkaraj kb1d51442019-07-23 10:41:02 -04001072 return err
1073}
1074
Girish Gowdra3fe4cc52021-03-24 23:04:19 -07001075// GetMeterInfoForOnu fetches the meter id from the kv store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +00001076// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
Girish Gowdra3fe4cc52021-03-24 23:04:19 -07001077func (RsrcMgr *OpenOltResourceMgr) GetMeterInfoForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
1078 UniID uint32, TpID uint32) (*MeterInfo, error) {
Gamze Abakafee36392019-10-03 11:17:24 +00001079 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Girish Gowdra3fe4cc52021-03-24 23:04:19 -07001080 var meterInfo MeterInfo
npujarec5762e2020-01-01 14:08:48 +05301081 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001082 if err == nil {
1083 if Value != nil {
Girish Gowdra3fe4cc52021-03-24 23:04:19 -07001084 logger.Debug(ctx, "Found meter info in KV store", log.Fields{"Direction": Direction})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001085 Val, er := kvstore.ToByte(Value.Value)
1086 if er != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001087 logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"error": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001088 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -04001089 }
Girish Gowdra3fe4cc52021-03-24 23:04:19 -07001090 if er = json.Unmarshal(Val, &meterInfo); er != nil {
1091 logger.Error(ctx, "Failed to unmarshal meter info", log.Fields{"error": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001092 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -04001093 }
1094 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001095 logger.Debug(ctx, "meter-does-not-exists-in-KVStore")
Manikkaraj kb1d51442019-07-23 10:41:02 -04001096 return nil, err
1097 }
1098 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001099 logger.Errorf(ctx, "Failed to get Meter config from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001100
1101 }
Girish Gowdra3fe4cc52021-03-24 23:04:19 -07001102 return &meterInfo, err
Manikkaraj kb1d51442019-07-23 10:41:02 -04001103}
1104
Girish Gowdra3fe4cc52021-03-24 23:04:19 -07001105// HandleMeterInfoRefCntUpdate increments or decrements the reference counter for a given meter.
1106// When reference count becomes 0, it clears the meter information from the kv store
1107func (RsrcMgr *OpenOltResourceMgr) HandleMeterInfoRefCntUpdate(ctx context.Context, Direction string,
1108 IntfID uint32, OnuID uint32, UniID uint32, TpID uint32, increment bool) error {
1109 meterInfo, err := RsrcMgr.GetMeterInfoForOnu(ctx, Direction, IntfID, OnuID, UniID, TpID)
Girish Gowdra01e8d972021-03-26 16:22:02 -07001110 if err != nil {
1111 return err
1112 } else if meterInfo == nil {
1113 // If we are increasing the reference count, we expect the meter information to be present on KV store.
1114 // But if decrementing the reference count, the meter is possibly already cleared from KV store. Just log warn but do not return error.
1115 if increment {
1116 logger.Errorf(ctx, "error-fetching-meter-info-for-intf-%d-onu-%d-uni-%d-tp-id-%d-direction-%s", IntfID, OnuID, UniID, TpID, Direction)
1117 return fmt.Errorf("error-fetching-meter-info-for-intf-%d-onu-%d-uni-%d-tp-id-%d-direction-%s", IntfID, OnuID, UniID, TpID, Direction)
1118 }
1119 logger.Warnw(ctx, "meter is already cleared",
1120 log.Fields{"intfID": IntfID, "onuID": OnuID, "uniID": UniID, "direction": Direction, "increment": increment})
1121 return nil
Girish Gowdra3fe4cc52021-03-24 23:04:19 -07001122 }
Girish Gowdra01e8d972021-03-26 16:22:02 -07001123
Girish Gowdra3fe4cc52021-03-24 23:04:19 -07001124 if increment {
1125 meterInfo.RefCnt++
1126 } else {
1127 meterInfo.RefCnt--
Girish Gowdra01e8d972021-03-26 16:22:02 -07001128 // If RefCnt become 0 clear the meter information from the DB.
Girish Gowdra3fe4cc52021-03-24 23:04:19 -07001129 if meterInfo.RefCnt == 0 {
1130 if err := RsrcMgr.RemoveMeterInfoForOnu(ctx, Direction, IntfID, OnuID, UniID, TpID); err != nil {
1131 return err
1132 }
1133 return nil
1134 }
1135 }
1136 if err := RsrcMgr.StoreMeterInfoForOnu(ctx, Direction, IntfID, OnuID, UniID, TpID, meterInfo); err != nil {
1137 return err
1138 }
1139 return nil
1140}
1141
1142// RemoveMeterInfoForOnu deletes the meter id from the kV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +00001143// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
Girish Gowdra3fe4cc52021-03-24 23:04:19 -07001144func (RsrcMgr *OpenOltResourceMgr) RemoveMeterInfoForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +00001145 UniID uint32, TpID uint32) error {
1146 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
npujarec5762e2020-01-01 14:08:48 +05301147 if err := RsrcMgr.KVStore.Delete(ctx, Path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001148 logger.Errorf(ctx, "Failed to delete meter id %s from kvstore ", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001149 return err
1150 }
1151 return nil
1152}
salmansiddiqui7ac62132019-08-22 03:58:50 +00001153
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301154//AddGemToOnuGemInfo adds gemport to onugem info kvstore
npujarec5762e2020-01-01 14:08:48 +05301155func (RsrcMgr *OpenOltResourceMgr) AddGemToOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, gemPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301156 var onuGemData []OnuGemInfo
1157 var err error
1158
npujarec5762e2020-01-01 14:08:48 +05301159 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001160 logger.Errorf(ctx, "failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301161 return err
1162 }
1163 if len(onuGemData) == 0 {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001164 logger.Errorw(ctx, "failed to ger Onuid info ", log.Fields{"intfid": intfID, "onuid": onuID})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301165 return err
1166 }
1167
1168 for idx, onugem := range onuGemData {
1169 if onugem.OnuID == onuID {
1170 for _, gem := range onuGemData[idx].GemPorts {
1171 if gem == gemPort {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001172 logger.Debugw(ctx, "Gem already present in onugem info, skpping addition", log.Fields{"gem": gem})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301173 return nil
1174 }
1175 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001176 logger.Debugw(ctx, "Added gem to onugem info", log.Fields{"gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301177 onuGemData[idx].GemPorts = append(onuGemData[idx].GemPorts, gemPort)
1178 break
1179 }
1180 }
npujarec5762e2020-01-01 14:08:48 +05301181 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301182 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001183 logger.Error(ctx, "Failed to add onugem to kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301184 return err
1185 }
1186 return err
1187}
1188
1189//GetOnuGemInfo gets onu gem info from the kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301190func (RsrcMgr *OpenOltResourceMgr) GetOnuGemInfo(ctx context.Context, IntfID uint32) ([]OnuGemInfo, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301191 var onuGemData []OnuGemInfo
1192
npujarec5762e2020-01-01 14:08:48 +05301193 if err := RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001194 logger.Errorf(ctx, "failed to get onuifo for intfid %d", IntfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301195 return nil, err
1196 }
1197
1198 return onuGemData, nil
1199}
1200
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001201// AddOnuGemInfo adds onu info on to the kvstore per interface
1202func (RsrcMgr *OpenOltResourceMgr) AddOnuGemInfo(ctx context.Context, IntfID uint32, onuGem OnuGemInfo) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301203 var onuGemData []OnuGemInfo
1204 var err error
1205
npujarec5762e2020-01-01 14:08:48 +05301206 if err = RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001207 logger.Errorf(ctx, "failed to get onuifo for intfid %d", IntfID)
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001208 return olterrors.NewErrPersistence("get", "OnuGemInfo", uint64(IntfID),
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001209 log.Fields{"onuGem": onuGem, "intfID": IntfID}, err)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301210 }
1211 onuGemData = append(onuGemData, onuGem)
npujarec5762e2020-01-01 14:08:48 +05301212 err = RsrcMgr.ResourceMgrs[IntfID].AddOnuGemInfo(ctx, IntfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301213 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001214 logger.Error(ctx, "Failed to add onugem to kv store")
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001215 return olterrors.NewErrPersistence("set", "OnuGemInfo", uint64(IntfID),
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001216 log.Fields{"onuGemData": onuGemData, "intfID": IntfID}, err)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301217 }
1218
Neha Sharma96b7bf22020-06-15 10:37:32 +00001219 logger.Debugw(ctx, "added onu to onugeminfo", log.Fields{"intf": IntfID, "onugem": onuGem})
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001220 return nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301221}
1222
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301223// AddUniPortToOnuInfo adds uni port to the onuinfo kvstore. check if the uni is already present if not update the kv store.
npujarec5762e2020-01-01 14:08:48 +05301224func (RsrcMgr *OpenOltResourceMgr) AddUniPortToOnuInfo(ctx context.Context, intfID uint32, onuID uint32, portNo uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301225 var onuGemData []OnuGemInfo
1226 var err error
1227
npujarec5762e2020-01-01 14:08:48 +05301228 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001229 logger.Errorf(ctx, "failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301230 return
1231 }
1232 for idx, onu := range onuGemData {
1233 if onu.OnuID == onuID {
1234 for _, uni := range onu.UniPorts {
1235 if uni == portNo {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001236 logger.Debugw(ctx, "uni already present in onugem info", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301237 return
1238 }
1239 }
1240 onuGemData[idx].UniPorts = append(onuGemData[idx].UniPorts, portNo)
1241 break
1242 }
1243 }
npujarec5762e2020-01-01 14:08:48 +05301244 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301245 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001246 logger.Errorw(ctx, "Failed to add uin port in onugem to kv store", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301247 return
1248 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301249}
1250
Esin Karaman7fb80c22020-07-16 14:23:33 +00001251//UpdateGemPortForPktIn updates gemport for pkt in path to kvstore, path being intfid, onuid, portno, vlan id, priority bit
npujarec5762e2020-01-01 14:08:48 +05301252func (RsrcMgr *OpenOltResourceMgr) UpdateGemPortForPktIn(ctx context.Context, pktIn PacketInInfoKey, gemPort uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301253
Esin Karaman7fb80c22020-07-16 14:23:33 +00001254 path := fmt.Sprintf(OnuPacketINPath, pktIn.IntfID, pktIn.OnuID, pktIn.LogicalPort, pktIn.VlanID, pktIn.Priority)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301255 Value, err := json.Marshal(gemPort)
1256 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001257 logger.Error(ctx, "Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301258 return
1259 }
npujarec5762e2020-01-01 14:08:48 +05301260 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001261 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"path": path, "value": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301262 return
1263 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001264 logger.Debugw(ctx, "added gem packet in successfully", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301265}
1266
Esin Karaman7fb80c22020-07-16 14:23:33 +00001267// GetGemPortFromOnuPktIn gets the gem port from onu pkt in path, path being intfid, onuid, portno, vlan id, priority bit
1268func (RsrcMgr *OpenOltResourceMgr) GetGemPortFromOnuPktIn(ctx context.Context, packetInInfoKey PacketInInfoKey) (uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301269
1270 var Val []byte
1271 var gemPort uint32
1272
Esin Karaman7fb80c22020-07-16 14:23:33 +00001273 path := fmt.Sprintf(OnuPacketINPath, packetInInfoKey.IntfID, packetInInfoKey.OnuID, packetInInfoKey.LogicalPort,
1274 packetInInfoKey.VlanID, packetInInfoKey.Priority)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301275
npujarec5762e2020-01-01 14:08:48 +05301276 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301277 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001278 logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301279 return uint32(0), err
1280 } else if value == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001281 logger.Debugw(ctx, "No pkt in gem found", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301282 return uint32(0), nil
1283 }
1284
1285 if Val, err = kvstore.ToByte(value.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001286 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301287 return uint32(0), err
1288 }
1289 if err = json.Unmarshal(Val, &gemPort); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001290 logger.Error(ctx, "Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301291 return uint32(0), err
1292 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001293 logger.Debugw(ctx, "found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301294
1295 return gemPort, nil
1296}
1297
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001298//DeletePacketInGemPortForOnu deletes the packet-in gemport for ONU
1299func (RsrcMgr *OpenOltResourceMgr) DeletePacketInGemPortForOnu(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301300
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001301 path := fmt.Sprintf(OnuPacketINPathPrefix, intfID, onuID, logicalPort)
1302 value, err := RsrcMgr.KVStore.List(ctx, path)
Esin Karaman7fb80c22020-07-16 14:23:33 +00001303 if err != nil {
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001304 logger.Errorf(ctx, "failed-to-read-value-from-path-%s", path)
1305 return errors.New("failed-to-read-value-from-path-" + path)
Esin Karaman7fb80c22020-07-16 14:23:33 +00001306 }
Esin Karaman7fb80c22020-07-16 14:23:33 +00001307
1308 //remove them one by one
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001309 for key := range value {
1310 // Formulate the right key path suffix ti be delete
Matteo Scandolodfa7a972020-11-06 13:03:40 -08001311 stringToBeReplaced := fmt.Sprintf(BasePathKvStore, RsrcMgr.KVStore.PathPrefix, RsrcMgr.DeviceID) + "/"
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001312 replacedWith := ""
1313 key = strings.Replace(key, stringToBeReplaced, replacedWith, 1)
1314
1315 logger.Debugf(ctx, "removing-key-%s", key)
Esin Karaman7fb80c22020-07-16 14:23:33 +00001316 if err := RsrcMgr.KVStore.Delete(ctx, key); err != nil {
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001317 logger.Errorf(ctx, "failed-to-remove-resource-%s", key)
Esin Karaman7fb80c22020-07-16 14:23:33 +00001318 return err
1319 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301320 }
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001321
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301322 return nil
1323}
1324
1325// DelOnuGemInfoForIntf deletes the onugem info from kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301326func (RsrcMgr *OpenOltResourceMgr) DelOnuGemInfoForIntf(ctx context.Context, intfID uint32) error {
1327 if err := RsrcMgr.ResourceMgrs[intfID].DelOnuGemInfoForIntf(ctx, intfID); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001328 logger.Errorw(ctx, "failed to delete onu gem info for", log.Fields{"intfid": intfID})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301329 return err
1330 }
1331 return nil
1332}
1333
1334//GetNNIFromKVStore gets NNi intfids from kvstore. path being per device
npujarec5762e2020-01-01 14:08:48 +05301335func (RsrcMgr *OpenOltResourceMgr) GetNNIFromKVStore(ctx context.Context) ([]uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301336
1337 var nni []uint32
1338 var Val []byte
1339
Kent Hagermane6ff1012020-07-14 15:07:53 -04001340 path := NnniIntfID
npujarec5762e2020-01-01 14:08:48 +05301341 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301342 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001343 logger.Error(ctx, "failed to get data from kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301344 return nil, err
1345 }
1346 if value != nil {
1347 if Val, err = kvstore.ToByte(value.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001348 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301349 return nil, err
1350 }
1351 if err = json.Unmarshal(Val, &nni); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001352 logger.Error(ctx, "Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301353 return nil, err
1354 }
1355 }
1356 return nni, err
1357}
1358
1359// AddNNIToKVStore adds Nni interfaces to kvstore, path being per device.
npujarec5762e2020-01-01 14:08:48 +05301360func (RsrcMgr *OpenOltResourceMgr) AddNNIToKVStore(ctx context.Context, nniIntf uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301361 var Value []byte
1362
npujarec5762e2020-01-01 14:08:48 +05301363 nni, err := RsrcMgr.GetNNIFromKVStore(ctx)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301364 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001365 logger.Error(ctx, "failed to fetch nni interfaces from kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301366 return err
1367 }
1368
Kent Hagermane6ff1012020-07-14 15:07:53 -04001369 path := NnniIntfID
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301370 nni = append(nni, nniIntf)
1371 Value, err = json.Marshal(nni)
1372 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001373 logger.Error(ctx, "Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301374 }
npujarec5762e2020-01-01 14:08:48 +05301375 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001376 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"path": path, "value": Value})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301377 return err
1378 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001379 logger.Debugw(ctx, "added nni to kv successfully", log.Fields{"path": path, "nni": nniIntf})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301380 return nil
1381}
1382
1383// DelNNiFromKVStore deletes nni interface list from kv store.
npujarec5762e2020-01-01 14:08:48 +05301384func (RsrcMgr *OpenOltResourceMgr) DelNNiFromKVStore(ctx context.Context) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301385
Kent Hagermane6ff1012020-07-14 15:07:53 -04001386 path := NnniIntfID
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301387
npujarec5762e2020-01-01 14:08:48 +05301388 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001389 logger.Errorw(ctx, "Failed to delete nni interfaces from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301390 return err
1391 }
1392 return nil
1393}
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301394
1395//UpdateFlowIDsForGem updates flow id per gemport
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001396func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDsForGem(ctx context.Context, intf uint32, gem uint32, flowIDs []uint64) error {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301397 var val []byte
1398 path := fmt.Sprintf(FlowIDsForGem, intf)
1399
npujarec5762e2020-01-01 14:08:48 +05301400 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301401 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001402 logger.Error(ctx, "Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301403 return err
1404 }
1405 if flowsForGem == nil {
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001406 flowsForGem = make(map[uint32][]uint64)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301407 }
1408 flowsForGem[gem] = flowIDs
1409 val, err = json.Marshal(flowsForGem)
1410 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001411 logger.Error(ctx, "Failed to marshal data", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301412 return err
1413 }
Girish Gowdrab77ded92020-04-08 11:45:05 -07001414
npujarec5762e2020-01-01 14:08:48 +05301415 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001416 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301417 return err
1418 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001419 logger.Debugw(ctx, "added flowid list for gem to kv successfully", log.Fields{"path": path, "flowidlist": flowsForGem[gem]})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301420 return nil
1421}
1422
1423//DeleteFlowIDsForGem deletes the flowID list entry per gem from kvstore.
npujarec5762e2020-01-01 14:08:48 +05301424func (RsrcMgr *OpenOltResourceMgr) DeleteFlowIDsForGem(ctx context.Context, intf uint32, gem uint32) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301425 path := fmt.Sprintf(FlowIDsForGem, intf)
1426 var val []byte
1427
npujarec5762e2020-01-01 14:08:48 +05301428 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301429 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001430 logger.Error(ctx, "Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301431 return
1432 }
1433 if flowsForGem == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001434 logger.Error(ctx, "No flowids found ", log.Fields{"intf": intf, "gemport": gem})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301435 return
1436 }
1437 // once we get the flows per gem map from kv , just delete the gem entry from the map
1438 delete(flowsForGem, gem)
1439 // once gem entry is deleted update the kv store.
1440 val, err = json.Marshal(flowsForGem)
1441 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001442 logger.Error(ctx, "Failed to marshal data", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301443 return
1444 }
Girish Gowdra38d533d2020-03-30 20:38:51 -07001445
npujarec5762e2020-01-01 14:08:48 +05301446 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001447 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301448 }
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301449}
1450
1451//GetFlowIDsGemMapForInterface gets flowids per gemport and interface
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001452func (RsrcMgr *OpenOltResourceMgr) GetFlowIDsGemMapForInterface(ctx context.Context, intf uint32) (map[uint32][]uint64, error) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301453 path := fmt.Sprintf(FlowIDsForGem, intf)
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001454 var flowsForGem map[uint32][]uint64
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301455 var val []byte
npujarec5762e2020-01-01 14:08:48 +05301456 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301457 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001458 logger.Error(ctx, "failed to get data from kv store")
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301459 return nil, err
1460 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001461 if value != nil && value.Value != nil {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301462 if val, err = kvstore.ToByte(value.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001463 logger.Error(ctx, "Failed to convert to byte array ", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301464 return nil, err
1465 }
1466 if err = json.Unmarshal(val, &flowsForGem); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001467 logger.Error(ctx, "Failed to unmarshall", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301468 return nil, err
1469 }
1470 }
1471 return flowsForGem, nil
1472}
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301473
1474//DeleteIntfIDGempMapPath deletes the intf id path used to store flow ids per gem to kvstore.
npujarec5762e2020-01-01 14:08:48 +05301475func (RsrcMgr *OpenOltResourceMgr) DeleteIntfIDGempMapPath(ctx context.Context, intf uint32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301476 path := fmt.Sprintf(FlowIDsForGem, intf)
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001477
npujarec5762e2020-01-01 14:08:48 +05301478 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001479 logger.Errorw(ctx, "Failed to delete nni interfaces from kv store", log.Fields{"path": path})
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301480 }
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301481}
1482
1483// RemoveResourceMap Clear resource map associated with (intfid, onuid, uniid) tuple.
npujarec5762e2020-01-01 14:08:48 +05301484func (RsrcMgr *OpenOltResourceMgr) RemoveResourceMap(ctx context.Context, intfID uint32, onuID int32, uniID int32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301485 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +05301486 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301487}
Esin Karamanccb714b2019-11-29 15:02:06 +00001488
1489//GetMcastQueuePerInterfaceMap gets multicast queue info per pon interface
npujarec5762e2020-01-01 14:08:48 +05301490func (RsrcMgr *OpenOltResourceMgr) GetMcastQueuePerInterfaceMap(ctx context.Context) (map[uint32][]uint32, error) {
Kent Hagermane6ff1012020-07-14 15:07:53 -04001491 path := McastQueuesForIntf
Esin Karamanccb714b2019-11-29 15:02:06 +00001492 var mcastQueueToIntfMap map[uint32][]uint32
1493 var val []byte
1494
npujarec5762e2020-01-01 14:08:48 +05301495 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001496 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001497 logger.Error(ctx, "failed to get data from kv store")
Esin Karamanccb714b2019-11-29 15:02:06 +00001498 return nil, err
1499 }
1500 if kvPair != nil && kvPair.Value != nil {
1501 if val, err = kvstore.ToByte(kvPair.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001502 logger.Error(ctx, "Failed to convert to byte array ", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001503 return nil, err
1504 }
1505 if err = json.Unmarshal(val, &mcastQueueToIntfMap); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001506 logger.Error(ctx, "Failed to unmarshall ", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001507 return nil, err
1508 }
1509 }
1510 return mcastQueueToIntfMap, nil
1511}
1512
1513//AddMcastQueueForIntf adds multicast queue for pon interface
npujarec5762e2020-01-01 14:08:48 +05301514func (RsrcMgr *OpenOltResourceMgr) AddMcastQueueForIntf(ctx context.Context, intf uint32, gem uint32, servicePriority uint32) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001515 var val []byte
Kent Hagermane6ff1012020-07-14 15:07:53 -04001516 path := McastQueuesForIntf
Esin Karamanccb714b2019-11-29 15:02:06 +00001517
npujarec5762e2020-01-01 14:08:48 +05301518 mcastQueues, err := RsrcMgr.GetMcastQueuePerInterfaceMap(ctx)
Esin Karamanccb714b2019-11-29 15:02:06 +00001519 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001520 logger.Errorw(ctx, "Failed to get multicast queue info for interface", log.Fields{"error": err, "intf": intf})
Esin Karamanccb714b2019-11-29 15:02:06 +00001521 return err
1522 }
1523 if mcastQueues == nil {
1524 mcastQueues = make(map[uint32][]uint32)
1525 }
1526 mcastQueues[intf] = []uint32{gem, servicePriority}
1527 if val, err = json.Marshal(mcastQueues); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001528 logger.Errorw(ctx, "Failed to marshal data", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001529 return err
1530 }
npujarec5762e2020-01-01 14:08:48 +05301531 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001532 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Esin Karamanccb714b2019-11-29 15:02:06 +00001533 return err
1534 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001535 logger.Debugw(ctx, "added multicast queue info to KV store successfully", log.Fields{"path": path, "mcastQueueInfo": mcastQueues[intf], "interfaceId": intf})
Esin Karamanccb714b2019-11-29 15:02:06 +00001536 return nil
1537}
1538
1539//AddFlowGroupToKVStore adds flow group into KV store
npujarec5762e2020-01-01 14:08:48 +05301540func (RsrcMgr *OpenOltResourceMgr) AddFlowGroupToKVStore(ctx context.Context, groupEntry *ofp.OfpGroupEntry, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001541 var Value []byte
1542 var err error
1543 var path string
1544 if cached {
1545 path = fmt.Sprintf(FlowGroupCached, groupEntry.Desc.GroupId)
1546 } else {
1547 path = fmt.Sprintf(FlowGroup, groupEntry.Desc.GroupId)
1548 }
1549 //build group info object
1550 var outPorts []uint32
1551 for _, ofBucket := range groupEntry.Desc.Buckets {
1552 for _, ofAction := range ofBucket.Actions {
1553 if ofAction.Type == ofp.OfpActionType_OFPAT_OUTPUT {
1554 outPorts = append(outPorts, ofAction.GetOutput().Port)
1555 }
1556 }
1557 }
1558 groupInfo := GroupInfo{
1559 GroupID: groupEntry.Desc.GroupId,
1560 OutPorts: outPorts,
1561 }
1562
1563 Value, err = json.Marshal(groupInfo)
1564
1565 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001566 logger.Error(ctx, "failed to Marshal flow group object")
Esin Karamanccb714b2019-11-29 15:02:06 +00001567 return err
1568 }
1569
npujarec5762e2020-01-01 14:08:48 +05301570 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001571 logger.Errorf(ctx, "Failed to update resource %s", path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001572 return err
1573 }
1574 return nil
1575}
1576
1577//RemoveFlowGroupFromKVStore removes flow group from KV store
Esin Karamand519bbf2020-07-01 11:16:03 +00001578func (RsrcMgr *OpenOltResourceMgr) RemoveFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001579 var path string
1580 if cached {
1581 path = fmt.Sprintf(FlowGroupCached, groupID)
1582 } else {
1583 path = fmt.Sprintf(FlowGroup, groupID)
1584 }
npujarec5762e2020-01-01 14:08:48 +05301585 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001586 logger.Errorf(ctx, "Failed to remove resource %s due to %s", path, err)
Esin Karamand519bbf2020-07-01 11:16:03 +00001587 return err
Esin Karamanccb714b2019-11-29 15:02:06 +00001588 }
Esin Karamand519bbf2020-07-01 11:16:03 +00001589 return nil
Esin Karamanccb714b2019-11-29 15:02:06 +00001590}
1591
1592//GetFlowGroupFromKVStore fetches flow group from the KV store. Returns (false, {} error) if any problem occurs during
1593//fetching the data. Returns (true, groupInfo, nil) if the group is fetched successfully.
1594// Returns (false, {}, nil) if the group does not exists in the KV store.
npujarec5762e2020-01-01 14:08:48 +05301595func (RsrcMgr *OpenOltResourceMgr) GetFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) (bool, GroupInfo, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001596 var groupInfo GroupInfo
1597 var path string
1598 if cached {
1599 path = fmt.Sprintf(FlowGroupCached, groupID)
1600 } else {
1601 path = fmt.Sprintf(FlowGroup, groupID)
1602 }
npujarec5762e2020-01-01 14:08:48 +05301603 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001604 if err != nil {
1605 return false, groupInfo, err
1606 }
1607 if kvPair != nil && kvPair.Value != nil {
1608 Val, err := kvstore.ToByte(kvPair.Value)
1609 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001610 logger.Errorw(ctx, "Failed to convert flow group into byte array", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001611 return false, groupInfo, err
1612 }
1613 if err = json.Unmarshal(Val, &groupInfo); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001614 logger.Errorw(ctx, "Failed to unmarshal", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001615 return false, groupInfo, err
1616 }
1617 return true, groupInfo, nil
1618 }
1619 return false, groupInfo, nil
1620}
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001621
1622// toByte converts an interface value to a []byte. The interface should either be of
1623// a string type or []byte. Otherwise, an error is returned.
1624func toByte(value interface{}) ([]byte, error) {
1625 switch t := value.(type) {
1626 case []byte:
1627 return value.([]byte), nil
1628 case string:
1629 return []byte(value.(string)), nil
1630 default:
1631 return nil, fmt.Errorf("unexpected-type-%T", t)
1632 }
1633}
1634
1635func checkForFlowIDInList(FlowIDList []uint64, FlowID uint64) (bool, uint64) {
1636 /*
1637 Check for a flow id in a given list of flow IDs.
1638 :param FLowIDList: List of Flow IDs
1639 :param FlowID: Flowd to check in the list
1640 : return true and the index if present false otherwise.
1641 */
1642
1643 for idx := range FlowIDList {
1644 if FlowID == FlowIDList[idx] {
1645 return true, uint64(idx)
1646 }
1647 }
1648 return false, 0
1649}