blob: bb78ebd49402d09c48906399765fef105e4c33e9 [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
salmansiddiqui598eb8e2019-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
Abhilash S.L8ee90712019-04-29 16:24:22 +0530144 ranges.Pools = append(ranges.Pools, &Pool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530145
Girish Gowdru0c588b22019-04-23 23:24:56 -0400146 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700147 Pool.Start = devInfo.AllocIdStart
148 Pool.End = devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400149 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
Abhilash S.L8ee90712019-04-29 16:24:22 +0530150 ranges.Pools = append(ranges.Pools, &Pool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530151
Girish Gowdru0c588b22019-04-23 23:24:56 -0400152 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700153 Pool.Start = devInfo.GemportIdStart
154 Pool.End = devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400155 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
Abhilash S.L8ee90712019-04-29 16:24:22 +0530156 ranges.Pools = append(ranges.Pools, &Pool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530157
Girish Gowdru0c588b22019-04-23 23:24:56 -0400158 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700159 Pool.Start = devInfo.FlowIdStart
160 Pool.End = devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400161 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
Abhilash S.L8ee90712019-04-29 16:24:22 +0530162 ranges.Pools = append(ranges.Pools, &Pool)
163 // Add to device info
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700164 devInfo.Ranges = append(devInfo.Ranges, &ranges)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400165 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530166
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700167 // Create a separate Resource Manager instance for each range. This assumes that
Girish Gowdru0c588b22019-04-23 23:24:56 -0400168 // each technology is represented by only a single range
169 var GlobalPONRsrcMgr *ponrmgr.PONResourceManager
170 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700171 for _, TechRange := range devInfo.Ranges {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400172 technology := TechRange.Technology
173 log.Debugf("Device info technology %s", technology)
174 Ranges[technology] = TechRange
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700175 RsrcMgrsByTech[technology], err = ponrmgr.NewPONResourceManager(technology, deviceType, deviceID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400176 Backend, ResourceMgr.Host, ResourceMgr.Port)
177 if err != nil {
178 log.Errorf("Failed to create pon resource manager instacnce for technology %s", technology)
179 return nil
180 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700181 // resource_mgrs_by_tech[technology] = resource_mgr
Girish Gowdru0c588b22019-04-23 23:24:56 -0400182 if GlobalPONRsrcMgr == nil {
183 GlobalPONRsrcMgr = RsrcMgrsByTech[technology]
184 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700185 for _, IntfID := range TechRange.IntfIds {
186 ResourceMgr.ResourceMgrs[uint32(IntfID)] = RsrcMgrsByTech[technology]
Girish Gowdru0c588b22019-04-23 23:24:56 -0400187 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700188 // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400189 InitializeDeviceResourceRangeAndPool(RsrcMgrsByTech[technology], GlobalPONRsrcMgr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700190 TechRange, devInfo)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400191 }
192 // After we have initialized resource ranges, initialize the
193 // resource pools accordingly.
194 for _, PONRMgr := range RsrcMgrsByTech {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700195 _ = PONRMgr.InitDeviceResourcePool()
Girish Gowdru0c588b22019-04-23 23:24:56 -0400196 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530197 log.Info("Initialization of resource manager success!")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400198 return &ResourceMgr
Abhilash S.L7f17e402019-03-15 17:40:41 +0530199}
200
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700201// InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply
202// device specific information. If KV doesn't exist
203// or is broader than the device, the device's information will
204// dictate the range limits
205func InitializeDeviceResourceRangeAndPool(ponRMgr *ponrmgr.PONResourceManager, globalPONRMgr *ponrmgr.PONResourceManager,
206 techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530207
Girish Gowdru0c588b22019-04-23 23:24:56 -0400208 // init the resource range pool according to the sharing type
Abhilash S.L7f17e402019-03-15 17:40:41 +0530209
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700210 log.Debugf("Resource range pool init for technology %s", ponRMgr.Technology)
211 // first load from KV profiles
212 status := ponRMgr.InitResourceRangesFromKVStore()
213 if !status {
214 log.Debugf("Failed to load resource ranges from KV store for tech %s", ponRMgr.Technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400215 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530216
Girish Gowdru0c588b22019-04-23 23:24:56 -0400217 /*
218 Then apply device specific information. If KV doesn't exist
219 or is broader than the device, the device's informationw ill
220 dictate the range limits
221 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700222 log.Debugf("Using device info to init pon resource ranges for tech", ponRMgr.Technology)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530223
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700224 ONUIDStart := devInfo.OnuIdStart
225 ONUIDEnd := devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400226 ONUIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
227 ONUIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700228 AllocIDStart := devInfo.AllocIdStart
229 AllocIDEnd := devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400230 AllocIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
231 AllocIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700232 GEMPortIDStart := devInfo.GemportIdStart
233 GEMPortIDEnd := devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400234 GEMPortIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
235 GEMPortIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700236 FlowIDStart := devInfo.FlowIdStart
237 FlowIDEnd := devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400238 FlowIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
239 FlowIDSharedPoolID := uint32(0)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530240
Girish Gowdru0c588b22019-04-23 23:24:56 -0400241 var FirstIntfPoolID uint32
242 var SharedPoolID uint32
Abhilash S.L7f17e402019-03-15 17:40:41 +0530243
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400244 /*
245 * 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 -0700246 * if resources are shared across interfaces then SharedPoolID is given a positive number.
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400247 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700248 for _, FirstIntfPoolID = range techRange.IntfIds {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400249 // skip the intf id 0
250 if FirstIntfPoolID == 0 {
251 continue
252 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400253 break
254 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530255
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700256 for _, RangePool := range techRange.Pools {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400257 if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400258 SharedPoolID = FirstIntfPoolID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400259 } else if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_SAME_TECH {
260 SharedPoolID = FirstIntfPoolID
261 } else {
262 SharedPoolID = 0
263 }
264 if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID {
265 ONUIDStart = RangePool.Start
266 ONUIDEnd = RangePool.End
267 ONUIDShared = RangePool.Sharing
268 ONUIDSharedPoolID = SharedPoolID
269 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID {
270 AllocIDStart = RangePool.Start
271 AllocIDEnd = RangePool.End
272 AllocIDShared = RangePool.Sharing
273 AllocIDSharedPoolID = SharedPoolID
274 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID {
275 GEMPortIDStart = RangePool.Start
276 GEMPortIDEnd = RangePool.End
277 GEMPortIDShared = RangePool.Sharing
278 GEMPortIDSharedPoolID = SharedPoolID
279 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID {
280 FlowIDStart = RangePool.Start
281 FlowIDEnd = RangePool.End
282 FlowIDShared = RangePool.Sharing
283 FlowIDSharedPoolID = SharedPoolID
284 }
285 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530286
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700287 log.Debugw("Device info init", log.Fields{"technology": techRange.Technology,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400288 "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd, "onu_id_shared_pool_id": ONUIDSharedPoolID,
289 "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd,
290 "alloc_id_shared_pool_id": AllocIDSharedPoolID,
291 "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd,
292 "gemport_id_shared_pool_id": GEMPortIDSharedPoolID,
293 "flow_id_start": FlowIDStart,
294 "flow_id_end_idx": FlowIDEnd,
295 "flow_id_shared_pool_id": FlowIDSharedPoolID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700296 "intf_ids": techRange.IntfIds,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400297 "uni_id_start": 0,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700298 "uni_id_end_idx": 1, /*MaxUNIIDperONU()*/
299 })
Abhilash S.L7f17e402019-03-15 17:40:41 +0530300
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700301 ponRMgr.InitDefaultPONResourceRanges(ONUIDStart, ONUIDEnd, ONUIDSharedPoolID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400302 AllocIDStart, AllocIDEnd, AllocIDSharedPoolID,
303 GEMPortIDStart, GEMPortIDEnd, GEMPortIDSharedPoolID,
304 FlowIDStart, FlowIDEnd, FlowIDSharedPoolID, 0, 1,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700305 devInfo.PonPorts, techRange.IntfIds)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530306
Girish Gowdru0c588b22019-04-23 23:24:56 -0400307 // For global sharing, make sure to refresh both local and global resource manager instances' range
Abhilash S.L7f17e402019-03-15 17:40:41 +0530308
Girish Gowdru0c588b22019-04-23 23:24:56 -0400309 if ONUIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700310 globalPONRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400311 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700312 ponRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
313 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400314 }
315 if AllocIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700316 globalPONRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400317 "", 0, nil)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530318
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700319 ponRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
320 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400321 }
322 if GEMPortIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700323 globalPONRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400324 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700325 ponRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
326 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400327 }
328 if FlowIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700329 globalPONRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400330 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700331 ponRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
332 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400333 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530334
Girish Gowdru0c588b22019-04-23 23:24:56 -0400335 // Make sure loaded range fits the platform bit encoding ranges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700336 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 +0530337}
338
339/* TODO
340def __del__(self):
341 self.log.info("clearing-device-resource-pool")
342 for key, resource_mgr in self.resource_mgrs.iteritems():
343 resource_mgr.clear_device_resource_pool()
344
345 def assert_pon_id_limit(self, pon_intf_id):
346 assert pon_intf_id in self.resource_mgrs
347
348 def assert_onu_id_limit(self, pon_intf_id, onu_id):
349 self.assert_pon_id_limit(pon_intf_id)
350 self.resource_mgrs[pon_intf_id].assert_resource_limits(onu_id, PONResourceManager.ONU_ID)
351
352 @property
353 def max_uni_id_per_onu(self):
354 return 0 #OpenOltPlatform.MAX_UNIS_PER_ONU-1, zero-based indexing Uncomment or override to make default multi-uni
355
356 def assert_uni_id_limit(self, pon_intf_id, onu_id, uni_id):
357 self.assert_onu_id_limit(pon_intf_id, onu_id)
358 self.resource_mgrs[pon_intf_id].assert_resource_limits(uni_id, PONResourceManager.UNI_ID)
359*/
360
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700361// GetONUID returns the available OnuID for the given pon-port
362func (RsrcMgr *OpenOltResourceMgr) GetONUID(ponIntfID uint32) (uint32, error) {
salmansiddiqui352a45c2019-08-19 10:15:36 +0000363 // Check if Pon Interface ID is present in Resource-manager-map
364 if _, ok := RsrcMgr.ResourceMgrs[ponIntfID]; !ok {
365 err := errors.New("invalid-pon-interface-" + strconv.Itoa(int(ponIntfID)))
366 return 0, err
367 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400368 // Get ONU id for a provided pon interface ID.
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700369 ONUID, err := RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400370 ponrmgr.ONU_ID, 1)
371 if err != nil {
372 log.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700373 ponIntfID, ponrmgr.ONU_ID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400374 return ONUID[0], err
375 }
376 if ONUID != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700377 RsrcMgr.ResourceMgrs[ponIntfID].InitResourceMap(fmt.Sprintf("%d,%d", ponIntfID, ONUID))
378 return ONUID[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400379 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530380
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700381 return 0, err // return OnuID 0 on error
Abhilash S.L7f17e402019-03-15 17:40:41 +0530382}
383
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700384// GetFlowIDInfo returns the slice of flow info of the given pon-port
385// Note: For flows which trap from the NNI and not really associated with any particular
386// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
387func (RsrcMgr *OpenOltResourceMgr) GetFlowIDInfo(ponIntfID uint32, onuID uint32, uniID uint32, flowID uint32) *[]FlowInfo {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530388 var flows []FlowInfo
389
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700390 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
391 if err := RsrcMgr.ResourceMgrs[ponIntfID].GetFlowIDInfo(FlowPath, flowID, &flows); err != nil {
392 log.Errorw("Error while getting flows from KV store", log.Fields{"flowId": flowID})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530393 return nil
394 }
395 if len(flows) == 0 {
396 log.Debugw("No flowInfo found in KV store", log.Fields{"flowPath": FlowPath})
397 return nil
398 }
399 return &flows
400}
401
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700402// GetCurrentFlowIDsForOnu fetches flow ID from the resource manager
403// Note: For flows which trap from the NNI and not really associated with any particular
404// 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 +0530405func (RsrcMgr *OpenOltResourceMgr) GetCurrentFlowIDsForOnu(PONIntfID uint32, ONUID uint32, UNIID uint32) []uint32 {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700406
Abhilash S.L8ee90712019-04-29 16:24:22 +0530407 FlowPath := fmt.Sprintf("%d,%d,%d", PONIntfID, ONUID, UNIID)
408 return RsrcMgr.ResourceMgrs[PONIntfID].GetCurrentFlowIDsForOnu(FlowPath)
409}
410
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700411// UpdateFlowIDInfo updates flow info for the given pon interface, onu id, and uni id
412// Note: For flows which trap from the NNI and not really associated with any particular
413// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
414func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDInfo(ponIntfID int32, onuID int32, uniID int32,
415 flowID uint32, flowData *[]FlowInfo) error {
416 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
417 return RsrcMgr.ResourceMgrs[uint32(ponIntfID)].UpdateFlowIDInfoForOnu(FlowPath, flowID, *flowData)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530418}
419
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700420// GetFlowID return flow ID for a given pon interface id, onu id and uni id
421func (RsrcMgr *OpenOltResourceMgr) GetFlowID(ponIntfID uint32, ONUID uint32, uniID uint32,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400422 gemportID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700423 flowStoreCookie uint64,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400424 flowCategory string, vlanPcp ...uint32) (uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530425
Girish Gowdru0c588b22019-04-23 23:24:56 -0400426 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700427 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, ONUID, uniID)
428 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(FlowPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400429 if FlowIDs != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700430 log.Debugw("Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "ONUID": ONUID, "uniID": uniID, "KVpath": FlowPath})
431 for _, flowID := range FlowIDs {
432 FlowInfo := RsrcMgr.GetFlowIDInfo(ponIntfID, ONUID, uniID, uint32(flowID))
salmansiddiqui598eb8e2019-08-22 03:58:50 +0000433 er := getFlowIDFromFlowInfo(FlowInfo, flowID, gemportID, flowStoreCookie, flowCategory, vlanPcp...)
434 if er == nil {
435 return flowID, er
Abhilash S.L8ee90712019-04-29 16:24:22 +0530436 }
437 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400438 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530439 log.Debug("No matching flows with flow cookie or flow category, allocating new flowid")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700440 FlowIDs, err = RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400441 ponrmgr.FLOW_ID, 1)
442 if err != nil {
443 log.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700444 ponIntfID, ponrmgr.FLOW_ID)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400445 return uint32(0), err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400446 }
447 if FlowIDs != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700448 _ = RsrcMgr.ResourceMgrs[ponIntfID].UpdateFlowIDForOnu(FlowPath, FlowIDs[0], true)
449 return FlowIDs[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400450 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530451
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700452 return 0, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530453}
454
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700455// GetAllocID return the first Alloc ID for a given pon interface id and onu id and then update the resource map on
456// the KV store with the list of alloc_ids allocated for the pon_intf_onu_id tuple
457// Currently of all the alloc_ids available, it returns the first alloc_id in the list for tha given ONU
458func (RsrcMgr *OpenOltResourceMgr) GetAllocID(intfID uint32, onuID uint32, uniID uint32) uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530459
Girish Gowdru0c588b22019-04-23 23:24:56 -0400460 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700461 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
462 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400463 if AllocID != nil {
464 // Since we support only one alloc_id for the ONU at the moment,
465 // return the first alloc_id in the list, if available, for that
466 // ONU.
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700467 log.Debugw("Retrieved alloc ID from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400468 return AllocID[0]
469 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700470 AllocID, err = RsrcMgr.ResourceMgrs[intfID].GetResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400471 ponrmgr.ALLOC_ID, 1)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530472
Girish Gowdru0c588b22019-04-23 23:24:56 -0400473 if AllocID == nil || err != nil {
474 log.Error("Failed to allocate alloc id")
475 return 0
476 }
477 // update the resource map on KV store with the list of alloc_id
478 // allocated for the pon_intf_onu_id tuple
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700479 err = RsrcMgr.ResourceMgrs[intfID].UpdateAllocIdsForOnu(IntfOnuIDUniID, AllocID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400480 if err != nil {
481 log.Error("Failed to update Alloc ID")
482 return 0
483 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530484 log.Debugw("Allocated new Tcont from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400485 return AllocID[0]
Abhilash S.L7f17e402019-03-15 17:40:41 +0530486}
487
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700488// UpdateAllocIdsForOnu updates alloc ids in kv store for a given pon interface id, onu id and uni id
489func (RsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ponPort uint32, onuID uint32, uniID uint32, allocID []uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530490
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700491 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
492 return RsrcMgr.ResourceMgrs[ponPort].UpdateAllocIdsForOnu(IntfOnuIDUniID,
493 allocID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530494}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700495
496// GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id
497func (RsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(intfID uint32, onuID uint32,
498 uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530499
Girish Gowdru0c588b22019-04-23 23:24:56 -0400500 /* Get gem ports for given pon interface , onu id and uni id. */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530501
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700502 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
503 return RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530504}
505
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700506// GetCurrentAllocIDForOnu returns alloc ids for given pon interface and onu id
507// Currently of all the alloc_ids available, it returns the first alloc_id in the list for tha given ONU
508func (RsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDForOnu(intfID uint32, onuID uint32, uniID uint32) uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530509
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700510 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
511 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400512 if AllocID != nil {
513 // Since we support only one alloc_id for the ONU at the moment,
514 // return the first alloc_id in the list, if available, for that
515 // ONU.
516 return AllocID[0]
517 }
518 return 0
Abhilash S.L7f17e402019-03-15 17:40:41 +0530519}
520
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700521// UpdateGEMportsPonportToOnuMapOnKVStore updates onu and uni id associated with the gem port to the kv store
522// This stored information is used when packet_indication is received and we need to derive the ONU Id for which
523// the packet arrived based on the pon_intf and gemport available in the packet_indication
524func (RsrcMgr *OpenOltResourceMgr) UpdateGEMportsPonportToOnuMapOnKVStore(gemPorts []uint32, PonPort uint32,
525 onuID uint32, uniID uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530526
Girish Gowdru0c588b22019-04-23 23:24:56 -0400527 /* Update onu and uni id associated with the gem port to the kv store. */
528 var IntfGEMPortPath string
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700529 Data := fmt.Sprintf("%d %d", onuID, uniID)
530 for _, GEM := range gemPorts {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400531 IntfGEMPortPath = fmt.Sprintf("%d,%d", PonPort, GEM)
532 Val, err := json.Marshal(Data)
533 if err != nil {
534 log.Error("failed to Marshal")
535 return err
536 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700537
Girish Gowdru0c588b22019-04-23 23:24:56 -0400538 if err = RsrcMgr.KVStore.Put(IntfGEMPortPath, Val); err != nil {
539 log.Errorf("Failed to update resource %s", IntfGEMPortPath)
540 return err
541 }
542 }
543 return nil
Abhilash S.L7f17e402019-03-15 17:40:41 +0530544}
545
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700546// GetGEMPortID gets gem port id for a particular pon port, onu id and uni id and then update the resource map on
547// the KV store with the list of gemport_id allocated for the pon_intf_onu_id tuple
548func (RsrcMgr *OpenOltResourceMgr) GetGEMPortID(ponPort uint32, onuID uint32,
549 uniID uint32, NumOfPorts uint32) ([]uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530550
Girish Gowdru0c588b22019-04-23 23:24:56 -0400551 /* Get gem port id for a particular pon port, onu id
552 and uni id.
553 */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530554
Girish Gowdru0c588b22019-04-23 23:24:56 -0400555 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700556 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530557
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700558 GEMPortList := RsrcMgr.ResourceMgrs[ponPort].GetCurrentGEMPortIDsForOnu(IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400559 if GEMPortList != nil {
560 return GEMPortList, nil
561 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530562
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700563 GEMPortList, err = RsrcMgr.ResourceMgrs[ponPort].GetResourceID(ponPort,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400564 ponrmgr.GEMPORT_ID, NumOfPorts)
565 if err != nil && GEMPortList == nil {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530566 log.Errorf("Failed to get gem port id for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400567 return nil, err
568 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530569
Girish Gowdru0c588b22019-04-23 23:24:56 -0400570 // update the resource map on KV store with the list of gemport_id
571 // allocated for the pon_intf_onu_id tuple
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700572 err = RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400573 GEMPortList)
574 if err != nil {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530575 log.Errorf("Failed to update GEM ports to kv store for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400576 return nil, err
577 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700578 _ = RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(GEMPortList, ponPort,
579 onuID, uniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400580 return GEMPortList, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530581}
582
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700583// UpdateGEMPortIDsForOnu updates gemport ids on to the kv store for a given pon port, onu id and uni id
584func (RsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ponPort uint32, onuID uint32,
585 uniID uint32, GEMPortList []uint32) error {
586 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
587 return RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400588 GEMPortList)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530589
590}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700591
592// FreeonuID releases(make free) onu id for a particular pon-port
593func (RsrcMgr *OpenOltResourceMgr) FreeonuID(intfID uint32, onuID []uint32) {
594
595 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID, ponrmgr.ONU_ID, onuID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530596
Girish Gowdru0c588b22019-04-23 23:24:56 -0400597 /* Free onu id for a particular interface.*/
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700598 var IntfonuID string
599 for _, onu := range onuID {
600 IntfonuID = fmt.Sprintf("%d,%d", intfID, onu)
601 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(IntfonuID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400602 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530603}
604
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700605// FreeFlowID returns the free flow id for a given interface, onu id and uni id
606func (RsrcMgr *OpenOltResourceMgr) FreeFlowID(IntfID uint32, onuID uint32,
607 uniID uint32, FlowID uint32) {
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400608 var IntfONUID string
609 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700610 IntfONUID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
611 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(IntfONUID, FlowID, false)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400612 if err != nil {
613 log.Error("Failed to Update flow id infor for %s", IntfONUID)
614 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700615 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(IntfONUID, FlowID)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400616}
617
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700618// FreeFlowIDs releases the flow Ids
619func (RsrcMgr *OpenOltResourceMgr) FreeFlowIDs(IntfID uint32, onuID uint32,
620 uniID uint32, FlowID []uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530621
Girish Gowdru0c588b22019-04-23 23:24:56 -0400622 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(IntfID, ponrmgr.FLOW_ID, FlowID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530623
Abhilash S.L8ee90712019-04-29 16:24:22 +0530624 var IntfOnuIDUniID string
Girish Gowdru0c588b22019-04-23 23:24:56 -0400625 var err error
626 for _, flow := range FlowID {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700627 IntfOnuIDUniID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530628 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(IntfOnuIDUniID, flow, false)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400629 if err != nil {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530630 log.Error("Failed to Update flow id infor for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400631 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530632 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(IntfOnuIDUniID, flow)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400633 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530634}
635
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700636// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id, and the clears the
637// resource map and the onuID associated with (pon_intf_id, gemport_id) tuple,
638func (RsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(intfID uint32, onuID uint32, uniID uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530639
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700640 var onuIDs []uint32
641 onuIDs = append(onuIDs, onuID)
642 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530643
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700644 AllocIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530645
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700646 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400647 ponrmgr.ALLOC_ID,
648 AllocIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530649
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700650 GEMPortIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(IntfOnuIDUniID)
651 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400652 ponrmgr.GEMPORT_ID,
653 GEMPortIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530654
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700655 FlowIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentFlowIDsForOnu(IntfOnuIDUniID)
656 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400657 ponrmgr.FLOW_ID,
658 FlowIDs)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700659 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400660 ponrmgr.ONU_ID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700661 onuIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530662
Girish Gowdru0c588b22019-04-23 23:24:56 -0400663 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700664 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530665
Girish Gowdru0c588b22019-04-23 23:24:56 -0400666 // Clear the ONU Id associated with the (pon_intf_id, gemport_id) tuple.
667 for _, GEM := range GEMPortIDs {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700668 _ = RsrcMgr.KVStore.Delete(fmt.Sprintf("%d,%d", intfID, GEM))
Girish Gowdru0c588b22019-04-23 23:24:56 -0400669 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530670}
671
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700672// IsFlowCookieOnKVStore checks if the given flow cookie is present on the kv store
673// Returns true if the flow cookie is found, otherwise it returns false
674func (RsrcMgr *OpenOltResourceMgr) IsFlowCookieOnKVStore(ponIntfID uint32, onuID uint32, uniID uint32,
675 flowStoreCookie uint64) bool {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530676
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700677 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
678 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(FlowPath)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400679 if FlowIDs != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700680 log.Debugw("Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "onuID": onuID, "uniID": uniID, "KVpath": FlowPath})
681 for _, flowID := range FlowIDs {
682 FlowInfo := RsrcMgr.GetFlowIDInfo(ponIntfID, onuID, uniID, uint32(flowID))
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400683 if FlowInfo != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700684 log.Debugw("Found flows", log.Fields{"flows": *FlowInfo, "flowId": flowID})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400685 for _, Info := range *FlowInfo {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700686 if Info.FlowStoreCookie == flowStoreCookie {
687 log.Debug("Found flow matching with flowStore cookie", log.Fields{"flowId": flowID, "flowStoreCookie": flowStoreCookie})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400688 return true
689 }
690 }
691 }
692 }
693 }
694 return false
695}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400696
salmansiddiqui598eb8e2019-08-22 03:58:50 +0000697// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
698// This path is formed as the following: tp_id/{IntfID, OnuID, UniID}
699func (RsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(IntfID uint32, OnuID uint32, UniID uint32) uint32 {
700 Path := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400701 var Data uint32
salmansiddiqui598eb8e2019-08-22 03:58:50 +0000702 Value, err := RsrcMgr.KVStore.Get(Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400703 if err == nil {
704 if Value != nil {
705 Val, err := kvstore.ToByte(Value.Value)
706 if err != nil {
707 log.Errorw("Failed to convert into byte array", log.Fields{"error": err})
708 return Data
709 }
710 if err = json.Unmarshal(Val, &Data); err != nil {
711 log.Error("Failed to unmarshal", log.Fields{"error": err})
712 return Data
713 }
714 }
715 } else {
716 log.Errorf("Failed to get TP id from kvstore for path %s", Path)
717 }
718 log.Debugf("Getting TP id %d from path %s", Data, Path)
719 return Data
720
721}
722
salmansiddiqui598eb8e2019-08-22 03:58:50 +0000723// RemoveTechProfileIDForOnu deletes the tech-profile-id from the KV-Store for the given onu based on the path
724// This path is formed as the following: tp_id/{IntfID, OnuID, UniID}
725func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(IntfID uint32, OnuID uint32, UniID uint32) error {
726 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
727 if err := RsrcMgr.KVStore.Delete(IntfOnuUniID); err != nil {
728 log.Error("Failed to delete techprofile id resource %s in KV store", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400729 return err
730 }
731 return nil
732}
733
salmansiddiqui598eb8e2019-08-22 03:58:50 +0000734//UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
735// This path is formed as the following: tp_id/{IntfID, OnuID, UniID}
736func (RsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(IntfID uint32, OnuID uint32,
737 UniID uint32, TpID uint32) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400738 var Value []byte
739 var err error
740
salmansiddiqui598eb8e2019-08-22 03:58:50 +0000741 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
742 log.Debugf("updating tp id %d on path %s", TpID, IntfOnuUniID)
743 Value, err = json.Marshal(TpID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400744 if err != nil {
745 log.Error("failed to Marshal")
746 return err
747 }
salmansiddiqui598eb8e2019-08-22 03:58:50 +0000748 if err = RsrcMgr.KVStore.Put(IntfOnuUniID, Value); err != nil {
749 log.Errorf("Failed to update resource %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400750 return err
751 }
752 return err
753}
754
salmansiddiqui598eb8e2019-08-22 03:58:50 +0000755// UpdateMeterIDForOnu updates the meter id in the KV-Store for the given onu based on the path
756// This path is formed as the following: tp_id/{IntfID, OnuID, UniID}/direction
757func (RsrcMgr *OpenOltResourceMgr) UpdateMeterIDForOnu(Direction string, IntfID uint32, OnuID uint32,
758 UniID uint32, MeterConfig *ofp.OfpMeterConfig) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400759 var Value []byte
760 var err error
761
salmansiddiqui598eb8e2019-08-22 03:58:50 +0000762 IntfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400763 Value, err = json.Marshal(*MeterConfig)
764 if err != nil {
765 log.Error("failed to Marshal meter config")
766 return err
767 }
salmansiddiqui598eb8e2019-08-22 03:58:50 +0000768 if err = RsrcMgr.KVStore.Put(IntfOnuUniID, Value); err != nil {
769 log.Errorf("Failed to store meter into KV store %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400770 return err
771 }
772 return err
773}
774
salmansiddiqui598eb8e2019-08-22 03:58:50 +0000775// GetMeterIDForOnu fetches the meter-id fromthe kv store for the given onu based on the path
776// This path is formed as the following: tp_id/{IntfID, OnuID, UniID}/direction
777func (RsrcMgr *OpenOltResourceMgr) GetMeterIDForOnu(Direction string, IntfID uint32, OnuID uint32, UniID uint32) (*ofp.OfpMeterConfig, error) {
778 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400779 var meterConfig ofp.OfpMeterConfig
salmansiddiqui598eb8e2019-08-22 03:58:50 +0000780 Value, err := RsrcMgr.KVStore.Get(Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400781 if err == nil {
782 if Value != nil {
783 log.Debug("Found meter in KV store", log.Fields{"Direction": Direction})
salmansiddiqui598eb8e2019-08-22 03:58:50 +0000784 Val, er := kvstore.ToByte(Value.Value)
785 if er != nil {
786 log.Errorw("Failed to convert into byte array", log.Fields{"error": er})
787 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400788 }
salmansiddiqui598eb8e2019-08-22 03:58:50 +0000789 if er = json.Unmarshal(Val, &meterConfig); er != nil {
790 log.Error("Failed to unmarshal meterconfig", log.Fields{"error": er})
791 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400792 }
793 } else {
794 log.Debug("meter-does-not-exists-in-KVStore")
795 return nil, err
796 }
797 } else {
798 log.Errorf("Failed to get Meter config from kvstore for path %s", Path)
799
800 }
801 return &meterConfig, err
802}
803
salmansiddiqui598eb8e2019-08-22 03:58:50 +0000804// RemoveMeterIDForOnu deletes the meter-id from the kV-Store for the given onu based on the path
805// This path is formed as the following: tp_id/{IntfID, OnuID, UniID}/direction
806func (RsrcMgr *OpenOltResourceMgr) RemoveMeterIDForOnu(Direction string, IntfID uint32, OnuID uint32, UniID uint32) error {
807 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, Direction)
808 if err := RsrcMgr.KVStore.Delete(Path); err != nil {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400809 log.Errorf("Failed to delete meter id %s from kvstore ", Path)
810 return err
811 }
812 return nil
813}
salmansiddiqui598eb8e2019-08-22 03:58:50 +0000814
815func getFlowIDFromFlowInfo(FlowInfo *[]FlowInfo, flowID, gemportID uint32, flowStoreCookie uint64, flowCategory string, vlanPcp ...uint32) error {
816 if FlowInfo != nil {
817 for _, Info := range *FlowInfo {
818 if int32(gemportID) == Info.Flow.GemportId && flowCategory != "" && Info.FlowCategory == flowCategory {
819 log.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
820 if Info.FlowCategory == "HSIA_FLOW" && Info.Flow.Classifier.OPbits == vlanPcp[0] {
821 log.Debug("Found matching vlan pcp ", log.Fields{"flowId": flowID, "Vlanpcp": vlanPcp[0]})
822 return nil
823 }
824 }
825 if int32(gemportID) == Info.Flow.GemportId && flowStoreCookie != 0 && Info.FlowStoreCookie == flowStoreCookie {
826 if flowCategory != "" && Info.FlowCategory == flowCategory {
827 log.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
828 return nil
829 }
830 }
831 }
832 }
833 log.Errorw("invalid flow-info", log.Fields{"flow_info": FlowInfo})
834 return errors.New("invalid flow-info")
835}