blob: db5f04494f05fba4498844a3f9dc0314e6f4e588 [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 (
npujarec5762e2020-01-01 14:08:48 +053021 "context"
Girish Gowdru0c588b22019-04-23 23:24:56 -040022 "encoding/json"
23 "errors"
24 "fmt"
25 "strconv"
26 "strings"
Abhilash S.L7f17e402019-03-15 17:40:41 +053027
Esin Karamanccb714b2019-11-29 15:02:06 +000028 "github.com/opencord/voltha-lib-go/v3/pkg/db"
29 "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
30 "github.com/opencord/voltha-lib-go/v3/pkg/log"
31 ponrmgr "github.com/opencord/voltha-lib-go/v3/pkg/ponresourcemanager"
32 ofp "github.com/opencord/voltha-protos/v3/go/openflow_13"
33 "github.com/opencord/voltha-protos/v3/go/openolt"
Abhilash S.L7f17e402019-03-15 17:40:41 +053034)
35
salmansiddiqui7ac62132019-08-22 03:58:50 +000036const (
37 // KvstoreTimeout specifies the time out for KV Store Connection
38 KvstoreTimeout = 5
39 // BasePathKvStore - service/voltha/openolt/<device_id>
40 BasePathKvStore = "service/voltha/openolt/{%s}"
Gamze Abakafee36392019-10-03 11:17:24 +000041 // TpIDPathSuffix - <(pon_id, onu_id, uni_id)>/tp_id
42 TpIDPathSuffix = "{%d,%d,%d}/tp_id"
43 //MeterIDPathSuffix - <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
44 MeterIDPathSuffix = "{%d,%d,%d}/{%d}/meter_id/{%s}"
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053045 //NnniIntfID - nniintfids
46 NnniIntfID = "nniintfids"
47 // OnuPacketINPath path on the kvstore to store packetin gemport,which will be used for packetin, pcketout
48 //format: onu_packetin/<intfid>,<onuid>,<logicalport>
49 OnuPacketINPath = "onu_packetin/{%d,%d,%d}"
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +053050 //FlowIDsForGem flowids_per_gem/<intfid>
51 FlowIDsForGem = "flowids_per_gem/{%d}"
Esin Karamanccb714b2019-11-29 15:02:06 +000052 //McastQueuesForIntf multicast queues for pon interfaces
53 McastQueuesForIntf = "mcast_qs_for_int"
54 //FlowGroup flow_groups/<flow_group_id>
55 // A group is stored under this path on the KV store after it has been installed to the device.
56 // It should also be deleted after it has been removed from the device accordingly.
57 FlowGroup = "flow_groups/{%d}"
58 //FlowGroupCached flow_groups_cached/<flow_group_id>
59 // When a group add request received, we create the group without setting any members to it since we cannot
60 // set any members to a group until it is associated with a multicast flow. It is a BAL limitation.
61 // When the related multicast flow has been created we perform set members operation for the group.
62 // That is why we need to keep the members of a group until the multicast flow creation request comes.
63 // We preserve the groups under "FlowGroupsCached" directory in the KV store temporarily. Having set members,
64 // we remove the group from the cached group store.
65 FlowGroupCached = "flow_groups_cached/{%d}"
salmansiddiqui7ac62132019-08-22 03:58:50 +000066)
Abhilash S.L7f17e402019-03-15 17:40:41 +053067
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070068// FlowInfo holds the flow information
Abhilash S.L8ee90712019-04-29 16:24:22 +053069type FlowInfo struct {
70 Flow *openolt.Flow
71 FlowStoreCookie uint64
72 FlowCategory string
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053073 LogicalFlowID uint64
74}
75
76// OnuGemInfo holds onu information along with gem port list and uni port list
77type OnuGemInfo struct {
78 OnuID uint32
79 SerialNumber string
80 IntfID uint32
81 GemPorts []uint32
82 UniPorts []uint32
83}
84
85// PacketInInfoKey is the key for packet in gemport
86type PacketInInfoKey struct {
87 IntfID uint32
88 OnuID uint32
89 LogicalPort uint32
Abhilash S.L8ee90712019-04-29 16:24:22 +053090}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070091
Esin Karamanccb714b2019-11-29 15:02:06 +000092// GroupInfo holds group information
93type GroupInfo struct {
94 GroupID uint32
95 OutPorts []uint32
96}
97
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070098// OpenOltResourceMgr holds resource related information as provided below for each field
Abhilash S.L7f17e402019-03-15 17:40:41 +053099type OpenOltResourceMgr struct {
sbarbaria8910ba2019-11-05 10:12:23 -0500100 DeviceID string // OLT device id
101 HostAndPort string // Host and port of the kv store to connect to
102 Args string // args
103 KVStore *db.Backend // backend kv store connection handle
Girish Gowdru0c588b22019-04-23 23:24:56 -0400104 DeviceType string
105 Host string // Host ip of the kv store
106 Port int // port of the kv store
107 DevInfo *openolt.DeviceInfo // device information
108 // array of pon resource managers per interface technology
109 ResourceMgrs map[uint32]*ponrmgr.PONResourceManager
Abhilash S.L7f17e402019-03-15 17:40:41 +0530110}
111
Manikkaraj kb1d51442019-07-23 10:41:02 -0400112func newKVClient(storeType string, address string, timeout uint32) (kvstore.Client, error) {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400113 log.Infow("kv-store-type", log.Fields{"store": storeType})
114 switch storeType {
115 case "consul":
116 return kvstore.NewConsulClient(address, int(timeout))
117 case "etcd":
118 return kvstore.NewEtcdClient(address, int(timeout))
119 }
120 return nil, errors.New("unsupported-kv-store")
Abhilash S.L7f17e402019-03-15 17:40:41 +0530121}
122
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700123// SetKVClient sets the KV client and return a kv backend
sbarbaria8910ba2019-11-05 10:12:23 -0500124func SetKVClient(backend string, Host string, Port int, DeviceID string) *db.Backend {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400125 addr := Host + ":" + strconv.Itoa(Port)
126 // TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
127 // issue between kv store and backend , core is not calling NewBackend directly
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700128 kvClient, err := newKVClient(backend, addr, KvstoreTimeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400129 if err != nil {
130 log.Fatalw("Failed to init KV client\n", log.Fields{"err": err})
131 return nil
132 }
sbarbaria8910ba2019-11-05 10:12:23 -0500133 kvbackend := &db.Backend{
Girish Gowdru0c588b22019-04-23 23:24:56 -0400134 Client: kvClient,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700135 StoreType: backend,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400136 Host: Host,
137 Port: Port,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700138 Timeout: KvstoreTimeout,
139 PathPrefix: fmt.Sprintf(BasePathKvStore, DeviceID)}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530140
Girish Gowdru0c588b22019-04-23 23:24:56 -0400141 return kvbackend
Abhilash S.L7f17e402019-03-15 17:40:41 +0530142}
143
Gamze Abakafee36392019-10-03 11:17:24 +0000144// NewResourceMgr init a New resource manager instance which in turn instantiates pon resource manager
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700145// instances according to technology. Initializes the default resource ranges for all
146// the resources.
npujarec5762e2020-01-01 14:08:48 +0530147func NewResourceMgr(ctx context.Context, deviceID string, KVStoreHostPort string, kvStoreType string, deviceType string, devInfo *openolt.DeviceInfo) *OpenOltResourceMgr {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400148 var ResourceMgr OpenOltResourceMgr
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700149 log.Debugf("Init new resource manager , host_port: %s, deviceid: %s", KVStoreHostPort, deviceID)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530150 ResourceMgr.HostAndPort = KVStoreHostPort
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700151 ResourceMgr.DeviceType = deviceType
152 ResourceMgr.DevInfo = devInfo
153 IPPort := strings.Split(KVStoreHostPort, ":")
154 ResourceMgr.Host = IPPort[0]
155 ResourceMgr.Port, _ = strconv.Atoi(IPPort[1])
Abhilash S.L7f17e402019-03-15 17:40:41 +0530156
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700157 Backend := kvStoreType
Girish Gowdru0c588b22019-04-23 23:24:56 -0400158 ResourceMgr.KVStore = SetKVClient(Backend, ResourceMgr.Host,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700159 ResourceMgr.Port, deviceID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400160 if ResourceMgr.KVStore == nil {
161 log.Error("Failed to setup KV store")
162 }
163 Ranges := make(map[string]*openolt.DeviceInfo_DeviceResourceRanges)
164 RsrcMgrsByTech := make(map[string]*ponrmgr.PONResourceManager)
165 ResourceMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530166
Girish Gowdru0c588b22019-04-23 23:24:56 -0400167 // TODO self.args = registry('main').get_args()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530168
Girish Gowdru0c588b22019-04-23 23:24:56 -0400169 /*
170 If a legacy driver returns protobuf without any ranges,s synthesize one from
Gamze Abakafee36392019-10-03 11:17:24 +0000171 the legacy global per-device information. This, in theory, is temporary until
Girish Gowdru0c588b22019-04-23 23:24:56 -0400172 the legacy drivers are upgrade to support pool ranges.
173 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700174 if devInfo.Ranges == nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400175 var ranges openolt.DeviceInfo_DeviceResourceRanges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700176 ranges.Technology = devInfo.GetTechnology()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530177
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700178 NumPONPorts := devInfo.GetPonPorts()
Girish Gowdru0c588b22019-04-23 23:24:56 -0400179 var index uint32
180 for index = 0; index < NumPONPorts; index++ {
181 ranges.IntfIds = append(ranges.IntfIds, index)
182 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530183
Abhilash S.L8ee90712019-04-29 16:24:22 +0530184 var Pool openolt.DeviceInfo_DeviceResourceRanges_Pool
Girish Gowdru0c588b22019-04-23 23:24:56 -0400185 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700186 Pool.Start = devInfo.OnuIdStart
187 Pool.End = devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400188 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
cbabuabf02352019-10-15 13:14:56 +0200189 onuPool := Pool
190 ranges.Pools = append(ranges.Pools, &onuPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530191
Girish Gowdru0c588b22019-04-23 23:24:56 -0400192 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700193 Pool.Start = devInfo.AllocIdStart
194 Pool.End = devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400195 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200196 allocPool := Pool
197 ranges.Pools = append(ranges.Pools, &allocPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530198
Girish Gowdru0c588b22019-04-23 23:24:56 -0400199 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700200 Pool.Start = devInfo.GemportIdStart
201 Pool.End = devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400202 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200203 gemPool := Pool
204 ranges.Pools = append(ranges.Pools, &gemPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530205
Girish Gowdru0c588b22019-04-23 23:24:56 -0400206 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700207 Pool.Start = devInfo.FlowIdStart
208 Pool.End = devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400209 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
Abhilash S.L8ee90712019-04-29 16:24:22 +0530210 ranges.Pools = append(ranges.Pools, &Pool)
211 // Add to device info
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700212 devInfo.Ranges = append(devInfo.Ranges, &ranges)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400213 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530214
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700215 // Create a separate Resource Manager instance for each range. This assumes that
Girish Gowdru0c588b22019-04-23 23:24:56 -0400216 // each technology is represented by only a single range
217 var GlobalPONRsrcMgr *ponrmgr.PONResourceManager
218 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700219 for _, TechRange := range devInfo.Ranges {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400220 technology := TechRange.Technology
221 log.Debugf("Device info technology %s", technology)
222 Ranges[technology] = TechRange
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700223 RsrcMgrsByTech[technology], err = ponrmgr.NewPONResourceManager(technology, deviceType, deviceID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400224 Backend, ResourceMgr.Host, ResourceMgr.Port)
225 if err != nil {
Gamze Abakafee36392019-10-03 11:17:24 +0000226 log.Errorf("Failed to create pon resource manager instance for technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400227 return nil
228 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700229 // resource_mgrs_by_tech[technology] = resource_mgr
Girish Gowdru0c588b22019-04-23 23:24:56 -0400230 if GlobalPONRsrcMgr == nil {
231 GlobalPONRsrcMgr = RsrcMgrsByTech[technology]
232 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700233 for _, IntfID := range TechRange.IntfIds {
234 ResourceMgr.ResourceMgrs[uint32(IntfID)] = RsrcMgrsByTech[technology]
Girish Gowdru0c588b22019-04-23 23:24:56 -0400235 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700236 // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
npujarec5762e2020-01-01 14:08:48 +0530237 InitializeDeviceResourceRangeAndPool(ctx, RsrcMgrsByTech[technology], GlobalPONRsrcMgr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700238 TechRange, devInfo)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400239 }
240 // After we have initialized resource ranges, initialize the
241 // resource pools accordingly.
242 for _, PONRMgr := range RsrcMgrsByTech {
npujarec5762e2020-01-01 14:08:48 +0530243 _ = PONRMgr.InitDeviceResourcePool(ctx)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400244 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530245 log.Info("Initialization of resource manager success!")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400246 return &ResourceMgr
Abhilash S.L7f17e402019-03-15 17:40:41 +0530247}
248
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700249// InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply
250// device specific information. If KV doesn't exist
251// or is broader than the device, the device's information will
252// dictate the range limits
npujarec5762e2020-01-01 14:08:48 +0530253func InitializeDeviceResourceRangeAndPool(ctx context.Context, ponRMgr *ponrmgr.PONResourceManager, globalPONRMgr *ponrmgr.PONResourceManager,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700254 techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530255
Girish Gowdru0c588b22019-04-23 23:24:56 -0400256 // init the resource range pool according to the sharing type
Abhilash S.L7f17e402019-03-15 17:40:41 +0530257
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700258 log.Debugf("Resource range pool init for technology %s", ponRMgr.Technology)
259 // first load from KV profiles
npujarec5762e2020-01-01 14:08:48 +0530260 status := ponRMgr.InitResourceRangesFromKVStore(ctx)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700261 if !status {
262 log.Debugf("Failed to load resource ranges from KV store for tech %s", ponRMgr.Technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400263 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530264
Girish Gowdru0c588b22019-04-23 23:24:56 -0400265 /*
266 Then apply device specific information. If KV doesn't exist
Gamze Abakafee36392019-10-03 11:17:24 +0000267 or is broader than the device, the device's information will
Girish Gowdru0c588b22019-04-23 23:24:56 -0400268 dictate the range limits
269 */
Girish Kumar8f73fe02019-12-09 13:19:37 +0000270 log.Debugw("Using device info to init pon resource ranges", log.Fields{"Tech": ponRMgr.Technology})
Abhilash S.L7f17e402019-03-15 17:40:41 +0530271
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700272 ONUIDStart := devInfo.OnuIdStart
273 ONUIDEnd := devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400274 ONUIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
275 ONUIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700276 AllocIDStart := devInfo.AllocIdStart
277 AllocIDEnd := devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400278 AllocIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
279 AllocIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700280 GEMPortIDStart := devInfo.GemportIdStart
281 GEMPortIDEnd := devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400282 GEMPortIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
283 GEMPortIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700284 FlowIDStart := devInfo.FlowIdStart
285 FlowIDEnd := devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400286 FlowIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
287 FlowIDSharedPoolID := uint32(0)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530288
Girish Gowdru0c588b22019-04-23 23:24:56 -0400289 var FirstIntfPoolID uint32
290 var SharedPoolID uint32
Abhilash S.L7f17e402019-03-15 17:40:41 +0530291
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400292 /*
293 * 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 -0700294 * if resources are shared across interfaces then SharedPoolID is given a positive number.
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400295 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700296 for _, FirstIntfPoolID = range techRange.IntfIds {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400297 // skip the intf id 0
298 if FirstIntfPoolID == 0 {
299 continue
300 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400301 break
302 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530303
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700304 for _, RangePool := range techRange.Pools {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400305 if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400306 SharedPoolID = FirstIntfPoolID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400307 } else if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_SAME_TECH {
308 SharedPoolID = FirstIntfPoolID
309 } else {
310 SharedPoolID = 0
311 }
312 if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID {
313 ONUIDStart = RangePool.Start
314 ONUIDEnd = RangePool.End
315 ONUIDShared = RangePool.Sharing
316 ONUIDSharedPoolID = SharedPoolID
317 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID {
318 AllocIDStart = RangePool.Start
319 AllocIDEnd = RangePool.End
320 AllocIDShared = RangePool.Sharing
321 AllocIDSharedPoolID = SharedPoolID
322 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID {
323 GEMPortIDStart = RangePool.Start
324 GEMPortIDEnd = RangePool.End
325 GEMPortIDShared = RangePool.Sharing
326 GEMPortIDSharedPoolID = SharedPoolID
327 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID {
328 FlowIDStart = RangePool.Start
329 FlowIDEnd = RangePool.End
330 FlowIDShared = RangePool.Sharing
331 FlowIDSharedPoolID = SharedPoolID
332 }
333 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530334
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700335 log.Debugw("Device info init", log.Fields{"technology": techRange.Technology,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400336 "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd, "onu_id_shared_pool_id": ONUIDSharedPoolID,
337 "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd,
338 "alloc_id_shared_pool_id": AllocIDSharedPoolID,
339 "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd,
340 "gemport_id_shared_pool_id": GEMPortIDSharedPoolID,
341 "flow_id_start": FlowIDStart,
342 "flow_id_end_idx": FlowIDEnd,
343 "flow_id_shared_pool_id": FlowIDSharedPoolID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700344 "intf_ids": techRange.IntfIds,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400345 "uni_id_start": 0,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700346 "uni_id_end_idx": 1, /*MaxUNIIDperONU()*/
347 })
Abhilash S.L7f17e402019-03-15 17:40:41 +0530348
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700349 ponRMgr.InitDefaultPONResourceRanges(ONUIDStart, ONUIDEnd, ONUIDSharedPoolID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400350 AllocIDStart, AllocIDEnd, AllocIDSharedPoolID,
351 GEMPortIDStart, GEMPortIDEnd, GEMPortIDSharedPoolID,
352 FlowIDStart, FlowIDEnd, FlowIDSharedPoolID, 0, 1,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700353 devInfo.PonPorts, techRange.IntfIds)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530354
Girish Gowdru0c588b22019-04-23 23:24:56 -0400355 // For global sharing, make sure to refresh both local and global resource manager instances' range
Abhilash S.L7f17e402019-03-15 17:40:41 +0530356
Girish Gowdru0c588b22019-04-23 23:24:56 -0400357 if ONUIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700358 globalPONRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400359 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700360 ponRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
361 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400362 }
363 if AllocIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700364 globalPONRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400365 "", 0, nil)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530366
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700367 ponRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
368 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400369 }
370 if GEMPortIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700371 globalPONRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400372 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700373 ponRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
374 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400375 }
376 if FlowIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700377 globalPONRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400378 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700379 ponRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
380 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400381 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530382
Girish Gowdru0c588b22019-04-23 23:24:56 -0400383 // Make sure loaded range fits the platform bit encoding ranges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700384 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 +0530385}
386
Devmalya Paul495b94a2019-08-27 19:42:00 -0400387// Delete clears used resources for the particular olt device being deleted
npujarec5762e2020-01-01 14:08:48 +0530388func (RsrcMgr *OpenOltResourceMgr) Delete(ctx context.Context) error {
Devmalya Paul495b94a2019-08-27 19:42:00 -0400389 /* TODO
390 def __del__(self):
391 self.log.info("clearing-device-resource-pool")
392 for key, resource_mgr in self.resource_mgrs.iteritems():
393 resource_mgr.clear_device_resource_pool()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530394
Devmalya Paul495b94a2019-08-27 19:42:00 -0400395 def assert_pon_id_limit(self, pon_intf_id):
396 assert pon_intf_id in self.resource_mgrs
Abhilash S.L7f17e402019-03-15 17:40:41 +0530397
Devmalya Paul495b94a2019-08-27 19:42:00 -0400398 def assert_onu_id_limit(self, pon_intf_id, onu_id):
399 self.assert_pon_id_limit(pon_intf_id)
400 self.resource_mgrs[pon_intf_id].assert_resource_limits(onu_id, PONResourceManager.ONU_ID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530401
Devmalya Paul495b94a2019-08-27 19:42:00 -0400402 @property
403 def max_uni_id_per_onu(self):
404 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 +0530405
Devmalya Paul495b94a2019-08-27 19:42:00 -0400406 def assert_uni_id_limit(self, pon_intf_id, onu_id, uni_id):
407 self.assert_onu_id_limit(pon_intf_id, onu_id)
408 self.resource_mgrs[pon_intf_id].assert_resource_limits(uni_id, PONResourceManager.UNI_ID)
409 */
410 for _, rsrcMgr := range RsrcMgr.ResourceMgrs {
npujarec5762e2020-01-01 14:08:48 +0530411 if err := rsrcMgr.ClearDeviceResourcePool(ctx); err != nil {
Devmalya Paul495b94a2019-08-27 19:42:00 -0400412 log.Debug("Failed to clear device resource pool")
413 return err
414 }
415 }
416 log.Debug("Cleared device resource pool")
417 return nil
418}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530419
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700420// GetONUID returns the available OnuID for the given pon-port
npujarec5762e2020-01-01 14:08:48 +0530421func (RsrcMgr *OpenOltResourceMgr) GetONUID(ctx context.Context, ponIntfID uint32) (uint32, error) {
salmansiddiqui352a45c2019-08-19 10:15:36 +0000422 // Check if Pon Interface ID is present in Resource-manager-map
423 if _, ok := RsrcMgr.ResourceMgrs[ponIntfID]; !ok {
424 err := errors.New("invalid-pon-interface-" + strconv.Itoa(int(ponIntfID)))
425 return 0, err
426 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400427 // Get ONU id for a provided pon interface ID.
npujarec5762e2020-01-01 14:08:48 +0530428 ONUID, err := RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ctx, ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400429 ponrmgr.ONU_ID, 1)
430 if err != nil {
431 log.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700432 ponIntfID, ponrmgr.ONU_ID)
cbabuabf02352019-10-15 13:14:56 +0200433 return 0, err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400434 }
435 if ONUID != nil {
npujarec5762e2020-01-01 14:08:48 +0530436 RsrcMgr.ResourceMgrs[ponIntfID].InitResourceMap(ctx, fmt.Sprintf("%d,%d", ponIntfID, ONUID[0]))
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700437 return ONUID[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400438 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530439
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700440 return 0, err // return OnuID 0 on error
Abhilash S.L7f17e402019-03-15 17:40:41 +0530441}
442
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700443// GetFlowIDInfo returns the slice of flow info of the given pon-port
444// Note: For flows which trap from the NNI and not really associated with any particular
445// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
npujarec5762e2020-01-01 14:08:48 +0530446func (RsrcMgr *OpenOltResourceMgr) GetFlowIDInfo(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32, flowID uint32) *[]FlowInfo {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530447 var flows []FlowInfo
448
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700449 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530450 if err := RsrcMgr.ResourceMgrs[ponIntfID].GetFlowIDInfo(ctx, FlowPath, flowID, &flows); err != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700451 log.Errorw("Error while getting flows from KV store", log.Fields{"flowId": flowID})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530452 return nil
453 }
454 if len(flows) == 0 {
455 log.Debugw("No flowInfo found in KV store", log.Fields{"flowPath": FlowPath})
456 return nil
457 }
458 return &flows
459}
460
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700461// GetCurrentFlowIDsForOnu fetches flow ID from the resource manager
462// Note: For flows which trap from the NNI and not really associated with any particular
463// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
npujarec5762e2020-01-01 14:08:48 +0530464func (RsrcMgr *OpenOltResourceMgr) GetCurrentFlowIDsForOnu(ctx context.Context, PONIntfID uint32, ONUID int32, UNIID int32) []uint32 {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700465
Abhilash S.L8ee90712019-04-29 16:24:22 +0530466 FlowPath := fmt.Sprintf("%d,%d,%d", PONIntfID, ONUID, UNIID)
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700467 if mgrs, exist := RsrcMgr.ResourceMgrs[PONIntfID]; exist {
npujarec5762e2020-01-01 14:08:48 +0530468 return mgrs.GetCurrentFlowIDsForOnu(ctx, FlowPath)
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700469 }
470 return nil
Abhilash S.L8ee90712019-04-29 16:24:22 +0530471}
472
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700473// UpdateFlowIDInfo updates flow info for the given pon interface, onu id, and uni id
474// Note: For flows which trap from the NNI and not really associated with any particular
475// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
npujarec5762e2020-01-01 14:08:48 +0530476func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDInfo(ctx context.Context, ponIntfID int32, onuID int32, uniID int32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700477 flowID uint32, flowData *[]FlowInfo) error {
478 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530479 return RsrcMgr.ResourceMgrs[uint32(ponIntfID)].UpdateFlowIDInfoForOnu(ctx, FlowPath, flowID, *flowData)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530480}
481
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700482// GetFlowID return flow ID for a given pon interface id, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530483func (RsrcMgr *OpenOltResourceMgr) GetFlowID(ctx context.Context, ponIntfID uint32, ONUID int32, uniID int32,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400484 gemportID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700485 flowStoreCookie uint64,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400486 flowCategory string, vlanPcp ...uint32) (uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530487
Girish Gowdru0c588b22019-04-23 23:24:56 -0400488 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700489 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, ONUID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530490 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(ctx, FlowPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400491 if FlowIDs != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700492 log.Debugw("Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "ONUID": ONUID, "uniID": uniID, "KVpath": FlowPath})
493 for _, flowID := range FlowIDs {
npujarec5762e2020-01-01 14:08:48 +0530494 FlowInfo := RsrcMgr.GetFlowIDInfo(ctx, ponIntfID, int32(ONUID), int32(uniID), uint32(flowID))
salmansiddiqui7ac62132019-08-22 03:58:50 +0000495 er := getFlowIDFromFlowInfo(FlowInfo, flowID, gemportID, flowStoreCookie, flowCategory, vlanPcp...)
496 if er == nil {
497 return flowID, er
Abhilash S.L8ee90712019-04-29 16:24:22 +0530498 }
499 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400500 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530501 log.Debug("No matching flows with flow cookie or flow category, allocating new flowid")
npujarec5762e2020-01-01 14:08:48 +0530502 FlowIDs, err = RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ctx, ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400503 ponrmgr.FLOW_ID, 1)
504 if err != nil {
505 log.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700506 ponIntfID, ponrmgr.FLOW_ID)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400507 return uint32(0), err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400508 }
509 if FlowIDs != nil {
npujarec5762e2020-01-01 14:08:48 +0530510 _ = RsrcMgr.ResourceMgrs[ponIntfID].UpdateFlowIDForOnu(ctx, FlowPath, FlowIDs[0], true)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700511 return FlowIDs[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400512 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530513
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700514 return 0, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530515}
516
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700517// GetAllocID return the first Alloc ID for a given pon interface id and onu id and then update the resource map on
518// the KV store with the list of alloc_ids allocated for the pon_intf_onu_id tuple
519// Currently of all the alloc_ids available, it returns the first alloc_id in the list for tha given ONU
npujarec5762e2020-01-01 14:08:48 +0530520func (RsrcMgr *OpenOltResourceMgr) GetAllocID(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530521
Girish Gowdru0c588b22019-04-23 23:24:56 -0400522 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700523 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530524 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400525 if AllocID != nil {
526 // Since we support only one alloc_id for the ONU at the moment,
527 // return the first alloc_id in the list, if available, for that
528 // ONU.
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700529 log.Debugw("Retrieved alloc ID from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400530 return AllocID[0]
531 }
npujarec5762e2020-01-01 14:08:48 +0530532 AllocID, err = RsrcMgr.ResourceMgrs[intfID].GetResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400533 ponrmgr.ALLOC_ID, 1)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530534
Girish Gowdru0c588b22019-04-23 23:24:56 -0400535 if AllocID == nil || err != nil {
536 log.Error("Failed to allocate alloc id")
537 return 0
538 }
539 // update the resource map on KV store with the list of alloc_id
540 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530541 err = RsrcMgr.ResourceMgrs[intfID].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID, AllocID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400542 if err != nil {
543 log.Error("Failed to update Alloc ID")
544 return 0
545 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530546 log.Debugw("Allocated new Tcont from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400547 return AllocID[0]
Abhilash S.L7f17e402019-03-15 17:40:41 +0530548}
549
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700550// UpdateAllocIdsForOnu updates alloc ids in kv store for a given pon interface id, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530551func (RsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ctx context.Context, ponPort uint32, onuID uint32, uniID uint32, allocID []uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530552
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700553 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530554 return RsrcMgr.ResourceMgrs[ponPort].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700555 allocID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530556}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700557
558// GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530559func (RsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(ctx context.Context, intfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700560 uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530561
Girish Gowdru0c588b22019-04-23 23:24:56 -0400562 /* Get gem ports for given pon interface , onu id and uni id. */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530563
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700564 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530565 return RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530566}
567
Gamze Abakafee36392019-10-03 11:17:24 +0000568// GetCurrentAllocIDsForOnu returns alloc ids for given pon interface and onu id
npujarec5762e2020-01-01 14:08:48 +0530569func (RsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDsForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530570
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700571 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530572 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400573 if AllocID != nil {
Gamze Abakafee36392019-10-03 11:17:24 +0000574 return AllocID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400575 }
Gamze Abakafee36392019-10-03 11:17:24 +0000576 return []uint32{}
577}
578
579// RemoveAllocIDForOnu removes the alloc id for given pon interface, onu id, uni id and alloc id
npujarec5762e2020-01-01 14:08:48 +0530580func (RsrcMgr *OpenOltResourceMgr) RemoveAllocIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, allocID uint32) {
581 allocIDs := RsrcMgr.GetCurrentAllocIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000582 for i := 0; i < len(allocIDs); i++ {
583 if allocIDs[i] == allocID {
584 allocIDs = append(allocIDs[:i], allocIDs[i+1:]...)
585 break
586 }
587 }
npujarec5762e2020-01-01 14:08:48 +0530588 err := RsrcMgr.UpdateAllocIdsForOnu(ctx, intfID, onuID, uniID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000589 if err != nil {
590 log.Errorf("Failed to Remove Alloc Id For Onu. IntfID %d onuID %d uniID %d allocID %d",
591 intfID, onuID, uniID, allocID)
592 }
593}
594
595// RemoveGemPortIDForOnu removes the gem port id for given pon interface, onu id, uni id and gem port id
npujarec5762e2020-01-01 14:08:48 +0530596func (RsrcMgr *OpenOltResourceMgr) RemoveGemPortIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, gemPortID uint32) {
597 gemPortIDs := RsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000598 for i := 0; i < len(gemPortIDs); i++ {
599 if gemPortIDs[i] == gemPortID {
600 gemPortIDs = append(gemPortIDs[:i], gemPortIDs[i+1:]...)
601 break
602 }
603 }
npujarec5762e2020-01-01 14:08:48 +0530604 err := RsrcMgr.UpdateGEMPortIDsForOnu(ctx, intfID, onuID, uniID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000605 if err != nil {
606 log.Errorf("Failed to Remove Gem Id For Onu. IntfID %d onuID %d uniID %d gemPortId %d",
607 intfID, onuID, uniID, gemPortID)
608 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530609}
610
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700611// UpdateGEMportsPonportToOnuMapOnKVStore updates onu and uni id associated with the gem port to the kv store
612// This stored information is used when packet_indication is received and we need to derive the ONU Id for which
613// the packet arrived based on the pon_intf and gemport available in the packet_indication
npujarec5762e2020-01-01 14:08:48 +0530614func (RsrcMgr *OpenOltResourceMgr) UpdateGEMportsPonportToOnuMapOnKVStore(ctx context.Context, gemPorts []uint32, PonPort uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700615 onuID uint32, uniID uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530616
Girish Gowdru0c588b22019-04-23 23:24:56 -0400617 /* Update onu and uni id associated with the gem port to the kv store. */
618 var IntfGEMPortPath string
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700619 Data := fmt.Sprintf("%d %d", onuID, uniID)
620 for _, GEM := range gemPorts {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400621 IntfGEMPortPath = fmt.Sprintf("%d,%d", PonPort, GEM)
622 Val, err := json.Marshal(Data)
623 if err != nil {
624 log.Error("failed to Marshal")
625 return err
626 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700627
npujarec5762e2020-01-01 14:08:48 +0530628 if err = RsrcMgr.KVStore.Put(ctx, IntfGEMPortPath, Val); err != nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400629 log.Errorf("Failed to update resource %s", IntfGEMPortPath)
630 return err
631 }
632 }
633 return nil
Abhilash S.L7f17e402019-03-15 17:40:41 +0530634}
635
Gamze Abakafee36392019-10-03 11:17:24 +0000636// RemoveGEMportPonportToOnuMapOnKVStore removes the relationship between the gem port and pon port
npujarec5762e2020-01-01 14:08:48 +0530637func (RsrcMgr *OpenOltResourceMgr) RemoveGEMportPonportToOnuMapOnKVStore(ctx context.Context, GemPort uint32, PonPort uint32) {
Gamze Abakafee36392019-10-03 11:17:24 +0000638 IntfGEMPortPath := fmt.Sprintf("%d,%d", PonPort, GemPort)
npujarec5762e2020-01-01 14:08:48 +0530639 err := RsrcMgr.KVStore.Delete(ctx, IntfGEMPortPath)
Gamze Abakafee36392019-10-03 11:17:24 +0000640 if err != nil {
641 log.Errorf("Failed to Remove Gem port-Pon port to onu map on kv store. Gem %d PonPort %d", GemPort, PonPort)
642 }
643}
644
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700645// GetGEMPortID gets gem port id for a particular pon port, onu id and uni id and then update the resource map on
646// the KV store with the list of gemport_id allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530647func (RsrcMgr *OpenOltResourceMgr) GetGEMPortID(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700648 uniID uint32, NumOfPorts uint32) ([]uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530649
Girish Gowdru0c588b22019-04-23 23:24:56 -0400650 /* Get gem port id for a particular pon port, onu id
651 and uni id.
652 */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530653
Girish Gowdru0c588b22019-04-23 23:24:56 -0400654 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700655 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530656
npujarec5762e2020-01-01 14:08:48 +0530657 GEMPortList := RsrcMgr.ResourceMgrs[ponPort].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400658 if GEMPortList != nil {
659 return GEMPortList, nil
660 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530661
npujarec5762e2020-01-01 14:08:48 +0530662 GEMPortList, err = RsrcMgr.ResourceMgrs[ponPort].GetResourceID(ctx, ponPort,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400663 ponrmgr.GEMPORT_ID, NumOfPorts)
664 if err != nil && GEMPortList == nil {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530665 log.Errorf("Failed to get gem port id for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400666 return nil, err
667 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530668
Girish Gowdru0c588b22019-04-23 23:24:56 -0400669 // update the resource map on KV store with the list of gemport_id
670 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530671 err = RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400672 GEMPortList)
673 if err != nil {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530674 log.Errorf("Failed to update GEM ports to kv store for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400675 return nil, err
676 }
npujarec5762e2020-01-01 14:08:48 +0530677 _ = RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(ctx, GEMPortList, ponPort,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700678 onuID, uniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400679 return GEMPortList, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530680}
681
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700682// UpdateGEMPortIDsForOnu updates gemport ids on to the kv store for a given pon port, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530683func (RsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700684 uniID uint32, GEMPortList []uint32) error {
685 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530686 return RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400687 GEMPortList)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530688
689}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700690
691// FreeonuID releases(make free) onu id for a particular pon-port
npujarec5762e2020-01-01 14:08:48 +0530692func (RsrcMgr *OpenOltResourceMgr) FreeonuID(ctx context.Context, intfID uint32, onuID []uint32) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700693
npujarec5762e2020-01-01 14:08:48 +0530694 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID, ponrmgr.ONU_ID, onuID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530695
Girish Gowdru0c588b22019-04-23 23:24:56 -0400696 /* Free onu id for a particular interface.*/
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700697 var IntfonuID string
698 for _, onu := range onuID {
699 IntfonuID = fmt.Sprintf("%d,%d", intfID, onu)
npujarec5762e2020-01-01 14:08:48 +0530700 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfonuID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400701 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530702}
703
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700704// FreeFlowID returns the free flow id for a given interface, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530705func (RsrcMgr *OpenOltResourceMgr) FreeFlowID(ctx context.Context, IntfID uint32, onuID int32,
Devmalya Paul495b94a2019-08-27 19:42:00 -0400706 uniID int32, FlowID uint32) {
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400707 var IntfONUID string
708 var err error
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530709 FlowIds := make([]uint32, 0)
710
711 FlowIds = append(FlowIds, FlowID)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700712 IntfONUID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530713 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(ctx, IntfONUID, FlowID, false)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400714 if err != nil {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530715 log.Errorw("Failed to Update flow id for", log.Fields{"intf": IntfONUID})
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400716 }
npujarec5762e2020-01-01 14:08:48 +0530717 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(ctx, IntfONUID, FlowID)
718 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.FLOW_ID, FlowIds)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400719}
720
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700721// FreeFlowIDs releases the flow Ids
npujarec5762e2020-01-01 14:08:48 +0530722func (RsrcMgr *OpenOltResourceMgr) FreeFlowIDs(ctx context.Context, IntfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700723 uniID uint32, FlowID []uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530724
npujarec5762e2020-01-01 14:08:48 +0530725 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.FLOW_ID, FlowID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530726
Abhilash S.L8ee90712019-04-29 16:24:22 +0530727 var IntfOnuIDUniID string
Girish Gowdru0c588b22019-04-23 23:24:56 -0400728 var err error
729 for _, flow := range FlowID {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700730 IntfOnuIDUniID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530731 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(ctx, IntfOnuIDUniID, flow, false)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400732 if err != nil {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530733 log.Errorw("Failed to Update flow id for", log.Fields{"intf": IntfOnuIDUniID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400734 }
npujarec5762e2020-01-01 14:08:48 +0530735 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(ctx, IntfOnuIDUniID, flow)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400736 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530737}
738
Gamze Abakafee36392019-10-03 11:17:24 +0000739// FreeAllocID frees AllocID on the PON resource pool and also frees the allocID association
740// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530741func (RsrcMgr *OpenOltResourceMgr) FreeAllocID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000742 uniID uint32, allocID uint32) {
npujarec5762e2020-01-01 14:08:48 +0530743 RsrcMgr.RemoveAllocIDForOnu(ctx, IntfID, onuID, uniID, allocID)
Gamze Abakafee36392019-10-03 11:17:24 +0000744 allocIDs := make([]uint32, 0)
745 allocIDs = append(allocIDs, allocID)
npujarec5762e2020-01-01 14:08:48 +0530746 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.ALLOC_ID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000747}
748
749// FreeGemPortID frees GemPortID on the PON resource pool and also frees the gemPortID association
750// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530751func (RsrcMgr *OpenOltResourceMgr) FreeGemPortID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000752 uniID uint32, gemPortID uint32) {
npujarec5762e2020-01-01 14:08:48 +0530753 RsrcMgr.RemoveGemPortIDForOnu(ctx, IntfID, onuID, uniID, gemPortID)
Gamze Abakafee36392019-10-03 11:17:24 +0000754 gemPortIDs := make([]uint32, 0)
755 gemPortIDs = append(gemPortIDs, gemPortID)
npujarec5762e2020-01-01 14:08:48 +0530756 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.GEMPORT_ID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000757}
758
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700759// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id, and the clears the
760// resource map and the onuID associated with (pon_intf_id, gemport_id) tuple,
npujarec5762e2020-01-01 14:08:48 +0530761func (RsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530762
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700763 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530764
npujarec5762e2020-01-01 14:08:48 +0530765 AllocIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530766
npujarec5762e2020-01-01 14:08:48 +0530767 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400768 ponrmgr.ALLOC_ID,
769 AllocIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530770
npujarec5762e2020-01-01 14:08:48 +0530771 GEMPortIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
772 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400773 ponrmgr.GEMPORT_ID,
774 GEMPortIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530775
npujarec5762e2020-01-01 14:08:48 +0530776 FlowIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentFlowIDsForOnu(ctx, IntfOnuIDUniID)
777 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400778 ponrmgr.FLOW_ID,
779 FlowIDs)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400780 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
npujarec5762e2020-01-01 14:08:48 +0530781 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400782 // Clear the ONU Id associated with the (pon_intf_id, gemport_id) tuple.
783 for _, GEM := range GEMPortIDs {
npujarec5762e2020-01-01 14:08:48 +0530784 _ = RsrcMgr.KVStore.Delete(ctx, fmt.Sprintf("%d,%d", intfID, GEM))
Girish Gowdru0c588b22019-04-23 23:24:56 -0400785 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530786}
787
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700788// IsFlowCookieOnKVStore checks if the given flow cookie is present on the kv store
789// Returns true if the flow cookie is found, otherwise it returns false
npujarec5762e2020-01-01 14:08:48 +0530790func (RsrcMgr *OpenOltResourceMgr) IsFlowCookieOnKVStore(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700791 flowStoreCookie uint64) bool {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530792
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700793 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530794 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(ctx, FlowPath)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400795 if FlowIDs != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700796 log.Debugw("Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "onuID": onuID, "uniID": uniID, "KVpath": FlowPath})
797 for _, flowID := range FlowIDs {
npujarec5762e2020-01-01 14:08:48 +0530798 FlowInfo := RsrcMgr.GetFlowIDInfo(ctx, ponIntfID, int32(onuID), int32(uniID), uint32(flowID))
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400799 if FlowInfo != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700800 log.Debugw("Found flows", log.Fields{"flows": *FlowInfo, "flowId": flowID})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400801 for _, Info := range *FlowInfo {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700802 if Info.FlowStoreCookie == flowStoreCookie {
803 log.Debug("Found flow matching with flowStore cookie", log.Fields{"flowId": flowID, "flowStoreCookie": flowStoreCookie})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400804 return true
805 }
806 }
807 }
808 }
809 }
810 return false
811}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400812
salmansiddiqui7ac62132019-08-22 03:58:50 +0000813// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000814// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530815func (RsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) []uint32 {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000816 Path := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000817 var Data []uint32
npujarec5762e2020-01-01 14:08:48 +0530818 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400819 if err == nil {
820 if Value != nil {
821 Val, err := kvstore.ToByte(Value.Value)
822 if err != nil {
823 log.Errorw("Failed to convert into byte array", log.Fields{"error": err})
824 return Data
825 }
826 if err = json.Unmarshal(Val, &Data); err != nil {
827 log.Error("Failed to unmarshal", log.Fields{"error": err})
828 return Data
829 }
830 }
831 } else {
832 log.Errorf("Failed to get TP id from kvstore for path %s", Path)
833 }
834 log.Debugf("Getting TP id %d from path %s", Data, Path)
835 return Data
836
837}
838
Gamze Abakafee36392019-10-03 11:17:24 +0000839// RemoveTechProfileIDsForOnu deletes all tech profile ids from the KV-Store for the given onu based on the path
840// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530841func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDsForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) error {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000842 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
npujarec5762e2020-01-01 14:08:48 +0530843 if err := RsrcMgr.KVStore.Delete(ctx, IntfOnuUniID); err != nil {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530844 log.Errorw("Failed to delete techprofile id resource in KV store", log.Fields{"path": IntfOnuUniID})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400845 return err
846 }
847 return nil
848}
849
Gamze Abakafee36392019-10-03 11:17:24 +0000850// RemoveTechProfileIDForOnu deletes a specific tech profile id from the KV-Store for the given onu based on the path
851// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530852func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32, TpID uint32) error {
853 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000854 for i, tpIDInList := range tpIDList {
855 if tpIDInList == TpID {
856 tpIDList = append(tpIDList[:i], tpIDList[i+1:]...)
857 }
858 }
859 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
860 Value, err := json.Marshal(tpIDList)
861 if err != nil {
862 log.Error("failed to Marshal")
863 return err
864 }
npujarec5762e2020-01-01 14:08:48 +0530865 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Gamze Abakafee36392019-10-03 11:17:24 +0000866 log.Errorf("Failed to update resource %s", IntfOnuUniID)
867 return err
868 }
869 return err
870}
871
872// UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
873// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530874func (RsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32,
salmansiddiqui7ac62132019-08-22 03:58:50 +0000875 UniID uint32, TpID uint32) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400876 var Value []byte
877 var err error
878
salmansiddiqui7ac62132019-08-22 03:58:50 +0000879 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000880
npujarec5762e2020-01-01 14:08:48 +0530881 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000882 for _, value := range tpIDList {
883 if value == TpID {
884 log.Debugf("TpID %d is already in tpIdList for the path %s", TpID, IntfOnuUniID)
885 return err
886 }
887 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000888 log.Debugf("updating tp id %d on path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000889 tpIDList = append(tpIDList, TpID)
890 Value, err = json.Marshal(tpIDList)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400891 if err != nil {
892 log.Error("failed to Marshal")
893 return err
894 }
npujarec5762e2020-01-01 14:08:48 +0530895 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000896 log.Errorf("Failed to update resource %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400897 return err
898 }
899 return err
900}
901
salmansiddiqui7ac62132019-08-22 03:58:50 +0000902// UpdateMeterIDForOnu updates the meter id in the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000903// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +0530904func (RsrcMgr *OpenOltResourceMgr) UpdateMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000905 UniID uint32, TpID uint32, MeterConfig *ofp.OfpMeterConfig) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400906 var Value []byte
907 var err error
908
Gamze Abakafee36392019-10-03 11:17:24 +0000909 IntfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400910 Value, err = json.Marshal(*MeterConfig)
911 if err != nil {
912 log.Error("failed to Marshal meter config")
913 return err
914 }
npujarec5762e2020-01-01 14:08:48 +0530915 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000916 log.Errorf("Failed to store meter into KV store %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400917 return err
918 }
919 return err
920}
921
Gamze Abakafee36392019-10-03 11:17:24 +0000922// GetMeterIDForOnu fetches the meter id from the kv store for the given onu based on the path
923// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +0530924func (RsrcMgr *OpenOltResourceMgr) GetMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000925 UniID uint32, TpID uint32) (*ofp.OfpMeterConfig, error) {
926 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400927 var meterConfig ofp.OfpMeterConfig
npujarec5762e2020-01-01 14:08:48 +0530928 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400929 if err == nil {
930 if Value != nil {
931 log.Debug("Found meter in KV store", log.Fields{"Direction": Direction})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000932 Val, er := kvstore.ToByte(Value.Value)
933 if er != nil {
934 log.Errorw("Failed to convert into byte array", log.Fields{"error": er})
935 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400936 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000937 if er = json.Unmarshal(Val, &meterConfig); er != nil {
938 log.Error("Failed to unmarshal meterconfig", log.Fields{"error": er})
939 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400940 }
941 } else {
942 log.Debug("meter-does-not-exists-in-KVStore")
943 return nil, err
944 }
945 } else {
946 log.Errorf("Failed to get Meter config from kvstore for path %s", Path)
947
948 }
949 return &meterConfig, err
950}
951
Gamze Abakafee36392019-10-03 11:17:24 +0000952// RemoveMeterIDForOnu deletes the meter id from the kV-Store for the given onu based on the path
953// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +0530954func (RsrcMgr *OpenOltResourceMgr) RemoveMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000955 UniID uint32, TpID uint32) error {
956 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
npujarec5762e2020-01-01 14:08:48 +0530957 if err := RsrcMgr.KVStore.Delete(ctx, Path); err != nil {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400958 log.Errorf("Failed to delete meter id %s from kvstore ", Path)
959 return err
960 }
961 return nil
962}
salmansiddiqui7ac62132019-08-22 03:58:50 +0000963
964func getFlowIDFromFlowInfo(FlowInfo *[]FlowInfo, flowID, gemportID uint32, flowStoreCookie uint64, flowCategory string, vlanPcp ...uint32) error {
965 if FlowInfo != nil {
966 for _, Info := range *FlowInfo {
967 if int32(gemportID) == Info.Flow.GemportId && flowCategory != "" && Info.FlowCategory == flowCategory {
968 log.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
969 if Info.FlowCategory == "HSIA_FLOW" && Info.Flow.Classifier.OPbits == vlanPcp[0] {
970 log.Debug("Found matching vlan pcp ", log.Fields{"flowId": flowID, "Vlanpcp": vlanPcp[0]})
971 return nil
972 }
973 }
974 if int32(gemportID) == Info.Flow.GemportId && flowStoreCookie != 0 && Info.FlowStoreCookie == flowStoreCookie {
975 if flowCategory != "" && Info.FlowCategory == flowCategory {
976 log.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
977 return nil
978 }
979 }
980 }
981 }
Gamze Abakafee36392019-10-03 11:17:24 +0000982 log.Debugw("the flow can be related to a different service", log.Fields{"flow_info": FlowInfo})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000983 return errors.New("invalid flow-info")
984}
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530985
986//AddGemToOnuGemInfo adds gemport to onugem info kvstore
npujarec5762e2020-01-01 14:08:48 +0530987func (RsrcMgr *OpenOltResourceMgr) AddGemToOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, gemPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530988 var onuGemData []OnuGemInfo
989 var err error
990
npujarec5762e2020-01-01 14:08:48 +0530991 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530992 log.Errorf("failed to get onuifo for intfid %d", intfID)
993 return err
994 }
995 if len(onuGemData) == 0 {
996 log.Errorw("failed to ger Onuid info ", log.Fields{"intfid": intfID, "onuid": onuID})
997 return err
998 }
999
1000 for idx, onugem := range onuGemData {
1001 if onugem.OnuID == onuID {
1002 for _, gem := range onuGemData[idx].GemPorts {
1003 if gem == gemPort {
1004 log.Debugw("Gem already present in onugem info, skpping addition", log.Fields{"gem": gem})
1005 return nil
1006 }
1007 }
1008 log.Debugw("Added gem to onugem info", log.Fields{"gem": gemPort})
1009 onuGemData[idx].GemPorts = append(onuGemData[idx].GemPorts, gemPort)
1010 break
1011 }
1012 }
npujarec5762e2020-01-01 14:08:48 +05301013 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301014 if err != nil {
1015 log.Error("Failed to add onugem to kv store")
1016 return err
1017 }
1018 return err
1019}
1020
1021//GetOnuGemInfo gets onu gem info from the kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301022func (RsrcMgr *OpenOltResourceMgr) GetOnuGemInfo(ctx context.Context, IntfID uint32) ([]OnuGemInfo, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301023 var onuGemData []OnuGemInfo
1024
npujarec5762e2020-01-01 14:08:48 +05301025 if err := RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301026 log.Errorf("failed to get onuifo for intfid %d", IntfID)
1027 return nil, err
1028 }
1029
1030 return onuGemData, nil
1031}
1032
1033// AddOnuInfo adds onu info on to the kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301034func (RsrcMgr *OpenOltResourceMgr) AddOnuInfo(ctx context.Context, IntfID uint32, onuGem OnuGemInfo) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301035 var onuGemData []OnuGemInfo
1036 var err error
1037
npujarec5762e2020-01-01 14:08:48 +05301038 if err = RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301039 log.Errorf("failed to get onuifo for intfid %d", IntfID)
1040 return err
1041 }
1042 onuGemData = append(onuGemData, onuGem)
npujarec5762e2020-01-01 14:08:48 +05301043 err = RsrcMgr.ResourceMgrs[IntfID].AddOnuGemInfo(ctx, IntfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301044 if err != nil {
1045 log.Error("Failed to add onugem to kv store")
1046 return err
1047 }
1048
1049 log.Debugw("added onu to onugeminfo", log.Fields{"intf": IntfID, "onugem": onuGem})
1050 return err
1051}
1052
1053// UpdateOnuInfo updates Onuinfo on the kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301054func (RsrcMgr *OpenOltResourceMgr) UpdateOnuInfo(ctx context.Context, IntfID uint32, onuGem []OnuGemInfo) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301055 var onuGemData []OnuGemInfo
1056 var err error
1057
npujarec5762e2020-01-01 14:08:48 +05301058 err = RsrcMgr.ResourceMgrs[IntfID].AddOnuGemInfo(ctx, IntfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301059 if err != nil {
1060 log.Error("Failed to add onugem to kv store")
1061 return err
1062 }
1063
1064 log.Debugw("updated onugeminfo", log.Fields{"intf": IntfID, "onugem": onuGem})
1065 return err
1066}
1067
1068// AddUniPortToOnuInfo adds uni port to the onuinfo kvstore. check if the uni is already present if not update the kv store.
npujarec5762e2020-01-01 14:08:48 +05301069func (RsrcMgr *OpenOltResourceMgr) AddUniPortToOnuInfo(ctx context.Context, intfID uint32, onuID uint32, portNo uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301070 var onuGemData []OnuGemInfo
1071 var err error
1072
npujarec5762e2020-01-01 14:08:48 +05301073 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301074 log.Errorf("failed to get onuifo for intfid %d", intfID)
1075 return
1076 }
1077 for idx, onu := range onuGemData {
1078 if onu.OnuID == onuID {
1079 for _, uni := range onu.UniPorts {
1080 if uni == portNo {
1081 log.Debugw("uni already present in onugem info", log.Fields{"uni": portNo})
1082 return
1083 }
1084 }
1085 onuGemData[idx].UniPorts = append(onuGemData[idx].UniPorts, portNo)
1086 break
1087 }
1088 }
npujarec5762e2020-01-01 14:08:48 +05301089 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301090 if err != nil {
1091 log.Errorw("Failed to add uin port in onugem to kv store", log.Fields{"uni": portNo})
1092 return
1093 }
1094 return
1095}
1096
1097//UpdateGemPortForPktIn updates gemport for pkt in path to kvstore, path being intfid, onuid, portno
npujarec5762e2020-01-01 14:08:48 +05301098func (RsrcMgr *OpenOltResourceMgr) UpdateGemPortForPktIn(ctx context.Context, pktIn PacketInInfoKey, gemPort uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301099
1100 path := fmt.Sprintf(OnuPacketINPath, pktIn.IntfID, pktIn.OnuID, pktIn.LogicalPort)
1101 Value, err := json.Marshal(gemPort)
1102 if err != nil {
1103 log.Error("Failed to marshal data")
1104 return
1105 }
npujarec5762e2020-01-01 14:08:48 +05301106 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301107 log.Errorw("Failed to put to kvstore", log.Fields{"path": path, "value": gemPort})
1108 return
1109 }
1110 log.Debugw("added gem packet in successfully", log.Fields{"path": path, "gem": gemPort})
1111
1112 return
1113}
1114
1115// GetGemPortFromOnuPktIn gets the gem port from onu pkt in path, path being intfid, onuid, portno
npujarec5762e2020-01-01 14:08:48 +05301116func (RsrcMgr *OpenOltResourceMgr) GetGemPortFromOnuPktIn(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) (uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301117
1118 var Val []byte
1119 var gemPort uint32
1120
1121 path := fmt.Sprintf(OnuPacketINPath, intfID, onuID, logicalPort)
1122
npujarec5762e2020-01-01 14:08:48 +05301123 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301124 if err != nil {
1125 log.Errorw("Failed to get from kv store", log.Fields{"path": path})
1126 return uint32(0), err
1127 } else if value == nil {
1128 log.Debugw("No pkt in gem found", log.Fields{"path": path})
1129 return uint32(0), nil
1130 }
1131
1132 if Val, err = kvstore.ToByte(value.Value); err != nil {
1133 log.Error("Failed to convert to byte array")
1134 return uint32(0), err
1135 }
1136 if err = json.Unmarshal(Val, &gemPort); err != nil {
1137 log.Error("Failed to unmarshall")
1138 return uint32(0), err
1139 }
1140 log.Debugw("found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
1141
1142 return gemPort, nil
1143}
1144
1145// DelGemPortPktIn deletes the gemport from the pkt in path
npujarec5762e2020-01-01 14:08:48 +05301146func (RsrcMgr *OpenOltResourceMgr) DelGemPortPktIn(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301147
1148 path := fmt.Sprintf(OnuPacketINPath, intfID, onuID, logicalPort)
npujarec5762e2020-01-01 14:08:48 +05301149 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301150 log.Errorf("Falied to remove resource %s", path)
1151 return err
1152 }
1153 return nil
1154}
1155
1156// DelOnuGemInfoForIntf deletes the onugem info from kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301157func (RsrcMgr *OpenOltResourceMgr) DelOnuGemInfoForIntf(ctx context.Context, intfID uint32) error {
1158 if err := RsrcMgr.ResourceMgrs[intfID].DelOnuGemInfoForIntf(ctx, intfID); err != nil {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301159 log.Errorw("failed to delete onu gem info for", log.Fields{"intfid": intfID})
1160 return err
1161 }
1162 return nil
1163}
1164
1165//GetNNIFromKVStore gets NNi intfids from kvstore. path being per device
npujarec5762e2020-01-01 14:08:48 +05301166func (RsrcMgr *OpenOltResourceMgr) GetNNIFromKVStore(ctx context.Context) ([]uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301167
1168 var nni []uint32
1169 var Val []byte
1170
1171 path := fmt.Sprintf(NnniIntfID)
npujarec5762e2020-01-01 14:08:48 +05301172 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301173 if err != nil {
1174 log.Error("failed to get data from kv store")
1175 return nil, err
1176 }
1177 if value != nil {
1178 if Val, err = kvstore.ToByte(value.Value); err != nil {
1179 log.Error("Failed to convert to byte array")
1180 return nil, err
1181 }
1182 if err = json.Unmarshal(Val, &nni); err != nil {
1183 log.Error("Failed to unmarshall")
1184 return nil, err
1185 }
1186 }
1187 return nni, err
1188}
1189
1190// AddNNIToKVStore adds Nni interfaces to kvstore, path being per device.
npujarec5762e2020-01-01 14:08:48 +05301191func (RsrcMgr *OpenOltResourceMgr) AddNNIToKVStore(ctx context.Context, nniIntf uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301192 var Value []byte
1193
npujarec5762e2020-01-01 14:08:48 +05301194 nni, err := RsrcMgr.GetNNIFromKVStore(ctx)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301195 if err != nil {
1196 log.Error("failed to fetch nni interfaces from kv store")
1197 return err
1198 }
1199
1200 path := fmt.Sprintf(NnniIntfID)
1201 nni = append(nni, nniIntf)
1202 Value, err = json.Marshal(nni)
1203 if err != nil {
1204 log.Error("Failed to marshal data")
1205 }
npujarec5762e2020-01-01 14:08:48 +05301206 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301207 log.Errorw("Failed to put to kvstore", log.Fields{"path": path, "value": Value})
1208 return err
1209 }
1210 log.Debugw("added nni to kv successfully", log.Fields{"path": path, "nni": nniIntf})
1211 return nil
1212}
1213
1214// DelNNiFromKVStore deletes nni interface list from kv store.
npujarec5762e2020-01-01 14:08:48 +05301215func (RsrcMgr *OpenOltResourceMgr) DelNNiFromKVStore(ctx context.Context) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301216
1217 path := fmt.Sprintf(NnniIntfID)
1218
npujarec5762e2020-01-01 14:08:48 +05301219 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301220 log.Errorw("Failed to delete nni interfaces from kv store", log.Fields{"path": path})
1221 return err
1222 }
1223 return nil
1224}
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301225
1226//UpdateFlowIDsForGem updates flow id per gemport
npujarec5762e2020-01-01 14:08:48 +05301227func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDsForGem(ctx context.Context, intf uint32, gem uint32, flowIDs []uint32) error {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301228 var val []byte
1229 path := fmt.Sprintf(FlowIDsForGem, intf)
1230
npujarec5762e2020-01-01 14:08:48 +05301231 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301232 if err != nil {
1233 log.Error("Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
1234 return err
1235 }
1236 if flowsForGem == nil {
1237 flowsForGem = make(map[uint32][]uint32)
1238 }
1239 flowsForGem[gem] = flowIDs
1240 val, err = json.Marshal(flowsForGem)
1241 if err != nil {
1242 log.Error("Failed to marshal data", log.Fields{"error": err})
1243 return err
1244 }
npujarec5762e2020-01-01 14:08:48 +05301245 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301246 log.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
1247 return err
1248 }
1249 log.Debugw("added flowid list for gem to kv successfully", log.Fields{"path": path, "flowidlist": flowsForGem[gem]})
1250 return nil
1251}
1252
1253//DeleteFlowIDsForGem deletes the flowID list entry per gem from kvstore.
npujarec5762e2020-01-01 14:08:48 +05301254func (RsrcMgr *OpenOltResourceMgr) DeleteFlowIDsForGem(ctx context.Context, intf uint32, gem uint32) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301255 path := fmt.Sprintf(FlowIDsForGem, intf)
1256 var val []byte
1257
npujarec5762e2020-01-01 14:08:48 +05301258 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301259 if err != nil {
1260 log.Error("Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
1261 return
1262 }
1263 if flowsForGem == nil {
1264 log.Error("No flowids found ", log.Fields{"intf": intf, "gemport": gem})
1265 return
1266 }
1267 // once we get the flows per gem map from kv , just delete the gem entry from the map
1268 delete(flowsForGem, gem)
1269 // once gem entry is deleted update the kv store.
1270 val, err = json.Marshal(flowsForGem)
1271 if err != nil {
1272 log.Error("Failed to marshal data", log.Fields{"error": err})
1273 return
1274 }
npujarec5762e2020-01-01 14:08:48 +05301275 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301276 log.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
1277 return
1278 }
1279 return
1280}
1281
1282//GetFlowIDsGemMapForInterface gets flowids per gemport and interface
npujarec5762e2020-01-01 14:08:48 +05301283func (RsrcMgr *OpenOltResourceMgr) GetFlowIDsGemMapForInterface(ctx context.Context, intf uint32) (map[uint32][]uint32, error) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301284 path := fmt.Sprintf(FlowIDsForGem, intf)
1285 var flowsForGem map[uint32][]uint32
1286 var val []byte
1287
npujarec5762e2020-01-01 14:08:48 +05301288 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301289 if err != nil {
1290 log.Error("failed to get data from kv store")
1291 return nil, err
1292 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001293 if value != nil && value.Value != nil {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301294 if val, err = kvstore.ToByte(value.Value); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +00001295 log.Error("Failed to convert to byte array ", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301296 return nil, err
1297 }
1298 if err = json.Unmarshal(val, &flowsForGem); err != nil {
1299 log.Error("Failed to unmarshall", log.Fields{"error": err})
1300 return nil, err
1301 }
1302 }
1303 return flowsForGem, nil
1304}
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301305
1306//DeleteIntfIDGempMapPath deletes the intf id path used to store flow ids per gem to kvstore.
npujarec5762e2020-01-01 14:08:48 +05301307func (RsrcMgr *OpenOltResourceMgr) DeleteIntfIDGempMapPath(ctx context.Context, intf uint32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301308 path := fmt.Sprintf(FlowIDsForGem, intf)
npujarec5762e2020-01-01 14:08:48 +05301309 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301310 log.Errorw("Failed to delete nni interfaces from kv store", log.Fields{"path": path})
1311 return
1312 }
1313 return
1314}
1315
1316// RemoveResourceMap Clear resource map associated with (intfid, onuid, uniid) tuple.
npujarec5762e2020-01-01 14:08:48 +05301317func (RsrcMgr *OpenOltResourceMgr) RemoveResourceMap(ctx context.Context, intfID uint32, onuID int32, uniID int32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301318 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +05301319 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301320}
Esin Karamanccb714b2019-11-29 15:02:06 +00001321
1322//GetMcastQueuePerInterfaceMap gets multicast queue info per pon interface
npujarec5762e2020-01-01 14:08:48 +05301323func (RsrcMgr *OpenOltResourceMgr) GetMcastQueuePerInterfaceMap(ctx context.Context) (map[uint32][]uint32, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001324 path := fmt.Sprintf(McastQueuesForIntf)
1325 var mcastQueueToIntfMap map[uint32][]uint32
1326 var val []byte
1327
npujarec5762e2020-01-01 14:08:48 +05301328 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001329 if err != nil {
1330 log.Error("failed to get data from kv store")
1331 return nil, err
1332 }
1333 if kvPair != nil && kvPair.Value != nil {
1334 if val, err = kvstore.ToByte(kvPair.Value); err != nil {
1335 log.Error("Failed to convert to byte array ", log.Fields{"error": err})
1336 return nil, err
1337 }
1338 if err = json.Unmarshal(val, &mcastQueueToIntfMap); err != nil {
1339 log.Error("Failed to unmarshall ", log.Fields{"error": err})
1340 return nil, err
1341 }
1342 }
1343 return mcastQueueToIntfMap, nil
1344}
1345
1346//AddMcastQueueForIntf adds multicast queue for pon interface
npujarec5762e2020-01-01 14:08:48 +05301347func (RsrcMgr *OpenOltResourceMgr) AddMcastQueueForIntf(ctx context.Context, intf uint32, gem uint32, servicePriority uint32) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001348 var val []byte
1349 path := fmt.Sprintf(McastQueuesForIntf)
1350
npujarec5762e2020-01-01 14:08:48 +05301351 mcastQueues, err := RsrcMgr.GetMcastQueuePerInterfaceMap(ctx)
Esin Karamanccb714b2019-11-29 15:02:06 +00001352 if err != nil {
1353 log.Errorw("Failed to get multicast queue info for interface", log.Fields{"error": err, "intf": intf})
1354 return err
1355 }
1356 if mcastQueues == nil {
1357 mcastQueues = make(map[uint32][]uint32)
1358 }
1359 mcastQueues[intf] = []uint32{gem, servicePriority}
1360 if val, err = json.Marshal(mcastQueues); err != nil {
1361 log.Errorw("Failed to marshal data", log.Fields{"error": err})
1362 return err
1363 }
npujarec5762e2020-01-01 14:08:48 +05301364 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +00001365 log.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
1366 return err
1367 }
1368 log.Debugw("added multicast queue info to KV store successfully", log.Fields{"path": path, "mcastQueueInfo": mcastQueues[intf], "interfaceId": intf})
1369 return nil
1370}
1371
1372//AddFlowGroupToKVStore adds flow group into KV store
npujarec5762e2020-01-01 14:08:48 +05301373func (RsrcMgr *OpenOltResourceMgr) AddFlowGroupToKVStore(ctx context.Context, groupEntry *ofp.OfpGroupEntry, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001374 var Value []byte
1375 var err error
1376 var path string
1377 if cached {
1378 path = fmt.Sprintf(FlowGroupCached, groupEntry.Desc.GroupId)
1379 } else {
1380 path = fmt.Sprintf(FlowGroup, groupEntry.Desc.GroupId)
1381 }
1382 //build group info object
1383 var outPorts []uint32
1384 for _, ofBucket := range groupEntry.Desc.Buckets {
1385 for _, ofAction := range ofBucket.Actions {
1386 if ofAction.Type == ofp.OfpActionType_OFPAT_OUTPUT {
1387 outPorts = append(outPorts, ofAction.GetOutput().Port)
1388 }
1389 }
1390 }
1391 groupInfo := GroupInfo{
1392 GroupID: groupEntry.Desc.GroupId,
1393 OutPorts: outPorts,
1394 }
1395
1396 Value, err = json.Marshal(groupInfo)
1397
1398 if err != nil {
1399 log.Error("failed to Marshal flow group object")
1400 return err
1401 }
1402
npujarec5762e2020-01-01 14:08:48 +05301403 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +00001404 log.Errorf("Failed to update resource %s", path)
1405 return err
1406 }
1407 return nil
1408}
1409
1410//RemoveFlowGroupFromKVStore removes flow group from KV store
npujarec5762e2020-01-01 14:08:48 +05301411func (RsrcMgr *OpenOltResourceMgr) RemoveFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) bool {
Esin Karamanccb714b2019-11-29 15:02:06 +00001412 var path string
1413 if cached {
1414 path = fmt.Sprintf(FlowGroupCached, groupID)
1415 } else {
1416 path = fmt.Sprintf(FlowGroup, groupID)
1417 }
npujarec5762e2020-01-01 14:08:48 +05301418 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +00001419 log.Errorf("Failed to remove resource %s due to %s", path, err)
1420 return false
1421 }
1422 return true
1423}
1424
1425//GetFlowGroupFromKVStore fetches flow group from the KV store. Returns (false, {} error) if any problem occurs during
1426//fetching the data. Returns (true, groupInfo, nil) if the group is fetched successfully.
1427// Returns (false, {}, nil) if the group does not exists in the KV store.
npujarec5762e2020-01-01 14:08:48 +05301428func (RsrcMgr *OpenOltResourceMgr) GetFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) (bool, GroupInfo, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001429 var groupInfo GroupInfo
1430 var path string
1431 if cached {
1432 path = fmt.Sprintf(FlowGroupCached, groupID)
1433 } else {
1434 path = fmt.Sprintf(FlowGroup, groupID)
1435 }
npujarec5762e2020-01-01 14:08:48 +05301436 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001437 if err != nil {
1438 return false, groupInfo, err
1439 }
1440 if kvPair != nil && kvPair.Value != nil {
1441 Val, err := kvstore.ToByte(kvPair.Value)
1442 if err != nil {
1443 log.Errorw("Failed to convert flow group into byte array", log.Fields{"error": err})
1444 return false, groupInfo, err
1445 }
1446 if err = json.Unmarshal(Val, &groupInfo); err != nil {
1447 log.Errorw("Failed to unmarshal", log.Fields{"error": err})
1448 return false, groupInfo, err
1449 }
1450 return true, groupInfo, nil
1451 }
1452 return false, groupInfo, nil
1453}