blob: 8f6271dffd96ab69557ad894865a56d4f28fed03 [file] [log] [blame]
Abhilash S.L7f17e402019-03-15 17:40:41 +05301/*
2 * Copyright 2019-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070017//Package resourcemanager provides the utility for managing resources
manikkaraj kbf256be2019-03-25 00:13:48 +053018package resourcemanager
Abhilash S.L7f17e402019-03-15 17:40:41 +053019
20import (
npujarec5762e2020-01-01 14:08:48 +053021 "context"
Girish Gowdru0c588b22019-04-23 23:24:56 -040022 "encoding/json"
23 "errors"
24 "fmt"
Andrea Campanellab83b39d2020-03-30 11:41:16 +020025 "github.com/opencord/voltha-openolt-adapter/internal/pkg/olterrors"
Girish Gowdru0c588b22019-04-23 23:24:56 -040026 "strconv"
27 "strings"
Girish Gowdra38d533d2020-03-30 20:38:51 -070028 "sync"
Neha Sharmacc656962020-04-14 14:26:11 +000029 "time"
Abhilash S.L7f17e402019-03-15 17:40:41 +053030
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"
50 // OnuPacketINPath path on the kvstore to store packetin gemport,which will be used for packetin, pcketout
51 //format: onu_packetin/<intfid>,<onuid>,<logicalport>
52 OnuPacketINPath = "onu_packetin/{%d,%d,%d}"
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +053053 //FlowIDsForGem flowids_per_gem/<intfid>
54 FlowIDsForGem = "flowids_per_gem/{%d}"
Esin Karamanccb714b2019-11-29 15:02:06 +000055 //McastQueuesForIntf multicast queues for pon interfaces
56 McastQueuesForIntf = "mcast_qs_for_int"
57 //FlowGroup flow_groups/<flow_group_id>
58 // A group is stored under this path on the KV store after it has been installed to the device.
59 // It should also be deleted after it has been removed from the device accordingly.
60 FlowGroup = "flow_groups/{%d}"
61 //FlowGroupCached flow_groups_cached/<flow_group_id>
62 // When a group add request received, we create the group without setting any members to it since we cannot
63 // set any members to a group until it is associated with a multicast flow. It is a BAL limitation.
64 // When the related multicast flow has been created we perform set members operation for the group.
65 // That is why we need to keep the members of a group until the multicast flow creation request comes.
66 // We preserve the groups under "FlowGroupsCached" directory in the KV store temporarily. Having set members,
67 // we remove the group from the cached group store.
68 FlowGroupCached = "flow_groups_cached/{%d}"
salmansiddiqui7ac62132019-08-22 03:58:50 +000069)
Abhilash S.L7f17e402019-03-15 17:40:41 +053070
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070071// FlowInfo holds the flow information
Abhilash S.L8ee90712019-04-29 16:24:22 +053072type FlowInfo struct {
73 Flow *openolt.Flow
74 FlowStoreCookie uint64
75 FlowCategory string
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053076 LogicalFlowID uint64
77}
78
79// OnuGemInfo holds onu information along with gem port list and uni port list
80type OnuGemInfo struct {
81 OnuID uint32
82 SerialNumber string
83 IntfID uint32
84 GemPorts []uint32
85 UniPorts []uint32
86}
87
88// PacketInInfoKey is the key for packet in gemport
89type PacketInInfoKey struct {
90 IntfID uint32
91 OnuID uint32
92 LogicalPort uint32
Abhilash S.L8ee90712019-04-29 16:24:22 +053093}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070094
Esin Karamanccb714b2019-11-29 15:02:06 +000095// GroupInfo holds group information
96type GroupInfo struct {
97 GroupID uint32
98 OutPorts []uint32
99}
100
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700101// OpenOltResourceMgr holds resource related information as provided below for each field
Abhilash S.L7f17e402019-03-15 17:40:41 +0530102type OpenOltResourceMgr struct {
sbarbaria8910ba2019-11-05 10:12:23 -0500103 DeviceID string // OLT device id
104 HostAndPort string // Host and port of the kv store to connect to
105 Args string // args
106 KVStore *db.Backend // backend kv store connection handle
Girish Gowdru0c588b22019-04-23 23:24:56 -0400107 DeviceType string
108 Host string // Host ip of the kv store
109 Port int // port of the kv store
110 DevInfo *openolt.DeviceInfo // device information
111 // array of pon resource managers per interface technology
112 ResourceMgrs map[uint32]*ponrmgr.PONResourceManager
Girish Gowdra38d533d2020-03-30 20:38:51 -0700113
114 // This protects concurrent gemport_id allocate/delete calls on a per PON port basis
115 GemPortIDMgmtLock []sync.RWMutex
116 // This protects concurrent alloc_id allocate/delete calls on a per PON port basis
117 AllocIDMgmtLock []sync.RWMutex
118 // This protects concurrent onu_id allocate/delete calls on a per PON port basis
119 OnuIDMgmtLock []sync.RWMutex
120 // This protects concurrent flow_id allocate/delete calls. We do not need this on a
121 // per PON port basis as flow IDs are unique across the OLT.
122 FlowIDMgmtLock sync.RWMutex
123
124 // This protects concurrent access to flowids_per_gem info stored on KV store
125 flowIDToGemInfoLock sync.RWMutex
Abhilash S.L7f17e402019-03-15 17:40:41 +0530126}
127
Neha Sharmacc656962020-04-14 14:26:11 +0000128func newKVClient(storeType string, address string, timeout time.Duration) (kvstore.Client, error) {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000129 logger.Infow("kv-store-type", log.Fields{"store": storeType})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400130 switch storeType {
131 case "consul":
Neha Sharmacc656962020-04-14 14:26:11 +0000132 return kvstore.NewConsulClient(address, timeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400133 case "etcd":
Neha Sharmacc656962020-04-14 14:26:11 +0000134 return kvstore.NewEtcdClient(address, timeout, log.FatalLevel)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400135 }
136 return nil, errors.New("unsupported-kv-store")
Abhilash S.L7f17e402019-03-15 17:40:41 +0530137}
138
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700139// SetKVClient sets the KV client and return a kv backend
sbarbaria8910ba2019-11-05 10:12:23 -0500140func SetKVClient(backend string, Host string, Port int, DeviceID string) *db.Backend {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400141 addr := Host + ":" + strconv.Itoa(Port)
142 // TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
143 // issue between kv store and backend , core is not calling NewBackend directly
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700144 kvClient, err := newKVClient(backend, addr, KvstoreTimeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400145 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000146 logger.Fatalw("Failed to init KV client\n", log.Fields{"err": err})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400147 return nil
148 }
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700149
sbarbaria8910ba2019-11-05 10:12:23 -0500150 kvbackend := &db.Backend{
Girish Gowdru0c588b22019-04-23 23:24:56 -0400151 Client: kvClient,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700152 StoreType: backend,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400153 Host: Host,
154 Port: Port,
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.
npujarec5762e2020-01-01 14:08:48 +0530164func NewResourceMgr(ctx context.Context, deviceID string, KVStoreHostPort string, kvStoreType string, deviceType string, devInfo *openolt.DeviceInfo) *OpenOltResourceMgr {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400165 var ResourceMgr OpenOltResourceMgr
Girish Kumar2ad402b2020-03-20 19:45:12 +0000166 logger.Debugf("Init new resource manager , host_port: %s, deviceid: %s", KVStoreHostPort, deviceID)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530167 ResourceMgr.HostAndPort = KVStoreHostPort
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700168 ResourceMgr.DeviceType = deviceType
169 ResourceMgr.DevInfo = devInfo
170 IPPort := strings.Split(KVStoreHostPort, ":")
171 ResourceMgr.Host = IPPort[0]
172 ResourceMgr.Port, _ = strconv.Atoi(IPPort[1])
Girish Gowdra38d533d2020-03-30 20:38:51 -0700173 NumPONPorts := devInfo.GetPonPorts()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530174
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700175 Backend := kvStoreType
Girish Gowdru0c588b22019-04-23 23:24:56 -0400176 ResourceMgr.KVStore = SetKVClient(Backend, ResourceMgr.Host,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700177 ResourceMgr.Port, deviceID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400178 if ResourceMgr.KVStore == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000179 logger.Error("Failed to setup KV store")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400180 }
181 Ranges := make(map[string]*openolt.DeviceInfo_DeviceResourceRanges)
182 RsrcMgrsByTech := make(map[string]*ponrmgr.PONResourceManager)
183 ResourceMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530184
Girish Gowdra38d533d2020-03-30 20:38:51 -0700185 ResourceMgr.AllocIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
186 ResourceMgr.GemPortIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
187 ResourceMgr.OnuIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
188
Girish Gowdru0c588b22019-04-23 23:24:56 -0400189 // TODO self.args = registry('main').get_args()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530190
Girish Gowdru0c588b22019-04-23 23:24:56 -0400191 /*
192 If a legacy driver returns protobuf without any ranges,s synthesize one from
Gamze Abakafee36392019-10-03 11:17:24 +0000193 the legacy global per-device information. This, in theory, is temporary until
Girish Gowdru0c588b22019-04-23 23:24:56 -0400194 the legacy drivers are upgrade to support pool ranges.
195 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700196 if devInfo.Ranges == nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400197 var ranges openolt.DeviceInfo_DeviceResourceRanges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700198 ranges.Technology = devInfo.GetTechnology()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530199
Girish Gowdru0c588b22019-04-23 23:24:56 -0400200 var index uint32
201 for index = 0; index < NumPONPorts; index++ {
202 ranges.IntfIds = append(ranges.IntfIds, index)
203 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530204
Abhilash S.L8ee90712019-04-29 16:24:22 +0530205 var Pool openolt.DeviceInfo_DeviceResourceRanges_Pool
Girish Gowdru0c588b22019-04-23 23:24:56 -0400206 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700207 Pool.Start = devInfo.OnuIdStart
208 Pool.End = devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400209 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
cbabuabf02352019-10-15 13:14:56 +0200210 onuPool := Pool
211 ranges.Pools = append(ranges.Pools, &onuPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530212
Girish Gowdru0c588b22019-04-23 23:24:56 -0400213 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700214 Pool.Start = devInfo.AllocIdStart
215 Pool.End = devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400216 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200217 allocPool := Pool
218 ranges.Pools = append(ranges.Pools, &allocPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530219
Girish Gowdru0c588b22019-04-23 23:24:56 -0400220 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700221 Pool.Start = devInfo.GemportIdStart
222 Pool.End = devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400223 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200224 gemPool := Pool
225 ranges.Pools = append(ranges.Pools, &gemPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530226
Girish Gowdru0c588b22019-04-23 23:24:56 -0400227 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700228 Pool.Start = devInfo.FlowIdStart
229 Pool.End = devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400230 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
Abhilash S.L8ee90712019-04-29 16:24:22 +0530231 ranges.Pools = append(ranges.Pools, &Pool)
232 // Add to device info
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700233 devInfo.Ranges = append(devInfo.Ranges, &ranges)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400234 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530235
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700236 // Create a separate Resource Manager instance for each range. This assumes that
Girish Gowdru0c588b22019-04-23 23:24:56 -0400237 // each technology is represented by only a single range
238 var GlobalPONRsrcMgr *ponrmgr.PONResourceManager
239 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700240 for _, TechRange := range devInfo.Ranges {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400241 technology := TechRange.Technology
Girish Kumar2ad402b2020-03-20 19:45:12 +0000242 logger.Debugf("Device info technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400243 Ranges[technology] = TechRange
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700244 RsrcMgrsByTech[technology], err = ponrmgr.NewPONResourceManager(technology, deviceType, deviceID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400245 Backend, ResourceMgr.Host, ResourceMgr.Port)
246 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000247 logger.Errorf("Failed to create pon resource manager instance for technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400248 return nil
249 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700250 // resource_mgrs_by_tech[technology] = resource_mgr
Girish Gowdru0c588b22019-04-23 23:24:56 -0400251 if GlobalPONRsrcMgr == nil {
252 GlobalPONRsrcMgr = RsrcMgrsByTech[technology]
253 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700254 for _, IntfID := range TechRange.IntfIds {
255 ResourceMgr.ResourceMgrs[uint32(IntfID)] = RsrcMgrsByTech[technology]
Girish Gowdru0c588b22019-04-23 23:24:56 -0400256 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700257 // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
npujarec5762e2020-01-01 14:08:48 +0530258 InitializeDeviceResourceRangeAndPool(ctx, RsrcMgrsByTech[technology], GlobalPONRsrcMgr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700259 TechRange, devInfo)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400260 }
261 // After we have initialized resource ranges, initialize the
262 // resource pools accordingly.
263 for _, PONRMgr := range RsrcMgrsByTech {
npujarec5762e2020-01-01 14:08:48 +0530264 _ = PONRMgr.InitDeviceResourcePool(ctx)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400265 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000266 logger.Info("Initialization of resource manager success!")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400267 return &ResourceMgr
Abhilash S.L7f17e402019-03-15 17:40:41 +0530268}
269
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700270// InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply
271// device specific information. If KV doesn't exist
272// or is broader than the device, the device's information will
273// dictate the range limits
npujarec5762e2020-01-01 14:08:48 +0530274func InitializeDeviceResourceRangeAndPool(ctx context.Context, ponRMgr *ponrmgr.PONResourceManager, globalPONRMgr *ponrmgr.PONResourceManager,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700275 techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530276
Girish Gowdru0c588b22019-04-23 23:24:56 -0400277 // init the resource range pool according to the sharing type
Abhilash S.L7f17e402019-03-15 17:40:41 +0530278
Girish Kumar2ad402b2020-03-20 19:45:12 +0000279 logger.Debugf("Resource range pool init for technology %s", ponRMgr.Technology)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700280 // first load from KV profiles
npujarec5762e2020-01-01 14:08:48 +0530281 status := ponRMgr.InitResourceRangesFromKVStore(ctx)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700282 if !status {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000283 logger.Debugf("Failed to load resource ranges from KV store for tech %s", ponRMgr.Technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400284 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530285
Girish Gowdru0c588b22019-04-23 23:24:56 -0400286 /*
287 Then apply device specific information. If KV doesn't exist
Gamze Abakafee36392019-10-03 11:17:24 +0000288 or is broader than the device, the device's information will
Girish Gowdru0c588b22019-04-23 23:24:56 -0400289 dictate the range limits
290 */
Girish Kumar2ad402b2020-03-20 19:45:12 +0000291 logger.Debugw("Using device info to init pon resource ranges", log.Fields{"Tech": ponRMgr.Technology})
Abhilash S.L7f17e402019-03-15 17:40:41 +0530292
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700293 ONUIDStart := devInfo.OnuIdStart
294 ONUIDEnd := devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400295 ONUIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
296 ONUIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700297 AllocIDStart := devInfo.AllocIdStart
298 AllocIDEnd := devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400299 AllocIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
300 AllocIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700301 GEMPortIDStart := devInfo.GemportIdStart
302 GEMPortIDEnd := devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400303 GEMPortIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
304 GEMPortIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700305 FlowIDStart := devInfo.FlowIdStart
306 FlowIDEnd := devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400307 FlowIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
308 FlowIDSharedPoolID := uint32(0)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530309
Girish Gowdru0c588b22019-04-23 23:24:56 -0400310 var FirstIntfPoolID uint32
311 var SharedPoolID uint32
Abhilash S.L7f17e402019-03-15 17:40:41 +0530312
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400313 /*
314 * 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 -0700315 * if resources are shared across interfaces then SharedPoolID is given a positive number.
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400316 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700317 for _, FirstIntfPoolID = range techRange.IntfIds {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400318 // skip the intf id 0
319 if FirstIntfPoolID == 0 {
320 continue
321 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400322 break
323 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530324
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700325 for _, RangePool := range techRange.Pools {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400326 if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400327 SharedPoolID = FirstIntfPoolID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400328 } else if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_SAME_TECH {
329 SharedPoolID = FirstIntfPoolID
330 } else {
331 SharedPoolID = 0
332 }
333 if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID {
334 ONUIDStart = RangePool.Start
335 ONUIDEnd = RangePool.End
336 ONUIDShared = RangePool.Sharing
337 ONUIDSharedPoolID = SharedPoolID
338 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID {
339 AllocIDStart = RangePool.Start
340 AllocIDEnd = RangePool.End
341 AllocIDShared = RangePool.Sharing
342 AllocIDSharedPoolID = SharedPoolID
343 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID {
344 GEMPortIDStart = RangePool.Start
345 GEMPortIDEnd = RangePool.End
346 GEMPortIDShared = RangePool.Sharing
347 GEMPortIDSharedPoolID = SharedPoolID
348 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID {
349 FlowIDStart = RangePool.Start
350 FlowIDEnd = RangePool.End
351 FlowIDShared = RangePool.Sharing
352 FlowIDSharedPoolID = SharedPoolID
353 }
354 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530355
Girish Kumar2ad402b2020-03-20 19:45:12 +0000356 logger.Debugw("Device info init", log.Fields{"technology": techRange.Technology,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400357 "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd, "onu_id_shared_pool_id": ONUIDSharedPoolID,
358 "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd,
359 "alloc_id_shared_pool_id": AllocIDSharedPoolID,
360 "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd,
361 "gemport_id_shared_pool_id": GEMPortIDSharedPoolID,
362 "flow_id_start": FlowIDStart,
363 "flow_id_end_idx": FlowIDEnd,
364 "flow_id_shared_pool_id": FlowIDSharedPoolID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700365 "intf_ids": techRange.IntfIds,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400366 "uni_id_start": 0,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700367 "uni_id_end_idx": 1, /*MaxUNIIDperONU()*/
368 })
Abhilash S.L7f17e402019-03-15 17:40:41 +0530369
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700370 ponRMgr.InitDefaultPONResourceRanges(ONUIDStart, ONUIDEnd, ONUIDSharedPoolID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400371 AllocIDStart, AllocIDEnd, AllocIDSharedPoolID,
372 GEMPortIDStart, GEMPortIDEnd, GEMPortIDSharedPoolID,
373 FlowIDStart, FlowIDEnd, FlowIDSharedPoolID, 0, 1,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700374 devInfo.PonPorts, techRange.IntfIds)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530375
Girish Gowdru0c588b22019-04-23 23:24:56 -0400376 // For global sharing, make sure to refresh both local and global resource manager instances' range
Abhilash S.L7f17e402019-03-15 17:40:41 +0530377
Girish Gowdru0c588b22019-04-23 23:24:56 -0400378 if ONUIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700379 globalPONRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400380 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700381 ponRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
382 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400383 }
384 if AllocIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700385 globalPONRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400386 "", 0, nil)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530387
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700388 ponRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
389 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400390 }
391 if GEMPortIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700392 globalPONRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400393 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700394 ponRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
395 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400396 }
397 if FlowIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700398 globalPONRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400399 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700400 ponRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
401 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400402 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530403
Girish Gowdru0c588b22019-04-23 23:24:56 -0400404 // Make sure loaded range fits the platform bit encoding ranges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700405 ponRMgr.UpdateRanges(ponrmgr.UNI_ID_START_IDX, 0, ponrmgr.UNI_ID_END_IDX /* TODO =OpenOltPlatform.MAX_UNIS_PER_ONU-1*/, 1, "", 0, nil)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530406}
407
Devmalya Paul495b94a2019-08-27 19:42:00 -0400408// Delete clears used resources for the particular olt device being deleted
npujarec5762e2020-01-01 14:08:48 +0530409func (RsrcMgr *OpenOltResourceMgr) Delete(ctx context.Context) error {
Devmalya Paul495b94a2019-08-27 19:42:00 -0400410 /* TODO
411 def __del__(self):
412 self.log.info("clearing-device-resource-pool")
413 for key, resource_mgr in self.resource_mgrs.iteritems():
414 resource_mgr.clear_device_resource_pool()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530415
Devmalya Paul495b94a2019-08-27 19:42:00 -0400416 def assert_pon_id_limit(self, pon_intf_id):
417 assert pon_intf_id in self.resource_mgrs
Abhilash S.L7f17e402019-03-15 17:40:41 +0530418
Devmalya Paul495b94a2019-08-27 19:42:00 -0400419 def assert_onu_id_limit(self, pon_intf_id, onu_id):
420 self.assert_pon_id_limit(pon_intf_id)
421 self.resource_mgrs[pon_intf_id].assert_resource_limits(onu_id, PONResourceManager.ONU_ID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530422
Devmalya Paul495b94a2019-08-27 19:42:00 -0400423 @property
424 def max_uni_id_per_onu(self):
425 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 +0530426
Devmalya Paul495b94a2019-08-27 19:42:00 -0400427 def assert_uni_id_limit(self, pon_intf_id, onu_id, uni_id):
428 self.assert_onu_id_limit(pon_intf_id, onu_id)
429 self.resource_mgrs[pon_intf_id].assert_resource_limits(uni_id, PONResourceManager.UNI_ID)
430 */
431 for _, rsrcMgr := range RsrcMgr.ResourceMgrs {
npujarec5762e2020-01-01 14:08:48 +0530432 if err := rsrcMgr.ClearDeviceResourcePool(ctx); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000433 logger.Debug("Failed to clear device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400434 return err
435 }
436 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000437 logger.Debug("Cleared device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400438 return nil
439}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530440
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700441// GetONUID returns the available OnuID for the given pon-port
npujarec5762e2020-01-01 14:08:48 +0530442func (RsrcMgr *OpenOltResourceMgr) GetONUID(ctx context.Context, ponIntfID uint32) (uint32, error) {
salmansiddiqui352a45c2019-08-19 10:15:36 +0000443 // Check if Pon Interface ID is present in Resource-manager-map
Girish Gowdrab77ded92020-04-08 11:45:05 -0700444 RsrcMgr.OnuIDMgmtLock[ponIntfID].Lock()
445 defer RsrcMgr.OnuIDMgmtLock[ponIntfID].Unlock()
446
salmansiddiqui352a45c2019-08-19 10:15:36 +0000447 if _, ok := RsrcMgr.ResourceMgrs[ponIntfID]; !ok {
448 err := errors.New("invalid-pon-interface-" + strconv.Itoa(int(ponIntfID)))
449 return 0, err
450 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400451 // Get ONU id for a provided pon interface ID.
npujarec5762e2020-01-01 14:08:48 +0530452 ONUID, err := RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ctx, ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400453 ponrmgr.ONU_ID, 1)
454 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000455 logger.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700456 ponIntfID, ponrmgr.ONU_ID)
cbabuabf02352019-10-15 13:14:56 +0200457 return 0, err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400458 }
459 if ONUID != nil {
npujarec5762e2020-01-01 14:08:48 +0530460 RsrcMgr.ResourceMgrs[ponIntfID].InitResourceMap(ctx, fmt.Sprintf("%d,%d", ponIntfID, ONUID[0]))
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700461 return ONUID[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400462 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530463
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700464 return 0, err // return OnuID 0 on error
Abhilash S.L7f17e402019-03-15 17:40:41 +0530465}
466
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700467// GetFlowIDInfo returns the slice of flow info of the given pon-port
468// Note: For flows which trap from the NNI and not really associated with any particular
469// 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 +0530470func (RsrcMgr *OpenOltResourceMgr) GetFlowIDInfo(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32, flowID uint32) *[]FlowInfo {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530471 var flows []FlowInfo
472
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700473 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530474 if err := RsrcMgr.ResourceMgrs[ponIntfID].GetFlowIDInfo(ctx, FlowPath, flowID, &flows); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000475 logger.Errorw("Error while getting flows from KV store", log.Fields{"flowId": flowID})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530476 return nil
477 }
478 if len(flows) == 0 {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000479 logger.Debugw("No flowInfo found in KV store", log.Fields{"flowPath": FlowPath})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530480 return nil
481 }
482 return &flows
483}
484
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700485// GetCurrentFlowIDsForOnu fetches flow ID from the resource manager
486// Note: For flows which trap from the NNI and not really associated with any particular
487// 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 +0530488func (RsrcMgr *OpenOltResourceMgr) GetCurrentFlowIDsForOnu(ctx context.Context, PONIntfID uint32, ONUID int32, UNIID int32) []uint32 {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700489
Abhilash S.L8ee90712019-04-29 16:24:22 +0530490 FlowPath := fmt.Sprintf("%d,%d,%d", PONIntfID, ONUID, UNIID)
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700491 if mgrs, exist := RsrcMgr.ResourceMgrs[PONIntfID]; exist {
npujarec5762e2020-01-01 14:08:48 +0530492 return mgrs.GetCurrentFlowIDsForOnu(ctx, FlowPath)
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700493 }
494 return nil
Abhilash S.L8ee90712019-04-29 16:24:22 +0530495}
496
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700497// UpdateFlowIDInfo updates flow info for the given pon interface, onu id, and uni id
498// Note: For flows which trap from the NNI and not really associated with any particular
499// 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 +0530500func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDInfo(ctx context.Context, ponIntfID int32, onuID int32, uniID int32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700501 flowID uint32, flowData *[]FlowInfo) error {
502 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530503 return RsrcMgr.ResourceMgrs[uint32(ponIntfID)].UpdateFlowIDInfoForOnu(ctx, FlowPath, flowID, *flowData)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530504}
505
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700506// GetFlowID return flow ID for a given pon interface id, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530507func (RsrcMgr *OpenOltResourceMgr) GetFlowID(ctx context.Context, ponIntfID uint32, ONUID int32, uniID int32,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400508 gemportID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700509 flowStoreCookie uint64,
Gamze Abaka724d0852020-03-18 12:10:24 +0000510 flowCategory string, vlanVid uint32, vlanPcp ...uint32) (uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530511
Girish Gowdru0c588b22019-04-23 23:24:56 -0400512 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700513 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, ONUID, uniID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700514
515 RsrcMgr.FlowIDMgmtLock.Lock()
516 defer RsrcMgr.FlowIDMgmtLock.Unlock()
517
npujarec5762e2020-01-01 14:08:48 +0530518 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(ctx, FlowPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400519 if FlowIDs != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000520 logger.Debugw("Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "ONUID": ONUID, "uniID": uniID, "KVpath": FlowPath})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700521 for _, flowID := range FlowIDs {
npujarec5762e2020-01-01 14:08:48 +0530522 FlowInfo := RsrcMgr.GetFlowIDInfo(ctx, ponIntfID, int32(ONUID), int32(uniID), uint32(flowID))
Gamze Abaka724d0852020-03-18 12:10:24 +0000523 er := getFlowIDFromFlowInfo(FlowInfo, flowID, gemportID, flowStoreCookie, flowCategory, vlanVid, vlanPcp...)
salmansiddiqui7ac62132019-08-22 03:58:50 +0000524 if er == nil {
Gamze Abaka724d0852020-03-18 12:10:24 +0000525 log.Debugw("Found flowid for the vlan, pcp, and gem",
526 log.Fields{"flowID": flowID, "vlanVid": vlanVid, "vlanPcp": vlanPcp, "gemPortID": gemportID})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000527 return flowID, er
Abhilash S.L8ee90712019-04-29 16:24:22 +0530528 }
529 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400530 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000531 logger.Debug("No matching flows with flow cookie or flow category, allocating new flowid")
npujarec5762e2020-01-01 14:08:48 +0530532 FlowIDs, err = RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ctx, ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400533 ponrmgr.FLOW_ID, 1)
534 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000535 logger.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700536 ponIntfID, ponrmgr.FLOW_ID)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400537 return uint32(0), err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400538 }
539 if FlowIDs != nil {
npujarec5762e2020-01-01 14:08:48 +0530540 _ = RsrcMgr.ResourceMgrs[ponIntfID].UpdateFlowIDForOnu(ctx, FlowPath, FlowIDs[0], true)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700541 return FlowIDs[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400542 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530543
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700544 return 0, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530545}
546
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700547// GetAllocID return the first Alloc ID for a given pon interface id and onu id and then update the resource map on
548// the KV store with the list of alloc_ids allocated for the pon_intf_onu_id tuple
549// 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 +0530550func (RsrcMgr *OpenOltResourceMgr) GetAllocID(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530551
Girish Gowdru0c588b22019-04-23 23:24:56 -0400552 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700553 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700554
555 RsrcMgr.AllocIDMgmtLock[intfID].Lock()
556 defer RsrcMgr.AllocIDMgmtLock[intfID].Unlock()
557
npujarec5762e2020-01-01 14:08:48 +0530558 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400559 if AllocID != nil {
560 // Since we support only one alloc_id for the ONU at the moment,
561 // return the first alloc_id in the list, if available, for that
562 // ONU.
Girish Kumar2ad402b2020-03-20 19:45:12 +0000563 logger.Debugw("Retrieved alloc ID from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400564 return AllocID[0]
565 }
npujarec5762e2020-01-01 14:08:48 +0530566 AllocID, err = RsrcMgr.ResourceMgrs[intfID].GetResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400567 ponrmgr.ALLOC_ID, 1)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530568
Girish Gowdru0c588b22019-04-23 23:24:56 -0400569 if AllocID == nil || err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000570 logger.Error("Failed to allocate alloc id")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400571 return 0
572 }
573 // update the resource map on KV store with the list of alloc_id
574 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530575 err = RsrcMgr.ResourceMgrs[intfID].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID, AllocID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400576 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000577 logger.Error("Failed to update Alloc ID")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400578 return 0
579 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000580 logger.Debugw("Allocated new Tcont from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400581 return AllocID[0]
Abhilash S.L7f17e402019-03-15 17:40:41 +0530582}
583
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700584// UpdateAllocIdsForOnu updates alloc ids in kv store for a given pon interface id, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530585func (RsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ctx context.Context, ponPort uint32, onuID uint32, uniID uint32, allocID []uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530586
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700587 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530588 return RsrcMgr.ResourceMgrs[ponPort].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700589 allocID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530590}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700591
592// GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530593func (RsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(ctx context.Context, intfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700594 uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530595
Girish Gowdru0c588b22019-04-23 23:24:56 -0400596 /* Get gem ports for given pon interface , onu id and uni id. */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530597
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700598 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530599 return RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530600}
601
Gamze Abakafee36392019-10-03 11:17:24 +0000602// GetCurrentAllocIDsForOnu returns alloc ids for given pon interface and onu id
npujarec5762e2020-01-01 14:08:48 +0530603func (RsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDsForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530604
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700605 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530606 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400607 if AllocID != nil {
Gamze Abakafee36392019-10-03 11:17:24 +0000608 return AllocID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400609 }
Gamze Abakafee36392019-10-03 11:17:24 +0000610 return []uint32{}
611}
612
613// RemoveAllocIDForOnu removes the alloc id for given pon interface, onu id, uni id and alloc id
npujarec5762e2020-01-01 14:08:48 +0530614func (RsrcMgr *OpenOltResourceMgr) RemoveAllocIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, allocID uint32) {
615 allocIDs := RsrcMgr.GetCurrentAllocIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000616 for i := 0; i < len(allocIDs); i++ {
617 if allocIDs[i] == allocID {
618 allocIDs = append(allocIDs[:i], allocIDs[i+1:]...)
619 break
620 }
621 }
npujarec5762e2020-01-01 14:08:48 +0530622 err := RsrcMgr.UpdateAllocIdsForOnu(ctx, intfID, onuID, uniID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000623 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000624 logger.Errorf("Failed to Remove Alloc Id For Onu. IntfID %d onuID %d uniID %d allocID %d",
Gamze Abakafee36392019-10-03 11:17:24 +0000625 intfID, onuID, uniID, allocID)
626 }
627}
628
629// RemoveGemPortIDForOnu removes the gem port id for given pon interface, onu id, uni id and gem port id
npujarec5762e2020-01-01 14:08:48 +0530630func (RsrcMgr *OpenOltResourceMgr) RemoveGemPortIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, gemPortID uint32) {
631 gemPortIDs := RsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000632 for i := 0; i < len(gemPortIDs); i++ {
633 if gemPortIDs[i] == gemPortID {
634 gemPortIDs = append(gemPortIDs[:i], gemPortIDs[i+1:]...)
635 break
636 }
637 }
npujarec5762e2020-01-01 14:08:48 +0530638 err := RsrcMgr.UpdateGEMPortIDsForOnu(ctx, intfID, onuID, uniID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000639 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000640 logger.Errorf("Failed to Remove Gem Id For Onu. IntfID %d onuID %d uniID %d gemPortId %d",
Gamze Abakafee36392019-10-03 11:17:24 +0000641 intfID, onuID, uniID, gemPortID)
642 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530643}
644
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700645// UpdateGEMportsPonportToOnuMapOnKVStore updates onu and uni id associated with the gem port to the kv store
646// This stored information is used when packet_indication is received and we need to derive the ONU Id for which
647// the packet arrived based on the pon_intf and gemport available in the packet_indication
npujarec5762e2020-01-01 14:08:48 +0530648func (RsrcMgr *OpenOltResourceMgr) UpdateGEMportsPonportToOnuMapOnKVStore(ctx context.Context, gemPorts []uint32, PonPort uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700649 onuID uint32, uniID uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530650
Girish Gowdru0c588b22019-04-23 23:24:56 -0400651 /* Update onu and uni id associated with the gem port to the kv store. */
652 var IntfGEMPortPath string
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700653 Data := fmt.Sprintf("%d %d", onuID, uniID)
654 for _, GEM := range gemPorts {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400655 IntfGEMPortPath = fmt.Sprintf("%d,%d", PonPort, GEM)
656 Val, err := json.Marshal(Data)
657 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000658 logger.Error("failed to Marshal")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400659 return err
660 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700661
npujarec5762e2020-01-01 14:08:48 +0530662 if err = RsrcMgr.KVStore.Put(ctx, IntfGEMPortPath, Val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000663 logger.Errorf("Failed to update resource %s", IntfGEMPortPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400664 return err
665 }
666 }
667 return nil
Abhilash S.L7f17e402019-03-15 17:40:41 +0530668}
669
Gamze Abakafee36392019-10-03 11:17:24 +0000670// RemoveGEMportPonportToOnuMapOnKVStore removes the relationship between the gem port and pon port
npujarec5762e2020-01-01 14:08:48 +0530671func (RsrcMgr *OpenOltResourceMgr) RemoveGEMportPonportToOnuMapOnKVStore(ctx context.Context, GemPort uint32, PonPort uint32) {
Gamze Abakafee36392019-10-03 11:17:24 +0000672 IntfGEMPortPath := fmt.Sprintf("%d,%d", PonPort, GemPort)
npujarec5762e2020-01-01 14:08:48 +0530673 err := RsrcMgr.KVStore.Delete(ctx, IntfGEMPortPath)
Gamze Abakafee36392019-10-03 11:17:24 +0000674 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000675 logger.Errorf("Failed to Remove Gem port-Pon port to onu map on kv store. Gem %d PonPort %d", GemPort, PonPort)
Gamze Abakafee36392019-10-03 11:17:24 +0000676 }
677}
678
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700679// GetGEMPortID gets gem port id for a particular pon port, onu id and uni id and then update the resource map on
680// the KV store with the list of gemport_id allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530681func (RsrcMgr *OpenOltResourceMgr) GetGEMPortID(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700682 uniID uint32, NumOfPorts uint32) ([]uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530683
Girish Gowdru0c588b22019-04-23 23:24:56 -0400684 /* Get gem port id for a particular pon port, onu id
685 and uni id.
686 */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530687
Girish Gowdru0c588b22019-04-23 23:24:56 -0400688 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700689 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530690
Girish Gowdrab77ded92020-04-08 11:45:05 -0700691 RsrcMgr.GemPortIDMgmtLock[ponPort].Lock()
692 defer RsrcMgr.GemPortIDMgmtLock[ponPort].Unlock()
693
npujarec5762e2020-01-01 14:08:48 +0530694 GEMPortList := RsrcMgr.ResourceMgrs[ponPort].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400695 if GEMPortList != nil {
696 return GEMPortList, nil
697 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530698
npujarec5762e2020-01-01 14:08:48 +0530699 GEMPortList, err = RsrcMgr.ResourceMgrs[ponPort].GetResourceID(ctx, ponPort,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400700 ponrmgr.GEMPORT_ID, NumOfPorts)
701 if err != nil && GEMPortList == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000702 logger.Errorf("Failed to get gem port id for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400703 return nil, err
704 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530705
Girish Gowdru0c588b22019-04-23 23:24:56 -0400706 // update the resource map on KV store with the list of gemport_id
707 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530708 err = RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400709 GEMPortList)
710 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000711 logger.Errorf("Failed to update GEM ports to kv store for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400712 return nil, err
713 }
npujarec5762e2020-01-01 14:08:48 +0530714 _ = RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(ctx, GEMPortList, ponPort,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700715 onuID, uniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400716 return GEMPortList, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530717}
718
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700719// 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 +0530720func (RsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700721 uniID uint32, GEMPortList []uint32) error {
722 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530723 return RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400724 GEMPortList)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530725
726}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700727
728// FreeonuID releases(make free) onu id for a particular pon-port
npujarec5762e2020-01-01 14:08:48 +0530729func (RsrcMgr *OpenOltResourceMgr) FreeonuID(ctx context.Context, intfID uint32, onuID []uint32) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700730
Girish Gowdra38d533d2020-03-30 20:38:51 -0700731 RsrcMgr.OnuIDMgmtLock[intfID].Lock()
Girish Gowdrab77ded92020-04-08 11:45:05 -0700732 defer RsrcMgr.OnuIDMgmtLock[intfID].Unlock()
733
npujarec5762e2020-01-01 14:08:48 +0530734 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID, ponrmgr.ONU_ID, onuID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530735
Girish Gowdru0c588b22019-04-23 23:24:56 -0400736 /* Free onu id for a particular interface.*/
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700737 var IntfonuID string
738 for _, onu := range onuID {
739 IntfonuID = fmt.Sprintf("%d,%d", intfID, onu)
npujarec5762e2020-01-01 14:08:48 +0530740 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfonuID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400741 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530742}
743
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700744// FreeFlowID returns the free flow id for a given interface, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530745func (RsrcMgr *OpenOltResourceMgr) FreeFlowID(ctx context.Context, IntfID uint32, onuID int32,
Devmalya Paul495b94a2019-08-27 19:42:00 -0400746 uniID int32, FlowID uint32) {
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400747 var IntfONUID string
748 var err error
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530749
Girish Gowdrab77ded92020-04-08 11:45:05 -0700750 RsrcMgr.FlowIDMgmtLock.Lock()
751 defer RsrcMgr.FlowIDMgmtLock.Unlock()
752
753 FlowIds := make([]uint32, 0)
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530754 FlowIds = append(FlowIds, FlowID)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700755 IntfONUID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530756 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(ctx, IntfONUID, FlowID, false)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400757 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000758 logger.Errorw("Failed to Update flow id for", log.Fields{"intf": IntfONUID})
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400759 }
npujarec5762e2020-01-01 14:08:48 +0530760 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(ctx, IntfONUID, FlowID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700761
npujarec5762e2020-01-01 14:08:48 +0530762 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.FLOW_ID, FlowIds)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400763}
764
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700765// FreeFlowIDs releases the flow Ids
npujarec5762e2020-01-01 14:08:48 +0530766func (RsrcMgr *OpenOltResourceMgr) FreeFlowIDs(ctx context.Context, IntfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700767 uniID uint32, FlowID []uint32) {
Girish Gowdra38d533d2020-03-30 20:38:51 -0700768 RsrcMgr.FlowIDMgmtLock.Lock()
Girish Gowdrab77ded92020-04-08 11:45:05 -0700769 defer RsrcMgr.FlowIDMgmtLock.Unlock()
770
npujarec5762e2020-01-01 14:08:48 +0530771 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.FLOW_ID, FlowID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530772
Abhilash S.L8ee90712019-04-29 16:24:22 +0530773 var IntfOnuIDUniID string
Girish Gowdru0c588b22019-04-23 23:24:56 -0400774 var err error
775 for _, flow := range FlowID {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700776 IntfOnuIDUniID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530777 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(ctx, IntfOnuIDUniID, flow, false)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400778 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000779 logger.Errorw("Failed to Update flow id for", log.Fields{"intf": IntfOnuIDUniID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400780 }
npujarec5762e2020-01-01 14:08:48 +0530781 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(ctx, IntfOnuIDUniID, flow)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400782 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530783}
784
Gamze Abakafee36392019-10-03 11:17:24 +0000785// FreeAllocID frees AllocID on the PON resource pool and also frees the allocID association
786// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530787func (RsrcMgr *OpenOltResourceMgr) FreeAllocID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000788 uniID uint32, allocID uint32) {
Girish Gowdrab77ded92020-04-08 11:45:05 -0700789 RsrcMgr.AllocIDMgmtLock[IntfID].Lock()
790 defer RsrcMgr.AllocIDMgmtLock[IntfID].Unlock()
791
npujarec5762e2020-01-01 14:08:48 +0530792 RsrcMgr.RemoveAllocIDForOnu(ctx, IntfID, onuID, uniID, allocID)
Gamze Abakafee36392019-10-03 11:17:24 +0000793 allocIDs := make([]uint32, 0)
794 allocIDs = append(allocIDs, allocID)
npujarec5762e2020-01-01 14:08:48 +0530795 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.ALLOC_ID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000796}
797
798// FreeGemPortID frees GemPortID on the PON resource pool and also frees the gemPortID association
799// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530800func (RsrcMgr *OpenOltResourceMgr) FreeGemPortID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000801 uniID uint32, gemPortID uint32) {
Girish Gowdrab77ded92020-04-08 11:45:05 -0700802 RsrcMgr.GemPortIDMgmtLock[IntfID].Lock()
803 defer RsrcMgr.GemPortIDMgmtLock[IntfID].Unlock()
804
npujarec5762e2020-01-01 14:08:48 +0530805 RsrcMgr.RemoveGemPortIDForOnu(ctx, IntfID, onuID, uniID, gemPortID)
Gamze Abakafee36392019-10-03 11:17:24 +0000806 gemPortIDs := make([]uint32, 0)
807 gemPortIDs = append(gemPortIDs, gemPortID)
npujarec5762e2020-01-01 14:08:48 +0530808 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.GEMPORT_ID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000809}
810
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700811// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id, and the clears the
812// resource map and the onuID associated with (pon_intf_id, gemport_id) tuple,
npujarec5762e2020-01-01 14:08:48 +0530813func (RsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530814
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700815 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530816
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700817 RsrcMgr.AllocIDMgmtLock[intfID].Lock()
Girish Gowdrab77ded92020-04-08 11:45:05 -0700818 AllocIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Matteo Scandolod625b4c2020-04-02 16:16:01 -0700819
npujarec5762e2020-01-01 14:08:48 +0530820 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400821 ponrmgr.ALLOC_ID,
822 AllocIDs)
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700823 RsrcMgr.AllocIDMgmtLock[intfID].Unlock()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530824
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700825 RsrcMgr.GemPortIDMgmtLock[intfID].Lock()
npujarec5762e2020-01-01 14:08:48 +0530826 GEMPortIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
827 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400828 ponrmgr.GEMPORT_ID,
829 GEMPortIDs)
Girish Gowdra0c595ba2020-04-09 15:04:27 -0700830 RsrcMgr.GemPortIDMgmtLock[intfID].Unlock()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530831
Girish Gowdra38d533d2020-03-30 20:38:51 -0700832 RsrcMgr.FlowIDMgmtLock.Lock()
npujarec5762e2020-01-01 14:08:48 +0530833 FlowIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentFlowIDsForOnu(ctx, IntfOnuIDUniID)
834 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400835 ponrmgr.FLOW_ID,
836 FlowIDs)
Girish Gowdra38d533d2020-03-30 20:38:51 -0700837 RsrcMgr.FlowIDMgmtLock.Unlock()
838
Girish Gowdru0c588b22019-04-23 23:24:56 -0400839 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
npujarec5762e2020-01-01 14:08:48 +0530840 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400841 // Clear the ONU Id associated with the (pon_intf_id, gemport_id) tuple.
842 for _, GEM := range GEMPortIDs {
npujarec5762e2020-01-01 14:08:48 +0530843 _ = RsrcMgr.KVStore.Delete(ctx, fmt.Sprintf("%d,%d", intfID, GEM))
Girish Gowdru0c588b22019-04-23 23:24:56 -0400844 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530845}
846
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700847// IsFlowCookieOnKVStore checks if the given flow cookie is present on the kv store
848// Returns true if the flow cookie is found, otherwise it returns false
npujarec5762e2020-01-01 14:08:48 +0530849func (RsrcMgr *OpenOltResourceMgr) IsFlowCookieOnKVStore(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700850 flowStoreCookie uint64) bool {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530851
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700852 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530853 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(ctx, FlowPath)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400854 if FlowIDs != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000855 logger.Debugw("Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "onuID": onuID, "uniID": uniID, "KVpath": FlowPath})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700856 for _, flowID := range FlowIDs {
npujarec5762e2020-01-01 14:08:48 +0530857 FlowInfo := RsrcMgr.GetFlowIDInfo(ctx, ponIntfID, int32(onuID), int32(uniID), uint32(flowID))
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400858 if FlowInfo != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000859 logger.Debugw("Found flows", log.Fields{"flows": *FlowInfo, "flowId": flowID})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400860 for _, Info := range *FlowInfo {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700861 if Info.FlowStoreCookie == flowStoreCookie {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000862 logger.Debug("Found flow matching with flowStore cookie", log.Fields{"flowId": flowID, "flowStoreCookie": flowStoreCookie})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400863 return true
864 }
865 }
866 }
867 }
868 }
869 return false
870}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400871
salmansiddiqui7ac62132019-08-22 03:58:50 +0000872// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000873// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530874func (RsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) []uint32 {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000875 Path := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000876 var Data []uint32
npujarec5762e2020-01-01 14:08:48 +0530877 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400878 if err == nil {
879 if Value != nil {
880 Val, err := kvstore.ToByte(Value.Value)
881 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000882 logger.Errorw("Failed to convert into byte array", log.Fields{"error": err})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400883 return Data
884 }
885 if err = json.Unmarshal(Val, &Data); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000886 logger.Error("Failed to unmarshal", log.Fields{"error": err})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400887 return Data
888 }
889 }
890 } else {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000891 logger.Errorf("Failed to get TP id from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400892 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000893 logger.Debugf("Getting TP id %d from path %s", Data, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400894 return Data
895
896}
897
Gamze Abakafee36392019-10-03 11:17:24 +0000898// RemoveTechProfileIDsForOnu deletes all tech profile ids from the KV-Store for the given onu based on the path
899// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530900func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDsForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) error {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000901 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
npujarec5762e2020-01-01 14:08:48 +0530902 if err := RsrcMgr.KVStore.Delete(ctx, IntfOnuUniID); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000903 logger.Errorw("Failed to delete techprofile id resource in KV store", log.Fields{"path": IntfOnuUniID})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400904 return err
905 }
906 return nil
907}
908
Gamze Abakafee36392019-10-03 11:17:24 +0000909// RemoveTechProfileIDForOnu deletes a specific tech profile id from the KV-Store for the given onu based on the path
910// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530911func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32, TpID uint32) error {
912 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000913 for i, tpIDInList := range tpIDList {
914 if tpIDInList == TpID {
915 tpIDList = append(tpIDList[:i], tpIDList[i+1:]...)
916 }
917 }
918 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
919 Value, err := json.Marshal(tpIDList)
920 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000921 logger.Error("failed to Marshal")
Gamze Abakafee36392019-10-03 11:17:24 +0000922 return err
923 }
npujarec5762e2020-01-01 14:08:48 +0530924 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000925 logger.Errorf("Failed to update resource %s", IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000926 return err
927 }
928 return err
929}
930
931// UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
932// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530933func (RsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32,
salmansiddiqui7ac62132019-08-22 03:58:50 +0000934 UniID uint32, TpID uint32) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400935 var Value []byte
936 var err error
937
salmansiddiqui7ac62132019-08-22 03:58:50 +0000938 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000939
npujarec5762e2020-01-01 14:08:48 +0530940 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000941 for _, value := range tpIDList {
942 if value == TpID {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000943 logger.Debugf("TpID %d is already in tpIdList for the path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000944 return err
945 }
946 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000947 logger.Debugf("updating tp id %d on path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000948 tpIDList = append(tpIDList, TpID)
949 Value, err = json.Marshal(tpIDList)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400950 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000951 logger.Error("failed to Marshal")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400952 return err
953 }
npujarec5762e2020-01-01 14:08:48 +0530954 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000955 logger.Errorf("Failed to update resource %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400956 return err
957 }
958 return err
959}
960
salmansiddiqui7ac62132019-08-22 03:58:50 +0000961// UpdateMeterIDForOnu updates the meter id in the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000962// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +0530963func (RsrcMgr *OpenOltResourceMgr) UpdateMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000964 UniID uint32, TpID uint32, MeterConfig *ofp.OfpMeterConfig) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400965 var Value []byte
966 var err error
967
Gamze Abakafee36392019-10-03 11:17:24 +0000968 IntfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400969 Value, err = json.Marshal(*MeterConfig)
970 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000971 logger.Error("failed to Marshal meter config")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400972 return err
973 }
npujarec5762e2020-01-01 14:08:48 +0530974 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000975 logger.Errorf("Failed to store meter into KV store %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400976 return err
977 }
978 return err
979}
980
Gamze Abakafee36392019-10-03 11:17:24 +0000981// GetMeterIDForOnu fetches the meter id from the kv store for the given onu based on the path
982// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +0530983func (RsrcMgr *OpenOltResourceMgr) GetMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000984 UniID uint32, TpID uint32) (*ofp.OfpMeterConfig, error) {
985 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400986 var meterConfig ofp.OfpMeterConfig
npujarec5762e2020-01-01 14:08:48 +0530987 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400988 if err == nil {
989 if Value != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000990 logger.Debug("Found meter in KV store", log.Fields{"Direction": Direction})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000991 Val, er := kvstore.ToByte(Value.Value)
992 if er != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000993 logger.Errorw("Failed to convert into byte array", log.Fields{"error": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000994 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400995 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000996 if er = json.Unmarshal(Val, &meterConfig); er != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000997 logger.Error("Failed to unmarshal meterconfig", log.Fields{"error": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000998 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400999 }
1000 } else {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001001 logger.Debug("meter-does-not-exists-in-KVStore")
Manikkaraj kb1d51442019-07-23 10:41:02 -04001002 return nil, err
1003 }
1004 } else {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001005 logger.Errorf("Failed to get Meter config from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001006
1007 }
1008 return &meterConfig, err
1009}
1010
Gamze Abakafee36392019-10-03 11:17:24 +00001011// RemoveMeterIDForOnu deletes the meter id from the kV-Store for the given onu based on the path
1012// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +05301013func (RsrcMgr *OpenOltResourceMgr) RemoveMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +00001014 UniID uint32, TpID uint32) error {
1015 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
npujarec5762e2020-01-01 14:08:48 +05301016 if err := RsrcMgr.KVStore.Delete(ctx, Path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001017 logger.Errorf("Failed to delete meter id %s from kvstore ", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001018 return err
1019 }
1020 return nil
1021}
salmansiddiqui7ac62132019-08-22 03:58:50 +00001022
Gamze Abaka724d0852020-03-18 12:10:24 +00001023func getFlowIDFromFlowInfo(FlowInfo *[]FlowInfo, flowID, gemportID uint32, flowStoreCookie uint64, flowCategory string,
1024 vlanVid uint32, vlanPcp ...uint32) error {
salmansiddiqui7ac62132019-08-22 03:58:50 +00001025 if FlowInfo != nil {
1026 for _, Info := range *FlowInfo {
1027 if int32(gemportID) == Info.Flow.GemportId && flowCategory != "" && Info.FlowCategory == flowCategory {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001028 logger.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
Gamze Abaka724d0852020-03-18 12:10:24 +00001029 if Info.FlowCategory == "HSIA_FLOW" {
1030 if err := checkVlanAndPbitEqualityForFlows(vlanVid, Info, vlanPcp[0]); err == nil {
1031 return nil
1032 }
salmansiddiqui7ac62132019-08-22 03:58:50 +00001033 }
1034 }
1035 if int32(gemportID) == Info.Flow.GemportId && flowStoreCookie != 0 && Info.FlowStoreCookie == flowStoreCookie {
1036 if flowCategory != "" && Info.FlowCategory == flowCategory {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001037 logger.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001038 return nil
1039 }
1040 }
1041 }
1042 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001043 logger.Debugw("the flow can be related to a different service", log.Fields{"flow_info": FlowInfo})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001044 return errors.New("invalid flow-info")
1045}
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301046
Gamze Abaka724d0852020-03-18 12:10:24 +00001047func checkVlanAndPbitEqualityForFlows(vlanVid uint32, Info FlowInfo, vlanPcp uint32) error {
1048 if err := checkVlanEqualityForFlows(vlanVid, Info); err != nil {
1049 return err
1050 }
1051
1052 //flow has remark action and pbits
1053 if Info.Flow.Action.Cmd.RemarkInnerPbits || Info.Flow.Action.Cmd.RemarkOuterPbits {
1054 if vlanPcp == Info.Flow.Action.OPbits || vlanPcp == Info.Flow.Action.IPbits {
1055 return nil
1056 }
1057 } else if vlanPcp == Info.Flow.Classifier.OPbits {
1058 //no remark action but flow has pbits
1059 return nil
1060 } else if vlanPcp == 0xff || Info.Flow.Classifier.OPbits == 0xff {
1061 // no pbit found
1062 return nil
1063 }
1064 return errors.New("not found in terms of pbit equality")
1065}
1066
1067func checkVlanEqualityForFlows(vlanVid uint32, Info FlowInfo) error {
1068 if vlanVid == Info.Flow.Action.OVid || vlanVid == Info.Flow.Classifier.IVid {
1069 return nil
1070 }
1071 return errors.New("not found in terms of vlan_id equality")
1072}
1073
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301074//AddGemToOnuGemInfo adds gemport to onugem info kvstore
npujarec5762e2020-01-01 14:08:48 +05301075func (RsrcMgr *OpenOltResourceMgr) AddGemToOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, gemPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301076 var onuGemData []OnuGemInfo
1077 var err error
1078
npujarec5762e2020-01-01 14:08:48 +05301079 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001080 logger.Errorf("failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301081 return err
1082 }
1083 if len(onuGemData) == 0 {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001084 logger.Errorw("failed to ger Onuid info ", log.Fields{"intfid": intfID, "onuid": onuID})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301085 return err
1086 }
1087
1088 for idx, onugem := range onuGemData {
1089 if onugem.OnuID == onuID {
1090 for _, gem := range onuGemData[idx].GemPorts {
1091 if gem == gemPort {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001092 logger.Debugw("Gem already present in onugem info, skpping addition", log.Fields{"gem": gem})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301093 return nil
1094 }
1095 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001096 logger.Debugw("Added gem to onugem info", log.Fields{"gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301097 onuGemData[idx].GemPorts = append(onuGemData[idx].GemPorts, gemPort)
1098 break
1099 }
1100 }
npujarec5762e2020-01-01 14:08:48 +05301101 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301102 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001103 logger.Error("Failed to add onugem to kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301104 return err
1105 }
1106 return err
1107}
1108
1109//GetOnuGemInfo gets onu gem info from the kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301110func (RsrcMgr *OpenOltResourceMgr) GetOnuGemInfo(ctx context.Context, IntfID uint32) ([]OnuGemInfo, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301111 var onuGemData []OnuGemInfo
1112
npujarec5762e2020-01-01 14:08:48 +05301113 if err := RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001114 logger.Errorf("failed to get onuifo for intfid %d", IntfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301115 return nil, err
1116 }
1117
1118 return onuGemData, nil
1119}
1120
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001121// AddOnuGemInfo adds onu info on to the kvstore per interface
1122func (RsrcMgr *OpenOltResourceMgr) AddOnuGemInfo(ctx context.Context, IntfID uint32, onuGem OnuGemInfo) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301123 var onuGemData []OnuGemInfo
1124 var err error
1125
npujarec5762e2020-01-01 14:08:48 +05301126 if err = RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001127 logger.Errorf("failed to get onuifo for intfid %d", IntfID)
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001128 return olterrors.NewErrPersistence("get", "OnuGemInfo", IntfID,
1129 log.Fields{"onuGem": onuGem, "intfID": IntfID}, err)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301130 }
1131 onuGemData = append(onuGemData, onuGem)
npujarec5762e2020-01-01 14:08:48 +05301132 err = RsrcMgr.ResourceMgrs[IntfID].AddOnuGemInfo(ctx, IntfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301133 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001134 logger.Error("Failed to add onugem to kv store")
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001135 return olterrors.NewErrPersistence("set", "OnuGemInfo", IntfID,
1136 log.Fields{"onuGemData": onuGemData, "intfID": IntfID}, err)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301137 }
1138
Girish Kumar2ad402b2020-03-20 19:45:12 +00001139 logger.Debugw("added onu to onugeminfo", log.Fields{"intf": IntfID, "onugem": onuGem})
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001140 return nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301141}
1142
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301143// 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 +05301144func (RsrcMgr *OpenOltResourceMgr) AddUniPortToOnuInfo(ctx context.Context, intfID uint32, onuID uint32, portNo uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301145 var onuGemData []OnuGemInfo
1146 var err error
1147
npujarec5762e2020-01-01 14:08:48 +05301148 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001149 logger.Errorf("failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301150 return
1151 }
1152 for idx, onu := range onuGemData {
1153 if onu.OnuID == onuID {
1154 for _, uni := range onu.UniPorts {
1155 if uni == portNo {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001156 logger.Debugw("uni already present in onugem info", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301157 return
1158 }
1159 }
1160 onuGemData[idx].UniPorts = append(onuGemData[idx].UniPorts, portNo)
1161 break
1162 }
1163 }
npujarec5762e2020-01-01 14:08:48 +05301164 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301165 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001166 logger.Errorw("Failed to add uin port in onugem to kv store", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301167 return
1168 }
1169 return
1170}
1171
1172//UpdateGemPortForPktIn updates gemport for pkt in path to kvstore, path being intfid, onuid, portno
npujarec5762e2020-01-01 14:08:48 +05301173func (RsrcMgr *OpenOltResourceMgr) UpdateGemPortForPktIn(ctx context.Context, pktIn PacketInInfoKey, gemPort uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301174
1175 path := fmt.Sprintf(OnuPacketINPath, pktIn.IntfID, pktIn.OnuID, pktIn.LogicalPort)
1176 Value, err := json.Marshal(gemPort)
1177 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001178 logger.Error("Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301179 return
1180 }
npujarec5762e2020-01-01 14:08:48 +05301181 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001182 logger.Errorw("Failed to put to kvstore", log.Fields{"path": path, "value": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301183 return
1184 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001185 logger.Debugw("added gem packet in successfully", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301186
1187 return
1188}
1189
1190// GetGemPortFromOnuPktIn gets the gem port from onu pkt in path, path being intfid, onuid, portno
npujarec5762e2020-01-01 14:08:48 +05301191func (RsrcMgr *OpenOltResourceMgr) GetGemPortFromOnuPktIn(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) (uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301192
1193 var Val []byte
1194 var gemPort uint32
1195
1196 path := fmt.Sprintf(OnuPacketINPath, intfID, onuID, logicalPort)
1197
npujarec5762e2020-01-01 14:08:48 +05301198 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301199 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001200 logger.Errorw("Failed to get from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301201 return uint32(0), err
1202 } else if value == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001203 logger.Debugw("No pkt in gem found", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301204 return uint32(0), nil
1205 }
1206
1207 if Val, err = kvstore.ToByte(value.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001208 logger.Error("Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301209 return uint32(0), err
1210 }
1211 if err = json.Unmarshal(Val, &gemPort); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001212 logger.Error("Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301213 return uint32(0), err
1214 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001215 logger.Debugw("found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301216
1217 return gemPort, nil
1218}
1219
1220// DelGemPortPktIn deletes the gemport from the pkt in path
npujarec5762e2020-01-01 14:08:48 +05301221func (RsrcMgr *OpenOltResourceMgr) DelGemPortPktIn(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301222
1223 path := fmt.Sprintf(OnuPacketINPath, intfID, onuID, logicalPort)
npujarec5762e2020-01-01 14:08:48 +05301224 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001225 logger.Errorf("Falied to remove resource %s", path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301226 return err
1227 }
1228 return nil
1229}
1230
1231// DelOnuGemInfoForIntf deletes the onugem info from kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301232func (RsrcMgr *OpenOltResourceMgr) DelOnuGemInfoForIntf(ctx context.Context, intfID uint32) error {
1233 if err := RsrcMgr.ResourceMgrs[intfID].DelOnuGemInfoForIntf(ctx, intfID); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001234 logger.Errorw("failed to delete onu gem info for", log.Fields{"intfid": intfID})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301235 return err
1236 }
1237 return nil
1238}
1239
1240//GetNNIFromKVStore gets NNi intfids from kvstore. path being per device
npujarec5762e2020-01-01 14:08:48 +05301241func (RsrcMgr *OpenOltResourceMgr) GetNNIFromKVStore(ctx context.Context) ([]uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301242
1243 var nni []uint32
1244 var Val []byte
1245
1246 path := fmt.Sprintf(NnniIntfID)
npujarec5762e2020-01-01 14:08:48 +05301247 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301248 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001249 logger.Error("failed to get data from kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301250 return nil, err
1251 }
1252 if value != nil {
1253 if Val, err = kvstore.ToByte(value.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001254 logger.Error("Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301255 return nil, err
1256 }
1257 if err = json.Unmarshal(Val, &nni); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001258 logger.Error("Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301259 return nil, err
1260 }
1261 }
1262 return nni, err
1263}
1264
1265// AddNNIToKVStore adds Nni interfaces to kvstore, path being per device.
npujarec5762e2020-01-01 14:08:48 +05301266func (RsrcMgr *OpenOltResourceMgr) AddNNIToKVStore(ctx context.Context, nniIntf uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301267 var Value []byte
1268
npujarec5762e2020-01-01 14:08:48 +05301269 nni, err := RsrcMgr.GetNNIFromKVStore(ctx)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301270 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001271 logger.Error("failed to fetch nni interfaces from kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301272 return err
1273 }
1274
1275 path := fmt.Sprintf(NnniIntfID)
1276 nni = append(nni, nniIntf)
1277 Value, err = json.Marshal(nni)
1278 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001279 logger.Error("Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301280 }
npujarec5762e2020-01-01 14:08:48 +05301281 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001282 logger.Errorw("Failed to put to kvstore", log.Fields{"path": path, "value": Value})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301283 return err
1284 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001285 logger.Debugw("added nni to kv successfully", log.Fields{"path": path, "nni": nniIntf})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301286 return nil
1287}
1288
1289// DelNNiFromKVStore deletes nni interface list from kv store.
npujarec5762e2020-01-01 14:08:48 +05301290func (RsrcMgr *OpenOltResourceMgr) DelNNiFromKVStore(ctx context.Context) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301291
1292 path := fmt.Sprintf(NnniIntfID)
1293
npujarec5762e2020-01-01 14:08:48 +05301294 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001295 logger.Errorw("Failed to delete nni interfaces from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301296 return err
1297 }
1298 return nil
1299}
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301300
1301//UpdateFlowIDsForGem updates flow id per gemport
npujarec5762e2020-01-01 14:08:48 +05301302func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDsForGem(ctx context.Context, intf uint32, gem uint32, flowIDs []uint32) error {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301303 var val []byte
1304 path := fmt.Sprintf(FlowIDsForGem, intf)
1305
npujarec5762e2020-01-01 14:08:48 +05301306 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301307 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001308 logger.Error("Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301309 return err
1310 }
1311 if flowsForGem == nil {
1312 flowsForGem = make(map[uint32][]uint32)
1313 }
1314 flowsForGem[gem] = flowIDs
1315 val, err = json.Marshal(flowsForGem)
1316 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001317 logger.Error("Failed to marshal data", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301318 return err
1319 }
Girish Gowdrab77ded92020-04-08 11:45:05 -07001320
Girish Gowdra38d533d2020-03-30 20:38:51 -07001321 RsrcMgr.flowIDToGemInfoLock.Lock()
1322 defer RsrcMgr.flowIDToGemInfoLock.Unlock()
npujarec5762e2020-01-01 14:08:48 +05301323 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001324 logger.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301325 return err
1326 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001327 logger.Debugw("added flowid list for gem to kv successfully", log.Fields{"path": path, "flowidlist": flowsForGem[gem]})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301328 return nil
1329}
1330
1331//DeleteFlowIDsForGem deletes the flowID list entry per gem from kvstore.
npujarec5762e2020-01-01 14:08:48 +05301332func (RsrcMgr *OpenOltResourceMgr) DeleteFlowIDsForGem(ctx context.Context, intf uint32, gem uint32) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301333 path := fmt.Sprintf(FlowIDsForGem, intf)
1334 var val []byte
1335
npujarec5762e2020-01-01 14:08:48 +05301336 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301337 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001338 logger.Error("Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301339 return
1340 }
1341 if flowsForGem == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001342 logger.Error("No flowids found ", log.Fields{"intf": intf, "gemport": gem})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301343 return
1344 }
1345 // once we get the flows per gem map from kv , just delete the gem entry from the map
1346 delete(flowsForGem, gem)
1347 // once gem entry is deleted update the kv store.
1348 val, err = json.Marshal(flowsForGem)
1349 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001350 logger.Error("Failed to marshal data", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301351 return
1352 }
Girish Gowdra38d533d2020-03-30 20:38:51 -07001353
1354 RsrcMgr.flowIDToGemInfoLock.Lock()
1355 defer RsrcMgr.flowIDToGemInfoLock.Unlock()
npujarec5762e2020-01-01 14:08:48 +05301356 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001357 logger.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301358 return
1359 }
1360 return
1361}
1362
1363//GetFlowIDsGemMapForInterface gets flowids per gemport and interface
npujarec5762e2020-01-01 14:08:48 +05301364func (RsrcMgr *OpenOltResourceMgr) GetFlowIDsGemMapForInterface(ctx context.Context, intf uint32) (map[uint32][]uint32, error) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301365 path := fmt.Sprintf(FlowIDsForGem, intf)
1366 var flowsForGem map[uint32][]uint32
1367 var val []byte
Girish Gowdra38d533d2020-03-30 20:38:51 -07001368 RsrcMgr.flowIDToGemInfoLock.RLock()
npujarec5762e2020-01-01 14:08:48 +05301369 value, err := RsrcMgr.KVStore.Get(ctx, path)
Girish Gowdra38d533d2020-03-30 20:38:51 -07001370 RsrcMgr.flowIDToGemInfoLock.RUnlock()
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301371 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001372 logger.Error("failed to get data from kv store")
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301373 return nil, err
1374 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001375 if value != nil && value.Value != nil {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301376 if val, err = kvstore.ToByte(value.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001377 logger.Error("Failed to convert to byte array ", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301378 return nil, err
1379 }
1380 if err = json.Unmarshal(val, &flowsForGem); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001381 logger.Error("Failed to unmarshall", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301382 return nil, err
1383 }
1384 }
1385 return flowsForGem, nil
1386}
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301387
1388//DeleteIntfIDGempMapPath deletes the intf id path used to store flow ids per gem to kvstore.
npujarec5762e2020-01-01 14:08:48 +05301389func (RsrcMgr *OpenOltResourceMgr) DeleteIntfIDGempMapPath(ctx context.Context, intf uint32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301390 path := fmt.Sprintf(FlowIDsForGem, intf)
Girish Gowdra38d533d2020-03-30 20:38:51 -07001391 RsrcMgr.flowIDToGemInfoLock.Lock()
1392 defer RsrcMgr.flowIDToGemInfoLock.Unlock()
npujarec5762e2020-01-01 14:08:48 +05301393 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001394 logger.Errorw("Failed to delete nni interfaces from kv store", log.Fields{"path": path})
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301395 return
1396 }
1397 return
1398}
1399
1400// RemoveResourceMap Clear resource map associated with (intfid, onuid, uniid) tuple.
npujarec5762e2020-01-01 14:08:48 +05301401func (RsrcMgr *OpenOltResourceMgr) RemoveResourceMap(ctx context.Context, intfID uint32, onuID int32, uniID int32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301402 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +05301403 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301404}
Esin Karamanccb714b2019-11-29 15:02:06 +00001405
1406//GetMcastQueuePerInterfaceMap gets multicast queue info per pon interface
npujarec5762e2020-01-01 14:08:48 +05301407func (RsrcMgr *OpenOltResourceMgr) GetMcastQueuePerInterfaceMap(ctx context.Context) (map[uint32][]uint32, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001408 path := fmt.Sprintf(McastQueuesForIntf)
1409 var mcastQueueToIntfMap map[uint32][]uint32
1410 var val []byte
1411
npujarec5762e2020-01-01 14:08:48 +05301412 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001413 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001414 logger.Error("failed to get data from kv store")
Esin Karamanccb714b2019-11-29 15:02:06 +00001415 return nil, err
1416 }
1417 if kvPair != nil && kvPair.Value != nil {
1418 if val, err = kvstore.ToByte(kvPair.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001419 logger.Error("Failed to convert to byte array ", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001420 return nil, err
1421 }
1422 if err = json.Unmarshal(val, &mcastQueueToIntfMap); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001423 logger.Error("Failed to unmarshall ", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001424 return nil, err
1425 }
1426 }
1427 return mcastQueueToIntfMap, nil
1428}
1429
1430//AddMcastQueueForIntf adds multicast queue for pon interface
npujarec5762e2020-01-01 14:08:48 +05301431func (RsrcMgr *OpenOltResourceMgr) AddMcastQueueForIntf(ctx context.Context, intf uint32, gem uint32, servicePriority uint32) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001432 var val []byte
1433 path := fmt.Sprintf(McastQueuesForIntf)
1434
npujarec5762e2020-01-01 14:08:48 +05301435 mcastQueues, err := RsrcMgr.GetMcastQueuePerInterfaceMap(ctx)
Esin Karamanccb714b2019-11-29 15:02:06 +00001436 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001437 logger.Errorw("Failed to get multicast queue info for interface", log.Fields{"error": err, "intf": intf})
Esin Karamanccb714b2019-11-29 15:02:06 +00001438 return err
1439 }
1440 if mcastQueues == nil {
1441 mcastQueues = make(map[uint32][]uint32)
1442 }
1443 mcastQueues[intf] = []uint32{gem, servicePriority}
1444 if val, err = json.Marshal(mcastQueues); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001445 logger.Errorw("Failed to marshal data", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001446 return err
1447 }
npujarec5762e2020-01-01 14:08:48 +05301448 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001449 logger.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Esin Karamanccb714b2019-11-29 15:02:06 +00001450 return err
1451 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001452 logger.Debugw("added multicast queue info to KV store successfully", log.Fields{"path": path, "mcastQueueInfo": mcastQueues[intf], "interfaceId": intf})
Esin Karamanccb714b2019-11-29 15:02:06 +00001453 return nil
1454}
1455
1456//AddFlowGroupToKVStore adds flow group into KV store
npujarec5762e2020-01-01 14:08:48 +05301457func (RsrcMgr *OpenOltResourceMgr) AddFlowGroupToKVStore(ctx context.Context, groupEntry *ofp.OfpGroupEntry, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001458 var Value []byte
1459 var err error
1460 var path string
1461 if cached {
1462 path = fmt.Sprintf(FlowGroupCached, groupEntry.Desc.GroupId)
1463 } else {
1464 path = fmt.Sprintf(FlowGroup, groupEntry.Desc.GroupId)
1465 }
1466 //build group info object
1467 var outPorts []uint32
1468 for _, ofBucket := range groupEntry.Desc.Buckets {
1469 for _, ofAction := range ofBucket.Actions {
1470 if ofAction.Type == ofp.OfpActionType_OFPAT_OUTPUT {
1471 outPorts = append(outPorts, ofAction.GetOutput().Port)
1472 }
1473 }
1474 }
1475 groupInfo := GroupInfo{
1476 GroupID: groupEntry.Desc.GroupId,
1477 OutPorts: outPorts,
1478 }
1479
1480 Value, err = json.Marshal(groupInfo)
1481
1482 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001483 logger.Error("failed to Marshal flow group object")
Esin Karamanccb714b2019-11-29 15:02:06 +00001484 return err
1485 }
1486
npujarec5762e2020-01-01 14:08:48 +05301487 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001488 logger.Errorf("Failed to update resource %s", path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001489 return err
1490 }
1491 return nil
1492}
1493
1494//RemoveFlowGroupFromKVStore removes flow group from KV store
npujarec5762e2020-01-01 14:08:48 +05301495func (RsrcMgr *OpenOltResourceMgr) RemoveFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) bool {
Esin Karamanccb714b2019-11-29 15:02:06 +00001496 var path string
1497 if cached {
1498 path = fmt.Sprintf(FlowGroupCached, groupID)
1499 } else {
1500 path = fmt.Sprintf(FlowGroup, groupID)
1501 }
npujarec5762e2020-01-01 14:08:48 +05301502 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001503 logger.Errorf("Failed to remove resource %s due to %s", path, err)
Esin Karamanccb714b2019-11-29 15:02:06 +00001504 return false
1505 }
1506 return true
1507}
1508
1509//GetFlowGroupFromKVStore fetches flow group from the KV store. Returns (false, {} error) if any problem occurs during
1510//fetching the data. Returns (true, groupInfo, nil) if the group is fetched successfully.
1511// Returns (false, {}, nil) if the group does not exists in the KV store.
npujarec5762e2020-01-01 14:08:48 +05301512func (RsrcMgr *OpenOltResourceMgr) GetFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) (bool, GroupInfo, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001513 var groupInfo GroupInfo
1514 var path string
1515 if cached {
1516 path = fmt.Sprintf(FlowGroupCached, groupID)
1517 } else {
1518 path = fmt.Sprintf(FlowGroup, groupID)
1519 }
npujarec5762e2020-01-01 14:08:48 +05301520 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001521 if err != nil {
1522 return false, groupInfo, err
1523 }
1524 if kvPair != nil && kvPair.Value != nil {
1525 Val, err := kvstore.ToByte(kvPair.Value)
1526 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001527 logger.Errorw("Failed to convert flow group into byte array", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001528 return false, groupInfo, err
1529 }
1530 if err = json.Unmarshal(Val, &groupInfo); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001531 logger.Errorw("Failed to unmarshal", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001532 return false, groupInfo, err
1533 }
1534 return true, groupInfo, nil
1535 }
1536 return false, groupInfo, nil
1537}