blob: 58414f03da911635a4835a30a5ea881715b34f22 [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"
Andrea Campanellab83b39d2020-03-30 11:41:16 +020025 "github.com/opencord/voltha-openolt-adapter/internal/pkg/olterrors"
Girish Gowdru0c588b22019-04-23 23:24:56 -040026 "strconv"
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
Esin Karamanccb714b2019-11-29 15:02:06 +000030 "github.com/opencord/voltha-lib-go/v3/pkg/db"
31 "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
32 "github.com/opencord/voltha-lib-go/v3/pkg/log"
33 ponrmgr "github.com/opencord/voltha-lib-go/v3/pkg/ponresourcemanager"
34 ofp "github.com/opencord/voltha-protos/v3/go/openflow_13"
35 "github.com/opencord/voltha-protos/v3/go/openolt"
Abhilash S.L7f17e402019-03-15 17:40:41 +053036)
37
salmansiddiqui7ac62132019-08-22 03:58:50 +000038const (
39 // KvstoreTimeout specifies the time out for KV Store Connection
Neha Sharmacc656962020-04-14 14:26:11 +000040 KvstoreTimeout = 5 * time.Second
salmansiddiqui7ac62132019-08-22 03:58:50 +000041 // BasePathKvStore - service/voltha/openolt/<device_id>
42 BasePathKvStore = "service/voltha/openolt/{%s}"
Gamze Abakafee36392019-10-03 11:17:24 +000043 // TpIDPathSuffix - <(pon_id, onu_id, uni_id)>/tp_id
44 TpIDPathSuffix = "{%d,%d,%d}/tp_id"
45 //MeterIDPathSuffix - <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
46 MeterIDPathSuffix = "{%d,%d,%d}/{%d}/meter_id/{%s}"
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053047 //NnniIntfID - nniintfids
48 NnniIntfID = "nniintfids"
Esin Karaman7fb80c22020-07-16 14:23:33 +000049 //OnuPacketINPathPrefix to be used as prefix for keys to be used to store packet-in gem ports
50 OnuPacketINPathPrefix = "onu_packetin/{%d,%d,%d"
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053051 // OnuPacketINPath path on the kvstore to store packetin gemport,which will be used for packetin, pcketout
Esin Karaman7fb80c22020-07-16 14:23:33 +000052 //format: onu_packetin/<intfid>,<onuid>,<logicalport>,<vlanId>,<priority>
53 OnuPacketINPath = OnuPacketINPathPrefix + ",%d,%d}"
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +053054 //FlowIDsForGem flowids_per_gem/<intfid>
55 FlowIDsForGem = "flowids_per_gem/{%d}"
Esin Karamanccb714b2019-11-29 15:02:06 +000056 //McastQueuesForIntf multicast queues for pon interfaces
57 McastQueuesForIntf = "mcast_qs_for_int"
58 //FlowGroup flow_groups/<flow_group_id>
59 // A group is stored under this path on the KV store after it has been installed to the device.
60 // It should also be deleted after it has been removed from the device accordingly.
61 FlowGroup = "flow_groups/{%d}"
62 //FlowGroupCached flow_groups_cached/<flow_group_id>
63 // When a group add request received, we create the group without setting any members to it since we cannot
64 // set any members to a group until it is associated with a multicast flow. It is a BAL limitation.
65 // When the related multicast flow has been created we perform set members operation for the group.
66 // That is why we need to keep the members of a group until the multicast flow creation request comes.
67 // We preserve the groups under "FlowGroupsCached" directory in the KV store temporarily. Having set members,
68 // we remove the group from the cached group store.
69 FlowGroupCached = "flow_groups_cached/{%d}"
salmansiddiqui7ac62132019-08-22 03:58:50 +000070)
Abhilash S.L7f17e402019-03-15 17:40:41 +053071
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070072// FlowInfo holds the flow information
Abhilash S.L8ee90712019-04-29 16:24:22 +053073type FlowInfo struct {
74 Flow *openolt.Flow
75 FlowStoreCookie uint64
76 FlowCategory string
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053077 LogicalFlowID uint64
78}
79
80// OnuGemInfo holds onu information along with gem port list and uni port list
81type OnuGemInfo struct {
82 OnuID uint32
83 SerialNumber string
84 IntfID uint32
85 GemPorts []uint32
86 UniPorts []uint32
87}
88
89// PacketInInfoKey is the key for packet in gemport
90type PacketInInfoKey struct {
91 IntfID uint32
92 OnuID uint32
93 LogicalPort uint32
Esin Karaman7fb80c22020-07-16 14:23:33 +000094 VlanID uint16
95 Priority uint8
Abhilash S.L8ee90712019-04-29 16:24:22 +053096}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070097
Esin Karamanccb714b2019-11-29 15:02:06 +000098// GroupInfo holds group information
99type GroupInfo struct {
100 GroupID uint32
101 OutPorts []uint32
102}
103
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700104// OpenOltResourceMgr holds resource related information as provided below for each field
Abhilash S.L7f17e402019-03-15 17:40:41 +0530105type OpenOltResourceMgr struct {
Neha Sharma3f221ae2020-04-29 19:02:12 +0000106 DeviceID string // OLT device id
107 Address string // Host and port of the kv store to connect to
108 Args string // args
109 KVStore *db.Backend // backend kv store connection handle
110 DeviceType string
111 DevInfo *openolt.DeviceInfo // device information
Girish Gowdru0c588b22019-04-23 23:24:56 -0400112 // array of pon resource managers per interface technology
113 ResourceMgrs map[uint32]*ponrmgr.PONResourceManager
Girish Gowdra38d533d2020-03-30 20:38:51 -0700114
115 // This protects concurrent gemport_id allocate/delete calls on a per PON port basis
116 GemPortIDMgmtLock []sync.RWMutex
117 // This protects concurrent alloc_id allocate/delete calls on a per PON port basis
118 AllocIDMgmtLock []sync.RWMutex
119 // This protects concurrent onu_id allocate/delete calls on a per PON port basis
120 OnuIDMgmtLock []sync.RWMutex
121 // This protects concurrent flow_id allocate/delete calls. We do not need this on a
122 // per PON port basis as flow IDs are unique across the OLT.
123 FlowIDMgmtLock sync.RWMutex
124
125 // This protects concurrent access to flowids_per_gem info stored on KV store
126 flowIDToGemInfoLock sync.RWMutex
Abhilash S.L7f17e402019-03-15 17:40:41 +0530127}
128
Neha Sharma96b7bf22020-06-15 10:37:32 +0000129func newKVClient(ctx context.Context, storeType string, address string, timeout time.Duration) (kvstore.Client, error) {
130 logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400131 switch storeType {
132 case "consul":
Neha Sharma96b7bf22020-06-15 10:37:32 +0000133 return kvstore.NewConsulClient(ctx, address, timeout)
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
Neha Sharma96b7bf22020-06-15 10:37:32 +0000141func SetKVClient(ctx context.Context, backend string, addr string, DeviceID 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,
155 PathPrefix: fmt.Sprintf(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.
Neha Sharma3f221ae2020-04-29 19:02:12 +0000163func NewResourceMgr(ctx context.Context, deviceID string, KVStoreAddress string, kvStoreType string, deviceType string, devInfo *openolt.DeviceInfo) *OpenOltResourceMgr {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400164 var ResourceMgr OpenOltResourceMgr
Neha Sharma96b7bf22020-06-15 10:37:32 +0000165 logger.Debugf(ctx, "Init new resource manager , address: %s, deviceid: %s", KVStoreAddress, deviceID)
Neha Sharma3f221ae2020-04-29 19:02:12 +0000166 ResourceMgr.Address = KVStoreAddress
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700167 ResourceMgr.DeviceType = deviceType
168 ResourceMgr.DevInfo = devInfo
Girish Gowdra38d533d2020-03-30 20:38:51 -0700169 NumPONPorts := devInfo.GetPonPorts()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530170
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700171 Backend := kvStoreType
Neha Sharma96b7bf22020-06-15 10:37:32 +0000172 ResourceMgr.KVStore = SetKVClient(ctx, Backend, ResourceMgr.Address, deviceID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400173 if ResourceMgr.KVStore == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000174 logger.Error(ctx, "Failed to setup KV store")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400175 }
176 Ranges := make(map[string]*openolt.DeviceInfo_DeviceResourceRanges)
177 RsrcMgrsByTech := make(map[string]*ponrmgr.PONResourceManager)
178 ResourceMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530179
Girish Gowdra38d533d2020-03-30 20:38:51 -0700180 ResourceMgr.AllocIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
181 ResourceMgr.GemPortIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
182 ResourceMgr.OnuIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
183
Girish Gowdru0c588b22019-04-23 23:24:56 -0400184 // TODO self.args = registry('main').get_args()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530185
Girish Gowdru0c588b22019-04-23 23:24:56 -0400186 /*
187 If a legacy driver returns protobuf without any ranges,s synthesize one from
Gamze Abakafee36392019-10-03 11:17:24 +0000188 the legacy global per-device information. This, in theory, is temporary until
Girish Gowdru0c588b22019-04-23 23:24:56 -0400189 the legacy drivers are upgrade to support pool ranges.
190 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700191 if devInfo.Ranges == nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400192 var ranges openolt.DeviceInfo_DeviceResourceRanges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700193 ranges.Technology = devInfo.GetTechnology()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530194
Girish Gowdru0c588b22019-04-23 23:24:56 -0400195 var index uint32
196 for index = 0; index < NumPONPorts; index++ {
197 ranges.IntfIds = append(ranges.IntfIds, index)
198 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530199
Abhilash S.L8ee90712019-04-29 16:24:22 +0530200 var Pool openolt.DeviceInfo_DeviceResourceRanges_Pool
Girish Gowdru0c588b22019-04-23 23:24:56 -0400201 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700202 Pool.Start = devInfo.OnuIdStart
203 Pool.End = devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400204 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
cbabuabf02352019-10-15 13:14:56 +0200205 onuPool := Pool
206 ranges.Pools = append(ranges.Pools, &onuPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530207
Girish Gowdru0c588b22019-04-23 23:24:56 -0400208 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700209 Pool.Start = devInfo.AllocIdStart
210 Pool.End = devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400211 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200212 allocPool := Pool
213 ranges.Pools = append(ranges.Pools, &allocPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530214
Girish Gowdru0c588b22019-04-23 23:24:56 -0400215 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700216 Pool.Start = devInfo.GemportIdStart
217 Pool.End = devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400218 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200219 gemPool := Pool
220 ranges.Pools = append(ranges.Pools, &gemPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530221
Girish Gowdru0c588b22019-04-23 23:24:56 -0400222 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700223 Pool.Start = devInfo.FlowIdStart
224 Pool.End = devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400225 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
Abhilash S.L8ee90712019-04-29 16:24:22 +0530226 ranges.Pools = append(ranges.Pools, &Pool)
227 // Add to device info
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700228 devInfo.Ranges = append(devInfo.Ranges, &ranges)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400229 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530230
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700231 // Create a separate Resource Manager instance for each range. This assumes that
Girish Gowdru0c588b22019-04-23 23:24:56 -0400232 // each technology is represented by only a single range
233 var GlobalPONRsrcMgr *ponrmgr.PONResourceManager
234 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700235 for _, TechRange := range devInfo.Ranges {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400236 technology := TechRange.Technology
Neha Sharma96b7bf22020-06-15 10:37:32 +0000237 logger.Debugf(ctx, "Device info technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400238 Ranges[technology] = TechRange
Neha Sharma96b7bf22020-06-15 10:37:32 +0000239
240 RsrcMgrsByTech[technology], err = ponrmgr.NewPONResourceManager(ctx, technology, deviceType, deviceID,
Neha Sharma3f221ae2020-04-29 19:02:12 +0000241 Backend, ResourceMgr.Address)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400242 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000243 logger.Errorf(ctx, "Failed to create pon resource manager instance for technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400244 return nil
245 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700246 // resource_mgrs_by_tech[technology] = resource_mgr
Girish Gowdru0c588b22019-04-23 23:24:56 -0400247 if GlobalPONRsrcMgr == nil {
248 GlobalPONRsrcMgr = RsrcMgrsByTech[technology]
249 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700250 for _, IntfID := range TechRange.IntfIds {
251 ResourceMgr.ResourceMgrs[uint32(IntfID)] = RsrcMgrsByTech[technology]
Girish Gowdru0c588b22019-04-23 23:24:56 -0400252 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700253 // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
npujarec5762e2020-01-01 14:08:48 +0530254 InitializeDeviceResourceRangeAndPool(ctx, RsrcMgrsByTech[technology], GlobalPONRsrcMgr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700255 TechRange, devInfo)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400256 }
257 // After we have initialized resource ranges, initialize the
258 // resource pools accordingly.
259 for _, PONRMgr := range RsrcMgrsByTech {
npujarec5762e2020-01-01 14:08:48 +0530260 _ = PONRMgr.InitDeviceResourcePool(ctx)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400261 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000262 logger.Info(ctx, "Initialization of resource manager success!")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400263 return &ResourceMgr
Abhilash S.L7f17e402019-03-15 17:40:41 +0530264}
265
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700266// InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply
267// device specific information. If KV doesn't exist
268// or is broader than the device, the device's information will
269// dictate the range limits
npujarec5762e2020-01-01 14:08:48 +0530270func InitializeDeviceResourceRangeAndPool(ctx context.Context, ponRMgr *ponrmgr.PONResourceManager, globalPONRMgr *ponrmgr.PONResourceManager,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700271 techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530272
Girish Gowdru0c588b22019-04-23 23:24:56 -0400273 // init the resource range pool according to the sharing type
Abhilash S.L7f17e402019-03-15 17:40:41 +0530274
Neha Sharma96b7bf22020-06-15 10:37:32 +0000275 logger.Debugf(ctx, "Resource range pool init for technology %s", ponRMgr.Technology)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700276 // first load from KV profiles
npujarec5762e2020-01-01 14:08:48 +0530277 status := ponRMgr.InitResourceRangesFromKVStore(ctx)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700278 if !status {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000279 logger.Debugf(ctx, "Failed to load resource ranges from KV store for tech %s", ponRMgr.Technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400280 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530281
Girish Gowdru0c588b22019-04-23 23:24:56 -0400282 /*
283 Then apply device specific information. If KV doesn't exist
Gamze Abakafee36392019-10-03 11:17:24 +0000284 or is broader than the device, the device's information will
Girish Gowdru0c588b22019-04-23 23:24:56 -0400285 dictate the range limits
286 */
Neha Sharma96b7bf22020-06-15 10:37:32 +0000287 logger.Debugw(ctx, "Using device info to init pon resource ranges", log.Fields{"Tech": ponRMgr.Technology})
Abhilash S.L7f17e402019-03-15 17:40:41 +0530288
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700289 ONUIDStart := devInfo.OnuIdStart
290 ONUIDEnd := devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400291 ONUIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
292 ONUIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700293 AllocIDStart := devInfo.AllocIdStart
294 AllocIDEnd := devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400295 AllocIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
296 AllocIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700297 GEMPortIDStart := devInfo.GemportIdStart
298 GEMPortIDEnd := devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400299 GEMPortIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
300 GEMPortIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700301 FlowIDStart := devInfo.FlowIdStart
302 FlowIDEnd := devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400303 FlowIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
304 FlowIDSharedPoolID := uint32(0)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530305
Girish Gowdru0c588b22019-04-23 23:24:56 -0400306 var FirstIntfPoolID uint32
307 var SharedPoolID uint32
Abhilash S.L7f17e402019-03-15 17:40:41 +0530308
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400309 /*
310 * 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 -0700311 * if resources are shared across interfaces then SharedPoolID is given a positive number.
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400312 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700313 for _, FirstIntfPoolID = range techRange.IntfIds {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400314 // skip the intf id 0
315 if FirstIntfPoolID == 0 {
316 continue
317 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400318 break
319 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530320
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700321 for _, RangePool := range techRange.Pools {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400322 if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400323 SharedPoolID = FirstIntfPoolID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400324 } else if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_SAME_TECH {
325 SharedPoolID = FirstIntfPoolID
326 } else {
327 SharedPoolID = 0
328 }
329 if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID {
330 ONUIDStart = RangePool.Start
331 ONUIDEnd = RangePool.End
332 ONUIDShared = RangePool.Sharing
333 ONUIDSharedPoolID = SharedPoolID
334 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID {
335 AllocIDStart = RangePool.Start
336 AllocIDEnd = RangePool.End
337 AllocIDShared = RangePool.Sharing
338 AllocIDSharedPoolID = SharedPoolID
339 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID {
340 GEMPortIDStart = RangePool.Start
341 GEMPortIDEnd = RangePool.End
342 GEMPortIDShared = RangePool.Sharing
343 GEMPortIDSharedPoolID = SharedPoolID
344 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID {
345 FlowIDStart = RangePool.Start
346 FlowIDEnd = RangePool.End
347 FlowIDShared = RangePool.Sharing
348 FlowIDSharedPoolID = SharedPoolID
349 }
350 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530351
Neha Sharma96b7bf22020-06-15 10:37:32 +0000352 logger.Debugw(ctx, "Device info init", log.Fields{"technology": techRange.Technology,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400353 "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd, "onu_id_shared_pool_id": ONUIDSharedPoolID,
354 "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd,
355 "alloc_id_shared_pool_id": AllocIDSharedPoolID,
356 "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd,
357 "gemport_id_shared_pool_id": GEMPortIDSharedPoolID,
358 "flow_id_start": FlowIDStart,
359 "flow_id_end_idx": FlowIDEnd,
360 "flow_id_shared_pool_id": FlowIDSharedPoolID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700361 "intf_ids": techRange.IntfIds,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400362 "uni_id_start": 0,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700363 "uni_id_end_idx": 1, /*MaxUNIIDperONU()*/
364 })
Abhilash S.L7f17e402019-03-15 17:40:41 +0530365
Neha Sharma96b7bf22020-06-15 10:37:32 +0000366 ponRMgr.InitDefaultPONResourceRanges(ctx, ONUIDStart, ONUIDEnd, ONUIDSharedPoolID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400367 AllocIDStart, AllocIDEnd, AllocIDSharedPoolID,
368 GEMPortIDStart, GEMPortIDEnd, GEMPortIDSharedPoolID,
369 FlowIDStart, FlowIDEnd, FlowIDSharedPoolID, 0, 1,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700370 devInfo.PonPorts, techRange.IntfIds)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530371
Girish Gowdru0c588b22019-04-23 23:24:56 -0400372 // For global sharing, make sure to refresh both local and global resource manager instances' range
Abhilash S.L7f17e402019-03-15 17:40:41 +0530373
Girish Gowdru0c588b22019-04-23 23:24:56 -0400374 if ONUIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000375 globalPONRMgr.UpdateRanges(ctx, ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400376 "", 0, nil)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000377 ponRMgr.UpdateRanges(ctx, ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700378 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400379 }
380 if AllocIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000381 globalPONRMgr.UpdateRanges(ctx, ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400382 "", 0, nil)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530383
Neha Sharma96b7bf22020-06-15 10:37:32 +0000384 ponRMgr.UpdateRanges(ctx, ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700385 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400386 }
387 if GEMPortIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000388 globalPONRMgr.UpdateRanges(ctx, ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400389 "", 0, nil)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000390 ponRMgr.UpdateRanges(ctx, ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700391 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400392 }
393 if FlowIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000394 globalPONRMgr.UpdateRanges(ctx, ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400395 "", 0, nil)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000396 ponRMgr.UpdateRanges(ctx, ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700397 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400398 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530399
Girish Gowdru0c588b22019-04-23 23:24:56 -0400400 // Make sure loaded range fits the platform bit encoding ranges
Neha Sharma96b7bf22020-06-15 10:37:32 +0000401 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 +0530402}
403
Devmalya Paul495b94a2019-08-27 19:42:00 -0400404// Delete clears used resources for the particular olt device being deleted
npujarec5762e2020-01-01 14:08:48 +0530405func (RsrcMgr *OpenOltResourceMgr) Delete(ctx context.Context) error {
Devmalya Paul495b94a2019-08-27 19:42:00 -0400406 /* TODO
407 def __del__(self):
408 self.log.info("clearing-device-resource-pool")
409 for key, resource_mgr in self.resource_mgrs.iteritems():
410 resource_mgr.clear_device_resource_pool()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530411
Devmalya Paul495b94a2019-08-27 19:42:00 -0400412 def assert_pon_id_limit(self, pon_intf_id):
413 assert pon_intf_id in self.resource_mgrs
Abhilash S.L7f17e402019-03-15 17:40:41 +0530414
Devmalya Paul495b94a2019-08-27 19:42:00 -0400415 def assert_onu_id_limit(self, pon_intf_id, onu_id):
416 self.assert_pon_id_limit(pon_intf_id)
417 self.resource_mgrs[pon_intf_id].assert_resource_limits(onu_id, PONResourceManager.ONU_ID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530418
Devmalya Paul495b94a2019-08-27 19:42:00 -0400419 @property
420 def max_uni_id_per_onu(self):
421 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 +0530422
Devmalya Paul495b94a2019-08-27 19:42:00 -0400423 def assert_uni_id_limit(self, pon_intf_id, onu_id, uni_id):
424 self.assert_onu_id_limit(pon_intf_id, onu_id)
425 self.resource_mgrs[pon_intf_id].assert_resource_limits(uni_id, PONResourceManager.UNI_ID)
426 */
427 for _, rsrcMgr := range RsrcMgr.ResourceMgrs {
npujarec5762e2020-01-01 14:08:48 +0530428 if err := rsrcMgr.ClearDeviceResourcePool(ctx); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000429 logger.Debug(ctx, "Failed to clear device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400430 return err
431 }
432 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000433 logger.Debug(ctx, "Cleared device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400434 return nil
435}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530436
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700437// GetONUID returns the available OnuID for the given pon-port
npujarec5762e2020-01-01 14:08:48 +0530438func (RsrcMgr *OpenOltResourceMgr) GetONUID(ctx context.Context, ponIntfID uint32) (uint32, error) {
salmansiddiqui352a45c2019-08-19 10:15:36 +0000439 // Check if Pon Interface ID is present in Resource-manager-map
Girish Gowdrab77ded92020-04-08 11:45:05 -0700440 RsrcMgr.OnuIDMgmtLock[ponIntfID].Lock()
441 defer RsrcMgr.OnuIDMgmtLock[ponIntfID].Unlock()
442
salmansiddiqui352a45c2019-08-19 10:15:36 +0000443 if _, ok := RsrcMgr.ResourceMgrs[ponIntfID]; !ok {
444 err := errors.New("invalid-pon-interface-" + strconv.Itoa(int(ponIntfID)))
445 return 0, err
446 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400447 // Get ONU id for a provided pon interface ID.
npujarec5762e2020-01-01 14:08:48 +0530448 ONUID, err := RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ctx, ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400449 ponrmgr.ONU_ID, 1)
450 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000451 logger.Errorf(ctx, "Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700452 ponIntfID, ponrmgr.ONU_ID)
cbabuabf02352019-10-15 13:14:56 +0200453 return 0, err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400454 }
455 if ONUID != nil {
npujarec5762e2020-01-01 14:08:48 +0530456 RsrcMgr.ResourceMgrs[ponIntfID].InitResourceMap(ctx, fmt.Sprintf("%d,%d", ponIntfID, ONUID[0]))
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700457 return ONUID[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400458 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530459
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700460 return 0, err // return OnuID 0 on error
Abhilash S.L7f17e402019-03-15 17:40:41 +0530461}
462
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700463// GetFlowIDInfo returns the slice of flow info of the given pon-port
464// Note: For flows which trap from the NNI and not really associated with any particular
465// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
npujarec5762e2020-01-01 14:08:48 +0530466func (RsrcMgr *OpenOltResourceMgr) GetFlowIDInfo(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32, flowID uint32) *[]FlowInfo {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530467 var flows []FlowInfo
468
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700469 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530470 if err := RsrcMgr.ResourceMgrs[ponIntfID].GetFlowIDInfo(ctx, FlowPath, flowID, &flows); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000471 logger.Errorw(ctx, "Error while getting flows from KV store", log.Fields{"flowId": flowID})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530472 return nil
473 }
474 if len(flows) == 0 {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000475 logger.Debugw(ctx, "No flowInfo found in KV store", log.Fields{"flowPath": FlowPath})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530476 return nil
477 }
478 return &flows
479}
480
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700481// GetCurrentFlowIDsForOnu fetches flow ID from the resource manager
482// Note: For flows which trap from the NNI and not really associated with any particular
483// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
npujarec5762e2020-01-01 14:08:48 +0530484func (RsrcMgr *OpenOltResourceMgr) GetCurrentFlowIDsForOnu(ctx context.Context, PONIntfID uint32, ONUID int32, UNIID int32) []uint32 {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700485
Abhilash S.L8ee90712019-04-29 16:24:22 +0530486 FlowPath := fmt.Sprintf("%d,%d,%d", PONIntfID, ONUID, UNIID)
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700487 if mgrs, exist := RsrcMgr.ResourceMgrs[PONIntfID]; exist {
npujarec5762e2020-01-01 14:08:48 +0530488 return mgrs.GetCurrentFlowIDsForOnu(ctx, FlowPath)
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700489 }
490 return nil
Abhilash S.L8ee90712019-04-29 16:24:22 +0530491}
492
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700493// UpdateFlowIDInfo updates flow info for the given pon interface, onu id, and uni id
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.
npujarec5762e2020-01-01 14:08:48 +0530496func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDInfo(ctx context.Context, ponIntfID int32, onuID int32, uniID int32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700497 flowID uint32, flowData *[]FlowInfo) error {
498 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530499 return RsrcMgr.ResourceMgrs[uint32(ponIntfID)].UpdateFlowIDInfoForOnu(ctx, FlowPath, flowID, *flowData)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530500}
501
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700502// GetFlowID return flow ID for a given pon interface id, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530503func (RsrcMgr *OpenOltResourceMgr) GetFlowID(ctx context.Context, ponIntfID uint32, ONUID int32, uniID int32,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400504 gemportID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700505 flowStoreCookie uint64,
Gamze Abaka724d0852020-03-18 12:10:24 +0000506 flowCategory string, vlanVid uint32, vlanPcp ...uint32) (uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530507
Girish Gowdru0c588b22019-04-23 23:24:56 -0400508 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700509 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, ONUID, uniID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700510
511 RsrcMgr.FlowIDMgmtLock.Lock()
512 defer RsrcMgr.FlowIDMgmtLock.Unlock()
513
npujarec5762e2020-01-01 14:08:48 +0530514 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(ctx, FlowPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400515 if FlowIDs != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000516 logger.Debugw(ctx, "Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "ONUID": ONUID, "uniID": uniID, "KVpath": FlowPath})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700517 for _, flowID := range FlowIDs {
npujarec5762e2020-01-01 14:08:48 +0530518 FlowInfo := RsrcMgr.GetFlowIDInfo(ctx, ponIntfID, int32(ONUID), int32(uniID), uint32(flowID))
Neha Sharma96b7bf22020-06-15 10:37:32 +0000519 er := getFlowIDFromFlowInfo(ctx, FlowInfo, flowID, gemportID, flowStoreCookie, flowCategory, vlanVid, vlanPcp...)
salmansiddiqui7ac62132019-08-22 03:58:50 +0000520 if er == nil {
Gamze Abaka724d0852020-03-18 12:10:24 +0000521 log.Debugw("Found flowid for the vlan, pcp, and gem",
522 log.Fields{"flowID": flowID, "vlanVid": vlanVid, "vlanPcp": vlanPcp, "gemPortID": gemportID})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000523 return flowID, er
Abhilash S.L8ee90712019-04-29 16:24:22 +0530524 }
525 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400526 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000527 logger.Debug(ctx, "No matching flows with flow cookie or flow category, allocating new flowid")
npujarec5762e2020-01-01 14:08:48 +0530528 FlowIDs, err = RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ctx, ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400529 ponrmgr.FLOW_ID, 1)
530 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000531 logger.Errorf(ctx, "Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700532 ponIntfID, ponrmgr.FLOW_ID)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400533 return uint32(0), err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400534 }
535 if FlowIDs != nil {
npujarec5762e2020-01-01 14:08:48 +0530536 _ = RsrcMgr.ResourceMgrs[ponIntfID].UpdateFlowIDForOnu(ctx, FlowPath, FlowIDs[0], true)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700537 return FlowIDs[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400538 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530539
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700540 return 0, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530541}
542
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700543// GetAllocID return the first Alloc ID for a given pon interface id and onu id and then update the resource map on
544// the KV store with the list of alloc_ids allocated for the pon_intf_onu_id tuple
545// 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 +0530546func (RsrcMgr *OpenOltResourceMgr) GetAllocID(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530547
Girish Gowdru0c588b22019-04-23 23:24:56 -0400548 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700549 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700550
551 RsrcMgr.AllocIDMgmtLock[intfID].Lock()
552 defer RsrcMgr.AllocIDMgmtLock[intfID].Unlock()
553
npujarec5762e2020-01-01 14:08:48 +0530554 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400555 if AllocID != nil {
556 // Since we support only one alloc_id for the ONU at the moment,
557 // return the first alloc_id in the list, if available, for that
558 // ONU.
Neha Sharma96b7bf22020-06-15 10:37:32 +0000559 logger.Debugw(ctx, "Retrieved alloc ID from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400560 return AllocID[0]
561 }
npujarec5762e2020-01-01 14:08:48 +0530562 AllocID, err = RsrcMgr.ResourceMgrs[intfID].GetResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400563 ponrmgr.ALLOC_ID, 1)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530564
Girish Gowdru0c588b22019-04-23 23:24:56 -0400565 if AllocID == nil || err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000566 logger.Error(ctx, "Failed to allocate alloc id")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400567 return 0
568 }
569 // update the resource map on KV store with the list of alloc_id
570 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530571 err = RsrcMgr.ResourceMgrs[intfID].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID, AllocID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400572 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000573 logger.Error(ctx, "Failed to update Alloc ID")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400574 return 0
575 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000576 logger.Debugw(ctx, "Allocated new Tcont from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400577 return AllocID[0]
Abhilash S.L7f17e402019-03-15 17:40:41 +0530578}
579
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700580// UpdateAllocIdsForOnu updates alloc ids in kv store for a given pon interface id, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530581func (RsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ctx context.Context, ponPort uint32, onuID uint32, uniID uint32, allocID []uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530582
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700583 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530584 return RsrcMgr.ResourceMgrs[ponPort].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700585 allocID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530586}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700587
588// GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530589func (RsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(ctx context.Context, intfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700590 uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530591
Girish Gowdru0c588b22019-04-23 23:24:56 -0400592 /* Get gem ports for given pon interface , onu id and uni id. */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530593
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700594 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530595 return RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530596}
597
Gamze Abakafee36392019-10-03 11:17:24 +0000598// GetCurrentAllocIDsForOnu returns alloc ids for given pon interface and onu id
npujarec5762e2020-01-01 14:08:48 +0530599func (RsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDsForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530600
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700601 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530602 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400603 if AllocID != nil {
Gamze Abakafee36392019-10-03 11:17:24 +0000604 return AllocID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400605 }
Gamze Abakafee36392019-10-03 11:17:24 +0000606 return []uint32{}
607}
608
609// RemoveAllocIDForOnu removes the alloc id for given pon interface, onu id, uni id and alloc id
npujarec5762e2020-01-01 14:08:48 +0530610func (RsrcMgr *OpenOltResourceMgr) RemoveAllocIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, allocID uint32) {
611 allocIDs := RsrcMgr.GetCurrentAllocIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000612 for i := 0; i < len(allocIDs); i++ {
613 if allocIDs[i] == allocID {
614 allocIDs = append(allocIDs[:i], allocIDs[i+1:]...)
615 break
616 }
617 }
npujarec5762e2020-01-01 14:08:48 +0530618 err := RsrcMgr.UpdateAllocIdsForOnu(ctx, intfID, onuID, uniID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000619 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000620 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 +0000621 intfID, onuID, uniID, allocID)
622 }
623}
624
625// RemoveGemPortIDForOnu removes the gem port id for given pon interface, onu id, uni id and gem port id
npujarec5762e2020-01-01 14:08:48 +0530626func (RsrcMgr *OpenOltResourceMgr) RemoveGemPortIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, gemPortID uint32) {
627 gemPortIDs := RsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000628 for i := 0; i < len(gemPortIDs); i++ {
629 if gemPortIDs[i] == gemPortID {
630 gemPortIDs = append(gemPortIDs[:i], gemPortIDs[i+1:]...)
631 break
632 }
633 }
npujarec5762e2020-01-01 14:08:48 +0530634 err := RsrcMgr.UpdateGEMPortIDsForOnu(ctx, intfID, onuID, uniID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000635 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000636 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 +0000637 intfID, onuID, uniID, gemPortID)
638 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530639}
640
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700641// UpdateGEMportsPonportToOnuMapOnKVStore updates onu and uni id associated with the gem port to the kv store
642// This stored information is used when packet_indication is received and we need to derive the ONU Id for which
643// the packet arrived based on the pon_intf and gemport available in the packet_indication
npujarec5762e2020-01-01 14:08:48 +0530644func (RsrcMgr *OpenOltResourceMgr) UpdateGEMportsPonportToOnuMapOnKVStore(ctx context.Context, gemPorts []uint32, PonPort uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700645 onuID uint32, uniID uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530646
Girish Gowdru0c588b22019-04-23 23:24:56 -0400647 /* Update onu and uni id associated with the gem port to the kv store. */
648 var IntfGEMPortPath string
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700649 Data := fmt.Sprintf("%d %d", onuID, uniID)
650 for _, GEM := range gemPorts {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400651 IntfGEMPortPath = fmt.Sprintf("%d,%d", PonPort, GEM)
652 Val, err := json.Marshal(Data)
653 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000654 logger.Error(ctx, "failed to Marshal")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400655 return err
656 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700657
npujarec5762e2020-01-01 14:08:48 +0530658 if err = RsrcMgr.KVStore.Put(ctx, IntfGEMPortPath, Val); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000659 logger.Errorf(ctx, "Failed to update resource %s", IntfGEMPortPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400660 return err
661 }
662 }
663 return nil
Abhilash S.L7f17e402019-03-15 17:40:41 +0530664}
665
Gamze Abakafee36392019-10-03 11:17:24 +0000666// RemoveGEMportPonportToOnuMapOnKVStore removes the relationship between the gem port and pon port
npujarec5762e2020-01-01 14:08:48 +0530667func (RsrcMgr *OpenOltResourceMgr) RemoveGEMportPonportToOnuMapOnKVStore(ctx context.Context, GemPort uint32, PonPort uint32) {
Gamze Abakafee36392019-10-03 11:17:24 +0000668 IntfGEMPortPath := fmt.Sprintf("%d,%d", PonPort, GemPort)
npujarec5762e2020-01-01 14:08:48 +0530669 err := RsrcMgr.KVStore.Delete(ctx, IntfGEMPortPath)
Gamze Abakafee36392019-10-03 11:17:24 +0000670 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000671 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 +0000672 }
673}
674
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700675// GetGEMPortID gets gem port id for a particular pon port, onu id and uni id and then update the resource map on
676// the KV store with the list of gemport_id allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530677func (RsrcMgr *OpenOltResourceMgr) GetGEMPortID(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700678 uniID uint32, NumOfPorts uint32) ([]uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530679
Girish Gowdru0c588b22019-04-23 23:24:56 -0400680 /* Get gem port id for a particular pon port, onu id
681 and uni id.
682 */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530683
Girish Gowdru0c588b22019-04-23 23:24:56 -0400684 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700685 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530686
Girish Gowdrab77ded92020-04-08 11:45:05 -0700687 RsrcMgr.GemPortIDMgmtLock[ponPort].Lock()
688 defer RsrcMgr.GemPortIDMgmtLock[ponPort].Unlock()
689
npujarec5762e2020-01-01 14:08:48 +0530690 GEMPortList := RsrcMgr.ResourceMgrs[ponPort].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400691 if GEMPortList != nil {
692 return GEMPortList, nil
693 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530694
npujarec5762e2020-01-01 14:08:48 +0530695 GEMPortList, err = RsrcMgr.ResourceMgrs[ponPort].GetResourceID(ctx, ponPort,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400696 ponrmgr.GEMPORT_ID, NumOfPorts)
697 if err != nil && GEMPortList == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000698 logger.Errorf(ctx, "Failed to get gem port id for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400699 return nil, err
700 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530701
Girish Gowdru0c588b22019-04-23 23:24:56 -0400702 // update the resource map on KV store with the list of gemport_id
703 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530704 err = RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400705 GEMPortList)
706 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000707 logger.Errorf(ctx, "Failed to update GEM ports to kv store for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400708 return nil, err
709 }
npujarec5762e2020-01-01 14:08:48 +0530710 _ = RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(ctx, GEMPortList, ponPort,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700711 onuID, uniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400712 return GEMPortList, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530713}
714
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700715// 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 +0530716func (RsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700717 uniID uint32, GEMPortList []uint32) error {
718 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530719 return RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400720 GEMPortList)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530721
722}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700723
724// FreeonuID releases(make free) onu id for a particular pon-port
npujarec5762e2020-01-01 14:08:48 +0530725func (RsrcMgr *OpenOltResourceMgr) FreeonuID(ctx context.Context, intfID uint32, onuID []uint32) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700726
Girish Gowdra38d533d2020-03-30 20:38:51 -0700727 RsrcMgr.OnuIDMgmtLock[intfID].Lock()
Girish Gowdrab77ded92020-04-08 11:45:05 -0700728 defer RsrcMgr.OnuIDMgmtLock[intfID].Unlock()
729
npujarec5762e2020-01-01 14:08:48 +0530730 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID, ponrmgr.ONU_ID, onuID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530731
Girish Gowdru0c588b22019-04-23 23:24:56 -0400732 /* Free onu id for a particular interface.*/
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700733 var IntfonuID string
734 for _, onu := range onuID {
735 IntfonuID = fmt.Sprintf("%d,%d", intfID, onu)
npujarec5762e2020-01-01 14:08:48 +0530736 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfonuID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400737 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530738}
739
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700740// FreeFlowID returns the free flow id for a given interface, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530741func (RsrcMgr *OpenOltResourceMgr) FreeFlowID(ctx context.Context, IntfID uint32, onuID int32,
Devmalya Paul495b94a2019-08-27 19:42:00 -0400742 uniID int32, FlowID uint32) {
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400743 var IntfONUID string
744 var err error
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530745
Girish Gowdrab77ded92020-04-08 11:45:05 -0700746 RsrcMgr.FlowIDMgmtLock.Lock()
747 defer RsrcMgr.FlowIDMgmtLock.Unlock()
748
749 FlowIds := make([]uint32, 0)
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530750 FlowIds = append(FlowIds, FlowID)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700751 IntfONUID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530752 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(ctx, IntfONUID, FlowID, false)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400753 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000754 logger.Errorw(ctx, "Failed to Update flow id for", log.Fields{"intf": IntfONUID})
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400755 }
npujarec5762e2020-01-01 14:08:48 +0530756 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(ctx, IntfONUID, FlowID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700757
npujarec5762e2020-01-01 14:08:48 +0530758 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.FLOW_ID, FlowIds)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400759}
760
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700761// FreeFlowIDs releases the flow Ids
npujarec5762e2020-01-01 14:08:48 +0530762func (RsrcMgr *OpenOltResourceMgr) FreeFlowIDs(ctx context.Context, IntfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700763 uniID uint32, FlowID []uint32) {
Girish Gowdra38d533d2020-03-30 20:38:51 -0700764 RsrcMgr.FlowIDMgmtLock.Lock()
Girish Gowdrab77ded92020-04-08 11:45:05 -0700765 defer RsrcMgr.FlowIDMgmtLock.Unlock()
766
npujarec5762e2020-01-01 14:08:48 +0530767 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.FLOW_ID, FlowID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530768
Abhilash S.L8ee90712019-04-29 16:24:22 +0530769 var IntfOnuIDUniID string
Girish Gowdru0c588b22019-04-23 23:24:56 -0400770 var err error
771 for _, flow := range FlowID {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700772 IntfOnuIDUniID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530773 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(ctx, IntfOnuIDUniID, flow, false)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400774 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000775 logger.Errorw(ctx, "Failed to Update flow id for", log.Fields{"intf": IntfOnuIDUniID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400776 }
npujarec5762e2020-01-01 14:08:48 +0530777 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(ctx, IntfOnuIDUniID, flow)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400778 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530779}
780
Gamze Abakafee36392019-10-03 11:17:24 +0000781// FreeAllocID frees AllocID on the PON resource pool and also frees the allocID association
782// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530783func (RsrcMgr *OpenOltResourceMgr) FreeAllocID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000784 uniID uint32, allocID uint32) {
Girish Gowdrab77ded92020-04-08 11:45:05 -0700785 RsrcMgr.AllocIDMgmtLock[IntfID].Lock()
786 defer RsrcMgr.AllocIDMgmtLock[IntfID].Unlock()
787
npujarec5762e2020-01-01 14:08:48 +0530788 RsrcMgr.RemoveAllocIDForOnu(ctx, IntfID, onuID, uniID, allocID)
Gamze Abakafee36392019-10-03 11:17:24 +0000789 allocIDs := make([]uint32, 0)
790 allocIDs = append(allocIDs, allocID)
npujarec5762e2020-01-01 14:08:48 +0530791 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.ALLOC_ID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000792}
793
794// FreeGemPortID frees GemPortID on the PON resource pool and also frees the gemPortID association
795// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530796func (RsrcMgr *OpenOltResourceMgr) FreeGemPortID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000797 uniID uint32, gemPortID uint32) {
Girish Gowdrab77ded92020-04-08 11:45:05 -0700798 RsrcMgr.GemPortIDMgmtLock[IntfID].Lock()
799 defer RsrcMgr.GemPortIDMgmtLock[IntfID].Unlock()
800
npujarec5762e2020-01-01 14:08:48 +0530801 RsrcMgr.RemoveGemPortIDForOnu(ctx, IntfID, onuID, uniID, gemPortID)
Gamze Abakafee36392019-10-03 11:17:24 +0000802 gemPortIDs := make([]uint32, 0)
803 gemPortIDs = append(gemPortIDs, gemPortID)
npujarec5762e2020-01-01 14:08:48 +0530804 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.GEMPORT_ID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000805}
806
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700807// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id, and the clears the
808// resource map and the onuID associated with (pon_intf_id, gemport_id) tuple,
npujarec5762e2020-01-01 14:08:48 +0530809func (RsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530810
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700811 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530812
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700813 RsrcMgr.AllocIDMgmtLock[intfID].Lock()
Girish Gowdrab77ded92020-04-08 11:45:05 -0700814 AllocIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700815
npujarec5762e2020-01-01 14:08:48 +0530816 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400817 ponrmgr.ALLOC_ID,
818 AllocIDs)
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700819 RsrcMgr.AllocIDMgmtLock[intfID].Unlock()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530820
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700821 RsrcMgr.GemPortIDMgmtLock[intfID].Lock()
npujarec5762e2020-01-01 14:08:48 +0530822 GEMPortIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
823 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400824 ponrmgr.GEMPORT_ID,
825 GEMPortIDs)
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700826 RsrcMgr.GemPortIDMgmtLock[intfID].Unlock()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530827
Girish Gowdra38d533d2020-03-30 20:38:51 -0700828 RsrcMgr.FlowIDMgmtLock.Lock()
npujarec5762e2020-01-01 14:08:48 +0530829 FlowIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentFlowIDsForOnu(ctx, IntfOnuIDUniID)
830 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400831 ponrmgr.FLOW_ID,
832 FlowIDs)
Girish Gowdra38d533d2020-03-30 20:38:51 -0700833 RsrcMgr.FlowIDMgmtLock.Unlock()
834
Girish Gowdru0c588b22019-04-23 23:24:56 -0400835 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
npujarec5762e2020-01-01 14:08:48 +0530836 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400837 // Clear the ONU Id associated with the (pon_intf_id, gemport_id) tuple.
838 for _, GEM := range GEMPortIDs {
npujarec5762e2020-01-01 14:08:48 +0530839 _ = RsrcMgr.KVStore.Delete(ctx, fmt.Sprintf("%d,%d", intfID, GEM))
Girish Gowdru0c588b22019-04-23 23:24:56 -0400840 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530841}
842
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700843// IsFlowCookieOnKVStore checks if the given flow cookie is present on the kv store
844// Returns true if the flow cookie is found, otherwise it returns false
npujarec5762e2020-01-01 14:08:48 +0530845func (RsrcMgr *OpenOltResourceMgr) IsFlowCookieOnKVStore(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700846 flowStoreCookie uint64) bool {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530847
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700848 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530849 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(ctx, FlowPath)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400850 if FlowIDs != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000851 logger.Debugw(ctx, "Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "onuID": onuID, "uniID": uniID, "KVpath": FlowPath})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700852 for _, flowID := range FlowIDs {
npujarec5762e2020-01-01 14:08:48 +0530853 FlowInfo := RsrcMgr.GetFlowIDInfo(ctx, ponIntfID, int32(onuID), int32(uniID), uint32(flowID))
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400854 if FlowInfo != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000855 logger.Debugw(ctx, "Found flows", log.Fields{"flows": *FlowInfo, "flowId": flowID})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400856 for _, Info := range *FlowInfo {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700857 if Info.FlowStoreCookie == flowStoreCookie {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000858 logger.Debug(ctx, "Found flow matching with flowStore cookie", log.Fields{"flowId": flowID, "flowStoreCookie": flowStoreCookie})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400859 return true
860 }
861 }
862 }
863 }
864 }
865 return false
866}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400867
salmansiddiqui7ac62132019-08-22 03:58:50 +0000868// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000869// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530870func (RsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) []uint32 {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000871 Path := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000872 var Data []uint32
npujarec5762e2020-01-01 14:08:48 +0530873 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400874 if err == nil {
875 if Value != nil {
876 Val, err := kvstore.ToByte(Value.Value)
877 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000878 logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"error": err})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400879 return Data
880 }
881 if err = json.Unmarshal(Val, &Data); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000882 logger.Error(ctx, "Failed to unmarshal", log.Fields{"error": err})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400883 return Data
884 }
885 }
886 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000887 logger.Errorf(ctx, "Failed to get TP id from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400888 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000889 logger.Debugf(ctx, "Getting TP id %d from path %s", Data, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400890 return Data
891
892}
893
Gamze Abakafee36392019-10-03 11:17:24 +0000894// RemoveTechProfileIDsForOnu deletes all tech profile ids from the KV-Store for the given onu based on the path
895// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530896func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDsForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) error {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000897 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
npujarec5762e2020-01-01 14:08:48 +0530898 if err := RsrcMgr.KVStore.Delete(ctx, IntfOnuUniID); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000899 logger.Errorw(ctx, "Failed to delete techprofile id resource in KV store", log.Fields{"path": IntfOnuUniID})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400900 return err
901 }
902 return nil
903}
904
Gamze Abakafee36392019-10-03 11:17:24 +0000905// RemoveTechProfileIDForOnu deletes a specific tech profile id from the KV-Store for the given onu based on the path
906// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530907func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32, TpID uint32) error {
908 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000909 for i, tpIDInList := range tpIDList {
910 if tpIDInList == TpID {
911 tpIDList = append(tpIDList[:i], tpIDList[i+1:]...)
912 }
913 }
914 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
915 Value, err := json.Marshal(tpIDList)
916 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000917 logger.Error(ctx, "failed to Marshal")
Gamze Abakafee36392019-10-03 11:17:24 +0000918 return err
919 }
npujarec5762e2020-01-01 14:08:48 +0530920 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000921 logger.Errorf(ctx, "Failed to update resource %s", IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000922 return err
923 }
924 return err
925}
926
927// UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
928// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530929func (RsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32,
salmansiddiqui7ac62132019-08-22 03:58:50 +0000930 UniID uint32, TpID uint32) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400931 var Value []byte
932 var err error
933
salmansiddiqui7ac62132019-08-22 03:58:50 +0000934 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000935
npujarec5762e2020-01-01 14:08:48 +0530936 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000937 for _, value := range tpIDList {
938 if value == TpID {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000939 logger.Debugf(ctx, "TpID %d is already in tpIdList for the path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000940 return err
941 }
942 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000943 logger.Debugf(ctx, "updating tp id %d on path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000944 tpIDList = append(tpIDList, TpID)
945 Value, err = json.Marshal(tpIDList)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400946 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000947 logger.Error(ctx, "failed to Marshal")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400948 return err
949 }
npujarec5762e2020-01-01 14:08:48 +0530950 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000951 logger.Errorf(ctx, "Failed to update resource %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400952 return err
953 }
954 return err
955}
956
salmansiddiqui7ac62132019-08-22 03:58:50 +0000957// UpdateMeterIDForOnu updates the meter id in the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000958// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +0530959func (RsrcMgr *OpenOltResourceMgr) UpdateMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000960 UniID uint32, TpID uint32, MeterConfig *ofp.OfpMeterConfig) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400961 var Value []byte
962 var err error
963
Gamze Abakafee36392019-10-03 11:17:24 +0000964 IntfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400965 Value, err = json.Marshal(*MeterConfig)
966 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000967 logger.Error(ctx, "failed to Marshal meter config")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400968 return err
969 }
npujarec5762e2020-01-01 14:08:48 +0530970 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000971 logger.Errorf(ctx, "Failed to store meter into KV store %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400972 return err
973 }
974 return err
975}
976
Gamze Abakafee36392019-10-03 11:17:24 +0000977// GetMeterIDForOnu fetches the meter id from the kv store for the given onu based on the path
978// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +0530979func (RsrcMgr *OpenOltResourceMgr) GetMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000980 UniID uint32, TpID uint32) (*ofp.OfpMeterConfig, error) {
981 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400982 var meterConfig ofp.OfpMeterConfig
npujarec5762e2020-01-01 14:08:48 +0530983 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400984 if err == nil {
985 if Value != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000986 logger.Debug(ctx, "Found meter in KV store", log.Fields{"Direction": Direction})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000987 Val, er := kvstore.ToByte(Value.Value)
988 if er != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000989 logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"error": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000990 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400991 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000992 if er = json.Unmarshal(Val, &meterConfig); er != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000993 logger.Error(ctx, "Failed to unmarshal meterconfig", log.Fields{"error": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000994 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400995 }
996 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000997 logger.Debug(ctx, "meter-does-not-exists-in-KVStore")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400998 return nil, err
999 }
1000 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001001 logger.Errorf(ctx, "Failed to get Meter config from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001002
1003 }
1004 return &meterConfig, err
1005}
1006
Gamze Abakafee36392019-10-03 11:17:24 +00001007// RemoveMeterIDForOnu deletes the meter id from the kV-Store for the given onu based on the path
1008// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +05301009func (RsrcMgr *OpenOltResourceMgr) RemoveMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +00001010 UniID uint32, TpID uint32) error {
1011 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
npujarec5762e2020-01-01 14:08:48 +05301012 if err := RsrcMgr.KVStore.Delete(ctx, Path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001013 logger.Errorf(ctx, "Failed to delete meter id %s from kvstore ", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001014 return err
1015 }
1016 return nil
1017}
salmansiddiqui7ac62132019-08-22 03:58:50 +00001018
Neha Sharma96b7bf22020-06-15 10:37:32 +00001019func getFlowIDFromFlowInfo(ctx context.Context, FlowInfo *[]FlowInfo, flowID, gemportID uint32, flowStoreCookie uint64, flowCategory string,
Gamze Abaka724d0852020-03-18 12:10:24 +00001020 vlanVid uint32, vlanPcp ...uint32) error {
salmansiddiqui7ac62132019-08-22 03:58:50 +00001021 if FlowInfo != nil {
1022 for _, Info := range *FlowInfo {
1023 if int32(gemportID) == Info.Flow.GemportId && flowCategory != "" && Info.FlowCategory == flowCategory {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001024 logger.Debug(ctx, "Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
Gamze Abaka724d0852020-03-18 12:10:24 +00001025 if Info.FlowCategory == "HSIA_FLOW" {
1026 if err := checkVlanAndPbitEqualityForFlows(vlanVid, Info, vlanPcp[0]); err == nil {
1027 return nil
1028 }
salmansiddiqui7ac62132019-08-22 03:58:50 +00001029 }
1030 }
1031 if int32(gemportID) == Info.Flow.GemportId && flowStoreCookie != 0 && Info.FlowStoreCookie == flowStoreCookie {
1032 if flowCategory != "" && Info.FlowCategory == flowCategory {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001033 logger.Debug(ctx, "Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001034 return nil
1035 }
1036 }
1037 }
1038 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001039 logger.Debugw(ctx, "the flow can be related to a different service", log.Fields{"flow_info": FlowInfo})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001040 return errors.New("invalid flow-info")
1041}
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301042
Gamze Abaka724d0852020-03-18 12:10:24 +00001043func checkVlanAndPbitEqualityForFlows(vlanVid uint32, Info FlowInfo, vlanPcp uint32) error {
1044 if err := checkVlanEqualityForFlows(vlanVid, Info); err != nil {
1045 return err
1046 }
1047
1048 //flow has remark action and pbits
1049 if Info.Flow.Action.Cmd.RemarkInnerPbits || Info.Flow.Action.Cmd.RemarkOuterPbits {
1050 if vlanPcp == Info.Flow.Action.OPbits || vlanPcp == Info.Flow.Action.IPbits {
1051 return nil
1052 }
1053 } else if vlanPcp == Info.Flow.Classifier.OPbits {
1054 //no remark action but flow has pbits
1055 return nil
1056 } else if vlanPcp == 0xff || Info.Flow.Classifier.OPbits == 0xff {
1057 // no pbit found
1058 return nil
1059 }
1060 return errors.New("not found in terms of pbit equality")
1061}
1062
1063func checkVlanEqualityForFlows(vlanVid uint32, Info FlowInfo) error {
1064 if vlanVid == Info.Flow.Action.OVid || vlanVid == Info.Flow.Classifier.IVid {
1065 return nil
1066 }
1067 return errors.New("not found in terms of vlan_id equality")
1068}
1069
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301070//AddGemToOnuGemInfo adds gemport to onugem info kvstore
npujarec5762e2020-01-01 14:08:48 +05301071func (RsrcMgr *OpenOltResourceMgr) AddGemToOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, gemPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301072 var onuGemData []OnuGemInfo
1073 var err error
1074
npujarec5762e2020-01-01 14:08:48 +05301075 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001076 logger.Errorf(ctx, "failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301077 return err
1078 }
1079 if len(onuGemData) == 0 {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001080 logger.Errorw(ctx, "failed to ger Onuid info ", log.Fields{"intfid": intfID, "onuid": onuID})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301081 return err
1082 }
1083
1084 for idx, onugem := range onuGemData {
1085 if onugem.OnuID == onuID {
1086 for _, gem := range onuGemData[idx].GemPorts {
1087 if gem == gemPort {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001088 logger.Debugw(ctx, "Gem already present in onugem info, skpping addition", log.Fields{"gem": gem})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301089 return nil
1090 }
1091 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001092 logger.Debugw(ctx, "Added gem to onugem info", log.Fields{"gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301093 onuGemData[idx].GemPorts = append(onuGemData[idx].GemPorts, gemPort)
1094 break
1095 }
1096 }
npujarec5762e2020-01-01 14:08:48 +05301097 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301098 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001099 logger.Error(ctx, "Failed to add onugem to kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301100 return err
1101 }
1102 return err
1103}
1104
1105//GetOnuGemInfo gets onu gem info from the kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301106func (RsrcMgr *OpenOltResourceMgr) GetOnuGemInfo(ctx context.Context, IntfID uint32) ([]OnuGemInfo, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301107 var onuGemData []OnuGemInfo
1108
npujarec5762e2020-01-01 14:08:48 +05301109 if err := RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001110 logger.Errorf(ctx, "failed to get onuifo for intfid %d", IntfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301111 return nil, err
1112 }
1113
1114 return onuGemData, nil
1115}
1116
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001117// AddOnuGemInfo adds onu info on to the kvstore per interface
1118func (RsrcMgr *OpenOltResourceMgr) AddOnuGemInfo(ctx context.Context, IntfID uint32, onuGem OnuGemInfo) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301119 var onuGemData []OnuGemInfo
1120 var err error
1121
npujarec5762e2020-01-01 14:08:48 +05301122 if err = RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001123 logger.Errorf(ctx, "failed to get onuifo for intfid %d", IntfID)
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001124 return olterrors.NewErrPersistence("get", "OnuGemInfo", IntfID,
1125 log.Fields{"onuGem": onuGem, "intfID": IntfID}, err)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301126 }
1127 onuGemData = append(onuGemData, onuGem)
npujarec5762e2020-01-01 14:08:48 +05301128 err = RsrcMgr.ResourceMgrs[IntfID].AddOnuGemInfo(ctx, IntfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301129 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001130 logger.Error(ctx, "Failed to add onugem to kv store")
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001131 return olterrors.NewErrPersistence("set", "OnuGemInfo", IntfID,
1132 log.Fields{"onuGemData": onuGemData, "intfID": IntfID}, err)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301133 }
1134
Neha Sharma96b7bf22020-06-15 10:37:32 +00001135 logger.Debugw(ctx, "added onu to onugeminfo", log.Fields{"intf": IntfID, "onugem": onuGem})
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001136 return nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301137}
1138
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301139// 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 +05301140func (RsrcMgr *OpenOltResourceMgr) AddUniPortToOnuInfo(ctx context.Context, intfID uint32, onuID uint32, portNo uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301141 var onuGemData []OnuGemInfo
1142 var err error
1143
npujarec5762e2020-01-01 14:08:48 +05301144 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001145 logger.Errorf(ctx, "failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301146 return
1147 }
1148 for idx, onu := range onuGemData {
1149 if onu.OnuID == onuID {
1150 for _, uni := range onu.UniPorts {
1151 if uni == portNo {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001152 logger.Debugw(ctx, "uni already present in onugem info", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301153 return
1154 }
1155 }
1156 onuGemData[idx].UniPorts = append(onuGemData[idx].UniPorts, portNo)
1157 break
1158 }
1159 }
npujarec5762e2020-01-01 14:08:48 +05301160 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301161 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001162 logger.Errorw(ctx, "Failed to add uin port in onugem to kv store", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301163 return
1164 }
1165 return
1166}
1167
Esin Karaman7fb80c22020-07-16 14:23:33 +00001168//UpdateGemPortForPktIn updates gemport for pkt in path to kvstore, path being intfid, onuid, portno, vlan id, priority bit
npujarec5762e2020-01-01 14:08:48 +05301169func (RsrcMgr *OpenOltResourceMgr) UpdateGemPortForPktIn(ctx context.Context, pktIn PacketInInfoKey, gemPort uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301170
Esin Karaman7fb80c22020-07-16 14:23:33 +00001171 path := fmt.Sprintf(OnuPacketINPath, pktIn.IntfID, pktIn.OnuID, pktIn.LogicalPort, pktIn.VlanID, pktIn.Priority)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301172 Value, err := json.Marshal(gemPort)
1173 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001174 logger.Error(ctx, "Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301175 return
1176 }
npujarec5762e2020-01-01 14:08:48 +05301177 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001178 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"path": path, "value": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301179 return
1180 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001181 logger.Debugw(ctx, "added gem packet in successfully", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301182
1183 return
1184}
1185
Esin Karaman7fb80c22020-07-16 14:23:33 +00001186// GetGemPortFromOnuPktIn gets the gem port from onu pkt in path, path being intfid, onuid, portno, vlan id, priority bit
1187func (RsrcMgr *OpenOltResourceMgr) GetGemPortFromOnuPktIn(ctx context.Context, packetInInfoKey PacketInInfoKey) (uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301188
1189 var Val []byte
1190 var gemPort uint32
1191
Esin Karaman7fb80c22020-07-16 14:23:33 +00001192 path := fmt.Sprintf(OnuPacketINPath, packetInInfoKey.IntfID, packetInInfoKey.OnuID, packetInInfoKey.LogicalPort,
1193 packetInInfoKey.VlanID, packetInInfoKey.Priority)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301194
npujarec5762e2020-01-01 14:08:48 +05301195 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301196 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001197 logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301198 return uint32(0), err
1199 } else if value == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001200 logger.Debugw(ctx, "No pkt in gem found", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301201 return uint32(0), nil
1202 }
1203
1204 if Val, err = kvstore.ToByte(value.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001205 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301206 return uint32(0), err
1207 }
1208 if err = json.Unmarshal(Val, &gemPort); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001209 logger.Error(ctx, "Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301210 return uint32(0), err
1211 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001212 logger.Debugw(ctx, "found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301213
1214 return gemPort, nil
1215}
1216
Esin Karaman7fb80c22020-07-16 14:23:33 +00001217//DelGemPortPktInOfAllServices deletes the gemports from pkt in path for all services
1218func (RsrcMgr *OpenOltResourceMgr) DelGemPortPktInOfAllServices(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301219
Esin Karaman7fb80c22020-07-16 14:23:33 +00001220 //retrieve stored gem port from the store first.
1221 Path := fmt.Sprintf(OnuPacketINPathPrefix, intfID, onuID, logicalPort)
1222 logger.Debugf(ctx, "getting flows from the path:%s", Path)
1223 Value, err := RsrcMgr.KVStore.List(ctx, Path)
1224 if err != nil {
1225 logger.Errorf(ctx, "failed to get flows from kvstore for path %s", Path)
1226 return errors.New("failed to get flows from kvstore for path " + Path)
1227 }
1228 logger.Debugf(ctx, "%d flows retrieved from the path:%s", len(Value), Path)
1229
1230 //remove them one by one
1231 for key := range Value {
1232 if err := RsrcMgr.KVStore.Delete(ctx, key); err != nil {
1233 logger.Errorf(ctx, "Falied to remove resource %s", key)
1234 return err
1235 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301236 }
1237 return nil
1238}
1239
1240// DelOnuGemInfoForIntf deletes the onugem info from kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301241func (RsrcMgr *OpenOltResourceMgr) DelOnuGemInfoForIntf(ctx context.Context, intfID uint32) error {
1242 if err := RsrcMgr.ResourceMgrs[intfID].DelOnuGemInfoForIntf(ctx, intfID); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001243 logger.Errorw(ctx, "failed to delete onu gem info for", log.Fields{"intfid": intfID})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301244 return err
1245 }
1246 return nil
1247}
1248
1249//GetNNIFromKVStore gets NNi intfids from kvstore. path being per device
npujarec5762e2020-01-01 14:08:48 +05301250func (RsrcMgr *OpenOltResourceMgr) GetNNIFromKVStore(ctx context.Context) ([]uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301251
1252 var nni []uint32
1253 var Val []byte
1254
1255 path := fmt.Sprintf(NnniIntfID)
npujarec5762e2020-01-01 14:08:48 +05301256 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301257 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001258 logger.Error(ctx, "failed to get data from kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301259 return nil, err
1260 }
1261 if value != nil {
1262 if Val, err = kvstore.ToByte(value.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001263 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301264 return nil, err
1265 }
1266 if err = json.Unmarshal(Val, &nni); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001267 logger.Error(ctx, "Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301268 return nil, err
1269 }
1270 }
1271 return nni, err
1272}
1273
1274// AddNNIToKVStore adds Nni interfaces to kvstore, path being per device.
npujarec5762e2020-01-01 14:08:48 +05301275func (RsrcMgr *OpenOltResourceMgr) AddNNIToKVStore(ctx context.Context, nniIntf uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301276 var Value []byte
1277
npujarec5762e2020-01-01 14:08:48 +05301278 nni, err := RsrcMgr.GetNNIFromKVStore(ctx)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301279 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001280 logger.Error(ctx, "failed to fetch nni interfaces from kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301281 return err
1282 }
1283
1284 path := fmt.Sprintf(NnniIntfID)
1285 nni = append(nni, nniIntf)
1286 Value, err = json.Marshal(nni)
1287 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001288 logger.Error(ctx, "Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301289 }
npujarec5762e2020-01-01 14:08:48 +05301290 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001291 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"path": path, "value": Value})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301292 return err
1293 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001294 logger.Debugw(ctx, "added nni to kv successfully", log.Fields{"path": path, "nni": nniIntf})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301295 return nil
1296}
1297
1298// DelNNiFromKVStore deletes nni interface list from kv store.
npujarec5762e2020-01-01 14:08:48 +05301299func (RsrcMgr *OpenOltResourceMgr) DelNNiFromKVStore(ctx context.Context) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301300
1301 path := fmt.Sprintf(NnniIntfID)
1302
npujarec5762e2020-01-01 14:08:48 +05301303 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001304 logger.Errorw(ctx, "Failed to delete nni interfaces from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301305 return err
1306 }
1307 return nil
1308}
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301309
1310//UpdateFlowIDsForGem updates flow id per gemport
npujarec5762e2020-01-01 14:08:48 +05301311func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDsForGem(ctx context.Context, intf uint32, gem uint32, flowIDs []uint32) error {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301312 var val []byte
1313 path := fmt.Sprintf(FlowIDsForGem, intf)
1314
npujarec5762e2020-01-01 14:08:48 +05301315 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301316 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001317 logger.Error(ctx, "Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301318 return err
1319 }
1320 if flowsForGem == nil {
1321 flowsForGem = make(map[uint32][]uint32)
1322 }
1323 flowsForGem[gem] = flowIDs
1324 val, err = json.Marshal(flowsForGem)
1325 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001326 logger.Error(ctx, "Failed to marshal data", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301327 return err
1328 }
Girish Gowdrab77ded92020-04-08 11:45:05 -07001329
Girish Gowdra38d533d2020-03-30 20:38:51 -07001330 RsrcMgr.flowIDToGemInfoLock.Lock()
1331 defer RsrcMgr.flowIDToGemInfoLock.Unlock()
npujarec5762e2020-01-01 14:08:48 +05301332 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001333 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301334 return err
1335 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001336 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 +05301337 return nil
1338}
1339
1340//DeleteFlowIDsForGem deletes the flowID list entry per gem from kvstore.
npujarec5762e2020-01-01 14:08:48 +05301341func (RsrcMgr *OpenOltResourceMgr) DeleteFlowIDsForGem(ctx context.Context, intf uint32, gem uint32) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301342 path := fmt.Sprintf(FlowIDsForGem, intf)
1343 var val []byte
1344
npujarec5762e2020-01-01 14:08:48 +05301345 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301346 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001347 logger.Error(ctx, "Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301348 return
1349 }
1350 if flowsForGem == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001351 logger.Error(ctx, "No flowids found ", log.Fields{"intf": intf, "gemport": gem})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301352 return
1353 }
1354 // once we get the flows per gem map from kv , just delete the gem entry from the map
1355 delete(flowsForGem, gem)
1356 // once gem entry is deleted update the kv store.
1357 val, err = json.Marshal(flowsForGem)
1358 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001359 logger.Error(ctx, "Failed to marshal data", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301360 return
1361 }
Girish Gowdra38d533d2020-03-30 20:38:51 -07001362
1363 RsrcMgr.flowIDToGemInfoLock.Lock()
1364 defer RsrcMgr.flowIDToGemInfoLock.Unlock()
npujarec5762e2020-01-01 14:08:48 +05301365 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001366 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301367 return
1368 }
1369 return
1370}
1371
1372//GetFlowIDsGemMapForInterface gets flowids per gemport and interface
npujarec5762e2020-01-01 14:08:48 +05301373func (RsrcMgr *OpenOltResourceMgr) GetFlowIDsGemMapForInterface(ctx context.Context, intf uint32) (map[uint32][]uint32, error) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301374 path := fmt.Sprintf(FlowIDsForGem, intf)
1375 var flowsForGem map[uint32][]uint32
1376 var val []byte
Girish Gowdra38d533d2020-03-30 20:38:51 -07001377 RsrcMgr.flowIDToGemInfoLock.RLock()
npujarec5762e2020-01-01 14:08:48 +05301378 value, err := RsrcMgr.KVStore.Get(ctx, path)
Girish Gowdra38d533d2020-03-30 20:38:51 -07001379 RsrcMgr.flowIDToGemInfoLock.RUnlock()
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301380 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001381 logger.Error(ctx, "failed to get data from kv store")
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301382 return nil, err
1383 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001384 if value != nil && value.Value != nil {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301385 if val, err = kvstore.ToByte(value.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001386 logger.Error(ctx, "Failed to convert to byte array ", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301387 return nil, err
1388 }
1389 if err = json.Unmarshal(val, &flowsForGem); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001390 logger.Error(ctx, "Failed to unmarshall", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301391 return nil, err
1392 }
1393 }
1394 return flowsForGem, nil
1395}
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301396
1397//DeleteIntfIDGempMapPath deletes the intf id path used to store flow ids per gem to kvstore.
npujarec5762e2020-01-01 14:08:48 +05301398func (RsrcMgr *OpenOltResourceMgr) DeleteIntfIDGempMapPath(ctx context.Context, intf uint32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301399 path := fmt.Sprintf(FlowIDsForGem, intf)
Girish Gowdra38d533d2020-03-30 20:38:51 -07001400 RsrcMgr.flowIDToGemInfoLock.Lock()
1401 defer RsrcMgr.flowIDToGemInfoLock.Unlock()
npujarec5762e2020-01-01 14:08:48 +05301402 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001403 logger.Errorw(ctx, "Failed to delete nni interfaces from kv store", log.Fields{"path": path})
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301404 return
1405 }
1406 return
1407}
1408
1409// RemoveResourceMap Clear resource map associated with (intfid, onuid, uniid) tuple.
npujarec5762e2020-01-01 14:08:48 +05301410func (RsrcMgr *OpenOltResourceMgr) RemoveResourceMap(ctx context.Context, intfID uint32, onuID int32, uniID int32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301411 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +05301412 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301413}
Esin Karamanccb714b2019-11-29 15:02:06 +00001414
1415//GetMcastQueuePerInterfaceMap gets multicast queue info per pon interface
npujarec5762e2020-01-01 14:08:48 +05301416func (RsrcMgr *OpenOltResourceMgr) GetMcastQueuePerInterfaceMap(ctx context.Context) (map[uint32][]uint32, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001417 path := fmt.Sprintf(McastQueuesForIntf)
1418 var mcastQueueToIntfMap map[uint32][]uint32
1419 var val []byte
1420
npujarec5762e2020-01-01 14:08:48 +05301421 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001422 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001423 logger.Error(ctx, "failed to get data from kv store")
Esin Karamanccb714b2019-11-29 15:02:06 +00001424 return nil, err
1425 }
1426 if kvPair != nil && kvPair.Value != nil {
1427 if val, err = kvstore.ToByte(kvPair.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001428 logger.Error(ctx, "Failed to convert to byte array ", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001429 return nil, err
1430 }
1431 if err = json.Unmarshal(val, &mcastQueueToIntfMap); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001432 logger.Error(ctx, "Failed to unmarshall ", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001433 return nil, err
1434 }
1435 }
1436 return mcastQueueToIntfMap, nil
1437}
1438
1439//AddMcastQueueForIntf adds multicast queue for pon interface
npujarec5762e2020-01-01 14:08:48 +05301440func (RsrcMgr *OpenOltResourceMgr) AddMcastQueueForIntf(ctx context.Context, intf uint32, gem uint32, servicePriority uint32) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001441 var val []byte
1442 path := fmt.Sprintf(McastQueuesForIntf)
1443
npujarec5762e2020-01-01 14:08:48 +05301444 mcastQueues, err := RsrcMgr.GetMcastQueuePerInterfaceMap(ctx)
Esin Karamanccb714b2019-11-29 15:02:06 +00001445 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001446 logger.Errorw(ctx, "Failed to get multicast queue info for interface", log.Fields{"error": err, "intf": intf})
Esin Karamanccb714b2019-11-29 15:02:06 +00001447 return err
1448 }
1449 if mcastQueues == nil {
1450 mcastQueues = make(map[uint32][]uint32)
1451 }
1452 mcastQueues[intf] = []uint32{gem, servicePriority}
1453 if val, err = json.Marshal(mcastQueues); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001454 logger.Errorw(ctx, "Failed to marshal data", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001455 return err
1456 }
npujarec5762e2020-01-01 14:08:48 +05301457 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001458 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Esin Karamanccb714b2019-11-29 15:02:06 +00001459 return err
1460 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001461 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 +00001462 return nil
1463}
1464
1465//AddFlowGroupToKVStore adds flow group into KV store
npujarec5762e2020-01-01 14:08:48 +05301466func (RsrcMgr *OpenOltResourceMgr) AddFlowGroupToKVStore(ctx context.Context, groupEntry *ofp.OfpGroupEntry, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001467 var Value []byte
1468 var err error
1469 var path string
1470 if cached {
1471 path = fmt.Sprintf(FlowGroupCached, groupEntry.Desc.GroupId)
1472 } else {
1473 path = fmt.Sprintf(FlowGroup, groupEntry.Desc.GroupId)
1474 }
1475 //build group info object
1476 var outPorts []uint32
1477 for _, ofBucket := range groupEntry.Desc.Buckets {
1478 for _, ofAction := range ofBucket.Actions {
1479 if ofAction.Type == ofp.OfpActionType_OFPAT_OUTPUT {
1480 outPorts = append(outPorts, ofAction.GetOutput().Port)
1481 }
1482 }
1483 }
1484 groupInfo := GroupInfo{
1485 GroupID: groupEntry.Desc.GroupId,
1486 OutPorts: outPorts,
1487 }
1488
1489 Value, err = json.Marshal(groupInfo)
1490
1491 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001492 logger.Error(ctx, "failed to Marshal flow group object")
Esin Karamanccb714b2019-11-29 15:02:06 +00001493 return err
1494 }
1495
npujarec5762e2020-01-01 14:08:48 +05301496 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001497 logger.Errorf(ctx, "Failed to update resource %s", path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001498 return err
1499 }
1500 return nil
1501}
1502
1503//RemoveFlowGroupFromKVStore removes flow group from KV store
Esin Karamand519bbf2020-07-01 11:16:03 +00001504func (RsrcMgr *OpenOltResourceMgr) RemoveFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001505 var path string
1506 if cached {
1507 path = fmt.Sprintf(FlowGroupCached, groupID)
1508 } else {
1509 path = fmt.Sprintf(FlowGroup, groupID)
1510 }
npujarec5762e2020-01-01 14:08:48 +05301511 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001512 logger.Errorf(ctx, "Failed to remove resource %s due to %s", path, err)
Esin Karamand519bbf2020-07-01 11:16:03 +00001513 return err
Esin Karamanccb714b2019-11-29 15:02:06 +00001514 }
Esin Karamand519bbf2020-07-01 11:16:03 +00001515 return nil
Esin Karamanccb714b2019-11-29 15:02:06 +00001516}
1517
1518//GetFlowGroupFromKVStore fetches flow group from the KV store. Returns (false, {} error) if any problem occurs during
1519//fetching the data. Returns (true, groupInfo, nil) if the group is fetched successfully.
1520// Returns (false, {}, nil) if the group does not exists in the KV store.
npujarec5762e2020-01-01 14:08:48 +05301521func (RsrcMgr *OpenOltResourceMgr) GetFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) (bool, GroupInfo, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001522 var groupInfo GroupInfo
1523 var path string
1524 if cached {
1525 path = fmt.Sprintf(FlowGroupCached, groupID)
1526 } else {
1527 path = fmt.Sprintf(FlowGroup, groupID)
1528 }
npujarec5762e2020-01-01 14:08:48 +05301529 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001530 if err != nil {
1531 return false, groupInfo, err
1532 }
1533 if kvPair != nil && kvPair.Value != nil {
1534 Val, err := kvstore.ToByte(kvPair.Value)
1535 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001536 logger.Errorw(ctx, "Failed to convert flow group into byte array", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001537 return false, groupInfo, err
1538 }
1539 if err = json.Unmarshal(Val, &groupInfo); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001540 logger.Errorw(ctx, "Failed to unmarshal", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001541 return false, groupInfo, err
1542 }
1543 return true, groupInfo, nil
1544 }
1545 return false, groupInfo, nil
1546}