blob: d3a97894a1d7bf53e4653cab024f1565df35c280 [file] [log] [blame]
Abhilash S.L7f17e402019-03-15 17:40:41 +05301/*
2 * Copyright 2019-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070017//Package resourcemanager provides the utility for managing resources
manikkaraj kbf256be2019-03-25 00:13:48 +053018package resourcemanager
Abhilash S.L7f17e402019-03-15 17:40:41 +053019
20import (
npujarec5762e2020-01-01 14:08:48 +053021 "context"
Girish Gowdru0c588b22019-04-23 23:24:56 -040022 "encoding/json"
23 "errors"
24 "fmt"
Andrea Campanellab83b39d2020-03-30 11:41:16 +020025 "github.com/opencord/voltha-openolt-adapter/internal/pkg/olterrors"
Girish Gowdru0c588b22019-04-23 23:24:56 -040026 "strconv"
27 "strings"
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
Abhilash S.L7f17e402019-03-15 17:40:41 +0530111}
112
Manikkaraj kb1d51442019-07-23 10:41:02 -0400113func newKVClient(storeType string, address string, timeout uint32) (kvstore.Client, error) {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000114 logger.Infow("kv-store-type", log.Fields{"store": storeType})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400115 switch storeType {
116 case "consul":
117 return kvstore.NewConsulClient(address, int(timeout))
118 case "etcd":
119 return kvstore.NewEtcdClient(address, int(timeout))
120 }
121 return nil, errors.New("unsupported-kv-store")
Abhilash S.L7f17e402019-03-15 17:40:41 +0530122}
123
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700124// SetKVClient sets the KV client and return a kv backend
sbarbaria8910ba2019-11-05 10:12:23 -0500125func SetKVClient(backend string, Host string, Port int, DeviceID string) *db.Backend {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400126 addr := Host + ":" + strconv.Itoa(Port)
127 // TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
128 // issue between kv store and backend , core is not calling NewBackend directly
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700129 kvClient, err := newKVClient(backend, addr, KvstoreTimeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400130 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000131 logger.Fatalw("Failed to init KV client\n", log.Fields{"err": err})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400132 return nil
133 }
sbarbaria8910ba2019-11-05 10:12:23 -0500134 kvbackend := &db.Backend{
Girish Gowdru0c588b22019-04-23 23:24:56 -0400135 Client: kvClient,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700136 StoreType: backend,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400137 Host: Host,
138 Port: Port,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700139 Timeout: KvstoreTimeout,
140 PathPrefix: fmt.Sprintf(BasePathKvStore, DeviceID)}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530141
Girish Gowdru0c588b22019-04-23 23:24:56 -0400142 return kvbackend
Abhilash S.L7f17e402019-03-15 17:40:41 +0530143}
144
Gamze Abakafee36392019-10-03 11:17:24 +0000145// NewResourceMgr init a New resource manager instance which in turn instantiates pon resource manager
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700146// instances according to technology. Initializes the default resource ranges for all
147// the resources.
npujarec5762e2020-01-01 14:08:48 +0530148func NewResourceMgr(ctx context.Context, deviceID string, KVStoreHostPort string, kvStoreType string, deviceType string, devInfo *openolt.DeviceInfo) *OpenOltResourceMgr {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400149 var ResourceMgr OpenOltResourceMgr
Girish Kumar2ad402b2020-03-20 19:45:12 +0000150 logger.Debugf("Init new resource manager , host_port: %s, deviceid: %s", KVStoreHostPort, deviceID)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530151 ResourceMgr.HostAndPort = KVStoreHostPort
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700152 ResourceMgr.DeviceType = deviceType
153 ResourceMgr.DevInfo = devInfo
154 IPPort := strings.Split(KVStoreHostPort, ":")
155 ResourceMgr.Host = IPPort[0]
156 ResourceMgr.Port, _ = strconv.Atoi(IPPort[1])
Abhilash S.L7f17e402019-03-15 17:40:41 +0530157
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700158 Backend := kvStoreType
Girish Gowdru0c588b22019-04-23 23:24:56 -0400159 ResourceMgr.KVStore = SetKVClient(Backend, ResourceMgr.Host,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700160 ResourceMgr.Port, deviceID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400161 if ResourceMgr.KVStore == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000162 logger.Error("Failed to setup KV store")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400163 }
164 Ranges := make(map[string]*openolt.DeviceInfo_DeviceResourceRanges)
165 RsrcMgrsByTech := make(map[string]*ponrmgr.PONResourceManager)
166 ResourceMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530167
Girish Gowdru0c588b22019-04-23 23:24:56 -0400168 // TODO self.args = registry('main').get_args()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530169
Girish Gowdru0c588b22019-04-23 23:24:56 -0400170 /*
171 If a legacy driver returns protobuf without any ranges,s synthesize one from
Gamze Abakafee36392019-10-03 11:17:24 +0000172 the legacy global per-device information. This, in theory, is temporary until
Girish Gowdru0c588b22019-04-23 23:24:56 -0400173 the legacy drivers are upgrade to support pool ranges.
174 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700175 if devInfo.Ranges == nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400176 var ranges openolt.DeviceInfo_DeviceResourceRanges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700177 ranges.Technology = devInfo.GetTechnology()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530178
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700179 NumPONPorts := devInfo.GetPonPorts()
Girish Gowdru0c588b22019-04-23 23:24:56 -0400180 var index uint32
181 for index = 0; index < NumPONPorts; index++ {
182 ranges.IntfIds = append(ranges.IntfIds, index)
183 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530184
Abhilash S.L8ee90712019-04-29 16:24:22 +0530185 var Pool openolt.DeviceInfo_DeviceResourceRanges_Pool
Girish Gowdru0c588b22019-04-23 23:24:56 -0400186 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700187 Pool.Start = devInfo.OnuIdStart
188 Pool.End = devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400189 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
cbabuabf02352019-10-15 13:14:56 +0200190 onuPool := Pool
191 ranges.Pools = append(ranges.Pools, &onuPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530192
Girish Gowdru0c588b22019-04-23 23:24:56 -0400193 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700194 Pool.Start = devInfo.AllocIdStart
195 Pool.End = devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400196 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200197 allocPool := Pool
198 ranges.Pools = append(ranges.Pools, &allocPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530199
Girish Gowdru0c588b22019-04-23 23:24:56 -0400200 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700201 Pool.Start = devInfo.GemportIdStart
202 Pool.End = devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400203 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200204 gemPool := Pool
205 ranges.Pools = append(ranges.Pools, &gemPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530206
Girish Gowdru0c588b22019-04-23 23:24:56 -0400207 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700208 Pool.Start = devInfo.FlowIdStart
209 Pool.End = devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400210 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
Abhilash S.L8ee90712019-04-29 16:24:22 +0530211 ranges.Pools = append(ranges.Pools, &Pool)
212 // Add to device info
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700213 devInfo.Ranges = append(devInfo.Ranges, &ranges)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400214 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530215
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700216 // Create a separate Resource Manager instance for each range. This assumes that
Girish Gowdru0c588b22019-04-23 23:24:56 -0400217 // each technology is represented by only a single range
218 var GlobalPONRsrcMgr *ponrmgr.PONResourceManager
219 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700220 for _, TechRange := range devInfo.Ranges {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400221 technology := TechRange.Technology
Girish Kumar2ad402b2020-03-20 19:45:12 +0000222 logger.Debugf("Device info technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400223 Ranges[technology] = TechRange
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700224 RsrcMgrsByTech[technology], err = ponrmgr.NewPONResourceManager(technology, deviceType, deviceID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400225 Backend, ResourceMgr.Host, ResourceMgr.Port)
226 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000227 logger.Errorf("Failed to create pon resource manager instance for technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400228 return nil
229 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700230 // resource_mgrs_by_tech[technology] = resource_mgr
Girish Gowdru0c588b22019-04-23 23:24:56 -0400231 if GlobalPONRsrcMgr == nil {
232 GlobalPONRsrcMgr = RsrcMgrsByTech[technology]
233 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700234 for _, IntfID := range TechRange.IntfIds {
235 ResourceMgr.ResourceMgrs[uint32(IntfID)] = RsrcMgrsByTech[technology]
Girish Gowdru0c588b22019-04-23 23:24:56 -0400236 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700237 // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
npujarec5762e2020-01-01 14:08:48 +0530238 InitializeDeviceResourceRangeAndPool(ctx, RsrcMgrsByTech[technology], GlobalPONRsrcMgr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700239 TechRange, devInfo)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400240 }
241 // After we have initialized resource ranges, initialize the
242 // resource pools accordingly.
243 for _, PONRMgr := range RsrcMgrsByTech {
npujarec5762e2020-01-01 14:08:48 +0530244 _ = PONRMgr.InitDeviceResourcePool(ctx)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400245 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000246 logger.Info("Initialization of resource manager success!")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400247 return &ResourceMgr
Abhilash S.L7f17e402019-03-15 17:40:41 +0530248}
249
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700250// InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply
251// device specific information. If KV doesn't exist
252// or is broader than the device, the device's information will
253// dictate the range limits
npujarec5762e2020-01-01 14:08:48 +0530254func InitializeDeviceResourceRangeAndPool(ctx context.Context, ponRMgr *ponrmgr.PONResourceManager, globalPONRMgr *ponrmgr.PONResourceManager,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700255 techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530256
Girish Gowdru0c588b22019-04-23 23:24:56 -0400257 // init the resource range pool according to the sharing type
Abhilash S.L7f17e402019-03-15 17:40:41 +0530258
Girish Kumar2ad402b2020-03-20 19:45:12 +0000259 logger.Debugf("Resource range pool init for technology %s", ponRMgr.Technology)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700260 // first load from KV profiles
npujarec5762e2020-01-01 14:08:48 +0530261 status := ponRMgr.InitResourceRangesFromKVStore(ctx)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700262 if !status {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000263 logger.Debugf("Failed to load resource ranges from KV store for tech %s", ponRMgr.Technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400264 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530265
Girish Gowdru0c588b22019-04-23 23:24:56 -0400266 /*
267 Then apply device specific information. If KV doesn't exist
Gamze Abakafee36392019-10-03 11:17:24 +0000268 or is broader than the device, the device's information will
Girish Gowdru0c588b22019-04-23 23:24:56 -0400269 dictate the range limits
270 */
Girish Kumar2ad402b2020-03-20 19:45:12 +0000271 logger.Debugw("Using device info to init pon resource ranges", log.Fields{"Tech": ponRMgr.Technology})
Abhilash S.L7f17e402019-03-15 17:40:41 +0530272
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700273 ONUIDStart := devInfo.OnuIdStart
274 ONUIDEnd := devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400275 ONUIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
276 ONUIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700277 AllocIDStart := devInfo.AllocIdStart
278 AllocIDEnd := devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400279 AllocIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
280 AllocIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700281 GEMPortIDStart := devInfo.GemportIdStart
282 GEMPortIDEnd := devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400283 GEMPortIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
284 GEMPortIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700285 FlowIDStart := devInfo.FlowIdStart
286 FlowIDEnd := devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400287 FlowIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
288 FlowIDSharedPoolID := uint32(0)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530289
Girish Gowdru0c588b22019-04-23 23:24:56 -0400290 var FirstIntfPoolID uint32
291 var SharedPoolID uint32
Abhilash S.L7f17e402019-03-15 17:40:41 +0530292
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400293 /*
294 * 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 -0700295 * if resources are shared across interfaces then SharedPoolID is given a positive number.
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400296 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700297 for _, FirstIntfPoolID = range techRange.IntfIds {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400298 // skip the intf id 0
299 if FirstIntfPoolID == 0 {
300 continue
301 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400302 break
303 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530304
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700305 for _, RangePool := range techRange.Pools {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400306 if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400307 SharedPoolID = FirstIntfPoolID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400308 } else if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_SAME_TECH {
309 SharedPoolID = FirstIntfPoolID
310 } else {
311 SharedPoolID = 0
312 }
313 if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID {
314 ONUIDStart = RangePool.Start
315 ONUIDEnd = RangePool.End
316 ONUIDShared = RangePool.Sharing
317 ONUIDSharedPoolID = SharedPoolID
318 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID {
319 AllocIDStart = RangePool.Start
320 AllocIDEnd = RangePool.End
321 AllocIDShared = RangePool.Sharing
322 AllocIDSharedPoolID = SharedPoolID
323 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID {
324 GEMPortIDStart = RangePool.Start
325 GEMPortIDEnd = RangePool.End
326 GEMPortIDShared = RangePool.Sharing
327 GEMPortIDSharedPoolID = SharedPoolID
328 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID {
329 FlowIDStart = RangePool.Start
330 FlowIDEnd = RangePool.End
331 FlowIDShared = RangePool.Sharing
332 FlowIDSharedPoolID = SharedPoolID
333 }
334 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530335
Girish Kumar2ad402b2020-03-20 19:45:12 +0000336 logger.Debugw("Device info init", log.Fields{"technology": techRange.Technology,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400337 "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd, "onu_id_shared_pool_id": ONUIDSharedPoolID,
338 "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd,
339 "alloc_id_shared_pool_id": AllocIDSharedPoolID,
340 "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd,
341 "gemport_id_shared_pool_id": GEMPortIDSharedPoolID,
342 "flow_id_start": FlowIDStart,
343 "flow_id_end_idx": FlowIDEnd,
344 "flow_id_shared_pool_id": FlowIDSharedPoolID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700345 "intf_ids": techRange.IntfIds,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400346 "uni_id_start": 0,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700347 "uni_id_end_idx": 1, /*MaxUNIIDperONU()*/
348 })
Abhilash S.L7f17e402019-03-15 17:40:41 +0530349
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700350 ponRMgr.InitDefaultPONResourceRanges(ONUIDStart, ONUIDEnd, ONUIDSharedPoolID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400351 AllocIDStart, AllocIDEnd, AllocIDSharedPoolID,
352 GEMPortIDStart, GEMPortIDEnd, GEMPortIDSharedPoolID,
353 FlowIDStart, FlowIDEnd, FlowIDSharedPoolID, 0, 1,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700354 devInfo.PonPorts, techRange.IntfIds)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530355
Girish Gowdru0c588b22019-04-23 23:24:56 -0400356 // For global sharing, make sure to refresh both local and global resource manager instances' range
Abhilash S.L7f17e402019-03-15 17:40:41 +0530357
Girish Gowdru0c588b22019-04-23 23:24:56 -0400358 if ONUIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700359 globalPONRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400360 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700361 ponRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
362 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400363 }
364 if AllocIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700365 globalPONRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400366 "", 0, nil)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530367
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700368 ponRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
369 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400370 }
371 if GEMPortIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700372 globalPONRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400373 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700374 ponRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
375 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400376 }
377 if FlowIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700378 globalPONRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400379 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700380 ponRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
381 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400382 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530383
Girish Gowdru0c588b22019-04-23 23:24:56 -0400384 // Make sure loaded range fits the platform bit encoding ranges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700385 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 +0530386}
387
Devmalya Paul495b94a2019-08-27 19:42:00 -0400388// Delete clears used resources for the particular olt device being deleted
npujarec5762e2020-01-01 14:08:48 +0530389func (RsrcMgr *OpenOltResourceMgr) Delete(ctx context.Context) error {
Devmalya Paul495b94a2019-08-27 19:42:00 -0400390 /* TODO
391 def __del__(self):
392 self.log.info("clearing-device-resource-pool")
393 for key, resource_mgr in self.resource_mgrs.iteritems():
394 resource_mgr.clear_device_resource_pool()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530395
Devmalya Paul495b94a2019-08-27 19:42:00 -0400396 def assert_pon_id_limit(self, pon_intf_id):
397 assert pon_intf_id in self.resource_mgrs
Abhilash S.L7f17e402019-03-15 17:40:41 +0530398
Devmalya Paul495b94a2019-08-27 19:42:00 -0400399 def assert_onu_id_limit(self, pon_intf_id, onu_id):
400 self.assert_pon_id_limit(pon_intf_id)
401 self.resource_mgrs[pon_intf_id].assert_resource_limits(onu_id, PONResourceManager.ONU_ID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530402
Devmalya Paul495b94a2019-08-27 19:42:00 -0400403 @property
404 def max_uni_id_per_onu(self):
405 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 +0530406
Devmalya Paul495b94a2019-08-27 19:42:00 -0400407 def assert_uni_id_limit(self, pon_intf_id, onu_id, uni_id):
408 self.assert_onu_id_limit(pon_intf_id, onu_id)
409 self.resource_mgrs[pon_intf_id].assert_resource_limits(uni_id, PONResourceManager.UNI_ID)
410 */
411 for _, rsrcMgr := range RsrcMgr.ResourceMgrs {
npujarec5762e2020-01-01 14:08:48 +0530412 if err := rsrcMgr.ClearDeviceResourcePool(ctx); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000413 logger.Debug("Failed to clear device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400414 return err
415 }
416 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000417 logger.Debug("Cleared device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400418 return nil
419}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530420
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700421// GetONUID returns the available OnuID for the given pon-port
npujarec5762e2020-01-01 14:08:48 +0530422func (RsrcMgr *OpenOltResourceMgr) GetONUID(ctx context.Context, ponIntfID uint32) (uint32, error) {
salmansiddiqui352a45c2019-08-19 10:15:36 +0000423 // Check if Pon Interface ID is present in Resource-manager-map
424 if _, ok := RsrcMgr.ResourceMgrs[ponIntfID]; !ok {
425 err := errors.New("invalid-pon-interface-" + strconv.Itoa(int(ponIntfID)))
426 return 0, err
427 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400428 // Get ONU id for a provided pon interface ID.
npujarec5762e2020-01-01 14:08:48 +0530429 ONUID, err := RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ctx, ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400430 ponrmgr.ONU_ID, 1)
431 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000432 logger.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700433 ponIntfID, ponrmgr.ONU_ID)
cbabuabf02352019-10-15 13:14:56 +0200434 return 0, err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400435 }
436 if ONUID != nil {
npujarec5762e2020-01-01 14:08:48 +0530437 RsrcMgr.ResourceMgrs[ponIntfID].InitResourceMap(ctx, fmt.Sprintf("%d,%d", ponIntfID, ONUID[0]))
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700438 return ONUID[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400439 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530440
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700441 return 0, err // return OnuID 0 on error
Abhilash S.L7f17e402019-03-15 17:40:41 +0530442}
443
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700444// GetFlowIDInfo returns the slice of flow info of the given pon-port
445// Note: For flows which trap from the NNI and not really associated with any particular
446// 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 +0530447func (RsrcMgr *OpenOltResourceMgr) GetFlowIDInfo(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32, flowID uint32) *[]FlowInfo {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530448 var flows []FlowInfo
449
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700450 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530451 if err := RsrcMgr.ResourceMgrs[ponIntfID].GetFlowIDInfo(ctx, FlowPath, flowID, &flows); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000452 logger.Errorw("Error while getting flows from KV store", log.Fields{"flowId": flowID})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530453 return nil
454 }
455 if len(flows) == 0 {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000456 logger.Debugw("No flowInfo found in KV store", log.Fields{"flowPath": FlowPath})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530457 return nil
458 }
459 return &flows
460}
461
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700462// GetCurrentFlowIDsForOnu fetches flow ID from the resource manager
463// Note: For flows which trap from the NNI and not really associated with any particular
464// 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 +0530465func (RsrcMgr *OpenOltResourceMgr) GetCurrentFlowIDsForOnu(ctx context.Context, PONIntfID uint32, ONUID int32, UNIID int32) []uint32 {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700466
Abhilash S.L8ee90712019-04-29 16:24:22 +0530467 FlowPath := fmt.Sprintf("%d,%d,%d", PONIntfID, ONUID, UNIID)
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700468 if mgrs, exist := RsrcMgr.ResourceMgrs[PONIntfID]; exist {
npujarec5762e2020-01-01 14:08:48 +0530469 return mgrs.GetCurrentFlowIDsForOnu(ctx, FlowPath)
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700470 }
471 return nil
Abhilash S.L8ee90712019-04-29 16:24:22 +0530472}
473
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700474// UpdateFlowIDInfo updates flow info for the given pon interface, onu id, and uni id
475// Note: For flows which trap from the NNI and not really associated with any particular
476// 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 +0530477func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDInfo(ctx context.Context, ponIntfID int32, onuID int32, uniID int32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700478 flowID uint32, flowData *[]FlowInfo) error {
479 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530480 return RsrcMgr.ResourceMgrs[uint32(ponIntfID)].UpdateFlowIDInfoForOnu(ctx, FlowPath, flowID, *flowData)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530481}
482
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700483// GetFlowID return flow ID for a given pon interface id, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530484func (RsrcMgr *OpenOltResourceMgr) GetFlowID(ctx context.Context, ponIntfID uint32, ONUID int32, uniID int32,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400485 gemportID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700486 flowStoreCookie uint64,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400487 flowCategory string, vlanPcp ...uint32) (uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530488
Girish Gowdru0c588b22019-04-23 23:24:56 -0400489 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700490 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, ONUID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530491 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(ctx, FlowPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400492 if FlowIDs != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000493 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 -0700494 for _, flowID := range FlowIDs {
npujarec5762e2020-01-01 14:08:48 +0530495 FlowInfo := RsrcMgr.GetFlowIDInfo(ctx, ponIntfID, int32(ONUID), int32(uniID), uint32(flowID))
salmansiddiqui7ac62132019-08-22 03:58:50 +0000496 er := getFlowIDFromFlowInfo(FlowInfo, flowID, gemportID, flowStoreCookie, flowCategory, vlanPcp...)
497 if er == nil {
498 return flowID, er
Abhilash S.L8ee90712019-04-29 16:24:22 +0530499 }
500 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400501 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000502 logger.Debug("No matching flows with flow cookie or flow category, allocating new flowid")
npujarec5762e2020-01-01 14:08:48 +0530503 FlowIDs, err = RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ctx, ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400504 ponrmgr.FLOW_ID, 1)
505 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000506 logger.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700507 ponIntfID, ponrmgr.FLOW_ID)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400508 return uint32(0), err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400509 }
510 if FlowIDs != nil {
npujarec5762e2020-01-01 14:08:48 +0530511 _ = RsrcMgr.ResourceMgrs[ponIntfID].UpdateFlowIDForOnu(ctx, FlowPath, FlowIDs[0], true)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700512 return FlowIDs[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400513 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530514
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700515 return 0, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530516}
517
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700518// GetAllocID return the first Alloc ID for a given pon interface id and onu id and then update the resource map on
519// the KV store with the list of alloc_ids allocated for the pon_intf_onu_id tuple
520// 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 +0530521func (RsrcMgr *OpenOltResourceMgr) GetAllocID(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530522
Girish Gowdru0c588b22019-04-23 23:24:56 -0400523 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700524 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530525 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400526 if AllocID != nil {
527 // Since we support only one alloc_id for the ONU at the moment,
528 // return the first alloc_id in the list, if available, for that
529 // ONU.
Girish Kumar2ad402b2020-03-20 19:45:12 +0000530 logger.Debugw("Retrieved alloc ID from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400531 return AllocID[0]
532 }
npujarec5762e2020-01-01 14:08:48 +0530533 AllocID, err = RsrcMgr.ResourceMgrs[intfID].GetResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400534 ponrmgr.ALLOC_ID, 1)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530535
Girish Gowdru0c588b22019-04-23 23:24:56 -0400536 if AllocID == nil || err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000537 logger.Error("Failed to allocate alloc id")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400538 return 0
539 }
540 // update the resource map on KV store with the list of alloc_id
541 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530542 err = RsrcMgr.ResourceMgrs[intfID].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID, AllocID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400543 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000544 logger.Error("Failed to update Alloc ID")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400545 return 0
546 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000547 logger.Debugw("Allocated new Tcont from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400548 return AllocID[0]
Abhilash S.L7f17e402019-03-15 17:40:41 +0530549}
550
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700551// UpdateAllocIdsForOnu updates alloc ids in kv store for a given pon interface id, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530552func (RsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ctx context.Context, ponPort uint32, onuID uint32, uniID uint32, allocID []uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530553
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700554 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530555 return RsrcMgr.ResourceMgrs[ponPort].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700556 allocID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530557}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700558
559// GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530560func (RsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(ctx context.Context, intfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700561 uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530562
Girish Gowdru0c588b22019-04-23 23:24:56 -0400563 /* Get gem ports for given pon interface , onu id and uni id. */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530564
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700565 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530566 return RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530567}
568
Gamze Abakafee36392019-10-03 11:17:24 +0000569// GetCurrentAllocIDsForOnu returns alloc ids for given pon interface and onu id
npujarec5762e2020-01-01 14:08:48 +0530570func (RsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDsForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530571
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700572 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530573 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400574 if AllocID != nil {
Gamze Abakafee36392019-10-03 11:17:24 +0000575 return AllocID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400576 }
Gamze Abakafee36392019-10-03 11:17:24 +0000577 return []uint32{}
578}
579
580// RemoveAllocIDForOnu removes the alloc id for given pon interface, onu id, uni id and alloc id
npujarec5762e2020-01-01 14:08:48 +0530581func (RsrcMgr *OpenOltResourceMgr) RemoveAllocIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, allocID uint32) {
582 allocIDs := RsrcMgr.GetCurrentAllocIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000583 for i := 0; i < len(allocIDs); i++ {
584 if allocIDs[i] == allocID {
585 allocIDs = append(allocIDs[:i], allocIDs[i+1:]...)
586 break
587 }
588 }
npujarec5762e2020-01-01 14:08:48 +0530589 err := RsrcMgr.UpdateAllocIdsForOnu(ctx, intfID, onuID, uniID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000590 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000591 logger.Errorf("Failed to Remove Alloc Id For Onu. IntfID %d onuID %d uniID %d allocID %d",
Gamze Abakafee36392019-10-03 11:17:24 +0000592 intfID, onuID, uniID, allocID)
593 }
594}
595
596// RemoveGemPortIDForOnu removes the gem port id for given pon interface, onu id, uni id and gem port id
npujarec5762e2020-01-01 14:08:48 +0530597func (RsrcMgr *OpenOltResourceMgr) RemoveGemPortIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, gemPortID uint32) {
598 gemPortIDs := RsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000599 for i := 0; i < len(gemPortIDs); i++ {
600 if gemPortIDs[i] == gemPortID {
601 gemPortIDs = append(gemPortIDs[:i], gemPortIDs[i+1:]...)
602 break
603 }
604 }
npujarec5762e2020-01-01 14:08:48 +0530605 err := RsrcMgr.UpdateGEMPortIDsForOnu(ctx, intfID, onuID, uniID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000606 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000607 logger.Errorf("Failed to Remove Gem Id For Onu. IntfID %d onuID %d uniID %d gemPortId %d",
Gamze Abakafee36392019-10-03 11:17:24 +0000608 intfID, onuID, uniID, gemPortID)
609 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530610}
611
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700612// UpdateGEMportsPonportToOnuMapOnKVStore updates onu and uni id associated with the gem port to the kv store
613// This stored information is used when packet_indication is received and we need to derive the ONU Id for which
614// the packet arrived based on the pon_intf and gemport available in the packet_indication
npujarec5762e2020-01-01 14:08:48 +0530615func (RsrcMgr *OpenOltResourceMgr) UpdateGEMportsPonportToOnuMapOnKVStore(ctx context.Context, gemPorts []uint32, PonPort uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700616 onuID uint32, uniID uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530617
Girish Gowdru0c588b22019-04-23 23:24:56 -0400618 /* Update onu and uni id associated with the gem port to the kv store. */
619 var IntfGEMPortPath string
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700620 Data := fmt.Sprintf("%d %d", onuID, uniID)
621 for _, GEM := range gemPorts {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400622 IntfGEMPortPath = fmt.Sprintf("%d,%d", PonPort, GEM)
623 Val, err := json.Marshal(Data)
624 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000625 logger.Error("failed to Marshal")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400626 return err
627 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700628
npujarec5762e2020-01-01 14:08:48 +0530629 if err = RsrcMgr.KVStore.Put(ctx, IntfGEMPortPath, Val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000630 logger.Errorf("Failed to update resource %s", IntfGEMPortPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400631 return err
632 }
633 }
634 return nil
Abhilash S.L7f17e402019-03-15 17:40:41 +0530635}
636
Gamze Abakafee36392019-10-03 11:17:24 +0000637// RemoveGEMportPonportToOnuMapOnKVStore removes the relationship between the gem port and pon port
npujarec5762e2020-01-01 14:08:48 +0530638func (RsrcMgr *OpenOltResourceMgr) RemoveGEMportPonportToOnuMapOnKVStore(ctx context.Context, GemPort uint32, PonPort uint32) {
Gamze Abakafee36392019-10-03 11:17:24 +0000639 IntfGEMPortPath := fmt.Sprintf("%d,%d", PonPort, GemPort)
npujarec5762e2020-01-01 14:08:48 +0530640 err := RsrcMgr.KVStore.Delete(ctx, IntfGEMPortPath)
Gamze Abakafee36392019-10-03 11:17:24 +0000641 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000642 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 +0000643 }
644}
645
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700646// GetGEMPortID gets gem port id for a particular pon port, onu id and uni id and then update the resource map on
647// the KV store with the list of gemport_id allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530648func (RsrcMgr *OpenOltResourceMgr) GetGEMPortID(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700649 uniID uint32, NumOfPorts uint32) ([]uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530650
Girish Gowdru0c588b22019-04-23 23:24:56 -0400651 /* Get gem port id for a particular pon port, onu id
652 and uni id.
653 */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530654
Girish Gowdru0c588b22019-04-23 23:24:56 -0400655 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700656 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530657
npujarec5762e2020-01-01 14:08:48 +0530658 GEMPortList := RsrcMgr.ResourceMgrs[ponPort].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400659 if GEMPortList != nil {
660 return GEMPortList, nil
661 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530662
npujarec5762e2020-01-01 14:08:48 +0530663 GEMPortList, err = RsrcMgr.ResourceMgrs[ponPort].GetResourceID(ctx, ponPort,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400664 ponrmgr.GEMPORT_ID, NumOfPorts)
665 if err != nil && GEMPortList == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000666 logger.Errorf("Failed to get gem port id for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400667 return nil, err
668 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530669
Girish Gowdru0c588b22019-04-23 23:24:56 -0400670 // update the resource map on KV store with the list of gemport_id
671 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530672 err = RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400673 GEMPortList)
674 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000675 logger.Errorf("Failed to update GEM ports to kv store for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400676 return nil, err
677 }
npujarec5762e2020-01-01 14:08:48 +0530678 _ = RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(ctx, GEMPortList, ponPort,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700679 onuID, uniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400680 return GEMPortList, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530681}
682
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700683// 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 +0530684func (RsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700685 uniID uint32, GEMPortList []uint32) error {
686 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530687 return RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400688 GEMPortList)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530689
690}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700691
692// FreeonuID releases(make free) onu id for a particular pon-port
npujarec5762e2020-01-01 14:08:48 +0530693func (RsrcMgr *OpenOltResourceMgr) FreeonuID(ctx context.Context, intfID uint32, onuID []uint32) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700694
npujarec5762e2020-01-01 14:08:48 +0530695 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID, ponrmgr.ONU_ID, onuID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530696
Girish Gowdru0c588b22019-04-23 23:24:56 -0400697 /* Free onu id for a particular interface.*/
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700698 var IntfonuID string
699 for _, onu := range onuID {
700 IntfonuID = fmt.Sprintf("%d,%d", intfID, onu)
npujarec5762e2020-01-01 14:08:48 +0530701 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfonuID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400702 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530703}
704
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700705// FreeFlowID returns the free flow id for a given interface, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530706func (RsrcMgr *OpenOltResourceMgr) FreeFlowID(ctx context.Context, IntfID uint32, onuID int32,
Devmalya Paul495b94a2019-08-27 19:42:00 -0400707 uniID int32, FlowID uint32) {
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400708 var IntfONUID string
709 var err error
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530710 FlowIds := make([]uint32, 0)
711
712 FlowIds = append(FlowIds, FlowID)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700713 IntfONUID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530714 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(ctx, IntfONUID, FlowID, false)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400715 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000716 logger.Errorw("Failed to Update flow id for", log.Fields{"intf": IntfONUID})
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400717 }
npujarec5762e2020-01-01 14:08:48 +0530718 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(ctx, IntfONUID, FlowID)
719 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.FLOW_ID, FlowIds)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400720}
721
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700722// FreeFlowIDs releases the flow Ids
npujarec5762e2020-01-01 14:08:48 +0530723func (RsrcMgr *OpenOltResourceMgr) FreeFlowIDs(ctx context.Context, IntfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700724 uniID uint32, FlowID []uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530725
npujarec5762e2020-01-01 14:08:48 +0530726 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.FLOW_ID, FlowID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530727
Abhilash S.L8ee90712019-04-29 16:24:22 +0530728 var IntfOnuIDUniID string
Girish Gowdru0c588b22019-04-23 23:24:56 -0400729 var err error
730 for _, flow := range FlowID {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700731 IntfOnuIDUniID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530732 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(ctx, IntfOnuIDUniID, flow, false)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400733 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000734 logger.Errorw("Failed to Update flow id for", log.Fields{"intf": IntfOnuIDUniID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400735 }
npujarec5762e2020-01-01 14:08:48 +0530736 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(ctx, IntfOnuIDUniID, flow)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400737 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530738}
739
Gamze Abakafee36392019-10-03 11:17:24 +0000740// FreeAllocID frees AllocID on the PON resource pool and also frees the allocID association
741// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530742func (RsrcMgr *OpenOltResourceMgr) FreeAllocID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000743 uniID uint32, allocID uint32) {
npujarec5762e2020-01-01 14:08:48 +0530744 RsrcMgr.RemoveAllocIDForOnu(ctx, IntfID, onuID, uniID, allocID)
Gamze Abakafee36392019-10-03 11:17:24 +0000745 allocIDs := make([]uint32, 0)
746 allocIDs = append(allocIDs, allocID)
npujarec5762e2020-01-01 14:08:48 +0530747 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.ALLOC_ID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000748}
749
750// FreeGemPortID frees GemPortID on the PON resource pool and also frees the gemPortID association
751// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530752func (RsrcMgr *OpenOltResourceMgr) FreeGemPortID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000753 uniID uint32, gemPortID uint32) {
npujarec5762e2020-01-01 14:08:48 +0530754 RsrcMgr.RemoveGemPortIDForOnu(ctx, IntfID, onuID, uniID, gemPortID)
Gamze Abakafee36392019-10-03 11:17:24 +0000755 gemPortIDs := make([]uint32, 0)
756 gemPortIDs = append(gemPortIDs, gemPortID)
npujarec5762e2020-01-01 14:08:48 +0530757 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.GEMPORT_ID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000758}
759
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700760// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id, and the clears the
761// resource map and the onuID associated with (pon_intf_id, gemport_id) tuple,
npujarec5762e2020-01-01 14:08:48 +0530762func (RsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530763
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700764 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530765
npujarec5762e2020-01-01 14:08:48 +0530766 AllocIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530767
npujarec5762e2020-01-01 14:08:48 +0530768 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400769 ponrmgr.ALLOC_ID,
770 AllocIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530771
npujarec5762e2020-01-01 14:08:48 +0530772 GEMPortIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
773 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400774 ponrmgr.GEMPORT_ID,
775 GEMPortIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530776
npujarec5762e2020-01-01 14:08:48 +0530777 FlowIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentFlowIDsForOnu(ctx, IntfOnuIDUniID)
778 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400779 ponrmgr.FLOW_ID,
780 FlowIDs)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400781 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
npujarec5762e2020-01-01 14:08:48 +0530782 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400783 // Clear the ONU Id associated with the (pon_intf_id, gemport_id) tuple.
784 for _, GEM := range GEMPortIDs {
npujarec5762e2020-01-01 14:08:48 +0530785 _ = RsrcMgr.KVStore.Delete(ctx, fmt.Sprintf("%d,%d", intfID, GEM))
Girish Gowdru0c588b22019-04-23 23:24:56 -0400786 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530787}
788
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700789// IsFlowCookieOnKVStore checks if the given flow cookie is present on the kv store
790// Returns true if the flow cookie is found, otherwise it returns false
npujarec5762e2020-01-01 14:08:48 +0530791func (RsrcMgr *OpenOltResourceMgr) IsFlowCookieOnKVStore(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700792 flowStoreCookie uint64) bool {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530793
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700794 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530795 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(ctx, FlowPath)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400796 if FlowIDs != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000797 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 -0700798 for _, flowID := range FlowIDs {
npujarec5762e2020-01-01 14:08:48 +0530799 FlowInfo := RsrcMgr.GetFlowIDInfo(ctx, ponIntfID, int32(onuID), int32(uniID), uint32(flowID))
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400800 if FlowInfo != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000801 logger.Debugw("Found flows", log.Fields{"flows": *FlowInfo, "flowId": flowID})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400802 for _, Info := range *FlowInfo {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700803 if Info.FlowStoreCookie == flowStoreCookie {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000804 logger.Debug("Found flow matching with flowStore cookie", log.Fields{"flowId": flowID, "flowStoreCookie": flowStoreCookie})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400805 return true
806 }
807 }
808 }
809 }
810 }
811 return false
812}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400813
salmansiddiqui7ac62132019-08-22 03:58:50 +0000814// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000815// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530816func (RsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) []uint32 {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000817 Path := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000818 var Data []uint32
npujarec5762e2020-01-01 14:08:48 +0530819 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400820 if err == nil {
821 if Value != nil {
822 Val, err := kvstore.ToByte(Value.Value)
823 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000824 logger.Errorw("Failed to convert into byte array", log.Fields{"error": err})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400825 return Data
826 }
827 if err = json.Unmarshal(Val, &Data); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000828 logger.Error("Failed to unmarshal", log.Fields{"error": err})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400829 return Data
830 }
831 }
832 } else {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000833 logger.Errorf("Failed to get TP id from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400834 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000835 logger.Debugf("Getting TP id %d from path %s", Data, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400836 return Data
837
838}
839
Gamze Abakafee36392019-10-03 11:17:24 +0000840// RemoveTechProfileIDsForOnu deletes all tech profile ids from the KV-Store for the given onu based on the path
841// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530842func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDsForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) error {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000843 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
npujarec5762e2020-01-01 14:08:48 +0530844 if err := RsrcMgr.KVStore.Delete(ctx, IntfOnuUniID); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000845 logger.Errorw("Failed to delete techprofile id resource in KV store", log.Fields{"path": IntfOnuUniID})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400846 return err
847 }
848 return nil
849}
850
Gamze Abakafee36392019-10-03 11:17:24 +0000851// RemoveTechProfileIDForOnu deletes a specific tech profile id from the KV-Store for the given onu based on the path
852// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530853func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32, TpID uint32) error {
854 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000855 for i, tpIDInList := range tpIDList {
856 if tpIDInList == TpID {
857 tpIDList = append(tpIDList[:i], tpIDList[i+1:]...)
858 }
859 }
860 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
861 Value, err := json.Marshal(tpIDList)
862 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000863 logger.Error("failed to Marshal")
Gamze Abakafee36392019-10-03 11:17:24 +0000864 return err
865 }
npujarec5762e2020-01-01 14:08:48 +0530866 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000867 logger.Errorf("Failed to update resource %s", IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000868 return err
869 }
870 return err
871}
872
873// UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
874// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530875func (RsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32,
salmansiddiqui7ac62132019-08-22 03:58:50 +0000876 UniID uint32, TpID uint32) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400877 var Value []byte
878 var err error
879
salmansiddiqui7ac62132019-08-22 03:58:50 +0000880 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000881
npujarec5762e2020-01-01 14:08:48 +0530882 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000883 for _, value := range tpIDList {
884 if value == TpID {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000885 logger.Debugf("TpID %d is already in tpIdList for the path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000886 return err
887 }
888 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000889 logger.Debugf("updating tp id %d on path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000890 tpIDList = append(tpIDList, TpID)
891 Value, err = json.Marshal(tpIDList)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400892 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000893 logger.Error("failed to Marshal")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400894 return err
895 }
npujarec5762e2020-01-01 14:08:48 +0530896 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000897 logger.Errorf("Failed to update resource %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400898 return err
899 }
900 return err
901}
902
salmansiddiqui7ac62132019-08-22 03:58:50 +0000903// UpdateMeterIDForOnu updates the meter id in the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000904// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +0530905func (RsrcMgr *OpenOltResourceMgr) UpdateMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000906 UniID uint32, TpID uint32, MeterConfig *ofp.OfpMeterConfig) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400907 var Value []byte
908 var err error
909
Gamze Abakafee36392019-10-03 11:17:24 +0000910 IntfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400911 Value, err = json.Marshal(*MeterConfig)
912 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000913 logger.Error("failed to Marshal meter config")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400914 return err
915 }
npujarec5762e2020-01-01 14:08:48 +0530916 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000917 logger.Errorf("Failed to store meter into KV store %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400918 return err
919 }
920 return err
921}
922
Gamze Abakafee36392019-10-03 11:17:24 +0000923// GetMeterIDForOnu fetches the meter id from the kv store for the given onu based on the path
924// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +0530925func (RsrcMgr *OpenOltResourceMgr) GetMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000926 UniID uint32, TpID uint32) (*ofp.OfpMeterConfig, error) {
927 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400928 var meterConfig ofp.OfpMeterConfig
npujarec5762e2020-01-01 14:08:48 +0530929 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400930 if err == nil {
931 if Value != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000932 logger.Debug("Found meter in KV store", log.Fields{"Direction": Direction})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000933 Val, er := kvstore.ToByte(Value.Value)
934 if er != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000935 logger.Errorw("Failed to convert into byte array", log.Fields{"error": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000936 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400937 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000938 if er = json.Unmarshal(Val, &meterConfig); er != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000939 logger.Error("Failed to unmarshal meterconfig", log.Fields{"error": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000940 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400941 }
942 } else {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000943 logger.Debug("meter-does-not-exists-in-KVStore")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400944 return nil, err
945 }
946 } else {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000947 logger.Errorf("Failed to get Meter config from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400948
949 }
950 return &meterConfig, err
951}
952
Gamze Abakafee36392019-10-03 11:17:24 +0000953// RemoveMeterIDForOnu deletes the meter id from the kV-Store for the given onu based on the path
954// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +0530955func (RsrcMgr *OpenOltResourceMgr) RemoveMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000956 UniID uint32, TpID uint32) error {
957 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
npujarec5762e2020-01-01 14:08:48 +0530958 if err := RsrcMgr.KVStore.Delete(ctx, Path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000959 logger.Errorf("Failed to delete meter id %s from kvstore ", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400960 return err
961 }
962 return nil
963}
salmansiddiqui7ac62132019-08-22 03:58:50 +0000964
965func getFlowIDFromFlowInfo(FlowInfo *[]FlowInfo, flowID, gemportID uint32, flowStoreCookie uint64, flowCategory string, vlanPcp ...uint32) error {
966 if FlowInfo != nil {
967 for _, Info := range *FlowInfo {
968 if int32(gemportID) == Info.Flow.GemportId && flowCategory != "" && Info.FlowCategory == flowCategory {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000969 logger.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000970 if Info.FlowCategory == "HSIA_FLOW" && Info.Flow.Classifier.OPbits == vlanPcp[0] {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000971 logger.Debug("Found matching vlan pcp ", log.Fields{"flowId": flowID, "Vlanpcp": vlanPcp[0]})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000972 return nil
973 }
974 }
975 if int32(gemportID) == Info.Flow.GemportId && flowStoreCookie != 0 && Info.FlowStoreCookie == flowStoreCookie {
976 if flowCategory != "" && Info.FlowCategory == flowCategory {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000977 logger.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000978 return nil
979 }
980 }
981 }
982 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000983 logger.Debugw("the flow can be related to a different service", log.Fields{"flow_info": FlowInfo})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000984 return errors.New("invalid flow-info")
985}
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530986
987//AddGemToOnuGemInfo adds gemport to onugem info kvstore
npujarec5762e2020-01-01 14:08:48 +0530988func (RsrcMgr *OpenOltResourceMgr) AddGemToOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, gemPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530989 var onuGemData []OnuGemInfo
990 var err error
991
npujarec5762e2020-01-01 14:08:48 +0530992 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000993 logger.Errorf("failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530994 return err
995 }
996 if len(onuGemData) == 0 {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000997 logger.Errorw("failed to ger Onuid info ", log.Fields{"intfid": intfID, "onuid": onuID})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530998 return err
999 }
1000
1001 for idx, onugem := range onuGemData {
1002 if onugem.OnuID == onuID {
1003 for _, gem := range onuGemData[idx].GemPorts {
1004 if gem == gemPort {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001005 logger.Debugw("Gem already present in onugem info, skpping addition", log.Fields{"gem": gem})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301006 return nil
1007 }
1008 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001009 logger.Debugw("Added gem to onugem info", log.Fields{"gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301010 onuGemData[idx].GemPorts = append(onuGemData[idx].GemPorts, gemPort)
1011 break
1012 }
1013 }
npujarec5762e2020-01-01 14:08:48 +05301014 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301015 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001016 logger.Error("Failed to add onugem to kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301017 return err
1018 }
1019 return err
1020}
1021
1022//GetOnuGemInfo gets onu gem info from the kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301023func (RsrcMgr *OpenOltResourceMgr) GetOnuGemInfo(ctx context.Context, IntfID uint32) ([]OnuGemInfo, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301024 var onuGemData []OnuGemInfo
1025
npujarec5762e2020-01-01 14:08:48 +05301026 if err := RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001027 logger.Errorf("failed to get onuifo for intfid %d", IntfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301028 return nil, err
1029 }
1030
1031 return onuGemData, nil
1032}
1033
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001034// AddOnuGemInfo adds onu info on to the kvstore per interface
1035func (RsrcMgr *OpenOltResourceMgr) AddOnuGemInfo(ctx context.Context, IntfID uint32, onuGem OnuGemInfo) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301036 var onuGemData []OnuGemInfo
1037 var err error
1038
npujarec5762e2020-01-01 14:08:48 +05301039 if err = RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001040 logger.Errorf("failed to get onuifo for intfid %d", IntfID)
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001041 return olterrors.NewErrPersistence("get", "OnuGemInfo", IntfID,
1042 log.Fields{"onuGem": onuGem, "intfID": IntfID}, err)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301043 }
1044 onuGemData = append(onuGemData, onuGem)
npujarec5762e2020-01-01 14:08:48 +05301045 err = RsrcMgr.ResourceMgrs[IntfID].AddOnuGemInfo(ctx, IntfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301046 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001047 logger.Error("Failed to add onugem to kv store")
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001048 return olterrors.NewErrPersistence("set", "OnuGemInfo", IntfID,
1049 log.Fields{"onuGemData": onuGemData, "intfID": IntfID}, err)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301050 }
1051
Girish Kumar2ad402b2020-03-20 19:45:12 +00001052 logger.Debugw("added onu to onugeminfo", log.Fields{"intf": IntfID, "onugem": onuGem})
Andrea Campanellab83b39d2020-03-30 11:41:16 +02001053 return nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301054}
1055
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001056// UpdateOnuGemInfo updates Onuinfo on the kvstore per interface
1057func (RsrcMgr *OpenOltResourceMgr) UpdateOnuGemInfo(ctx context.Context, IntfID uint32, onuGem []OnuGemInfo) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301058
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001059 // TODO: VOL-2643
1060 err := RsrcMgr.ResourceMgrs[IntfID].AddOnuGemInfo(ctx, IntfID, onuGem)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301061 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001062 logger.Debugw("persistence-update-failed", log.Fields{
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001063 "interface-id": IntfID,
1064 "gem-info": onuGem,
1065 "error": err})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301066 return err
1067 }
1068
Girish Kumar2ad402b2020-03-20 19:45:12 +00001069 logger.Debugw("updated onugeminfo", log.Fields{"intf": IntfID, "onugem": onuGem})
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001070 return nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301071}
1072
1073// 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 +05301074func (RsrcMgr *OpenOltResourceMgr) AddUniPortToOnuInfo(ctx context.Context, intfID uint32, onuID uint32, portNo uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301075 var onuGemData []OnuGemInfo
1076 var err error
1077
npujarec5762e2020-01-01 14:08:48 +05301078 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001079 logger.Errorf("failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301080 return
1081 }
1082 for idx, onu := range onuGemData {
1083 if onu.OnuID == onuID {
1084 for _, uni := range onu.UniPorts {
1085 if uni == portNo {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001086 logger.Debugw("uni already present in onugem info", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301087 return
1088 }
1089 }
1090 onuGemData[idx].UniPorts = append(onuGemData[idx].UniPorts, portNo)
1091 break
1092 }
1093 }
npujarec5762e2020-01-01 14:08:48 +05301094 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301095 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001096 logger.Errorw("Failed to add uin port in onugem to kv store", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301097 return
1098 }
1099 return
1100}
1101
1102//UpdateGemPortForPktIn updates gemport for pkt in path to kvstore, path being intfid, onuid, portno
npujarec5762e2020-01-01 14:08:48 +05301103func (RsrcMgr *OpenOltResourceMgr) UpdateGemPortForPktIn(ctx context.Context, pktIn PacketInInfoKey, gemPort uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301104
1105 path := fmt.Sprintf(OnuPacketINPath, pktIn.IntfID, pktIn.OnuID, pktIn.LogicalPort)
1106 Value, err := json.Marshal(gemPort)
1107 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001108 logger.Error("Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301109 return
1110 }
npujarec5762e2020-01-01 14:08:48 +05301111 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001112 logger.Errorw("Failed to put to kvstore", log.Fields{"path": path, "value": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301113 return
1114 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001115 logger.Debugw("added gem packet in successfully", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301116
1117 return
1118}
1119
1120// GetGemPortFromOnuPktIn gets the gem port from onu pkt in path, path being intfid, onuid, portno
npujarec5762e2020-01-01 14:08:48 +05301121func (RsrcMgr *OpenOltResourceMgr) GetGemPortFromOnuPktIn(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) (uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301122
1123 var Val []byte
1124 var gemPort uint32
1125
1126 path := fmt.Sprintf(OnuPacketINPath, intfID, onuID, logicalPort)
1127
npujarec5762e2020-01-01 14:08:48 +05301128 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301129 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001130 logger.Errorw("Failed to get from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301131 return uint32(0), err
1132 } else if value == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001133 logger.Debugw("No pkt in gem found", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301134 return uint32(0), nil
1135 }
1136
1137 if Val, err = kvstore.ToByte(value.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001138 logger.Error("Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301139 return uint32(0), err
1140 }
1141 if err = json.Unmarshal(Val, &gemPort); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001142 logger.Error("Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301143 return uint32(0), err
1144 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001145 logger.Debugw("found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301146
1147 return gemPort, nil
1148}
1149
1150// DelGemPortPktIn deletes the gemport from the pkt in path
npujarec5762e2020-01-01 14:08:48 +05301151func (RsrcMgr *OpenOltResourceMgr) DelGemPortPktIn(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301152
1153 path := fmt.Sprintf(OnuPacketINPath, intfID, onuID, logicalPort)
npujarec5762e2020-01-01 14:08:48 +05301154 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001155 logger.Errorf("Falied to remove resource %s", path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301156 return err
1157 }
1158 return nil
1159}
1160
1161// DelOnuGemInfoForIntf deletes the onugem info from kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301162func (RsrcMgr *OpenOltResourceMgr) DelOnuGemInfoForIntf(ctx context.Context, intfID uint32) error {
1163 if err := RsrcMgr.ResourceMgrs[intfID].DelOnuGemInfoForIntf(ctx, intfID); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001164 logger.Errorw("failed to delete onu gem info for", log.Fields{"intfid": intfID})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301165 return err
1166 }
1167 return nil
1168}
1169
1170//GetNNIFromKVStore gets NNi intfids from kvstore. path being per device
npujarec5762e2020-01-01 14:08:48 +05301171func (RsrcMgr *OpenOltResourceMgr) GetNNIFromKVStore(ctx context.Context) ([]uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301172
1173 var nni []uint32
1174 var Val []byte
1175
1176 path := fmt.Sprintf(NnniIntfID)
npujarec5762e2020-01-01 14:08:48 +05301177 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301178 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001179 logger.Error("failed to get data from kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301180 return nil, err
1181 }
1182 if value != nil {
1183 if Val, err = kvstore.ToByte(value.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001184 logger.Error("Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301185 return nil, err
1186 }
1187 if err = json.Unmarshal(Val, &nni); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001188 logger.Error("Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301189 return nil, err
1190 }
1191 }
1192 return nni, err
1193}
1194
1195// AddNNIToKVStore adds Nni interfaces to kvstore, path being per device.
npujarec5762e2020-01-01 14:08:48 +05301196func (RsrcMgr *OpenOltResourceMgr) AddNNIToKVStore(ctx context.Context, nniIntf uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301197 var Value []byte
1198
npujarec5762e2020-01-01 14:08:48 +05301199 nni, err := RsrcMgr.GetNNIFromKVStore(ctx)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301200 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001201 logger.Error("failed to fetch nni interfaces from kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301202 return err
1203 }
1204
1205 path := fmt.Sprintf(NnniIntfID)
1206 nni = append(nni, nniIntf)
1207 Value, err = json.Marshal(nni)
1208 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001209 logger.Error("Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301210 }
npujarec5762e2020-01-01 14:08:48 +05301211 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001212 logger.Errorw("Failed to put to kvstore", log.Fields{"path": path, "value": Value})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301213 return err
1214 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001215 logger.Debugw("added nni to kv successfully", log.Fields{"path": path, "nni": nniIntf})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301216 return nil
1217}
1218
1219// DelNNiFromKVStore deletes nni interface list from kv store.
npujarec5762e2020-01-01 14:08:48 +05301220func (RsrcMgr *OpenOltResourceMgr) DelNNiFromKVStore(ctx context.Context) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301221
1222 path := fmt.Sprintf(NnniIntfID)
1223
npujarec5762e2020-01-01 14:08:48 +05301224 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001225 logger.Errorw("Failed to delete nni interfaces from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301226 return err
1227 }
1228 return nil
1229}
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301230
1231//UpdateFlowIDsForGem updates flow id per gemport
npujarec5762e2020-01-01 14:08:48 +05301232func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDsForGem(ctx context.Context, intf uint32, gem uint32, flowIDs []uint32) error {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301233 var val []byte
1234 path := fmt.Sprintf(FlowIDsForGem, intf)
1235
npujarec5762e2020-01-01 14:08:48 +05301236 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301237 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001238 logger.Error("Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301239 return err
1240 }
1241 if flowsForGem == nil {
1242 flowsForGem = make(map[uint32][]uint32)
1243 }
1244 flowsForGem[gem] = flowIDs
1245 val, err = json.Marshal(flowsForGem)
1246 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001247 logger.Error("Failed to marshal data", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301248 return err
1249 }
npujarec5762e2020-01-01 14:08:48 +05301250 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001251 logger.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301252 return err
1253 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001254 logger.Debugw("added flowid list for gem to kv successfully", log.Fields{"path": path, "flowidlist": flowsForGem[gem]})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301255 return nil
1256}
1257
1258//DeleteFlowIDsForGem deletes the flowID list entry per gem from kvstore.
npujarec5762e2020-01-01 14:08:48 +05301259func (RsrcMgr *OpenOltResourceMgr) DeleteFlowIDsForGem(ctx context.Context, intf uint32, gem uint32) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301260 path := fmt.Sprintf(FlowIDsForGem, intf)
1261 var val []byte
1262
npujarec5762e2020-01-01 14:08:48 +05301263 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301264 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001265 logger.Error("Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301266 return
1267 }
1268 if flowsForGem == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001269 logger.Error("No flowids found ", log.Fields{"intf": intf, "gemport": gem})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301270 return
1271 }
1272 // once we get the flows per gem map from kv , just delete the gem entry from the map
1273 delete(flowsForGem, gem)
1274 // once gem entry is deleted update the kv store.
1275 val, err = json.Marshal(flowsForGem)
1276 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001277 logger.Error("Failed to marshal data", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301278 return
1279 }
npujarec5762e2020-01-01 14:08:48 +05301280 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001281 logger.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301282 return
1283 }
1284 return
1285}
1286
1287//GetFlowIDsGemMapForInterface gets flowids per gemport and interface
npujarec5762e2020-01-01 14:08:48 +05301288func (RsrcMgr *OpenOltResourceMgr) GetFlowIDsGemMapForInterface(ctx context.Context, intf uint32) (map[uint32][]uint32, error) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301289 path := fmt.Sprintf(FlowIDsForGem, intf)
1290 var flowsForGem map[uint32][]uint32
1291 var val []byte
1292
npujarec5762e2020-01-01 14:08:48 +05301293 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301294 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001295 logger.Error("failed to get data from kv store")
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301296 return nil, err
1297 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001298 if value != nil && value.Value != nil {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301299 if val, err = kvstore.ToByte(value.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001300 logger.Error("Failed to convert to byte array ", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301301 return nil, err
1302 }
1303 if err = json.Unmarshal(val, &flowsForGem); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001304 logger.Error("Failed to unmarshall", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301305 return nil, err
1306 }
1307 }
1308 return flowsForGem, nil
1309}
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301310
1311//DeleteIntfIDGempMapPath deletes the intf id path used to store flow ids per gem to kvstore.
npujarec5762e2020-01-01 14:08:48 +05301312func (RsrcMgr *OpenOltResourceMgr) DeleteIntfIDGempMapPath(ctx context.Context, intf uint32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301313 path := fmt.Sprintf(FlowIDsForGem, intf)
npujarec5762e2020-01-01 14:08:48 +05301314 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001315 logger.Errorw("Failed to delete nni interfaces from kv store", log.Fields{"path": path})
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301316 return
1317 }
1318 return
1319}
1320
1321// RemoveResourceMap Clear resource map associated with (intfid, onuid, uniid) tuple.
npujarec5762e2020-01-01 14:08:48 +05301322func (RsrcMgr *OpenOltResourceMgr) RemoveResourceMap(ctx context.Context, intfID uint32, onuID int32, uniID int32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301323 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +05301324 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301325}
Esin Karamanccb714b2019-11-29 15:02:06 +00001326
1327//GetMcastQueuePerInterfaceMap gets multicast queue info per pon interface
npujarec5762e2020-01-01 14:08:48 +05301328func (RsrcMgr *OpenOltResourceMgr) GetMcastQueuePerInterfaceMap(ctx context.Context) (map[uint32][]uint32, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001329 path := fmt.Sprintf(McastQueuesForIntf)
1330 var mcastQueueToIntfMap map[uint32][]uint32
1331 var val []byte
1332
npujarec5762e2020-01-01 14:08:48 +05301333 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001334 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001335 logger.Error("failed to get data from kv store")
Esin Karamanccb714b2019-11-29 15:02:06 +00001336 return nil, err
1337 }
1338 if kvPair != nil && kvPair.Value != nil {
1339 if val, err = kvstore.ToByte(kvPair.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001340 logger.Error("Failed to convert to byte array ", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001341 return nil, err
1342 }
1343 if err = json.Unmarshal(val, &mcastQueueToIntfMap); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001344 logger.Error("Failed to unmarshall ", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001345 return nil, err
1346 }
1347 }
1348 return mcastQueueToIntfMap, nil
1349}
1350
1351//AddMcastQueueForIntf adds multicast queue for pon interface
npujarec5762e2020-01-01 14:08:48 +05301352func (RsrcMgr *OpenOltResourceMgr) AddMcastQueueForIntf(ctx context.Context, intf uint32, gem uint32, servicePriority uint32) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001353 var val []byte
1354 path := fmt.Sprintf(McastQueuesForIntf)
1355
npujarec5762e2020-01-01 14:08:48 +05301356 mcastQueues, err := RsrcMgr.GetMcastQueuePerInterfaceMap(ctx)
Esin Karamanccb714b2019-11-29 15:02:06 +00001357 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001358 logger.Errorw("Failed to get multicast queue info for interface", log.Fields{"error": err, "intf": intf})
Esin Karamanccb714b2019-11-29 15:02:06 +00001359 return err
1360 }
1361 if mcastQueues == nil {
1362 mcastQueues = make(map[uint32][]uint32)
1363 }
1364 mcastQueues[intf] = []uint32{gem, servicePriority}
1365 if val, err = json.Marshal(mcastQueues); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001366 logger.Errorw("Failed to marshal data", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001367 return err
1368 }
npujarec5762e2020-01-01 14:08:48 +05301369 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001370 logger.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Esin Karamanccb714b2019-11-29 15:02:06 +00001371 return err
1372 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001373 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 +00001374 return nil
1375}
1376
1377//AddFlowGroupToKVStore adds flow group into KV store
npujarec5762e2020-01-01 14:08:48 +05301378func (RsrcMgr *OpenOltResourceMgr) AddFlowGroupToKVStore(ctx context.Context, groupEntry *ofp.OfpGroupEntry, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001379 var Value []byte
1380 var err error
1381 var path string
1382 if cached {
1383 path = fmt.Sprintf(FlowGroupCached, groupEntry.Desc.GroupId)
1384 } else {
1385 path = fmt.Sprintf(FlowGroup, groupEntry.Desc.GroupId)
1386 }
1387 //build group info object
1388 var outPorts []uint32
1389 for _, ofBucket := range groupEntry.Desc.Buckets {
1390 for _, ofAction := range ofBucket.Actions {
1391 if ofAction.Type == ofp.OfpActionType_OFPAT_OUTPUT {
1392 outPorts = append(outPorts, ofAction.GetOutput().Port)
1393 }
1394 }
1395 }
1396 groupInfo := GroupInfo{
1397 GroupID: groupEntry.Desc.GroupId,
1398 OutPorts: outPorts,
1399 }
1400
1401 Value, err = json.Marshal(groupInfo)
1402
1403 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001404 logger.Error("failed to Marshal flow group object")
Esin Karamanccb714b2019-11-29 15:02:06 +00001405 return err
1406 }
1407
npujarec5762e2020-01-01 14:08:48 +05301408 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001409 logger.Errorf("Failed to update resource %s", path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001410 return err
1411 }
1412 return nil
1413}
1414
1415//RemoveFlowGroupFromKVStore removes flow group from KV store
npujarec5762e2020-01-01 14:08:48 +05301416func (RsrcMgr *OpenOltResourceMgr) RemoveFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) bool {
Esin Karamanccb714b2019-11-29 15:02:06 +00001417 var path string
1418 if cached {
1419 path = fmt.Sprintf(FlowGroupCached, groupID)
1420 } else {
1421 path = fmt.Sprintf(FlowGroup, groupID)
1422 }
npujarec5762e2020-01-01 14:08:48 +05301423 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001424 logger.Errorf("Failed to remove resource %s due to %s", path, err)
Esin Karamanccb714b2019-11-29 15:02:06 +00001425 return false
1426 }
1427 return true
1428}
1429
1430//GetFlowGroupFromKVStore fetches flow group from the KV store. Returns (false, {} error) if any problem occurs during
1431//fetching the data. Returns (true, groupInfo, nil) if the group is fetched successfully.
1432// Returns (false, {}, nil) if the group does not exists in the KV store.
npujarec5762e2020-01-01 14:08:48 +05301433func (RsrcMgr *OpenOltResourceMgr) GetFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) (bool, GroupInfo, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001434 var groupInfo GroupInfo
1435 var path string
1436 if cached {
1437 path = fmt.Sprintf(FlowGroupCached, groupID)
1438 } else {
1439 path = fmt.Sprintf(FlowGroup, groupID)
1440 }
npujarec5762e2020-01-01 14:08:48 +05301441 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001442 if err != nil {
1443 return false, groupInfo, err
1444 }
1445 if kvPair != nil && kvPair.Value != nil {
1446 Val, err := kvstore.ToByte(kvPair.Value)
1447 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001448 logger.Errorw("Failed to convert flow group into byte array", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001449 return false, groupInfo, err
1450 }
1451 if err = json.Unmarshal(Val, &groupInfo); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001452 logger.Errorw("Failed to unmarshal", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001453 return false, groupInfo, err
1454 }
1455 return true, groupInfo, nil
1456 }
1457 return false, groupInfo, nil
1458}