blob: 3c011173a8f942c07bb437a3980b8a136ae37cd7 [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}"
salmansiddiqui7ac62132019-08-22 03:58:50 +000049)
Abhilash S.L7f17e402019-03-15 17:40:41 +053050
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070051// FlowInfo holds the flow information
Abhilash S.L8ee90712019-04-29 16:24:22 +053052type FlowInfo struct {
53 Flow *openolt.Flow
54 FlowStoreCookie uint64
55 FlowCategory string
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053056 LogicalFlowID uint64
57}
58
59// OnuGemInfo holds onu information along with gem port list and uni port list
60type OnuGemInfo struct {
61 OnuID uint32
62 SerialNumber string
63 IntfID uint32
64 GemPorts []uint32
65 UniPorts []uint32
66}
67
68// PacketInInfoKey is the key for packet in gemport
69type PacketInInfoKey struct {
70 IntfID uint32
71 OnuID uint32
72 LogicalPort uint32
Abhilash S.L8ee90712019-04-29 16:24:22 +053073}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070074
75// OpenOltResourceMgr holds resource related information as provided below for each field
Abhilash S.L7f17e402019-03-15 17:40:41 +053076type OpenOltResourceMgr struct {
sbarbaria8910ba2019-11-05 10:12:23 -050077 DeviceID string // OLT device id
78 HostAndPort string // Host and port of the kv store to connect to
79 Args string // args
80 KVStore *db.Backend // backend kv store connection handle
Girish Gowdru0c588b22019-04-23 23:24:56 -040081 DeviceType string
82 Host string // Host ip of the kv store
83 Port int // port of the kv store
84 DevInfo *openolt.DeviceInfo // device information
85 // array of pon resource managers per interface technology
86 ResourceMgrs map[uint32]*ponrmgr.PONResourceManager
Abhilash S.L7f17e402019-03-15 17:40:41 +053087}
88
Manikkaraj kb1d51442019-07-23 10:41:02 -040089func newKVClient(storeType string, address string, timeout uint32) (kvstore.Client, error) {
Girish Gowdru0c588b22019-04-23 23:24:56 -040090 log.Infow("kv-store-type", log.Fields{"store": storeType})
91 switch storeType {
92 case "consul":
93 return kvstore.NewConsulClient(address, int(timeout))
94 case "etcd":
95 return kvstore.NewEtcdClient(address, int(timeout))
96 }
97 return nil, errors.New("unsupported-kv-store")
Abhilash S.L7f17e402019-03-15 17:40:41 +053098}
99
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700100// SetKVClient sets the KV client and return a kv backend
sbarbaria8910ba2019-11-05 10:12:23 -0500101func SetKVClient(backend string, Host string, Port int, DeviceID string) *db.Backend {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400102 addr := Host + ":" + strconv.Itoa(Port)
103 // TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
104 // issue between kv store and backend , core is not calling NewBackend directly
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700105 kvClient, err := newKVClient(backend, addr, KvstoreTimeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400106 if err != nil {
107 log.Fatalw("Failed to init KV client\n", log.Fields{"err": err})
108 return nil
109 }
sbarbaria8910ba2019-11-05 10:12:23 -0500110 kvbackend := &db.Backend{
Girish Gowdru0c588b22019-04-23 23:24:56 -0400111 Client: kvClient,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700112 StoreType: backend,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400113 Host: Host,
114 Port: Port,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700115 Timeout: KvstoreTimeout,
116 PathPrefix: fmt.Sprintf(BasePathKvStore, DeviceID)}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530117
Girish Gowdru0c588b22019-04-23 23:24:56 -0400118 return kvbackend
Abhilash S.L7f17e402019-03-15 17:40:41 +0530119}
120
Gamze Abakafee36392019-10-03 11:17:24 +0000121// NewResourceMgr init a New resource manager instance which in turn instantiates pon resource manager
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700122// instances according to technology. Initializes the default resource ranges for all
123// the resources.
124func NewResourceMgr(deviceID string, KVStoreHostPort string, kvStoreType string, deviceType string, devInfo *openolt.DeviceInfo) *OpenOltResourceMgr {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400125 var ResourceMgr OpenOltResourceMgr
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700126 log.Debugf("Init new resource manager , host_port: %s, deviceid: %s", KVStoreHostPort, deviceID)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530127 ResourceMgr.HostAndPort = KVStoreHostPort
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700128 ResourceMgr.DeviceType = deviceType
129 ResourceMgr.DevInfo = devInfo
130 IPPort := strings.Split(KVStoreHostPort, ":")
131 ResourceMgr.Host = IPPort[0]
132 ResourceMgr.Port, _ = strconv.Atoi(IPPort[1])
Abhilash S.L7f17e402019-03-15 17:40:41 +0530133
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700134 Backend := kvStoreType
Girish Gowdru0c588b22019-04-23 23:24:56 -0400135 ResourceMgr.KVStore = SetKVClient(Backend, ResourceMgr.Host,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700136 ResourceMgr.Port, deviceID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400137 if ResourceMgr.KVStore == nil {
138 log.Error("Failed to setup KV store")
139 }
140 Ranges := make(map[string]*openolt.DeviceInfo_DeviceResourceRanges)
141 RsrcMgrsByTech := make(map[string]*ponrmgr.PONResourceManager)
142 ResourceMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530143
Girish Gowdru0c588b22019-04-23 23:24:56 -0400144 // TODO self.args = registry('main').get_args()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530145
Girish Gowdru0c588b22019-04-23 23:24:56 -0400146 /*
147 If a legacy driver returns protobuf without any ranges,s synthesize one from
Gamze Abakafee36392019-10-03 11:17:24 +0000148 the legacy global per-device information. This, in theory, is temporary until
Girish Gowdru0c588b22019-04-23 23:24:56 -0400149 the legacy drivers are upgrade to support pool ranges.
150 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700151 if devInfo.Ranges == nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400152 var ranges openolt.DeviceInfo_DeviceResourceRanges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700153 ranges.Technology = devInfo.GetTechnology()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530154
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700155 NumPONPorts := devInfo.GetPonPorts()
Girish Gowdru0c588b22019-04-23 23:24:56 -0400156 var index uint32
157 for index = 0; index < NumPONPorts; index++ {
158 ranges.IntfIds = append(ranges.IntfIds, index)
159 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530160
Abhilash S.L8ee90712019-04-29 16:24:22 +0530161 var Pool openolt.DeviceInfo_DeviceResourceRanges_Pool
Girish Gowdru0c588b22019-04-23 23:24:56 -0400162 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700163 Pool.Start = devInfo.OnuIdStart
164 Pool.End = devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400165 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
cbabuabf02352019-10-15 13:14:56 +0200166 onuPool := Pool
167 ranges.Pools = append(ranges.Pools, &onuPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530168
Girish Gowdru0c588b22019-04-23 23:24:56 -0400169 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700170 Pool.Start = devInfo.AllocIdStart
171 Pool.End = devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400172 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200173 allocPool := Pool
174 ranges.Pools = append(ranges.Pools, &allocPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530175
Girish Gowdru0c588b22019-04-23 23:24:56 -0400176 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700177 Pool.Start = devInfo.GemportIdStart
178 Pool.End = devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400179 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200180 gemPool := Pool
181 ranges.Pools = append(ranges.Pools, &gemPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530182
Girish Gowdru0c588b22019-04-23 23:24:56 -0400183 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700184 Pool.Start = devInfo.FlowIdStart
185 Pool.End = devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400186 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
Abhilash S.L8ee90712019-04-29 16:24:22 +0530187 ranges.Pools = append(ranges.Pools, &Pool)
188 // Add to device info
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700189 devInfo.Ranges = append(devInfo.Ranges, &ranges)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400190 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530191
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700192 // Create a separate Resource Manager instance for each range. This assumes that
Girish Gowdru0c588b22019-04-23 23:24:56 -0400193 // each technology is represented by only a single range
194 var GlobalPONRsrcMgr *ponrmgr.PONResourceManager
195 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700196 for _, TechRange := range devInfo.Ranges {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400197 technology := TechRange.Technology
198 log.Debugf("Device info technology %s", technology)
199 Ranges[technology] = TechRange
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700200 RsrcMgrsByTech[technology], err = ponrmgr.NewPONResourceManager(technology, deviceType, deviceID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400201 Backend, ResourceMgr.Host, ResourceMgr.Port)
202 if err != nil {
Gamze Abakafee36392019-10-03 11:17:24 +0000203 log.Errorf("Failed to create pon resource manager instance for technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400204 return nil
205 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700206 // resource_mgrs_by_tech[technology] = resource_mgr
Girish Gowdru0c588b22019-04-23 23:24:56 -0400207 if GlobalPONRsrcMgr == nil {
208 GlobalPONRsrcMgr = RsrcMgrsByTech[technology]
209 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700210 for _, IntfID := range TechRange.IntfIds {
211 ResourceMgr.ResourceMgrs[uint32(IntfID)] = RsrcMgrsByTech[technology]
Girish Gowdru0c588b22019-04-23 23:24:56 -0400212 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700213 // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400214 InitializeDeviceResourceRangeAndPool(RsrcMgrsByTech[technology], GlobalPONRsrcMgr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700215 TechRange, devInfo)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400216 }
217 // After we have initialized resource ranges, initialize the
218 // resource pools accordingly.
219 for _, PONRMgr := range RsrcMgrsByTech {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700220 _ = PONRMgr.InitDeviceResourcePool()
Girish Gowdru0c588b22019-04-23 23:24:56 -0400221 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530222 log.Info("Initialization of resource manager success!")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400223 return &ResourceMgr
Abhilash S.L7f17e402019-03-15 17:40:41 +0530224}
225
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700226// InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply
227// device specific information. If KV doesn't exist
228// or is broader than the device, the device's information will
229// dictate the range limits
230func InitializeDeviceResourceRangeAndPool(ponRMgr *ponrmgr.PONResourceManager, globalPONRMgr *ponrmgr.PONResourceManager,
231 techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530232
Girish Gowdru0c588b22019-04-23 23:24:56 -0400233 // init the resource range pool according to the sharing type
Abhilash S.L7f17e402019-03-15 17:40:41 +0530234
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700235 log.Debugf("Resource range pool init for technology %s", ponRMgr.Technology)
236 // first load from KV profiles
237 status := ponRMgr.InitResourceRangesFromKVStore()
238 if !status {
239 log.Debugf("Failed to load resource ranges from KV store for tech %s", ponRMgr.Technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400240 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530241
Girish Gowdru0c588b22019-04-23 23:24:56 -0400242 /*
243 Then apply device specific information. If KV doesn't exist
Gamze Abakafee36392019-10-03 11:17:24 +0000244 or is broader than the device, the device's information will
Girish Gowdru0c588b22019-04-23 23:24:56 -0400245 dictate the range limits
246 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700247 log.Debugf("Using device info to init pon resource ranges for tech", ponRMgr.Technology)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530248
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700249 ONUIDStart := devInfo.OnuIdStart
250 ONUIDEnd := devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400251 ONUIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
252 ONUIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700253 AllocIDStart := devInfo.AllocIdStart
254 AllocIDEnd := devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400255 AllocIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
256 AllocIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700257 GEMPortIDStart := devInfo.GemportIdStart
258 GEMPortIDEnd := devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400259 GEMPortIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
260 GEMPortIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700261 FlowIDStart := devInfo.FlowIdStart
262 FlowIDEnd := devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400263 FlowIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
264 FlowIDSharedPoolID := uint32(0)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530265
Girish Gowdru0c588b22019-04-23 23:24:56 -0400266 var FirstIntfPoolID uint32
267 var SharedPoolID uint32
Abhilash S.L7f17e402019-03-15 17:40:41 +0530268
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400269 /*
270 * 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 -0700271 * if resources are shared across interfaces then SharedPoolID is given a positive number.
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400272 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700273 for _, FirstIntfPoolID = range techRange.IntfIds {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400274 // skip the intf id 0
275 if FirstIntfPoolID == 0 {
276 continue
277 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400278 break
279 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530280
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700281 for _, RangePool := range techRange.Pools {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400282 if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400283 SharedPoolID = FirstIntfPoolID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400284 } else if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_SAME_TECH {
285 SharedPoolID = FirstIntfPoolID
286 } else {
287 SharedPoolID = 0
288 }
289 if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID {
290 ONUIDStart = RangePool.Start
291 ONUIDEnd = RangePool.End
292 ONUIDShared = RangePool.Sharing
293 ONUIDSharedPoolID = SharedPoolID
294 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID {
295 AllocIDStart = RangePool.Start
296 AllocIDEnd = RangePool.End
297 AllocIDShared = RangePool.Sharing
298 AllocIDSharedPoolID = SharedPoolID
299 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID {
300 GEMPortIDStart = RangePool.Start
301 GEMPortIDEnd = RangePool.End
302 GEMPortIDShared = RangePool.Sharing
303 GEMPortIDSharedPoolID = SharedPoolID
304 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID {
305 FlowIDStart = RangePool.Start
306 FlowIDEnd = RangePool.End
307 FlowIDShared = RangePool.Sharing
308 FlowIDSharedPoolID = SharedPoolID
309 }
310 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530311
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700312 log.Debugw("Device info init", log.Fields{"technology": techRange.Technology,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400313 "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd, "onu_id_shared_pool_id": ONUIDSharedPoolID,
314 "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd,
315 "alloc_id_shared_pool_id": AllocIDSharedPoolID,
316 "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd,
317 "gemport_id_shared_pool_id": GEMPortIDSharedPoolID,
318 "flow_id_start": FlowIDStart,
319 "flow_id_end_idx": FlowIDEnd,
320 "flow_id_shared_pool_id": FlowIDSharedPoolID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700321 "intf_ids": techRange.IntfIds,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400322 "uni_id_start": 0,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700323 "uni_id_end_idx": 1, /*MaxUNIIDperONU()*/
324 })
Abhilash S.L7f17e402019-03-15 17:40:41 +0530325
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700326 ponRMgr.InitDefaultPONResourceRanges(ONUIDStart, ONUIDEnd, ONUIDSharedPoolID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400327 AllocIDStart, AllocIDEnd, AllocIDSharedPoolID,
328 GEMPortIDStart, GEMPortIDEnd, GEMPortIDSharedPoolID,
329 FlowIDStart, FlowIDEnd, FlowIDSharedPoolID, 0, 1,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700330 devInfo.PonPorts, techRange.IntfIds)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530331
Girish Gowdru0c588b22019-04-23 23:24:56 -0400332 // For global sharing, make sure to refresh both local and global resource manager instances' range
Abhilash S.L7f17e402019-03-15 17:40:41 +0530333
Girish Gowdru0c588b22019-04-23 23:24:56 -0400334 if ONUIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700335 globalPONRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400336 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700337 ponRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
338 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400339 }
340 if AllocIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700341 globalPONRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400342 "", 0, nil)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530343
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700344 ponRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
345 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400346 }
347 if GEMPortIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700348 globalPONRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400349 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700350 ponRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
351 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400352 }
353 if FlowIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700354 globalPONRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400355 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700356 ponRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
357 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400358 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530359
Girish Gowdru0c588b22019-04-23 23:24:56 -0400360 // Make sure loaded range fits the platform bit encoding ranges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700361 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 +0530362}
363
Devmalya Paul495b94a2019-08-27 19:42:00 -0400364// Delete clears used resources for the particular olt device being deleted
365func (RsrcMgr *OpenOltResourceMgr) Delete() error {
366 /* TODO
367 def __del__(self):
368 self.log.info("clearing-device-resource-pool")
369 for key, resource_mgr in self.resource_mgrs.iteritems():
370 resource_mgr.clear_device_resource_pool()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530371
Devmalya Paul495b94a2019-08-27 19:42:00 -0400372 def assert_pon_id_limit(self, pon_intf_id):
373 assert pon_intf_id in self.resource_mgrs
Abhilash S.L7f17e402019-03-15 17:40:41 +0530374
Devmalya Paul495b94a2019-08-27 19:42:00 -0400375 def assert_onu_id_limit(self, pon_intf_id, onu_id):
376 self.assert_pon_id_limit(pon_intf_id)
377 self.resource_mgrs[pon_intf_id].assert_resource_limits(onu_id, PONResourceManager.ONU_ID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530378
Devmalya Paul495b94a2019-08-27 19:42:00 -0400379 @property
380 def max_uni_id_per_onu(self):
381 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 +0530382
Devmalya Paul495b94a2019-08-27 19:42:00 -0400383 def assert_uni_id_limit(self, pon_intf_id, onu_id, uni_id):
384 self.assert_onu_id_limit(pon_intf_id, onu_id)
385 self.resource_mgrs[pon_intf_id].assert_resource_limits(uni_id, PONResourceManager.UNI_ID)
386 */
387 for _, rsrcMgr := range RsrcMgr.ResourceMgrs {
388 if err := rsrcMgr.ClearDeviceResourcePool(); err != nil {
389 log.Debug("Failed to clear device resource pool")
390 return err
391 }
392 }
393 log.Debug("Cleared device resource pool")
394 return nil
395}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530396
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700397// GetONUID returns the available OnuID for the given pon-port
398func (RsrcMgr *OpenOltResourceMgr) GetONUID(ponIntfID uint32) (uint32, error) {
salmansiddiqui352a45c2019-08-19 10:15:36 +0000399 // Check if Pon Interface ID is present in Resource-manager-map
400 if _, ok := RsrcMgr.ResourceMgrs[ponIntfID]; !ok {
401 err := errors.New("invalid-pon-interface-" + strconv.Itoa(int(ponIntfID)))
402 return 0, err
403 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400404 // Get ONU id for a provided pon interface ID.
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700405 ONUID, err := RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400406 ponrmgr.ONU_ID, 1)
407 if err != nil {
408 log.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700409 ponIntfID, ponrmgr.ONU_ID)
cbabuabf02352019-10-15 13:14:56 +0200410 return 0, err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400411 }
412 if ONUID != nil {
Devmalya Paul495b94a2019-08-27 19:42:00 -0400413 RsrcMgr.ResourceMgrs[ponIntfID].InitResourceMap(fmt.Sprintf("%d,%d", ponIntfID, ONUID[0]))
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700414 return ONUID[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400415 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530416
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700417 return 0, err // return OnuID 0 on error
Abhilash S.L7f17e402019-03-15 17:40:41 +0530418}
419
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700420// GetFlowIDInfo returns the slice of flow info of the given pon-port
421// Note: For flows which trap from the NNI and not really associated with any particular
422// 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 +0530423func (RsrcMgr *OpenOltResourceMgr) GetFlowIDInfo(ponIntfID uint32, onuID int32, uniID int32, flowID uint32) *[]FlowInfo {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530424 var flows []FlowInfo
425
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700426 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
427 if err := RsrcMgr.ResourceMgrs[ponIntfID].GetFlowIDInfo(FlowPath, flowID, &flows); err != nil {
428 log.Errorw("Error while getting flows from KV store", log.Fields{"flowId": flowID})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530429 return nil
430 }
431 if len(flows) == 0 {
432 log.Debugw("No flowInfo found in KV store", log.Fields{"flowPath": FlowPath})
433 return nil
434 }
435 return &flows
436}
437
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700438// GetCurrentFlowIDsForOnu fetches flow ID from the resource manager
439// Note: For flows which trap from the NNI and not really associated with any particular
440// 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 +0530441func (RsrcMgr *OpenOltResourceMgr) GetCurrentFlowIDsForOnu(PONIntfID uint32, ONUID int32, UNIID int32) []uint32 {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700442
Abhilash S.L8ee90712019-04-29 16:24:22 +0530443 FlowPath := fmt.Sprintf("%d,%d,%d", PONIntfID, ONUID, UNIID)
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700444 if mgrs, exist := RsrcMgr.ResourceMgrs[PONIntfID]; exist {
445 return mgrs.GetCurrentFlowIDsForOnu(FlowPath)
446 }
447 return nil
Abhilash S.L8ee90712019-04-29 16:24:22 +0530448}
449
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700450// UpdateFlowIDInfo updates flow info for the given pon interface, onu id, and uni id
451// Note: For flows which trap from the NNI and not really associated with any particular
452// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
453func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDInfo(ponIntfID int32, onuID int32, uniID int32,
454 flowID uint32, flowData *[]FlowInfo) error {
455 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
456 return RsrcMgr.ResourceMgrs[uint32(ponIntfID)].UpdateFlowIDInfoForOnu(FlowPath, flowID, *flowData)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530457}
458
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700459// GetFlowID return flow ID for a given pon interface id, onu id and uni id
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530460func (RsrcMgr *OpenOltResourceMgr) GetFlowID(ponIntfID uint32, ONUID int32, uniID int32,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400461 gemportID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700462 flowStoreCookie uint64,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400463 flowCategory string, vlanPcp ...uint32) (uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530464
Girish Gowdru0c588b22019-04-23 23:24:56 -0400465 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700466 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, ONUID, uniID)
467 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(FlowPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400468 if FlowIDs != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700469 log.Debugw("Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "ONUID": ONUID, "uniID": uniID, "KVpath": FlowPath})
470 for _, flowID := range FlowIDs {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530471 FlowInfo := RsrcMgr.GetFlowIDInfo(ponIntfID, int32(ONUID), int32(uniID), uint32(flowID))
salmansiddiqui7ac62132019-08-22 03:58:50 +0000472 er := getFlowIDFromFlowInfo(FlowInfo, flowID, gemportID, flowStoreCookie, flowCategory, vlanPcp...)
473 if er == nil {
474 return flowID, er
Abhilash S.L8ee90712019-04-29 16:24:22 +0530475 }
476 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400477 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530478 log.Debug("No matching flows with flow cookie or flow category, allocating new flowid")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700479 FlowIDs, err = RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400480 ponrmgr.FLOW_ID, 1)
481 if err != nil {
482 log.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700483 ponIntfID, ponrmgr.FLOW_ID)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400484 return uint32(0), err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400485 }
486 if FlowIDs != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700487 _ = RsrcMgr.ResourceMgrs[ponIntfID].UpdateFlowIDForOnu(FlowPath, FlowIDs[0], true)
488 return FlowIDs[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400489 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530490
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700491 return 0, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530492}
493
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700494// GetAllocID return the first Alloc ID for a given pon interface id and onu id and then update the resource map on
495// the KV store with the list of alloc_ids allocated for the pon_intf_onu_id tuple
496// Currently of all the alloc_ids available, it returns the first alloc_id in the list for tha given ONU
497func (RsrcMgr *OpenOltResourceMgr) GetAllocID(intfID uint32, onuID uint32, uniID uint32) uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530498
Girish Gowdru0c588b22019-04-23 23:24:56 -0400499 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700500 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
501 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400502 if AllocID != nil {
503 // Since we support only one alloc_id for the ONU at the moment,
504 // return the first alloc_id in the list, if available, for that
505 // ONU.
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700506 log.Debugw("Retrieved alloc ID from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400507 return AllocID[0]
508 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700509 AllocID, err = RsrcMgr.ResourceMgrs[intfID].GetResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400510 ponrmgr.ALLOC_ID, 1)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530511
Girish Gowdru0c588b22019-04-23 23:24:56 -0400512 if AllocID == nil || err != nil {
513 log.Error("Failed to allocate alloc id")
514 return 0
515 }
516 // update the resource map on KV store with the list of alloc_id
517 // allocated for the pon_intf_onu_id tuple
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700518 err = RsrcMgr.ResourceMgrs[intfID].UpdateAllocIdsForOnu(IntfOnuIDUniID, AllocID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400519 if err != nil {
520 log.Error("Failed to update Alloc ID")
521 return 0
522 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530523 log.Debugw("Allocated new Tcont from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400524 return AllocID[0]
Abhilash S.L7f17e402019-03-15 17:40:41 +0530525}
526
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700527// UpdateAllocIdsForOnu updates alloc ids in kv store for a given pon interface id, onu id and uni id
528func (RsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ponPort uint32, onuID uint32, uniID uint32, allocID []uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530529
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700530 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
531 return RsrcMgr.ResourceMgrs[ponPort].UpdateAllocIdsForOnu(IntfOnuIDUniID,
532 allocID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530533}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700534
535// GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id
536func (RsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(intfID uint32, onuID uint32,
537 uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530538
Girish Gowdru0c588b22019-04-23 23:24:56 -0400539 /* Get gem ports for given pon interface , onu id and uni id. */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530540
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700541 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
542 return RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530543}
544
Gamze Abakafee36392019-10-03 11:17:24 +0000545// GetCurrentAllocIDsForOnu returns alloc ids for given pon interface and onu id
546func (RsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDsForOnu(intfID uint32, onuID uint32, uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530547
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700548 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
549 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400550 if AllocID != nil {
Gamze Abakafee36392019-10-03 11:17:24 +0000551 return AllocID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400552 }
Gamze Abakafee36392019-10-03 11:17:24 +0000553 return []uint32{}
554}
555
556// RemoveAllocIDForOnu removes the alloc id for given pon interface, onu id, uni id and alloc id
557func (RsrcMgr *OpenOltResourceMgr) RemoveAllocIDForOnu(intfID uint32, onuID uint32, uniID uint32, allocID uint32) {
558 allocIDs := RsrcMgr.GetCurrentAllocIDsForOnu(intfID, onuID, uniID)
559 for i := 0; i < len(allocIDs); i++ {
560 if allocIDs[i] == allocID {
561 allocIDs = append(allocIDs[:i], allocIDs[i+1:]...)
562 break
563 }
564 }
565 err := RsrcMgr.UpdateAllocIdsForOnu(intfID, onuID, uniID, allocIDs)
566 if err != nil {
567 log.Errorf("Failed to Remove Alloc Id For Onu. IntfID %d onuID %d uniID %d allocID %d",
568 intfID, onuID, uniID, allocID)
569 }
570}
571
572// RemoveGemPortIDForOnu removes the gem port id for given pon interface, onu id, uni id and gem port id
573func (RsrcMgr *OpenOltResourceMgr) RemoveGemPortIDForOnu(intfID uint32, onuID uint32, uniID uint32, gemPortID uint32) {
574 gemPortIDs := RsrcMgr.GetCurrentGEMPortIDsForOnu(intfID, onuID, uniID)
575 for i := 0; i < len(gemPortIDs); i++ {
576 if gemPortIDs[i] == gemPortID {
577 gemPortIDs = append(gemPortIDs[:i], gemPortIDs[i+1:]...)
578 break
579 }
580 }
581 err := RsrcMgr.UpdateGEMPortIDsForOnu(intfID, onuID, uniID, gemPortIDs)
582 if err != nil {
583 log.Errorf("Failed to Remove Gem Id For Onu. IntfID %d onuID %d uniID %d gemPortId %d",
584 intfID, onuID, uniID, gemPortID)
585 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530586}
587
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700588// UpdateGEMportsPonportToOnuMapOnKVStore updates onu and uni id associated with the gem port to the kv store
589// This stored information is used when packet_indication is received and we need to derive the ONU Id for which
590// the packet arrived based on the pon_intf and gemport available in the packet_indication
591func (RsrcMgr *OpenOltResourceMgr) UpdateGEMportsPonportToOnuMapOnKVStore(gemPorts []uint32, PonPort uint32,
592 onuID uint32, uniID uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530593
Girish Gowdru0c588b22019-04-23 23:24:56 -0400594 /* Update onu and uni id associated with the gem port to the kv store. */
595 var IntfGEMPortPath string
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700596 Data := fmt.Sprintf("%d %d", onuID, uniID)
597 for _, GEM := range gemPorts {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400598 IntfGEMPortPath = fmt.Sprintf("%d,%d", PonPort, GEM)
599 Val, err := json.Marshal(Data)
600 if err != nil {
601 log.Error("failed to Marshal")
602 return err
603 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700604
Girish Gowdru0c588b22019-04-23 23:24:56 -0400605 if err = RsrcMgr.KVStore.Put(IntfGEMPortPath, Val); err != nil {
606 log.Errorf("Failed to update resource %s", IntfGEMPortPath)
607 return err
608 }
609 }
610 return nil
Abhilash S.L7f17e402019-03-15 17:40:41 +0530611}
612
Gamze Abakafee36392019-10-03 11:17:24 +0000613// RemoveGEMportPonportToOnuMapOnKVStore removes the relationship between the gem port and pon port
614func (RsrcMgr *OpenOltResourceMgr) RemoveGEMportPonportToOnuMapOnKVStore(GemPort uint32, PonPort uint32) {
615 IntfGEMPortPath := fmt.Sprintf("%d,%d", PonPort, GemPort)
616 err := RsrcMgr.KVStore.Delete(IntfGEMPortPath)
617 if err != nil {
618 log.Errorf("Failed to Remove Gem port-Pon port to onu map on kv store. Gem %d PonPort %d", GemPort, PonPort)
619 }
620}
621
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700622// GetGEMPortID gets gem port id for a particular pon port, onu id and uni id and then update the resource map on
623// the KV store with the list of gemport_id allocated for the pon_intf_onu_id tuple
624func (RsrcMgr *OpenOltResourceMgr) GetGEMPortID(ponPort uint32, onuID uint32,
625 uniID uint32, NumOfPorts uint32) ([]uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530626
Girish Gowdru0c588b22019-04-23 23:24:56 -0400627 /* Get gem port id for a particular pon port, onu id
628 and uni id.
629 */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530630
Girish Gowdru0c588b22019-04-23 23:24:56 -0400631 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700632 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530633
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700634 GEMPortList := RsrcMgr.ResourceMgrs[ponPort].GetCurrentGEMPortIDsForOnu(IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400635 if GEMPortList != nil {
636 return GEMPortList, nil
637 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530638
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700639 GEMPortList, err = RsrcMgr.ResourceMgrs[ponPort].GetResourceID(ponPort,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400640 ponrmgr.GEMPORT_ID, NumOfPorts)
641 if err != nil && GEMPortList == nil {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530642 log.Errorf("Failed to get gem port id for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400643 return nil, err
644 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530645
Girish Gowdru0c588b22019-04-23 23:24:56 -0400646 // update the resource map on KV store with the list of gemport_id
647 // allocated for the pon_intf_onu_id tuple
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700648 err = RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400649 GEMPortList)
650 if err != nil {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530651 log.Errorf("Failed to update GEM ports to kv store for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400652 return nil, err
653 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700654 _ = RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(GEMPortList, ponPort,
655 onuID, uniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400656 return GEMPortList, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530657}
658
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700659// UpdateGEMPortIDsForOnu updates gemport ids on to the kv store for a given pon port, onu id and uni id
660func (RsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ponPort uint32, onuID uint32,
661 uniID uint32, GEMPortList []uint32) error {
662 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
663 return RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400664 GEMPortList)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530665
666}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700667
668// FreeonuID releases(make free) onu id for a particular pon-port
669func (RsrcMgr *OpenOltResourceMgr) FreeonuID(intfID uint32, onuID []uint32) {
670
671 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID, ponrmgr.ONU_ID, onuID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530672
Girish Gowdru0c588b22019-04-23 23:24:56 -0400673 /* Free onu id for a particular interface.*/
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700674 var IntfonuID string
675 for _, onu := range onuID {
676 IntfonuID = fmt.Sprintf("%d,%d", intfID, onu)
677 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(IntfonuID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400678 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530679}
680
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700681// FreeFlowID returns the free flow id for a given interface, onu id and uni id
Devmalya Paul495b94a2019-08-27 19:42:00 -0400682func (RsrcMgr *OpenOltResourceMgr) FreeFlowID(IntfID uint32, onuID int32,
683 uniID int32, FlowID uint32) {
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400684 var IntfONUID string
685 var err error
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530686 FlowIds := make([]uint32, 0)
687
688 FlowIds = append(FlowIds, FlowID)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700689 IntfONUID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
690 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(IntfONUID, FlowID, false)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400691 if err != nil {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530692 log.Errorw("Failed to Update flow id for", log.Fields{"intf": IntfONUID})
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400693 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700694 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(IntfONUID, FlowID)
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530695 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(IntfID, ponrmgr.FLOW_ID, FlowIds)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400696}
697
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700698// FreeFlowIDs releases the flow Ids
699func (RsrcMgr *OpenOltResourceMgr) FreeFlowIDs(IntfID uint32, onuID uint32,
700 uniID uint32, FlowID []uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530701
Girish Gowdru0c588b22019-04-23 23:24:56 -0400702 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(IntfID, ponrmgr.FLOW_ID, FlowID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530703
Abhilash S.L8ee90712019-04-29 16:24:22 +0530704 var IntfOnuIDUniID string
Girish Gowdru0c588b22019-04-23 23:24:56 -0400705 var err error
706 for _, flow := range FlowID {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700707 IntfOnuIDUniID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530708 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(IntfOnuIDUniID, flow, false)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400709 if err != nil {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530710 log.Errorw("Failed to Update flow id for", log.Fields{"intf": IntfOnuIDUniID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400711 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530712 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(IntfOnuIDUniID, flow)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400713 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530714}
715
Gamze Abakafee36392019-10-03 11:17:24 +0000716// FreeAllocID frees AllocID on the PON resource pool and also frees the allocID association
717// for the given OLT device.
718func (RsrcMgr *OpenOltResourceMgr) FreeAllocID(IntfID uint32, onuID uint32,
719 uniID uint32, allocID uint32) {
720 RsrcMgr.RemoveAllocIDForOnu(IntfID, onuID, uniID, allocID)
721 allocIDs := make([]uint32, 0)
722 allocIDs = append(allocIDs, allocID)
723 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(IntfID, ponrmgr.ALLOC_ID, allocIDs)
724}
725
726// FreeGemPortID frees GemPortID on the PON resource pool and also frees the gemPortID association
727// for the given OLT device.
728func (RsrcMgr *OpenOltResourceMgr) FreeGemPortID(IntfID uint32, onuID uint32,
729 uniID uint32, gemPortID uint32) {
730 RsrcMgr.RemoveGemPortIDForOnu(IntfID, onuID, uniID, gemPortID)
731 gemPortIDs := make([]uint32, 0)
732 gemPortIDs = append(gemPortIDs, gemPortID)
733 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(IntfID, ponrmgr.GEMPORT_ID, gemPortIDs)
734}
735
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700736// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id, and the clears the
737// resource map and the onuID associated with (pon_intf_id, gemport_id) tuple,
738func (RsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(intfID uint32, onuID uint32, uniID uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530739
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700740 var onuIDs []uint32
741 onuIDs = append(onuIDs, onuID)
742 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)
Devmalya Paul495b94a2019-08-27 19:42:00 -0400759 if int32(onuID) >= 0 {
760 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
761 ponrmgr.ONU_ID,
762 onuIDs)
763 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400764 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700765 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530766
Girish Gowdru0c588b22019-04-23 23:24:56 -0400767 // Clear the ONU Id associated with the (pon_intf_id, gemport_id) tuple.
768 for _, GEM := range GEMPortIDs {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700769 _ = RsrcMgr.KVStore.Delete(fmt.Sprintf("%d,%d", intfID, GEM))
Girish Gowdru0c588b22019-04-23 23:24:56 -0400770 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530771}
772
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700773// IsFlowCookieOnKVStore checks if the given flow cookie is present on the kv store
774// Returns true if the flow cookie is found, otherwise it returns false
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530775func (RsrcMgr *OpenOltResourceMgr) IsFlowCookieOnKVStore(ponIntfID uint32, onuID int32, uniID int32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700776 flowStoreCookie uint64) bool {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530777
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700778 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
779 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(FlowPath)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400780 if FlowIDs != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700781 log.Debugw("Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "onuID": onuID, "uniID": uniID, "KVpath": FlowPath})
782 for _, flowID := range FlowIDs {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530783 FlowInfo := RsrcMgr.GetFlowIDInfo(ponIntfID, int32(onuID), int32(uniID), uint32(flowID))
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400784 if FlowInfo != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700785 log.Debugw("Found flows", log.Fields{"flows": *FlowInfo, "flowId": flowID})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400786 for _, Info := range *FlowInfo {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700787 if Info.FlowStoreCookie == flowStoreCookie {
788 log.Debug("Found flow matching with flowStore cookie", log.Fields{"flowId": flowID, "flowStoreCookie": flowStoreCookie})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400789 return true
790 }
791 }
792 }
793 }
794 }
795 return false
796}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400797
salmansiddiqui7ac62132019-08-22 03:58:50 +0000798// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000799// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
800func (RsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(IntfID uint32, OnuID uint32, UniID uint32) []uint32 {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000801 Path := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000802 var Data []uint32
salmansiddiqui7ac62132019-08-22 03:58:50 +0000803 Value, err := RsrcMgr.KVStore.Get(Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400804 if err == nil {
805 if Value != nil {
806 Val, err := kvstore.ToByte(Value.Value)
807 if err != nil {
808 log.Errorw("Failed to convert into byte array", log.Fields{"error": err})
809 return Data
810 }
811 if err = json.Unmarshal(Val, &Data); err != nil {
812 log.Error("Failed to unmarshal", log.Fields{"error": err})
813 return Data
814 }
815 }
816 } else {
817 log.Errorf("Failed to get TP id from kvstore for path %s", Path)
818 }
819 log.Debugf("Getting TP id %d from path %s", Data, Path)
820 return Data
821
822}
823
Gamze Abakafee36392019-10-03 11:17:24 +0000824// RemoveTechProfileIDsForOnu deletes all tech profile ids from the KV-Store for the given onu based on the path
825// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
826func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDsForOnu(IntfID uint32, OnuID uint32, UniID uint32) error {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000827 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
828 if err := RsrcMgr.KVStore.Delete(IntfOnuUniID); err != nil {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530829 log.Errorw("Failed to delete techprofile id resource in KV store", log.Fields{"path": IntfOnuUniID})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400830 return err
831 }
832 return nil
833}
834
Gamze Abakafee36392019-10-03 11:17:24 +0000835// RemoveTechProfileIDForOnu deletes a specific tech profile id from the KV-Store for the given onu based on the path
836// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
837func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(IntfID uint32, OnuID uint32, UniID uint32, TpID uint32) error {
838 tpIDList := RsrcMgr.GetTechProfileIDForOnu(IntfID, OnuID, UniID)
839 for i, tpIDInList := range tpIDList {
840 if tpIDInList == TpID {
841 tpIDList = append(tpIDList[:i], tpIDList[i+1:]...)
842 }
843 }
844 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
845 Value, err := json.Marshal(tpIDList)
846 if err != nil {
847 log.Error("failed to Marshal")
848 return err
849 }
850 if err = RsrcMgr.KVStore.Put(IntfOnuUniID, Value); err != nil {
851 log.Errorf("Failed to update resource %s", IntfOnuUniID)
852 return err
853 }
854 return err
855}
856
857// UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
858// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
salmansiddiqui7ac62132019-08-22 03:58:50 +0000859func (RsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(IntfID uint32, OnuID uint32,
860 UniID uint32, TpID uint32) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400861 var Value []byte
862 var err error
863
salmansiddiqui7ac62132019-08-22 03:58:50 +0000864 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000865
866 tpIDList := RsrcMgr.GetTechProfileIDForOnu(IntfID, OnuID, UniID)
867 for _, value := range tpIDList {
868 if value == TpID {
869 log.Debugf("TpID %d is already in tpIdList for the path %s", TpID, IntfOnuUniID)
870 return err
871 }
872 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000873 log.Debugf("updating tp id %d on path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000874 tpIDList = append(tpIDList, TpID)
875 Value, err = json.Marshal(tpIDList)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400876 if err != nil {
877 log.Error("failed to Marshal")
878 return err
879 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000880 if err = RsrcMgr.KVStore.Put(IntfOnuUniID, Value); err != nil {
881 log.Errorf("Failed to update resource %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400882 return err
883 }
884 return err
885}
886
salmansiddiqui7ac62132019-08-22 03:58:50 +0000887// UpdateMeterIDForOnu updates the meter id in the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000888// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
salmansiddiqui7ac62132019-08-22 03:58:50 +0000889func (RsrcMgr *OpenOltResourceMgr) UpdateMeterIDForOnu(Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000890 UniID uint32, TpID uint32, MeterConfig *ofp.OfpMeterConfig) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400891 var Value []byte
892 var err error
893
Gamze Abakafee36392019-10-03 11:17:24 +0000894 IntfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400895 Value, err = json.Marshal(*MeterConfig)
896 if err != nil {
897 log.Error("failed to Marshal meter config")
898 return err
899 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000900 if err = RsrcMgr.KVStore.Put(IntfOnuUniID, Value); err != nil {
901 log.Errorf("Failed to store meter into KV store %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400902 return err
903 }
904 return err
905}
906
Gamze Abakafee36392019-10-03 11:17:24 +0000907// GetMeterIDForOnu fetches the meter id from the kv store for the given onu based on the path
908// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
909func (RsrcMgr *OpenOltResourceMgr) GetMeterIDForOnu(Direction string, IntfID uint32, OnuID uint32,
910 UniID uint32, TpID uint32) (*ofp.OfpMeterConfig, error) {
911 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400912 var meterConfig ofp.OfpMeterConfig
salmansiddiqui7ac62132019-08-22 03:58:50 +0000913 Value, err := RsrcMgr.KVStore.Get(Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400914 if err == nil {
915 if Value != nil {
916 log.Debug("Found meter in KV store", log.Fields{"Direction": Direction})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000917 Val, er := kvstore.ToByte(Value.Value)
918 if er != nil {
919 log.Errorw("Failed to convert into byte array", log.Fields{"error": er})
920 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400921 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000922 if er = json.Unmarshal(Val, &meterConfig); er != nil {
923 log.Error("Failed to unmarshal meterconfig", log.Fields{"error": er})
924 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400925 }
926 } else {
927 log.Debug("meter-does-not-exists-in-KVStore")
928 return nil, err
929 }
930 } else {
931 log.Errorf("Failed to get Meter config from kvstore for path %s", Path)
932
933 }
934 return &meterConfig, err
935}
936
Gamze Abakafee36392019-10-03 11:17:24 +0000937// RemoveMeterIDForOnu deletes the meter id from the kV-Store for the given onu based on the path
938// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
939func (RsrcMgr *OpenOltResourceMgr) RemoveMeterIDForOnu(Direction string, IntfID uint32, OnuID uint32,
940 UniID uint32, TpID uint32) error {
941 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
salmansiddiqui7ac62132019-08-22 03:58:50 +0000942 if err := RsrcMgr.KVStore.Delete(Path); err != nil {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400943 log.Errorf("Failed to delete meter id %s from kvstore ", Path)
944 return err
945 }
946 return nil
947}
salmansiddiqui7ac62132019-08-22 03:58:50 +0000948
949func getFlowIDFromFlowInfo(FlowInfo *[]FlowInfo, flowID, gemportID uint32, flowStoreCookie uint64, flowCategory string, vlanPcp ...uint32) error {
950 if FlowInfo != nil {
951 for _, Info := range *FlowInfo {
952 if int32(gemportID) == Info.Flow.GemportId && flowCategory != "" && Info.FlowCategory == flowCategory {
953 log.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
954 if Info.FlowCategory == "HSIA_FLOW" && Info.Flow.Classifier.OPbits == vlanPcp[0] {
955 log.Debug("Found matching vlan pcp ", log.Fields{"flowId": flowID, "Vlanpcp": vlanPcp[0]})
956 return nil
957 }
958 }
959 if int32(gemportID) == Info.Flow.GemportId && flowStoreCookie != 0 && Info.FlowStoreCookie == flowStoreCookie {
960 if flowCategory != "" && Info.FlowCategory == flowCategory {
961 log.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
962 return nil
963 }
964 }
965 }
966 }
Gamze Abakafee36392019-10-03 11:17:24 +0000967 log.Debugw("the flow can be related to a different service", log.Fields{"flow_info": FlowInfo})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000968 return errors.New("invalid flow-info")
969}
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530970
971//AddGemToOnuGemInfo adds gemport to onugem info kvstore
972func (RsrcMgr *OpenOltResourceMgr) AddGemToOnuGemInfo(intfID uint32, onuID uint32, gemPort uint32) error {
973 var onuGemData []OnuGemInfo
974 var err error
975
976 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(intfID, &onuGemData); err != nil {
977 log.Errorf("failed to get onuifo for intfid %d", intfID)
978 return err
979 }
980 if len(onuGemData) == 0 {
981 log.Errorw("failed to ger Onuid info ", log.Fields{"intfid": intfID, "onuid": onuID})
982 return err
983 }
984
985 for idx, onugem := range onuGemData {
986 if onugem.OnuID == onuID {
987 for _, gem := range onuGemData[idx].GemPorts {
988 if gem == gemPort {
989 log.Debugw("Gem already present in onugem info, skpping addition", log.Fields{"gem": gem})
990 return nil
991 }
992 }
993 log.Debugw("Added gem to onugem info", log.Fields{"gem": gemPort})
994 onuGemData[idx].GemPorts = append(onuGemData[idx].GemPorts, gemPort)
995 break
996 }
997 }
998 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(intfID, onuGemData)
999 if err != nil {
1000 log.Error("Failed to add onugem to kv store")
1001 return err
1002 }
1003 return err
1004}
1005
1006//GetOnuGemInfo gets onu gem info from the kvstore per interface
1007func (RsrcMgr *OpenOltResourceMgr) GetOnuGemInfo(IntfID uint32) ([]OnuGemInfo, error) {
1008 var onuGemData []OnuGemInfo
1009
1010 if err := RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(IntfID, &onuGemData); err != nil {
1011 log.Errorf("failed to get onuifo for intfid %d", IntfID)
1012 return nil, err
1013 }
1014
1015 return onuGemData, nil
1016}
1017
1018// AddOnuInfo adds onu info on to the kvstore per interface
1019func (RsrcMgr *OpenOltResourceMgr) AddOnuInfo(IntfID uint32, onuGem OnuGemInfo) error {
1020 var onuGemData []OnuGemInfo
1021 var err error
1022
1023 if err = RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(IntfID, &onuGemData); err != nil {
1024 log.Errorf("failed to get onuifo for intfid %d", IntfID)
1025 return err
1026 }
1027 onuGemData = append(onuGemData, onuGem)
1028 err = RsrcMgr.ResourceMgrs[IntfID].AddOnuGemInfo(IntfID, onuGemData)
1029 if err != nil {
1030 log.Error("Failed to add onugem to kv store")
1031 return err
1032 }
1033
1034 log.Debugw("added onu to onugeminfo", log.Fields{"intf": IntfID, "onugem": onuGem})
1035 return err
1036}
1037
1038// UpdateOnuInfo updates Onuinfo on the kvstore per interface
1039func (RsrcMgr *OpenOltResourceMgr) UpdateOnuInfo(IntfID uint32, onuGem []OnuGemInfo) error {
1040 var onuGemData []OnuGemInfo
1041 var err error
1042
1043 err = RsrcMgr.ResourceMgrs[IntfID].AddOnuGemInfo(IntfID, onuGemData)
1044 if err != nil {
1045 log.Error("Failed to add onugem to kv store")
1046 return err
1047 }
1048
1049 log.Debugw("updated onugeminfo", log.Fields{"intf": IntfID, "onugem": onuGem})
1050 return err
1051}
1052
1053// AddUniPortToOnuInfo adds uni port to the onuinfo kvstore. check if the uni is already present if not update the kv store.
1054func (RsrcMgr *OpenOltResourceMgr) AddUniPortToOnuInfo(intfID uint32, onuID uint32, portNo uint32) {
1055 var onuGemData []OnuGemInfo
1056 var err error
1057
1058 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(intfID, &onuGemData); err != nil {
1059 log.Errorf("failed to get onuifo for intfid %d", intfID)
1060 return
1061 }
1062 for idx, onu := range onuGemData {
1063 if onu.OnuID == onuID {
1064 for _, uni := range onu.UniPorts {
1065 if uni == portNo {
1066 log.Debugw("uni already present in onugem info", log.Fields{"uni": portNo})
1067 return
1068 }
1069 }
1070 onuGemData[idx].UniPorts = append(onuGemData[idx].UniPorts, portNo)
1071 break
1072 }
1073 }
1074 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(intfID, onuGemData)
1075 if err != nil {
1076 log.Errorw("Failed to add uin port in onugem to kv store", log.Fields{"uni": portNo})
1077 return
1078 }
1079 return
1080}
1081
1082//UpdateGemPortForPktIn updates gemport for pkt in path to kvstore, path being intfid, onuid, portno
1083func (RsrcMgr *OpenOltResourceMgr) UpdateGemPortForPktIn(pktIn PacketInInfoKey, gemPort uint32) {
1084
1085 path := fmt.Sprintf(OnuPacketINPath, pktIn.IntfID, pktIn.OnuID, pktIn.LogicalPort)
1086 Value, err := json.Marshal(gemPort)
1087 if err != nil {
1088 log.Error("Failed to marshal data")
1089 return
1090 }
1091 if err = RsrcMgr.KVStore.Put(path, Value); err != nil {
1092 log.Errorw("Failed to put to kvstore", log.Fields{"path": path, "value": gemPort})
1093 return
1094 }
1095 log.Debugw("added gem packet in successfully", log.Fields{"path": path, "gem": gemPort})
1096
1097 return
1098}
1099
1100// GetGemPortFromOnuPktIn gets the gem port from onu pkt in path, path being intfid, onuid, portno
1101func (RsrcMgr *OpenOltResourceMgr) GetGemPortFromOnuPktIn(intfID uint32, onuID uint32, logicalPort uint32) (uint32, error) {
1102
1103 var Val []byte
1104 var gemPort uint32
1105
1106 path := fmt.Sprintf(OnuPacketINPath, intfID, onuID, logicalPort)
1107
1108 value, err := RsrcMgr.KVStore.Get(path)
1109 if err != nil {
1110 log.Errorw("Failed to get from kv store", log.Fields{"path": path})
1111 return uint32(0), err
1112 } else if value == nil {
1113 log.Debugw("No pkt in gem found", log.Fields{"path": path})
1114 return uint32(0), nil
1115 }
1116
1117 if Val, err = kvstore.ToByte(value.Value); err != nil {
1118 log.Error("Failed to convert to byte array")
1119 return uint32(0), err
1120 }
1121 if err = json.Unmarshal(Val, &gemPort); err != nil {
1122 log.Error("Failed to unmarshall")
1123 return uint32(0), err
1124 }
1125 log.Debugw("found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
1126
1127 return gemPort, nil
1128}
1129
1130// DelGemPortPktIn deletes the gemport from the pkt in path
1131func (RsrcMgr *OpenOltResourceMgr) DelGemPortPktIn(intfID uint32, onuID uint32, logicalPort uint32) error {
1132
1133 path := fmt.Sprintf(OnuPacketINPath, intfID, onuID, logicalPort)
1134 if err := RsrcMgr.KVStore.Delete(path); err != nil {
1135 log.Errorf("Falied to remove resource %s", path)
1136 return err
1137 }
1138 return nil
1139}
1140
1141// DelOnuGemInfoForIntf deletes the onugem info from kvstore per interface
1142func (RsrcMgr *OpenOltResourceMgr) DelOnuGemInfoForIntf(intfID uint32) error {
1143 if err := RsrcMgr.ResourceMgrs[intfID].DelOnuGemInfoForIntf(intfID); err != nil {
1144 log.Errorw("failed to delete onu gem info for", log.Fields{"intfid": intfID})
1145 return err
1146 }
1147 return nil
1148}
1149
1150//GetNNIFromKVStore gets NNi intfids from kvstore. path being per device
1151func (RsrcMgr *OpenOltResourceMgr) GetNNIFromKVStore() ([]uint32, error) {
1152
1153 var nni []uint32
1154 var Val []byte
1155
1156 path := fmt.Sprintf(NnniIntfID)
1157 value, err := RsrcMgr.KVStore.Get(path)
1158 if err != nil {
1159 log.Error("failed to get data from kv store")
1160 return nil, err
1161 }
1162 if value != nil {
1163 if Val, err = kvstore.ToByte(value.Value); err != nil {
1164 log.Error("Failed to convert to byte array")
1165 return nil, err
1166 }
1167 if err = json.Unmarshal(Val, &nni); err != nil {
1168 log.Error("Failed to unmarshall")
1169 return nil, err
1170 }
1171 }
1172 return nni, err
1173}
1174
1175// AddNNIToKVStore adds Nni interfaces to kvstore, path being per device.
1176func (RsrcMgr *OpenOltResourceMgr) AddNNIToKVStore(nniIntf uint32) error {
1177 var Value []byte
1178
1179 nni, err := RsrcMgr.GetNNIFromKVStore()
1180 if err != nil {
1181 log.Error("failed to fetch nni interfaces from kv store")
1182 return err
1183 }
1184
1185 path := fmt.Sprintf(NnniIntfID)
1186 nni = append(nni, nniIntf)
1187 Value, err = json.Marshal(nni)
1188 if err != nil {
1189 log.Error("Failed to marshal data")
1190 }
1191 if err = RsrcMgr.KVStore.Put(path, Value); err != nil {
1192 log.Errorw("Failed to put to kvstore", log.Fields{"path": path, "value": Value})
1193 return err
1194 }
1195 log.Debugw("added nni to kv successfully", log.Fields{"path": path, "nni": nniIntf})
1196 return nil
1197}
1198
1199// DelNNiFromKVStore deletes nni interface list from kv store.
1200func (RsrcMgr *OpenOltResourceMgr) DelNNiFromKVStore() error {
1201
1202 path := fmt.Sprintf(NnniIntfID)
1203
1204 if err := RsrcMgr.KVStore.Delete(path); err != nil {
1205 log.Errorw("Failed to delete nni interfaces from kv store", log.Fields{"path": path})
1206 return err
1207 }
1208 return nil
1209}