blob: a7f773034bf204b02fe91d36dec056c9683463e9 [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"
Andrea Campanellab83b39d2020-03-30 11:41:16 +020025 "github.com/opencord/voltha-openolt-adapter/internal/pkg/olterrors"
Girish Gowdru0c588b22019-04-23 23:24:56 -040026 "strconv"
27 "strings"
Girish Gowdra38d533d2020-03-30 20:38:51 -070028 "sync"
Abhilash S.L7f17e402019-03-15 17:40:41 +053029
Esin Karamanccb714b2019-11-29 15:02:06 +000030 "github.com/opencord/voltha-lib-go/v3/pkg/db"
31 "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
32 "github.com/opencord/voltha-lib-go/v3/pkg/log"
33 ponrmgr "github.com/opencord/voltha-lib-go/v3/pkg/ponresourcemanager"
34 ofp "github.com/opencord/voltha-protos/v3/go/openflow_13"
35 "github.com/opencord/voltha-protos/v3/go/openolt"
Abhilash S.L7f17e402019-03-15 17:40:41 +053036)
37
salmansiddiqui7ac62132019-08-22 03:58:50 +000038const (
39 // KvstoreTimeout specifies the time out for KV Store Connection
40 KvstoreTimeout = 5
41 // BasePathKvStore - service/voltha/openolt/<device_id>
42 BasePathKvStore = "service/voltha/openolt/{%s}"
Gamze Abakafee36392019-10-03 11:17:24 +000043 // TpIDPathSuffix - <(pon_id, onu_id, uni_id)>/tp_id
44 TpIDPathSuffix = "{%d,%d,%d}/tp_id"
45 //MeterIDPathSuffix - <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
46 MeterIDPathSuffix = "{%d,%d,%d}/{%d}/meter_id/{%s}"
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053047 //NnniIntfID - nniintfids
48 NnniIntfID = "nniintfids"
49 // OnuPacketINPath path on the kvstore to store packetin gemport,which will be used for packetin, pcketout
50 //format: onu_packetin/<intfid>,<onuid>,<logicalport>
51 OnuPacketINPath = "onu_packetin/{%d,%d,%d}"
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +053052 //FlowIDsForGem flowids_per_gem/<intfid>
53 FlowIDsForGem = "flowids_per_gem/{%d}"
Esin Karamanccb714b2019-11-29 15:02:06 +000054 //McastQueuesForIntf multicast queues for pon interfaces
55 McastQueuesForIntf = "mcast_qs_for_int"
56 //FlowGroup flow_groups/<flow_group_id>
57 // A group is stored under this path on the KV store after it has been installed to the device.
58 // It should also be deleted after it has been removed from the device accordingly.
59 FlowGroup = "flow_groups/{%d}"
60 //FlowGroupCached flow_groups_cached/<flow_group_id>
61 // When a group add request received, we create the group without setting any members to it since we cannot
62 // set any members to a group until it is associated with a multicast flow. It is a BAL limitation.
63 // When the related multicast flow has been created we perform set members operation for the group.
64 // That is why we need to keep the members of a group until the multicast flow creation request comes.
65 // We preserve the groups under "FlowGroupsCached" directory in the KV store temporarily. Having set members,
66 // we remove the group from the cached group store.
67 FlowGroupCached = "flow_groups_cached/{%d}"
salmansiddiqui7ac62132019-08-22 03:58:50 +000068)
Abhilash S.L7f17e402019-03-15 17:40:41 +053069
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070070// FlowInfo holds the flow information
Abhilash S.L8ee90712019-04-29 16:24:22 +053071type FlowInfo struct {
72 Flow *openolt.Flow
73 FlowStoreCookie uint64
74 FlowCategory string
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053075 LogicalFlowID uint64
76}
77
78// OnuGemInfo holds onu information along with gem port list and uni port list
79type OnuGemInfo struct {
80 OnuID uint32
81 SerialNumber string
82 IntfID uint32
83 GemPorts []uint32
84 UniPorts []uint32
85}
86
87// PacketInInfoKey is the key for packet in gemport
88type PacketInInfoKey struct {
89 IntfID uint32
90 OnuID uint32
91 LogicalPort uint32
Abhilash S.L8ee90712019-04-29 16:24:22 +053092}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070093
Esin Karamanccb714b2019-11-29 15:02:06 +000094// GroupInfo holds group information
95type GroupInfo struct {
96 GroupID uint32
97 OutPorts []uint32
98}
99
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700100// OpenOltResourceMgr holds resource related information as provided below for each field
Abhilash S.L7f17e402019-03-15 17:40:41 +0530101type OpenOltResourceMgr struct {
sbarbaria8910ba2019-11-05 10:12:23 -0500102 DeviceID string // OLT device id
103 HostAndPort string // Host and port of the kv store to connect to
104 Args string // args
105 KVStore *db.Backend // backend kv store connection handle
Girish Gowdru0c588b22019-04-23 23:24:56 -0400106 DeviceType string
107 Host string // Host ip of the kv store
108 Port int // port of the kv store
109 DevInfo *openolt.DeviceInfo // device information
110 // array of pon resource managers per interface technology
111 ResourceMgrs map[uint32]*ponrmgr.PONResourceManager
Girish Gowdra38d533d2020-03-30 20:38:51 -0700112
113 // This protects concurrent gemport_id allocate/delete calls on a per PON port basis
114 GemPortIDMgmtLock []sync.RWMutex
115 // This protects concurrent alloc_id allocate/delete calls on a per PON port basis
116 AllocIDMgmtLock []sync.RWMutex
117 // This protects concurrent onu_id allocate/delete calls on a per PON port basis
118 OnuIDMgmtLock []sync.RWMutex
119 // This protects concurrent flow_id allocate/delete calls. We do not need this on a
120 // per PON port basis as flow IDs are unique across the OLT.
121 FlowIDMgmtLock sync.RWMutex
122
123 // This protects concurrent access to flowids_per_gem info stored on KV store
124 flowIDToGemInfoLock sync.RWMutex
Abhilash S.L7f17e402019-03-15 17:40:41 +0530125}
126
Manikkaraj kb1d51442019-07-23 10:41:02 -0400127func newKVClient(storeType string, address string, timeout uint32) (kvstore.Client, error) {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000128 logger.Infow("kv-store-type", log.Fields{"store": storeType})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400129 switch storeType {
130 case "consul":
131 return kvstore.NewConsulClient(address, int(timeout))
132 case "etcd":
Scott Bakered4a8e72020-04-17 11:10:20 -0700133 return kvstore.NewEtcdClient(address, int(timeout), log.FatalLevel)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400134 }
135 return nil, errors.New("unsupported-kv-store")
Abhilash S.L7f17e402019-03-15 17:40:41 +0530136}
137
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700138// SetKVClient sets the KV client and return a kv backend
sbarbaria8910ba2019-11-05 10:12:23 -0500139func SetKVClient(backend string, Host string, Port int, DeviceID string) *db.Backend {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400140 addr := Host + ":" + strconv.Itoa(Port)
141 // TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
142 // issue between kv store and backend , core is not calling NewBackend directly
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700143 kvClient, err := newKVClient(backend, addr, KvstoreTimeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400144 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000145 logger.Fatalw("Failed to init KV client\n", log.Fields{"err": err})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400146 return nil
147 }
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700148
sbarbaria8910ba2019-11-05 10:12:23 -0500149 kvbackend := &db.Backend{
Girish Gowdru0c588b22019-04-23 23:24:56 -0400150 Client: kvClient,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700151 StoreType: backend,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400152 Host: Host,
153 Port: Port,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700154 Timeout: KvstoreTimeout,
155 PathPrefix: fmt.Sprintf(BasePathKvStore, DeviceID)}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530156
Girish Gowdru0c588b22019-04-23 23:24:56 -0400157 return kvbackend
Abhilash S.L7f17e402019-03-15 17:40:41 +0530158}
159
Gamze Abakafee36392019-10-03 11:17:24 +0000160// NewResourceMgr init a New resource manager instance which in turn instantiates pon resource manager
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700161// instances according to technology. Initializes the default resource ranges for all
162// the resources.
npujarec5762e2020-01-01 14:08:48 +0530163func NewResourceMgr(ctx context.Context, deviceID string, KVStoreHostPort string, kvStoreType string, deviceType string, devInfo *openolt.DeviceInfo) *OpenOltResourceMgr {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400164 var ResourceMgr OpenOltResourceMgr
Girish Kumar2ad402b2020-03-20 19:45:12 +0000165 logger.Debugf("Init new resource manager , host_port: %s, deviceid: %s", KVStoreHostPort, deviceID)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530166 ResourceMgr.HostAndPort = KVStoreHostPort
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700167 ResourceMgr.DeviceType = deviceType
168 ResourceMgr.DevInfo = devInfo
169 IPPort := strings.Split(KVStoreHostPort, ":")
170 ResourceMgr.Host = IPPort[0]
171 ResourceMgr.Port, _ = strconv.Atoi(IPPort[1])
Girish Gowdra38d533d2020-03-30 20:38:51 -0700172 NumPONPorts := devInfo.GetPonPorts()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530173
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700174 Backend := kvStoreType
Girish Gowdru0c588b22019-04-23 23:24:56 -0400175 ResourceMgr.KVStore = SetKVClient(Backend, ResourceMgr.Host,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700176 ResourceMgr.Port, deviceID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400177 if ResourceMgr.KVStore == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000178 logger.Error("Failed to setup KV store")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400179 }
180 Ranges := make(map[string]*openolt.DeviceInfo_DeviceResourceRanges)
181 RsrcMgrsByTech := make(map[string]*ponrmgr.PONResourceManager)
182 ResourceMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530183
Girish Gowdra38d533d2020-03-30 20:38:51 -0700184 ResourceMgr.AllocIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
185 ResourceMgr.GemPortIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
186 ResourceMgr.OnuIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
187
Girish Gowdru0c588b22019-04-23 23:24:56 -0400188 // TODO self.args = registry('main').get_args()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530189
Girish Gowdru0c588b22019-04-23 23:24:56 -0400190 /*
191 If a legacy driver returns protobuf without any ranges,s synthesize one from
Gamze Abakafee36392019-10-03 11:17:24 +0000192 the legacy global per-device information. This, in theory, is temporary until
Girish Gowdru0c588b22019-04-23 23:24:56 -0400193 the legacy drivers are upgrade to support pool ranges.
194 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700195 if devInfo.Ranges == nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400196 var ranges openolt.DeviceInfo_DeviceResourceRanges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700197 ranges.Technology = devInfo.GetTechnology()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530198
Girish Gowdru0c588b22019-04-23 23:24:56 -0400199 var index uint32
200 for index = 0; index < NumPONPorts; index++ {
201 ranges.IntfIds = append(ranges.IntfIds, index)
202 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530203
Abhilash S.L8ee90712019-04-29 16:24:22 +0530204 var Pool openolt.DeviceInfo_DeviceResourceRanges_Pool
Girish Gowdru0c588b22019-04-23 23:24:56 -0400205 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700206 Pool.Start = devInfo.OnuIdStart
207 Pool.End = devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400208 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
cbabuabf02352019-10-15 13:14:56 +0200209 onuPool := Pool
210 ranges.Pools = append(ranges.Pools, &onuPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530211
Girish Gowdru0c588b22019-04-23 23:24:56 -0400212 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700213 Pool.Start = devInfo.AllocIdStart
214 Pool.End = devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400215 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200216 allocPool := Pool
217 ranges.Pools = append(ranges.Pools, &allocPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530218
Girish Gowdru0c588b22019-04-23 23:24:56 -0400219 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700220 Pool.Start = devInfo.GemportIdStart
221 Pool.End = devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400222 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200223 gemPool := Pool
224 ranges.Pools = append(ranges.Pools, &gemPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530225
Girish Gowdru0c588b22019-04-23 23:24:56 -0400226 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700227 Pool.Start = devInfo.FlowIdStart
228 Pool.End = devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400229 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
Abhilash S.L8ee90712019-04-29 16:24:22 +0530230 ranges.Pools = append(ranges.Pools, &Pool)
231 // Add to device info
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700232 devInfo.Ranges = append(devInfo.Ranges, &ranges)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400233 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530234
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700235 // Create a separate Resource Manager instance for each range. This assumes that
Girish Gowdru0c588b22019-04-23 23:24:56 -0400236 // each technology is represented by only a single range
237 var GlobalPONRsrcMgr *ponrmgr.PONResourceManager
238 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700239 for _, TechRange := range devInfo.Ranges {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400240 technology := TechRange.Technology
Girish Kumar2ad402b2020-03-20 19:45:12 +0000241 logger.Debugf("Device info technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400242 Ranges[technology] = TechRange
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700243 RsrcMgrsByTech[technology], err = ponrmgr.NewPONResourceManager(technology, deviceType, deviceID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400244 Backend, ResourceMgr.Host, ResourceMgr.Port)
245 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000246 logger.Errorf("Failed to create pon resource manager instance for technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400247 return nil
248 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700249 // resource_mgrs_by_tech[technology] = resource_mgr
Girish Gowdru0c588b22019-04-23 23:24:56 -0400250 if GlobalPONRsrcMgr == nil {
251 GlobalPONRsrcMgr = RsrcMgrsByTech[technology]
252 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700253 for _, IntfID := range TechRange.IntfIds {
254 ResourceMgr.ResourceMgrs[uint32(IntfID)] = RsrcMgrsByTech[technology]
Girish Gowdru0c588b22019-04-23 23:24:56 -0400255 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700256 // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
npujarec5762e2020-01-01 14:08:48 +0530257 InitializeDeviceResourceRangeAndPool(ctx, RsrcMgrsByTech[technology], GlobalPONRsrcMgr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700258 TechRange, devInfo)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400259 }
260 // After we have initialized resource ranges, initialize the
261 // resource pools accordingly.
262 for _, PONRMgr := range RsrcMgrsByTech {
npujarec5762e2020-01-01 14:08:48 +0530263 _ = PONRMgr.InitDeviceResourcePool(ctx)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400264 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000265 logger.Info("Initialization of resource manager success!")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400266 return &ResourceMgr
Abhilash S.L7f17e402019-03-15 17:40:41 +0530267}
268
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700269// InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply
270// device specific information. If KV doesn't exist
271// or is broader than the device, the device's information will
272// dictate the range limits
npujarec5762e2020-01-01 14:08:48 +0530273func InitializeDeviceResourceRangeAndPool(ctx context.Context, ponRMgr *ponrmgr.PONResourceManager, globalPONRMgr *ponrmgr.PONResourceManager,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700274 techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530275
Girish Gowdru0c588b22019-04-23 23:24:56 -0400276 // init the resource range pool according to the sharing type
Abhilash S.L7f17e402019-03-15 17:40:41 +0530277
Girish Kumar2ad402b2020-03-20 19:45:12 +0000278 logger.Debugf("Resource range pool init for technology %s", ponRMgr.Technology)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700279 // first load from KV profiles
npujarec5762e2020-01-01 14:08:48 +0530280 status := ponRMgr.InitResourceRangesFromKVStore(ctx)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700281 if !status {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000282 logger.Debugf("Failed to load resource ranges from KV store for tech %s", ponRMgr.Technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400283 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530284
Girish Gowdru0c588b22019-04-23 23:24:56 -0400285 /*
286 Then apply device specific information. If KV doesn't exist
Gamze Abakafee36392019-10-03 11:17:24 +0000287 or is broader than the device, the device's information will
Girish Gowdru0c588b22019-04-23 23:24:56 -0400288 dictate the range limits
289 */
Girish Kumar2ad402b2020-03-20 19:45:12 +0000290 logger.Debugw("Using device info to init pon resource ranges", log.Fields{"Tech": ponRMgr.Technology})
Abhilash S.L7f17e402019-03-15 17:40:41 +0530291
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700292 ONUIDStart := devInfo.OnuIdStart
293 ONUIDEnd := devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400294 ONUIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
295 ONUIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700296 AllocIDStart := devInfo.AllocIdStart
297 AllocIDEnd := devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400298 AllocIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
299 AllocIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700300 GEMPortIDStart := devInfo.GemportIdStart
301 GEMPortIDEnd := devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400302 GEMPortIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
303 GEMPortIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700304 FlowIDStart := devInfo.FlowIdStart
305 FlowIDEnd := devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400306 FlowIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
307 FlowIDSharedPoolID := uint32(0)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530308
Girish Gowdru0c588b22019-04-23 23:24:56 -0400309 var FirstIntfPoolID uint32
310 var SharedPoolID uint32
Abhilash S.L7f17e402019-03-15 17:40:41 +0530311
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400312 /*
313 * 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 -0700314 * if resources are shared across interfaces then SharedPoolID is given a positive number.
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400315 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700316 for _, FirstIntfPoolID = range techRange.IntfIds {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400317 // skip the intf id 0
318 if FirstIntfPoolID == 0 {
319 continue
320 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400321 break
322 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530323
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700324 for _, RangePool := range techRange.Pools {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400325 if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400326 SharedPoolID = FirstIntfPoolID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400327 } else if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_SAME_TECH {
328 SharedPoolID = FirstIntfPoolID
329 } else {
330 SharedPoolID = 0
331 }
332 if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID {
333 ONUIDStart = RangePool.Start
334 ONUIDEnd = RangePool.End
335 ONUIDShared = RangePool.Sharing
336 ONUIDSharedPoolID = SharedPoolID
337 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID {
338 AllocIDStart = RangePool.Start
339 AllocIDEnd = RangePool.End
340 AllocIDShared = RangePool.Sharing
341 AllocIDSharedPoolID = SharedPoolID
342 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID {
343 GEMPortIDStart = RangePool.Start
344 GEMPortIDEnd = RangePool.End
345 GEMPortIDShared = RangePool.Sharing
346 GEMPortIDSharedPoolID = SharedPoolID
347 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID {
348 FlowIDStart = RangePool.Start
349 FlowIDEnd = RangePool.End
350 FlowIDShared = RangePool.Sharing
351 FlowIDSharedPoolID = SharedPoolID
352 }
353 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530354
Girish Kumar2ad402b2020-03-20 19:45:12 +0000355 logger.Debugw("Device info init", log.Fields{"technology": techRange.Technology,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400356 "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd, "onu_id_shared_pool_id": ONUIDSharedPoolID,
357 "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd,
358 "alloc_id_shared_pool_id": AllocIDSharedPoolID,
359 "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd,
360 "gemport_id_shared_pool_id": GEMPortIDSharedPoolID,
361 "flow_id_start": FlowIDStart,
362 "flow_id_end_idx": FlowIDEnd,
363 "flow_id_shared_pool_id": FlowIDSharedPoolID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700364 "intf_ids": techRange.IntfIds,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400365 "uni_id_start": 0,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700366 "uni_id_end_idx": 1, /*MaxUNIIDperONU()*/
367 })
Abhilash S.L7f17e402019-03-15 17:40:41 +0530368
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700369 ponRMgr.InitDefaultPONResourceRanges(ONUIDStart, ONUIDEnd, ONUIDSharedPoolID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400370 AllocIDStart, AllocIDEnd, AllocIDSharedPoolID,
371 GEMPortIDStart, GEMPortIDEnd, GEMPortIDSharedPoolID,
372 FlowIDStart, FlowIDEnd, FlowIDSharedPoolID, 0, 1,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700373 devInfo.PonPorts, techRange.IntfIds)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530374
Girish Gowdru0c588b22019-04-23 23:24:56 -0400375 // For global sharing, make sure to refresh both local and global resource manager instances' range
Abhilash S.L7f17e402019-03-15 17:40:41 +0530376
Girish Gowdru0c588b22019-04-23 23:24:56 -0400377 if ONUIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700378 globalPONRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400379 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700380 ponRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
381 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400382 }
383 if AllocIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700384 globalPONRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400385 "", 0, nil)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530386
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700387 ponRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
388 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400389 }
390 if GEMPortIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700391 globalPONRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400392 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700393 ponRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
394 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400395 }
396 if FlowIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700397 globalPONRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400398 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700399 ponRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
400 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400401 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530402
Girish Gowdru0c588b22019-04-23 23:24:56 -0400403 // Make sure loaded range fits the platform bit encoding ranges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700404 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 +0530405}
406
Devmalya Paul495b94a2019-08-27 19:42:00 -0400407// Delete clears used resources for the particular olt device being deleted
npujarec5762e2020-01-01 14:08:48 +0530408func (RsrcMgr *OpenOltResourceMgr) Delete(ctx context.Context) error {
Devmalya Paul495b94a2019-08-27 19:42:00 -0400409 /* TODO
410 def __del__(self):
411 self.log.info("clearing-device-resource-pool")
412 for key, resource_mgr in self.resource_mgrs.iteritems():
413 resource_mgr.clear_device_resource_pool()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530414
Devmalya Paul495b94a2019-08-27 19:42:00 -0400415 def assert_pon_id_limit(self, pon_intf_id):
416 assert pon_intf_id in self.resource_mgrs
Abhilash S.L7f17e402019-03-15 17:40:41 +0530417
Devmalya Paul495b94a2019-08-27 19:42:00 -0400418 def assert_onu_id_limit(self, pon_intf_id, onu_id):
419 self.assert_pon_id_limit(pon_intf_id)
420 self.resource_mgrs[pon_intf_id].assert_resource_limits(onu_id, PONResourceManager.ONU_ID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530421
Devmalya Paul495b94a2019-08-27 19:42:00 -0400422 @property
423 def max_uni_id_per_onu(self):
424 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 +0530425
Devmalya Paul495b94a2019-08-27 19:42:00 -0400426 def assert_uni_id_limit(self, pon_intf_id, onu_id, uni_id):
427 self.assert_onu_id_limit(pon_intf_id, onu_id)
428 self.resource_mgrs[pon_intf_id].assert_resource_limits(uni_id, PONResourceManager.UNI_ID)
429 */
430 for _, rsrcMgr := range RsrcMgr.ResourceMgrs {
npujarec5762e2020-01-01 14:08:48 +0530431 if err := rsrcMgr.ClearDeviceResourcePool(ctx); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000432 logger.Debug("Failed to clear device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400433 return err
434 }
435 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000436 logger.Debug("Cleared device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400437 return nil
438}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530439
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700440// GetONUID returns the available OnuID for the given pon-port
npujarec5762e2020-01-01 14:08:48 +0530441func (RsrcMgr *OpenOltResourceMgr) GetONUID(ctx context.Context, ponIntfID uint32) (uint32, error) {
salmansiddiqui352a45c2019-08-19 10:15:36 +0000442 // Check if Pon Interface ID is present in Resource-manager-map
Girish Gowdrab77ded92020-04-08 11:45:05 -0700443 RsrcMgr.OnuIDMgmtLock[ponIntfID].Lock()
444 defer RsrcMgr.OnuIDMgmtLock[ponIntfID].Unlock()
445
salmansiddiqui352a45c2019-08-19 10:15:36 +0000446 if _, ok := RsrcMgr.ResourceMgrs[ponIntfID]; !ok {
447 err := errors.New("invalid-pon-interface-" + strconv.Itoa(int(ponIntfID)))
448 return 0, err
449 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400450 // Get ONU id for a provided pon interface ID.
npujarec5762e2020-01-01 14:08:48 +0530451 ONUID, err := RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ctx, ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400452 ponrmgr.ONU_ID, 1)
453 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000454 logger.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700455 ponIntfID, ponrmgr.ONU_ID)
cbabuabf02352019-10-15 13:14:56 +0200456 return 0, err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400457 }
458 if ONUID != nil {
npujarec5762e2020-01-01 14:08:48 +0530459 RsrcMgr.ResourceMgrs[ponIntfID].InitResourceMap(ctx, fmt.Sprintf("%d,%d", ponIntfID, ONUID[0]))
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700460 return ONUID[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400461 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530462
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700463 return 0, err // return OnuID 0 on error
Abhilash S.L7f17e402019-03-15 17:40:41 +0530464}
465
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700466// GetFlowIDInfo returns the slice of flow info of the given pon-port
467// Note: For flows which trap from the NNI and not really associated with any particular
468// 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 +0530469func (RsrcMgr *OpenOltResourceMgr) GetFlowIDInfo(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32, flowID uint32) *[]FlowInfo {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530470 var flows []FlowInfo
471
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700472 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530473 if err := RsrcMgr.ResourceMgrs[ponIntfID].GetFlowIDInfo(ctx, FlowPath, flowID, &flows); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000474 logger.Errorw("Error while getting flows from KV store", log.Fields{"flowId": flowID})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530475 return nil
476 }
477 if len(flows) == 0 {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000478 logger.Debugw("No flowInfo found in KV store", log.Fields{"flowPath": FlowPath})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530479 return nil
480 }
481 return &flows
482}
483
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700484// GetCurrentFlowIDsForOnu fetches flow ID from the resource manager
485// Note: For flows which trap from the NNI and not really associated with any particular
486// 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 +0530487func (RsrcMgr *OpenOltResourceMgr) GetCurrentFlowIDsForOnu(ctx context.Context, PONIntfID uint32, ONUID int32, UNIID int32) []uint32 {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700488
Abhilash S.L8ee90712019-04-29 16:24:22 +0530489 FlowPath := fmt.Sprintf("%d,%d,%d", PONIntfID, ONUID, UNIID)
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700490 if mgrs, exist := RsrcMgr.ResourceMgrs[PONIntfID]; exist {
npujarec5762e2020-01-01 14:08:48 +0530491 return mgrs.GetCurrentFlowIDsForOnu(ctx, FlowPath)
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700492 }
493 return nil
Abhilash S.L8ee90712019-04-29 16:24:22 +0530494}
495
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700496// UpdateFlowIDInfo updates flow info for the given pon interface, onu id, and uni id
497// Note: For flows which trap from the NNI and not really associated with any particular
498// 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 +0530499func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDInfo(ctx context.Context, ponIntfID int32, onuID int32, uniID int32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700500 flowID uint32, flowData *[]FlowInfo) error {
501 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530502 return RsrcMgr.ResourceMgrs[uint32(ponIntfID)].UpdateFlowIDInfoForOnu(ctx, FlowPath, flowID, *flowData)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530503}
504
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700505// GetFlowID return flow ID for a given pon interface id, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530506func (RsrcMgr *OpenOltResourceMgr) GetFlowID(ctx context.Context, ponIntfID uint32, ONUID int32, uniID int32,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400507 gemportID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700508 flowStoreCookie uint64,
Gamze Abaka724d0852020-03-18 12:10:24 +0000509 flowCategory string, vlanVid uint32, vlanPcp ...uint32) (uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530510
Girish Gowdru0c588b22019-04-23 23:24:56 -0400511 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700512 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, ONUID, uniID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700513
514 RsrcMgr.FlowIDMgmtLock.Lock()
515 defer RsrcMgr.FlowIDMgmtLock.Unlock()
516
npujarec5762e2020-01-01 14:08:48 +0530517 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(ctx, FlowPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400518 if FlowIDs != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000519 logger.Debugw("Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "ONUID": ONUID, "uniID": uniID, "KVpath": FlowPath})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700520 for _, flowID := range FlowIDs {
npujarec5762e2020-01-01 14:08:48 +0530521 FlowInfo := RsrcMgr.GetFlowIDInfo(ctx, ponIntfID, int32(ONUID), int32(uniID), uint32(flowID))
Gamze Abaka724d0852020-03-18 12:10:24 +0000522 er := getFlowIDFromFlowInfo(FlowInfo, flowID, gemportID, flowStoreCookie, flowCategory, vlanVid, vlanPcp...)
salmansiddiqui7ac62132019-08-22 03:58:50 +0000523 if er == nil {
Gamze Abaka724d0852020-03-18 12:10:24 +0000524 log.Debugw("Found flowid for the vlan, pcp, and gem",
525 log.Fields{"flowID": flowID, "vlanVid": vlanVid, "vlanPcp": vlanPcp, "gemPortID": gemportID})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000526 return flowID, er
Abhilash S.L8ee90712019-04-29 16:24:22 +0530527 }
528 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400529 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000530 logger.Debug("No matching flows with flow cookie or flow category, allocating new flowid")
npujarec5762e2020-01-01 14:08:48 +0530531 FlowIDs, err = RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ctx, ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400532 ponrmgr.FLOW_ID, 1)
533 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000534 logger.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700535 ponIntfID, ponrmgr.FLOW_ID)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400536 return uint32(0), err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400537 }
538 if FlowIDs != nil {
npujarec5762e2020-01-01 14:08:48 +0530539 _ = RsrcMgr.ResourceMgrs[ponIntfID].UpdateFlowIDForOnu(ctx, FlowPath, FlowIDs[0], true)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700540 return FlowIDs[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400541 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530542
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700543 return 0, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530544}
545
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700546// GetAllocID return the first Alloc ID for a given pon interface id and onu id and then update the resource map on
547// the KV store with the list of alloc_ids allocated for the pon_intf_onu_id tuple
548// 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 +0530549func (RsrcMgr *OpenOltResourceMgr) GetAllocID(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530550
Girish Gowdru0c588b22019-04-23 23:24:56 -0400551 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700552 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700553
554 RsrcMgr.AllocIDMgmtLock[intfID].Lock()
555 defer RsrcMgr.AllocIDMgmtLock[intfID].Unlock()
556
npujarec5762e2020-01-01 14:08:48 +0530557 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400558 if AllocID != nil {
559 // Since we support only one alloc_id for the ONU at the moment,
560 // return the first alloc_id in the list, if available, for that
561 // ONU.
Girish Kumar2ad402b2020-03-20 19:45:12 +0000562 logger.Debugw("Retrieved alloc ID from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400563 return AllocID[0]
564 }
npujarec5762e2020-01-01 14:08:48 +0530565 AllocID, err = RsrcMgr.ResourceMgrs[intfID].GetResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400566 ponrmgr.ALLOC_ID, 1)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530567
Girish Gowdru0c588b22019-04-23 23:24:56 -0400568 if AllocID == nil || err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000569 logger.Error("Failed to allocate alloc id")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400570 return 0
571 }
572 // update the resource map on KV store with the list of alloc_id
573 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530574 err = RsrcMgr.ResourceMgrs[intfID].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID, AllocID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400575 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000576 logger.Error("Failed to update Alloc ID")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400577 return 0
578 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000579 logger.Debugw("Allocated new Tcont from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400580 return AllocID[0]
Abhilash S.L7f17e402019-03-15 17:40:41 +0530581}
582
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700583// UpdateAllocIdsForOnu updates alloc ids in kv store for a given pon interface id, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530584func (RsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ctx context.Context, ponPort uint32, onuID uint32, uniID uint32, allocID []uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530585
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700586 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530587 return RsrcMgr.ResourceMgrs[ponPort].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700588 allocID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530589}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700590
591// GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530592func (RsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(ctx context.Context, intfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700593 uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530594
Girish Gowdru0c588b22019-04-23 23:24:56 -0400595 /* Get gem ports for given pon interface , onu id and uni id. */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530596
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700597 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530598 return RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530599}
600
Gamze Abakafee36392019-10-03 11:17:24 +0000601// GetCurrentAllocIDsForOnu returns alloc ids for given pon interface and onu id
npujarec5762e2020-01-01 14:08:48 +0530602func (RsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDsForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530603
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700604 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530605 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400606 if AllocID != nil {
Gamze Abakafee36392019-10-03 11:17:24 +0000607 return AllocID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400608 }
Gamze Abakafee36392019-10-03 11:17:24 +0000609 return []uint32{}
610}
611
612// RemoveAllocIDForOnu removes the alloc id for given pon interface, onu id, uni id and alloc id
npujarec5762e2020-01-01 14:08:48 +0530613func (RsrcMgr *OpenOltResourceMgr) RemoveAllocIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, allocID uint32) {
614 allocIDs := RsrcMgr.GetCurrentAllocIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000615 for i := 0; i < len(allocIDs); i++ {
616 if allocIDs[i] == allocID {
617 allocIDs = append(allocIDs[:i], allocIDs[i+1:]...)
618 break
619 }
620 }
npujarec5762e2020-01-01 14:08:48 +0530621 err := RsrcMgr.UpdateAllocIdsForOnu(ctx, intfID, onuID, uniID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000622 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000623 logger.Errorf("Failed to Remove Alloc Id For Onu. IntfID %d onuID %d uniID %d allocID %d",
Gamze Abakafee36392019-10-03 11:17:24 +0000624 intfID, onuID, uniID, allocID)
625 }
626}
627
628// RemoveGemPortIDForOnu removes the gem port id for given pon interface, onu id, uni id and gem port id
npujarec5762e2020-01-01 14:08:48 +0530629func (RsrcMgr *OpenOltResourceMgr) RemoveGemPortIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, gemPortID uint32) {
630 gemPortIDs := RsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000631 for i := 0; i < len(gemPortIDs); i++ {
632 if gemPortIDs[i] == gemPortID {
633 gemPortIDs = append(gemPortIDs[:i], gemPortIDs[i+1:]...)
634 break
635 }
636 }
npujarec5762e2020-01-01 14:08:48 +0530637 err := RsrcMgr.UpdateGEMPortIDsForOnu(ctx, intfID, onuID, uniID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000638 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000639 logger.Errorf("Failed to Remove Gem Id For Onu. IntfID %d onuID %d uniID %d gemPortId %d",
Gamze Abakafee36392019-10-03 11:17:24 +0000640 intfID, onuID, uniID, gemPortID)
641 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530642}
643
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700644// UpdateGEMportsPonportToOnuMapOnKVStore updates onu and uni id associated with the gem port to the kv store
645// This stored information is used when packet_indication is received and we need to derive the ONU Id for which
646// the packet arrived based on the pon_intf and gemport available in the packet_indication
npujarec5762e2020-01-01 14:08:48 +0530647func (RsrcMgr *OpenOltResourceMgr) UpdateGEMportsPonportToOnuMapOnKVStore(ctx context.Context, gemPorts []uint32, PonPort uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700648 onuID uint32, uniID uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530649
Girish Gowdru0c588b22019-04-23 23:24:56 -0400650 /* Update onu and uni id associated with the gem port to the kv store. */
651 var IntfGEMPortPath string
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700652 Data := fmt.Sprintf("%d %d", onuID, uniID)
653 for _, GEM := range gemPorts {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400654 IntfGEMPortPath = fmt.Sprintf("%d,%d", PonPort, GEM)
655 Val, err := json.Marshal(Data)
656 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000657 logger.Error("failed to Marshal")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400658 return err
659 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700660
npujarec5762e2020-01-01 14:08:48 +0530661 if err = RsrcMgr.KVStore.Put(ctx, IntfGEMPortPath, Val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000662 logger.Errorf("Failed to update resource %s", IntfGEMPortPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400663 return err
664 }
665 }
666 return nil
Abhilash S.L7f17e402019-03-15 17:40:41 +0530667}
668
Gamze Abakafee36392019-10-03 11:17:24 +0000669// RemoveGEMportPonportToOnuMapOnKVStore removes the relationship between the gem port and pon port
npujarec5762e2020-01-01 14:08:48 +0530670func (RsrcMgr *OpenOltResourceMgr) RemoveGEMportPonportToOnuMapOnKVStore(ctx context.Context, GemPort uint32, PonPort uint32) {
Gamze Abakafee36392019-10-03 11:17:24 +0000671 IntfGEMPortPath := fmt.Sprintf("%d,%d", PonPort, GemPort)
npujarec5762e2020-01-01 14:08:48 +0530672 err := RsrcMgr.KVStore.Delete(ctx, IntfGEMPortPath)
Gamze Abakafee36392019-10-03 11:17:24 +0000673 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000674 logger.Errorf("Failed to Remove Gem port-Pon port to onu map on kv store. Gem %d PonPort %d", GemPort, PonPort)
Gamze Abakafee36392019-10-03 11:17:24 +0000675 }
676}
677
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700678// GetGEMPortID gets gem port id for a particular pon port, onu id and uni id and then update the resource map on
679// the KV store with the list of gemport_id allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530680func (RsrcMgr *OpenOltResourceMgr) GetGEMPortID(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700681 uniID uint32, NumOfPorts uint32) ([]uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530682
Girish Gowdru0c588b22019-04-23 23:24:56 -0400683 /* Get gem port id for a particular pon port, onu id
684 and uni id.
685 */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530686
Girish Gowdru0c588b22019-04-23 23:24:56 -0400687 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700688 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530689
Girish Gowdrab77ded92020-04-08 11:45:05 -0700690 RsrcMgr.GemPortIDMgmtLock[ponPort].Lock()
691 defer RsrcMgr.GemPortIDMgmtLock[ponPort].Unlock()
692
npujarec5762e2020-01-01 14:08:48 +0530693 GEMPortList := RsrcMgr.ResourceMgrs[ponPort].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400694 if GEMPortList != nil {
695 return GEMPortList, nil
696 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530697
npujarec5762e2020-01-01 14:08:48 +0530698 GEMPortList, err = RsrcMgr.ResourceMgrs[ponPort].GetResourceID(ctx, ponPort,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400699 ponrmgr.GEMPORT_ID, NumOfPorts)
700 if err != nil && GEMPortList == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000701 logger.Errorf("Failed to get gem port id for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400702 return nil, err
703 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530704
Girish Gowdru0c588b22019-04-23 23:24:56 -0400705 // update the resource map on KV store with the list of gemport_id
706 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530707 err = RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400708 GEMPortList)
709 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000710 logger.Errorf("Failed to update GEM ports to kv store for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400711 return nil, err
712 }
npujarec5762e2020-01-01 14:08:48 +0530713 _ = RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(ctx, GEMPortList, ponPort,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700714 onuID, uniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400715 return GEMPortList, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530716}
717
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700718// 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 +0530719func (RsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700720 uniID uint32, GEMPortList []uint32) error {
721 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530722 return RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400723 GEMPortList)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530724
725}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700726
727// FreeonuID releases(make free) onu id for a particular pon-port
npujarec5762e2020-01-01 14:08:48 +0530728func (RsrcMgr *OpenOltResourceMgr) FreeonuID(ctx context.Context, intfID uint32, onuID []uint32) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700729
Girish Gowdra38d533d2020-03-30 20:38:51 -0700730 RsrcMgr.OnuIDMgmtLock[intfID].Lock()
Girish Gowdrab77ded92020-04-08 11:45:05 -0700731 defer RsrcMgr.OnuIDMgmtLock[intfID].Unlock()
732
npujarec5762e2020-01-01 14:08:48 +0530733 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID, ponrmgr.ONU_ID, onuID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530734
Girish Gowdru0c588b22019-04-23 23:24:56 -0400735 /* Free onu id for a particular interface.*/
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700736 var IntfonuID string
737 for _, onu := range onuID {
738 IntfonuID = fmt.Sprintf("%d,%d", intfID, onu)
npujarec5762e2020-01-01 14:08:48 +0530739 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfonuID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400740 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530741}
742
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700743// FreeFlowID returns the free flow id for a given interface, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530744func (RsrcMgr *OpenOltResourceMgr) FreeFlowID(ctx context.Context, IntfID uint32, onuID int32,
Devmalya Paul495b94a2019-08-27 19:42:00 -0400745 uniID int32, FlowID uint32) {
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400746 var IntfONUID string
747 var err error
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530748
Girish Gowdrab77ded92020-04-08 11:45:05 -0700749 RsrcMgr.FlowIDMgmtLock.Lock()
750 defer RsrcMgr.FlowIDMgmtLock.Unlock()
751
752 FlowIds := make([]uint32, 0)
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530753 FlowIds = append(FlowIds, FlowID)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700754 IntfONUID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530755 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(ctx, IntfONUID, FlowID, false)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400756 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000757 logger.Errorw("Failed to Update flow id for", log.Fields{"intf": IntfONUID})
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400758 }
npujarec5762e2020-01-01 14:08:48 +0530759 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(ctx, IntfONUID, FlowID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700760
npujarec5762e2020-01-01 14:08:48 +0530761 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.FLOW_ID, FlowIds)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400762}
763
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700764// FreeFlowIDs releases the flow Ids
npujarec5762e2020-01-01 14:08:48 +0530765func (RsrcMgr *OpenOltResourceMgr) FreeFlowIDs(ctx context.Context, IntfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700766 uniID uint32, FlowID []uint32) {
Girish Gowdra38d533d2020-03-30 20:38:51 -0700767 RsrcMgr.FlowIDMgmtLock.Lock()
Girish Gowdrab77ded92020-04-08 11:45:05 -0700768 defer RsrcMgr.FlowIDMgmtLock.Unlock()
769
npujarec5762e2020-01-01 14:08:48 +0530770 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.FLOW_ID, FlowID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530771
Abhilash S.L8ee90712019-04-29 16:24:22 +0530772 var IntfOnuIDUniID string
Girish Gowdru0c588b22019-04-23 23:24:56 -0400773 var err error
774 for _, flow := range FlowID {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700775 IntfOnuIDUniID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530776 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(ctx, IntfOnuIDUniID, flow, false)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400777 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000778 logger.Errorw("Failed to Update flow id for", log.Fields{"intf": IntfOnuIDUniID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400779 }
npujarec5762e2020-01-01 14:08:48 +0530780 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(ctx, IntfOnuIDUniID, flow)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400781 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530782}
783
Gamze Abakafee36392019-10-03 11:17:24 +0000784// FreeAllocID frees AllocID on the PON resource pool and also frees the allocID association
785// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530786func (RsrcMgr *OpenOltResourceMgr) FreeAllocID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000787 uniID uint32, allocID uint32) {
Girish Gowdrab77ded92020-04-08 11:45:05 -0700788 RsrcMgr.AllocIDMgmtLock[IntfID].Lock()
789 defer RsrcMgr.AllocIDMgmtLock[IntfID].Unlock()
790
npujarec5762e2020-01-01 14:08:48 +0530791 RsrcMgr.RemoveAllocIDForOnu(ctx, IntfID, onuID, uniID, allocID)
Gamze Abakafee36392019-10-03 11:17:24 +0000792 allocIDs := make([]uint32, 0)
793 allocIDs = append(allocIDs, allocID)
npujarec5762e2020-01-01 14:08:48 +0530794 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.ALLOC_ID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000795}
796
797// FreeGemPortID frees GemPortID on the PON resource pool and also frees the gemPortID association
798// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530799func (RsrcMgr *OpenOltResourceMgr) FreeGemPortID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000800 uniID uint32, gemPortID uint32) {
Girish Gowdrab77ded92020-04-08 11:45:05 -0700801 RsrcMgr.GemPortIDMgmtLock[IntfID].Lock()
802 defer RsrcMgr.GemPortIDMgmtLock[IntfID].Unlock()
803
npujarec5762e2020-01-01 14:08:48 +0530804 RsrcMgr.RemoveGemPortIDForOnu(ctx, IntfID, onuID, uniID, gemPortID)
Gamze Abakafee36392019-10-03 11:17:24 +0000805 gemPortIDs := make([]uint32, 0)
806 gemPortIDs = append(gemPortIDs, gemPortID)
npujarec5762e2020-01-01 14:08:48 +0530807 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.GEMPORT_ID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000808}
809
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700810// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id, and the clears the
811// resource map and the onuID associated with (pon_intf_id, gemport_id) tuple,
npujarec5762e2020-01-01 14:08:48 +0530812func (RsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530813
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700814 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530815
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700816 RsrcMgr.AllocIDMgmtLock[intfID].Lock()
Girish Gowdrab77ded92020-04-08 11:45:05 -0700817 AllocIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700818
npujarec5762e2020-01-01 14:08:48 +0530819 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400820 ponrmgr.ALLOC_ID,
821 AllocIDs)
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700822 RsrcMgr.AllocIDMgmtLock[intfID].Unlock()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530823
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700824 RsrcMgr.GemPortIDMgmtLock[intfID].Lock()
npujarec5762e2020-01-01 14:08:48 +0530825 GEMPortIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
826 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400827 ponrmgr.GEMPORT_ID,
828 GEMPortIDs)
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700829 RsrcMgr.GemPortIDMgmtLock[intfID].Unlock()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530830
Girish Gowdra38d533d2020-03-30 20:38:51 -0700831 RsrcMgr.FlowIDMgmtLock.Lock()
npujarec5762e2020-01-01 14:08:48 +0530832 FlowIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentFlowIDsForOnu(ctx, IntfOnuIDUniID)
833 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400834 ponrmgr.FLOW_ID,
835 FlowIDs)
Girish Gowdra38d533d2020-03-30 20:38:51 -0700836 RsrcMgr.FlowIDMgmtLock.Unlock()
837
Girish Gowdru0c588b22019-04-23 23:24:56 -0400838 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
npujarec5762e2020-01-01 14:08:48 +0530839 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400840 // Clear the ONU Id associated with the (pon_intf_id, gemport_id) tuple.
841 for _, GEM := range GEMPortIDs {
npujarec5762e2020-01-01 14:08:48 +0530842 _ = RsrcMgr.KVStore.Delete(ctx, fmt.Sprintf("%d,%d", intfID, GEM))
Girish Gowdru0c588b22019-04-23 23:24:56 -0400843 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530844}
845
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700846// IsFlowCookieOnKVStore checks if the given flow cookie is present on the kv store
847// Returns true if the flow cookie is found, otherwise it returns false
npujarec5762e2020-01-01 14:08:48 +0530848func (RsrcMgr *OpenOltResourceMgr) IsFlowCookieOnKVStore(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700849 flowStoreCookie uint64) bool {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530850
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700851 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530852 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(ctx, FlowPath)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400853 if FlowIDs != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000854 logger.Debugw("Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "onuID": onuID, "uniID": uniID, "KVpath": FlowPath})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700855 for _, flowID := range FlowIDs {
npujarec5762e2020-01-01 14:08:48 +0530856 FlowInfo := RsrcMgr.GetFlowIDInfo(ctx, ponIntfID, int32(onuID), int32(uniID), uint32(flowID))
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400857 if FlowInfo != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000858 logger.Debugw("Found flows", log.Fields{"flows": *FlowInfo, "flowId": flowID})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400859 for _, Info := range *FlowInfo {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700860 if Info.FlowStoreCookie == flowStoreCookie {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000861 logger.Debug("Found flow matching with flowStore cookie", log.Fields{"flowId": flowID, "flowStoreCookie": flowStoreCookie})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400862 return true
863 }
864 }
865 }
866 }
867 }
868 return false
869}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400870
salmansiddiqui7ac62132019-08-22 03:58:50 +0000871// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000872// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530873func (RsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) []uint32 {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000874 Path := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000875 var Data []uint32
npujarec5762e2020-01-01 14:08:48 +0530876 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400877 if err == nil {
878 if Value != nil {
879 Val, err := kvstore.ToByte(Value.Value)
880 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000881 logger.Errorw("Failed to convert into byte array", log.Fields{"error": err})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400882 return Data
883 }
884 if err = json.Unmarshal(Val, &Data); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000885 logger.Error("Failed to unmarshal", log.Fields{"error": err})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400886 return Data
887 }
888 }
889 } else {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000890 logger.Errorf("Failed to get TP id from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400891 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000892 logger.Debugf("Getting TP id %d from path %s", Data, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400893 return Data
894
895}
896
Gamze Abakafee36392019-10-03 11:17:24 +0000897// RemoveTechProfileIDsForOnu deletes all tech profile ids from the KV-Store for the given onu based on the path
898// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530899func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDsForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) error {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000900 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
npujarec5762e2020-01-01 14:08:48 +0530901 if err := RsrcMgr.KVStore.Delete(ctx, IntfOnuUniID); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000902 logger.Errorw("Failed to delete techprofile id resource in KV store", log.Fields{"path": IntfOnuUniID})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400903 return err
904 }
905 return nil
906}
907
Gamze Abakafee36392019-10-03 11:17:24 +0000908// RemoveTechProfileIDForOnu deletes a specific tech profile id from the KV-Store for the given onu based on the path
909// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530910func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32, TpID uint32) error {
911 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000912 for i, tpIDInList := range tpIDList {
913 if tpIDInList == TpID {
914 tpIDList = append(tpIDList[:i], tpIDList[i+1:]...)
915 }
916 }
917 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
918 Value, err := json.Marshal(tpIDList)
919 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000920 logger.Error("failed to Marshal")
Gamze Abakafee36392019-10-03 11:17:24 +0000921 return err
922 }
npujarec5762e2020-01-01 14:08:48 +0530923 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000924 logger.Errorf("Failed to update resource %s", IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000925 return err
926 }
927 return err
928}
929
930// UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
931// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530932func (RsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32,
salmansiddiqui7ac62132019-08-22 03:58:50 +0000933 UniID uint32, TpID uint32) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400934 var Value []byte
935 var err error
936
salmansiddiqui7ac62132019-08-22 03:58:50 +0000937 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000938
npujarec5762e2020-01-01 14:08:48 +0530939 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000940 for _, value := range tpIDList {
941 if value == TpID {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000942 logger.Debugf("TpID %d is already in tpIdList for the path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000943 return err
944 }
945 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000946 logger.Debugf("updating tp id %d on path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000947 tpIDList = append(tpIDList, TpID)
948 Value, err = json.Marshal(tpIDList)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400949 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000950 logger.Error("failed to Marshal")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400951 return err
952 }
npujarec5762e2020-01-01 14:08:48 +0530953 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000954 logger.Errorf("Failed to update resource %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400955 return err
956 }
957 return err
958}
959
salmansiddiqui7ac62132019-08-22 03:58:50 +0000960// UpdateMeterIDForOnu updates the meter id in the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000961// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +0530962func (RsrcMgr *OpenOltResourceMgr) UpdateMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000963 UniID uint32, TpID uint32, MeterConfig *ofp.OfpMeterConfig) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400964 var Value []byte
965 var err error
966
Gamze Abakafee36392019-10-03 11:17:24 +0000967 IntfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400968 Value, err = json.Marshal(*MeterConfig)
969 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000970 logger.Error("failed to Marshal meter config")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400971 return err
972 }
npujarec5762e2020-01-01 14:08:48 +0530973 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000974 logger.Errorf("Failed to store meter into KV store %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400975 return err
976 }
977 return err
978}
979
Gamze Abakafee36392019-10-03 11:17:24 +0000980// GetMeterIDForOnu fetches the meter id from the kv store for the given onu based on the path
981// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +0530982func (RsrcMgr *OpenOltResourceMgr) GetMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000983 UniID uint32, TpID uint32) (*ofp.OfpMeterConfig, error) {
984 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400985 var meterConfig ofp.OfpMeterConfig
npujarec5762e2020-01-01 14:08:48 +0530986 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400987 if err == nil {
988 if Value != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000989 logger.Debug("Found meter in KV store", log.Fields{"Direction": Direction})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000990 Val, er := kvstore.ToByte(Value.Value)
991 if er != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000992 logger.Errorw("Failed to convert into byte array", log.Fields{"error": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000993 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400994 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000995 if er = json.Unmarshal(Val, &meterConfig); er != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000996 logger.Error("Failed to unmarshal meterconfig", log.Fields{"error": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000997 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400998 }
999 } else {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001000 logger.Debug("meter-does-not-exists-in-KVStore")
Manikkaraj kb1d51442019-07-23 10:41:02 -04001001 return nil, err
1002 }
1003 } else {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001004 logger.Errorf("Failed to get Meter config from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001005
1006 }
1007 return &meterConfig, err
1008}
1009
Gamze Abakafee36392019-10-03 11:17:24 +00001010// RemoveMeterIDForOnu deletes the meter id from the kV-Store for the given onu based on the path
1011// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +05301012func (RsrcMgr *OpenOltResourceMgr) RemoveMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +00001013 UniID uint32, TpID uint32) error {
1014 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
npujarec5762e2020-01-01 14:08:48 +05301015 if err := RsrcMgr.KVStore.Delete(ctx, Path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001016 logger.Errorf("Failed to delete meter id %s from kvstore ", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001017 return err
1018 }
1019 return nil
1020}
salmansiddiqui7ac62132019-08-22 03:58:50 +00001021
Gamze Abaka724d0852020-03-18 12:10:24 +00001022func getFlowIDFromFlowInfo(FlowInfo *[]FlowInfo, flowID, gemportID uint32, flowStoreCookie uint64, flowCategory string,
1023 vlanVid uint32, vlanPcp ...uint32) error {
salmansiddiqui7ac62132019-08-22 03:58:50 +00001024 if FlowInfo != nil {
1025 for _, Info := range *FlowInfo {
1026 if int32(gemportID) == Info.Flow.GemportId && flowCategory != "" && Info.FlowCategory == flowCategory {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001027 logger.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
Gamze Abaka724d0852020-03-18 12:10:24 +00001028 if Info.FlowCategory == "HSIA_FLOW" {
1029 if err := checkVlanAndPbitEqualityForFlows(vlanVid, Info, vlanPcp[0]); err == nil {
1030 return nil
1031 }
salmansiddiqui7ac62132019-08-22 03:58:50 +00001032 }
1033 }
1034 if int32(gemportID) == Info.Flow.GemportId && flowStoreCookie != 0 && Info.FlowStoreCookie == flowStoreCookie {
1035 if flowCategory != "" && Info.FlowCategory == flowCategory {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001036 logger.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001037 return nil
1038 }
1039 }
1040 }
1041 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001042 logger.Debugw("the flow can be related to a different service", log.Fields{"flow_info": FlowInfo})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001043 return errors.New("invalid flow-info")
1044}
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301045
Gamze Abaka724d0852020-03-18 12:10:24 +00001046func checkVlanAndPbitEqualityForFlows(vlanVid uint32, Info FlowInfo, vlanPcp uint32) error {
1047 if err := checkVlanEqualityForFlows(vlanVid, Info); err != nil {
1048 return err
1049 }
1050
1051 //flow has remark action and pbits
1052 if Info.Flow.Action.Cmd.RemarkInnerPbits || Info.Flow.Action.Cmd.RemarkOuterPbits {
1053 if vlanPcp == Info.Flow.Action.OPbits || vlanPcp == Info.Flow.Action.IPbits {
1054 return nil
1055 }
1056 } else if vlanPcp == Info.Flow.Classifier.OPbits {
1057 //no remark action but flow has pbits
1058 return nil
1059 } else if vlanPcp == 0xff || Info.Flow.Classifier.OPbits == 0xff {
1060 // no pbit found
1061 return nil
1062 }
1063 return errors.New("not found in terms of pbit equality")
1064}
1065
1066func checkVlanEqualityForFlows(vlanVid uint32, Info FlowInfo) error {
1067 if vlanVid == Info.Flow.Action.OVid || vlanVid == Info.Flow.Classifier.IVid {
1068 return nil
1069 }
1070 return errors.New("not found in terms of vlan_id equality")
1071}
1072
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301073//AddGemToOnuGemInfo adds gemport to onugem info kvstore
npujarec5762e2020-01-01 14:08:48 +05301074func (RsrcMgr *OpenOltResourceMgr) AddGemToOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, gemPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301075 var onuGemData []OnuGemInfo
1076 var err error
1077
npujarec5762e2020-01-01 14:08:48 +05301078 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001079 logger.Errorf("failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301080 return err
1081 }
1082 if len(onuGemData) == 0 {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001083 logger.Errorw("failed to ger Onuid info ", log.Fields{"intfid": intfID, "onuid": onuID})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301084 return err
1085 }
1086
1087 for idx, onugem := range onuGemData {
1088 if onugem.OnuID == onuID {
1089 for _, gem := range onuGemData[idx].GemPorts {
1090 if gem == gemPort {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001091 logger.Debugw("Gem already present in onugem info, skpping addition", log.Fields{"gem": gem})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301092 return nil
1093 }
1094 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001095 logger.Debugw("Added gem to onugem info", log.Fields{"gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301096 onuGemData[idx].GemPorts = append(onuGemData[idx].GemPorts, gemPort)
1097 break
1098 }
1099 }
npujarec5762e2020-01-01 14:08:48 +05301100 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301101 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001102 logger.Error("Failed to add onugem to kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301103 return err
1104 }
1105 return err
1106}
1107
1108//GetOnuGemInfo gets onu gem info from the kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301109func (RsrcMgr *OpenOltResourceMgr) GetOnuGemInfo(ctx context.Context, IntfID uint32) ([]OnuGemInfo, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301110 var onuGemData []OnuGemInfo
1111
npujarec5762e2020-01-01 14:08:48 +05301112 if err := RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001113 logger.Errorf("failed to get onuifo for intfid %d", IntfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301114 return nil, err
1115 }
1116
1117 return onuGemData, nil
1118}
1119
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001120// AddOnuGemInfo adds onu info on to the kvstore per interface
1121func (RsrcMgr *OpenOltResourceMgr) AddOnuGemInfo(ctx context.Context, IntfID uint32, onuGem OnuGemInfo) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301122 var onuGemData []OnuGemInfo
1123 var err error
1124
npujarec5762e2020-01-01 14:08:48 +05301125 if err = RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001126 logger.Errorf("failed to get onuifo for intfid %d", IntfID)
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001127 return olterrors.NewErrPersistence("get", "OnuGemInfo", IntfID,
1128 log.Fields{"onuGem": onuGem, "intfID": IntfID}, err)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301129 }
1130 onuGemData = append(onuGemData, onuGem)
npujarec5762e2020-01-01 14:08:48 +05301131 err = RsrcMgr.ResourceMgrs[IntfID].AddOnuGemInfo(ctx, IntfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301132 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001133 logger.Error("Failed to add onugem to kv store")
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001134 return olterrors.NewErrPersistence("set", "OnuGemInfo", IntfID,
1135 log.Fields{"onuGemData": onuGemData, "intfID": IntfID}, err)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301136 }
1137
Girish Kumar2ad402b2020-03-20 19:45:12 +00001138 logger.Debugw("added onu to onugeminfo", log.Fields{"intf": IntfID, "onugem": onuGem})
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001139 return nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301140}
1141
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301142// 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 +05301143func (RsrcMgr *OpenOltResourceMgr) AddUniPortToOnuInfo(ctx context.Context, intfID uint32, onuID uint32, portNo uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301144 var onuGemData []OnuGemInfo
1145 var err error
1146
npujarec5762e2020-01-01 14:08:48 +05301147 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001148 logger.Errorf("failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301149 return
1150 }
1151 for idx, onu := range onuGemData {
1152 if onu.OnuID == onuID {
1153 for _, uni := range onu.UniPorts {
1154 if uni == portNo {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001155 logger.Debugw("uni already present in onugem info", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301156 return
1157 }
1158 }
1159 onuGemData[idx].UniPorts = append(onuGemData[idx].UniPorts, portNo)
1160 break
1161 }
1162 }
npujarec5762e2020-01-01 14:08:48 +05301163 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301164 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001165 logger.Errorw("Failed to add uin port in onugem to kv store", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301166 return
1167 }
1168 return
1169}
1170
1171//UpdateGemPortForPktIn updates gemport for pkt in path to kvstore, path being intfid, onuid, portno
npujarec5762e2020-01-01 14:08:48 +05301172func (RsrcMgr *OpenOltResourceMgr) UpdateGemPortForPktIn(ctx context.Context, pktIn PacketInInfoKey, gemPort uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301173
1174 path := fmt.Sprintf(OnuPacketINPath, pktIn.IntfID, pktIn.OnuID, pktIn.LogicalPort)
1175 Value, err := json.Marshal(gemPort)
1176 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001177 logger.Error("Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301178 return
1179 }
npujarec5762e2020-01-01 14:08:48 +05301180 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001181 logger.Errorw("Failed to put to kvstore", log.Fields{"path": path, "value": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301182 return
1183 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001184 logger.Debugw("added gem packet in successfully", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301185
1186 return
1187}
1188
1189// GetGemPortFromOnuPktIn gets the gem port from onu pkt in path, path being intfid, onuid, portno
npujarec5762e2020-01-01 14:08:48 +05301190func (RsrcMgr *OpenOltResourceMgr) GetGemPortFromOnuPktIn(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) (uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301191
1192 var Val []byte
1193 var gemPort uint32
1194
1195 path := fmt.Sprintf(OnuPacketINPath, intfID, onuID, logicalPort)
1196
npujarec5762e2020-01-01 14:08:48 +05301197 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301198 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001199 logger.Errorw("Failed to get from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301200 return uint32(0), err
1201 } else if value == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001202 logger.Debugw("No pkt in gem found", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301203 return uint32(0), nil
1204 }
1205
1206 if Val, err = kvstore.ToByte(value.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001207 logger.Error("Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301208 return uint32(0), err
1209 }
1210 if err = json.Unmarshal(Val, &gemPort); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001211 logger.Error("Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301212 return uint32(0), err
1213 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001214 logger.Debugw("found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301215
1216 return gemPort, nil
1217}
1218
1219// DelGemPortPktIn deletes the gemport from the pkt in path
npujarec5762e2020-01-01 14:08:48 +05301220func (RsrcMgr *OpenOltResourceMgr) DelGemPortPktIn(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301221
1222 path := fmt.Sprintf(OnuPacketINPath, intfID, onuID, logicalPort)
npujarec5762e2020-01-01 14:08:48 +05301223 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001224 logger.Errorf("Falied to remove resource %s", path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301225 return err
1226 }
1227 return nil
1228}
1229
1230// DelOnuGemInfoForIntf deletes the onugem info from kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301231func (RsrcMgr *OpenOltResourceMgr) DelOnuGemInfoForIntf(ctx context.Context, intfID uint32) error {
1232 if err := RsrcMgr.ResourceMgrs[intfID].DelOnuGemInfoForIntf(ctx, intfID); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001233 logger.Errorw("failed to delete onu gem info for", log.Fields{"intfid": intfID})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301234 return err
1235 }
1236 return nil
1237}
1238
1239//GetNNIFromKVStore gets NNi intfids from kvstore. path being per device
npujarec5762e2020-01-01 14:08:48 +05301240func (RsrcMgr *OpenOltResourceMgr) GetNNIFromKVStore(ctx context.Context) ([]uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301241
1242 var nni []uint32
1243 var Val []byte
1244
1245 path := fmt.Sprintf(NnniIntfID)
npujarec5762e2020-01-01 14:08:48 +05301246 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301247 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001248 logger.Error("failed to get data from kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301249 return nil, err
1250 }
1251 if value != nil {
1252 if Val, err = kvstore.ToByte(value.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001253 logger.Error("Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301254 return nil, err
1255 }
1256 if err = json.Unmarshal(Val, &nni); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001257 logger.Error("Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301258 return nil, err
1259 }
1260 }
1261 return nni, err
1262}
1263
1264// AddNNIToKVStore adds Nni interfaces to kvstore, path being per device.
npujarec5762e2020-01-01 14:08:48 +05301265func (RsrcMgr *OpenOltResourceMgr) AddNNIToKVStore(ctx context.Context, nniIntf uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301266 var Value []byte
1267
npujarec5762e2020-01-01 14:08:48 +05301268 nni, err := RsrcMgr.GetNNIFromKVStore(ctx)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301269 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001270 logger.Error("failed to fetch nni interfaces from kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301271 return err
1272 }
1273
1274 path := fmt.Sprintf(NnniIntfID)
1275 nni = append(nni, nniIntf)
1276 Value, err = json.Marshal(nni)
1277 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001278 logger.Error("Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301279 }
npujarec5762e2020-01-01 14:08:48 +05301280 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001281 logger.Errorw("Failed to put to kvstore", log.Fields{"path": path, "value": Value})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301282 return err
1283 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001284 logger.Debugw("added nni to kv successfully", log.Fields{"path": path, "nni": nniIntf})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301285 return nil
1286}
1287
1288// DelNNiFromKVStore deletes nni interface list from kv store.
npujarec5762e2020-01-01 14:08:48 +05301289func (RsrcMgr *OpenOltResourceMgr) DelNNiFromKVStore(ctx context.Context) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301290
1291 path := fmt.Sprintf(NnniIntfID)
1292
npujarec5762e2020-01-01 14:08:48 +05301293 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001294 logger.Errorw("Failed to delete nni interfaces from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301295 return err
1296 }
1297 return nil
1298}
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301299
1300//UpdateFlowIDsForGem updates flow id per gemport
npujarec5762e2020-01-01 14:08:48 +05301301func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDsForGem(ctx context.Context, intf uint32, gem uint32, flowIDs []uint32) error {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301302 var val []byte
1303 path := fmt.Sprintf(FlowIDsForGem, intf)
1304
npujarec5762e2020-01-01 14:08:48 +05301305 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301306 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001307 logger.Error("Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301308 return err
1309 }
1310 if flowsForGem == nil {
1311 flowsForGem = make(map[uint32][]uint32)
1312 }
1313 flowsForGem[gem] = flowIDs
1314 val, err = json.Marshal(flowsForGem)
1315 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001316 logger.Error("Failed to marshal data", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301317 return err
1318 }
Girish Gowdrab77ded92020-04-08 11:45:05 -07001319
Girish Gowdra38d533d2020-03-30 20:38:51 -07001320 RsrcMgr.flowIDToGemInfoLock.Lock()
1321 defer RsrcMgr.flowIDToGemInfoLock.Unlock()
npujarec5762e2020-01-01 14:08:48 +05301322 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001323 logger.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301324 return err
1325 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001326 logger.Debugw("added flowid list for gem to kv successfully", log.Fields{"path": path, "flowidlist": flowsForGem[gem]})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301327 return nil
1328}
1329
1330//DeleteFlowIDsForGem deletes the flowID list entry per gem from kvstore.
npujarec5762e2020-01-01 14:08:48 +05301331func (RsrcMgr *OpenOltResourceMgr) DeleteFlowIDsForGem(ctx context.Context, intf uint32, gem uint32) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301332 path := fmt.Sprintf(FlowIDsForGem, intf)
1333 var val []byte
1334
npujarec5762e2020-01-01 14:08:48 +05301335 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301336 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001337 logger.Error("Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301338 return
1339 }
1340 if flowsForGem == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001341 logger.Error("No flowids found ", log.Fields{"intf": intf, "gemport": gem})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301342 return
1343 }
1344 // once we get the flows per gem map from kv , just delete the gem entry from the map
1345 delete(flowsForGem, gem)
1346 // once gem entry is deleted update the kv store.
1347 val, err = json.Marshal(flowsForGem)
1348 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001349 logger.Error("Failed to marshal data", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301350 return
1351 }
Girish Gowdra38d533d2020-03-30 20:38:51 -07001352
1353 RsrcMgr.flowIDToGemInfoLock.Lock()
1354 defer RsrcMgr.flowIDToGemInfoLock.Unlock()
npujarec5762e2020-01-01 14:08:48 +05301355 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001356 logger.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301357 return
1358 }
1359 return
1360}
1361
1362//GetFlowIDsGemMapForInterface gets flowids per gemport and interface
npujarec5762e2020-01-01 14:08:48 +05301363func (RsrcMgr *OpenOltResourceMgr) GetFlowIDsGemMapForInterface(ctx context.Context, intf uint32) (map[uint32][]uint32, error) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301364 path := fmt.Sprintf(FlowIDsForGem, intf)
1365 var flowsForGem map[uint32][]uint32
1366 var val []byte
Girish Gowdra38d533d2020-03-30 20:38:51 -07001367 RsrcMgr.flowIDToGemInfoLock.RLock()
npujarec5762e2020-01-01 14:08:48 +05301368 value, err := RsrcMgr.KVStore.Get(ctx, path)
Girish Gowdra38d533d2020-03-30 20:38:51 -07001369 RsrcMgr.flowIDToGemInfoLock.RUnlock()
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301370 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001371 logger.Error("failed to get data from kv store")
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301372 return nil, err
1373 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001374 if value != nil && value.Value != nil {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301375 if val, err = kvstore.ToByte(value.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001376 logger.Error("Failed to convert to byte array ", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301377 return nil, err
1378 }
1379 if err = json.Unmarshal(val, &flowsForGem); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001380 logger.Error("Failed to unmarshall", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301381 return nil, err
1382 }
1383 }
1384 return flowsForGem, nil
1385}
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301386
1387//DeleteIntfIDGempMapPath deletes the intf id path used to store flow ids per gem to kvstore.
npujarec5762e2020-01-01 14:08:48 +05301388func (RsrcMgr *OpenOltResourceMgr) DeleteIntfIDGempMapPath(ctx context.Context, intf uint32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301389 path := fmt.Sprintf(FlowIDsForGem, intf)
Girish Gowdra38d533d2020-03-30 20:38:51 -07001390 RsrcMgr.flowIDToGemInfoLock.Lock()
1391 defer RsrcMgr.flowIDToGemInfoLock.Unlock()
npujarec5762e2020-01-01 14:08:48 +05301392 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001393 logger.Errorw("Failed to delete nni interfaces from kv store", log.Fields{"path": path})
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301394 return
1395 }
1396 return
1397}
1398
1399// RemoveResourceMap Clear resource map associated with (intfid, onuid, uniid) tuple.
npujarec5762e2020-01-01 14:08:48 +05301400func (RsrcMgr *OpenOltResourceMgr) RemoveResourceMap(ctx context.Context, intfID uint32, onuID int32, uniID int32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301401 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +05301402 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301403}
Esin Karamanccb714b2019-11-29 15:02:06 +00001404
1405//GetMcastQueuePerInterfaceMap gets multicast queue info per pon interface
npujarec5762e2020-01-01 14:08:48 +05301406func (RsrcMgr *OpenOltResourceMgr) GetMcastQueuePerInterfaceMap(ctx context.Context) (map[uint32][]uint32, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001407 path := fmt.Sprintf(McastQueuesForIntf)
1408 var mcastQueueToIntfMap map[uint32][]uint32
1409 var val []byte
1410
npujarec5762e2020-01-01 14:08:48 +05301411 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001412 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001413 logger.Error("failed to get data from kv store")
Esin Karamanccb714b2019-11-29 15:02:06 +00001414 return nil, err
1415 }
1416 if kvPair != nil && kvPair.Value != nil {
1417 if val, err = kvstore.ToByte(kvPair.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001418 logger.Error("Failed to convert to byte array ", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001419 return nil, err
1420 }
1421 if err = json.Unmarshal(val, &mcastQueueToIntfMap); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001422 logger.Error("Failed to unmarshall ", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001423 return nil, err
1424 }
1425 }
1426 return mcastQueueToIntfMap, nil
1427}
1428
1429//AddMcastQueueForIntf adds multicast queue for pon interface
npujarec5762e2020-01-01 14:08:48 +05301430func (RsrcMgr *OpenOltResourceMgr) AddMcastQueueForIntf(ctx context.Context, intf uint32, gem uint32, servicePriority uint32) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001431 var val []byte
1432 path := fmt.Sprintf(McastQueuesForIntf)
1433
npujarec5762e2020-01-01 14:08:48 +05301434 mcastQueues, err := RsrcMgr.GetMcastQueuePerInterfaceMap(ctx)
Esin Karamanccb714b2019-11-29 15:02:06 +00001435 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001436 logger.Errorw("Failed to get multicast queue info for interface", log.Fields{"error": err, "intf": intf})
Esin Karamanccb714b2019-11-29 15:02:06 +00001437 return err
1438 }
1439 if mcastQueues == nil {
1440 mcastQueues = make(map[uint32][]uint32)
1441 }
1442 mcastQueues[intf] = []uint32{gem, servicePriority}
1443 if val, err = json.Marshal(mcastQueues); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001444 logger.Errorw("Failed to marshal data", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001445 return err
1446 }
npujarec5762e2020-01-01 14:08:48 +05301447 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001448 logger.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Esin Karamanccb714b2019-11-29 15:02:06 +00001449 return err
1450 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001451 logger.Debugw("added multicast queue info to KV store successfully", log.Fields{"path": path, "mcastQueueInfo": mcastQueues[intf], "interfaceId": intf})
Esin Karamanccb714b2019-11-29 15:02:06 +00001452 return nil
1453}
1454
1455//AddFlowGroupToKVStore adds flow group into KV store
npujarec5762e2020-01-01 14:08:48 +05301456func (RsrcMgr *OpenOltResourceMgr) AddFlowGroupToKVStore(ctx context.Context, groupEntry *ofp.OfpGroupEntry, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001457 var Value []byte
1458 var err error
1459 var path string
1460 if cached {
1461 path = fmt.Sprintf(FlowGroupCached, groupEntry.Desc.GroupId)
1462 } else {
1463 path = fmt.Sprintf(FlowGroup, groupEntry.Desc.GroupId)
1464 }
1465 //build group info object
1466 var outPorts []uint32
1467 for _, ofBucket := range groupEntry.Desc.Buckets {
1468 for _, ofAction := range ofBucket.Actions {
1469 if ofAction.Type == ofp.OfpActionType_OFPAT_OUTPUT {
1470 outPorts = append(outPorts, ofAction.GetOutput().Port)
1471 }
1472 }
1473 }
1474 groupInfo := GroupInfo{
1475 GroupID: groupEntry.Desc.GroupId,
1476 OutPorts: outPorts,
1477 }
1478
1479 Value, err = json.Marshal(groupInfo)
1480
1481 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001482 logger.Error("failed to Marshal flow group object")
Esin Karamanccb714b2019-11-29 15:02:06 +00001483 return err
1484 }
1485
npujarec5762e2020-01-01 14:08:48 +05301486 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001487 logger.Errorf("Failed to update resource %s", path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001488 return err
1489 }
1490 return nil
1491}
1492
1493//RemoveFlowGroupFromKVStore removes flow group from KV store
npujarec5762e2020-01-01 14:08:48 +05301494func (RsrcMgr *OpenOltResourceMgr) RemoveFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) bool {
Esin Karamanccb714b2019-11-29 15:02:06 +00001495 var path string
1496 if cached {
1497 path = fmt.Sprintf(FlowGroupCached, groupID)
1498 } else {
1499 path = fmt.Sprintf(FlowGroup, groupID)
1500 }
npujarec5762e2020-01-01 14:08:48 +05301501 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001502 logger.Errorf("Failed to remove resource %s due to %s", path, err)
Esin Karamanccb714b2019-11-29 15:02:06 +00001503 return false
1504 }
1505 return true
1506}
1507
1508//GetFlowGroupFromKVStore fetches flow group from the KV store. Returns (false, {} error) if any problem occurs during
1509//fetching the data. Returns (true, groupInfo, nil) if the group is fetched successfully.
1510// Returns (false, {}, nil) if the group does not exists in the KV store.
npujarec5762e2020-01-01 14:08:48 +05301511func (RsrcMgr *OpenOltResourceMgr) GetFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) (bool, GroupInfo, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001512 var groupInfo GroupInfo
1513 var path string
1514 if cached {
1515 path = fmt.Sprintf(FlowGroupCached, groupID)
1516 } else {
1517 path = fmt.Sprintf(FlowGroup, groupID)
1518 }
npujarec5762e2020-01-01 14:08:48 +05301519 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001520 if err != nil {
1521 return false, groupInfo, err
1522 }
1523 if kvPair != nil && kvPair.Value != nil {
1524 Val, err := kvstore.ToByte(kvPair.Value)
1525 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001526 logger.Errorw("Failed to convert flow group into byte array", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001527 return false, groupInfo, err
1528 }
1529 if err = json.Unmarshal(Val, &groupInfo); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001530 logger.Errorw("Failed to unmarshal", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001531 return false, groupInfo, err
1532 }
1533 return true, groupInfo, nil
1534 }
1535 return false, groupInfo, nil
1536}