blob: e412bb676d6317e3fcc685a53c17b1ee7e46d094 [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"
Scott Bakerc6e54cb2019-11-04 09:31:25 -080031 ofp "github.com/opencord/voltha-protos/v2/go/openflow_13"
32 "github.com/opencord/voltha-protos/v2/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)
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700422 if mgrs, exist := RsrcMgr.ResourceMgrs[PONIntfID]; exist {
423 return mgrs.GetCurrentFlowIDsForOnu(FlowPath)
424 }
425 return nil
Abhilash S.L8ee90712019-04-29 16:24:22 +0530426}
427
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700428// UpdateFlowIDInfo updates flow info for the given pon interface, onu id, and uni id
429// Note: For flows which trap from the NNI and not really associated with any particular
430// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
431func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDInfo(ponIntfID int32, onuID int32, uniID int32,
432 flowID uint32, flowData *[]FlowInfo) error {
433 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
434 return RsrcMgr.ResourceMgrs[uint32(ponIntfID)].UpdateFlowIDInfoForOnu(FlowPath, flowID, *flowData)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530435}
436
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700437// GetFlowID return flow ID for a given pon interface id, onu id and uni id
438func (RsrcMgr *OpenOltResourceMgr) GetFlowID(ponIntfID uint32, ONUID uint32, uniID uint32,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400439 gemportID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700440 flowStoreCookie uint64,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400441 flowCategory string, vlanPcp ...uint32) (uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530442
Girish Gowdru0c588b22019-04-23 23:24:56 -0400443 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700444 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, ONUID, uniID)
445 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(FlowPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400446 if FlowIDs != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700447 log.Debugw("Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "ONUID": ONUID, "uniID": uniID, "KVpath": FlowPath})
448 for _, flowID := range FlowIDs {
449 FlowInfo := RsrcMgr.GetFlowIDInfo(ponIntfID, ONUID, uniID, uint32(flowID))
salmansiddiqui7ac62132019-08-22 03:58:50 +0000450 er := getFlowIDFromFlowInfo(FlowInfo, flowID, gemportID, flowStoreCookie, flowCategory, vlanPcp...)
451 if er == nil {
452 return flowID, er
Abhilash S.L8ee90712019-04-29 16:24:22 +0530453 }
454 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400455 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530456 log.Debug("No matching flows with flow cookie or flow category, allocating new flowid")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700457 FlowIDs, err = RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400458 ponrmgr.FLOW_ID, 1)
459 if err != nil {
460 log.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700461 ponIntfID, ponrmgr.FLOW_ID)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400462 return uint32(0), err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400463 }
464 if FlowIDs != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700465 _ = RsrcMgr.ResourceMgrs[ponIntfID].UpdateFlowIDForOnu(FlowPath, FlowIDs[0], true)
466 return FlowIDs[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400467 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530468
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700469 return 0, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530470}
471
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700472// GetAllocID return the first Alloc ID for a given pon interface id and onu id and then update the resource map on
473// the KV store with the list of alloc_ids allocated for the pon_intf_onu_id tuple
474// Currently of all the alloc_ids available, it returns the first alloc_id in the list for tha given ONU
475func (RsrcMgr *OpenOltResourceMgr) GetAllocID(intfID uint32, onuID uint32, uniID uint32) uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530476
Girish Gowdru0c588b22019-04-23 23:24:56 -0400477 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700478 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
479 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400480 if AllocID != nil {
481 // Since we support only one alloc_id for the ONU at the moment,
482 // return the first alloc_id in the list, if available, for that
483 // ONU.
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700484 log.Debugw("Retrieved alloc ID from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400485 return AllocID[0]
486 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700487 AllocID, err = RsrcMgr.ResourceMgrs[intfID].GetResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400488 ponrmgr.ALLOC_ID, 1)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530489
Girish Gowdru0c588b22019-04-23 23:24:56 -0400490 if AllocID == nil || err != nil {
491 log.Error("Failed to allocate alloc id")
492 return 0
493 }
494 // update the resource map on KV store with the list of alloc_id
495 // allocated for the pon_intf_onu_id tuple
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700496 err = RsrcMgr.ResourceMgrs[intfID].UpdateAllocIdsForOnu(IntfOnuIDUniID, AllocID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400497 if err != nil {
498 log.Error("Failed to update Alloc ID")
499 return 0
500 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530501 log.Debugw("Allocated new Tcont from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400502 return AllocID[0]
Abhilash S.L7f17e402019-03-15 17:40:41 +0530503}
504
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700505// UpdateAllocIdsForOnu updates alloc ids in kv store for a given pon interface id, onu id and uni id
506func (RsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ponPort uint32, onuID uint32, uniID uint32, allocID []uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530507
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700508 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
509 return RsrcMgr.ResourceMgrs[ponPort].UpdateAllocIdsForOnu(IntfOnuIDUniID,
510 allocID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530511}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700512
513// GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id
514func (RsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(intfID uint32, onuID uint32,
515 uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530516
Girish Gowdru0c588b22019-04-23 23:24:56 -0400517 /* Get gem ports for given pon interface , onu id and uni id. */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530518
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700519 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
520 return RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530521}
522
Gamze Abakafee36392019-10-03 11:17:24 +0000523// GetCurrentAllocIDsForOnu returns alloc ids for given pon interface and onu id
524func (RsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDsForOnu(intfID uint32, onuID uint32, uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530525
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700526 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
527 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400528 if AllocID != nil {
Gamze Abakafee36392019-10-03 11:17:24 +0000529 return AllocID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400530 }
Gamze Abakafee36392019-10-03 11:17:24 +0000531 return []uint32{}
532}
533
534// RemoveAllocIDForOnu removes the alloc id for given pon interface, onu id, uni id and alloc id
535func (RsrcMgr *OpenOltResourceMgr) RemoveAllocIDForOnu(intfID uint32, onuID uint32, uniID uint32, allocID uint32) {
536 allocIDs := RsrcMgr.GetCurrentAllocIDsForOnu(intfID, onuID, uniID)
537 for i := 0; i < len(allocIDs); i++ {
538 if allocIDs[i] == allocID {
539 allocIDs = append(allocIDs[:i], allocIDs[i+1:]...)
540 break
541 }
542 }
543 err := RsrcMgr.UpdateAllocIdsForOnu(intfID, onuID, uniID, allocIDs)
544 if err != nil {
545 log.Errorf("Failed to Remove Alloc Id For Onu. IntfID %d onuID %d uniID %d allocID %d",
546 intfID, onuID, uniID, allocID)
547 }
548}
549
550// RemoveGemPortIDForOnu removes the gem port id for given pon interface, onu id, uni id and gem port id
551func (RsrcMgr *OpenOltResourceMgr) RemoveGemPortIDForOnu(intfID uint32, onuID uint32, uniID uint32, gemPortID uint32) {
552 gemPortIDs := RsrcMgr.GetCurrentGEMPortIDsForOnu(intfID, onuID, uniID)
553 for i := 0; i < len(gemPortIDs); i++ {
554 if gemPortIDs[i] == gemPortID {
555 gemPortIDs = append(gemPortIDs[:i], gemPortIDs[i+1:]...)
556 break
557 }
558 }
559 err := RsrcMgr.UpdateGEMPortIDsForOnu(intfID, onuID, uniID, gemPortIDs)
560 if err != nil {
561 log.Errorf("Failed to Remove Gem Id For Onu. IntfID %d onuID %d uniID %d gemPortId %d",
562 intfID, onuID, uniID, gemPortID)
563 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530564}
565
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700566// UpdateGEMportsPonportToOnuMapOnKVStore updates onu and uni id associated with the gem port to the kv store
567// This stored information is used when packet_indication is received and we need to derive the ONU Id for which
568// the packet arrived based on the pon_intf and gemport available in the packet_indication
569func (RsrcMgr *OpenOltResourceMgr) UpdateGEMportsPonportToOnuMapOnKVStore(gemPorts []uint32, PonPort uint32,
570 onuID uint32, uniID uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530571
Girish Gowdru0c588b22019-04-23 23:24:56 -0400572 /* Update onu and uni id associated with the gem port to the kv store. */
573 var IntfGEMPortPath string
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700574 Data := fmt.Sprintf("%d %d", onuID, uniID)
575 for _, GEM := range gemPorts {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400576 IntfGEMPortPath = fmt.Sprintf("%d,%d", PonPort, GEM)
577 Val, err := json.Marshal(Data)
578 if err != nil {
579 log.Error("failed to Marshal")
580 return err
581 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700582
Girish Gowdru0c588b22019-04-23 23:24:56 -0400583 if err = RsrcMgr.KVStore.Put(IntfGEMPortPath, Val); err != nil {
584 log.Errorf("Failed to update resource %s", IntfGEMPortPath)
585 return err
586 }
587 }
588 return nil
Abhilash S.L7f17e402019-03-15 17:40:41 +0530589}
590
Gamze Abakafee36392019-10-03 11:17:24 +0000591// RemoveGEMportPonportToOnuMapOnKVStore removes the relationship between the gem port and pon port
592func (RsrcMgr *OpenOltResourceMgr) RemoveGEMportPonportToOnuMapOnKVStore(GemPort uint32, PonPort uint32) {
593 IntfGEMPortPath := fmt.Sprintf("%d,%d", PonPort, GemPort)
594 err := RsrcMgr.KVStore.Delete(IntfGEMPortPath)
595 if err != nil {
596 log.Errorf("Failed to Remove Gem port-Pon port to onu map on kv store. Gem %d PonPort %d", GemPort, PonPort)
597 }
598}
599
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700600// GetGEMPortID gets gem port id for a particular pon port, onu id and uni id and then update the resource map on
601// the KV store with the list of gemport_id allocated for the pon_intf_onu_id tuple
602func (RsrcMgr *OpenOltResourceMgr) GetGEMPortID(ponPort uint32, onuID uint32,
603 uniID uint32, NumOfPorts uint32) ([]uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530604
Girish Gowdru0c588b22019-04-23 23:24:56 -0400605 /* Get gem port id for a particular pon port, onu id
606 and uni id.
607 */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530608
Girish Gowdru0c588b22019-04-23 23:24:56 -0400609 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700610 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530611
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700612 GEMPortList := RsrcMgr.ResourceMgrs[ponPort].GetCurrentGEMPortIDsForOnu(IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400613 if GEMPortList != nil {
614 return GEMPortList, nil
615 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530616
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700617 GEMPortList, err = RsrcMgr.ResourceMgrs[ponPort].GetResourceID(ponPort,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400618 ponrmgr.GEMPORT_ID, NumOfPorts)
619 if err != nil && GEMPortList == nil {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530620 log.Errorf("Failed to get gem port id for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400621 return nil, err
622 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530623
Girish Gowdru0c588b22019-04-23 23:24:56 -0400624 // update the resource map on KV store with the list of gemport_id
625 // allocated for the pon_intf_onu_id tuple
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700626 err = RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400627 GEMPortList)
628 if err != nil {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530629 log.Errorf("Failed to update GEM ports to kv store for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400630 return nil, err
631 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700632 _ = RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(GEMPortList, ponPort,
633 onuID, uniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400634 return GEMPortList, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530635}
636
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700637// UpdateGEMPortIDsForOnu updates gemport ids on to the kv store for a given pon port, onu id and uni id
638func (RsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ponPort uint32, onuID uint32,
639 uniID uint32, GEMPortList []uint32) error {
640 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
641 return RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400642 GEMPortList)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530643
644}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700645
646// FreeonuID releases(make free) onu id for a particular pon-port
647func (RsrcMgr *OpenOltResourceMgr) FreeonuID(intfID uint32, onuID []uint32) {
648
649 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID, ponrmgr.ONU_ID, onuID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530650
Girish Gowdru0c588b22019-04-23 23:24:56 -0400651 /* Free onu id for a particular interface.*/
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700652 var IntfonuID string
653 for _, onu := range onuID {
654 IntfonuID = fmt.Sprintf("%d,%d", intfID, onu)
655 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(IntfonuID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400656 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530657}
658
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700659// FreeFlowID returns the free flow id for a given interface, onu id and uni id
Devmalya Paul495b94a2019-08-27 19:42:00 -0400660func (RsrcMgr *OpenOltResourceMgr) FreeFlowID(IntfID uint32, onuID int32,
661 uniID int32, FlowID uint32) {
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400662 var IntfONUID string
663 var err error
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530664 FlowIds := make([]uint32, 0)
665
666 FlowIds = append(FlowIds, FlowID)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700667 IntfONUID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
668 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(IntfONUID, FlowID, false)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400669 if err != nil {
670 log.Error("Failed to Update flow id infor for %s", IntfONUID)
671 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700672 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(IntfONUID, FlowID)
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530673 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(IntfID, ponrmgr.FLOW_ID, FlowIds)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400674}
675
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700676// FreeFlowIDs releases the flow Ids
677func (RsrcMgr *OpenOltResourceMgr) FreeFlowIDs(IntfID uint32, onuID uint32,
678 uniID uint32, FlowID []uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530679
Girish Gowdru0c588b22019-04-23 23:24:56 -0400680 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(IntfID, ponrmgr.FLOW_ID, FlowID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530681
Abhilash S.L8ee90712019-04-29 16:24:22 +0530682 var IntfOnuIDUniID string
Girish Gowdru0c588b22019-04-23 23:24:56 -0400683 var err error
684 for _, flow := range FlowID {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700685 IntfOnuIDUniID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530686 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(IntfOnuIDUniID, flow, false)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400687 if err != nil {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530688 log.Error("Failed to Update flow id infor for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400689 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530690 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(IntfOnuIDUniID, flow)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400691 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530692}
693
Gamze Abakafee36392019-10-03 11:17:24 +0000694// FreeAllocID frees AllocID on the PON resource pool and also frees the allocID association
695// for the given OLT device.
696func (RsrcMgr *OpenOltResourceMgr) FreeAllocID(IntfID uint32, onuID uint32,
697 uniID uint32, allocID uint32) {
698 RsrcMgr.RemoveAllocIDForOnu(IntfID, onuID, uniID, allocID)
699 allocIDs := make([]uint32, 0)
700 allocIDs = append(allocIDs, allocID)
701 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(IntfID, ponrmgr.ALLOC_ID, allocIDs)
702}
703
704// FreeGemPortID frees GemPortID on the PON resource pool and also frees the gemPortID association
705// for the given OLT device.
706func (RsrcMgr *OpenOltResourceMgr) FreeGemPortID(IntfID uint32, onuID uint32,
707 uniID uint32, gemPortID uint32) {
708 RsrcMgr.RemoveGemPortIDForOnu(IntfID, onuID, uniID, gemPortID)
709 gemPortIDs := make([]uint32, 0)
710 gemPortIDs = append(gemPortIDs, gemPortID)
711 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(IntfID, ponrmgr.GEMPORT_ID, gemPortIDs)
712}
713
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700714// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id, and the clears the
715// resource map and the onuID associated with (pon_intf_id, gemport_id) tuple,
716func (RsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(intfID uint32, onuID uint32, uniID uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530717
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700718 var onuIDs []uint32
719 onuIDs = append(onuIDs, onuID)
720 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530721
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700722 AllocIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530723
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700724 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400725 ponrmgr.ALLOC_ID,
726 AllocIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530727
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700728 GEMPortIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(IntfOnuIDUniID)
729 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400730 ponrmgr.GEMPORT_ID,
731 GEMPortIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530732
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700733 FlowIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentFlowIDsForOnu(IntfOnuIDUniID)
734 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400735 ponrmgr.FLOW_ID,
736 FlowIDs)
Devmalya Paul495b94a2019-08-27 19:42:00 -0400737 if int32(onuID) >= 0 {
738 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
739 ponrmgr.ONU_ID,
740 onuIDs)
741 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400742 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700743 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530744
Girish Gowdru0c588b22019-04-23 23:24:56 -0400745 // Clear the ONU Id associated with the (pon_intf_id, gemport_id) tuple.
746 for _, GEM := range GEMPortIDs {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700747 _ = RsrcMgr.KVStore.Delete(fmt.Sprintf("%d,%d", intfID, GEM))
Girish Gowdru0c588b22019-04-23 23:24:56 -0400748 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530749}
750
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700751// IsFlowCookieOnKVStore checks if the given flow cookie is present on the kv store
752// Returns true if the flow cookie is found, otherwise it returns false
753func (RsrcMgr *OpenOltResourceMgr) IsFlowCookieOnKVStore(ponIntfID uint32, onuID uint32, uniID uint32,
754 flowStoreCookie uint64) bool {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530755
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700756 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
757 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(FlowPath)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400758 if FlowIDs != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700759 log.Debugw("Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "onuID": onuID, "uniID": uniID, "KVpath": FlowPath})
760 for _, flowID := range FlowIDs {
761 FlowInfo := RsrcMgr.GetFlowIDInfo(ponIntfID, onuID, uniID, uint32(flowID))
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400762 if FlowInfo != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700763 log.Debugw("Found flows", log.Fields{"flows": *FlowInfo, "flowId": flowID})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400764 for _, Info := range *FlowInfo {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700765 if Info.FlowStoreCookie == flowStoreCookie {
766 log.Debug("Found flow matching with flowStore cookie", log.Fields{"flowId": flowID, "flowStoreCookie": flowStoreCookie})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400767 return true
768 }
769 }
770 }
771 }
772 }
773 return false
774}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400775
salmansiddiqui7ac62132019-08-22 03:58:50 +0000776// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000777// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
778func (RsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(IntfID uint32, OnuID uint32, UniID uint32) []uint32 {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000779 Path := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000780 var Data []uint32
salmansiddiqui7ac62132019-08-22 03:58:50 +0000781 Value, err := RsrcMgr.KVStore.Get(Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400782 if err == nil {
783 if Value != nil {
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 Data
788 }
789 if err = json.Unmarshal(Val, &Data); err != nil {
790 log.Error("Failed to unmarshal", log.Fields{"error": err})
791 return Data
792 }
793 }
794 } else {
795 log.Errorf("Failed to get TP id from kvstore for path %s", Path)
796 }
797 log.Debugf("Getting TP id %d from path %s", Data, Path)
798 return Data
799
800}
801
Gamze Abakafee36392019-10-03 11:17:24 +0000802// RemoveTechProfileIDsForOnu deletes all tech profile ids from the KV-Store for the given onu based on the path
803// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
804func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDsForOnu(IntfID uint32, OnuID uint32, UniID uint32) error {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000805 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
806 if err := RsrcMgr.KVStore.Delete(IntfOnuUniID); err != nil {
807 log.Error("Failed to delete techprofile id resource %s in KV store", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400808 return err
809 }
810 return nil
811}
812
Gamze Abakafee36392019-10-03 11:17:24 +0000813// RemoveTechProfileIDForOnu deletes a specific tech profile id from the KV-Store for the given onu based on the path
814// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
815func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(IntfID uint32, OnuID uint32, UniID uint32, TpID uint32) error {
816 tpIDList := RsrcMgr.GetTechProfileIDForOnu(IntfID, OnuID, UniID)
817 for i, tpIDInList := range tpIDList {
818 if tpIDInList == TpID {
819 tpIDList = append(tpIDList[:i], tpIDList[i+1:]...)
820 }
821 }
822 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
823 Value, err := json.Marshal(tpIDList)
824 if err != nil {
825 log.Error("failed to Marshal")
826 return err
827 }
828 if err = RsrcMgr.KVStore.Put(IntfOnuUniID, Value); err != nil {
829 log.Errorf("Failed to update resource %s", IntfOnuUniID)
830 return err
831 }
832 return err
833}
834
835// UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
836// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
salmansiddiqui7ac62132019-08-22 03:58:50 +0000837func (RsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(IntfID uint32, OnuID uint32,
838 UniID uint32, TpID uint32) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400839 var Value []byte
840 var err error
841
salmansiddiqui7ac62132019-08-22 03:58:50 +0000842 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000843
844 tpIDList := RsrcMgr.GetTechProfileIDForOnu(IntfID, OnuID, UniID)
845 for _, value := range tpIDList {
846 if value == TpID {
847 log.Debugf("TpID %d is already in tpIdList for the path %s", TpID, IntfOnuUniID)
848 return err
849 }
850 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000851 log.Debugf("updating tp id %d on path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000852 tpIDList = append(tpIDList, TpID)
853 Value, err = json.Marshal(tpIDList)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400854 if err != nil {
855 log.Error("failed to Marshal")
856 return err
857 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000858 if err = RsrcMgr.KVStore.Put(IntfOnuUniID, Value); err != nil {
859 log.Errorf("Failed to update resource %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400860 return err
861 }
862 return err
863}
864
salmansiddiqui7ac62132019-08-22 03:58:50 +0000865// UpdateMeterIDForOnu updates the meter id in the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000866// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
salmansiddiqui7ac62132019-08-22 03:58:50 +0000867func (RsrcMgr *OpenOltResourceMgr) UpdateMeterIDForOnu(Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000868 UniID uint32, TpID uint32, MeterConfig *ofp.OfpMeterConfig) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400869 var Value []byte
870 var err error
871
Gamze Abakafee36392019-10-03 11:17:24 +0000872 IntfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400873 Value, err = json.Marshal(*MeterConfig)
874 if err != nil {
875 log.Error("failed to Marshal meter config")
876 return err
877 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000878 if err = RsrcMgr.KVStore.Put(IntfOnuUniID, Value); err != nil {
879 log.Errorf("Failed to store meter into KV store %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400880 return err
881 }
882 return err
883}
884
Gamze Abakafee36392019-10-03 11:17:24 +0000885// GetMeterIDForOnu fetches the meter id from the kv store for the given onu based on the path
886// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
887func (RsrcMgr *OpenOltResourceMgr) GetMeterIDForOnu(Direction string, IntfID uint32, OnuID uint32,
888 UniID uint32, TpID uint32) (*ofp.OfpMeterConfig, error) {
889 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400890 var meterConfig ofp.OfpMeterConfig
salmansiddiqui7ac62132019-08-22 03:58:50 +0000891 Value, err := RsrcMgr.KVStore.Get(Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400892 if err == nil {
893 if Value != nil {
894 log.Debug("Found meter in KV store", log.Fields{"Direction": Direction})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000895 Val, er := kvstore.ToByte(Value.Value)
896 if er != nil {
897 log.Errorw("Failed to convert into byte array", log.Fields{"error": er})
898 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400899 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000900 if er = json.Unmarshal(Val, &meterConfig); er != nil {
901 log.Error("Failed to unmarshal meterconfig", log.Fields{"error": er})
902 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400903 }
904 } else {
905 log.Debug("meter-does-not-exists-in-KVStore")
906 return nil, err
907 }
908 } else {
909 log.Errorf("Failed to get Meter config from kvstore for path %s", Path)
910
911 }
912 return &meterConfig, err
913}
914
Gamze Abakafee36392019-10-03 11:17:24 +0000915// RemoveMeterIDForOnu deletes the meter id from the kV-Store for the given onu based on the path
916// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
917func (RsrcMgr *OpenOltResourceMgr) RemoveMeterIDForOnu(Direction string, IntfID uint32, OnuID uint32,
918 UniID uint32, TpID uint32) error {
919 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
salmansiddiqui7ac62132019-08-22 03:58:50 +0000920 if err := RsrcMgr.KVStore.Delete(Path); err != nil {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400921 log.Errorf("Failed to delete meter id %s from kvstore ", Path)
922 return err
923 }
924 return nil
925}
salmansiddiqui7ac62132019-08-22 03:58:50 +0000926
927func getFlowIDFromFlowInfo(FlowInfo *[]FlowInfo, flowID, gemportID uint32, flowStoreCookie uint64, flowCategory string, vlanPcp ...uint32) error {
928 if FlowInfo != nil {
929 for _, Info := range *FlowInfo {
930 if int32(gemportID) == Info.Flow.GemportId && flowCategory != "" && Info.FlowCategory == flowCategory {
931 log.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
932 if Info.FlowCategory == "HSIA_FLOW" && Info.Flow.Classifier.OPbits == vlanPcp[0] {
933 log.Debug("Found matching vlan pcp ", log.Fields{"flowId": flowID, "Vlanpcp": vlanPcp[0]})
934 return nil
935 }
936 }
937 if int32(gemportID) == Info.Flow.GemportId && flowStoreCookie != 0 && Info.FlowStoreCookie == flowStoreCookie {
938 if flowCategory != "" && Info.FlowCategory == flowCategory {
939 log.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
940 return nil
941 }
942 }
943 }
944 }
Gamze Abakafee36392019-10-03 11:17:24 +0000945 log.Debugw("the flow can be related to a different service", log.Fields{"flow_info": FlowInfo})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000946 return errors.New("invalid flow-info")
947}