blob: 8785e25666c07e36e1b7635b033e6315ed1f895b [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"
Girish Gowdra38d533d2020-03-30 20:38:51 -070027 "sync"
Neha Sharmacc656962020-04-14 14:26:11 +000028 "time"
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
Neha Sharmacc656962020-04-14 14:26:11 +000040 KvstoreTimeout = 5 * time.Second
salmansiddiqui7ac62132019-08-22 03:58:50 +000041 // 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"
Esin Karaman04166c82020-07-16 14:23:33 +000049 //OnuPacketINPathPrefix to be used as prefix for keys to be used to store packet-in gem ports
50 OnuPacketINPathPrefix = "onu_packetin/{%d,%d,%d"
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053051 // OnuPacketINPath path on the kvstore to store packetin gemport,which will be used for packetin, pcketout
Esin Karaman04166c82020-07-16 14:23:33 +000052 //format: onu_packetin/<intfid>,<onuid>,<logicalport>,<vlanId>,<priority>
53 OnuPacketINPath = OnuPacketINPathPrefix + ",%d,%d}"
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +053054 //FlowIDsForGem flowids_per_gem/<intfid>
55 FlowIDsForGem = "flowids_per_gem/{%d}"
Esin Karamanccb714b2019-11-29 15:02:06 +000056 //McastQueuesForIntf multicast queues for pon interfaces
57 McastQueuesForIntf = "mcast_qs_for_int"
58 //FlowGroup flow_groups/<flow_group_id>
59 // A group is stored under this path on the KV store after it has been installed to the device.
60 // It should also be deleted after it has been removed from the device accordingly.
61 FlowGroup = "flow_groups/{%d}"
62 //FlowGroupCached flow_groups_cached/<flow_group_id>
63 // When a group add request received, we create the group without setting any members to it since we cannot
64 // set any members to a group until it is associated with a multicast flow. It is a BAL limitation.
65 // When the related multicast flow has been created we perform set members operation for the group.
66 // That is why we need to keep the members of a group until the multicast flow creation request comes.
67 // We preserve the groups under "FlowGroupsCached" directory in the KV store temporarily. Having set members,
68 // we remove the group from the cached group store.
69 FlowGroupCached = "flow_groups_cached/{%d}"
salmansiddiqui7ac62132019-08-22 03:58:50 +000070)
Abhilash S.L7f17e402019-03-15 17:40:41 +053071
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070072// FlowInfo holds the flow information
Abhilash S.L8ee90712019-04-29 16:24:22 +053073type FlowInfo struct {
74 Flow *openolt.Flow
75 FlowStoreCookie uint64
76 FlowCategory string
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053077 LogicalFlowID uint64
78}
79
80// OnuGemInfo holds onu information along with gem port list and uni port list
81type OnuGemInfo struct {
82 OnuID uint32
83 SerialNumber string
84 IntfID uint32
85 GemPorts []uint32
86 UniPorts []uint32
87}
88
89// PacketInInfoKey is the key for packet in gemport
90type PacketInInfoKey struct {
91 IntfID uint32
92 OnuID uint32
93 LogicalPort uint32
Esin Karaman04166c82020-07-16 14:23:33 +000094 VlanID uint16
95 Priority uint8
Abhilash S.L8ee90712019-04-29 16:24:22 +053096}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070097
Esin Karamanccb714b2019-11-29 15:02:06 +000098// GroupInfo holds group information
99type GroupInfo struct {
100 GroupID uint32
101 OutPorts []uint32
102}
103
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700104// OpenOltResourceMgr holds resource related information as provided below for each field
Abhilash S.L7f17e402019-03-15 17:40:41 +0530105type OpenOltResourceMgr struct {
Neha Sharma3f221ae2020-04-29 19:02:12 +0000106 DeviceID string // OLT device id
107 Address string // Host and port of the kv store to connect to
108 Args string // args
109 KVStore *db.Backend // backend kv store connection handle
110 DeviceType string
111 DevInfo *openolt.DeviceInfo // device information
Girish Gowdru0c588b22019-04-23 23:24:56 -0400112 // array of pon resource managers per interface technology
113 ResourceMgrs map[uint32]*ponrmgr.PONResourceManager
Girish Gowdra38d533d2020-03-30 20:38:51 -0700114
115 // This protects concurrent gemport_id allocate/delete calls on a per PON port basis
116 GemPortIDMgmtLock []sync.RWMutex
117 // This protects concurrent alloc_id allocate/delete calls on a per PON port basis
118 AllocIDMgmtLock []sync.RWMutex
119 // This protects concurrent onu_id allocate/delete calls on a per PON port basis
120 OnuIDMgmtLock []sync.RWMutex
121 // This protects concurrent flow_id allocate/delete calls. We do not need this on a
122 // per PON port basis as flow IDs are unique across the OLT.
123 FlowIDMgmtLock sync.RWMutex
124
125 // This protects concurrent access to flowids_per_gem info stored on KV store
126 flowIDToGemInfoLock sync.RWMutex
Abhilash S.L7f17e402019-03-15 17:40:41 +0530127}
128
Neha Sharmacc656962020-04-14 14:26:11 +0000129func newKVClient(storeType string, address string, timeout time.Duration) (kvstore.Client, error) {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000130 logger.Infow("kv-store-type", log.Fields{"store": storeType})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400131 switch storeType {
132 case "consul":
Neha Sharmacc656962020-04-14 14:26:11 +0000133 return kvstore.NewConsulClient(address, timeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400134 case "etcd":
Neha Sharmacc656962020-04-14 14:26:11 +0000135 return kvstore.NewEtcdClient(address, timeout, log.FatalLevel)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400136 }
137 return nil, errors.New("unsupported-kv-store")
Abhilash S.L7f17e402019-03-15 17:40:41 +0530138}
139
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700140// SetKVClient sets the KV client and return a kv backend
Neha Sharma3f221ae2020-04-29 19:02:12 +0000141func SetKVClient(backend string, addr string, DeviceID string) *db.Backend {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400142 // TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
143 // issue between kv store and backend , core is not calling NewBackend directly
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700144 kvClient, err := newKVClient(backend, addr, KvstoreTimeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400145 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000146 logger.Fatalw("Failed to init KV client\n", log.Fields{"err": err})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400147 return nil
148 }
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700149
sbarbaria8910ba2019-11-05 10:12:23 -0500150 kvbackend := &db.Backend{
Girish Gowdru0c588b22019-04-23 23:24:56 -0400151 Client: kvClient,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700152 StoreType: backend,
Neha Sharma3f221ae2020-04-29 19:02:12 +0000153 Address: addr,
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.
Neha Sharma3f221ae2020-04-29 19:02:12 +0000163func NewResourceMgr(ctx context.Context, deviceID string, KVStoreAddress string, kvStoreType string, deviceType string, devInfo *openolt.DeviceInfo) *OpenOltResourceMgr {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400164 var ResourceMgr OpenOltResourceMgr
Neha Sharma3f221ae2020-04-29 19:02:12 +0000165 logger.Debugf("Init new resource manager , address: %s, deviceid: %s", KVStoreAddress, deviceID)
166 ResourceMgr.Address = KVStoreAddress
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700167 ResourceMgr.DeviceType = deviceType
168 ResourceMgr.DevInfo = devInfo
Girish Gowdra38d533d2020-03-30 20:38:51 -0700169 NumPONPorts := devInfo.GetPonPorts()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530170
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700171 Backend := kvStoreType
Neha Sharma3f221ae2020-04-29 19:02:12 +0000172 ResourceMgr.KVStore = SetKVClient(Backend, ResourceMgr.Address, deviceID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400173 if ResourceMgr.KVStore == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000174 logger.Error("Failed to setup KV store")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400175 }
176 Ranges := make(map[string]*openolt.DeviceInfo_DeviceResourceRanges)
177 RsrcMgrsByTech := make(map[string]*ponrmgr.PONResourceManager)
178 ResourceMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530179
Girish Gowdra38d533d2020-03-30 20:38:51 -0700180 ResourceMgr.AllocIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
181 ResourceMgr.GemPortIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
182 ResourceMgr.OnuIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
183
Girish Gowdru0c588b22019-04-23 23:24:56 -0400184 // TODO self.args = registry('main').get_args()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530185
Girish Gowdru0c588b22019-04-23 23:24:56 -0400186 /*
187 If a legacy driver returns protobuf without any ranges,s synthesize one from
Gamze Abakafee36392019-10-03 11:17:24 +0000188 the legacy global per-device information. This, in theory, is temporary until
Girish Gowdru0c588b22019-04-23 23:24:56 -0400189 the legacy drivers are upgrade to support pool ranges.
190 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700191 if devInfo.Ranges == nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400192 var ranges openolt.DeviceInfo_DeviceResourceRanges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700193 ranges.Technology = devInfo.GetTechnology()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530194
Girish Gowdru0c588b22019-04-23 23:24:56 -0400195 var index uint32
196 for index = 0; index < NumPONPorts; index++ {
197 ranges.IntfIds = append(ranges.IntfIds, index)
198 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530199
Abhilash S.L8ee90712019-04-29 16:24:22 +0530200 var Pool openolt.DeviceInfo_DeviceResourceRanges_Pool
Girish Gowdru0c588b22019-04-23 23:24:56 -0400201 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700202 Pool.Start = devInfo.OnuIdStart
203 Pool.End = devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400204 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
cbabuabf02352019-10-15 13:14:56 +0200205 onuPool := Pool
206 ranges.Pools = append(ranges.Pools, &onuPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530207
Girish Gowdru0c588b22019-04-23 23:24:56 -0400208 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700209 Pool.Start = devInfo.AllocIdStart
210 Pool.End = devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400211 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200212 allocPool := Pool
213 ranges.Pools = append(ranges.Pools, &allocPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530214
Girish Gowdru0c588b22019-04-23 23:24:56 -0400215 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700216 Pool.Start = devInfo.GemportIdStart
217 Pool.End = devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400218 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200219 gemPool := Pool
220 ranges.Pools = append(ranges.Pools, &gemPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530221
Girish Gowdru0c588b22019-04-23 23:24:56 -0400222 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700223 Pool.Start = devInfo.FlowIdStart
224 Pool.End = devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400225 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
Abhilash S.L8ee90712019-04-29 16:24:22 +0530226 ranges.Pools = append(ranges.Pools, &Pool)
227 // Add to device info
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700228 devInfo.Ranges = append(devInfo.Ranges, &ranges)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400229 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530230
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700231 // Create a separate Resource Manager instance for each range. This assumes that
Girish Gowdru0c588b22019-04-23 23:24:56 -0400232 // each technology is represented by only a single range
233 var GlobalPONRsrcMgr *ponrmgr.PONResourceManager
234 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700235 for _, TechRange := range devInfo.Ranges {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400236 technology := TechRange.Technology
Girish Kumar2ad402b2020-03-20 19:45:12 +0000237 logger.Debugf("Device info technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400238 Ranges[technology] = TechRange
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700239 RsrcMgrsByTech[technology], err = ponrmgr.NewPONResourceManager(technology, deviceType, deviceID,
Neha Sharma3f221ae2020-04-29 19:02:12 +0000240 Backend, ResourceMgr.Address)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400241 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000242 logger.Errorf("Failed to create pon resource manager instance for technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400243 return nil
244 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700245 // resource_mgrs_by_tech[technology] = resource_mgr
Girish Gowdru0c588b22019-04-23 23:24:56 -0400246 if GlobalPONRsrcMgr == nil {
247 GlobalPONRsrcMgr = RsrcMgrsByTech[technology]
248 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700249 for _, IntfID := range TechRange.IntfIds {
250 ResourceMgr.ResourceMgrs[uint32(IntfID)] = RsrcMgrsByTech[technology]
Girish Gowdru0c588b22019-04-23 23:24:56 -0400251 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700252 // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
npujarec5762e2020-01-01 14:08:48 +0530253 InitializeDeviceResourceRangeAndPool(ctx, RsrcMgrsByTech[technology], GlobalPONRsrcMgr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700254 TechRange, devInfo)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400255 }
256 // After we have initialized resource ranges, initialize the
257 // resource pools accordingly.
258 for _, PONRMgr := range RsrcMgrsByTech {
npujarec5762e2020-01-01 14:08:48 +0530259 _ = PONRMgr.InitDeviceResourcePool(ctx)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400260 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000261 logger.Info("Initialization of resource manager success!")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400262 return &ResourceMgr
Abhilash S.L7f17e402019-03-15 17:40:41 +0530263}
264
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700265// InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply
266// device specific information. If KV doesn't exist
267// or is broader than the device, the device's information will
268// dictate the range limits
npujarec5762e2020-01-01 14:08:48 +0530269func InitializeDeviceResourceRangeAndPool(ctx context.Context, ponRMgr *ponrmgr.PONResourceManager, globalPONRMgr *ponrmgr.PONResourceManager,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700270 techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530271
Girish Gowdru0c588b22019-04-23 23:24:56 -0400272 // init the resource range pool according to the sharing type
Abhilash S.L7f17e402019-03-15 17:40:41 +0530273
Girish Kumar2ad402b2020-03-20 19:45:12 +0000274 logger.Debugf("Resource range pool init for technology %s", ponRMgr.Technology)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700275 // first load from KV profiles
npujarec5762e2020-01-01 14:08:48 +0530276 status := ponRMgr.InitResourceRangesFromKVStore(ctx)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700277 if !status {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000278 logger.Debugf("Failed to load resource ranges from KV store for tech %s", ponRMgr.Technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400279 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530280
Girish Gowdru0c588b22019-04-23 23:24:56 -0400281 /*
282 Then apply device specific information. If KV doesn't exist
Gamze Abakafee36392019-10-03 11:17:24 +0000283 or is broader than the device, the device's information will
Girish Gowdru0c588b22019-04-23 23:24:56 -0400284 dictate the range limits
285 */
Girish Kumar2ad402b2020-03-20 19:45:12 +0000286 logger.Debugw("Using device info to init pon resource ranges", log.Fields{"Tech": ponRMgr.Technology})
Abhilash S.L7f17e402019-03-15 17:40:41 +0530287
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700288 ONUIDStart := devInfo.OnuIdStart
289 ONUIDEnd := devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400290 ONUIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
291 ONUIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700292 AllocIDStart := devInfo.AllocIdStart
293 AllocIDEnd := devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400294 AllocIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
295 AllocIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700296 GEMPortIDStart := devInfo.GemportIdStart
297 GEMPortIDEnd := devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400298 GEMPortIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
299 GEMPortIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700300 FlowIDStart := devInfo.FlowIdStart
301 FlowIDEnd := devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400302 FlowIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
303 FlowIDSharedPoolID := uint32(0)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530304
Girish Gowdru0c588b22019-04-23 23:24:56 -0400305 var FirstIntfPoolID uint32
306 var SharedPoolID uint32
Abhilash S.L7f17e402019-03-15 17:40:41 +0530307
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400308 /*
309 * 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 -0700310 * if resources are shared across interfaces then SharedPoolID is given a positive number.
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400311 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700312 for _, FirstIntfPoolID = range techRange.IntfIds {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400313 // skip the intf id 0
314 if FirstIntfPoolID == 0 {
315 continue
316 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400317 break
318 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530319
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700320 for _, RangePool := range techRange.Pools {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400321 if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400322 SharedPoolID = FirstIntfPoolID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400323 } else if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_SAME_TECH {
324 SharedPoolID = FirstIntfPoolID
325 } else {
326 SharedPoolID = 0
327 }
328 if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID {
329 ONUIDStart = RangePool.Start
330 ONUIDEnd = RangePool.End
331 ONUIDShared = RangePool.Sharing
332 ONUIDSharedPoolID = SharedPoolID
333 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID {
334 AllocIDStart = RangePool.Start
335 AllocIDEnd = RangePool.End
336 AllocIDShared = RangePool.Sharing
337 AllocIDSharedPoolID = SharedPoolID
338 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID {
339 GEMPortIDStart = RangePool.Start
340 GEMPortIDEnd = RangePool.End
341 GEMPortIDShared = RangePool.Sharing
342 GEMPortIDSharedPoolID = SharedPoolID
343 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID {
344 FlowIDStart = RangePool.Start
345 FlowIDEnd = RangePool.End
346 FlowIDShared = RangePool.Sharing
347 FlowIDSharedPoolID = SharedPoolID
348 }
349 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530350
Girish Kumar2ad402b2020-03-20 19:45:12 +0000351 logger.Debugw("Device info init", log.Fields{"technology": techRange.Technology,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400352 "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd, "onu_id_shared_pool_id": ONUIDSharedPoolID,
353 "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd,
354 "alloc_id_shared_pool_id": AllocIDSharedPoolID,
355 "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd,
356 "gemport_id_shared_pool_id": GEMPortIDSharedPoolID,
357 "flow_id_start": FlowIDStart,
358 "flow_id_end_idx": FlowIDEnd,
359 "flow_id_shared_pool_id": FlowIDSharedPoolID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700360 "intf_ids": techRange.IntfIds,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400361 "uni_id_start": 0,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700362 "uni_id_end_idx": 1, /*MaxUNIIDperONU()*/
363 })
Abhilash S.L7f17e402019-03-15 17:40:41 +0530364
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700365 ponRMgr.InitDefaultPONResourceRanges(ONUIDStart, ONUIDEnd, ONUIDSharedPoolID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400366 AllocIDStart, AllocIDEnd, AllocIDSharedPoolID,
367 GEMPortIDStart, GEMPortIDEnd, GEMPortIDSharedPoolID,
368 FlowIDStart, FlowIDEnd, FlowIDSharedPoolID, 0, 1,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700369 devInfo.PonPorts, techRange.IntfIds)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530370
Girish Gowdru0c588b22019-04-23 23:24:56 -0400371 // For global sharing, make sure to refresh both local and global resource manager instances' range
Abhilash S.L7f17e402019-03-15 17:40:41 +0530372
Girish Gowdru0c588b22019-04-23 23:24:56 -0400373 if ONUIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700374 globalPONRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400375 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700376 ponRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
377 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400378 }
379 if AllocIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700380 globalPONRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400381 "", 0, nil)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530382
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700383 ponRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
384 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400385 }
386 if GEMPortIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700387 globalPONRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400388 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700389 ponRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
390 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400391 }
392 if FlowIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700393 globalPONRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400394 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700395 ponRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
396 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400397 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530398
Girish Gowdru0c588b22019-04-23 23:24:56 -0400399 // Make sure loaded range fits the platform bit encoding ranges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700400 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 +0530401}
402
Devmalya Paul495b94a2019-08-27 19:42:00 -0400403// Delete clears used resources for the particular olt device being deleted
npujarec5762e2020-01-01 14:08:48 +0530404func (RsrcMgr *OpenOltResourceMgr) Delete(ctx context.Context) error {
Devmalya Paul495b94a2019-08-27 19:42:00 -0400405 /* TODO
406 def __del__(self):
407 self.log.info("clearing-device-resource-pool")
408 for key, resource_mgr in self.resource_mgrs.iteritems():
409 resource_mgr.clear_device_resource_pool()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530410
Devmalya Paul495b94a2019-08-27 19:42:00 -0400411 def assert_pon_id_limit(self, pon_intf_id):
412 assert pon_intf_id in self.resource_mgrs
Abhilash S.L7f17e402019-03-15 17:40:41 +0530413
Devmalya Paul495b94a2019-08-27 19:42:00 -0400414 def assert_onu_id_limit(self, pon_intf_id, onu_id):
415 self.assert_pon_id_limit(pon_intf_id)
416 self.resource_mgrs[pon_intf_id].assert_resource_limits(onu_id, PONResourceManager.ONU_ID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530417
Devmalya Paul495b94a2019-08-27 19:42:00 -0400418 @property
419 def max_uni_id_per_onu(self):
420 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 +0530421
Devmalya Paul495b94a2019-08-27 19:42:00 -0400422 def assert_uni_id_limit(self, pon_intf_id, onu_id, uni_id):
423 self.assert_onu_id_limit(pon_intf_id, onu_id)
424 self.resource_mgrs[pon_intf_id].assert_resource_limits(uni_id, PONResourceManager.UNI_ID)
425 */
426 for _, rsrcMgr := range RsrcMgr.ResourceMgrs {
npujarec5762e2020-01-01 14:08:48 +0530427 if err := rsrcMgr.ClearDeviceResourcePool(ctx); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000428 logger.Debug("Failed to clear device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400429 return err
430 }
431 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000432 logger.Debug("Cleared device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400433 return nil
434}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530435
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700436// GetONUID returns the available OnuID for the given pon-port
npujarec5762e2020-01-01 14:08:48 +0530437func (RsrcMgr *OpenOltResourceMgr) GetONUID(ctx context.Context, ponIntfID uint32) (uint32, error) {
salmansiddiqui352a45c2019-08-19 10:15:36 +0000438 // Check if Pon Interface ID is present in Resource-manager-map
Girish Gowdrab77ded92020-04-08 11:45:05 -0700439 RsrcMgr.OnuIDMgmtLock[ponIntfID].Lock()
440 defer RsrcMgr.OnuIDMgmtLock[ponIntfID].Unlock()
441
salmansiddiqui352a45c2019-08-19 10:15:36 +0000442 if _, ok := RsrcMgr.ResourceMgrs[ponIntfID]; !ok {
443 err := errors.New("invalid-pon-interface-" + strconv.Itoa(int(ponIntfID)))
444 return 0, err
445 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400446 // Get ONU id for a provided pon interface ID.
npujarec5762e2020-01-01 14:08:48 +0530447 ONUID, err := RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ctx, ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400448 ponrmgr.ONU_ID, 1)
449 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000450 logger.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700451 ponIntfID, ponrmgr.ONU_ID)
cbabuabf02352019-10-15 13:14:56 +0200452 return 0, err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400453 }
454 if ONUID != nil {
npujarec5762e2020-01-01 14:08:48 +0530455 RsrcMgr.ResourceMgrs[ponIntfID].InitResourceMap(ctx, fmt.Sprintf("%d,%d", ponIntfID, ONUID[0]))
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700456 return ONUID[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400457 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530458
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700459 return 0, err // return OnuID 0 on error
Abhilash S.L7f17e402019-03-15 17:40:41 +0530460}
461
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700462// GetFlowIDInfo returns the slice of flow info of the given pon-port
463// Note: For flows which trap from the NNI and not really associated with any particular
464// 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 +0530465func (RsrcMgr *OpenOltResourceMgr) GetFlowIDInfo(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32, flowID uint32) *[]FlowInfo {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530466 var flows []FlowInfo
467
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700468 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530469 if err := RsrcMgr.ResourceMgrs[ponIntfID].GetFlowIDInfo(ctx, FlowPath, flowID, &flows); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000470 logger.Errorw("Error while getting flows from KV store", log.Fields{"flowId": flowID})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530471 return nil
472 }
473 if len(flows) == 0 {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000474 logger.Debugw("No flowInfo found in KV store", log.Fields{"flowPath": FlowPath})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530475 return nil
476 }
477 return &flows
478}
479
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700480// GetCurrentFlowIDsForOnu fetches flow ID from the resource manager
481// Note: For flows which trap from the NNI and not really associated with any particular
482// 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 +0530483func (RsrcMgr *OpenOltResourceMgr) GetCurrentFlowIDsForOnu(ctx context.Context, PONIntfID uint32, ONUID int32, UNIID int32) []uint32 {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700484
Abhilash S.L8ee90712019-04-29 16:24:22 +0530485 FlowPath := fmt.Sprintf("%d,%d,%d", PONIntfID, ONUID, UNIID)
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700486 if mgrs, exist := RsrcMgr.ResourceMgrs[PONIntfID]; exist {
npujarec5762e2020-01-01 14:08:48 +0530487 return mgrs.GetCurrentFlowIDsForOnu(ctx, FlowPath)
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700488 }
489 return nil
Abhilash S.L8ee90712019-04-29 16:24:22 +0530490}
491
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700492// UpdateFlowIDInfo updates flow info for the given pon interface, onu id, and uni id
493// Note: For flows which trap from the NNI and not really associated with any particular
494// 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 +0530495func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDInfo(ctx context.Context, ponIntfID int32, onuID int32, uniID int32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700496 flowID uint32, flowData *[]FlowInfo) error {
497 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530498 return RsrcMgr.ResourceMgrs[uint32(ponIntfID)].UpdateFlowIDInfoForOnu(ctx, FlowPath, flowID, *flowData)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530499}
500
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700501// GetFlowID return flow ID for a given pon interface id, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530502func (RsrcMgr *OpenOltResourceMgr) GetFlowID(ctx context.Context, ponIntfID uint32, ONUID int32, uniID int32,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400503 gemportID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700504 flowStoreCookie uint64,
Gamze Abaka724d0852020-03-18 12:10:24 +0000505 flowCategory string, vlanVid uint32, vlanPcp ...uint32) (uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530506
Girish Gowdru0c588b22019-04-23 23:24:56 -0400507 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700508 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, ONUID, uniID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700509
510 RsrcMgr.FlowIDMgmtLock.Lock()
511 defer RsrcMgr.FlowIDMgmtLock.Unlock()
512
npujarec5762e2020-01-01 14:08:48 +0530513 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(ctx, FlowPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400514 if FlowIDs != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000515 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 -0700516 for _, flowID := range FlowIDs {
npujarec5762e2020-01-01 14:08:48 +0530517 FlowInfo := RsrcMgr.GetFlowIDInfo(ctx, ponIntfID, int32(ONUID), int32(uniID), uint32(flowID))
Gamze Abaka724d0852020-03-18 12:10:24 +0000518 er := getFlowIDFromFlowInfo(FlowInfo, flowID, gemportID, flowStoreCookie, flowCategory, vlanVid, vlanPcp...)
salmansiddiqui7ac62132019-08-22 03:58:50 +0000519 if er == nil {
Gamze Abaka724d0852020-03-18 12:10:24 +0000520 log.Debugw("Found flowid for the vlan, pcp, and gem",
521 log.Fields{"flowID": flowID, "vlanVid": vlanVid, "vlanPcp": vlanPcp, "gemPortID": gemportID})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000522 return flowID, er
Abhilash S.L8ee90712019-04-29 16:24:22 +0530523 }
524 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400525 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000526 logger.Debug("No matching flows with flow cookie or flow category, allocating new flowid")
npujarec5762e2020-01-01 14:08:48 +0530527 FlowIDs, err = RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ctx, ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400528 ponrmgr.FLOW_ID, 1)
529 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000530 logger.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700531 ponIntfID, ponrmgr.FLOW_ID)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400532 return uint32(0), err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400533 }
534 if FlowIDs != nil {
npujarec5762e2020-01-01 14:08:48 +0530535 _ = RsrcMgr.ResourceMgrs[ponIntfID].UpdateFlowIDForOnu(ctx, FlowPath, FlowIDs[0], true)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700536 return FlowIDs[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400537 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530538
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700539 return 0, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530540}
541
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700542// GetAllocID return the first Alloc ID for a given pon interface id and onu id and then update the resource map on
543// the KV store with the list of alloc_ids allocated for the pon_intf_onu_id tuple
544// 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 +0530545func (RsrcMgr *OpenOltResourceMgr) GetAllocID(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530546
Girish Gowdru0c588b22019-04-23 23:24:56 -0400547 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700548 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700549
550 RsrcMgr.AllocIDMgmtLock[intfID].Lock()
551 defer RsrcMgr.AllocIDMgmtLock[intfID].Unlock()
552
npujarec5762e2020-01-01 14:08:48 +0530553 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400554 if AllocID != nil {
555 // Since we support only one alloc_id for the ONU at the moment,
556 // return the first alloc_id in the list, if available, for that
557 // ONU.
Girish Kumar2ad402b2020-03-20 19:45:12 +0000558 logger.Debugw("Retrieved alloc ID from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400559 return AllocID[0]
560 }
npujarec5762e2020-01-01 14:08:48 +0530561 AllocID, err = RsrcMgr.ResourceMgrs[intfID].GetResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400562 ponrmgr.ALLOC_ID, 1)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530563
Girish Gowdru0c588b22019-04-23 23:24:56 -0400564 if AllocID == nil || err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000565 logger.Error("Failed to allocate alloc id")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400566 return 0
567 }
568 // update the resource map on KV store with the list of alloc_id
569 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530570 err = RsrcMgr.ResourceMgrs[intfID].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID, AllocID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400571 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000572 logger.Error("Failed to update Alloc ID")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400573 return 0
574 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000575 logger.Debugw("Allocated new Tcont from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400576 return AllocID[0]
Abhilash S.L7f17e402019-03-15 17:40:41 +0530577}
578
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700579// UpdateAllocIdsForOnu updates alloc ids in kv store for a given pon interface id, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530580func (RsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ctx context.Context, ponPort uint32, onuID uint32, uniID uint32, allocID []uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530581
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700582 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530583 return RsrcMgr.ResourceMgrs[ponPort].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700584 allocID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530585}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700586
587// GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530588func (RsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(ctx context.Context, intfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700589 uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530590
Girish Gowdru0c588b22019-04-23 23:24:56 -0400591 /* Get gem ports for given pon interface , onu id and uni id. */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530592
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700593 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530594 return RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530595}
596
Gamze Abakafee36392019-10-03 11:17:24 +0000597// GetCurrentAllocIDsForOnu returns alloc ids for given pon interface and onu id
npujarec5762e2020-01-01 14:08:48 +0530598func (RsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDsForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530599
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700600 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530601 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400602 if AllocID != nil {
Gamze Abakafee36392019-10-03 11:17:24 +0000603 return AllocID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400604 }
Gamze Abakafee36392019-10-03 11:17:24 +0000605 return []uint32{}
606}
607
608// RemoveAllocIDForOnu removes the alloc id for given pon interface, onu id, uni id and alloc id
npujarec5762e2020-01-01 14:08:48 +0530609func (RsrcMgr *OpenOltResourceMgr) RemoveAllocIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, allocID uint32) {
610 allocIDs := RsrcMgr.GetCurrentAllocIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000611 for i := 0; i < len(allocIDs); i++ {
612 if allocIDs[i] == allocID {
613 allocIDs = append(allocIDs[:i], allocIDs[i+1:]...)
614 break
615 }
616 }
npujarec5762e2020-01-01 14:08:48 +0530617 err := RsrcMgr.UpdateAllocIdsForOnu(ctx, intfID, onuID, uniID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000618 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000619 logger.Errorf("Failed to Remove Alloc Id For Onu. IntfID %d onuID %d uniID %d allocID %d",
Gamze Abakafee36392019-10-03 11:17:24 +0000620 intfID, onuID, uniID, allocID)
621 }
622}
623
624// RemoveGemPortIDForOnu removes the gem port id for given pon interface, onu id, uni id and gem port id
npujarec5762e2020-01-01 14:08:48 +0530625func (RsrcMgr *OpenOltResourceMgr) RemoveGemPortIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, gemPortID uint32) {
626 gemPortIDs := RsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000627 for i := 0; i < len(gemPortIDs); i++ {
628 if gemPortIDs[i] == gemPortID {
629 gemPortIDs = append(gemPortIDs[:i], gemPortIDs[i+1:]...)
630 break
631 }
632 }
npujarec5762e2020-01-01 14:08:48 +0530633 err := RsrcMgr.UpdateGEMPortIDsForOnu(ctx, intfID, onuID, uniID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000634 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000635 logger.Errorf("Failed to Remove Gem Id For Onu. IntfID %d onuID %d uniID %d gemPortId %d",
Gamze Abakafee36392019-10-03 11:17:24 +0000636 intfID, onuID, uniID, gemPortID)
637 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530638}
639
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700640// UpdateGEMportsPonportToOnuMapOnKVStore updates onu and uni id associated with the gem port to the kv store
641// This stored information is used when packet_indication is received and we need to derive the ONU Id for which
642// the packet arrived based on the pon_intf and gemport available in the packet_indication
npujarec5762e2020-01-01 14:08:48 +0530643func (RsrcMgr *OpenOltResourceMgr) UpdateGEMportsPonportToOnuMapOnKVStore(ctx context.Context, gemPorts []uint32, PonPort uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700644 onuID uint32, uniID uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530645
Girish Gowdru0c588b22019-04-23 23:24:56 -0400646 /* Update onu and uni id associated with the gem port to the kv store. */
647 var IntfGEMPortPath string
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700648 Data := fmt.Sprintf("%d %d", onuID, uniID)
649 for _, GEM := range gemPorts {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400650 IntfGEMPortPath = fmt.Sprintf("%d,%d", PonPort, GEM)
651 Val, err := json.Marshal(Data)
652 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000653 logger.Error("failed to Marshal")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400654 return err
655 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700656
npujarec5762e2020-01-01 14:08:48 +0530657 if err = RsrcMgr.KVStore.Put(ctx, IntfGEMPortPath, Val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000658 logger.Errorf("Failed to update resource %s", IntfGEMPortPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400659 return err
660 }
661 }
662 return nil
Abhilash S.L7f17e402019-03-15 17:40:41 +0530663}
664
Gamze Abakafee36392019-10-03 11:17:24 +0000665// RemoveGEMportPonportToOnuMapOnKVStore removes the relationship between the gem port and pon port
npujarec5762e2020-01-01 14:08:48 +0530666func (RsrcMgr *OpenOltResourceMgr) RemoveGEMportPonportToOnuMapOnKVStore(ctx context.Context, GemPort uint32, PonPort uint32) {
Gamze Abakafee36392019-10-03 11:17:24 +0000667 IntfGEMPortPath := fmt.Sprintf("%d,%d", PonPort, GemPort)
npujarec5762e2020-01-01 14:08:48 +0530668 err := RsrcMgr.KVStore.Delete(ctx, IntfGEMPortPath)
Gamze Abakafee36392019-10-03 11:17:24 +0000669 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000670 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 +0000671 }
672}
673
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700674// GetGEMPortID gets gem port id for a particular pon port, onu id and uni id and then update the resource map on
675// the KV store with the list of gemport_id allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530676func (RsrcMgr *OpenOltResourceMgr) GetGEMPortID(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700677 uniID uint32, NumOfPorts uint32) ([]uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530678
Girish Gowdru0c588b22019-04-23 23:24:56 -0400679 /* Get gem port id for a particular pon port, onu id
680 and uni id.
681 */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530682
Girish Gowdru0c588b22019-04-23 23:24:56 -0400683 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700684 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530685
Girish Gowdrab77ded92020-04-08 11:45:05 -0700686 RsrcMgr.GemPortIDMgmtLock[ponPort].Lock()
687 defer RsrcMgr.GemPortIDMgmtLock[ponPort].Unlock()
688
npujarec5762e2020-01-01 14:08:48 +0530689 GEMPortList := RsrcMgr.ResourceMgrs[ponPort].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400690 if GEMPortList != nil {
691 return GEMPortList, nil
692 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530693
npujarec5762e2020-01-01 14:08:48 +0530694 GEMPortList, err = RsrcMgr.ResourceMgrs[ponPort].GetResourceID(ctx, ponPort,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400695 ponrmgr.GEMPORT_ID, NumOfPorts)
696 if err != nil && GEMPortList == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000697 logger.Errorf("Failed to get gem port id for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400698 return nil, err
699 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530700
Girish Gowdru0c588b22019-04-23 23:24:56 -0400701 // update the resource map on KV store with the list of gemport_id
702 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530703 err = RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400704 GEMPortList)
705 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000706 logger.Errorf("Failed to update GEM ports to kv store for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400707 return nil, err
708 }
npujarec5762e2020-01-01 14:08:48 +0530709 _ = RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(ctx, GEMPortList, ponPort,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700710 onuID, uniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400711 return GEMPortList, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530712}
713
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700714// 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 +0530715func (RsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700716 uniID uint32, GEMPortList []uint32) error {
717 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530718 return RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400719 GEMPortList)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530720
721}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700722
723// FreeonuID releases(make free) onu id for a particular pon-port
npujarec5762e2020-01-01 14:08:48 +0530724func (RsrcMgr *OpenOltResourceMgr) FreeonuID(ctx context.Context, intfID uint32, onuID []uint32) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700725
Girish Gowdra38d533d2020-03-30 20:38:51 -0700726 RsrcMgr.OnuIDMgmtLock[intfID].Lock()
Girish Gowdrab77ded92020-04-08 11:45:05 -0700727 defer RsrcMgr.OnuIDMgmtLock[intfID].Unlock()
728
npujarec5762e2020-01-01 14:08:48 +0530729 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID, ponrmgr.ONU_ID, onuID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530730
Girish Gowdru0c588b22019-04-23 23:24:56 -0400731 /* Free onu id for a particular interface.*/
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700732 var IntfonuID string
733 for _, onu := range onuID {
734 IntfonuID = fmt.Sprintf("%d,%d", intfID, onu)
npujarec5762e2020-01-01 14:08:48 +0530735 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfonuID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400736 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530737}
738
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700739// FreeFlowID returns the free flow id for a given interface, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530740func (RsrcMgr *OpenOltResourceMgr) FreeFlowID(ctx context.Context, IntfID uint32, onuID int32,
Devmalya Paul495b94a2019-08-27 19:42:00 -0400741 uniID int32, FlowID uint32) {
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400742 var IntfONUID string
743 var err error
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530744
Girish Gowdrab77ded92020-04-08 11:45:05 -0700745 RsrcMgr.FlowIDMgmtLock.Lock()
746 defer RsrcMgr.FlowIDMgmtLock.Unlock()
747
748 FlowIds := make([]uint32, 0)
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530749 FlowIds = append(FlowIds, FlowID)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700750 IntfONUID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530751 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(ctx, IntfONUID, FlowID, false)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400752 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000753 logger.Errorw("Failed to Update flow id for", log.Fields{"intf": IntfONUID})
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400754 }
npujarec5762e2020-01-01 14:08:48 +0530755 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(ctx, IntfONUID, FlowID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700756
npujarec5762e2020-01-01 14:08:48 +0530757 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.FLOW_ID, FlowIds)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400758}
759
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700760// FreeFlowIDs releases the flow Ids
npujarec5762e2020-01-01 14:08:48 +0530761func (RsrcMgr *OpenOltResourceMgr) FreeFlowIDs(ctx context.Context, IntfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700762 uniID uint32, FlowID []uint32) {
Girish Gowdra38d533d2020-03-30 20:38:51 -0700763 RsrcMgr.FlowIDMgmtLock.Lock()
Girish Gowdrab77ded92020-04-08 11:45:05 -0700764 defer RsrcMgr.FlowIDMgmtLock.Unlock()
765
npujarec5762e2020-01-01 14:08:48 +0530766 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.FLOW_ID, FlowID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530767
Abhilash S.L8ee90712019-04-29 16:24:22 +0530768 var IntfOnuIDUniID string
Girish Gowdru0c588b22019-04-23 23:24:56 -0400769 var err error
770 for _, flow := range FlowID {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700771 IntfOnuIDUniID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530772 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(ctx, IntfOnuIDUniID, flow, false)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400773 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000774 logger.Errorw("Failed to Update flow id for", log.Fields{"intf": IntfOnuIDUniID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400775 }
npujarec5762e2020-01-01 14:08:48 +0530776 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(ctx, IntfOnuIDUniID, flow)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400777 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530778}
779
Gamze Abakafee36392019-10-03 11:17:24 +0000780// FreeAllocID frees AllocID on the PON resource pool and also frees the allocID association
781// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530782func (RsrcMgr *OpenOltResourceMgr) FreeAllocID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000783 uniID uint32, allocID uint32) {
Girish Gowdrab77ded92020-04-08 11:45:05 -0700784 RsrcMgr.AllocIDMgmtLock[IntfID].Lock()
785 defer RsrcMgr.AllocIDMgmtLock[IntfID].Unlock()
786
npujarec5762e2020-01-01 14:08:48 +0530787 RsrcMgr.RemoveAllocIDForOnu(ctx, IntfID, onuID, uniID, allocID)
Gamze Abakafee36392019-10-03 11:17:24 +0000788 allocIDs := make([]uint32, 0)
789 allocIDs = append(allocIDs, allocID)
npujarec5762e2020-01-01 14:08:48 +0530790 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.ALLOC_ID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000791}
792
793// FreeGemPortID frees GemPortID on the PON resource pool and also frees the gemPortID association
794// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530795func (RsrcMgr *OpenOltResourceMgr) FreeGemPortID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000796 uniID uint32, gemPortID uint32) {
Girish Gowdrab77ded92020-04-08 11:45:05 -0700797 RsrcMgr.GemPortIDMgmtLock[IntfID].Lock()
798 defer RsrcMgr.GemPortIDMgmtLock[IntfID].Unlock()
799
npujarec5762e2020-01-01 14:08:48 +0530800 RsrcMgr.RemoveGemPortIDForOnu(ctx, IntfID, onuID, uniID, gemPortID)
Gamze Abakafee36392019-10-03 11:17:24 +0000801 gemPortIDs := make([]uint32, 0)
802 gemPortIDs = append(gemPortIDs, gemPortID)
npujarec5762e2020-01-01 14:08:48 +0530803 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.GEMPORT_ID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000804}
805
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700806// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id, and the clears the
807// resource map and the onuID associated with (pon_intf_id, gemport_id) tuple,
npujarec5762e2020-01-01 14:08:48 +0530808func (RsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530809
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700810 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530811
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700812 RsrcMgr.AllocIDMgmtLock[intfID].Lock()
Girish Gowdrab77ded92020-04-08 11:45:05 -0700813 AllocIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700814
npujarec5762e2020-01-01 14:08:48 +0530815 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400816 ponrmgr.ALLOC_ID,
817 AllocIDs)
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700818 RsrcMgr.AllocIDMgmtLock[intfID].Unlock()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530819
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700820 RsrcMgr.GemPortIDMgmtLock[intfID].Lock()
npujarec5762e2020-01-01 14:08:48 +0530821 GEMPortIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
822 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400823 ponrmgr.GEMPORT_ID,
824 GEMPortIDs)
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700825 RsrcMgr.GemPortIDMgmtLock[intfID].Unlock()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530826
Girish Gowdra38d533d2020-03-30 20:38:51 -0700827 RsrcMgr.FlowIDMgmtLock.Lock()
npujarec5762e2020-01-01 14:08:48 +0530828 FlowIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentFlowIDsForOnu(ctx, IntfOnuIDUniID)
829 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400830 ponrmgr.FLOW_ID,
831 FlowIDs)
Girish Gowdra38d533d2020-03-30 20:38:51 -0700832 RsrcMgr.FlowIDMgmtLock.Unlock()
833
Girish Gowdru0c588b22019-04-23 23:24:56 -0400834 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
npujarec5762e2020-01-01 14:08:48 +0530835 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400836 // Clear the ONU Id associated with the (pon_intf_id, gemport_id) tuple.
837 for _, GEM := range GEMPortIDs {
npujarec5762e2020-01-01 14:08:48 +0530838 _ = RsrcMgr.KVStore.Delete(ctx, fmt.Sprintf("%d,%d", intfID, GEM))
Girish Gowdru0c588b22019-04-23 23:24:56 -0400839 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530840}
841
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700842// IsFlowCookieOnKVStore checks if the given flow cookie is present on the kv store
843// Returns true if the flow cookie is found, otherwise it returns false
npujarec5762e2020-01-01 14:08:48 +0530844func (RsrcMgr *OpenOltResourceMgr) IsFlowCookieOnKVStore(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700845 flowStoreCookie uint64) bool {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530846
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700847 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530848 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(ctx, FlowPath)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400849 if FlowIDs != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000850 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 -0700851 for _, flowID := range FlowIDs {
npujarec5762e2020-01-01 14:08:48 +0530852 FlowInfo := RsrcMgr.GetFlowIDInfo(ctx, ponIntfID, int32(onuID), int32(uniID), uint32(flowID))
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400853 if FlowInfo != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000854 logger.Debugw("Found flows", log.Fields{"flows": *FlowInfo, "flowId": flowID})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400855 for _, Info := range *FlowInfo {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700856 if Info.FlowStoreCookie == flowStoreCookie {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000857 logger.Debug("Found flow matching with flowStore cookie", log.Fields{"flowId": flowID, "flowStoreCookie": flowStoreCookie})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400858 return true
859 }
860 }
861 }
862 }
863 }
864 return false
865}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400866
salmansiddiqui7ac62132019-08-22 03:58:50 +0000867// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000868// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530869func (RsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) []uint32 {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000870 Path := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000871 var Data []uint32
npujarec5762e2020-01-01 14:08:48 +0530872 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400873 if err == nil {
874 if Value != nil {
875 Val, err := kvstore.ToByte(Value.Value)
876 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000877 logger.Errorw("Failed to convert into byte array", log.Fields{"error": err})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400878 return Data
879 }
880 if err = json.Unmarshal(Val, &Data); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000881 logger.Error("Failed to unmarshal", log.Fields{"error": err})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400882 return Data
883 }
884 }
885 } else {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000886 logger.Errorf("Failed to get TP id from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400887 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000888 logger.Debugf("Getting TP id %d from path %s", Data, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400889 return Data
890
891}
892
Gamze Abakafee36392019-10-03 11:17:24 +0000893// RemoveTechProfileIDsForOnu deletes all tech profile ids from the KV-Store for the given onu based on the path
894// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530895func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDsForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) error {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000896 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
npujarec5762e2020-01-01 14:08:48 +0530897 if err := RsrcMgr.KVStore.Delete(ctx, IntfOnuUniID); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000898 logger.Errorw("Failed to delete techprofile id resource in KV store", log.Fields{"path": IntfOnuUniID})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400899 return err
900 }
901 return nil
902}
903
Gamze Abakafee36392019-10-03 11:17:24 +0000904// RemoveTechProfileIDForOnu deletes a specific tech profile id from the KV-Store for the given onu based on the path
905// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530906func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32, TpID uint32) error {
907 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000908 for i, tpIDInList := range tpIDList {
909 if tpIDInList == TpID {
910 tpIDList = append(tpIDList[:i], tpIDList[i+1:]...)
911 }
912 }
913 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
914 Value, err := json.Marshal(tpIDList)
915 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000916 logger.Error("failed to Marshal")
Gamze Abakafee36392019-10-03 11:17:24 +0000917 return err
918 }
npujarec5762e2020-01-01 14:08:48 +0530919 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000920 logger.Errorf("Failed to update resource %s", IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000921 return err
922 }
923 return err
924}
925
926// UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
927// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530928func (RsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32,
salmansiddiqui7ac62132019-08-22 03:58:50 +0000929 UniID uint32, TpID uint32) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400930 var Value []byte
931 var err error
932
salmansiddiqui7ac62132019-08-22 03:58:50 +0000933 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000934
npujarec5762e2020-01-01 14:08:48 +0530935 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000936 for _, value := range tpIDList {
937 if value == TpID {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000938 logger.Debugf("TpID %d is already in tpIdList for the path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000939 return err
940 }
941 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000942 logger.Debugf("updating tp id %d on path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000943 tpIDList = append(tpIDList, TpID)
944 Value, err = json.Marshal(tpIDList)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400945 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000946 logger.Error("failed to Marshal")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400947 return err
948 }
npujarec5762e2020-01-01 14:08:48 +0530949 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000950 logger.Errorf("Failed to update resource %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400951 return err
952 }
953 return err
954}
955
salmansiddiqui7ac62132019-08-22 03:58:50 +0000956// UpdateMeterIDForOnu updates the meter id in the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000957// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +0530958func (RsrcMgr *OpenOltResourceMgr) UpdateMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000959 UniID uint32, TpID uint32, MeterConfig *ofp.OfpMeterConfig) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400960 var Value []byte
961 var err error
962
Gamze Abakafee36392019-10-03 11:17:24 +0000963 IntfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400964 Value, err = json.Marshal(*MeterConfig)
965 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000966 logger.Error("failed to Marshal meter config")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400967 return err
968 }
npujarec5762e2020-01-01 14:08:48 +0530969 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000970 logger.Errorf("Failed to store meter into KV store %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400971 return err
972 }
973 return err
974}
975
Gamze Abakafee36392019-10-03 11:17:24 +0000976// GetMeterIDForOnu fetches the meter id from the kv store for the given onu based on the path
977// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +0530978func (RsrcMgr *OpenOltResourceMgr) GetMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000979 UniID uint32, TpID uint32) (*ofp.OfpMeterConfig, error) {
980 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400981 var meterConfig ofp.OfpMeterConfig
npujarec5762e2020-01-01 14:08:48 +0530982 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400983 if err == nil {
984 if Value != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000985 logger.Debug("Found meter in KV store", log.Fields{"Direction": Direction})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000986 Val, er := kvstore.ToByte(Value.Value)
987 if er != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000988 logger.Errorw("Failed to convert into byte array", log.Fields{"error": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000989 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400990 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000991 if er = json.Unmarshal(Val, &meterConfig); er != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000992 logger.Error("Failed to unmarshal meterconfig", log.Fields{"error": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000993 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400994 }
995 } else {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000996 logger.Debug("meter-does-not-exists-in-KVStore")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400997 return nil, err
998 }
999 } else {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001000 logger.Errorf("Failed to get Meter config from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001001
1002 }
1003 return &meterConfig, err
1004}
1005
Gamze Abakafee36392019-10-03 11:17:24 +00001006// RemoveMeterIDForOnu deletes the meter id from the kV-Store for the given onu based on the path
1007// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +05301008func (RsrcMgr *OpenOltResourceMgr) RemoveMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +00001009 UniID uint32, TpID uint32) error {
1010 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
npujarec5762e2020-01-01 14:08:48 +05301011 if err := RsrcMgr.KVStore.Delete(ctx, Path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001012 logger.Errorf("Failed to delete meter id %s from kvstore ", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001013 return err
1014 }
1015 return nil
1016}
salmansiddiqui7ac62132019-08-22 03:58:50 +00001017
Gamze Abaka724d0852020-03-18 12:10:24 +00001018func getFlowIDFromFlowInfo(FlowInfo *[]FlowInfo, flowID, gemportID uint32, flowStoreCookie uint64, flowCategory string,
1019 vlanVid uint32, vlanPcp ...uint32) error {
salmansiddiqui7ac62132019-08-22 03:58:50 +00001020 if FlowInfo != nil {
1021 for _, Info := range *FlowInfo {
1022 if int32(gemportID) == Info.Flow.GemportId && flowCategory != "" && Info.FlowCategory == flowCategory {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001023 logger.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
Gamze Abaka724d0852020-03-18 12:10:24 +00001024 if Info.FlowCategory == "HSIA_FLOW" {
1025 if err := checkVlanAndPbitEqualityForFlows(vlanVid, Info, vlanPcp[0]); err == nil {
1026 return nil
1027 }
salmansiddiqui7ac62132019-08-22 03:58:50 +00001028 }
1029 }
1030 if int32(gemportID) == Info.Flow.GemportId && flowStoreCookie != 0 && Info.FlowStoreCookie == flowStoreCookie {
1031 if flowCategory != "" && Info.FlowCategory == flowCategory {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001032 logger.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001033 return nil
1034 }
1035 }
1036 }
1037 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001038 logger.Debugw("the flow can be related to a different service", log.Fields{"flow_info": FlowInfo})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001039 return errors.New("invalid flow-info")
1040}
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301041
Gamze Abaka724d0852020-03-18 12:10:24 +00001042func checkVlanAndPbitEqualityForFlows(vlanVid uint32, Info FlowInfo, vlanPcp uint32) error {
1043 if err := checkVlanEqualityForFlows(vlanVid, Info); err != nil {
1044 return err
1045 }
1046
1047 //flow has remark action and pbits
1048 if Info.Flow.Action.Cmd.RemarkInnerPbits || Info.Flow.Action.Cmd.RemarkOuterPbits {
1049 if vlanPcp == Info.Flow.Action.OPbits || vlanPcp == Info.Flow.Action.IPbits {
1050 return nil
1051 }
1052 } else if vlanPcp == Info.Flow.Classifier.OPbits {
1053 //no remark action but flow has pbits
1054 return nil
1055 } else if vlanPcp == 0xff || Info.Flow.Classifier.OPbits == 0xff {
1056 // no pbit found
1057 return nil
1058 }
1059 return errors.New("not found in terms of pbit equality")
1060}
1061
1062func checkVlanEqualityForFlows(vlanVid uint32, Info FlowInfo) error {
1063 if vlanVid == Info.Flow.Action.OVid || vlanVid == Info.Flow.Classifier.IVid {
1064 return nil
1065 }
1066 return errors.New("not found in terms of vlan_id equality")
1067}
1068
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301069//AddGemToOnuGemInfo adds gemport to onugem info kvstore
npujarec5762e2020-01-01 14:08:48 +05301070func (RsrcMgr *OpenOltResourceMgr) AddGemToOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, gemPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301071 var onuGemData []OnuGemInfo
1072 var err error
1073
npujarec5762e2020-01-01 14:08:48 +05301074 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001075 logger.Errorf("failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301076 return err
1077 }
1078 if len(onuGemData) == 0 {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001079 logger.Errorw("failed to ger Onuid info ", log.Fields{"intfid": intfID, "onuid": onuID})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301080 return err
1081 }
1082
1083 for idx, onugem := range onuGemData {
1084 if onugem.OnuID == onuID {
1085 for _, gem := range onuGemData[idx].GemPorts {
1086 if gem == gemPort {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001087 logger.Debugw("Gem already present in onugem info, skpping addition", log.Fields{"gem": gem})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301088 return nil
1089 }
1090 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001091 logger.Debugw("Added gem to onugem info", log.Fields{"gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301092 onuGemData[idx].GemPorts = append(onuGemData[idx].GemPorts, gemPort)
1093 break
1094 }
1095 }
npujarec5762e2020-01-01 14:08:48 +05301096 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301097 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001098 logger.Error("Failed to add onugem to kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301099 return err
1100 }
1101 return err
1102}
1103
1104//GetOnuGemInfo gets onu gem info from the kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301105func (RsrcMgr *OpenOltResourceMgr) GetOnuGemInfo(ctx context.Context, IntfID uint32) ([]OnuGemInfo, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301106 var onuGemData []OnuGemInfo
1107
npujarec5762e2020-01-01 14:08:48 +05301108 if err := RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001109 logger.Errorf("failed to get onuifo for intfid %d", IntfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301110 return nil, err
1111 }
1112
1113 return onuGemData, nil
1114}
1115
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001116// AddOnuGemInfo adds onu info on to the kvstore per interface
1117func (RsrcMgr *OpenOltResourceMgr) AddOnuGemInfo(ctx context.Context, IntfID uint32, onuGem OnuGemInfo) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301118 var onuGemData []OnuGemInfo
1119 var err error
1120
npujarec5762e2020-01-01 14:08:48 +05301121 if err = RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001122 logger.Errorf("failed to get onuifo for intfid %d", IntfID)
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001123 return olterrors.NewErrPersistence("get", "OnuGemInfo", IntfID,
1124 log.Fields{"onuGem": onuGem, "intfID": IntfID}, err)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301125 }
1126 onuGemData = append(onuGemData, onuGem)
npujarec5762e2020-01-01 14:08:48 +05301127 err = RsrcMgr.ResourceMgrs[IntfID].AddOnuGemInfo(ctx, IntfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301128 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001129 logger.Error("Failed to add onugem to kv store")
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001130 return olterrors.NewErrPersistence("set", "OnuGemInfo", IntfID,
1131 log.Fields{"onuGemData": onuGemData, "intfID": IntfID}, err)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301132 }
1133
Girish Kumar2ad402b2020-03-20 19:45:12 +00001134 logger.Debugw("added onu to onugeminfo", log.Fields{"intf": IntfID, "onugem": onuGem})
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001135 return nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301136}
1137
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301138// 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 +05301139func (RsrcMgr *OpenOltResourceMgr) AddUniPortToOnuInfo(ctx context.Context, intfID uint32, onuID uint32, portNo uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301140 var onuGemData []OnuGemInfo
1141 var err error
1142
npujarec5762e2020-01-01 14:08:48 +05301143 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001144 logger.Errorf("failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301145 return
1146 }
1147 for idx, onu := range onuGemData {
1148 if onu.OnuID == onuID {
1149 for _, uni := range onu.UniPorts {
1150 if uni == portNo {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001151 logger.Debugw("uni already present in onugem info", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301152 return
1153 }
1154 }
1155 onuGemData[idx].UniPorts = append(onuGemData[idx].UniPorts, portNo)
1156 break
1157 }
1158 }
npujarec5762e2020-01-01 14:08:48 +05301159 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301160 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001161 logger.Errorw("Failed to add uin port in onugem to kv store", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301162 return
1163 }
1164 return
1165}
1166
Esin Karaman04166c82020-07-16 14:23:33 +00001167//UpdateGemPortForPktIn updates gemport for pkt in path to kvstore, path being intfid, onuid, portno, vlan id, priority bit
npujarec5762e2020-01-01 14:08:48 +05301168func (RsrcMgr *OpenOltResourceMgr) UpdateGemPortForPktIn(ctx context.Context, pktIn PacketInInfoKey, gemPort uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301169
Esin Karaman04166c82020-07-16 14:23:33 +00001170 path := fmt.Sprintf(OnuPacketINPath, pktIn.IntfID, pktIn.OnuID, pktIn.LogicalPort, pktIn.VlanID, pktIn.Priority)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301171 Value, err := json.Marshal(gemPort)
1172 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001173 logger.Error("Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301174 return
1175 }
npujarec5762e2020-01-01 14:08:48 +05301176 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001177 logger.Errorw("Failed to put to kvstore", log.Fields{"path": path, "value": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301178 return
1179 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001180 logger.Debugw("added gem packet in successfully", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301181
1182 return
1183}
1184
Esin Karaman04166c82020-07-16 14:23:33 +00001185// GetGemPortFromOnuPktIn gets the gem port from onu pkt in path, path being intfid, onuid, portno, vlan id, priority bit
1186func (RsrcMgr *OpenOltResourceMgr) GetGemPortFromOnuPktIn(ctx context.Context, packetInInfoKey PacketInInfoKey) (uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301187
1188 var Val []byte
1189 var gemPort uint32
1190
Esin Karaman04166c82020-07-16 14:23:33 +00001191 path := fmt.Sprintf(OnuPacketINPath, packetInInfoKey.IntfID, packetInInfoKey.OnuID, packetInInfoKey.LogicalPort,
1192 packetInInfoKey.VlanID, packetInInfoKey.Priority)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301193
npujarec5762e2020-01-01 14:08:48 +05301194 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301195 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001196 logger.Errorw("Failed to get from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301197 return uint32(0), err
1198 } else if value == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001199 logger.Debugw("No pkt in gem found", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301200 return uint32(0), nil
1201 }
1202
1203 if Val, err = kvstore.ToByte(value.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001204 logger.Error("Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301205 return uint32(0), err
1206 }
1207 if err = json.Unmarshal(Val, &gemPort); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001208 logger.Error("Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301209 return uint32(0), err
1210 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001211 logger.Debugw("found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301212
1213 return gemPort, nil
1214}
1215
Esin Karaman04166c82020-07-16 14:23:33 +00001216//DelGemPortPktInOfAllServices deletes the gemports from pkt in path for all services
1217func (RsrcMgr *OpenOltResourceMgr) DelGemPortPktInOfAllServices(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301218
Esin Karaman04166c82020-07-16 14:23:33 +00001219 //retrieve stored gem port from the store first.
1220 Path := fmt.Sprintf(OnuPacketINPathPrefix, intfID, onuID, logicalPort)
1221 logger.Debugf("getting flows from the path:%s", Path)
1222 Value, err := RsrcMgr.KVStore.List(ctx, Path)
1223 if err != nil {
1224 logger.Errorf("failed to get flows from kvstore for path %s", Path)
1225 return errors.New("failed to get flows from kvstore for path " + Path)
1226 }
1227 logger.Debugf("%d flows retrieved from the path:%s", len(Value), Path)
1228
1229 //remove them one by one
1230 for key := range Value {
1231 if err := RsrcMgr.KVStore.Delete(ctx, key); err != nil {
1232 logger.Errorf("Failed to remove resource %s", key)
1233 return err
1234 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301235 }
1236 return nil
1237}
1238
1239// DelOnuGemInfoForIntf deletes the onugem info from kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301240func (RsrcMgr *OpenOltResourceMgr) DelOnuGemInfoForIntf(ctx context.Context, intfID uint32) error {
1241 if err := RsrcMgr.ResourceMgrs[intfID].DelOnuGemInfoForIntf(ctx, intfID); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001242 logger.Errorw("failed to delete onu gem info for", log.Fields{"intfid": intfID})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301243 return err
1244 }
1245 return nil
1246}
1247
1248//GetNNIFromKVStore gets NNi intfids from kvstore. path being per device
npujarec5762e2020-01-01 14:08:48 +05301249func (RsrcMgr *OpenOltResourceMgr) GetNNIFromKVStore(ctx context.Context) ([]uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301250
1251 var nni []uint32
1252 var Val []byte
1253
1254 path := fmt.Sprintf(NnniIntfID)
npujarec5762e2020-01-01 14:08:48 +05301255 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301256 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001257 logger.Error("failed to get data from kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301258 return nil, err
1259 }
1260 if value != nil {
1261 if Val, err = kvstore.ToByte(value.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001262 logger.Error("Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301263 return nil, err
1264 }
1265 if err = json.Unmarshal(Val, &nni); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001266 logger.Error("Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301267 return nil, err
1268 }
1269 }
1270 return nni, err
1271}
1272
1273// AddNNIToKVStore adds Nni interfaces to kvstore, path being per device.
npujarec5762e2020-01-01 14:08:48 +05301274func (RsrcMgr *OpenOltResourceMgr) AddNNIToKVStore(ctx context.Context, nniIntf uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301275 var Value []byte
1276
npujarec5762e2020-01-01 14:08:48 +05301277 nni, err := RsrcMgr.GetNNIFromKVStore(ctx)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301278 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001279 logger.Error("failed to fetch nni interfaces from kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301280 return err
1281 }
1282
1283 path := fmt.Sprintf(NnniIntfID)
1284 nni = append(nni, nniIntf)
1285 Value, err = json.Marshal(nni)
1286 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001287 logger.Error("Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301288 }
npujarec5762e2020-01-01 14:08:48 +05301289 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001290 logger.Errorw("Failed to put to kvstore", log.Fields{"path": path, "value": Value})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301291 return err
1292 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001293 logger.Debugw("added nni to kv successfully", log.Fields{"path": path, "nni": nniIntf})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301294 return nil
1295}
1296
1297// DelNNiFromKVStore deletes nni interface list from kv store.
npujarec5762e2020-01-01 14:08:48 +05301298func (RsrcMgr *OpenOltResourceMgr) DelNNiFromKVStore(ctx context.Context) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301299
1300 path := fmt.Sprintf(NnniIntfID)
1301
npujarec5762e2020-01-01 14:08:48 +05301302 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001303 logger.Errorw("Failed to delete nni interfaces from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301304 return err
1305 }
1306 return nil
1307}
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301308
1309//UpdateFlowIDsForGem updates flow id per gemport
npujarec5762e2020-01-01 14:08:48 +05301310func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDsForGem(ctx context.Context, intf uint32, gem uint32, flowIDs []uint32) error {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301311 var val []byte
1312 path := fmt.Sprintf(FlowIDsForGem, intf)
1313
npujarec5762e2020-01-01 14:08:48 +05301314 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301315 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001316 logger.Error("Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301317 return err
1318 }
1319 if flowsForGem == nil {
1320 flowsForGem = make(map[uint32][]uint32)
1321 }
1322 flowsForGem[gem] = flowIDs
1323 val, err = json.Marshal(flowsForGem)
1324 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001325 logger.Error("Failed to marshal data", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301326 return err
1327 }
Girish Gowdrab77ded92020-04-08 11:45:05 -07001328
Girish Gowdra38d533d2020-03-30 20:38:51 -07001329 RsrcMgr.flowIDToGemInfoLock.Lock()
1330 defer RsrcMgr.flowIDToGemInfoLock.Unlock()
npujarec5762e2020-01-01 14:08:48 +05301331 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001332 logger.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301333 return err
1334 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001335 logger.Debugw("added flowid list for gem to kv successfully", log.Fields{"path": path, "flowidlist": flowsForGem[gem]})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301336 return nil
1337}
1338
1339//DeleteFlowIDsForGem deletes the flowID list entry per gem from kvstore.
npujarec5762e2020-01-01 14:08:48 +05301340func (RsrcMgr *OpenOltResourceMgr) DeleteFlowIDsForGem(ctx context.Context, intf uint32, gem uint32) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301341 path := fmt.Sprintf(FlowIDsForGem, intf)
1342 var val []byte
1343
npujarec5762e2020-01-01 14:08:48 +05301344 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301345 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001346 logger.Error("Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301347 return
1348 }
1349 if flowsForGem == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001350 logger.Error("No flowids found ", log.Fields{"intf": intf, "gemport": gem})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301351 return
1352 }
1353 // once we get the flows per gem map from kv , just delete the gem entry from the map
1354 delete(flowsForGem, gem)
1355 // once gem entry is deleted update the kv store.
1356 val, err = json.Marshal(flowsForGem)
1357 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001358 logger.Error("Failed to marshal data", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301359 return
1360 }
Girish Gowdra38d533d2020-03-30 20:38:51 -07001361
1362 RsrcMgr.flowIDToGemInfoLock.Lock()
1363 defer RsrcMgr.flowIDToGemInfoLock.Unlock()
npujarec5762e2020-01-01 14:08:48 +05301364 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001365 logger.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301366 return
1367 }
1368 return
1369}
1370
1371//GetFlowIDsGemMapForInterface gets flowids per gemport and interface
npujarec5762e2020-01-01 14:08:48 +05301372func (RsrcMgr *OpenOltResourceMgr) GetFlowIDsGemMapForInterface(ctx context.Context, intf uint32) (map[uint32][]uint32, error) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301373 path := fmt.Sprintf(FlowIDsForGem, intf)
1374 var flowsForGem map[uint32][]uint32
1375 var val []byte
Girish Gowdra38d533d2020-03-30 20:38:51 -07001376 RsrcMgr.flowIDToGemInfoLock.RLock()
npujarec5762e2020-01-01 14:08:48 +05301377 value, err := RsrcMgr.KVStore.Get(ctx, path)
Girish Gowdra38d533d2020-03-30 20:38:51 -07001378 RsrcMgr.flowIDToGemInfoLock.RUnlock()
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301379 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001380 logger.Error("failed to get data from kv store")
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301381 return nil, err
1382 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001383 if value != nil && value.Value != nil {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301384 if val, err = kvstore.ToByte(value.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001385 logger.Error("Failed to convert to byte array ", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301386 return nil, err
1387 }
1388 if err = json.Unmarshal(val, &flowsForGem); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001389 logger.Error("Failed to unmarshall", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301390 return nil, err
1391 }
1392 }
1393 return flowsForGem, nil
1394}
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301395
1396//DeleteIntfIDGempMapPath deletes the intf id path used to store flow ids per gem to kvstore.
npujarec5762e2020-01-01 14:08:48 +05301397func (RsrcMgr *OpenOltResourceMgr) DeleteIntfIDGempMapPath(ctx context.Context, intf uint32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301398 path := fmt.Sprintf(FlowIDsForGem, intf)
Girish Gowdra38d533d2020-03-30 20:38:51 -07001399 RsrcMgr.flowIDToGemInfoLock.Lock()
1400 defer RsrcMgr.flowIDToGemInfoLock.Unlock()
npujarec5762e2020-01-01 14:08:48 +05301401 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001402 logger.Errorw("Failed to delete nni interfaces from kv store", log.Fields{"path": path})
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301403 return
1404 }
1405 return
1406}
1407
1408// RemoveResourceMap Clear resource map associated with (intfid, onuid, uniid) tuple.
npujarec5762e2020-01-01 14:08:48 +05301409func (RsrcMgr *OpenOltResourceMgr) RemoveResourceMap(ctx context.Context, intfID uint32, onuID int32, uniID int32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301410 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +05301411 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301412}
Esin Karamanccb714b2019-11-29 15:02:06 +00001413
1414//GetMcastQueuePerInterfaceMap gets multicast queue info per pon interface
npujarec5762e2020-01-01 14:08:48 +05301415func (RsrcMgr *OpenOltResourceMgr) GetMcastQueuePerInterfaceMap(ctx context.Context) (map[uint32][]uint32, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001416 path := fmt.Sprintf(McastQueuesForIntf)
1417 var mcastQueueToIntfMap map[uint32][]uint32
1418 var val []byte
1419
npujarec5762e2020-01-01 14:08:48 +05301420 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001421 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001422 logger.Error("failed to get data from kv store")
Esin Karamanccb714b2019-11-29 15:02:06 +00001423 return nil, err
1424 }
1425 if kvPair != nil && kvPair.Value != nil {
1426 if val, err = kvstore.ToByte(kvPair.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001427 logger.Error("Failed to convert to byte array ", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001428 return nil, err
1429 }
1430 if err = json.Unmarshal(val, &mcastQueueToIntfMap); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001431 logger.Error("Failed to unmarshall ", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001432 return nil, err
1433 }
1434 }
1435 return mcastQueueToIntfMap, nil
1436}
1437
1438//AddMcastQueueForIntf adds multicast queue for pon interface
npujarec5762e2020-01-01 14:08:48 +05301439func (RsrcMgr *OpenOltResourceMgr) AddMcastQueueForIntf(ctx context.Context, intf uint32, gem uint32, servicePriority uint32) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001440 var val []byte
1441 path := fmt.Sprintf(McastQueuesForIntf)
1442
npujarec5762e2020-01-01 14:08:48 +05301443 mcastQueues, err := RsrcMgr.GetMcastQueuePerInterfaceMap(ctx)
Esin Karamanccb714b2019-11-29 15:02:06 +00001444 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001445 logger.Errorw("Failed to get multicast queue info for interface", log.Fields{"error": err, "intf": intf})
Esin Karamanccb714b2019-11-29 15:02:06 +00001446 return err
1447 }
1448 if mcastQueues == nil {
1449 mcastQueues = make(map[uint32][]uint32)
1450 }
1451 mcastQueues[intf] = []uint32{gem, servicePriority}
1452 if val, err = json.Marshal(mcastQueues); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001453 logger.Errorw("Failed to marshal data", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001454 return err
1455 }
npujarec5762e2020-01-01 14:08:48 +05301456 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001457 logger.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Esin Karamanccb714b2019-11-29 15:02:06 +00001458 return err
1459 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001460 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 +00001461 return nil
1462}
1463
1464//AddFlowGroupToKVStore adds flow group into KV store
npujarec5762e2020-01-01 14:08:48 +05301465func (RsrcMgr *OpenOltResourceMgr) AddFlowGroupToKVStore(ctx context.Context, groupEntry *ofp.OfpGroupEntry, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001466 var Value []byte
1467 var err error
1468 var path string
1469 if cached {
1470 path = fmt.Sprintf(FlowGroupCached, groupEntry.Desc.GroupId)
1471 } else {
1472 path = fmt.Sprintf(FlowGroup, groupEntry.Desc.GroupId)
1473 }
1474 //build group info object
1475 var outPorts []uint32
1476 for _, ofBucket := range groupEntry.Desc.Buckets {
1477 for _, ofAction := range ofBucket.Actions {
1478 if ofAction.Type == ofp.OfpActionType_OFPAT_OUTPUT {
1479 outPorts = append(outPorts, ofAction.GetOutput().Port)
1480 }
1481 }
1482 }
1483 groupInfo := GroupInfo{
1484 GroupID: groupEntry.Desc.GroupId,
1485 OutPorts: outPorts,
1486 }
1487
1488 Value, err = json.Marshal(groupInfo)
1489
1490 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001491 logger.Error("failed to Marshal flow group object")
Esin Karamanccb714b2019-11-29 15:02:06 +00001492 return err
1493 }
1494
npujarec5762e2020-01-01 14:08:48 +05301495 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001496 logger.Errorf("Failed to update resource %s", path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001497 return err
1498 }
1499 return nil
1500}
1501
1502//RemoveFlowGroupFromKVStore removes flow group from KV store
npujarec5762e2020-01-01 14:08:48 +05301503func (RsrcMgr *OpenOltResourceMgr) RemoveFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) bool {
Esin Karamanccb714b2019-11-29 15:02:06 +00001504 var path string
1505 if cached {
1506 path = fmt.Sprintf(FlowGroupCached, groupID)
1507 } else {
1508 path = fmt.Sprintf(FlowGroup, groupID)
1509 }
npujarec5762e2020-01-01 14:08:48 +05301510 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001511 logger.Errorf("Failed to remove resource %s due to %s", path, err)
Esin Karamanccb714b2019-11-29 15:02:06 +00001512 return false
1513 }
1514 return true
1515}
1516
1517//GetFlowGroupFromKVStore fetches flow group from the KV store. Returns (false, {} error) if any problem occurs during
1518//fetching the data. Returns (true, groupInfo, nil) if the group is fetched successfully.
1519// Returns (false, {}, nil) if the group does not exists in the KV store.
npujarec5762e2020-01-01 14:08:48 +05301520func (RsrcMgr *OpenOltResourceMgr) GetFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) (bool, GroupInfo, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001521 var groupInfo GroupInfo
1522 var path string
1523 if cached {
1524 path = fmt.Sprintf(FlowGroupCached, groupID)
1525 } else {
1526 path = fmt.Sprintf(FlowGroup, groupID)
1527 }
npujarec5762e2020-01-01 14:08:48 +05301528 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001529 if err != nil {
1530 return false, groupInfo, err
1531 }
1532 if kvPair != nil && kvPair.Value != nil {
1533 Val, err := kvstore.ToByte(kvPair.Value)
1534 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001535 logger.Errorw("Failed to convert flow group into byte array", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001536 return false, groupInfo, err
1537 }
1538 if err = json.Unmarshal(Val, &groupInfo); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001539 logger.Errorw("Failed to unmarshal", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001540 return false, groupInfo, err
1541 }
1542 return true, groupInfo, nil
1543 }
1544 return false, groupInfo, nil
1545}