blob: 3557bbf06f7f7030b12f48e41c535531d6731088 [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 Sharma96b7bf22020-06-15 10:37:32 +0000125func newKVClient(ctx context.Context, storeType string, address string, timeout time.Duration) (kvstore.Client, error) {
126 logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400127 switch storeType {
128 case "consul":
Neha Sharma96b7bf22020-06-15 10:37:32 +0000129 return kvstore.NewConsulClient(ctx, address, timeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400130 case "etcd":
Neha Sharma96b7bf22020-06-15 10:37:32 +0000131 return kvstore.NewEtcdClient(ctx, 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 Sharma96b7bf22020-06-15 10:37:32 +0000137func SetKVClient(ctx context.Context, 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
Neha Sharma96b7bf22020-06-15 10:37:32 +0000140 kvClient, err := newKVClient(ctx, backend, addr, KvstoreTimeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400141 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000142 logger.Fatalw(ctx, "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 Sharma96b7bf22020-06-15 10:37:32 +0000161 logger.Debugf(ctx, "Init new resource manager , address: %s, deviceid: %s", KVStoreAddress, deviceID)
Neha Sharma3f221ae2020-04-29 19:02:12 +0000162 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 Sharma96b7bf22020-06-15 10:37:32 +0000168 ResourceMgr.KVStore = SetKVClient(ctx, Backend, ResourceMgr.Address, deviceID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400169 if ResourceMgr.KVStore == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000170 logger.Error(ctx, "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
Neha Sharma96b7bf22020-06-15 10:37:32 +0000233 logger.Debugf(ctx, "Device info technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400234 Ranges[technology] = TechRange
Neha Sharma96b7bf22020-06-15 10:37:32 +0000235
236 RsrcMgrsByTech[technology], err = ponrmgr.NewPONResourceManager(ctx, technology, deviceType, deviceID,
Neha Sharma3f221ae2020-04-29 19:02:12 +0000237 Backend, ResourceMgr.Address)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400238 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000239 logger.Errorf(ctx, "Failed to create pon resource manager instance for technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400240 return nil
241 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700242 // resource_mgrs_by_tech[technology] = resource_mgr
Girish Gowdru0c588b22019-04-23 23:24:56 -0400243 if GlobalPONRsrcMgr == nil {
244 GlobalPONRsrcMgr = RsrcMgrsByTech[technology]
245 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700246 for _, IntfID := range TechRange.IntfIds {
247 ResourceMgr.ResourceMgrs[uint32(IntfID)] = RsrcMgrsByTech[technology]
Girish Gowdru0c588b22019-04-23 23:24:56 -0400248 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700249 // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
npujarec5762e2020-01-01 14:08:48 +0530250 InitializeDeviceResourceRangeAndPool(ctx, RsrcMgrsByTech[technology], GlobalPONRsrcMgr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700251 TechRange, devInfo)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400252 }
253 // After we have initialized resource ranges, initialize the
254 // resource pools accordingly.
255 for _, PONRMgr := range RsrcMgrsByTech {
npujarec5762e2020-01-01 14:08:48 +0530256 _ = PONRMgr.InitDeviceResourcePool(ctx)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400257 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000258 logger.Info(ctx, "Initialization of resource manager success!")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400259 return &ResourceMgr
Abhilash S.L7f17e402019-03-15 17:40:41 +0530260}
261
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700262// InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply
263// device specific information. If KV doesn't exist
264// or is broader than the device, the device's information will
265// dictate the range limits
npujarec5762e2020-01-01 14:08:48 +0530266func InitializeDeviceResourceRangeAndPool(ctx context.Context, ponRMgr *ponrmgr.PONResourceManager, globalPONRMgr *ponrmgr.PONResourceManager,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700267 techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530268
Girish Gowdru0c588b22019-04-23 23:24:56 -0400269 // init the resource range pool according to the sharing type
Abhilash S.L7f17e402019-03-15 17:40:41 +0530270
Neha Sharma96b7bf22020-06-15 10:37:32 +0000271 logger.Debugf(ctx, "Resource range pool init for technology %s", ponRMgr.Technology)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700272 // first load from KV profiles
npujarec5762e2020-01-01 14:08:48 +0530273 status := ponRMgr.InitResourceRangesFromKVStore(ctx)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700274 if !status {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000275 logger.Debugf(ctx, "Failed to load resource ranges from KV store for tech %s", ponRMgr.Technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400276 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530277
Girish Gowdru0c588b22019-04-23 23:24:56 -0400278 /*
279 Then apply device specific information. If KV doesn't exist
Gamze Abakafee36392019-10-03 11:17:24 +0000280 or is broader than the device, the device's information will
Girish Gowdru0c588b22019-04-23 23:24:56 -0400281 dictate the range limits
282 */
Neha Sharma96b7bf22020-06-15 10:37:32 +0000283 logger.Debugw(ctx, "Using device info to init pon resource ranges", log.Fields{"Tech": ponRMgr.Technology})
Abhilash S.L7f17e402019-03-15 17:40:41 +0530284
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700285 ONUIDStart := devInfo.OnuIdStart
286 ONUIDEnd := devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400287 ONUIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
288 ONUIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700289 AllocIDStart := devInfo.AllocIdStart
290 AllocIDEnd := devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400291 AllocIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
292 AllocIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700293 GEMPortIDStart := devInfo.GemportIdStart
294 GEMPortIDEnd := devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400295 GEMPortIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
296 GEMPortIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700297 FlowIDStart := devInfo.FlowIdStart
298 FlowIDEnd := devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400299 FlowIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
300 FlowIDSharedPoolID := uint32(0)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530301
Girish Gowdru0c588b22019-04-23 23:24:56 -0400302 var FirstIntfPoolID uint32
303 var SharedPoolID uint32
Abhilash S.L7f17e402019-03-15 17:40:41 +0530304
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400305 /*
306 * 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 -0700307 * if resources are shared across interfaces then SharedPoolID is given a positive number.
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400308 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700309 for _, FirstIntfPoolID = range techRange.IntfIds {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400310 // skip the intf id 0
311 if FirstIntfPoolID == 0 {
312 continue
313 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400314 break
315 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530316
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700317 for _, RangePool := range techRange.Pools {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400318 if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400319 SharedPoolID = FirstIntfPoolID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400320 } else if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_SAME_TECH {
321 SharedPoolID = FirstIntfPoolID
322 } else {
323 SharedPoolID = 0
324 }
325 if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID {
326 ONUIDStart = RangePool.Start
327 ONUIDEnd = RangePool.End
328 ONUIDShared = RangePool.Sharing
329 ONUIDSharedPoolID = SharedPoolID
330 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID {
331 AllocIDStart = RangePool.Start
332 AllocIDEnd = RangePool.End
333 AllocIDShared = RangePool.Sharing
334 AllocIDSharedPoolID = SharedPoolID
335 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID {
336 GEMPortIDStart = RangePool.Start
337 GEMPortIDEnd = RangePool.End
338 GEMPortIDShared = RangePool.Sharing
339 GEMPortIDSharedPoolID = SharedPoolID
340 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID {
341 FlowIDStart = RangePool.Start
342 FlowIDEnd = RangePool.End
343 FlowIDShared = RangePool.Sharing
344 FlowIDSharedPoolID = SharedPoolID
345 }
346 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530347
Neha Sharma96b7bf22020-06-15 10:37:32 +0000348 logger.Debugw(ctx, "Device info init", log.Fields{"technology": techRange.Technology,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400349 "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd, "onu_id_shared_pool_id": ONUIDSharedPoolID,
350 "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd,
351 "alloc_id_shared_pool_id": AllocIDSharedPoolID,
352 "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd,
353 "gemport_id_shared_pool_id": GEMPortIDSharedPoolID,
354 "flow_id_start": FlowIDStart,
355 "flow_id_end_idx": FlowIDEnd,
356 "flow_id_shared_pool_id": FlowIDSharedPoolID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700357 "intf_ids": techRange.IntfIds,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400358 "uni_id_start": 0,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700359 "uni_id_end_idx": 1, /*MaxUNIIDperONU()*/
360 })
Abhilash S.L7f17e402019-03-15 17:40:41 +0530361
Neha Sharma96b7bf22020-06-15 10:37:32 +0000362 ponRMgr.InitDefaultPONResourceRanges(ctx, ONUIDStart, ONUIDEnd, ONUIDSharedPoolID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400363 AllocIDStart, AllocIDEnd, AllocIDSharedPoolID,
364 GEMPortIDStart, GEMPortIDEnd, GEMPortIDSharedPoolID,
365 FlowIDStart, FlowIDEnd, FlowIDSharedPoolID, 0, 1,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700366 devInfo.PonPorts, techRange.IntfIds)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530367
Girish Gowdru0c588b22019-04-23 23:24:56 -0400368 // For global sharing, make sure to refresh both local and global resource manager instances' range
Abhilash S.L7f17e402019-03-15 17:40:41 +0530369
Girish Gowdru0c588b22019-04-23 23:24:56 -0400370 if ONUIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000371 globalPONRMgr.UpdateRanges(ctx, ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400372 "", 0, nil)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000373 ponRMgr.UpdateRanges(ctx, ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700374 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400375 }
376 if AllocIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000377 globalPONRMgr.UpdateRanges(ctx, ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400378 "", 0, nil)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530379
Neha Sharma96b7bf22020-06-15 10:37:32 +0000380 ponRMgr.UpdateRanges(ctx, ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700381 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400382 }
383 if GEMPortIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000384 globalPONRMgr.UpdateRanges(ctx, ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400385 "", 0, nil)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000386 ponRMgr.UpdateRanges(ctx, ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700387 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400388 }
389 if FlowIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000390 globalPONRMgr.UpdateRanges(ctx, ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400391 "", 0, nil)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000392 ponRMgr.UpdateRanges(ctx, ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700393 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400394 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530395
Girish Gowdru0c588b22019-04-23 23:24:56 -0400396 // Make sure loaded range fits the platform bit encoding ranges
Neha Sharma96b7bf22020-06-15 10:37:32 +0000397 ponRMgr.UpdateRanges(ctx, ponrmgr.UNI_ID_START_IDX, 0, ponrmgr.UNI_ID_END_IDX /* TODO =OpenOltPlatform.MAX_UNIS_PER_ONU-1*/, 1, "", 0, nil)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530398}
399
Devmalya Paul495b94a2019-08-27 19:42:00 -0400400// Delete clears used resources for the particular olt device being deleted
npujarec5762e2020-01-01 14:08:48 +0530401func (RsrcMgr *OpenOltResourceMgr) Delete(ctx context.Context) error {
Devmalya Paul495b94a2019-08-27 19:42:00 -0400402 /* TODO
403 def __del__(self):
404 self.log.info("clearing-device-resource-pool")
405 for key, resource_mgr in self.resource_mgrs.iteritems():
406 resource_mgr.clear_device_resource_pool()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530407
Devmalya Paul495b94a2019-08-27 19:42:00 -0400408 def assert_pon_id_limit(self, pon_intf_id):
409 assert pon_intf_id in self.resource_mgrs
Abhilash S.L7f17e402019-03-15 17:40:41 +0530410
Devmalya Paul495b94a2019-08-27 19:42:00 -0400411 def assert_onu_id_limit(self, pon_intf_id, onu_id):
412 self.assert_pon_id_limit(pon_intf_id)
413 self.resource_mgrs[pon_intf_id].assert_resource_limits(onu_id, PONResourceManager.ONU_ID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530414
Devmalya Paul495b94a2019-08-27 19:42:00 -0400415 @property
416 def max_uni_id_per_onu(self):
417 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 +0530418
Devmalya Paul495b94a2019-08-27 19:42:00 -0400419 def assert_uni_id_limit(self, pon_intf_id, onu_id, uni_id):
420 self.assert_onu_id_limit(pon_intf_id, onu_id)
421 self.resource_mgrs[pon_intf_id].assert_resource_limits(uni_id, PONResourceManager.UNI_ID)
422 */
423 for _, rsrcMgr := range RsrcMgr.ResourceMgrs {
npujarec5762e2020-01-01 14:08:48 +0530424 if err := rsrcMgr.ClearDeviceResourcePool(ctx); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000425 logger.Debug(ctx, "Failed to clear device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400426 return err
427 }
428 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000429 logger.Debug(ctx, "Cleared device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400430 return nil
431}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530432
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700433// GetONUID returns the available OnuID for the given pon-port
npujarec5762e2020-01-01 14:08:48 +0530434func (RsrcMgr *OpenOltResourceMgr) GetONUID(ctx context.Context, ponIntfID uint32) (uint32, error) {
salmansiddiqui352a45c2019-08-19 10:15:36 +0000435 // Check if Pon Interface ID is present in Resource-manager-map
Girish Gowdrab77ded92020-04-08 11:45:05 -0700436 RsrcMgr.OnuIDMgmtLock[ponIntfID].Lock()
437 defer RsrcMgr.OnuIDMgmtLock[ponIntfID].Unlock()
438
salmansiddiqui352a45c2019-08-19 10:15:36 +0000439 if _, ok := RsrcMgr.ResourceMgrs[ponIntfID]; !ok {
440 err := errors.New("invalid-pon-interface-" + strconv.Itoa(int(ponIntfID)))
441 return 0, err
442 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400443 // Get ONU id for a provided pon interface ID.
npujarec5762e2020-01-01 14:08:48 +0530444 ONUID, err := RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ctx, ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400445 ponrmgr.ONU_ID, 1)
446 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000447 logger.Errorf(ctx, "Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700448 ponIntfID, ponrmgr.ONU_ID)
cbabuabf02352019-10-15 13:14:56 +0200449 return 0, err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400450 }
451 if ONUID != nil {
npujarec5762e2020-01-01 14:08:48 +0530452 RsrcMgr.ResourceMgrs[ponIntfID].InitResourceMap(ctx, fmt.Sprintf("%d,%d", ponIntfID, ONUID[0]))
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700453 return ONUID[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400454 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530455
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700456 return 0, err // return OnuID 0 on error
Abhilash S.L7f17e402019-03-15 17:40:41 +0530457}
458
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700459// GetFlowIDInfo returns the slice of flow info of the given pon-port
460// Note: For flows which trap from the NNI and not really associated with any particular
461// 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 +0530462func (RsrcMgr *OpenOltResourceMgr) GetFlowIDInfo(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32, flowID uint32) *[]FlowInfo {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530463 var flows []FlowInfo
464
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700465 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530466 if err := RsrcMgr.ResourceMgrs[ponIntfID].GetFlowIDInfo(ctx, FlowPath, flowID, &flows); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000467 logger.Errorw(ctx, "Error while getting flows from KV store", log.Fields{"flowId": flowID})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530468 return nil
469 }
470 if len(flows) == 0 {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000471 logger.Debugw(ctx, "No flowInfo found in KV store", log.Fields{"flowPath": FlowPath})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530472 return nil
473 }
474 return &flows
475}
476
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700477// GetCurrentFlowIDsForOnu fetches flow ID from the resource manager
478// Note: For flows which trap from the NNI and not really associated with any particular
479// 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 +0530480func (RsrcMgr *OpenOltResourceMgr) GetCurrentFlowIDsForOnu(ctx context.Context, PONIntfID uint32, ONUID int32, UNIID int32) []uint32 {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700481
Abhilash S.L8ee90712019-04-29 16:24:22 +0530482 FlowPath := fmt.Sprintf("%d,%d,%d", PONIntfID, ONUID, UNIID)
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700483 if mgrs, exist := RsrcMgr.ResourceMgrs[PONIntfID]; exist {
npujarec5762e2020-01-01 14:08:48 +0530484 return mgrs.GetCurrentFlowIDsForOnu(ctx, FlowPath)
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700485 }
486 return nil
Abhilash S.L8ee90712019-04-29 16:24:22 +0530487}
488
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700489// UpdateFlowIDInfo updates flow info for the given pon interface, onu id, and uni id
490// Note: For flows which trap from the NNI and not really associated with any particular
491// 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 +0530492func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDInfo(ctx context.Context, ponIntfID int32, onuID int32, uniID int32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700493 flowID uint32, flowData *[]FlowInfo) error {
494 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530495 return RsrcMgr.ResourceMgrs[uint32(ponIntfID)].UpdateFlowIDInfoForOnu(ctx, FlowPath, flowID, *flowData)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530496}
497
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700498// GetFlowID return flow ID for a given pon interface id, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530499func (RsrcMgr *OpenOltResourceMgr) GetFlowID(ctx context.Context, ponIntfID uint32, ONUID int32, uniID int32,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400500 gemportID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700501 flowStoreCookie uint64,
Gamze Abaka724d0852020-03-18 12:10:24 +0000502 flowCategory string, vlanVid uint32, vlanPcp ...uint32) (uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530503
Girish Gowdru0c588b22019-04-23 23:24:56 -0400504 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700505 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, ONUID, uniID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700506
507 RsrcMgr.FlowIDMgmtLock.Lock()
508 defer RsrcMgr.FlowIDMgmtLock.Unlock()
509
npujarec5762e2020-01-01 14:08:48 +0530510 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(ctx, FlowPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400511 if FlowIDs != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000512 logger.Debugw(ctx, "Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "ONUID": ONUID, "uniID": uniID, "KVpath": FlowPath})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700513 for _, flowID := range FlowIDs {
npujarec5762e2020-01-01 14:08:48 +0530514 FlowInfo := RsrcMgr.GetFlowIDInfo(ctx, ponIntfID, int32(ONUID), int32(uniID), uint32(flowID))
Neha Sharma96b7bf22020-06-15 10:37:32 +0000515 er := getFlowIDFromFlowInfo(ctx, FlowInfo, flowID, gemportID, flowStoreCookie, flowCategory, vlanVid, vlanPcp...)
salmansiddiqui7ac62132019-08-22 03:58:50 +0000516 if er == nil {
Gamze Abaka724d0852020-03-18 12:10:24 +0000517 log.Debugw("Found flowid for the vlan, pcp, and gem",
518 log.Fields{"flowID": flowID, "vlanVid": vlanVid, "vlanPcp": vlanPcp, "gemPortID": gemportID})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000519 return flowID, er
Abhilash S.L8ee90712019-04-29 16:24:22 +0530520 }
521 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400522 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000523 logger.Debug(ctx, "No matching flows with flow cookie or flow category, allocating new flowid")
npujarec5762e2020-01-01 14:08:48 +0530524 FlowIDs, err = RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ctx, ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400525 ponrmgr.FLOW_ID, 1)
526 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000527 logger.Errorf(ctx, "Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700528 ponIntfID, ponrmgr.FLOW_ID)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400529 return uint32(0), err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400530 }
531 if FlowIDs != nil {
npujarec5762e2020-01-01 14:08:48 +0530532 _ = RsrcMgr.ResourceMgrs[ponIntfID].UpdateFlowIDForOnu(ctx, FlowPath, FlowIDs[0], true)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700533 return FlowIDs[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400534 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530535
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700536 return 0, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530537}
538
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700539// GetAllocID return the first Alloc ID for a given pon interface id and onu id and then update the resource map on
540// the KV store with the list of alloc_ids allocated for the pon_intf_onu_id tuple
541// 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 +0530542func (RsrcMgr *OpenOltResourceMgr) GetAllocID(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530543
Girish Gowdru0c588b22019-04-23 23:24:56 -0400544 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700545 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700546
547 RsrcMgr.AllocIDMgmtLock[intfID].Lock()
548 defer RsrcMgr.AllocIDMgmtLock[intfID].Unlock()
549
npujarec5762e2020-01-01 14:08:48 +0530550 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400551 if AllocID != nil {
552 // Since we support only one alloc_id for the ONU at the moment,
553 // return the first alloc_id in the list, if available, for that
554 // ONU.
Neha Sharma96b7bf22020-06-15 10:37:32 +0000555 logger.Debugw(ctx, "Retrieved alloc ID from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400556 return AllocID[0]
557 }
npujarec5762e2020-01-01 14:08:48 +0530558 AllocID, err = RsrcMgr.ResourceMgrs[intfID].GetResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400559 ponrmgr.ALLOC_ID, 1)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530560
Girish Gowdru0c588b22019-04-23 23:24:56 -0400561 if AllocID == nil || err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000562 logger.Error(ctx, "Failed to allocate alloc id")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400563 return 0
564 }
565 // update the resource map on KV store with the list of alloc_id
566 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530567 err = RsrcMgr.ResourceMgrs[intfID].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID, AllocID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400568 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000569 logger.Error(ctx, "Failed to update Alloc ID")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400570 return 0
571 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000572 logger.Debugw(ctx, "Allocated new Tcont from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400573 return AllocID[0]
Abhilash S.L7f17e402019-03-15 17:40:41 +0530574}
575
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700576// UpdateAllocIdsForOnu updates alloc ids in kv store for a given pon interface id, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530577func (RsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ctx context.Context, ponPort uint32, onuID uint32, uniID uint32, allocID []uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530578
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700579 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530580 return RsrcMgr.ResourceMgrs[ponPort].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700581 allocID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530582}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700583
584// GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530585func (RsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(ctx context.Context, intfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700586 uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530587
Girish Gowdru0c588b22019-04-23 23:24:56 -0400588 /* Get gem ports for given pon interface , onu id and uni id. */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530589
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700590 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530591 return RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530592}
593
Gamze Abakafee36392019-10-03 11:17:24 +0000594// GetCurrentAllocIDsForOnu returns alloc ids for given pon interface and onu id
npujarec5762e2020-01-01 14:08:48 +0530595func (RsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDsForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530596
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700597 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530598 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400599 if AllocID != nil {
Gamze Abakafee36392019-10-03 11:17:24 +0000600 return AllocID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400601 }
Gamze Abakafee36392019-10-03 11:17:24 +0000602 return []uint32{}
603}
604
605// RemoveAllocIDForOnu removes the alloc id for given pon interface, onu id, uni id and alloc id
npujarec5762e2020-01-01 14:08:48 +0530606func (RsrcMgr *OpenOltResourceMgr) RemoveAllocIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, allocID uint32) {
607 allocIDs := RsrcMgr.GetCurrentAllocIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000608 for i := 0; i < len(allocIDs); i++ {
609 if allocIDs[i] == allocID {
610 allocIDs = append(allocIDs[:i], allocIDs[i+1:]...)
611 break
612 }
613 }
npujarec5762e2020-01-01 14:08:48 +0530614 err := RsrcMgr.UpdateAllocIdsForOnu(ctx, intfID, onuID, uniID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000615 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000616 logger.Errorf(ctx, "Failed to Remove Alloc Id For Onu. IntfID %d onuID %d uniID %d allocID %d",
Gamze Abakafee36392019-10-03 11:17:24 +0000617 intfID, onuID, uniID, allocID)
618 }
619}
620
621// RemoveGemPortIDForOnu removes the gem port id for given pon interface, onu id, uni id and gem port id
npujarec5762e2020-01-01 14:08:48 +0530622func (RsrcMgr *OpenOltResourceMgr) RemoveGemPortIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, gemPortID uint32) {
623 gemPortIDs := RsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000624 for i := 0; i < len(gemPortIDs); i++ {
625 if gemPortIDs[i] == gemPortID {
626 gemPortIDs = append(gemPortIDs[:i], gemPortIDs[i+1:]...)
627 break
628 }
629 }
npujarec5762e2020-01-01 14:08:48 +0530630 err := RsrcMgr.UpdateGEMPortIDsForOnu(ctx, intfID, onuID, uniID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000631 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000632 logger.Errorf(ctx, "Failed to Remove Gem Id For Onu. IntfID %d onuID %d uniID %d gemPortId %d",
Gamze Abakafee36392019-10-03 11:17:24 +0000633 intfID, onuID, uniID, gemPortID)
634 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530635}
636
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700637// UpdateGEMportsPonportToOnuMapOnKVStore updates onu and uni id associated with the gem port to the kv store
638// This stored information is used when packet_indication is received and we need to derive the ONU Id for which
639// the packet arrived based on the pon_intf and gemport available in the packet_indication
npujarec5762e2020-01-01 14:08:48 +0530640func (RsrcMgr *OpenOltResourceMgr) UpdateGEMportsPonportToOnuMapOnKVStore(ctx context.Context, gemPorts []uint32, PonPort uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700641 onuID uint32, uniID uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530642
Girish Gowdru0c588b22019-04-23 23:24:56 -0400643 /* Update onu and uni id associated with the gem port to the kv store. */
644 var IntfGEMPortPath string
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700645 Data := fmt.Sprintf("%d %d", onuID, uniID)
646 for _, GEM := range gemPorts {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400647 IntfGEMPortPath = fmt.Sprintf("%d,%d", PonPort, GEM)
648 Val, err := json.Marshal(Data)
649 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000650 logger.Error(ctx, "failed to Marshal")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400651 return err
652 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700653
npujarec5762e2020-01-01 14:08:48 +0530654 if err = RsrcMgr.KVStore.Put(ctx, IntfGEMPortPath, Val); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000655 logger.Errorf(ctx, "Failed to update resource %s", IntfGEMPortPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400656 return err
657 }
658 }
659 return nil
Abhilash S.L7f17e402019-03-15 17:40:41 +0530660}
661
Gamze Abakafee36392019-10-03 11:17:24 +0000662// RemoveGEMportPonportToOnuMapOnKVStore removes the relationship between the gem port and pon port
npujarec5762e2020-01-01 14:08:48 +0530663func (RsrcMgr *OpenOltResourceMgr) RemoveGEMportPonportToOnuMapOnKVStore(ctx context.Context, GemPort uint32, PonPort uint32) {
Gamze Abakafee36392019-10-03 11:17:24 +0000664 IntfGEMPortPath := fmt.Sprintf("%d,%d", PonPort, GemPort)
npujarec5762e2020-01-01 14:08:48 +0530665 err := RsrcMgr.KVStore.Delete(ctx, IntfGEMPortPath)
Gamze Abakafee36392019-10-03 11:17:24 +0000666 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000667 logger.Errorf(ctx, "Failed to Remove Gem port-Pon port to onu map on kv store. Gem %d PonPort %d", GemPort, PonPort)
Gamze Abakafee36392019-10-03 11:17:24 +0000668 }
669}
670
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700671// GetGEMPortID gets gem port id for a particular pon port, onu id and uni id and then update the resource map on
672// the KV store with the list of gemport_id allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530673func (RsrcMgr *OpenOltResourceMgr) GetGEMPortID(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700674 uniID uint32, NumOfPorts uint32) ([]uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530675
Girish Gowdru0c588b22019-04-23 23:24:56 -0400676 /* Get gem port id for a particular pon port, onu id
677 and uni id.
678 */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530679
Girish Gowdru0c588b22019-04-23 23:24:56 -0400680 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700681 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530682
Girish Gowdrab77ded92020-04-08 11:45:05 -0700683 RsrcMgr.GemPortIDMgmtLock[ponPort].Lock()
684 defer RsrcMgr.GemPortIDMgmtLock[ponPort].Unlock()
685
npujarec5762e2020-01-01 14:08:48 +0530686 GEMPortList := RsrcMgr.ResourceMgrs[ponPort].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400687 if GEMPortList != nil {
688 return GEMPortList, nil
689 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530690
npujarec5762e2020-01-01 14:08:48 +0530691 GEMPortList, err = RsrcMgr.ResourceMgrs[ponPort].GetResourceID(ctx, ponPort,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400692 ponrmgr.GEMPORT_ID, NumOfPorts)
693 if err != nil && GEMPortList == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000694 logger.Errorf(ctx, "Failed to get gem port id for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400695 return nil, err
696 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530697
Girish Gowdru0c588b22019-04-23 23:24:56 -0400698 // update the resource map on KV store with the list of gemport_id
699 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530700 err = RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400701 GEMPortList)
702 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000703 logger.Errorf(ctx, "Failed to update GEM ports to kv store for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400704 return nil, err
705 }
npujarec5762e2020-01-01 14:08:48 +0530706 _ = RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(ctx, GEMPortList, ponPort,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700707 onuID, uniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400708 return GEMPortList, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530709}
710
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700711// 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 +0530712func (RsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700713 uniID uint32, GEMPortList []uint32) error {
714 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530715 return RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400716 GEMPortList)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530717
718}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700719
720// FreeonuID releases(make free) onu id for a particular pon-port
npujarec5762e2020-01-01 14:08:48 +0530721func (RsrcMgr *OpenOltResourceMgr) FreeonuID(ctx context.Context, intfID uint32, onuID []uint32) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700722
Girish Gowdra38d533d2020-03-30 20:38:51 -0700723 RsrcMgr.OnuIDMgmtLock[intfID].Lock()
Girish Gowdrab77ded92020-04-08 11:45:05 -0700724 defer RsrcMgr.OnuIDMgmtLock[intfID].Unlock()
725
npujarec5762e2020-01-01 14:08:48 +0530726 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID, ponrmgr.ONU_ID, onuID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530727
Girish Gowdru0c588b22019-04-23 23:24:56 -0400728 /* Free onu id for a particular interface.*/
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700729 var IntfonuID string
730 for _, onu := range onuID {
731 IntfonuID = fmt.Sprintf("%d,%d", intfID, onu)
npujarec5762e2020-01-01 14:08:48 +0530732 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfonuID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400733 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530734}
735
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700736// FreeFlowID returns the free flow id for a given interface, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530737func (RsrcMgr *OpenOltResourceMgr) FreeFlowID(ctx context.Context, IntfID uint32, onuID int32,
Devmalya Paul495b94a2019-08-27 19:42:00 -0400738 uniID int32, FlowID uint32) {
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400739 var IntfONUID string
740 var err error
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530741
Girish Gowdrab77ded92020-04-08 11:45:05 -0700742 RsrcMgr.FlowIDMgmtLock.Lock()
743 defer RsrcMgr.FlowIDMgmtLock.Unlock()
744
745 FlowIds := make([]uint32, 0)
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530746 FlowIds = append(FlowIds, FlowID)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700747 IntfONUID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530748 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(ctx, IntfONUID, FlowID, false)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400749 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000750 logger.Errorw(ctx, "Failed to Update flow id for", log.Fields{"intf": IntfONUID})
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400751 }
npujarec5762e2020-01-01 14:08:48 +0530752 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(ctx, IntfONUID, FlowID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700753
npujarec5762e2020-01-01 14:08:48 +0530754 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.FLOW_ID, FlowIds)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400755}
756
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700757// FreeFlowIDs releases the flow Ids
npujarec5762e2020-01-01 14:08:48 +0530758func (RsrcMgr *OpenOltResourceMgr) FreeFlowIDs(ctx context.Context, IntfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700759 uniID uint32, FlowID []uint32) {
Girish Gowdra38d533d2020-03-30 20:38:51 -0700760 RsrcMgr.FlowIDMgmtLock.Lock()
Girish Gowdrab77ded92020-04-08 11:45:05 -0700761 defer RsrcMgr.FlowIDMgmtLock.Unlock()
762
npujarec5762e2020-01-01 14:08:48 +0530763 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.FLOW_ID, FlowID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530764
Abhilash S.L8ee90712019-04-29 16:24:22 +0530765 var IntfOnuIDUniID string
Girish Gowdru0c588b22019-04-23 23:24:56 -0400766 var err error
767 for _, flow := range FlowID {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700768 IntfOnuIDUniID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530769 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(ctx, IntfOnuIDUniID, flow, false)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400770 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000771 logger.Errorw(ctx, "Failed to Update flow id for", log.Fields{"intf": IntfOnuIDUniID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400772 }
npujarec5762e2020-01-01 14:08:48 +0530773 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(ctx, IntfOnuIDUniID, flow)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400774 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530775}
776
Gamze Abakafee36392019-10-03 11:17:24 +0000777// FreeAllocID frees AllocID on the PON resource pool and also frees the allocID association
778// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530779func (RsrcMgr *OpenOltResourceMgr) FreeAllocID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000780 uniID uint32, allocID uint32) {
Girish Gowdrab77ded92020-04-08 11:45:05 -0700781 RsrcMgr.AllocIDMgmtLock[IntfID].Lock()
782 defer RsrcMgr.AllocIDMgmtLock[IntfID].Unlock()
783
npujarec5762e2020-01-01 14:08:48 +0530784 RsrcMgr.RemoveAllocIDForOnu(ctx, IntfID, onuID, uniID, allocID)
Gamze Abakafee36392019-10-03 11:17:24 +0000785 allocIDs := make([]uint32, 0)
786 allocIDs = append(allocIDs, allocID)
npujarec5762e2020-01-01 14:08:48 +0530787 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.ALLOC_ID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000788}
789
790// FreeGemPortID frees GemPortID on the PON resource pool and also frees the gemPortID association
791// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530792func (RsrcMgr *OpenOltResourceMgr) FreeGemPortID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000793 uniID uint32, gemPortID uint32) {
Girish Gowdrab77ded92020-04-08 11:45:05 -0700794 RsrcMgr.GemPortIDMgmtLock[IntfID].Lock()
795 defer RsrcMgr.GemPortIDMgmtLock[IntfID].Unlock()
796
npujarec5762e2020-01-01 14:08:48 +0530797 RsrcMgr.RemoveGemPortIDForOnu(ctx, IntfID, onuID, uniID, gemPortID)
Gamze Abakafee36392019-10-03 11:17:24 +0000798 gemPortIDs := make([]uint32, 0)
799 gemPortIDs = append(gemPortIDs, gemPortID)
npujarec5762e2020-01-01 14:08:48 +0530800 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.GEMPORT_ID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000801}
802
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700803// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id, and the clears the
804// resource map and the onuID associated with (pon_intf_id, gemport_id) tuple,
npujarec5762e2020-01-01 14:08:48 +0530805func (RsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530806
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700807 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530808
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700809 RsrcMgr.AllocIDMgmtLock[intfID].Lock()
Girish Gowdrab77ded92020-04-08 11:45:05 -0700810 AllocIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700811
npujarec5762e2020-01-01 14:08:48 +0530812 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400813 ponrmgr.ALLOC_ID,
814 AllocIDs)
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700815 RsrcMgr.AllocIDMgmtLock[intfID].Unlock()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530816
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700817 RsrcMgr.GemPortIDMgmtLock[intfID].Lock()
npujarec5762e2020-01-01 14:08:48 +0530818 GEMPortIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
819 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400820 ponrmgr.GEMPORT_ID,
821 GEMPortIDs)
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700822 RsrcMgr.GemPortIDMgmtLock[intfID].Unlock()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530823
Girish Gowdra38d533d2020-03-30 20:38:51 -0700824 RsrcMgr.FlowIDMgmtLock.Lock()
npujarec5762e2020-01-01 14:08:48 +0530825 FlowIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentFlowIDsForOnu(ctx, IntfOnuIDUniID)
826 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400827 ponrmgr.FLOW_ID,
828 FlowIDs)
Girish Gowdra38d533d2020-03-30 20:38:51 -0700829 RsrcMgr.FlowIDMgmtLock.Unlock()
830
Girish Gowdru0c588b22019-04-23 23:24:56 -0400831 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
npujarec5762e2020-01-01 14:08:48 +0530832 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400833 // Clear the ONU Id associated with the (pon_intf_id, gemport_id) tuple.
834 for _, GEM := range GEMPortIDs {
npujarec5762e2020-01-01 14:08:48 +0530835 _ = RsrcMgr.KVStore.Delete(ctx, fmt.Sprintf("%d,%d", intfID, GEM))
Girish Gowdru0c588b22019-04-23 23:24:56 -0400836 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530837}
838
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700839// IsFlowCookieOnKVStore checks if the given flow cookie is present on the kv store
840// Returns true if the flow cookie is found, otherwise it returns false
npujarec5762e2020-01-01 14:08:48 +0530841func (RsrcMgr *OpenOltResourceMgr) IsFlowCookieOnKVStore(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700842 flowStoreCookie uint64) bool {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530843
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700844 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530845 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(ctx, FlowPath)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400846 if FlowIDs != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000847 logger.Debugw(ctx, "Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "onuID": onuID, "uniID": uniID, "KVpath": FlowPath})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700848 for _, flowID := range FlowIDs {
npujarec5762e2020-01-01 14:08:48 +0530849 FlowInfo := RsrcMgr.GetFlowIDInfo(ctx, ponIntfID, int32(onuID), int32(uniID), uint32(flowID))
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400850 if FlowInfo != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000851 logger.Debugw(ctx, "Found flows", log.Fields{"flows": *FlowInfo, "flowId": flowID})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400852 for _, Info := range *FlowInfo {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700853 if Info.FlowStoreCookie == flowStoreCookie {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000854 logger.Debug(ctx, "Found flow matching with flowStore cookie", log.Fields{"flowId": flowID, "flowStoreCookie": flowStoreCookie})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400855 return true
856 }
857 }
858 }
859 }
860 }
861 return false
862}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400863
salmansiddiqui7ac62132019-08-22 03:58:50 +0000864// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000865// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530866func (RsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) []uint32 {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000867 Path := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000868 var Data []uint32
npujarec5762e2020-01-01 14:08:48 +0530869 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400870 if err == nil {
871 if Value != nil {
872 Val, err := kvstore.ToByte(Value.Value)
873 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000874 logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"error": err})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400875 return Data
876 }
877 if err = json.Unmarshal(Val, &Data); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000878 logger.Error(ctx, "Failed to unmarshal", log.Fields{"error": err})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400879 return Data
880 }
881 }
882 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000883 logger.Errorf(ctx, "Failed to get TP id from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400884 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000885 logger.Debugf(ctx, "Getting TP id %d from path %s", Data, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400886 return Data
887
888}
889
Gamze Abakafee36392019-10-03 11:17:24 +0000890// RemoveTechProfileIDsForOnu deletes all tech profile ids from the KV-Store for the given onu based on the path
891// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530892func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDsForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) error {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000893 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
npujarec5762e2020-01-01 14:08:48 +0530894 if err := RsrcMgr.KVStore.Delete(ctx, IntfOnuUniID); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000895 logger.Errorw(ctx, "Failed to delete techprofile id resource in KV store", log.Fields{"path": IntfOnuUniID})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400896 return err
897 }
898 return nil
899}
900
Gamze Abakafee36392019-10-03 11:17:24 +0000901// RemoveTechProfileIDForOnu deletes a specific tech profile id from the KV-Store for the given onu based on the path
902// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530903func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32, TpID uint32) error {
904 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000905 for i, tpIDInList := range tpIDList {
906 if tpIDInList == TpID {
907 tpIDList = append(tpIDList[:i], tpIDList[i+1:]...)
908 }
909 }
910 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
911 Value, err := json.Marshal(tpIDList)
912 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000913 logger.Error(ctx, "failed to Marshal")
Gamze Abakafee36392019-10-03 11:17:24 +0000914 return err
915 }
npujarec5762e2020-01-01 14:08:48 +0530916 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000917 logger.Errorf(ctx, "Failed to update resource %s", IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000918 return err
919 }
920 return err
921}
922
923// UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
924// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530925func (RsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32,
salmansiddiqui7ac62132019-08-22 03:58:50 +0000926 UniID uint32, TpID uint32) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400927 var Value []byte
928 var err error
929
salmansiddiqui7ac62132019-08-22 03:58:50 +0000930 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000931
npujarec5762e2020-01-01 14:08:48 +0530932 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000933 for _, value := range tpIDList {
934 if value == TpID {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000935 logger.Debugf(ctx, "TpID %d is already in tpIdList for the path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000936 return err
937 }
938 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000939 logger.Debugf(ctx, "updating tp id %d on path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000940 tpIDList = append(tpIDList, TpID)
941 Value, err = json.Marshal(tpIDList)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400942 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000943 logger.Error(ctx, "failed to Marshal")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400944 return err
945 }
npujarec5762e2020-01-01 14:08:48 +0530946 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000947 logger.Errorf(ctx, "Failed to update resource %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400948 return err
949 }
950 return err
951}
952
salmansiddiqui7ac62132019-08-22 03:58:50 +0000953// UpdateMeterIDForOnu updates the meter id in the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000954// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +0530955func (RsrcMgr *OpenOltResourceMgr) UpdateMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000956 UniID uint32, TpID uint32, MeterConfig *ofp.OfpMeterConfig) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400957 var Value []byte
958 var err error
959
Gamze Abakafee36392019-10-03 11:17:24 +0000960 IntfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400961 Value, err = json.Marshal(*MeterConfig)
962 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000963 logger.Error(ctx, "failed to Marshal meter config")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400964 return err
965 }
npujarec5762e2020-01-01 14:08:48 +0530966 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000967 logger.Errorf(ctx, "Failed to store meter into KV store %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400968 return err
969 }
970 return err
971}
972
Gamze Abakafee36392019-10-03 11:17:24 +0000973// GetMeterIDForOnu fetches the meter id from the kv store for the given onu based on the path
974// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +0530975func (RsrcMgr *OpenOltResourceMgr) GetMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000976 UniID uint32, TpID uint32) (*ofp.OfpMeterConfig, error) {
977 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400978 var meterConfig ofp.OfpMeterConfig
npujarec5762e2020-01-01 14:08:48 +0530979 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400980 if err == nil {
981 if Value != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000982 logger.Debug(ctx, "Found meter in KV store", log.Fields{"Direction": Direction})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000983 Val, er := kvstore.ToByte(Value.Value)
984 if er != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000985 logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"error": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000986 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400987 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000988 if er = json.Unmarshal(Val, &meterConfig); er != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000989 logger.Error(ctx, "Failed to unmarshal meterconfig", log.Fields{"error": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000990 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400991 }
992 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000993 logger.Debug(ctx, "meter-does-not-exists-in-KVStore")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400994 return nil, err
995 }
996 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000997 logger.Errorf(ctx, "Failed to get Meter config from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400998
999 }
1000 return &meterConfig, err
1001}
1002
Gamze Abakafee36392019-10-03 11:17:24 +00001003// RemoveMeterIDForOnu deletes the meter id from the kV-Store for the given onu based on the path
1004// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +05301005func (RsrcMgr *OpenOltResourceMgr) RemoveMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +00001006 UniID uint32, TpID uint32) error {
1007 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
npujarec5762e2020-01-01 14:08:48 +05301008 if err := RsrcMgr.KVStore.Delete(ctx, Path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001009 logger.Errorf(ctx, "Failed to delete meter id %s from kvstore ", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001010 return err
1011 }
1012 return nil
1013}
salmansiddiqui7ac62132019-08-22 03:58:50 +00001014
Neha Sharma96b7bf22020-06-15 10:37:32 +00001015func getFlowIDFromFlowInfo(ctx context.Context, FlowInfo *[]FlowInfo, flowID, gemportID uint32, flowStoreCookie uint64, flowCategory string,
Gamze Abaka724d0852020-03-18 12:10:24 +00001016 vlanVid uint32, vlanPcp ...uint32) error {
salmansiddiqui7ac62132019-08-22 03:58:50 +00001017 if FlowInfo != nil {
1018 for _, Info := range *FlowInfo {
1019 if int32(gemportID) == Info.Flow.GemportId && flowCategory != "" && Info.FlowCategory == flowCategory {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001020 logger.Debug(ctx, "Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
Gamze Abaka724d0852020-03-18 12:10:24 +00001021 if Info.FlowCategory == "HSIA_FLOW" {
1022 if err := checkVlanAndPbitEqualityForFlows(vlanVid, Info, vlanPcp[0]); err == nil {
1023 return nil
1024 }
salmansiddiqui7ac62132019-08-22 03:58:50 +00001025 }
1026 }
1027 if int32(gemportID) == Info.Flow.GemportId && flowStoreCookie != 0 && Info.FlowStoreCookie == flowStoreCookie {
1028 if flowCategory != "" && Info.FlowCategory == flowCategory {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001029 logger.Debug(ctx, "Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001030 return nil
1031 }
1032 }
1033 }
1034 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001035 logger.Debugw(ctx, "the flow can be related to a different service", log.Fields{"flow_info": FlowInfo})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001036 return errors.New("invalid flow-info")
1037}
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301038
Gamze Abaka724d0852020-03-18 12:10:24 +00001039func checkVlanAndPbitEqualityForFlows(vlanVid uint32, Info FlowInfo, vlanPcp uint32) error {
1040 if err := checkVlanEqualityForFlows(vlanVid, Info); err != nil {
1041 return err
1042 }
1043
1044 //flow has remark action and pbits
1045 if Info.Flow.Action.Cmd.RemarkInnerPbits || Info.Flow.Action.Cmd.RemarkOuterPbits {
1046 if vlanPcp == Info.Flow.Action.OPbits || vlanPcp == Info.Flow.Action.IPbits {
1047 return nil
1048 }
1049 } else if vlanPcp == Info.Flow.Classifier.OPbits {
1050 //no remark action but flow has pbits
1051 return nil
1052 } else if vlanPcp == 0xff || Info.Flow.Classifier.OPbits == 0xff {
1053 // no pbit found
1054 return nil
1055 }
1056 return errors.New("not found in terms of pbit equality")
1057}
1058
1059func checkVlanEqualityForFlows(vlanVid uint32, Info FlowInfo) error {
1060 if vlanVid == Info.Flow.Action.OVid || vlanVid == Info.Flow.Classifier.IVid {
1061 return nil
1062 }
1063 return errors.New("not found in terms of vlan_id equality")
1064}
1065
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301066//AddGemToOnuGemInfo adds gemport to onugem info kvstore
npujarec5762e2020-01-01 14:08:48 +05301067func (RsrcMgr *OpenOltResourceMgr) AddGemToOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, gemPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301068 var onuGemData []OnuGemInfo
1069 var err error
1070
npujarec5762e2020-01-01 14:08:48 +05301071 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001072 logger.Errorf(ctx, "failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301073 return err
1074 }
1075 if len(onuGemData) == 0 {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001076 logger.Errorw(ctx, "failed to ger Onuid info ", log.Fields{"intfid": intfID, "onuid": onuID})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301077 return err
1078 }
1079
1080 for idx, onugem := range onuGemData {
1081 if onugem.OnuID == onuID {
1082 for _, gem := range onuGemData[idx].GemPorts {
1083 if gem == gemPort {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001084 logger.Debugw(ctx, "Gem already present in onugem info, skpping addition", log.Fields{"gem": gem})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301085 return nil
1086 }
1087 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001088 logger.Debugw(ctx, "Added gem to onugem info", log.Fields{"gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301089 onuGemData[idx].GemPorts = append(onuGemData[idx].GemPorts, gemPort)
1090 break
1091 }
1092 }
npujarec5762e2020-01-01 14:08:48 +05301093 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301094 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001095 logger.Error(ctx, "Failed to add onugem to kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301096 return err
1097 }
1098 return err
1099}
1100
1101//GetOnuGemInfo gets onu gem info from the kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301102func (RsrcMgr *OpenOltResourceMgr) GetOnuGemInfo(ctx context.Context, IntfID uint32) ([]OnuGemInfo, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301103 var onuGemData []OnuGemInfo
1104
npujarec5762e2020-01-01 14:08:48 +05301105 if err := RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001106 logger.Errorf(ctx, "failed to get onuifo for intfid %d", IntfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301107 return nil, err
1108 }
1109
1110 return onuGemData, nil
1111}
1112
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001113// AddOnuGemInfo adds onu info on to the kvstore per interface
1114func (RsrcMgr *OpenOltResourceMgr) AddOnuGemInfo(ctx context.Context, IntfID uint32, onuGem OnuGemInfo) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301115 var onuGemData []OnuGemInfo
1116 var err error
1117
npujarec5762e2020-01-01 14:08:48 +05301118 if err = RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001119 logger.Errorf(ctx, "failed to get onuifo for intfid %d", IntfID)
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001120 return olterrors.NewErrPersistence("get", "OnuGemInfo", IntfID,
1121 log.Fields{"onuGem": onuGem, "intfID": IntfID}, err)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301122 }
1123 onuGemData = append(onuGemData, onuGem)
npujarec5762e2020-01-01 14:08:48 +05301124 err = RsrcMgr.ResourceMgrs[IntfID].AddOnuGemInfo(ctx, IntfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301125 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001126 logger.Error(ctx, "Failed to add onugem to kv store")
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001127 return olterrors.NewErrPersistence("set", "OnuGemInfo", IntfID,
1128 log.Fields{"onuGemData": onuGemData, "intfID": IntfID}, err)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301129 }
1130
Neha Sharma96b7bf22020-06-15 10:37:32 +00001131 logger.Debugw(ctx, "added onu to onugeminfo", log.Fields{"intf": IntfID, "onugem": onuGem})
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001132 return nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301133}
1134
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301135// 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 +05301136func (RsrcMgr *OpenOltResourceMgr) AddUniPortToOnuInfo(ctx context.Context, intfID uint32, onuID uint32, portNo uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301137 var onuGemData []OnuGemInfo
1138 var err error
1139
npujarec5762e2020-01-01 14:08:48 +05301140 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001141 logger.Errorf(ctx, "failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301142 return
1143 }
1144 for idx, onu := range onuGemData {
1145 if onu.OnuID == onuID {
1146 for _, uni := range onu.UniPorts {
1147 if uni == portNo {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001148 logger.Debugw(ctx, "uni already present in onugem info", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301149 return
1150 }
1151 }
1152 onuGemData[idx].UniPorts = append(onuGemData[idx].UniPorts, portNo)
1153 break
1154 }
1155 }
npujarec5762e2020-01-01 14:08:48 +05301156 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301157 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001158 logger.Errorw(ctx, "Failed to add uin port in onugem to kv store", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301159 return
1160 }
1161 return
1162}
1163
1164//UpdateGemPortForPktIn updates gemport for pkt in path to kvstore, path being intfid, onuid, portno
npujarec5762e2020-01-01 14:08:48 +05301165func (RsrcMgr *OpenOltResourceMgr) UpdateGemPortForPktIn(ctx context.Context, pktIn PacketInInfoKey, gemPort uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301166
1167 path := fmt.Sprintf(OnuPacketINPath, pktIn.IntfID, pktIn.OnuID, pktIn.LogicalPort)
1168 Value, err := json.Marshal(gemPort)
1169 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001170 logger.Error(ctx, "Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301171 return
1172 }
npujarec5762e2020-01-01 14:08:48 +05301173 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001174 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"path": path, "value": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301175 return
1176 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001177 logger.Debugw(ctx, "added gem packet in successfully", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301178
1179 return
1180}
1181
1182// GetGemPortFromOnuPktIn gets the gem port from onu pkt in path, path being intfid, onuid, portno
npujarec5762e2020-01-01 14:08:48 +05301183func (RsrcMgr *OpenOltResourceMgr) GetGemPortFromOnuPktIn(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) (uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301184
1185 var Val []byte
1186 var gemPort uint32
1187
1188 path := fmt.Sprintf(OnuPacketINPath, intfID, onuID, logicalPort)
1189
npujarec5762e2020-01-01 14:08:48 +05301190 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301191 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001192 logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301193 return uint32(0), err
1194 } else if value == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001195 logger.Debugw(ctx, "No pkt in gem found", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301196 return uint32(0), nil
1197 }
1198
1199 if Val, err = kvstore.ToByte(value.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001200 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301201 return uint32(0), err
1202 }
1203 if err = json.Unmarshal(Val, &gemPort); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001204 logger.Error(ctx, "Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301205 return uint32(0), err
1206 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001207 logger.Debugw(ctx, "found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301208
1209 return gemPort, nil
1210}
1211
1212// DelGemPortPktIn deletes the gemport from the pkt in path
npujarec5762e2020-01-01 14:08:48 +05301213func (RsrcMgr *OpenOltResourceMgr) DelGemPortPktIn(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301214
1215 path := fmt.Sprintf(OnuPacketINPath, intfID, onuID, logicalPort)
npujarec5762e2020-01-01 14:08:48 +05301216 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001217 logger.Errorf(ctx, "Falied to remove resource %s", path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301218 return err
1219 }
1220 return nil
1221}
1222
1223// DelOnuGemInfoForIntf deletes the onugem info from kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301224func (RsrcMgr *OpenOltResourceMgr) DelOnuGemInfoForIntf(ctx context.Context, intfID uint32) error {
1225 if err := RsrcMgr.ResourceMgrs[intfID].DelOnuGemInfoForIntf(ctx, intfID); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001226 logger.Errorw(ctx, "failed to delete onu gem info for", log.Fields{"intfid": intfID})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301227 return err
1228 }
1229 return nil
1230}
1231
1232//GetNNIFromKVStore gets NNi intfids from kvstore. path being per device
npujarec5762e2020-01-01 14:08:48 +05301233func (RsrcMgr *OpenOltResourceMgr) GetNNIFromKVStore(ctx context.Context) ([]uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301234
1235 var nni []uint32
1236 var Val []byte
1237
1238 path := fmt.Sprintf(NnniIntfID)
npujarec5762e2020-01-01 14:08:48 +05301239 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301240 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001241 logger.Error(ctx, "failed to get data from kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301242 return nil, err
1243 }
1244 if value != nil {
1245 if Val, err = kvstore.ToByte(value.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001246 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301247 return nil, err
1248 }
1249 if err = json.Unmarshal(Val, &nni); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001250 logger.Error(ctx, "Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301251 return nil, err
1252 }
1253 }
1254 return nni, err
1255}
1256
1257// AddNNIToKVStore adds Nni interfaces to kvstore, path being per device.
npujarec5762e2020-01-01 14:08:48 +05301258func (RsrcMgr *OpenOltResourceMgr) AddNNIToKVStore(ctx context.Context, nniIntf uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301259 var Value []byte
1260
npujarec5762e2020-01-01 14:08:48 +05301261 nni, err := RsrcMgr.GetNNIFromKVStore(ctx)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301262 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001263 logger.Error(ctx, "failed to fetch nni interfaces from kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301264 return err
1265 }
1266
1267 path := fmt.Sprintf(NnniIntfID)
1268 nni = append(nni, nniIntf)
1269 Value, err = json.Marshal(nni)
1270 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001271 logger.Error(ctx, "Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301272 }
npujarec5762e2020-01-01 14:08:48 +05301273 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001274 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"path": path, "value": Value})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301275 return err
1276 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001277 logger.Debugw(ctx, "added nni to kv successfully", log.Fields{"path": path, "nni": nniIntf})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301278 return nil
1279}
1280
1281// DelNNiFromKVStore deletes nni interface list from kv store.
npujarec5762e2020-01-01 14:08:48 +05301282func (RsrcMgr *OpenOltResourceMgr) DelNNiFromKVStore(ctx context.Context) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301283
1284 path := fmt.Sprintf(NnniIntfID)
1285
npujarec5762e2020-01-01 14:08:48 +05301286 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001287 logger.Errorw(ctx, "Failed to delete nni interfaces from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301288 return err
1289 }
1290 return nil
1291}
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301292
1293//UpdateFlowIDsForGem updates flow id per gemport
npujarec5762e2020-01-01 14:08:48 +05301294func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDsForGem(ctx context.Context, intf uint32, gem uint32, flowIDs []uint32) error {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301295 var val []byte
1296 path := fmt.Sprintf(FlowIDsForGem, intf)
1297
npujarec5762e2020-01-01 14:08:48 +05301298 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301299 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001300 logger.Error(ctx, "Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301301 return err
1302 }
1303 if flowsForGem == nil {
1304 flowsForGem = make(map[uint32][]uint32)
1305 }
1306 flowsForGem[gem] = flowIDs
1307 val, err = json.Marshal(flowsForGem)
1308 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001309 logger.Error(ctx, "Failed to marshal data", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301310 return err
1311 }
Girish Gowdrab77ded92020-04-08 11:45:05 -07001312
Girish Gowdra38d533d2020-03-30 20:38:51 -07001313 RsrcMgr.flowIDToGemInfoLock.Lock()
1314 defer RsrcMgr.flowIDToGemInfoLock.Unlock()
npujarec5762e2020-01-01 14:08:48 +05301315 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001316 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301317 return err
1318 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001319 logger.Debugw(ctx, "added flowid list for gem to kv successfully", log.Fields{"path": path, "flowidlist": flowsForGem[gem]})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301320 return nil
1321}
1322
1323//DeleteFlowIDsForGem deletes the flowID list entry per gem from kvstore.
npujarec5762e2020-01-01 14:08:48 +05301324func (RsrcMgr *OpenOltResourceMgr) DeleteFlowIDsForGem(ctx context.Context, intf uint32, gem uint32) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301325 path := fmt.Sprintf(FlowIDsForGem, intf)
1326 var val []byte
1327
npujarec5762e2020-01-01 14:08:48 +05301328 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301329 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001330 logger.Error(ctx, "Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301331 return
1332 }
1333 if flowsForGem == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001334 logger.Error(ctx, "No flowids found ", log.Fields{"intf": intf, "gemport": gem})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301335 return
1336 }
1337 // once we get the flows per gem map from kv , just delete the gem entry from the map
1338 delete(flowsForGem, gem)
1339 // once gem entry is deleted update the kv store.
1340 val, err = json.Marshal(flowsForGem)
1341 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001342 logger.Error(ctx, "Failed to marshal data", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301343 return
1344 }
Girish Gowdra38d533d2020-03-30 20:38:51 -07001345
1346 RsrcMgr.flowIDToGemInfoLock.Lock()
1347 defer RsrcMgr.flowIDToGemInfoLock.Unlock()
npujarec5762e2020-01-01 14:08:48 +05301348 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001349 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301350 return
1351 }
1352 return
1353}
1354
1355//GetFlowIDsGemMapForInterface gets flowids per gemport and interface
npujarec5762e2020-01-01 14:08:48 +05301356func (RsrcMgr *OpenOltResourceMgr) GetFlowIDsGemMapForInterface(ctx context.Context, intf uint32) (map[uint32][]uint32, error) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301357 path := fmt.Sprintf(FlowIDsForGem, intf)
1358 var flowsForGem map[uint32][]uint32
1359 var val []byte
Girish Gowdra38d533d2020-03-30 20:38:51 -07001360 RsrcMgr.flowIDToGemInfoLock.RLock()
npujarec5762e2020-01-01 14:08:48 +05301361 value, err := RsrcMgr.KVStore.Get(ctx, path)
Girish Gowdra38d533d2020-03-30 20:38:51 -07001362 RsrcMgr.flowIDToGemInfoLock.RUnlock()
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301363 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001364 logger.Error(ctx, "failed to get data from kv store")
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301365 return nil, err
1366 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001367 if value != nil && value.Value != nil {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301368 if val, err = kvstore.ToByte(value.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001369 logger.Error(ctx, "Failed to convert to byte array ", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301370 return nil, err
1371 }
1372 if err = json.Unmarshal(val, &flowsForGem); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001373 logger.Error(ctx, "Failed to unmarshall", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301374 return nil, err
1375 }
1376 }
1377 return flowsForGem, nil
1378}
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301379
1380//DeleteIntfIDGempMapPath deletes the intf id path used to store flow ids per gem to kvstore.
npujarec5762e2020-01-01 14:08:48 +05301381func (RsrcMgr *OpenOltResourceMgr) DeleteIntfIDGempMapPath(ctx context.Context, intf uint32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301382 path := fmt.Sprintf(FlowIDsForGem, intf)
Girish Gowdra38d533d2020-03-30 20:38:51 -07001383 RsrcMgr.flowIDToGemInfoLock.Lock()
1384 defer RsrcMgr.flowIDToGemInfoLock.Unlock()
npujarec5762e2020-01-01 14:08:48 +05301385 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001386 logger.Errorw(ctx, "Failed to delete nni interfaces from kv store", log.Fields{"path": path})
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301387 return
1388 }
1389 return
1390}
1391
1392// RemoveResourceMap Clear resource map associated with (intfid, onuid, uniid) tuple.
npujarec5762e2020-01-01 14:08:48 +05301393func (RsrcMgr *OpenOltResourceMgr) RemoveResourceMap(ctx context.Context, intfID uint32, onuID int32, uniID int32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301394 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +05301395 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301396}
Esin Karamanccb714b2019-11-29 15:02:06 +00001397
1398//GetMcastQueuePerInterfaceMap gets multicast queue info per pon interface
npujarec5762e2020-01-01 14:08:48 +05301399func (RsrcMgr *OpenOltResourceMgr) GetMcastQueuePerInterfaceMap(ctx context.Context) (map[uint32][]uint32, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001400 path := fmt.Sprintf(McastQueuesForIntf)
1401 var mcastQueueToIntfMap map[uint32][]uint32
1402 var val []byte
1403
npujarec5762e2020-01-01 14:08:48 +05301404 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001405 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001406 logger.Error(ctx, "failed to get data from kv store")
Esin Karamanccb714b2019-11-29 15:02:06 +00001407 return nil, err
1408 }
1409 if kvPair != nil && kvPair.Value != nil {
1410 if val, err = kvstore.ToByte(kvPair.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001411 logger.Error(ctx, "Failed to convert to byte array ", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001412 return nil, err
1413 }
1414 if err = json.Unmarshal(val, &mcastQueueToIntfMap); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001415 logger.Error(ctx, "Failed to unmarshall ", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001416 return nil, err
1417 }
1418 }
1419 return mcastQueueToIntfMap, nil
1420}
1421
1422//AddMcastQueueForIntf adds multicast queue for pon interface
npujarec5762e2020-01-01 14:08:48 +05301423func (RsrcMgr *OpenOltResourceMgr) AddMcastQueueForIntf(ctx context.Context, intf uint32, gem uint32, servicePriority uint32) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001424 var val []byte
1425 path := fmt.Sprintf(McastQueuesForIntf)
1426
npujarec5762e2020-01-01 14:08:48 +05301427 mcastQueues, err := RsrcMgr.GetMcastQueuePerInterfaceMap(ctx)
Esin Karamanccb714b2019-11-29 15:02:06 +00001428 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001429 logger.Errorw(ctx, "Failed to get multicast queue info for interface", log.Fields{"error": err, "intf": intf})
Esin Karamanccb714b2019-11-29 15:02:06 +00001430 return err
1431 }
1432 if mcastQueues == nil {
1433 mcastQueues = make(map[uint32][]uint32)
1434 }
1435 mcastQueues[intf] = []uint32{gem, servicePriority}
1436 if val, err = json.Marshal(mcastQueues); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001437 logger.Errorw(ctx, "Failed to marshal data", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001438 return err
1439 }
npujarec5762e2020-01-01 14:08:48 +05301440 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001441 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Esin Karamanccb714b2019-11-29 15:02:06 +00001442 return err
1443 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001444 logger.Debugw(ctx, "added multicast queue info to KV store successfully", log.Fields{"path": path, "mcastQueueInfo": mcastQueues[intf], "interfaceId": intf})
Esin Karamanccb714b2019-11-29 15:02:06 +00001445 return nil
1446}
1447
1448//AddFlowGroupToKVStore adds flow group into KV store
npujarec5762e2020-01-01 14:08:48 +05301449func (RsrcMgr *OpenOltResourceMgr) AddFlowGroupToKVStore(ctx context.Context, groupEntry *ofp.OfpGroupEntry, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001450 var Value []byte
1451 var err error
1452 var path string
1453 if cached {
1454 path = fmt.Sprintf(FlowGroupCached, groupEntry.Desc.GroupId)
1455 } else {
1456 path = fmt.Sprintf(FlowGroup, groupEntry.Desc.GroupId)
1457 }
1458 //build group info object
1459 var outPorts []uint32
1460 for _, ofBucket := range groupEntry.Desc.Buckets {
1461 for _, ofAction := range ofBucket.Actions {
1462 if ofAction.Type == ofp.OfpActionType_OFPAT_OUTPUT {
1463 outPorts = append(outPorts, ofAction.GetOutput().Port)
1464 }
1465 }
1466 }
1467 groupInfo := GroupInfo{
1468 GroupID: groupEntry.Desc.GroupId,
1469 OutPorts: outPorts,
1470 }
1471
1472 Value, err = json.Marshal(groupInfo)
1473
1474 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001475 logger.Error(ctx, "failed to Marshal flow group object")
Esin Karamanccb714b2019-11-29 15:02:06 +00001476 return err
1477 }
1478
npujarec5762e2020-01-01 14:08:48 +05301479 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001480 logger.Errorf(ctx, "Failed to update resource %s", path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001481 return err
1482 }
1483 return nil
1484}
1485
1486//RemoveFlowGroupFromKVStore removes flow group from KV store
npujarec5762e2020-01-01 14:08:48 +05301487func (RsrcMgr *OpenOltResourceMgr) RemoveFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) bool {
Esin Karamanccb714b2019-11-29 15:02:06 +00001488 var path string
1489 if cached {
1490 path = fmt.Sprintf(FlowGroupCached, groupID)
1491 } else {
1492 path = fmt.Sprintf(FlowGroup, groupID)
1493 }
npujarec5762e2020-01-01 14:08:48 +05301494 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001495 logger.Errorf(ctx, "Failed to remove resource %s due to %s", path, err)
Esin Karamanccb714b2019-11-29 15:02:06 +00001496 return false
1497 }
1498 return true
1499}
1500
1501//GetFlowGroupFromKVStore fetches flow group from the KV store. Returns (false, {} error) if any problem occurs during
1502//fetching the data. Returns (true, groupInfo, nil) if the group is fetched successfully.
1503// Returns (false, {}, nil) if the group does not exists in the KV store.
npujarec5762e2020-01-01 14:08:48 +05301504func (RsrcMgr *OpenOltResourceMgr) GetFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) (bool, GroupInfo, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001505 var groupInfo GroupInfo
1506 var path string
1507 if cached {
1508 path = fmt.Sprintf(FlowGroupCached, groupID)
1509 } else {
1510 path = fmt.Sprintf(FlowGroup, groupID)
1511 }
npujarec5762e2020-01-01 14:08:48 +05301512 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001513 if err != nil {
1514 return false, groupInfo, err
1515 }
1516 if kvPair != nil && kvPair.Value != nil {
1517 Val, err := kvstore.ToByte(kvPair.Value)
1518 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001519 logger.Errorw(ctx, "Failed to convert flow group into byte array", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001520 return false, groupInfo, err
1521 }
1522 if err = json.Unmarshal(Val, &groupInfo); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001523 logger.Errorw(ctx, "Failed to unmarshal", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001524 return false, groupInfo, err
1525 }
1526 return true, groupInfo, nil
1527 }
1528 return false, groupInfo, nil
1529}