blob: 8907323106a8f0d48863e2c4a2d367d2467006e3 [file] [log] [blame]
Abhilash S.L7f17e402019-03-15 17:40:41 +05301/*
2 * Copyright 2019-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070017//Package resourcemanager provides the utility for managing resources
manikkaraj kbf256be2019-03-25 00:13:48 +053018package resourcemanager
Abhilash S.L7f17e402019-03-15 17:40:41 +053019
20import (
npujarec5762e2020-01-01 14:08:48 +053021 "context"
Girish Gowdru0c588b22019-04-23 23:24:56 -040022 "encoding/json"
23 "errors"
24 "fmt"
25 "strconv"
26 "strings"
Girish Gowdra3f182d82020-03-30 20:38:51 -070027 "sync"
Abhilash S.L7f17e402019-03-15 17:40:41 +053028
Esin Karamanccb714b2019-11-29 15:02:06 +000029 "github.com/opencord/voltha-lib-go/v3/pkg/db"
30 "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
31 "github.com/opencord/voltha-lib-go/v3/pkg/log"
32 ponrmgr "github.com/opencord/voltha-lib-go/v3/pkg/ponresourcemanager"
33 ofp "github.com/opencord/voltha-protos/v3/go/openflow_13"
34 "github.com/opencord/voltha-protos/v3/go/openolt"
Abhilash S.L7f17e402019-03-15 17:40:41 +053035)
36
salmansiddiqui7ac62132019-08-22 03:58:50 +000037const (
38 // KvstoreTimeout specifies the time out for KV Store Connection
39 KvstoreTimeout = 5
40 // BasePathKvStore - service/voltha/openolt/<device_id>
41 BasePathKvStore = "service/voltha/openolt/{%s}"
Gamze Abakafee36392019-10-03 11:17:24 +000042 // TpIDPathSuffix - <(pon_id, onu_id, uni_id)>/tp_id
43 TpIDPathSuffix = "{%d,%d,%d}/tp_id"
44 //MeterIDPathSuffix - <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
45 MeterIDPathSuffix = "{%d,%d,%d}/{%d}/meter_id/{%s}"
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053046 //NnniIntfID - nniintfids
47 NnniIntfID = "nniintfids"
48 // OnuPacketINPath path on the kvstore to store packetin gemport,which will be used for packetin, pcketout
49 //format: onu_packetin/<intfid>,<onuid>,<logicalport>
50 OnuPacketINPath = "onu_packetin/{%d,%d,%d}"
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +053051 //FlowIDsForGem flowids_per_gem/<intfid>
52 FlowIDsForGem = "flowids_per_gem/{%d}"
Esin Karamanccb714b2019-11-29 15:02:06 +000053 //McastQueuesForIntf multicast queues for pon interfaces
54 McastQueuesForIntf = "mcast_qs_for_int"
55 //FlowGroup flow_groups/<flow_group_id>
56 // A group is stored under this path on the KV store after it has been installed to the device.
57 // It should also be deleted after it has been removed from the device accordingly.
58 FlowGroup = "flow_groups/{%d}"
59 //FlowGroupCached flow_groups_cached/<flow_group_id>
60 // When a group add request received, we create the group without setting any members to it since we cannot
61 // set any members to a group until it is associated with a multicast flow. It is a BAL limitation.
62 // When the related multicast flow has been created we perform set members operation for the group.
63 // That is why we need to keep the members of a group until the multicast flow creation request comes.
64 // We preserve the groups under "FlowGroupsCached" directory in the KV store temporarily. Having set members,
65 // we remove the group from the cached group store.
66 FlowGroupCached = "flow_groups_cached/{%d}"
salmansiddiqui7ac62132019-08-22 03:58:50 +000067)
Abhilash S.L7f17e402019-03-15 17:40:41 +053068
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070069// FlowInfo holds the flow information
Abhilash S.L8ee90712019-04-29 16:24:22 +053070type FlowInfo struct {
71 Flow *openolt.Flow
72 FlowStoreCookie uint64
73 FlowCategory string
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053074 LogicalFlowID uint64
75}
76
77// OnuGemInfo holds onu information along with gem port list and uni port list
78type OnuGemInfo struct {
79 OnuID uint32
80 SerialNumber string
81 IntfID uint32
82 GemPorts []uint32
83 UniPorts []uint32
84}
85
86// PacketInInfoKey is the key for packet in gemport
87type PacketInInfoKey struct {
88 IntfID uint32
89 OnuID uint32
90 LogicalPort uint32
Abhilash S.L8ee90712019-04-29 16:24:22 +053091}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070092
Esin Karamanccb714b2019-11-29 15:02:06 +000093// GroupInfo holds group information
94type GroupInfo struct {
95 GroupID uint32
96 OutPorts []uint32
97}
98
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070099// OpenOltResourceMgr holds resource related information as provided below for each field
Abhilash S.L7f17e402019-03-15 17:40:41 +0530100type OpenOltResourceMgr struct {
sbarbaria8910ba2019-11-05 10:12:23 -0500101 DeviceID string // OLT device id
102 HostAndPort string // Host and port of the kv store to connect to
103 Args string // args
104 KVStore *db.Backend // backend kv store connection handle
Girish Gowdru0c588b22019-04-23 23:24:56 -0400105 DeviceType string
106 Host string // Host ip of the kv store
107 Port int // port of the kv store
108 DevInfo *openolt.DeviceInfo // device information
109 // array of pon resource managers per interface technology
110 ResourceMgrs map[uint32]*ponrmgr.PONResourceManager
Girish Gowdra3f182d82020-03-30 20:38:51 -0700111
112 // This protects concurrent gemport_id allocate/delete calls on a per PON port basis
113 GemPortIDMgmtLock []sync.RWMutex
114 // This protects concurrent alloc_id allocate/delete calls on a per PON port basis
115 AllocIDMgmtLock []sync.RWMutex
116 // This protects concurrent onu_id allocate/delete calls on a per PON port basis
117 OnuIDMgmtLock []sync.RWMutex
118 // This protects concurrent flow_id allocate/delete calls. We do not need this on a
119 // per PON port basis as flow IDs are unique across the OLT.
120 FlowIDMgmtLock sync.RWMutex
121
122 // This protects concurrent access to flowids_per_gem info stored on KV store
123 flowIDToGemInfoLock sync.RWMutex
Abhilash S.L7f17e402019-03-15 17:40:41 +0530124}
125
Manikkaraj kb1d51442019-07-23 10:41:02 -0400126func newKVClient(storeType string, address string, timeout uint32) (kvstore.Client, error) {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000127 logger.Infow("kv-store-type", log.Fields{"store": storeType})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400128 switch storeType {
129 case "consul":
130 return kvstore.NewConsulClient(address, int(timeout))
131 case "etcd":
132 return kvstore.NewEtcdClient(address, int(timeout))
133 }
134 return nil, errors.New("unsupported-kv-store")
Abhilash S.L7f17e402019-03-15 17:40:41 +0530135}
136
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700137// SetKVClient sets the KV client and return a kv backend
sbarbaria8910ba2019-11-05 10:12:23 -0500138func SetKVClient(backend string, Host string, Port int, DeviceID string) *db.Backend {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400139 addr := Host + ":" + strconv.Itoa(Port)
140 // TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
141 // issue between kv store and backend , core is not calling NewBackend directly
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700142 kvClient, err := newKVClient(backend, addr, KvstoreTimeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400143 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000144 logger.Fatalw("Failed to init KV client\n", log.Fields{"err": err})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400145 return nil
146 }
sbarbaria8910ba2019-11-05 10:12:23 -0500147 kvbackend := &db.Backend{
Girish Gowdru0c588b22019-04-23 23:24:56 -0400148 Client: kvClient,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700149 StoreType: backend,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400150 Host: Host,
151 Port: Port,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700152 Timeout: KvstoreTimeout,
153 PathPrefix: fmt.Sprintf(BasePathKvStore, DeviceID)}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530154
Girish Gowdru0c588b22019-04-23 23:24:56 -0400155 return kvbackend
Abhilash S.L7f17e402019-03-15 17:40:41 +0530156}
157
Gamze Abakafee36392019-10-03 11:17:24 +0000158// NewResourceMgr init a New resource manager instance which in turn instantiates pon resource manager
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700159// instances according to technology. Initializes the default resource ranges for all
160// the resources.
npujarec5762e2020-01-01 14:08:48 +0530161func NewResourceMgr(ctx context.Context, deviceID string, KVStoreHostPort string, kvStoreType string, deviceType string, devInfo *openolt.DeviceInfo) *OpenOltResourceMgr {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400162 var ResourceMgr OpenOltResourceMgr
Girish Kumar2ad402b2020-03-20 19:45:12 +0000163 logger.Debugf("Init new resource manager , host_port: %s, deviceid: %s", KVStoreHostPort, deviceID)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530164 ResourceMgr.HostAndPort = KVStoreHostPort
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700165 ResourceMgr.DeviceType = deviceType
166 ResourceMgr.DevInfo = devInfo
167 IPPort := strings.Split(KVStoreHostPort, ":")
168 ResourceMgr.Host = IPPort[0]
169 ResourceMgr.Port, _ = strconv.Atoi(IPPort[1])
Girish Gowdra3f182d82020-03-30 20:38:51 -0700170 NumPONPorts := devInfo.GetPonPorts()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530171
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700172 Backend := kvStoreType
Girish Gowdru0c588b22019-04-23 23:24:56 -0400173 ResourceMgr.KVStore = SetKVClient(Backend, ResourceMgr.Host,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700174 ResourceMgr.Port, deviceID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400175 if ResourceMgr.KVStore == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000176 logger.Error("Failed to setup KV store")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400177 }
178 Ranges := make(map[string]*openolt.DeviceInfo_DeviceResourceRanges)
179 RsrcMgrsByTech := make(map[string]*ponrmgr.PONResourceManager)
180 ResourceMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530181
Girish Gowdra3f182d82020-03-30 20:38:51 -0700182 ResourceMgr.AllocIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
183 ResourceMgr.GemPortIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
184 ResourceMgr.OnuIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
185
Girish Gowdru0c588b22019-04-23 23:24:56 -0400186 // TODO self.args = registry('main').get_args()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530187
Girish Gowdru0c588b22019-04-23 23:24:56 -0400188 /*
189 If a legacy driver returns protobuf without any ranges,s synthesize one from
Gamze Abakafee36392019-10-03 11:17:24 +0000190 the legacy global per-device information. This, in theory, is temporary until
Girish Gowdru0c588b22019-04-23 23:24:56 -0400191 the legacy drivers are upgrade to support pool ranges.
192 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700193 if devInfo.Ranges == nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400194 var ranges openolt.DeviceInfo_DeviceResourceRanges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700195 ranges.Technology = devInfo.GetTechnology()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530196
Girish Gowdru0c588b22019-04-23 23:24:56 -0400197 var index uint32
198 for index = 0; index < NumPONPorts; index++ {
199 ranges.IntfIds = append(ranges.IntfIds, index)
200 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530201
Abhilash S.L8ee90712019-04-29 16:24:22 +0530202 var Pool openolt.DeviceInfo_DeviceResourceRanges_Pool
Girish Gowdru0c588b22019-04-23 23:24:56 -0400203 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700204 Pool.Start = devInfo.OnuIdStart
205 Pool.End = devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400206 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
cbabuabf02352019-10-15 13:14:56 +0200207 onuPool := Pool
208 ranges.Pools = append(ranges.Pools, &onuPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530209
Girish Gowdru0c588b22019-04-23 23:24:56 -0400210 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700211 Pool.Start = devInfo.AllocIdStart
212 Pool.End = devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400213 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200214 allocPool := Pool
215 ranges.Pools = append(ranges.Pools, &allocPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530216
Girish Gowdru0c588b22019-04-23 23:24:56 -0400217 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700218 Pool.Start = devInfo.GemportIdStart
219 Pool.End = devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400220 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200221 gemPool := Pool
222 ranges.Pools = append(ranges.Pools, &gemPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530223
Girish Gowdru0c588b22019-04-23 23:24:56 -0400224 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700225 Pool.Start = devInfo.FlowIdStart
226 Pool.End = devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400227 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
Abhilash S.L8ee90712019-04-29 16:24:22 +0530228 ranges.Pools = append(ranges.Pools, &Pool)
229 // Add to device info
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700230 devInfo.Ranges = append(devInfo.Ranges, &ranges)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400231 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530232
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700233 // Create a separate Resource Manager instance for each range. This assumes that
Girish Gowdru0c588b22019-04-23 23:24:56 -0400234 // each technology is represented by only a single range
235 var GlobalPONRsrcMgr *ponrmgr.PONResourceManager
236 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700237 for _, TechRange := range devInfo.Ranges {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400238 technology := TechRange.Technology
Girish Kumar2ad402b2020-03-20 19:45:12 +0000239 logger.Debugf("Device info technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400240 Ranges[technology] = TechRange
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700241 RsrcMgrsByTech[technology], err = ponrmgr.NewPONResourceManager(technology, deviceType, deviceID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400242 Backend, ResourceMgr.Host, ResourceMgr.Port)
243 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000244 logger.Errorf("Failed to create pon resource manager instance for technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400245 return nil
246 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700247 // resource_mgrs_by_tech[technology] = resource_mgr
Girish Gowdru0c588b22019-04-23 23:24:56 -0400248 if GlobalPONRsrcMgr == nil {
249 GlobalPONRsrcMgr = RsrcMgrsByTech[technology]
250 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700251 for _, IntfID := range TechRange.IntfIds {
252 ResourceMgr.ResourceMgrs[uint32(IntfID)] = RsrcMgrsByTech[technology]
Girish Gowdru0c588b22019-04-23 23:24:56 -0400253 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700254 // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
npujarec5762e2020-01-01 14:08:48 +0530255 InitializeDeviceResourceRangeAndPool(ctx, RsrcMgrsByTech[technology], GlobalPONRsrcMgr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700256 TechRange, devInfo)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400257 }
258 // After we have initialized resource ranges, initialize the
259 // resource pools accordingly.
260 for _, PONRMgr := range RsrcMgrsByTech {
npujarec5762e2020-01-01 14:08:48 +0530261 _ = PONRMgr.InitDeviceResourcePool(ctx)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400262 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000263 logger.Info("Initialization of resource manager success!")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400264 return &ResourceMgr
Abhilash S.L7f17e402019-03-15 17:40:41 +0530265}
266
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700267// InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply
268// device specific information. If KV doesn't exist
269// or is broader than the device, the device's information will
270// dictate the range limits
npujarec5762e2020-01-01 14:08:48 +0530271func InitializeDeviceResourceRangeAndPool(ctx context.Context, ponRMgr *ponrmgr.PONResourceManager, globalPONRMgr *ponrmgr.PONResourceManager,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700272 techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530273
Girish Gowdru0c588b22019-04-23 23:24:56 -0400274 // init the resource range pool according to the sharing type
Abhilash S.L7f17e402019-03-15 17:40:41 +0530275
Girish Kumar2ad402b2020-03-20 19:45:12 +0000276 logger.Debugf("Resource range pool init for technology %s", ponRMgr.Technology)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700277 // first load from KV profiles
npujarec5762e2020-01-01 14:08:48 +0530278 status := ponRMgr.InitResourceRangesFromKVStore(ctx)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700279 if !status {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000280 logger.Debugf("Failed to load resource ranges from KV store for tech %s", ponRMgr.Technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400281 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530282
Girish Gowdru0c588b22019-04-23 23:24:56 -0400283 /*
284 Then apply device specific information. If KV doesn't exist
Gamze Abakafee36392019-10-03 11:17:24 +0000285 or is broader than the device, the device's information will
Girish Gowdru0c588b22019-04-23 23:24:56 -0400286 dictate the range limits
287 */
Girish Kumar2ad402b2020-03-20 19:45:12 +0000288 logger.Debugw("Using device info to init pon resource ranges", log.Fields{"Tech": ponRMgr.Technology})
Abhilash S.L7f17e402019-03-15 17:40:41 +0530289
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700290 ONUIDStart := devInfo.OnuIdStart
291 ONUIDEnd := devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400292 ONUIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
293 ONUIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700294 AllocIDStart := devInfo.AllocIdStart
295 AllocIDEnd := devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400296 AllocIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
297 AllocIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700298 GEMPortIDStart := devInfo.GemportIdStart
299 GEMPortIDEnd := devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400300 GEMPortIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
301 GEMPortIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700302 FlowIDStart := devInfo.FlowIdStart
303 FlowIDEnd := devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400304 FlowIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
305 FlowIDSharedPoolID := uint32(0)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530306
Girish Gowdru0c588b22019-04-23 23:24:56 -0400307 var FirstIntfPoolID uint32
308 var SharedPoolID uint32
Abhilash S.L7f17e402019-03-15 17:40:41 +0530309
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400310 /*
311 * As a zero check is made against SharedPoolID to check whether the resources are shared across all intfs
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700312 * if resources are shared across interfaces then SharedPoolID is given a positive number.
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400313 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700314 for _, FirstIntfPoolID = range techRange.IntfIds {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400315 // skip the intf id 0
316 if FirstIntfPoolID == 0 {
317 continue
318 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400319 break
320 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530321
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700322 for _, RangePool := range techRange.Pools {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400323 if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400324 SharedPoolID = FirstIntfPoolID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400325 } else if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_SAME_TECH {
326 SharedPoolID = FirstIntfPoolID
327 } else {
328 SharedPoolID = 0
329 }
330 if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID {
331 ONUIDStart = RangePool.Start
332 ONUIDEnd = RangePool.End
333 ONUIDShared = RangePool.Sharing
334 ONUIDSharedPoolID = SharedPoolID
335 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID {
336 AllocIDStart = RangePool.Start
337 AllocIDEnd = RangePool.End
338 AllocIDShared = RangePool.Sharing
339 AllocIDSharedPoolID = SharedPoolID
340 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID {
341 GEMPortIDStart = RangePool.Start
342 GEMPortIDEnd = RangePool.End
343 GEMPortIDShared = RangePool.Sharing
344 GEMPortIDSharedPoolID = SharedPoolID
345 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID {
346 FlowIDStart = RangePool.Start
347 FlowIDEnd = RangePool.End
348 FlowIDShared = RangePool.Sharing
349 FlowIDSharedPoolID = SharedPoolID
350 }
351 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530352
Girish Kumar2ad402b2020-03-20 19:45:12 +0000353 logger.Debugw("Device info init", log.Fields{"technology": techRange.Technology,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400354 "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd, "onu_id_shared_pool_id": ONUIDSharedPoolID,
355 "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd,
356 "alloc_id_shared_pool_id": AllocIDSharedPoolID,
357 "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd,
358 "gemport_id_shared_pool_id": GEMPortIDSharedPoolID,
359 "flow_id_start": FlowIDStart,
360 "flow_id_end_idx": FlowIDEnd,
361 "flow_id_shared_pool_id": FlowIDSharedPoolID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700362 "intf_ids": techRange.IntfIds,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400363 "uni_id_start": 0,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700364 "uni_id_end_idx": 1, /*MaxUNIIDperONU()*/
365 })
Abhilash S.L7f17e402019-03-15 17:40:41 +0530366
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700367 ponRMgr.InitDefaultPONResourceRanges(ONUIDStart, ONUIDEnd, ONUIDSharedPoolID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400368 AllocIDStart, AllocIDEnd, AllocIDSharedPoolID,
369 GEMPortIDStart, GEMPortIDEnd, GEMPortIDSharedPoolID,
370 FlowIDStart, FlowIDEnd, FlowIDSharedPoolID, 0, 1,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700371 devInfo.PonPorts, techRange.IntfIds)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530372
Girish Gowdru0c588b22019-04-23 23:24:56 -0400373 // For global sharing, make sure to refresh both local and global resource manager instances' range
Abhilash S.L7f17e402019-03-15 17:40:41 +0530374
Girish Gowdru0c588b22019-04-23 23:24:56 -0400375 if ONUIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700376 globalPONRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400377 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700378 ponRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
379 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400380 }
381 if AllocIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700382 globalPONRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400383 "", 0, nil)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530384
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700385 ponRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
386 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400387 }
388 if GEMPortIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700389 globalPONRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400390 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700391 ponRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
392 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400393 }
394 if FlowIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700395 globalPONRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400396 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700397 ponRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
398 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400399 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530400
Girish Gowdru0c588b22019-04-23 23:24:56 -0400401 // Make sure loaded range fits the platform bit encoding ranges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700402 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 +0530403}
404
Devmalya Paul495b94a2019-08-27 19:42:00 -0400405// Delete clears used resources for the particular olt device being deleted
npujarec5762e2020-01-01 14:08:48 +0530406func (RsrcMgr *OpenOltResourceMgr) Delete(ctx context.Context) error {
Devmalya Paul495b94a2019-08-27 19:42:00 -0400407 /* TODO
408 def __del__(self):
409 self.log.info("clearing-device-resource-pool")
410 for key, resource_mgr in self.resource_mgrs.iteritems():
411 resource_mgr.clear_device_resource_pool()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530412
Devmalya Paul495b94a2019-08-27 19:42:00 -0400413 def assert_pon_id_limit(self, pon_intf_id):
414 assert pon_intf_id in self.resource_mgrs
Abhilash S.L7f17e402019-03-15 17:40:41 +0530415
Devmalya Paul495b94a2019-08-27 19:42:00 -0400416 def assert_onu_id_limit(self, pon_intf_id, onu_id):
417 self.assert_pon_id_limit(pon_intf_id)
418 self.resource_mgrs[pon_intf_id].assert_resource_limits(onu_id, PONResourceManager.ONU_ID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530419
Devmalya Paul495b94a2019-08-27 19:42:00 -0400420 @property
421 def max_uni_id_per_onu(self):
422 return 0 #OpenOltPlatform.MAX_UNIS_PER_ONU-1, zero-based indexing Uncomment or override to make default multi-uni
Abhilash S.L7f17e402019-03-15 17:40:41 +0530423
Devmalya Paul495b94a2019-08-27 19:42:00 -0400424 def assert_uni_id_limit(self, pon_intf_id, onu_id, uni_id):
425 self.assert_onu_id_limit(pon_intf_id, onu_id)
426 self.resource_mgrs[pon_intf_id].assert_resource_limits(uni_id, PONResourceManager.UNI_ID)
427 */
428 for _, rsrcMgr := range RsrcMgr.ResourceMgrs {
npujarec5762e2020-01-01 14:08:48 +0530429 if err := rsrcMgr.ClearDeviceResourcePool(ctx); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000430 logger.Debug("Failed to clear device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400431 return err
432 }
433 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000434 logger.Debug("Cleared device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400435 return nil
436}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530437
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700438// GetONUID returns the available OnuID for the given pon-port
npujarec5762e2020-01-01 14:08:48 +0530439func (RsrcMgr *OpenOltResourceMgr) GetONUID(ctx context.Context, ponIntfID uint32) (uint32, error) {
salmansiddiqui352a45c2019-08-19 10:15:36 +0000440 // Check if Pon Interface ID is present in Resource-manager-map
Girish Gowdrade2739e2020-04-08 11:45:05 -0700441 RsrcMgr.OnuIDMgmtLock[ponIntfID].Lock()
442 defer RsrcMgr.OnuIDMgmtLock[ponIntfID].Unlock()
443
salmansiddiqui352a45c2019-08-19 10:15:36 +0000444 if _, ok := RsrcMgr.ResourceMgrs[ponIntfID]; !ok {
445 err := errors.New("invalid-pon-interface-" + strconv.Itoa(int(ponIntfID)))
446 return 0, err
447 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400448 // Get ONU id for a provided pon interface ID.
npujarec5762e2020-01-01 14:08:48 +0530449 ONUID, err := RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ctx, ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400450 ponrmgr.ONU_ID, 1)
451 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000452 logger.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700453 ponIntfID, ponrmgr.ONU_ID)
cbabuabf02352019-10-15 13:14:56 +0200454 return 0, err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400455 }
456 if ONUID != nil {
npujarec5762e2020-01-01 14:08:48 +0530457 RsrcMgr.ResourceMgrs[ponIntfID].InitResourceMap(ctx, fmt.Sprintf("%d,%d", ponIntfID, ONUID[0]))
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700458 return ONUID[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400459 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530460
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700461 return 0, err // return OnuID 0 on error
Abhilash S.L7f17e402019-03-15 17:40:41 +0530462}
463
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700464// GetFlowIDInfo returns the slice of flow info of the given pon-port
465// Note: For flows which trap from the NNI and not really associated with any particular
466// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
npujarec5762e2020-01-01 14:08:48 +0530467func (RsrcMgr *OpenOltResourceMgr) GetFlowIDInfo(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32, flowID uint32) *[]FlowInfo {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530468 var flows []FlowInfo
469
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700470 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530471 if err := RsrcMgr.ResourceMgrs[ponIntfID].GetFlowIDInfo(ctx, FlowPath, flowID, &flows); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000472 logger.Errorw("Error while getting flows from KV store", log.Fields{"flowId": flowID})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530473 return nil
474 }
475 if len(flows) == 0 {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000476 logger.Debugw("No flowInfo found in KV store", log.Fields{"flowPath": FlowPath})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530477 return nil
478 }
479 return &flows
480}
481
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700482// GetCurrentFlowIDsForOnu fetches flow ID from the resource manager
483// Note: For flows which trap from the NNI and not really associated with any particular
484// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
npujarec5762e2020-01-01 14:08:48 +0530485func (RsrcMgr *OpenOltResourceMgr) GetCurrentFlowIDsForOnu(ctx context.Context, PONIntfID uint32, ONUID int32, UNIID int32) []uint32 {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700486
Abhilash S.L8ee90712019-04-29 16:24:22 +0530487 FlowPath := fmt.Sprintf("%d,%d,%d", PONIntfID, ONUID, UNIID)
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700488 if mgrs, exist := RsrcMgr.ResourceMgrs[PONIntfID]; exist {
npujarec5762e2020-01-01 14:08:48 +0530489 return mgrs.GetCurrentFlowIDsForOnu(ctx, FlowPath)
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700490 }
491 return nil
Abhilash S.L8ee90712019-04-29 16:24:22 +0530492}
493
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700494// UpdateFlowIDInfo updates flow info for the given pon interface, onu id, and uni id
495// Note: For flows which trap from the NNI and not really associated with any particular
496// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
npujarec5762e2020-01-01 14:08:48 +0530497func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDInfo(ctx context.Context, ponIntfID int32, onuID int32, uniID int32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700498 flowID uint32, flowData *[]FlowInfo) error {
499 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530500 return RsrcMgr.ResourceMgrs[uint32(ponIntfID)].UpdateFlowIDInfoForOnu(ctx, FlowPath, flowID, *flowData)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530501}
502
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700503// GetFlowID return flow ID for a given pon interface id, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530504func (RsrcMgr *OpenOltResourceMgr) GetFlowID(ctx context.Context, ponIntfID uint32, ONUID int32, uniID int32,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400505 gemportID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700506 flowStoreCookie uint64,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400507 flowCategory string, vlanPcp ...uint32) (uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530508
Girish Gowdru0c588b22019-04-23 23:24:56 -0400509 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700510 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, ONUID, uniID)
Girish Gowdrade2739e2020-04-08 11:45:05 -0700511
512 RsrcMgr.FlowIDMgmtLock.Lock()
513 defer RsrcMgr.FlowIDMgmtLock.Unlock()
514
npujarec5762e2020-01-01 14:08:48 +0530515 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(ctx, FlowPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400516 if FlowIDs != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000517 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 -0700518 for _, flowID := range FlowIDs {
npujarec5762e2020-01-01 14:08:48 +0530519 FlowInfo := RsrcMgr.GetFlowIDInfo(ctx, ponIntfID, int32(ONUID), int32(uniID), uint32(flowID))
salmansiddiqui7ac62132019-08-22 03:58:50 +0000520 er := getFlowIDFromFlowInfo(FlowInfo, flowID, gemportID, flowStoreCookie, flowCategory, vlanPcp...)
521 if er == nil {
522 return flowID, er
Abhilash S.L8ee90712019-04-29 16:24:22 +0530523 }
524 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400525 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000526 logger.Debug("No matching flows with flow cookie or flow category, allocating new flowid")
npujarec5762e2020-01-01 14:08:48 +0530527 FlowIDs, err = RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ctx, ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400528 ponrmgr.FLOW_ID, 1)
529 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000530 logger.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700531 ponIntfID, ponrmgr.FLOW_ID)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400532 return uint32(0), err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400533 }
534 if FlowIDs != nil {
npujarec5762e2020-01-01 14:08:48 +0530535 _ = RsrcMgr.ResourceMgrs[ponIntfID].UpdateFlowIDForOnu(ctx, FlowPath, FlowIDs[0], true)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700536 return FlowIDs[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400537 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530538
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700539 return 0, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530540}
541
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700542// GetAllocID return the first Alloc ID for a given pon interface id and onu id and then update the resource map on
543// the KV store with the list of alloc_ids allocated for the pon_intf_onu_id tuple
544// Currently of all the alloc_ids available, it returns the first alloc_id in the list for tha given ONU
npujarec5762e2020-01-01 14:08:48 +0530545func (RsrcMgr *OpenOltResourceMgr) GetAllocID(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530546
Girish Gowdru0c588b22019-04-23 23:24:56 -0400547 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700548 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Girish Gowdrade2739e2020-04-08 11:45:05 -0700549
550 RsrcMgr.AllocIDMgmtLock[intfID].Lock()
551 defer RsrcMgr.AllocIDMgmtLock[intfID].Unlock()
552
npujarec5762e2020-01-01 14:08:48 +0530553 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400554 if AllocID != nil {
555 // Since we support only one alloc_id for the ONU at the moment,
556 // return the first alloc_id in the list, if available, for that
557 // ONU.
Girish Kumar2ad402b2020-03-20 19:45:12 +0000558 logger.Debugw("Retrieved alloc ID from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400559 return AllocID[0]
560 }
npujarec5762e2020-01-01 14:08:48 +0530561 AllocID, err = RsrcMgr.ResourceMgrs[intfID].GetResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400562 ponrmgr.ALLOC_ID, 1)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530563
Girish Gowdru0c588b22019-04-23 23:24:56 -0400564 if AllocID == nil || err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000565 logger.Error("Failed to allocate alloc id")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400566 return 0
567 }
568 // update the resource map on KV store with the list of alloc_id
569 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530570 err = RsrcMgr.ResourceMgrs[intfID].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID, AllocID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400571 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000572 logger.Error("Failed to update Alloc ID")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400573 return 0
574 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000575 logger.Debugw("Allocated new Tcont from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400576 return AllocID[0]
Abhilash S.L7f17e402019-03-15 17:40:41 +0530577}
578
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700579// UpdateAllocIdsForOnu updates alloc ids in kv store for a given pon interface id, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530580func (RsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ctx context.Context, ponPort uint32, onuID uint32, uniID uint32, allocID []uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530581
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700582 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530583 return RsrcMgr.ResourceMgrs[ponPort].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700584 allocID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530585}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700586
587// GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530588func (RsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(ctx context.Context, intfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700589 uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530590
Girish Gowdru0c588b22019-04-23 23:24:56 -0400591 /* Get gem ports for given pon interface , onu id and uni id. */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530592
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700593 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530594 return RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530595}
596
Gamze Abakafee36392019-10-03 11:17:24 +0000597// GetCurrentAllocIDsForOnu returns alloc ids for given pon interface and onu id
npujarec5762e2020-01-01 14:08:48 +0530598func (RsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDsForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530599
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700600 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530601 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400602 if AllocID != nil {
Gamze Abakafee36392019-10-03 11:17:24 +0000603 return AllocID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400604 }
Gamze Abakafee36392019-10-03 11:17:24 +0000605 return []uint32{}
606}
607
608// RemoveAllocIDForOnu removes the alloc id for given pon interface, onu id, uni id and alloc id
npujarec5762e2020-01-01 14:08:48 +0530609func (RsrcMgr *OpenOltResourceMgr) RemoveAllocIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, allocID uint32) {
610 allocIDs := RsrcMgr.GetCurrentAllocIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000611 for i := 0; i < len(allocIDs); i++ {
612 if allocIDs[i] == allocID {
613 allocIDs = append(allocIDs[:i], allocIDs[i+1:]...)
614 break
615 }
616 }
npujarec5762e2020-01-01 14:08:48 +0530617 err := RsrcMgr.UpdateAllocIdsForOnu(ctx, intfID, onuID, uniID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000618 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000619 logger.Errorf("Failed to Remove Alloc Id For Onu. IntfID %d onuID %d uniID %d allocID %d",
Gamze Abakafee36392019-10-03 11:17:24 +0000620 intfID, onuID, uniID, allocID)
621 }
622}
623
624// RemoveGemPortIDForOnu removes the gem port id for given pon interface, onu id, uni id and gem port id
npujarec5762e2020-01-01 14:08:48 +0530625func (RsrcMgr *OpenOltResourceMgr) RemoveGemPortIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, gemPortID uint32) {
626 gemPortIDs := RsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000627 for i := 0; i < len(gemPortIDs); i++ {
628 if gemPortIDs[i] == gemPortID {
629 gemPortIDs = append(gemPortIDs[:i], gemPortIDs[i+1:]...)
630 break
631 }
632 }
npujarec5762e2020-01-01 14:08:48 +0530633 err := RsrcMgr.UpdateGEMPortIDsForOnu(ctx, intfID, onuID, uniID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000634 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000635 logger.Errorf("Failed to Remove Gem Id For Onu. IntfID %d onuID %d uniID %d gemPortId %d",
Gamze Abakafee36392019-10-03 11:17:24 +0000636 intfID, onuID, uniID, gemPortID)
637 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530638}
639
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700640// UpdateGEMportsPonportToOnuMapOnKVStore updates onu and uni id associated with the gem port to the kv store
641// This stored information is used when packet_indication is received and we need to derive the ONU Id for which
642// the packet arrived based on the pon_intf and gemport available in the packet_indication
npujarec5762e2020-01-01 14:08:48 +0530643func (RsrcMgr *OpenOltResourceMgr) UpdateGEMportsPonportToOnuMapOnKVStore(ctx context.Context, gemPorts []uint32, PonPort uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700644 onuID uint32, uniID uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530645
Girish Gowdru0c588b22019-04-23 23:24:56 -0400646 /* Update onu and uni id associated with the gem port to the kv store. */
647 var IntfGEMPortPath string
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700648 Data := fmt.Sprintf("%d %d", onuID, uniID)
649 for _, GEM := range gemPorts {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400650 IntfGEMPortPath = fmt.Sprintf("%d,%d", PonPort, GEM)
651 Val, err := json.Marshal(Data)
652 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000653 logger.Error("failed to Marshal")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400654 return err
655 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700656
npujarec5762e2020-01-01 14:08:48 +0530657 if err = RsrcMgr.KVStore.Put(ctx, IntfGEMPortPath, Val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000658 logger.Errorf("Failed to update resource %s", IntfGEMPortPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400659 return err
660 }
661 }
662 return nil
Abhilash S.L7f17e402019-03-15 17:40:41 +0530663}
664
Gamze Abakafee36392019-10-03 11:17:24 +0000665// RemoveGEMportPonportToOnuMapOnKVStore removes the relationship between the gem port and pon port
npujarec5762e2020-01-01 14:08:48 +0530666func (RsrcMgr *OpenOltResourceMgr) RemoveGEMportPonportToOnuMapOnKVStore(ctx context.Context, GemPort uint32, PonPort uint32) {
Gamze Abakafee36392019-10-03 11:17:24 +0000667 IntfGEMPortPath := fmt.Sprintf("%d,%d", PonPort, GemPort)
npujarec5762e2020-01-01 14:08:48 +0530668 err := RsrcMgr.KVStore.Delete(ctx, IntfGEMPortPath)
Gamze Abakafee36392019-10-03 11:17:24 +0000669 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000670 logger.Errorf("Failed to Remove Gem port-Pon port to onu map on kv store. Gem %d PonPort %d", GemPort, PonPort)
Gamze Abakafee36392019-10-03 11:17:24 +0000671 }
672}
673
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700674// GetGEMPortID gets gem port id for a particular pon port, onu id and uni id and then update the resource map on
675// the KV store with the list of gemport_id allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530676func (RsrcMgr *OpenOltResourceMgr) GetGEMPortID(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700677 uniID uint32, NumOfPorts uint32) ([]uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530678
Girish Gowdru0c588b22019-04-23 23:24:56 -0400679 /* Get gem port id for a particular pon port, onu id
680 and uni id.
681 */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530682
Girish Gowdru0c588b22019-04-23 23:24:56 -0400683 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700684 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530685
Girish Gowdrade2739e2020-04-08 11:45:05 -0700686 RsrcMgr.GemPortIDMgmtLock[ponPort].Lock()
687 defer RsrcMgr.GemPortIDMgmtLock[ponPort].Unlock()
688
npujarec5762e2020-01-01 14:08:48 +0530689 GEMPortList := RsrcMgr.ResourceMgrs[ponPort].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400690 if GEMPortList != nil {
691 return GEMPortList, nil
692 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530693
npujarec5762e2020-01-01 14:08:48 +0530694 GEMPortList, err = RsrcMgr.ResourceMgrs[ponPort].GetResourceID(ctx, ponPort,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400695 ponrmgr.GEMPORT_ID, NumOfPorts)
696 if err != nil && GEMPortList == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000697 logger.Errorf("Failed to get gem port id for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400698 return nil, err
699 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530700
Girish Gowdru0c588b22019-04-23 23:24:56 -0400701 // update the resource map on KV store with the list of gemport_id
702 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530703 err = RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400704 GEMPortList)
705 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000706 logger.Errorf("Failed to update GEM ports to kv store for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400707 return nil, err
708 }
npujarec5762e2020-01-01 14:08:48 +0530709 _ = RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(ctx, GEMPortList, ponPort,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700710 onuID, uniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400711 return GEMPortList, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530712}
713
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700714// UpdateGEMPortIDsForOnu updates gemport ids on to the kv store for a given pon port, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530715func (RsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700716 uniID uint32, GEMPortList []uint32) error {
717 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530718 return RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400719 GEMPortList)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530720
721}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700722
723// FreeonuID releases(make free) onu id for a particular pon-port
npujarec5762e2020-01-01 14:08:48 +0530724func (RsrcMgr *OpenOltResourceMgr) FreeonuID(ctx context.Context, intfID uint32, onuID []uint32) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700725
Girish Gowdra3f182d82020-03-30 20:38:51 -0700726 RsrcMgr.OnuIDMgmtLock[intfID].Lock()
Girish Gowdrade2739e2020-04-08 11:45:05 -0700727 defer RsrcMgr.OnuIDMgmtLock[intfID].Unlock()
728
npujarec5762e2020-01-01 14:08:48 +0530729 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID, ponrmgr.ONU_ID, onuID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530730
Girish Gowdru0c588b22019-04-23 23:24:56 -0400731 /* Free onu id for a particular interface.*/
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700732 var IntfonuID string
733 for _, onu := range onuID {
734 IntfonuID = fmt.Sprintf("%d,%d", intfID, onu)
npujarec5762e2020-01-01 14:08:48 +0530735 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfonuID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400736 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530737}
738
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700739// FreeFlowID returns the free flow id for a given interface, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530740func (RsrcMgr *OpenOltResourceMgr) FreeFlowID(ctx context.Context, IntfID uint32, onuID int32,
Devmalya Paul495b94a2019-08-27 19:42:00 -0400741 uniID int32, FlowID uint32) {
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400742 var IntfONUID string
743 var err error
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530744
Girish Gowdrade2739e2020-04-08 11:45:05 -0700745 RsrcMgr.FlowIDMgmtLock.Lock()
746 defer RsrcMgr.FlowIDMgmtLock.Unlock()
747
748 FlowIds := make([]uint32, 0)
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530749 FlowIds = append(FlowIds, FlowID)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700750 IntfONUID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530751 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(ctx, IntfONUID, FlowID, false)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400752 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000753 logger.Errorw("Failed to Update flow id for", log.Fields{"intf": IntfONUID})
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400754 }
npujarec5762e2020-01-01 14:08:48 +0530755 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(ctx, IntfONUID, FlowID)
Girish Gowdrade2739e2020-04-08 11:45:05 -0700756
npujarec5762e2020-01-01 14:08:48 +0530757 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.FLOW_ID, FlowIds)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400758}
759
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700760// FreeFlowIDs releases the flow Ids
npujarec5762e2020-01-01 14:08:48 +0530761func (RsrcMgr *OpenOltResourceMgr) FreeFlowIDs(ctx context.Context, IntfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700762 uniID uint32, FlowID []uint32) {
Girish Gowdra3f182d82020-03-30 20:38:51 -0700763 RsrcMgr.FlowIDMgmtLock.Lock()
Girish Gowdrade2739e2020-04-08 11:45:05 -0700764 defer RsrcMgr.FlowIDMgmtLock.Unlock()
765
npujarec5762e2020-01-01 14:08:48 +0530766 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.FLOW_ID, FlowID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530767
Abhilash S.L8ee90712019-04-29 16:24:22 +0530768 var IntfOnuIDUniID string
Girish Gowdru0c588b22019-04-23 23:24:56 -0400769 var err error
770 for _, flow := range FlowID {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700771 IntfOnuIDUniID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530772 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(ctx, IntfOnuIDUniID, flow, false)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400773 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000774 logger.Errorw("Failed to Update flow id for", log.Fields{"intf": IntfOnuIDUniID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400775 }
npujarec5762e2020-01-01 14:08:48 +0530776 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(ctx, IntfOnuIDUniID, flow)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400777 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530778}
779
Gamze Abakafee36392019-10-03 11:17:24 +0000780// FreeAllocID frees AllocID on the PON resource pool and also frees the allocID association
781// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530782func (RsrcMgr *OpenOltResourceMgr) FreeAllocID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000783 uniID uint32, allocID uint32) {
Girish Gowdrade2739e2020-04-08 11:45:05 -0700784 RsrcMgr.AllocIDMgmtLock[IntfID].Lock()
785 defer RsrcMgr.AllocIDMgmtLock[IntfID].Unlock()
786
npujarec5762e2020-01-01 14:08:48 +0530787 RsrcMgr.RemoveAllocIDForOnu(ctx, IntfID, onuID, uniID, allocID)
Gamze Abakafee36392019-10-03 11:17:24 +0000788 allocIDs := make([]uint32, 0)
789 allocIDs = append(allocIDs, allocID)
npujarec5762e2020-01-01 14:08:48 +0530790 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.ALLOC_ID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000791}
792
793// FreeGemPortID frees GemPortID on the PON resource pool and also frees the gemPortID association
794// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530795func (RsrcMgr *OpenOltResourceMgr) FreeGemPortID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000796 uniID uint32, gemPortID uint32) {
Girish Gowdrade2739e2020-04-08 11:45:05 -0700797 RsrcMgr.GemPortIDMgmtLock[IntfID].Lock()
798 defer RsrcMgr.GemPortIDMgmtLock[IntfID].Unlock()
799
npujarec5762e2020-01-01 14:08:48 +0530800 RsrcMgr.RemoveGemPortIDForOnu(ctx, IntfID, onuID, uniID, gemPortID)
Gamze Abakafee36392019-10-03 11:17:24 +0000801 gemPortIDs := make([]uint32, 0)
802 gemPortIDs = append(gemPortIDs, gemPortID)
npujarec5762e2020-01-01 14:08:48 +0530803 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.GEMPORT_ID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000804}
805
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700806// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id, and the clears the
807// resource map and the onuID associated with (pon_intf_id, gemport_id) tuple,
npujarec5762e2020-01-01 14:08:48 +0530808func (RsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530809
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700810 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530811
Girish Gowdra6f012432020-04-09 15:04:27 -0700812 RsrcMgr.AllocIDMgmtLock[intfID].Lock()
Girish Gowdrade2739e2020-04-08 11:45:05 -0700813 AllocIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
npujarec5762e2020-01-01 14:08:48 +0530814 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400815 ponrmgr.ALLOC_ID,
816 AllocIDs)
Girish Gowdra6f012432020-04-09 15:04:27 -0700817 RsrcMgr.AllocIDMgmtLock[intfID].Unlock()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530818
Girish Gowdra6f012432020-04-09 15:04:27 -0700819 RsrcMgr.GemPortIDMgmtLock[intfID].Lock()
npujarec5762e2020-01-01 14:08:48 +0530820 GEMPortIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
821 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400822 ponrmgr.GEMPORT_ID,
823 GEMPortIDs)
Girish Gowdra6f012432020-04-09 15:04:27 -0700824 RsrcMgr.GemPortIDMgmtLock[intfID].Unlock()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530825
Girish Gowdra3f182d82020-03-30 20:38:51 -0700826 RsrcMgr.FlowIDMgmtLock.Lock()
npujarec5762e2020-01-01 14:08:48 +0530827 FlowIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentFlowIDsForOnu(ctx, IntfOnuIDUniID)
828 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400829 ponrmgr.FLOW_ID,
830 FlowIDs)
Girish Gowdra3f182d82020-03-30 20:38:51 -0700831 RsrcMgr.FlowIDMgmtLock.Unlock()
832
Girish Gowdru0c588b22019-04-23 23:24:56 -0400833 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
npujarec5762e2020-01-01 14:08:48 +0530834 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400835 // Clear the ONU Id associated with the (pon_intf_id, gemport_id) tuple.
836 for _, GEM := range GEMPortIDs {
npujarec5762e2020-01-01 14:08:48 +0530837 _ = RsrcMgr.KVStore.Delete(ctx, fmt.Sprintf("%d,%d", intfID, GEM))
Girish Gowdru0c588b22019-04-23 23:24:56 -0400838 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530839}
840
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700841// IsFlowCookieOnKVStore checks if the given flow cookie is present on the kv store
842// Returns true if the flow cookie is found, otherwise it returns false
npujarec5762e2020-01-01 14:08:48 +0530843func (RsrcMgr *OpenOltResourceMgr) IsFlowCookieOnKVStore(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700844 flowStoreCookie uint64) bool {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530845
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700846 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530847 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(ctx, FlowPath)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400848 if FlowIDs != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000849 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 -0700850 for _, flowID := range FlowIDs {
npujarec5762e2020-01-01 14:08:48 +0530851 FlowInfo := RsrcMgr.GetFlowIDInfo(ctx, ponIntfID, int32(onuID), int32(uniID), uint32(flowID))
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400852 if FlowInfo != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000853 logger.Debugw("Found flows", log.Fields{"flows": *FlowInfo, "flowId": flowID})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400854 for _, Info := range *FlowInfo {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700855 if Info.FlowStoreCookie == flowStoreCookie {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000856 logger.Debug("Found flow matching with flowStore cookie", log.Fields{"flowId": flowID, "flowStoreCookie": flowStoreCookie})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400857 return true
858 }
859 }
860 }
861 }
862 }
863 return false
864}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400865
salmansiddiqui7ac62132019-08-22 03:58:50 +0000866// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000867// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530868func (RsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) []uint32 {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000869 Path := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000870 var Data []uint32
npujarec5762e2020-01-01 14:08:48 +0530871 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400872 if err == nil {
873 if Value != nil {
874 Val, err := kvstore.ToByte(Value.Value)
875 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000876 logger.Errorw("Failed to convert into byte array", log.Fields{"error": err})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400877 return Data
878 }
879 if err = json.Unmarshal(Val, &Data); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000880 logger.Error("Failed to unmarshal", log.Fields{"error": err})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400881 return Data
882 }
883 }
884 } else {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000885 logger.Errorf("Failed to get TP id from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400886 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000887 logger.Debugf("Getting TP id %d from path %s", Data, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400888 return Data
889
890}
891
Gamze Abakafee36392019-10-03 11:17:24 +0000892// RemoveTechProfileIDsForOnu deletes all tech profile ids from the KV-Store for the given onu based on the path
893// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530894func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDsForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) error {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000895 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
npujarec5762e2020-01-01 14:08:48 +0530896 if err := RsrcMgr.KVStore.Delete(ctx, IntfOnuUniID); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000897 logger.Errorw("Failed to delete techprofile id resource in KV store", log.Fields{"path": IntfOnuUniID})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400898 return err
899 }
900 return nil
901}
902
Gamze Abakafee36392019-10-03 11:17:24 +0000903// RemoveTechProfileIDForOnu deletes a specific tech profile id from the KV-Store for the given onu based on the path
904// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530905func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32, TpID uint32) error {
906 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000907 for i, tpIDInList := range tpIDList {
908 if tpIDInList == TpID {
909 tpIDList = append(tpIDList[:i], tpIDList[i+1:]...)
910 }
911 }
912 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
913 Value, err := json.Marshal(tpIDList)
914 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000915 logger.Error("failed to Marshal")
Gamze Abakafee36392019-10-03 11:17:24 +0000916 return err
917 }
npujarec5762e2020-01-01 14:08:48 +0530918 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000919 logger.Errorf("Failed to update resource %s", IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000920 return err
921 }
922 return err
923}
924
925// UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
926// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530927func (RsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32,
salmansiddiqui7ac62132019-08-22 03:58:50 +0000928 UniID uint32, TpID uint32) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400929 var Value []byte
930 var err error
931
salmansiddiqui7ac62132019-08-22 03:58:50 +0000932 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000933
npujarec5762e2020-01-01 14:08:48 +0530934 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000935 for _, value := range tpIDList {
936 if value == TpID {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000937 logger.Debugf("TpID %d is already in tpIdList for the path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000938 return err
939 }
940 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000941 logger.Debugf("updating tp id %d on path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000942 tpIDList = append(tpIDList, TpID)
943 Value, err = json.Marshal(tpIDList)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400944 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000945 logger.Error("failed to Marshal")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400946 return err
947 }
npujarec5762e2020-01-01 14:08:48 +0530948 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000949 logger.Errorf("Failed to update resource %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400950 return err
951 }
952 return err
953}
954
salmansiddiqui7ac62132019-08-22 03:58:50 +0000955// UpdateMeterIDForOnu updates the meter id in the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000956// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +0530957func (RsrcMgr *OpenOltResourceMgr) UpdateMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000958 UniID uint32, TpID uint32, MeterConfig *ofp.OfpMeterConfig) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400959 var Value []byte
960 var err error
961
Gamze Abakafee36392019-10-03 11:17:24 +0000962 IntfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400963 Value, err = json.Marshal(*MeterConfig)
964 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000965 logger.Error("failed to Marshal meter config")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400966 return err
967 }
npujarec5762e2020-01-01 14:08:48 +0530968 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000969 logger.Errorf("Failed to store meter into KV store %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400970 return err
971 }
972 return err
973}
974
Gamze Abakafee36392019-10-03 11:17:24 +0000975// GetMeterIDForOnu fetches the meter id from the kv store for the given onu based on the path
976// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +0530977func (RsrcMgr *OpenOltResourceMgr) GetMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000978 UniID uint32, TpID uint32) (*ofp.OfpMeterConfig, error) {
979 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400980 var meterConfig ofp.OfpMeterConfig
npujarec5762e2020-01-01 14:08:48 +0530981 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400982 if err == nil {
983 if Value != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000984 logger.Debug("Found meter in KV store", log.Fields{"Direction": Direction})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000985 Val, er := kvstore.ToByte(Value.Value)
986 if er != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000987 logger.Errorw("Failed to convert into byte array", log.Fields{"error": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000988 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400989 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000990 if er = json.Unmarshal(Val, &meterConfig); er != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000991 logger.Error("Failed to unmarshal meterconfig", log.Fields{"error": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000992 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400993 }
994 } else {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000995 logger.Debug("meter-does-not-exists-in-KVStore")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400996 return nil, err
997 }
998 } else {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000999 logger.Errorf("Failed to get Meter config from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001000
1001 }
1002 return &meterConfig, err
1003}
1004
Gamze Abakafee36392019-10-03 11:17:24 +00001005// RemoveMeterIDForOnu deletes the meter id from the kV-Store for the given onu based on the path
1006// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +05301007func (RsrcMgr *OpenOltResourceMgr) RemoveMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +00001008 UniID uint32, TpID uint32) error {
1009 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
npujarec5762e2020-01-01 14:08:48 +05301010 if err := RsrcMgr.KVStore.Delete(ctx, Path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001011 logger.Errorf("Failed to delete meter id %s from kvstore ", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001012 return err
1013 }
1014 return nil
1015}
salmansiddiqui7ac62132019-08-22 03:58:50 +00001016
1017func getFlowIDFromFlowInfo(FlowInfo *[]FlowInfo, flowID, gemportID uint32, flowStoreCookie uint64, flowCategory string, vlanPcp ...uint32) error {
1018 if FlowInfo != nil {
1019 for _, Info := range *FlowInfo {
1020 if int32(gemportID) == Info.Flow.GemportId && flowCategory != "" && Info.FlowCategory == flowCategory {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001021 logger.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001022 if Info.FlowCategory == "HSIA_FLOW" && Info.Flow.Classifier.OPbits == vlanPcp[0] {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001023 logger.Debug("Found matching vlan pcp ", log.Fields{"flowId": flowID, "Vlanpcp": vlanPcp[0]})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001024 return nil
1025 }
1026 }
1027 if int32(gemportID) == Info.Flow.GemportId && flowStoreCookie != 0 && Info.FlowStoreCookie == flowStoreCookie {
1028 if flowCategory != "" && Info.FlowCategory == flowCategory {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001029 logger.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001030 return nil
1031 }
1032 }
1033 }
1034 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001035 logger.Debugw("the flow can be related to a different service", log.Fields{"flow_info": FlowInfo})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001036 return errors.New("invalid flow-info")
1037}
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301038
1039//AddGemToOnuGemInfo adds gemport to onugem info kvstore
npujarec5762e2020-01-01 14:08:48 +05301040func (RsrcMgr *OpenOltResourceMgr) AddGemToOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, gemPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301041 var onuGemData []OnuGemInfo
1042 var err error
1043
npujarec5762e2020-01-01 14:08:48 +05301044 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001045 logger.Errorf("failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301046 return err
1047 }
1048 if len(onuGemData) == 0 {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001049 logger.Errorw("failed to ger Onuid info ", log.Fields{"intfid": intfID, "onuid": onuID})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301050 return err
1051 }
1052
1053 for idx, onugem := range onuGemData {
1054 if onugem.OnuID == onuID {
1055 for _, gem := range onuGemData[idx].GemPorts {
1056 if gem == gemPort {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001057 logger.Debugw("Gem already present in onugem info, skpping addition", log.Fields{"gem": gem})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301058 return nil
1059 }
1060 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001061 logger.Debugw("Added gem to onugem info", log.Fields{"gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301062 onuGemData[idx].GemPorts = append(onuGemData[idx].GemPorts, gemPort)
1063 break
1064 }
1065 }
npujarec5762e2020-01-01 14:08:48 +05301066 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301067 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001068 logger.Error("Failed to add onugem to kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301069 return err
1070 }
1071 return err
1072}
1073
1074//GetOnuGemInfo gets onu gem info from the kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301075func (RsrcMgr *OpenOltResourceMgr) GetOnuGemInfo(ctx context.Context, IntfID uint32) ([]OnuGemInfo, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301076 var onuGemData []OnuGemInfo
1077
npujarec5762e2020-01-01 14:08:48 +05301078 if err := RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001079 logger.Errorf("failed to get onuifo for intfid %d", IntfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301080 return nil, err
1081 }
1082
1083 return onuGemData, nil
1084}
1085
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001086// AddOnuGemInfo adds onu info on to the kvstore per interface
1087func (RsrcMgr *OpenOltResourceMgr) AddOnuGemInfo(ctx context.Context, IntfID uint32, onuGem OnuGemInfo) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301088 var onuGemData []OnuGemInfo
1089 var err error
1090
npujarec5762e2020-01-01 14:08:48 +05301091 if err = RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001092 logger.Errorf("failed to get onuifo for intfid %d", IntfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301093 return err
1094 }
1095 onuGemData = append(onuGemData, onuGem)
npujarec5762e2020-01-01 14:08:48 +05301096 err = RsrcMgr.ResourceMgrs[IntfID].AddOnuGemInfo(ctx, IntfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301097 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001098 logger.Error("Failed to add onugem to kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301099 return err
1100 }
1101
Girish Kumar2ad402b2020-03-20 19:45:12 +00001102 logger.Debugw("added onu to onugeminfo", log.Fields{"intf": IntfID, "onugem": onuGem})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301103 return err
1104}
1105
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001106// UpdateOnuGemInfo updates Onuinfo on the kvstore per interface
1107func (RsrcMgr *OpenOltResourceMgr) UpdateOnuGemInfo(ctx context.Context, IntfID uint32, onuGem []OnuGemInfo) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301108
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001109 // TODO: VOL-2643
1110 err := RsrcMgr.ResourceMgrs[IntfID].AddOnuGemInfo(ctx, IntfID, onuGem)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301111 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001112 logger.Debugw("persistence-update-failed", log.Fields{
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001113 "interface-id": IntfID,
1114 "gem-info": onuGem,
1115 "error": err})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301116 return err
1117 }
1118
Girish Kumar2ad402b2020-03-20 19:45:12 +00001119 logger.Debugw("updated onugeminfo", log.Fields{"intf": IntfID, "onugem": onuGem})
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001120 return nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301121}
1122
1123// 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 +05301124func (RsrcMgr *OpenOltResourceMgr) AddUniPortToOnuInfo(ctx context.Context, intfID uint32, onuID uint32, portNo uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301125 var onuGemData []OnuGemInfo
1126 var err error
1127
npujarec5762e2020-01-01 14:08:48 +05301128 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001129 logger.Errorf("failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301130 return
1131 }
1132 for idx, onu := range onuGemData {
1133 if onu.OnuID == onuID {
1134 for _, uni := range onu.UniPorts {
1135 if uni == portNo {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001136 logger.Debugw("uni already present in onugem info", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301137 return
1138 }
1139 }
1140 onuGemData[idx].UniPorts = append(onuGemData[idx].UniPorts, portNo)
1141 break
1142 }
1143 }
npujarec5762e2020-01-01 14:08:48 +05301144 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301145 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001146 logger.Errorw("Failed to add uin port in onugem to kv store", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301147 return
1148 }
1149 return
1150}
1151
1152//UpdateGemPortForPktIn updates gemport for pkt in path to kvstore, path being intfid, onuid, portno
npujarec5762e2020-01-01 14:08:48 +05301153func (RsrcMgr *OpenOltResourceMgr) UpdateGemPortForPktIn(ctx context.Context, pktIn PacketInInfoKey, gemPort uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301154
1155 path := fmt.Sprintf(OnuPacketINPath, pktIn.IntfID, pktIn.OnuID, pktIn.LogicalPort)
1156 Value, err := json.Marshal(gemPort)
1157 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001158 logger.Error("Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301159 return
1160 }
npujarec5762e2020-01-01 14:08:48 +05301161 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001162 logger.Errorw("Failed to put to kvstore", log.Fields{"path": path, "value": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301163 return
1164 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001165 logger.Debugw("added gem packet in successfully", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301166
1167 return
1168}
1169
1170// GetGemPortFromOnuPktIn gets the gem port from onu pkt in path, path being intfid, onuid, portno
npujarec5762e2020-01-01 14:08:48 +05301171func (RsrcMgr *OpenOltResourceMgr) GetGemPortFromOnuPktIn(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) (uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301172
1173 var Val []byte
1174 var gemPort uint32
1175
1176 path := fmt.Sprintf(OnuPacketINPath, intfID, onuID, logicalPort)
1177
npujarec5762e2020-01-01 14:08:48 +05301178 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301179 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001180 logger.Errorw("Failed to get from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301181 return uint32(0), err
1182 } else if value == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001183 logger.Debugw("No pkt in gem found", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301184 return uint32(0), nil
1185 }
1186
1187 if Val, err = kvstore.ToByte(value.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001188 logger.Error("Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301189 return uint32(0), err
1190 }
1191 if err = json.Unmarshal(Val, &gemPort); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001192 logger.Error("Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301193 return uint32(0), err
1194 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001195 logger.Debugw("found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301196
1197 return gemPort, nil
1198}
1199
1200// DelGemPortPktIn deletes the gemport from the pkt in path
npujarec5762e2020-01-01 14:08:48 +05301201func (RsrcMgr *OpenOltResourceMgr) DelGemPortPktIn(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301202
1203 path := fmt.Sprintf(OnuPacketINPath, intfID, onuID, logicalPort)
npujarec5762e2020-01-01 14:08:48 +05301204 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001205 logger.Errorf("Falied to remove resource %s", path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301206 return err
1207 }
1208 return nil
1209}
1210
1211// DelOnuGemInfoForIntf deletes the onugem info from kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301212func (RsrcMgr *OpenOltResourceMgr) DelOnuGemInfoForIntf(ctx context.Context, intfID uint32) error {
1213 if err := RsrcMgr.ResourceMgrs[intfID].DelOnuGemInfoForIntf(ctx, intfID); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001214 logger.Errorw("failed to delete onu gem info for", log.Fields{"intfid": intfID})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301215 return err
1216 }
1217 return nil
1218}
1219
1220//GetNNIFromKVStore gets NNi intfids from kvstore. path being per device
npujarec5762e2020-01-01 14:08:48 +05301221func (RsrcMgr *OpenOltResourceMgr) GetNNIFromKVStore(ctx context.Context) ([]uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301222
1223 var nni []uint32
1224 var Val []byte
1225
1226 path := fmt.Sprintf(NnniIntfID)
npujarec5762e2020-01-01 14:08:48 +05301227 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301228 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001229 logger.Error("failed to get data from kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301230 return nil, err
1231 }
1232 if value != nil {
1233 if Val, err = kvstore.ToByte(value.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001234 logger.Error("Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301235 return nil, err
1236 }
1237 if err = json.Unmarshal(Val, &nni); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001238 logger.Error("Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301239 return nil, err
1240 }
1241 }
1242 return nni, err
1243}
1244
1245// AddNNIToKVStore adds Nni interfaces to kvstore, path being per device.
npujarec5762e2020-01-01 14:08:48 +05301246func (RsrcMgr *OpenOltResourceMgr) AddNNIToKVStore(ctx context.Context, nniIntf uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301247 var Value []byte
1248
npujarec5762e2020-01-01 14:08:48 +05301249 nni, err := RsrcMgr.GetNNIFromKVStore(ctx)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301250 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001251 logger.Error("failed to fetch nni interfaces from kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301252 return err
1253 }
1254
1255 path := fmt.Sprintf(NnniIntfID)
1256 nni = append(nni, nniIntf)
1257 Value, err = json.Marshal(nni)
1258 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001259 logger.Error("Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301260 }
npujarec5762e2020-01-01 14:08:48 +05301261 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001262 logger.Errorw("Failed to put to kvstore", log.Fields{"path": path, "value": Value})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301263 return err
1264 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001265 logger.Debugw("added nni to kv successfully", log.Fields{"path": path, "nni": nniIntf})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301266 return nil
1267}
1268
1269// DelNNiFromKVStore deletes nni interface list from kv store.
npujarec5762e2020-01-01 14:08:48 +05301270func (RsrcMgr *OpenOltResourceMgr) DelNNiFromKVStore(ctx context.Context) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301271
1272 path := fmt.Sprintf(NnniIntfID)
1273
npujarec5762e2020-01-01 14:08:48 +05301274 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001275 logger.Errorw("Failed to delete nni interfaces from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301276 return err
1277 }
1278 return nil
1279}
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301280
1281//UpdateFlowIDsForGem updates flow id per gemport
npujarec5762e2020-01-01 14:08:48 +05301282func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDsForGem(ctx context.Context, intf uint32, gem uint32, flowIDs []uint32) error {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301283 var val []byte
1284 path := fmt.Sprintf(FlowIDsForGem, intf)
1285
npujarec5762e2020-01-01 14:08:48 +05301286 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301287 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001288 logger.Error("Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301289 return err
1290 }
1291 if flowsForGem == nil {
1292 flowsForGem = make(map[uint32][]uint32)
1293 }
1294 flowsForGem[gem] = flowIDs
1295 val, err = json.Marshal(flowsForGem)
1296 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001297 logger.Error("Failed to marshal data", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301298 return err
1299 }
Girish Gowdrade2739e2020-04-08 11:45:05 -07001300
Girish Gowdra3f182d82020-03-30 20:38:51 -07001301 RsrcMgr.flowIDToGemInfoLock.Lock()
1302 defer RsrcMgr.flowIDToGemInfoLock.Unlock()
npujarec5762e2020-01-01 14:08:48 +05301303 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001304 logger.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301305 return err
1306 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001307 logger.Debugw("added flowid list for gem to kv successfully", log.Fields{"path": path, "flowidlist": flowsForGem[gem]})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301308 return nil
1309}
1310
1311//DeleteFlowIDsForGem deletes the flowID list entry per gem from kvstore.
npujarec5762e2020-01-01 14:08:48 +05301312func (RsrcMgr *OpenOltResourceMgr) DeleteFlowIDsForGem(ctx context.Context, intf uint32, gem uint32) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301313 path := fmt.Sprintf(FlowIDsForGem, intf)
1314 var val []byte
1315
npujarec5762e2020-01-01 14:08:48 +05301316 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301317 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001318 logger.Error("Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301319 return
1320 }
1321 if flowsForGem == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001322 logger.Error("No flowids found ", log.Fields{"intf": intf, "gemport": gem})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301323 return
1324 }
1325 // once we get the flows per gem map from kv , just delete the gem entry from the map
1326 delete(flowsForGem, gem)
1327 // once gem entry is deleted update the kv store.
1328 val, err = json.Marshal(flowsForGem)
1329 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001330 logger.Error("Failed to marshal data", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301331 return
1332 }
Girish Gowdra3f182d82020-03-30 20:38:51 -07001333
1334 RsrcMgr.flowIDToGemInfoLock.Lock()
1335 defer RsrcMgr.flowIDToGemInfoLock.Unlock()
npujarec5762e2020-01-01 14:08:48 +05301336 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001337 logger.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301338 return
1339 }
1340 return
1341}
1342
1343//GetFlowIDsGemMapForInterface gets flowids per gemport and interface
npujarec5762e2020-01-01 14:08:48 +05301344func (RsrcMgr *OpenOltResourceMgr) GetFlowIDsGemMapForInterface(ctx context.Context, intf uint32) (map[uint32][]uint32, error) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301345 path := fmt.Sprintf(FlowIDsForGem, intf)
1346 var flowsForGem map[uint32][]uint32
1347 var val []byte
Girish Gowdra3f182d82020-03-30 20:38:51 -07001348 RsrcMgr.flowIDToGemInfoLock.RLock()
npujarec5762e2020-01-01 14:08:48 +05301349 value, err := RsrcMgr.KVStore.Get(ctx, path)
Girish Gowdra3f182d82020-03-30 20:38:51 -07001350 RsrcMgr.flowIDToGemInfoLock.RUnlock()
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301351 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001352 logger.Error("failed to get data from kv store")
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301353 return nil, err
1354 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001355 if value != nil && value.Value != nil {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301356 if val, err = kvstore.ToByte(value.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001357 logger.Error("Failed to convert to byte array ", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301358 return nil, err
1359 }
1360 if err = json.Unmarshal(val, &flowsForGem); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001361 logger.Error("Failed to unmarshall", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301362 return nil, err
1363 }
1364 }
1365 return flowsForGem, nil
1366}
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301367
1368//DeleteIntfIDGempMapPath deletes the intf id path used to store flow ids per gem to kvstore.
npujarec5762e2020-01-01 14:08:48 +05301369func (RsrcMgr *OpenOltResourceMgr) DeleteIntfIDGempMapPath(ctx context.Context, intf uint32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301370 path := fmt.Sprintf(FlowIDsForGem, intf)
Girish Gowdra3f182d82020-03-30 20:38:51 -07001371 RsrcMgr.flowIDToGemInfoLock.Lock()
1372 defer RsrcMgr.flowIDToGemInfoLock.Unlock()
npujarec5762e2020-01-01 14:08:48 +05301373 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001374 logger.Errorw("Failed to delete nni interfaces from kv store", log.Fields{"path": path})
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301375 return
1376 }
1377 return
1378}
1379
1380// RemoveResourceMap Clear resource map associated with (intfid, onuid, uniid) tuple.
npujarec5762e2020-01-01 14:08:48 +05301381func (RsrcMgr *OpenOltResourceMgr) RemoveResourceMap(ctx context.Context, intfID uint32, onuID int32, uniID int32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301382 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +05301383 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301384}
Esin Karamanccb714b2019-11-29 15:02:06 +00001385
1386//GetMcastQueuePerInterfaceMap gets multicast queue info per pon interface
npujarec5762e2020-01-01 14:08:48 +05301387func (RsrcMgr *OpenOltResourceMgr) GetMcastQueuePerInterfaceMap(ctx context.Context) (map[uint32][]uint32, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001388 path := fmt.Sprintf(McastQueuesForIntf)
1389 var mcastQueueToIntfMap map[uint32][]uint32
1390 var val []byte
1391
npujarec5762e2020-01-01 14:08:48 +05301392 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001393 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001394 logger.Error("failed to get data from kv store")
Esin Karamanccb714b2019-11-29 15:02:06 +00001395 return nil, err
1396 }
1397 if kvPair != nil && kvPair.Value != nil {
1398 if val, err = kvstore.ToByte(kvPair.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001399 logger.Error("Failed to convert to byte array ", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001400 return nil, err
1401 }
1402 if err = json.Unmarshal(val, &mcastQueueToIntfMap); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001403 logger.Error("Failed to unmarshall ", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001404 return nil, err
1405 }
1406 }
1407 return mcastQueueToIntfMap, nil
1408}
1409
1410//AddMcastQueueForIntf adds multicast queue for pon interface
npujarec5762e2020-01-01 14:08:48 +05301411func (RsrcMgr *OpenOltResourceMgr) AddMcastQueueForIntf(ctx context.Context, intf uint32, gem uint32, servicePriority uint32) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001412 var val []byte
1413 path := fmt.Sprintf(McastQueuesForIntf)
1414
npujarec5762e2020-01-01 14:08:48 +05301415 mcastQueues, err := RsrcMgr.GetMcastQueuePerInterfaceMap(ctx)
Esin Karamanccb714b2019-11-29 15:02:06 +00001416 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001417 logger.Errorw("Failed to get multicast queue info for interface", log.Fields{"error": err, "intf": intf})
Esin Karamanccb714b2019-11-29 15:02:06 +00001418 return err
1419 }
1420 if mcastQueues == nil {
1421 mcastQueues = make(map[uint32][]uint32)
1422 }
1423 mcastQueues[intf] = []uint32{gem, servicePriority}
1424 if val, err = json.Marshal(mcastQueues); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001425 logger.Errorw("Failed to marshal data", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001426 return err
1427 }
npujarec5762e2020-01-01 14:08:48 +05301428 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001429 logger.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Esin Karamanccb714b2019-11-29 15:02:06 +00001430 return err
1431 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001432 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 +00001433 return nil
1434}
1435
1436//AddFlowGroupToKVStore adds flow group into KV store
npujarec5762e2020-01-01 14:08:48 +05301437func (RsrcMgr *OpenOltResourceMgr) AddFlowGroupToKVStore(ctx context.Context, groupEntry *ofp.OfpGroupEntry, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001438 var Value []byte
1439 var err error
1440 var path string
1441 if cached {
1442 path = fmt.Sprintf(FlowGroupCached, groupEntry.Desc.GroupId)
1443 } else {
1444 path = fmt.Sprintf(FlowGroup, groupEntry.Desc.GroupId)
1445 }
1446 //build group info object
1447 var outPorts []uint32
1448 for _, ofBucket := range groupEntry.Desc.Buckets {
1449 for _, ofAction := range ofBucket.Actions {
1450 if ofAction.Type == ofp.OfpActionType_OFPAT_OUTPUT {
1451 outPorts = append(outPorts, ofAction.GetOutput().Port)
1452 }
1453 }
1454 }
1455 groupInfo := GroupInfo{
1456 GroupID: groupEntry.Desc.GroupId,
1457 OutPorts: outPorts,
1458 }
1459
1460 Value, err = json.Marshal(groupInfo)
1461
1462 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001463 logger.Error("failed to Marshal flow group object")
Esin Karamanccb714b2019-11-29 15:02:06 +00001464 return err
1465 }
1466
npujarec5762e2020-01-01 14:08:48 +05301467 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001468 logger.Errorf("Failed to update resource %s", path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001469 return err
1470 }
1471 return nil
1472}
1473
1474//RemoveFlowGroupFromKVStore removes flow group from KV store
npujarec5762e2020-01-01 14:08:48 +05301475func (RsrcMgr *OpenOltResourceMgr) RemoveFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) bool {
Esin Karamanccb714b2019-11-29 15:02:06 +00001476 var path string
1477 if cached {
1478 path = fmt.Sprintf(FlowGroupCached, groupID)
1479 } else {
1480 path = fmt.Sprintf(FlowGroup, groupID)
1481 }
npujarec5762e2020-01-01 14:08:48 +05301482 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001483 logger.Errorf("Failed to remove resource %s due to %s", path, err)
Esin Karamanccb714b2019-11-29 15:02:06 +00001484 return false
1485 }
1486 return true
1487}
1488
1489//GetFlowGroupFromKVStore fetches flow group from the KV store. Returns (false, {} error) if any problem occurs during
1490//fetching the data. Returns (true, groupInfo, nil) if the group is fetched successfully.
1491// Returns (false, {}, nil) if the group does not exists in the KV store.
npujarec5762e2020-01-01 14:08:48 +05301492func (RsrcMgr *OpenOltResourceMgr) GetFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) (bool, GroupInfo, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001493 var groupInfo GroupInfo
1494 var path string
1495 if cached {
1496 path = fmt.Sprintf(FlowGroupCached, groupID)
1497 } else {
1498 path = fmt.Sprintf(FlowGroup, groupID)
1499 }
npujarec5762e2020-01-01 14:08:48 +05301500 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001501 if err != nil {
1502 return false, groupInfo, err
1503 }
1504 if kvPair != nil && kvPair.Value != nil {
1505 Val, err := kvstore.ToByte(kvPair.Value)
1506 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001507 logger.Errorw("Failed to convert flow group into byte array", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001508 return false, groupInfo, err
1509 }
1510 if err = json.Unmarshal(Val, &groupInfo); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001511 logger.Errorw("Failed to unmarshal", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001512 return false, groupInfo, err
1513 }
1514 return true, groupInfo, nil
1515 }
1516 return false, groupInfo, nil
1517}