blob: fda117165c29b6ba071344c067840a2a1410700e [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 var onuIDs []uint32
743 onuIDs = append(onuIDs, onuID)
744 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530745
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700746 AllocIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530747
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700748 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400749 ponrmgr.ALLOC_ID,
750 AllocIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530751
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700752 GEMPortIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(IntfOnuIDUniID)
753 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400754 ponrmgr.GEMPORT_ID,
755 GEMPortIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530756
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700757 FlowIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentFlowIDsForOnu(IntfOnuIDUniID)
758 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400759 ponrmgr.FLOW_ID,
760 FlowIDs)
Devmalya Paul495b94a2019-08-27 19:42:00 -0400761 if int32(onuID) >= 0 {
762 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
763 ponrmgr.ONU_ID,
764 onuIDs)
765 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400766 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700767 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530768
Girish Gowdru0c588b22019-04-23 23:24:56 -0400769 // Clear the ONU Id associated with the (pon_intf_id, gemport_id) tuple.
770 for _, GEM := range GEMPortIDs {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700771 _ = RsrcMgr.KVStore.Delete(fmt.Sprintf("%d,%d", intfID, GEM))
Girish Gowdru0c588b22019-04-23 23:24:56 -0400772 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530773}
774
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700775// IsFlowCookieOnKVStore checks if the given flow cookie is present on the kv store
776// Returns true if the flow cookie is found, otherwise it returns false
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530777func (RsrcMgr *OpenOltResourceMgr) IsFlowCookieOnKVStore(ponIntfID uint32, onuID int32, uniID int32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700778 flowStoreCookie uint64) bool {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530779
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700780 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
781 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(FlowPath)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400782 if FlowIDs != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700783 log.Debugw("Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "onuID": onuID, "uniID": uniID, "KVpath": FlowPath})
784 for _, flowID := range FlowIDs {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530785 FlowInfo := RsrcMgr.GetFlowIDInfo(ponIntfID, int32(onuID), int32(uniID), uint32(flowID))
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400786 if FlowInfo != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700787 log.Debugw("Found flows", log.Fields{"flows": *FlowInfo, "flowId": flowID})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400788 for _, Info := range *FlowInfo {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700789 if Info.FlowStoreCookie == flowStoreCookie {
790 log.Debug("Found flow matching with flowStore cookie", log.Fields{"flowId": flowID, "flowStoreCookie": flowStoreCookie})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400791 return true
792 }
793 }
794 }
795 }
796 }
797 return false
798}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400799
salmansiddiqui7ac62132019-08-22 03:58:50 +0000800// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000801// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
802func (RsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(IntfID uint32, OnuID uint32, UniID uint32) []uint32 {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000803 Path := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000804 var Data []uint32
salmansiddiqui7ac62132019-08-22 03:58:50 +0000805 Value, err := RsrcMgr.KVStore.Get(Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400806 if err == nil {
807 if Value != nil {
808 Val, err := kvstore.ToByte(Value.Value)
809 if err != nil {
810 log.Errorw("Failed to convert into byte array", log.Fields{"error": err})
811 return Data
812 }
813 if err = json.Unmarshal(Val, &Data); err != nil {
814 log.Error("Failed to unmarshal", log.Fields{"error": err})
815 return Data
816 }
817 }
818 } else {
819 log.Errorf("Failed to get TP id from kvstore for path %s", Path)
820 }
821 log.Debugf("Getting TP id %d from path %s", Data, Path)
822 return Data
823
824}
825
Gamze Abakafee36392019-10-03 11:17:24 +0000826// RemoveTechProfileIDsForOnu deletes all tech profile ids from the KV-Store for the given onu based on the path
827// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
828func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDsForOnu(IntfID uint32, OnuID uint32, UniID uint32) error {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000829 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
830 if err := RsrcMgr.KVStore.Delete(IntfOnuUniID); err != nil {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530831 log.Errorw("Failed to delete techprofile id resource in KV store", log.Fields{"path": IntfOnuUniID})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400832 return err
833 }
834 return nil
835}
836
Gamze Abakafee36392019-10-03 11:17:24 +0000837// RemoveTechProfileIDForOnu deletes a specific tech profile id from the KV-Store for the given onu based on the path
838// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
839func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(IntfID uint32, OnuID uint32, UniID uint32, TpID uint32) error {
840 tpIDList := RsrcMgr.GetTechProfileIDForOnu(IntfID, OnuID, UniID)
841 for i, tpIDInList := range tpIDList {
842 if tpIDInList == TpID {
843 tpIDList = append(tpIDList[:i], tpIDList[i+1:]...)
844 }
845 }
846 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
847 Value, err := json.Marshal(tpIDList)
848 if err != nil {
849 log.Error("failed to Marshal")
850 return err
851 }
852 if err = RsrcMgr.KVStore.Put(IntfOnuUniID, Value); err != nil {
853 log.Errorf("Failed to update resource %s", IntfOnuUniID)
854 return err
855 }
856 return err
857}
858
859// UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
860// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
salmansiddiqui7ac62132019-08-22 03:58:50 +0000861func (RsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(IntfID uint32, OnuID uint32,
862 UniID uint32, TpID uint32) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400863 var Value []byte
864 var err error
865
salmansiddiqui7ac62132019-08-22 03:58:50 +0000866 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000867
868 tpIDList := RsrcMgr.GetTechProfileIDForOnu(IntfID, OnuID, UniID)
869 for _, value := range tpIDList {
870 if value == TpID {
871 log.Debugf("TpID %d is already in tpIdList for the path %s", TpID, IntfOnuUniID)
872 return err
873 }
874 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000875 log.Debugf("updating tp id %d on path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000876 tpIDList = append(tpIDList, TpID)
877 Value, err = json.Marshal(tpIDList)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400878 if err != nil {
879 log.Error("failed to Marshal")
880 return err
881 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000882 if err = RsrcMgr.KVStore.Put(IntfOnuUniID, Value); err != nil {
883 log.Errorf("Failed to update resource %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400884 return err
885 }
886 return err
887}
888
salmansiddiqui7ac62132019-08-22 03:58:50 +0000889// UpdateMeterIDForOnu updates the meter id in the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000890// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
salmansiddiqui7ac62132019-08-22 03:58:50 +0000891func (RsrcMgr *OpenOltResourceMgr) UpdateMeterIDForOnu(Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000892 UniID uint32, TpID uint32, MeterConfig *ofp.OfpMeterConfig) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400893 var Value []byte
894 var err error
895
Gamze Abakafee36392019-10-03 11:17:24 +0000896 IntfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400897 Value, err = json.Marshal(*MeterConfig)
898 if err != nil {
899 log.Error("failed to Marshal meter config")
900 return err
901 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000902 if err = RsrcMgr.KVStore.Put(IntfOnuUniID, Value); err != nil {
903 log.Errorf("Failed to store meter into KV store %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400904 return err
905 }
906 return err
907}
908
Gamze Abakafee36392019-10-03 11:17:24 +0000909// GetMeterIDForOnu fetches the meter id from the kv store for the given onu based on the path
910// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
911func (RsrcMgr *OpenOltResourceMgr) GetMeterIDForOnu(Direction string, IntfID uint32, OnuID uint32,
912 UniID uint32, TpID uint32) (*ofp.OfpMeterConfig, error) {
913 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400914 var meterConfig ofp.OfpMeterConfig
salmansiddiqui7ac62132019-08-22 03:58:50 +0000915 Value, err := RsrcMgr.KVStore.Get(Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400916 if err == nil {
917 if Value != nil {
918 log.Debug("Found meter in KV store", log.Fields{"Direction": Direction})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000919 Val, er := kvstore.ToByte(Value.Value)
920 if er != nil {
921 log.Errorw("Failed to convert into byte array", log.Fields{"error": er})
922 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400923 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000924 if er = json.Unmarshal(Val, &meterConfig); er != nil {
925 log.Error("Failed to unmarshal meterconfig", log.Fields{"error": er})
926 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400927 }
928 } else {
929 log.Debug("meter-does-not-exists-in-KVStore")
930 return nil, err
931 }
932 } else {
933 log.Errorf("Failed to get Meter config from kvstore for path %s", Path)
934
935 }
936 return &meterConfig, err
937}
938
Gamze Abakafee36392019-10-03 11:17:24 +0000939// RemoveMeterIDForOnu deletes the meter id from the kV-Store for the given onu based on the path
940// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
941func (RsrcMgr *OpenOltResourceMgr) RemoveMeterIDForOnu(Direction string, IntfID uint32, OnuID uint32,
942 UniID uint32, TpID uint32) error {
943 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
salmansiddiqui7ac62132019-08-22 03:58:50 +0000944 if err := RsrcMgr.KVStore.Delete(Path); err != nil {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400945 log.Errorf("Failed to delete meter id %s from kvstore ", Path)
946 return err
947 }
948 return nil
949}
salmansiddiqui7ac62132019-08-22 03:58:50 +0000950
951func getFlowIDFromFlowInfo(FlowInfo *[]FlowInfo, flowID, gemportID uint32, flowStoreCookie uint64, flowCategory string, vlanPcp ...uint32) error {
952 if FlowInfo != nil {
953 for _, Info := range *FlowInfo {
954 if int32(gemportID) == Info.Flow.GemportId && flowCategory != "" && Info.FlowCategory == flowCategory {
955 log.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
956 if Info.FlowCategory == "HSIA_FLOW" && Info.Flow.Classifier.OPbits == vlanPcp[0] {
957 log.Debug("Found matching vlan pcp ", log.Fields{"flowId": flowID, "Vlanpcp": vlanPcp[0]})
958 return nil
959 }
960 }
961 if int32(gemportID) == Info.Flow.GemportId && flowStoreCookie != 0 && Info.FlowStoreCookie == flowStoreCookie {
962 if flowCategory != "" && Info.FlowCategory == flowCategory {
963 log.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
964 return nil
965 }
966 }
967 }
968 }
Gamze Abakafee36392019-10-03 11:17:24 +0000969 log.Debugw("the flow can be related to a different service", log.Fields{"flow_info": FlowInfo})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000970 return errors.New("invalid flow-info")
971}
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530972
973//AddGemToOnuGemInfo adds gemport to onugem info kvstore
974func (RsrcMgr *OpenOltResourceMgr) AddGemToOnuGemInfo(intfID uint32, onuID uint32, gemPort uint32) error {
975 var onuGemData []OnuGemInfo
976 var err error
977
978 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(intfID, &onuGemData); err != nil {
979 log.Errorf("failed to get onuifo for intfid %d", intfID)
980 return err
981 }
982 if len(onuGemData) == 0 {
983 log.Errorw("failed to ger Onuid info ", log.Fields{"intfid": intfID, "onuid": onuID})
984 return err
985 }
986
987 for idx, onugem := range onuGemData {
988 if onugem.OnuID == onuID {
989 for _, gem := range onuGemData[idx].GemPorts {
990 if gem == gemPort {
991 log.Debugw("Gem already present in onugem info, skpping addition", log.Fields{"gem": gem})
992 return nil
993 }
994 }
995 log.Debugw("Added gem to onugem info", log.Fields{"gem": gemPort})
996 onuGemData[idx].GemPorts = append(onuGemData[idx].GemPorts, gemPort)
997 break
998 }
999 }
1000 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(intfID, onuGemData)
1001 if err != nil {
1002 log.Error("Failed to add onugem to kv store")
1003 return err
1004 }
1005 return err
1006}
1007
1008//GetOnuGemInfo gets onu gem info from the kvstore per interface
1009func (RsrcMgr *OpenOltResourceMgr) GetOnuGemInfo(IntfID uint32) ([]OnuGemInfo, error) {
1010 var onuGemData []OnuGemInfo
1011
1012 if err := RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(IntfID, &onuGemData); err != nil {
1013 log.Errorf("failed to get onuifo for intfid %d", IntfID)
1014 return nil, err
1015 }
1016
1017 return onuGemData, nil
1018}
1019
1020// AddOnuInfo adds onu info on to the kvstore per interface
1021func (RsrcMgr *OpenOltResourceMgr) AddOnuInfo(IntfID uint32, onuGem OnuGemInfo) error {
1022 var onuGemData []OnuGemInfo
1023 var err error
1024
1025 if err = RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(IntfID, &onuGemData); err != nil {
1026 log.Errorf("failed to get onuifo for intfid %d", IntfID)
1027 return err
1028 }
1029 onuGemData = append(onuGemData, onuGem)
1030 err = RsrcMgr.ResourceMgrs[IntfID].AddOnuGemInfo(IntfID, onuGemData)
1031 if err != nil {
1032 log.Error("Failed to add onugem to kv store")
1033 return err
1034 }
1035
1036 log.Debugw("added onu to onugeminfo", log.Fields{"intf": IntfID, "onugem": onuGem})
1037 return err
1038}
1039
1040// UpdateOnuInfo updates Onuinfo on the kvstore per interface
1041func (RsrcMgr *OpenOltResourceMgr) UpdateOnuInfo(IntfID uint32, onuGem []OnuGemInfo) error {
1042 var onuGemData []OnuGemInfo
1043 var err error
1044
1045 err = RsrcMgr.ResourceMgrs[IntfID].AddOnuGemInfo(IntfID, onuGemData)
1046 if err != nil {
1047 log.Error("Failed to add onugem to kv store")
1048 return err
1049 }
1050
1051 log.Debugw("updated onugeminfo", log.Fields{"intf": IntfID, "onugem": onuGem})
1052 return err
1053}
1054
1055// AddUniPortToOnuInfo adds uni port to the onuinfo kvstore. check if the uni is already present if not update the kv store.
1056func (RsrcMgr *OpenOltResourceMgr) AddUniPortToOnuInfo(intfID uint32, onuID uint32, portNo uint32) {
1057 var onuGemData []OnuGemInfo
1058 var err error
1059
1060 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(intfID, &onuGemData); err != nil {
1061 log.Errorf("failed to get onuifo for intfid %d", intfID)
1062 return
1063 }
1064 for idx, onu := range onuGemData {
1065 if onu.OnuID == onuID {
1066 for _, uni := range onu.UniPorts {
1067 if uni == portNo {
1068 log.Debugw("uni already present in onugem info", log.Fields{"uni": portNo})
1069 return
1070 }
1071 }
1072 onuGemData[idx].UniPorts = append(onuGemData[idx].UniPorts, portNo)
1073 break
1074 }
1075 }
1076 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(intfID, onuGemData)
1077 if err != nil {
1078 log.Errorw("Failed to add uin port in onugem to kv store", log.Fields{"uni": portNo})
1079 return
1080 }
1081 return
1082}
1083
1084//UpdateGemPortForPktIn updates gemport for pkt in path to kvstore, path being intfid, onuid, portno
1085func (RsrcMgr *OpenOltResourceMgr) UpdateGemPortForPktIn(pktIn PacketInInfoKey, gemPort uint32) {
1086
1087 path := fmt.Sprintf(OnuPacketINPath, pktIn.IntfID, pktIn.OnuID, pktIn.LogicalPort)
1088 Value, err := json.Marshal(gemPort)
1089 if err != nil {
1090 log.Error("Failed to marshal data")
1091 return
1092 }
1093 if err = RsrcMgr.KVStore.Put(path, Value); err != nil {
1094 log.Errorw("Failed to put to kvstore", log.Fields{"path": path, "value": gemPort})
1095 return
1096 }
1097 log.Debugw("added gem packet in successfully", log.Fields{"path": path, "gem": gemPort})
1098
1099 return
1100}
1101
1102// GetGemPortFromOnuPktIn gets the gem port from onu pkt in path, path being intfid, onuid, portno
1103func (RsrcMgr *OpenOltResourceMgr) GetGemPortFromOnuPktIn(intfID uint32, onuID uint32, logicalPort uint32) (uint32, error) {
1104
1105 var Val []byte
1106 var gemPort uint32
1107
1108 path := fmt.Sprintf(OnuPacketINPath, intfID, onuID, logicalPort)
1109
1110 value, err := RsrcMgr.KVStore.Get(path)
1111 if err != nil {
1112 log.Errorw("Failed to get from kv store", log.Fields{"path": path})
1113 return uint32(0), err
1114 } else if value == nil {
1115 log.Debugw("No pkt in gem found", log.Fields{"path": path})
1116 return uint32(0), nil
1117 }
1118
1119 if Val, err = kvstore.ToByte(value.Value); err != nil {
1120 log.Error("Failed to convert to byte array")
1121 return uint32(0), err
1122 }
1123 if err = json.Unmarshal(Val, &gemPort); err != nil {
1124 log.Error("Failed to unmarshall")
1125 return uint32(0), err
1126 }
1127 log.Debugw("found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
1128
1129 return gemPort, nil
1130}
1131
1132// DelGemPortPktIn deletes the gemport from the pkt in path
1133func (RsrcMgr *OpenOltResourceMgr) DelGemPortPktIn(intfID uint32, onuID uint32, logicalPort uint32) error {
1134
1135 path := fmt.Sprintf(OnuPacketINPath, intfID, onuID, logicalPort)
1136 if err := RsrcMgr.KVStore.Delete(path); err != nil {
1137 log.Errorf("Falied to remove resource %s", path)
1138 return err
1139 }
1140 return nil
1141}
1142
1143// DelOnuGemInfoForIntf deletes the onugem info from kvstore per interface
1144func (RsrcMgr *OpenOltResourceMgr) DelOnuGemInfoForIntf(intfID uint32) error {
1145 if err := RsrcMgr.ResourceMgrs[intfID].DelOnuGemInfoForIntf(intfID); err != nil {
1146 log.Errorw("failed to delete onu gem info for", log.Fields{"intfid": intfID})
1147 return err
1148 }
1149 return nil
1150}
1151
1152//GetNNIFromKVStore gets NNi intfids from kvstore. path being per device
1153func (RsrcMgr *OpenOltResourceMgr) GetNNIFromKVStore() ([]uint32, error) {
1154
1155 var nni []uint32
1156 var Val []byte
1157
1158 path := fmt.Sprintf(NnniIntfID)
1159 value, err := RsrcMgr.KVStore.Get(path)
1160 if err != nil {
1161 log.Error("failed to get data from kv store")
1162 return nil, err
1163 }
1164 if value != nil {
1165 if Val, err = kvstore.ToByte(value.Value); err != nil {
1166 log.Error("Failed to convert to byte array")
1167 return nil, err
1168 }
1169 if err = json.Unmarshal(Val, &nni); err != nil {
1170 log.Error("Failed to unmarshall")
1171 return nil, err
1172 }
1173 }
1174 return nni, err
1175}
1176
1177// AddNNIToKVStore adds Nni interfaces to kvstore, path being per device.
1178func (RsrcMgr *OpenOltResourceMgr) AddNNIToKVStore(nniIntf uint32) error {
1179 var Value []byte
1180
1181 nni, err := RsrcMgr.GetNNIFromKVStore()
1182 if err != nil {
1183 log.Error("failed to fetch nni interfaces from kv store")
1184 return err
1185 }
1186
1187 path := fmt.Sprintf(NnniIntfID)
1188 nni = append(nni, nniIntf)
1189 Value, err = json.Marshal(nni)
1190 if err != nil {
1191 log.Error("Failed to marshal data")
1192 }
1193 if err = RsrcMgr.KVStore.Put(path, Value); err != nil {
1194 log.Errorw("Failed to put to kvstore", log.Fields{"path": path, "value": Value})
1195 return err
1196 }
1197 log.Debugw("added nni to kv successfully", log.Fields{"path": path, "nni": nniIntf})
1198 return nil
1199}
1200
1201// DelNNiFromKVStore deletes nni interface list from kv store.
1202func (RsrcMgr *OpenOltResourceMgr) DelNNiFromKVStore() error {
1203
1204 path := fmt.Sprintf(NnniIntfID)
1205
1206 if err := RsrcMgr.KVStore.Delete(path); err != nil {
1207 log.Errorw("Failed to delete nni interfaces from kv store", log.Fields{"path": path})
1208 return err
1209 }
1210 return nil
1211}
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301212
1213//UpdateFlowIDsForGem updates flow id per gemport
1214func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDsForGem(intf uint32, gem uint32, flowIDs []uint32) error {
1215 var val []byte
1216 path := fmt.Sprintf(FlowIDsForGem, intf)
1217
1218 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(intf)
1219 if err != nil {
1220 log.Error("Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
1221 return err
1222 }
1223 if flowsForGem == nil {
1224 flowsForGem = make(map[uint32][]uint32)
1225 }
1226 flowsForGem[gem] = flowIDs
1227 val, err = json.Marshal(flowsForGem)
1228 if err != nil {
1229 log.Error("Failed to marshal data", log.Fields{"error": err})
1230 return err
1231 }
1232 if err = RsrcMgr.KVStore.Put(path, val); err != nil {
1233 log.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
1234 return err
1235 }
1236 log.Debugw("added flowid list for gem to kv successfully", log.Fields{"path": path, "flowidlist": flowsForGem[gem]})
1237 return nil
1238}
1239
1240//DeleteFlowIDsForGem deletes the flowID list entry per gem from kvstore.
1241func (RsrcMgr *OpenOltResourceMgr) DeleteFlowIDsForGem(intf uint32, gem uint32) {
1242 path := fmt.Sprintf(FlowIDsForGem, intf)
1243 var val []byte
1244
1245 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(intf)
1246 if err != nil {
1247 log.Error("Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
1248 return
1249 }
1250 if flowsForGem == nil {
1251 log.Error("No flowids found ", log.Fields{"intf": intf, "gemport": gem})
1252 return
1253 }
1254 // once we get the flows per gem map from kv , just delete the gem entry from the map
1255 delete(flowsForGem, gem)
1256 // once gem entry is deleted update the kv store.
1257 val, err = json.Marshal(flowsForGem)
1258 if err != nil {
1259 log.Error("Failed to marshal data", log.Fields{"error": err})
1260 return
1261 }
1262 if err = RsrcMgr.KVStore.Put(path, val); err != nil {
1263 log.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
1264 return
1265 }
1266 return
1267}
1268
1269//GetFlowIDsGemMapForInterface gets flowids per gemport and interface
1270func (RsrcMgr *OpenOltResourceMgr) GetFlowIDsGemMapForInterface(intf uint32) (map[uint32][]uint32, error) {
1271 path := fmt.Sprintf(FlowIDsForGem, intf)
1272 var flowsForGem map[uint32][]uint32
1273 var val []byte
1274
1275 value, err := RsrcMgr.KVStore.Get(path)
1276 if err != nil {
1277 log.Error("failed to get data from kv store")
1278 return nil, err
1279 }
1280 if value != nil {
1281 if val, err = kvstore.ToByte(value.Value); err != nil {
1282 log.Error("Failed to convert to byte array", log.Fields{"error": err})
1283 return nil, err
1284 }
1285 if err = json.Unmarshal(val, &flowsForGem); err != nil {
1286 log.Error("Failed to unmarshall", log.Fields{"error": err})
1287 return nil, err
1288 }
1289 }
1290 return flowsForGem, nil
1291}