blob: 5f5d150bb67c7ed49210c3b395dff824935d9d40 [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 (
Girish Gowdru0c588b22019-04-23 23:24:56 -040021 "encoding/json"
22 "errors"
23 "fmt"
24 "strconv"
25 "strings"
Abhilash S.L7f17e402019-03-15 17:40:41 +053026
Girish Gowdru0c588b22019-04-23 23:24:56 -040027 "github.com/opencord/voltha-go/common/log"
28 ponrmgr "github.com/opencord/voltha-go/common/ponresourcemanager"
29 "github.com/opencord/voltha-go/db/kvstore"
30 "github.com/opencord/voltha-go/db/model"
Manikkaraj kb1d51442019-07-23 10:41:02 -040031 ofp "github.com/opencord/voltha-protos/go/openflow_13"
Girish Gowdru0c588b22019-04-23 23:24:56 -040032 "github.com/opencord/voltha-protos/go/openolt"
Abhilash S.L7f17e402019-03-15 17:40:41 +053033)
34
salmansiddiqui7ac62132019-08-22 03:58:50 +000035const (
36 // KvstoreTimeout specifies the time out for KV Store Connection
37 KvstoreTimeout = 5
38 // BasePathKvStore - service/voltha/openolt/<device_id>
39 BasePathKvStore = "service/voltha/openolt/{%s}"
40 // TpIDPathSuffix - tp_id/<(pon_id, onu_id, uni_id)>
41 TpIDPathSuffix = "tp_id/{%d,%d,%d}"
42 //MeterIDPathSuffix - meter_id/<(pon_id, onu_id, uni_id)>/<direction>
43 MeterIDPathSuffix = "meter_id/{%d,%d,%d}/{%s}"
44)
Abhilash S.L7f17e402019-03-15 17:40:41 +053045
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070046// FlowInfo holds the flow information
Abhilash S.L8ee90712019-04-29 16:24:22 +053047type FlowInfo struct {
48 Flow *openolt.Flow
49 FlowStoreCookie uint64
50 FlowCategory string
51}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070052
53// OpenOltResourceMgr holds resource related information as provided below for each field
Abhilash S.L7f17e402019-03-15 17:40:41 +053054type OpenOltResourceMgr struct {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070055 DeviceID string // OLT device id
Girish Gowdru0c588b22019-04-23 23:24:56 -040056 HostAndPort string // Host and port of the kv store to connect to
57 Args string // args
58 KVStore *model.Backend // backend kv store connection handle
59 DeviceType string
60 Host string // Host ip of the kv store
61 Port int // port of the kv store
62 DevInfo *openolt.DeviceInfo // device information
63 // array of pon resource managers per interface technology
64 ResourceMgrs map[uint32]*ponrmgr.PONResourceManager
Abhilash S.L7f17e402019-03-15 17:40:41 +053065}
66
Manikkaraj kb1d51442019-07-23 10:41:02 -040067func newKVClient(storeType string, address string, timeout uint32) (kvstore.Client, error) {
Girish Gowdru0c588b22019-04-23 23:24:56 -040068 log.Infow("kv-store-type", log.Fields{"store": storeType})
69 switch storeType {
70 case "consul":
71 return kvstore.NewConsulClient(address, int(timeout))
72 case "etcd":
73 return kvstore.NewEtcdClient(address, int(timeout))
74 }
75 return nil, errors.New("unsupported-kv-store")
Abhilash S.L7f17e402019-03-15 17:40:41 +053076}
77
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070078// SetKVClient sets the KV client and return a kv backend
79func SetKVClient(backend string, Host string, Port int, DeviceID string) *model.Backend {
Girish Gowdru0c588b22019-04-23 23:24:56 -040080 addr := Host + ":" + strconv.Itoa(Port)
81 // TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
82 // issue between kv store and backend , core is not calling NewBackend directly
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070083 kvClient, err := newKVClient(backend, addr, KvstoreTimeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -040084 if err != nil {
85 log.Fatalw("Failed to init KV client\n", log.Fields{"err": err})
86 return nil
87 }
88 kvbackend := &model.Backend{
89 Client: kvClient,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070090 StoreType: backend,
Girish Gowdru0c588b22019-04-23 23:24:56 -040091 Host: Host,
92 Port: Port,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070093 Timeout: KvstoreTimeout,
94 PathPrefix: fmt.Sprintf(BasePathKvStore, DeviceID)}
Abhilash S.L7f17e402019-03-15 17:40:41 +053095
Girish Gowdru0c588b22019-04-23 23:24:56 -040096 return kvbackend
Abhilash S.L7f17e402019-03-15 17:40:41 +053097}
98
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070099// NewResourceMgr init a New resource maanger instance which in turn instantiates pon resource manager
100// instances according to technology. Initializes the default resource ranges for all
101// the resources.
102func NewResourceMgr(deviceID string, KVStoreHostPort string, kvStoreType string, deviceType string, devInfo *openolt.DeviceInfo) *OpenOltResourceMgr {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400103 var ResourceMgr OpenOltResourceMgr
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700104 log.Debugf("Init new resource manager , host_port: %s, deviceid: %s", KVStoreHostPort, deviceID)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530105 ResourceMgr.HostAndPort = KVStoreHostPort
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700106 ResourceMgr.DeviceType = deviceType
107 ResourceMgr.DevInfo = devInfo
108 IPPort := strings.Split(KVStoreHostPort, ":")
109 ResourceMgr.Host = IPPort[0]
110 ResourceMgr.Port, _ = strconv.Atoi(IPPort[1])
Abhilash S.L7f17e402019-03-15 17:40:41 +0530111
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700112 Backend := kvStoreType
Girish Gowdru0c588b22019-04-23 23:24:56 -0400113 ResourceMgr.KVStore = SetKVClient(Backend, ResourceMgr.Host,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700114 ResourceMgr.Port, deviceID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400115 if ResourceMgr.KVStore == nil {
116 log.Error("Failed to setup KV store")
117 }
118 Ranges := make(map[string]*openolt.DeviceInfo_DeviceResourceRanges)
119 RsrcMgrsByTech := make(map[string]*ponrmgr.PONResourceManager)
120 ResourceMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530121
Girish Gowdru0c588b22019-04-23 23:24:56 -0400122 // TODO self.args = registry('main').get_args()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530123
Girish Gowdru0c588b22019-04-23 23:24:56 -0400124 /*
125 If a legacy driver returns protobuf without any ranges,s synthesize one from
126 the legacy global per-device informaiton. This, in theory, is temporary until
127 the legacy drivers are upgrade to support pool ranges.
128 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700129 if devInfo.Ranges == nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400130 var ranges openolt.DeviceInfo_DeviceResourceRanges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700131 ranges.Technology = devInfo.GetTechnology()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530132
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700133 NumPONPorts := devInfo.GetPonPorts()
Girish Gowdru0c588b22019-04-23 23:24:56 -0400134 var index uint32
135 for index = 0; index < NumPONPorts; index++ {
136 ranges.IntfIds = append(ranges.IntfIds, index)
137 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530138
Abhilash S.L8ee90712019-04-29 16:24:22 +0530139 var Pool openolt.DeviceInfo_DeviceResourceRanges_Pool
Girish Gowdru0c588b22019-04-23 23:24:56 -0400140 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700141 Pool.Start = devInfo.OnuIdStart
142 Pool.End = devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400143 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
cbabuabf02352019-10-15 13:14:56 +0200144 onuPool := Pool
145 ranges.Pools = append(ranges.Pools, &onuPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530146
Girish Gowdru0c588b22019-04-23 23:24:56 -0400147 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700148 Pool.Start = devInfo.AllocIdStart
149 Pool.End = devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400150 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200151 allocPool := Pool
152 ranges.Pools = append(ranges.Pools, &allocPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530153
Girish Gowdru0c588b22019-04-23 23:24:56 -0400154 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700155 Pool.Start = devInfo.GemportIdStart
156 Pool.End = devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400157 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200158 gemPool := Pool
159 ranges.Pools = append(ranges.Pools, &gemPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530160
Girish Gowdru0c588b22019-04-23 23:24:56 -0400161 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700162 Pool.Start = devInfo.FlowIdStart
163 Pool.End = devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400164 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
Abhilash S.L8ee90712019-04-29 16:24:22 +0530165 ranges.Pools = append(ranges.Pools, &Pool)
166 // Add to device info
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700167 devInfo.Ranges = append(devInfo.Ranges, &ranges)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400168 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530169
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700170 // Create a separate Resource Manager instance for each range. This assumes that
Girish Gowdru0c588b22019-04-23 23:24:56 -0400171 // each technology is represented by only a single range
172 var GlobalPONRsrcMgr *ponrmgr.PONResourceManager
173 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700174 for _, TechRange := range devInfo.Ranges {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400175 technology := TechRange.Technology
176 log.Debugf("Device info technology %s", technology)
177 Ranges[technology] = TechRange
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700178 RsrcMgrsByTech[technology], err = ponrmgr.NewPONResourceManager(technology, deviceType, deviceID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400179 Backend, ResourceMgr.Host, ResourceMgr.Port)
180 if err != nil {
181 log.Errorf("Failed to create pon resource manager instacnce for technology %s", technology)
182 return nil
183 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700184 // resource_mgrs_by_tech[technology] = resource_mgr
Girish Gowdru0c588b22019-04-23 23:24:56 -0400185 if GlobalPONRsrcMgr == nil {
186 GlobalPONRsrcMgr = RsrcMgrsByTech[technology]
187 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700188 for _, IntfID := range TechRange.IntfIds {
189 ResourceMgr.ResourceMgrs[uint32(IntfID)] = RsrcMgrsByTech[technology]
Girish Gowdru0c588b22019-04-23 23:24:56 -0400190 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700191 // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400192 InitializeDeviceResourceRangeAndPool(RsrcMgrsByTech[technology], GlobalPONRsrcMgr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700193 TechRange, devInfo)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400194 }
195 // After we have initialized resource ranges, initialize the
196 // resource pools accordingly.
197 for _, PONRMgr := range RsrcMgrsByTech {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700198 _ = PONRMgr.InitDeviceResourcePool()
Girish Gowdru0c588b22019-04-23 23:24:56 -0400199 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530200 log.Info("Initialization of resource manager success!")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400201 return &ResourceMgr
Abhilash S.L7f17e402019-03-15 17:40:41 +0530202}
203
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700204// InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply
205// device specific information. If KV doesn't exist
206// or is broader than the device, the device's information will
207// dictate the range limits
208func InitializeDeviceResourceRangeAndPool(ponRMgr *ponrmgr.PONResourceManager, globalPONRMgr *ponrmgr.PONResourceManager,
209 techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530210
Girish Gowdru0c588b22019-04-23 23:24:56 -0400211 // init the resource range pool according to the sharing type
Abhilash S.L7f17e402019-03-15 17:40:41 +0530212
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700213 log.Debugf("Resource range pool init for technology %s", ponRMgr.Technology)
214 // first load from KV profiles
215 status := ponRMgr.InitResourceRangesFromKVStore()
216 if !status {
217 log.Debugf("Failed to load resource ranges from KV store for tech %s", ponRMgr.Technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400218 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530219
Girish Gowdru0c588b22019-04-23 23:24:56 -0400220 /*
221 Then apply device specific information. If KV doesn't exist
222 or is broader than the device, the device's informationw ill
223 dictate the range limits
224 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700225 log.Debugf("Using device info to init pon resource ranges for tech", ponRMgr.Technology)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530226
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700227 ONUIDStart := devInfo.OnuIdStart
228 ONUIDEnd := devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400229 ONUIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
230 ONUIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700231 AllocIDStart := devInfo.AllocIdStart
232 AllocIDEnd := devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400233 AllocIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
234 AllocIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700235 GEMPortIDStart := devInfo.GemportIdStart
236 GEMPortIDEnd := devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400237 GEMPortIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
238 GEMPortIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700239 FlowIDStart := devInfo.FlowIdStart
240 FlowIDEnd := devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400241 FlowIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
242 FlowIDSharedPoolID := uint32(0)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530243
Girish Gowdru0c588b22019-04-23 23:24:56 -0400244 var FirstIntfPoolID uint32
245 var SharedPoolID uint32
Abhilash S.L7f17e402019-03-15 17:40:41 +0530246
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400247 /*
248 * 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 -0700249 * if resources are shared across interfaces then SharedPoolID is given a positive number.
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400250 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700251 for _, FirstIntfPoolID = range techRange.IntfIds {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400252 // skip the intf id 0
253 if FirstIntfPoolID == 0 {
254 continue
255 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400256 break
257 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530258
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700259 for _, RangePool := range techRange.Pools {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400260 if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400261 SharedPoolID = FirstIntfPoolID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400262 } else if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_SAME_TECH {
263 SharedPoolID = FirstIntfPoolID
264 } else {
265 SharedPoolID = 0
266 }
267 if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID {
268 ONUIDStart = RangePool.Start
269 ONUIDEnd = RangePool.End
270 ONUIDShared = RangePool.Sharing
271 ONUIDSharedPoolID = SharedPoolID
272 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID {
273 AllocIDStart = RangePool.Start
274 AllocIDEnd = RangePool.End
275 AllocIDShared = RangePool.Sharing
276 AllocIDSharedPoolID = SharedPoolID
277 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID {
278 GEMPortIDStart = RangePool.Start
279 GEMPortIDEnd = RangePool.End
280 GEMPortIDShared = RangePool.Sharing
281 GEMPortIDSharedPoolID = SharedPoolID
282 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID {
283 FlowIDStart = RangePool.Start
284 FlowIDEnd = RangePool.End
285 FlowIDShared = RangePool.Sharing
286 FlowIDSharedPoolID = SharedPoolID
287 }
288 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530289
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700290 log.Debugw("Device info init", log.Fields{"technology": techRange.Technology,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400291 "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd, "onu_id_shared_pool_id": ONUIDSharedPoolID,
292 "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd,
293 "alloc_id_shared_pool_id": AllocIDSharedPoolID,
294 "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd,
295 "gemport_id_shared_pool_id": GEMPortIDSharedPoolID,
296 "flow_id_start": FlowIDStart,
297 "flow_id_end_idx": FlowIDEnd,
298 "flow_id_shared_pool_id": FlowIDSharedPoolID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700299 "intf_ids": techRange.IntfIds,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400300 "uni_id_start": 0,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700301 "uni_id_end_idx": 1, /*MaxUNIIDperONU()*/
302 })
Abhilash S.L7f17e402019-03-15 17:40:41 +0530303
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700304 ponRMgr.InitDefaultPONResourceRanges(ONUIDStart, ONUIDEnd, ONUIDSharedPoolID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400305 AllocIDStart, AllocIDEnd, AllocIDSharedPoolID,
306 GEMPortIDStart, GEMPortIDEnd, GEMPortIDSharedPoolID,
307 FlowIDStart, FlowIDEnd, FlowIDSharedPoolID, 0, 1,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700308 devInfo.PonPorts, techRange.IntfIds)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530309
Girish Gowdru0c588b22019-04-23 23:24:56 -0400310 // For global sharing, make sure to refresh both local and global resource manager instances' range
Abhilash S.L7f17e402019-03-15 17:40:41 +0530311
Girish Gowdru0c588b22019-04-23 23:24:56 -0400312 if ONUIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700313 globalPONRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400314 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700315 ponRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
316 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400317 }
318 if AllocIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700319 globalPONRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400320 "", 0, nil)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530321
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700322 ponRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
323 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400324 }
325 if GEMPortIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700326 globalPONRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400327 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700328 ponRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
329 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400330 }
331 if FlowIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700332 globalPONRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400333 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700334 ponRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
335 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400336 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530337
Girish Gowdru0c588b22019-04-23 23:24:56 -0400338 // Make sure loaded range fits the platform bit encoding ranges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700339 ponRMgr.UpdateRanges(ponrmgr.UNI_ID_START_IDX, 0, ponrmgr.UNI_ID_END_IDX /* TODO =OpenOltPlatform.MAX_UNIS_PER_ONU-1*/, 1, "", 0, nil)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530340}
341
Devmalya Paul495b94a2019-08-27 19:42:00 -0400342// Delete clears used resources for the particular olt device being deleted
343func (RsrcMgr *OpenOltResourceMgr) Delete() error {
344 /* TODO
345 def __del__(self):
346 self.log.info("clearing-device-resource-pool")
347 for key, resource_mgr in self.resource_mgrs.iteritems():
348 resource_mgr.clear_device_resource_pool()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530349
Devmalya Paul495b94a2019-08-27 19:42:00 -0400350 def assert_pon_id_limit(self, pon_intf_id):
351 assert pon_intf_id in self.resource_mgrs
Abhilash S.L7f17e402019-03-15 17:40:41 +0530352
Devmalya Paul495b94a2019-08-27 19:42:00 -0400353 def assert_onu_id_limit(self, pon_intf_id, onu_id):
354 self.assert_pon_id_limit(pon_intf_id)
355 self.resource_mgrs[pon_intf_id].assert_resource_limits(onu_id, PONResourceManager.ONU_ID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530356
Devmalya Paul495b94a2019-08-27 19:42:00 -0400357 @property
358 def max_uni_id_per_onu(self):
359 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 +0530360
Devmalya Paul495b94a2019-08-27 19:42:00 -0400361 def assert_uni_id_limit(self, pon_intf_id, onu_id, uni_id):
362 self.assert_onu_id_limit(pon_intf_id, onu_id)
363 self.resource_mgrs[pon_intf_id].assert_resource_limits(uni_id, PONResourceManager.UNI_ID)
364 */
365 for _, rsrcMgr := range RsrcMgr.ResourceMgrs {
366 if err := rsrcMgr.ClearDeviceResourcePool(); err != nil {
367 log.Debug("Failed to clear device resource pool")
368 return err
369 }
370 }
371 log.Debug("Cleared device resource pool")
372 return nil
373}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530374
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700375// GetONUID returns the available OnuID for the given pon-port
376func (RsrcMgr *OpenOltResourceMgr) GetONUID(ponIntfID uint32) (uint32, error) {
salmansiddiqui352a45c2019-08-19 10:15:36 +0000377 // Check if Pon Interface ID is present in Resource-manager-map
378 if _, ok := RsrcMgr.ResourceMgrs[ponIntfID]; !ok {
379 err := errors.New("invalid-pon-interface-" + strconv.Itoa(int(ponIntfID)))
380 return 0, err
381 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400382 // Get ONU id for a provided pon interface ID.
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700383 ONUID, err := RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400384 ponrmgr.ONU_ID, 1)
385 if err != nil {
386 log.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700387 ponIntfID, ponrmgr.ONU_ID)
cbabuabf02352019-10-15 13:14:56 +0200388 return 0, err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400389 }
390 if ONUID != nil {
Devmalya Paul495b94a2019-08-27 19:42:00 -0400391 RsrcMgr.ResourceMgrs[ponIntfID].InitResourceMap(fmt.Sprintf("%d,%d", ponIntfID, ONUID[0]))
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700392 return ONUID[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400393 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530394
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700395 return 0, err // return OnuID 0 on error
Abhilash S.L7f17e402019-03-15 17:40:41 +0530396}
397
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700398// GetFlowIDInfo returns the slice of flow info of the given pon-port
399// Note: For flows which trap from the NNI and not really associated with any particular
400// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
401func (RsrcMgr *OpenOltResourceMgr) GetFlowIDInfo(ponIntfID uint32, onuID uint32, uniID uint32, flowID uint32) *[]FlowInfo {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530402 var flows []FlowInfo
403
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700404 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
405 if err := RsrcMgr.ResourceMgrs[ponIntfID].GetFlowIDInfo(FlowPath, flowID, &flows); err != nil {
406 log.Errorw("Error while getting flows from KV store", log.Fields{"flowId": flowID})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530407 return nil
408 }
409 if len(flows) == 0 {
410 log.Debugw("No flowInfo found in KV store", log.Fields{"flowPath": FlowPath})
411 return nil
412 }
413 return &flows
414}
415
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700416// GetCurrentFlowIDsForOnu fetches flow ID from the resource manager
417// Note: For flows which trap from the NNI and not really associated with any particular
418// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
Abhilash S.L8ee90712019-04-29 16:24:22 +0530419func (RsrcMgr *OpenOltResourceMgr) GetCurrentFlowIDsForOnu(PONIntfID uint32, ONUID uint32, UNIID uint32) []uint32 {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700420
Abhilash S.L8ee90712019-04-29 16:24:22 +0530421 FlowPath := fmt.Sprintf("%d,%d,%d", PONIntfID, ONUID, UNIID)
422 return RsrcMgr.ResourceMgrs[PONIntfID].GetCurrentFlowIDsForOnu(FlowPath)
423}
424
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700425// UpdateFlowIDInfo updates flow info for the given pon interface, onu id, and uni id
426// Note: For flows which trap from the NNI and not really associated with any particular
427// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
428func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDInfo(ponIntfID int32, onuID int32, uniID int32,
429 flowID uint32, flowData *[]FlowInfo) error {
430 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
431 return RsrcMgr.ResourceMgrs[uint32(ponIntfID)].UpdateFlowIDInfoForOnu(FlowPath, flowID, *flowData)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530432}
433
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700434// GetFlowID return flow ID for a given pon interface id, onu id and uni id
435func (RsrcMgr *OpenOltResourceMgr) GetFlowID(ponIntfID uint32, ONUID uint32, uniID uint32,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400436 gemportID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700437 flowStoreCookie uint64,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400438 flowCategory string, vlanPcp ...uint32) (uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530439
Girish Gowdru0c588b22019-04-23 23:24:56 -0400440 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700441 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, ONUID, uniID)
442 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(FlowPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400443 if FlowIDs != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700444 log.Debugw("Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "ONUID": ONUID, "uniID": uniID, "KVpath": FlowPath})
445 for _, flowID := range FlowIDs {
446 FlowInfo := RsrcMgr.GetFlowIDInfo(ponIntfID, ONUID, uniID, uint32(flowID))
salmansiddiqui7ac62132019-08-22 03:58:50 +0000447 er := getFlowIDFromFlowInfo(FlowInfo, flowID, gemportID, flowStoreCookie, flowCategory, vlanPcp...)
448 if er == nil {
449 return flowID, er
Abhilash S.L8ee90712019-04-29 16:24:22 +0530450 }
451 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400452 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530453 log.Debug("No matching flows with flow cookie or flow category, allocating new flowid")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700454 FlowIDs, err = RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400455 ponrmgr.FLOW_ID, 1)
456 if err != nil {
457 log.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700458 ponIntfID, ponrmgr.FLOW_ID)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400459 return uint32(0), err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400460 }
461 if FlowIDs != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700462 _ = RsrcMgr.ResourceMgrs[ponIntfID].UpdateFlowIDForOnu(FlowPath, FlowIDs[0], true)
463 return FlowIDs[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400464 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530465
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700466 return 0, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530467}
468
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700469// GetAllocID return the first Alloc ID for a given pon interface id and onu id and then update the resource map on
470// the KV store with the list of alloc_ids allocated for the pon_intf_onu_id tuple
471// Currently of all the alloc_ids available, it returns the first alloc_id in the list for tha given ONU
472func (RsrcMgr *OpenOltResourceMgr) GetAllocID(intfID uint32, onuID uint32, uniID uint32) uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530473
Girish Gowdru0c588b22019-04-23 23:24:56 -0400474 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700475 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
476 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400477 if AllocID != nil {
478 // Since we support only one alloc_id for the ONU at the moment,
479 // return the first alloc_id in the list, if available, for that
480 // ONU.
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700481 log.Debugw("Retrieved alloc ID from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400482 return AllocID[0]
483 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700484 AllocID, err = RsrcMgr.ResourceMgrs[intfID].GetResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400485 ponrmgr.ALLOC_ID, 1)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530486
Girish Gowdru0c588b22019-04-23 23:24:56 -0400487 if AllocID == nil || err != nil {
488 log.Error("Failed to allocate alloc id")
489 return 0
490 }
491 // update the resource map on KV store with the list of alloc_id
492 // allocated for the pon_intf_onu_id tuple
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700493 err = RsrcMgr.ResourceMgrs[intfID].UpdateAllocIdsForOnu(IntfOnuIDUniID, AllocID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400494 if err != nil {
495 log.Error("Failed to update Alloc ID")
496 return 0
497 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530498 log.Debugw("Allocated new Tcont from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400499 return AllocID[0]
Abhilash S.L7f17e402019-03-15 17:40:41 +0530500}
501
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700502// UpdateAllocIdsForOnu updates alloc ids in kv store for a given pon interface id, onu id and uni id
503func (RsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ponPort uint32, onuID uint32, uniID uint32, allocID []uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530504
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700505 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
506 return RsrcMgr.ResourceMgrs[ponPort].UpdateAllocIdsForOnu(IntfOnuIDUniID,
507 allocID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530508}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700509
510// GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id
511func (RsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(intfID uint32, onuID uint32,
512 uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530513
Girish Gowdru0c588b22019-04-23 23:24:56 -0400514 /* Get gem ports for given pon interface , onu id and uni id. */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530515
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700516 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
517 return RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530518}
519
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700520// GetCurrentAllocIDForOnu returns alloc ids for given pon interface and onu id
521// Currently of all the alloc_ids available, it returns the first alloc_id in the list for tha given ONU
522func (RsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDForOnu(intfID uint32, onuID uint32, uniID uint32) uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530523
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700524 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
525 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400526 if AllocID != nil {
527 // Since we support only one alloc_id for the ONU at the moment,
528 // return the first alloc_id in the list, if available, for that
529 // ONU.
530 return AllocID[0]
531 }
532 return 0
Abhilash S.L7f17e402019-03-15 17:40:41 +0530533}
534
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700535// UpdateGEMportsPonportToOnuMapOnKVStore updates onu and uni id associated with the gem port to the kv store
536// This stored information is used when packet_indication is received and we need to derive the ONU Id for which
537// the packet arrived based on the pon_intf and gemport available in the packet_indication
538func (RsrcMgr *OpenOltResourceMgr) UpdateGEMportsPonportToOnuMapOnKVStore(gemPorts []uint32, PonPort uint32,
539 onuID uint32, uniID uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530540
Girish Gowdru0c588b22019-04-23 23:24:56 -0400541 /* Update onu and uni id associated with the gem port to the kv store. */
542 var IntfGEMPortPath string
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700543 Data := fmt.Sprintf("%d %d", onuID, uniID)
544 for _, GEM := range gemPorts {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400545 IntfGEMPortPath = fmt.Sprintf("%d,%d", PonPort, GEM)
546 Val, err := json.Marshal(Data)
547 if err != nil {
548 log.Error("failed to Marshal")
549 return err
550 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700551
Girish Gowdru0c588b22019-04-23 23:24:56 -0400552 if err = RsrcMgr.KVStore.Put(IntfGEMPortPath, Val); err != nil {
553 log.Errorf("Failed to update resource %s", IntfGEMPortPath)
554 return err
555 }
556 }
557 return nil
Abhilash S.L7f17e402019-03-15 17:40:41 +0530558}
559
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700560// GetGEMPortID gets gem port id for a particular pon port, onu id and uni id and then update the resource map on
561// the KV store with the list of gemport_id allocated for the pon_intf_onu_id tuple
562func (RsrcMgr *OpenOltResourceMgr) GetGEMPortID(ponPort uint32, onuID uint32,
563 uniID uint32, NumOfPorts uint32) ([]uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530564
Girish Gowdru0c588b22019-04-23 23:24:56 -0400565 /* Get gem port id for a particular pon port, onu id
566 and uni id.
567 */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530568
Girish Gowdru0c588b22019-04-23 23:24:56 -0400569 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700570 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530571
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700572 GEMPortList := RsrcMgr.ResourceMgrs[ponPort].GetCurrentGEMPortIDsForOnu(IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400573 if GEMPortList != nil {
574 return GEMPortList, nil
575 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530576
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700577 GEMPortList, err = RsrcMgr.ResourceMgrs[ponPort].GetResourceID(ponPort,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400578 ponrmgr.GEMPORT_ID, NumOfPorts)
579 if err != nil && GEMPortList == nil {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530580 log.Errorf("Failed to get gem port id for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400581 return nil, err
582 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530583
Girish Gowdru0c588b22019-04-23 23:24:56 -0400584 // update the resource map on KV store with the list of gemport_id
585 // allocated for the pon_intf_onu_id tuple
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700586 err = RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400587 GEMPortList)
588 if err != nil {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530589 log.Errorf("Failed to update GEM ports to kv store for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400590 return nil, err
591 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700592 _ = RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(GEMPortList, ponPort,
593 onuID, uniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400594 return GEMPortList, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530595}
596
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700597// UpdateGEMPortIDsForOnu updates gemport ids on to the kv store for a given pon port, onu id and uni id
598func (RsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ponPort uint32, onuID uint32,
599 uniID uint32, GEMPortList []uint32) error {
600 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
601 return RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400602 GEMPortList)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530603
604}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700605
606// FreeonuID releases(make free) onu id for a particular pon-port
607func (RsrcMgr *OpenOltResourceMgr) FreeonuID(intfID uint32, onuID []uint32) {
608
609 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID, ponrmgr.ONU_ID, onuID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530610
Girish Gowdru0c588b22019-04-23 23:24:56 -0400611 /* Free onu id for a particular interface.*/
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700612 var IntfonuID string
613 for _, onu := range onuID {
614 IntfonuID = fmt.Sprintf("%d,%d", intfID, onu)
615 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(IntfonuID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400616 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530617}
618
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700619// FreeFlowID returns the free flow id for a given interface, onu id and uni id
Devmalya Paul495b94a2019-08-27 19:42:00 -0400620func (RsrcMgr *OpenOltResourceMgr) FreeFlowID(IntfID uint32, onuID int32,
621 uniID int32, FlowID uint32) {
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400622 var IntfONUID string
623 var err error
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530624 FlowIds := make([]uint32, 0)
625
626 FlowIds = append(FlowIds, FlowID)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700627 IntfONUID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
628 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(IntfONUID, FlowID, false)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400629 if err != nil {
630 log.Error("Failed to Update flow id infor for %s", IntfONUID)
631 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700632 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(IntfONUID, FlowID)
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530633 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(IntfID, ponrmgr.FLOW_ID, FlowIds)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400634}
635
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700636// FreeFlowIDs releases the flow Ids
637func (RsrcMgr *OpenOltResourceMgr) FreeFlowIDs(IntfID uint32, onuID uint32,
638 uniID uint32, FlowID []uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530639
Girish Gowdru0c588b22019-04-23 23:24:56 -0400640 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(IntfID, ponrmgr.FLOW_ID, FlowID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530641
Abhilash S.L8ee90712019-04-29 16:24:22 +0530642 var IntfOnuIDUniID string
Girish Gowdru0c588b22019-04-23 23:24:56 -0400643 var err error
644 for _, flow := range FlowID {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700645 IntfOnuIDUniID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530646 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(IntfOnuIDUniID, flow, false)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400647 if err != nil {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530648 log.Error("Failed to Update flow id infor for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400649 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530650 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(IntfOnuIDUniID, flow)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400651 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530652}
653
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700654// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id, and the clears the
655// resource map and the onuID associated with (pon_intf_id, gemport_id) tuple,
656func (RsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(intfID uint32, onuID uint32, uniID uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530657
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700658 var onuIDs []uint32
659 onuIDs = append(onuIDs, onuID)
660 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530661
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700662 AllocIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530663
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700664 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400665 ponrmgr.ALLOC_ID,
666 AllocIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530667
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700668 GEMPortIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(IntfOnuIDUniID)
669 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400670 ponrmgr.GEMPORT_ID,
671 GEMPortIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530672
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700673 FlowIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentFlowIDsForOnu(IntfOnuIDUniID)
674 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400675 ponrmgr.FLOW_ID,
676 FlowIDs)
Devmalya Paul495b94a2019-08-27 19:42:00 -0400677 if int32(onuID) >= 0 {
678 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
679 ponrmgr.ONU_ID,
680 onuIDs)
681 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400682 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700683 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530684
Girish Gowdru0c588b22019-04-23 23:24:56 -0400685 // Clear the ONU Id associated with the (pon_intf_id, gemport_id) tuple.
686 for _, GEM := range GEMPortIDs {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700687 _ = RsrcMgr.KVStore.Delete(fmt.Sprintf("%d,%d", intfID, GEM))
Girish Gowdru0c588b22019-04-23 23:24:56 -0400688 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530689}
690
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700691// IsFlowCookieOnKVStore checks if the given flow cookie is present on the kv store
692// Returns true if the flow cookie is found, otherwise it returns false
693func (RsrcMgr *OpenOltResourceMgr) IsFlowCookieOnKVStore(ponIntfID uint32, onuID uint32, uniID uint32,
694 flowStoreCookie uint64) bool {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530695
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700696 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
697 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(FlowPath)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400698 if FlowIDs != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700699 log.Debugw("Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "onuID": onuID, "uniID": uniID, "KVpath": FlowPath})
700 for _, flowID := range FlowIDs {
701 FlowInfo := RsrcMgr.GetFlowIDInfo(ponIntfID, onuID, uniID, uint32(flowID))
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400702 if FlowInfo != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700703 log.Debugw("Found flows", log.Fields{"flows": *FlowInfo, "flowId": flowID})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400704 for _, Info := range *FlowInfo {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700705 if Info.FlowStoreCookie == flowStoreCookie {
706 log.Debug("Found flow matching with flowStore cookie", log.Fields{"flowId": flowID, "flowStoreCookie": flowStoreCookie})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400707 return true
708 }
709 }
710 }
711 }
712 }
713 return false
714}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400715
salmansiddiqui7ac62132019-08-22 03:58:50 +0000716// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
717// This path is formed as the following: tp_id/{IntfID, OnuID, UniID}
718func (RsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(IntfID uint32, OnuID uint32, UniID uint32) uint32 {
719 Path := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400720 var Data uint32
salmansiddiqui7ac62132019-08-22 03:58:50 +0000721 Value, err := RsrcMgr.KVStore.Get(Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400722 if err == nil {
723 if Value != nil {
724 Val, err := kvstore.ToByte(Value.Value)
725 if err != nil {
726 log.Errorw("Failed to convert into byte array", log.Fields{"error": err})
727 return Data
728 }
729 if err = json.Unmarshal(Val, &Data); err != nil {
730 log.Error("Failed to unmarshal", log.Fields{"error": err})
731 return Data
732 }
733 }
734 } else {
735 log.Errorf("Failed to get TP id from kvstore for path %s", Path)
736 }
737 log.Debugf("Getting TP id %d from path %s", Data, Path)
738 return Data
739
740}
741
salmansiddiqui7ac62132019-08-22 03:58:50 +0000742// RemoveTechProfileIDForOnu deletes the tech-profile-id from the KV-Store for the given onu based on the path
743// This path is formed as the following: tp_id/{IntfID, OnuID, UniID}
744func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(IntfID uint32, OnuID uint32, UniID uint32) error {
745 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
746 if err := RsrcMgr.KVStore.Delete(IntfOnuUniID); err != nil {
747 log.Error("Failed to delete techprofile id resource %s in KV store", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400748 return err
749 }
750 return nil
751}
752
salmansiddiqui7ac62132019-08-22 03:58:50 +0000753//UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
754// This path is formed as the following: tp_id/{IntfID, OnuID, UniID}
755func (RsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(IntfID uint32, OnuID uint32,
756 UniID uint32, TpID uint32) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400757 var Value []byte
758 var err error
759
salmansiddiqui7ac62132019-08-22 03:58:50 +0000760 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
761 log.Debugf("updating tp id %d on path %s", TpID, IntfOnuUniID)
762 Value, err = json.Marshal(TpID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400763 if err != nil {
764 log.Error("failed to Marshal")
765 return err
766 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000767 if err = RsrcMgr.KVStore.Put(IntfOnuUniID, Value); err != nil {
768 log.Errorf("Failed to update resource %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400769 return err
770 }
771 return err
772}
773
salmansiddiqui7ac62132019-08-22 03:58:50 +0000774// UpdateMeterIDForOnu updates the meter id in the KV-Store for the given onu based on the path
775// This path is formed as the following: tp_id/{IntfID, OnuID, UniID}/direction
776func (RsrcMgr *OpenOltResourceMgr) UpdateMeterIDForOnu(Direction string, IntfID uint32, OnuID uint32,
777 UniID uint32, MeterConfig *ofp.OfpMeterConfig) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400778 var Value []byte
779 var err error
780
salmansiddiqui7ac62132019-08-22 03:58:50 +0000781 IntfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400782 Value, err = json.Marshal(*MeterConfig)
783 if err != nil {
784 log.Error("failed to Marshal meter config")
785 return err
786 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000787 if err = RsrcMgr.KVStore.Put(IntfOnuUniID, Value); err != nil {
788 log.Errorf("Failed to store meter into KV store %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400789 return err
790 }
791 return err
792}
793
salmansiddiqui7ac62132019-08-22 03:58:50 +0000794// GetMeterIDForOnu fetches the meter-id fromthe kv store for the given onu based on the path
795// This path is formed as the following: tp_id/{IntfID, OnuID, UniID}/direction
796func (RsrcMgr *OpenOltResourceMgr) GetMeterIDForOnu(Direction string, IntfID uint32, OnuID uint32, UniID uint32) (*ofp.OfpMeterConfig, error) {
797 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400798 var meterConfig ofp.OfpMeterConfig
salmansiddiqui7ac62132019-08-22 03:58:50 +0000799 Value, err := RsrcMgr.KVStore.Get(Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400800 if err == nil {
801 if Value != nil {
802 log.Debug("Found meter in KV store", log.Fields{"Direction": Direction})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000803 Val, er := kvstore.ToByte(Value.Value)
804 if er != nil {
805 log.Errorw("Failed to convert into byte array", log.Fields{"error": er})
806 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400807 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000808 if er = json.Unmarshal(Val, &meterConfig); er != nil {
809 log.Error("Failed to unmarshal meterconfig", log.Fields{"error": er})
810 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400811 }
812 } else {
813 log.Debug("meter-does-not-exists-in-KVStore")
814 return nil, err
815 }
816 } else {
817 log.Errorf("Failed to get Meter config from kvstore for path %s", Path)
818
819 }
820 return &meterConfig, err
821}
822
salmansiddiqui7ac62132019-08-22 03:58:50 +0000823// RemoveMeterIDForOnu deletes the meter-id from the kV-Store for the given onu based on the path
824// This path is formed as the following: tp_id/{IntfID, OnuID, UniID}/direction
825func (RsrcMgr *OpenOltResourceMgr) RemoveMeterIDForOnu(Direction string, IntfID uint32, OnuID uint32, UniID uint32) error {
826 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, Direction)
827 if err := RsrcMgr.KVStore.Delete(Path); err != nil {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400828 log.Errorf("Failed to delete meter id %s from kvstore ", Path)
829 return err
830 }
831 return nil
832}
salmansiddiqui7ac62132019-08-22 03:58:50 +0000833
834func getFlowIDFromFlowInfo(FlowInfo *[]FlowInfo, flowID, gemportID uint32, flowStoreCookie uint64, flowCategory string, vlanPcp ...uint32) error {
835 if FlowInfo != nil {
836 for _, Info := range *FlowInfo {
837 if int32(gemportID) == Info.Flow.GemportId && flowCategory != "" && Info.FlowCategory == flowCategory {
838 log.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
839 if Info.FlowCategory == "HSIA_FLOW" && Info.Flow.Classifier.OPbits == vlanPcp[0] {
840 log.Debug("Found matching vlan pcp ", log.Fields{"flowId": flowID, "Vlanpcp": vlanPcp[0]})
841 return nil
842 }
843 }
844 if int32(gemportID) == Info.Flow.GemportId && flowStoreCookie != 0 && Info.FlowStoreCookie == flowStoreCookie {
845 if flowCategory != "" && Info.FlowCategory == flowCategory {
846 log.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
847 return nil
848 }
849 }
850 }
851 }
852 log.Errorw("invalid flow-info", log.Fields{"flow_info": FlowInfo})
853 return errors.New("invalid flow-info")
854}