blob: b43aa2b03e823be41a95b79a6b5a2839f1961d0a [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
Scott Baker51290152019-10-24 14:23:20 -070027 "github.com/opencord/voltha-lib-go/v2/pkg/db/kvstore"
28 "github.com/opencord/voltha-lib-go/v2/pkg/db/model"
29 "github.com/opencord/voltha-lib-go/v2/pkg/log"
30 ponrmgr "github.com/opencord/voltha-lib-go/v2/pkg/ponresourcemanager"
Manikkaraj kb1d51442019-07-23 10:41:02 -040031 ofp "github.com/opencord/voltha-protos/go/openflow_13"
Girish Gowdru0c588b22019-04-23 23:24:56 -040032 "github.com/opencord/voltha-protos/go/openolt"
Abhilash S.L7f17e402019-03-15 17:40:41 +053033)
34
salmansiddiqui7ac62132019-08-22 03:58:50 +000035const (
36 // KvstoreTimeout specifies the time out for KV Store Connection
37 KvstoreTimeout = 5
38 // BasePathKvStore - service/voltha/openolt/<device_id>
39 BasePathKvStore = "service/voltha/openolt/{%s}"
Gamze Abakafee36392019-10-03 11:17:24 +000040 // TpIDPathSuffix - <(pon_id, onu_id, uni_id)>/tp_id
41 TpIDPathSuffix = "{%d,%d,%d}/tp_id"
42 //MeterIDPathSuffix - <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
43 MeterIDPathSuffix = "{%d,%d,%d}/{%d}/meter_id/{%s}"
salmansiddiqui7ac62132019-08-22 03:58:50 +000044)
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
Gamze Abakafee36392019-10-03 11:17:24 +000099// NewResourceMgr init a New resource manager instance which in turn instantiates pon resource manager
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700100// 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
Gamze Abakafee36392019-10-03 11:17:24 +0000126 the legacy global per-device information. This, in theory, is temporary until
Girish Gowdru0c588b22019-04-23 23:24:56 -0400127 the legacy drivers are upgrade to support pool ranges.
128 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700129 if devInfo.Ranges == nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400130 var ranges openolt.DeviceInfo_DeviceResourceRanges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700131 ranges.Technology = devInfo.GetTechnology()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530132
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700133 NumPONPorts := devInfo.GetPonPorts()
Girish Gowdru0c588b22019-04-23 23:24:56 -0400134 var index uint32
135 for index = 0; index < NumPONPorts; index++ {
136 ranges.IntfIds = append(ranges.IntfIds, index)
137 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530138
Abhilash S.L8ee90712019-04-29 16:24:22 +0530139 var Pool openolt.DeviceInfo_DeviceResourceRanges_Pool
Girish Gowdru0c588b22019-04-23 23:24:56 -0400140 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700141 Pool.Start = devInfo.OnuIdStart
142 Pool.End = devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400143 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
cbabuabf02352019-10-15 13:14:56 +0200144 onuPool := Pool
145 ranges.Pools = append(ranges.Pools, &onuPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530146
Girish Gowdru0c588b22019-04-23 23:24:56 -0400147 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700148 Pool.Start = devInfo.AllocIdStart
149 Pool.End = devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400150 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200151 allocPool := Pool
152 ranges.Pools = append(ranges.Pools, &allocPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530153
Girish Gowdru0c588b22019-04-23 23:24:56 -0400154 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700155 Pool.Start = devInfo.GemportIdStart
156 Pool.End = devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400157 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200158 gemPool := Pool
159 ranges.Pools = append(ranges.Pools, &gemPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530160
Girish Gowdru0c588b22019-04-23 23:24:56 -0400161 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700162 Pool.Start = devInfo.FlowIdStart
163 Pool.End = devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400164 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
Abhilash S.L8ee90712019-04-29 16:24:22 +0530165 ranges.Pools = append(ranges.Pools, &Pool)
166 // Add to device info
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700167 devInfo.Ranges = append(devInfo.Ranges, &ranges)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400168 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530169
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700170 // Create a separate Resource Manager instance for each range. This assumes that
Girish Gowdru0c588b22019-04-23 23:24:56 -0400171 // each technology is represented by only a single range
172 var GlobalPONRsrcMgr *ponrmgr.PONResourceManager
173 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700174 for _, TechRange := range devInfo.Ranges {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400175 technology := TechRange.Technology
176 log.Debugf("Device info technology %s", technology)
177 Ranges[technology] = TechRange
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700178 RsrcMgrsByTech[technology], err = ponrmgr.NewPONResourceManager(technology, deviceType, deviceID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400179 Backend, ResourceMgr.Host, ResourceMgr.Port)
180 if err != nil {
Gamze Abakafee36392019-10-03 11:17:24 +0000181 log.Errorf("Failed to create pon resource manager instance for technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400182 return nil
183 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700184 // resource_mgrs_by_tech[technology] = resource_mgr
Girish Gowdru0c588b22019-04-23 23:24:56 -0400185 if GlobalPONRsrcMgr == nil {
186 GlobalPONRsrcMgr = RsrcMgrsByTech[technology]
187 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700188 for _, IntfID := range TechRange.IntfIds {
189 ResourceMgr.ResourceMgrs[uint32(IntfID)] = RsrcMgrsByTech[technology]
Girish Gowdru0c588b22019-04-23 23:24:56 -0400190 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700191 // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400192 InitializeDeviceResourceRangeAndPool(RsrcMgrsByTech[technology], GlobalPONRsrcMgr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700193 TechRange, devInfo)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400194 }
195 // After we have initialized resource ranges, initialize the
196 // resource pools accordingly.
197 for _, PONRMgr := range RsrcMgrsByTech {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700198 _ = PONRMgr.InitDeviceResourcePool()
Girish Gowdru0c588b22019-04-23 23:24:56 -0400199 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530200 log.Info("Initialization of resource manager success!")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400201 return &ResourceMgr
Abhilash S.L7f17e402019-03-15 17:40:41 +0530202}
203
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700204// InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply
205// device specific information. If KV doesn't exist
206// or is broader than the device, the device's information will
207// dictate the range limits
208func InitializeDeviceResourceRangeAndPool(ponRMgr *ponrmgr.PONResourceManager, globalPONRMgr *ponrmgr.PONResourceManager,
209 techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530210
Girish Gowdru0c588b22019-04-23 23:24:56 -0400211 // init the resource range pool according to the sharing type
Abhilash S.L7f17e402019-03-15 17:40:41 +0530212
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700213 log.Debugf("Resource range pool init for technology %s", ponRMgr.Technology)
214 // first load from KV profiles
215 status := ponRMgr.InitResourceRangesFromKVStore()
216 if !status {
217 log.Debugf("Failed to load resource ranges from KV store for tech %s", ponRMgr.Technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400218 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530219
Girish Gowdru0c588b22019-04-23 23:24:56 -0400220 /*
221 Then apply device specific information. If KV doesn't exist
Gamze Abakafee36392019-10-03 11:17:24 +0000222 or is broader than the device, the device's information will
Girish Gowdru0c588b22019-04-23 23:24:56 -0400223 dictate the range limits
224 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700225 log.Debugf("Using device info to init pon resource ranges for tech", ponRMgr.Technology)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530226
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700227 ONUIDStart := devInfo.OnuIdStart
228 ONUIDEnd := devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400229 ONUIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
230 ONUIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700231 AllocIDStart := devInfo.AllocIdStart
232 AllocIDEnd := devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400233 AllocIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
234 AllocIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700235 GEMPortIDStart := devInfo.GemportIdStart
236 GEMPortIDEnd := devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400237 GEMPortIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
238 GEMPortIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700239 FlowIDStart := devInfo.FlowIdStart
240 FlowIDEnd := devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400241 FlowIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
242 FlowIDSharedPoolID := uint32(0)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530243
Girish Gowdru0c588b22019-04-23 23:24:56 -0400244 var FirstIntfPoolID uint32
245 var SharedPoolID uint32
Abhilash S.L7f17e402019-03-15 17:40:41 +0530246
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400247 /*
248 * As a zero check is made against SharedPoolID to check whether the resources are shared across all intfs
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700249 * if resources are shared across interfaces then SharedPoolID is given a positive number.
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400250 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700251 for _, FirstIntfPoolID = range techRange.IntfIds {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400252 // skip the intf id 0
253 if FirstIntfPoolID == 0 {
254 continue
255 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400256 break
257 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530258
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700259 for _, RangePool := range techRange.Pools {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400260 if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400261 SharedPoolID = FirstIntfPoolID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400262 } else if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_SAME_TECH {
263 SharedPoolID = FirstIntfPoolID
264 } else {
265 SharedPoolID = 0
266 }
267 if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID {
268 ONUIDStart = RangePool.Start
269 ONUIDEnd = RangePool.End
270 ONUIDShared = RangePool.Sharing
271 ONUIDSharedPoolID = SharedPoolID
272 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID {
273 AllocIDStart = RangePool.Start
274 AllocIDEnd = RangePool.End
275 AllocIDShared = RangePool.Sharing
276 AllocIDSharedPoolID = SharedPoolID
277 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID {
278 GEMPortIDStart = RangePool.Start
279 GEMPortIDEnd = RangePool.End
280 GEMPortIDShared = RangePool.Sharing
281 GEMPortIDSharedPoolID = SharedPoolID
282 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID {
283 FlowIDStart = RangePool.Start
284 FlowIDEnd = RangePool.End
285 FlowIDShared = RangePool.Sharing
286 FlowIDSharedPoolID = SharedPoolID
287 }
288 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530289
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700290 log.Debugw("Device info init", log.Fields{"technology": techRange.Technology,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400291 "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd, "onu_id_shared_pool_id": ONUIDSharedPoolID,
292 "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd,
293 "alloc_id_shared_pool_id": AllocIDSharedPoolID,
294 "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd,
295 "gemport_id_shared_pool_id": GEMPortIDSharedPoolID,
296 "flow_id_start": FlowIDStart,
297 "flow_id_end_idx": FlowIDEnd,
298 "flow_id_shared_pool_id": FlowIDSharedPoolID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700299 "intf_ids": techRange.IntfIds,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400300 "uni_id_start": 0,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700301 "uni_id_end_idx": 1, /*MaxUNIIDperONU()*/
302 })
Abhilash S.L7f17e402019-03-15 17:40:41 +0530303
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700304 ponRMgr.InitDefaultPONResourceRanges(ONUIDStart, ONUIDEnd, ONUIDSharedPoolID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400305 AllocIDStart, AllocIDEnd, AllocIDSharedPoolID,
306 GEMPortIDStart, GEMPortIDEnd, GEMPortIDSharedPoolID,
307 FlowIDStart, FlowIDEnd, FlowIDSharedPoolID, 0, 1,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700308 devInfo.PonPorts, techRange.IntfIds)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530309
Girish Gowdru0c588b22019-04-23 23:24:56 -0400310 // For global sharing, make sure to refresh both local and global resource manager instances' range
Abhilash S.L7f17e402019-03-15 17:40:41 +0530311
Girish Gowdru0c588b22019-04-23 23:24:56 -0400312 if ONUIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700313 globalPONRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400314 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700315 ponRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
316 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400317 }
318 if AllocIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700319 globalPONRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400320 "", 0, nil)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530321
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700322 ponRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
323 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400324 }
325 if GEMPortIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700326 globalPONRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400327 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700328 ponRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
329 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400330 }
331 if FlowIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700332 globalPONRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400333 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700334 ponRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
335 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400336 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530337
Girish Gowdru0c588b22019-04-23 23:24:56 -0400338 // Make sure loaded range fits the platform bit encoding ranges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700339 ponRMgr.UpdateRanges(ponrmgr.UNI_ID_START_IDX, 0, ponrmgr.UNI_ID_END_IDX /* TODO =OpenOltPlatform.MAX_UNIS_PER_ONU-1*/, 1, "", 0, nil)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530340}
341
Devmalya Paul495b94a2019-08-27 19:42:00 -0400342// Delete clears used resources for the particular olt device being deleted
343func (RsrcMgr *OpenOltResourceMgr) Delete() error {
344 /* TODO
345 def __del__(self):
346 self.log.info("clearing-device-resource-pool")
347 for key, resource_mgr in self.resource_mgrs.iteritems():
348 resource_mgr.clear_device_resource_pool()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530349
Devmalya Paul495b94a2019-08-27 19:42:00 -0400350 def assert_pon_id_limit(self, pon_intf_id):
351 assert pon_intf_id in self.resource_mgrs
Abhilash S.L7f17e402019-03-15 17:40:41 +0530352
Devmalya Paul495b94a2019-08-27 19:42:00 -0400353 def assert_onu_id_limit(self, pon_intf_id, onu_id):
354 self.assert_pon_id_limit(pon_intf_id)
355 self.resource_mgrs[pon_intf_id].assert_resource_limits(onu_id, PONResourceManager.ONU_ID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530356
Devmalya Paul495b94a2019-08-27 19:42:00 -0400357 @property
358 def max_uni_id_per_onu(self):
359 return 0 #OpenOltPlatform.MAX_UNIS_PER_ONU-1, zero-based indexing Uncomment or override to make default multi-uni
Abhilash S.L7f17e402019-03-15 17:40:41 +0530360
Devmalya Paul495b94a2019-08-27 19:42:00 -0400361 def assert_uni_id_limit(self, pon_intf_id, onu_id, uni_id):
362 self.assert_onu_id_limit(pon_intf_id, onu_id)
363 self.resource_mgrs[pon_intf_id].assert_resource_limits(uni_id, PONResourceManager.UNI_ID)
364 */
365 for _, rsrcMgr := range RsrcMgr.ResourceMgrs {
366 if err := rsrcMgr.ClearDeviceResourcePool(); err != nil {
367 log.Debug("Failed to clear device resource pool")
368 return err
369 }
370 }
371 log.Debug("Cleared device resource pool")
372 return nil
373}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530374
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700375// GetONUID returns the available OnuID for the given pon-port
376func (RsrcMgr *OpenOltResourceMgr) GetONUID(ponIntfID uint32) (uint32, error) {
salmansiddiqui352a45c2019-08-19 10:15:36 +0000377 // Check if Pon Interface ID is present in Resource-manager-map
378 if _, ok := RsrcMgr.ResourceMgrs[ponIntfID]; !ok {
379 err := errors.New("invalid-pon-interface-" + strconv.Itoa(int(ponIntfID)))
380 return 0, err
381 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400382 // Get ONU id for a provided pon interface ID.
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700383 ONUID, err := RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400384 ponrmgr.ONU_ID, 1)
385 if err != nil {
386 log.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700387 ponIntfID, ponrmgr.ONU_ID)
cbabuabf02352019-10-15 13:14:56 +0200388 return 0, err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400389 }
390 if ONUID != nil {
Devmalya Paul495b94a2019-08-27 19:42:00 -0400391 RsrcMgr.ResourceMgrs[ponIntfID].InitResourceMap(fmt.Sprintf("%d,%d", ponIntfID, ONUID[0]))
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700392 return ONUID[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400393 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530394
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700395 return 0, err // return OnuID 0 on error
Abhilash S.L7f17e402019-03-15 17:40:41 +0530396}
397
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700398// GetFlowIDInfo returns the slice of flow info of the given pon-port
399// Note: For flows which trap from the NNI and not really associated with any particular
400// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
401func (RsrcMgr *OpenOltResourceMgr) GetFlowIDInfo(ponIntfID uint32, onuID uint32, uniID uint32, flowID uint32) *[]FlowInfo {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530402 var flows []FlowInfo
403
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700404 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
405 if err := RsrcMgr.ResourceMgrs[ponIntfID].GetFlowIDInfo(FlowPath, flowID, &flows); err != nil {
406 log.Errorw("Error while getting flows from KV store", log.Fields{"flowId": flowID})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530407 return nil
408 }
409 if len(flows) == 0 {
410 log.Debugw("No flowInfo found in KV store", log.Fields{"flowPath": FlowPath})
411 return nil
412 }
413 return &flows
414}
415
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700416// GetCurrentFlowIDsForOnu fetches flow ID from the resource manager
417// Note: For flows which trap from the NNI and not really associated with any particular
418// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
Abhilash S.L8ee90712019-04-29 16:24:22 +0530419func (RsrcMgr *OpenOltResourceMgr) GetCurrentFlowIDsForOnu(PONIntfID uint32, ONUID uint32, UNIID uint32) []uint32 {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700420
Abhilash S.L8ee90712019-04-29 16:24:22 +0530421 FlowPath := fmt.Sprintf("%d,%d,%d", PONIntfID, ONUID, UNIID)
422 return RsrcMgr.ResourceMgrs[PONIntfID].GetCurrentFlowIDsForOnu(FlowPath)
423}
424
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700425// UpdateFlowIDInfo updates flow info for the given pon interface, onu id, and uni id
426// Note: For flows which trap from the NNI and not really associated with any particular
427// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
428func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDInfo(ponIntfID int32, onuID int32, uniID int32,
429 flowID uint32, flowData *[]FlowInfo) error {
430 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
431 return RsrcMgr.ResourceMgrs[uint32(ponIntfID)].UpdateFlowIDInfoForOnu(FlowPath, flowID, *flowData)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530432}
433
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700434// GetFlowID return flow ID for a given pon interface id, onu id and uni id
435func (RsrcMgr *OpenOltResourceMgr) GetFlowID(ponIntfID uint32, ONUID uint32, uniID uint32,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400436 gemportID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700437 flowStoreCookie uint64,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400438 flowCategory string, vlanPcp ...uint32) (uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530439
Girish Gowdru0c588b22019-04-23 23:24:56 -0400440 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700441 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, ONUID, uniID)
442 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(FlowPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400443 if FlowIDs != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700444 log.Debugw("Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "ONUID": ONUID, "uniID": uniID, "KVpath": FlowPath})
445 for _, flowID := range FlowIDs {
446 FlowInfo := RsrcMgr.GetFlowIDInfo(ponIntfID, ONUID, uniID, uint32(flowID))
salmansiddiqui7ac62132019-08-22 03:58:50 +0000447 er := getFlowIDFromFlowInfo(FlowInfo, flowID, gemportID, flowStoreCookie, flowCategory, vlanPcp...)
448 if er == nil {
449 return flowID, er
Abhilash S.L8ee90712019-04-29 16:24:22 +0530450 }
451 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400452 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530453 log.Debug("No matching flows with flow cookie or flow category, allocating new flowid")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700454 FlowIDs, err = RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400455 ponrmgr.FLOW_ID, 1)
456 if err != nil {
457 log.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700458 ponIntfID, ponrmgr.FLOW_ID)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400459 return uint32(0), err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400460 }
461 if FlowIDs != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700462 _ = RsrcMgr.ResourceMgrs[ponIntfID].UpdateFlowIDForOnu(FlowPath, FlowIDs[0], true)
463 return FlowIDs[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400464 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530465
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700466 return 0, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530467}
468
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700469// GetAllocID return the first Alloc ID for a given pon interface id and onu id and then update the resource map on
470// the KV store with the list of alloc_ids allocated for the pon_intf_onu_id tuple
471// Currently of all the alloc_ids available, it returns the first alloc_id in the list for tha given ONU
472func (RsrcMgr *OpenOltResourceMgr) GetAllocID(intfID uint32, onuID uint32, uniID uint32) uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530473
Girish Gowdru0c588b22019-04-23 23:24:56 -0400474 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700475 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
476 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400477 if AllocID != nil {
478 // Since we support only one alloc_id for the ONU at the moment,
479 // return the first alloc_id in the list, if available, for that
480 // ONU.
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700481 log.Debugw("Retrieved alloc ID from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400482 return AllocID[0]
483 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700484 AllocID, err = RsrcMgr.ResourceMgrs[intfID].GetResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400485 ponrmgr.ALLOC_ID, 1)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530486
Girish Gowdru0c588b22019-04-23 23:24:56 -0400487 if AllocID == nil || err != nil {
488 log.Error("Failed to allocate alloc id")
489 return 0
490 }
491 // update the resource map on KV store with the list of alloc_id
492 // allocated for the pon_intf_onu_id tuple
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700493 err = RsrcMgr.ResourceMgrs[intfID].UpdateAllocIdsForOnu(IntfOnuIDUniID, AllocID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400494 if err != nil {
495 log.Error("Failed to update Alloc ID")
496 return 0
497 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530498 log.Debugw("Allocated new Tcont from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400499 return AllocID[0]
Abhilash S.L7f17e402019-03-15 17:40:41 +0530500}
501
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700502// UpdateAllocIdsForOnu updates alloc ids in kv store for a given pon interface id, onu id and uni id
503func (RsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ponPort uint32, onuID uint32, uniID uint32, allocID []uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530504
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700505 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
506 return RsrcMgr.ResourceMgrs[ponPort].UpdateAllocIdsForOnu(IntfOnuIDUniID,
507 allocID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530508}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700509
510// GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id
511func (RsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(intfID uint32, onuID uint32,
512 uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530513
Girish Gowdru0c588b22019-04-23 23:24:56 -0400514 /* Get gem ports for given pon interface , onu id and uni id. */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530515
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700516 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
517 return RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530518}
519
Gamze Abakafee36392019-10-03 11:17:24 +0000520// GetCurrentAllocIDsForOnu returns alloc ids for given pon interface and onu id
521func (RsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDsForOnu(intfID uint32, onuID uint32, uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530522
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700523 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
524 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400525 if AllocID != nil {
Gamze Abakafee36392019-10-03 11:17:24 +0000526 return AllocID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400527 }
Gamze Abakafee36392019-10-03 11:17:24 +0000528 return []uint32{}
529}
530
531// RemoveAllocIDForOnu removes the alloc id for given pon interface, onu id, uni id and alloc id
532func (RsrcMgr *OpenOltResourceMgr) RemoveAllocIDForOnu(intfID uint32, onuID uint32, uniID uint32, allocID uint32) {
533 allocIDs := RsrcMgr.GetCurrentAllocIDsForOnu(intfID, onuID, uniID)
534 for i := 0; i < len(allocIDs); i++ {
535 if allocIDs[i] == allocID {
536 allocIDs = append(allocIDs[:i], allocIDs[i+1:]...)
537 break
538 }
539 }
540 err := RsrcMgr.UpdateAllocIdsForOnu(intfID, onuID, uniID, allocIDs)
541 if err != nil {
542 log.Errorf("Failed to Remove Alloc Id For Onu. IntfID %d onuID %d uniID %d allocID %d",
543 intfID, onuID, uniID, allocID)
544 }
545}
546
547// RemoveGemPortIDForOnu removes the gem port id for given pon interface, onu id, uni id and gem port id
548func (RsrcMgr *OpenOltResourceMgr) RemoveGemPortIDForOnu(intfID uint32, onuID uint32, uniID uint32, gemPortID uint32) {
549 gemPortIDs := RsrcMgr.GetCurrentGEMPortIDsForOnu(intfID, onuID, uniID)
550 for i := 0; i < len(gemPortIDs); i++ {
551 if gemPortIDs[i] == gemPortID {
552 gemPortIDs = append(gemPortIDs[:i], gemPortIDs[i+1:]...)
553 break
554 }
555 }
556 err := RsrcMgr.UpdateGEMPortIDsForOnu(intfID, onuID, uniID, gemPortIDs)
557 if err != nil {
558 log.Errorf("Failed to Remove Gem Id For Onu. IntfID %d onuID %d uniID %d gemPortId %d",
559 intfID, onuID, uniID, gemPortID)
560 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530561}
562
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700563// UpdateGEMportsPonportToOnuMapOnKVStore updates onu and uni id associated with the gem port to the kv store
564// This stored information is used when packet_indication is received and we need to derive the ONU Id for which
565// the packet arrived based on the pon_intf and gemport available in the packet_indication
566func (RsrcMgr *OpenOltResourceMgr) UpdateGEMportsPonportToOnuMapOnKVStore(gemPorts []uint32, PonPort uint32,
567 onuID uint32, uniID uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530568
Girish Gowdru0c588b22019-04-23 23:24:56 -0400569 /* Update onu and uni id associated with the gem port to the kv store. */
570 var IntfGEMPortPath string
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700571 Data := fmt.Sprintf("%d %d", onuID, uniID)
572 for _, GEM := range gemPorts {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400573 IntfGEMPortPath = fmt.Sprintf("%d,%d", PonPort, GEM)
574 Val, err := json.Marshal(Data)
575 if err != nil {
576 log.Error("failed to Marshal")
577 return err
578 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700579
Girish Gowdru0c588b22019-04-23 23:24:56 -0400580 if err = RsrcMgr.KVStore.Put(IntfGEMPortPath, Val); err != nil {
581 log.Errorf("Failed to update resource %s", IntfGEMPortPath)
582 return err
583 }
584 }
585 return nil
Abhilash S.L7f17e402019-03-15 17:40:41 +0530586}
587
Gamze Abakafee36392019-10-03 11:17:24 +0000588// RemoveGEMportPonportToOnuMapOnKVStore removes the relationship between the gem port and pon port
589func (RsrcMgr *OpenOltResourceMgr) RemoveGEMportPonportToOnuMapOnKVStore(GemPort uint32, PonPort uint32) {
590 IntfGEMPortPath := fmt.Sprintf("%d,%d", PonPort, GemPort)
591 err := RsrcMgr.KVStore.Delete(IntfGEMPortPath)
592 if err != nil {
593 log.Errorf("Failed to Remove Gem port-Pon port to onu map on kv store. Gem %d PonPort %d", GemPort, PonPort)
594 }
595}
596
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700597// GetGEMPortID gets gem port id for a particular pon port, onu id and uni id and then update the resource map on
598// the KV store with the list of gemport_id allocated for the pon_intf_onu_id tuple
599func (RsrcMgr *OpenOltResourceMgr) GetGEMPortID(ponPort uint32, onuID uint32,
600 uniID uint32, NumOfPorts uint32) ([]uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530601
Girish Gowdru0c588b22019-04-23 23:24:56 -0400602 /* Get gem port id for a particular pon port, onu id
603 and uni id.
604 */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530605
Girish Gowdru0c588b22019-04-23 23:24:56 -0400606 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700607 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530608
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700609 GEMPortList := RsrcMgr.ResourceMgrs[ponPort].GetCurrentGEMPortIDsForOnu(IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400610 if GEMPortList != nil {
611 return GEMPortList, nil
612 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530613
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700614 GEMPortList, err = RsrcMgr.ResourceMgrs[ponPort].GetResourceID(ponPort,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400615 ponrmgr.GEMPORT_ID, NumOfPorts)
616 if err != nil && GEMPortList == nil {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530617 log.Errorf("Failed to get gem port id for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400618 return nil, err
619 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530620
Girish Gowdru0c588b22019-04-23 23:24:56 -0400621 // update the resource map on KV store with the list of gemport_id
622 // allocated for the pon_intf_onu_id tuple
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700623 err = RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400624 GEMPortList)
625 if err != nil {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530626 log.Errorf("Failed to update GEM ports to kv store for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400627 return nil, err
628 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700629 _ = RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(GEMPortList, ponPort,
630 onuID, uniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400631 return GEMPortList, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530632}
633
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700634// UpdateGEMPortIDsForOnu updates gemport ids on to the kv store for a given pon port, onu id and uni id
635func (RsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ponPort uint32, onuID uint32,
636 uniID uint32, GEMPortList []uint32) error {
637 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
638 return RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400639 GEMPortList)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530640
641}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700642
643// FreeonuID releases(make free) onu id for a particular pon-port
644func (RsrcMgr *OpenOltResourceMgr) FreeonuID(intfID uint32, onuID []uint32) {
645
646 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID, ponrmgr.ONU_ID, onuID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530647
Girish Gowdru0c588b22019-04-23 23:24:56 -0400648 /* Free onu id for a particular interface.*/
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700649 var IntfonuID string
650 for _, onu := range onuID {
651 IntfonuID = fmt.Sprintf("%d,%d", intfID, onu)
652 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(IntfonuID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400653 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530654}
655
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700656// FreeFlowID returns the free flow id for a given interface, onu id and uni id
Devmalya Paul495b94a2019-08-27 19:42:00 -0400657func (RsrcMgr *OpenOltResourceMgr) FreeFlowID(IntfID uint32, onuID int32,
658 uniID int32, FlowID uint32) {
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400659 var IntfONUID string
660 var err error
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530661 FlowIds := make([]uint32, 0)
662
663 FlowIds = append(FlowIds, FlowID)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700664 IntfONUID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
665 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(IntfONUID, FlowID, false)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400666 if err != nil {
667 log.Error("Failed to Update flow id infor for %s", IntfONUID)
668 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700669 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(IntfONUID, FlowID)
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530670 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(IntfID, ponrmgr.FLOW_ID, FlowIds)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400671}
672
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700673// FreeFlowIDs releases the flow Ids
674func (RsrcMgr *OpenOltResourceMgr) FreeFlowIDs(IntfID uint32, onuID uint32,
675 uniID uint32, FlowID []uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530676
Girish Gowdru0c588b22019-04-23 23:24:56 -0400677 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(IntfID, ponrmgr.FLOW_ID, FlowID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530678
Abhilash S.L8ee90712019-04-29 16:24:22 +0530679 var IntfOnuIDUniID string
Girish Gowdru0c588b22019-04-23 23:24:56 -0400680 var err error
681 for _, flow := range FlowID {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700682 IntfOnuIDUniID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530683 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(IntfOnuIDUniID, flow, false)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400684 if err != nil {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530685 log.Error("Failed to Update flow id infor for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400686 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530687 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(IntfOnuIDUniID, flow)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400688 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530689}
690
Gamze Abakafee36392019-10-03 11:17:24 +0000691// FreeAllocID frees AllocID on the PON resource pool and also frees the allocID association
692// for the given OLT device.
693func (RsrcMgr *OpenOltResourceMgr) FreeAllocID(IntfID uint32, onuID uint32,
694 uniID uint32, allocID uint32) {
695 RsrcMgr.RemoveAllocIDForOnu(IntfID, onuID, uniID, allocID)
696 allocIDs := make([]uint32, 0)
697 allocIDs = append(allocIDs, allocID)
698 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(IntfID, ponrmgr.ALLOC_ID, allocIDs)
699}
700
701// FreeGemPortID frees GemPortID on the PON resource pool and also frees the gemPortID association
702// for the given OLT device.
703func (RsrcMgr *OpenOltResourceMgr) FreeGemPortID(IntfID uint32, onuID uint32,
704 uniID uint32, gemPortID uint32) {
705 RsrcMgr.RemoveGemPortIDForOnu(IntfID, onuID, uniID, gemPortID)
706 gemPortIDs := make([]uint32, 0)
707 gemPortIDs = append(gemPortIDs, gemPortID)
708 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(IntfID, ponrmgr.GEMPORT_ID, gemPortIDs)
709}
710
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700711// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id, and the clears the
712// resource map and the onuID associated with (pon_intf_id, gemport_id) tuple,
713func (RsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(intfID uint32, onuID uint32, uniID uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530714
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700715 var onuIDs []uint32
716 onuIDs = append(onuIDs, onuID)
717 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530718
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700719 AllocIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530720
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700721 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400722 ponrmgr.ALLOC_ID,
723 AllocIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530724
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700725 GEMPortIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(IntfOnuIDUniID)
726 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400727 ponrmgr.GEMPORT_ID,
728 GEMPortIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530729
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700730 FlowIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentFlowIDsForOnu(IntfOnuIDUniID)
731 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400732 ponrmgr.FLOW_ID,
733 FlowIDs)
Devmalya Paul495b94a2019-08-27 19:42:00 -0400734 if int32(onuID) >= 0 {
735 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
736 ponrmgr.ONU_ID,
737 onuIDs)
738 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400739 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700740 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530741
Girish Gowdru0c588b22019-04-23 23:24:56 -0400742 // Clear the ONU Id associated with the (pon_intf_id, gemport_id) tuple.
743 for _, GEM := range GEMPortIDs {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700744 _ = RsrcMgr.KVStore.Delete(fmt.Sprintf("%d,%d", intfID, GEM))
Girish Gowdru0c588b22019-04-23 23:24:56 -0400745 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530746}
747
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700748// IsFlowCookieOnKVStore checks if the given flow cookie is present on the kv store
749// Returns true if the flow cookie is found, otherwise it returns false
750func (RsrcMgr *OpenOltResourceMgr) IsFlowCookieOnKVStore(ponIntfID uint32, onuID uint32, uniID uint32,
751 flowStoreCookie uint64) bool {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530752
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700753 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
754 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(FlowPath)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400755 if FlowIDs != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700756 log.Debugw("Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "onuID": onuID, "uniID": uniID, "KVpath": FlowPath})
757 for _, flowID := range FlowIDs {
758 FlowInfo := RsrcMgr.GetFlowIDInfo(ponIntfID, onuID, uniID, uint32(flowID))
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400759 if FlowInfo != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700760 log.Debugw("Found flows", log.Fields{"flows": *FlowInfo, "flowId": flowID})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400761 for _, Info := range *FlowInfo {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700762 if Info.FlowStoreCookie == flowStoreCookie {
763 log.Debug("Found flow matching with flowStore cookie", log.Fields{"flowId": flowID, "flowStoreCookie": flowStoreCookie})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400764 return true
765 }
766 }
767 }
768 }
769 }
770 return false
771}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400772
salmansiddiqui7ac62132019-08-22 03:58:50 +0000773// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000774// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
775func (RsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(IntfID uint32, OnuID uint32, UniID uint32) []uint32 {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000776 Path := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000777 var Data []uint32
salmansiddiqui7ac62132019-08-22 03:58:50 +0000778 Value, err := RsrcMgr.KVStore.Get(Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400779 if err == nil {
780 if Value != nil {
781 Val, err := kvstore.ToByte(Value.Value)
782 if err != nil {
783 log.Errorw("Failed to convert into byte array", log.Fields{"error": err})
784 return Data
785 }
786 if err = json.Unmarshal(Val, &Data); err != nil {
787 log.Error("Failed to unmarshal", log.Fields{"error": err})
788 return Data
789 }
790 }
791 } else {
792 log.Errorf("Failed to get TP id from kvstore for path %s", Path)
793 }
794 log.Debugf("Getting TP id %d from path %s", Data, Path)
795 return Data
796
797}
798
Gamze Abakafee36392019-10-03 11:17:24 +0000799// RemoveTechProfileIDsForOnu deletes all tech profile ids from the KV-Store for the given onu based on the path
800// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
801func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDsForOnu(IntfID uint32, OnuID uint32, UniID uint32) error {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000802 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
803 if err := RsrcMgr.KVStore.Delete(IntfOnuUniID); err != nil {
804 log.Error("Failed to delete techprofile id resource %s in KV store", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400805 return err
806 }
807 return nil
808}
809
Gamze Abakafee36392019-10-03 11:17:24 +0000810// RemoveTechProfileIDForOnu deletes a specific tech profile id from the KV-Store for the given onu based on the path
811// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
812func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(IntfID uint32, OnuID uint32, UniID uint32, TpID uint32) error {
813 tpIDList := RsrcMgr.GetTechProfileIDForOnu(IntfID, OnuID, UniID)
814 for i, tpIDInList := range tpIDList {
815 if tpIDInList == TpID {
816 tpIDList = append(tpIDList[:i], tpIDList[i+1:]...)
817 }
818 }
819 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
820 Value, err := json.Marshal(tpIDList)
821 if err != nil {
822 log.Error("failed to Marshal")
823 return err
824 }
825 if err = RsrcMgr.KVStore.Put(IntfOnuUniID, Value); err != nil {
826 log.Errorf("Failed to update resource %s", IntfOnuUniID)
827 return err
828 }
829 return err
830}
831
832// UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
833// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
salmansiddiqui7ac62132019-08-22 03:58:50 +0000834func (RsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(IntfID uint32, OnuID uint32,
835 UniID uint32, TpID uint32) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400836 var Value []byte
837 var err error
838
salmansiddiqui7ac62132019-08-22 03:58:50 +0000839 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000840
841 tpIDList := RsrcMgr.GetTechProfileIDForOnu(IntfID, OnuID, UniID)
842 for _, value := range tpIDList {
843 if value == TpID {
844 log.Debugf("TpID %d is already in tpIdList for the path %s", TpID, IntfOnuUniID)
845 return err
846 }
847 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000848 log.Debugf("updating tp id %d on path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000849 tpIDList = append(tpIDList, TpID)
850 Value, err = json.Marshal(tpIDList)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400851 if err != nil {
852 log.Error("failed to Marshal")
853 return err
854 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000855 if err = RsrcMgr.KVStore.Put(IntfOnuUniID, Value); err != nil {
856 log.Errorf("Failed to update resource %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400857 return err
858 }
859 return err
860}
861
salmansiddiqui7ac62132019-08-22 03:58:50 +0000862// UpdateMeterIDForOnu updates the meter id in the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000863// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
salmansiddiqui7ac62132019-08-22 03:58:50 +0000864func (RsrcMgr *OpenOltResourceMgr) UpdateMeterIDForOnu(Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000865 UniID uint32, TpID uint32, MeterConfig *ofp.OfpMeterConfig) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400866 var Value []byte
867 var err error
868
Gamze Abakafee36392019-10-03 11:17:24 +0000869 IntfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400870 Value, err = json.Marshal(*MeterConfig)
871 if err != nil {
872 log.Error("failed to Marshal meter config")
873 return err
874 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000875 if err = RsrcMgr.KVStore.Put(IntfOnuUniID, Value); err != nil {
876 log.Errorf("Failed to store meter into KV store %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400877 return err
878 }
879 return err
880}
881
Gamze Abakafee36392019-10-03 11:17:24 +0000882// GetMeterIDForOnu fetches the meter id from the kv store for the given onu based on the path
883// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
884func (RsrcMgr *OpenOltResourceMgr) GetMeterIDForOnu(Direction string, IntfID uint32, OnuID uint32,
885 UniID uint32, TpID uint32) (*ofp.OfpMeterConfig, error) {
886 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400887 var meterConfig ofp.OfpMeterConfig
salmansiddiqui7ac62132019-08-22 03:58:50 +0000888 Value, err := RsrcMgr.KVStore.Get(Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400889 if err == nil {
890 if Value != nil {
891 log.Debug("Found meter in KV store", log.Fields{"Direction": Direction})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000892 Val, er := kvstore.ToByte(Value.Value)
893 if er != nil {
894 log.Errorw("Failed to convert into byte array", log.Fields{"error": er})
895 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400896 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000897 if er = json.Unmarshal(Val, &meterConfig); er != nil {
898 log.Error("Failed to unmarshal meterconfig", log.Fields{"error": er})
899 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400900 }
901 } else {
902 log.Debug("meter-does-not-exists-in-KVStore")
903 return nil, err
904 }
905 } else {
906 log.Errorf("Failed to get Meter config from kvstore for path %s", Path)
907
908 }
909 return &meterConfig, err
910}
911
Gamze Abakafee36392019-10-03 11:17:24 +0000912// RemoveMeterIDForOnu deletes the meter id from the kV-Store for the given onu based on the path
913// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
914func (RsrcMgr *OpenOltResourceMgr) RemoveMeterIDForOnu(Direction string, IntfID uint32, OnuID uint32,
915 UniID uint32, TpID uint32) error {
916 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
salmansiddiqui7ac62132019-08-22 03:58:50 +0000917 if err := RsrcMgr.KVStore.Delete(Path); err != nil {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400918 log.Errorf("Failed to delete meter id %s from kvstore ", Path)
919 return err
920 }
921 return nil
922}
salmansiddiqui7ac62132019-08-22 03:58:50 +0000923
924func getFlowIDFromFlowInfo(FlowInfo *[]FlowInfo, flowID, gemportID uint32, flowStoreCookie uint64, flowCategory string, vlanPcp ...uint32) error {
925 if FlowInfo != nil {
926 for _, Info := range *FlowInfo {
927 if int32(gemportID) == Info.Flow.GemportId && flowCategory != "" && Info.FlowCategory == flowCategory {
928 log.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
929 if Info.FlowCategory == "HSIA_FLOW" && Info.Flow.Classifier.OPbits == vlanPcp[0] {
930 log.Debug("Found matching vlan pcp ", log.Fields{"flowId": flowID, "Vlanpcp": vlanPcp[0]})
931 return nil
932 }
933 }
934 if int32(gemportID) == Info.Flow.GemportId && flowStoreCookie != 0 && Info.FlowStoreCookie == flowStoreCookie {
935 if flowCategory != "" && Info.FlowCategory == flowCategory {
936 log.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
937 return nil
938 }
939 }
940 }
941 }
Gamze Abakafee36392019-10-03 11:17:24 +0000942 log.Debugw("the flow can be related to a different service", log.Fields{"flow_info": FlowInfo})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000943 return errors.New("invalid flow-info")
944}