blob: 8ab03f0c2f32d3eecb26ae335040e5f4182207da [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"
49 // OnuPacketINPath path on the kvstore to store packetin gemport,which will be used for packetin, pcketout
50 //format: onu_packetin/<intfid>,<onuid>,<logicalport>
51 OnuPacketINPath = "onu_packetin/{%d,%d,%d}"
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +053052 //FlowIDsForGem flowids_per_gem/<intfid>
53 FlowIDsForGem = "flowids_per_gem/{%d}"
Esin Karamanccb714b2019-11-29 15:02:06 +000054 //McastQueuesForIntf multicast queues for pon interfaces
55 McastQueuesForIntf = "mcast_qs_for_int"
56 //FlowGroup flow_groups/<flow_group_id>
57 // A group is stored under this path on the KV store after it has been installed to the device.
58 // It should also be deleted after it has been removed from the device accordingly.
59 FlowGroup = "flow_groups/{%d}"
60 //FlowGroupCached flow_groups_cached/<flow_group_id>
61 // When a group add request received, we create the group without setting any members to it since we cannot
62 // set any members to a group until it is associated with a multicast flow. It is a BAL limitation.
63 // When the related multicast flow has been created we perform set members operation for the group.
64 // That is why we need to keep the members of a group until the multicast flow creation request comes.
65 // We preserve the groups under "FlowGroupsCached" directory in the KV store temporarily. Having set members,
66 // we remove the group from the cached group store.
67 FlowGroupCached = "flow_groups_cached/{%d}"
salmansiddiqui7ac62132019-08-22 03:58:50 +000068)
Abhilash S.L7f17e402019-03-15 17:40:41 +053069
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070070// FlowInfo holds the flow information
Abhilash S.L8ee90712019-04-29 16:24:22 +053071type FlowInfo struct {
72 Flow *openolt.Flow
73 FlowStoreCookie uint64
74 FlowCategory string
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053075 LogicalFlowID uint64
76}
77
78// OnuGemInfo holds onu information along with gem port list and uni port list
79type OnuGemInfo struct {
80 OnuID uint32
81 SerialNumber string
82 IntfID uint32
83 GemPorts []uint32
84 UniPorts []uint32
85}
86
87// PacketInInfoKey is the key for packet in gemport
88type PacketInInfoKey struct {
89 IntfID uint32
90 OnuID uint32
91 LogicalPort uint32
Abhilash S.L8ee90712019-04-29 16:24:22 +053092}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070093
Esin Karamanccb714b2019-11-29 15:02:06 +000094// GroupInfo holds group information
95type GroupInfo struct {
96 GroupID uint32
97 OutPorts []uint32
98}
99
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700100// OpenOltResourceMgr holds resource related information as provided below for each field
Abhilash S.L7f17e402019-03-15 17:40:41 +0530101type OpenOltResourceMgr struct {
Neha Sharma3f221ae2020-04-29 19:02:12 +0000102 DeviceID string // OLT device id
103 Address string // Host and port of the kv store to connect to
104 Args string // args
105 KVStore *db.Backend // backend kv store connection handle
106 DeviceType string
107 DevInfo *openolt.DeviceInfo // device information
Girish Gowdru0c588b22019-04-23 23:24:56 -0400108 // array of pon resource managers per interface technology
109 ResourceMgrs map[uint32]*ponrmgr.PONResourceManager
Girish Gowdra38d533d2020-03-30 20:38:51 -0700110
111 // This protects concurrent gemport_id allocate/delete calls on a per PON port basis
112 GemPortIDMgmtLock []sync.RWMutex
113 // This protects concurrent alloc_id allocate/delete calls on a per PON port basis
114 AllocIDMgmtLock []sync.RWMutex
115 // This protects concurrent onu_id allocate/delete calls on a per PON port basis
116 OnuIDMgmtLock []sync.RWMutex
117 // This protects concurrent flow_id allocate/delete calls. We do not need this on a
118 // per PON port basis as flow IDs are unique across the OLT.
119 FlowIDMgmtLock sync.RWMutex
120
121 // This protects concurrent access to flowids_per_gem info stored on KV store
122 flowIDToGemInfoLock sync.RWMutex
Abhilash S.L7f17e402019-03-15 17:40:41 +0530123}
124
Neha Sharmacc656962020-04-14 14:26:11 +0000125func newKVClient(storeType string, address string, timeout time.Duration) (kvstore.Client, error) {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000126 logger.Infow("kv-store-type", log.Fields{"store": storeType})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400127 switch storeType {
128 case "consul":
Neha Sharmacc656962020-04-14 14:26:11 +0000129 return kvstore.NewConsulClient(address, timeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400130 case "etcd":
Neha Sharmacc656962020-04-14 14:26:11 +0000131 return kvstore.NewEtcdClient(address, timeout, log.FatalLevel)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400132 }
133 return nil, errors.New("unsupported-kv-store")
Abhilash S.L7f17e402019-03-15 17:40:41 +0530134}
135
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700136// SetKVClient sets the KV client and return a kv backend
Neha Sharma3f221ae2020-04-29 19:02:12 +0000137func SetKVClient(backend string, addr string, DeviceID string) *db.Backend {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400138 // TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
139 // issue between kv store and backend , core is not calling NewBackend directly
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700140 kvClient, err := newKVClient(backend, addr, KvstoreTimeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400141 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000142 logger.Fatalw("Failed to init KV client\n", log.Fields{"err": err})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400143 return nil
144 }
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700145
sbarbaria8910ba2019-11-05 10:12:23 -0500146 kvbackend := &db.Backend{
Girish Gowdru0c588b22019-04-23 23:24:56 -0400147 Client: kvClient,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700148 StoreType: backend,
Neha Sharma3f221ae2020-04-29 19:02:12 +0000149 Address: addr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700150 Timeout: KvstoreTimeout,
151 PathPrefix: fmt.Sprintf(BasePathKvStore, DeviceID)}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530152
Girish Gowdru0c588b22019-04-23 23:24:56 -0400153 return kvbackend
Abhilash S.L7f17e402019-03-15 17:40:41 +0530154}
155
Gamze Abakafee36392019-10-03 11:17:24 +0000156// NewResourceMgr init a New resource manager instance which in turn instantiates pon resource manager
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700157// instances according to technology. Initializes the default resource ranges for all
158// the resources.
Neha Sharma3f221ae2020-04-29 19:02:12 +0000159func NewResourceMgr(ctx context.Context, deviceID string, KVStoreAddress string, kvStoreType string, deviceType string, devInfo *openolt.DeviceInfo) *OpenOltResourceMgr {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400160 var ResourceMgr OpenOltResourceMgr
Neha Sharma3f221ae2020-04-29 19:02:12 +0000161 logger.Debugf("Init new resource manager , address: %s, deviceid: %s", KVStoreAddress, deviceID)
162 ResourceMgr.Address = KVStoreAddress
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700163 ResourceMgr.DeviceType = deviceType
164 ResourceMgr.DevInfo = devInfo
Girish Gowdra38d533d2020-03-30 20:38:51 -0700165 NumPONPorts := devInfo.GetPonPorts()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530166
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700167 Backend := kvStoreType
Neha Sharma3f221ae2020-04-29 19:02:12 +0000168 ResourceMgr.KVStore = SetKVClient(Backend, ResourceMgr.Address, deviceID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400169 if ResourceMgr.KVStore == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000170 logger.Error("Failed to setup KV store")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400171 }
172 Ranges := make(map[string]*openolt.DeviceInfo_DeviceResourceRanges)
173 RsrcMgrsByTech := make(map[string]*ponrmgr.PONResourceManager)
174 ResourceMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530175
Girish Gowdra38d533d2020-03-30 20:38:51 -0700176 ResourceMgr.AllocIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
177 ResourceMgr.GemPortIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
178 ResourceMgr.OnuIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
179
Girish Gowdru0c588b22019-04-23 23:24:56 -0400180 // TODO self.args = registry('main').get_args()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530181
Girish Gowdru0c588b22019-04-23 23:24:56 -0400182 /*
183 If a legacy driver returns protobuf without any ranges,s synthesize one from
Gamze Abakafee36392019-10-03 11:17:24 +0000184 the legacy global per-device information. This, in theory, is temporary until
Girish Gowdru0c588b22019-04-23 23:24:56 -0400185 the legacy drivers are upgrade to support pool ranges.
186 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700187 if devInfo.Ranges == nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400188 var ranges openolt.DeviceInfo_DeviceResourceRanges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700189 ranges.Technology = devInfo.GetTechnology()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530190
Girish Gowdru0c588b22019-04-23 23:24:56 -0400191 var index uint32
192 for index = 0; index < NumPONPorts; index++ {
193 ranges.IntfIds = append(ranges.IntfIds, index)
194 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530195
Abhilash S.L8ee90712019-04-29 16:24:22 +0530196 var Pool openolt.DeviceInfo_DeviceResourceRanges_Pool
Girish Gowdru0c588b22019-04-23 23:24:56 -0400197 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700198 Pool.Start = devInfo.OnuIdStart
199 Pool.End = devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400200 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
cbabuabf02352019-10-15 13:14:56 +0200201 onuPool := Pool
202 ranges.Pools = append(ranges.Pools, &onuPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530203
Girish Gowdru0c588b22019-04-23 23:24:56 -0400204 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700205 Pool.Start = devInfo.AllocIdStart
206 Pool.End = devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400207 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200208 allocPool := Pool
209 ranges.Pools = append(ranges.Pools, &allocPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530210
Girish Gowdru0c588b22019-04-23 23:24:56 -0400211 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700212 Pool.Start = devInfo.GemportIdStart
213 Pool.End = devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400214 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200215 gemPool := Pool
216 ranges.Pools = append(ranges.Pools, &gemPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530217
Girish Gowdru0c588b22019-04-23 23:24:56 -0400218 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700219 Pool.Start = devInfo.FlowIdStart
220 Pool.End = devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400221 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
Abhilash S.L8ee90712019-04-29 16:24:22 +0530222 ranges.Pools = append(ranges.Pools, &Pool)
223 // Add to device info
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700224 devInfo.Ranges = append(devInfo.Ranges, &ranges)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400225 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530226
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700227 // Create a separate Resource Manager instance for each range. This assumes that
Girish Gowdru0c588b22019-04-23 23:24:56 -0400228 // each technology is represented by only a single range
229 var GlobalPONRsrcMgr *ponrmgr.PONResourceManager
230 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700231 for _, TechRange := range devInfo.Ranges {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400232 technology := TechRange.Technology
Girish Kumar2ad402b2020-03-20 19:45:12 +0000233 logger.Debugf("Device info technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400234 Ranges[technology] = TechRange
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700235 RsrcMgrsByTech[technology], err = ponrmgr.NewPONResourceManager(technology, deviceType, deviceID,
Neha Sharma3f221ae2020-04-29 19:02:12 +0000236 Backend, ResourceMgr.Address)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400237 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000238 logger.Errorf("Failed to create pon resource manager instance for technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400239 return nil
240 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700241 // resource_mgrs_by_tech[technology] = resource_mgr
Girish Gowdru0c588b22019-04-23 23:24:56 -0400242 if GlobalPONRsrcMgr == nil {
243 GlobalPONRsrcMgr = RsrcMgrsByTech[technology]
244 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700245 for _, IntfID := range TechRange.IntfIds {
246 ResourceMgr.ResourceMgrs[uint32(IntfID)] = RsrcMgrsByTech[technology]
Girish Gowdru0c588b22019-04-23 23:24:56 -0400247 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700248 // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
npujarec5762e2020-01-01 14:08:48 +0530249 InitializeDeviceResourceRangeAndPool(ctx, RsrcMgrsByTech[technology], GlobalPONRsrcMgr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700250 TechRange, devInfo)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400251 }
252 // After we have initialized resource ranges, initialize the
253 // resource pools accordingly.
254 for _, PONRMgr := range RsrcMgrsByTech {
npujarec5762e2020-01-01 14:08:48 +0530255 _ = PONRMgr.InitDeviceResourcePool(ctx)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400256 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000257 logger.Info("Initialization of resource manager success!")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400258 return &ResourceMgr
Abhilash S.L7f17e402019-03-15 17:40:41 +0530259}
260
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700261// InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply
262// device specific information. If KV doesn't exist
263// or is broader than the device, the device's information will
264// dictate the range limits
npujarec5762e2020-01-01 14:08:48 +0530265func InitializeDeviceResourceRangeAndPool(ctx context.Context, ponRMgr *ponrmgr.PONResourceManager, globalPONRMgr *ponrmgr.PONResourceManager,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700266 techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530267
Girish Gowdru0c588b22019-04-23 23:24:56 -0400268 // init the resource range pool according to the sharing type
Abhilash S.L7f17e402019-03-15 17:40:41 +0530269
Girish Kumar2ad402b2020-03-20 19:45:12 +0000270 logger.Debugf("Resource range pool init for technology %s", ponRMgr.Technology)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700271 // first load from KV profiles
npujarec5762e2020-01-01 14:08:48 +0530272 status := ponRMgr.InitResourceRangesFromKVStore(ctx)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700273 if !status {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000274 logger.Debugf("Failed to load resource ranges from KV store for tech %s", ponRMgr.Technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400275 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530276
Girish Gowdru0c588b22019-04-23 23:24:56 -0400277 /*
278 Then apply device specific information. If KV doesn't exist
Gamze Abakafee36392019-10-03 11:17:24 +0000279 or is broader than the device, the device's information will
Girish Gowdru0c588b22019-04-23 23:24:56 -0400280 dictate the range limits
281 */
Girish Kumar2ad402b2020-03-20 19:45:12 +0000282 logger.Debugw("Using device info to init pon resource ranges", log.Fields{"Tech": ponRMgr.Technology})
Abhilash S.L7f17e402019-03-15 17:40:41 +0530283
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700284 ONUIDStart := devInfo.OnuIdStart
285 ONUIDEnd := devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400286 ONUIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
287 ONUIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700288 AllocIDStart := devInfo.AllocIdStart
289 AllocIDEnd := devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400290 AllocIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
291 AllocIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700292 GEMPortIDStart := devInfo.GemportIdStart
293 GEMPortIDEnd := devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400294 GEMPortIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
295 GEMPortIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700296 FlowIDStart := devInfo.FlowIdStart
297 FlowIDEnd := devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400298 FlowIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
299 FlowIDSharedPoolID := uint32(0)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530300
Girish Gowdru0c588b22019-04-23 23:24:56 -0400301 var FirstIntfPoolID uint32
302 var SharedPoolID uint32
Abhilash S.L7f17e402019-03-15 17:40:41 +0530303
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400304 /*
305 * 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 -0700306 * if resources are shared across interfaces then SharedPoolID is given a positive number.
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400307 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700308 for _, FirstIntfPoolID = range techRange.IntfIds {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400309 // skip the intf id 0
310 if FirstIntfPoolID == 0 {
311 continue
312 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400313 break
314 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530315
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700316 for _, RangePool := range techRange.Pools {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400317 if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400318 SharedPoolID = FirstIntfPoolID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400319 } else if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_SAME_TECH {
320 SharedPoolID = FirstIntfPoolID
321 } else {
322 SharedPoolID = 0
323 }
324 if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID {
325 ONUIDStart = RangePool.Start
326 ONUIDEnd = RangePool.End
327 ONUIDShared = RangePool.Sharing
328 ONUIDSharedPoolID = SharedPoolID
329 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID {
330 AllocIDStart = RangePool.Start
331 AllocIDEnd = RangePool.End
332 AllocIDShared = RangePool.Sharing
333 AllocIDSharedPoolID = SharedPoolID
334 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID {
335 GEMPortIDStart = RangePool.Start
336 GEMPortIDEnd = RangePool.End
337 GEMPortIDShared = RangePool.Sharing
338 GEMPortIDSharedPoolID = SharedPoolID
339 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID {
340 FlowIDStart = RangePool.Start
341 FlowIDEnd = RangePool.End
342 FlowIDShared = RangePool.Sharing
343 FlowIDSharedPoolID = SharedPoolID
344 }
345 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530346
Girish Kumar2ad402b2020-03-20 19:45:12 +0000347 logger.Debugw("Device info init", log.Fields{"technology": techRange.Technology,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400348 "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd, "onu_id_shared_pool_id": ONUIDSharedPoolID,
349 "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd,
350 "alloc_id_shared_pool_id": AllocIDSharedPoolID,
351 "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd,
352 "gemport_id_shared_pool_id": GEMPortIDSharedPoolID,
353 "flow_id_start": FlowIDStart,
354 "flow_id_end_idx": FlowIDEnd,
355 "flow_id_shared_pool_id": FlowIDSharedPoolID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700356 "intf_ids": techRange.IntfIds,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400357 "uni_id_start": 0,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700358 "uni_id_end_idx": 1, /*MaxUNIIDperONU()*/
359 })
Abhilash S.L7f17e402019-03-15 17:40:41 +0530360
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700361 ponRMgr.InitDefaultPONResourceRanges(ONUIDStart, ONUIDEnd, ONUIDSharedPoolID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400362 AllocIDStart, AllocIDEnd, AllocIDSharedPoolID,
363 GEMPortIDStart, GEMPortIDEnd, GEMPortIDSharedPoolID,
364 FlowIDStart, FlowIDEnd, FlowIDSharedPoolID, 0, 1,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700365 devInfo.PonPorts, techRange.IntfIds)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530366
Girish Gowdru0c588b22019-04-23 23:24:56 -0400367 // For global sharing, make sure to refresh both local and global resource manager instances' range
Abhilash S.L7f17e402019-03-15 17:40:41 +0530368
Girish Gowdru0c588b22019-04-23 23:24:56 -0400369 if ONUIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700370 globalPONRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400371 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700372 ponRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
373 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400374 }
375 if AllocIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700376 globalPONRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400377 "", 0, nil)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530378
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700379 ponRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
380 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400381 }
382 if GEMPortIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700383 globalPONRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400384 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700385 ponRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
386 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400387 }
388 if FlowIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700389 globalPONRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400390 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700391 ponRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
392 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400393 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530394
Girish Gowdru0c588b22019-04-23 23:24:56 -0400395 // Make sure loaded range fits the platform bit encoding ranges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700396 ponRMgr.UpdateRanges(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 +0530397}
398
Devmalya Paul495b94a2019-08-27 19:42:00 -0400399// Delete clears used resources for the particular olt device being deleted
npujarec5762e2020-01-01 14:08:48 +0530400func (RsrcMgr *OpenOltResourceMgr) Delete(ctx context.Context) error {
Devmalya Paul495b94a2019-08-27 19:42:00 -0400401 /* TODO
402 def __del__(self):
403 self.log.info("clearing-device-resource-pool")
404 for key, resource_mgr in self.resource_mgrs.iteritems():
405 resource_mgr.clear_device_resource_pool()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530406
Devmalya Paul495b94a2019-08-27 19:42:00 -0400407 def assert_pon_id_limit(self, pon_intf_id):
408 assert pon_intf_id in self.resource_mgrs
Abhilash S.L7f17e402019-03-15 17:40:41 +0530409
Devmalya Paul495b94a2019-08-27 19:42:00 -0400410 def assert_onu_id_limit(self, pon_intf_id, onu_id):
411 self.assert_pon_id_limit(pon_intf_id)
412 self.resource_mgrs[pon_intf_id].assert_resource_limits(onu_id, PONResourceManager.ONU_ID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530413
Devmalya Paul495b94a2019-08-27 19:42:00 -0400414 @property
415 def max_uni_id_per_onu(self):
416 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 +0530417
Devmalya Paul495b94a2019-08-27 19:42:00 -0400418 def assert_uni_id_limit(self, pon_intf_id, onu_id, uni_id):
419 self.assert_onu_id_limit(pon_intf_id, onu_id)
420 self.resource_mgrs[pon_intf_id].assert_resource_limits(uni_id, PONResourceManager.UNI_ID)
421 */
422 for _, rsrcMgr := range RsrcMgr.ResourceMgrs {
npujarec5762e2020-01-01 14:08:48 +0530423 if err := rsrcMgr.ClearDeviceResourcePool(ctx); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000424 logger.Debug("Failed to clear device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400425 return err
426 }
427 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000428 logger.Debug("Cleared device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400429 return nil
430}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530431
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700432// GetONUID returns the available OnuID for the given pon-port
npujarec5762e2020-01-01 14:08:48 +0530433func (RsrcMgr *OpenOltResourceMgr) GetONUID(ctx context.Context, ponIntfID uint32) (uint32, error) {
salmansiddiqui352a45c2019-08-19 10:15:36 +0000434 // Check if Pon Interface ID is present in Resource-manager-map
Girish Gowdrab77ded92020-04-08 11:45:05 -0700435 RsrcMgr.OnuIDMgmtLock[ponIntfID].Lock()
436 defer RsrcMgr.OnuIDMgmtLock[ponIntfID].Unlock()
437
salmansiddiqui352a45c2019-08-19 10:15:36 +0000438 if _, ok := RsrcMgr.ResourceMgrs[ponIntfID]; !ok {
439 err := errors.New("invalid-pon-interface-" + strconv.Itoa(int(ponIntfID)))
440 return 0, err
441 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400442 // Get ONU id for a provided pon interface ID.
npujarec5762e2020-01-01 14:08:48 +0530443 ONUID, err := RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ctx, ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400444 ponrmgr.ONU_ID, 1)
445 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000446 logger.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700447 ponIntfID, ponrmgr.ONU_ID)
cbabuabf02352019-10-15 13:14:56 +0200448 return 0, err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400449 }
450 if ONUID != nil {
npujarec5762e2020-01-01 14:08:48 +0530451 RsrcMgr.ResourceMgrs[ponIntfID].InitResourceMap(ctx, fmt.Sprintf("%d,%d", ponIntfID, ONUID[0]))
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700452 return ONUID[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400453 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530454
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700455 return 0, err // return OnuID 0 on error
Abhilash S.L7f17e402019-03-15 17:40:41 +0530456}
457
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700458// GetFlowIDInfo returns the slice of flow info of the given pon-port
459// Note: For flows which trap from the NNI and not really associated with any particular
460// 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 +0530461func (RsrcMgr *OpenOltResourceMgr) GetFlowIDInfo(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32, flowID uint32) *[]FlowInfo {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530462 var flows []FlowInfo
463
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700464 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530465 if err := RsrcMgr.ResourceMgrs[ponIntfID].GetFlowIDInfo(ctx, FlowPath, flowID, &flows); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000466 logger.Errorw("Error while getting flows from KV store", log.Fields{"flowId": flowID})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530467 return nil
468 }
469 if len(flows) == 0 {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000470 logger.Debugw("No flowInfo found in KV store", log.Fields{"flowPath": FlowPath})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530471 return nil
472 }
473 return &flows
474}
475
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700476// GetCurrentFlowIDsForOnu fetches flow ID from the resource manager
477// Note: For flows which trap from the NNI and not really associated with any particular
478// 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 +0530479func (RsrcMgr *OpenOltResourceMgr) GetCurrentFlowIDsForOnu(ctx context.Context, PONIntfID uint32, ONUID int32, UNIID int32) []uint32 {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700480
Abhilash S.L8ee90712019-04-29 16:24:22 +0530481 FlowPath := fmt.Sprintf("%d,%d,%d", PONIntfID, ONUID, UNIID)
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700482 if mgrs, exist := RsrcMgr.ResourceMgrs[PONIntfID]; exist {
npujarec5762e2020-01-01 14:08:48 +0530483 return mgrs.GetCurrentFlowIDsForOnu(ctx, FlowPath)
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700484 }
485 return nil
Abhilash S.L8ee90712019-04-29 16:24:22 +0530486}
487
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700488// UpdateFlowIDInfo updates flow info for the given pon interface, onu id, and uni id
489// Note: For flows which trap from the NNI and not really associated with any particular
490// 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 +0530491func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDInfo(ctx context.Context, ponIntfID int32, onuID int32, uniID int32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700492 flowID uint32, flowData *[]FlowInfo) error {
493 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530494 return RsrcMgr.ResourceMgrs[uint32(ponIntfID)].UpdateFlowIDInfoForOnu(ctx, FlowPath, flowID, *flowData)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530495}
496
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700497// GetFlowID return flow ID for a given pon interface id, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530498func (RsrcMgr *OpenOltResourceMgr) GetFlowID(ctx context.Context, ponIntfID uint32, ONUID int32, uniID int32,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400499 gemportID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700500 flowStoreCookie uint64,
Gamze Abaka724d0852020-03-18 12:10:24 +0000501 flowCategory string, vlanVid uint32, vlanPcp ...uint32) (uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530502
Girish Gowdru0c588b22019-04-23 23:24:56 -0400503 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700504 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, ONUID, uniID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700505
506 RsrcMgr.FlowIDMgmtLock.Lock()
507 defer RsrcMgr.FlowIDMgmtLock.Unlock()
508
npujarec5762e2020-01-01 14:08:48 +0530509 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(ctx, FlowPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400510 if FlowIDs != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000511 logger.Debugw("Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "ONUID": ONUID, "uniID": uniID, "KVpath": FlowPath})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700512 for _, flowID := range FlowIDs {
npujarec5762e2020-01-01 14:08:48 +0530513 FlowInfo := RsrcMgr.GetFlowIDInfo(ctx, ponIntfID, int32(ONUID), int32(uniID), uint32(flowID))
Gamze Abaka724d0852020-03-18 12:10:24 +0000514 er := getFlowIDFromFlowInfo(FlowInfo, flowID, gemportID, flowStoreCookie, flowCategory, vlanVid, vlanPcp...)
salmansiddiqui7ac62132019-08-22 03:58:50 +0000515 if er == nil {
Gamze Abaka724d0852020-03-18 12:10:24 +0000516 log.Debugw("Found flowid for the vlan, pcp, and gem",
517 log.Fields{"flowID": flowID, "vlanVid": vlanVid, "vlanPcp": vlanPcp, "gemPortID": gemportID})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000518 return flowID, er
Abhilash S.L8ee90712019-04-29 16:24:22 +0530519 }
520 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400521 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000522 logger.Debug("No matching flows with flow cookie or flow category, allocating new flowid")
npujarec5762e2020-01-01 14:08:48 +0530523 FlowIDs, err = RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ctx, ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400524 ponrmgr.FLOW_ID, 1)
525 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000526 logger.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700527 ponIntfID, ponrmgr.FLOW_ID)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400528 return uint32(0), err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400529 }
530 if FlowIDs != nil {
npujarec5762e2020-01-01 14:08:48 +0530531 _ = RsrcMgr.ResourceMgrs[ponIntfID].UpdateFlowIDForOnu(ctx, FlowPath, FlowIDs[0], true)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700532 return FlowIDs[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400533 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530534
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700535 return 0, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530536}
537
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700538// GetAllocID return the first Alloc ID for a given pon interface id and onu id and then update the resource map on
539// the KV store with the list of alloc_ids allocated for the pon_intf_onu_id tuple
540// 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 +0530541func (RsrcMgr *OpenOltResourceMgr) GetAllocID(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530542
Girish Gowdru0c588b22019-04-23 23:24:56 -0400543 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700544 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700545
546 RsrcMgr.AllocIDMgmtLock[intfID].Lock()
547 defer RsrcMgr.AllocIDMgmtLock[intfID].Unlock()
548
npujarec5762e2020-01-01 14:08:48 +0530549 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400550 if AllocID != nil {
551 // Since we support only one alloc_id for the ONU at the moment,
552 // return the first alloc_id in the list, if available, for that
553 // ONU.
Girish Kumar2ad402b2020-03-20 19:45:12 +0000554 logger.Debugw("Retrieved alloc ID from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400555 return AllocID[0]
556 }
npujarec5762e2020-01-01 14:08:48 +0530557 AllocID, err = RsrcMgr.ResourceMgrs[intfID].GetResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400558 ponrmgr.ALLOC_ID, 1)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530559
Girish Gowdru0c588b22019-04-23 23:24:56 -0400560 if AllocID == nil || err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000561 logger.Error("Failed to allocate alloc id")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400562 return 0
563 }
564 // update the resource map on KV store with the list of alloc_id
565 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530566 err = RsrcMgr.ResourceMgrs[intfID].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID, AllocID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400567 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000568 logger.Error("Failed to update Alloc ID")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400569 return 0
570 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000571 logger.Debugw("Allocated new Tcont from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400572 return AllocID[0]
Abhilash S.L7f17e402019-03-15 17:40:41 +0530573}
574
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700575// UpdateAllocIdsForOnu updates alloc ids in kv store for a given pon interface id, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530576func (RsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ctx context.Context, ponPort uint32, onuID uint32, uniID uint32, allocID []uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530577
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700578 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530579 return RsrcMgr.ResourceMgrs[ponPort].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700580 allocID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530581}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700582
583// GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530584func (RsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(ctx context.Context, intfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700585 uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530586
Girish Gowdru0c588b22019-04-23 23:24:56 -0400587 /* Get gem ports for given pon interface , onu id and uni id. */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530588
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700589 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530590 return RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530591}
592
Gamze Abakafee36392019-10-03 11:17:24 +0000593// GetCurrentAllocIDsForOnu returns alloc ids for given pon interface and onu id
npujarec5762e2020-01-01 14:08:48 +0530594func (RsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDsForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530595
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700596 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530597 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400598 if AllocID != nil {
Gamze Abakafee36392019-10-03 11:17:24 +0000599 return AllocID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400600 }
Gamze Abakafee36392019-10-03 11:17:24 +0000601 return []uint32{}
602}
603
604// RemoveAllocIDForOnu removes the alloc id for given pon interface, onu id, uni id and alloc id
npujarec5762e2020-01-01 14:08:48 +0530605func (RsrcMgr *OpenOltResourceMgr) RemoveAllocIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, allocID uint32) {
606 allocIDs := RsrcMgr.GetCurrentAllocIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000607 for i := 0; i < len(allocIDs); i++ {
608 if allocIDs[i] == allocID {
609 allocIDs = append(allocIDs[:i], allocIDs[i+1:]...)
610 break
611 }
612 }
npujarec5762e2020-01-01 14:08:48 +0530613 err := RsrcMgr.UpdateAllocIdsForOnu(ctx, intfID, onuID, uniID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000614 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000615 logger.Errorf("Failed to Remove Alloc Id For Onu. IntfID %d onuID %d uniID %d allocID %d",
Gamze Abakafee36392019-10-03 11:17:24 +0000616 intfID, onuID, uniID, allocID)
617 }
618}
619
620// RemoveGemPortIDForOnu removes the gem port id for given pon interface, onu id, uni id and gem port id
npujarec5762e2020-01-01 14:08:48 +0530621func (RsrcMgr *OpenOltResourceMgr) RemoveGemPortIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, gemPortID uint32) {
622 gemPortIDs := RsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000623 for i := 0; i < len(gemPortIDs); i++ {
624 if gemPortIDs[i] == gemPortID {
625 gemPortIDs = append(gemPortIDs[:i], gemPortIDs[i+1:]...)
626 break
627 }
628 }
npujarec5762e2020-01-01 14:08:48 +0530629 err := RsrcMgr.UpdateGEMPortIDsForOnu(ctx, intfID, onuID, uniID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000630 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000631 logger.Errorf("Failed to Remove Gem Id For Onu. IntfID %d onuID %d uniID %d gemPortId %d",
Gamze Abakafee36392019-10-03 11:17:24 +0000632 intfID, onuID, uniID, gemPortID)
633 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530634}
635
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700636// UpdateGEMportsPonportToOnuMapOnKVStore updates onu and uni id associated with the gem port to the kv store
637// This stored information is used when packet_indication is received and we need to derive the ONU Id for which
638// the packet arrived based on the pon_intf and gemport available in the packet_indication
npujarec5762e2020-01-01 14:08:48 +0530639func (RsrcMgr *OpenOltResourceMgr) UpdateGEMportsPonportToOnuMapOnKVStore(ctx context.Context, gemPorts []uint32, PonPort uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700640 onuID uint32, uniID uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530641
Girish Gowdru0c588b22019-04-23 23:24:56 -0400642 /* Update onu and uni id associated with the gem port to the kv store. */
643 var IntfGEMPortPath string
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700644 Data := fmt.Sprintf("%d %d", onuID, uniID)
645 for _, GEM := range gemPorts {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400646 IntfGEMPortPath = fmt.Sprintf("%d,%d", PonPort, GEM)
647 Val, err := json.Marshal(Data)
648 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000649 logger.Error("failed to Marshal")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400650 return err
651 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700652
npujarec5762e2020-01-01 14:08:48 +0530653 if err = RsrcMgr.KVStore.Put(ctx, IntfGEMPortPath, Val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000654 logger.Errorf("Failed to update resource %s", IntfGEMPortPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400655 return err
656 }
657 }
658 return nil
Abhilash S.L7f17e402019-03-15 17:40:41 +0530659}
660
Gamze Abakafee36392019-10-03 11:17:24 +0000661// RemoveGEMportPonportToOnuMapOnKVStore removes the relationship between the gem port and pon port
npujarec5762e2020-01-01 14:08:48 +0530662func (RsrcMgr *OpenOltResourceMgr) RemoveGEMportPonportToOnuMapOnKVStore(ctx context.Context, GemPort uint32, PonPort uint32) {
Gamze Abakafee36392019-10-03 11:17:24 +0000663 IntfGEMPortPath := fmt.Sprintf("%d,%d", PonPort, GemPort)
npujarec5762e2020-01-01 14:08:48 +0530664 err := RsrcMgr.KVStore.Delete(ctx, IntfGEMPortPath)
Gamze Abakafee36392019-10-03 11:17:24 +0000665 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000666 logger.Errorf("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 +0000667 }
668}
669
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700670// GetGEMPortID gets gem port id for a particular pon port, onu id and uni id and then update the resource map on
671// the KV store with the list of gemport_id allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530672func (RsrcMgr *OpenOltResourceMgr) GetGEMPortID(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700673 uniID uint32, NumOfPorts uint32) ([]uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530674
Girish Gowdru0c588b22019-04-23 23:24:56 -0400675 /* Get gem port id for a particular pon port, onu id
676 and uni id.
677 */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530678
Girish Gowdru0c588b22019-04-23 23:24:56 -0400679 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700680 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530681
Girish Gowdrab77ded92020-04-08 11:45:05 -0700682 RsrcMgr.GemPortIDMgmtLock[ponPort].Lock()
683 defer RsrcMgr.GemPortIDMgmtLock[ponPort].Unlock()
684
npujarec5762e2020-01-01 14:08:48 +0530685 GEMPortList := RsrcMgr.ResourceMgrs[ponPort].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400686 if GEMPortList != nil {
687 return GEMPortList, nil
688 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530689
npujarec5762e2020-01-01 14:08:48 +0530690 GEMPortList, err = RsrcMgr.ResourceMgrs[ponPort].GetResourceID(ctx, ponPort,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400691 ponrmgr.GEMPORT_ID, NumOfPorts)
692 if err != nil && GEMPortList == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000693 logger.Errorf("Failed to get gem port id for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400694 return nil, err
695 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530696
Girish Gowdru0c588b22019-04-23 23:24:56 -0400697 // update the resource map on KV store with the list of gemport_id
698 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530699 err = RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400700 GEMPortList)
701 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000702 logger.Errorf("Failed to update GEM ports to kv store for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400703 return nil, err
704 }
npujarec5762e2020-01-01 14:08:48 +0530705 _ = RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(ctx, GEMPortList, ponPort,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700706 onuID, uniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400707 return GEMPortList, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530708}
709
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700710// 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 +0530711func (RsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700712 uniID uint32, GEMPortList []uint32) error {
713 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530714 return RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400715 GEMPortList)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530716
717}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700718
719// FreeonuID releases(make free) onu id for a particular pon-port
npujarec5762e2020-01-01 14:08:48 +0530720func (RsrcMgr *OpenOltResourceMgr) FreeonuID(ctx context.Context, intfID uint32, onuID []uint32) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700721
Girish Gowdra38d533d2020-03-30 20:38:51 -0700722 RsrcMgr.OnuIDMgmtLock[intfID].Lock()
Girish Gowdrab77ded92020-04-08 11:45:05 -0700723 defer RsrcMgr.OnuIDMgmtLock[intfID].Unlock()
724
npujarec5762e2020-01-01 14:08:48 +0530725 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID, ponrmgr.ONU_ID, onuID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530726
Girish Gowdru0c588b22019-04-23 23:24:56 -0400727 /* Free onu id for a particular interface.*/
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700728 var IntfonuID string
729 for _, onu := range onuID {
730 IntfonuID = fmt.Sprintf("%d,%d", intfID, onu)
npujarec5762e2020-01-01 14:08:48 +0530731 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfonuID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400732 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530733}
734
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700735// FreeFlowID returns the free flow id for a given interface, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530736func (RsrcMgr *OpenOltResourceMgr) FreeFlowID(ctx context.Context, IntfID uint32, onuID int32,
Devmalya Paul495b94a2019-08-27 19:42:00 -0400737 uniID int32, FlowID uint32) {
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400738 var IntfONUID string
739 var err error
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530740
Girish Gowdrab77ded92020-04-08 11:45:05 -0700741 RsrcMgr.FlowIDMgmtLock.Lock()
742 defer RsrcMgr.FlowIDMgmtLock.Unlock()
743
744 FlowIds := make([]uint32, 0)
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530745 FlowIds = append(FlowIds, FlowID)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700746 IntfONUID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530747 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(ctx, IntfONUID, FlowID, false)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400748 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000749 logger.Errorw("Failed to Update flow id for", log.Fields{"intf": IntfONUID})
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400750 }
npujarec5762e2020-01-01 14:08:48 +0530751 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(ctx, IntfONUID, FlowID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700752
npujarec5762e2020-01-01 14:08:48 +0530753 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.FLOW_ID, FlowIds)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400754}
755
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700756// FreeFlowIDs releases the flow Ids
npujarec5762e2020-01-01 14:08:48 +0530757func (RsrcMgr *OpenOltResourceMgr) FreeFlowIDs(ctx context.Context, IntfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700758 uniID uint32, FlowID []uint32) {
Girish Gowdra38d533d2020-03-30 20:38:51 -0700759 RsrcMgr.FlowIDMgmtLock.Lock()
Girish Gowdrab77ded92020-04-08 11:45:05 -0700760 defer RsrcMgr.FlowIDMgmtLock.Unlock()
761
npujarec5762e2020-01-01 14:08:48 +0530762 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.FLOW_ID, FlowID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530763
Abhilash S.L8ee90712019-04-29 16:24:22 +0530764 var IntfOnuIDUniID string
Girish Gowdru0c588b22019-04-23 23:24:56 -0400765 var err error
766 for _, flow := range FlowID {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700767 IntfOnuIDUniID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530768 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(ctx, IntfOnuIDUniID, flow, false)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400769 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000770 logger.Errorw("Failed to Update flow id for", log.Fields{"intf": IntfOnuIDUniID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400771 }
npujarec5762e2020-01-01 14:08:48 +0530772 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(ctx, IntfOnuIDUniID, flow)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400773 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530774}
775
Gamze Abakafee36392019-10-03 11:17:24 +0000776// FreeAllocID frees AllocID on the PON resource pool and also frees the allocID association
777// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530778func (RsrcMgr *OpenOltResourceMgr) FreeAllocID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000779 uniID uint32, allocID uint32) {
Girish Gowdrab77ded92020-04-08 11:45:05 -0700780 RsrcMgr.AllocIDMgmtLock[IntfID].Lock()
781 defer RsrcMgr.AllocIDMgmtLock[IntfID].Unlock()
782
npujarec5762e2020-01-01 14:08:48 +0530783 RsrcMgr.RemoveAllocIDForOnu(ctx, IntfID, onuID, uniID, allocID)
Gamze Abakafee36392019-10-03 11:17:24 +0000784 allocIDs := make([]uint32, 0)
785 allocIDs = append(allocIDs, allocID)
npujarec5762e2020-01-01 14:08:48 +0530786 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.ALLOC_ID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000787}
788
789// FreeGemPortID frees GemPortID on the PON resource pool and also frees the gemPortID association
790// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530791func (RsrcMgr *OpenOltResourceMgr) FreeGemPortID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000792 uniID uint32, gemPortID uint32) {
Girish Gowdrab77ded92020-04-08 11:45:05 -0700793 RsrcMgr.GemPortIDMgmtLock[IntfID].Lock()
794 defer RsrcMgr.GemPortIDMgmtLock[IntfID].Unlock()
795
npujarec5762e2020-01-01 14:08:48 +0530796 RsrcMgr.RemoveGemPortIDForOnu(ctx, IntfID, onuID, uniID, gemPortID)
Gamze Abakafee36392019-10-03 11:17:24 +0000797 gemPortIDs := make([]uint32, 0)
798 gemPortIDs = append(gemPortIDs, gemPortID)
npujarec5762e2020-01-01 14:08:48 +0530799 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.GEMPORT_ID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000800}
801
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700802// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id, and the clears the
803// resource map and the onuID associated with (pon_intf_id, gemport_id) tuple,
npujarec5762e2020-01-01 14:08:48 +0530804func (RsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530805
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700806 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530807
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700808 RsrcMgr.AllocIDMgmtLock[intfID].Lock()
Girish Gowdrab77ded92020-04-08 11:45:05 -0700809 AllocIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700810
npujarec5762e2020-01-01 14:08:48 +0530811 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400812 ponrmgr.ALLOC_ID,
813 AllocIDs)
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700814 RsrcMgr.AllocIDMgmtLock[intfID].Unlock()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530815
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700816 RsrcMgr.GemPortIDMgmtLock[intfID].Lock()
npujarec5762e2020-01-01 14:08:48 +0530817 GEMPortIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
818 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400819 ponrmgr.GEMPORT_ID,
820 GEMPortIDs)
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700821 RsrcMgr.GemPortIDMgmtLock[intfID].Unlock()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530822
Girish Gowdra38d533d2020-03-30 20:38:51 -0700823 RsrcMgr.FlowIDMgmtLock.Lock()
npujarec5762e2020-01-01 14:08:48 +0530824 FlowIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentFlowIDsForOnu(ctx, IntfOnuIDUniID)
825 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400826 ponrmgr.FLOW_ID,
827 FlowIDs)
Girish Gowdra38d533d2020-03-30 20:38:51 -0700828 RsrcMgr.FlowIDMgmtLock.Unlock()
829
Girish Gowdru0c588b22019-04-23 23:24:56 -0400830 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
npujarec5762e2020-01-01 14:08:48 +0530831 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400832 // Clear the ONU Id associated with the (pon_intf_id, gemport_id) tuple.
833 for _, GEM := range GEMPortIDs {
npujarec5762e2020-01-01 14:08:48 +0530834 _ = RsrcMgr.KVStore.Delete(ctx, fmt.Sprintf("%d,%d", intfID, GEM))
Girish Gowdru0c588b22019-04-23 23:24:56 -0400835 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530836}
837
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700838// IsFlowCookieOnKVStore checks if the given flow cookie is present on the kv store
839// Returns true if the flow cookie is found, otherwise it returns false
npujarec5762e2020-01-01 14:08:48 +0530840func (RsrcMgr *OpenOltResourceMgr) IsFlowCookieOnKVStore(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700841 flowStoreCookie uint64) bool {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530842
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700843 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530844 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(ctx, FlowPath)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400845 if FlowIDs != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000846 logger.Debugw("Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "onuID": onuID, "uniID": uniID, "KVpath": FlowPath})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700847 for _, flowID := range FlowIDs {
npujarec5762e2020-01-01 14:08:48 +0530848 FlowInfo := RsrcMgr.GetFlowIDInfo(ctx, ponIntfID, int32(onuID), int32(uniID), uint32(flowID))
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400849 if FlowInfo != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000850 logger.Debugw("Found flows", log.Fields{"flows": *FlowInfo, "flowId": flowID})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400851 for _, Info := range *FlowInfo {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700852 if Info.FlowStoreCookie == flowStoreCookie {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000853 logger.Debug("Found flow matching with flowStore cookie", log.Fields{"flowId": flowID, "flowStoreCookie": flowStoreCookie})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400854 return true
855 }
856 }
857 }
858 }
859 }
860 return false
861}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400862
salmansiddiqui7ac62132019-08-22 03:58:50 +0000863// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000864// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530865func (RsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) []uint32 {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000866 Path := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000867 var Data []uint32
npujarec5762e2020-01-01 14:08:48 +0530868 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400869 if err == nil {
870 if Value != nil {
871 Val, err := kvstore.ToByte(Value.Value)
872 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000873 logger.Errorw("Failed to convert into byte array", log.Fields{"error": err})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400874 return Data
875 }
876 if err = json.Unmarshal(Val, &Data); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000877 logger.Error("Failed to unmarshal", log.Fields{"error": err})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400878 return Data
879 }
880 }
881 } else {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000882 logger.Errorf("Failed to get TP id from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400883 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000884 logger.Debugf("Getting TP id %d from path %s", Data, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400885 return Data
886
887}
888
Gamze Abakafee36392019-10-03 11:17:24 +0000889// RemoveTechProfileIDsForOnu deletes all tech profile ids from the KV-Store for the given onu based on the path
890// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530891func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDsForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) error {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000892 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
npujarec5762e2020-01-01 14:08:48 +0530893 if err := RsrcMgr.KVStore.Delete(ctx, IntfOnuUniID); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000894 logger.Errorw("Failed to delete techprofile id resource in KV store", log.Fields{"path": IntfOnuUniID})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400895 return err
896 }
897 return nil
898}
899
Gamze Abakafee36392019-10-03 11:17:24 +0000900// RemoveTechProfileIDForOnu deletes a specific tech profile id from the KV-Store for the given onu based on the path
901// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530902func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32, TpID uint32) error {
903 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000904 for i, tpIDInList := range tpIDList {
905 if tpIDInList == TpID {
906 tpIDList = append(tpIDList[:i], tpIDList[i+1:]...)
907 }
908 }
909 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
910 Value, err := json.Marshal(tpIDList)
911 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000912 logger.Error("failed to Marshal")
Gamze Abakafee36392019-10-03 11:17:24 +0000913 return err
914 }
npujarec5762e2020-01-01 14:08:48 +0530915 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000916 logger.Errorf("Failed to update resource %s", IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000917 return err
918 }
919 return err
920}
921
922// UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
923// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530924func (RsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32,
salmansiddiqui7ac62132019-08-22 03:58:50 +0000925 UniID uint32, TpID uint32) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400926 var Value []byte
927 var err error
928
salmansiddiqui7ac62132019-08-22 03:58:50 +0000929 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000930
npujarec5762e2020-01-01 14:08:48 +0530931 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000932 for _, value := range tpIDList {
933 if value == TpID {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000934 logger.Debugf("TpID %d is already in tpIdList for the path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000935 return err
936 }
937 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000938 logger.Debugf("updating tp id %d on path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000939 tpIDList = append(tpIDList, TpID)
940 Value, err = json.Marshal(tpIDList)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400941 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000942 logger.Error("failed to Marshal")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400943 return err
944 }
npujarec5762e2020-01-01 14:08:48 +0530945 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000946 logger.Errorf("Failed to update resource %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400947 return err
948 }
949 return err
950}
951
salmansiddiqui7ac62132019-08-22 03:58:50 +0000952// UpdateMeterIDForOnu updates the meter id in the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000953// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +0530954func (RsrcMgr *OpenOltResourceMgr) UpdateMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000955 UniID uint32, TpID uint32, MeterConfig *ofp.OfpMeterConfig) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400956 var Value []byte
957 var err error
958
Gamze Abakafee36392019-10-03 11:17:24 +0000959 IntfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400960 Value, err = json.Marshal(*MeterConfig)
961 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000962 logger.Error("failed to Marshal meter config")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400963 return err
964 }
npujarec5762e2020-01-01 14:08:48 +0530965 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000966 logger.Errorf("Failed to store meter into KV store %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400967 return err
968 }
969 return err
970}
971
Gamze Abakafee36392019-10-03 11:17:24 +0000972// GetMeterIDForOnu fetches the meter id from the kv store for the given onu based on the path
973// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +0530974func (RsrcMgr *OpenOltResourceMgr) GetMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000975 UniID uint32, TpID uint32) (*ofp.OfpMeterConfig, error) {
976 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400977 var meterConfig ofp.OfpMeterConfig
npujarec5762e2020-01-01 14:08:48 +0530978 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400979 if err == nil {
980 if Value != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000981 logger.Debug("Found meter in KV store", log.Fields{"Direction": Direction})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000982 Val, er := kvstore.ToByte(Value.Value)
983 if er != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000984 logger.Errorw("Failed to convert into byte array", log.Fields{"error": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000985 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400986 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000987 if er = json.Unmarshal(Val, &meterConfig); er != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000988 logger.Error("Failed to unmarshal meterconfig", log.Fields{"error": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000989 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400990 }
991 } else {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000992 logger.Debug("meter-does-not-exists-in-KVStore")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400993 return nil, err
994 }
995 } else {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000996 logger.Errorf("Failed to get Meter config from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400997
998 }
999 return &meterConfig, err
1000}
1001
Gamze Abakafee36392019-10-03 11:17:24 +00001002// RemoveMeterIDForOnu deletes the meter id from the kV-Store for the given onu based on the path
1003// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +05301004func (RsrcMgr *OpenOltResourceMgr) RemoveMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +00001005 UniID uint32, TpID uint32) error {
1006 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
npujarec5762e2020-01-01 14:08:48 +05301007 if err := RsrcMgr.KVStore.Delete(ctx, Path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001008 logger.Errorf("Failed to delete meter id %s from kvstore ", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001009 return err
1010 }
1011 return nil
1012}
salmansiddiqui7ac62132019-08-22 03:58:50 +00001013
Gamze Abaka724d0852020-03-18 12:10:24 +00001014func getFlowIDFromFlowInfo(FlowInfo *[]FlowInfo, flowID, gemportID uint32, flowStoreCookie uint64, flowCategory string,
1015 vlanVid uint32, vlanPcp ...uint32) error {
salmansiddiqui7ac62132019-08-22 03:58:50 +00001016 if FlowInfo != nil {
1017 for _, Info := range *FlowInfo {
1018 if int32(gemportID) == Info.Flow.GemportId && flowCategory != "" && Info.FlowCategory == flowCategory {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001019 logger.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
Gamze Abaka724d0852020-03-18 12:10:24 +00001020 if Info.FlowCategory == "HSIA_FLOW" {
1021 if err := checkVlanAndPbitEqualityForFlows(vlanVid, Info, vlanPcp[0]); err == nil {
1022 return nil
1023 }
salmansiddiqui7ac62132019-08-22 03:58:50 +00001024 }
1025 }
1026 if int32(gemportID) == Info.Flow.GemportId && flowStoreCookie != 0 && Info.FlowStoreCookie == flowStoreCookie {
1027 if flowCategory != "" && Info.FlowCategory == flowCategory {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001028 logger.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001029 return nil
1030 }
1031 }
1032 }
1033 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001034 logger.Debugw("the flow can be related to a different service", log.Fields{"flow_info": FlowInfo})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001035 return errors.New("invalid flow-info")
1036}
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301037
Gamze Abaka724d0852020-03-18 12:10:24 +00001038func checkVlanAndPbitEqualityForFlows(vlanVid uint32, Info FlowInfo, vlanPcp uint32) error {
1039 if err := checkVlanEqualityForFlows(vlanVid, Info); err != nil {
1040 return err
1041 }
1042
1043 //flow has remark action and pbits
1044 if Info.Flow.Action.Cmd.RemarkInnerPbits || Info.Flow.Action.Cmd.RemarkOuterPbits {
1045 if vlanPcp == Info.Flow.Action.OPbits || vlanPcp == Info.Flow.Action.IPbits {
1046 return nil
1047 }
1048 } else if vlanPcp == Info.Flow.Classifier.OPbits {
1049 //no remark action but flow has pbits
1050 return nil
1051 } else if vlanPcp == 0xff || Info.Flow.Classifier.OPbits == 0xff {
1052 // no pbit found
1053 return nil
1054 }
1055 return errors.New("not found in terms of pbit equality")
1056}
1057
1058func checkVlanEqualityForFlows(vlanVid uint32, Info FlowInfo) error {
1059 if vlanVid == Info.Flow.Action.OVid || vlanVid == Info.Flow.Classifier.IVid {
1060 return nil
1061 }
1062 return errors.New("not found in terms of vlan_id equality")
1063}
1064
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301065//AddGemToOnuGemInfo adds gemport to onugem info kvstore
npujarec5762e2020-01-01 14:08:48 +05301066func (RsrcMgr *OpenOltResourceMgr) AddGemToOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, gemPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301067 var onuGemData []OnuGemInfo
1068 var err error
1069
npujarec5762e2020-01-01 14:08:48 +05301070 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001071 logger.Errorf("failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301072 return err
1073 }
1074 if len(onuGemData) == 0 {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001075 logger.Errorw("failed to ger Onuid info ", log.Fields{"intfid": intfID, "onuid": onuID})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301076 return err
1077 }
1078
1079 for idx, onugem := range onuGemData {
1080 if onugem.OnuID == onuID {
1081 for _, gem := range onuGemData[idx].GemPorts {
1082 if gem == gemPort {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001083 logger.Debugw("Gem already present in onugem info, skpping addition", log.Fields{"gem": gem})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301084 return nil
1085 }
1086 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001087 logger.Debugw("Added gem to onugem info", log.Fields{"gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301088 onuGemData[idx].GemPorts = append(onuGemData[idx].GemPorts, gemPort)
1089 break
1090 }
1091 }
npujarec5762e2020-01-01 14:08:48 +05301092 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301093 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001094 logger.Error("Failed to add onugem to kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301095 return err
1096 }
1097 return err
1098}
1099
1100//GetOnuGemInfo gets onu gem info from the kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301101func (RsrcMgr *OpenOltResourceMgr) GetOnuGemInfo(ctx context.Context, IntfID uint32) ([]OnuGemInfo, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301102 var onuGemData []OnuGemInfo
1103
npujarec5762e2020-01-01 14:08:48 +05301104 if err := RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001105 logger.Errorf("failed to get onuifo for intfid %d", IntfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301106 return nil, err
1107 }
1108
1109 return onuGemData, nil
1110}
1111
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001112// AddOnuGemInfo adds onu info on to the kvstore per interface
1113func (RsrcMgr *OpenOltResourceMgr) AddOnuGemInfo(ctx context.Context, IntfID uint32, onuGem OnuGemInfo) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301114 var onuGemData []OnuGemInfo
1115 var err error
1116
npujarec5762e2020-01-01 14:08:48 +05301117 if err = RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001118 logger.Errorf("failed to get onuifo for intfid %d", IntfID)
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001119 return olterrors.NewErrPersistence("get", "OnuGemInfo", IntfID,
1120 log.Fields{"onuGem": onuGem, "intfID": IntfID}, err)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301121 }
1122 onuGemData = append(onuGemData, onuGem)
npujarec5762e2020-01-01 14:08:48 +05301123 err = RsrcMgr.ResourceMgrs[IntfID].AddOnuGemInfo(ctx, IntfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301124 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001125 logger.Error("Failed to add onugem to kv store")
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001126 return olterrors.NewErrPersistence("set", "OnuGemInfo", IntfID,
1127 log.Fields{"onuGemData": onuGemData, "intfID": IntfID}, err)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301128 }
1129
Girish Kumar2ad402b2020-03-20 19:45:12 +00001130 logger.Debugw("added onu to onugeminfo", log.Fields{"intf": IntfID, "onugem": onuGem})
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001131 return nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301132}
1133
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301134// 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 +05301135func (RsrcMgr *OpenOltResourceMgr) AddUniPortToOnuInfo(ctx context.Context, intfID uint32, onuID uint32, portNo uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301136 var onuGemData []OnuGemInfo
1137 var err error
1138
npujarec5762e2020-01-01 14:08:48 +05301139 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001140 logger.Errorf("failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301141 return
1142 }
1143 for idx, onu := range onuGemData {
1144 if onu.OnuID == onuID {
1145 for _, uni := range onu.UniPorts {
1146 if uni == portNo {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001147 logger.Debugw("uni already present in onugem info", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301148 return
1149 }
1150 }
1151 onuGemData[idx].UniPorts = append(onuGemData[idx].UniPorts, portNo)
1152 break
1153 }
1154 }
npujarec5762e2020-01-01 14:08:48 +05301155 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301156 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001157 logger.Errorw("Failed to add uin port in onugem to kv store", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301158 return
1159 }
1160 return
1161}
1162
1163//UpdateGemPortForPktIn updates gemport for pkt in path to kvstore, path being intfid, onuid, portno
npujarec5762e2020-01-01 14:08:48 +05301164func (RsrcMgr *OpenOltResourceMgr) UpdateGemPortForPktIn(ctx context.Context, pktIn PacketInInfoKey, gemPort uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301165
1166 path := fmt.Sprintf(OnuPacketINPath, pktIn.IntfID, pktIn.OnuID, pktIn.LogicalPort)
1167 Value, err := json.Marshal(gemPort)
1168 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001169 logger.Error("Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301170 return
1171 }
npujarec5762e2020-01-01 14:08:48 +05301172 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001173 logger.Errorw("Failed to put to kvstore", log.Fields{"path": path, "value": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301174 return
1175 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001176 logger.Debugw("added gem packet in successfully", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301177
1178 return
1179}
1180
1181// GetGemPortFromOnuPktIn gets the gem port from onu pkt in path, path being intfid, onuid, portno
npujarec5762e2020-01-01 14:08:48 +05301182func (RsrcMgr *OpenOltResourceMgr) GetGemPortFromOnuPktIn(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) (uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301183
1184 var Val []byte
1185 var gemPort uint32
1186
1187 path := fmt.Sprintf(OnuPacketINPath, intfID, onuID, logicalPort)
1188
npujarec5762e2020-01-01 14:08:48 +05301189 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301190 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001191 logger.Errorw("Failed to get from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301192 return uint32(0), err
1193 } else if value == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001194 logger.Debugw("No pkt in gem found", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301195 return uint32(0), nil
1196 }
1197
1198 if Val, err = kvstore.ToByte(value.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001199 logger.Error("Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301200 return uint32(0), err
1201 }
1202 if err = json.Unmarshal(Val, &gemPort); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001203 logger.Error("Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301204 return uint32(0), err
1205 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001206 logger.Debugw("found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301207
1208 return gemPort, nil
1209}
1210
1211// DelGemPortPktIn deletes the gemport from the pkt in path
npujarec5762e2020-01-01 14:08:48 +05301212func (RsrcMgr *OpenOltResourceMgr) DelGemPortPktIn(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301213
1214 path := fmt.Sprintf(OnuPacketINPath, intfID, onuID, logicalPort)
npujarec5762e2020-01-01 14:08:48 +05301215 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001216 logger.Errorf("Falied to remove resource %s", path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301217 return err
1218 }
1219 return nil
1220}
1221
1222// DelOnuGemInfoForIntf deletes the onugem info from kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301223func (RsrcMgr *OpenOltResourceMgr) DelOnuGemInfoForIntf(ctx context.Context, intfID uint32) error {
1224 if err := RsrcMgr.ResourceMgrs[intfID].DelOnuGemInfoForIntf(ctx, intfID); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001225 logger.Errorw("failed to delete onu gem info for", log.Fields{"intfid": intfID})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301226 return err
1227 }
1228 return nil
1229}
1230
1231//GetNNIFromKVStore gets NNi intfids from kvstore. path being per device
npujarec5762e2020-01-01 14:08:48 +05301232func (RsrcMgr *OpenOltResourceMgr) GetNNIFromKVStore(ctx context.Context) ([]uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301233
1234 var nni []uint32
1235 var Val []byte
1236
1237 path := fmt.Sprintf(NnniIntfID)
npujarec5762e2020-01-01 14:08:48 +05301238 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301239 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001240 logger.Error("failed to get data from kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301241 return nil, err
1242 }
1243 if value != nil {
1244 if Val, err = kvstore.ToByte(value.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001245 logger.Error("Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301246 return nil, err
1247 }
1248 if err = json.Unmarshal(Val, &nni); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001249 logger.Error("Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301250 return nil, err
1251 }
1252 }
1253 return nni, err
1254}
1255
1256// AddNNIToKVStore adds Nni interfaces to kvstore, path being per device.
npujarec5762e2020-01-01 14:08:48 +05301257func (RsrcMgr *OpenOltResourceMgr) AddNNIToKVStore(ctx context.Context, nniIntf uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301258 var Value []byte
1259
npujarec5762e2020-01-01 14:08:48 +05301260 nni, err := RsrcMgr.GetNNIFromKVStore(ctx)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301261 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001262 logger.Error("failed to fetch nni interfaces from kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301263 return err
1264 }
1265
1266 path := fmt.Sprintf(NnniIntfID)
1267 nni = append(nni, nniIntf)
1268 Value, err = json.Marshal(nni)
1269 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001270 logger.Error("Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301271 }
npujarec5762e2020-01-01 14:08:48 +05301272 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001273 logger.Errorw("Failed to put to kvstore", log.Fields{"path": path, "value": Value})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301274 return err
1275 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001276 logger.Debugw("added nni to kv successfully", log.Fields{"path": path, "nni": nniIntf})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301277 return nil
1278}
1279
1280// DelNNiFromKVStore deletes nni interface list from kv store.
npujarec5762e2020-01-01 14:08:48 +05301281func (RsrcMgr *OpenOltResourceMgr) DelNNiFromKVStore(ctx context.Context) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301282
1283 path := fmt.Sprintf(NnniIntfID)
1284
npujarec5762e2020-01-01 14:08:48 +05301285 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001286 logger.Errorw("Failed to delete nni interfaces from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301287 return err
1288 }
1289 return nil
1290}
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301291
1292//UpdateFlowIDsForGem updates flow id per gemport
npujarec5762e2020-01-01 14:08:48 +05301293func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDsForGem(ctx context.Context, intf uint32, gem uint32, flowIDs []uint32) error {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301294 var val []byte
1295 path := fmt.Sprintf(FlowIDsForGem, intf)
1296
npujarec5762e2020-01-01 14:08:48 +05301297 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301298 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001299 logger.Error("Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301300 return err
1301 }
1302 if flowsForGem == nil {
1303 flowsForGem = make(map[uint32][]uint32)
1304 }
1305 flowsForGem[gem] = flowIDs
1306 val, err = json.Marshal(flowsForGem)
1307 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001308 logger.Error("Failed to marshal data", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301309 return err
1310 }
Girish Gowdrab77ded92020-04-08 11:45:05 -07001311
Girish Gowdra38d533d2020-03-30 20:38:51 -07001312 RsrcMgr.flowIDToGemInfoLock.Lock()
1313 defer RsrcMgr.flowIDToGemInfoLock.Unlock()
npujarec5762e2020-01-01 14:08:48 +05301314 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001315 logger.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301316 return err
1317 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001318 logger.Debugw("added flowid list for gem to kv successfully", log.Fields{"path": path, "flowidlist": flowsForGem[gem]})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301319 return nil
1320}
1321
1322//DeleteFlowIDsForGem deletes the flowID list entry per gem from kvstore.
npujarec5762e2020-01-01 14:08:48 +05301323func (RsrcMgr *OpenOltResourceMgr) DeleteFlowIDsForGem(ctx context.Context, intf uint32, gem uint32) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301324 path := fmt.Sprintf(FlowIDsForGem, intf)
1325 var val []byte
1326
npujarec5762e2020-01-01 14:08:48 +05301327 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301328 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001329 logger.Error("Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301330 return
1331 }
1332 if flowsForGem == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001333 logger.Error("No flowids found ", log.Fields{"intf": intf, "gemport": gem})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301334 return
1335 }
1336 // once we get the flows per gem map from kv , just delete the gem entry from the map
1337 delete(flowsForGem, gem)
1338 // once gem entry is deleted update the kv store.
1339 val, err = json.Marshal(flowsForGem)
1340 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001341 logger.Error("Failed to marshal data", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301342 return
1343 }
Girish Gowdra38d533d2020-03-30 20:38:51 -07001344
1345 RsrcMgr.flowIDToGemInfoLock.Lock()
1346 defer RsrcMgr.flowIDToGemInfoLock.Unlock()
npujarec5762e2020-01-01 14:08:48 +05301347 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001348 logger.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301349 return
1350 }
1351 return
1352}
1353
1354//GetFlowIDsGemMapForInterface gets flowids per gemport and interface
npujarec5762e2020-01-01 14:08:48 +05301355func (RsrcMgr *OpenOltResourceMgr) GetFlowIDsGemMapForInterface(ctx context.Context, intf uint32) (map[uint32][]uint32, error) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301356 path := fmt.Sprintf(FlowIDsForGem, intf)
1357 var flowsForGem map[uint32][]uint32
1358 var val []byte
Girish Gowdra38d533d2020-03-30 20:38:51 -07001359 RsrcMgr.flowIDToGemInfoLock.RLock()
npujarec5762e2020-01-01 14:08:48 +05301360 value, err := RsrcMgr.KVStore.Get(ctx, path)
Girish Gowdra38d533d2020-03-30 20:38:51 -07001361 RsrcMgr.flowIDToGemInfoLock.RUnlock()
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301362 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001363 logger.Error("failed to get data from kv store")
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301364 return nil, err
1365 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001366 if value != nil && value.Value != nil {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301367 if val, err = kvstore.ToByte(value.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001368 logger.Error("Failed to convert to byte array ", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301369 return nil, err
1370 }
1371 if err = json.Unmarshal(val, &flowsForGem); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001372 logger.Error("Failed to unmarshall", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301373 return nil, err
1374 }
1375 }
1376 return flowsForGem, nil
1377}
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301378
1379//DeleteIntfIDGempMapPath deletes the intf id path used to store flow ids per gem to kvstore.
npujarec5762e2020-01-01 14:08:48 +05301380func (RsrcMgr *OpenOltResourceMgr) DeleteIntfIDGempMapPath(ctx context.Context, intf uint32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301381 path := fmt.Sprintf(FlowIDsForGem, intf)
Girish Gowdra38d533d2020-03-30 20:38:51 -07001382 RsrcMgr.flowIDToGemInfoLock.Lock()
1383 defer RsrcMgr.flowIDToGemInfoLock.Unlock()
npujarec5762e2020-01-01 14:08:48 +05301384 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001385 logger.Errorw("Failed to delete nni interfaces from kv store", log.Fields{"path": path})
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301386 return
1387 }
1388 return
1389}
1390
1391// RemoveResourceMap Clear resource map associated with (intfid, onuid, uniid) tuple.
npujarec5762e2020-01-01 14:08:48 +05301392func (RsrcMgr *OpenOltResourceMgr) RemoveResourceMap(ctx context.Context, intfID uint32, onuID int32, uniID int32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301393 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +05301394 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301395}
Esin Karamanccb714b2019-11-29 15:02:06 +00001396
1397//GetMcastQueuePerInterfaceMap gets multicast queue info per pon interface
npujarec5762e2020-01-01 14:08:48 +05301398func (RsrcMgr *OpenOltResourceMgr) GetMcastQueuePerInterfaceMap(ctx context.Context) (map[uint32][]uint32, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001399 path := fmt.Sprintf(McastQueuesForIntf)
1400 var mcastQueueToIntfMap map[uint32][]uint32
1401 var val []byte
1402
npujarec5762e2020-01-01 14:08:48 +05301403 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001404 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001405 logger.Error("failed to get data from kv store")
Esin Karamanccb714b2019-11-29 15:02:06 +00001406 return nil, err
1407 }
1408 if kvPair != nil && kvPair.Value != nil {
1409 if val, err = kvstore.ToByte(kvPair.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001410 logger.Error("Failed to convert to byte array ", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001411 return nil, err
1412 }
1413 if err = json.Unmarshal(val, &mcastQueueToIntfMap); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001414 logger.Error("Failed to unmarshall ", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001415 return nil, err
1416 }
1417 }
1418 return mcastQueueToIntfMap, nil
1419}
1420
1421//AddMcastQueueForIntf adds multicast queue for pon interface
npujarec5762e2020-01-01 14:08:48 +05301422func (RsrcMgr *OpenOltResourceMgr) AddMcastQueueForIntf(ctx context.Context, intf uint32, gem uint32, servicePriority uint32) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001423 var val []byte
1424 path := fmt.Sprintf(McastQueuesForIntf)
1425
npujarec5762e2020-01-01 14:08:48 +05301426 mcastQueues, err := RsrcMgr.GetMcastQueuePerInterfaceMap(ctx)
Esin Karamanccb714b2019-11-29 15:02:06 +00001427 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001428 logger.Errorw("Failed to get multicast queue info for interface", log.Fields{"error": err, "intf": intf})
Esin Karamanccb714b2019-11-29 15:02:06 +00001429 return err
1430 }
1431 if mcastQueues == nil {
1432 mcastQueues = make(map[uint32][]uint32)
1433 }
1434 mcastQueues[intf] = []uint32{gem, servicePriority}
1435 if val, err = json.Marshal(mcastQueues); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001436 logger.Errorw("Failed to marshal data", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001437 return err
1438 }
npujarec5762e2020-01-01 14:08:48 +05301439 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001440 logger.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Esin Karamanccb714b2019-11-29 15:02:06 +00001441 return err
1442 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001443 logger.Debugw("added multicast queue info to KV store successfully", log.Fields{"path": path, "mcastQueueInfo": mcastQueues[intf], "interfaceId": intf})
Esin Karamanccb714b2019-11-29 15:02:06 +00001444 return nil
1445}
1446
1447//AddFlowGroupToKVStore adds flow group into KV store
npujarec5762e2020-01-01 14:08:48 +05301448func (RsrcMgr *OpenOltResourceMgr) AddFlowGroupToKVStore(ctx context.Context, groupEntry *ofp.OfpGroupEntry, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001449 var Value []byte
1450 var err error
1451 var path string
1452 if cached {
1453 path = fmt.Sprintf(FlowGroupCached, groupEntry.Desc.GroupId)
1454 } else {
1455 path = fmt.Sprintf(FlowGroup, groupEntry.Desc.GroupId)
1456 }
1457 //build group info object
1458 var outPorts []uint32
1459 for _, ofBucket := range groupEntry.Desc.Buckets {
1460 for _, ofAction := range ofBucket.Actions {
1461 if ofAction.Type == ofp.OfpActionType_OFPAT_OUTPUT {
1462 outPorts = append(outPorts, ofAction.GetOutput().Port)
1463 }
1464 }
1465 }
1466 groupInfo := GroupInfo{
1467 GroupID: groupEntry.Desc.GroupId,
1468 OutPorts: outPorts,
1469 }
1470
1471 Value, err = json.Marshal(groupInfo)
1472
1473 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001474 logger.Error("failed to Marshal flow group object")
Esin Karamanccb714b2019-11-29 15:02:06 +00001475 return err
1476 }
1477
npujarec5762e2020-01-01 14:08:48 +05301478 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001479 logger.Errorf("Failed to update resource %s", path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001480 return err
1481 }
1482 return nil
1483}
1484
1485//RemoveFlowGroupFromKVStore removes flow group from KV store
npujarec5762e2020-01-01 14:08:48 +05301486func (RsrcMgr *OpenOltResourceMgr) RemoveFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) bool {
Esin Karamanccb714b2019-11-29 15:02:06 +00001487 var path string
1488 if cached {
1489 path = fmt.Sprintf(FlowGroupCached, groupID)
1490 } else {
1491 path = fmt.Sprintf(FlowGroup, groupID)
1492 }
npujarec5762e2020-01-01 14:08:48 +05301493 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001494 logger.Errorf("Failed to remove resource %s due to %s", path, err)
Esin Karamanccb714b2019-11-29 15:02:06 +00001495 return false
1496 }
1497 return true
1498}
1499
1500//GetFlowGroupFromKVStore fetches flow group from the KV store. Returns (false, {} error) if any problem occurs during
1501//fetching the data. Returns (true, groupInfo, nil) if the group is fetched successfully.
1502// Returns (false, {}, nil) if the group does not exists in the KV store.
npujarec5762e2020-01-01 14:08:48 +05301503func (RsrcMgr *OpenOltResourceMgr) GetFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) (bool, GroupInfo, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001504 var groupInfo GroupInfo
1505 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 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001512 if err != nil {
1513 return false, groupInfo, err
1514 }
1515 if kvPair != nil && kvPair.Value != nil {
1516 Val, err := kvstore.ToByte(kvPair.Value)
1517 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001518 logger.Errorw("Failed to convert flow group into byte array", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001519 return false, groupInfo, err
1520 }
1521 if err = json.Unmarshal(Val, &groupInfo); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001522 logger.Errorw("Failed to unmarshal", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001523 return false, groupInfo, err
1524 }
1525 return true, groupInfo, nil
1526 }
1527 return false, groupInfo, nil
1528}