blob: a6747cdde2806543e02ff451f8fee5acebc240d6 [file] [log] [blame]
Abhilash S.L7f17e402019-03-15 17:40:41 +05301/*
2 * Copyright 2019-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070017//Package resourcemanager provides the utility for managing resources
manikkaraj kbf256be2019-03-25 00:13:48 +053018package resourcemanager
Abhilash S.L7f17e402019-03-15 17:40:41 +053019
20import (
npujarec5762e2020-01-01 14:08:48 +053021 "context"
Girish Gowdru0c588b22019-04-23 23:24:56 -040022 "encoding/json"
23 "errors"
24 "fmt"
25 "strconv"
26 "strings"
Girish Gowdra3f182d82020-03-30 20:38:51 -070027 "sync"
Abhilash S.L7f17e402019-03-15 17:40:41 +053028
Esin Karamanccb714b2019-11-29 15:02:06 +000029 "github.com/opencord/voltha-lib-go/v3/pkg/db"
30 "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
31 "github.com/opencord/voltha-lib-go/v3/pkg/log"
32 ponrmgr "github.com/opencord/voltha-lib-go/v3/pkg/ponresourcemanager"
33 ofp "github.com/opencord/voltha-protos/v3/go/openflow_13"
34 "github.com/opencord/voltha-protos/v3/go/openolt"
Abhilash S.L7f17e402019-03-15 17:40:41 +053035)
36
salmansiddiqui7ac62132019-08-22 03:58:50 +000037const (
38 // KvstoreTimeout specifies the time out for KV Store Connection
39 KvstoreTimeout = 5
40 // BasePathKvStore - service/voltha/openolt/<device_id>
41 BasePathKvStore = "service/voltha/openolt/{%s}"
Gamze Abakafee36392019-10-03 11:17:24 +000042 // TpIDPathSuffix - <(pon_id, onu_id, uni_id)>/tp_id
43 TpIDPathSuffix = "{%d,%d,%d}/tp_id"
44 //MeterIDPathSuffix - <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
45 MeterIDPathSuffix = "{%d,%d,%d}/{%d}/meter_id/{%s}"
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053046 //NnniIntfID - nniintfids
47 NnniIntfID = "nniintfids"
48 // OnuPacketINPath path on the kvstore to store packetin gemport,which will be used for packetin, pcketout
49 //format: onu_packetin/<intfid>,<onuid>,<logicalport>
50 OnuPacketINPath = "onu_packetin/{%d,%d,%d}"
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +053051 //FlowIDsForGem flowids_per_gem/<intfid>
52 FlowIDsForGem = "flowids_per_gem/{%d}"
Esin Karamanccb714b2019-11-29 15:02:06 +000053 //McastQueuesForIntf multicast queues for pon interfaces
54 McastQueuesForIntf = "mcast_qs_for_int"
55 //FlowGroup flow_groups/<flow_group_id>
56 // A group is stored under this path on the KV store after it has been installed to the device.
57 // It should also be deleted after it has been removed from the device accordingly.
58 FlowGroup = "flow_groups/{%d}"
59 //FlowGroupCached flow_groups_cached/<flow_group_id>
60 // When a group add request received, we create the group without setting any members to it since we cannot
61 // set any members to a group until it is associated with a multicast flow. It is a BAL limitation.
62 // When the related multicast flow has been created we perform set members operation for the group.
63 // That is why we need to keep the members of a group until the multicast flow creation request comes.
64 // We preserve the groups under "FlowGroupsCached" directory in the KV store temporarily. Having set members,
65 // we remove the group from the cached group store.
66 FlowGroupCached = "flow_groups_cached/{%d}"
salmansiddiqui7ac62132019-08-22 03:58:50 +000067)
Abhilash S.L7f17e402019-03-15 17:40:41 +053068
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070069// FlowInfo holds the flow information
Abhilash S.L8ee90712019-04-29 16:24:22 +053070type FlowInfo struct {
71 Flow *openolt.Flow
72 FlowStoreCookie uint64
73 FlowCategory string
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053074 LogicalFlowID uint64
75}
76
77// OnuGemInfo holds onu information along with gem port list and uni port list
78type OnuGemInfo struct {
79 OnuID uint32
80 SerialNumber string
81 IntfID uint32
82 GemPorts []uint32
83 UniPorts []uint32
84}
85
86// PacketInInfoKey is the key for packet in gemport
87type PacketInInfoKey struct {
88 IntfID uint32
89 OnuID uint32
90 LogicalPort uint32
Abhilash S.L8ee90712019-04-29 16:24:22 +053091}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070092
Esin Karamanccb714b2019-11-29 15:02:06 +000093// GroupInfo holds group information
94type GroupInfo struct {
95 GroupID uint32
96 OutPorts []uint32
97}
98
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070099// OpenOltResourceMgr holds resource related information as provided below for each field
Abhilash S.L7f17e402019-03-15 17:40:41 +0530100type OpenOltResourceMgr struct {
sbarbaria8910ba2019-11-05 10:12:23 -0500101 DeviceID string // OLT device id
102 HostAndPort string // Host and port of the kv store to connect to
103 Args string // args
104 KVStore *db.Backend // backend kv store connection handle
Girish Gowdru0c588b22019-04-23 23:24:56 -0400105 DeviceType string
106 Host string // Host ip of the kv store
107 Port int // port of the kv store
108 DevInfo *openolt.DeviceInfo // device information
109 // array of pon resource managers per interface technology
110 ResourceMgrs map[uint32]*ponrmgr.PONResourceManager
Girish Gowdra3f182d82020-03-30 20:38:51 -0700111
112 // This protects concurrent gemport_id allocate/delete calls on a per PON port basis
113 GemPortIDMgmtLock []sync.RWMutex
114 // This protects concurrent alloc_id allocate/delete calls on a per PON port basis
115 AllocIDMgmtLock []sync.RWMutex
116 // This protects concurrent onu_id allocate/delete calls on a per PON port basis
117 OnuIDMgmtLock []sync.RWMutex
118 // This protects concurrent flow_id allocate/delete calls. We do not need this on a
119 // per PON port basis as flow IDs are unique across the OLT.
120 FlowIDMgmtLock sync.RWMutex
121
122 // This protects concurrent access to flowids_per_gem info stored on KV store
123 flowIDToGemInfoLock sync.RWMutex
Abhilash S.L7f17e402019-03-15 17:40:41 +0530124}
125
Manikkaraj kb1d51442019-07-23 10:41:02 -0400126func newKVClient(storeType string, address string, timeout uint32) (kvstore.Client, error) {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000127 logger.Infow("kv-store-type", log.Fields{"store": storeType})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400128 switch storeType {
129 case "consul":
130 return kvstore.NewConsulClient(address, int(timeout))
131 case "etcd":
132 return kvstore.NewEtcdClient(address, int(timeout))
133 }
134 return nil, errors.New("unsupported-kv-store")
Abhilash S.L7f17e402019-03-15 17:40:41 +0530135}
136
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700137// SetKVClient sets the KV client and return a kv backend
sbarbaria8910ba2019-11-05 10:12:23 -0500138func SetKVClient(backend string, Host string, Port int, DeviceID string) *db.Backend {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400139 addr := Host + ":" + strconv.Itoa(Port)
140 // TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
141 // issue between kv store and backend , core is not calling NewBackend directly
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700142 kvClient, err := newKVClient(backend, addr, KvstoreTimeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400143 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000144 logger.Fatalw("Failed to init KV client\n", log.Fields{"err": err})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400145 return nil
146 }
sbarbaria8910ba2019-11-05 10:12:23 -0500147 kvbackend := &db.Backend{
Girish Gowdru0c588b22019-04-23 23:24:56 -0400148 Client: kvClient,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700149 StoreType: backend,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400150 Host: Host,
151 Port: Port,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700152 Timeout: KvstoreTimeout,
153 PathPrefix: fmt.Sprintf(BasePathKvStore, DeviceID)}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530154
Girish Gowdru0c588b22019-04-23 23:24:56 -0400155 return kvbackend
Abhilash S.L7f17e402019-03-15 17:40:41 +0530156}
157
Gamze Abakafee36392019-10-03 11:17:24 +0000158// NewResourceMgr init a New resource manager instance which in turn instantiates pon resource manager
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700159// instances according to technology. Initializes the default resource ranges for all
160// the resources.
npujarec5762e2020-01-01 14:08:48 +0530161func NewResourceMgr(ctx context.Context, deviceID string, KVStoreHostPort string, kvStoreType string, deviceType string, devInfo *openolt.DeviceInfo) *OpenOltResourceMgr {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400162 var ResourceMgr OpenOltResourceMgr
Girish Kumar2ad402b2020-03-20 19:45:12 +0000163 logger.Debugf("Init new resource manager , host_port: %s, deviceid: %s", KVStoreHostPort, deviceID)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530164 ResourceMgr.HostAndPort = KVStoreHostPort
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700165 ResourceMgr.DeviceType = deviceType
166 ResourceMgr.DevInfo = devInfo
167 IPPort := strings.Split(KVStoreHostPort, ":")
168 ResourceMgr.Host = IPPort[0]
169 ResourceMgr.Port, _ = strconv.Atoi(IPPort[1])
Girish Gowdra3f182d82020-03-30 20:38:51 -0700170 NumPONPorts := devInfo.GetPonPorts()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530171
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700172 Backend := kvStoreType
Girish Gowdru0c588b22019-04-23 23:24:56 -0400173 ResourceMgr.KVStore = SetKVClient(Backend, ResourceMgr.Host,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700174 ResourceMgr.Port, deviceID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400175 if ResourceMgr.KVStore == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000176 logger.Error("Failed to setup KV store")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400177 }
178 Ranges := make(map[string]*openolt.DeviceInfo_DeviceResourceRanges)
179 RsrcMgrsByTech := make(map[string]*ponrmgr.PONResourceManager)
180 ResourceMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530181
Girish Gowdra3f182d82020-03-30 20:38:51 -0700182 ResourceMgr.AllocIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
183 ResourceMgr.GemPortIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
184 ResourceMgr.OnuIDMgmtLock = make([]sync.RWMutex, NumPONPorts)
185
Girish Gowdru0c588b22019-04-23 23:24:56 -0400186 // TODO self.args = registry('main').get_args()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530187
Girish Gowdru0c588b22019-04-23 23:24:56 -0400188 /*
189 If a legacy driver returns protobuf without any ranges,s synthesize one from
Gamze Abakafee36392019-10-03 11:17:24 +0000190 the legacy global per-device information. This, in theory, is temporary until
Girish Gowdru0c588b22019-04-23 23:24:56 -0400191 the legacy drivers are upgrade to support pool ranges.
192 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700193 if devInfo.Ranges == nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400194 var ranges openolt.DeviceInfo_DeviceResourceRanges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700195 ranges.Technology = devInfo.GetTechnology()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530196
Girish Gowdru0c588b22019-04-23 23:24:56 -0400197 var index uint32
198 for index = 0; index < NumPONPorts; index++ {
199 ranges.IntfIds = append(ranges.IntfIds, index)
200 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530201
Abhilash S.L8ee90712019-04-29 16:24:22 +0530202 var Pool openolt.DeviceInfo_DeviceResourceRanges_Pool
Girish Gowdru0c588b22019-04-23 23:24:56 -0400203 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700204 Pool.Start = devInfo.OnuIdStart
205 Pool.End = devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400206 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
cbabuabf02352019-10-15 13:14:56 +0200207 onuPool := Pool
208 ranges.Pools = append(ranges.Pools, &onuPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530209
Girish Gowdru0c588b22019-04-23 23:24:56 -0400210 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700211 Pool.Start = devInfo.AllocIdStart
212 Pool.End = devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400213 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200214 allocPool := Pool
215 ranges.Pools = append(ranges.Pools, &allocPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530216
Girish Gowdru0c588b22019-04-23 23:24:56 -0400217 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700218 Pool.Start = devInfo.GemportIdStart
219 Pool.End = devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400220 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200221 gemPool := Pool
222 ranges.Pools = append(ranges.Pools, &gemPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530223
Girish Gowdru0c588b22019-04-23 23:24:56 -0400224 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700225 Pool.Start = devInfo.FlowIdStart
226 Pool.End = devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400227 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
Abhilash S.L8ee90712019-04-29 16:24:22 +0530228 ranges.Pools = append(ranges.Pools, &Pool)
229 // Add to device info
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700230 devInfo.Ranges = append(devInfo.Ranges, &ranges)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400231 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530232
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700233 // Create a separate Resource Manager instance for each range. This assumes that
Girish Gowdru0c588b22019-04-23 23:24:56 -0400234 // each technology is represented by only a single range
235 var GlobalPONRsrcMgr *ponrmgr.PONResourceManager
236 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700237 for _, TechRange := range devInfo.Ranges {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400238 technology := TechRange.Technology
Girish Kumar2ad402b2020-03-20 19:45:12 +0000239 logger.Debugf("Device info technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400240 Ranges[technology] = TechRange
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700241 RsrcMgrsByTech[technology], err = ponrmgr.NewPONResourceManager(technology, deviceType, deviceID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400242 Backend, ResourceMgr.Host, ResourceMgr.Port)
243 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000244 logger.Errorf("Failed to create pon resource manager instance for technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400245 return nil
246 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700247 // resource_mgrs_by_tech[technology] = resource_mgr
Girish Gowdru0c588b22019-04-23 23:24:56 -0400248 if GlobalPONRsrcMgr == nil {
249 GlobalPONRsrcMgr = RsrcMgrsByTech[technology]
250 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700251 for _, IntfID := range TechRange.IntfIds {
252 ResourceMgr.ResourceMgrs[uint32(IntfID)] = RsrcMgrsByTech[technology]
Girish Gowdru0c588b22019-04-23 23:24:56 -0400253 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700254 // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
npujarec5762e2020-01-01 14:08:48 +0530255 InitializeDeviceResourceRangeAndPool(ctx, RsrcMgrsByTech[technology], GlobalPONRsrcMgr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700256 TechRange, devInfo)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400257 }
258 // After we have initialized resource ranges, initialize the
259 // resource pools accordingly.
260 for _, PONRMgr := range RsrcMgrsByTech {
npujarec5762e2020-01-01 14:08:48 +0530261 _ = PONRMgr.InitDeviceResourcePool(ctx)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400262 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000263 logger.Info("Initialization of resource manager success!")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400264 return &ResourceMgr
Abhilash S.L7f17e402019-03-15 17:40:41 +0530265}
266
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700267// InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply
268// device specific information. If KV doesn't exist
269// or is broader than the device, the device's information will
270// dictate the range limits
npujarec5762e2020-01-01 14:08:48 +0530271func InitializeDeviceResourceRangeAndPool(ctx context.Context, ponRMgr *ponrmgr.PONResourceManager, globalPONRMgr *ponrmgr.PONResourceManager,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700272 techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530273
Girish Gowdru0c588b22019-04-23 23:24:56 -0400274 // init the resource range pool according to the sharing type
Abhilash S.L7f17e402019-03-15 17:40:41 +0530275
Girish Kumar2ad402b2020-03-20 19:45:12 +0000276 logger.Debugf("Resource range pool init for technology %s", ponRMgr.Technology)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700277 // first load from KV profiles
npujarec5762e2020-01-01 14:08:48 +0530278 status := ponRMgr.InitResourceRangesFromKVStore(ctx)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700279 if !status {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000280 logger.Debugf("Failed to load resource ranges from KV store for tech %s", ponRMgr.Technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400281 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530282
Girish Gowdru0c588b22019-04-23 23:24:56 -0400283 /*
284 Then apply device specific information. If KV doesn't exist
Gamze Abakafee36392019-10-03 11:17:24 +0000285 or is broader than the device, the device's information will
Girish Gowdru0c588b22019-04-23 23:24:56 -0400286 dictate the range limits
287 */
Girish Kumar2ad402b2020-03-20 19:45:12 +0000288 logger.Debugw("Using device info to init pon resource ranges", log.Fields{"Tech": ponRMgr.Technology})
Abhilash S.L7f17e402019-03-15 17:40:41 +0530289
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700290 ONUIDStart := devInfo.OnuIdStart
291 ONUIDEnd := devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400292 ONUIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
293 ONUIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700294 AllocIDStart := devInfo.AllocIdStart
295 AllocIDEnd := devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400296 AllocIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
297 AllocIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700298 GEMPortIDStart := devInfo.GemportIdStart
299 GEMPortIDEnd := devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400300 GEMPortIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
301 GEMPortIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700302 FlowIDStart := devInfo.FlowIdStart
303 FlowIDEnd := devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400304 FlowIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
305 FlowIDSharedPoolID := uint32(0)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530306
Girish Gowdru0c588b22019-04-23 23:24:56 -0400307 var FirstIntfPoolID uint32
308 var SharedPoolID uint32
Abhilash S.L7f17e402019-03-15 17:40:41 +0530309
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400310 /*
311 * As a zero check is made against SharedPoolID to check whether the resources are shared across all intfs
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700312 * if resources are shared across interfaces then SharedPoolID is given a positive number.
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400313 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700314 for _, FirstIntfPoolID = range techRange.IntfIds {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400315 // skip the intf id 0
316 if FirstIntfPoolID == 0 {
317 continue
318 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400319 break
320 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530321
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700322 for _, RangePool := range techRange.Pools {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400323 if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400324 SharedPoolID = FirstIntfPoolID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400325 } else if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_SAME_TECH {
326 SharedPoolID = FirstIntfPoolID
327 } else {
328 SharedPoolID = 0
329 }
330 if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID {
331 ONUIDStart = RangePool.Start
332 ONUIDEnd = RangePool.End
333 ONUIDShared = RangePool.Sharing
334 ONUIDSharedPoolID = SharedPoolID
335 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID {
336 AllocIDStart = RangePool.Start
337 AllocIDEnd = RangePool.End
338 AllocIDShared = RangePool.Sharing
339 AllocIDSharedPoolID = SharedPoolID
340 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID {
341 GEMPortIDStart = RangePool.Start
342 GEMPortIDEnd = RangePool.End
343 GEMPortIDShared = RangePool.Sharing
344 GEMPortIDSharedPoolID = SharedPoolID
345 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID {
346 FlowIDStart = RangePool.Start
347 FlowIDEnd = RangePool.End
348 FlowIDShared = RangePool.Sharing
349 FlowIDSharedPoolID = SharedPoolID
350 }
351 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530352
Girish Kumar2ad402b2020-03-20 19:45:12 +0000353 logger.Debugw("Device info init", log.Fields{"technology": techRange.Technology,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400354 "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd, "onu_id_shared_pool_id": ONUIDSharedPoolID,
355 "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd,
356 "alloc_id_shared_pool_id": AllocIDSharedPoolID,
357 "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd,
358 "gemport_id_shared_pool_id": GEMPortIDSharedPoolID,
359 "flow_id_start": FlowIDStart,
360 "flow_id_end_idx": FlowIDEnd,
361 "flow_id_shared_pool_id": FlowIDSharedPoolID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700362 "intf_ids": techRange.IntfIds,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400363 "uni_id_start": 0,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700364 "uni_id_end_idx": 1, /*MaxUNIIDperONU()*/
365 })
Abhilash S.L7f17e402019-03-15 17:40:41 +0530366
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700367 ponRMgr.InitDefaultPONResourceRanges(ONUIDStart, ONUIDEnd, ONUIDSharedPoolID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400368 AllocIDStart, AllocIDEnd, AllocIDSharedPoolID,
369 GEMPortIDStart, GEMPortIDEnd, GEMPortIDSharedPoolID,
370 FlowIDStart, FlowIDEnd, FlowIDSharedPoolID, 0, 1,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700371 devInfo.PonPorts, techRange.IntfIds)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530372
Girish Gowdru0c588b22019-04-23 23:24:56 -0400373 // For global sharing, make sure to refresh both local and global resource manager instances' range
Abhilash S.L7f17e402019-03-15 17:40:41 +0530374
Girish Gowdru0c588b22019-04-23 23:24:56 -0400375 if ONUIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700376 globalPONRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400377 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700378 ponRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
379 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400380 }
381 if AllocIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700382 globalPONRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400383 "", 0, nil)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530384
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700385 ponRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
386 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400387 }
388 if GEMPortIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700389 globalPONRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400390 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700391 ponRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
392 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400393 }
394 if FlowIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700395 globalPONRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400396 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700397 ponRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
398 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400399 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530400
Girish Gowdru0c588b22019-04-23 23:24:56 -0400401 // Make sure loaded range fits the platform bit encoding ranges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700402 ponRMgr.UpdateRanges(ponrmgr.UNI_ID_START_IDX, 0, ponrmgr.UNI_ID_END_IDX /* TODO =OpenOltPlatform.MAX_UNIS_PER_ONU-1*/, 1, "", 0, nil)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530403}
404
Devmalya Paul495b94a2019-08-27 19:42:00 -0400405// Delete clears used resources for the particular olt device being deleted
npujarec5762e2020-01-01 14:08:48 +0530406func (RsrcMgr *OpenOltResourceMgr) Delete(ctx context.Context) error {
Devmalya Paul495b94a2019-08-27 19:42:00 -0400407 /* TODO
408 def __del__(self):
409 self.log.info("clearing-device-resource-pool")
410 for key, resource_mgr in self.resource_mgrs.iteritems():
411 resource_mgr.clear_device_resource_pool()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530412
Devmalya Paul495b94a2019-08-27 19:42:00 -0400413 def assert_pon_id_limit(self, pon_intf_id):
414 assert pon_intf_id in self.resource_mgrs
Abhilash S.L7f17e402019-03-15 17:40:41 +0530415
Devmalya Paul495b94a2019-08-27 19:42:00 -0400416 def assert_onu_id_limit(self, pon_intf_id, onu_id):
417 self.assert_pon_id_limit(pon_intf_id)
418 self.resource_mgrs[pon_intf_id].assert_resource_limits(onu_id, PONResourceManager.ONU_ID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530419
Devmalya Paul495b94a2019-08-27 19:42:00 -0400420 @property
421 def max_uni_id_per_onu(self):
422 return 0 #OpenOltPlatform.MAX_UNIS_PER_ONU-1, zero-based indexing Uncomment or override to make default multi-uni
Abhilash S.L7f17e402019-03-15 17:40:41 +0530423
Devmalya Paul495b94a2019-08-27 19:42:00 -0400424 def assert_uni_id_limit(self, pon_intf_id, onu_id, uni_id):
425 self.assert_onu_id_limit(pon_intf_id, onu_id)
426 self.resource_mgrs[pon_intf_id].assert_resource_limits(uni_id, PONResourceManager.UNI_ID)
427 */
428 for _, rsrcMgr := range RsrcMgr.ResourceMgrs {
npujarec5762e2020-01-01 14:08:48 +0530429 if err := rsrcMgr.ClearDeviceResourcePool(ctx); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000430 logger.Debug("Failed to clear device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400431 return err
432 }
433 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000434 logger.Debug("Cleared device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400435 return nil
436}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530437
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700438// GetONUID returns the available OnuID for the given pon-port
npujarec5762e2020-01-01 14:08:48 +0530439func (RsrcMgr *OpenOltResourceMgr) GetONUID(ctx context.Context, ponIntfID uint32) (uint32, error) {
salmansiddiqui352a45c2019-08-19 10:15:36 +0000440 // Check if Pon Interface ID is present in Resource-manager-map
441 if _, ok := RsrcMgr.ResourceMgrs[ponIntfID]; !ok {
442 err := errors.New("invalid-pon-interface-" + strconv.Itoa(int(ponIntfID)))
443 return 0, err
444 }
Girish Gowdra3f182d82020-03-30 20:38:51 -0700445 RsrcMgr.OnuIDMgmtLock[ponIntfID].Lock()
Girish Gowdru0c588b22019-04-23 23:24:56 -0400446 // Get ONU id for a provided pon interface ID.
npujarec5762e2020-01-01 14:08:48 +0530447 ONUID, err := RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ctx, ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400448 ponrmgr.ONU_ID, 1)
Girish Gowdra3f182d82020-03-30 20:38:51 -0700449 RsrcMgr.OnuIDMgmtLock[ponIntfID].Unlock()
Girish Gowdru0c588b22019-04-23 23:24:56 -0400450 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000451 logger.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700452 ponIntfID, ponrmgr.ONU_ID)
cbabuabf02352019-10-15 13:14:56 +0200453 return 0, err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400454 }
455 if ONUID != nil {
npujarec5762e2020-01-01 14:08:48 +0530456 RsrcMgr.ResourceMgrs[ponIntfID].InitResourceMap(ctx, fmt.Sprintf("%d,%d", ponIntfID, ONUID[0]))
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700457 return ONUID[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400458 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530459
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700460 return 0, err // return OnuID 0 on error
Abhilash S.L7f17e402019-03-15 17:40:41 +0530461}
462
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700463// GetFlowIDInfo returns the slice of flow info of the given pon-port
464// Note: For flows which trap from the NNI and not really associated with any particular
465// 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 +0530466func (RsrcMgr *OpenOltResourceMgr) GetFlowIDInfo(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32, flowID uint32) *[]FlowInfo {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530467 var flows []FlowInfo
468
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700469 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530470 if err := RsrcMgr.ResourceMgrs[ponIntfID].GetFlowIDInfo(ctx, FlowPath, flowID, &flows); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000471 logger.Errorw("Error while getting flows from KV store", log.Fields{"flowId": flowID})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530472 return nil
473 }
474 if len(flows) == 0 {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000475 logger.Debugw("No flowInfo found in KV store", log.Fields{"flowPath": FlowPath})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530476 return nil
477 }
478 return &flows
479}
480
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700481// GetCurrentFlowIDsForOnu fetches flow ID from the resource manager
482// Note: For flows which trap from the NNI and not really associated with any particular
483// 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 +0530484func (RsrcMgr *OpenOltResourceMgr) GetCurrentFlowIDsForOnu(ctx context.Context, PONIntfID uint32, ONUID int32, UNIID int32) []uint32 {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700485
Abhilash S.L8ee90712019-04-29 16:24:22 +0530486 FlowPath := fmt.Sprintf("%d,%d,%d", PONIntfID, ONUID, UNIID)
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700487 if mgrs, exist := RsrcMgr.ResourceMgrs[PONIntfID]; exist {
npujarec5762e2020-01-01 14:08:48 +0530488 return mgrs.GetCurrentFlowIDsForOnu(ctx, FlowPath)
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700489 }
490 return nil
Abhilash S.L8ee90712019-04-29 16:24:22 +0530491}
492
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700493// UpdateFlowIDInfo updates flow info for the given pon interface, onu id, and uni id
494// Note: For flows which trap from the NNI and not really associated with any particular
495// 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 +0530496func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDInfo(ctx context.Context, ponIntfID int32, onuID int32, uniID int32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700497 flowID uint32, flowData *[]FlowInfo) error {
498 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530499 return RsrcMgr.ResourceMgrs[uint32(ponIntfID)].UpdateFlowIDInfoForOnu(ctx, FlowPath, flowID, *flowData)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530500}
501
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700502// GetFlowID return flow ID for a given pon interface id, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530503func (RsrcMgr *OpenOltResourceMgr) GetFlowID(ctx context.Context, ponIntfID uint32, ONUID int32, uniID int32,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400504 gemportID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700505 flowStoreCookie uint64,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400506 flowCategory string, vlanPcp ...uint32) (uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530507
Girish Gowdru0c588b22019-04-23 23:24:56 -0400508 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700509 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, ONUID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530510 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(ctx, FlowPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400511 if FlowIDs != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000512 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 -0700513 for _, flowID := range FlowIDs {
npujarec5762e2020-01-01 14:08:48 +0530514 FlowInfo := RsrcMgr.GetFlowIDInfo(ctx, ponIntfID, int32(ONUID), int32(uniID), uint32(flowID))
salmansiddiqui7ac62132019-08-22 03:58:50 +0000515 er := getFlowIDFromFlowInfo(FlowInfo, flowID, gemportID, flowStoreCookie, flowCategory, vlanPcp...)
516 if er == nil {
517 return flowID, er
Abhilash S.L8ee90712019-04-29 16:24:22 +0530518 }
519 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400520 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000521 logger.Debug("No matching flows with flow cookie or flow category, allocating new flowid")
Girish Gowdra3f182d82020-03-30 20:38:51 -0700522 RsrcMgr.FlowIDMgmtLock.Lock()
npujarec5762e2020-01-01 14:08:48 +0530523 FlowIDs, err = RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ctx, ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400524 ponrmgr.FLOW_ID, 1)
Girish Gowdra3f182d82020-03-30 20:38:51 -0700525 RsrcMgr.FlowIDMgmtLock.Unlock()
Girish Gowdru0c588b22019-04-23 23:24:56 -0400526 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000527 logger.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700528 ponIntfID, ponrmgr.FLOW_ID)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400529 return uint32(0), err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400530 }
531 if FlowIDs != nil {
npujarec5762e2020-01-01 14:08:48 +0530532 _ = RsrcMgr.ResourceMgrs[ponIntfID].UpdateFlowIDForOnu(ctx, FlowPath, FlowIDs[0], true)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700533 return FlowIDs[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400534 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530535
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700536 return 0, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530537}
538
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700539// GetAllocID return the first Alloc ID for a given pon interface id and onu id and then update the resource map on
540// the KV store with the list of alloc_ids allocated for the pon_intf_onu_id tuple
541// 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 +0530542func (RsrcMgr *OpenOltResourceMgr) GetAllocID(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530543
Girish Gowdru0c588b22019-04-23 23:24:56 -0400544 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700545 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530546 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400547 if AllocID != nil {
548 // Since we support only one alloc_id for the ONU at the moment,
549 // return the first alloc_id in the list, if available, for that
550 // ONU.
Girish Kumar2ad402b2020-03-20 19:45:12 +0000551 logger.Debugw("Retrieved alloc ID from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400552 return AllocID[0]
553 }
Girish Gowdra3f182d82020-03-30 20:38:51 -0700554 RsrcMgr.AllocIDMgmtLock[intfID].Lock()
npujarec5762e2020-01-01 14:08:48 +0530555 AllocID, err = RsrcMgr.ResourceMgrs[intfID].GetResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400556 ponrmgr.ALLOC_ID, 1)
Girish Gowdra3f182d82020-03-30 20:38:51 -0700557 RsrcMgr.AllocIDMgmtLock[intfID].Unlock()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530558
Girish Gowdru0c588b22019-04-23 23:24:56 -0400559 if AllocID == nil || err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000560 logger.Error("Failed to allocate alloc id")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400561 return 0
562 }
563 // update the resource map on KV store with the list of alloc_id
564 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530565 err = RsrcMgr.ResourceMgrs[intfID].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID, AllocID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400566 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000567 logger.Error("Failed to update Alloc ID")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400568 return 0
569 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000570 logger.Debugw("Allocated new Tcont from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400571 return AllocID[0]
Abhilash S.L7f17e402019-03-15 17:40:41 +0530572}
573
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700574// UpdateAllocIdsForOnu updates alloc ids in kv store for a given pon interface id, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530575func (RsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ctx context.Context, ponPort uint32, onuID uint32, uniID uint32, allocID []uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530576
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700577 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530578 return RsrcMgr.ResourceMgrs[ponPort].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700579 allocID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530580}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700581
582// GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530583func (RsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(ctx context.Context, intfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700584 uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530585
Girish Gowdru0c588b22019-04-23 23:24:56 -0400586 /* Get gem ports for given pon interface , onu id and uni id. */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530587
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700588 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530589 return RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530590}
591
Gamze Abakafee36392019-10-03 11:17:24 +0000592// GetCurrentAllocIDsForOnu returns alloc ids for given pon interface and onu id
npujarec5762e2020-01-01 14:08:48 +0530593func (RsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDsForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530594
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700595 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530596 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400597 if AllocID != nil {
Gamze Abakafee36392019-10-03 11:17:24 +0000598 return AllocID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400599 }
Gamze Abakafee36392019-10-03 11:17:24 +0000600 return []uint32{}
601}
602
603// RemoveAllocIDForOnu removes the alloc id for given pon interface, onu id, uni id and alloc id
npujarec5762e2020-01-01 14:08:48 +0530604func (RsrcMgr *OpenOltResourceMgr) RemoveAllocIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, allocID uint32) {
605 allocIDs := RsrcMgr.GetCurrentAllocIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000606 for i := 0; i < len(allocIDs); i++ {
607 if allocIDs[i] == allocID {
608 allocIDs = append(allocIDs[:i], allocIDs[i+1:]...)
609 break
610 }
611 }
npujarec5762e2020-01-01 14:08:48 +0530612 err := RsrcMgr.UpdateAllocIdsForOnu(ctx, intfID, onuID, uniID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000613 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000614 logger.Errorf("Failed to Remove Alloc Id For Onu. IntfID %d onuID %d uniID %d allocID %d",
Gamze Abakafee36392019-10-03 11:17:24 +0000615 intfID, onuID, uniID, allocID)
616 }
617}
618
619// RemoveGemPortIDForOnu removes the gem port id for given pon interface, onu id, uni id and gem port id
npujarec5762e2020-01-01 14:08:48 +0530620func (RsrcMgr *OpenOltResourceMgr) RemoveGemPortIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, gemPortID uint32) {
621 gemPortIDs := RsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000622 for i := 0; i < len(gemPortIDs); i++ {
623 if gemPortIDs[i] == gemPortID {
624 gemPortIDs = append(gemPortIDs[:i], gemPortIDs[i+1:]...)
625 break
626 }
627 }
npujarec5762e2020-01-01 14:08:48 +0530628 err := RsrcMgr.UpdateGEMPortIDsForOnu(ctx, intfID, onuID, uniID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000629 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000630 logger.Errorf("Failed to Remove Gem Id For Onu. IntfID %d onuID %d uniID %d gemPortId %d",
Gamze Abakafee36392019-10-03 11:17:24 +0000631 intfID, onuID, uniID, gemPortID)
632 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530633}
634
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700635// UpdateGEMportsPonportToOnuMapOnKVStore updates onu and uni id associated with the gem port to the kv store
636// This stored information is used when packet_indication is received and we need to derive the ONU Id for which
637// the packet arrived based on the pon_intf and gemport available in the packet_indication
npujarec5762e2020-01-01 14:08:48 +0530638func (RsrcMgr *OpenOltResourceMgr) UpdateGEMportsPonportToOnuMapOnKVStore(ctx context.Context, gemPorts []uint32, PonPort uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700639 onuID uint32, uniID uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530640
Girish Gowdru0c588b22019-04-23 23:24:56 -0400641 /* Update onu and uni id associated with the gem port to the kv store. */
642 var IntfGEMPortPath string
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700643 Data := fmt.Sprintf("%d %d", onuID, uniID)
644 for _, GEM := range gemPorts {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400645 IntfGEMPortPath = fmt.Sprintf("%d,%d", PonPort, GEM)
646 Val, err := json.Marshal(Data)
647 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000648 logger.Error("failed to Marshal")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400649 return err
650 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700651
npujarec5762e2020-01-01 14:08:48 +0530652 if err = RsrcMgr.KVStore.Put(ctx, IntfGEMPortPath, Val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000653 logger.Errorf("Failed to update resource %s", IntfGEMPortPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400654 return err
655 }
656 }
657 return nil
Abhilash S.L7f17e402019-03-15 17:40:41 +0530658}
659
Gamze Abakafee36392019-10-03 11:17:24 +0000660// RemoveGEMportPonportToOnuMapOnKVStore removes the relationship between the gem port and pon port
npujarec5762e2020-01-01 14:08:48 +0530661func (RsrcMgr *OpenOltResourceMgr) RemoveGEMportPonportToOnuMapOnKVStore(ctx context.Context, GemPort uint32, PonPort uint32) {
Gamze Abakafee36392019-10-03 11:17:24 +0000662 IntfGEMPortPath := fmt.Sprintf("%d,%d", PonPort, GemPort)
npujarec5762e2020-01-01 14:08:48 +0530663 err := RsrcMgr.KVStore.Delete(ctx, IntfGEMPortPath)
Gamze Abakafee36392019-10-03 11:17:24 +0000664 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000665 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 +0000666 }
667}
668
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700669// GetGEMPortID gets gem port id for a particular pon port, onu id and uni id and then update the resource map on
670// the KV store with the list of gemport_id allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530671func (RsrcMgr *OpenOltResourceMgr) GetGEMPortID(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700672 uniID uint32, NumOfPorts uint32) ([]uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530673
Girish Gowdru0c588b22019-04-23 23:24:56 -0400674 /* Get gem port id for a particular pon port, onu id
675 and uni id.
676 */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530677
Girish Gowdru0c588b22019-04-23 23:24:56 -0400678 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700679 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530680
npujarec5762e2020-01-01 14:08:48 +0530681 GEMPortList := RsrcMgr.ResourceMgrs[ponPort].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400682 if GEMPortList != nil {
683 return GEMPortList, nil
684 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530685
Girish Gowdra3f182d82020-03-30 20:38:51 -0700686 RsrcMgr.GemPortIDMgmtLock[ponPort].Lock()
npujarec5762e2020-01-01 14:08:48 +0530687 GEMPortList, err = RsrcMgr.ResourceMgrs[ponPort].GetResourceID(ctx, ponPort,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400688 ponrmgr.GEMPORT_ID, NumOfPorts)
Girish Gowdra3f182d82020-03-30 20:38:51 -0700689 RsrcMgr.GemPortIDMgmtLock[ponPort].Unlock()
Girish Gowdru0c588b22019-04-23 23:24:56 -0400690 if err != nil && GEMPortList == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000691 logger.Errorf("Failed to get gem port id for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400692 return nil, err
693 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530694
Girish Gowdru0c588b22019-04-23 23:24:56 -0400695 // update the resource map on KV store with the list of gemport_id
696 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530697 err = RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400698 GEMPortList)
699 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000700 logger.Errorf("Failed to update GEM ports to kv store for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400701 return nil, err
702 }
npujarec5762e2020-01-01 14:08:48 +0530703 _ = RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(ctx, GEMPortList, ponPort,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700704 onuID, uniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400705 return GEMPortList, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530706}
707
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700708// 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 +0530709func (RsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700710 uniID uint32, GEMPortList []uint32) error {
711 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530712 return RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400713 GEMPortList)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530714
715}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700716
717// FreeonuID releases(make free) onu id for a particular pon-port
npujarec5762e2020-01-01 14:08:48 +0530718func (RsrcMgr *OpenOltResourceMgr) FreeonuID(ctx context.Context, intfID uint32, onuID []uint32) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700719
Girish Gowdra3f182d82020-03-30 20:38:51 -0700720 RsrcMgr.OnuIDMgmtLock[intfID].Lock()
npujarec5762e2020-01-01 14:08:48 +0530721 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID, ponrmgr.ONU_ID, onuID)
Girish Gowdra3f182d82020-03-30 20:38:51 -0700722 RsrcMgr.OnuIDMgmtLock[intfID].Unlock()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530723
Girish Gowdru0c588b22019-04-23 23:24:56 -0400724 /* Free onu id for a particular interface.*/
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700725 var IntfonuID string
726 for _, onu := range onuID {
727 IntfonuID = fmt.Sprintf("%d,%d", intfID, onu)
npujarec5762e2020-01-01 14:08:48 +0530728 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfonuID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400729 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530730}
731
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700732// FreeFlowID returns the free flow id for a given interface, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530733func (RsrcMgr *OpenOltResourceMgr) FreeFlowID(ctx context.Context, IntfID uint32, onuID int32,
Devmalya Paul495b94a2019-08-27 19:42:00 -0400734 uniID int32, FlowID uint32) {
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400735 var IntfONUID string
736 var err error
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530737 FlowIds := make([]uint32, 0)
738
739 FlowIds = append(FlowIds, FlowID)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700740 IntfONUID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530741 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(ctx, IntfONUID, FlowID, false)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400742 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000743 logger.Errorw("Failed to Update flow id for", log.Fields{"intf": IntfONUID})
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400744 }
npujarec5762e2020-01-01 14:08:48 +0530745 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(ctx, IntfONUID, FlowID)
Girish Gowdra3f182d82020-03-30 20:38:51 -0700746 RsrcMgr.FlowIDMgmtLock.Lock()
747 defer RsrcMgr.FlowIDMgmtLock.Unlock()
npujarec5762e2020-01-01 14:08:48 +0530748 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.FLOW_ID, FlowIds)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400749}
750
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700751// FreeFlowIDs releases the flow Ids
npujarec5762e2020-01-01 14:08:48 +0530752func (RsrcMgr *OpenOltResourceMgr) FreeFlowIDs(ctx context.Context, IntfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700753 uniID uint32, FlowID []uint32) {
Girish Gowdra3f182d82020-03-30 20:38:51 -0700754 RsrcMgr.FlowIDMgmtLock.Lock()
npujarec5762e2020-01-01 14:08:48 +0530755 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.FLOW_ID, FlowID)
Girish Gowdra3f182d82020-03-30 20:38:51 -0700756 RsrcMgr.FlowIDMgmtLock.Unlock()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530757
Abhilash S.L8ee90712019-04-29 16:24:22 +0530758 var IntfOnuIDUniID string
Girish Gowdru0c588b22019-04-23 23:24:56 -0400759 var err error
760 for _, flow := range FlowID {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700761 IntfOnuIDUniID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530762 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(ctx, IntfOnuIDUniID, flow, false)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400763 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000764 logger.Errorw("Failed to Update flow id for", log.Fields{"intf": IntfOnuIDUniID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400765 }
npujarec5762e2020-01-01 14:08:48 +0530766 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(ctx, IntfOnuIDUniID, flow)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400767 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530768}
769
Gamze Abakafee36392019-10-03 11:17:24 +0000770// FreeAllocID frees AllocID on the PON resource pool and also frees the allocID association
771// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530772func (RsrcMgr *OpenOltResourceMgr) FreeAllocID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000773 uniID uint32, allocID uint32) {
npujarec5762e2020-01-01 14:08:48 +0530774 RsrcMgr.RemoveAllocIDForOnu(ctx, IntfID, onuID, uniID, allocID)
Gamze Abakafee36392019-10-03 11:17:24 +0000775 allocIDs := make([]uint32, 0)
776 allocIDs = append(allocIDs, allocID)
Girish Gowdra3f182d82020-03-30 20:38:51 -0700777 RsrcMgr.AllocIDMgmtLock[IntfID].Lock()
778 defer RsrcMgr.AllocIDMgmtLock[IntfID].Unlock()
npujarec5762e2020-01-01 14:08:48 +0530779 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.ALLOC_ID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000780}
781
782// FreeGemPortID frees GemPortID on the PON resource pool and also frees the gemPortID association
783// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530784func (RsrcMgr *OpenOltResourceMgr) FreeGemPortID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000785 uniID uint32, gemPortID uint32) {
npujarec5762e2020-01-01 14:08:48 +0530786 RsrcMgr.RemoveGemPortIDForOnu(ctx, IntfID, onuID, uniID, gemPortID)
Gamze Abakafee36392019-10-03 11:17:24 +0000787 gemPortIDs := make([]uint32, 0)
788 gemPortIDs = append(gemPortIDs, gemPortID)
Girish Gowdra3f182d82020-03-30 20:38:51 -0700789 RsrcMgr.GemPortIDMgmtLock[IntfID].Lock()
790 defer RsrcMgr.GemPortIDMgmtLock[IntfID].Unlock()
npujarec5762e2020-01-01 14:08:48 +0530791 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.GEMPORT_ID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000792}
793
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700794// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id, and the clears the
795// resource map and the onuID associated with (pon_intf_id, gemport_id) tuple,
npujarec5762e2020-01-01 14:08:48 +0530796func (RsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530797
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700798 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530799
npujarec5762e2020-01-01 14:08:48 +0530800 AllocIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530801
Girish Gowdra3f182d82020-03-30 20:38:51 -0700802 RsrcMgr.AllocIDMgmtLock[onuID].Lock()
npujarec5762e2020-01-01 14:08:48 +0530803 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400804 ponrmgr.ALLOC_ID,
805 AllocIDs)
Girish Gowdra3f182d82020-03-30 20:38:51 -0700806 RsrcMgr.AllocIDMgmtLock[onuID].Unlock()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530807
Girish Gowdra3f182d82020-03-30 20:38:51 -0700808 RsrcMgr.GemPortIDMgmtLock[onuID].Lock()
npujarec5762e2020-01-01 14:08:48 +0530809 GEMPortIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
810 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400811 ponrmgr.GEMPORT_ID,
812 GEMPortIDs)
Girish Gowdra3f182d82020-03-30 20:38:51 -0700813 RsrcMgr.GemPortIDMgmtLock[onuID].Unlock()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530814
Girish Gowdra3f182d82020-03-30 20:38:51 -0700815 RsrcMgr.FlowIDMgmtLock.Lock()
npujarec5762e2020-01-01 14:08:48 +0530816 FlowIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentFlowIDsForOnu(ctx, IntfOnuIDUniID)
817 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400818 ponrmgr.FLOW_ID,
819 FlowIDs)
Girish Gowdra3f182d82020-03-30 20:38:51 -0700820 RsrcMgr.FlowIDMgmtLock.Unlock()
821
Girish Gowdru0c588b22019-04-23 23:24:56 -0400822 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
npujarec5762e2020-01-01 14:08:48 +0530823 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400824 // Clear the ONU Id associated with the (pon_intf_id, gemport_id) tuple.
825 for _, GEM := range GEMPortIDs {
npujarec5762e2020-01-01 14:08:48 +0530826 _ = RsrcMgr.KVStore.Delete(ctx, fmt.Sprintf("%d,%d", intfID, GEM))
Girish Gowdru0c588b22019-04-23 23:24:56 -0400827 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530828}
829
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700830// IsFlowCookieOnKVStore checks if the given flow cookie is present on the kv store
831// Returns true if the flow cookie is found, otherwise it returns false
npujarec5762e2020-01-01 14:08:48 +0530832func (RsrcMgr *OpenOltResourceMgr) IsFlowCookieOnKVStore(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700833 flowStoreCookie uint64) bool {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530834
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700835 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530836 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(ctx, FlowPath)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400837 if FlowIDs != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000838 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 -0700839 for _, flowID := range FlowIDs {
npujarec5762e2020-01-01 14:08:48 +0530840 FlowInfo := RsrcMgr.GetFlowIDInfo(ctx, ponIntfID, int32(onuID), int32(uniID), uint32(flowID))
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400841 if FlowInfo != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000842 logger.Debugw("Found flows", log.Fields{"flows": *FlowInfo, "flowId": flowID})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400843 for _, Info := range *FlowInfo {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700844 if Info.FlowStoreCookie == flowStoreCookie {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000845 logger.Debug("Found flow matching with flowStore cookie", log.Fields{"flowId": flowID, "flowStoreCookie": flowStoreCookie})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400846 return true
847 }
848 }
849 }
850 }
851 }
852 return false
853}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400854
salmansiddiqui7ac62132019-08-22 03:58:50 +0000855// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000856// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530857func (RsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) []uint32 {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000858 Path := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000859 var Data []uint32
npujarec5762e2020-01-01 14:08:48 +0530860 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400861 if err == nil {
862 if Value != nil {
863 Val, err := kvstore.ToByte(Value.Value)
864 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000865 logger.Errorw("Failed to convert into byte array", log.Fields{"error": err})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400866 return Data
867 }
868 if err = json.Unmarshal(Val, &Data); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000869 logger.Error("Failed to unmarshal", log.Fields{"error": err})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400870 return Data
871 }
872 }
873 } else {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000874 logger.Errorf("Failed to get TP id from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400875 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000876 logger.Debugf("Getting TP id %d from path %s", Data, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400877 return Data
878
879}
880
Gamze Abakafee36392019-10-03 11:17:24 +0000881// RemoveTechProfileIDsForOnu deletes all tech profile ids from the KV-Store for the given onu based on the path
882// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530883func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDsForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) error {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000884 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
npujarec5762e2020-01-01 14:08:48 +0530885 if err := RsrcMgr.KVStore.Delete(ctx, IntfOnuUniID); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000886 logger.Errorw("Failed to delete techprofile id resource in KV store", log.Fields{"path": IntfOnuUniID})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400887 return err
888 }
889 return nil
890}
891
Gamze Abakafee36392019-10-03 11:17:24 +0000892// RemoveTechProfileIDForOnu deletes a specific tech profile id from the KV-Store for the given onu based on the path
893// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530894func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32, TpID uint32) error {
895 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000896 for i, tpIDInList := range tpIDList {
897 if tpIDInList == TpID {
898 tpIDList = append(tpIDList[:i], tpIDList[i+1:]...)
899 }
900 }
901 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
902 Value, err := json.Marshal(tpIDList)
903 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000904 logger.Error("failed to Marshal")
Gamze Abakafee36392019-10-03 11:17:24 +0000905 return err
906 }
npujarec5762e2020-01-01 14:08:48 +0530907 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000908 logger.Errorf("Failed to update resource %s", IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000909 return err
910 }
911 return err
912}
913
914// UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
915// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530916func (RsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32,
salmansiddiqui7ac62132019-08-22 03:58:50 +0000917 UniID uint32, TpID uint32) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400918 var Value []byte
919 var err error
920
salmansiddiqui7ac62132019-08-22 03:58:50 +0000921 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000922
npujarec5762e2020-01-01 14:08:48 +0530923 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000924 for _, value := range tpIDList {
925 if value == TpID {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000926 logger.Debugf("TpID %d is already in tpIdList for the path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000927 return err
928 }
929 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000930 logger.Debugf("updating tp id %d on path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000931 tpIDList = append(tpIDList, TpID)
932 Value, err = json.Marshal(tpIDList)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400933 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000934 logger.Error("failed to Marshal")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400935 return err
936 }
npujarec5762e2020-01-01 14:08:48 +0530937 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000938 logger.Errorf("Failed to update resource %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400939 return err
940 }
941 return err
942}
943
salmansiddiqui7ac62132019-08-22 03:58:50 +0000944// UpdateMeterIDForOnu updates the meter id in the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000945// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +0530946func (RsrcMgr *OpenOltResourceMgr) UpdateMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000947 UniID uint32, TpID uint32, MeterConfig *ofp.OfpMeterConfig) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400948 var Value []byte
949 var err error
950
Gamze Abakafee36392019-10-03 11:17:24 +0000951 IntfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400952 Value, err = json.Marshal(*MeterConfig)
953 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000954 logger.Error("failed to Marshal meter config")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400955 return err
956 }
npujarec5762e2020-01-01 14:08:48 +0530957 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000958 logger.Errorf("Failed to store meter into KV store %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400959 return err
960 }
961 return err
962}
963
Gamze Abakafee36392019-10-03 11:17:24 +0000964// GetMeterIDForOnu fetches the meter id from the kv store for the given onu based on the path
965// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +0530966func (RsrcMgr *OpenOltResourceMgr) GetMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000967 UniID uint32, TpID uint32) (*ofp.OfpMeterConfig, error) {
968 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400969 var meterConfig ofp.OfpMeterConfig
npujarec5762e2020-01-01 14:08:48 +0530970 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400971 if err == nil {
972 if Value != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000973 logger.Debug("Found meter in KV store", log.Fields{"Direction": Direction})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000974 Val, er := kvstore.ToByte(Value.Value)
975 if er != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000976 logger.Errorw("Failed to convert into byte array", log.Fields{"error": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000977 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400978 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000979 if er = json.Unmarshal(Val, &meterConfig); er != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000980 logger.Error("Failed to unmarshal meterconfig", log.Fields{"error": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000981 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400982 }
983 } else {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000984 logger.Debug("meter-does-not-exists-in-KVStore")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400985 return nil, err
986 }
987 } else {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000988 logger.Errorf("Failed to get Meter config from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400989
990 }
991 return &meterConfig, err
992}
993
Gamze Abakafee36392019-10-03 11:17:24 +0000994// RemoveMeterIDForOnu deletes the meter id from the kV-Store for the given onu based on the path
995// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +0530996func (RsrcMgr *OpenOltResourceMgr) RemoveMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000997 UniID uint32, TpID uint32) error {
998 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
npujarec5762e2020-01-01 14:08:48 +0530999 if err := RsrcMgr.KVStore.Delete(ctx, Path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001000 logger.Errorf("Failed to delete meter id %s from kvstore ", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -04001001 return err
1002 }
1003 return nil
1004}
salmansiddiqui7ac62132019-08-22 03:58:50 +00001005
1006func getFlowIDFromFlowInfo(FlowInfo *[]FlowInfo, flowID, gemportID uint32, flowStoreCookie uint64, flowCategory string, vlanPcp ...uint32) error {
1007 if FlowInfo != nil {
1008 for _, Info := range *FlowInfo {
1009 if int32(gemportID) == Info.Flow.GemportId && flowCategory != "" && Info.FlowCategory == flowCategory {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001010 logger.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001011 if Info.FlowCategory == "HSIA_FLOW" && Info.Flow.Classifier.OPbits == vlanPcp[0] {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001012 logger.Debug("Found matching vlan pcp ", log.Fields{"flowId": flowID, "Vlanpcp": vlanPcp[0]})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001013 return nil
1014 }
1015 }
1016 if int32(gemportID) == Info.Flow.GemportId && flowStoreCookie != 0 && Info.FlowStoreCookie == flowStoreCookie {
1017 if flowCategory != "" && Info.FlowCategory == flowCategory {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001018 logger.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001019 return nil
1020 }
1021 }
1022 }
1023 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001024 logger.Debugw("the flow can be related to a different service", log.Fields{"flow_info": FlowInfo})
salmansiddiqui7ac62132019-08-22 03:58:50 +00001025 return errors.New("invalid flow-info")
1026}
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301027
1028//AddGemToOnuGemInfo adds gemport to onugem info kvstore
npujarec5762e2020-01-01 14:08:48 +05301029func (RsrcMgr *OpenOltResourceMgr) AddGemToOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, gemPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301030 var onuGemData []OnuGemInfo
1031 var err error
1032
npujarec5762e2020-01-01 14:08:48 +05301033 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001034 logger.Errorf("failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301035 return err
1036 }
1037 if len(onuGemData) == 0 {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001038 logger.Errorw("failed to ger Onuid info ", log.Fields{"intfid": intfID, "onuid": onuID})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301039 return err
1040 }
1041
1042 for idx, onugem := range onuGemData {
1043 if onugem.OnuID == onuID {
1044 for _, gem := range onuGemData[idx].GemPorts {
1045 if gem == gemPort {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001046 logger.Debugw("Gem already present in onugem info, skpping addition", log.Fields{"gem": gem})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301047 return nil
1048 }
1049 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001050 logger.Debugw("Added gem to onugem info", log.Fields{"gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301051 onuGemData[idx].GemPorts = append(onuGemData[idx].GemPorts, gemPort)
1052 break
1053 }
1054 }
npujarec5762e2020-01-01 14:08:48 +05301055 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301056 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001057 logger.Error("Failed to add onugem to kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301058 return err
1059 }
1060 return err
1061}
1062
1063//GetOnuGemInfo gets onu gem info from the kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301064func (RsrcMgr *OpenOltResourceMgr) GetOnuGemInfo(ctx context.Context, IntfID uint32) ([]OnuGemInfo, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301065 var onuGemData []OnuGemInfo
1066
npujarec5762e2020-01-01 14:08:48 +05301067 if err := RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001068 logger.Errorf("failed to get onuifo for intfid %d", IntfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301069 return nil, err
1070 }
1071
1072 return onuGemData, nil
1073}
1074
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001075// AddOnuGemInfo adds onu info on to the kvstore per interface
1076func (RsrcMgr *OpenOltResourceMgr) AddOnuGemInfo(ctx context.Context, IntfID uint32, onuGem OnuGemInfo) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301077 var onuGemData []OnuGemInfo
1078 var err error
1079
npujarec5762e2020-01-01 14:08:48 +05301080 if err = RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001081 logger.Errorf("failed to get onuifo for intfid %d", IntfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301082 return err
1083 }
1084 onuGemData = append(onuGemData, onuGem)
npujarec5762e2020-01-01 14:08:48 +05301085 err = RsrcMgr.ResourceMgrs[IntfID].AddOnuGemInfo(ctx, IntfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301086 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001087 logger.Error("Failed to add onugem to kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301088 return err
1089 }
1090
Girish Kumar2ad402b2020-03-20 19:45:12 +00001091 logger.Debugw("added onu to onugeminfo", log.Fields{"intf": IntfID, "onugem": onuGem})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301092 return err
1093}
1094
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001095// UpdateOnuGemInfo updates Onuinfo on the kvstore per interface
1096func (RsrcMgr *OpenOltResourceMgr) UpdateOnuGemInfo(ctx context.Context, IntfID uint32, onuGem []OnuGemInfo) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301097
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001098 // TODO: VOL-2643
1099 err := RsrcMgr.ResourceMgrs[IntfID].AddOnuGemInfo(ctx, IntfID, onuGem)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301100 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001101 logger.Debugw("persistence-update-failed", log.Fields{
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001102 "interface-id": IntfID,
1103 "gem-info": onuGem,
1104 "error": err})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301105 return err
1106 }
1107
Girish Kumar2ad402b2020-03-20 19:45:12 +00001108 logger.Debugw("updated onugeminfo", log.Fields{"intf": IntfID, "onugem": onuGem})
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001109 return nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301110}
1111
1112// 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 +05301113func (RsrcMgr *OpenOltResourceMgr) AddUniPortToOnuInfo(ctx context.Context, intfID uint32, onuID uint32, portNo uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301114 var onuGemData []OnuGemInfo
1115 var err error
1116
npujarec5762e2020-01-01 14:08:48 +05301117 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001118 logger.Errorf("failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301119 return
1120 }
1121 for idx, onu := range onuGemData {
1122 if onu.OnuID == onuID {
1123 for _, uni := range onu.UniPorts {
1124 if uni == portNo {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001125 logger.Debugw("uni already present in onugem info", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301126 return
1127 }
1128 }
1129 onuGemData[idx].UniPorts = append(onuGemData[idx].UniPorts, portNo)
1130 break
1131 }
1132 }
npujarec5762e2020-01-01 14:08:48 +05301133 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301134 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001135 logger.Errorw("Failed to add uin port in onugem to kv store", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301136 return
1137 }
1138 return
1139}
1140
1141//UpdateGemPortForPktIn updates gemport for pkt in path to kvstore, path being intfid, onuid, portno
npujarec5762e2020-01-01 14:08:48 +05301142func (RsrcMgr *OpenOltResourceMgr) UpdateGemPortForPktIn(ctx context.Context, pktIn PacketInInfoKey, gemPort uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301143
1144 path := fmt.Sprintf(OnuPacketINPath, pktIn.IntfID, pktIn.OnuID, pktIn.LogicalPort)
1145 Value, err := json.Marshal(gemPort)
1146 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001147 logger.Error("Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301148 return
1149 }
npujarec5762e2020-01-01 14:08:48 +05301150 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001151 logger.Errorw("Failed to put to kvstore", log.Fields{"path": path, "value": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301152 return
1153 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001154 logger.Debugw("added gem packet in successfully", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301155
1156 return
1157}
1158
1159// GetGemPortFromOnuPktIn gets the gem port from onu pkt in path, path being intfid, onuid, portno
npujarec5762e2020-01-01 14:08:48 +05301160func (RsrcMgr *OpenOltResourceMgr) GetGemPortFromOnuPktIn(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) (uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301161
1162 var Val []byte
1163 var gemPort uint32
1164
1165 path := fmt.Sprintf(OnuPacketINPath, intfID, onuID, logicalPort)
1166
npujarec5762e2020-01-01 14:08:48 +05301167 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301168 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001169 logger.Errorw("Failed to get from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301170 return uint32(0), err
1171 } else if value == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001172 logger.Debugw("No pkt in gem found", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301173 return uint32(0), nil
1174 }
1175
1176 if Val, err = kvstore.ToByte(value.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001177 logger.Error("Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301178 return uint32(0), err
1179 }
1180 if err = json.Unmarshal(Val, &gemPort); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001181 logger.Error("Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301182 return uint32(0), err
1183 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001184 logger.Debugw("found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301185
1186 return gemPort, nil
1187}
1188
1189// DelGemPortPktIn deletes the gemport from the pkt in path
npujarec5762e2020-01-01 14:08:48 +05301190func (RsrcMgr *OpenOltResourceMgr) DelGemPortPktIn(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301191
1192 path := fmt.Sprintf(OnuPacketINPath, intfID, onuID, logicalPort)
npujarec5762e2020-01-01 14:08:48 +05301193 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001194 logger.Errorf("Falied to remove resource %s", path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301195 return err
1196 }
1197 return nil
1198}
1199
1200// DelOnuGemInfoForIntf deletes the onugem info from kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301201func (RsrcMgr *OpenOltResourceMgr) DelOnuGemInfoForIntf(ctx context.Context, intfID uint32) error {
1202 if err := RsrcMgr.ResourceMgrs[intfID].DelOnuGemInfoForIntf(ctx, intfID); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001203 logger.Errorw("failed to delete onu gem info for", log.Fields{"intfid": intfID})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301204 return err
1205 }
1206 return nil
1207}
1208
1209//GetNNIFromKVStore gets NNi intfids from kvstore. path being per device
npujarec5762e2020-01-01 14:08:48 +05301210func (RsrcMgr *OpenOltResourceMgr) GetNNIFromKVStore(ctx context.Context) ([]uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301211
1212 var nni []uint32
1213 var Val []byte
1214
1215 path := fmt.Sprintf(NnniIntfID)
npujarec5762e2020-01-01 14:08:48 +05301216 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301217 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001218 logger.Error("failed to get data from kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301219 return nil, err
1220 }
1221 if value != nil {
1222 if Val, err = kvstore.ToByte(value.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001223 logger.Error("Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301224 return nil, err
1225 }
1226 if err = json.Unmarshal(Val, &nni); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001227 logger.Error("Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301228 return nil, err
1229 }
1230 }
1231 return nni, err
1232}
1233
1234// AddNNIToKVStore adds Nni interfaces to kvstore, path being per device.
npujarec5762e2020-01-01 14:08:48 +05301235func (RsrcMgr *OpenOltResourceMgr) AddNNIToKVStore(ctx context.Context, nniIntf uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301236 var Value []byte
1237
npujarec5762e2020-01-01 14:08:48 +05301238 nni, err := RsrcMgr.GetNNIFromKVStore(ctx)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301239 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001240 logger.Error("failed to fetch nni interfaces from kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301241 return err
1242 }
1243
1244 path := fmt.Sprintf(NnniIntfID)
1245 nni = append(nni, nniIntf)
1246 Value, err = json.Marshal(nni)
1247 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001248 logger.Error("Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301249 }
npujarec5762e2020-01-01 14:08:48 +05301250 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001251 logger.Errorw("Failed to put to kvstore", log.Fields{"path": path, "value": Value})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301252 return err
1253 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001254 logger.Debugw("added nni to kv successfully", log.Fields{"path": path, "nni": nniIntf})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301255 return nil
1256}
1257
1258// DelNNiFromKVStore deletes nni interface list from kv store.
npujarec5762e2020-01-01 14:08:48 +05301259func (RsrcMgr *OpenOltResourceMgr) DelNNiFromKVStore(ctx context.Context) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301260
1261 path := fmt.Sprintf(NnniIntfID)
1262
npujarec5762e2020-01-01 14:08:48 +05301263 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001264 logger.Errorw("Failed to delete nni interfaces from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301265 return err
1266 }
1267 return nil
1268}
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301269
1270//UpdateFlowIDsForGem updates flow id per gemport
npujarec5762e2020-01-01 14:08:48 +05301271func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDsForGem(ctx context.Context, intf uint32, gem uint32, flowIDs []uint32) error {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301272 var val []byte
1273 path := fmt.Sprintf(FlowIDsForGem, intf)
1274
npujarec5762e2020-01-01 14:08:48 +05301275 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301276 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001277 logger.Error("Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301278 return err
1279 }
1280 if flowsForGem == nil {
1281 flowsForGem = make(map[uint32][]uint32)
1282 }
1283 flowsForGem[gem] = flowIDs
1284 val, err = json.Marshal(flowsForGem)
1285 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001286 logger.Error("Failed to marshal data", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301287 return err
1288 }
Girish Gowdra3f182d82020-03-30 20:38:51 -07001289 RsrcMgr.flowIDToGemInfoLock.Lock()
1290 defer RsrcMgr.flowIDToGemInfoLock.Unlock()
npujarec5762e2020-01-01 14:08:48 +05301291 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001292 logger.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301293 return err
1294 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001295 logger.Debugw("added flowid list for gem to kv successfully", log.Fields{"path": path, "flowidlist": flowsForGem[gem]})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301296 return nil
1297}
1298
1299//DeleteFlowIDsForGem deletes the flowID list entry per gem from kvstore.
npujarec5762e2020-01-01 14:08:48 +05301300func (RsrcMgr *OpenOltResourceMgr) DeleteFlowIDsForGem(ctx context.Context, intf uint32, gem uint32) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301301 path := fmt.Sprintf(FlowIDsForGem, intf)
1302 var val []byte
1303
npujarec5762e2020-01-01 14:08:48 +05301304 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301305 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001306 logger.Error("Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301307 return
1308 }
1309 if flowsForGem == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001310 logger.Error("No flowids found ", log.Fields{"intf": intf, "gemport": gem})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301311 return
1312 }
1313 // once we get the flows per gem map from kv , just delete the gem entry from the map
1314 delete(flowsForGem, gem)
1315 // once gem entry is deleted update the kv store.
1316 val, err = json.Marshal(flowsForGem)
1317 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001318 logger.Error("Failed to marshal data", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301319 return
1320 }
Girish Gowdra3f182d82020-03-30 20:38:51 -07001321
1322 RsrcMgr.flowIDToGemInfoLock.Lock()
1323 defer RsrcMgr.flowIDToGemInfoLock.Unlock()
1324
npujarec5762e2020-01-01 14:08:48 +05301325 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001326 logger.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301327 return
1328 }
1329 return
1330}
1331
1332//GetFlowIDsGemMapForInterface gets flowids per gemport and interface
npujarec5762e2020-01-01 14:08:48 +05301333func (RsrcMgr *OpenOltResourceMgr) GetFlowIDsGemMapForInterface(ctx context.Context, intf uint32) (map[uint32][]uint32, error) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301334 path := fmt.Sprintf(FlowIDsForGem, intf)
1335 var flowsForGem map[uint32][]uint32
1336 var val []byte
Girish Gowdra3f182d82020-03-30 20:38:51 -07001337 RsrcMgr.flowIDToGemInfoLock.RLock()
npujarec5762e2020-01-01 14:08:48 +05301338 value, err := RsrcMgr.KVStore.Get(ctx, path)
Girish Gowdra3f182d82020-03-30 20:38:51 -07001339 RsrcMgr.flowIDToGemInfoLock.RUnlock()
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301340 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001341 logger.Error("failed to get data from kv store")
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301342 return nil, err
1343 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001344 if value != nil && value.Value != nil {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301345 if val, err = kvstore.ToByte(value.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001346 logger.Error("Failed to convert to byte array ", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301347 return nil, err
1348 }
1349 if err = json.Unmarshal(val, &flowsForGem); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001350 logger.Error("Failed to unmarshall", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301351 return nil, err
1352 }
1353 }
1354 return flowsForGem, nil
1355}
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301356
1357//DeleteIntfIDGempMapPath deletes the intf id path used to store flow ids per gem to kvstore.
npujarec5762e2020-01-01 14:08:48 +05301358func (RsrcMgr *OpenOltResourceMgr) DeleteIntfIDGempMapPath(ctx context.Context, intf uint32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301359 path := fmt.Sprintf(FlowIDsForGem, intf)
Girish Gowdra3f182d82020-03-30 20:38:51 -07001360 RsrcMgr.flowIDToGemInfoLock.Lock()
1361 defer RsrcMgr.flowIDToGemInfoLock.Unlock()
npujarec5762e2020-01-01 14:08:48 +05301362 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001363 logger.Errorw("Failed to delete nni interfaces from kv store", log.Fields{"path": path})
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301364 return
1365 }
1366 return
1367}
1368
1369// RemoveResourceMap Clear resource map associated with (intfid, onuid, uniid) tuple.
npujarec5762e2020-01-01 14:08:48 +05301370func (RsrcMgr *OpenOltResourceMgr) RemoveResourceMap(ctx context.Context, intfID uint32, onuID int32, uniID int32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301371 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +05301372 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301373}
Esin Karamanccb714b2019-11-29 15:02:06 +00001374
1375//GetMcastQueuePerInterfaceMap gets multicast queue info per pon interface
npujarec5762e2020-01-01 14:08:48 +05301376func (RsrcMgr *OpenOltResourceMgr) GetMcastQueuePerInterfaceMap(ctx context.Context) (map[uint32][]uint32, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001377 path := fmt.Sprintf(McastQueuesForIntf)
1378 var mcastQueueToIntfMap map[uint32][]uint32
1379 var val []byte
1380
npujarec5762e2020-01-01 14:08:48 +05301381 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001382 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001383 logger.Error("failed to get data from kv store")
Esin Karamanccb714b2019-11-29 15:02:06 +00001384 return nil, err
1385 }
1386 if kvPair != nil && kvPair.Value != nil {
1387 if val, err = kvstore.ToByte(kvPair.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001388 logger.Error("Failed to convert to byte array ", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001389 return nil, err
1390 }
1391 if err = json.Unmarshal(val, &mcastQueueToIntfMap); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001392 logger.Error("Failed to unmarshall ", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001393 return nil, err
1394 }
1395 }
1396 return mcastQueueToIntfMap, nil
1397}
1398
1399//AddMcastQueueForIntf adds multicast queue for pon interface
npujarec5762e2020-01-01 14:08:48 +05301400func (RsrcMgr *OpenOltResourceMgr) AddMcastQueueForIntf(ctx context.Context, intf uint32, gem uint32, servicePriority uint32) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001401 var val []byte
1402 path := fmt.Sprintf(McastQueuesForIntf)
1403
npujarec5762e2020-01-01 14:08:48 +05301404 mcastQueues, err := RsrcMgr.GetMcastQueuePerInterfaceMap(ctx)
Esin Karamanccb714b2019-11-29 15:02:06 +00001405 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001406 logger.Errorw("Failed to get multicast queue info for interface", log.Fields{"error": err, "intf": intf})
Esin Karamanccb714b2019-11-29 15:02:06 +00001407 return err
1408 }
1409 if mcastQueues == nil {
1410 mcastQueues = make(map[uint32][]uint32)
1411 }
1412 mcastQueues[intf] = []uint32{gem, servicePriority}
1413 if val, err = json.Marshal(mcastQueues); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001414 logger.Errorw("Failed to marshal data", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001415 return err
1416 }
npujarec5762e2020-01-01 14:08:48 +05301417 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001418 logger.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Esin Karamanccb714b2019-11-29 15:02:06 +00001419 return err
1420 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001421 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 +00001422 return nil
1423}
1424
1425//AddFlowGroupToKVStore adds flow group into KV store
npujarec5762e2020-01-01 14:08:48 +05301426func (RsrcMgr *OpenOltResourceMgr) AddFlowGroupToKVStore(ctx context.Context, groupEntry *ofp.OfpGroupEntry, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001427 var Value []byte
1428 var err error
1429 var path string
1430 if cached {
1431 path = fmt.Sprintf(FlowGroupCached, groupEntry.Desc.GroupId)
1432 } else {
1433 path = fmt.Sprintf(FlowGroup, groupEntry.Desc.GroupId)
1434 }
1435 //build group info object
1436 var outPorts []uint32
1437 for _, ofBucket := range groupEntry.Desc.Buckets {
1438 for _, ofAction := range ofBucket.Actions {
1439 if ofAction.Type == ofp.OfpActionType_OFPAT_OUTPUT {
1440 outPorts = append(outPorts, ofAction.GetOutput().Port)
1441 }
1442 }
1443 }
1444 groupInfo := GroupInfo{
1445 GroupID: groupEntry.Desc.GroupId,
1446 OutPorts: outPorts,
1447 }
1448
1449 Value, err = json.Marshal(groupInfo)
1450
1451 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001452 logger.Error("failed to Marshal flow group object")
Esin Karamanccb714b2019-11-29 15:02:06 +00001453 return err
1454 }
1455
npujarec5762e2020-01-01 14:08:48 +05301456 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001457 logger.Errorf("Failed to update resource %s", path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001458 return err
1459 }
1460 return nil
1461}
1462
1463//RemoveFlowGroupFromKVStore removes flow group from KV store
npujarec5762e2020-01-01 14:08:48 +05301464func (RsrcMgr *OpenOltResourceMgr) RemoveFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) bool {
Esin Karamanccb714b2019-11-29 15:02:06 +00001465 var path string
1466 if cached {
1467 path = fmt.Sprintf(FlowGroupCached, groupID)
1468 } else {
1469 path = fmt.Sprintf(FlowGroup, groupID)
1470 }
npujarec5762e2020-01-01 14:08:48 +05301471 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001472 logger.Errorf("Failed to remove resource %s due to %s", path, err)
Esin Karamanccb714b2019-11-29 15:02:06 +00001473 return false
1474 }
1475 return true
1476}
1477
1478//GetFlowGroupFromKVStore fetches flow group from the KV store. Returns (false, {} error) if any problem occurs during
1479//fetching the data. Returns (true, groupInfo, nil) if the group is fetched successfully.
1480// Returns (false, {}, nil) if the group does not exists in the KV store.
npujarec5762e2020-01-01 14:08:48 +05301481func (RsrcMgr *OpenOltResourceMgr) GetFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) (bool, GroupInfo, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001482 var groupInfo GroupInfo
1483 var path string
1484 if cached {
1485 path = fmt.Sprintf(FlowGroupCached, groupID)
1486 } else {
1487 path = fmt.Sprintf(FlowGroup, groupID)
1488 }
npujarec5762e2020-01-01 14:08:48 +05301489 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001490 if err != nil {
1491 return false, groupInfo, err
1492 }
1493 if kvPair != nil && kvPair.Value != nil {
1494 Val, err := kvstore.ToByte(kvPair.Value)
1495 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001496 logger.Errorw("Failed to convert flow group into byte array", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001497 return false, groupInfo, err
1498 }
1499 if err = json.Unmarshal(Val, &groupInfo); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001500 logger.Errorw("Failed to unmarshal", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001501 return false, groupInfo, err
1502 }
1503 return true, groupInfo, nil
1504 }
1505 return false, groupInfo, nil
1506}