blob: 1db98b9f6e668aaf78ded2340a1b99f07461b715 [file] [log] [blame]
Abhilash S.L7f17e402019-03-15 17:40:41 +05301/*
2 * Copyright 2019-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070017//Package resourcemanager provides the utility for managing resources
manikkaraj kbf256be2019-03-25 00:13:48 +053018package resourcemanager
Abhilash S.L7f17e402019-03-15 17:40:41 +053019
20import (
npujarec5762e2020-01-01 14:08:48 +053021 "context"
Girish Gowdru0c588b22019-04-23 23:24:56 -040022 "encoding/json"
23 "errors"
24 "fmt"
25 "strconv"
26 "strings"
Abhilash S.L7f17e402019-03-15 17:40:41 +053027
Esin Karamanccb714b2019-11-29 15:02:06 +000028 "github.com/opencord/voltha-lib-go/v3/pkg/db"
29 "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
30 "github.com/opencord/voltha-lib-go/v3/pkg/log"
31 ponrmgr "github.com/opencord/voltha-lib-go/v3/pkg/ponresourcemanager"
32 ofp "github.com/opencord/voltha-protos/v3/go/openflow_13"
33 "github.com/opencord/voltha-protos/v3/go/openolt"
Abhilash S.L7f17e402019-03-15 17:40:41 +053034)
35
salmansiddiqui7ac62132019-08-22 03:58:50 +000036const (
37 // KvstoreTimeout specifies the time out for KV Store Connection
38 KvstoreTimeout = 5
39 // BasePathKvStore - service/voltha/openolt/<device_id>
40 BasePathKvStore = "service/voltha/openolt/{%s}"
Gamze Abakafee36392019-10-03 11:17:24 +000041 // TpIDPathSuffix - <(pon_id, onu_id, uni_id)>/tp_id
42 TpIDPathSuffix = "{%d,%d,%d}/tp_id"
43 //MeterIDPathSuffix - <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
44 MeterIDPathSuffix = "{%d,%d,%d}/{%d}/meter_id/{%s}"
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053045 //NnniIntfID - nniintfids
46 NnniIntfID = "nniintfids"
47 // OnuPacketINPath path on the kvstore to store packetin gemport,which will be used for packetin, pcketout
48 //format: onu_packetin/<intfid>,<onuid>,<logicalport>
49 OnuPacketINPath = "onu_packetin/{%d,%d,%d}"
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +053050 //FlowIDsForGem flowids_per_gem/<intfid>
51 FlowIDsForGem = "flowids_per_gem/{%d}"
Esin Karamanccb714b2019-11-29 15:02:06 +000052 //McastQueuesForIntf multicast queues for pon interfaces
53 McastQueuesForIntf = "mcast_qs_for_int"
54 //FlowGroup flow_groups/<flow_group_id>
55 // A group is stored under this path on the KV store after it has been installed to the device.
56 // It should also be deleted after it has been removed from the device accordingly.
57 FlowGroup = "flow_groups/{%d}"
58 //FlowGroupCached flow_groups_cached/<flow_group_id>
59 // When a group add request received, we create the group without setting any members to it since we cannot
60 // set any members to a group until it is associated with a multicast flow. It is a BAL limitation.
61 // When the related multicast flow has been created we perform set members operation for the group.
62 // That is why we need to keep the members of a group until the multicast flow creation request comes.
63 // We preserve the groups under "FlowGroupsCached" directory in the KV store temporarily. Having set members,
64 // we remove the group from the cached group store.
65 FlowGroupCached = "flow_groups_cached/{%d}"
salmansiddiqui7ac62132019-08-22 03:58:50 +000066)
Abhilash S.L7f17e402019-03-15 17:40:41 +053067
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070068// FlowInfo holds the flow information
Abhilash S.L8ee90712019-04-29 16:24:22 +053069type FlowInfo struct {
70 Flow *openolt.Flow
71 FlowStoreCookie uint64
72 FlowCategory string
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053073 LogicalFlowID uint64
74}
75
76// OnuGemInfo holds onu information along with gem port list and uni port list
77type OnuGemInfo struct {
78 OnuID uint32
79 SerialNumber string
80 IntfID uint32
81 GemPorts []uint32
82 UniPorts []uint32
83}
84
85// PacketInInfoKey is the key for packet in gemport
86type PacketInInfoKey struct {
87 IntfID uint32
88 OnuID uint32
89 LogicalPort uint32
Abhilash S.L8ee90712019-04-29 16:24:22 +053090}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070091
Esin Karamanccb714b2019-11-29 15:02:06 +000092// GroupInfo holds group information
93type GroupInfo struct {
94 GroupID uint32
95 OutPorts []uint32
96}
97
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070098// OpenOltResourceMgr holds resource related information as provided below for each field
Abhilash S.L7f17e402019-03-15 17:40:41 +053099type OpenOltResourceMgr struct {
sbarbaria8910ba2019-11-05 10:12:23 -0500100 DeviceID string // OLT device id
101 HostAndPort string // Host and port of the kv store to connect to
102 Args string // args
103 KVStore *db.Backend // backend kv store connection handle
Girish Gowdru0c588b22019-04-23 23:24:56 -0400104 DeviceType string
105 Host string // Host ip of the kv store
106 Port int // port of the kv store
107 DevInfo *openolt.DeviceInfo // device information
108 // array of pon resource managers per interface technology
109 ResourceMgrs map[uint32]*ponrmgr.PONResourceManager
Abhilash S.L7f17e402019-03-15 17:40:41 +0530110}
111
Manikkaraj kb1d51442019-07-23 10:41:02 -0400112func newKVClient(storeType string, address string, timeout uint32) (kvstore.Client, error) {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000113 logger.Infow("kv-store-type", log.Fields{"store": storeType})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400114 switch storeType {
115 case "consul":
116 return kvstore.NewConsulClient(address, int(timeout))
117 case "etcd":
118 return kvstore.NewEtcdClient(address, int(timeout))
119 }
120 return nil, errors.New("unsupported-kv-store")
Abhilash S.L7f17e402019-03-15 17:40:41 +0530121}
122
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700123// SetKVClient sets the KV client and return a kv backend
sbarbaria8910ba2019-11-05 10:12:23 -0500124func SetKVClient(backend string, Host string, Port int, DeviceID string) *db.Backend {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400125 addr := Host + ":" + strconv.Itoa(Port)
126 // TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
127 // issue between kv store and backend , core is not calling NewBackend directly
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700128 kvClient, err := newKVClient(backend, addr, KvstoreTimeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400129 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000130 logger.Fatalw("Failed to init KV client\n", log.Fields{"err": err})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400131 return nil
132 }
sbarbaria8910ba2019-11-05 10:12:23 -0500133 kvbackend := &db.Backend{
Girish Gowdru0c588b22019-04-23 23:24:56 -0400134 Client: kvClient,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700135 StoreType: backend,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400136 Host: Host,
137 Port: Port,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700138 Timeout: KvstoreTimeout,
139 PathPrefix: fmt.Sprintf(BasePathKvStore, DeviceID)}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530140
Girish Gowdru0c588b22019-04-23 23:24:56 -0400141 return kvbackend
Abhilash S.L7f17e402019-03-15 17:40:41 +0530142}
143
Gamze Abakafee36392019-10-03 11:17:24 +0000144// NewResourceMgr init a New resource manager instance which in turn instantiates pon resource manager
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700145// instances according to technology. Initializes the default resource ranges for all
146// the resources.
npujarec5762e2020-01-01 14:08:48 +0530147func NewResourceMgr(ctx context.Context, deviceID string, KVStoreHostPort string, kvStoreType string, deviceType string, devInfo *openolt.DeviceInfo) *OpenOltResourceMgr {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400148 var ResourceMgr OpenOltResourceMgr
Girish Kumar2ad402b2020-03-20 19:45:12 +0000149 logger.Debugf("Init new resource manager , host_port: %s, deviceid: %s", KVStoreHostPort, deviceID)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530150 ResourceMgr.HostAndPort = KVStoreHostPort
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700151 ResourceMgr.DeviceType = deviceType
152 ResourceMgr.DevInfo = devInfo
153 IPPort := strings.Split(KVStoreHostPort, ":")
154 ResourceMgr.Host = IPPort[0]
155 ResourceMgr.Port, _ = strconv.Atoi(IPPort[1])
Abhilash S.L7f17e402019-03-15 17:40:41 +0530156
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700157 Backend := kvStoreType
Girish Gowdru0c588b22019-04-23 23:24:56 -0400158 ResourceMgr.KVStore = SetKVClient(Backend, ResourceMgr.Host,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700159 ResourceMgr.Port, deviceID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400160 if ResourceMgr.KVStore == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000161 logger.Error("Failed to setup KV store")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400162 }
163 Ranges := make(map[string]*openolt.DeviceInfo_DeviceResourceRanges)
164 RsrcMgrsByTech := make(map[string]*ponrmgr.PONResourceManager)
165 ResourceMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530166
Girish Gowdru0c588b22019-04-23 23:24:56 -0400167 // TODO self.args = registry('main').get_args()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530168
Girish Gowdru0c588b22019-04-23 23:24:56 -0400169 /*
170 If a legacy driver returns protobuf without any ranges,s synthesize one from
Gamze Abakafee36392019-10-03 11:17:24 +0000171 the legacy global per-device information. This, in theory, is temporary until
Girish Gowdru0c588b22019-04-23 23:24:56 -0400172 the legacy drivers are upgrade to support pool ranges.
173 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700174 if devInfo.Ranges == nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400175 var ranges openolt.DeviceInfo_DeviceResourceRanges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700176 ranges.Technology = devInfo.GetTechnology()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530177
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700178 NumPONPorts := devInfo.GetPonPorts()
Girish Gowdru0c588b22019-04-23 23:24:56 -0400179 var index uint32
180 for index = 0; index < NumPONPorts; index++ {
181 ranges.IntfIds = append(ranges.IntfIds, index)
182 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530183
Abhilash S.L8ee90712019-04-29 16:24:22 +0530184 var Pool openolt.DeviceInfo_DeviceResourceRanges_Pool
Girish Gowdru0c588b22019-04-23 23:24:56 -0400185 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700186 Pool.Start = devInfo.OnuIdStart
187 Pool.End = devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400188 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
cbabuabf02352019-10-15 13:14:56 +0200189 onuPool := Pool
190 ranges.Pools = append(ranges.Pools, &onuPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530191
Girish Gowdru0c588b22019-04-23 23:24:56 -0400192 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700193 Pool.Start = devInfo.AllocIdStart
194 Pool.End = devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400195 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200196 allocPool := Pool
197 ranges.Pools = append(ranges.Pools, &allocPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530198
Girish Gowdru0c588b22019-04-23 23:24:56 -0400199 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700200 Pool.Start = devInfo.GemportIdStart
201 Pool.End = devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400202 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200203 gemPool := Pool
204 ranges.Pools = append(ranges.Pools, &gemPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530205
Girish Gowdru0c588b22019-04-23 23:24:56 -0400206 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700207 Pool.Start = devInfo.FlowIdStart
208 Pool.End = devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400209 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
Abhilash S.L8ee90712019-04-29 16:24:22 +0530210 ranges.Pools = append(ranges.Pools, &Pool)
211 // Add to device info
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700212 devInfo.Ranges = append(devInfo.Ranges, &ranges)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400213 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530214
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700215 // Create a separate Resource Manager instance for each range. This assumes that
Girish Gowdru0c588b22019-04-23 23:24:56 -0400216 // each technology is represented by only a single range
217 var GlobalPONRsrcMgr *ponrmgr.PONResourceManager
218 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700219 for _, TechRange := range devInfo.Ranges {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400220 technology := TechRange.Technology
Girish Kumar2ad402b2020-03-20 19:45:12 +0000221 logger.Debugf("Device info technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400222 Ranges[technology] = TechRange
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700223 RsrcMgrsByTech[technology], err = ponrmgr.NewPONResourceManager(technology, deviceType, deviceID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400224 Backend, ResourceMgr.Host, ResourceMgr.Port)
225 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000226 logger.Errorf("Failed to create pon resource manager instance for technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400227 return nil
228 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700229 // resource_mgrs_by_tech[technology] = resource_mgr
Girish Gowdru0c588b22019-04-23 23:24:56 -0400230 if GlobalPONRsrcMgr == nil {
231 GlobalPONRsrcMgr = RsrcMgrsByTech[technology]
232 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700233 for _, IntfID := range TechRange.IntfIds {
234 ResourceMgr.ResourceMgrs[uint32(IntfID)] = RsrcMgrsByTech[technology]
Girish Gowdru0c588b22019-04-23 23:24:56 -0400235 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700236 // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
npujarec5762e2020-01-01 14:08:48 +0530237 InitializeDeviceResourceRangeAndPool(ctx, RsrcMgrsByTech[technology], GlobalPONRsrcMgr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700238 TechRange, devInfo)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400239 }
240 // After we have initialized resource ranges, initialize the
241 // resource pools accordingly.
242 for _, PONRMgr := range RsrcMgrsByTech {
npujarec5762e2020-01-01 14:08:48 +0530243 _ = PONRMgr.InitDeviceResourcePool(ctx)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400244 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000245 logger.Info("Initialization of resource manager success!")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400246 return &ResourceMgr
Abhilash S.L7f17e402019-03-15 17:40:41 +0530247}
248
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700249// InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply
250// device specific information. If KV doesn't exist
251// or is broader than the device, the device's information will
252// dictate the range limits
npujarec5762e2020-01-01 14:08:48 +0530253func InitializeDeviceResourceRangeAndPool(ctx context.Context, ponRMgr *ponrmgr.PONResourceManager, globalPONRMgr *ponrmgr.PONResourceManager,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700254 techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530255
Girish Gowdru0c588b22019-04-23 23:24:56 -0400256 // init the resource range pool according to the sharing type
Abhilash S.L7f17e402019-03-15 17:40:41 +0530257
Girish Kumar2ad402b2020-03-20 19:45:12 +0000258 logger.Debugf("Resource range pool init for technology %s", ponRMgr.Technology)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700259 // first load from KV profiles
npujarec5762e2020-01-01 14:08:48 +0530260 status := ponRMgr.InitResourceRangesFromKVStore(ctx)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700261 if !status {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000262 logger.Debugf("Failed to load resource ranges from KV store for tech %s", ponRMgr.Technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400263 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530264
Girish Gowdru0c588b22019-04-23 23:24:56 -0400265 /*
266 Then apply device specific information. If KV doesn't exist
Gamze Abakafee36392019-10-03 11:17:24 +0000267 or is broader than the device, the device's information will
Girish Gowdru0c588b22019-04-23 23:24:56 -0400268 dictate the range limits
269 */
Girish Kumar2ad402b2020-03-20 19:45:12 +0000270 logger.Debugw("Using device info to init pon resource ranges", log.Fields{"Tech": ponRMgr.Technology})
Abhilash S.L7f17e402019-03-15 17:40:41 +0530271
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700272 ONUIDStart := devInfo.OnuIdStart
273 ONUIDEnd := devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400274 ONUIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
275 ONUIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700276 AllocIDStart := devInfo.AllocIdStart
277 AllocIDEnd := devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400278 AllocIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
279 AllocIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700280 GEMPortIDStart := devInfo.GemportIdStart
281 GEMPortIDEnd := devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400282 GEMPortIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
283 GEMPortIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700284 FlowIDStart := devInfo.FlowIdStart
285 FlowIDEnd := devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400286 FlowIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
287 FlowIDSharedPoolID := uint32(0)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530288
Girish Gowdru0c588b22019-04-23 23:24:56 -0400289 var FirstIntfPoolID uint32
290 var SharedPoolID uint32
Abhilash S.L7f17e402019-03-15 17:40:41 +0530291
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400292 /*
293 * 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 -0700294 * if resources are shared across interfaces then SharedPoolID is given a positive number.
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400295 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700296 for _, FirstIntfPoolID = range techRange.IntfIds {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400297 // skip the intf id 0
298 if FirstIntfPoolID == 0 {
299 continue
300 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400301 break
302 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530303
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700304 for _, RangePool := range techRange.Pools {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400305 if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400306 SharedPoolID = FirstIntfPoolID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400307 } else if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_SAME_TECH {
308 SharedPoolID = FirstIntfPoolID
309 } else {
310 SharedPoolID = 0
311 }
312 if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID {
313 ONUIDStart = RangePool.Start
314 ONUIDEnd = RangePool.End
315 ONUIDShared = RangePool.Sharing
316 ONUIDSharedPoolID = SharedPoolID
317 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID {
318 AllocIDStart = RangePool.Start
319 AllocIDEnd = RangePool.End
320 AllocIDShared = RangePool.Sharing
321 AllocIDSharedPoolID = SharedPoolID
322 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID {
323 GEMPortIDStart = RangePool.Start
324 GEMPortIDEnd = RangePool.End
325 GEMPortIDShared = RangePool.Sharing
326 GEMPortIDSharedPoolID = SharedPoolID
327 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID {
328 FlowIDStart = RangePool.Start
329 FlowIDEnd = RangePool.End
330 FlowIDShared = RangePool.Sharing
331 FlowIDSharedPoolID = SharedPoolID
332 }
333 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530334
Girish Kumar2ad402b2020-03-20 19:45:12 +0000335 logger.Debugw("Device info init", log.Fields{"technology": techRange.Technology,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400336 "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd, "onu_id_shared_pool_id": ONUIDSharedPoolID,
337 "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd,
338 "alloc_id_shared_pool_id": AllocIDSharedPoolID,
339 "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd,
340 "gemport_id_shared_pool_id": GEMPortIDSharedPoolID,
341 "flow_id_start": FlowIDStart,
342 "flow_id_end_idx": FlowIDEnd,
343 "flow_id_shared_pool_id": FlowIDSharedPoolID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700344 "intf_ids": techRange.IntfIds,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400345 "uni_id_start": 0,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700346 "uni_id_end_idx": 1, /*MaxUNIIDperONU()*/
347 })
Abhilash S.L7f17e402019-03-15 17:40:41 +0530348
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700349 ponRMgr.InitDefaultPONResourceRanges(ONUIDStart, ONUIDEnd, ONUIDSharedPoolID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400350 AllocIDStart, AllocIDEnd, AllocIDSharedPoolID,
351 GEMPortIDStart, GEMPortIDEnd, GEMPortIDSharedPoolID,
352 FlowIDStart, FlowIDEnd, FlowIDSharedPoolID, 0, 1,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700353 devInfo.PonPorts, techRange.IntfIds)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530354
Girish Gowdru0c588b22019-04-23 23:24:56 -0400355 // For global sharing, make sure to refresh both local and global resource manager instances' range
Abhilash S.L7f17e402019-03-15 17:40:41 +0530356
Girish Gowdru0c588b22019-04-23 23:24:56 -0400357 if ONUIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700358 globalPONRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400359 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700360 ponRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
361 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400362 }
363 if AllocIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700364 globalPONRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400365 "", 0, nil)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530366
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700367 ponRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
368 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400369 }
370 if GEMPortIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700371 globalPONRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400372 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700373 ponRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
374 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400375 }
376 if FlowIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700377 globalPONRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400378 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700379 ponRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
380 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400381 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530382
Girish Gowdru0c588b22019-04-23 23:24:56 -0400383 // Make sure loaded range fits the platform bit encoding ranges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700384 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 +0530385}
386
Devmalya Paul495b94a2019-08-27 19:42:00 -0400387// Delete clears used resources for the particular olt device being deleted
npujarec5762e2020-01-01 14:08:48 +0530388func (RsrcMgr *OpenOltResourceMgr) Delete(ctx context.Context) error {
Devmalya Paul495b94a2019-08-27 19:42:00 -0400389 /* TODO
390 def __del__(self):
391 self.log.info("clearing-device-resource-pool")
392 for key, resource_mgr in self.resource_mgrs.iteritems():
393 resource_mgr.clear_device_resource_pool()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530394
Devmalya Paul495b94a2019-08-27 19:42:00 -0400395 def assert_pon_id_limit(self, pon_intf_id):
396 assert pon_intf_id in self.resource_mgrs
Abhilash S.L7f17e402019-03-15 17:40:41 +0530397
Devmalya Paul495b94a2019-08-27 19:42:00 -0400398 def assert_onu_id_limit(self, pon_intf_id, onu_id):
399 self.assert_pon_id_limit(pon_intf_id)
400 self.resource_mgrs[pon_intf_id].assert_resource_limits(onu_id, PONResourceManager.ONU_ID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530401
Devmalya Paul495b94a2019-08-27 19:42:00 -0400402 @property
403 def max_uni_id_per_onu(self):
404 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 +0530405
Devmalya Paul495b94a2019-08-27 19:42:00 -0400406 def assert_uni_id_limit(self, pon_intf_id, onu_id, uni_id):
407 self.assert_onu_id_limit(pon_intf_id, onu_id)
408 self.resource_mgrs[pon_intf_id].assert_resource_limits(uni_id, PONResourceManager.UNI_ID)
409 */
410 for _, rsrcMgr := range RsrcMgr.ResourceMgrs {
npujarec5762e2020-01-01 14:08:48 +0530411 if err := rsrcMgr.ClearDeviceResourcePool(ctx); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000412 logger.Debug("Failed to clear device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400413 return err
414 }
415 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000416 logger.Debug("Cleared device resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400417 return nil
418}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530419
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700420// GetONUID returns the available OnuID for the given pon-port
npujarec5762e2020-01-01 14:08:48 +0530421func (RsrcMgr *OpenOltResourceMgr) GetONUID(ctx context.Context, ponIntfID uint32) (uint32, error) {
salmansiddiqui352a45c2019-08-19 10:15:36 +0000422 // Check if Pon Interface ID is present in Resource-manager-map
423 if _, ok := RsrcMgr.ResourceMgrs[ponIntfID]; !ok {
424 err := errors.New("invalid-pon-interface-" + strconv.Itoa(int(ponIntfID)))
425 return 0, err
426 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400427 // Get ONU id for a provided pon interface ID.
npujarec5762e2020-01-01 14:08:48 +0530428 ONUID, err := RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ctx, ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400429 ponrmgr.ONU_ID, 1)
430 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000431 logger.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700432 ponIntfID, ponrmgr.ONU_ID)
cbabuabf02352019-10-15 13:14:56 +0200433 return 0, err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400434 }
435 if ONUID != nil {
npujarec5762e2020-01-01 14:08:48 +0530436 RsrcMgr.ResourceMgrs[ponIntfID].InitResourceMap(ctx, fmt.Sprintf("%d,%d", ponIntfID, ONUID[0]))
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700437 return ONUID[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400438 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530439
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700440 return 0, err // return OnuID 0 on error
Abhilash S.L7f17e402019-03-15 17:40:41 +0530441}
442
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700443// GetFlowIDInfo returns the slice of flow info of the given pon-port
444// Note: For flows which trap from the NNI and not really associated with any particular
445// 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 +0530446func (RsrcMgr *OpenOltResourceMgr) GetFlowIDInfo(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32, flowID uint32) *[]FlowInfo {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530447 var flows []FlowInfo
448
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700449 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530450 if err := RsrcMgr.ResourceMgrs[ponIntfID].GetFlowIDInfo(ctx, FlowPath, flowID, &flows); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000451 logger.Errorw("Error while getting flows from KV store", log.Fields{"flowId": flowID})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530452 return nil
453 }
454 if len(flows) == 0 {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000455 logger.Debugw("No flowInfo found in KV store", log.Fields{"flowPath": FlowPath})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530456 return nil
457 }
458 return &flows
459}
460
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700461// GetCurrentFlowIDsForOnu fetches flow ID from the resource manager
462// Note: For flows which trap from the NNI and not really associated with any particular
463// 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 +0530464func (RsrcMgr *OpenOltResourceMgr) GetCurrentFlowIDsForOnu(ctx context.Context, PONIntfID uint32, ONUID int32, UNIID int32) []uint32 {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700465
Abhilash S.L8ee90712019-04-29 16:24:22 +0530466 FlowPath := fmt.Sprintf("%d,%d,%d", PONIntfID, ONUID, UNIID)
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700467 if mgrs, exist := RsrcMgr.ResourceMgrs[PONIntfID]; exist {
npujarec5762e2020-01-01 14:08:48 +0530468 return mgrs.GetCurrentFlowIDsForOnu(ctx, FlowPath)
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700469 }
470 return nil
Abhilash S.L8ee90712019-04-29 16:24:22 +0530471}
472
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700473// UpdateFlowIDInfo updates flow info for the given pon interface, onu id, and uni id
474// Note: For flows which trap from the NNI and not really associated with any particular
475// 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 +0530476func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDInfo(ctx context.Context, ponIntfID int32, onuID int32, uniID int32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700477 flowID uint32, flowData *[]FlowInfo) error {
478 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530479 return RsrcMgr.ResourceMgrs[uint32(ponIntfID)].UpdateFlowIDInfoForOnu(ctx, FlowPath, flowID, *flowData)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530480}
481
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700482// GetFlowID return flow ID for a given pon interface id, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530483func (RsrcMgr *OpenOltResourceMgr) GetFlowID(ctx context.Context, ponIntfID uint32, ONUID int32, uniID int32,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400484 gemportID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700485 flowStoreCookie uint64,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400486 flowCategory string, vlanPcp ...uint32) (uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530487
Girish Gowdru0c588b22019-04-23 23:24:56 -0400488 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700489 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, ONUID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530490 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(ctx, FlowPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400491 if FlowIDs != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000492 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 -0700493 for _, flowID := range FlowIDs {
npujarec5762e2020-01-01 14:08:48 +0530494 FlowInfo := RsrcMgr.GetFlowIDInfo(ctx, ponIntfID, int32(ONUID), int32(uniID), uint32(flowID))
salmansiddiqui7ac62132019-08-22 03:58:50 +0000495 er := getFlowIDFromFlowInfo(FlowInfo, flowID, gemportID, flowStoreCookie, flowCategory, vlanPcp...)
496 if er == nil {
497 return flowID, er
Abhilash S.L8ee90712019-04-29 16:24:22 +0530498 }
499 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400500 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000501 logger.Debug("No matching flows with flow cookie or flow category, allocating new flowid")
npujarec5762e2020-01-01 14:08:48 +0530502 FlowIDs, err = RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ctx, ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400503 ponrmgr.FLOW_ID, 1)
504 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000505 logger.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700506 ponIntfID, ponrmgr.FLOW_ID)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400507 return uint32(0), err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400508 }
509 if FlowIDs != nil {
npujarec5762e2020-01-01 14:08:48 +0530510 _ = RsrcMgr.ResourceMgrs[ponIntfID].UpdateFlowIDForOnu(ctx, FlowPath, FlowIDs[0], true)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700511 return FlowIDs[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400512 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530513
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700514 return 0, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530515}
516
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700517// GetAllocID return the first Alloc ID for a given pon interface id and onu id and then update the resource map on
518// the KV store with the list of alloc_ids allocated for the pon_intf_onu_id tuple
519// 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 +0530520func (RsrcMgr *OpenOltResourceMgr) GetAllocID(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530521
Girish Gowdru0c588b22019-04-23 23:24:56 -0400522 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700523 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530524 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400525 if AllocID != nil {
526 // Since we support only one alloc_id for the ONU at the moment,
527 // return the first alloc_id in the list, if available, for that
528 // ONU.
Girish Kumar2ad402b2020-03-20 19:45:12 +0000529 logger.Debugw("Retrieved alloc ID from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400530 return AllocID[0]
531 }
npujarec5762e2020-01-01 14:08:48 +0530532 AllocID, err = RsrcMgr.ResourceMgrs[intfID].GetResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400533 ponrmgr.ALLOC_ID, 1)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530534
Girish Gowdru0c588b22019-04-23 23:24:56 -0400535 if AllocID == nil || err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000536 logger.Error("Failed to allocate alloc id")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400537 return 0
538 }
539 // update the resource map on KV store with the list of alloc_id
540 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530541 err = RsrcMgr.ResourceMgrs[intfID].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID, AllocID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400542 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000543 logger.Error("Failed to update Alloc ID")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400544 return 0
545 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000546 logger.Debugw("Allocated new Tcont from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400547 return AllocID[0]
Abhilash S.L7f17e402019-03-15 17:40:41 +0530548}
549
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700550// UpdateAllocIdsForOnu updates alloc ids in kv store for a given pon interface id, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530551func (RsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ctx context.Context, ponPort uint32, onuID uint32, uniID uint32, allocID []uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530552
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700553 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530554 return RsrcMgr.ResourceMgrs[ponPort].UpdateAllocIdsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700555 allocID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530556}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700557
558// GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530559func (RsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(ctx context.Context, intfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700560 uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530561
Girish Gowdru0c588b22019-04-23 23:24:56 -0400562 /* Get gem ports for given pon interface , onu id and uni id. */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530563
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700564 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530565 return RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530566}
567
Gamze Abakafee36392019-10-03 11:17:24 +0000568// GetCurrentAllocIDsForOnu returns alloc ids for given pon interface and onu id
npujarec5762e2020-01-01 14:08:48 +0530569func (RsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDsForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530570
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700571 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530572 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400573 if AllocID != nil {
Gamze Abakafee36392019-10-03 11:17:24 +0000574 return AllocID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400575 }
Gamze Abakafee36392019-10-03 11:17:24 +0000576 return []uint32{}
577}
578
579// RemoveAllocIDForOnu removes the alloc id for given pon interface, onu id, uni id and alloc id
npujarec5762e2020-01-01 14:08:48 +0530580func (RsrcMgr *OpenOltResourceMgr) RemoveAllocIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, allocID uint32) {
581 allocIDs := RsrcMgr.GetCurrentAllocIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000582 for i := 0; i < len(allocIDs); i++ {
583 if allocIDs[i] == allocID {
584 allocIDs = append(allocIDs[:i], allocIDs[i+1:]...)
585 break
586 }
587 }
npujarec5762e2020-01-01 14:08:48 +0530588 err := RsrcMgr.UpdateAllocIdsForOnu(ctx, intfID, onuID, uniID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000589 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000590 logger.Errorf("Failed to Remove Alloc Id For Onu. IntfID %d onuID %d uniID %d allocID %d",
Gamze Abakafee36392019-10-03 11:17:24 +0000591 intfID, onuID, uniID, allocID)
592 }
593}
594
595// RemoveGemPortIDForOnu removes the gem port id for given pon interface, onu id, uni id and gem port id
npujarec5762e2020-01-01 14:08:48 +0530596func (RsrcMgr *OpenOltResourceMgr) RemoveGemPortIDForOnu(ctx context.Context, intfID uint32, onuID uint32, uniID uint32, gemPortID uint32) {
597 gemPortIDs := RsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, intfID, onuID, uniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000598 for i := 0; i < len(gemPortIDs); i++ {
599 if gemPortIDs[i] == gemPortID {
600 gemPortIDs = append(gemPortIDs[:i], gemPortIDs[i+1:]...)
601 break
602 }
603 }
npujarec5762e2020-01-01 14:08:48 +0530604 err := RsrcMgr.UpdateGEMPortIDsForOnu(ctx, intfID, onuID, uniID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000605 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000606 logger.Errorf("Failed to Remove Gem Id For Onu. IntfID %d onuID %d uniID %d gemPortId %d",
Gamze Abakafee36392019-10-03 11:17:24 +0000607 intfID, onuID, uniID, gemPortID)
608 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530609}
610
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700611// UpdateGEMportsPonportToOnuMapOnKVStore updates onu and uni id associated with the gem port to the kv store
612// This stored information is used when packet_indication is received and we need to derive the ONU Id for which
613// the packet arrived based on the pon_intf and gemport available in the packet_indication
npujarec5762e2020-01-01 14:08:48 +0530614func (RsrcMgr *OpenOltResourceMgr) UpdateGEMportsPonportToOnuMapOnKVStore(ctx context.Context, gemPorts []uint32, PonPort uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700615 onuID uint32, uniID uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530616
Girish Gowdru0c588b22019-04-23 23:24:56 -0400617 /* Update onu and uni id associated with the gem port to the kv store. */
618 var IntfGEMPortPath string
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700619 Data := fmt.Sprintf("%d %d", onuID, uniID)
620 for _, GEM := range gemPorts {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400621 IntfGEMPortPath = fmt.Sprintf("%d,%d", PonPort, GEM)
622 Val, err := json.Marshal(Data)
623 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000624 logger.Error("failed to Marshal")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400625 return err
626 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700627
npujarec5762e2020-01-01 14:08:48 +0530628 if err = RsrcMgr.KVStore.Put(ctx, IntfGEMPortPath, Val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000629 logger.Errorf("Failed to update resource %s", IntfGEMPortPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400630 return err
631 }
632 }
633 return nil
Abhilash S.L7f17e402019-03-15 17:40:41 +0530634}
635
Gamze Abakafee36392019-10-03 11:17:24 +0000636// RemoveGEMportPonportToOnuMapOnKVStore removes the relationship between the gem port and pon port
npujarec5762e2020-01-01 14:08:48 +0530637func (RsrcMgr *OpenOltResourceMgr) RemoveGEMportPonportToOnuMapOnKVStore(ctx context.Context, GemPort uint32, PonPort uint32) {
Gamze Abakafee36392019-10-03 11:17:24 +0000638 IntfGEMPortPath := fmt.Sprintf("%d,%d", PonPort, GemPort)
npujarec5762e2020-01-01 14:08:48 +0530639 err := RsrcMgr.KVStore.Delete(ctx, IntfGEMPortPath)
Gamze Abakafee36392019-10-03 11:17:24 +0000640 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000641 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 +0000642 }
643}
644
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700645// GetGEMPortID gets gem port id for a particular pon port, onu id and uni id and then update the resource map on
646// the KV store with the list of gemport_id allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530647func (RsrcMgr *OpenOltResourceMgr) GetGEMPortID(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700648 uniID uint32, NumOfPorts uint32) ([]uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530649
Girish Gowdru0c588b22019-04-23 23:24:56 -0400650 /* Get gem port id for a particular pon port, onu id
651 and uni id.
652 */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530653
Girish Gowdru0c588b22019-04-23 23:24:56 -0400654 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700655 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530656
npujarec5762e2020-01-01 14:08:48 +0530657 GEMPortList := RsrcMgr.ResourceMgrs[ponPort].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400658 if GEMPortList != nil {
659 return GEMPortList, nil
660 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530661
npujarec5762e2020-01-01 14:08:48 +0530662 GEMPortList, err = RsrcMgr.ResourceMgrs[ponPort].GetResourceID(ctx, ponPort,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400663 ponrmgr.GEMPORT_ID, NumOfPorts)
664 if err != nil && GEMPortList == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000665 logger.Errorf("Failed to get gem port id for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400666 return nil, err
667 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530668
Girish Gowdru0c588b22019-04-23 23:24:56 -0400669 // update the resource map on KV store with the list of gemport_id
670 // allocated for the pon_intf_onu_id tuple
npujarec5762e2020-01-01 14:08:48 +0530671 err = RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400672 GEMPortList)
673 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000674 logger.Errorf("Failed to update GEM ports to kv store for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400675 return nil, err
676 }
npujarec5762e2020-01-01 14:08:48 +0530677 _ = RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(ctx, GEMPortList, ponPort,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700678 onuID, uniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400679 return GEMPortList, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530680}
681
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700682// 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 +0530683func (RsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ctx context.Context, ponPort uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700684 uniID uint32, GEMPortList []uint32) error {
685 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530686 return RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(ctx, IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400687 GEMPortList)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530688
689}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700690
691// FreeonuID releases(make free) onu id for a particular pon-port
npujarec5762e2020-01-01 14:08:48 +0530692func (RsrcMgr *OpenOltResourceMgr) FreeonuID(ctx context.Context, intfID uint32, onuID []uint32) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700693
npujarec5762e2020-01-01 14:08:48 +0530694 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID, ponrmgr.ONU_ID, onuID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530695
Girish Gowdru0c588b22019-04-23 23:24:56 -0400696 /* Free onu id for a particular interface.*/
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700697 var IntfonuID string
698 for _, onu := range onuID {
699 IntfonuID = fmt.Sprintf("%d,%d", intfID, onu)
npujarec5762e2020-01-01 14:08:48 +0530700 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfonuID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400701 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530702}
703
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700704// FreeFlowID returns the free flow id for a given interface, onu id and uni id
npujarec5762e2020-01-01 14:08:48 +0530705func (RsrcMgr *OpenOltResourceMgr) FreeFlowID(ctx context.Context, IntfID uint32, onuID int32,
Devmalya Paul495b94a2019-08-27 19:42:00 -0400706 uniID int32, FlowID uint32) {
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400707 var IntfONUID string
708 var err error
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530709 FlowIds := make([]uint32, 0)
710
711 FlowIds = append(FlowIds, FlowID)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700712 IntfONUID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530713 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(ctx, IntfONUID, FlowID, false)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400714 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000715 logger.Errorw("Failed to Update flow id for", log.Fields{"intf": IntfONUID})
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400716 }
npujarec5762e2020-01-01 14:08:48 +0530717 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(ctx, IntfONUID, FlowID)
718 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.FLOW_ID, FlowIds)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400719}
720
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700721// FreeFlowIDs releases the flow Ids
npujarec5762e2020-01-01 14:08:48 +0530722func (RsrcMgr *OpenOltResourceMgr) FreeFlowIDs(ctx context.Context, IntfID uint32, onuID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700723 uniID uint32, FlowID []uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530724
npujarec5762e2020-01-01 14:08:48 +0530725 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.FLOW_ID, FlowID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530726
Abhilash S.L8ee90712019-04-29 16:24:22 +0530727 var IntfOnuIDUniID string
Girish Gowdru0c588b22019-04-23 23:24:56 -0400728 var err error
729 for _, flow := range FlowID {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700730 IntfOnuIDUniID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530731 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(ctx, IntfOnuIDUniID, flow, false)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400732 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000733 logger.Errorw("Failed to Update flow id for", log.Fields{"intf": IntfOnuIDUniID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400734 }
npujarec5762e2020-01-01 14:08:48 +0530735 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(ctx, IntfOnuIDUniID, flow)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400736 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530737}
738
Gamze Abakafee36392019-10-03 11:17:24 +0000739// FreeAllocID frees AllocID on the PON resource pool and also frees the allocID association
740// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530741func (RsrcMgr *OpenOltResourceMgr) FreeAllocID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000742 uniID uint32, allocID uint32) {
npujarec5762e2020-01-01 14:08:48 +0530743 RsrcMgr.RemoveAllocIDForOnu(ctx, IntfID, onuID, uniID, allocID)
Gamze Abakafee36392019-10-03 11:17:24 +0000744 allocIDs := make([]uint32, 0)
745 allocIDs = append(allocIDs, allocID)
npujarec5762e2020-01-01 14:08:48 +0530746 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.ALLOC_ID, allocIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000747}
748
749// FreeGemPortID frees GemPortID on the PON resource pool and also frees the gemPortID association
750// for the given OLT device.
npujarec5762e2020-01-01 14:08:48 +0530751func (RsrcMgr *OpenOltResourceMgr) FreeGemPortID(ctx context.Context, IntfID uint32, onuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000752 uniID uint32, gemPortID uint32) {
npujarec5762e2020-01-01 14:08:48 +0530753 RsrcMgr.RemoveGemPortIDForOnu(ctx, IntfID, onuID, uniID, gemPortID)
Gamze Abakafee36392019-10-03 11:17:24 +0000754 gemPortIDs := make([]uint32, 0)
755 gemPortIDs = append(gemPortIDs, gemPortID)
npujarec5762e2020-01-01 14:08:48 +0530756 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(ctx, IntfID, ponrmgr.GEMPORT_ID, gemPortIDs)
Gamze Abakafee36392019-10-03 11:17:24 +0000757}
758
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700759// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id, and the clears the
760// resource map and the onuID associated with (pon_intf_id, gemport_id) tuple,
npujarec5762e2020-01-01 14:08:48 +0530761func (RsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(ctx context.Context, intfID uint32, onuID uint32, uniID uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530762
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700763 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530764
npujarec5762e2020-01-01 14:08:48 +0530765 AllocIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(ctx, IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530766
npujarec5762e2020-01-01 14:08:48 +0530767 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400768 ponrmgr.ALLOC_ID,
769 AllocIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530770
npujarec5762e2020-01-01 14:08:48 +0530771 GEMPortIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(ctx, IntfOnuIDUniID)
772 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400773 ponrmgr.GEMPORT_ID,
774 GEMPortIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530775
npujarec5762e2020-01-01 14:08:48 +0530776 FlowIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentFlowIDsForOnu(ctx, IntfOnuIDUniID)
777 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(ctx, intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400778 ponrmgr.FLOW_ID,
779 FlowIDs)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400780 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
npujarec5762e2020-01-01 14:08:48 +0530781 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400782 // Clear the ONU Id associated with the (pon_intf_id, gemport_id) tuple.
783 for _, GEM := range GEMPortIDs {
npujarec5762e2020-01-01 14:08:48 +0530784 _ = RsrcMgr.KVStore.Delete(ctx, fmt.Sprintf("%d,%d", intfID, GEM))
Girish Gowdru0c588b22019-04-23 23:24:56 -0400785 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530786}
787
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700788// IsFlowCookieOnKVStore checks if the given flow cookie is present on the kv store
789// Returns true if the flow cookie is found, otherwise it returns false
npujarec5762e2020-01-01 14:08:48 +0530790func (RsrcMgr *OpenOltResourceMgr) IsFlowCookieOnKVStore(ctx context.Context, ponIntfID uint32, onuID int32, uniID int32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700791 flowStoreCookie uint64) bool {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530792
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700793 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +0530794 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(ctx, FlowPath)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400795 if FlowIDs != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000796 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 -0700797 for _, flowID := range FlowIDs {
npujarec5762e2020-01-01 14:08:48 +0530798 FlowInfo := RsrcMgr.GetFlowIDInfo(ctx, ponIntfID, int32(onuID), int32(uniID), uint32(flowID))
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400799 if FlowInfo != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000800 logger.Debugw("Found flows", log.Fields{"flows": *FlowInfo, "flowId": flowID})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400801 for _, Info := range *FlowInfo {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700802 if Info.FlowStoreCookie == flowStoreCookie {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000803 logger.Debug("Found flow matching with flowStore cookie", log.Fields{"flowId": flowID, "flowStoreCookie": flowStoreCookie})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400804 return true
805 }
806 }
807 }
808 }
809 }
810 return false
811}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400812
salmansiddiqui7ac62132019-08-22 03:58:50 +0000813// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000814// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530815func (RsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) []uint32 {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000816 Path := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000817 var Data []uint32
npujarec5762e2020-01-01 14:08:48 +0530818 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400819 if err == nil {
820 if Value != nil {
821 Val, err := kvstore.ToByte(Value.Value)
822 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000823 logger.Errorw("Failed to convert into byte array", log.Fields{"error": err})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400824 return Data
825 }
826 if err = json.Unmarshal(Val, &Data); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000827 logger.Error("Failed to unmarshal", log.Fields{"error": err})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400828 return Data
829 }
830 }
831 } else {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000832 logger.Errorf("Failed to get TP id from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400833 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000834 logger.Debugf("Getting TP id %d from path %s", Data, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400835 return Data
836
837}
838
Gamze Abakafee36392019-10-03 11:17:24 +0000839// RemoveTechProfileIDsForOnu deletes all tech profile ids from the KV-Store for the given onu based on the path
840// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530841func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDsForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32) error {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000842 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
npujarec5762e2020-01-01 14:08:48 +0530843 if err := RsrcMgr.KVStore.Delete(ctx, IntfOnuUniID); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000844 logger.Errorw("Failed to delete techprofile id resource in KV store", log.Fields{"path": IntfOnuUniID})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400845 return err
846 }
847 return nil
848}
849
Gamze Abakafee36392019-10-03 11:17:24 +0000850// RemoveTechProfileIDForOnu deletes a specific tech profile id from the KV-Store for the given onu based on the path
851// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530852func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32, UniID uint32, TpID uint32) error {
853 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000854 for i, tpIDInList := range tpIDList {
855 if tpIDInList == TpID {
856 tpIDList = append(tpIDList[:i], tpIDList[i+1:]...)
857 }
858 }
859 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
860 Value, err := json.Marshal(tpIDList)
861 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000862 logger.Error("failed to Marshal")
Gamze Abakafee36392019-10-03 11:17:24 +0000863 return err
864 }
npujarec5762e2020-01-01 14:08:48 +0530865 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000866 logger.Errorf("Failed to update resource %s", IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000867 return err
868 }
869 return err
870}
871
872// UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
873// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
npujarec5762e2020-01-01 14:08:48 +0530874func (RsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(ctx context.Context, IntfID uint32, OnuID uint32,
salmansiddiqui7ac62132019-08-22 03:58:50 +0000875 UniID uint32, TpID uint32) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400876 var Value []byte
877 var err error
878
salmansiddiqui7ac62132019-08-22 03:58:50 +0000879 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000880
npujarec5762e2020-01-01 14:08:48 +0530881 tpIDList := RsrcMgr.GetTechProfileIDForOnu(ctx, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000882 for _, value := range tpIDList {
883 if value == TpID {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000884 logger.Debugf("TpID %d is already in tpIdList for the path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000885 return err
886 }
887 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000888 logger.Debugf("updating tp id %d on path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000889 tpIDList = append(tpIDList, TpID)
890 Value, err = json.Marshal(tpIDList)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400891 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000892 logger.Error("failed to Marshal")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400893 return err
894 }
npujarec5762e2020-01-01 14:08:48 +0530895 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000896 logger.Errorf("Failed to update resource %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400897 return err
898 }
899 return err
900}
901
salmansiddiqui7ac62132019-08-22 03:58:50 +0000902// UpdateMeterIDForOnu updates the meter id in the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000903// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +0530904func (RsrcMgr *OpenOltResourceMgr) UpdateMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000905 UniID uint32, TpID uint32, MeterConfig *ofp.OfpMeterConfig) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400906 var Value []byte
907 var err error
908
Gamze Abakafee36392019-10-03 11:17:24 +0000909 IntfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400910 Value, err = json.Marshal(*MeterConfig)
911 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000912 logger.Error("failed to Marshal meter config")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400913 return err
914 }
npujarec5762e2020-01-01 14:08:48 +0530915 if err = RsrcMgr.KVStore.Put(ctx, IntfOnuUniID, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000916 logger.Errorf("Failed to store meter into KV store %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400917 return err
918 }
919 return err
920}
921
Gamze Abakafee36392019-10-03 11:17:24 +0000922// GetMeterIDForOnu fetches the meter id from the kv store for the given onu based on the path
923// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +0530924func (RsrcMgr *OpenOltResourceMgr) GetMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000925 UniID uint32, TpID uint32) (*ofp.OfpMeterConfig, error) {
926 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400927 var meterConfig ofp.OfpMeterConfig
npujarec5762e2020-01-01 14:08:48 +0530928 Value, err := RsrcMgr.KVStore.Get(ctx, Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400929 if err == nil {
930 if Value != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000931 logger.Debug("Found meter in KV store", log.Fields{"Direction": Direction})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000932 Val, er := kvstore.ToByte(Value.Value)
933 if er != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000934 logger.Errorw("Failed to convert into byte array", log.Fields{"error": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000935 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400936 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000937 if er = json.Unmarshal(Val, &meterConfig); er != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000938 logger.Error("Failed to unmarshal meterconfig", log.Fields{"error": er})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000939 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400940 }
941 } else {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000942 logger.Debug("meter-does-not-exists-in-KVStore")
Manikkaraj kb1d51442019-07-23 10:41:02 -0400943 return nil, err
944 }
945 } else {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000946 logger.Errorf("Failed to get Meter config from kvstore for path %s", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400947
948 }
949 return &meterConfig, err
950}
951
Gamze Abakafee36392019-10-03 11:17:24 +0000952// RemoveMeterIDForOnu deletes the meter id from the kV-Store for the given onu based on the path
953// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
npujarec5762e2020-01-01 14:08:48 +0530954func (RsrcMgr *OpenOltResourceMgr) RemoveMeterIDForOnu(ctx context.Context, Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000955 UniID uint32, TpID uint32) error {
956 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
npujarec5762e2020-01-01 14:08:48 +0530957 if err := RsrcMgr.KVStore.Delete(ctx, Path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000958 logger.Errorf("Failed to delete meter id %s from kvstore ", Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400959 return err
960 }
961 return nil
962}
salmansiddiqui7ac62132019-08-22 03:58:50 +0000963
964func getFlowIDFromFlowInfo(FlowInfo *[]FlowInfo, flowID, gemportID uint32, flowStoreCookie uint64, flowCategory string, vlanPcp ...uint32) error {
965 if FlowInfo != nil {
966 for _, Info := range *FlowInfo {
967 if int32(gemportID) == Info.Flow.GemportId && flowCategory != "" && Info.FlowCategory == flowCategory {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000968 logger.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000969 if Info.FlowCategory == "HSIA_FLOW" && Info.Flow.Classifier.OPbits == vlanPcp[0] {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000970 logger.Debug("Found matching vlan pcp ", log.Fields{"flowId": flowID, "Vlanpcp": vlanPcp[0]})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000971 return nil
972 }
973 }
974 if int32(gemportID) == Info.Flow.GemportId && flowStoreCookie != 0 && Info.FlowStoreCookie == flowStoreCookie {
975 if flowCategory != "" && Info.FlowCategory == flowCategory {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000976 logger.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000977 return nil
978 }
979 }
980 }
981 }
Girish Kumar2ad402b2020-03-20 19:45:12 +0000982 logger.Debugw("the flow can be related to a different service", log.Fields{"flow_info": FlowInfo})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000983 return errors.New("invalid flow-info")
984}
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530985
986//AddGemToOnuGemInfo adds gemport to onugem info kvstore
npujarec5762e2020-01-01 14:08:48 +0530987func (RsrcMgr *OpenOltResourceMgr) AddGemToOnuGemInfo(ctx context.Context, intfID uint32, onuID uint32, gemPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530988 var onuGemData []OnuGemInfo
989 var err error
990
npujarec5762e2020-01-01 14:08:48 +0530991 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000992 logger.Errorf("failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530993 return err
994 }
995 if len(onuGemData) == 0 {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000996 logger.Errorw("failed to ger Onuid info ", log.Fields{"intfid": intfID, "onuid": onuID})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530997 return err
998 }
999
1000 for idx, onugem := range onuGemData {
1001 if onugem.OnuID == onuID {
1002 for _, gem := range onuGemData[idx].GemPorts {
1003 if gem == gemPort {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001004 logger.Debugw("Gem already present in onugem info, skpping addition", log.Fields{"gem": gem})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301005 return nil
1006 }
1007 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001008 logger.Debugw("Added gem to onugem info", log.Fields{"gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301009 onuGemData[idx].GemPorts = append(onuGemData[idx].GemPorts, gemPort)
1010 break
1011 }
1012 }
npujarec5762e2020-01-01 14:08:48 +05301013 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301014 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001015 logger.Error("Failed to add onugem to kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301016 return err
1017 }
1018 return err
1019}
1020
1021//GetOnuGemInfo gets onu gem info from the kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301022func (RsrcMgr *OpenOltResourceMgr) GetOnuGemInfo(ctx context.Context, IntfID uint32) ([]OnuGemInfo, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301023 var onuGemData []OnuGemInfo
1024
npujarec5762e2020-01-01 14:08:48 +05301025 if err := RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001026 logger.Errorf("failed to get onuifo for intfid %d", IntfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301027 return nil, err
1028 }
1029
1030 return onuGemData, nil
1031}
1032
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001033// AddOnuGemInfo adds onu info on to the kvstore per interface
1034func (RsrcMgr *OpenOltResourceMgr) AddOnuGemInfo(ctx context.Context, IntfID uint32, onuGem OnuGemInfo) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301035 var onuGemData []OnuGemInfo
1036 var err error
1037
npujarec5762e2020-01-01 14:08:48 +05301038 if err = RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001039 logger.Errorf("failed to get onuifo for intfid %d", IntfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301040 return err
1041 }
1042 onuGemData = append(onuGemData, onuGem)
npujarec5762e2020-01-01 14:08:48 +05301043 err = RsrcMgr.ResourceMgrs[IntfID].AddOnuGemInfo(ctx, IntfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301044 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001045 logger.Error("Failed to add onugem to kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301046 return err
1047 }
1048
Girish Kumar2ad402b2020-03-20 19:45:12 +00001049 logger.Debugw("added onu to onugeminfo", log.Fields{"intf": IntfID, "onugem": onuGem})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301050 return err
1051}
1052
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001053// UpdateOnuGemInfo updates Onuinfo on the kvstore per interface
1054func (RsrcMgr *OpenOltResourceMgr) UpdateOnuGemInfo(ctx context.Context, IntfID uint32, onuGem []OnuGemInfo) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301055
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001056 // TODO: VOL-2643
1057 err := RsrcMgr.ResourceMgrs[IntfID].AddOnuGemInfo(ctx, IntfID, onuGem)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301058 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001059 logger.Debugw("persistence-update-failed", log.Fields{
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001060 "interface-id": IntfID,
1061 "gem-info": onuGem,
1062 "error": err})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301063 return err
1064 }
1065
Girish Kumar2ad402b2020-03-20 19:45:12 +00001066 logger.Debugw("updated onugeminfo", log.Fields{"intf": IntfID, "onugem": onuGem})
Chaitrashree G S1a55b882020-02-04 17:35:35 -05001067 return nil
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301068}
1069
1070// 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 +05301071func (RsrcMgr *OpenOltResourceMgr) AddUniPortToOnuInfo(ctx context.Context, intfID uint32, onuID uint32, portNo uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301072 var onuGemData []OnuGemInfo
1073 var err error
1074
npujarec5762e2020-01-01 14:08:48 +05301075 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(ctx, intfID, &onuGemData); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001076 logger.Errorf("failed to get onuifo for intfid %d", intfID)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301077 return
1078 }
1079 for idx, onu := range onuGemData {
1080 if onu.OnuID == onuID {
1081 for _, uni := range onu.UniPorts {
1082 if uni == portNo {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001083 logger.Debugw("uni already present in onugem info", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301084 return
1085 }
1086 }
1087 onuGemData[idx].UniPorts = append(onuGemData[idx].UniPorts, portNo)
1088 break
1089 }
1090 }
npujarec5762e2020-01-01 14:08:48 +05301091 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(ctx, intfID, onuGemData)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301092 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001093 logger.Errorw("Failed to add uin port in onugem to kv store", log.Fields{"uni": portNo})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301094 return
1095 }
1096 return
1097}
1098
1099//UpdateGemPortForPktIn updates gemport for pkt in path to kvstore, path being intfid, onuid, portno
npujarec5762e2020-01-01 14:08:48 +05301100func (RsrcMgr *OpenOltResourceMgr) UpdateGemPortForPktIn(ctx context.Context, pktIn PacketInInfoKey, gemPort uint32) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301101
1102 path := fmt.Sprintf(OnuPacketINPath, pktIn.IntfID, pktIn.OnuID, pktIn.LogicalPort)
1103 Value, err := json.Marshal(gemPort)
1104 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001105 logger.Error("Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301106 return
1107 }
npujarec5762e2020-01-01 14:08:48 +05301108 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001109 logger.Errorw("Failed to put to kvstore", log.Fields{"path": path, "value": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301110 return
1111 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001112 logger.Debugw("added gem packet in successfully", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301113
1114 return
1115}
1116
1117// GetGemPortFromOnuPktIn gets the gem port from onu pkt in path, path being intfid, onuid, portno
npujarec5762e2020-01-01 14:08:48 +05301118func (RsrcMgr *OpenOltResourceMgr) GetGemPortFromOnuPktIn(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) (uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301119
1120 var Val []byte
1121 var gemPort uint32
1122
1123 path := fmt.Sprintf(OnuPacketINPath, intfID, onuID, logicalPort)
1124
npujarec5762e2020-01-01 14:08:48 +05301125 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301126 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001127 logger.Errorw("Failed to get from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301128 return uint32(0), err
1129 } else if value == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001130 logger.Debugw("No pkt in gem found", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301131 return uint32(0), nil
1132 }
1133
1134 if Val, err = kvstore.ToByte(value.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001135 logger.Error("Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301136 return uint32(0), err
1137 }
1138 if err = json.Unmarshal(Val, &gemPort); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001139 logger.Error("Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301140 return uint32(0), err
1141 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001142 logger.Debugw("found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301143
1144 return gemPort, nil
1145}
1146
1147// DelGemPortPktIn deletes the gemport from the pkt in path
npujarec5762e2020-01-01 14:08:48 +05301148func (RsrcMgr *OpenOltResourceMgr) DelGemPortPktIn(ctx context.Context, intfID uint32, onuID uint32, logicalPort uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301149
1150 path := fmt.Sprintf(OnuPacketINPath, intfID, onuID, logicalPort)
npujarec5762e2020-01-01 14:08:48 +05301151 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001152 logger.Errorf("Falied to remove resource %s", path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301153 return err
1154 }
1155 return nil
1156}
1157
1158// DelOnuGemInfoForIntf deletes the onugem info from kvstore per interface
npujarec5762e2020-01-01 14:08:48 +05301159func (RsrcMgr *OpenOltResourceMgr) DelOnuGemInfoForIntf(ctx context.Context, intfID uint32) error {
1160 if err := RsrcMgr.ResourceMgrs[intfID].DelOnuGemInfoForIntf(ctx, intfID); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001161 logger.Errorw("failed to delete onu gem info for", log.Fields{"intfid": intfID})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301162 return err
1163 }
1164 return nil
1165}
1166
1167//GetNNIFromKVStore gets NNi intfids from kvstore. path being per device
npujarec5762e2020-01-01 14:08:48 +05301168func (RsrcMgr *OpenOltResourceMgr) GetNNIFromKVStore(ctx context.Context) ([]uint32, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301169
1170 var nni []uint32
1171 var Val []byte
1172
1173 path := fmt.Sprintf(NnniIntfID)
npujarec5762e2020-01-01 14:08:48 +05301174 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301175 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001176 logger.Error("failed to get data from kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301177 return nil, err
1178 }
1179 if value != nil {
1180 if Val, err = kvstore.ToByte(value.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001181 logger.Error("Failed to convert to byte array")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301182 return nil, err
1183 }
1184 if err = json.Unmarshal(Val, &nni); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001185 logger.Error("Failed to unmarshall")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301186 return nil, err
1187 }
1188 }
1189 return nni, err
1190}
1191
1192// AddNNIToKVStore adds Nni interfaces to kvstore, path being per device.
npujarec5762e2020-01-01 14:08:48 +05301193func (RsrcMgr *OpenOltResourceMgr) AddNNIToKVStore(ctx context.Context, nniIntf uint32) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301194 var Value []byte
1195
npujarec5762e2020-01-01 14:08:48 +05301196 nni, err := RsrcMgr.GetNNIFromKVStore(ctx)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301197 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001198 logger.Error("failed to fetch nni interfaces from kv store")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301199 return err
1200 }
1201
1202 path := fmt.Sprintf(NnniIntfID)
1203 nni = append(nni, nniIntf)
1204 Value, err = json.Marshal(nni)
1205 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001206 logger.Error("Failed to marshal data")
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301207 }
npujarec5762e2020-01-01 14:08:48 +05301208 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001209 logger.Errorw("Failed to put to kvstore", log.Fields{"path": path, "value": Value})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301210 return err
1211 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001212 logger.Debugw("added nni to kv successfully", log.Fields{"path": path, "nni": nniIntf})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301213 return nil
1214}
1215
1216// DelNNiFromKVStore deletes nni interface list from kv store.
npujarec5762e2020-01-01 14:08:48 +05301217func (RsrcMgr *OpenOltResourceMgr) DelNNiFromKVStore(ctx context.Context) error {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301218
1219 path := fmt.Sprintf(NnniIntfID)
1220
npujarec5762e2020-01-01 14:08:48 +05301221 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001222 logger.Errorw("Failed to delete nni interfaces from kv store", log.Fields{"path": path})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301223 return err
1224 }
1225 return nil
1226}
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301227
1228//UpdateFlowIDsForGem updates flow id per gemport
npujarec5762e2020-01-01 14:08:48 +05301229func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDsForGem(ctx context.Context, intf uint32, gem uint32, flowIDs []uint32) error {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301230 var val []byte
1231 path := fmt.Sprintf(FlowIDsForGem, intf)
1232
npujarec5762e2020-01-01 14:08:48 +05301233 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301234 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001235 logger.Error("Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301236 return err
1237 }
1238 if flowsForGem == nil {
1239 flowsForGem = make(map[uint32][]uint32)
1240 }
1241 flowsForGem[gem] = flowIDs
1242 val, err = json.Marshal(flowsForGem)
1243 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001244 logger.Error("Failed to marshal data", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301245 return err
1246 }
npujarec5762e2020-01-01 14:08:48 +05301247 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001248 logger.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301249 return err
1250 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001251 logger.Debugw("added flowid list for gem to kv successfully", log.Fields{"path": path, "flowidlist": flowsForGem[gem]})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301252 return nil
1253}
1254
1255//DeleteFlowIDsForGem deletes the flowID list entry per gem from kvstore.
npujarec5762e2020-01-01 14:08:48 +05301256func (RsrcMgr *OpenOltResourceMgr) DeleteFlowIDsForGem(ctx context.Context, intf uint32, gem uint32) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301257 path := fmt.Sprintf(FlowIDsForGem, intf)
1258 var val []byte
1259
npujarec5762e2020-01-01 14:08:48 +05301260 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(ctx, intf)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301261 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001262 logger.Error("Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301263 return
1264 }
1265 if flowsForGem == nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001266 logger.Error("No flowids found ", log.Fields{"intf": intf, "gemport": gem})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301267 return
1268 }
1269 // once we get the flows per gem map from kv , just delete the gem entry from the map
1270 delete(flowsForGem, gem)
1271 // once gem entry is deleted update the kv store.
1272 val, err = json.Marshal(flowsForGem)
1273 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001274 logger.Error("Failed to marshal data", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301275 return
1276 }
npujarec5762e2020-01-01 14:08:48 +05301277 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001278 logger.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301279 return
1280 }
1281 return
1282}
1283
1284//GetFlowIDsGemMapForInterface gets flowids per gemport and interface
npujarec5762e2020-01-01 14:08:48 +05301285func (RsrcMgr *OpenOltResourceMgr) GetFlowIDsGemMapForInterface(ctx context.Context, intf uint32) (map[uint32][]uint32, error) {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301286 path := fmt.Sprintf(FlowIDsForGem, intf)
1287 var flowsForGem map[uint32][]uint32
1288 var val []byte
1289
npujarec5762e2020-01-01 14:08:48 +05301290 value, err := RsrcMgr.KVStore.Get(ctx, path)
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301291 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001292 logger.Error("failed to get data from kv store")
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301293 return nil, err
1294 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001295 if value != nil && value.Value != nil {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301296 if val, err = kvstore.ToByte(value.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001297 logger.Error("Failed to convert to byte array ", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301298 return nil, err
1299 }
1300 if err = json.Unmarshal(val, &flowsForGem); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001301 logger.Error("Failed to unmarshall", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301302 return nil, err
1303 }
1304 }
1305 return flowsForGem, nil
1306}
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301307
1308//DeleteIntfIDGempMapPath deletes the intf id path used to store flow ids per gem to kvstore.
npujarec5762e2020-01-01 14:08:48 +05301309func (RsrcMgr *OpenOltResourceMgr) DeleteIntfIDGempMapPath(ctx context.Context, intf uint32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301310 path := fmt.Sprintf(FlowIDsForGem, intf)
npujarec5762e2020-01-01 14:08:48 +05301311 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001312 logger.Errorw("Failed to delete nni interfaces from kv store", log.Fields{"path": path})
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301313 return
1314 }
1315 return
1316}
1317
1318// RemoveResourceMap Clear resource map associated with (intfid, onuid, uniid) tuple.
npujarec5762e2020-01-01 14:08:48 +05301319func (RsrcMgr *OpenOltResourceMgr) RemoveResourceMap(ctx context.Context, intfID uint32, onuID int32, uniID int32) {
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301320 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
npujarec5762e2020-01-01 14:08:48 +05301321 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(ctx, IntfOnuIDUniID)
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301322}
Esin Karamanccb714b2019-11-29 15:02:06 +00001323
1324//GetMcastQueuePerInterfaceMap gets multicast queue info per pon interface
npujarec5762e2020-01-01 14:08:48 +05301325func (RsrcMgr *OpenOltResourceMgr) GetMcastQueuePerInterfaceMap(ctx context.Context) (map[uint32][]uint32, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001326 path := fmt.Sprintf(McastQueuesForIntf)
1327 var mcastQueueToIntfMap map[uint32][]uint32
1328 var val []byte
1329
npujarec5762e2020-01-01 14:08:48 +05301330 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001331 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001332 logger.Error("failed to get data from kv store")
Esin Karamanccb714b2019-11-29 15:02:06 +00001333 return nil, err
1334 }
1335 if kvPair != nil && kvPair.Value != nil {
1336 if val, err = kvstore.ToByte(kvPair.Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001337 logger.Error("Failed to convert to byte array ", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001338 return nil, err
1339 }
1340 if err = json.Unmarshal(val, &mcastQueueToIntfMap); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001341 logger.Error("Failed to unmarshall ", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001342 return nil, err
1343 }
1344 }
1345 return mcastQueueToIntfMap, nil
1346}
1347
1348//AddMcastQueueForIntf adds multicast queue for pon interface
npujarec5762e2020-01-01 14:08:48 +05301349func (RsrcMgr *OpenOltResourceMgr) AddMcastQueueForIntf(ctx context.Context, intf uint32, gem uint32, servicePriority uint32) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001350 var val []byte
1351 path := fmt.Sprintf(McastQueuesForIntf)
1352
npujarec5762e2020-01-01 14:08:48 +05301353 mcastQueues, err := RsrcMgr.GetMcastQueuePerInterfaceMap(ctx)
Esin Karamanccb714b2019-11-29 15:02:06 +00001354 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001355 logger.Errorw("Failed to get multicast queue info for interface", log.Fields{"error": err, "intf": intf})
Esin Karamanccb714b2019-11-29 15:02:06 +00001356 return err
1357 }
1358 if mcastQueues == nil {
1359 mcastQueues = make(map[uint32][]uint32)
1360 }
1361 mcastQueues[intf] = []uint32{gem, servicePriority}
1362 if val, err = json.Marshal(mcastQueues); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001363 logger.Errorw("Failed to marshal data", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001364 return err
1365 }
npujarec5762e2020-01-01 14:08:48 +05301366 if err = RsrcMgr.KVStore.Put(ctx, path, val); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001367 logger.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
Esin Karamanccb714b2019-11-29 15:02:06 +00001368 return err
1369 }
Girish Kumar2ad402b2020-03-20 19:45:12 +00001370 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 +00001371 return nil
1372}
1373
1374//AddFlowGroupToKVStore adds flow group into KV store
npujarec5762e2020-01-01 14:08:48 +05301375func (RsrcMgr *OpenOltResourceMgr) AddFlowGroupToKVStore(ctx context.Context, groupEntry *ofp.OfpGroupEntry, cached bool) error {
Esin Karamanccb714b2019-11-29 15:02:06 +00001376 var Value []byte
1377 var err error
1378 var path string
1379 if cached {
1380 path = fmt.Sprintf(FlowGroupCached, groupEntry.Desc.GroupId)
1381 } else {
1382 path = fmt.Sprintf(FlowGroup, groupEntry.Desc.GroupId)
1383 }
1384 //build group info object
1385 var outPorts []uint32
1386 for _, ofBucket := range groupEntry.Desc.Buckets {
1387 for _, ofAction := range ofBucket.Actions {
1388 if ofAction.Type == ofp.OfpActionType_OFPAT_OUTPUT {
1389 outPorts = append(outPorts, ofAction.GetOutput().Port)
1390 }
1391 }
1392 }
1393 groupInfo := GroupInfo{
1394 GroupID: groupEntry.Desc.GroupId,
1395 OutPorts: outPorts,
1396 }
1397
1398 Value, err = json.Marshal(groupInfo)
1399
1400 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001401 logger.Error("failed to Marshal flow group object")
Esin Karamanccb714b2019-11-29 15:02:06 +00001402 return err
1403 }
1404
npujarec5762e2020-01-01 14:08:48 +05301405 if err = RsrcMgr.KVStore.Put(ctx, path, Value); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001406 logger.Errorf("Failed to update resource %s", path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001407 return err
1408 }
1409 return nil
1410}
1411
1412//RemoveFlowGroupFromKVStore removes flow group from KV store
npujarec5762e2020-01-01 14:08:48 +05301413func (RsrcMgr *OpenOltResourceMgr) RemoveFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) bool {
Esin Karamanccb714b2019-11-29 15:02:06 +00001414 var path string
1415 if cached {
1416 path = fmt.Sprintf(FlowGroupCached, groupID)
1417 } else {
1418 path = fmt.Sprintf(FlowGroup, groupID)
1419 }
npujarec5762e2020-01-01 14:08:48 +05301420 if err := RsrcMgr.KVStore.Delete(ctx, path); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001421 logger.Errorf("Failed to remove resource %s due to %s", path, err)
Esin Karamanccb714b2019-11-29 15:02:06 +00001422 return false
1423 }
1424 return true
1425}
1426
1427//GetFlowGroupFromKVStore fetches flow group from the KV store. Returns (false, {} error) if any problem occurs during
1428//fetching the data. Returns (true, groupInfo, nil) if the group is fetched successfully.
1429// Returns (false, {}, nil) if the group does not exists in the KV store.
npujarec5762e2020-01-01 14:08:48 +05301430func (RsrcMgr *OpenOltResourceMgr) GetFlowGroupFromKVStore(ctx context.Context, groupID uint32, cached bool) (bool, GroupInfo, error) {
Esin Karamanccb714b2019-11-29 15:02:06 +00001431 var groupInfo GroupInfo
1432 var path string
1433 if cached {
1434 path = fmt.Sprintf(FlowGroupCached, groupID)
1435 } else {
1436 path = fmt.Sprintf(FlowGroup, groupID)
1437 }
npujarec5762e2020-01-01 14:08:48 +05301438 kvPair, err := RsrcMgr.KVStore.Get(ctx, path)
Esin Karamanccb714b2019-11-29 15:02:06 +00001439 if err != nil {
1440 return false, groupInfo, err
1441 }
1442 if kvPair != nil && kvPair.Value != nil {
1443 Val, err := kvstore.ToByte(kvPair.Value)
1444 if err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001445 logger.Errorw("Failed to convert flow group into byte array", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001446 return false, groupInfo, err
1447 }
1448 if err = json.Unmarshal(Val, &groupInfo); err != nil {
Girish Kumar2ad402b2020-03-20 19:45:12 +00001449 logger.Errorw("Failed to unmarshal", log.Fields{"error": err})
Esin Karamanccb714b2019-11-29 15:02:06 +00001450 return false, groupInfo, err
1451 }
1452 return true, groupInfo, nil
1453 }
1454 return false, groupInfo, nil
1455}