blob: dc5300aaa9d26c6e1761019f9e50a7b3aa6d1a44 [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
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070035// KvstoreTimeout specifies the time out for KV Store Connection
36const KvstoreTimeout = 5
Abhilash S.L7f17e402019-03-15 17:40:41 +053037
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070038// BasePathKvStore - service/voltha/openolt/<device_id>
39const BasePathKvStore = "service/voltha/openolt/{%s}"
40
Manikkaraj kb1d51442019-07-23 10:41:02 -040041const TP_ID_PATH_SUFFIX = "tp_id/{%d,%d,%d}" // tp_id/<(pon_id, onu_id, uni_id)>
42const METER_ID_PATH_SUFFIX = "meter_id/{%d,%d,%d}/{%s}" // meter_id/<(pon_id, onu_id, uni_id)>/<direction>
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070043// FlowInfo holds the flow information
Abhilash S.L8ee90712019-04-29 16:24:22 +053044type FlowInfo struct {
45 Flow *openolt.Flow
46 FlowStoreCookie uint64
47 FlowCategory string
48}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070049
50// OpenOltResourceMgr holds resource related information as provided below for each field
Abhilash S.L7f17e402019-03-15 17:40:41 +053051type OpenOltResourceMgr struct {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070052 DeviceID string // OLT device id
Girish Gowdru0c588b22019-04-23 23:24:56 -040053 HostAndPort string // Host and port of the kv store to connect to
54 Args string // args
55 KVStore *model.Backend // backend kv store connection handle
56 DeviceType string
57 Host string // Host ip of the kv store
58 Port int // port of the kv store
59 DevInfo *openolt.DeviceInfo // device information
60 // array of pon resource managers per interface technology
61 ResourceMgrs map[uint32]*ponrmgr.PONResourceManager
Abhilash S.L7f17e402019-03-15 17:40:41 +053062}
63
Manikkaraj kb1d51442019-07-23 10:41:02 -040064func newKVClient(storeType string, address string, timeout uint32) (kvstore.Client, error) {
Girish Gowdru0c588b22019-04-23 23:24:56 -040065 log.Infow("kv-store-type", log.Fields{"store": storeType})
66 switch storeType {
67 case "consul":
68 return kvstore.NewConsulClient(address, int(timeout))
69 case "etcd":
70 return kvstore.NewEtcdClient(address, int(timeout))
71 }
72 return nil, errors.New("unsupported-kv-store")
Abhilash S.L7f17e402019-03-15 17:40:41 +053073}
74
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070075// SetKVClient sets the KV client and return a kv backend
76func SetKVClient(backend string, Host string, Port int, DeviceID string) *model.Backend {
Girish Gowdru0c588b22019-04-23 23:24:56 -040077 addr := Host + ":" + strconv.Itoa(Port)
78 // TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
79 // issue between kv store and backend , core is not calling NewBackend directly
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070080 kvClient, err := newKVClient(backend, addr, KvstoreTimeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -040081 if err != nil {
82 log.Fatalw("Failed to init KV client\n", log.Fields{"err": err})
83 return nil
84 }
85 kvbackend := &model.Backend{
86 Client: kvClient,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070087 StoreType: backend,
Girish Gowdru0c588b22019-04-23 23:24:56 -040088 Host: Host,
89 Port: Port,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070090 Timeout: KvstoreTimeout,
91 PathPrefix: fmt.Sprintf(BasePathKvStore, DeviceID)}
Abhilash S.L7f17e402019-03-15 17:40:41 +053092
Girish Gowdru0c588b22019-04-23 23:24:56 -040093 return kvbackend
Abhilash S.L7f17e402019-03-15 17:40:41 +053094}
95
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070096// NewResourceMgr init a New resource maanger instance which in turn instantiates pon resource manager
97// instances according to technology. Initializes the default resource ranges for all
98// the resources.
99func NewResourceMgr(deviceID string, KVStoreHostPort string, kvStoreType string, deviceType string, devInfo *openolt.DeviceInfo) *OpenOltResourceMgr {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400100 var ResourceMgr OpenOltResourceMgr
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700101 log.Debugf("Init new resource manager , host_port: %s, deviceid: %s", KVStoreHostPort, deviceID)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530102 ResourceMgr.HostAndPort = KVStoreHostPort
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700103 ResourceMgr.DeviceType = deviceType
104 ResourceMgr.DevInfo = devInfo
105 IPPort := strings.Split(KVStoreHostPort, ":")
106 ResourceMgr.Host = IPPort[0]
107 ResourceMgr.Port, _ = strconv.Atoi(IPPort[1])
Abhilash S.L7f17e402019-03-15 17:40:41 +0530108
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700109 Backend := kvStoreType
Girish Gowdru0c588b22019-04-23 23:24:56 -0400110 ResourceMgr.KVStore = SetKVClient(Backend, ResourceMgr.Host,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700111 ResourceMgr.Port, deviceID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400112 if ResourceMgr.KVStore == nil {
113 log.Error("Failed to setup KV store")
114 }
115 Ranges := make(map[string]*openolt.DeviceInfo_DeviceResourceRanges)
116 RsrcMgrsByTech := make(map[string]*ponrmgr.PONResourceManager)
117 ResourceMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530118
Girish Gowdru0c588b22019-04-23 23:24:56 -0400119 // TODO self.args = registry('main').get_args()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530120
Girish Gowdru0c588b22019-04-23 23:24:56 -0400121 /*
122 If a legacy driver returns protobuf without any ranges,s synthesize one from
123 the legacy global per-device informaiton. This, in theory, is temporary until
124 the legacy drivers are upgrade to support pool ranges.
125 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700126 if devInfo.Ranges == nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400127 var ranges openolt.DeviceInfo_DeviceResourceRanges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700128 ranges.Technology = devInfo.GetTechnology()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530129
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700130 NumPONPorts := devInfo.GetPonPorts()
Girish Gowdru0c588b22019-04-23 23:24:56 -0400131 var index uint32
132 for index = 0; index < NumPONPorts; index++ {
133 ranges.IntfIds = append(ranges.IntfIds, index)
134 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530135
Abhilash S.L8ee90712019-04-29 16:24:22 +0530136 var Pool openolt.DeviceInfo_DeviceResourceRanges_Pool
Girish Gowdru0c588b22019-04-23 23:24:56 -0400137 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700138 Pool.Start = devInfo.OnuIdStart
139 Pool.End = devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400140 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
Abhilash S.L8ee90712019-04-29 16:24:22 +0530141 ranges.Pools = append(ranges.Pools, &Pool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530142
Girish Gowdru0c588b22019-04-23 23:24:56 -0400143 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700144 Pool.Start = devInfo.AllocIdStart
145 Pool.End = devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400146 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
Abhilash S.L8ee90712019-04-29 16:24:22 +0530147 ranges.Pools = append(ranges.Pools, &Pool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530148
Girish Gowdru0c588b22019-04-23 23:24:56 -0400149 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700150 Pool.Start = devInfo.GemportIdStart
151 Pool.End = devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400152 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
Abhilash S.L8ee90712019-04-29 16:24:22 +0530153 ranges.Pools = append(ranges.Pools, &Pool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530154
Girish Gowdru0c588b22019-04-23 23:24:56 -0400155 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700156 Pool.Start = devInfo.FlowIdStart
157 Pool.End = devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400158 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
Abhilash S.L8ee90712019-04-29 16:24:22 +0530159 ranges.Pools = append(ranges.Pools, &Pool)
160 // Add to device info
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700161 devInfo.Ranges = append(devInfo.Ranges, &ranges)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400162 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530163
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700164 // Create a separate Resource Manager instance for each range. This assumes that
Girish Gowdru0c588b22019-04-23 23:24:56 -0400165 // each technology is represented by only a single range
166 var GlobalPONRsrcMgr *ponrmgr.PONResourceManager
167 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700168 for _, TechRange := range devInfo.Ranges {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400169 technology := TechRange.Technology
170 log.Debugf("Device info technology %s", technology)
171 Ranges[technology] = TechRange
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700172 RsrcMgrsByTech[technology], err = ponrmgr.NewPONResourceManager(technology, deviceType, deviceID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400173 Backend, ResourceMgr.Host, ResourceMgr.Port)
174 if err != nil {
175 log.Errorf("Failed to create pon resource manager instacnce for technology %s", technology)
176 return nil
177 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700178 // resource_mgrs_by_tech[technology] = resource_mgr
Girish Gowdru0c588b22019-04-23 23:24:56 -0400179 if GlobalPONRsrcMgr == nil {
180 GlobalPONRsrcMgr = RsrcMgrsByTech[technology]
181 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700182 for _, IntfID := range TechRange.IntfIds {
183 ResourceMgr.ResourceMgrs[uint32(IntfID)] = RsrcMgrsByTech[technology]
Girish Gowdru0c588b22019-04-23 23:24:56 -0400184 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700185 // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400186 InitializeDeviceResourceRangeAndPool(RsrcMgrsByTech[technology], GlobalPONRsrcMgr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700187 TechRange, devInfo)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400188 }
189 // After we have initialized resource ranges, initialize the
190 // resource pools accordingly.
191 for _, PONRMgr := range RsrcMgrsByTech {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700192 _ = PONRMgr.InitDeviceResourcePool()
Girish Gowdru0c588b22019-04-23 23:24:56 -0400193 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530194 log.Info("Initialization of resource manager success!")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400195 return &ResourceMgr
Abhilash S.L7f17e402019-03-15 17:40:41 +0530196}
197
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700198// InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply
199// device specific information. If KV doesn't exist
200// or is broader than the device, the device's information will
201// dictate the range limits
202func InitializeDeviceResourceRangeAndPool(ponRMgr *ponrmgr.PONResourceManager, globalPONRMgr *ponrmgr.PONResourceManager,
203 techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530204
Girish Gowdru0c588b22019-04-23 23:24:56 -0400205 // init the resource range pool according to the sharing type
Abhilash S.L7f17e402019-03-15 17:40:41 +0530206
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700207 log.Debugf("Resource range pool init for technology %s", ponRMgr.Technology)
208 // first load from KV profiles
209 status := ponRMgr.InitResourceRangesFromKVStore()
210 if !status {
211 log.Debugf("Failed to load resource ranges from KV store for tech %s", ponRMgr.Technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400212 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530213
Girish Gowdru0c588b22019-04-23 23:24:56 -0400214 /*
215 Then apply device specific information. If KV doesn't exist
216 or is broader than the device, the device's informationw ill
217 dictate the range limits
218 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700219 log.Debugf("Using device info to init pon resource ranges for tech", ponRMgr.Technology)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530220
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700221 ONUIDStart := devInfo.OnuIdStart
222 ONUIDEnd := devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400223 ONUIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
224 ONUIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700225 AllocIDStart := devInfo.AllocIdStart
226 AllocIDEnd := devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400227 AllocIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
228 AllocIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700229 GEMPortIDStart := devInfo.GemportIdStart
230 GEMPortIDEnd := devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400231 GEMPortIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
232 GEMPortIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700233 FlowIDStart := devInfo.FlowIdStart
234 FlowIDEnd := devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400235 FlowIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
236 FlowIDSharedPoolID := uint32(0)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530237
Girish Gowdru0c588b22019-04-23 23:24:56 -0400238 var FirstIntfPoolID uint32
239 var SharedPoolID uint32
Abhilash S.L7f17e402019-03-15 17:40:41 +0530240
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400241 /*
242 * 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 -0700243 * if resources are shared across interfaces then SharedPoolID is given a positive number.
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400244 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700245 for _, FirstIntfPoolID = range techRange.IntfIds {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400246 // skip the intf id 0
247 if FirstIntfPoolID == 0 {
248 continue
249 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400250 break
251 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530252
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700253 for _, RangePool := range techRange.Pools {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400254 if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400255 SharedPoolID = FirstIntfPoolID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400256 } else if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_SAME_TECH {
257 SharedPoolID = FirstIntfPoolID
258 } else {
259 SharedPoolID = 0
260 }
261 if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID {
262 ONUIDStart = RangePool.Start
263 ONUIDEnd = RangePool.End
264 ONUIDShared = RangePool.Sharing
265 ONUIDSharedPoolID = SharedPoolID
266 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID {
267 AllocIDStart = RangePool.Start
268 AllocIDEnd = RangePool.End
269 AllocIDShared = RangePool.Sharing
270 AllocIDSharedPoolID = SharedPoolID
271 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID {
272 GEMPortIDStart = RangePool.Start
273 GEMPortIDEnd = RangePool.End
274 GEMPortIDShared = RangePool.Sharing
275 GEMPortIDSharedPoolID = SharedPoolID
276 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID {
277 FlowIDStart = RangePool.Start
278 FlowIDEnd = RangePool.End
279 FlowIDShared = RangePool.Sharing
280 FlowIDSharedPoolID = SharedPoolID
281 }
282 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530283
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700284 log.Debugw("Device info init", log.Fields{"technology": techRange.Technology,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400285 "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd, "onu_id_shared_pool_id": ONUIDSharedPoolID,
286 "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd,
287 "alloc_id_shared_pool_id": AllocIDSharedPoolID,
288 "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd,
289 "gemport_id_shared_pool_id": GEMPortIDSharedPoolID,
290 "flow_id_start": FlowIDStart,
291 "flow_id_end_idx": FlowIDEnd,
292 "flow_id_shared_pool_id": FlowIDSharedPoolID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700293 "intf_ids": techRange.IntfIds,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400294 "uni_id_start": 0,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700295 "uni_id_end_idx": 1, /*MaxUNIIDperONU()*/
296 })
Abhilash S.L7f17e402019-03-15 17:40:41 +0530297
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700298 ponRMgr.InitDefaultPONResourceRanges(ONUIDStart, ONUIDEnd, ONUIDSharedPoolID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400299 AllocIDStart, AllocIDEnd, AllocIDSharedPoolID,
300 GEMPortIDStart, GEMPortIDEnd, GEMPortIDSharedPoolID,
301 FlowIDStart, FlowIDEnd, FlowIDSharedPoolID, 0, 1,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700302 devInfo.PonPorts, techRange.IntfIds)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530303
Girish Gowdru0c588b22019-04-23 23:24:56 -0400304 // For global sharing, make sure to refresh both local and global resource manager instances' range
Abhilash S.L7f17e402019-03-15 17:40:41 +0530305
Girish Gowdru0c588b22019-04-23 23:24:56 -0400306 if ONUIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700307 globalPONRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400308 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700309 ponRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
310 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400311 }
312 if AllocIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700313 globalPONRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400314 "", 0, nil)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530315
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700316 ponRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
317 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400318 }
319 if GEMPortIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700320 globalPONRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400321 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700322 ponRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
323 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400324 }
325 if FlowIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700326 globalPONRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400327 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700328 ponRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
329 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400330 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530331
Girish Gowdru0c588b22019-04-23 23:24:56 -0400332 // Make sure loaded range fits the platform bit encoding ranges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700333 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 +0530334}
335
336/* TODO
337def __del__(self):
338 self.log.info("clearing-device-resource-pool")
339 for key, resource_mgr in self.resource_mgrs.iteritems():
340 resource_mgr.clear_device_resource_pool()
341
342 def assert_pon_id_limit(self, pon_intf_id):
343 assert pon_intf_id in self.resource_mgrs
344
345 def assert_onu_id_limit(self, pon_intf_id, onu_id):
346 self.assert_pon_id_limit(pon_intf_id)
347 self.resource_mgrs[pon_intf_id].assert_resource_limits(onu_id, PONResourceManager.ONU_ID)
348
349 @property
350 def max_uni_id_per_onu(self):
351 return 0 #OpenOltPlatform.MAX_UNIS_PER_ONU-1, zero-based indexing Uncomment or override to make default multi-uni
352
353 def assert_uni_id_limit(self, pon_intf_id, onu_id, uni_id):
354 self.assert_onu_id_limit(pon_intf_id, onu_id)
355 self.resource_mgrs[pon_intf_id].assert_resource_limits(uni_id, PONResourceManager.UNI_ID)
356*/
357
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700358// GetONUID returns the available OnuID for the given pon-port
359func (RsrcMgr *OpenOltResourceMgr) GetONUID(ponIntfID uint32) (uint32, error) {
salmansiddiqui352a45c2019-08-19 10:15:36 +0000360 // Check if Pon Interface ID is present in Resource-manager-map
361 if _, ok := RsrcMgr.ResourceMgrs[ponIntfID]; !ok {
362 err := errors.New("invalid-pon-interface-" + strconv.Itoa(int(ponIntfID)))
363 return 0, err
364 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400365 // Get ONU id for a provided pon interface ID.
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700366 ONUID, err := RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400367 ponrmgr.ONU_ID, 1)
368 if err != nil {
369 log.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700370 ponIntfID, ponrmgr.ONU_ID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400371 return ONUID[0], err
372 }
373 if ONUID != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700374 RsrcMgr.ResourceMgrs[ponIntfID].InitResourceMap(fmt.Sprintf("%d,%d", ponIntfID, ONUID))
375 return ONUID[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400376 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530377
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700378 return 0, err // return OnuID 0 on error
Abhilash S.L7f17e402019-03-15 17:40:41 +0530379}
380
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700381// GetFlowIDInfo returns the slice of flow info of the given pon-port
382// Note: For flows which trap from the NNI and not really associated with any particular
383// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
384func (RsrcMgr *OpenOltResourceMgr) GetFlowIDInfo(ponIntfID uint32, onuID uint32, uniID uint32, flowID uint32) *[]FlowInfo {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530385 var flows []FlowInfo
386
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700387 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
388 if err := RsrcMgr.ResourceMgrs[ponIntfID].GetFlowIDInfo(FlowPath, flowID, &flows); err != nil {
389 log.Errorw("Error while getting flows from KV store", log.Fields{"flowId": flowID})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530390 return nil
391 }
392 if len(flows) == 0 {
393 log.Debugw("No flowInfo found in KV store", log.Fields{"flowPath": FlowPath})
394 return nil
395 }
396 return &flows
397}
398
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700399// GetCurrentFlowIDsForOnu fetches flow ID from the resource manager
400// Note: For flows which trap from the NNI and not really associated with any particular
401// 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 +0530402func (RsrcMgr *OpenOltResourceMgr) GetCurrentFlowIDsForOnu(PONIntfID uint32, ONUID uint32, UNIID uint32) []uint32 {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700403
Abhilash S.L8ee90712019-04-29 16:24:22 +0530404 FlowPath := fmt.Sprintf("%d,%d,%d", PONIntfID, ONUID, UNIID)
405 return RsrcMgr.ResourceMgrs[PONIntfID].GetCurrentFlowIDsForOnu(FlowPath)
406}
407
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700408// UpdateFlowIDInfo updates flow info for the given pon interface, onu id, and uni id
409// Note: For flows which trap from the NNI and not really associated with any particular
410// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
411func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDInfo(ponIntfID int32, onuID int32, uniID int32,
412 flowID uint32, flowData *[]FlowInfo) error {
413 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
414 return RsrcMgr.ResourceMgrs[uint32(ponIntfID)].UpdateFlowIDInfoForOnu(FlowPath, flowID, *flowData)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530415}
416
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700417// GetFlowID return flow ID for a given pon interface id, onu id and uni id
418func (RsrcMgr *OpenOltResourceMgr) GetFlowID(ponIntfID uint32, ONUID uint32, uniID uint32,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400419 gemportID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700420 flowStoreCookie uint64,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400421 flowCategory string, vlanPcp ...uint32) (uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530422
Girish Gowdru0c588b22019-04-23 23:24:56 -0400423 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700424 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, ONUID, uniID)
425 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(FlowPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400426 if FlowIDs != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700427 log.Debugw("Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "ONUID": ONUID, "uniID": uniID, "KVpath": FlowPath})
428 for _, flowID := range FlowIDs {
429 FlowInfo := RsrcMgr.GetFlowIDInfo(ponIntfID, ONUID, uniID, uint32(flowID))
Abhilash S.L8ee90712019-04-29 16:24:22 +0530430 if FlowInfo != nil {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530431 for _, Info := range *FlowInfo {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400432 if int32(gemportID) == Info.Flow.GemportId && flowCategory != "" && Info.FlowCategory == flowCategory {
433 log.Debug("Found flow matching with flow catagory", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
434 if Info.FlowCategory == "HSIA_FLOW" && Info.Flow.Classifier.OPbits == vlanPcp[0] {
435 log.Debug("Found matching vlan pcp ", log.Fields{"flowId": flowID, "Vlanpcp": vlanPcp[0]})
436 return flowID, nil
437 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530438 }
Manikkaraj kb1d51442019-07-23 10:41:02 -0400439 if int32(gemportID) == Info.Flow.GemportId && flowStoreCookie != 0 && Info.FlowStoreCookie == flowStoreCookie {
440 if flowCategory != "" && Info.FlowCategory == flowCategory {
441 log.Debug("Found flow matching with flow catagory", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
442 return flowID, nil
443 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530444 }
445 }
446 }
447 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400448 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530449 log.Debug("No matching flows with flow cookie or flow category, allocating new flowid")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700450 FlowIDs, err = RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400451 ponrmgr.FLOW_ID, 1)
452 if err != nil {
453 log.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700454 ponIntfID, ponrmgr.FLOW_ID)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400455 return uint32(0), err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400456 }
457 if FlowIDs != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700458 _ = RsrcMgr.ResourceMgrs[ponIntfID].UpdateFlowIDForOnu(FlowPath, FlowIDs[0], true)
459 return FlowIDs[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400460 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530461
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700462 return 0, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530463}
464
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700465// GetAllocID return the first Alloc ID for a given pon interface id and onu id and then update the resource map on
466// the KV store with the list of alloc_ids allocated for the pon_intf_onu_id tuple
467// Currently of all the alloc_ids available, it returns the first alloc_id in the list for tha given ONU
468func (RsrcMgr *OpenOltResourceMgr) GetAllocID(intfID uint32, onuID uint32, uniID uint32) uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530469
Girish Gowdru0c588b22019-04-23 23:24:56 -0400470 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700471 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
472 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400473 if AllocID != nil {
474 // Since we support only one alloc_id for the ONU at the moment,
475 // return the first alloc_id in the list, if available, for that
476 // ONU.
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700477 log.Debugw("Retrieved alloc ID from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400478 return AllocID[0]
479 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700480 AllocID, err = RsrcMgr.ResourceMgrs[intfID].GetResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400481 ponrmgr.ALLOC_ID, 1)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530482
Girish Gowdru0c588b22019-04-23 23:24:56 -0400483 if AllocID == nil || err != nil {
484 log.Error("Failed to allocate alloc id")
485 return 0
486 }
487 // update the resource map on KV store with the list of alloc_id
488 // allocated for the pon_intf_onu_id tuple
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700489 err = RsrcMgr.ResourceMgrs[intfID].UpdateAllocIdsForOnu(IntfOnuIDUniID, AllocID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400490 if err != nil {
491 log.Error("Failed to update Alloc ID")
492 return 0
493 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530494 log.Debugw("Allocated new Tcont from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400495 return AllocID[0]
Abhilash S.L7f17e402019-03-15 17:40:41 +0530496}
497
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700498// UpdateAllocIdsForOnu updates alloc ids in kv store for a given pon interface id, onu id and uni id
499func (RsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ponPort uint32, onuID uint32, uniID uint32, allocID []uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530500
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700501 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
502 return RsrcMgr.ResourceMgrs[ponPort].UpdateAllocIdsForOnu(IntfOnuIDUniID,
503 allocID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530504}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700505
506// GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id
507func (RsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(intfID uint32, onuID uint32,
508 uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530509
Girish Gowdru0c588b22019-04-23 23:24:56 -0400510 /* Get gem ports for given pon interface , onu id and uni id. */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530511
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700512 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
513 return RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530514}
515
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700516// GetCurrentAllocIDForOnu returns alloc ids for given pon interface and onu id
517// Currently of all the alloc_ids available, it returns the first alloc_id in the list for tha given ONU
518func (RsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDForOnu(intfID uint32, onuID uint32, uniID uint32) uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530519
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700520 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
521 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400522 if AllocID != nil {
523 // Since we support only one alloc_id for the ONU at the moment,
524 // return the first alloc_id in the list, if available, for that
525 // ONU.
526 return AllocID[0]
527 }
528 return 0
Abhilash S.L7f17e402019-03-15 17:40:41 +0530529}
530
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700531// UpdateGEMportsPonportToOnuMapOnKVStore updates onu and uni id associated with the gem port to the kv store
532// This stored information is used when packet_indication is received and we need to derive the ONU Id for which
533// the packet arrived based on the pon_intf and gemport available in the packet_indication
534func (RsrcMgr *OpenOltResourceMgr) UpdateGEMportsPonportToOnuMapOnKVStore(gemPorts []uint32, PonPort uint32,
535 onuID uint32, uniID uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530536
Girish Gowdru0c588b22019-04-23 23:24:56 -0400537 /* Update onu and uni id associated with the gem port to the kv store. */
538 var IntfGEMPortPath string
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700539 Data := fmt.Sprintf("%d %d", onuID, uniID)
540 for _, GEM := range gemPorts {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400541 IntfGEMPortPath = fmt.Sprintf("%d,%d", PonPort, GEM)
542 Val, err := json.Marshal(Data)
543 if err != nil {
544 log.Error("failed to Marshal")
545 return err
546 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700547
Girish Gowdru0c588b22019-04-23 23:24:56 -0400548 if err = RsrcMgr.KVStore.Put(IntfGEMPortPath, Val); err != nil {
549 log.Errorf("Failed to update resource %s", IntfGEMPortPath)
550 return err
551 }
552 }
553 return nil
Abhilash S.L7f17e402019-03-15 17:40:41 +0530554}
555
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700556// GetGEMPortID gets gem port id for a particular pon port, onu id and uni id and then update the resource map on
557// the KV store with the list of gemport_id allocated for the pon_intf_onu_id tuple
558func (RsrcMgr *OpenOltResourceMgr) GetGEMPortID(ponPort uint32, onuID uint32,
559 uniID uint32, NumOfPorts uint32) ([]uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530560
Girish Gowdru0c588b22019-04-23 23:24:56 -0400561 /* Get gem port id for a particular pon port, onu id
562 and uni id.
563 */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530564
Girish Gowdru0c588b22019-04-23 23:24:56 -0400565 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700566 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530567
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700568 GEMPortList := RsrcMgr.ResourceMgrs[ponPort].GetCurrentGEMPortIDsForOnu(IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400569 if GEMPortList != nil {
570 return GEMPortList, nil
571 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530572
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700573 GEMPortList, err = RsrcMgr.ResourceMgrs[ponPort].GetResourceID(ponPort,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400574 ponrmgr.GEMPORT_ID, NumOfPorts)
575 if err != nil && GEMPortList == nil {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530576 log.Errorf("Failed to get gem port id for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400577 return nil, err
578 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530579
Girish Gowdru0c588b22019-04-23 23:24:56 -0400580 // update the resource map on KV store with the list of gemport_id
581 // allocated for the pon_intf_onu_id tuple
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700582 err = RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400583 GEMPortList)
584 if err != nil {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530585 log.Errorf("Failed to update GEM ports to kv store for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400586 return nil, err
587 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700588 _ = RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(GEMPortList, ponPort,
589 onuID, uniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400590 return GEMPortList, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530591}
592
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700593// UpdateGEMPortIDsForOnu updates gemport ids on to the kv store for a given pon port, onu id and uni id
594func (RsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ponPort uint32, onuID uint32,
595 uniID uint32, GEMPortList []uint32) error {
596 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
597 return RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400598 GEMPortList)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530599
600}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700601
602// FreeonuID releases(make free) onu id for a particular pon-port
603func (RsrcMgr *OpenOltResourceMgr) FreeonuID(intfID uint32, onuID []uint32) {
604
605 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID, ponrmgr.ONU_ID, onuID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530606
Girish Gowdru0c588b22019-04-23 23:24:56 -0400607 /* Free onu id for a particular interface.*/
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700608 var IntfonuID string
609 for _, onu := range onuID {
610 IntfonuID = fmt.Sprintf("%d,%d", intfID, onu)
611 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(IntfonuID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400612 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530613}
614
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700615// FreeFlowID returns the free flow id for a given interface, onu id and uni id
616func (RsrcMgr *OpenOltResourceMgr) FreeFlowID(IntfID uint32, onuID uint32,
617 uniID uint32, FlowID uint32) {
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400618 var IntfONUID string
619 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700620 IntfONUID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
621 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(IntfONUID, FlowID, false)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400622 if err != nil {
623 log.Error("Failed to Update flow id infor for %s", IntfONUID)
624 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700625 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(IntfONUID, FlowID)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400626}
627
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700628// FreeFlowIDs releases the flow Ids
629func (RsrcMgr *OpenOltResourceMgr) FreeFlowIDs(IntfID uint32, onuID uint32,
630 uniID uint32, FlowID []uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530631
Girish Gowdru0c588b22019-04-23 23:24:56 -0400632 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(IntfID, ponrmgr.FLOW_ID, FlowID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530633
Abhilash S.L8ee90712019-04-29 16:24:22 +0530634 var IntfOnuIDUniID string
Girish Gowdru0c588b22019-04-23 23:24:56 -0400635 var err error
636 for _, flow := range FlowID {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700637 IntfOnuIDUniID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530638 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(IntfOnuIDUniID, flow, false)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400639 if err != nil {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530640 log.Error("Failed to Update flow id infor for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400641 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530642 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(IntfOnuIDUniID, flow)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400643 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530644}
645
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700646// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id, and the clears the
647// resource map and the onuID associated with (pon_intf_id, gemport_id) tuple,
648func (RsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(intfID uint32, onuID uint32, uniID uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530649
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700650 var onuIDs []uint32
651 onuIDs = append(onuIDs, onuID)
652 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530653
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700654 AllocIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530655
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700656 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400657 ponrmgr.ALLOC_ID,
658 AllocIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530659
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700660 GEMPortIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(IntfOnuIDUniID)
661 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400662 ponrmgr.GEMPORT_ID,
663 GEMPortIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530664
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700665 FlowIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentFlowIDsForOnu(IntfOnuIDUniID)
666 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400667 ponrmgr.FLOW_ID,
668 FlowIDs)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700669 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400670 ponrmgr.ONU_ID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700671 onuIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530672
Girish Gowdru0c588b22019-04-23 23:24:56 -0400673 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700674 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530675
Girish Gowdru0c588b22019-04-23 23:24:56 -0400676 // Clear the ONU Id associated with the (pon_intf_id, gemport_id) tuple.
677 for _, GEM := range GEMPortIDs {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700678 _ = RsrcMgr.KVStore.Delete(fmt.Sprintf("%d,%d", intfID, GEM))
Girish Gowdru0c588b22019-04-23 23:24:56 -0400679 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530680}
681
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700682// IsFlowCookieOnKVStore checks if the given flow cookie is present on the kv store
683// Returns true if the flow cookie is found, otherwise it returns false
684func (RsrcMgr *OpenOltResourceMgr) IsFlowCookieOnKVStore(ponIntfID uint32, onuID uint32, uniID uint32,
685 flowStoreCookie uint64) bool {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530686
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700687 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
688 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(FlowPath)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400689 if FlowIDs != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700690 log.Debugw("Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "onuID": onuID, "uniID": uniID, "KVpath": FlowPath})
691 for _, flowID := range FlowIDs {
692 FlowInfo := RsrcMgr.GetFlowIDInfo(ponIntfID, onuID, uniID, uint32(flowID))
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400693 if FlowInfo != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700694 log.Debugw("Found flows", log.Fields{"flows": *FlowInfo, "flowId": flowID})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400695 for _, Info := range *FlowInfo {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700696 if Info.FlowStoreCookie == flowStoreCookie {
697 log.Debug("Found flow matching with flowStore cookie", log.Fields{"flowId": flowID, "flowStoreCookie": flowStoreCookie})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400698 return true
699 }
700 }
701 }
702 }
703 }
704 return false
705}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400706
707func (RMgr *OpenOltResourceMgr) GetTechProfileIdForOnu(IntfId uint32, OnuId uint32, UniId uint32) uint32 {
708 Path := fmt.Sprintf(TP_ID_PATH_SUFFIX, IntfId, OnuId, UniId)
709 var Data uint32
710 Value, err := RMgr.KVStore.Get(Path)
711 if err == nil {
712 if Value != nil {
713 Val, err := kvstore.ToByte(Value.Value)
714 if err != nil {
715 log.Errorw("Failed to convert into byte array", log.Fields{"error": err})
716 return Data
717 }
718 if err = json.Unmarshal(Val, &Data); err != nil {
719 log.Error("Failed to unmarshal", log.Fields{"error": err})
720 return Data
721 }
722 }
723 } else {
724 log.Errorf("Failed to get TP id from kvstore for path %s", Path)
725 }
726 log.Debugf("Getting TP id %d from path %s", Data, Path)
727 return Data
728
729}
730
731func (RMgr *OpenOltResourceMgr) RemoveTechProfileIdForOnu(IntfId uint32, OnuId uint32, UniId uint32) error {
732 IntfOnuUniId := fmt.Sprintf(TP_ID_PATH_SUFFIX, IntfId, OnuId, UniId)
733 if err := RMgr.KVStore.Delete(IntfOnuUniId); err != nil {
734 log.Error("Failed to delete techprofile id resource %s in KV store", IntfOnuUniId)
735 return err
736 }
737 return nil
738}
739
740func (RMgr *OpenOltResourceMgr) UpdateTechProfileIdForOnu(IntfId uint32, OnuId uint32,
741 UniId uint32, TpId uint32) error {
742 var Value []byte
743 var err error
744
745 IntfOnuUniId := fmt.Sprintf(TP_ID_PATH_SUFFIX, IntfId, OnuId, UniId)
746 log.Debugf("updating tp id %d on path %s", TpId, IntfOnuUniId)
747 Value, err = json.Marshal(TpId)
748 if err != nil {
749 log.Error("failed to Marshal")
750 return err
751 }
752 if err = RMgr.KVStore.Put(IntfOnuUniId, Value); err != nil {
753 log.Errorf("Failed to update resource %s", IntfOnuUniId)
754 return err
755 }
756 return err
757}
758
759func (RMgr *OpenOltResourceMgr) UpdateMeterIdForOnu(Direction string, IntfId uint32, OnuId uint32,
760 UniId uint32, MeterConfig *ofp.OfpMeterConfig) error {
761 var Value []byte
762 var err error
763
764 IntfOnuUniId := fmt.Sprintf(METER_ID_PATH_SUFFIX, IntfId, OnuId, UniId, Direction)
765 Value, err = json.Marshal(*MeterConfig)
766 if err != nil {
767 log.Error("failed to Marshal meter config")
768 return err
769 }
770 if err = RMgr.KVStore.Put(IntfOnuUniId, Value); err != nil {
771 log.Errorf("Failed to store meter into KV store %s", IntfOnuUniId)
772 return err
773 }
774 return err
775}
776
777func (RMgr *OpenOltResourceMgr) GetMeterIdForOnu(Direction string, IntfId uint32, OnuId uint32, UniId uint32) (*ofp.OfpMeterConfig, error) {
778 Path := fmt.Sprintf(METER_ID_PATH_SUFFIX, IntfId, OnuId, UniId, Direction)
779 var meterConfig ofp.OfpMeterConfig
780 Value, err := RMgr.KVStore.Get(Path)
781 if err == nil {
782 if Value != nil {
783 log.Debug("Found meter in KV store", log.Fields{"Direction": Direction})
784 Val, err := kvstore.ToByte(Value.Value)
785 if err != nil {
786 log.Errorw("Failed to convert into byte array", log.Fields{"error": err})
787 return nil, err
788 }
789 if err = json.Unmarshal(Val, &meterConfig); err != nil {
790 log.Error("Failed to unmarshal meterconfig", log.Fields{"error": err})
791 return nil, err
792 }
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
804func (RMgr *OpenOltResourceMgr) RemoveMeterIdForOnu(Direction string, IntfId uint32, OnuId uint32, UniId uint32) error {
805 Path := fmt.Sprintf(METER_ID_PATH_SUFFIX, IntfId, OnuId, UniId, Direction)
806 if err := RMgr.KVStore.Delete(Path); err != nil {
807 log.Errorf("Failed to delete meter id %s from kvstore ", Path)
808 return err
809 }
810 return nil
811}