blob: 5f884725cc86f799fc07e67182d5241d1d8e0692 [file] [log] [blame]
Abhilash S.L7f17e402019-03-15 17:40:41 +05301/*
2 * Copyright 2019-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070017//Package resourcemanager provides the utility for managing resources
manikkaraj kbf256be2019-03-25 00:13:48 +053018package resourcemanager
Abhilash S.L7f17e402019-03-15 17:40:41 +053019
20import (
npujarec5762e2020-01-01 14:08:48 +053021 "context"
Girish Gowdru0c588b22019-04-23 23:24:56 -040022 "encoding/json"
23 "errors"
24 "fmt"
25 "strconv"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070026 "strings"
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
Kent Hagermane6ff1012020-07-14 15:07:53 -040030 "github.com/opencord/voltha-openolt-adapter/internal/pkg/olterrors"
31
Girish Gowdraa09aeab2020-09-14 16:30:52 -070032 "github.com/opencord/voltha-lib-go/v4/pkg/db"
33 "github.com/opencord/voltha-lib-go/v4/pkg/db/kvstore"
34 "github.com/opencord/voltha-lib-go/v4/pkg/log"
35 ponrmgr "github.com/opencord/voltha-lib-go/v4/pkg/ponresourcemanager"
36 ofp "github.com/opencord/voltha-protos/v4/go/openflow_13"
37 "github.com/opencord/voltha-protos/v4/go/openolt"
Abhilash S.L7f17e402019-03-15 17:40:41 +053038)
39
salmansiddiqui7ac62132019-08-22 03:58:50 +000040const (
41 // KvstoreTimeout specifies the time out for KV Store Connection
Neha Sharmacc656962020-04-14 14:26:11 +000042 KvstoreTimeout = 5 * time.Second
Matteo Scandolodfa7a972020-11-06 13:03:40 -080043 // BasePathKvStore - <pathPrefix>/openolt/<device_id>
44 BasePathKvStore = "%s/openolt/{%s}"
Gamze Abakafee36392019-10-03 11:17:24 +000045 // TpIDPathSuffix - <(pon_id, onu_id, uni_id)>/tp_id
46 TpIDPathSuffix = "{%d,%d,%d}/tp_id"
47 //MeterIDPathSuffix - <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
48 MeterIDPathSuffix = "{%d,%d,%d}/{%d}/meter_id/{%s}"
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053049 //NnniIntfID - nniintfids
50 NnniIntfID = "nniintfids"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070051 // OnuPacketINPathPrefix - path prefix where ONU packet-in vlanID/PCP is stored
52 //format: onu_packetin/{<intfid>,<onuid>,<logicalport>}
53 OnuPacketINPathPrefix = "onu_packetin/{%d,%d,%d}"
54 // OnuPacketINPath path on the kvstore to store packetin gemport,which will be used for packetin, packetout
55 //format: onu_packetin/{<intfid>,<onuid>,<logicalport>}/{<vlanId>,<priority>}
56 OnuPacketINPath = OnuPacketINPathPrefix + "/{%d,%d}"
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +053057 //FlowIDsForGem flowids_per_gem/<intfid>
58 FlowIDsForGem = "flowids_per_gem/{%d}"
Esin Karamanccb714b2019-11-29 15:02:06 +000059 //McastQueuesForIntf multicast queues for pon interfaces
60 McastQueuesForIntf = "mcast_qs_for_int"
61 //FlowGroup flow_groups/<flow_group_id>
62 // A group is stored under this path on the KV store after it has been installed to the device.
63 // It should also be deleted after it has been removed from the device accordingly.
64 FlowGroup = "flow_groups/{%d}"
65 //FlowGroupCached flow_groups_cached/<flow_group_id>
66 // When a group add request received, we create the group without setting any members to it since we cannot
67 // set any members to a group until it is associated with a multicast flow. It is a BAL limitation.
68 // When the related multicast flow has been created we perform set members operation for the group.
69 // That is why we need to keep the members of a group until the multicast flow creation request comes.
70 // We preserve the groups under "FlowGroupsCached" directory in the KV store temporarily. Having set members,
71 // we remove the group from the cached group store.
72 FlowGroupCached = "flow_groups_cached/{%d}"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070073
74 //FlowIDPath - Path on the KV store for storing list of Flow IDs for a given subscriber
75 //Format: BasePathKvStore/<(pon_intf_id, onu_id, uni_id)>/flow_ids
76 FlowIDPath = "{%s}/flow_ids"
77 //FlowIDInfoPath - Used to store more metadata associated with the flow_id
78 //Format: BasePathKvStore/<(pon_intf_id, onu_id, uni_id)>/flow_id_info/<flow_id>
79 FlowIDInfoPath = "{%s}/flow_id_info/{%d}"
salmansiddiqui7ac62132019-08-22 03:58:50 +000080)
Abhilash S.L7f17e402019-03-15 17:40:41 +053081
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070082// FlowInfo holds the flow information
Abhilash S.L8ee90712019-04-29 16:24:22 +053083type FlowInfo struct {
Girish Gowdraa09aeab2020-09-14 16:30:52 -070084 Flow *openolt.Flow
85 IsSymmtricFlow bool
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053086}
87
88// OnuGemInfo holds onu information along with gem port list and uni port list
89type OnuGemInfo struct {
90 OnuID uint32
91 SerialNumber string
92 IntfID uint32
93 GemPorts []uint32
94 UniPorts []uint32
95}
96
97// PacketInInfoKey is the key for packet in gemport
98type PacketInInfoKey struct {
99 IntfID uint32
100 OnuID uint32
101 LogicalPort uint32
Esin Karaman7fb80c22020-07-16 14:23:33 +0000102 VlanID uint16
103 Priority uint8
Abhilash S.L8ee90712019-04-29 16:24:22 +0530104}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700105
Esin Karamanccb714b2019-11-29 15:02:06 +0000106// GroupInfo holds group information
107type GroupInfo struct {
108 GroupID uint32
109 OutPorts []uint32
110}
111
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700112// OpenOltResourceMgr holds resource related information as provided below for each field
Abhilash S.L7f17e402019-03-15 17:40:41 +0530113type OpenOltResourceMgr struct {
Neha Sharma3f221ae2020-04-29 19:02:12 +0000114 DeviceID string // OLT device id
115 Address string // Host and port of the kv store to connect to
116 Args string // args
117 KVStore *db.Backend // backend kv store connection handle
118 DeviceType string
119 DevInfo *openolt.DeviceInfo // device information
Girish Gowdru0c588b22019-04-23 23:24:56 -0400120 // array of pon resource managers per interface technology
121 ResourceMgrs map[uint32]*ponrmgr.PONResourceManager
Girish Gowdra38d533d2020-03-30 20:38:51 -0700122
123 // This protects concurrent gemport_id allocate/delete calls on a per PON port basis
124 GemPortIDMgmtLock []sync.RWMutex
125 // This protects concurrent alloc_id allocate/delete calls on a per PON port basis
126 AllocIDMgmtLock []sync.RWMutex
127 // This protects concurrent onu_id allocate/delete calls on a per PON port basis
128 OnuIDMgmtLock []sync.RWMutex
Abhilash S.L7f17e402019-03-15 17:40:41 +0530129}
130
Neha Sharma96b7bf22020-06-15 10:37:32 +0000131func newKVClient(ctx context.Context, storeType string, address string, timeout time.Duration) (kvstore.Client, error) {
132 logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400133 switch storeType {
134 case "consul":
Neha Sharma96b7bf22020-06-15 10:37:32 +0000135 return kvstore.NewConsulClient(ctx, address, timeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400136 case "etcd":
Neha Sharma96b7bf22020-06-15 10:37:32 +0000137 return kvstore.NewEtcdClient(ctx, address, timeout, log.FatalLevel)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400138 }
139 return nil, errors.New("unsupported-kv-store")
Abhilash S.L7f17e402019-03-15 17:40:41 +0530140}
141
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700142// SetKVClient sets the KV client and return a kv backend
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800143func SetKVClient(ctx context.Context, backend string, addr string, DeviceID string, basePathKvStore string) *db.Backend {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400144 // TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
145 // issue between kv store and backend , core is not calling NewBackend directly
Neha Sharma96b7bf22020-06-15 10:37:32 +0000146 kvClient, err := newKVClient(ctx, backend, addr, KvstoreTimeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400147 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000148 logger.Fatalw(ctx, "Failed to init KV client\n", log.Fields{"err": err})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400149 return nil
150 }
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700151
sbarbaria8910ba2019-11-05 10:12:23 -0500152 kvbackend := &db.Backend{
Girish Gowdru0c588b22019-04-23 23:24:56 -0400153 Client: kvClient,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700154 StoreType: backend,
Neha Sharma3f221ae2020-04-29 19:02:12 +0000155 Address: addr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700156 Timeout: KvstoreTimeout,
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800157 PathPrefix: fmt.Sprintf(BasePathKvStore, basePathKvStore, DeviceID)}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530158
Girish Gowdru0c588b22019-04-23 23:24:56 -0400159 return kvbackend
Abhilash S.L7f17e402019-03-15 17:40:41 +0530160}
161
Gamze Abakafee36392019-10-03 11:17:24 +0000162// NewResourceMgr init a New resource manager instance which in turn instantiates pon resource manager
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700163// instances according to technology. Initializes the default resource ranges for all
164// the resources.
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800165func NewResourceMgr(ctx context.Context, deviceID string, KVStoreAddress string, kvStoreType string, deviceType string, devInfo *openolt.DeviceInfo, basePathKvStore string) *OpenOltResourceMgr {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400166 var ResourceMgr OpenOltResourceMgr
divyadesai3af43e12020-08-18 07:10:54 +0000167 logger.Debugf(ctx, "Init new resource manager , address: %s, device-id: %s", KVStoreAddress, deviceID)
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700168 ResourceMgr.DeviceID = deviceID
Neha Sharma3f221ae2020-04-29 19:02:12 +0000169 ResourceMgr.Address = KVStoreAddress
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700170 ResourceMgr.DeviceType = deviceType
171 ResourceMgr.DevInfo = devInfo
Girish Gowdra38d533d2020-03-30 20:38:51 -0700172 NumPONPorts := devInfo.GetPonPorts()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530173
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700174 Backend := kvStoreType
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800175 ResourceMgr.KVStore = SetKVClient(ctx, Backend, ResourceMgr.Address, deviceID, basePathKvStore)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400176 if ResourceMgr.KVStore == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000177 logger.Error(ctx, "Failed to setup KV store")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400178 }
179 Ranges := make(map[string]*openolt.DeviceInfo_DeviceResourceRanges)
180 RsrcMgrsByTech := make(map[string]*ponrmgr.PONResourceManager)
181 ResourceMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530182
Girish Gowdra38d533d2020-03-30 20:38:51 -0700183 ResourceMgr.AllocIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
184 ResourceMgr.GemPortIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
185 ResourceMgr.OnuIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
186
Girish Gowdru0c588b22019-04-23 23:24:56 -0400187 // TODO self.args = registry('main').get_args()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530188
Girish Gowdru0c588b22019-04-23 23:24:56 -0400189 /*
190 If a legacy driver returns protobuf without any ranges,s synthesize one from
Gamze Abakafee36392019-10-03 11:17:24 +0000191 the legacy global per-device information. This, in theory, is temporary until
Girish Gowdru0c588b22019-04-23 23:24:56 -0400192 the legacy drivers are upgrade to support pool ranges.
193 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700194 if devInfo.Ranges == nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400195 var ranges openolt.DeviceInfo_DeviceResourceRanges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700196 ranges.Technology = devInfo.GetTechnology()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530197
Girish Gowdru0c588b22019-04-23 23:24:56 -0400198 var index uint32
199 for index = 0; index < NumPONPorts; index++ {
200 ranges.IntfIds = append(ranges.IntfIds, index)
201 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530202
Abhilash S.L8ee90712019-04-29 16:24:22 +0530203 var Pool openolt.DeviceInfo_DeviceResourceRanges_Pool
Girish Gowdru0c588b22019-04-23 23:24:56 -0400204 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700205 Pool.Start = devInfo.OnuIdStart
206 Pool.End = devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400207 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
cbabuabf02352019-10-15 13:14:56 +0200208 onuPool := Pool
209 ranges.Pools = append(ranges.Pools, &onuPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530210
Girish Gowdru0c588b22019-04-23 23:24:56 -0400211 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700212 Pool.Start = devInfo.AllocIdStart
213 Pool.End = devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400214 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200215 allocPool := Pool
216 ranges.Pools = append(ranges.Pools, &allocPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530217
Girish Gowdru0c588b22019-04-23 23:24:56 -0400218 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700219 Pool.Start = devInfo.GemportIdStart
220 Pool.End = devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400221 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200222 gemPool := Pool
223 ranges.Pools = append(ranges.Pools, &gemPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530224
Girish Gowdru0c588b22019-04-23 23:24:56 -0400225 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700226 Pool.Start = devInfo.FlowIdStart
227 Pool.End = devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400228 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
Abhilash S.L8ee90712019-04-29 16:24:22 +0530229 ranges.Pools = append(ranges.Pools, &Pool)
230 // Add to device info
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700231 devInfo.Ranges = append(devInfo.Ranges, &ranges)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400232 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530233
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700234 // Create a separate Resource Manager instance for each range. This assumes that
Girish Gowdru0c588b22019-04-23 23:24:56 -0400235 // each technology is represented by only a single range
236 var GlobalPONRsrcMgr *ponrmgr.PONResourceManager
237 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700238 for _, TechRange := range devInfo.Ranges {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400239 technology := TechRange.Technology
Neha Sharma96b7bf22020-06-15 10:37:32 +0000240 logger.Debugf(ctx, "Device info technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400241 Ranges[technology] = TechRange
Neha Sharma96b7bf22020-06-15 10:37:32 +0000242
243 RsrcMgrsByTech[technology], err = ponrmgr.NewPONResourceManager(ctx, technology, deviceType, deviceID,
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800244 Backend, ResourceMgr.Address, basePathKvStore)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400245 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000246 logger.Errorf(ctx, "Failed to create pon resource manager instance for technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400247 return nil
248 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700249 // resource_mgrs_by_tech[technology] = resource_mgr
Girish Gowdru0c588b22019-04-23 23:24:56 -0400250 if GlobalPONRsrcMgr == nil {
251 GlobalPONRsrcMgr = RsrcMgrsByTech[technology]
252 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700253 for _, IntfID := range TechRange.IntfIds {
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700254 ResourceMgr.ResourceMgrs[IntfID] = RsrcMgrsByTech[technology]
Girish Gowdru0c588b22019-04-23 23:24:56 -0400255 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700256 // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
npujarec5762e2020-01-01 14:08:48 +0530257 InitializeDeviceResourceRangeAndPool(ctx, RsrcMgrsByTech[technology], GlobalPONRsrcMgr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700258 TechRange, devInfo)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400259 }
260 // After we have initialized resource ranges, initialize the
261 // resource pools accordingly.
262 for _, PONRMgr := range RsrcMgrsByTech {
npujarec5762e2020-01-01 14:08:48 +0530263 _ = PONRMgr.InitDeviceResourcePool(ctx)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400264 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000265 logger.Info(ctx, "Initialization of resource manager success!")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400266 return &ResourceMgr
Abhilash S.L7f17e402019-03-15 17:40:41 +0530267}
268
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700269// InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply
270// device specific information. If KV doesn't exist
271// or is broader than the device, the device's information will
272// dictate the range limits
npujarec5762e2020-01-01 14:08:48 +0530273func InitializeDeviceResourceRangeAndPool(ctx context.Context, ponRMgr *ponrmgr.PONResourceManager, globalPONRMgr *ponrmgr.PONResourceManager,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700274 techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530275
Girish Gowdru0c588b22019-04-23 23:24:56 -0400276 // init the resource range pool according to the sharing type
Abhilash S.L7f17e402019-03-15 17:40:41 +0530277
Neha Sharma96b7bf22020-06-15 10:37:32 +0000278 logger.Debugf(ctx, "Resource range pool init for technology %s", ponRMgr.Technology)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700279 // first load from KV profiles
npujarec5762e2020-01-01 14:08:48 +0530280 status := ponRMgr.InitResourceRangesFromKVStore(ctx)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700281 if !status {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000282 logger.Debugf(ctx, "Failed to load resource ranges from KV store for tech %s", ponRMgr.Technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400283 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530284
Girish Gowdru0c588b22019-04-23 23:24:56 -0400285 /*
286 Then apply device specific information. If KV doesn't exist
Gamze Abakafee36392019-10-03 11:17:24 +0000287 or is broader than the device, the device's information will
Girish Gowdru0c588b22019-04-23 23:24:56 -0400288 dictate the range limits
289 */
Neha Sharma96b7bf22020-06-15 10:37:32 +0000290 logger.Debugw(ctx, "Using device info to init pon resource ranges", log.Fields{"Tech": ponRMgr.Technology})
Abhilash S.L7f17e402019-03-15 17:40:41 +0530291
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700292 ONUIDStart := devInfo.OnuIdStart
293 ONUIDEnd := devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400294 ONUIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
295 ONUIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700296 AllocIDStart := devInfo.AllocIdStart
297 AllocIDEnd := devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400298 AllocIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
299 AllocIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700300 GEMPortIDStart := devInfo.GemportIdStart
301 GEMPortIDEnd := devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400302 GEMPortIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
303 GEMPortIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700304 FlowIDStart := devInfo.FlowIdStart
305 FlowIDEnd := devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400306 FlowIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
307 FlowIDSharedPoolID := uint32(0)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530308
Girish Gowdru0c588b22019-04-23 23:24:56 -0400309 var FirstIntfPoolID uint32
310 var SharedPoolID uint32
Abhilash S.L7f17e402019-03-15 17:40:41 +0530311
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400312 /*
313 * As a zero check is made against SharedPoolID to check whether the resources are shared across all intfs
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700314 * if resources are shared across interfaces then SharedPoolID is given a positive number.
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400315 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700316 for _, FirstIntfPoolID = range techRange.IntfIds {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400317 // skip the intf id 0
318 if FirstIntfPoolID == 0 {
319 continue
320 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400321 break
322 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530323
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700324 for _, RangePool := range techRange.Pools {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400325 if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400326 SharedPoolID = FirstIntfPoolID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400327 } else if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_SAME_TECH {
328 SharedPoolID = FirstIntfPoolID
329 } else {
330 SharedPoolID = 0
331 }
332 if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID {
333 ONUIDStart = RangePool.Start
334 ONUIDEnd = RangePool.End
335 ONUIDShared = RangePool.Sharing
336 ONUIDSharedPoolID = SharedPoolID
337 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID {
338 AllocIDStart = RangePool.Start
339 AllocIDEnd = RangePool.End
340 AllocIDShared = RangePool.Sharing
341 AllocIDSharedPoolID = SharedPoolID
342 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID {
343 GEMPortIDStart = RangePool.Start
344 GEMPortIDEnd = RangePool.End
345 GEMPortIDShared = RangePool.Sharing
346 GEMPortIDSharedPoolID = SharedPoolID
347 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID {
348 FlowIDStart = RangePool.Start
349 FlowIDEnd = RangePool.End
350 FlowIDShared = RangePool.Sharing
351 FlowIDSharedPoolID = SharedPoolID
352 }
353 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530354
Neha Sharma96b7bf22020-06-15 10:37:32 +0000355 logger.Debugw(ctx, "Device info init", log.Fields{"technology": techRange.Technology,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400356 "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd, "onu_id_shared_pool_id": ONUIDSharedPoolID,
357 "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd,
358 "alloc_id_shared_pool_id": AllocIDSharedPoolID,
359 "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd,
360 "gemport_id_shared_pool_id": GEMPortIDSharedPoolID,
361 "flow_id_start": FlowIDStart,
362 "flow_id_end_idx": FlowIDEnd,
363 "flow_id_shared_pool_id": FlowIDSharedPoolID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700364 "intf_ids": techRange.IntfIds,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400365 "uni_id_start": 0,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700366 "uni_id_end_idx": 1, /*MaxUNIIDperONU()*/
367 })
Abhilash S.L7f17e402019-03-15 17:40:41 +0530368
Neha Sharma96b7bf22020-06-15 10:37:32 +0000369 ponRMgr.InitDefaultPONResourceRanges(ctx, ONUIDStart, ONUIDEnd, ONUIDSharedPoolID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400370 AllocIDStart, AllocIDEnd, AllocIDSharedPoolID,
371 GEMPortIDStart, GEMPortIDEnd, GEMPortIDSharedPoolID,
372 FlowIDStart, FlowIDEnd, FlowIDSharedPoolID, 0, 1,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700373 devInfo.PonPorts, techRange.IntfIds)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530374
Girish Gowdru0c588b22019-04-23 23:24:56 -0400375 // For global sharing, make sure to refresh both local and global resource manager instances' range
Abhilash S.L7f17e402019-03-15 17:40:41 +0530376
Girish Gowdru0c588b22019-04-23 23:24:56 -0400377 if ONUIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000378 globalPONRMgr.UpdateRanges(ctx, ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400379 "", 0, nil)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000380 ponRMgr.UpdateRanges(ctx, ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700381 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400382 }
383 if AllocIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000384 globalPONRMgr.UpdateRanges(ctx, ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400385 "", 0, nil)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530386
Neha Sharma96b7bf22020-06-15 10:37:32 +0000387 ponRMgr.UpdateRanges(ctx, ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700388 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400389 }
390 if GEMPortIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000391 globalPONRMgr.UpdateRanges(ctx, ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400392 "", 0, nil)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000393 ponRMgr.UpdateRanges(ctx, ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700394 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400395 }
396 if FlowIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000397 globalPONRMgr.UpdateRanges(ctx, ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400398 "", 0, nil)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000399 ponRMgr.UpdateRanges(ctx, ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700400 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400401 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530402
Girish Gowdru0c588b22019-04-23 23:24:56 -0400403 // Make sure loaded range fits the platform bit encoding ranges
Neha Sharma96b7bf22020-06-15 10:37:32 +0000404 ponRMgr.UpdateRanges(ctx, ponrmgr.UNI_ID_START_IDX, 0, ponrmgr.UNI_ID_END_IDX /* TODO =OpenOltPlatform.MAX_UNIS_PER_ONU-1*/, 1, "", 0, nil)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530405}
406
Devmalya Paul495b94a2019-08-27 19:42:00 -0400407// Delete clears used resources for the particular olt device being deleted
npujarec5762e2020-01-01 14:08:48 +0530408func (RsrcMgr *OpenOltResourceMgr) Delete(ctx context.Context) error {
Devmalya Paul495b94a2019-08-27 19:42:00 -0400409 /* TODO
410 def __del__(self):
411 self.log.info("clearing-device-resource-pool")
412 for key, resource_mgr in self.resource_mgrs.iteritems():
413 resource_mgr.clear_device_resource_pool()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530414
Devmalya Paul495b94a2019-08-27 19:42:00 -0400415 def assert_pon_id_limit(self, pon_intf_id):
416 assert pon_intf_id in self.resource_mgrs
Abhilash S.L7f17e402019-03-15 17:40:41 +0530417
Devmalya Paul495b94a2019-08-27 19:42:00 -0400418 def assert_onu_id_limit(self, pon_intf_id, onu_id):
419 self.assert_pon_id_limit(pon_intf_id)
420 self.resource_mgrs[pon_intf_id].assert_resource_limits(onu_id, PONResourceManager.ONU_ID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530421
Devmalya Paul495b94a2019-08-27 19:42:00 -0400422 @property
423 def max_uni_id_per_onu(self):
424 return 0 #OpenOltPlatform.MAX_UNIS_PER_ONU-1, zero-based indexing Uncomment or override to make default multi-uni
Abhilash S.L7f17e402019-03-15 17:40:41 +0530425
Devmalya Paul495b94a2019-08-27 19:42:00 -0400426 def assert_uni_id_limit(self, pon_intf_id, onu_id, uni_id):
427 self.assert_onu_id_limit(pon_intf_id, onu_id)
428 self.resource_mgrs[pon_intf_id].assert_resource_limits(uni_id, PONResourceManager.UNI_ID)
429 */
430 for _, rsrcMgr := range RsrcMgr.ResourceMgrs {
npujarec5762e2020-01-01 14:08:48 +0530431 if err := rsrcMgr.ClearDeviceResourcePool(ctx); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000432 logger.Debug(ctx, "Failed to clear device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400433 return err
434 }
435 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000436 logger.Debug(ctx, "Cleared device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400437 return nil
438}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530439
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700440// GetONUID returns the available OnuID for the given pon-port
npujarec5762e2020-01-01 14:08:48 +0530441func (RsrcMgr *OpenOltResourceMgr) GetONUID(ctx context.Context, ponIntfID uint32) (uint32, error) {
salmansiddiqui352a45c2019-08-19 10:15:36 +0000442 // Check if Pon Interface ID is present in Resource-manager-map
Girish Gowdrab77ded92020-04-08 11:45:05 -0700443 RsrcMgr.OnuIDMgmtLock[ponIntfID].Lock()
444 defer RsrcMgr.OnuIDMgmtLock[ponIntfID].Unlock()
445
salmansiddiqui352a45c2019-08-19 10:15:36 +0000446 if _, ok := RsrcMgr.ResourceMgrs[ponIntfID]; !ok {
447 err := errors.New("invalid-pon-interface-" + strconv.Itoa(int(ponIntfID)))
448 return 0, err
449 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400450 // Get ONU id for a provided pon interface ID.
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700451 onuID, err := RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ctx, ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400452 ponrmgr.ONU_ID, 1)
453 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000454 logger.Errorf(ctx, "Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700455 ponIntfID, ponrmgr.ONU_ID)
cbabuabf02352019-10-15 13:14:56 +0200456 return 0, err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400457 }
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700458 if onuID != nil {
459 RsrcMgr.ResourceMgrs[ponIntfID].InitResourceMap(ctx, fmt.Sprintf("%d,%d", ponIntfID, onuID[0]))
460 return onuID[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400461 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530462
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700463 return 0, err // return OnuID 0 on error
Abhilash S.L7f17e402019-03-15 17:40:41 +0530464}
465
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700466// GetFlowIDInfo returns the slice of flow info of the given pon-port
467// Note: For flows which trap from the NNI and not really associated with any particular
468// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700469func (RsrcMgr *OpenOltResourceMgr) GetFlowIDInfo(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32, flowID uint64) *FlowInfo {
470 var flowInfo FlowInfo
Abhilash S.L8ee90712019-04-29 16:24:22 +0530471
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700472 subs := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
473 Path := fmt.Sprintf(FlowIDInfoPath, subs, flowID)
474 value, err := RsrcMgr.KVStore.Get(ctx, Path)
475 if err == nil {
476 if value != nil {
477 Val, err := toByte(value.Value)
478 if err != nil {
479 logger.Errorw(ctx, "Failed to convert flowinfo into byte array", log.Fields{"error": err, "subs": subs})
480 return nil
481 }
482 if err = json.Unmarshal(Val, &flowInfo); err != nil {
483 logger.Errorw(ctx, "Failed to unmarshal", log.Fields{"error": err, "subs": subs})
484 return nil
485 }
486 }
487 }
488 if flowInfo.Flow == nil {
489 logger.Debugw(ctx, "No flowInfo found in KV store", log.Fields{"subs": subs})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530490 return nil
491 }
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700492 return &flowInfo
Abhilash S.L8ee90712019-04-29 16:24:22 +0530493}
494
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700495// GetCurrentFlowIDsForOnu fetches flow ID from the resource manager
496// Note: For flows which trap from the NNI and not really associated with any particular
497// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700498func (RsrcMgr *OpenOltResourceMgr) GetCurrentFlowIDsForOnu(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32) ([]uint64, error) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700499
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700500 subs := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
501 path := fmt.Sprintf(FlowIDPath, subs)
502
503 var data []uint64
504 value, err := RsrcMgr.KVStore.Get(ctx, path)
505 if err == nil {
506 if value != nil {
507 Val, _ := toByte(value.Value)
508 if err = json.Unmarshal(Val, &data); err != nil {
509 logger.Error(ctx, "Failed to unmarshal")
510 return nil, err
511 }
512 }
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700513 }
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700514 return data, nil
Abhilash S.L8ee90712019-04-29 16:24:22 +0530515}
516
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700517// UpdateFlowIDInfo updates flow info for the given pon interface, onu id, and uni id
518// Note: For flows which trap from the NNI and not really associated with any particular
519// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700520func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDInfo(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32,
521 flowID uint64, flowData FlowInfo) error {
522
523 subs := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
524 path := fmt.Sprintf(FlowIDInfoPath, subs, flowID)
525
526 var value []byte
527 var err error
528 value, err = json.Marshal(flowData)
529 if err != nil {
530 logger.Errorf(ctx, "failed to Marshal, resource path %s", path)
531 return err
532 }
533
534 if err = RsrcMgr.KVStore.Put(ctx, path, value); err != nil {
535 logger.Errorf(ctx, "Failed to update resource %s", path)
536 }
537
538 // Update the flowID list for the ONU
539 if err = RsrcMgr.UpdateFlowIDForOnu(ctx, ponIntfID, onuID, uniID, flowID, true); err != nil {
540 // If the operation fails, try to remove FlowInfo from the KV store
541 _ = RsrcMgr.KVStore.Delete(ctx, path)
542 return err
543 }
544 return err
Abhilash S.L8ee90712019-04-29 16:24:22 +0530545}
546
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700547// UpdateFlowIDForOnu updates the flow_id list of the ONU (add or remove flow_id from the list)
548func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDForOnu(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32, flowID uint64, add bool) error {
549 /*
550 Update the flow_id list of the ONU (add or remove flow_id from the list)
551 :param pon_intf_onu_id: reference of PON interface id and onu id
552 :param flow_id: flow ID
553 :param add: Boolean flag to indicate whether the flow_id should be
554 added or removed from the list. Defaults to adding the flow.
555 */
556 var Value []byte
557 var err error
558 var retVal bool
559 var idx uint64
560 subs := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
561 path := fmt.Sprintf(FlowIDPath, subs)
562 flowIDs, err := RsrcMgr.GetCurrentFlowIDsForOnu(ctx, ponIntfID, onuID, uniID)
563 if err != nil {
564 // Error logged in the called function
565 return err
566 }
567
568 if add {
569 if retVal, _ = checkForFlowIDInList(flowIDs, flowID); retVal {
570 return nil
571 }
572 flowIDs = append(flowIDs, flowID)
573 } else {
574 if retVal, idx = checkForFlowIDInList(flowIDs, flowID); !retVal {
575 return nil
576 }
577 // delete the index and shift
578 flowIDs = append(flowIDs[:idx], flowIDs[idx+1:]...)
579 }
580 Value, err = json.Marshal(flowIDs)
581 if err != nil {
582 logger.Error(ctx, "Failed to Marshal")
583 return err
584 }
585
586 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
587 logger.Errorf(ctx, "Failed to update resource %s", path)
588 return err
589 }
590 return err
591}
592
593// RemoveFlowIDInfo remove flow info for the given pon interface, onu id, and uni id
594// Note: For flows which trap from the NNI and not really associated with any particular
595// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
596func (RsrcMgr *OpenOltResourceMgr) RemoveFlowIDInfo(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32,
597 flowID uint64) error {
598
599 subs := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
600 path := fmt.Sprintf(FlowIDInfoPath, subs, flowID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530601
Girish Gowdru0c588b22019-04-23 23:24:56 -0400602 var err error
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700603 if err = RsrcMgr.KVStore.Delete(ctx, path); err != nil {
604 logger.Errorf(ctx, "Failed to delete resource %s", path)
605 return err
606 }
Girish Gowdrab77ded92020-04-08 11:45:05 -0700607
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700608 // Update the flowID list for the ONU
609 err = RsrcMgr.UpdateFlowIDForOnu(ctx, ponIntfID, onuID, uniID, flowID, false)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700610
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700611 return err
612}
613
614// RemoveAllFlowsForIntfOnuUniKey removes flow info for the given interface, onu id, and uni id
615func (RsrcMgr *OpenOltResourceMgr) RemoveAllFlowsForIntfOnuUniKey(ctx context.Context, intf uint32, onuID int32, uniID int32) error {
616 flowIDs, err := RsrcMgr.GetCurrentFlowIDsForOnu(ctx, intf, onuID, uniID)
617 if err != nil {
618 // error logged in the called function
619 return err
620 }
621 for _, flID := range flowIDs {
622 if err := RsrcMgr.RemoveFlowIDInfo(ctx, intf, onuID, uniID, flID); err != nil {
623 logger.Errorw(ctx, "failed-to-delete-flow-id-info", log.Fields{"intf": intf, "onuID": onuID, "uniID": uniID, "flowID": flID})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530624 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400625 }
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700626 subs := fmt.Sprintf("%d,%d,%d", intf, onuID, uniID)
627 path := fmt.Sprintf(FlowIDPath, subs)
628 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
629 logger.Errorf(ctx, "Failed to delete resource %s", path)
630 return err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400631 }
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700632 return nil
Abhilash S.L7f17e402019-03-15 17:40:41 +0530633}
634
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700635// GetAllocID return the first Alloc ID for a given pon interface id and onu id and then update the resource map on
636// the KV store with the list of alloc_ids allocated for the pon_intf_onu_id tuple
637// 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 +0530638func (RsrcMgr *OpenOltResourceMgr) GetAllocID(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530639
Girish Gowdru0c588b22019-04-23 23:24:56 -0400640 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700641 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700642
643 RsrcMgr.AllocIDMgmtLock[intfID].Lock()
644 defer RsrcMgr.AllocIDMgmtLock[intfID].Unlock()
645
npujarec5762e2020-01-01 14:08:48 +0530646 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400647 if AllocID != nil {
648 // Since we support only one alloc_id for the ONU at the moment,
649 // return the first alloc_id in the list, if available, for that
650 // ONU.
Neha Sharma96b7bf22020-06-15 10:37:32 +0000651 logger.Debugw(ctx, "Retrieved alloc ID from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400652 return AllocID[0]
653 }
npujarec5762e2020-01-01 14:08:48 +0530654 AllocID, err = RsrcMgr.ResourceMgrs[intfID].GetResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400655 ponrmgr.ALLOC_ID, 1)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530656
Girish Gowdru0c588b22019-04-23 23:24:56 -0400657 if AllocID == nil || err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000658 logger.Error(ctx, "Failed to allocate alloc id")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400659 return 0
660 }
661 // update the resource map on KV store with the list of alloc_id
662 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530663 err = RsrcMgr.ResourceMgrs[intfID].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID, AllocID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400664 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000665 logger.Error(ctx, "Failed to update Alloc ID")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400666 return 0
667 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000668 logger.Debugw(ctx, "Allocated new Tcont from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400669 return AllocID[0]
Abhilash S.L7f17e402019-03-15 17:40:41 +0530670}
671
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700672// UpdateAllocIdsForOnu updates alloc ids in kv store for a given pon interface id, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530673func (RsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ctx context.Context, ponPort uint32, onuID uint32, uniID uint32, allocID []uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530674
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700675 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530676 return RsrcMgr.ResourceMgrs[ponPort].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700677 allocID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530678}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700679
680// GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530681func (RsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(ctx context.Context, intfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700682 uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530683
Girish Gowdru0c588b22019-04-23 23:24:56 -0400684 /* Get gem ports for given pon interface , onu id and uni id. */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530685
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700686 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530687 return RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530688}
689
Gamze Abakafee36392019-10-03 11:17:24 +0000690// GetCurrentAllocIDsForOnu returns alloc ids for given pon interface and onu id
npujarec5762e2020-01-01 14:08:48 +0530691func (RsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDsForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530692
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700693 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530694 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400695 if AllocID != nil {
Gamze Abakafee36392019-10-03 11:17:24 +0000696 return AllocID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400697 }
Gamze Abakafee36392019-10-03 11:17:24 +0000698 return []uint32{}
699}
700
701// RemoveAllocIDForOnu removes the alloc id for given pon interface, onu id, uni id and alloc id
npujarec5762e2020-01-01 14:08:48 +0530702func (RsrcMgr *OpenOltResourceMgr) RemoveAllocIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, allocID uint32) {
703 allocIDs := RsrcMgr.GetCurrentAllocIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000704 for i := 0; i < len(allocIDs); i++ {
705 if allocIDs[i] == allocID {
706 allocIDs = append(allocIDs[:i], allocIDs[i+1:]...)
707 break
708 }
709 }
npujarec5762e2020-01-01 14:08:48 +0530710 err := RsrcMgr.UpdateAllocIdsForOnu(ctx, intfID, onuID, uniID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000711 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000712 logger.Errorf(ctx, "Failed to Remove Alloc Id For Onu. IntfID %d onuID %d uniID %d allocID %d",
Gamze Abakafee36392019-10-03 11:17:24 +0000713 intfID, onuID, uniID, allocID)
714 }
715}
716
717// RemoveGemPortIDForOnu removes the gem port id for given pon interface, onu id, uni id and gem port id
npujarec5762e2020-01-01 14:08:48 +0530718func (RsrcMgr *OpenOltResourceMgr) RemoveGemPortIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, gemPortID uint32) {
719 gemPortIDs := RsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000720 for i := 0; i < len(gemPortIDs); i++ {
721 if gemPortIDs[i] == gemPortID {
722 gemPortIDs = append(gemPortIDs[:i], gemPortIDs[i+1:]...)
723 break
724 }
725 }
npujarec5762e2020-01-01 14:08:48 +0530726 err := RsrcMgr.UpdateGEMPortIDsForOnu(ctx, intfID, onuID, uniID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000727 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000728 logger.Errorf(ctx, "Failed to Remove Gem Id For Onu. IntfID %d onuID %d uniID %d gemPortId %d",
Gamze Abakafee36392019-10-03 11:17:24 +0000729 intfID, onuID, uniID, gemPortID)
730 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530731}
732
Esin Karamandf392e12020-12-16 13:33:09 +0000733//GetUniPortByPonPortGemPortFromKVStore retrieves onu and uni ID associated with the pon and gem ports.
734func (RsrcMgr *OpenOltResourceMgr) GetUniPortByPonPortGemPortFromKVStore(ctx context.Context, PonPort uint32, GemPort uint32) (uint32, uint32, error) {
735 IntfGEMPortPath := fmt.Sprintf("%d,%d", PonPort, GemPort)
736 logger.Debugf(ctx, "Getting ONU and UNI IDs from the path %s", IntfGEMPortPath)
737 var Data []uint32
738 Value, err := RsrcMgr.KVStore.Get(ctx, IntfGEMPortPath)
739 if err == nil {
740 if Value != nil {
741 Val, _ := ponrmgr.ToByte(Value.Value)
742 if err = json.Unmarshal(Val, &Data); err != nil {
743 logger.Errorw(ctx, "Failed to unmarshal", log.Fields{"error": err})
744 return 0, 0, errors.New("failed to unmarshal the data retrieved")
745 }
746 }
747 } else {
748 logger.Errorf(ctx, "Failed to get data from kvstore for %s", IntfGEMPortPath, err)
749 return 0, 0, errors.New("could not get data")
750 }
751 if len(Data) < 2 {
752 return 0, 0, errors.New("invalid data format")
753 }
754 return Data[0], Data[1], nil
755}
756
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700757// UpdateGEMportsPonportToOnuMapOnKVStore updates onu and uni id associated with the gem port to the kv store
758// This stored information is used when packet_indication is received and we need to derive the ONU Id for which
759// the packet arrived based on the pon_intf and gemport available in the packet_indication
npujarec5762e2020-01-01 14:08:48 +0530760func (RsrcMgr *OpenOltResourceMgr) UpdateGEMportsPonportToOnuMapOnKVStore(ctx context.Context, gemPorts []uint32, PonPort uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700761 onuID uint32, uniID uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530762
Girish Gowdru0c588b22019-04-23 23:24:56 -0400763 /* Update onu and uni id associated with the gem port to the kv store. */
764 var IntfGEMPortPath string
Esin Karamandf392e12020-12-16 13:33:09 +0000765 Data := []uint32{onuID, uniID}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700766 for _, GEM := range gemPorts {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400767 IntfGEMPortPath = fmt.Sprintf("%d,%d", PonPort, GEM)
768 Val, err := json.Marshal(Data)
769 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000770 logger.Error(ctx, "failed to Marshal")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400771 return err
772 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700773
npujarec5762e2020-01-01 14:08:48 +0530774 if err = RsrcMgr.KVStore.Put(ctx, IntfGEMPortPath, Val); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000775 logger.Errorf(ctx, "Failed to update resource %s", IntfGEMPortPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400776 return err
777 }
778 }
779 return nil
Abhilash S.L7f17e402019-03-15 17:40:41 +0530780}
781
Gamze Abakafee36392019-10-03 11:17:24 +0000782// RemoveGEMportPonportToOnuMapOnKVStore removes the relationship between the gem port and pon port
npujarec5762e2020-01-01 14:08:48 +0530783func (RsrcMgr *OpenOltResourceMgr) RemoveGEMportPonportToOnuMapOnKVStore(ctx context.Context, GemPort uint32, PonPort uint32) {
Gamze Abakafee36392019-10-03 11:17:24 +0000784 IntfGEMPortPath := fmt.Sprintf("%d,%d", PonPort, GemPort)
npujarec5762e2020-01-01 14:08:48 +0530785 err := RsrcMgr.KVStore.Delete(ctx, IntfGEMPortPath)
Gamze Abakafee36392019-10-03 11:17:24 +0000786 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000787 logger.Errorf(ctx, "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 +0000788 }
789}
790
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700791// GetGEMPortID gets gem port id for a particular pon port, onu id and uni id and then update the resource map on
792// the KV store with the list of gemport_id allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530793func (RsrcMgr *OpenOltResourceMgr) GetGEMPortID(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700794 uniID uint32, NumOfPorts uint32) ([]uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530795
Girish Gowdru0c588b22019-04-23 23:24:56 -0400796 /* Get gem port id for a particular pon port, onu id
797 and uni id.
798 */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530799
Girish Gowdru0c588b22019-04-23 23:24:56 -0400800 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700801 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530802
Girish Gowdrab77ded92020-04-08 11:45:05 -0700803 RsrcMgr.GemPortIDMgmtLock[ponPort].Lock()
804 defer RsrcMgr.GemPortIDMgmtLock[ponPort].Unlock()
805
npujarec5762e2020-01-01 14:08:48 +0530806 GEMPortList := RsrcMgr.ResourceMgrs[ponPort].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400807 if GEMPortList != nil {
808 return GEMPortList, nil
809 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530810
npujarec5762e2020-01-01 14:08:48 +0530811 GEMPortList, err = RsrcMgr.ResourceMgrs[ponPort].GetResourceID(ctx, ponPort,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400812 ponrmgr.GEMPORT_ID, NumOfPorts)
813 if err != nil && GEMPortList == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000814 logger.Errorf(ctx, "Failed to get gem port id for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400815 return nil, err
816 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530817
Girish Gowdru0c588b22019-04-23 23:24:56 -0400818 // update the resource map on KV store with the list of gemport_id
819 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530820 err = RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400821 GEMPortList)
822 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000823 logger.Errorf(ctx, "Failed to update GEM ports to kv store for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400824 return nil, err
825 }
npujarec5762e2020-01-01 14:08:48 +0530826 _ = RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(ctx, GEMPortList, ponPort,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700827 onuID, uniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400828 return GEMPortList, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530829}
830
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700831// 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 +0530832func (RsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700833 uniID uint32, GEMPortList []uint32) error {
834 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530835 return RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400836 GEMPortList)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530837
838}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700839
840// FreeonuID releases(make free) onu id for a particular pon-port
npujarec5762e2020-01-01 14:08:48 +0530841func (RsrcMgr *OpenOltResourceMgr) FreeonuID(ctx context.Context, intfID uint32, onuID []uint32) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700842
Girish Gowdra38d533d2020-03-30 20:38:51 -0700843 RsrcMgr.OnuIDMgmtLock[intfID].Lock()
Girish Gowdrab77ded92020-04-08 11:45:05 -0700844 defer RsrcMgr.OnuIDMgmtLock[intfID].Unlock()
845
npujarec5762e2020-01-01 14:08:48 +0530846 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID, ponrmgr.ONU_ID, onuID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530847
Girish Gowdru0c588b22019-04-23 23:24:56 -0400848 /* Free onu id for a particular interface.*/
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700849 var IntfonuID string
850 for _, onu := range onuID {
851 IntfonuID = fmt.Sprintf("%d,%d", intfID, onu)
npujarec5762e2020-01-01 14:08:48 +0530852 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfonuID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400853 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530854}
855
Gamze Abakafee36392019-10-03 11:17:24 +0000856// FreeAllocID frees AllocID on the PON resource pool and also frees the allocID association
857// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530858func (RsrcMgr *OpenOltResourceMgr) FreeAllocID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000859 uniID uint32, allocID uint32) {
Girish Gowdrab77ded92020-04-08 11:45:05 -0700860 RsrcMgr.AllocIDMgmtLock[IntfID].Lock()
861 defer RsrcMgr.AllocIDMgmtLock[IntfID].Unlock()
862
npujarec5762e2020-01-01 14:08:48 +0530863 RsrcMgr.RemoveAllocIDForOnu(ctx, IntfID, onuID, uniID, allocID)
Gamze Abakafee36392019-10-03 11:17:24 +0000864 allocIDs := make([]uint32, 0)
865 allocIDs = append(allocIDs, allocID)
npujarec5762e2020-01-01 14:08:48 +0530866 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.ALLOC_ID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000867}
868
869// FreeGemPortID frees GemPortID on the PON resource pool and also frees the gemPortID association
870// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530871func (RsrcMgr *OpenOltResourceMgr) FreeGemPortID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000872 uniID uint32, gemPortID uint32) {
Girish Gowdrab77ded92020-04-08 11:45:05 -0700873 RsrcMgr.GemPortIDMgmtLock[IntfID].Lock()
874 defer RsrcMgr.GemPortIDMgmtLock[IntfID].Unlock()
875
npujarec5762e2020-01-01 14:08:48 +0530876 RsrcMgr.RemoveGemPortIDForOnu(ctx, IntfID, onuID, uniID, gemPortID)
Gamze Abakafee36392019-10-03 11:17:24 +0000877 gemPortIDs := make([]uint32, 0)
878 gemPortIDs = append(gemPortIDs, gemPortID)
npujarec5762e2020-01-01 14:08:48 +0530879 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.GEMPORT_ID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000880}
881
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700882// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id, and the clears the
883// resource map and the onuID associated with (pon_intf_id, gemport_id) tuple,
npujarec5762e2020-01-01 14:08:48 +0530884func (RsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530885
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700886 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530887
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700888 RsrcMgr.AllocIDMgmtLock[intfID].Lock()
Girish Gowdrab77ded92020-04-08 11:45:05 -0700889 AllocIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700890
npujarec5762e2020-01-01 14:08:48 +0530891 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400892 ponrmgr.ALLOC_ID,
893 AllocIDs)
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700894 RsrcMgr.AllocIDMgmtLock[intfID].Unlock()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530895
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700896 RsrcMgr.GemPortIDMgmtLock[intfID].Lock()
npujarec5762e2020-01-01 14:08:48 +0530897 GEMPortIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
898 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400899 ponrmgr.GEMPORT_ID,
900 GEMPortIDs)
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700901 RsrcMgr.GemPortIDMgmtLock[intfID].Unlock()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530902
Girish Gowdru0c588b22019-04-23 23:24:56 -0400903 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
npujarec5762e2020-01-01 14:08:48 +0530904 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400905 // Clear the ONU Id associated with the (pon_intf_id, gemport_id) tuple.
906 for _, GEM := range GEMPortIDs {
npujarec5762e2020-01-01 14:08:48 +0530907 _ = RsrcMgr.KVStore.Delete(ctx, fmt.Sprintf("%d,%d", intfID, GEM))
Girish Gowdru0c588b22019-04-23 23:24:56 -0400908 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530909}
910
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700911// IsFlowOnKvStore checks if the given flowID is present on the kv store
912// Returns true if the flowID is found, otherwise it returns false
913func (RsrcMgr *OpenOltResourceMgr) IsFlowOnKvStore(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32,
914 flowID uint64) bool {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530915
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700916 FlowIDs, err := RsrcMgr.GetCurrentFlowIDsForOnu(ctx, ponIntfID, onuID, uniID)
917 if err != nil {
918 // error logged in the called function
919 return false
920 }
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400921 if FlowIDs != nil {
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700922 logger.Debugw(ctx, "Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "onuID": onuID, "uniID": uniID})
923 for _, id := range FlowIDs {
924 if flowID == id {
925 return true
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400926 }
927 }
928 }
929 return false
930}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400931
salmansiddiqui7ac62132019-08-22 03:58:50 +0000932// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000933// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530934func (RsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) []uint32 {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000935 Path := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000936 var Data []uint32
npujarec5762e2020-01-01 14:08:48 +0530937 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400938 if err == nil {
939 if Value != nil {
940 Val, err := kvstore.ToByte(Value.Value)
941 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000942 logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"error": err})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400943 return Data
944 }
945 if err = json.Unmarshal(Val, &Data); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000946 logger.Error(ctx, "Failed to unmarshal", log.Fields{"error": err})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400947 return Data
948 }
949 }
950 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000951 logger.Errorf(ctx, "Failed to get TP id from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400952 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000953 logger.Debugf(ctx, "Getting TP id %d from path %s", Data, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400954 return Data
955
956}
957
Gamze Abakafee36392019-10-03 11:17:24 +0000958// RemoveTechProfileIDsForOnu deletes all tech profile ids from the KV-Store for the given onu based on the path
959// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530960func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDsForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) error {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000961 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
npujarec5762e2020-01-01 14:08:48 +0530962 if err := RsrcMgr.KVStore.Delete(ctx, IntfOnuUniID); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000963 logger.Errorw(ctx, "Failed to delete techprofile id resource in KV store", log.Fields{"path": IntfOnuUniID})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400964 return err
965 }
966 return nil
967}
968
Gamze Abakafee36392019-10-03 11:17:24 +0000969// RemoveTechProfileIDForOnu deletes a specific tech profile id from the KV-Store for the given onu based on the path
970// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530971func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32, TpID uint32) error {
972 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000973 for i, tpIDInList := range tpIDList {
974 if tpIDInList == TpID {
975 tpIDList = append(tpIDList[:i], tpIDList[i+1:]...)
976 }
977 }
978 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
979 Value, err := json.Marshal(tpIDList)
980 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000981 logger.Error(ctx, "failed to Marshal")
Gamze Abakafee36392019-10-03 11:17:24 +0000982 return err
983 }
npujarec5762e2020-01-01 14:08:48 +0530984 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000985 logger.Errorf(ctx, "Failed to update resource %s", IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000986 return err
987 }
988 return err
989}
990
991// UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
992// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530993func (RsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32,
salmansiddiqui7ac62132019-08-22 03:58:50 +0000994 UniID uint32, TpID uint32) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400995 var Value []byte
996 var err error
997
salmansiddiqui7ac62132019-08-22 03:58:50 +0000998 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000999
npujarec5762e2020-01-01 14:08:48 +05301000 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +00001001 for _, value := range tpIDList {
1002 if value == TpID {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001003 logger.Debugf(ctx, "TpID %d is already in tpIdList for the path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +00001004 return err
1005 }
1006 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001007 logger.Debugf(ctx, "updating tp id %d on path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +00001008 tpIDList = append(tpIDList, TpID)
1009 Value, err = json.Marshal(tpIDList)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001010 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001011 logger.Error(ctx, "failed to Marshal")
Manikkaraj kb1d51442019-07-23 10:41:02 -04001012 return err
1013 }
npujarec5762e2020-01-01 14:08:48 +05301014 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001015 logger.Errorf(ctx, "Failed to update resource %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001016 return err
1017 }
1018 return err
1019}
1020
salmansiddiqui7ac62132019-08-22 03:58:50 +00001021// UpdateMeterIDForOnu updates the meter id in the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +00001022// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +05301023func (RsrcMgr *OpenOltResourceMgr) UpdateMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +00001024 UniID uint32, TpID uint32, MeterConfig *ofp.OfpMeterConfig) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -04001025 var Value []byte
1026 var err error
1027
Gamze Abakafee36392019-10-03 11:17:24 +00001028 IntfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001029 Value, err = json.Marshal(*MeterConfig)
1030 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001031 logger.Error(ctx, "failed to Marshal meter config")
Manikkaraj kb1d51442019-07-23 10:41:02 -04001032 return err
1033 }
npujarec5762e2020-01-01 14:08:48 +05301034 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001035 logger.Errorf(ctx, "Failed to store meter into KV store %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001036 return err
1037 }
1038 return err
1039}
1040
Gamze Abakafee36392019-10-03 11:17:24 +00001041// GetMeterIDForOnu fetches the meter id from the kv store for the given onu based on the path
1042// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +05301043func (RsrcMgr *OpenOltResourceMgr) GetMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +00001044 UniID uint32, TpID uint32) (*ofp.OfpMeterConfig, error) {
1045 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001046 var meterConfig ofp.OfpMeterConfig
npujarec5762e2020-01-01 14:08:48 +05301047 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001048 if err == nil {
1049 if Value != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001050 logger.Debug(ctx, "Found meter in KV store", log.Fields{"Direction": Direction})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001051 Val, er := kvstore.ToByte(Value.Value)
1052 if er != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001053 logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"error": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001054 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -04001055 }
salmansiddiqui7ac62132019-08-22 03:58:50 +00001056 if er = json.Unmarshal(Val, &meterConfig); er != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001057 logger.Error(ctx, "Failed to unmarshal meterconfig", log.Fields{"error": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001058 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -04001059 }
1060 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001061 logger.Debug(ctx, "meter-does-not-exists-in-KVStore")
Manikkaraj kb1d51442019-07-23 10:41:02 -04001062 return nil, err
1063 }
1064 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001065 logger.Errorf(ctx, "Failed to get Meter config from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001066
1067 }
1068 return &meterConfig, err
1069}
1070
Gamze Abakafee36392019-10-03 11:17:24 +00001071// RemoveMeterIDForOnu deletes the meter id from the kV-Store for the given onu based on the path
1072// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +05301073func (RsrcMgr *OpenOltResourceMgr) RemoveMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +00001074 UniID uint32, TpID uint32) error {
1075 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
npujarec5762e2020-01-01 14:08:48 +05301076 if err := RsrcMgr.KVStore.Delete(ctx, Path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001077 logger.Errorf(ctx, "Failed to delete meter id %s from kvstore ", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001078 return err
1079 }
1080 return nil
1081}
salmansiddiqui7ac62132019-08-22 03:58:50 +00001082
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301083//AddGemToOnuGemInfo adds gemport to onugem info kvstore
npujarec5762e2020-01-01 14:08:48 +05301084func (RsrcMgr *OpenOltResourceMgr) AddGemToOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, gemPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301085 var onuGemData []OnuGemInfo
1086 var err error
1087
npujarec5762e2020-01-01 14:08:48 +05301088 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001089 logger.Errorf(ctx, "failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301090 return err
1091 }
1092 if len(onuGemData) == 0 {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001093 logger.Errorw(ctx, "failed to ger Onuid info ", log.Fields{"intfid": intfID, "onuid": onuID})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301094 return err
1095 }
1096
1097 for idx, onugem := range onuGemData {
1098 if onugem.OnuID == onuID {
1099 for _, gem := range onuGemData[idx].GemPorts {
1100 if gem == gemPort {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001101 logger.Debugw(ctx, "Gem already present in onugem info, skpping addition", log.Fields{"gem": gem})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301102 return nil
1103 }
1104 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001105 logger.Debugw(ctx, "Added gem to onugem info", log.Fields{"gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301106 onuGemData[idx].GemPorts = append(onuGemData[idx].GemPorts, gemPort)
1107 break
1108 }
1109 }
npujarec5762e2020-01-01 14:08:48 +05301110 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301111 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001112 logger.Error(ctx, "Failed to add onugem to kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301113 return err
1114 }
1115 return err
1116}
1117
1118//GetOnuGemInfo gets onu gem info from the kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301119func (RsrcMgr *OpenOltResourceMgr) GetOnuGemInfo(ctx context.Context, IntfID uint32) ([]OnuGemInfo, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301120 var onuGemData []OnuGemInfo
1121
npujarec5762e2020-01-01 14:08:48 +05301122 if err := RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001123 logger.Errorf(ctx, "failed to get onuifo for intfid %d", IntfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301124 return nil, err
1125 }
1126
1127 return onuGemData, nil
1128}
1129
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001130// AddOnuGemInfo adds onu info on to the kvstore per interface
1131func (RsrcMgr *OpenOltResourceMgr) AddOnuGemInfo(ctx context.Context, IntfID uint32, onuGem OnuGemInfo) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301132 var onuGemData []OnuGemInfo
1133 var err error
1134
npujarec5762e2020-01-01 14:08:48 +05301135 if err = RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001136 logger.Errorf(ctx, "failed to get onuifo for intfid %d", IntfID)
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001137 return olterrors.NewErrPersistence("get", "OnuGemInfo", uint64(IntfID),
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001138 log.Fields{"onuGem": onuGem, "intfID": IntfID}, err)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301139 }
1140 onuGemData = append(onuGemData, onuGem)
npujarec5762e2020-01-01 14:08:48 +05301141 err = RsrcMgr.ResourceMgrs[IntfID].AddOnuGemInfo(ctx, IntfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301142 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001143 logger.Error(ctx, "Failed to add onugem to kv store")
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001144 return olterrors.NewErrPersistence("set", "OnuGemInfo", uint64(IntfID),
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001145 log.Fields{"onuGemData": onuGemData, "intfID": IntfID}, err)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301146 }
1147
Neha Sharma96b7bf22020-06-15 10:37:32 +00001148 logger.Debugw(ctx, "added onu to onugeminfo", log.Fields{"intf": IntfID, "onugem": onuGem})
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001149 return nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301150}
1151
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301152// 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 +05301153func (RsrcMgr *OpenOltResourceMgr) AddUniPortToOnuInfo(ctx context.Context, intfID uint32, onuID uint32, portNo uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301154 var onuGemData []OnuGemInfo
1155 var err error
1156
npujarec5762e2020-01-01 14:08:48 +05301157 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001158 logger.Errorf(ctx, "failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301159 return
1160 }
1161 for idx, onu := range onuGemData {
1162 if onu.OnuID == onuID {
1163 for _, uni := range onu.UniPorts {
1164 if uni == portNo {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001165 logger.Debugw(ctx, "uni already present in onugem info", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301166 return
1167 }
1168 }
1169 onuGemData[idx].UniPorts = append(onuGemData[idx].UniPorts, portNo)
1170 break
1171 }
1172 }
npujarec5762e2020-01-01 14:08:48 +05301173 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301174 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001175 logger.Errorw(ctx, "Failed to add uin port in onugem to kv store", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301176 return
1177 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301178}
1179
Esin Karaman7fb80c22020-07-16 14:23:33 +00001180//UpdateGemPortForPktIn updates gemport for pkt in path to kvstore, path being intfid, onuid, portno, vlan id, priority bit
npujarec5762e2020-01-01 14:08:48 +05301181func (RsrcMgr *OpenOltResourceMgr) UpdateGemPortForPktIn(ctx context.Context, pktIn PacketInInfoKey, gemPort uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301182
Esin Karaman7fb80c22020-07-16 14:23:33 +00001183 path := fmt.Sprintf(OnuPacketINPath, pktIn.IntfID, pktIn.OnuID, pktIn.LogicalPort, pktIn.VlanID, pktIn.Priority)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301184 Value, err := json.Marshal(gemPort)
1185 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001186 logger.Error(ctx, "Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301187 return
1188 }
npujarec5762e2020-01-01 14:08:48 +05301189 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001190 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"path": path, "value": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301191 return
1192 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001193 logger.Debugw(ctx, "added gem packet in successfully", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301194}
1195
Esin Karaman7fb80c22020-07-16 14:23:33 +00001196// GetGemPortFromOnuPktIn gets the gem port from onu pkt in path, path being intfid, onuid, portno, vlan id, priority bit
1197func (RsrcMgr *OpenOltResourceMgr) GetGemPortFromOnuPktIn(ctx context.Context, packetInInfoKey PacketInInfoKey) (uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301198
1199 var Val []byte
1200 var gemPort uint32
1201
Esin Karaman7fb80c22020-07-16 14:23:33 +00001202 path := fmt.Sprintf(OnuPacketINPath, packetInInfoKey.IntfID, packetInInfoKey.OnuID, packetInInfoKey.LogicalPort,
1203 packetInInfoKey.VlanID, packetInInfoKey.Priority)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301204
npujarec5762e2020-01-01 14:08:48 +05301205 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301206 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001207 logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301208 return uint32(0), err
1209 } else if value == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001210 logger.Debugw(ctx, "No pkt in gem found", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301211 return uint32(0), nil
1212 }
1213
1214 if Val, err = kvstore.ToByte(value.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001215 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301216 return uint32(0), err
1217 }
1218 if err = json.Unmarshal(Val, &gemPort); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001219 logger.Error(ctx, "Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301220 return uint32(0), err
1221 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001222 logger.Debugw(ctx, "found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301223
1224 return gemPort, nil
1225}
1226
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001227//DeletePacketInGemPortForOnu deletes the packet-in gemport for ONU
1228func (RsrcMgr *OpenOltResourceMgr) DeletePacketInGemPortForOnu(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301229
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001230 path := fmt.Sprintf(OnuPacketINPathPrefix, intfID, onuID, logicalPort)
1231 value, err := RsrcMgr.KVStore.List(ctx, path)
Esin Karaman7fb80c22020-07-16 14:23:33 +00001232 if err != nil {
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001233 logger.Errorf(ctx, "failed-to-read-value-from-path-%s", path)
1234 return errors.New("failed-to-read-value-from-path-" + path)
Esin Karaman7fb80c22020-07-16 14:23:33 +00001235 }
Esin Karaman7fb80c22020-07-16 14:23:33 +00001236
1237 //remove them one by one
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001238 for key := range value {
1239 // Formulate the right key path suffix ti be delete
Matteo Scandolodfa7a972020-11-06 13:03:40 -08001240 stringToBeReplaced := fmt.Sprintf(BasePathKvStore, RsrcMgr.KVStore.PathPrefix, RsrcMgr.DeviceID) + "/"
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001241 replacedWith := ""
1242 key = strings.Replace(key, stringToBeReplaced, replacedWith, 1)
1243
1244 logger.Debugf(ctx, "removing-key-%s", key)
Esin Karaman7fb80c22020-07-16 14:23:33 +00001245 if err := RsrcMgr.KVStore.Delete(ctx, key); err != nil {
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001246 logger.Errorf(ctx, "failed-to-remove-resource-%s", key)
Esin Karaman7fb80c22020-07-16 14:23:33 +00001247 return err
1248 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301249 }
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001250
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301251 return nil
1252}
1253
1254// DelOnuGemInfoForIntf deletes the onugem info from kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301255func (RsrcMgr *OpenOltResourceMgr) DelOnuGemInfoForIntf(ctx context.Context, intfID uint32) error {
1256 if err := RsrcMgr.ResourceMgrs[intfID].DelOnuGemInfoForIntf(ctx, intfID); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001257 logger.Errorw(ctx, "failed to delete onu gem info for", log.Fields{"intfid": intfID})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301258 return err
1259 }
1260 return nil
1261}
1262
1263//GetNNIFromKVStore gets NNi intfids from kvstore. path being per device
npujarec5762e2020-01-01 14:08:48 +05301264func (RsrcMgr *OpenOltResourceMgr) GetNNIFromKVStore(ctx context.Context) ([]uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301265
1266 var nni []uint32
1267 var Val []byte
1268
Kent Hagermane6ff1012020-07-14 15:07:53 -04001269 path := NnniIntfID
npujarec5762e2020-01-01 14:08:48 +05301270 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301271 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001272 logger.Error(ctx, "failed to get data from kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301273 return nil, err
1274 }
1275 if value != nil {
1276 if Val, err = kvstore.ToByte(value.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001277 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301278 return nil, err
1279 }
1280 if err = json.Unmarshal(Val, &nni); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001281 logger.Error(ctx, "Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301282 return nil, err
1283 }
1284 }
1285 return nni, err
1286}
1287
1288// AddNNIToKVStore adds Nni interfaces to kvstore, path being per device.
npujarec5762e2020-01-01 14:08:48 +05301289func (RsrcMgr *OpenOltResourceMgr) AddNNIToKVStore(ctx context.Context, nniIntf uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301290 var Value []byte
1291
npujarec5762e2020-01-01 14:08:48 +05301292 nni, err := RsrcMgr.GetNNIFromKVStore(ctx)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301293 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001294 logger.Error(ctx, "failed to fetch nni interfaces from kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301295 return err
1296 }
1297
Kent Hagermane6ff1012020-07-14 15:07:53 -04001298 path := NnniIntfID
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301299 nni = append(nni, nniIntf)
1300 Value, err = json.Marshal(nni)
1301 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001302 logger.Error(ctx, "Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301303 }
npujarec5762e2020-01-01 14:08:48 +05301304 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001305 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"path": path, "value": Value})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301306 return err
1307 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001308 logger.Debugw(ctx, "added nni to kv successfully", log.Fields{"path": path, "nni": nniIntf})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301309 return nil
1310}
1311
1312// DelNNiFromKVStore deletes nni interface list from kv store.
npujarec5762e2020-01-01 14:08:48 +05301313func (RsrcMgr *OpenOltResourceMgr) DelNNiFromKVStore(ctx context.Context) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301314
Kent Hagermane6ff1012020-07-14 15:07:53 -04001315 path := NnniIntfID
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301316
npujarec5762e2020-01-01 14:08:48 +05301317 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001318 logger.Errorw(ctx, "Failed to delete nni interfaces from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301319 return err
1320 }
1321 return nil
1322}
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301323
1324//UpdateFlowIDsForGem updates flow id per gemport
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001325func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDsForGem(ctx context.Context, intf uint32, gem uint32, flowIDs []uint64) error {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301326 var val []byte
1327 path := fmt.Sprintf(FlowIDsForGem, intf)
1328
npujarec5762e2020-01-01 14:08:48 +05301329 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301330 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001331 logger.Error(ctx, "Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301332 return err
1333 }
1334 if flowsForGem == nil {
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001335 flowsForGem = make(map[uint32][]uint64)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301336 }
1337 flowsForGem[gem] = flowIDs
1338 val, err = json.Marshal(flowsForGem)
1339 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001340 logger.Error(ctx, "Failed to marshal data", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301341 return err
1342 }
Girish Gowdrab77ded92020-04-08 11:45:05 -07001343
npujarec5762e2020-01-01 14:08:48 +05301344 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001345 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301346 return err
1347 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001348 logger.Debugw(ctx, "added flowid list for gem to kv successfully", log.Fields{"path": path, "flowidlist": flowsForGem[gem]})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301349 return nil
1350}
1351
1352//DeleteFlowIDsForGem deletes the flowID list entry per gem from kvstore.
npujarec5762e2020-01-01 14:08:48 +05301353func (RsrcMgr *OpenOltResourceMgr) DeleteFlowIDsForGem(ctx context.Context, intf uint32, gem uint32) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301354 path := fmt.Sprintf(FlowIDsForGem, intf)
1355 var val []byte
1356
npujarec5762e2020-01-01 14:08:48 +05301357 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301358 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001359 logger.Error(ctx, "Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301360 return
1361 }
1362 if flowsForGem == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001363 logger.Error(ctx, "No flowids found ", log.Fields{"intf": intf, "gemport": gem})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301364 return
1365 }
1366 // once we get the flows per gem map from kv , just delete the gem entry from the map
1367 delete(flowsForGem, gem)
1368 // once gem entry is deleted update the kv store.
1369 val, err = json.Marshal(flowsForGem)
1370 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001371 logger.Error(ctx, "Failed to marshal data", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301372 return
1373 }
Girish Gowdra38d533d2020-03-30 20:38:51 -07001374
npujarec5762e2020-01-01 14:08:48 +05301375 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001376 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301377 }
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301378}
1379
1380//GetFlowIDsGemMapForInterface gets flowids per gemport and interface
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001381func (RsrcMgr *OpenOltResourceMgr) GetFlowIDsGemMapForInterface(ctx context.Context, intf uint32) (map[uint32][]uint64, error) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301382 path := fmt.Sprintf(FlowIDsForGem, intf)
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001383 var flowsForGem map[uint32][]uint64
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301384 var val []byte
npujarec5762e2020-01-01 14:08:48 +05301385 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301386 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001387 logger.Error(ctx, "failed to get data from kv store")
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301388 return nil, err
1389 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001390 if value != nil && value.Value != nil {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301391 if val, err = kvstore.ToByte(value.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001392 logger.Error(ctx, "Failed to convert to byte array ", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301393 return nil, err
1394 }
1395 if err = json.Unmarshal(val, &flowsForGem); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001396 logger.Error(ctx, "Failed to unmarshall", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301397 return nil, err
1398 }
1399 }
1400 return flowsForGem, nil
1401}
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301402
1403//DeleteIntfIDGempMapPath deletes the intf id path used to store flow ids per gem to kvstore.
npujarec5762e2020-01-01 14:08:48 +05301404func (RsrcMgr *OpenOltResourceMgr) DeleteIntfIDGempMapPath(ctx context.Context, intf uint32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301405 path := fmt.Sprintf(FlowIDsForGem, intf)
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001406
npujarec5762e2020-01-01 14:08:48 +05301407 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001408 logger.Errorw(ctx, "Failed to delete nni interfaces from kv store", log.Fields{"path": path})
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301409 }
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301410}
1411
1412// RemoveResourceMap Clear resource map associated with (intfid, onuid, uniid) tuple.
npujarec5762e2020-01-01 14:08:48 +05301413func (RsrcMgr *OpenOltResourceMgr) RemoveResourceMap(ctx context.Context, intfID uint32, onuID int32, uniID int32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301414 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +05301415 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301416}
Esin Karamanccb714b2019-11-29 15:02:06 +00001417
1418//GetMcastQueuePerInterfaceMap gets multicast queue info per pon interface
npujarec5762e2020-01-01 14:08:48 +05301419func (RsrcMgr *OpenOltResourceMgr) GetMcastQueuePerInterfaceMap(ctx context.Context) (map[uint32][]uint32, error) {
Kent Hagermane6ff1012020-07-14 15:07:53 -04001420 path := McastQueuesForIntf
Esin Karamanccb714b2019-11-29 15:02:06 +00001421 var mcastQueueToIntfMap map[uint32][]uint32
1422 var val []byte
1423
npujarec5762e2020-01-01 14:08:48 +05301424 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001425 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001426 logger.Error(ctx, "failed to get data from kv store")
Esin Karamanccb714b2019-11-29 15:02:06 +00001427 return nil, err
1428 }
1429 if kvPair != nil && kvPair.Value != nil {
1430 if val, err = kvstore.ToByte(kvPair.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001431 logger.Error(ctx, "Failed to convert to byte array ", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001432 return nil, err
1433 }
1434 if err = json.Unmarshal(val, &mcastQueueToIntfMap); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001435 logger.Error(ctx, "Failed to unmarshall ", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001436 return nil, err
1437 }
1438 }
1439 return mcastQueueToIntfMap, nil
1440}
1441
1442//AddMcastQueueForIntf adds multicast queue for pon interface
npujarec5762e2020-01-01 14:08:48 +05301443func (RsrcMgr *OpenOltResourceMgr) AddMcastQueueForIntf(ctx context.Context, intf uint32, gem uint32, servicePriority uint32) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001444 var val []byte
Kent Hagermane6ff1012020-07-14 15:07:53 -04001445 path := McastQueuesForIntf
Esin Karamanccb714b2019-11-29 15:02:06 +00001446
npujarec5762e2020-01-01 14:08:48 +05301447 mcastQueues, err := RsrcMgr.GetMcastQueuePerInterfaceMap(ctx)
Esin Karamanccb714b2019-11-29 15:02:06 +00001448 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001449 logger.Errorw(ctx, "Failed to get multicast queue info for interface", log.Fields{"error": err, "intf": intf})
Esin Karamanccb714b2019-11-29 15:02:06 +00001450 return err
1451 }
1452 if mcastQueues == nil {
1453 mcastQueues = make(map[uint32][]uint32)
1454 }
1455 mcastQueues[intf] = []uint32{gem, servicePriority}
1456 if val, err = json.Marshal(mcastQueues); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001457 logger.Errorw(ctx, "Failed to marshal data", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001458 return err
1459 }
npujarec5762e2020-01-01 14:08:48 +05301460 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001461 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Esin Karamanccb714b2019-11-29 15:02:06 +00001462 return err
1463 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001464 logger.Debugw(ctx, "added multicast queue info to KV store successfully", log.Fields{"path": path, "mcastQueueInfo": mcastQueues[intf], "interfaceId": intf})
Esin Karamanccb714b2019-11-29 15:02:06 +00001465 return nil
1466}
1467
1468//AddFlowGroupToKVStore adds flow group into KV store
npujarec5762e2020-01-01 14:08:48 +05301469func (RsrcMgr *OpenOltResourceMgr) AddFlowGroupToKVStore(ctx context.Context, groupEntry *ofp.OfpGroupEntry, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001470 var Value []byte
1471 var err error
1472 var path string
1473 if cached {
1474 path = fmt.Sprintf(FlowGroupCached, groupEntry.Desc.GroupId)
1475 } else {
1476 path = fmt.Sprintf(FlowGroup, groupEntry.Desc.GroupId)
1477 }
1478 //build group info object
1479 var outPorts []uint32
1480 for _, ofBucket := range groupEntry.Desc.Buckets {
1481 for _, ofAction := range ofBucket.Actions {
1482 if ofAction.Type == ofp.OfpActionType_OFPAT_OUTPUT {
1483 outPorts = append(outPorts, ofAction.GetOutput().Port)
1484 }
1485 }
1486 }
1487 groupInfo := GroupInfo{
1488 GroupID: groupEntry.Desc.GroupId,
1489 OutPorts: outPorts,
1490 }
1491
1492 Value, err = json.Marshal(groupInfo)
1493
1494 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001495 logger.Error(ctx, "failed to Marshal flow group object")
Esin Karamanccb714b2019-11-29 15:02:06 +00001496 return err
1497 }
1498
npujarec5762e2020-01-01 14:08:48 +05301499 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001500 logger.Errorf(ctx, "Failed to update resource %s", path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001501 return err
1502 }
1503 return nil
1504}
1505
1506//RemoveFlowGroupFromKVStore removes flow group from KV store
Esin Karamand519bbf2020-07-01 11:16:03 +00001507func (RsrcMgr *OpenOltResourceMgr) RemoveFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001508 var path string
1509 if cached {
1510 path = fmt.Sprintf(FlowGroupCached, groupID)
1511 } else {
1512 path = fmt.Sprintf(FlowGroup, groupID)
1513 }
npujarec5762e2020-01-01 14:08:48 +05301514 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001515 logger.Errorf(ctx, "Failed to remove resource %s due to %s", path, err)
Esin Karamand519bbf2020-07-01 11:16:03 +00001516 return err
Esin Karamanccb714b2019-11-29 15:02:06 +00001517 }
Esin Karamand519bbf2020-07-01 11:16:03 +00001518 return nil
Esin Karamanccb714b2019-11-29 15:02:06 +00001519}
1520
1521//GetFlowGroupFromKVStore fetches flow group from the KV store. Returns (false, {} error) if any problem occurs during
1522//fetching the data. Returns (true, groupInfo, nil) if the group is fetched successfully.
1523// Returns (false, {}, nil) if the group does not exists in the KV store.
npujarec5762e2020-01-01 14:08:48 +05301524func (RsrcMgr *OpenOltResourceMgr) GetFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) (bool, GroupInfo, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001525 var groupInfo GroupInfo
1526 var path string
1527 if cached {
1528 path = fmt.Sprintf(FlowGroupCached, groupID)
1529 } else {
1530 path = fmt.Sprintf(FlowGroup, groupID)
1531 }
npujarec5762e2020-01-01 14:08:48 +05301532 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001533 if err != nil {
1534 return false, groupInfo, err
1535 }
1536 if kvPair != nil && kvPair.Value != nil {
1537 Val, err := kvstore.ToByte(kvPair.Value)
1538 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001539 logger.Errorw(ctx, "Failed to convert flow group into byte array", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001540 return false, groupInfo, err
1541 }
1542 if err = json.Unmarshal(Val, &groupInfo); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001543 logger.Errorw(ctx, "Failed to unmarshal", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001544 return false, groupInfo, err
1545 }
1546 return true, groupInfo, nil
1547 }
1548 return false, groupInfo, nil
1549}
Girish Gowdraa09aeab2020-09-14 16:30:52 -07001550
1551// toByte converts an interface value to a []byte. The interface should either be of
1552// a string type or []byte. Otherwise, an error is returned.
1553func toByte(value interface{}) ([]byte, error) {
1554 switch t := value.(type) {
1555 case []byte:
1556 return value.([]byte), nil
1557 case string:
1558 return []byte(value.(string)), nil
1559 default:
1560 return nil, fmt.Errorf("unexpected-type-%T", t)
1561 }
1562}
1563
1564func checkForFlowIDInList(FlowIDList []uint64, FlowID uint64) (bool, uint64) {
1565 /*
1566 Check for a flow id in a given list of flow IDs.
1567 :param FLowIDList: List of Flow IDs
1568 :param FlowID: Flowd to check in the list
1569 : return true and the index if present false otherwise.
1570 */
1571
1572 for idx := range FlowIDList {
1573 if FlowID == FlowIDList[idx] {
1574 return true, uint64(idx)
1575 }
1576 }
1577 return false, 0
1578}