blob: 7a3f8ec358061365866b7fc9910eb701b0487b48 [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 Gowdru6a80bbd2019-07-02 07:36:09 -0700112// OpenOltResourceMgr holds resource related information as provided below for each field
Abhilash S.L7f17e402019-03-15 17:40:41 +0530113type OpenOltResourceMgr struct {
Neha Sharma3f221ae2020-04-29 19:02:12 +0000114 DeviceID string // OLT device id
115 Address string // Host and port of the kv store to connect to
116 Args string // args
117 KVStore *db.Backend // backend kv store connection handle
118 DeviceType string
119 DevInfo *openolt.DeviceInfo // device information
Girish Gowdru0c588b22019-04-23 23:24:56 -0400120 // array of pon resource managers per interface technology
121 ResourceMgrs map[uint32]*ponrmgr.PONResourceManager
Girish Gowdra38d533d2020-03-30 20:38:51 -0700122
123 // This protects concurrent gemport_id allocate/delete calls on a per PON port basis
124 GemPortIDMgmtLock []sync.RWMutex
125 // This protects concurrent alloc_id allocate/delete calls on a per PON port basis
126 AllocIDMgmtLock []sync.RWMutex
127 // This protects concurrent onu_id allocate/delete calls on a per PON port basis
128 OnuIDMgmtLock []sync.RWMutex
Abhilash S.L7f17e402019-03-15 17:40:41 +0530129}
130
Neha Sharma96b7bf22020-06-15 10:37:32 +0000131func newKVClient(ctx context.Context, storeType string, address string, timeout time.Duration) (kvstore.Client, error) {
132 logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400133 switch storeType {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400134 case "etcd":
Neha Sharma96b7bf22020-06-15 10:37:32 +0000135 return kvstore.NewEtcdClient(ctx, address, timeout, log.FatalLevel)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400136 }
137 return nil, errors.New("unsupported-kv-store")
Abhilash S.L7f17e402019-03-15 17:40:41 +0530138}
139
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700140// SetKVClient sets the KV client and return a kv backend
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800141func SetKVClient(ctx context.Context, backend string, addr string, DeviceID string, basePathKvStore string) *db.Backend {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400142 // TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
143 // issue between kv store and backend , core is not calling NewBackend directly
Neha Sharma96b7bf22020-06-15 10:37:32 +0000144 kvClient, err := newKVClient(ctx, backend, addr, KvstoreTimeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400145 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000146 logger.Fatalw(ctx, "Failed to init KV client\n", log.Fields{"err": err})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400147 return nil
148 }
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700149
sbarbaria8910ba2019-11-05 10:12:23 -0500150 kvbackend := &db.Backend{
Girish Gowdru0c588b22019-04-23 23:24:56 -0400151 Client: kvClient,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700152 StoreType: backend,
Neha Sharma3f221ae2020-04-29 19:02:12 +0000153 Address: addr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700154 Timeout: KvstoreTimeout,
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800155 PathPrefix: fmt.Sprintf(BasePathKvStore, basePathKvStore, DeviceID)}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530156
Girish Gowdru0c588b22019-04-23 23:24:56 -0400157 return kvbackend
Abhilash S.L7f17e402019-03-15 17:40:41 +0530158}
159
Gamze Abakafee36392019-10-03 11:17:24 +0000160// NewResourceMgr init a New resource manager instance which in turn instantiates pon resource manager
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700161// instances according to technology. Initializes the default resource ranges for all
162// the resources.
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800163func 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 -0400164 var ResourceMgr OpenOltResourceMgr
divyadesai3af43e12020-08-18 07:10:54 +0000165 logger.Debugf(ctx, "Init new resource manager , address: %s, device-id: %s", KVStoreAddress, deviceID)
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700166 ResourceMgr.DeviceID = deviceID
Neha Sharma3f221ae2020-04-29 19:02:12 +0000167 ResourceMgr.Address = KVStoreAddress
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700168 ResourceMgr.DeviceType = deviceType
169 ResourceMgr.DevInfo = devInfo
Girish Gowdra38d533d2020-03-30 20:38:51 -0700170 NumPONPorts := devInfo.GetPonPorts()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530171
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700172 Backend := kvStoreType
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800173 ResourceMgr.KVStore = SetKVClient(ctx, Backend, ResourceMgr.Address, deviceID, basePathKvStore)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400174 if ResourceMgr.KVStore == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000175 logger.Error(ctx, "Failed to setup KV store")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400176 }
177 Ranges := make(map[string]*openolt.DeviceInfo_DeviceResourceRanges)
178 RsrcMgrsByTech := make(map[string]*ponrmgr.PONResourceManager)
179 ResourceMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530180
Girish Gowdra38d533d2020-03-30 20:38:51 -0700181 ResourceMgr.AllocIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
182 ResourceMgr.GemPortIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
183 ResourceMgr.OnuIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
184
Girish Gowdru0c588b22019-04-23 23:24:56 -0400185 // TODO self.args = registry('main').get_args()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530186
Girish Gowdru0c588b22019-04-23 23:24:56 -0400187 /*
188 If a legacy driver returns protobuf without any ranges,s synthesize one from
Gamze Abakafee36392019-10-03 11:17:24 +0000189 the legacy global per-device information. This, in theory, is temporary until
Girish Gowdru0c588b22019-04-23 23:24:56 -0400190 the legacy drivers are upgrade to support pool ranges.
191 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700192 if devInfo.Ranges == nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400193 var ranges openolt.DeviceInfo_DeviceResourceRanges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700194 ranges.Technology = devInfo.GetTechnology()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530195
Girish Gowdru0c588b22019-04-23 23:24:56 -0400196 var index uint32
197 for index = 0; index < NumPONPorts; index++ {
198 ranges.IntfIds = append(ranges.IntfIds, index)
199 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530200
Abhilash S.L8ee90712019-04-29 16:24:22 +0530201 var Pool openolt.DeviceInfo_DeviceResourceRanges_Pool
Girish Gowdru0c588b22019-04-23 23:24:56 -0400202 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700203 Pool.Start = devInfo.OnuIdStart
204 Pool.End = devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400205 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
cbabuabf02352019-10-15 13:14:56 +0200206 onuPool := Pool
207 ranges.Pools = append(ranges.Pools, &onuPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530208
Girish Gowdru0c588b22019-04-23 23:24:56 -0400209 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700210 Pool.Start = devInfo.AllocIdStart
211 Pool.End = devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400212 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200213 allocPool := Pool
214 ranges.Pools = append(ranges.Pools, &allocPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530215
Girish Gowdru0c588b22019-04-23 23:24:56 -0400216 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700217 Pool.Start = devInfo.GemportIdStart
218 Pool.End = devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400219 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200220 gemPool := Pool
221 ranges.Pools = append(ranges.Pools, &gemPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530222
Girish Gowdru0c588b22019-04-23 23:24:56 -0400223 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700224 Pool.Start = devInfo.FlowIdStart
225 Pool.End = devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400226 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
Abhilash S.L8ee90712019-04-29 16:24:22 +0530227 ranges.Pools = append(ranges.Pools, &Pool)
228 // Add to device info
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700229 devInfo.Ranges = append(devInfo.Ranges, &ranges)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400230 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530231
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700232 // Create a separate Resource Manager instance for each range. This assumes that
Girish Gowdru0c588b22019-04-23 23:24:56 -0400233 // each technology is represented by only a single range
234 var GlobalPONRsrcMgr *ponrmgr.PONResourceManager
235 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700236 for _, TechRange := range devInfo.Ranges {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400237 technology := TechRange.Technology
Neha Sharma96b7bf22020-06-15 10:37:32 +0000238 logger.Debugf(ctx, "Device info technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400239 Ranges[technology] = TechRange
Neha Sharma96b7bf22020-06-15 10:37:32 +0000240
241 RsrcMgrsByTech[technology], err = ponrmgr.NewPONResourceManager(ctx, technology, deviceType, deviceID,
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800242 Backend, ResourceMgr.Address, basePathKvStore)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400243 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000244 logger.Errorf(ctx, "Failed to create pon resource manager instance for technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400245 return nil
246 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700247 // resource_mgrs_by_tech[technology] = resource_mgr
Girish Gowdru0c588b22019-04-23 23:24:56 -0400248 if GlobalPONRsrcMgr == nil {
249 GlobalPONRsrcMgr = RsrcMgrsByTech[technology]
250 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700251 for _, IntfID := range TechRange.IntfIds {
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700252 ResourceMgr.ResourceMgrs[IntfID] = RsrcMgrsByTech[technology]
Girish Gowdru0c588b22019-04-23 23:24:56 -0400253 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700254 // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
npujarec5762e2020-01-01 14:08:48 +0530255 InitializeDeviceResourceRangeAndPool(ctx, RsrcMgrsByTech[technology], GlobalPONRsrcMgr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700256 TechRange, devInfo)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400257 }
258 // After we have initialized resource ranges, initialize the
259 // resource pools accordingly.
260 for _, PONRMgr := range RsrcMgrsByTech {
npujarec5762e2020-01-01 14:08:48 +0530261 _ = PONRMgr.InitDeviceResourcePool(ctx)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400262 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000263 logger.Info(ctx, "Initialization of resource manager success!")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400264 return &ResourceMgr
Abhilash S.L7f17e402019-03-15 17:40:41 +0530265}
266
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700267// InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply
268// device specific information. If KV doesn't exist
269// or is broader than the device, the device's information will
270// dictate the range limits
npujarec5762e2020-01-01 14:08:48 +0530271func InitializeDeviceResourceRangeAndPool(ctx context.Context, ponRMgr *ponrmgr.PONResourceManager, globalPONRMgr *ponrmgr.PONResourceManager,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700272 techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530273
Girish Gowdru0c588b22019-04-23 23:24:56 -0400274 // init the resource range pool according to the sharing type
Abhilash S.L7f17e402019-03-15 17:40:41 +0530275
Neha Sharma96b7bf22020-06-15 10:37:32 +0000276 logger.Debugf(ctx, "Resource range pool init for technology %s", ponRMgr.Technology)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700277 // first load from KV profiles
npujarec5762e2020-01-01 14:08:48 +0530278 status := ponRMgr.InitResourceRangesFromKVStore(ctx)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700279 if !status {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000280 logger.Debugf(ctx, "Failed to load resource ranges from KV store for tech %s", ponRMgr.Technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400281 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530282
Girish Gowdru0c588b22019-04-23 23:24:56 -0400283 /*
284 Then apply device specific information. If KV doesn't exist
Gamze Abakafee36392019-10-03 11:17:24 +0000285 or is broader than the device, the device's information will
Girish Gowdru0c588b22019-04-23 23:24:56 -0400286 dictate the range limits
287 */
Neha Sharma96b7bf22020-06-15 10:37:32 +0000288 logger.Debugw(ctx, "Using device info to init pon resource ranges", log.Fields{"Tech": ponRMgr.Technology})
Abhilash S.L7f17e402019-03-15 17:40:41 +0530289
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700290 ONUIDStart := devInfo.OnuIdStart
291 ONUIDEnd := devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400292 ONUIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
293 ONUIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700294 AllocIDStart := devInfo.AllocIdStart
295 AllocIDEnd := devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400296 AllocIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
297 AllocIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700298 GEMPortIDStart := devInfo.GemportIdStart
299 GEMPortIDEnd := devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400300 GEMPortIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
301 GEMPortIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700302 FlowIDStart := devInfo.FlowIdStart
303 FlowIDEnd := devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400304 FlowIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
305 FlowIDSharedPoolID := uint32(0)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530306
Girish Gowdru0c588b22019-04-23 23:24:56 -0400307 var FirstIntfPoolID uint32
308 var SharedPoolID uint32
Abhilash S.L7f17e402019-03-15 17:40:41 +0530309
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400310 /*
311 * 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 -0700312 * if resources are shared across interfaces then SharedPoolID is given a positive number.
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400313 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700314 for _, FirstIntfPoolID = range techRange.IntfIds {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400315 // skip the intf id 0
316 if FirstIntfPoolID == 0 {
317 continue
318 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400319 break
320 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530321
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700322 for _, RangePool := range techRange.Pools {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400323 if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400324 SharedPoolID = FirstIntfPoolID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400325 } else if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_SAME_TECH {
326 SharedPoolID = FirstIntfPoolID
327 } else {
328 SharedPoolID = 0
329 }
330 if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID {
331 ONUIDStart = RangePool.Start
332 ONUIDEnd = RangePool.End
333 ONUIDShared = RangePool.Sharing
334 ONUIDSharedPoolID = SharedPoolID
335 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID {
336 AllocIDStart = RangePool.Start
337 AllocIDEnd = RangePool.End
338 AllocIDShared = RangePool.Sharing
339 AllocIDSharedPoolID = SharedPoolID
340 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID {
341 GEMPortIDStart = RangePool.Start
342 GEMPortIDEnd = RangePool.End
343 GEMPortIDShared = RangePool.Sharing
344 GEMPortIDSharedPoolID = SharedPoolID
345 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID {
346 FlowIDStart = RangePool.Start
347 FlowIDEnd = RangePool.End
348 FlowIDShared = RangePool.Sharing
349 FlowIDSharedPoolID = SharedPoolID
350 }
351 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530352
Neha Sharma96b7bf22020-06-15 10:37:32 +0000353 logger.Debugw(ctx, "Device info init", log.Fields{"technology": techRange.Technology,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400354 "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd, "onu_id_shared_pool_id": ONUIDSharedPoolID,
355 "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd,
356 "alloc_id_shared_pool_id": AllocIDSharedPoolID,
357 "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd,
358 "gemport_id_shared_pool_id": GEMPortIDSharedPoolID,
359 "flow_id_start": FlowIDStart,
360 "flow_id_end_idx": FlowIDEnd,
361 "flow_id_shared_pool_id": FlowIDSharedPoolID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700362 "intf_ids": techRange.IntfIds,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400363 "uni_id_start": 0,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700364 "uni_id_end_idx": 1, /*MaxUNIIDperONU()*/
365 })
Abhilash S.L7f17e402019-03-15 17:40:41 +0530366
Neha Sharma96b7bf22020-06-15 10:37:32 +0000367 ponRMgr.InitDefaultPONResourceRanges(ctx, ONUIDStart, ONUIDEnd, ONUIDSharedPoolID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400368 AllocIDStart, AllocIDEnd, AllocIDSharedPoolID,
369 GEMPortIDStart, GEMPortIDEnd, GEMPortIDSharedPoolID,
370 FlowIDStart, FlowIDEnd, FlowIDSharedPoolID, 0, 1,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700371 devInfo.PonPorts, techRange.IntfIds)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530372
Girish Gowdru0c588b22019-04-23 23:24:56 -0400373 // For global sharing, make sure to refresh both local and global resource manager instances' range
Abhilash S.L7f17e402019-03-15 17:40:41 +0530374
Girish Gowdru0c588b22019-04-23 23:24:56 -0400375 if ONUIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000376 globalPONRMgr.UpdateRanges(ctx, ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400377 "", 0, nil)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000378 ponRMgr.UpdateRanges(ctx, ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700379 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400380 }
381 if AllocIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000382 globalPONRMgr.UpdateRanges(ctx, ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400383 "", 0, nil)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530384
Neha Sharma96b7bf22020-06-15 10:37:32 +0000385 ponRMgr.UpdateRanges(ctx, ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700386 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400387 }
388 if GEMPortIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000389 globalPONRMgr.UpdateRanges(ctx, ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400390 "", 0, nil)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000391 ponRMgr.UpdateRanges(ctx, ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700392 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400393 }
394 if FlowIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000395 globalPONRMgr.UpdateRanges(ctx, ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400396 "", 0, nil)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000397 ponRMgr.UpdateRanges(ctx, ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700398 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400399 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530400
Girish Gowdru0c588b22019-04-23 23:24:56 -0400401 // Make sure loaded range fits the platform bit encoding ranges
Neha Sharma96b7bf22020-06-15 10:37:32 +0000402 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 +0530403}
404
Devmalya Paul495b94a2019-08-27 19:42:00 -0400405// Delete clears used resources for the particular olt device being deleted
npujarec5762e2020-01-01 14:08:48 +0530406func (RsrcMgr *OpenOltResourceMgr) Delete(ctx context.Context) error {
Devmalya Paul495b94a2019-08-27 19:42:00 -0400407 /* TODO
408 def __del__(self):
409 self.log.info("clearing-device-resource-pool")
410 for key, resource_mgr in self.resource_mgrs.iteritems():
411 resource_mgr.clear_device_resource_pool()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530412
Devmalya Paul495b94a2019-08-27 19:42:00 -0400413 def assert_pon_id_limit(self, pon_intf_id):
414 assert pon_intf_id in self.resource_mgrs
Abhilash S.L7f17e402019-03-15 17:40:41 +0530415
Devmalya Paul495b94a2019-08-27 19:42:00 -0400416 def assert_onu_id_limit(self, pon_intf_id, onu_id):
417 self.assert_pon_id_limit(pon_intf_id)
418 self.resource_mgrs[pon_intf_id].assert_resource_limits(onu_id, PONResourceManager.ONU_ID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530419
Devmalya Paul495b94a2019-08-27 19:42:00 -0400420 @property
421 def max_uni_id_per_onu(self):
422 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 +0530423
Devmalya Paul495b94a2019-08-27 19:42:00 -0400424 def assert_uni_id_limit(self, pon_intf_id, onu_id, uni_id):
425 self.assert_onu_id_limit(pon_intf_id, onu_id)
426 self.resource_mgrs[pon_intf_id].assert_resource_limits(uni_id, PONResourceManager.UNI_ID)
427 */
428 for _, rsrcMgr := range RsrcMgr.ResourceMgrs {
npujarec5762e2020-01-01 14:08:48 +0530429 if err := rsrcMgr.ClearDeviceResourcePool(ctx); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000430 logger.Debug(ctx, "Failed to clear device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400431 return err
432 }
433 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000434 logger.Debug(ctx, "Cleared device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400435 return nil
436}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530437
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700438// GetONUID returns the available OnuID for the given pon-port
npujarec5762e2020-01-01 14:08:48 +0530439func (RsrcMgr *OpenOltResourceMgr) GetONUID(ctx context.Context, ponIntfID uint32) (uint32, error) {
salmansiddiqui352a45c2019-08-19 10:15:36 +0000440 // Check if Pon Interface ID is present in Resource-manager-map
Girish Gowdrab77ded92020-04-08 11:45:05 -0700441 RsrcMgr.OnuIDMgmtLock[ponIntfID].Lock()
442 defer RsrcMgr.OnuIDMgmtLock[ponIntfID].Unlock()
443
salmansiddiqui352a45c2019-08-19 10:15:36 +0000444 if _, ok := RsrcMgr.ResourceMgrs[ponIntfID]; !ok {
445 err := errors.New("invalid-pon-interface-" + strconv.Itoa(int(ponIntfID)))
446 return 0, err
447 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400448 // Get ONU id for a provided pon interface ID.
Matteo Scandolo84585372021-03-18 14:21:22 -0700449 onuID, err := RsrcMgr.ResourceMgrs[ponIntfID].TechProfileMgr.GetResourceID(ctx, ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400450 ponrmgr.ONU_ID, 1)
451 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000452 logger.Errorf(ctx, "Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700453 ponIntfID, ponrmgr.ONU_ID)
cbabuabf02352019-10-15 13:14:56 +0200454 return 0, err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400455 }
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700456 if onuID != nil {
457 RsrcMgr.ResourceMgrs[ponIntfID].InitResourceMap(ctx, fmt.Sprintf("%d,%d", ponIntfID, onuID[0]))
458 return onuID[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400459 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530460
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700461 return 0, err // return OnuID 0 on error
Abhilash S.L7f17e402019-03-15 17:40:41 +0530462}
463
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700464// GetFlowIDInfo returns the slice of flow info of the given pon-port
465// Note: For flows which trap from the NNI and not really associated with any particular
466// 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 -0700467func (RsrcMgr *OpenOltResourceMgr) GetFlowIDInfo(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32, flowID uint64) *FlowInfo {
468 var flowInfo FlowInfo
Abhilash S.L8ee90712019-04-29 16:24:22 +0530469
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700470 subs := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
471 Path := fmt.Sprintf(FlowIDInfoPath, subs, flowID)
472 value, err := RsrcMgr.KVStore.Get(ctx, Path)
473 if err == nil {
474 if value != nil {
475 Val, err := toByte(value.Value)
476 if err != nil {
477 logger.Errorw(ctx, "Failed to convert flowinfo into byte array", log.Fields{"error": err, "subs": subs})
478 return nil
479 }
480 if err = json.Unmarshal(Val, &flowInfo); err != nil {
481 logger.Errorw(ctx, "Failed to unmarshal", log.Fields{"error": err, "subs": subs})
482 return nil
483 }
484 }
485 }
486 if flowInfo.Flow == nil {
487 logger.Debugw(ctx, "No flowInfo found in KV store", log.Fields{"subs": subs})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530488 return nil
489 }
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700490 return &flowInfo
Abhilash S.L8ee90712019-04-29 16:24:22 +0530491}
492
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700493// GetCurrentFlowIDsForOnu fetches flow ID from the resource manager
494// Note: For flows which trap from the NNI and not really associated with any particular
495// 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 -0700496func (RsrcMgr *OpenOltResourceMgr) GetCurrentFlowIDsForOnu(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32) ([]uint64, error) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700497
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700498 subs := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
499 path := fmt.Sprintf(FlowIDPath, subs)
500
501 var data []uint64
502 value, err := RsrcMgr.KVStore.Get(ctx, path)
503 if err == nil {
504 if value != nil {
505 Val, _ := toByte(value.Value)
506 if err = json.Unmarshal(Val, &data); err != nil {
507 logger.Error(ctx, "Failed to unmarshal")
508 return nil, err
509 }
510 }
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700511 }
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700512 return data, nil
Abhilash S.L8ee90712019-04-29 16:24:22 +0530513}
514
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700515// UpdateFlowIDInfo updates flow info for the given pon interface, onu id, and uni id
516// Note: For flows which trap from the NNI and not really associated with any particular
517// 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 -0700518func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDInfo(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32,
519 flowID uint64, flowData FlowInfo) error {
520
521 subs := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
522 path := fmt.Sprintf(FlowIDInfoPath, subs, flowID)
523
524 var value []byte
525 var err error
526 value, err = json.Marshal(flowData)
527 if err != nil {
528 logger.Errorf(ctx, "failed to Marshal, resource path %s", path)
529 return err
530 }
531
532 if err = RsrcMgr.KVStore.Put(ctx, path, value); err != nil {
533 logger.Errorf(ctx, "Failed to update resource %s", path)
534 }
535
536 // Update the flowID list for the ONU
537 if err = RsrcMgr.UpdateFlowIDForOnu(ctx, ponIntfID, onuID, uniID, flowID, true); err != nil {
538 // If the operation fails, try to remove FlowInfo from the KV store
539 _ = RsrcMgr.KVStore.Delete(ctx, path)
540 return err
541 }
542 return err
Abhilash S.L8ee90712019-04-29 16:24:22 +0530543}
544
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700545// UpdateFlowIDForOnu updates the flow_id list of the ONU (add or remove flow_id from the list)
546func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDForOnu(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32, flowID uint64, add bool) error {
547 /*
548 Update the flow_id list of the ONU (add or remove flow_id from the list)
549 :param pon_intf_onu_id: reference of PON interface id and onu id
550 :param flow_id: flow ID
551 :param add: Boolean flag to indicate whether the flow_id should be
552 added or removed from the list. Defaults to adding the flow.
553 */
554 var Value []byte
555 var err error
556 var retVal bool
557 var idx uint64
558 subs := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
559 path := fmt.Sprintf(FlowIDPath, subs)
560 flowIDs, err := RsrcMgr.GetCurrentFlowIDsForOnu(ctx, ponIntfID, onuID, uniID)
561 if err != nil {
562 // Error logged in the called function
563 return err
564 }
565
566 if add {
567 if retVal, _ = checkForFlowIDInList(flowIDs, flowID); retVal {
568 return nil
569 }
570 flowIDs = append(flowIDs, flowID)
571 } else {
572 if retVal, idx = checkForFlowIDInList(flowIDs, flowID); !retVal {
573 return nil
574 }
575 // delete the index and shift
576 flowIDs = append(flowIDs[:idx], flowIDs[idx+1:]...)
577 }
578 Value, err = json.Marshal(flowIDs)
579 if err != nil {
580 logger.Error(ctx, "Failed to Marshal")
581 return err
582 }
583
584 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
585 logger.Errorf(ctx, "Failed to update resource %s", path)
586 return err
587 }
588 return err
589}
590
591// RemoveFlowIDInfo remove flow info for the given pon interface, onu id, and uni id
592// Note: For flows which trap from the NNI and not really associated with any particular
593// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
594func (RsrcMgr *OpenOltResourceMgr) RemoveFlowIDInfo(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32,
595 flowID uint64) error {
596
597 subs := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
598 path := fmt.Sprintf(FlowIDInfoPath, subs, flowID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530599
Girish Gowdru0c588b22019-04-23 23:24:56 -0400600 var err error
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700601 if err = RsrcMgr.KVStore.Delete(ctx, path); err != nil {
602 logger.Errorf(ctx, "Failed to delete resource %s", path)
603 return err
604 }
Girish Gowdrab77ded92020-04-08 11:45:05 -0700605
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700606 // Update the flowID list for the ONU
607 err = RsrcMgr.UpdateFlowIDForOnu(ctx, ponIntfID, onuID, uniID, flowID, false)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700608
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700609 return err
610}
611
612// RemoveAllFlowsForIntfOnuUniKey removes flow info for the given interface, onu id, and uni id
613func (RsrcMgr *OpenOltResourceMgr) RemoveAllFlowsForIntfOnuUniKey(ctx context.Context, intf uint32, onuID int32, uniID int32) error {
614 flowIDs, err := RsrcMgr.GetCurrentFlowIDsForOnu(ctx, intf, onuID, uniID)
615 if err != nil {
616 // error logged in the called function
617 return err
618 }
619 for _, flID := range flowIDs {
620 if err := RsrcMgr.RemoveFlowIDInfo(ctx, intf, onuID, uniID, flID); err != nil {
621 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 +0530622 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400623 }
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700624 subs := fmt.Sprintf("%d,%d,%d", intf, onuID, uniID)
625 path := fmt.Sprintf(FlowIDPath, subs)
626 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
627 logger.Errorf(ctx, "Failed to delete resource %s", path)
628 return err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400629 }
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700630 return nil
Abhilash S.L7f17e402019-03-15 17:40:41 +0530631}
632
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700633// GetAllocID return the first Alloc ID for a given pon interface id and onu id and then update the resource map on
634// the KV store with the list of alloc_ids allocated for the pon_intf_onu_id tuple
635// 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 +0530636func (RsrcMgr *OpenOltResourceMgr) GetAllocID(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530637
Girish Gowdru0c588b22019-04-23 23:24:56 -0400638 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700639 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700640
641 RsrcMgr.AllocIDMgmtLock[intfID].Lock()
642 defer RsrcMgr.AllocIDMgmtLock[intfID].Unlock()
643
npujarec5762e2020-01-01 14:08:48 +0530644 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400645 if AllocID != nil {
646 // Since we support only one alloc_id for the ONU at the moment,
647 // return the first alloc_id in the list, if available, for that
648 // ONU.
Neha Sharma96b7bf22020-06-15 10:37:32 +0000649 logger.Debugw(ctx, "Retrieved alloc ID from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400650 return AllocID[0]
651 }
npujarec5762e2020-01-01 14:08:48 +0530652 AllocID, err = RsrcMgr.ResourceMgrs[intfID].GetResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400653 ponrmgr.ALLOC_ID, 1)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530654
Girish Gowdru0c588b22019-04-23 23:24:56 -0400655 if AllocID == nil || err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000656 logger.Error(ctx, "Failed to allocate alloc id")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400657 return 0
658 }
659 // update the resource map on KV store with the list of alloc_id
660 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530661 err = RsrcMgr.ResourceMgrs[intfID].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID, AllocID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400662 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000663 logger.Error(ctx, "Failed to update Alloc ID")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400664 return 0
665 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000666 logger.Debugw(ctx, "Allocated new Tcont from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400667 return AllocID[0]
Abhilash S.L7f17e402019-03-15 17:40:41 +0530668}
669
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700670// UpdateAllocIdsForOnu updates alloc ids in kv store for a given pon interface id, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530671func (RsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ctx context.Context, ponPort uint32, onuID uint32, uniID uint32, allocID []uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530672
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700673 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530674 return RsrcMgr.ResourceMgrs[ponPort].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700675 allocID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530676}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700677
678// GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530679func (RsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(ctx context.Context, intfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700680 uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530681
Girish Gowdru0c588b22019-04-23 23:24:56 -0400682 /* Get gem ports for given pon interface , onu id and uni id. */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530683
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700684 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530685 return RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530686}
687
Gamze Abakafee36392019-10-03 11:17:24 +0000688// GetCurrentAllocIDsForOnu returns alloc ids for given pon interface and onu id
npujarec5762e2020-01-01 14:08:48 +0530689func (RsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDsForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530690
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700691 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530692 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400693 if AllocID != nil {
Gamze Abakafee36392019-10-03 11:17:24 +0000694 return AllocID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400695 }
Gamze Abakafee36392019-10-03 11:17:24 +0000696 return []uint32{}
697}
698
699// RemoveAllocIDForOnu removes the alloc id for given pon interface, onu id, uni id and alloc id
npujarec5762e2020-01-01 14:08:48 +0530700func (RsrcMgr *OpenOltResourceMgr) RemoveAllocIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, allocID uint32) {
701 allocIDs := RsrcMgr.GetCurrentAllocIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000702 for i := 0; i < len(allocIDs); i++ {
703 if allocIDs[i] == allocID {
704 allocIDs = append(allocIDs[:i], allocIDs[i+1:]...)
705 break
706 }
707 }
npujarec5762e2020-01-01 14:08:48 +0530708 err := RsrcMgr.UpdateAllocIdsForOnu(ctx, intfID, onuID, uniID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000709 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000710 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 +0000711 intfID, onuID, uniID, allocID)
712 }
713}
714
715// RemoveGemPortIDForOnu removes the gem port id for given pon interface, onu id, uni id and gem port id
npujarec5762e2020-01-01 14:08:48 +0530716func (RsrcMgr *OpenOltResourceMgr) RemoveGemPortIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, gemPortID uint32) {
717 gemPortIDs := RsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000718 for i := 0; i < len(gemPortIDs); i++ {
719 if gemPortIDs[i] == gemPortID {
720 gemPortIDs = append(gemPortIDs[:i], gemPortIDs[i+1:]...)
721 break
722 }
723 }
npujarec5762e2020-01-01 14:08:48 +0530724 err := RsrcMgr.UpdateGEMPortIDsForOnu(ctx, intfID, onuID, uniID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000725 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000726 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 +0000727 intfID, onuID, uniID, gemPortID)
728 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530729}
730
Esin Karamandf392e12020-12-16 13:33:09 +0000731//GetUniPortByPonPortGemPortFromKVStore retrieves onu and uni ID associated with the pon and gem ports.
732func (RsrcMgr *OpenOltResourceMgr) GetUniPortByPonPortGemPortFromKVStore(ctx context.Context, PonPort uint32, GemPort uint32) (uint32, uint32, error) {
733 IntfGEMPortPath := fmt.Sprintf("%d,%d", PonPort, GemPort)
734 logger.Debugf(ctx, "Getting ONU and UNI IDs from the path %s", IntfGEMPortPath)
735 var Data []uint32
736 Value, err := RsrcMgr.KVStore.Get(ctx, IntfGEMPortPath)
737 if err == nil {
738 if Value != nil {
739 Val, _ := ponrmgr.ToByte(Value.Value)
740 if err = json.Unmarshal(Val, &Data); err != nil {
741 logger.Errorw(ctx, "Failed to unmarshal", log.Fields{"error": err})
742 return 0, 0, errors.New("failed to unmarshal the data retrieved")
743 }
744 }
745 } else {
746 logger.Errorf(ctx, "Failed to get data from kvstore for %s", IntfGEMPortPath, err)
747 return 0, 0, errors.New("could not get data")
748 }
749 if len(Data) < 2 {
750 return 0, 0, errors.New("invalid data format")
751 }
752 return Data[0], Data[1], nil
753}
754
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700755// UpdateGEMportsPonportToOnuMapOnKVStore updates onu and uni id associated with the gem port to the kv store
756// This stored information is used when packet_indication is received and we need to derive the ONU Id for which
757// the packet arrived based on the pon_intf and gemport available in the packet_indication
npujarec5762e2020-01-01 14:08:48 +0530758func (RsrcMgr *OpenOltResourceMgr) UpdateGEMportsPonportToOnuMapOnKVStore(ctx context.Context, gemPorts []uint32, PonPort uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700759 onuID uint32, uniID uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530760
Girish Gowdru0c588b22019-04-23 23:24:56 -0400761 /* Update onu and uni id associated with the gem port to the kv store. */
762 var IntfGEMPortPath string
Esin Karamandf392e12020-12-16 13:33:09 +0000763 Data := []uint32{onuID, uniID}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700764 for _, GEM := range gemPorts {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400765 IntfGEMPortPath = fmt.Sprintf("%d,%d", PonPort, GEM)
766 Val, err := json.Marshal(Data)
767 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000768 logger.Error(ctx, "failed to Marshal")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400769 return err
770 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700771
npujarec5762e2020-01-01 14:08:48 +0530772 if err = RsrcMgr.KVStore.Put(ctx, IntfGEMPortPath, Val); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000773 logger.Errorf(ctx, "Failed to update resource %s", IntfGEMPortPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400774 return err
775 }
776 }
777 return nil
Abhilash S.L7f17e402019-03-15 17:40:41 +0530778}
779
Gamze Abakafee36392019-10-03 11:17:24 +0000780// RemoveGEMportPonportToOnuMapOnKVStore removes the relationship between the gem port and pon port
npujarec5762e2020-01-01 14:08:48 +0530781func (RsrcMgr *OpenOltResourceMgr) RemoveGEMportPonportToOnuMapOnKVStore(ctx context.Context, GemPort uint32, PonPort uint32) {
Gamze Abakafee36392019-10-03 11:17:24 +0000782 IntfGEMPortPath := fmt.Sprintf("%d,%d", PonPort, GemPort)
npujarec5762e2020-01-01 14:08:48 +0530783 err := RsrcMgr.KVStore.Delete(ctx, IntfGEMPortPath)
Gamze Abakafee36392019-10-03 11:17:24 +0000784 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000785 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 +0000786 }
787}
788
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700789// GetGEMPortID gets gem port id for a particular pon port, onu id and uni id and then update the resource map on
790// the KV store with the list of gemport_id allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530791func (RsrcMgr *OpenOltResourceMgr) GetGEMPortID(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700792 uniID uint32, NumOfPorts uint32) ([]uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530793
Girish Gowdru0c588b22019-04-23 23:24:56 -0400794 /* Get gem port id for a particular pon port, onu id
795 and uni id.
796 */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530797
Girish Gowdru0c588b22019-04-23 23:24:56 -0400798 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700799 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530800
Girish Gowdrab77ded92020-04-08 11:45:05 -0700801 RsrcMgr.GemPortIDMgmtLock[ponPort].Lock()
802 defer RsrcMgr.GemPortIDMgmtLock[ponPort].Unlock()
803
npujarec5762e2020-01-01 14:08:48 +0530804 GEMPortList := RsrcMgr.ResourceMgrs[ponPort].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400805 if GEMPortList != nil {
806 return GEMPortList, nil
807 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530808
npujarec5762e2020-01-01 14:08:48 +0530809 GEMPortList, err = RsrcMgr.ResourceMgrs[ponPort].GetResourceID(ctx, ponPort,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400810 ponrmgr.GEMPORT_ID, NumOfPorts)
811 if err != nil && GEMPortList == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000812 logger.Errorf(ctx, "Failed to get gem port id for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400813 return nil, err
814 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530815
Girish Gowdru0c588b22019-04-23 23:24:56 -0400816 // update the resource map on KV store with the list of gemport_id
817 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530818 err = RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400819 GEMPortList)
820 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000821 logger.Errorf(ctx, "Failed to update GEM ports to kv store for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400822 return nil, err
823 }
npujarec5762e2020-01-01 14:08:48 +0530824 _ = RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(ctx, GEMPortList, ponPort,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700825 onuID, uniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400826 return GEMPortList, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530827}
828
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700829// 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 +0530830func (RsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700831 uniID uint32, GEMPortList []uint32) error {
832 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530833 return RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400834 GEMPortList)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530835
836}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700837
838// FreeonuID releases(make free) onu id for a particular pon-port
npujarec5762e2020-01-01 14:08:48 +0530839func (RsrcMgr *OpenOltResourceMgr) FreeonuID(ctx context.Context, intfID uint32, onuID []uint32) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700840
Girish Gowdra38d533d2020-03-30 20:38:51 -0700841 RsrcMgr.OnuIDMgmtLock[intfID].Lock()
Girish Gowdrab77ded92020-04-08 11:45:05 -0700842 defer RsrcMgr.OnuIDMgmtLock[intfID].Unlock()
843
Matteo Scandolo84585372021-03-18 14:21:22 -0700844 if err := RsrcMgr.ResourceMgrs[intfID].TechProfileMgr.FreeResourceID(ctx, intfID, ponrmgr.ONU_ID, onuID); err != nil {
845 logger.Errorw(ctx, "error-while-freeing-onu-id", log.Fields{
846 "intf-id": intfID,
847 "onu-id": onuID,
848 "err": err.Error(),
849 })
850 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530851
Girish Gowdru0c588b22019-04-23 23:24:56 -0400852 /* Free onu id for a particular interface.*/
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700853 var IntfonuID string
854 for _, onu := range onuID {
855 IntfonuID = fmt.Sprintf("%d,%d", intfID, onu)
npujarec5762e2020-01-01 14:08:48 +0530856 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfonuID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400857 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530858}
859
Gamze Abakafee36392019-10-03 11:17:24 +0000860// FreeAllocID frees AllocID on the PON resource pool and also frees the allocID association
861// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530862func (RsrcMgr *OpenOltResourceMgr) FreeAllocID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000863 uniID uint32, allocID uint32) {
Girish Gowdrab77ded92020-04-08 11:45:05 -0700864 RsrcMgr.AllocIDMgmtLock[IntfID].Lock()
865 defer RsrcMgr.AllocIDMgmtLock[IntfID].Unlock()
866
npujarec5762e2020-01-01 14:08:48 +0530867 RsrcMgr.RemoveAllocIDForOnu(ctx, IntfID, onuID, uniID, allocID)
Gamze Abakafee36392019-10-03 11:17:24 +0000868 allocIDs := make([]uint32, 0)
869 allocIDs = append(allocIDs, allocID)
Matteo Scandolo84585372021-03-18 14:21:22 -0700870 if err := RsrcMgr.ResourceMgrs[IntfID].TechProfileMgr.FreeResourceID(ctx, IntfID, ponrmgr.ALLOC_ID, allocIDs); err != nil {
871 logger.Errorw(ctx, "error-while-freeing-alloc-id", log.Fields{
872 "intf-id": IntfID,
873 "onu-id": onuID,
874 "err": err.Error(),
875 })
876 }
Gamze Abakafee36392019-10-03 11:17:24 +0000877}
878
879// FreeGemPortID frees GemPortID on the PON resource pool and also frees the gemPortID association
880// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530881func (RsrcMgr *OpenOltResourceMgr) FreeGemPortID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000882 uniID uint32, gemPortID uint32) {
Girish Gowdrab77ded92020-04-08 11:45:05 -0700883 RsrcMgr.GemPortIDMgmtLock[IntfID].Lock()
884 defer RsrcMgr.GemPortIDMgmtLock[IntfID].Unlock()
885
npujarec5762e2020-01-01 14:08:48 +0530886 RsrcMgr.RemoveGemPortIDForOnu(ctx, IntfID, onuID, uniID, gemPortID)
Gamze Abakafee36392019-10-03 11:17:24 +0000887 gemPortIDs := make([]uint32, 0)
888 gemPortIDs = append(gemPortIDs, gemPortID)
Matteo Scandolo84585372021-03-18 14:21:22 -0700889 if err := RsrcMgr.ResourceMgrs[IntfID].TechProfileMgr.FreeResourceID(ctx, IntfID, ponrmgr.GEMPORT_ID, gemPortIDs); err != nil {
890 logger.Errorw(ctx, "error-while-freeing-gem-port-id", log.Fields{
891 "intf-id": IntfID,
892 "onu-id": onuID,
893 "err": err.Error(),
894 })
895 }
Gamze Abakafee36392019-10-03 11:17:24 +0000896}
897
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700898// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id, and the clears the
899// resource map and the onuID associated with (pon_intf_id, gemport_id) tuple,
npujarec5762e2020-01-01 14:08:48 +0530900func (RsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530901
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700902 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530903
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700904 RsrcMgr.AllocIDMgmtLock[intfID].Lock()
Girish Gowdrab77ded92020-04-08 11:45:05 -0700905 AllocIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700906
Matteo Scandolo84585372021-03-18 14:21:22 -0700907 if err := RsrcMgr.ResourceMgrs[intfID].TechProfileMgr.FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400908 ponrmgr.ALLOC_ID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700909 AllocIDs); err != nil {
910 logger.Errorw(ctx, "error-while-freeing-all-alloc-ids-for-onu", log.Fields{
911 "intf-id": intfID,
912 "onu-id": onuID,
913 "err": err.Error(),
914 })
915 }
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700916 RsrcMgr.AllocIDMgmtLock[intfID].Unlock()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530917
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700918 RsrcMgr.GemPortIDMgmtLock[intfID].Lock()
npujarec5762e2020-01-01 14:08:48 +0530919 GEMPortIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Matteo Scandolo84585372021-03-18 14:21:22 -0700920 if err := RsrcMgr.ResourceMgrs[intfID].TechProfileMgr.FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400921 ponrmgr.GEMPORT_ID,
Matteo Scandolo84585372021-03-18 14:21:22 -0700922 GEMPortIDs); err != nil {
923 logger.Errorw(ctx, "error-while-freeing-all-gem-port-ids-for-onu", log.Fields{
924 "intf-id": intfID,
925 "onu-id": onuID,
926 "err": err.Error(),
927 })
928 }
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700929 RsrcMgr.GemPortIDMgmtLock[intfID].Unlock()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530930
Girish Gowdru0c588b22019-04-23 23:24:56 -0400931 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
npujarec5762e2020-01-01 14:08:48 +0530932 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400933 // Clear the ONU Id associated with the (pon_intf_id, gemport_id) tuple.
934 for _, GEM := range GEMPortIDs {
npujarec5762e2020-01-01 14:08:48 +0530935 _ = RsrcMgr.KVStore.Delete(ctx, fmt.Sprintf("%d,%d", intfID, GEM))
Girish Gowdru0c588b22019-04-23 23:24:56 -0400936 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530937}
938
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700939// IsFlowOnKvStore checks if the given flowID is present on the kv store
940// Returns true if the flowID is found, otherwise it returns false
941func (RsrcMgr *OpenOltResourceMgr) IsFlowOnKvStore(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32,
942 flowID uint64) bool {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530943
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700944 FlowIDs, err := RsrcMgr.GetCurrentFlowIDsForOnu(ctx, ponIntfID, onuID, uniID)
945 if err != nil {
946 // error logged in the called function
947 return false
948 }
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400949 if FlowIDs != nil {
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700950 logger.Debugw(ctx, "Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "onuID": onuID, "uniID": uniID})
951 for _, id := range FlowIDs {
952 if flowID == id {
953 return true
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400954 }
955 }
956 }
957 return false
958}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400959
salmansiddiqui7ac62132019-08-22 03:58:50 +0000960// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000961// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530962func (RsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) []uint32 {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000963 Path := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000964 var Data []uint32
npujarec5762e2020-01-01 14:08:48 +0530965 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400966 if err == nil {
967 if Value != nil {
968 Val, err := kvstore.ToByte(Value.Value)
969 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000970 logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"error": err})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400971 return Data
972 }
973 if err = json.Unmarshal(Val, &Data); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000974 logger.Error(ctx, "Failed to unmarshal", log.Fields{"error": err})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400975 return Data
976 }
977 }
978 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000979 logger.Errorf(ctx, "Failed to get TP id from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400980 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000981 logger.Debugf(ctx, "Getting TP id %d from path %s", Data, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400982 return Data
983
984}
985
Gamze Abakafee36392019-10-03 11:17:24 +0000986// RemoveTechProfileIDsForOnu deletes all tech profile ids from the KV-Store for the given onu based on the path
987// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530988func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDsForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) error {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000989 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
npujarec5762e2020-01-01 14:08:48 +0530990 if err := RsrcMgr.KVStore.Delete(ctx, IntfOnuUniID); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000991 logger.Errorw(ctx, "Failed to delete techprofile id resource in KV store", log.Fields{"path": IntfOnuUniID})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400992 return err
993 }
994 return nil
995}
996
Gamze Abakafee36392019-10-03 11:17:24 +0000997// RemoveTechProfileIDForOnu deletes a specific tech profile id from the KV-Store for the given onu based on the path
998// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530999func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32, TpID uint32) error {
1000 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +00001001 for i, tpIDInList := range tpIDList {
1002 if tpIDInList == TpID {
1003 tpIDList = append(tpIDList[:i], tpIDList[i+1:]...)
1004 }
1005 }
1006 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
1007 Value, err := json.Marshal(tpIDList)
1008 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001009 logger.Error(ctx, "failed to Marshal")
Gamze Abakafee36392019-10-03 11:17:24 +00001010 return err
1011 }
npujarec5762e2020-01-01 14:08:48 +05301012 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001013 logger.Errorf(ctx, "Failed to update resource %s", IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +00001014 return err
1015 }
1016 return err
1017}
1018
1019// UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
1020// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +05301021func (RsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32,
salmansiddiqui7ac62132019-08-22 03:58:50 +00001022 UniID uint32, TpID uint32) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -04001023 var Value []byte
1024 var err error
1025
salmansiddiqui7ac62132019-08-22 03:58:50 +00001026 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +00001027
npujarec5762e2020-01-01 14:08:48 +05301028 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +00001029 for _, value := range tpIDList {
1030 if value == TpID {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001031 logger.Debugf(ctx, "TpID %d is already in tpIdList for the path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +00001032 return err
1033 }
1034 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001035 logger.Debugf(ctx, "updating tp id %d on path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +00001036 tpIDList = append(tpIDList, TpID)
1037 Value, err = json.Marshal(tpIDList)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001038 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001039 logger.Error(ctx, "failed to Marshal")
Manikkaraj kb1d51442019-07-23 10:41:02 -04001040 return err
1041 }
npujarec5762e2020-01-01 14:08:48 +05301042 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001043 logger.Errorf(ctx, "Failed to update resource %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001044 return err
1045 }
1046 return err
1047}
1048
salmansiddiqui7ac62132019-08-22 03:58:50 +00001049// UpdateMeterIDForOnu updates the meter id in the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +00001050// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +05301051func (RsrcMgr *OpenOltResourceMgr) UpdateMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +00001052 UniID uint32, TpID uint32, MeterConfig *ofp.OfpMeterConfig) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -04001053 var Value []byte
1054 var err error
1055
Gamze Abakafee36392019-10-03 11:17:24 +00001056 IntfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001057 Value, err = json.Marshal(*MeterConfig)
1058 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001059 logger.Error(ctx, "failed to Marshal meter config")
Manikkaraj kb1d51442019-07-23 10:41:02 -04001060 return err
1061 }
npujarec5762e2020-01-01 14:08:48 +05301062 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001063 logger.Errorf(ctx, "Failed to store meter into KV store %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001064 return err
1065 }
1066 return err
1067}
1068
Gamze Abakafee36392019-10-03 11:17:24 +00001069// GetMeterIDForOnu fetches the meter id from the kv store for the given onu based on the path
1070// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +05301071func (RsrcMgr *OpenOltResourceMgr) GetMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +00001072 UniID uint32, TpID uint32) (*ofp.OfpMeterConfig, error) {
1073 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001074 var meterConfig ofp.OfpMeterConfig
npujarec5762e2020-01-01 14:08:48 +05301075 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001076 if err == nil {
1077 if Value != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001078 logger.Debug(ctx, "Found meter in KV store", log.Fields{"Direction": Direction})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001079 Val, er := kvstore.ToByte(Value.Value)
1080 if er != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001081 logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"error": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001082 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -04001083 }
salmansiddiqui7ac62132019-08-22 03:58:50 +00001084 if er = json.Unmarshal(Val, &meterConfig); er != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001085 logger.Error(ctx, "Failed to unmarshal meterconfig", log.Fields{"error": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001086 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -04001087 }
1088 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001089 logger.Debug(ctx, "meter-does-not-exists-in-KVStore")
Manikkaraj kb1d51442019-07-23 10:41:02 -04001090 return nil, err
1091 }
1092 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001093 logger.Errorf(ctx, "Failed to get Meter config from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001094
1095 }
1096 return &meterConfig, err
1097}
1098
Gamze Abakafee36392019-10-03 11:17:24 +00001099// RemoveMeterIDForOnu deletes the meter id from the kV-Store for the given onu based on the path
1100// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +05301101func (RsrcMgr *OpenOltResourceMgr) RemoveMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +00001102 UniID uint32, TpID uint32) error {
1103 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
npujarec5762e2020-01-01 14:08:48 +05301104 if err := RsrcMgr.KVStore.Delete(ctx, Path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001105 logger.Errorf(ctx, "Failed to delete meter id %s from kvstore ", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001106 return err
1107 }
1108 return nil
1109}
salmansiddiqui7ac62132019-08-22 03:58:50 +00001110
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301111//AddGemToOnuGemInfo adds gemport to onugem info kvstore
npujarec5762e2020-01-01 14:08:48 +05301112func (RsrcMgr *OpenOltResourceMgr) AddGemToOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, gemPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301113 var onuGemData []OnuGemInfo
1114 var err error
1115
npujarec5762e2020-01-01 14:08:48 +05301116 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001117 logger.Errorf(ctx, "failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301118 return err
1119 }
1120 if len(onuGemData) == 0 {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001121 logger.Errorw(ctx, "failed to ger Onuid info ", log.Fields{"intfid": intfID, "onuid": onuID})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301122 return err
1123 }
1124
1125 for idx, onugem := range onuGemData {
1126 if onugem.OnuID == onuID {
1127 for _, gem := range onuGemData[idx].GemPorts {
1128 if gem == gemPort {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001129 logger.Debugw(ctx, "Gem already present in onugem info, skpping addition", log.Fields{"gem": gem})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301130 return nil
1131 }
1132 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001133 logger.Debugw(ctx, "Added gem to onugem info", log.Fields{"gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301134 onuGemData[idx].GemPorts = append(onuGemData[idx].GemPorts, gemPort)
1135 break
1136 }
1137 }
npujarec5762e2020-01-01 14:08:48 +05301138 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301139 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001140 logger.Error(ctx, "Failed to add onugem to kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301141 return err
1142 }
1143 return err
1144}
1145
1146//GetOnuGemInfo gets onu gem info from the kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301147func (RsrcMgr *OpenOltResourceMgr) GetOnuGemInfo(ctx context.Context, IntfID uint32) ([]OnuGemInfo, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301148 var onuGemData []OnuGemInfo
1149
npujarec5762e2020-01-01 14:08:48 +05301150 if err := RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001151 logger.Errorf(ctx, "failed to get onuifo for intfid %d", IntfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301152 return nil, err
1153 }
1154
1155 return onuGemData, nil
1156}
1157
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001158// AddOnuGemInfo adds onu info on to the kvstore per interface
1159func (RsrcMgr *OpenOltResourceMgr) AddOnuGemInfo(ctx context.Context, IntfID uint32, onuGem OnuGemInfo) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301160 var onuGemData []OnuGemInfo
1161 var err error
1162
npujarec5762e2020-01-01 14:08:48 +05301163 if err = RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001164 logger.Errorf(ctx, "failed to get onuifo for intfid %d", IntfID)
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001165 return olterrors.NewErrPersistence("get", "OnuGemInfo", uint64(IntfID),
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001166 log.Fields{"onuGem": onuGem, "intfID": IntfID}, err)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301167 }
1168 onuGemData = append(onuGemData, onuGem)
npujarec5762e2020-01-01 14:08:48 +05301169 err = RsrcMgr.ResourceMgrs[IntfID].AddOnuGemInfo(ctx, IntfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301170 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001171 logger.Error(ctx, "Failed to add onugem to kv store")
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001172 return olterrors.NewErrPersistence("set", "OnuGemInfo", uint64(IntfID),
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001173 log.Fields{"onuGemData": onuGemData, "intfID": IntfID}, err)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301174 }
1175
Neha Sharma96b7bf22020-06-15 10:37:32 +00001176 logger.Debugw(ctx, "added onu to onugeminfo", log.Fields{"intf": IntfID, "onugem": onuGem})
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001177 return nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301178}
1179
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301180// 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 +05301181func (RsrcMgr *OpenOltResourceMgr) AddUniPortToOnuInfo(ctx context.Context, intfID uint32, onuID uint32, portNo uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301182 var onuGemData []OnuGemInfo
1183 var err error
1184
npujarec5762e2020-01-01 14:08:48 +05301185 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001186 logger.Errorf(ctx, "failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301187 return
1188 }
1189 for idx, onu := range onuGemData {
1190 if onu.OnuID == onuID {
1191 for _, uni := range onu.UniPorts {
1192 if uni == portNo {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001193 logger.Debugw(ctx, "uni already present in onugem info", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301194 return
1195 }
1196 }
1197 onuGemData[idx].UniPorts = append(onuGemData[idx].UniPorts, portNo)
1198 break
1199 }
1200 }
npujarec5762e2020-01-01 14:08:48 +05301201 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301202 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001203 logger.Errorw(ctx, "Failed to add uin port in onugem to kv store", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301204 return
1205 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301206}
1207
Esin Karaman7fb80c22020-07-16 14:23:33 +00001208//UpdateGemPortForPktIn updates gemport for pkt in path to kvstore, path being intfid, onuid, portno, vlan id, priority bit
npujarec5762e2020-01-01 14:08:48 +05301209func (RsrcMgr *OpenOltResourceMgr) UpdateGemPortForPktIn(ctx context.Context, pktIn PacketInInfoKey, gemPort uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301210
Esin Karaman7fb80c22020-07-16 14:23:33 +00001211 path := fmt.Sprintf(OnuPacketINPath, pktIn.IntfID, pktIn.OnuID, pktIn.LogicalPort, pktIn.VlanID, pktIn.Priority)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301212 Value, err := json.Marshal(gemPort)
1213 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001214 logger.Error(ctx, "Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301215 return
1216 }
npujarec5762e2020-01-01 14:08:48 +05301217 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001218 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"path": path, "value": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301219 return
1220 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001221 logger.Debugw(ctx, "added gem packet in successfully", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301222}
1223
Esin Karaman7fb80c22020-07-16 14:23:33 +00001224// GetGemPortFromOnuPktIn gets the gem port from onu pkt in path, path being intfid, onuid, portno, vlan id, priority bit
1225func (RsrcMgr *OpenOltResourceMgr) GetGemPortFromOnuPktIn(ctx context.Context, packetInInfoKey PacketInInfoKey) (uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301226
1227 var Val []byte
1228 var gemPort uint32
1229
Esin Karaman7fb80c22020-07-16 14:23:33 +00001230 path := fmt.Sprintf(OnuPacketINPath, packetInInfoKey.IntfID, packetInInfoKey.OnuID, packetInInfoKey.LogicalPort,
1231 packetInInfoKey.VlanID, packetInInfoKey.Priority)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301232
npujarec5762e2020-01-01 14:08:48 +05301233 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301234 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001235 logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301236 return uint32(0), err
1237 } else if value == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001238 logger.Debugw(ctx, "No pkt in gem found", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301239 return uint32(0), nil
1240 }
1241
1242 if Val, err = kvstore.ToByte(value.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001243 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301244 return uint32(0), err
1245 }
1246 if err = json.Unmarshal(Val, &gemPort); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001247 logger.Error(ctx, "Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301248 return uint32(0), err
1249 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001250 logger.Debugw(ctx, "found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301251
1252 return gemPort, nil
1253}
1254
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001255//DeletePacketInGemPortForOnu deletes the packet-in gemport for ONU
1256func (RsrcMgr *OpenOltResourceMgr) DeletePacketInGemPortForOnu(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301257
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001258 path := fmt.Sprintf(OnuPacketINPathPrefix, intfID, onuID, logicalPort)
1259 value, err := RsrcMgr.KVStore.List(ctx, path)
Esin Karaman7fb80c22020-07-16 14:23:33 +00001260 if err != nil {
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001261 logger.Errorf(ctx, "failed-to-read-value-from-path-%s", path)
1262 return errors.New("failed-to-read-value-from-path-" + path)
Esin Karaman7fb80c22020-07-16 14:23:33 +00001263 }
Esin Karaman7fb80c22020-07-16 14:23:33 +00001264
1265 //remove them one by one
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001266 for key := range value {
1267 // Formulate the right key path suffix ti be delete
Matteo Scandolodfa7a972020-11-06 13:03:40 -08001268 stringToBeReplaced := fmt.Sprintf(BasePathKvStore, RsrcMgr.KVStore.PathPrefix, RsrcMgr.DeviceID) + "/"
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001269 replacedWith := ""
1270 key = strings.Replace(key, stringToBeReplaced, replacedWith, 1)
1271
1272 logger.Debugf(ctx, "removing-key-%s", key)
Esin Karaman7fb80c22020-07-16 14:23:33 +00001273 if err := RsrcMgr.KVStore.Delete(ctx, key); err != nil {
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001274 logger.Errorf(ctx, "failed-to-remove-resource-%s", key)
Esin Karaman7fb80c22020-07-16 14:23:33 +00001275 return err
1276 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301277 }
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001278
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301279 return nil
1280}
1281
1282// DelOnuGemInfoForIntf deletes the onugem info from kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301283func (RsrcMgr *OpenOltResourceMgr) DelOnuGemInfoForIntf(ctx context.Context, intfID uint32) error {
1284 if err := RsrcMgr.ResourceMgrs[intfID].DelOnuGemInfoForIntf(ctx, intfID); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001285 logger.Errorw(ctx, "failed to delete onu gem info for", log.Fields{"intfid": intfID})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301286 return err
1287 }
1288 return nil
1289}
1290
1291//GetNNIFromKVStore gets NNi intfids from kvstore. path being per device
npujarec5762e2020-01-01 14:08:48 +05301292func (RsrcMgr *OpenOltResourceMgr) GetNNIFromKVStore(ctx context.Context) ([]uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301293
1294 var nni []uint32
1295 var Val []byte
1296
Kent Hagermane6ff1012020-07-14 15:07:53 -04001297 path := NnniIntfID
npujarec5762e2020-01-01 14:08:48 +05301298 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301299 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001300 logger.Error(ctx, "failed to get data from kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301301 return nil, err
1302 }
1303 if value != nil {
1304 if Val, err = kvstore.ToByte(value.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001305 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301306 return nil, err
1307 }
1308 if err = json.Unmarshal(Val, &nni); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001309 logger.Error(ctx, "Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301310 return nil, err
1311 }
1312 }
1313 return nni, err
1314}
1315
1316// AddNNIToKVStore adds Nni interfaces to kvstore, path being per device.
npujarec5762e2020-01-01 14:08:48 +05301317func (RsrcMgr *OpenOltResourceMgr) AddNNIToKVStore(ctx context.Context, nniIntf uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301318 var Value []byte
1319
npujarec5762e2020-01-01 14:08:48 +05301320 nni, err := RsrcMgr.GetNNIFromKVStore(ctx)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301321 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001322 logger.Error(ctx, "failed to fetch nni interfaces from kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301323 return err
1324 }
1325
Kent Hagermane6ff1012020-07-14 15:07:53 -04001326 path := NnniIntfID
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301327 nni = append(nni, nniIntf)
1328 Value, err = json.Marshal(nni)
1329 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001330 logger.Error(ctx, "Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301331 }
npujarec5762e2020-01-01 14:08:48 +05301332 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001333 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"path": path, "value": Value})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301334 return err
1335 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001336 logger.Debugw(ctx, "added nni to kv successfully", log.Fields{"path": path, "nni": nniIntf})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301337 return nil
1338}
1339
1340// DelNNiFromKVStore deletes nni interface list from kv store.
npujarec5762e2020-01-01 14:08:48 +05301341func (RsrcMgr *OpenOltResourceMgr) DelNNiFromKVStore(ctx context.Context) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301342
Kent Hagermane6ff1012020-07-14 15:07:53 -04001343 path := NnniIntfID
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301344
npujarec5762e2020-01-01 14:08:48 +05301345 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001346 logger.Errorw(ctx, "Failed to delete nni interfaces from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301347 return err
1348 }
1349 return nil
1350}
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301351
1352//UpdateFlowIDsForGem updates flow id per gemport
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001353func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDsForGem(ctx context.Context, intf uint32, gem uint32, flowIDs []uint64) error {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301354 var val []byte
1355 path := fmt.Sprintf(FlowIDsForGem, intf)
1356
npujarec5762e2020-01-01 14:08:48 +05301357 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301358 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001359 logger.Error(ctx, "Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301360 return err
1361 }
1362 if flowsForGem == nil {
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001363 flowsForGem = make(map[uint32][]uint64)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301364 }
1365 flowsForGem[gem] = flowIDs
1366 val, err = json.Marshal(flowsForGem)
1367 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001368 logger.Error(ctx, "Failed to marshal data", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301369 return err
1370 }
Girish Gowdrab77ded92020-04-08 11:45:05 -07001371
npujarec5762e2020-01-01 14:08:48 +05301372 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001373 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301374 return err
1375 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001376 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 +05301377 return nil
1378}
1379
1380//DeleteFlowIDsForGem deletes the flowID list entry per gem from kvstore.
npujarec5762e2020-01-01 14:08:48 +05301381func (RsrcMgr *OpenOltResourceMgr) DeleteFlowIDsForGem(ctx context.Context, intf uint32, gem uint32) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301382 path := fmt.Sprintf(FlowIDsForGem, intf)
1383 var val []byte
1384
npujarec5762e2020-01-01 14:08:48 +05301385 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301386 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001387 logger.Error(ctx, "Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301388 return
1389 }
1390 if flowsForGem == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001391 logger.Error(ctx, "No flowids found ", log.Fields{"intf": intf, "gemport": gem})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301392 return
1393 }
1394 // once we get the flows per gem map from kv , just delete the gem entry from the map
1395 delete(flowsForGem, gem)
1396 // once gem entry is deleted update the kv store.
1397 val, err = json.Marshal(flowsForGem)
1398 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001399 logger.Error(ctx, "Failed to marshal data", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301400 return
1401 }
Girish Gowdra38d533d2020-03-30 20:38:51 -07001402
npujarec5762e2020-01-01 14:08:48 +05301403 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001404 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301405 }
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301406}
1407
1408//GetFlowIDsGemMapForInterface gets flowids per gemport and interface
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001409func (RsrcMgr *OpenOltResourceMgr) GetFlowIDsGemMapForInterface(ctx context.Context, intf uint32) (map[uint32][]uint64, error) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301410 path := fmt.Sprintf(FlowIDsForGem, intf)
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001411 var flowsForGem map[uint32][]uint64
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301412 var val []byte
npujarec5762e2020-01-01 14:08:48 +05301413 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301414 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001415 logger.Error(ctx, "failed to get data from kv store")
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301416 return nil, err
1417 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001418 if value != nil && value.Value != nil {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301419 if val, err = kvstore.ToByte(value.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001420 logger.Error(ctx, "Failed to convert to byte array ", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301421 return nil, err
1422 }
1423 if err = json.Unmarshal(val, &flowsForGem); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001424 logger.Error(ctx, "Failed to unmarshall", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301425 return nil, err
1426 }
1427 }
1428 return flowsForGem, nil
1429}
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301430
1431//DeleteIntfIDGempMapPath deletes the intf id path used to store flow ids per gem to kvstore.
npujarec5762e2020-01-01 14:08:48 +05301432func (RsrcMgr *OpenOltResourceMgr) DeleteIntfIDGempMapPath(ctx context.Context, intf uint32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301433 path := fmt.Sprintf(FlowIDsForGem, intf)
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001434
npujarec5762e2020-01-01 14:08:48 +05301435 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001436 logger.Errorw(ctx, "Failed to delete nni interfaces from kv store", log.Fields{"path": path})
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301437 }
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301438}
1439
1440// RemoveResourceMap Clear resource map associated with (intfid, onuid, uniid) tuple.
npujarec5762e2020-01-01 14:08:48 +05301441func (RsrcMgr *OpenOltResourceMgr) RemoveResourceMap(ctx context.Context, intfID uint32, onuID int32, uniID int32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301442 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +05301443 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301444}
Esin Karamanccb714b2019-11-29 15:02:06 +00001445
1446//GetMcastQueuePerInterfaceMap gets multicast queue info per pon interface
npujarec5762e2020-01-01 14:08:48 +05301447func (RsrcMgr *OpenOltResourceMgr) GetMcastQueuePerInterfaceMap(ctx context.Context) (map[uint32][]uint32, error) {
Kent Hagermane6ff1012020-07-14 15:07:53 -04001448 path := McastQueuesForIntf
Esin Karamanccb714b2019-11-29 15:02:06 +00001449 var mcastQueueToIntfMap map[uint32][]uint32
1450 var val []byte
1451
npujarec5762e2020-01-01 14:08:48 +05301452 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001453 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001454 logger.Error(ctx, "failed to get data from kv store")
Esin Karamanccb714b2019-11-29 15:02:06 +00001455 return nil, err
1456 }
1457 if kvPair != nil && kvPair.Value != nil {
1458 if val, err = kvstore.ToByte(kvPair.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001459 logger.Error(ctx, "Failed to convert to byte array ", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001460 return nil, err
1461 }
1462 if err = json.Unmarshal(val, &mcastQueueToIntfMap); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001463 logger.Error(ctx, "Failed to unmarshall ", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001464 return nil, err
1465 }
1466 }
1467 return mcastQueueToIntfMap, nil
1468}
1469
1470//AddMcastQueueForIntf adds multicast queue for pon interface
npujarec5762e2020-01-01 14:08:48 +05301471func (RsrcMgr *OpenOltResourceMgr) AddMcastQueueForIntf(ctx context.Context, intf uint32, gem uint32, servicePriority uint32) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001472 var val []byte
Kent Hagermane6ff1012020-07-14 15:07:53 -04001473 path := McastQueuesForIntf
Esin Karamanccb714b2019-11-29 15:02:06 +00001474
npujarec5762e2020-01-01 14:08:48 +05301475 mcastQueues, err := RsrcMgr.GetMcastQueuePerInterfaceMap(ctx)
Esin Karamanccb714b2019-11-29 15:02:06 +00001476 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001477 logger.Errorw(ctx, "Failed to get multicast queue info for interface", log.Fields{"error": err, "intf": intf})
Esin Karamanccb714b2019-11-29 15:02:06 +00001478 return err
1479 }
1480 if mcastQueues == nil {
1481 mcastQueues = make(map[uint32][]uint32)
1482 }
1483 mcastQueues[intf] = []uint32{gem, servicePriority}
1484 if val, err = json.Marshal(mcastQueues); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001485 logger.Errorw(ctx, "Failed to marshal data", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001486 return err
1487 }
npujarec5762e2020-01-01 14:08:48 +05301488 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001489 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Esin Karamanccb714b2019-11-29 15:02:06 +00001490 return err
1491 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001492 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 +00001493 return nil
1494}
1495
1496//AddFlowGroupToKVStore adds flow group into KV store
npujarec5762e2020-01-01 14:08:48 +05301497func (RsrcMgr *OpenOltResourceMgr) AddFlowGroupToKVStore(ctx context.Context, groupEntry *ofp.OfpGroupEntry, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001498 var Value []byte
1499 var err error
1500 var path string
1501 if cached {
1502 path = fmt.Sprintf(FlowGroupCached, groupEntry.Desc.GroupId)
1503 } else {
1504 path = fmt.Sprintf(FlowGroup, groupEntry.Desc.GroupId)
1505 }
1506 //build group info object
1507 var outPorts []uint32
1508 for _, ofBucket := range groupEntry.Desc.Buckets {
1509 for _, ofAction := range ofBucket.Actions {
1510 if ofAction.Type == ofp.OfpActionType_OFPAT_OUTPUT {
1511 outPorts = append(outPorts, ofAction.GetOutput().Port)
1512 }
1513 }
1514 }
1515 groupInfo := GroupInfo{
1516 GroupID: groupEntry.Desc.GroupId,
1517 OutPorts: outPorts,
1518 }
1519
1520 Value, err = json.Marshal(groupInfo)
1521
1522 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001523 logger.Error(ctx, "failed to Marshal flow group object")
Esin Karamanccb714b2019-11-29 15:02:06 +00001524 return err
1525 }
1526
npujarec5762e2020-01-01 14:08:48 +05301527 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001528 logger.Errorf(ctx, "Failed to update resource %s", path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001529 return err
1530 }
1531 return nil
1532}
1533
1534//RemoveFlowGroupFromKVStore removes flow group from KV store
Esin Karamand519bbf2020-07-01 11:16:03 +00001535func (RsrcMgr *OpenOltResourceMgr) RemoveFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001536 var path string
1537 if cached {
1538 path = fmt.Sprintf(FlowGroupCached, groupID)
1539 } else {
1540 path = fmt.Sprintf(FlowGroup, groupID)
1541 }
npujarec5762e2020-01-01 14:08:48 +05301542 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001543 logger.Errorf(ctx, "Failed to remove resource %s due to %s", path, err)
Esin Karamand519bbf2020-07-01 11:16:03 +00001544 return err
Esin Karamanccb714b2019-11-29 15:02:06 +00001545 }
Esin Karamand519bbf2020-07-01 11:16:03 +00001546 return nil
Esin Karamanccb714b2019-11-29 15:02:06 +00001547}
1548
1549//GetFlowGroupFromKVStore fetches flow group from the KV store. Returns (false, {} error) if any problem occurs during
1550//fetching the data. Returns (true, groupInfo, nil) if the group is fetched successfully.
1551// Returns (false, {}, nil) if the group does not exists in the KV store.
npujarec5762e2020-01-01 14:08:48 +05301552func (RsrcMgr *OpenOltResourceMgr) GetFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) (bool, GroupInfo, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001553 var groupInfo GroupInfo
1554 var path string
1555 if cached {
1556 path = fmt.Sprintf(FlowGroupCached, groupID)
1557 } else {
1558 path = fmt.Sprintf(FlowGroup, groupID)
1559 }
npujarec5762e2020-01-01 14:08:48 +05301560 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001561 if err != nil {
1562 return false, groupInfo, err
1563 }
1564 if kvPair != nil && kvPair.Value != nil {
1565 Val, err := kvstore.ToByte(kvPair.Value)
1566 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001567 logger.Errorw(ctx, "Failed to convert flow group into byte array", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001568 return false, groupInfo, err
1569 }
1570 if err = json.Unmarshal(Val, &groupInfo); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001571 logger.Errorw(ctx, "Failed to unmarshal", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001572 return false, groupInfo, err
1573 }
1574 return true, groupInfo, nil
1575 }
1576 return false, groupInfo, nil
1577}
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001578
1579// toByte converts an interface value to a []byte. The interface should either be of
1580// a string type or []byte. Otherwise, an error is returned.
1581func toByte(value interface{}) ([]byte, error) {
1582 switch t := value.(type) {
1583 case []byte:
1584 return value.([]byte), nil
1585 case string:
1586 return []byte(value.(string)), nil
1587 default:
1588 return nil, fmt.Errorf("unexpected-type-%T", t)
1589 }
1590}
1591
1592func checkForFlowIDInList(FlowIDList []uint64, FlowID uint64) (bool, uint64) {
1593 /*
1594 Check for a flow id in a given list of flow IDs.
1595 :param FLowIDList: List of Flow IDs
1596 :param FlowID: Flowd to check in the list
1597 : return true and the index if present false otherwise.
1598 */
1599
1600 for idx := range FlowIDList {
1601 if FlowID == FlowIDList[idx] {
1602 return true, uint64(idx)
1603 }
1604 }
1605 return false, 0
1606}