blob: 0504eaf3275800f5ec0432fc65180d438783832b [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 Gowdra38d533d2020-03-30 20:38:51 -070026 "sync"
Neha Sharmacc656962020-04-14 14:26:11 +000027 "time"
Abhilash S.L7f17e402019-03-15 17:40:41 +053028
Kent Hagermane6ff1012020-07-14 15:07:53 -040029 "github.com/opencord/voltha-openolt-adapter/internal/pkg/olterrors"
30
Esin Karamanccb714b2019-11-29 15:02:06 +000031 "github.com/opencord/voltha-lib-go/v3/pkg/db"
32 "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
33 "github.com/opencord/voltha-lib-go/v3/pkg/log"
34 ponrmgr "github.com/opencord/voltha-lib-go/v3/pkg/ponresourcemanager"
35 ofp "github.com/opencord/voltha-protos/v3/go/openflow_13"
36 "github.com/opencord/voltha-protos/v3/go/openolt"
Abhilash S.L7f17e402019-03-15 17:40:41 +053037)
38
salmansiddiqui7ac62132019-08-22 03:58:50 +000039const (
40 // KvstoreTimeout specifies the time out for KV Store Connection
Neha Sharmacc656962020-04-14 14:26:11 +000041 KvstoreTimeout = 5 * time.Second
salmansiddiqui7ac62132019-08-22 03:58:50 +000042 // BasePathKvStore - service/voltha/openolt/<device_id>
43 BasePathKvStore = "service/voltha/openolt/{%s}"
Gamze Abakafee36392019-10-03 11:17:24 +000044 // TpIDPathSuffix - <(pon_id, onu_id, uni_id)>/tp_id
45 TpIDPathSuffix = "{%d,%d,%d}/tp_id"
46 //MeterIDPathSuffix - <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
47 MeterIDPathSuffix = "{%d,%d,%d}/{%d}/meter_id/{%s}"
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053048 //NnniIntfID - nniintfids
49 NnniIntfID = "nniintfids"
Esin Karaman7fb80c22020-07-16 14:23:33 +000050 //OnuPacketINPathPrefix to be used as prefix for keys to be used to store packet-in gem ports
51 OnuPacketINPathPrefix = "onu_packetin/{%d,%d,%d"
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053052 // OnuPacketINPath path on the kvstore to store packetin gemport,which will be used for packetin, pcketout
Esin Karaman7fb80c22020-07-16 14:23:33 +000053 //format: onu_packetin/<intfid>,<onuid>,<logicalport>,<vlanId>,<priority>
54 OnuPacketINPath = OnuPacketINPathPrefix + ",%d,%d}"
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +053055 //FlowIDsForGem flowids_per_gem/<intfid>
56 FlowIDsForGem = "flowids_per_gem/{%d}"
Esin Karamanccb714b2019-11-29 15:02:06 +000057 //McastQueuesForIntf multicast queues for pon interfaces
58 McastQueuesForIntf = "mcast_qs_for_int"
59 //FlowGroup flow_groups/<flow_group_id>
60 // A group is stored under this path on the KV store after it has been installed to the device.
61 // It should also be deleted after it has been removed from the device accordingly.
62 FlowGroup = "flow_groups/{%d}"
63 //FlowGroupCached flow_groups_cached/<flow_group_id>
64 // When a group add request received, we create the group without setting any members to it since we cannot
65 // set any members to a group until it is associated with a multicast flow. It is a BAL limitation.
66 // When the related multicast flow has been created we perform set members operation for the group.
67 // That is why we need to keep the members of a group until the multicast flow creation request comes.
68 // We preserve the groups under "FlowGroupsCached" directory in the KV store temporarily. Having set members,
69 // we remove the group from the cached group store.
70 FlowGroupCached = "flow_groups_cached/{%d}"
salmansiddiqui7ac62132019-08-22 03:58:50 +000071)
Abhilash S.L7f17e402019-03-15 17:40:41 +053072
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070073// FlowInfo holds the flow information
Abhilash S.L8ee90712019-04-29 16:24:22 +053074type FlowInfo struct {
75 Flow *openolt.Flow
76 FlowStoreCookie uint64
77 FlowCategory string
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053078 LogicalFlowID uint64
79}
80
81// OnuGemInfo holds onu information along with gem port list and uni port list
82type OnuGemInfo struct {
83 OnuID uint32
84 SerialNumber string
85 IntfID uint32
86 GemPorts []uint32
87 UniPorts []uint32
88}
89
90// PacketInInfoKey is the key for packet in gemport
91type PacketInInfoKey struct {
92 IntfID uint32
93 OnuID uint32
94 LogicalPort uint32
Esin Karaman7fb80c22020-07-16 14:23:33 +000095 VlanID uint16
96 Priority uint8
Abhilash S.L8ee90712019-04-29 16:24:22 +053097}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070098
Esin Karamanccb714b2019-11-29 15:02:06 +000099// GroupInfo holds group information
100type GroupInfo struct {
101 GroupID uint32
102 OutPorts []uint32
103}
104
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700105// OpenOltResourceMgr holds resource related information as provided below for each field
Abhilash S.L7f17e402019-03-15 17:40:41 +0530106type OpenOltResourceMgr struct {
Neha Sharma3f221ae2020-04-29 19:02:12 +0000107 DeviceID string // OLT device id
108 Address string // Host and port of the kv store to connect to
109 Args string // args
110 KVStore *db.Backend // backend kv store connection handle
111 DeviceType string
112 DevInfo *openolt.DeviceInfo // device information
Girish Gowdru0c588b22019-04-23 23:24:56 -0400113 // array of pon resource managers per interface technology
114 ResourceMgrs map[uint32]*ponrmgr.PONResourceManager
Girish Gowdra38d533d2020-03-30 20:38:51 -0700115
116 // This protects concurrent gemport_id allocate/delete calls on a per PON port basis
117 GemPortIDMgmtLock []sync.RWMutex
118 // This protects concurrent alloc_id allocate/delete calls on a per PON port basis
119 AllocIDMgmtLock []sync.RWMutex
120 // This protects concurrent onu_id allocate/delete calls on a per PON port basis
121 OnuIDMgmtLock []sync.RWMutex
122 // This protects concurrent flow_id allocate/delete calls. We do not need this on a
123 // per PON port basis as flow IDs are unique across the OLT.
124 FlowIDMgmtLock sync.RWMutex
125
126 // This protects concurrent access to flowids_per_gem info stored on KV store
127 flowIDToGemInfoLock sync.RWMutex
Abhilash S.L7f17e402019-03-15 17:40:41 +0530128}
129
Neha Sharma96b7bf22020-06-15 10:37:32 +0000130func newKVClient(ctx context.Context, storeType string, address string, timeout time.Duration) (kvstore.Client, error) {
131 logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400132 switch storeType {
133 case "consul":
Neha Sharma96b7bf22020-06-15 10:37:32 +0000134 return kvstore.NewConsulClient(ctx, address, timeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400135 case "etcd":
Neha Sharma96b7bf22020-06-15 10:37:32 +0000136 return kvstore.NewEtcdClient(ctx, address, timeout, log.FatalLevel)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400137 }
138 return nil, errors.New("unsupported-kv-store")
Abhilash S.L7f17e402019-03-15 17:40:41 +0530139}
140
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700141// SetKVClient sets the KV client and return a kv backend
Neha Sharma96b7bf22020-06-15 10:37:32 +0000142func SetKVClient(ctx context.Context, backend string, addr string, DeviceID string) *db.Backend {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400143 // TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
144 // issue between kv store and backend , core is not calling NewBackend directly
Neha Sharma96b7bf22020-06-15 10:37:32 +0000145 kvClient, err := newKVClient(ctx, backend, addr, KvstoreTimeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400146 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000147 logger.Fatalw(ctx, "Failed to init KV client\n", log.Fields{"err": err})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400148 return nil
149 }
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700150
sbarbaria8910ba2019-11-05 10:12:23 -0500151 kvbackend := &db.Backend{
Girish Gowdru0c588b22019-04-23 23:24:56 -0400152 Client: kvClient,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700153 StoreType: backend,
Neha Sharma3f221ae2020-04-29 19:02:12 +0000154 Address: addr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700155 Timeout: KvstoreTimeout,
156 PathPrefix: fmt.Sprintf(BasePathKvStore, DeviceID)}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530157
Girish Gowdru0c588b22019-04-23 23:24:56 -0400158 return kvbackend
Abhilash S.L7f17e402019-03-15 17:40:41 +0530159}
160
Gamze Abakafee36392019-10-03 11:17:24 +0000161// NewResourceMgr init a New resource manager instance which in turn instantiates pon resource manager
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700162// instances according to technology. Initializes the default resource ranges for all
163// the resources.
Neha Sharma3f221ae2020-04-29 19:02:12 +0000164func NewResourceMgr(ctx context.Context, deviceID string, KVStoreAddress string, kvStoreType string, deviceType string, devInfo *openolt.DeviceInfo) *OpenOltResourceMgr {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400165 var ResourceMgr OpenOltResourceMgr
Neha Sharma96b7bf22020-06-15 10:37:32 +0000166 logger.Debugf(ctx, "Init new resource manager , address: %s, deviceid: %s", KVStoreAddress, deviceID)
Neha Sharma3f221ae2020-04-29 19:02:12 +0000167 ResourceMgr.Address = KVStoreAddress
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700168 ResourceMgr.DeviceType = deviceType
169 ResourceMgr.DevInfo = devInfo
Girish Gowdra38d533d2020-03-30 20:38:51 -0700170 NumPONPorts := devInfo.GetPonPorts()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530171
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700172 Backend := kvStoreType
Neha Sharma96b7bf22020-06-15 10:37:32 +0000173 ResourceMgr.KVStore = SetKVClient(ctx, Backend, ResourceMgr.Address, deviceID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400174 if ResourceMgr.KVStore == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000175 logger.Error(ctx, "Failed to setup KV store")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400176 }
177 Ranges := make(map[string]*openolt.DeviceInfo_DeviceResourceRanges)
178 RsrcMgrsByTech := make(map[string]*ponrmgr.PONResourceManager)
179 ResourceMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530180
Girish Gowdra38d533d2020-03-30 20:38:51 -0700181 ResourceMgr.AllocIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
182 ResourceMgr.GemPortIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
183 ResourceMgr.OnuIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
184
Girish Gowdru0c588b22019-04-23 23:24:56 -0400185 // TODO self.args = registry('main').get_args()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530186
Girish Gowdru0c588b22019-04-23 23:24:56 -0400187 /*
188 If a legacy driver returns protobuf without any ranges,s synthesize one from
Gamze Abakafee36392019-10-03 11:17:24 +0000189 the legacy global per-device information. This, in theory, is temporary until
Girish Gowdru0c588b22019-04-23 23:24:56 -0400190 the legacy drivers are upgrade to support pool ranges.
191 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700192 if devInfo.Ranges == nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400193 var ranges openolt.DeviceInfo_DeviceResourceRanges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700194 ranges.Technology = devInfo.GetTechnology()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530195
Girish Gowdru0c588b22019-04-23 23:24:56 -0400196 var index uint32
197 for index = 0; index < NumPONPorts; index++ {
198 ranges.IntfIds = append(ranges.IntfIds, index)
199 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530200
Abhilash S.L8ee90712019-04-29 16:24:22 +0530201 var Pool openolt.DeviceInfo_DeviceResourceRanges_Pool
Girish Gowdru0c588b22019-04-23 23:24:56 -0400202 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700203 Pool.Start = devInfo.OnuIdStart
204 Pool.End = devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400205 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
cbabuabf02352019-10-15 13:14:56 +0200206 onuPool := Pool
207 ranges.Pools = append(ranges.Pools, &onuPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530208
Girish Gowdru0c588b22019-04-23 23:24:56 -0400209 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700210 Pool.Start = devInfo.AllocIdStart
211 Pool.End = devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400212 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200213 allocPool := Pool
214 ranges.Pools = append(ranges.Pools, &allocPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530215
Girish Gowdru0c588b22019-04-23 23:24:56 -0400216 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700217 Pool.Start = devInfo.GemportIdStart
218 Pool.End = devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400219 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200220 gemPool := Pool
221 ranges.Pools = append(ranges.Pools, &gemPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530222
Girish Gowdru0c588b22019-04-23 23:24:56 -0400223 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700224 Pool.Start = devInfo.FlowIdStart
225 Pool.End = devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400226 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
Abhilash S.L8ee90712019-04-29 16:24:22 +0530227 ranges.Pools = append(ranges.Pools, &Pool)
228 // Add to device info
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700229 devInfo.Ranges = append(devInfo.Ranges, &ranges)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400230 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530231
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700232 // Create a separate Resource Manager instance for each range. This assumes that
Girish Gowdru0c588b22019-04-23 23:24:56 -0400233 // each technology is represented by only a single range
234 var GlobalPONRsrcMgr *ponrmgr.PONResourceManager
235 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700236 for _, TechRange := range devInfo.Ranges {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400237 technology := TechRange.Technology
Neha Sharma96b7bf22020-06-15 10:37:32 +0000238 logger.Debugf(ctx, "Device info technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400239 Ranges[technology] = TechRange
Neha Sharma96b7bf22020-06-15 10:37:32 +0000240
241 RsrcMgrsByTech[technology], err = ponrmgr.NewPONResourceManager(ctx, technology, deviceType, deviceID,
Neha Sharma3f221ae2020-04-29 19:02:12 +0000242 Backend, ResourceMgr.Address)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400243 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000244 logger.Errorf(ctx, "Failed to create pon resource manager instance for technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400245 return nil
246 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700247 // resource_mgrs_by_tech[technology] = resource_mgr
Girish Gowdru0c588b22019-04-23 23:24:56 -0400248 if GlobalPONRsrcMgr == nil {
249 GlobalPONRsrcMgr = RsrcMgrsByTech[technology]
250 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700251 for _, IntfID := range TechRange.IntfIds {
252 ResourceMgr.ResourceMgrs[uint32(IntfID)] = RsrcMgrsByTech[technology]
Girish Gowdru0c588b22019-04-23 23:24:56 -0400253 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700254 // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
npujarec5762e2020-01-01 14:08:48 +0530255 InitializeDeviceResourceRangeAndPool(ctx, RsrcMgrsByTech[technology], GlobalPONRsrcMgr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700256 TechRange, devInfo)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400257 }
258 // After we have initialized resource ranges, initialize the
259 // resource pools accordingly.
260 for _, PONRMgr := range RsrcMgrsByTech {
npujarec5762e2020-01-01 14:08:48 +0530261 _ = PONRMgr.InitDeviceResourcePool(ctx)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400262 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000263 logger.Info(ctx, "Initialization of resource manager success!")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400264 return &ResourceMgr
Abhilash S.L7f17e402019-03-15 17:40:41 +0530265}
266
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700267// InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply
268// device specific information. If KV doesn't exist
269// or is broader than the device, the device's information will
270// dictate the range limits
npujarec5762e2020-01-01 14:08:48 +0530271func InitializeDeviceResourceRangeAndPool(ctx context.Context, ponRMgr *ponrmgr.PONResourceManager, globalPONRMgr *ponrmgr.PONResourceManager,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700272 techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530273
Girish Gowdru0c588b22019-04-23 23:24:56 -0400274 // init the resource range pool according to the sharing type
Abhilash S.L7f17e402019-03-15 17:40:41 +0530275
Neha Sharma96b7bf22020-06-15 10:37:32 +0000276 logger.Debugf(ctx, "Resource range pool init for technology %s", ponRMgr.Technology)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700277 // first load from KV profiles
npujarec5762e2020-01-01 14:08:48 +0530278 status := ponRMgr.InitResourceRangesFromKVStore(ctx)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700279 if !status {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000280 logger.Debugf(ctx, "Failed to load resource ranges from KV store for tech %s", ponRMgr.Technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400281 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530282
Girish Gowdru0c588b22019-04-23 23:24:56 -0400283 /*
284 Then apply device specific information. If KV doesn't exist
Gamze Abakafee36392019-10-03 11:17:24 +0000285 or is broader than the device, the device's information will
Girish Gowdru0c588b22019-04-23 23:24:56 -0400286 dictate the range limits
287 */
Neha Sharma96b7bf22020-06-15 10:37:32 +0000288 logger.Debugw(ctx, "Using device info to init pon resource ranges", log.Fields{"Tech": ponRMgr.Technology})
Abhilash S.L7f17e402019-03-15 17:40:41 +0530289
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700290 ONUIDStart := devInfo.OnuIdStart
291 ONUIDEnd := devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400292 ONUIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
293 ONUIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700294 AllocIDStart := devInfo.AllocIdStart
295 AllocIDEnd := devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400296 AllocIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
297 AllocIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700298 GEMPortIDStart := devInfo.GemportIdStart
299 GEMPortIDEnd := devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400300 GEMPortIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
301 GEMPortIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700302 FlowIDStart := devInfo.FlowIdStart
303 FlowIDEnd := devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400304 FlowIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
305 FlowIDSharedPoolID := uint32(0)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530306
Girish Gowdru0c588b22019-04-23 23:24:56 -0400307 var FirstIntfPoolID uint32
308 var SharedPoolID uint32
Abhilash S.L7f17e402019-03-15 17:40:41 +0530309
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400310 /*
311 * 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 -0700312 * if resources are shared across interfaces then SharedPoolID is given a positive number.
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400313 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700314 for _, FirstIntfPoolID = range techRange.IntfIds {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400315 // skip the intf id 0
316 if FirstIntfPoolID == 0 {
317 continue
318 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400319 break
320 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530321
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700322 for _, RangePool := range techRange.Pools {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400323 if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400324 SharedPoolID = FirstIntfPoolID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400325 } else if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_SAME_TECH {
326 SharedPoolID = FirstIntfPoolID
327 } else {
328 SharedPoolID = 0
329 }
330 if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID {
331 ONUIDStart = RangePool.Start
332 ONUIDEnd = RangePool.End
333 ONUIDShared = RangePool.Sharing
334 ONUIDSharedPoolID = SharedPoolID
335 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID {
336 AllocIDStart = RangePool.Start
337 AllocIDEnd = RangePool.End
338 AllocIDShared = RangePool.Sharing
339 AllocIDSharedPoolID = SharedPoolID
340 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID {
341 GEMPortIDStart = RangePool.Start
342 GEMPortIDEnd = RangePool.End
343 GEMPortIDShared = RangePool.Sharing
344 GEMPortIDSharedPoolID = SharedPoolID
345 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID {
346 FlowIDStart = RangePool.Start
347 FlowIDEnd = RangePool.End
348 FlowIDShared = RangePool.Sharing
349 FlowIDSharedPoolID = SharedPoolID
350 }
351 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530352
Neha Sharma96b7bf22020-06-15 10:37:32 +0000353 logger.Debugw(ctx, "Device info init", log.Fields{"technology": techRange.Technology,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400354 "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd, "onu_id_shared_pool_id": ONUIDSharedPoolID,
355 "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd,
356 "alloc_id_shared_pool_id": AllocIDSharedPoolID,
357 "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd,
358 "gemport_id_shared_pool_id": GEMPortIDSharedPoolID,
359 "flow_id_start": FlowIDStart,
360 "flow_id_end_idx": FlowIDEnd,
361 "flow_id_shared_pool_id": FlowIDSharedPoolID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700362 "intf_ids": techRange.IntfIds,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400363 "uni_id_start": 0,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700364 "uni_id_end_idx": 1, /*MaxUNIIDperONU()*/
365 })
Abhilash S.L7f17e402019-03-15 17:40:41 +0530366
Neha Sharma96b7bf22020-06-15 10:37:32 +0000367 ponRMgr.InitDefaultPONResourceRanges(ctx, ONUIDStart, ONUIDEnd, ONUIDSharedPoolID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400368 AllocIDStart, AllocIDEnd, AllocIDSharedPoolID,
369 GEMPortIDStart, GEMPortIDEnd, GEMPortIDSharedPoolID,
370 FlowIDStart, FlowIDEnd, FlowIDSharedPoolID, 0, 1,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700371 devInfo.PonPorts, techRange.IntfIds)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530372
Girish Gowdru0c588b22019-04-23 23:24:56 -0400373 // For global sharing, make sure to refresh both local and global resource manager instances' range
Abhilash S.L7f17e402019-03-15 17:40:41 +0530374
Girish Gowdru0c588b22019-04-23 23:24:56 -0400375 if ONUIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000376 globalPONRMgr.UpdateRanges(ctx, ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400377 "", 0, nil)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000378 ponRMgr.UpdateRanges(ctx, ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700379 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400380 }
381 if AllocIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000382 globalPONRMgr.UpdateRanges(ctx, ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400383 "", 0, nil)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530384
Neha Sharma96b7bf22020-06-15 10:37:32 +0000385 ponRMgr.UpdateRanges(ctx, ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700386 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400387 }
388 if GEMPortIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000389 globalPONRMgr.UpdateRanges(ctx, ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400390 "", 0, nil)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000391 ponRMgr.UpdateRanges(ctx, ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700392 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400393 }
394 if FlowIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000395 globalPONRMgr.UpdateRanges(ctx, ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400396 "", 0, nil)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000397 ponRMgr.UpdateRanges(ctx, ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700398 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400399 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530400
Girish Gowdru0c588b22019-04-23 23:24:56 -0400401 // Make sure loaded range fits the platform bit encoding ranges
Neha Sharma96b7bf22020-06-15 10:37:32 +0000402 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 +0530403}
404
Devmalya Paul495b94a2019-08-27 19:42:00 -0400405// Delete clears used resources for the particular olt device being deleted
npujarec5762e2020-01-01 14:08:48 +0530406func (RsrcMgr *OpenOltResourceMgr) Delete(ctx context.Context) error {
Devmalya Paul495b94a2019-08-27 19:42:00 -0400407 /* TODO
408 def __del__(self):
409 self.log.info("clearing-device-resource-pool")
410 for key, resource_mgr in self.resource_mgrs.iteritems():
411 resource_mgr.clear_device_resource_pool()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530412
Devmalya Paul495b94a2019-08-27 19:42:00 -0400413 def assert_pon_id_limit(self, pon_intf_id):
414 assert pon_intf_id in self.resource_mgrs
Abhilash S.L7f17e402019-03-15 17:40:41 +0530415
Devmalya Paul495b94a2019-08-27 19:42:00 -0400416 def assert_onu_id_limit(self, pon_intf_id, onu_id):
417 self.assert_pon_id_limit(pon_intf_id)
418 self.resource_mgrs[pon_intf_id].assert_resource_limits(onu_id, PONResourceManager.ONU_ID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530419
Devmalya Paul495b94a2019-08-27 19:42:00 -0400420 @property
421 def max_uni_id_per_onu(self):
422 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 +0530423
Devmalya Paul495b94a2019-08-27 19:42:00 -0400424 def assert_uni_id_limit(self, pon_intf_id, onu_id, uni_id):
425 self.assert_onu_id_limit(pon_intf_id, onu_id)
426 self.resource_mgrs[pon_intf_id].assert_resource_limits(uni_id, PONResourceManager.UNI_ID)
427 */
428 for _, rsrcMgr := range RsrcMgr.ResourceMgrs {
npujarec5762e2020-01-01 14:08:48 +0530429 if err := rsrcMgr.ClearDeviceResourcePool(ctx); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000430 logger.Debug(ctx, "Failed to clear device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400431 return err
432 }
433 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000434 logger.Debug(ctx, "Cleared device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400435 return nil
436}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530437
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700438// GetONUID returns the available OnuID for the given pon-port
npujarec5762e2020-01-01 14:08:48 +0530439func (RsrcMgr *OpenOltResourceMgr) GetONUID(ctx context.Context, ponIntfID uint32) (uint32, error) {
salmansiddiqui352a45c2019-08-19 10:15:36 +0000440 // Check if Pon Interface ID is present in Resource-manager-map
Girish Gowdrab77ded92020-04-08 11:45:05 -0700441 RsrcMgr.OnuIDMgmtLock[ponIntfID].Lock()
442 defer RsrcMgr.OnuIDMgmtLock[ponIntfID].Unlock()
443
salmansiddiqui352a45c2019-08-19 10:15:36 +0000444 if _, ok := RsrcMgr.ResourceMgrs[ponIntfID]; !ok {
445 err := errors.New("invalid-pon-interface-" + strconv.Itoa(int(ponIntfID)))
446 return 0, err
447 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400448 // Get ONU id for a provided pon interface ID.
npujarec5762e2020-01-01 14:08:48 +0530449 ONUID, err := RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ctx, ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400450 ponrmgr.ONU_ID, 1)
451 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000452 logger.Errorf(ctx, "Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700453 ponIntfID, ponrmgr.ONU_ID)
cbabuabf02352019-10-15 13:14:56 +0200454 return 0, err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400455 }
456 if ONUID != nil {
npujarec5762e2020-01-01 14:08:48 +0530457 RsrcMgr.ResourceMgrs[ponIntfID].InitResourceMap(ctx, fmt.Sprintf("%d,%d", ponIntfID, ONUID[0]))
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700458 return ONUID[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400459 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530460
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700461 return 0, err // return OnuID 0 on error
Abhilash S.L7f17e402019-03-15 17:40:41 +0530462}
463
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700464// GetFlowIDInfo returns the slice of flow info of the given pon-port
465// Note: For flows which trap from the NNI and not really associated with any particular
466// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
npujarec5762e2020-01-01 14:08:48 +0530467func (RsrcMgr *OpenOltResourceMgr) GetFlowIDInfo(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32, flowID uint32) *[]FlowInfo {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530468 var flows []FlowInfo
469
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700470 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530471 if err := RsrcMgr.ResourceMgrs[ponIntfID].GetFlowIDInfo(ctx, FlowPath, flowID, &flows); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000472 logger.Errorw(ctx, "Error while getting flows from KV store", log.Fields{"flowId": flowID})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530473 return nil
474 }
475 if len(flows) == 0 {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000476 logger.Debugw(ctx, "No flowInfo found in KV store", log.Fields{"flowPath": FlowPath})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530477 return nil
478 }
479 return &flows
480}
481
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700482// GetCurrentFlowIDsForOnu fetches flow ID from the resource manager
483// Note: For flows which trap from the NNI and not really associated with any particular
484// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
npujarec5762e2020-01-01 14:08:48 +0530485func (RsrcMgr *OpenOltResourceMgr) GetCurrentFlowIDsForOnu(ctx context.Context, PONIntfID uint32, ONUID int32, UNIID int32) []uint32 {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700486
Abhilash S.L8ee90712019-04-29 16:24:22 +0530487 FlowPath := fmt.Sprintf("%d,%d,%d", PONIntfID, ONUID, UNIID)
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700488 if mgrs, exist := RsrcMgr.ResourceMgrs[PONIntfID]; exist {
npujarec5762e2020-01-01 14:08:48 +0530489 return mgrs.GetCurrentFlowIDsForOnu(ctx, FlowPath)
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700490 }
491 return nil
Abhilash S.L8ee90712019-04-29 16:24:22 +0530492}
493
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700494// UpdateFlowIDInfo updates flow info for the given pon interface, onu id, and uni id
495// Note: For flows which trap from the NNI and not really associated with any particular
496// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
npujarec5762e2020-01-01 14:08:48 +0530497func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDInfo(ctx context.Context, ponIntfID int32, onuID int32, uniID int32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700498 flowID uint32, flowData *[]FlowInfo) error {
499 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530500 return RsrcMgr.ResourceMgrs[uint32(ponIntfID)].UpdateFlowIDInfoForOnu(ctx, FlowPath, flowID, *flowData)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530501}
502
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700503// GetFlowID return flow ID for a given pon interface id, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530504func (RsrcMgr *OpenOltResourceMgr) GetFlowID(ctx context.Context, ponIntfID uint32, ONUID int32, uniID int32,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400505 gemportID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700506 flowStoreCookie uint64,
Gamze Abaka724d0852020-03-18 12:10:24 +0000507 flowCategory string, vlanVid uint32, vlanPcp ...uint32) (uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530508
Girish Gowdru0c588b22019-04-23 23:24:56 -0400509 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700510 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, ONUID, uniID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700511
512 RsrcMgr.FlowIDMgmtLock.Lock()
513 defer RsrcMgr.FlowIDMgmtLock.Unlock()
514
npujarec5762e2020-01-01 14:08:48 +0530515 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(ctx, FlowPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400516 if FlowIDs != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000517 logger.Debugw(ctx, "Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "ONUID": ONUID, "uniID": uniID, "KVpath": FlowPath})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700518 for _, flowID := range FlowIDs {
npujarec5762e2020-01-01 14:08:48 +0530519 FlowInfo := RsrcMgr.GetFlowIDInfo(ctx, ponIntfID, int32(ONUID), int32(uniID), uint32(flowID))
Neha Sharma96b7bf22020-06-15 10:37:32 +0000520 er := getFlowIDFromFlowInfo(ctx, FlowInfo, flowID, gemportID, flowStoreCookie, flowCategory, vlanVid, vlanPcp...)
salmansiddiqui7ac62132019-08-22 03:58:50 +0000521 if er == nil {
Gamze Abaka724d0852020-03-18 12:10:24 +0000522 log.Debugw("Found flowid for the vlan, pcp, and gem",
523 log.Fields{"flowID": flowID, "vlanVid": vlanVid, "vlanPcp": vlanPcp, "gemPortID": gemportID})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000524 return flowID, er
Abhilash S.L8ee90712019-04-29 16:24:22 +0530525 }
526 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400527 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000528 logger.Debug(ctx, "No matching flows with flow cookie or flow category, allocating new flowid")
npujarec5762e2020-01-01 14:08:48 +0530529 FlowIDs, err = RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ctx, ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400530 ponrmgr.FLOW_ID, 1)
531 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000532 logger.Errorf(ctx, "Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700533 ponIntfID, ponrmgr.FLOW_ID)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400534 return uint32(0), err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400535 }
536 if FlowIDs != nil {
npujarec5762e2020-01-01 14:08:48 +0530537 _ = RsrcMgr.ResourceMgrs[ponIntfID].UpdateFlowIDForOnu(ctx, FlowPath, FlowIDs[0], true)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700538 return FlowIDs[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400539 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530540
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700541 return 0, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530542}
543
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700544// GetAllocID return the first Alloc ID for a given pon interface id and onu id and then update the resource map on
545// the KV store with the list of alloc_ids allocated for the pon_intf_onu_id tuple
546// 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 +0530547func (RsrcMgr *OpenOltResourceMgr) GetAllocID(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530548
Girish Gowdru0c588b22019-04-23 23:24:56 -0400549 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700550 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700551
552 RsrcMgr.AllocIDMgmtLock[intfID].Lock()
553 defer RsrcMgr.AllocIDMgmtLock[intfID].Unlock()
554
npujarec5762e2020-01-01 14:08:48 +0530555 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400556 if AllocID != nil {
557 // Since we support only one alloc_id for the ONU at the moment,
558 // return the first alloc_id in the list, if available, for that
559 // ONU.
Neha Sharma96b7bf22020-06-15 10:37:32 +0000560 logger.Debugw(ctx, "Retrieved alloc ID from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400561 return AllocID[0]
562 }
npujarec5762e2020-01-01 14:08:48 +0530563 AllocID, err = RsrcMgr.ResourceMgrs[intfID].GetResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400564 ponrmgr.ALLOC_ID, 1)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530565
Girish Gowdru0c588b22019-04-23 23:24:56 -0400566 if AllocID == nil || err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000567 logger.Error(ctx, "Failed to allocate alloc id")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400568 return 0
569 }
570 // update the resource map on KV store with the list of alloc_id
571 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530572 err = RsrcMgr.ResourceMgrs[intfID].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID, AllocID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400573 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000574 logger.Error(ctx, "Failed to update Alloc ID")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400575 return 0
576 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000577 logger.Debugw(ctx, "Allocated new Tcont from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400578 return AllocID[0]
Abhilash S.L7f17e402019-03-15 17:40:41 +0530579}
580
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700581// UpdateAllocIdsForOnu updates alloc ids in kv store for a given pon interface id, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530582func (RsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ctx context.Context, ponPort uint32, onuID uint32, uniID uint32, allocID []uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530583
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700584 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530585 return RsrcMgr.ResourceMgrs[ponPort].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700586 allocID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530587}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700588
589// GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530590func (RsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(ctx context.Context, intfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700591 uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530592
Girish Gowdru0c588b22019-04-23 23:24:56 -0400593 /* Get gem ports for given pon interface , onu id and uni id. */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530594
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700595 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530596 return RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530597}
598
Gamze Abakafee36392019-10-03 11:17:24 +0000599// GetCurrentAllocIDsForOnu returns alloc ids for given pon interface and onu id
npujarec5762e2020-01-01 14:08:48 +0530600func (RsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDsForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530601
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700602 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530603 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400604 if AllocID != nil {
Gamze Abakafee36392019-10-03 11:17:24 +0000605 return AllocID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400606 }
Gamze Abakafee36392019-10-03 11:17:24 +0000607 return []uint32{}
608}
609
610// RemoveAllocIDForOnu removes the alloc id for given pon interface, onu id, uni id and alloc id
npujarec5762e2020-01-01 14:08:48 +0530611func (RsrcMgr *OpenOltResourceMgr) RemoveAllocIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, allocID uint32) {
612 allocIDs := RsrcMgr.GetCurrentAllocIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000613 for i := 0; i < len(allocIDs); i++ {
614 if allocIDs[i] == allocID {
615 allocIDs = append(allocIDs[:i], allocIDs[i+1:]...)
616 break
617 }
618 }
npujarec5762e2020-01-01 14:08:48 +0530619 err := RsrcMgr.UpdateAllocIdsForOnu(ctx, intfID, onuID, uniID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000620 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000621 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 +0000622 intfID, onuID, uniID, allocID)
623 }
624}
625
626// RemoveGemPortIDForOnu removes the gem port id for given pon interface, onu id, uni id and gem port id
npujarec5762e2020-01-01 14:08:48 +0530627func (RsrcMgr *OpenOltResourceMgr) RemoveGemPortIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, gemPortID uint32) {
628 gemPortIDs := RsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000629 for i := 0; i < len(gemPortIDs); i++ {
630 if gemPortIDs[i] == gemPortID {
631 gemPortIDs = append(gemPortIDs[:i], gemPortIDs[i+1:]...)
632 break
633 }
634 }
npujarec5762e2020-01-01 14:08:48 +0530635 err := RsrcMgr.UpdateGEMPortIDsForOnu(ctx, intfID, onuID, uniID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000636 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000637 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 +0000638 intfID, onuID, uniID, gemPortID)
639 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530640}
641
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700642// UpdateGEMportsPonportToOnuMapOnKVStore updates onu and uni id associated with the gem port to the kv store
643// This stored information is used when packet_indication is received and we need to derive the ONU Id for which
644// the packet arrived based on the pon_intf and gemport available in the packet_indication
npujarec5762e2020-01-01 14:08:48 +0530645func (RsrcMgr *OpenOltResourceMgr) UpdateGEMportsPonportToOnuMapOnKVStore(ctx context.Context, gemPorts []uint32, PonPort uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700646 onuID uint32, uniID uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530647
Girish Gowdru0c588b22019-04-23 23:24:56 -0400648 /* Update onu and uni id associated with the gem port to the kv store. */
649 var IntfGEMPortPath string
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700650 Data := fmt.Sprintf("%d %d", onuID, uniID)
651 for _, GEM := range gemPorts {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400652 IntfGEMPortPath = fmt.Sprintf("%d,%d", PonPort, GEM)
653 Val, err := json.Marshal(Data)
654 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000655 logger.Error(ctx, "failed to Marshal")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400656 return err
657 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700658
npujarec5762e2020-01-01 14:08:48 +0530659 if err = RsrcMgr.KVStore.Put(ctx, IntfGEMPortPath, Val); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000660 logger.Errorf(ctx, "Failed to update resource %s", IntfGEMPortPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400661 return err
662 }
663 }
664 return nil
Abhilash S.L7f17e402019-03-15 17:40:41 +0530665}
666
Gamze Abakafee36392019-10-03 11:17:24 +0000667// RemoveGEMportPonportToOnuMapOnKVStore removes the relationship between the gem port and pon port
npujarec5762e2020-01-01 14:08:48 +0530668func (RsrcMgr *OpenOltResourceMgr) RemoveGEMportPonportToOnuMapOnKVStore(ctx context.Context, GemPort uint32, PonPort uint32) {
Gamze Abakafee36392019-10-03 11:17:24 +0000669 IntfGEMPortPath := fmt.Sprintf("%d,%d", PonPort, GemPort)
npujarec5762e2020-01-01 14:08:48 +0530670 err := RsrcMgr.KVStore.Delete(ctx, IntfGEMPortPath)
Gamze Abakafee36392019-10-03 11:17:24 +0000671 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000672 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 +0000673 }
674}
675
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700676// GetGEMPortID gets gem port id for a particular pon port, onu id and uni id and then update the resource map on
677// the KV store with the list of gemport_id allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530678func (RsrcMgr *OpenOltResourceMgr) GetGEMPortID(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700679 uniID uint32, NumOfPorts uint32) ([]uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530680
Girish Gowdru0c588b22019-04-23 23:24:56 -0400681 /* Get gem port id for a particular pon port, onu id
682 and uni id.
683 */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530684
Girish Gowdru0c588b22019-04-23 23:24:56 -0400685 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700686 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530687
Girish Gowdrab77ded92020-04-08 11:45:05 -0700688 RsrcMgr.GemPortIDMgmtLock[ponPort].Lock()
689 defer RsrcMgr.GemPortIDMgmtLock[ponPort].Unlock()
690
npujarec5762e2020-01-01 14:08:48 +0530691 GEMPortList := RsrcMgr.ResourceMgrs[ponPort].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400692 if GEMPortList != nil {
693 return GEMPortList, nil
694 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530695
npujarec5762e2020-01-01 14:08:48 +0530696 GEMPortList, err = RsrcMgr.ResourceMgrs[ponPort].GetResourceID(ctx, ponPort,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400697 ponrmgr.GEMPORT_ID, NumOfPorts)
698 if err != nil && GEMPortList == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000699 logger.Errorf(ctx, "Failed to get gem port id for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400700 return nil, err
701 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530702
Girish Gowdru0c588b22019-04-23 23:24:56 -0400703 // update the resource map on KV store with the list of gemport_id
704 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530705 err = RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400706 GEMPortList)
707 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000708 logger.Errorf(ctx, "Failed to update GEM ports to kv store for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400709 return nil, err
710 }
npujarec5762e2020-01-01 14:08:48 +0530711 _ = RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(ctx, GEMPortList, ponPort,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700712 onuID, uniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400713 return GEMPortList, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530714}
715
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700716// 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 +0530717func (RsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700718 uniID uint32, GEMPortList []uint32) error {
719 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530720 return RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400721 GEMPortList)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530722
723}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700724
725// FreeonuID releases(make free) onu id for a particular pon-port
npujarec5762e2020-01-01 14:08:48 +0530726func (RsrcMgr *OpenOltResourceMgr) FreeonuID(ctx context.Context, intfID uint32, onuID []uint32) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700727
Girish Gowdra38d533d2020-03-30 20:38:51 -0700728 RsrcMgr.OnuIDMgmtLock[intfID].Lock()
Girish Gowdrab77ded92020-04-08 11:45:05 -0700729 defer RsrcMgr.OnuIDMgmtLock[intfID].Unlock()
730
npujarec5762e2020-01-01 14:08:48 +0530731 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID, ponrmgr.ONU_ID, onuID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530732
Girish Gowdru0c588b22019-04-23 23:24:56 -0400733 /* Free onu id for a particular interface.*/
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700734 var IntfonuID string
735 for _, onu := range onuID {
736 IntfonuID = fmt.Sprintf("%d,%d", intfID, onu)
npujarec5762e2020-01-01 14:08:48 +0530737 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfonuID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400738 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530739}
740
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700741// FreeFlowID returns the free flow id for a given interface, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530742func (RsrcMgr *OpenOltResourceMgr) FreeFlowID(ctx context.Context, IntfID uint32, onuID int32,
Devmalya Paul495b94a2019-08-27 19:42:00 -0400743 uniID int32, FlowID uint32) {
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400744 var IntfONUID string
745 var err error
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530746
Girish Gowdrab77ded92020-04-08 11:45:05 -0700747 RsrcMgr.FlowIDMgmtLock.Lock()
748 defer RsrcMgr.FlowIDMgmtLock.Unlock()
749
750 FlowIds := make([]uint32, 0)
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530751 FlowIds = append(FlowIds, FlowID)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700752 IntfONUID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530753 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(ctx, IntfONUID, FlowID, false)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400754 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000755 logger.Errorw(ctx, "Failed to Update flow id for", log.Fields{"intf": IntfONUID})
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400756 }
npujarec5762e2020-01-01 14:08:48 +0530757 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(ctx, IntfONUID, FlowID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700758
npujarec5762e2020-01-01 14:08:48 +0530759 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.FLOW_ID, FlowIds)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400760}
761
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700762// FreeFlowIDs releases the flow Ids
npujarec5762e2020-01-01 14:08:48 +0530763func (RsrcMgr *OpenOltResourceMgr) FreeFlowIDs(ctx context.Context, IntfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700764 uniID uint32, FlowID []uint32) {
Girish Gowdra38d533d2020-03-30 20:38:51 -0700765 RsrcMgr.FlowIDMgmtLock.Lock()
Girish Gowdrab77ded92020-04-08 11:45:05 -0700766 defer RsrcMgr.FlowIDMgmtLock.Unlock()
767
npujarec5762e2020-01-01 14:08:48 +0530768 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.FLOW_ID, FlowID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530769
Abhilash S.L8ee90712019-04-29 16:24:22 +0530770 var IntfOnuIDUniID string
Girish Gowdru0c588b22019-04-23 23:24:56 -0400771 var err error
772 for _, flow := range FlowID {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700773 IntfOnuIDUniID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530774 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(ctx, IntfOnuIDUniID, flow, false)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400775 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000776 logger.Errorw(ctx, "Failed to Update flow id for", log.Fields{"intf": IntfOnuIDUniID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400777 }
npujarec5762e2020-01-01 14:08:48 +0530778 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(ctx, IntfOnuIDUniID, flow)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400779 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530780}
781
Gamze Abakafee36392019-10-03 11:17:24 +0000782// FreeAllocID frees AllocID on the PON resource pool and also frees the allocID association
783// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530784func (RsrcMgr *OpenOltResourceMgr) FreeAllocID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000785 uniID uint32, allocID uint32) {
Girish Gowdrab77ded92020-04-08 11:45:05 -0700786 RsrcMgr.AllocIDMgmtLock[IntfID].Lock()
787 defer RsrcMgr.AllocIDMgmtLock[IntfID].Unlock()
788
npujarec5762e2020-01-01 14:08:48 +0530789 RsrcMgr.RemoveAllocIDForOnu(ctx, IntfID, onuID, uniID, allocID)
Gamze Abakafee36392019-10-03 11:17:24 +0000790 allocIDs := make([]uint32, 0)
791 allocIDs = append(allocIDs, allocID)
npujarec5762e2020-01-01 14:08:48 +0530792 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.ALLOC_ID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000793}
794
795// FreeGemPortID frees GemPortID on the PON resource pool and also frees the gemPortID association
796// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530797func (RsrcMgr *OpenOltResourceMgr) FreeGemPortID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000798 uniID uint32, gemPortID uint32) {
Girish Gowdrab77ded92020-04-08 11:45:05 -0700799 RsrcMgr.GemPortIDMgmtLock[IntfID].Lock()
800 defer RsrcMgr.GemPortIDMgmtLock[IntfID].Unlock()
801
npujarec5762e2020-01-01 14:08:48 +0530802 RsrcMgr.RemoveGemPortIDForOnu(ctx, IntfID, onuID, uniID, gemPortID)
Gamze Abakafee36392019-10-03 11:17:24 +0000803 gemPortIDs := make([]uint32, 0)
804 gemPortIDs = append(gemPortIDs, gemPortID)
npujarec5762e2020-01-01 14:08:48 +0530805 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.GEMPORT_ID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000806}
807
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700808// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id, and the clears the
809// resource map and the onuID associated with (pon_intf_id, gemport_id) tuple,
npujarec5762e2020-01-01 14:08:48 +0530810func (RsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530811
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700812 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530813
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700814 RsrcMgr.AllocIDMgmtLock[intfID].Lock()
Girish Gowdrab77ded92020-04-08 11:45:05 -0700815 AllocIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700816
npujarec5762e2020-01-01 14:08:48 +0530817 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400818 ponrmgr.ALLOC_ID,
819 AllocIDs)
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700820 RsrcMgr.AllocIDMgmtLock[intfID].Unlock()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530821
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700822 RsrcMgr.GemPortIDMgmtLock[intfID].Lock()
npujarec5762e2020-01-01 14:08:48 +0530823 GEMPortIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
824 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400825 ponrmgr.GEMPORT_ID,
826 GEMPortIDs)
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700827 RsrcMgr.GemPortIDMgmtLock[intfID].Unlock()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530828
Girish Gowdra38d533d2020-03-30 20:38:51 -0700829 RsrcMgr.FlowIDMgmtLock.Lock()
npujarec5762e2020-01-01 14:08:48 +0530830 FlowIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentFlowIDsForOnu(ctx, IntfOnuIDUniID)
831 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400832 ponrmgr.FLOW_ID,
833 FlowIDs)
Girish Gowdra38d533d2020-03-30 20:38:51 -0700834 RsrcMgr.FlowIDMgmtLock.Unlock()
835
Girish Gowdru0c588b22019-04-23 23:24:56 -0400836 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
npujarec5762e2020-01-01 14:08:48 +0530837 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400838 // Clear the ONU Id associated with the (pon_intf_id, gemport_id) tuple.
839 for _, GEM := range GEMPortIDs {
npujarec5762e2020-01-01 14:08:48 +0530840 _ = RsrcMgr.KVStore.Delete(ctx, fmt.Sprintf("%d,%d", intfID, GEM))
Girish Gowdru0c588b22019-04-23 23:24:56 -0400841 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530842}
843
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700844// IsFlowCookieOnKVStore checks if the given flow cookie is present on the kv store
845// Returns true if the flow cookie is found, otherwise it returns false
npujarec5762e2020-01-01 14:08:48 +0530846func (RsrcMgr *OpenOltResourceMgr) IsFlowCookieOnKVStore(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700847 flowStoreCookie uint64) bool {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530848
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700849 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530850 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(ctx, FlowPath)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400851 if FlowIDs != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000852 logger.Debugw(ctx, "Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "onuID": onuID, "uniID": uniID, "KVpath": FlowPath})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700853 for _, flowID := range FlowIDs {
npujarec5762e2020-01-01 14:08:48 +0530854 FlowInfo := RsrcMgr.GetFlowIDInfo(ctx, ponIntfID, int32(onuID), int32(uniID), uint32(flowID))
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400855 if FlowInfo != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000856 logger.Debugw(ctx, "Found flows", log.Fields{"flows": *FlowInfo, "flowId": flowID})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400857 for _, Info := range *FlowInfo {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700858 if Info.FlowStoreCookie == flowStoreCookie {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000859 logger.Debug(ctx, "Found flow matching with flowStore cookie", log.Fields{"flowId": flowID, "flowStoreCookie": flowStoreCookie})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400860 return true
861 }
862 }
863 }
864 }
865 }
866 return false
867}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400868
salmansiddiqui7ac62132019-08-22 03:58:50 +0000869// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000870// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530871func (RsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) []uint32 {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000872 Path := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000873 var Data []uint32
npujarec5762e2020-01-01 14:08:48 +0530874 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400875 if err == nil {
876 if Value != nil {
877 Val, err := kvstore.ToByte(Value.Value)
878 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000879 logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"error": err})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400880 return Data
881 }
882 if err = json.Unmarshal(Val, &Data); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000883 logger.Error(ctx, "Failed to unmarshal", log.Fields{"error": err})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400884 return Data
885 }
886 }
887 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000888 logger.Errorf(ctx, "Failed to get TP id from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400889 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000890 logger.Debugf(ctx, "Getting TP id %d from path %s", Data, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400891 return Data
892
893}
894
Gamze Abakafee36392019-10-03 11:17:24 +0000895// RemoveTechProfileIDsForOnu deletes all tech profile ids from the KV-Store for the given onu based on the path
896// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530897func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDsForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) error {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000898 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
npujarec5762e2020-01-01 14:08:48 +0530899 if err := RsrcMgr.KVStore.Delete(ctx, IntfOnuUniID); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000900 logger.Errorw(ctx, "Failed to delete techprofile id resource in KV store", log.Fields{"path": IntfOnuUniID})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400901 return err
902 }
903 return nil
904}
905
Gamze Abakafee36392019-10-03 11:17:24 +0000906// RemoveTechProfileIDForOnu deletes a specific tech profile id from the KV-Store for the given onu based on the path
907// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530908func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32, TpID uint32) error {
909 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000910 for i, tpIDInList := range tpIDList {
911 if tpIDInList == TpID {
912 tpIDList = append(tpIDList[:i], tpIDList[i+1:]...)
913 }
914 }
915 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
916 Value, err := json.Marshal(tpIDList)
917 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000918 logger.Error(ctx, "failed to Marshal")
Gamze Abakafee36392019-10-03 11:17:24 +0000919 return err
920 }
npujarec5762e2020-01-01 14:08:48 +0530921 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000922 logger.Errorf(ctx, "Failed to update resource %s", IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000923 return err
924 }
925 return err
926}
927
928// UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
929// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530930func (RsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32,
salmansiddiqui7ac62132019-08-22 03:58:50 +0000931 UniID uint32, TpID uint32) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400932 var Value []byte
933 var err error
934
salmansiddiqui7ac62132019-08-22 03:58:50 +0000935 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000936
npujarec5762e2020-01-01 14:08:48 +0530937 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000938 for _, value := range tpIDList {
939 if value == TpID {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000940 logger.Debugf(ctx, "TpID %d is already in tpIdList for the path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000941 return err
942 }
943 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000944 logger.Debugf(ctx, "updating tp id %d on path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000945 tpIDList = append(tpIDList, TpID)
946 Value, err = json.Marshal(tpIDList)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400947 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000948 logger.Error(ctx, "failed to Marshal")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400949 return err
950 }
npujarec5762e2020-01-01 14:08:48 +0530951 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000952 logger.Errorf(ctx, "Failed to update resource %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400953 return err
954 }
955 return err
956}
957
salmansiddiqui7ac62132019-08-22 03:58:50 +0000958// UpdateMeterIDForOnu updates the meter id in the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000959// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +0530960func (RsrcMgr *OpenOltResourceMgr) UpdateMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000961 UniID uint32, TpID uint32, MeterConfig *ofp.OfpMeterConfig) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400962 var Value []byte
963 var err error
964
Gamze Abakafee36392019-10-03 11:17:24 +0000965 IntfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400966 Value, err = json.Marshal(*MeterConfig)
967 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000968 logger.Error(ctx, "failed to Marshal meter config")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400969 return err
970 }
npujarec5762e2020-01-01 14:08:48 +0530971 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000972 logger.Errorf(ctx, "Failed to store meter into KV store %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400973 return err
974 }
975 return err
976}
977
Gamze Abakafee36392019-10-03 11:17:24 +0000978// GetMeterIDForOnu fetches the meter id from the kv store for the given onu based on the path
979// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +0530980func (RsrcMgr *OpenOltResourceMgr) GetMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000981 UniID uint32, TpID uint32) (*ofp.OfpMeterConfig, error) {
982 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400983 var meterConfig ofp.OfpMeterConfig
npujarec5762e2020-01-01 14:08:48 +0530984 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400985 if err == nil {
986 if Value != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000987 logger.Debug(ctx, "Found meter in KV store", log.Fields{"Direction": Direction})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000988 Val, er := kvstore.ToByte(Value.Value)
989 if er != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000990 logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"error": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000991 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400992 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000993 if er = json.Unmarshal(Val, &meterConfig); er != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000994 logger.Error(ctx, "Failed to unmarshal meterconfig", log.Fields{"error": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000995 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400996 }
997 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000998 logger.Debug(ctx, "meter-does-not-exists-in-KVStore")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400999 return nil, err
1000 }
1001 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001002 logger.Errorf(ctx, "Failed to get Meter config from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001003
1004 }
1005 return &meterConfig, err
1006}
1007
Gamze Abakafee36392019-10-03 11:17:24 +00001008// RemoveMeterIDForOnu deletes the meter id from the kV-Store for the given onu based on the path
1009// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +05301010func (RsrcMgr *OpenOltResourceMgr) RemoveMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +00001011 UniID uint32, TpID uint32) error {
1012 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
npujarec5762e2020-01-01 14:08:48 +05301013 if err := RsrcMgr.KVStore.Delete(ctx, Path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001014 logger.Errorf(ctx, "Failed to delete meter id %s from kvstore ", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001015 return err
1016 }
1017 return nil
1018}
salmansiddiqui7ac62132019-08-22 03:58:50 +00001019
Neha Sharma96b7bf22020-06-15 10:37:32 +00001020func getFlowIDFromFlowInfo(ctx context.Context, FlowInfo *[]FlowInfo, flowID, gemportID uint32, flowStoreCookie uint64, flowCategory string,
Gamze Abaka724d0852020-03-18 12:10:24 +00001021 vlanVid uint32, vlanPcp ...uint32) error {
salmansiddiqui7ac62132019-08-22 03:58:50 +00001022 if FlowInfo != nil {
1023 for _, Info := range *FlowInfo {
1024 if int32(gemportID) == Info.Flow.GemportId && flowCategory != "" && Info.FlowCategory == flowCategory {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001025 logger.Debug(ctx, "Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
Gamze Abaka724d0852020-03-18 12:10:24 +00001026 if Info.FlowCategory == "HSIA_FLOW" {
1027 if err := checkVlanAndPbitEqualityForFlows(vlanVid, Info, vlanPcp[0]); err == nil {
1028 return nil
1029 }
salmansiddiqui7ac62132019-08-22 03:58:50 +00001030 }
1031 }
1032 if int32(gemportID) == Info.Flow.GemportId && flowStoreCookie != 0 && Info.FlowStoreCookie == flowStoreCookie {
1033 if flowCategory != "" && Info.FlowCategory == flowCategory {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001034 logger.Debug(ctx, "Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001035 return nil
1036 }
1037 }
1038 }
1039 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001040 logger.Debugw(ctx, "the flow can be related to a different service", log.Fields{"flow_info": FlowInfo})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001041 return errors.New("invalid flow-info")
1042}
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301043
Gamze Abaka724d0852020-03-18 12:10:24 +00001044func checkVlanAndPbitEqualityForFlows(vlanVid uint32, Info FlowInfo, vlanPcp uint32) error {
1045 if err := checkVlanEqualityForFlows(vlanVid, Info); err != nil {
1046 return err
1047 }
1048
1049 //flow has remark action and pbits
1050 if Info.Flow.Action.Cmd.RemarkInnerPbits || Info.Flow.Action.Cmd.RemarkOuterPbits {
1051 if vlanPcp == Info.Flow.Action.OPbits || vlanPcp == Info.Flow.Action.IPbits {
1052 return nil
1053 }
1054 } else if vlanPcp == Info.Flow.Classifier.OPbits {
1055 //no remark action but flow has pbits
1056 return nil
1057 } else if vlanPcp == 0xff || Info.Flow.Classifier.OPbits == 0xff {
1058 // no pbit found
1059 return nil
1060 }
1061 return errors.New("not found in terms of pbit equality")
1062}
1063
1064func checkVlanEqualityForFlows(vlanVid uint32, Info FlowInfo) error {
1065 if vlanVid == Info.Flow.Action.OVid || vlanVid == Info.Flow.Classifier.IVid {
1066 return nil
1067 }
1068 return errors.New("not found in terms of vlan_id equality")
1069}
1070
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301071//AddGemToOnuGemInfo adds gemport to onugem info kvstore
npujarec5762e2020-01-01 14:08:48 +05301072func (RsrcMgr *OpenOltResourceMgr) AddGemToOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, gemPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301073 var onuGemData []OnuGemInfo
1074 var err error
1075
npujarec5762e2020-01-01 14:08:48 +05301076 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001077 logger.Errorf(ctx, "failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301078 return err
1079 }
1080 if len(onuGemData) == 0 {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001081 logger.Errorw(ctx, "failed to ger Onuid info ", log.Fields{"intfid": intfID, "onuid": onuID})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301082 return err
1083 }
1084
1085 for idx, onugem := range onuGemData {
1086 if onugem.OnuID == onuID {
1087 for _, gem := range onuGemData[idx].GemPorts {
1088 if gem == gemPort {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001089 logger.Debugw(ctx, "Gem already present in onugem info, skpping addition", log.Fields{"gem": gem})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301090 return nil
1091 }
1092 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001093 logger.Debugw(ctx, "Added gem to onugem info", log.Fields{"gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301094 onuGemData[idx].GemPorts = append(onuGemData[idx].GemPorts, gemPort)
1095 break
1096 }
1097 }
npujarec5762e2020-01-01 14:08:48 +05301098 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301099 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001100 logger.Error(ctx, "Failed to add onugem to kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301101 return err
1102 }
1103 return err
1104}
1105
1106//GetOnuGemInfo gets onu gem info from the kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301107func (RsrcMgr *OpenOltResourceMgr) GetOnuGemInfo(ctx context.Context, IntfID uint32) ([]OnuGemInfo, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301108 var onuGemData []OnuGemInfo
1109
npujarec5762e2020-01-01 14:08:48 +05301110 if err := RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001111 logger.Errorf(ctx, "failed to get onuifo for intfid %d", IntfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301112 return nil, err
1113 }
1114
1115 return onuGemData, nil
1116}
1117
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001118// AddOnuGemInfo adds onu info on to the kvstore per interface
1119func (RsrcMgr *OpenOltResourceMgr) AddOnuGemInfo(ctx context.Context, IntfID uint32, onuGem OnuGemInfo) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301120 var onuGemData []OnuGemInfo
1121 var err error
1122
npujarec5762e2020-01-01 14:08:48 +05301123 if err = RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001124 logger.Errorf(ctx, "failed to get onuifo for intfid %d", IntfID)
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001125 return olterrors.NewErrPersistence("get", "OnuGemInfo", IntfID,
1126 log.Fields{"onuGem": onuGem, "intfID": IntfID}, err)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301127 }
1128 onuGemData = append(onuGemData, onuGem)
npujarec5762e2020-01-01 14:08:48 +05301129 err = RsrcMgr.ResourceMgrs[IntfID].AddOnuGemInfo(ctx, IntfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301130 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001131 logger.Error(ctx, "Failed to add onugem to kv store")
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001132 return olterrors.NewErrPersistence("set", "OnuGemInfo", IntfID,
1133 log.Fields{"onuGemData": onuGemData, "intfID": IntfID}, err)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301134 }
1135
Neha Sharma96b7bf22020-06-15 10:37:32 +00001136 logger.Debugw(ctx, "added onu to onugeminfo", log.Fields{"intf": IntfID, "onugem": onuGem})
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001137 return nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301138}
1139
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301140// 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 +05301141func (RsrcMgr *OpenOltResourceMgr) AddUniPortToOnuInfo(ctx context.Context, intfID uint32, onuID uint32, portNo uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301142 var onuGemData []OnuGemInfo
1143 var err error
1144
npujarec5762e2020-01-01 14:08:48 +05301145 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001146 logger.Errorf(ctx, "failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301147 return
1148 }
1149 for idx, onu := range onuGemData {
1150 if onu.OnuID == onuID {
1151 for _, uni := range onu.UniPorts {
1152 if uni == portNo {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001153 logger.Debugw(ctx, "uni already present in onugem info", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301154 return
1155 }
1156 }
1157 onuGemData[idx].UniPorts = append(onuGemData[idx].UniPorts, portNo)
1158 break
1159 }
1160 }
npujarec5762e2020-01-01 14:08:48 +05301161 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301162 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001163 logger.Errorw(ctx, "Failed to add uin port in onugem to kv store", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301164 return
1165 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301166}
1167
Esin Karaman7fb80c22020-07-16 14:23:33 +00001168//UpdateGemPortForPktIn updates gemport for pkt in path to kvstore, path being intfid, onuid, portno, vlan id, priority bit
npujarec5762e2020-01-01 14:08:48 +05301169func (RsrcMgr *OpenOltResourceMgr) UpdateGemPortForPktIn(ctx context.Context, pktIn PacketInInfoKey, gemPort uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301170
Esin Karaman7fb80c22020-07-16 14:23:33 +00001171 path := fmt.Sprintf(OnuPacketINPath, pktIn.IntfID, pktIn.OnuID, pktIn.LogicalPort, pktIn.VlanID, pktIn.Priority)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301172 Value, err := json.Marshal(gemPort)
1173 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001174 logger.Error(ctx, "Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301175 return
1176 }
npujarec5762e2020-01-01 14:08:48 +05301177 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001178 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"path": path, "value": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301179 return
1180 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001181 logger.Debugw(ctx, "added gem packet in successfully", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301182}
1183
Esin Karaman7fb80c22020-07-16 14:23:33 +00001184// GetGemPortFromOnuPktIn gets the gem port from onu pkt in path, path being intfid, onuid, portno, vlan id, priority bit
1185func (RsrcMgr *OpenOltResourceMgr) GetGemPortFromOnuPktIn(ctx context.Context, packetInInfoKey PacketInInfoKey) (uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301186
1187 var Val []byte
1188 var gemPort uint32
1189
Esin Karaman7fb80c22020-07-16 14:23:33 +00001190 path := fmt.Sprintf(OnuPacketINPath, packetInInfoKey.IntfID, packetInInfoKey.OnuID, packetInInfoKey.LogicalPort,
1191 packetInInfoKey.VlanID, packetInInfoKey.Priority)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301192
npujarec5762e2020-01-01 14:08:48 +05301193 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301194 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001195 logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301196 return uint32(0), err
1197 } else if value == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001198 logger.Debugw(ctx, "No pkt in gem found", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301199 return uint32(0), nil
1200 }
1201
1202 if Val, err = kvstore.ToByte(value.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001203 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301204 return uint32(0), err
1205 }
1206 if err = json.Unmarshal(Val, &gemPort); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001207 logger.Error(ctx, "Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301208 return uint32(0), err
1209 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001210 logger.Debugw(ctx, "found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301211
1212 return gemPort, nil
1213}
1214
Esin Karaman7fb80c22020-07-16 14:23:33 +00001215//DelGemPortPktInOfAllServices deletes the gemports from pkt in path for all services
1216func (RsrcMgr *OpenOltResourceMgr) DelGemPortPktInOfAllServices(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301217
Esin Karaman7fb80c22020-07-16 14:23:33 +00001218 //retrieve stored gem port from the store first.
1219 Path := fmt.Sprintf(OnuPacketINPathPrefix, intfID, onuID, logicalPort)
1220 logger.Debugf(ctx, "getting flows from the path:%s", Path)
1221 Value, err := RsrcMgr.KVStore.List(ctx, Path)
1222 if err != nil {
1223 logger.Errorf(ctx, "failed to get flows from kvstore for path %s", Path)
1224 return errors.New("failed to get flows from kvstore for path " + Path)
1225 }
1226 logger.Debugf(ctx, "%d flows retrieved from the path:%s", len(Value), Path)
1227
1228 //remove them one by one
1229 for key := range Value {
1230 if err := RsrcMgr.KVStore.Delete(ctx, key); err != nil {
1231 logger.Errorf(ctx, "Falied to remove resource %s", key)
1232 return err
1233 }
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301234 }
1235 return nil
1236}
1237
1238// DelOnuGemInfoForIntf deletes the onugem info from kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301239func (RsrcMgr *OpenOltResourceMgr) DelOnuGemInfoForIntf(ctx context.Context, intfID uint32) error {
1240 if err := RsrcMgr.ResourceMgrs[intfID].DelOnuGemInfoForIntf(ctx, intfID); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001241 logger.Errorw(ctx, "failed to delete onu gem info for", log.Fields{"intfid": intfID})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301242 return err
1243 }
1244 return nil
1245}
1246
1247//GetNNIFromKVStore gets NNi intfids from kvstore. path being per device
npujarec5762e2020-01-01 14:08:48 +05301248func (RsrcMgr *OpenOltResourceMgr) GetNNIFromKVStore(ctx context.Context) ([]uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301249
1250 var nni []uint32
1251 var Val []byte
1252
Kent Hagermane6ff1012020-07-14 15:07:53 -04001253 path := NnniIntfID
npujarec5762e2020-01-01 14:08:48 +05301254 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301255 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001256 logger.Error(ctx, "failed to get data from kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301257 return nil, err
1258 }
1259 if value != nil {
1260 if Val, err = kvstore.ToByte(value.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001261 logger.Error(ctx, "Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301262 return nil, err
1263 }
1264 if err = json.Unmarshal(Val, &nni); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001265 logger.Error(ctx, "Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301266 return nil, err
1267 }
1268 }
1269 return nni, err
1270}
1271
1272// AddNNIToKVStore adds Nni interfaces to kvstore, path being per device.
npujarec5762e2020-01-01 14:08:48 +05301273func (RsrcMgr *OpenOltResourceMgr) AddNNIToKVStore(ctx context.Context, nniIntf uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301274 var Value []byte
1275
npujarec5762e2020-01-01 14:08:48 +05301276 nni, err := RsrcMgr.GetNNIFromKVStore(ctx)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301277 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001278 logger.Error(ctx, "failed to fetch nni interfaces from kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301279 return err
1280 }
1281
Kent Hagermane6ff1012020-07-14 15:07:53 -04001282 path := NnniIntfID
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301283 nni = append(nni, nniIntf)
1284 Value, err = json.Marshal(nni)
1285 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001286 logger.Error(ctx, "Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301287 }
npujarec5762e2020-01-01 14:08:48 +05301288 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001289 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"path": path, "value": Value})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301290 return err
1291 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001292 logger.Debugw(ctx, "added nni to kv successfully", log.Fields{"path": path, "nni": nniIntf})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301293 return nil
1294}
1295
1296// DelNNiFromKVStore deletes nni interface list from kv store.
npujarec5762e2020-01-01 14:08:48 +05301297func (RsrcMgr *OpenOltResourceMgr) DelNNiFromKVStore(ctx context.Context) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301298
Kent Hagermane6ff1012020-07-14 15:07:53 -04001299 path := NnniIntfID
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301300
npujarec5762e2020-01-01 14:08:48 +05301301 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001302 logger.Errorw(ctx, "Failed to delete nni interfaces from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301303 return err
1304 }
1305 return nil
1306}
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301307
1308//UpdateFlowIDsForGem updates flow id per gemport
npujarec5762e2020-01-01 14:08:48 +05301309func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDsForGem(ctx context.Context, intf uint32, gem uint32, flowIDs []uint32) error {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301310 var val []byte
1311 path := fmt.Sprintf(FlowIDsForGem, intf)
1312
npujarec5762e2020-01-01 14:08:48 +05301313 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301314 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001315 logger.Error(ctx, "Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301316 return err
1317 }
1318 if flowsForGem == nil {
1319 flowsForGem = make(map[uint32][]uint32)
1320 }
1321 flowsForGem[gem] = flowIDs
1322 val, err = json.Marshal(flowsForGem)
1323 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001324 logger.Error(ctx, "Failed to marshal data", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301325 return err
1326 }
Girish Gowdrab77ded92020-04-08 11:45:05 -07001327
Girish Gowdra38d533d2020-03-30 20:38:51 -07001328 RsrcMgr.flowIDToGemInfoLock.Lock()
1329 defer RsrcMgr.flowIDToGemInfoLock.Unlock()
npujarec5762e2020-01-01 14:08:48 +05301330 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001331 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301332 return err
1333 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001334 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 +05301335 return nil
1336}
1337
1338//DeleteFlowIDsForGem deletes the flowID list entry per gem from kvstore.
npujarec5762e2020-01-01 14:08:48 +05301339func (RsrcMgr *OpenOltResourceMgr) DeleteFlowIDsForGem(ctx context.Context, intf uint32, gem uint32) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301340 path := fmt.Sprintf(FlowIDsForGem, intf)
1341 var val []byte
1342
npujarec5762e2020-01-01 14:08:48 +05301343 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301344 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001345 logger.Error(ctx, "Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301346 return
1347 }
1348 if flowsForGem == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001349 logger.Error(ctx, "No flowids found ", log.Fields{"intf": intf, "gemport": gem})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301350 return
1351 }
1352 // once we get the flows per gem map from kv , just delete the gem entry from the map
1353 delete(flowsForGem, gem)
1354 // once gem entry is deleted update the kv store.
1355 val, err = json.Marshal(flowsForGem)
1356 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001357 logger.Error(ctx, "Failed to marshal data", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301358 return
1359 }
Girish Gowdra38d533d2020-03-30 20:38:51 -07001360
1361 RsrcMgr.flowIDToGemInfoLock.Lock()
1362 defer RsrcMgr.flowIDToGemInfoLock.Unlock()
npujarec5762e2020-01-01 14:08:48 +05301363 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001364 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301365 }
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301366}
1367
1368//GetFlowIDsGemMapForInterface gets flowids per gemport and interface
npujarec5762e2020-01-01 14:08:48 +05301369func (RsrcMgr *OpenOltResourceMgr) GetFlowIDsGemMapForInterface(ctx context.Context, intf uint32) (map[uint32][]uint32, error) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301370 path := fmt.Sprintf(FlowIDsForGem, intf)
1371 var flowsForGem map[uint32][]uint32
1372 var val []byte
Girish Gowdra38d533d2020-03-30 20:38:51 -07001373 RsrcMgr.flowIDToGemInfoLock.RLock()
npujarec5762e2020-01-01 14:08:48 +05301374 value, err := RsrcMgr.KVStore.Get(ctx, path)
Girish Gowdra38d533d2020-03-30 20:38:51 -07001375 RsrcMgr.flowIDToGemInfoLock.RUnlock()
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301376 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001377 logger.Error(ctx, "failed to get data from kv store")
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301378 return nil, err
1379 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001380 if value != nil && value.Value != nil {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301381 if val, err = kvstore.ToByte(value.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001382 logger.Error(ctx, "Failed to convert to byte array ", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301383 return nil, err
1384 }
1385 if err = json.Unmarshal(val, &flowsForGem); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001386 logger.Error(ctx, "Failed to unmarshall", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301387 return nil, err
1388 }
1389 }
1390 return flowsForGem, nil
1391}
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301392
1393//DeleteIntfIDGempMapPath deletes the intf id path used to store flow ids per gem to kvstore.
npujarec5762e2020-01-01 14:08:48 +05301394func (RsrcMgr *OpenOltResourceMgr) DeleteIntfIDGempMapPath(ctx context.Context, intf uint32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301395 path := fmt.Sprintf(FlowIDsForGem, intf)
Girish Gowdra38d533d2020-03-30 20:38:51 -07001396 RsrcMgr.flowIDToGemInfoLock.Lock()
1397 defer RsrcMgr.flowIDToGemInfoLock.Unlock()
npujarec5762e2020-01-01 14:08:48 +05301398 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001399 logger.Errorw(ctx, "Failed to delete nni interfaces from kv store", log.Fields{"path": path})
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301400 }
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301401}
1402
1403// RemoveResourceMap Clear resource map associated with (intfid, onuid, uniid) tuple.
npujarec5762e2020-01-01 14:08:48 +05301404func (RsrcMgr *OpenOltResourceMgr) RemoveResourceMap(ctx context.Context, intfID uint32, onuID int32, uniID int32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301405 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +05301406 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301407}
Esin Karamanccb714b2019-11-29 15:02:06 +00001408
1409//GetMcastQueuePerInterfaceMap gets multicast queue info per pon interface
npujarec5762e2020-01-01 14:08:48 +05301410func (RsrcMgr *OpenOltResourceMgr) GetMcastQueuePerInterfaceMap(ctx context.Context) (map[uint32][]uint32, error) {
Kent Hagermane6ff1012020-07-14 15:07:53 -04001411 path := McastQueuesForIntf
Esin Karamanccb714b2019-11-29 15:02:06 +00001412 var mcastQueueToIntfMap map[uint32][]uint32
1413 var val []byte
1414
npujarec5762e2020-01-01 14:08:48 +05301415 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001416 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001417 logger.Error(ctx, "failed to get data from kv store")
Esin Karamanccb714b2019-11-29 15:02:06 +00001418 return nil, err
1419 }
1420 if kvPair != nil && kvPair.Value != nil {
1421 if val, err = kvstore.ToByte(kvPair.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001422 logger.Error(ctx, "Failed to convert to byte array ", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001423 return nil, err
1424 }
1425 if err = json.Unmarshal(val, &mcastQueueToIntfMap); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001426 logger.Error(ctx, "Failed to unmarshall ", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001427 return nil, err
1428 }
1429 }
1430 return mcastQueueToIntfMap, nil
1431}
1432
1433//AddMcastQueueForIntf adds multicast queue for pon interface
npujarec5762e2020-01-01 14:08:48 +05301434func (RsrcMgr *OpenOltResourceMgr) AddMcastQueueForIntf(ctx context.Context, intf uint32, gem uint32, servicePriority uint32) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001435 var val []byte
Kent Hagermane6ff1012020-07-14 15:07:53 -04001436 path := McastQueuesForIntf
Esin Karamanccb714b2019-11-29 15:02:06 +00001437
npujarec5762e2020-01-01 14:08:48 +05301438 mcastQueues, err := RsrcMgr.GetMcastQueuePerInterfaceMap(ctx)
Esin Karamanccb714b2019-11-29 15:02:06 +00001439 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001440 logger.Errorw(ctx, "Failed to get multicast queue info for interface", log.Fields{"error": err, "intf": intf})
Esin Karamanccb714b2019-11-29 15:02:06 +00001441 return err
1442 }
1443 if mcastQueues == nil {
1444 mcastQueues = make(map[uint32][]uint32)
1445 }
1446 mcastQueues[intf] = []uint32{gem, servicePriority}
1447 if val, err = json.Marshal(mcastQueues); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001448 logger.Errorw(ctx, "Failed to marshal data", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001449 return err
1450 }
npujarec5762e2020-01-01 14:08:48 +05301451 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001452 logger.Errorw(ctx, "Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Esin Karamanccb714b2019-11-29 15:02:06 +00001453 return err
1454 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001455 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 +00001456 return nil
1457}
1458
1459//AddFlowGroupToKVStore adds flow group into KV store
npujarec5762e2020-01-01 14:08:48 +05301460func (RsrcMgr *OpenOltResourceMgr) AddFlowGroupToKVStore(ctx context.Context, groupEntry *ofp.OfpGroupEntry, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001461 var Value []byte
1462 var err error
1463 var path string
1464 if cached {
1465 path = fmt.Sprintf(FlowGroupCached, groupEntry.Desc.GroupId)
1466 } else {
1467 path = fmt.Sprintf(FlowGroup, groupEntry.Desc.GroupId)
1468 }
1469 //build group info object
1470 var outPorts []uint32
1471 for _, ofBucket := range groupEntry.Desc.Buckets {
1472 for _, ofAction := range ofBucket.Actions {
1473 if ofAction.Type == ofp.OfpActionType_OFPAT_OUTPUT {
1474 outPorts = append(outPorts, ofAction.GetOutput().Port)
1475 }
1476 }
1477 }
1478 groupInfo := GroupInfo{
1479 GroupID: groupEntry.Desc.GroupId,
1480 OutPorts: outPorts,
1481 }
1482
1483 Value, err = json.Marshal(groupInfo)
1484
1485 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001486 logger.Error(ctx, "failed to Marshal flow group object")
Esin Karamanccb714b2019-11-29 15:02:06 +00001487 return err
1488 }
1489
npujarec5762e2020-01-01 14:08:48 +05301490 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001491 logger.Errorf(ctx, "Failed to update resource %s", path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001492 return err
1493 }
1494 return nil
1495}
1496
1497//RemoveFlowGroupFromKVStore removes flow group from KV store
Esin Karamand519bbf2020-07-01 11:16:03 +00001498func (RsrcMgr *OpenOltResourceMgr) RemoveFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001499 var path string
1500 if cached {
1501 path = fmt.Sprintf(FlowGroupCached, groupID)
1502 } else {
1503 path = fmt.Sprintf(FlowGroup, groupID)
1504 }
npujarec5762e2020-01-01 14:08:48 +05301505 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001506 logger.Errorf(ctx, "Failed to remove resource %s due to %s", path, err)
Esin Karamand519bbf2020-07-01 11:16:03 +00001507 return err
Esin Karamanccb714b2019-11-29 15:02:06 +00001508 }
Esin Karamand519bbf2020-07-01 11:16:03 +00001509 return nil
Esin Karamanccb714b2019-11-29 15:02:06 +00001510}
1511
1512//GetFlowGroupFromKVStore fetches flow group from the KV store. Returns (false, {} error) if any problem occurs during
1513//fetching the data. Returns (true, groupInfo, nil) if the group is fetched successfully.
1514// Returns (false, {}, nil) if the group does not exists in the KV store.
npujarec5762e2020-01-01 14:08:48 +05301515func (RsrcMgr *OpenOltResourceMgr) GetFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) (bool, GroupInfo, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001516 var groupInfo GroupInfo
1517 var path string
1518 if cached {
1519 path = fmt.Sprintf(FlowGroupCached, groupID)
1520 } else {
1521 path = fmt.Sprintf(FlowGroup, groupID)
1522 }
npujarec5762e2020-01-01 14:08:48 +05301523 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001524 if err != nil {
1525 return false, groupInfo, err
1526 }
1527 if kvPair != nil && kvPair.Value != nil {
1528 Val, err := kvstore.ToByte(kvPair.Value)
1529 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001530 logger.Errorw(ctx, "Failed to convert flow group into byte array", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001531 return false, groupInfo, err
1532 }
1533 if err = json.Unmarshal(Val, &groupInfo); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001534 logger.Errorw(ctx, "Failed to unmarshal", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001535 return false, groupInfo, err
1536 }
1537 return true, groupInfo, nil
1538 }
1539 return false, groupInfo, nil
1540}