blob: 540a45787b4537acfa2f5f7d85204f72c5e62817 [file] [log] [blame]
Abhilash S.L7f17e402019-03-15 17:40:41 +05301/*
2 * Copyright 2019-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070017//Package resourcemanager provides the utility for managing resources
manikkaraj kbf256be2019-03-25 00:13:48 +053018package resourcemanager
Abhilash S.L7f17e402019-03-15 17:40:41 +053019
20import (
npujarec5762e2020-01-01 14:08:48 +053021 "context"
Girish Gowdru0c588b22019-04-23 23:24:56 -040022 "encoding/json"
23 "errors"
24 "fmt"
Andrea Campanellab83b39d2020-03-30 11:41:16 +020025 "github.com/opencord/voltha-openolt-adapter/internal/pkg/olterrors"
Girish Gowdru0c588b22019-04-23 23:24:56 -040026 "strconv"
27 "strings"
Girish Gowdra38d533d2020-03-30 20:38:51 -070028 "sync"
Abhilash S.L7f17e402019-03-15 17:40:41 +053029
Esin Karamanccb714b2019-11-29 15:02:06 +000030 "github.com/opencord/voltha-lib-go/v3/pkg/db"
31 "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
32 "github.com/opencord/voltha-lib-go/v3/pkg/log"
33 ponrmgr "github.com/opencord/voltha-lib-go/v3/pkg/ponresourcemanager"
34 ofp "github.com/opencord/voltha-protos/v3/go/openflow_13"
35 "github.com/opencord/voltha-protos/v3/go/openolt"
Abhilash S.L7f17e402019-03-15 17:40:41 +053036)
37
salmansiddiqui7ac62132019-08-22 03:58:50 +000038const (
39 // KvstoreTimeout specifies the time out for KV Store Connection
40 KvstoreTimeout = 5
41 // BasePathKvStore - service/voltha/openolt/<device_id>
42 BasePathKvStore = "service/voltha/openolt/{%s}"
Gamze Abakafee36392019-10-03 11:17:24 +000043 // TpIDPathSuffix - <(pon_id, onu_id, uni_id)>/tp_id
44 TpIDPathSuffix = "{%d,%d,%d}/tp_id"
45 //MeterIDPathSuffix - <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
46 MeterIDPathSuffix = "{%d,%d,%d}/{%d}/meter_id/{%s}"
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053047 //NnniIntfID - nniintfids
48 NnniIntfID = "nniintfids"
49 // OnuPacketINPath path on the kvstore to store packetin gemport,which will be used for packetin, pcketout
50 //format: onu_packetin/<intfid>,<onuid>,<logicalport>
51 OnuPacketINPath = "onu_packetin/{%d,%d,%d}"
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +053052 //FlowIDsForGem flowids_per_gem/<intfid>
53 FlowIDsForGem = "flowids_per_gem/{%d}"
Esin Karamanccb714b2019-11-29 15:02:06 +000054 //McastQueuesForIntf multicast queues for pon interfaces
55 McastQueuesForIntf = "mcast_qs_for_int"
56 //FlowGroup flow_groups/<flow_group_id>
57 // A group is stored under this path on the KV store after it has been installed to the device.
58 // It should also be deleted after it has been removed from the device accordingly.
59 FlowGroup = "flow_groups/{%d}"
60 //FlowGroupCached flow_groups_cached/<flow_group_id>
61 // When a group add request received, we create the group without setting any members to it since we cannot
62 // set any members to a group until it is associated with a multicast flow. It is a BAL limitation.
63 // When the related multicast flow has been created we perform set members operation for the group.
64 // That is why we need to keep the members of a group until the multicast flow creation request comes.
65 // We preserve the groups under "FlowGroupsCached" directory in the KV store temporarily. Having set members,
66 // we remove the group from the cached group store.
67 FlowGroupCached = "flow_groups_cached/{%d}"
salmansiddiqui7ac62132019-08-22 03:58:50 +000068)
Abhilash S.L7f17e402019-03-15 17:40:41 +053069
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070070// FlowInfo holds the flow information
Abhilash S.L8ee90712019-04-29 16:24:22 +053071type FlowInfo struct {
72 Flow *openolt.Flow
73 FlowStoreCookie uint64
74 FlowCategory string
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053075 LogicalFlowID uint64
76}
77
78// OnuGemInfo holds onu information along with gem port list and uni port list
79type OnuGemInfo struct {
80 OnuID uint32
81 SerialNumber string
82 IntfID uint32
83 GemPorts []uint32
84 UniPorts []uint32
85}
86
87// PacketInInfoKey is the key for packet in gemport
88type PacketInInfoKey struct {
89 IntfID uint32
90 OnuID uint32
91 LogicalPort uint32
Abhilash S.L8ee90712019-04-29 16:24:22 +053092}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070093
Esin Karamanccb714b2019-11-29 15:02:06 +000094// GroupInfo holds group information
95type GroupInfo struct {
96 GroupID uint32
97 OutPorts []uint32
98}
99
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700100// OpenOltResourceMgr holds resource related information as provided below for each field
Abhilash S.L7f17e402019-03-15 17:40:41 +0530101type OpenOltResourceMgr struct {
sbarbaria8910ba2019-11-05 10:12:23 -0500102 DeviceID string // OLT device id
103 HostAndPort string // Host and port of the kv store to connect to
104 Args string // args
105 KVStore *db.Backend // backend kv store connection handle
Girish Gowdru0c588b22019-04-23 23:24:56 -0400106 DeviceType string
107 Host string // Host ip of the kv store
108 Port int // port of the kv store
109 DevInfo *openolt.DeviceInfo // device information
110 // array of pon resource managers per interface technology
111 ResourceMgrs map[uint32]*ponrmgr.PONResourceManager
Girish Gowdra38d533d2020-03-30 20:38:51 -0700112
113 // This protects concurrent gemport_id allocate/delete calls on a per PON port basis
114 GemPortIDMgmtLock []sync.RWMutex
115 // This protects concurrent alloc_id allocate/delete calls on a per PON port basis
116 AllocIDMgmtLock []sync.RWMutex
117 // This protects concurrent onu_id allocate/delete calls on a per PON port basis
118 OnuIDMgmtLock []sync.RWMutex
119 // This protects concurrent flow_id allocate/delete calls. We do not need this on a
120 // per PON port basis as flow IDs are unique across the OLT.
121 FlowIDMgmtLock sync.RWMutex
122
123 // This protects concurrent access to flowids_per_gem info stored on KV store
124 flowIDToGemInfoLock sync.RWMutex
Abhilash S.L7f17e402019-03-15 17:40:41 +0530125}
126
Manikkaraj kb1d51442019-07-23 10:41:02 -0400127func newKVClient(storeType string, address string, timeout uint32) (kvstore.Client, error) {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000128 logger.Infow("kv-store-type", log.Fields{"store": storeType})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400129 switch storeType {
130 case "consul":
131 return kvstore.NewConsulClient(address, int(timeout))
132 case "etcd":
133 return kvstore.NewEtcdClient(address, int(timeout))
134 }
135 return nil, errors.New("unsupported-kv-store")
Abhilash S.L7f17e402019-03-15 17:40:41 +0530136}
137
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700138// SetKVClient sets the KV client and return a kv backend
sbarbaria8910ba2019-11-05 10:12:23 -0500139func SetKVClient(backend string, Host string, Port int, DeviceID string) *db.Backend {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400140 addr := Host + ":" + strconv.Itoa(Port)
141 // TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
142 // issue between kv store and backend , core is not calling NewBackend directly
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700143 kvClient, err := newKVClient(backend, addr, KvstoreTimeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400144 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000145 logger.Fatalw("Failed to init KV client\n", log.Fields{"err": err})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400146 return nil
147 }
sbarbaria8910ba2019-11-05 10:12:23 -0500148 kvbackend := &db.Backend{
Girish Gowdru0c588b22019-04-23 23:24:56 -0400149 Client: kvClient,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700150 StoreType: backend,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400151 Host: Host,
152 Port: Port,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700153 Timeout: KvstoreTimeout,
154 PathPrefix: fmt.Sprintf(BasePathKvStore, DeviceID)}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530155
Girish Gowdru0c588b22019-04-23 23:24:56 -0400156 return kvbackend
Abhilash S.L7f17e402019-03-15 17:40:41 +0530157}
158
Gamze Abakafee36392019-10-03 11:17:24 +0000159// NewResourceMgr init a New resource manager instance which in turn instantiates pon resource manager
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700160// instances according to technology. Initializes the default resource ranges for all
161// the resources.
npujarec5762e2020-01-01 14:08:48 +0530162func NewResourceMgr(ctx context.Context, deviceID string, KVStoreHostPort string, kvStoreType string, deviceType string, devInfo *openolt.DeviceInfo) *OpenOltResourceMgr {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400163 var ResourceMgr OpenOltResourceMgr
Girish Kumar2ad402b2020-03-20 19:45:12 +0000164 logger.Debugf("Init new resource manager , host_port: %s, deviceid: %s", KVStoreHostPort, deviceID)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530165 ResourceMgr.HostAndPort = KVStoreHostPort
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700166 ResourceMgr.DeviceType = deviceType
167 ResourceMgr.DevInfo = devInfo
168 IPPort := strings.Split(KVStoreHostPort, ":")
169 ResourceMgr.Host = IPPort[0]
170 ResourceMgr.Port, _ = strconv.Atoi(IPPort[1])
Girish Gowdra38d533d2020-03-30 20:38:51 -0700171 NumPONPorts := devInfo.GetPonPorts()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530172
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700173 Backend := kvStoreType
Girish Gowdru0c588b22019-04-23 23:24:56 -0400174 ResourceMgr.KVStore = SetKVClient(Backend, ResourceMgr.Host,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700175 ResourceMgr.Port, deviceID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400176 if ResourceMgr.KVStore == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000177 logger.Error("Failed to setup KV store")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400178 }
179 Ranges := make(map[string]*openolt.DeviceInfo_DeviceResourceRanges)
180 RsrcMgrsByTech := make(map[string]*ponrmgr.PONResourceManager)
181 ResourceMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530182
Girish Gowdra38d533d2020-03-30 20:38:51 -0700183 ResourceMgr.AllocIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
184 ResourceMgr.GemPortIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
185 ResourceMgr.OnuIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
186
Girish Gowdru0c588b22019-04-23 23:24:56 -0400187 // TODO self.args = registry('main').get_args()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530188
Girish Gowdru0c588b22019-04-23 23:24:56 -0400189 /*
190 If a legacy driver returns protobuf without any ranges,s synthesize one from
Gamze Abakafee36392019-10-03 11:17:24 +0000191 the legacy global per-device information. This, in theory, is temporary until
Girish Gowdru0c588b22019-04-23 23:24:56 -0400192 the legacy drivers are upgrade to support pool ranges.
193 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700194 if devInfo.Ranges == nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400195 var ranges openolt.DeviceInfo_DeviceResourceRanges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700196 ranges.Technology = devInfo.GetTechnology()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530197
Girish Gowdru0c588b22019-04-23 23:24:56 -0400198 var index uint32
199 for index = 0; index < NumPONPorts; index++ {
200 ranges.IntfIds = append(ranges.IntfIds, index)
201 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530202
Abhilash S.L8ee90712019-04-29 16:24:22 +0530203 var Pool openolt.DeviceInfo_DeviceResourceRanges_Pool
Girish Gowdru0c588b22019-04-23 23:24:56 -0400204 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700205 Pool.Start = devInfo.OnuIdStart
206 Pool.End = devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400207 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
cbabuabf02352019-10-15 13:14:56 +0200208 onuPool := Pool
209 ranges.Pools = append(ranges.Pools, &onuPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530210
Girish Gowdru0c588b22019-04-23 23:24:56 -0400211 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700212 Pool.Start = devInfo.AllocIdStart
213 Pool.End = devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400214 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200215 allocPool := Pool
216 ranges.Pools = append(ranges.Pools, &allocPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530217
Girish Gowdru0c588b22019-04-23 23:24:56 -0400218 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700219 Pool.Start = devInfo.GemportIdStart
220 Pool.End = devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400221 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200222 gemPool := Pool
223 ranges.Pools = append(ranges.Pools, &gemPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530224
Girish Gowdru0c588b22019-04-23 23:24:56 -0400225 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700226 Pool.Start = devInfo.FlowIdStart
227 Pool.End = devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400228 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
Abhilash S.L8ee90712019-04-29 16:24:22 +0530229 ranges.Pools = append(ranges.Pools, &Pool)
230 // Add to device info
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700231 devInfo.Ranges = append(devInfo.Ranges, &ranges)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400232 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530233
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700234 // Create a separate Resource Manager instance for each range. This assumes that
Girish Gowdru0c588b22019-04-23 23:24:56 -0400235 // each technology is represented by only a single range
236 var GlobalPONRsrcMgr *ponrmgr.PONResourceManager
237 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700238 for _, TechRange := range devInfo.Ranges {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400239 technology := TechRange.Technology
Girish Kumar2ad402b2020-03-20 19:45:12 +0000240 logger.Debugf("Device info technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400241 Ranges[technology] = TechRange
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700242 RsrcMgrsByTech[technology], err = ponrmgr.NewPONResourceManager(technology, deviceType, deviceID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400243 Backend, ResourceMgr.Host, ResourceMgr.Port)
244 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000245 logger.Errorf("Failed to create pon resource manager instance for technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400246 return nil
247 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700248 // resource_mgrs_by_tech[technology] = resource_mgr
Girish Gowdru0c588b22019-04-23 23:24:56 -0400249 if GlobalPONRsrcMgr == nil {
250 GlobalPONRsrcMgr = RsrcMgrsByTech[technology]
251 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700252 for _, IntfID := range TechRange.IntfIds {
253 ResourceMgr.ResourceMgrs[uint32(IntfID)] = RsrcMgrsByTech[technology]
Girish Gowdru0c588b22019-04-23 23:24:56 -0400254 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700255 // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
npujarec5762e2020-01-01 14:08:48 +0530256 InitializeDeviceResourceRangeAndPool(ctx, RsrcMgrsByTech[technology], GlobalPONRsrcMgr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700257 TechRange, devInfo)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400258 }
259 // After we have initialized resource ranges, initialize the
260 // resource pools accordingly.
261 for _, PONRMgr := range RsrcMgrsByTech {
npujarec5762e2020-01-01 14:08:48 +0530262 _ = PONRMgr.InitDeviceResourcePool(ctx)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400263 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000264 logger.Info("Initialization of resource manager success!")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400265 return &ResourceMgr
Abhilash S.L7f17e402019-03-15 17:40:41 +0530266}
267
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700268// InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply
269// device specific information. If KV doesn't exist
270// or is broader than the device, the device's information will
271// dictate the range limits
npujarec5762e2020-01-01 14:08:48 +0530272func InitializeDeviceResourceRangeAndPool(ctx context.Context, ponRMgr *ponrmgr.PONResourceManager, globalPONRMgr *ponrmgr.PONResourceManager,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700273 techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530274
Girish Gowdru0c588b22019-04-23 23:24:56 -0400275 // init the resource range pool according to the sharing type
Abhilash S.L7f17e402019-03-15 17:40:41 +0530276
Girish Kumar2ad402b2020-03-20 19:45:12 +0000277 logger.Debugf("Resource range pool init for technology %s", ponRMgr.Technology)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700278 // first load from KV profiles
npujarec5762e2020-01-01 14:08:48 +0530279 status := ponRMgr.InitResourceRangesFromKVStore(ctx)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700280 if !status {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000281 logger.Debugf("Failed to load resource ranges from KV store for tech %s", ponRMgr.Technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400282 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530283
Girish Gowdru0c588b22019-04-23 23:24:56 -0400284 /*
285 Then apply device specific information. If KV doesn't exist
Gamze Abakafee36392019-10-03 11:17:24 +0000286 or is broader than the device, the device's information will
Girish Gowdru0c588b22019-04-23 23:24:56 -0400287 dictate the range limits
288 */
Girish Kumar2ad402b2020-03-20 19:45:12 +0000289 logger.Debugw("Using device info to init pon resource ranges", log.Fields{"Tech": ponRMgr.Technology})
Abhilash S.L7f17e402019-03-15 17:40:41 +0530290
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700291 ONUIDStart := devInfo.OnuIdStart
292 ONUIDEnd := devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400293 ONUIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
294 ONUIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700295 AllocIDStart := devInfo.AllocIdStart
296 AllocIDEnd := devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400297 AllocIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
298 AllocIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700299 GEMPortIDStart := devInfo.GemportIdStart
300 GEMPortIDEnd := devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400301 GEMPortIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
302 GEMPortIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700303 FlowIDStart := devInfo.FlowIdStart
304 FlowIDEnd := devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400305 FlowIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
306 FlowIDSharedPoolID := uint32(0)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530307
Girish Gowdru0c588b22019-04-23 23:24:56 -0400308 var FirstIntfPoolID uint32
309 var SharedPoolID uint32
Abhilash S.L7f17e402019-03-15 17:40:41 +0530310
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400311 /*
312 * 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 -0700313 * if resources are shared across interfaces then SharedPoolID is given a positive number.
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400314 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700315 for _, FirstIntfPoolID = range techRange.IntfIds {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400316 // skip the intf id 0
317 if FirstIntfPoolID == 0 {
318 continue
319 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400320 break
321 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530322
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700323 for _, RangePool := range techRange.Pools {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400324 if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400325 SharedPoolID = FirstIntfPoolID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400326 } else if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_SAME_TECH {
327 SharedPoolID = FirstIntfPoolID
328 } else {
329 SharedPoolID = 0
330 }
331 if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID {
332 ONUIDStart = RangePool.Start
333 ONUIDEnd = RangePool.End
334 ONUIDShared = RangePool.Sharing
335 ONUIDSharedPoolID = SharedPoolID
336 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID {
337 AllocIDStart = RangePool.Start
338 AllocIDEnd = RangePool.End
339 AllocIDShared = RangePool.Sharing
340 AllocIDSharedPoolID = SharedPoolID
341 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID {
342 GEMPortIDStart = RangePool.Start
343 GEMPortIDEnd = RangePool.End
344 GEMPortIDShared = RangePool.Sharing
345 GEMPortIDSharedPoolID = SharedPoolID
346 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID {
347 FlowIDStart = RangePool.Start
348 FlowIDEnd = RangePool.End
349 FlowIDShared = RangePool.Sharing
350 FlowIDSharedPoolID = SharedPoolID
351 }
352 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530353
Girish Kumar2ad402b2020-03-20 19:45:12 +0000354 logger.Debugw("Device info init", log.Fields{"technology": techRange.Technology,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400355 "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd, "onu_id_shared_pool_id": ONUIDSharedPoolID,
356 "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd,
357 "alloc_id_shared_pool_id": AllocIDSharedPoolID,
358 "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd,
359 "gemport_id_shared_pool_id": GEMPortIDSharedPoolID,
360 "flow_id_start": FlowIDStart,
361 "flow_id_end_idx": FlowIDEnd,
362 "flow_id_shared_pool_id": FlowIDSharedPoolID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700363 "intf_ids": techRange.IntfIds,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400364 "uni_id_start": 0,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700365 "uni_id_end_idx": 1, /*MaxUNIIDperONU()*/
366 })
Abhilash S.L7f17e402019-03-15 17:40:41 +0530367
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700368 ponRMgr.InitDefaultPONResourceRanges(ONUIDStart, ONUIDEnd, ONUIDSharedPoolID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400369 AllocIDStart, AllocIDEnd, AllocIDSharedPoolID,
370 GEMPortIDStart, GEMPortIDEnd, GEMPortIDSharedPoolID,
371 FlowIDStart, FlowIDEnd, FlowIDSharedPoolID, 0, 1,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700372 devInfo.PonPorts, techRange.IntfIds)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530373
Girish Gowdru0c588b22019-04-23 23:24:56 -0400374 // For global sharing, make sure to refresh both local and global resource manager instances' range
Abhilash S.L7f17e402019-03-15 17:40:41 +0530375
Girish Gowdru0c588b22019-04-23 23:24:56 -0400376 if ONUIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700377 globalPONRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400378 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700379 ponRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
380 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400381 }
382 if AllocIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700383 globalPONRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400384 "", 0, nil)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530385
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700386 ponRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
387 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400388 }
389 if GEMPortIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700390 globalPONRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400391 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700392 ponRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
393 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400394 }
395 if FlowIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700396 globalPONRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400397 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700398 ponRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
399 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400400 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530401
Girish Gowdru0c588b22019-04-23 23:24:56 -0400402 // Make sure loaded range fits the platform bit encoding ranges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700403 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 +0530404}
405
Devmalya Paul495b94a2019-08-27 19:42:00 -0400406// Delete clears used resources for the particular olt device being deleted
npujarec5762e2020-01-01 14:08:48 +0530407func (RsrcMgr *OpenOltResourceMgr) Delete(ctx context.Context) error {
Devmalya Paul495b94a2019-08-27 19:42:00 -0400408 /* TODO
409 def __del__(self):
410 self.log.info("clearing-device-resource-pool")
411 for key, resource_mgr in self.resource_mgrs.iteritems():
412 resource_mgr.clear_device_resource_pool()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530413
Devmalya Paul495b94a2019-08-27 19:42:00 -0400414 def assert_pon_id_limit(self, pon_intf_id):
415 assert pon_intf_id in self.resource_mgrs
Abhilash S.L7f17e402019-03-15 17:40:41 +0530416
Devmalya Paul495b94a2019-08-27 19:42:00 -0400417 def assert_onu_id_limit(self, pon_intf_id, onu_id):
418 self.assert_pon_id_limit(pon_intf_id)
419 self.resource_mgrs[pon_intf_id].assert_resource_limits(onu_id, PONResourceManager.ONU_ID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530420
Devmalya Paul495b94a2019-08-27 19:42:00 -0400421 @property
422 def max_uni_id_per_onu(self):
423 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 +0530424
Devmalya Paul495b94a2019-08-27 19:42:00 -0400425 def assert_uni_id_limit(self, pon_intf_id, onu_id, uni_id):
426 self.assert_onu_id_limit(pon_intf_id, onu_id)
427 self.resource_mgrs[pon_intf_id].assert_resource_limits(uni_id, PONResourceManager.UNI_ID)
428 */
429 for _, rsrcMgr := range RsrcMgr.ResourceMgrs {
npujarec5762e2020-01-01 14:08:48 +0530430 if err := rsrcMgr.ClearDeviceResourcePool(ctx); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000431 logger.Debug("Failed to clear device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400432 return err
433 }
434 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000435 logger.Debug("Cleared device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400436 return nil
437}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530438
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700439// GetONUID returns the available OnuID for the given pon-port
npujarec5762e2020-01-01 14:08:48 +0530440func (RsrcMgr *OpenOltResourceMgr) GetONUID(ctx context.Context, ponIntfID uint32) (uint32, error) {
salmansiddiqui352a45c2019-08-19 10:15:36 +0000441 // Check if Pon Interface ID is present in Resource-manager-map
Girish Gowdrab77ded92020-04-08 11:45:05 -0700442 RsrcMgr.OnuIDMgmtLock[ponIntfID].Lock()
443 defer RsrcMgr.OnuIDMgmtLock[ponIntfID].Unlock()
444
salmansiddiqui352a45c2019-08-19 10:15:36 +0000445 if _, ok := RsrcMgr.ResourceMgrs[ponIntfID]; !ok {
446 err := errors.New("invalid-pon-interface-" + strconv.Itoa(int(ponIntfID)))
447 return 0, err
448 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400449 // Get ONU id for a provided pon interface ID.
npujarec5762e2020-01-01 14:08:48 +0530450 ONUID, err := RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ctx, ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400451 ponrmgr.ONU_ID, 1)
452 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000453 logger.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700454 ponIntfID, ponrmgr.ONU_ID)
cbabuabf02352019-10-15 13:14:56 +0200455 return 0, err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400456 }
457 if ONUID != nil {
npujarec5762e2020-01-01 14:08:48 +0530458 RsrcMgr.ResourceMgrs[ponIntfID].InitResourceMap(ctx, fmt.Sprintf("%d,%d", ponIntfID, ONUID[0]))
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700459 return ONUID[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400460 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530461
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700462 return 0, err // return OnuID 0 on error
Abhilash S.L7f17e402019-03-15 17:40:41 +0530463}
464
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700465// GetFlowIDInfo returns the slice of flow info of the given pon-port
466// Note: For flows which trap from the NNI and not really associated with any particular
467// 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 +0530468func (RsrcMgr *OpenOltResourceMgr) GetFlowIDInfo(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32, flowID uint32) *[]FlowInfo {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530469 var flows []FlowInfo
470
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700471 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530472 if err := RsrcMgr.ResourceMgrs[ponIntfID].GetFlowIDInfo(ctx, FlowPath, flowID, &flows); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000473 logger.Errorw("Error while getting flows from KV store", log.Fields{"flowId": flowID})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530474 return nil
475 }
476 if len(flows) == 0 {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000477 logger.Debugw("No flowInfo found in KV store", log.Fields{"flowPath": FlowPath})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530478 return nil
479 }
480 return &flows
481}
482
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700483// GetCurrentFlowIDsForOnu fetches flow ID from the resource manager
484// Note: For flows which trap from the NNI and not really associated with any particular
485// 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 +0530486func (RsrcMgr *OpenOltResourceMgr) GetCurrentFlowIDsForOnu(ctx context.Context, PONIntfID uint32, ONUID int32, UNIID int32) []uint32 {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700487
Abhilash S.L8ee90712019-04-29 16:24:22 +0530488 FlowPath := fmt.Sprintf("%d,%d,%d", PONIntfID, ONUID, UNIID)
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700489 if mgrs, exist := RsrcMgr.ResourceMgrs[PONIntfID]; exist {
npujarec5762e2020-01-01 14:08:48 +0530490 return mgrs.GetCurrentFlowIDsForOnu(ctx, FlowPath)
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700491 }
492 return nil
Abhilash S.L8ee90712019-04-29 16:24:22 +0530493}
494
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700495// UpdateFlowIDInfo updates flow info for the given pon interface, onu id, and uni id
496// Note: For flows which trap from the NNI and not really associated with any particular
497// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
npujarec5762e2020-01-01 14:08:48 +0530498func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDInfo(ctx context.Context, ponIntfID int32, onuID int32, uniID int32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700499 flowID uint32, flowData *[]FlowInfo) error {
500 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530501 return RsrcMgr.ResourceMgrs[uint32(ponIntfID)].UpdateFlowIDInfoForOnu(ctx, FlowPath, flowID, *flowData)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530502}
503
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700504// GetFlowID return flow ID for a given pon interface id, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530505func (RsrcMgr *OpenOltResourceMgr) GetFlowID(ctx context.Context, ponIntfID uint32, ONUID int32, uniID int32,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400506 gemportID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700507 flowStoreCookie uint64,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400508 flowCategory string, vlanPcp ...uint32) (uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530509
Girish Gowdru0c588b22019-04-23 23:24:56 -0400510 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700511 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, ONUID, uniID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700512
513 RsrcMgr.FlowIDMgmtLock.Lock()
514 defer RsrcMgr.FlowIDMgmtLock.Unlock()
515
npujarec5762e2020-01-01 14:08:48 +0530516 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(ctx, FlowPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400517 if FlowIDs != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000518 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 -0700519 for _, flowID := range FlowIDs {
npujarec5762e2020-01-01 14:08:48 +0530520 FlowInfo := RsrcMgr.GetFlowIDInfo(ctx, ponIntfID, int32(ONUID), int32(uniID), uint32(flowID))
salmansiddiqui7ac62132019-08-22 03:58:50 +0000521 er := getFlowIDFromFlowInfo(FlowInfo, flowID, gemportID, flowStoreCookie, flowCategory, vlanPcp...)
522 if er == nil {
523 return flowID, er
Abhilash S.L8ee90712019-04-29 16:24:22 +0530524 }
525 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400526 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000527 logger.Debug("No matching flows with flow cookie or flow category, allocating new flowid")
npujarec5762e2020-01-01 14:08:48 +0530528 FlowIDs, err = RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ctx, ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400529 ponrmgr.FLOW_ID, 1)
530 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000531 logger.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700532 ponIntfID, ponrmgr.FLOW_ID)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400533 return uint32(0), err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400534 }
535 if FlowIDs != nil {
npujarec5762e2020-01-01 14:08:48 +0530536 _ = RsrcMgr.ResourceMgrs[ponIntfID].UpdateFlowIDForOnu(ctx, FlowPath, FlowIDs[0], true)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700537 return FlowIDs[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400538 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530539
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700540 return 0, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530541}
542
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700543// GetAllocID return the first Alloc ID for a given pon interface id and onu id and then update the resource map on
544// the KV store with the list of alloc_ids allocated for the pon_intf_onu_id tuple
545// 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 +0530546func (RsrcMgr *OpenOltResourceMgr) GetAllocID(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530547
Girish Gowdru0c588b22019-04-23 23:24:56 -0400548 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700549 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700550
551 RsrcMgr.AllocIDMgmtLock[intfID].Lock()
552 defer RsrcMgr.AllocIDMgmtLock[intfID].Unlock()
553
npujarec5762e2020-01-01 14:08:48 +0530554 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400555 if AllocID != nil {
556 // Since we support only one alloc_id for the ONU at the moment,
557 // return the first alloc_id in the list, if available, for that
558 // ONU.
Girish Kumar2ad402b2020-03-20 19:45:12 +0000559 logger.Debugw("Retrieved alloc ID from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400560 return AllocID[0]
561 }
npujarec5762e2020-01-01 14:08:48 +0530562 AllocID, err = RsrcMgr.ResourceMgrs[intfID].GetResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400563 ponrmgr.ALLOC_ID, 1)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530564
Girish Gowdru0c588b22019-04-23 23:24:56 -0400565 if AllocID == nil || err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000566 logger.Error("Failed to allocate alloc id")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400567 return 0
568 }
569 // update the resource map on KV store with the list of alloc_id
570 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530571 err = RsrcMgr.ResourceMgrs[intfID].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID, AllocID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400572 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000573 logger.Error("Failed to update Alloc ID")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400574 return 0
575 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000576 logger.Debugw("Allocated new Tcont from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400577 return AllocID[0]
Abhilash S.L7f17e402019-03-15 17:40:41 +0530578}
579
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700580// UpdateAllocIdsForOnu updates alloc ids in kv store for a given pon interface id, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530581func (RsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ctx context.Context, ponPort uint32, onuID uint32, uniID uint32, allocID []uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530582
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700583 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530584 return RsrcMgr.ResourceMgrs[ponPort].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700585 allocID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530586}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700587
588// GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530589func (RsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(ctx context.Context, intfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700590 uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530591
Girish Gowdru0c588b22019-04-23 23:24:56 -0400592 /* Get gem ports for given pon interface , onu id and uni id. */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530593
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700594 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530595 return RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530596}
597
Gamze Abakafee36392019-10-03 11:17:24 +0000598// GetCurrentAllocIDsForOnu returns alloc ids for given pon interface and onu id
npujarec5762e2020-01-01 14:08:48 +0530599func (RsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDsForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530600
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700601 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530602 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400603 if AllocID != nil {
Gamze Abakafee36392019-10-03 11:17:24 +0000604 return AllocID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400605 }
Gamze Abakafee36392019-10-03 11:17:24 +0000606 return []uint32{}
607}
608
609// RemoveAllocIDForOnu removes the alloc id for given pon interface, onu id, uni id and alloc id
npujarec5762e2020-01-01 14:08:48 +0530610func (RsrcMgr *OpenOltResourceMgr) RemoveAllocIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, allocID uint32) {
611 allocIDs := RsrcMgr.GetCurrentAllocIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000612 for i := 0; i < len(allocIDs); i++ {
613 if allocIDs[i] == allocID {
614 allocIDs = append(allocIDs[:i], allocIDs[i+1:]...)
615 break
616 }
617 }
npujarec5762e2020-01-01 14:08:48 +0530618 err := RsrcMgr.UpdateAllocIdsForOnu(ctx, intfID, onuID, uniID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000619 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000620 logger.Errorf("Failed to Remove Alloc Id For Onu. IntfID %d onuID %d uniID %d allocID %d",
Gamze Abakafee36392019-10-03 11:17:24 +0000621 intfID, onuID, uniID, allocID)
622 }
623}
624
625// RemoveGemPortIDForOnu removes the gem port id for given pon interface, onu id, uni id and gem port id
npujarec5762e2020-01-01 14:08:48 +0530626func (RsrcMgr *OpenOltResourceMgr) RemoveGemPortIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, gemPortID uint32) {
627 gemPortIDs := RsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000628 for i := 0; i < len(gemPortIDs); i++ {
629 if gemPortIDs[i] == gemPortID {
630 gemPortIDs = append(gemPortIDs[:i], gemPortIDs[i+1:]...)
631 break
632 }
633 }
npujarec5762e2020-01-01 14:08:48 +0530634 err := RsrcMgr.UpdateGEMPortIDsForOnu(ctx, intfID, onuID, uniID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000635 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000636 logger.Errorf("Failed to Remove Gem Id For Onu. IntfID %d onuID %d uniID %d gemPortId %d",
Gamze Abakafee36392019-10-03 11:17:24 +0000637 intfID, onuID, uniID, gemPortID)
638 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530639}
640
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700641// UpdateGEMportsPonportToOnuMapOnKVStore updates onu and uni id associated with the gem port to the kv store
642// This stored information is used when packet_indication is received and we need to derive the ONU Id for which
643// the packet arrived based on the pon_intf and gemport available in the packet_indication
npujarec5762e2020-01-01 14:08:48 +0530644func (RsrcMgr *OpenOltResourceMgr) UpdateGEMportsPonportToOnuMapOnKVStore(ctx context.Context, gemPorts []uint32, PonPort uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700645 onuID uint32, uniID uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530646
Girish Gowdru0c588b22019-04-23 23:24:56 -0400647 /* Update onu and uni id associated with the gem port to the kv store. */
648 var IntfGEMPortPath string
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700649 Data := fmt.Sprintf("%d %d", onuID, uniID)
650 for _, GEM := range gemPorts {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400651 IntfGEMPortPath = fmt.Sprintf("%d,%d", PonPort, GEM)
652 Val, err := json.Marshal(Data)
653 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000654 logger.Error("failed to Marshal")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400655 return err
656 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700657
npujarec5762e2020-01-01 14:08:48 +0530658 if err = RsrcMgr.KVStore.Put(ctx, IntfGEMPortPath, Val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000659 logger.Errorf("Failed to update resource %s", IntfGEMPortPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400660 return err
661 }
662 }
663 return nil
Abhilash S.L7f17e402019-03-15 17:40:41 +0530664}
665
Gamze Abakafee36392019-10-03 11:17:24 +0000666// RemoveGEMportPonportToOnuMapOnKVStore removes the relationship between the gem port and pon port
npujarec5762e2020-01-01 14:08:48 +0530667func (RsrcMgr *OpenOltResourceMgr) RemoveGEMportPonportToOnuMapOnKVStore(ctx context.Context, GemPort uint32, PonPort uint32) {
Gamze Abakafee36392019-10-03 11:17:24 +0000668 IntfGEMPortPath := fmt.Sprintf("%d,%d", PonPort, GemPort)
npujarec5762e2020-01-01 14:08:48 +0530669 err := RsrcMgr.KVStore.Delete(ctx, IntfGEMPortPath)
Gamze Abakafee36392019-10-03 11:17:24 +0000670 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000671 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 +0000672 }
673}
674
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700675// GetGEMPortID gets gem port id for a particular pon port, onu id and uni id and then update the resource map on
676// the KV store with the list of gemport_id allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530677func (RsrcMgr *OpenOltResourceMgr) GetGEMPortID(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700678 uniID uint32, NumOfPorts uint32) ([]uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530679
Girish Gowdru0c588b22019-04-23 23:24:56 -0400680 /* Get gem port id for a particular pon port, onu id
681 and uni id.
682 */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530683
Girish Gowdru0c588b22019-04-23 23:24:56 -0400684 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700685 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530686
Girish Gowdrab77ded92020-04-08 11:45:05 -0700687 RsrcMgr.GemPortIDMgmtLock[ponPort].Lock()
688 defer RsrcMgr.GemPortIDMgmtLock[ponPort].Unlock()
689
npujarec5762e2020-01-01 14:08:48 +0530690 GEMPortList := RsrcMgr.ResourceMgrs[ponPort].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400691 if GEMPortList != nil {
692 return GEMPortList, nil
693 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530694
npujarec5762e2020-01-01 14:08:48 +0530695 GEMPortList, err = RsrcMgr.ResourceMgrs[ponPort].GetResourceID(ctx, ponPort,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400696 ponrmgr.GEMPORT_ID, NumOfPorts)
697 if err != nil && GEMPortList == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000698 logger.Errorf("Failed to get gem port id for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400699 return nil, err
700 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530701
Girish Gowdru0c588b22019-04-23 23:24:56 -0400702 // update the resource map on KV store with the list of gemport_id
703 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530704 err = RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400705 GEMPortList)
706 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000707 logger.Errorf("Failed to update GEM ports to kv store for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400708 return nil, err
709 }
npujarec5762e2020-01-01 14:08:48 +0530710 _ = RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(ctx, GEMPortList, ponPort,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700711 onuID, uniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400712 return GEMPortList, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530713}
714
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700715// 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 +0530716func (RsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700717 uniID uint32, GEMPortList []uint32) error {
718 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530719 return RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400720 GEMPortList)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530721
722}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700723
724// FreeonuID releases(make free) onu id for a particular pon-port
npujarec5762e2020-01-01 14:08:48 +0530725func (RsrcMgr *OpenOltResourceMgr) FreeonuID(ctx context.Context, intfID uint32, onuID []uint32) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700726
Girish Gowdra38d533d2020-03-30 20:38:51 -0700727 RsrcMgr.OnuIDMgmtLock[intfID].Lock()
Girish Gowdrab77ded92020-04-08 11:45:05 -0700728 defer RsrcMgr.OnuIDMgmtLock[intfID].Unlock()
729
npujarec5762e2020-01-01 14:08:48 +0530730 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID, ponrmgr.ONU_ID, onuID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530731
Girish Gowdru0c588b22019-04-23 23:24:56 -0400732 /* Free onu id for a particular interface.*/
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700733 var IntfonuID string
734 for _, onu := range onuID {
735 IntfonuID = fmt.Sprintf("%d,%d", intfID, onu)
npujarec5762e2020-01-01 14:08:48 +0530736 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfonuID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400737 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530738}
739
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700740// FreeFlowID returns the free flow id for a given interface, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530741func (RsrcMgr *OpenOltResourceMgr) FreeFlowID(ctx context.Context, IntfID uint32, onuID int32,
Devmalya Paul495b94a2019-08-27 19:42:00 -0400742 uniID int32, FlowID uint32) {
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400743 var IntfONUID string
744 var err error
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530745
Girish Gowdrab77ded92020-04-08 11:45:05 -0700746 RsrcMgr.FlowIDMgmtLock.Lock()
747 defer RsrcMgr.FlowIDMgmtLock.Unlock()
748
749 FlowIds := make([]uint32, 0)
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530750 FlowIds = append(FlowIds, FlowID)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700751 IntfONUID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530752 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(ctx, IntfONUID, FlowID, false)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400753 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000754 logger.Errorw("Failed to Update flow id for", log.Fields{"intf": IntfONUID})
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400755 }
npujarec5762e2020-01-01 14:08:48 +0530756 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(ctx, IntfONUID, FlowID)
Girish Gowdrab77ded92020-04-08 11:45:05 -0700757
npujarec5762e2020-01-01 14:08:48 +0530758 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.FLOW_ID, FlowIds)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400759}
760
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700761// FreeFlowIDs releases the flow Ids
npujarec5762e2020-01-01 14:08:48 +0530762func (RsrcMgr *OpenOltResourceMgr) FreeFlowIDs(ctx context.Context, IntfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700763 uniID uint32, FlowID []uint32) {
Girish Gowdra38d533d2020-03-30 20:38:51 -0700764 RsrcMgr.FlowIDMgmtLock.Lock()
Girish Gowdrab77ded92020-04-08 11:45:05 -0700765 defer RsrcMgr.FlowIDMgmtLock.Unlock()
766
npujarec5762e2020-01-01 14:08:48 +0530767 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.FLOW_ID, FlowID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530768
Abhilash S.L8ee90712019-04-29 16:24:22 +0530769 var IntfOnuIDUniID string
Girish Gowdru0c588b22019-04-23 23:24:56 -0400770 var err error
771 for _, flow := range FlowID {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700772 IntfOnuIDUniID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530773 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(ctx, IntfOnuIDUniID, flow, false)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400774 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000775 logger.Errorw("Failed to Update flow id for", log.Fields{"intf": IntfOnuIDUniID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400776 }
npujarec5762e2020-01-01 14:08:48 +0530777 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(ctx, IntfOnuIDUniID, flow)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400778 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530779}
780
Gamze Abakafee36392019-10-03 11:17:24 +0000781// FreeAllocID frees AllocID on the PON resource pool and also frees the allocID association
782// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530783func (RsrcMgr *OpenOltResourceMgr) FreeAllocID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000784 uniID uint32, allocID uint32) {
Girish Gowdrab77ded92020-04-08 11:45:05 -0700785 RsrcMgr.AllocIDMgmtLock[IntfID].Lock()
786 defer RsrcMgr.AllocIDMgmtLock[IntfID].Unlock()
787
npujarec5762e2020-01-01 14:08:48 +0530788 RsrcMgr.RemoveAllocIDForOnu(ctx, IntfID, onuID, uniID, allocID)
Gamze Abakafee36392019-10-03 11:17:24 +0000789 allocIDs := make([]uint32, 0)
790 allocIDs = append(allocIDs, allocID)
npujarec5762e2020-01-01 14:08:48 +0530791 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.ALLOC_ID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000792}
793
794// FreeGemPortID frees GemPortID on the PON resource pool and also frees the gemPortID association
795// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530796func (RsrcMgr *OpenOltResourceMgr) FreeGemPortID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000797 uniID uint32, gemPortID uint32) {
Girish Gowdrab77ded92020-04-08 11:45:05 -0700798 RsrcMgr.GemPortIDMgmtLock[IntfID].Lock()
799 defer RsrcMgr.GemPortIDMgmtLock[IntfID].Unlock()
800
npujarec5762e2020-01-01 14:08:48 +0530801 RsrcMgr.RemoveGemPortIDForOnu(ctx, IntfID, onuID, uniID, gemPortID)
Gamze Abakafee36392019-10-03 11:17:24 +0000802 gemPortIDs := make([]uint32, 0)
803 gemPortIDs = append(gemPortIDs, gemPortID)
npujarec5762e2020-01-01 14:08:48 +0530804 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.GEMPORT_ID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000805}
806
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700807// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id, and the clears the
808// resource map and the onuID associated with (pon_intf_id, gemport_id) tuple,
npujarec5762e2020-01-01 14:08:48 +0530809func (RsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530810
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700811 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530812
Girish Gowdra38d533d2020-03-30 20:38:51 -0700813 RsrcMgr.AllocIDMgmtLock[onuID].Lock()
Girish Gowdrab77ded92020-04-08 11:45:05 -0700814 AllocIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
npujarec5762e2020-01-01 14:08:48 +0530815 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400816 ponrmgr.ALLOC_ID,
817 AllocIDs)
Girish Gowdra38d533d2020-03-30 20:38:51 -0700818 RsrcMgr.AllocIDMgmtLock[onuID].Unlock()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530819
Girish Gowdra38d533d2020-03-30 20:38:51 -0700820 RsrcMgr.GemPortIDMgmtLock[onuID].Lock()
npujarec5762e2020-01-01 14:08:48 +0530821 GEMPortIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
822 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400823 ponrmgr.GEMPORT_ID,
824 GEMPortIDs)
Girish Gowdra38d533d2020-03-30 20:38:51 -0700825 RsrcMgr.GemPortIDMgmtLock[onuID].Unlock()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530826
Girish Gowdra38d533d2020-03-30 20:38:51 -0700827 RsrcMgr.FlowIDMgmtLock.Lock()
npujarec5762e2020-01-01 14:08:48 +0530828 FlowIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentFlowIDsForOnu(ctx, IntfOnuIDUniID)
829 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400830 ponrmgr.FLOW_ID,
831 FlowIDs)
Girish Gowdra38d533d2020-03-30 20:38:51 -0700832 RsrcMgr.FlowIDMgmtLock.Unlock()
833
Girish Gowdru0c588b22019-04-23 23:24:56 -0400834 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
npujarec5762e2020-01-01 14:08:48 +0530835 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400836 // Clear the ONU Id associated with the (pon_intf_id, gemport_id) tuple.
837 for _, GEM := range GEMPortIDs {
npujarec5762e2020-01-01 14:08:48 +0530838 _ = RsrcMgr.KVStore.Delete(ctx, fmt.Sprintf("%d,%d", intfID, GEM))
Girish Gowdru0c588b22019-04-23 23:24:56 -0400839 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530840}
841
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700842// IsFlowCookieOnKVStore checks if the given flow cookie is present on the kv store
843// Returns true if the flow cookie is found, otherwise it returns false
npujarec5762e2020-01-01 14:08:48 +0530844func (RsrcMgr *OpenOltResourceMgr) IsFlowCookieOnKVStore(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700845 flowStoreCookie uint64) bool {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530846
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700847 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530848 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(ctx, FlowPath)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400849 if FlowIDs != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000850 logger.Debugw("Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "onuID": onuID, "uniID": uniID, "KVpath": FlowPath})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700851 for _, flowID := range FlowIDs {
npujarec5762e2020-01-01 14:08:48 +0530852 FlowInfo := RsrcMgr.GetFlowIDInfo(ctx, ponIntfID, int32(onuID), int32(uniID), uint32(flowID))
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400853 if FlowInfo != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000854 logger.Debugw("Found flows", log.Fields{"flows": *FlowInfo, "flowId": flowID})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400855 for _, Info := range *FlowInfo {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700856 if Info.FlowStoreCookie == flowStoreCookie {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000857 logger.Debug("Found flow matching with flowStore cookie", log.Fields{"flowId": flowID, "flowStoreCookie": flowStoreCookie})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400858 return true
859 }
860 }
861 }
862 }
863 }
864 return false
865}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400866
salmansiddiqui7ac62132019-08-22 03:58:50 +0000867// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000868// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530869func (RsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) []uint32 {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000870 Path := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000871 var Data []uint32
npujarec5762e2020-01-01 14:08:48 +0530872 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400873 if err == nil {
874 if Value != nil {
875 Val, err := kvstore.ToByte(Value.Value)
876 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000877 logger.Errorw("Failed to convert into byte array", log.Fields{"error": err})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400878 return Data
879 }
880 if err = json.Unmarshal(Val, &Data); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000881 logger.Error("Failed to unmarshal", log.Fields{"error": err})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400882 return Data
883 }
884 }
885 } else {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000886 logger.Errorf("Failed to get TP id from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400887 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000888 logger.Debugf("Getting TP id %d from path %s", Data, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400889 return Data
890
891}
892
Gamze Abakafee36392019-10-03 11:17:24 +0000893// RemoveTechProfileIDsForOnu deletes all tech profile ids from the KV-Store for the given onu based on the path
894// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530895func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDsForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) error {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000896 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
npujarec5762e2020-01-01 14:08:48 +0530897 if err := RsrcMgr.KVStore.Delete(ctx, IntfOnuUniID); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000898 logger.Errorw("Failed to delete techprofile id resource in KV store", log.Fields{"path": IntfOnuUniID})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400899 return err
900 }
901 return nil
902}
903
Gamze Abakafee36392019-10-03 11:17:24 +0000904// RemoveTechProfileIDForOnu deletes a specific tech profile id from the KV-Store for the given onu based on the path
905// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530906func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32, TpID uint32) error {
907 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000908 for i, tpIDInList := range tpIDList {
909 if tpIDInList == TpID {
910 tpIDList = append(tpIDList[:i], tpIDList[i+1:]...)
911 }
912 }
913 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
914 Value, err := json.Marshal(tpIDList)
915 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000916 logger.Error("failed to Marshal")
Gamze Abakafee36392019-10-03 11:17:24 +0000917 return err
918 }
npujarec5762e2020-01-01 14:08:48 +0530919 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000920 logger.Errorf("Failed to update resource %s", IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000921 return err
922 }
923 return err
924}
925
926// UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
927// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530928func (RsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32,
salmansiddiqui7ac62132019-08-22 03:58:50 +0000929 UniID uint32, TpID uint32) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400930 var Value []byte
931 var err error
932
salmansiddiqui7ac62132019-08-22 03:58:50 +0000933 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000934
npujarec5762e2020-01-01 14:08:48 +0530935 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000936 for _, value := range tpIDList {
937 if value == TpID {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000938 logger.Debugf("TpID %d is already in tpIdList for the path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000939 return err
940 }
941 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000942 logger.Debugf("updating tp id %d on path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000943 tpIDList = append(tpIDList, TpID)
944 Value, err = json.Marshal(tpIDList)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400945 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000946 logger.Error("failed to Marshal")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400947 return err
948 }
npujarec5762e2020-01-01 14:08:48 +0530949 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000950 logger.Errorf("Failed to update resource %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400951 return err
952 }
953 return err
954}
955
salmansiddiqui7ac62132019-08-22 03:58:50 +0000956// UpdateMeterIDForOnu updates the meter id in the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000957// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +0530958func (RsrcMgr *OpenOltResourceMgr) UpdateMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000959 UniID uint32, TpID uint32, MeterConfig *ofp.OfpMeterConfig) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400960 var Value []byte
961 var err error
962
Gamze Abakafee36392019-10-03 11:17:24 +0000963 IntfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400964 Value, err = json.Marshal(*MeterConfig)
965 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000966 logger.Error("failed to Marshal meter config")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400967 return err
968 }
npujarec5762e2020-01-01 14:08:48 +0530969 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000970 logger.Errorf("Failed to store meter into KV store %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400971 return err
972 }
973 return err
974}
975
Gamze Abakafee36392019-10-03 11:17:24 +0000976// GetMeterIDForOnu fetches the meter id from the kv store for the given onu based on the path
977// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +0530978func (RsrcMgr *OpenOltResourceMgr) GetMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000979 UniID uint32, TpID uint32) (*ofp.OfpMeterConfig, error) {
980 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400981 var meterConfig ofp.OfpMeterConfig
npujarec5762e2020-01-01 14:08:48 +0530982 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400983 if err == nil {
984 if Value != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000985 logger.Debug("Found meter in KV store", log.Fields{"Direction": Direction})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000986 Val, er := kvstore.ToByte(Value.Value)
987 if er != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000988 logger.Errorw("Failed to convert into byte array", log.Fields{"error": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000989 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400990 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000991 if er = json.Unmarshal(Val, &meterConfig); er != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000992 logger.Error("Failed to unmarshal meterconfig", log.Fields{"error": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000993 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400994 }
995 } else {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000996 logger.Debug("meter-does-not-exists-in-KVStore")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400997 return nil, err
998 }
999 } else {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001000 logger.Errorf("Failed to get Meter config from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001001
1002 }
1003 return &meterConfig, err
1004}
1005
Gamze Abakafee36392019-10-03 11:17:24 +00001006// RemoveMeterIDForOnu deletes the meter id from the kV-Store for the given onu based on the path
1007// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +05301008func (RsrcMgr *OpenOltResourceMgr) RemoveMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +00001009 UniID uint32, TpID uint32) error {
1010 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
npujarec5762e2020-01-01 14:08:48 +05301011 if err := RsrcMgr.KVStore.Delete(ctx, Path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001012 logger.Errorf("Failed to delete meter id %s from kvstore ", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001013 return err
1014 }
1015 return nil
1016}
salmansiddiqui7ac62132019-08-22 03:58:50 +00001017
1018func getFlowIDFromFlowInfo(FlowInfo *[]FlowInfo, flowID, gemportID uint32, flowStoreCookie uint64, flowCategory string, vlanPcp ...uint32) error {
1019 if FlowInfo != nil {
1020 for _, Info := range *FlowInfo {
1021 if int32(gemportID) == Info.Flow.GemportId && flowCategory != "" && Info.FlowCategory == flowCategory {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001022 logger.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001023 if Info.FlowCategory == "HSIA_FLOW" && Info.Flow.Classifier.OPbits == vlanPcp[0] {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001024 logger.Debug("Found matching vlan pcp ", log.Fields{"flowId": flowID, "Vlanpcp": vlanPcp[0]})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001025 return nil
1026 }
1027 }
1028 if int32(gemportID) == Info.Flow.GemportId && flowStoreCookie != 0 && Info.FlowStoreCookie == flowStoreCookie {
1029 if flowCategory != "" && Info.FlowCategory == flowCategory {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001030 logger.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001031 return nil
1032 }
1033 }
1034 }
1035 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001036 logger.Debugw("the flow can be related to a different service", log.Fields{"flow_info": FlowInfo})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001037 return errors.New("invalid flow-info")
1038}
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301039
1040//AddGemToOnuGemInfo adds gemport to onugem info kvstore
npujarec5762e2020-01-01 14:08:48 +05301041func (RsrcMgr *OpenOltResourceMgr) AddGemToOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, gemPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301042 var onuGemData []OnuGemInfo
1043 var err error
1044
npujarec5762e2020-01-01 14:08:48 +05301045 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001046 logger.Errorf("failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301047 return err
1048 }
1049 if len(onuGemData) == 0 {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001050 logger.Errorw("failed to ger Onuid info ", log.Fields{"intfid": intfID, "onuid": onuID})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301051 return err
1052 }
1053
1054 for idx, onugem := range onuGemData {
1055 if onugem.OnuID == onuID {
1056 for _, gem := range onuGemData[idx].GemPorts {
1057 if gem == gemPort {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001058 logger.Debugw("Gem already present in onugem info, skpping addition", log.Fields{"gem": gem})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301059 return nil
1060 }
1061 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001062 logger.Debugw("Added gem to onugem info", log.Fields{"gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301063 onuGemData[idx].GemPorts = append(onuGemData[idx].GemPorts, gemPort)
1064 break
1065 }
1066 }
npujarec5762e2020-01-01 14:08:48 +05301067 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301068 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001069 logger.Error("Failed to add onugem to kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301070 return err
1071 }
1072 return err
1073}
1074
1075//GetOnuGemInfo gets onu gem info from the kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301076func (RsrcMgr *OpenOltResourceMgr) GetOnuGemInfo(ctx context.Context, IntfID uint32) ([]OnuGemInfo, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301077 var onuGemData []OnuGemInfo
1078
npujarec5762e2020-01-01 14:08:48 +05301079 if err := RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001080 logger.Errorf("failed to get onuifo for intfid %d", IntfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301081 return nil, err
1082 }
1083
1084 return onuGemData, nil
1085}
1086
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001087// AddOnuGemInfo adds onu info on to the kvstore per interface
1088func (RsrcMgr *OpenOltResourceMgr) AddOnuGemInfo(ctx context.Context, IntfID uint32, onuGem OnuGemInfo) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301089 var onuGemData []OnuGemInfo
1090 var err error
1091
npujarec5762e2020-01-01 14:08:48 +05301092 if err = RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001093 logger.Errorf("failed to get onuifo for intfid %d", IntfID)
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001094 return olterrors.NewErrPersistence("get", "OnuGemInfo", IntfID,
1095 log.Fields{"onuGem": onuGem, "intfID": IntfID}, err)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301096 }
1097 onuGemData = append(onuGemData, onuGem)
npujarec5762e2020-01-01 14:08:48 +05301098 err = RsrcMgr.ResourceMgrs[IntfID].AddOnuGemInfo(ctx, IntfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301099 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001100 logger.Error("Failed to add onugem to kv store")
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001101 return olterrors.NewErrPersistence("set", "OnuGemInfo", IntfID,
1102 log.Fields{"onuGemData": onuGemData, "intfID": IntfID}, err)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301103 }
1104
Girish Kumar2ad402b2020-03-20 19:45:12 +00001105 logger.Debugw("added onu to onugeminfo", log.Fields{"intf": IntfID, "onugem": onuGem})
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001106 return nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301107}
1108
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301109// 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 +05301110func (RsrcMgr *OpenOltResourceMgr) AddUniPortToOnuInfo(ctx context.Context, intfID uint32, onuID uint32, portNo uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301111 var onuGemData []OnuGemInfo
1112 var err error
1113
npujarec5762e2020-01-01 14:08:48 +05301114 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001115 logger.Errorf("failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301116 return
1117 }
1118 for idx, onu := range onuGemData {
1119 if onu.OnuID == onuID {
1120 for _, uni := range onu.UniPorts {
1121 if uni == portNo {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001122 logger.Debugw("uni already present in onugem info", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301123 return
1124 }
1125 }
1126 onuGemData[idx].UniPorts = append(onuGemData[idx].UniPorts, portNo)
1127 break
1128 }
1129 }
npujarec5762e2020-01-01 14:08:48 +05301130 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301131 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001132 logger.Errorw("Failed to add uin port in onugem to kv store", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301133 return
1134 }
1135 return
1136}
1137
1138//UpdateGemPortForPktIn updates gemport for pkt in path to kvstore, path being intfid, onuid, portno
npujarec5762e2020-01-01 14:08:48 +05301139func (RsrcMgr *OpenOltResourceMgr) UpdateGemPortForPktIn(ctx context.Context, pktIn PacketInInfoKey, gemPort uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301140
1141 path := fmt.Sprintf(OnuPacketINPath, pktIn.IntfID, pktIn.OnuID, pktIn.LogicalPort)
1142 Value, err := json.Marshal(gemPort)
1143 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001144 logger.Error("Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301145 return
1146 }
npujarec5762e2020-01-01 14:08:48 +05301147 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001148 logger.Errorw("Failed to put to kvstore", log.Fields{"path": path, "value": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301149 return
1150 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001151 logger.Debugw("added gem packet in successfully", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301152
1153 return
1154}
1155
1156// GetGemPortFromOnuPktIn gets the gem port from onu pkt in path, path being intfid, onuid, portno
npujarec5762e2020-01-01 14:08:48 +05301157func (RsrcMgr *OpenOltResourceMgr) GetGemPortFromOnuPktIn(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) (uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301158
1159 var Val []byte
1160 var gemPort uint32
1161
1162 path := fmt.Sprintf(OnuPacketINPath, intfID, onuID, logicalPort)
1163
npujarec5762e2020-01-01 14:08:48 +05301164 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301165 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001166 logger.Errorw("Failed to get from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301167 return uint32(0), err
1168 } else if value == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001169 logger.Debugw("No pkt in gem found", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301170 return uint32(0), nil
1171 }
1172
1173 if Val, err = kvstore.ToByte(value.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001174 logger.Error("Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301175 return uint32(0), err
1176 }
1177 if err = json.Unmarshal(Val, &gemPort); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001178 logger.Error("Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301179 return uint32(0), err
1180 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001181 logger.Debugw("found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301182
1183 return gemPort, nil
1184}
1185
1186// DelGemPortPktIn deletes the gemport from the pkt in path
npujarec5762e2020-01-01 14:08:48 +05301187func (RsrcMgr *OpenOltResourceMgr) DelGemPortPktIn(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301188
1189 path := fmt.Sprintf(OnuPacketINPath, intfID, onuID, logicalPort)
npujarec5762e2020-01-01 14:08:48 +05301190 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001191 logger.Errorf("Falied to remove resource %s", path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301192 return err
1193 }
1194 return nil
1195}
1196
1197// DelOnuGemInfoForIntf deletes the onugem info from kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301198func (RsrcMgr *OpenOltResourceMgr) DelOnuGemInfoForIntf(ctx context.Context, intfID uint32) error {
1199 if err := RsrcMgr.ResourceMgrs[intfID].DelOnuGemInfoForIntf(ctx, intfID); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001200 logger.Errorw("failed to delete onu gem info for", log.Fields{"intfid": intfID})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301201 return err
1202 }
1203 return nil
1204}
1205
1206//GetNNIFromKVStore gets NNi intfids from kvstore. path being per device
npujarec5762e2020-01-01 14:08:48 +05301207func (RsrcMgr *OpenOltResourceMgr) GetNNIFromKVStore(ctx context.Context) ([]uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301208
1209 var nni []uint32
1210 var Val []byte
1211
1212 path := fmt.Sprintf(NnniIntfID)
npujarec5762e2020-01-01 14:08:48 +05301213 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301214 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001215 logger.Error("failed to get data from kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301216 return nil, err
1217 }
1218 if value != nil {
1219 if Val, err = kvstore.ToByte(value.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001220 logger.Error("Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301221 return nil, err
1222 }
1223 if err = json.Unmarshal(Val, &nni); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001224 logger.Error("Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301225 return nil, err
1226 }
1227 }
1228 return nni, err
1229}
1230
1231// AddNNIToKVStore adds Nni interfaces to kvstore, path being per device.
npujarec5762e2020-01-01 14:08:48 +05301232func (RsrcMgr *OpenOltResourceMgr) AddNNIToKVStore(ctx context.Context, nniIntf uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301233 var Value []byte
1234
npujarec5762e2020-01-01 14:08:48 +05301235 nni, err := RsrcMgr.GetNNIFromKVStore(ctx)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301236 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001237 logger.Error("failed to fetch nni interfaces from kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301238 return err
1239 }
1240
1241 path := fmt.Sprintf(NnniIntfID)
1242 nni = append(nni, nniIntf)
1243 Value, err = json.Marshal(nni)
1244 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001245 logger.Error("Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301246 }
npujarec5762e2020-01-01 14:08:48 +05301247 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001248 logger.Errorw("Failed to put to kvstore", log.Fields{"path": path, "value": Value})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301249 return err
1250 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001251 logger.Debugw("added nni to kv successfully", log.Fields{"path": path, "nni": nniIntf})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301252 return nil
1253}
1254
1255// DelNNiFromKVStore deletes nni interface list from kv store.
npujarec5762e2020-01-01 14:08:48 +05301256func (RsrcMgr *OpenOltResourceMgr) DelNNiFromKVStore(ctx context.Context) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301257
1258 path := fmt.Sprintf(NnniIntfID)
1259
npujarec5762e2020-01-01 14:08:48 +05301260 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001261 logger.Errorw("Failed to delete nni interfaces from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301262 return err
1263 }
1264 return nil
1265}
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301266
1267//UpdateFlowIDsForGem updates flow id per gemport
npujarec5762e2020-01-01 14:08:48 +05301268func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDsForGem(ctx context.Context, intf uint32, gem uint32, flowIDs []uint32) error {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301269 var val []byte
1270 path := fmt.Sprintf(FlowIDsForGem, intf)
1271
npujarec5762e2020-01-01 14:08:48 +05301272 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301273 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001274 logger.Error("Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301275 return err
1276 }
1277 if flowsForGem == nil {
1278 flowsForGem = make(map[uint32][]uint32)
1279 }
1280 flowsForGem[gem] = flowIDs
1281 val, err = json.Marshal(flowsForGem)
1282 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001283 logger.Error("Failed to marshal data", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301284 return err
1285 }
Girish Gowdrab77ded92020-04-08 11:45:05 -07001286
Girish Gowdra38d533d2020-03-30 20:38:51 -07001287 RsrcMgr.flowIDToGemInfoLock.Lock()
1288 defer RsrcMgr.flowIDToGemInfoLock.Unlock()
npujarec5762e2020-01-01 14:08:48 +05301289 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001290 logger.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301291 return err
1292 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001293 logger.Debugw("added flowid list for gem to kv successfully", log.Fields{"path": path, "flowidlist": flowsForGem[gem]})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301294 return nil
1295}
1296
1297//DeleteFlowIDsForGem deletes the flowID list entry per gem from kvstore.
npujarec5762e2020-01-01 14:08:48 +05301298func (RsrcMgr *OpenOltResourceMgr) DeleteFlowIDsForGem(ctx context.Context, intf uint32, gem uint32) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301299 path := fmt.Sprintf(FlowIDsForGem, intf)
1300 var val []byte
1301
npujarec5762e2020-01-01 14:08:48 +05301302 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301303 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001304 logger.Error("Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301305 return
1306 }
1307 if flowsForGem == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001308 logger.Error("No flowids found ", log.Fields{"intf": intf, "gemport": gem})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301309 return
1310 }
1311 // once we get the flows per gem map from kv , just delete the gem entry from the map
1312 delete(flowsForGem, gem)
1313 // once gem entry is deleted update the kv store.
1314 val, err = json.Marshal(flowsForGem)
1315 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001316 logger.Error("Failed to marshal data", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301317 return
1318 }
Girish Gowdra38d533d2020-03-30 20:38:51 -07001319
1320 RsrcMgr.flowIDToGemInfoLock.Lock()
1321 defer RsrcMgr.flowIDToGemInfoLock.Unlock()
npujarec5762e2020-01-01 14:08:48 +05301322 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001323 logger.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301324 return
1325 }
1326 return
1327}
1328
1329//GetFlowIDsGemMapForInterface gets flowids per gemport and interface
npujarec5762e2020-01-01 14:08:48 +05301330func (RsrcMgr *OpenOltResourceMgr) GetFlowIDsGemMapForInterface(ctx context.Context, intf uint32) (map[uint32][]uint32, error) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301331 path := fmt.Sprintf(FlowIDsForGem, intf)
1332 var flowsForGem map[uint32][]uint32
1333 var val []byte
Girish Gowdra38d533d2020-03-30 20:38:51 -07001334 RsrcMgr.flowIDToGemInfoLock.RLock()
npujarec5762e2020-01-01 14:08:48 +05301335 value, err := RsrcMgr.KVStore.Get(ctx, path)
Girish Gowdra38d533d2020-03-30 20:38:51 -07001336 RsrcMgr.flowIDToGemInfoLock.RUnlock()
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301337 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001338 logger.Error("failed to get data from kv store")
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301339 return nil, err
1340 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001341 if value != nil && value.Value != nil {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301342 if val, err = kvstore.ToByte(value.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001343 logger.Error("Failed to convert to byte array ", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301344 return nil, err
1345 }
1346 if err = json.Unmarshal(val, &flowsForGem); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001347 logger.Error("Failed to unmarshall", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301348 return nil, err
1349 }
1350 }
1351 return flowsForGem, nil
1352}
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301353
1354//DeleteIntfIDGempMapPath deletes the intf id path used to store flow ids per gem to kvstore.
npujarec5762e2020-01-01 14:08:48 +05301355func (RsrcMgr *OpenOltResourceMgr) DeleteIntfIDGempMapPath(ctx context.Context, intf uint32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301356 path := fmt.Sprintf(FlowIDsForGem, intf)
Girish Gowdra38d533d2020-03-30 20:38:51 -07001357 RsrcMgr.flowIDToGemInfoLock.Lock()
1358 defer RsrcMgr.flowIDToGemInfoLock.Unlock()
npujarec5762e2020-01-01 14:08:48 +05301359 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001360 logger.Errorw("Failed to delete nni interfaces from kv store", log.Fields{"path": path})
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301361 return
1362 }
1363 return
1364}
1365
1366// RemoveResourceMap Clear resource map associated with (intfid, onuid, uniid) tuple.
npujarec5762e2020-01-01 14:08:48 +05301367func (RsrcMgr *OpenOltResourceMgr) RemoveResourceMap(ctx context.Context, intfID uint32, onuID int32, uniID int32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301368 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +05301369 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301370}
Esin Karamanccb714b2019-11-29 15:02:06 +00001371
1372//GetMcastQueuePerInterfaceMap gets multicast queue info per pon interface
npujarec5762e2020-01-01 14:08:48 +05301373func (RsrcMgr *OpenOltResourceMgr) GetMcastQueuePerInterfaceMap(ctx context.Context) (map[uint32][]uint32, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001374 path := fmt.Sprintf(McastQueuesForIntf)
1375 var mcastQueueToIntfMap map[uint32][]uint32
1376 var val []byte
1377
npujarec5762e2020-01-01 14:08:48 +05301378 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001379 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001380 logger.Error("failed to get data from kv store")
Esin Karamanccb714b2019-11-29 15:02:06 +00001381 return nil, err
1382 }
1383 if kvPair != nil && kvPair.Value != nil {
1384 if val, err = kvstore.ToByte(kvPair.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001385 logger.Error("Failed to convert to byte array ", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001386 return nil, err
1387 }
1388 if err = json.Unmarshal(val, &mcastQueueToIntfMap); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001389 logger.Error("Failed to unmarshall ", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001390 return nil, err
1391 }
1392 }
1393 return mcastQueueToIntfMap, nil
1394}
1395
1396//AddMcastQueueForIntf adds multicast queue for pon interface
npujarec5762e2020-01-01 14:08:48 +05301397func (RsrcMgr *OpenOltResourceMgr) AddMcastQueueForIntf(ctx context.Context, intf uint32, gem uint32, servicePriority uint32) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001398 var val []byte
1399 path := fmt.Sprintf(McastQueuesForIntf)
1400
npujarec5762e2020-01-01 14:08:48 +05301401 mcastQueues, err := RsrcMgr.GetMcastQueuePerInterfaceMap(ctx)
Esin Karamanccb714b2019-11-29 15:02:06 +00001402 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001403 logger.Errorw("Failed to get multicast queue info for interface", log.Fields{"error": err, "intf": intf})
Esin Karamanccb714b2019-11-29 15:02:06 +00001404 return err
1405 }
1406 if mcastQueues == nil {
1407 mcastQueues = make(map[uint32][]uint32)
1408 }
1409 mcastQueues[intf] = []uint32{gem, servicePriority}
1410 if val, err = json.Marshal(mcastQueues); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001411 logger.Errorw("Failed to marshal data", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001412 return err
1413 }
npujarec5762e2020-01-01 14:08:48 +05301414 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001415 logger.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Esin Karamanccb714b2019-11-29 15:02:06 +00001416 return err
1417 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001418 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 +00001419 return nil
1420}
1421
1422//AddFlowGroupToKVStore adds flow group into KV store
npujarec5762e2020-01-01 14:08:48 +05301423func (RsrcMgr *OpenOltResourceMgr) AddFlowGroupToKVStore(ctx context.Context, groupEntry *ofp.OfpGroupEntry, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001424 var Value []byte
1425 var err error
1426 var path string
1427 if cached {
1428 path = fmt.Sprintf(FlowGroupCached, groupEntry.Desc.GroupId)
1429 } else {
1430 path = fmt.Sprintf(FlowGroup, groupEntry.Desc.GroupId)
1431 }
1432 //build group info object
1433 var outPorts []uint32
1434 for _, ofBucket := range groupEntry.Desc.Buckets {
1435 for _, ofAction := range ofBucket.Actions {
1436 if ofAction.Type == ofp.OfpActionType_OFPAT_OUTPUT {
1437 outPorts = append(outPorts, ofAction.GetOutput().Port)
1438 }
1439 }
1440 }
1441 groupInfo := GroupInfo{
1442 GroupID: groupEntry.Desc.GroupId,
1443 OutPorts: outPorts,
1444 }
1445
1446 Value, err = json.Marshal(groupInfo)
1447
1448 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001449 logger.Error("failed to Marshal flow group object")
Esin Karamanccb714b2019-11-29 15:02:06 +00001450 return err
1451 }
1452
npujarec5762e2020-01-01 14:08:48 +05301453 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001454 logger.Errorf("Failed to update resource %s", path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001455 return err
1456 }
1457 return nil
1458}
1459
1460//RemoveFlowGroupFromKVStore removes flow group from KV store
npujarec5762e2020-01-01 14:08:48 +05301461func (RsrcMgr *OpenOltResourceMgr) RemoveFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) bool {
Esin Karamanccb714b2019-11-29 15:02:06 +00001462 var path string
1463 if cached {
1464 path = fmt.Sprintf(FlowGroupCached, groupID)
1465 } else {
1466 path = fmt.Sprintf(FlowGroup, groupID)
1467 }
npujarec5762e2020-01-01 14:08:48 +05301468 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001469 logger.Errorf("Failed to remove resource %s due to %s", path, err)
Esin Karamanccb714b2019-11-29 15:02:06 +00001470 return false
1471 }
1472 return true
1473}
1474
1475//GetFlowGroupFromKVStore fetches flow group from the KV store. Returns (false, {} error) if any problem occurs during
1476//fetching the data. Returns (true, groupInfo, nil) if the group is fetched successfully.
1477// Returns (false, {}, nil) if the group does not exists in the KV store.
npujarec5762e2020-01-01 14:08:48 +05301478func (RsrcMgr *OpenOltResourceMgr) GetFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) (bool, GroupInfo, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001479 var groupInfo GroupInfo
1480 var path string
1481 if cached {
1482 path = fmt.Sprintf(FlowGroupCached, groupID)
1483 } else {
1484 path = fmt.Sprintf(FlowGroup, groupID)
1485 }
npujarec5762e2020-01-01 14:08:48 +05301486 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001487 if err != nil {
1488 return false, groupInfo, err
1489 }
1490 if kvPair != nil && kvPair.Value != nil {
1491 Val, err := kvstore.ToByte(kvPair.Value)
1492 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001493 logger.Errorw("Failed to convert flow group into byte array", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001494 return false, groupInfo, err
1495 }
1496 if err = json.Unmarshal(Val, &groupInfo); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001497 logger.Errorw("Failed to unmarshal", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001498 return false, groupInfo, err
1499 }
1500 return true, groupInfo, nil
1501 }
1502 return false, groupInfo, nil
1503}