blob: 21f21c9520ec3b9eca19517e7af25711a839186f [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
sbarbaria8910ba2019-11-05 10:12:23 -050027 "github.com/opencord/voltha-lib-go/v2/pkg/db"
Scott Baker51290152019-10-24 14:23:20 -070028 "github.com/opencord/voltha-lib-go/v2/pkg/db/kvstore"
Scott Baker51290152019-10-24 14:23:20 -070029 "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}"
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053044 //NnniIntfID - nniintfids
45 NnniIntfID = "nniintfids"
46 // OnuPacketINPath path on the kvstore to store packetin gemport,which will be used for packetin, pcketout
47 //format: onu_packetin/<intfid>,<onuid>,<logicalport>
48 OnuPacketINPath = "onu_packetin/{%d,%d,%d}"
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +053049 //FlowIDsForGem flowids_per_gem/<intfid>
50 FlowIDsForGem = "flowids_per_gem/{%d}"
salmansiddiqui7ac62132019-08-22 03:58:50 +000051)
Abhilash S.L7f17e402019-03-15 17:40:41 +053052
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070053// FlowInfo holds the flow information
Abhilash S.L8ee90712019-04-29 16:24:22 +053054type FlowInfo struct {
55 Flow *openolt.Flow
56 FlowStoreCookie uint64
57 FlowCategory string
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053058 LogicalFlowID uint64
59}
60
61// OnuGemInfo holds onu information along with gem port list and uni port list
62type OnuGemInfo struct {
63 OnuID uint32
64 SerialNumber string
65 IntfID uint32
66 GemPorts []uint32
67 UniPorts []uint32
68}
69
70// PacketInInfoKey is the key for packet in gemport
71type PacketInInfoKey struct {
72 IntfID uint32
73 OnuID uint32
74 LogicalPort uint32
Abhilash S.L8ee90712019-04-29 16:24:22 +053075}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070076
77// OpenOltResourceMgr holds resource related information as provided below for each field
Abhilash S.L7f17e402019-03-15 17:40:41 +053078type OpenOltResourceMgr struct {
sbarbaria8910ba2019-11-05 10:12:23 -050079 DeviceID string // OLT device id
80 HostAndPort string // Host and port of the kv store to connect to
81 Args string // args
82 KVStore *db.Backend // backend kv store connection handle
Girish Gowdru0c588b22019-04-23 23:24:56 -040083 DeviceType string
84 Host string // Host ip of the kv store
85 Port int // port of the kv store
86 DevInfo *openolt.DeviceInfo // device information
87 // array of pon resource managers per interface technology
88 ResourceMgrs map[uint32]*ponrmgr.PONResourceManager
Abhilash S.L7f17e402019-03-15 17:40:41 +053089}
90
Manikkaraj kb1d51442019-07-23 10:41:02 -040091func newKVClient(storeType string, address string, timeout uint32) (kvstore.Client, error) {
Girish Gowdru0c588b22019-04-23 23:24:56 -040092 log.Infow("kv-store-type", log.Fields{"store": storeType})
93 switch storeType {
94 case "consul":
95 return kvstore.NewConsulClient(address, int(timeout))
96 case "etcd":
97 return kvstore.NewEtcdClient(address, int(timeout))
98 }
99 return nil, errors.New("unsupported-kv-store")
Abhilash S.L7f17e402019-03-15 17:40:41 +0530100}
101
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700102// SetKVClient sets the KV client and return a kv backend
sbarbaria8910ba2019-11-05 10:12:23 -0500103func SetKVClient(backend string, Host string, Port int, DeviceID string) *db.Backend {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400104 addr := Host + ":" + strconv.Itoa(Port)
105 // TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
106 // issue between kv store and backend , core is not calling NewBackend directly
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700107 kvClient, err := newKVClient(backend, addr, KvstoreTimeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400108 if err != nil {
109 log.Fatalw("Failed to init KV client\n", log.Fields{"err": err})
110 return nil
111 }
sbarbaria8910ba2019-11-05 10:12:23 -0500112 kvbackend := &db.Backend{
Girish Gowdru0c588b22019-04-23 23:24:56 -0400113 Client: kvClient,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700114 StoreType: backend,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400115 Host: Host,
116 Port: Port,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700117 Timeout: KvstoreTimeout,
118 PathPrefix: fmt.Sprintf(BasePathKvStore, DeviceID)}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530119
Girish Gowdru0c588b22019-04-23 23:24:56 -0400120 return kvbackend
Abhilash S.L7f17e402019-03-15 17:40:41 +0530121}
122
Gamze Abakafee36392019-10-03 11:17:24 +0000123// NewResourceMgr init a New resource manager instance which in turn instantiates pon resource manager
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700124// instances according to technology. Initializes the default resource ranges for all
125// the resources.
126func NewResourceMgr(deviceID string, KVStoreHostPort string, kvStoreType string, deviceType string, devInfo *openolt.DeviceInfo) *OpenOltResourceMgr {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400127 var ResourceMgr OpenOltResourceMgr
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700128 log.Debugf("Init new resource manager , host_port: %s, deviceid: %s", KVStoreHostPort, deviceID)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530129 ResourceMgr.HostAndPort = KVStoreHostPort
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700130 ResourceMgr.DeviceType = deviceType
131 ResourceMgr.DevInfo = devInfo
132 IPPort := strings.Split(KVStoreHostPort, ":")
133 ResourceMgr.Host = IPPort[0]
134 ResourceMgr.Port, _ = strconv.Atoi(IPPort[1])
Abhilash S.L7f17e402019-03-15 17:40:41 +0530135
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700136 Backend := kvStoreType
Girish Gowdru0c588b22019-04-23 23:24:56 -0400137 ResourceMgr.KVStore = SetKVClient(Backend, ResourceMgr.Host,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700138 ResourceMgr.Port, deviceID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400139 if ResourceMgr.KVStore == nil {
140 log.Error("Failed to setup KV store")
141 }
142 Ranges := make(map[string]*openolt.DeviceInfo_DeviceResourceRanges)
143 RsrcMgrsByTech := make(map[string]*ponrmgr.PONResourceManager)
144 ResourceMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530145
Girish Gowdru0c588b22019-04-23 23:24:56 -0400146 // TODO self.args = registry('main').get_args()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530147
Girish Gowdru0c588b22019-04-23 23:24:56 -0400148 /*
149 If a legacy driver returns protobuf without any ranges,s synthesize one from
Gamze Abakafee36392019-10-03 11:17:24 +0000150 the legacy global per-device information. This, in theory, is temporary until
Girish Gowdru0c588b22019-04-23 23:24:56 -0400151 the legacy drivers are upgrade to support pool ranges.
152 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700153 if devInfo.Ranges == nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400154 var ranges openolt.DeviceInfo_DeviceResourceRanges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700155 ranges.Technology = devInfo.GetTechnology()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530156
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700157 NumPONPorts := devInfo.GetPonPorts()
Girish Gowdru0c588b22019-04-23 23:24:56 -0400158 var index uint32
159 for index = 0; index < NumPONPorts; index++ {
160 ranges.IntfIds = append(ranges.IntfIds, index)
161 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530162
Abhilash S.L8ee90712019-04-29 16:24:22 +0530163 var Pool openolt.DeviceInfo_DeviceResourceRanges_Pool
Girish Gowdru0c588b22019-04-23 23:24:56 -0400164 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700165 Pool.Start = devInfo.OnuIdStart
166 Pool.End = devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400167 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
cbabuabf02352019-10-15 13:14:56 +0200168 onuPool := Pool
169 ranges.Pools = append(ranges.Pools, &onuPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530170
Girish Gowdru0c588b22019-04-23 23:24:56 -0400171 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700172 Pool.Start = devInfo.AllocIdStart
173 Pool.End = devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400174 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200175 allocPool := Pool
176 ranges.Pools = append(ranges.Pools, &allocPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530177
Girish Gowdru0c588b22019-04-23 23:24:56 -0400178 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700179 Pool.Start = devInfo.GemportIdStart
180 Pool.End = devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400181 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200182 gemPool := Pool
183 ranges.Pools = append(ranges.Pools, &gemPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530184
Girish Gowdru0c588b22019-04-23 23:24:56 -0400185 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700186 Pool.Start = devInfo.FlowIdStart
187 Pool.End = devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400188 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
Abhilash S.L8ee90712019-04-29 16:24:22 +0530189 ranges.Pools = append(ranges.Pools, &Pool)
190 // Add to device info
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700191 devInfo.Ranges = append(devInfo.Ranges, &ranges)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400192 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530193
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700194 // Create a separate Resource Manager instance for each range. This assumes that
Girish Gowdru0c588b22019-04-23 23:24:56 -0400195 // each technology is represented by only a single range
196 var GlobalPONRsrcMgr *ponrmgr.PONResourceManager
197 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700198 for _, TechRange := range devInfo.Ranges {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400199 technology := TechRange.Technology
200 log.Debugf("Device info technology %s", technology)
201 Ranges[technology] = TechRange
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700202 RsrcMgrsByTech[technology], err = ponrmgr.NewPONResourceManager(technology, deviceType, deviceID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400203 Backend, ResourceMgr.Host, ResourceMgr.Port)
204 if err != nil {
Gamze Abakafee36392019-10-03 11:17:24 +0000205 log.Errorf("Failed to create pon resource manager instance for technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400206 return nil
207 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700208 // resource_mgrs_by_tech[technology] = resource_mgr
Girish Gowdru0c588b22019-04-23 23:24:56 -0400209 if GlobalPONRsrcMgr == nil {
210 GlobalPONRsrcMgr = RsrcMgrsByTech[technology]
211 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700212 for _, IntfID := range TechRange.IntfIds {
213 ResourceMgr.ResourceMgrs[uint32(IntfID)] = RsrcMgrsByTech[technology]
Girish Gowdru0c588b22019-04-23 23:24:56 -0400214 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700215 // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400216 InitializeDeviceResourceRangeAndPool(RsrcMgrsByTech[technology], GlobalPONRsrcMgr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700217 TechRange, devInfo)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400218 }
219 // After we have initialized resource ranges, initialize the
220 // resource pools accordingly.
221 for _, PONRMgr := range RsrcMgrsByTech {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700222 _ = PONRMgr.InitDeviceResourcePool()
Girish Gowdru0c588b22019-04-23 23:24:56 -0400223 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530224 log.Info("Initialization of resource manager success!")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400225 return &ResourceMgr
Abhilash S.L7f17e402019-03-15 17:40:41 +0530226}
227
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700228// InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply
229// device specific information. If KV doesn't exist
230// or is broader than the device, the device's information will
231// dictate the range limits
232func InitializeDeviceResourceRangeAndPool(ponRMgr *ponrmgr.PONResourceManager, globalPONRMgr *ponrmgr.PONResourceManager,
233 techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530234
Girish Gowdru0c588b22019-04-23 23:24:56 -0400235 // init the resource range pool according to the sharing type
Abhilash S.L7f17e402019-03-15 17:40:41 +0530236
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700237 log.Debugf("Resource range pool init for technology %s", ponRMgr.Technology)
238 // first load from KV profiles
239 status := ponRMgr.InitResourceRangesFromKVStore()
240 if !status {
241 log.Debugf("Failed to load resource ranges from KV store for tech %s", ponRMgr.Technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400242 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530243
Girish Gowdru0c588b22019-04-23 23:24:56 -0400244 /*
245 Then apply device specific information. If KV doesn't exist
Gamze Abakafee36392019-10-03 11:17:24 +0000246 or is broader than the device, the device's information will
Girish Gowdru0c588b22019-04-23 23:24:56 -0400247 dictate the range limits
248 */
Girish Kumar8f73fe02019-12-09 13:19:37 +0000249 log.Debugw("Using device info to init pon resource ranges", log.Fields{"Tech": ponRMgr.Technology})
Abhilash S.L7f17e402019-03-15 17:40:41 +0530250
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700251 ONUIDStart := devInfo.OnuIdStart
252 ONUIDEnd := devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400253 ONUIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
254 ONUIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700255 AllocIDStart := devInfo.AllocIdStart
256 AllocIDEnd := devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400257 AllocIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
258 AllocIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700259 GEMPortIDStart := devInfo.GemportIdStart
260 GEMPortIDEnd := devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400261 GEMPortIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
262 GEMPortIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700263 FlowIDStart := devInfo.FlowIdStart
264 FlowIDEnd := devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400265 FlowIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
266 FlowIDSharedPoolID := uint32(0)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530267
Girish Gowdru0c588b22019-04-23 23:24:56 -0400268 var FirstIntfPoolID uint32
269 var SharedPoolID uint32
Abhilash S.L7f17e402019-03-15 17:40:41 +0530270
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400271 /*
272 * 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 -0700273 * if resources are shared across interfaces then SharedPoolID is given a positive number.
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400274 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700275 for _, FirstIntfPoolID = range techRange.IntfIds {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400276 // skip the intf id 0
277 if FirstIntfPoolID == 0 {
278 continue
279 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400280 break
281 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530282
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700283 for _, RangePool := range techRange.Pools {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400284 if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400285 SharedPoolID = FirstIntfPoolID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400286 } else if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_SAME_TECH {
287 SharedPoolID = FirstIntfPoolID
288 } else {
289 SharedPoolID = 0
290 }
291 if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID {
292 ONUIDStart = RangePool.Start
293 ONUIDEnd = RangePool.End
294 ONUIDShared = RangePool.Sharing
295 ONUIDSharedPoolID = SharedPoolID
296 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID {
297 AllocIDStart = RangePool.Start
298 AllocIDEnd = RangePool.End
299 AllocIDShared = RangePool.Sharing
300 AllocIDSharedPoolID = SharedPoolID
301 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID {
302 GEMPortIDStart = RangePool.Start
303 GEMPortIDEnd = RangePool.End
304 GEMPortIDShared = RangePool.Sharing
305 GEMPortIDSharedPoolID = SharedPoolID
306 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID {
307 FlowIDStart = RangePool.Start
308 FlowIDEnd = RangePool.End
309 FlowIDShared = RangePool.Sharing
310 FlowIDSharedPoolID = SharedPoolID
311 }
312 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530313
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700314 log.Debugw("Device info init", log.Fields{"technology": techRange.Technology,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400315 "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd, "onu_id_shared_pool_id": ONUIDSharedPoolID,
316 "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd,
317 "alloc_id_shared_pool_id": AllocIDSharedPoolID,
318 "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd,
319 "gemport_id_shared_pool_id": GEMPortIDSharedPoolID,
320 "flow_id_start": FlowIDStart,
321 "flow_id_end_idx": FlowIDEnd,
322 "flow_id_shared_pool_id": FlowIDSharedPoolID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700323 "intf_ids": techRange.IntfIds,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400324 "uni_id_start": 0,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700325 "uni_id_end_idx": 1, /*MaxUNIIDperONU()*/
326 })
Abhilash S.L7f17e402019-03-15 17:40:41 +0530327
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700328 ponRMgr.InitDefaultPONResourceRanges(ONUIDStart, ONUIDEnd, ONUIDSharedPoolID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400329 AllocIDStart, AllocIDEnd, AllocIDSharedPoolID,
330 GEMPortIDStart, GEMPortIDEnd, GEMPortIDSharedPoolID,
331 FlowIDStart, FlowIDEnd, FlowIDSharedPoolID, 0, 1,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700332 devInfo.PonPorts, techRange.IntfIds)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530333
Girish Gowdru0c588b22019-04-23 23:24:56 -0400334 // For global sharing, make sure to refresh both local and global resource manager instances' range
Abhilash S.L7f17e402019-03-15 17:40:41 +0530335
Girish Gowdru0c588b22019-04-23 23:24:56 -0400336 if ONUIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700337 globalPONRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400338 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700339 ponRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
340 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400341 }
342 if AllocIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700343 globalPONRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400344 "", 0, nil)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530345
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700346 ponRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
347 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400348 }
349 if GEMPortIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700350 globalPONRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400351 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700352 ponRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
353 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400354 }
355 if FlowIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700356 globalPONRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400357 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700358 ponRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
359 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400360 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530361
Girish Gowdru0c588b22019-04-23 23:24:56 -0400362 // Make sure loaded range fits the platform bit encoding ranges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700363 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 +0530364}
365
Devmalya Paul495b94a2019-08-27 19:42:00 -0400366// Delete clears used resources for the particular olt device being deleted
367func (RsrcMgr *OpenOltResourceMgr) Delete() error {
368 /* TODO
369 def __del__(self):
370 self.log.info("clearing-device-resource-pool")
371 for key, resource_mgr in self.resource_mgrs.iteritems():
372 resource_mgr.clear_device_resource_pool()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530373
Devmalya Paul495b94a2019-08-27 19:42:00 -0400374 def assert_pon_id_limit(self, pon_intf_id):
375 assert pon_intf_id in self.resource_mgrs
Abhilash S.L7f17e402019-03-15 17:40:41 +0530376
Devmalya Paul495b94a2019-08-27 19:42:00 -0400377 def assert_onu_id_limit(self, pon_intf_id, onu_id):
378 self.assert_pon_id_limit(pon_intf_id)
379 self.resource_mgrs[pon_intf_id].assert_resource_limits(onu_id, PONResourceManager.ONU_ID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530380
Devmalya Paul495b94a2019-08-27 19:42:00 -0400381 @property
382 def max_uni_id_per_onu(self):
383 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 +0530384
Devmalya Paul495b94a2019-08-27 19:42:00 -0400385 def assert_uni_id_limit(self, pon_intf_id, onu_id, uni_id):
386 self.assert_onu_id_limit(pon_intf_id, onu_id)
387 self.resource_mgrs[pon_intf_id].assert_resource_limits(uni_id, PONResourceManager.UNI_ID)
388 */
389 for _, rsrcMgr := range RsrcMgr.ResourceMgrs {
390 if err := rsrcMgr.ClearDeviceResourcePool(); err != nil {
391 log.Debug("Failed to clear device resource pool")
392 return err
393 }
394 }
395 log.Debug("Cleared device resource pool")
396 return nil
397}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530398
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700399// GetONUID returns the available OnuID for the given pon-port
400func (RsrcMgr *OpenOltResourceMgr) GetONUID(ponIntfID uint32) (uint32, error) {
salmansiddiqui352a45c2019-08-19 10:15:36 +0000401 // Check if Pon Interface ID is present in Resource-manager-map
402 if _, ok := RsrcMgr.ResourceMgrs[ponIntfID]; !ok {
403 err := errors.New("invalid-pon-interface-" + strconv.Itoa(int(ponIntfID)))
404 return 0, err
405 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400406 // Get ONU id for a provided pon interface ID.
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700407 ONUID, err := RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400408 ponrmgr.ONU_ID, 1)
409 if err != nil {
410 log.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700411 ponIntfID, ponrmgr.ONU_ID)
cbabuabf02352019-10-15 13:14:56 +0200412 return 0, err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400413 }
414 if ONUID != nil {
Devmalya Paul495b94a2019-08-27 19:42:00 -0400415 RsrcMgr.ResourceMgrs[ponIntfID].InitResourceMap(fmt.Sprintf("%d,%d", ponIntfID, ONUID[0]))
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700416 return ONUID[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400417 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530418
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700419 return 0, err // return OnuID 0 on error
Abhilash S.L7f17e402019-03-15 17:40:41 +0530420}
421
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700422// GetFlowIDInfo returns the slice of flow info of the given pon-port
423// Note: For flows which trap from the NNI and not really associated with any particular
424// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530425func (RsrcMgr *OpenOltResourceMgr) GetFlowIDInfo(ponIntfID uint32, onuID int32, uniID int32, flowID uint32) *[]FlowInfo {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530426 var flows []FlowInfo
427
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700428 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
429 if err := RsrcMgr.ResourceMgrs[ponIntfID].GetFlowIDInfo(FlowPath, flowID, &flows); err != nil {
430 log.Errorw("Error while getting flows from KV store", log.Fields{"flowId": flowID})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530431 return nil
432 }
433 if len(flows) == 0 {
434 log.Debugw("No flowInfo found in KV store", log.Fields{"flowPath": FlowPath})
435 return nil
436 }
437 return &flows
438}
439
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700440// GetCurrentFlowIDsForOnu fetches flow ID from the resource manager
441// Note: For flows which trap from the NNI and not really associated with any particular
442// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530443func (RsrcMgr *OpenOltResourceMgr) GetCurrentFlowIDsForOnu(PONIntfID uint32, ONUID int32, UNIID int32) []uint32 {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700444
Abhilash S.L8ee90712019-04-29 16:24:22 +0530445 FlowPath := fmt.Sprintf("%d,%d,%d", PONIntfID, ONUID, UNIID)
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700446 if mgrs, exist := RsrcMgr.ResourceMgrs[PONIntfID]; exist {
447 return mgrs.GetCurrentFlowIDsForOnu(FlowPath)
448 }
449 return nil
Abhilash S.L8ee90712019-04-29 16:24:22 +0530450}
451
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700452// UpdateFlowIDInfo updates flow info for the given pon interface, onu id, and uni id
453// Note: For flows which trap from the NNI and not really associated with any particular
454// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
455func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDInfo(ponIntfID int32, onuID int32, uniID int32,
456 flowID uint32, flowData *[]FlowInfo) error {
457 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
458 return RsrcMgr.ResourceMgrs[uint32(ponIntfID)].UpdateFlowIDInfoForOnu(FlowPath, flowID, *flowData)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530459}
460
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700461// GetFlowID return flow ID for a given pon interface id, onu id and uni id
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530462func (RsrcMgr *OpenOltResourceMgr) GetFlowID(ponIntfID uint32, ONUID int32, uniID int32,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400463 gemportID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700464 flowStoreCookie uint64,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400465 flowCategory string, vlanPcp ...uint32) (uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530466
Girish Gowdru0c588b22019-04-23 23:24:56 -0400467 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700468 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, ONUID, uniID)
469 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(FlowPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400470 if FlowIDs != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700471 log.Debugw("Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "ONUID": ONUID, "uniID": uniID, "KVpath": FlowPath})
472 for _, flowID := range FlowIDs {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530473 FlowInfo := RsrcMgr.GetFlowIDInfo(ponIntfID, int32(ONUID), int32(uniID), uint32(flowID))
salmansiddiqui7ac62132019-08-22 03:58:50 +0000474 er := getFlowIDFromFlowInfo(FlowInfo, flowID, gemportID, flowStoreCookie, flowCategory, vlanPcp...)
475 if er == nil {
476 return flowID, er
Abhilash S.L8ee90712019-04-29 16:24:22 +0530477 }
478 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400479 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530480 log.Debug("No matching flows with flow cookie or flow category, allocating new flowid")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700481 FlowIDs, err = RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400482 ponrmgr.FLOW_ID, 1)
483 if err != nil {
484 log.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700485 ponIntfID, ponrmgr.FLOW_ID)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400486 return uint32(0), err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400487 }
488 if FlowIDs != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700489 _ = RsrcMgr.ResourceMgrs[ponIntfID].UpdateFlowIDForOnu(FlowPath, FlowIDs[0], true)
490 return FlowIDs[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400491 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530492
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700493 return 0, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530494}
495
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700496// GetAllocID return the first Alloc ID for a given pon interface id and onu id and then update the resource map on
497// the KV store with the list of alloc_ids allocated for the pon_intf_onu_id tuple
498// Currently of all the alloc_ids available, it returns the first alloc_id in the list for tha given ONU
499func (RsrcMgr *OpenOltResourceMgr) GetAllocID(intfID uint32, onuID uint32, uniID uint32) uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530500
Girish Gowdru0c588b22019-04-23 23:24:56 -0400501 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700502 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
503 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400504 if AllocID != nil {
505 // Since we support only one alloc_id for the ONU at the moment,
506 // return the first alloc_id in the list, if available, for that
507 // ONU.
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700508 log.Debugw("Retrieved alloc ID from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400509 return AllocID[0]
510 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700511 AllocID, err = RsrcMgr.ResourceMgrs[intfID].GetResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400512 ponrmgr.ALLOC_ID, 1)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530513
Girish Gowdru0c588b22019-04-23 23:24:56 -0400514 if AllocID == nil || err != nil {
515 log.Error("Failed to allocate alloc id")
516 return 0
517 }
518 // update the resource map on KV store with the list of alloc_id
519 // allocated for the pon_intf_onu_id tuple
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700520 err = RsrcMgr.ResourceMgrs[intfID].UpdateAllocIdsForOnu(IntfOnuIDUniID, AllocID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400521 if err != nil {
522 log.Error("Failed to update Alloc ID")
523 return 0
524 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530525 log.Debugw("Allocated new Tcont from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400526 return AllocID[0]
Abhilash S.L7f17e402019-03-15 17:40:41 +0530527}
528
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700529// UpdateAllocIdsForOnu updates alloc ids in kv store for a given pon interface id, onu id and uni id
530func (RsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ponPort uint32, onuID uint32, uniID uint32, allocID []uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530531
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700532 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
533 return RsrcMgr.ResourceMgrs[ponPort].UpdateAllocIdsForOnu(IntfOnuIDUniID,
534 allocID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530535}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700536
537// GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id
538func (RsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(intfID uint32, onuID uint32,
539 uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530540
Girish Gowdru0c588b22019-04-23 23:24:56 -0400541 /* Get gem ports for given pon interface , onu id and uni id. */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530542
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700543 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
544 return RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530545}
546
Gamze Abakafee36392019-10-03 11:17:24 +0000547// GetCurrentAllocIDsForOnu returns alloc ids for given pon interface and onu id
548func (RsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDsForOnu(intfID uint32, onuID uint32, uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530549
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700550 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
551 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400552 if AllocID != nil {
Gamze Abakafee36392019-10-03 11:17:24 +0000553 return AllocID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400554 }
Gamze Abakafee36392019-10-03 11:17:24 +0000555 return []uint32{}
556}
557
558// RemoveAllocIDForOnu removes the alloc id for given pon interface, onu id, uni id and alloc id
559func (RsrcMgr *OpenOltResourceMgr) RemoveAllocIDForOnu(intfID uint32, onuID uint32, uniID uint32, allocID uint32) {
560 allocIDs := RsrcMgr.GetCurrentAllocIDsForOnu(intfID, onuID, uniID)
561 for i := 0; i < len(allocIDs); i++ {
562 if allocIDs[i] == allocID {
563 allocIDs = append(allocIDs[:i], allocIDs[i+1:]...)
564 break
565 }
566 }
567 err := RsrcMgr.UpdateAllocIdsForOnu(intfID, onuID, uniID, allocIDs)
568 if err != nil {
569 log.Errorf("Failed to Remove Alloc Id For Onu. IntfID %d onuID %d uniID %d allocID %d",
570 intfID, onuID, uniID, allocID)
571 }
572}
573
574// RemoveGemPortIDForOnu removes the gem port id for given pon interface, onu id, uni id and gem port id
575func (RsrcMgr *OpenOltResourceMgr) RemoveGemPortIDForOnu(intfID uint32, onuID uint32, uniID uint32, gemPortID uint32) {
576 gemPortIDs := RsrcMgr.GetCurrentGEMPortIDsForOnu(intfID, onuID, uniID)
577 for i := 0; i < len(gemPortIDs); i++ {
578 if gemPortIDs[i] == gemPortID {
579 gemPortIDs = append(gemPortIDs[:i], gemPortIDs[i+1:]...)
580 break
581 }
582 }
583 err := RsrcMgr.UpdateGEMPortIDsForOnu(intfID, onuID, uniID, gemPortIDs)
584 if err != nil {
585 log.Errorf("Failed to Remove Gem Id For Onu. IntfID %d onuID %d uniID %d gemPortId %d",
586 intfID, onuID, uniID, gemPortID)
587 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530588}
589
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700590// UpdateGEMportsPonportToOnuMapOnKVStore updates onu and uni id associated with the gem port to the kv store
591// This stored information is used when packet_indication is received and we need to derive the ONU Id for which
592// the packet arrived based on the pon_intf and gemport available in the packet_indication
593func (RsrcMgr *OpenOltResourceMgr) UpdateGEMportsPonportToOnuMapOnKVStore(gemPorts []uint32, PonPort uint32,
594 onuID uint32, uniID uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530595
Girish Gowdru0c588b22019-04-23 23:24:56 -0400596 /* Update onu and uni id associated with the gem port to the kv store. */
597 var IntfGEMPortPath string
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700598 Data := fmt.Sprintf("%d %d", onuID, uniID)
599 for _, GEM := range gemPorts {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400600 IntfGEMPortPath = fmt.Sprintf("%d,%d", PonPort, GEM)
601 Val, err := json.Marshal(Data)
602 if err != nil {
603 log.Error("failed to Marshal")
604 return err
605 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700606
Girish Gowdru0c588b22019-04-23 23:24:56 -0400607 if err = RsrcMgr.KVStore.Put(IntfGEMPortPath, Val); err != nil {
608 log.Errorf("Failed to update resource %s", IntfGEMPortPath)
609 return err
610 }
611 }
612 return nil
Abhilash S.L7f17e402019-03-15 17:40:41 +0530613}
614
Gamze Abakafee36392019-10-03 11:17:24 +0000615// RemoveGEMportPonportToOnuMapOnKVStore removes the relationship between the gem port and pon port
616func (RsrcMgr *OpenOltResourceMgr) RemoveGEMportPonportToOnuMapOnKVStore(GemPort uint32, PonPort uint32) {
617 IntfGEMPortPath := fmt.Sprintf("%d,%d", PonPort, GemPort)
618 err := RsrcMgr.KVStore.Delete(IntfGEMPortPath)
619 if err != nil {
620 log.Errorf("Failed to Remove Gem port-Pon port to onu map on kv store. Gem %d PonPort %d", GemPort, PonPort)
621 }
622}
623
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700624// GetGEMPortID gets gem port id for a particular pon port, onu id and uni id and then update the resource map on
625// the KV store with the list of gemport_id allocated for the pon_intf_onu_id tuple
626func (RsrcMgr *OpenOltResourceMgr) GetGEMPortID(ponPort uint32, onuID uint32,
627 uniID uint32, NumOfPorts uint32) ([]uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530628
Girish Gowdru0c588b22019-04-23 23:24:56 -0400629 /* Get gem port id for a particular pon port, onu id
630 and uni id.
631 */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530632
Girish Gowdru0c588b22019-04-23 23:24:56 -0400633 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700634 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530635
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700636 GEMPortList := RsrcMgr.ResourceMgrs[ponPort].GetCurrentGEMPortIDsForOnu(IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400637 if GEMPortList != nil {
638 return GEMPortList, nil
639 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530640
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700641 GEMPortList, err = RsrcMgr.ResourceMgrs[ponPort].GetResourceID(ponPort,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400642 ponrmgr.GEMPORT_ID, NumOfPorts)
643 if err != nil && GEMPortList == nil {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530644 log.Errorf("Failed to get gem port id for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400645 return nil, err
646 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530647
Girish Gowdru0c588b22019-04-23 23:24:56 -0400648 // update the resource map on KV store with the list of gemport_id
649 // allocated for the pon_intf_onu_id tuple
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700650 err = RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400651 GEMPortList)
652 if err != nil {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530653 log.Errorf("Failed to update GEM ports to kv store for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400654 return nil, err
655 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700656 _ = RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(GEMPortList, ponPort,
657 onuID, uniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400658 return GEMPortList, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530659}
660
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700661// UpdateGEMPortIDsForOnu updates gemport ids on to the kv store for a given pon port, onu id and uni id
662func (RsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ponPort uint32, onuID uint32,
663 uniID uint32, GEMPortList []uint32) error {
664 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
665 return RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400666 GEMPortList)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530667
668}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700669
670// FreeonuID releases(make free) onu id for a particular pon-port
671func (RsrcMgr *OpenOltResourceMgr) FreeonuID(intfID uint32, onuID []uint32) {
672
673 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID, ponrmgr.ONU_ID, onuID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530674
Girish Gowdru0c588b22019-04-23 23:24:56 -0400675 /* Free onu id for a particular interface.*/
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700676 var IntfonuID string
677 for _, onu := range onuID {
678 IntfonuID = fmt.Sprintf("%d,%d", intfID, onu)
679 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(IntfonuID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400680 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530681}
682
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700683// FreeFlowID returns the free flow id for a given interface, onu id and uni id
Devmalya Paul495b94a2019-08-27 19:42:00 -0400684func (RsrcMgr *OpenOltResourceMgr) FreeFlowID(IntfID uint32, onuID int32,
685 uniID int32, FlowID uint32) {
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400686 var IntfONUID string
687 var err error
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530688 FlowIds := make([]uint32, 0)
689
690 FlowIds = append(FlowIds, FlowID)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700691 IntfONUID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
692 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(IntfONUID, FlowID, false)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400693 if err != nil {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530694 log.Errorw("Failed to Update flow id for", log.Fields{"intf": IntfONUID})
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400695 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700696 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(IntfONUID, FlowID)
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530697 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(IntfID, ponrmgr.FLOW_ID, FlowIds)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400698}
699
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700700// FreeFlowIDs releases the flow Ids
701func (RsrcMgr *OpenOltResourceMgr) FreeFlowIDs(IntfID uint32, onuID uint32,
702 uniID uint32, FlowID []uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530703
Girish Gowdru0c588b22019-04-23 23:24:56 -0400704 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(IntfID, ponrmgr.FLOW_ID, FlowID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530705
Abhilash S.L8ee90712019-04-29 16:24:22 +0530706 var IntfOnuIDUniID string
Girish Gowdru0c588b22019-04-23 23:24:56 -0400707 var err error
708 for _, flow := range FlowID {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700709 IntfOnuIDUniID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530710 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(IntfOnuIDUniID, flow, false)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400711 if err != nil {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530712 log.Errorw("Failed to Update flow id for", log.Fields{"intf": IntfOnuIDUniID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400713 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530714 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(IntfOnuIDUniID, flow)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400715 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530716}
717
Gamze Abakafee36392019-10-03 11:17:24 +0000718// FreeAllocID frees AllocID on the PON resource pool and also frees the allocID association
719// for the given OLT device.
720func (RsrcMgr *OpenOltResourceMgr) FreeAllocID(IntfID uint32, onuID uint32,
721 uniID uint32, allocID uint32) {
722 RsrcMgr.RemoveAllocIDForOnu(IntfID, onuID, uniID, allocID)
723 allocIDs := make([]uint32, 0)
724 allocIDs = append(allocIDs, allocID)
725 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(IntfID, ponrmgr.ALLOC_ID, allocIDs)
726}
727
728// FreeGemPortID frees GemPortID on the PON resource pool and also frees the gemPortID association
729// for the given OLT device.
730func (RsrcMgr *OpenOltResourceMgr) FreeGemPortID(IntfID uint32, onuID uint32,
731 uniID uint32, gemPortID uint32) {
732 RsrcMgr.RemoveGemPortIDForOnu(IntfID, onuID, uniID, gemPortID)
733 gemPortIDs := make([]uint32, 0)
734 gemPortIDs = append(gemPortIDs, gemPortID)
735 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(IntfID, ponrmgr.GEMPORT_ID, gemPortIDs)
736}
737
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700738// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id, and the clears the
739// resource map and the onuID associated with (pon_intf_id, gemport_id) tuple,
740func (RsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(intfID uint32, onuID uint32, uniID uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530741
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700742 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530743
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700744 AllocIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530745
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700746 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400747 ponrmgr.ALLOC_ID,
748 AllocIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530749
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700750 GEMPortIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(IntfOnuIDUniID)
751 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400752 ponrmgr.GEMPORT_ID,
753 GEMPortIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530754
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700755 FlowIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentFlowIDsForOnu(IntfOnuIDUniID)
756 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400757 ponrmgr.FLOW_ID,
758 FlowIDs)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400759 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700760 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400761 // Clear the ONU Id associated with the (pon_intf_id, gemport_id) tuple.
762 for _, GEM := range GEMPortIDs {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700763 _ = RsrcMgr.KVStore.Delete(fmt.Sprintf("%d,%d", intfID, GEM))
Girish Gowdru0c588b22019-04-23 23:24:56 -0400764 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530765}
766
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700767// IsFlowCookieOnKVStore checks if the given flow cookie is present on the kv store
768// Returns true if the flow cookie is found, otherwise it returns false
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530769func (RsrcMgr *OpenOltResourceMgr) IsFlowCookieOnKVStore(ponIntfID uint32, onuID int32, uniID int32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700770 flowStoreCookie uint64) bool {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530771
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700772 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
773 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(FlowPath)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400774 if FlowIDs != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700775 log.Debugw("Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "onuID": onuID, "uniID": uniID, "KVpath": FlowPath})
776 for _, flowID := range FlowIDs {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530777 FlowInfo := RsrcMgr.GetFlowIDInfo(ponIntfID, int32(onuID), int32(uniID), uint32(flowID))
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400778 if FlowInfo != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700779 log.Debugw("Found flows", log.Fields{"flows": *FlowInfo, "flowId": flowID})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400780 for _, Info := range *FlowInfo {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700781 if Info.FlowStoreCookie == flowStoreCookie {
782 log.Debug("Found flow matching with flowStore cookie", log.Fields{"flowId": flowID, "flowStoreCookie": flowStoreCookie})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400783 return true
784 }
785 }
786 }
787 }
788 }
789 return false
790}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400791
salmansiddiqui7ac62132019-08-22 03:58:50 +0000792// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000793// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
794func (RsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(IntfID uint32, OnuID uint32, UniID uint32) []uint32 {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000795 Path := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000796 var Data []uint32
salmansiddiqui7ac62132019-08-22 03:58:50 +0000797 Value, err := RsrcMgr.KVStore.Get(Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400798 if err == nil {
799 if Value != nil {
800 Val, err := kvstore.ToByte(Value.Value)
801 if err != nil {
802 log.Errorw("Failed to convert into byte array", log.Fields{"error": err})
803 return Data
804 }
805 if err = json.Unmarshal(Val, &Data); err != nil {
806 log.Error("Failed to unmarshal", log.Fields{"error": err})
807 return Data
808 }
809 }
810 } else {
811 log.Errorf("Failed to get TP id from kvstore for path %s", Path)
812 }
813 log.Debugf("Getting TP id %d from path %s", Data, Path)
814 return Data
815
816}
817
Gamze Abakafee36392019-10-03 11:17:24 +0000818// RemoveTechProfileIDsForOnu deletes all tech profile ids from the KV-Store for the given onu based on the path
819// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
820func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDsForOnu(IntfID uint32, OnuID uint32, UniID uint32) error {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000821 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
822 if err := RsrcMgr.KVStore.Delete(IntfOnuUniID); err != nil {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530823 log.Errorw("Failed to delete techprofile id resource in KV store", log.Fields{"path": IntfOnuUniID})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400824 return err
825 }
826 return nil
827}
828
Gamze Abakafee36392019-10-03 11:17:24 +0000829// RemoveTechProfileIDForOnu deletes a specific tech profile id from the KV-Store for the given onu based on the path
830// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
831func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(IntfID uint32, OnuID uint32, UniID uint32, TpID uint32) error {
832 tpIDList := RsrcMgr.GetTechProfileIDForOnu(IntfID, OnuID, UniID)
833 for i, tpIDInList := range tpIDList {
834 if tpIDInList == TpID {
835 tpIDList = append(tpIDList[:i], tpIDList[i+1:]...)
836 }
837 }
838 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
839 Value, err := json.Marshal(tpIDList)
840 if err != nil {
841 log.Error("failed to Marshal")
842 return err
843 }
844 if err = RsrcMgr.KVStore.Put(IntfOnuUniID, Value); err != nil {
845 log.Errorf("Failed to update resource %s", IntfOnuUniID)
846 return err
847 }
848 return err
849}
850
851// UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
852// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
salmansiddiqui7ac62132019-08-22 03:58:50 +0000853func (RsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(IntfID uint32, OnuID uint32,
854 UniID uint32, TpID uint32) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400855 var Value []byte
856 var err error
857
salmansiddiqui7ac62132019-08-22 03:58:50 +0000858 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000859
860 tpIDList := RsrcMgr.GetTechProfileIDForOnu(IntfID, OnuID, UniID)
861 for _, value := range tpIDList {
862 if value == TpID {
863 log.Debugf("TpID %d is already in tpIdList for the path %s", TpID, IntfOnuUniID)
864 return err
865 }
866 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000867 log.Debugf("updating tp id %d on path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000868 tpIDList = append(tpIDList, TpID)
869 Value, err = json.Marshal(tpIDList)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400870 if err != nil {
871 log.Error("failed to Marshal")
872 return err
873 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000874 if err = RsrcMgr.KVStore.Put(IntfOnuUniID, Value); err != nil {
875 log.Errorf("Failed to update resource %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400876 return err
877 }
878 return err
879}
880
salmansiddiqui7ac62132019-08-22 03:58:50 +0000881// UpdateMeterIDForOnu updates the meter id in the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000882// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
salmansiddiqui7ac62132019-08-22 03:58:50 +0000883func (RsrcMgr *OpenOltResourceMgr) UpdateMeterIDForOnu(Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000884 UniID uint32, TpID uint32, MeterConfig *ofp.OfpMeterConfig) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400885 var Value []byte
886 var err error
887
Gamze Abakafee36392019-10-03 11:17:24 +0000888 IntfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400889 Value, err = json.Marshal(*MeterConfig)
890 if err != nil {
891 log.Error("failed to Marshal meter config")
892 return err
893 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000894 if err = RsrcMgr.KVStore.Put(IntfOnuUniID, Value); err != nil {
895 log.Errorf("Failed to store meter into KV store %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400896 return err
897 }
898 return err
899}
900
Gamze Abakafee36392019-10-03 11:17:24 +0000901// GetMeterIDForOnu fetches the meter id from the kv store for the given onu based on the path
902// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
903func (RsrcMgr *OpenOltResourceMgr) GetMeterIDForOnu(Direction string, IntfID uint32, OnuID uint32,
904 UniID uint32, TpID uint32) (*ofp.OfpMeterConfig, error) {
905 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400906 var meterConfig ofp.OfpMeterConfig
salmansiddiqui7ac62132019-08-22 03:58:50 +0000907 Value, err := RsrcMgr.KVStore.Get(Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400908 if err == nil {
909 if Value != nil {
910 log.Debug("Found meter in KV store", log.Fields{"Direction": Direction})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000911 Val, er := kvstore.ToByte(Value.Value)
912 if er != nil {
913 log.Errorw("Failed to convert into byte array", log.Fields{"error": er})
914 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400915 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000916 if er = json.Unmarshal(Val, &meterConfig); er != nil {
917 log.Error("Failed to unmarshal meterconfig", log.Fields{"error": er})
918 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400919 }
920 } else {
921 log.Debug("meter-does-not-exists-in-KVStore")
922 return nil, err
923 }
924 } else {
925 log.Errorf("Failed to get Meter config from kvstore for path %s", Path)
926
927 }
928 return &meterConfig, err
929}
930
Gamze Abakafee36392019-10-03 11:17:24 +0000931// RemoveMeterIDForOnu deletes the meter id from the kV-Store for the given onu based on the path
932// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
933func (RsrcMgr *OpenOltResourceMgr) RemoveMeterIDForOnu(Direction string, IntfID uint32, OnuID uint32,
934 UniID uint32, TpID uint32) error {
935 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
salmansiddiqui7ac62132019-08-22 03:58:50 +0000936 if err := RsrcMgr.KVStore.Delete(Path); err != nil {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400937 log.Errorf("Failed to delete meter id %s from kvstore ", Path)
938 return err
939 }
940 return nil
941}
salmansiddiqui7ac62132019-08-22 03:58:50 +0000942
943func getFlowIDFromFlowInfo(FlowInfo *[]FlowInfo, flowID, gemportID uint32, flowStoreCookie uint64, flowCategory string, vlanPcp ...uint32) error {
944 if FlowInfo != nil {
945 for _, Info := range *FlowInfo {
946 if int32(gemportID) == Info.Flow.GemportId && flowCategory != "" && Info.FlowCategory == flowCategory {
947 log.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
948 if Info.FlowCategory == "HSIA_FLOW" && Info.Flow.Classifier.OPbits == vlanPcp[0] {
949 log.Debug("Found matching vlan pcp ", log.Fields{"flowId": flowID, "Vlanpcp": vlanPcp[0]})
950 return nil
951 }
952 }
953 if int32(gemportID) == Info.Flow.GemportId && flowStoreCookie != 0 && Info.FlowStoreCookie == flowStoreCookie {
954 if flowCategory != "" && Info.FlowCategory == flowCategory {
955 log.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
956 return nil
957 }
958 }
959 }
960 }
Gamze Abakafee36392019-10-03 11:17:24 +0000961 log.Debugw("the flow can be related to a different service", log.Fields{"flow_info": FlowInfo})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000962 return errors.New("invalid flow-info")
963}
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530964
965//AddGemToOnuGemInfo adds gemport to onugem info kvstore
966func (RsrcMgr *OpenOltResourceMgr) AddGemToOnuGemInfo(intfID uint32, onuID uint32, gemPort uint32) error {
967 var onuGemData []OnuGemInfo
968 var err error
969
970 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(intfID, &onuGemData); err != nil {
971 log.Errorf("failed to get onuifo for intfid %d", intfID)
972 return err
973 }
974 if len(onuGemData) == 0 {
975 log.Errorw("failed to ger Onuid info ", log.Fields{"intfid": intfID, "onuid": onuID})
976 return err
977 }
978
979 for idx, onugem := range onuGemData {
980 if onugem.OnuID == onuID {
981 for _, gem := range onuGemData[idx].GemPorts {
982 if gem == gemPort {
983 log.Debugw("Gem already present in onugem info, skpping addition", log.Fields{"gem": gem})
984 return nil
985 }
986 }
987 log.Debugw("Added gem to onugem info", log.Fields{"gem": gemPort})
988 onuGemData[idx].GemPorts = append(onuGemData[idx].GemPorts, gemPort)
989 break
990 }
991 }
992 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(intfID, onuGemData)
993 if err != nil {
994 log.Error("Failed to add onugem to kv store")
995 return err
996 }
997 return err
998}
999
1000//GetOnuGemInfo gets onu gem info from the kvstore per interface
1001func (RsrcMgr *OpenOltResourceMgr) GetOnuGemInfo(IntfID uint32) ([]OnuGemInfo, error) {
1002 var onuGemData []OnuGemInfo
1003
1004 if err := RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(IntfID, &onuGemData); err != nil {
1005 log.Errorf("failed to get onuifo for intfid %d", IntfID)
1006 return nil, err
1007 }
1008
1009 return onuGemData, nil
1010}
1011
1012// AddOnuInfo adds onu info on to the kvstore per interface
1013func (RsrcMgr *OpenOltResourceMgr) AddOnuInfo(IntfID uint32, onuGem OnuGemInfo) error {
1014 var onuGemData []OnuGemInfo
1015 var err error
1016
1017 if err = RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(IntfID, &onuGemData); err != nil {
1018 log.Errorf("failed to get onuifo for intfid %d", IntfID)
1019 return err
1020 }
1021 onuGemData = append(onuGemData, onuGem)
1022 err = RsrcMgr.ResourceMgrs[IntfID].AddOnuGemInfo(IntfID, onuGemData)
1023 if err != nil {
1024 log.Error("Failed to add onugem to kv store")
1025 return err
1026 }
1027
1028 log.Debugw("added onu to onugeminfo", log.Fields{"intf": IntfID, "onugem": onuGem})
1029 return err
1030}
1031
1032// UpdateOnuInfo updates Onuinfo on the kvstore per interface
1033func (RsrcMgr *OpenOltResourceMgr) UpdateOnuInfo(IntfID uint32, onuGem []OnuGemInfo) error {
1034 var onuGemData []OnuGemInfo
1035 var err error
1036
1037 err = RsrcMgr.ResourceMgrs[IntfID].AddOnuGemInfo(IntfID, onuGemData)
1038 if err != nil {
1039 log.Error("Failed to add onugem to kv store")
1040 return err
1041 }
1042
1043 log.Debugw("updated onugeminfo", log.Fields{"intf": IntfID, "onugem": onuGem})
1044 return err
1045}
1046
1047// AddUniPortToOnuInfo adds uni port to the onuinfo kvstore. check if the uni is already present if not update the kv store.
1048func (RsrcMgr *OpenOltResourceMgr) AddUniPortToOnuInfo(intfID uint32, onuID uint32, portNo uint32) {
1049 var onuGemData []OnuGemInfo
1050 var err error
1051
1052 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(intfID, &onuGemData); err != nil {
1053 log.Errorf("failed to get onuifo for intfid %d", intfID)
1054 return
1055 }
1056 for idx, onu := range onuGemData {
1057 if onu.OnuID == onuID {
1058 for _, uni := range onu.UniPorts {
1059 if uni == portNo {
1060 log.Debugw("uni already present in onugem info", log.Fields{"uni": portNo})
1061 return
1062 }
1063 }
1064 onuGemData[idx].UniPorts = append(onuGemData[idx].UniPorts, portNo)
1065 break
1066 }
1067 }
1068 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(intfID, onuGemData)
1069 if err != nil {
1070 log.Errorw("Failed to add uin port in onugem to kv store", log.Fields{"uni": portNo})
1071 return
1072 }
1073 return
1074}
1075
1076//UpdateGemPortForPktIn updates gemport for pkt in path to kvstore, path being intfid, onuid, portno
1077func (RsrcMgr *OpenOltResourceMgr) UpdateGemPortForPktIn(pktIn PacketInInfoKey, gemPort uint32) {
1078
1079 path := fmt.Sprintf(OnuPacketINPath, pktIn.IntfID, pktIn.OnuID, pktIn.LogicalPort)
1080 Value, err := json.Marshal(gemPort)
1081 if err != nil {
1082 log.Error("Failed to marshal data")
1083 return
1084 }
1085 if err = RsrcMgr.KVStore.Put(path, Value); err != nil {
1086 log.Errorw("Failed to put to kvstore", log.Fields{"path": path, "value": gemPort})
1087 return
1088 }
1089 log.Debugw("added gem packet in successfully", log.Fields{"path": path, "gem": gemPort})
1090
1091 return
1092}
1093
1094// GetGemPortFromOnuPktIn gets the gem port from onu pkt in path, path being intfid, onuid, portno
1095func (RsrcMgr *OpenOltResourceMgr) GetGemPortFromOnuPktIn(intfID uint32, onuID uint32, logicalPort uint32) (uint32, error) {
1096
1097 var Val []byte
1098 var gemPort uint32
1099
1100 path := fmt.Sprintf(OnuPacketINPath, intfID, onuID, logicalPort)
1101
1102 value, err := RsrcMgr.KVStore.Get(path)
1103 if err != nil {
1104 log.Errorw("Failed to get from kv store", log.Fields{"path": path})
1105 return uint32(0), err
1106 } else if value == nil {
1107 log.Debugw("No pkt in gem found", log.Fields{"path": path})
1108 return uint32(0), nil
1109 }
1110
1111 if Val, err = kvstore.ToByte(value.Value); err != nil {
1112 log.Error("Failed to convert to byte array")
1113 return uint32(0), err
1114 }
1115 if err = json.Unmarshal(Val, &gemPort); err != nil {
1116 log.Error("Failed to unmarshall")
1117 return uint32(0), err
1118 }
1119 log.Debugw("found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
1120
1121 return gemPort, nil
1122}
1123
1124// DelGemPortPktIn deletes the gemport from the pkt in path
1125func (RsrcMgr *OpenOltResourceMgr) DelGemPortPktIn(intfID uint32, onuID uint32, logicalPort uint32) error {
1126
1127 path := fmt.Sprintf(OnuPacketINPath, intfID, onuID, logicalPort)
1128 if err := RsrcMgr.KVStore.Delete(path); err != nil {
1129 log.Errorf("Falied to remove resource %s", path)
1130 return err
1131 }
1132 return nil
1133}
1134
1135// DelOnuGemInfoForIntf deletes the onugem info from kvstore per interface
1136func (RsrcMgr *OpenOltResourceMgr) DelOnuGemInfoForIntf(intfID uint32) error {
1137 if err := RsrcMgr.ResourceMgrs[intfID].DelOnuGemInfoForIntf(intfID); err != nil {
1138 log.Errorw("failed to delete onu gem info for", log.Fields{"intfid": intfID})
1139 return err
1140 }
1141 return nil
1142}
1143
1144//GetNNIFromKVStore gets NNi intfids from kvstore. path being per device
1145func (RsrcMgr *OpenOltResourceMgr) GetNNIFromKVStore() ([]uint32, error) {
1146
1147 var nni []uint32
1148 var Val []byte
1149
1150 path := fmt.Sprintf(NnniIntfID)
1151 value, err := RsrcMgr.KVStore.Get(path)
1152 if err != nil {
1153 log.Error("failed to get data from kv store")
1154 return nil, err
1155 }
1156 if value != nil {
1157 if Val, err = kvstore.ToByte(value.Value); err != nil {
1158 log.Error("Failed to convert to byte array")
1159 return nil, err
1160 }
1161 if err = json.Unmarshal(Val, &nni); err != nil {
1162 log.Error("Failed to unmarshall")
1163 return nil, err
1164 }
1165 }
1166 return nni, err
1167}
1168
1169// AddNNIToKVStore adds Nni interfaces to kvstore, path being per device.
1170func (RsrcMgr *OpenOltResourceMgr) AddNNIToKVStore(nniIntf uint32) error {
1171 var Value []byte
1172
1173 nni, err := RsrcMgr.GetNNIFromKVStore()
1174 if err != nil {
1175 log.Error("failed to fetch nni interfaces from kv store")
1176 return err
1177 }
1178
1179 path := fmt.Sprintf(NnniIntfID)
1180 nni = append(nni, nniIntf)
1181 Value, err = json.Marshal(nni)
1182 if err != nil {
1183 log.Error("Failed to marshal data")
1184 }
1185 if err = RsrcMgr.KVStore.Put(path, Value); err != nil {
1186 log.Errorw("Failed to put to kvstore", log.Fields{"path": path, "value": Value})
1187 return err
1188 }
1189 log.Debugw("added nni to kv successfully", log.Fields{"path": path, "nni": nniIntf})
1190 return nil
1191}
1192
1193// DelNNiFromKVStore deletes nni interface list from kv store.
1194func (RsrcMgr *OpenOltResourceMgr) DelNNiFromKVStore() error {
1195
1196 path := fmt.Sprintf(NnniIntfID)
1197
1198 if err := RsrcMgr.KVStore.Delete(path); err != nil {
1199 log.Errorw("Failed to delete nni interfaces from kv store", log.Fields{"path": path})
1200 return err
1201 }
1202 return nil
1203}
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301204
1205//UpdateFlowIDsForGem updates flow id per gemport
1206func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDsForGem(intf uint32, gem uint32, flowIDs []uint32) error {
1207 var val []byte
1208 path := fmt.Sprintf(FlowIDsForGem, intf)
1209
1210 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(intf)
1211 if err != nil {
1212 log.Error("Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
1213 return err
1214 }
1215 if flowsForGem == nil {
1216 flowsForGem = make(map[uint32][]uint32)
1217 }
1218 flowsForGem[gem] = flowIDs
1219 val, err = json.Marshal(flowsForGem)
1220 if err != nil {
1221 log.Error("Failed to marshal data", log.Fields{"error": err})
1222 return err
1223 }
1224 if err = RsrcMgr.KVStore.Put(path, val); err != nil {
1225 log.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
1226 return err
1227 }
1228 log.Debugw("added flowid list for gem to kv successfully", log.Fields{"path": path, "flowidlist": flowsForGem[gem]})
1229 return nil
1230}
1231
1232//DeleteFlowIDsForGem deletes the flowID list entry per gem from kvstore.
1233func (RsrcMgr *OpenOltResourceMgr) DeleteFlowIDsForGem(intf uint32, gem uint32) {
1234 path := fmt.Sprintf(FlowIDsForGem, intf)
1235 var val []byte
1236
1237 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(intf)
1238 if err != nil {
1239 log.Error("Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
1240 return
1241 }
1242 if flowsForGem == nil {
1243 log.Error("No flowids found ", log.Fields{"intf": intf, "gemport": gem})
1244 return
1245 }
1246 // once we get the flows per gem map from kv , just delete the gem entry from the map
1247 delete(flowsForGem, gem)
1248 // once gem entry is deleted update the kv store.
1249 val, err = json.Marshal(flowsForGem)
1250 if err != nil {
1251 log.Error("Failed to marshal data", log.Fields{"error": err})
1252 return
1253 }
1254 if err = RsrcMgr.KVStore.Put(path, val); err != nil {
1255 log.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
1256 return
1257 }
1258 return
1259}
1260
1261//GetFlowIDsGemMapForInterface gets flowids per gemport and interface
1262func (RsrcMgr *OpenOltResourceMgr) GetFlowIDsGemMapForInterface(intf uint32) (map[uint32][]uint32, error) {
1263 path := fmt.Sprintf(FlowIDsForGem, intf)
1264 var flowsForGem map[uint32][]uint32
1265 var val []byte
1266
1267 value, err := RsrcMgr.KVStore.Get(path)
1268 if err != nil {
1269 log.Error("failed to get data from kv store")
1270 return nil, err
1271 }
1272 if value != nil {
1273 if val, err = kvstore.ToByte(value.Value); err != nil {
1274 log.Error("Failed to convert to byte array", log.Fields{"error": err})
1275 return nil, err
1276 }
1277 if err = json.Unmarshal(val, &flowsForGem); err != nil {
1278 log.Error("Failed to unmarshall", log.Fields{"error": err})
1279 return nil, err
1280 }
1281 }
1282 return flowsForGem, nil
1283}
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301284
1285//DeleteIntfIDGempMapPath deletes the intf id path used to store flow ids per gem to kvstore.
1286func (RsrcMgr *OpenOltResourceMgr) DeleteIntfIDGempMapPath(intf uint32) {
1287
1288 path := fmt.Sprintf(FlowIDsForGem, intf)
1289 if err := RsrcMgr.KVStore.Delete(path); err != nil {
1290 log.Errorw("Failed to delete nni interfaces from kv store", log.Fields{"path": path})
1291 return
1292 }
1293 return
1294}
1295
1296// RemoveResourceMap Clear resource map associated with (intfid, onuid, uniid) tuple.
1297func (RsrcMgr *OpenOltResourceMgr) RemoveResourceMap(intfID uint32, onuID int32, uniID int32) {
1298 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
1299 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(IntfOnuIDUniID)
1300}