blob: 0e3da4da37c41a5a0f0298a6bdafa20efbc83e60 [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 (
Girish Gowdru0c588b22019-04-23 23:24:56 -040021 "encoding/json"
22 "errors"
23 "fmt"
24 "strconv"
25 "strings"
Abhilash S.L7f17e402019-03-15 17:40:41 +053026
Esin Karamanccb714b2019-11-29 15:02:06 +000027 "github.com/opencord/voltha-lib-go/v3/pkg/db"
28 "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
29 "github.com/opencord/voltha-lib-go/v3/pkg/log"
30 ponrmgr "github.com/opencord/voltha-lib-go/v3/pkg/ponresourcemanager"
31 ofp "github.com/opencord/voltha-protos/v3/go/openflow_13"
32 "github.com/opencord/voltha-protos/v3/go/openolt"
Abhilash S.L7f17e402019-03-15 17:40:41 +053033)
34
salmansiddiqui7ac62132019-08-22 03:58:50 +000035const (
36 // KvstoreTimeout specifies the time out for KV Store Connection
37 KvstoreTimeout = 5
38 // BasePathKvStore - service/voltha/openolt/<device_id>
39 BasePathKvStore = "service/voltha/openolt/{%s}"
Gamze Abakafee36392019-10-03 11:17:24 +000040 // TpIDPathSuffix - <(pon_id, onu_id, uni_id)>/tp_id
41 TpIDPathSuffix = "{%d,%d,%d}/tp_id"
42 //MeterIDPathSuffix - <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
43 MeterIDPathSuffix = "{%d,%d,%d}/{%d}/meter_id/{%s}"
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053044 //NnniIntfID - nniintfids
45 NnniIntfID = "nniintfids"
46 // OnuPacketINPath path on the kvstore to store packetin gemport,which will be used for packetin, pcketout
47 //format: onu_packetin/<intfid>,<onuid>,<logicalport>
48 OnuPacketINPath = "onu_packetin/{%d,%d,%d}"
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +053049 //FlowIDsForGem flowids_per_gem/<intfid>
50 FlowIDsForGem = "flowids_per_gem/{%d}"
Esin Karamanccb714b2019-11-29 15:02:06 +000051 //McastQueuesForIntf multicast queues for pon interfaces
52 McastQueuesForIntf = "mcast_qs_for_int"
53 //FlowGroup flow_groups/<flow_group_id>
54 // A group is stored under this path on the KV store after it has been installed to the device.
55 // It should also be deleted after it has been removed from the device accordingly.
56 FlowGroup = "flow_groups/{%d}"
57 //FlowGroupCached flow_groups_cached/<flow_group_id>
58 // When a group add request received, we create the group without setting any members to it since we cannot
59 // set any members to a group until it is associated with a multicast flow. It is a BAL limitation.
60 // When the related multicast flow has been created we perform set members operation for the group.
61 // That is why we need to keep the members of a group until the multicast flow creation request comes.
62 // We preserve the groups under "FlowGroupsCached" directory in the KV store temporarily. Having set members,
63 // we remove the group from the cached group store.
64 FlowGroupCached = "flow_groups_cached/{%d}"
salmansiddiqui7ac62132019-08-22 03:58:50 +000065)
Abhilash S.L7f17e402019-03-15 17:40:41 +053066
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070067// FlowInfo holds the flow information
Abhilash S.L8ee90712019-04-29 16:24:22 +053068type FlowInfo struct {
69 Flow *openolt.Flow
70 FlowStoreCookie uint64
71 FlowCategory string
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +053072 LogicalFlowID uint64
73}
74
75// OnuGemInfo holds onu information along with gem port list and uni port list
76type OnuGemInfo struct {
77 OnuID uint32
78 SerialNumber string
79 IntfID uint32
80 GemPorts []uint32
81 UniPorts []uint32
82}
83
84// PacketInInfoKey is the key for packet in gemport
85type PacketInInfoKey struct {
86 IntfID uint32
87 OnuID uint32
88 LogicalPort uint32
Abhilash S.L8ee90712019-04-29 16:24:22 +053089}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070090
Esin Karamanccb714b2019-11-29 15:02:06 +000091// GroupInfo holds group information
92type GroupInfo struct {
93 GroupID uint32
94 OutPorts []uint32
95}
96
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070097// OpenOltResourceMgr holds resource related information as provided below for each field
Abhilash S.L7f17e402019-03-15 17:40:41 +053098type OpenOltResourceMgr struct {
sbarbaria8910ba2019-11-05 10:12:23 -050099 DeviceID string // OLT device id
100 HostAndPort string // Host and port of the kv store to connect to
101 Args string // args
102 KVStore *db.Backend // backend kv store connection handle
Girish Gowdru0c588b22019-04-23 23:24:56 -0400103 DeviceType string
104 Host string // Host ip of the kv store
105 Port int // port of the kv store
106 DevInfo *openolt.DeviceInfo // device information
107 // array of pon resource managers per interface technology
108 ResourceMgrs map[uint32]*ponrmgr.PONResourceManager
Abhilash S.L7f17e402019-03-15 17:40:41 +0530109}
110
Manikkaraj kb1d51442019-07-23 10:41:02 -0400111func newKVClient(storeType string, address string, timeout uint32) (kvstore.Client, error) {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400112 log.Infow("kv-store-type", log.Fields{"store": storeType})
113 switch storeType {
114 case "consul":
115 return kvstore.NewConsulClient(address, int(timeout))
116 case "etcd":
117 return kvstore.NewEtcdClient(address, int(timeout))
118 }
119 return nil, errors.New("unsupported-kv-store")
Abhilash S.L7f17e402019-03-15 17:40:41 +0530120}
121
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700122// SetKVClient sets the KV client and return a kv backend
sbarbaria8910ba2019-11-05 10:12:23 -0500123func SetKVClient(backend string, Host string, Port int, DeviceID string) *db.Backend {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400124 addr := Host + ":" + strconv.Itoa(Port)
125 // TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
126 // issue between kv store and backend , core is not calling NewBackend directly
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700127 kvClient, err := newKVClient(backend, addr, KvstoreTimeout)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400128 if err != nil {
129 log.Fatalw("Failed to init KV client\n", log.Fields{"err": err})
130 return nil
131 }
sbarbaria8910ba2019-11-05 10:12:23 -0500132 kvbackend := &db.Backend{
Girish Gowdru0c588b22019-04-23 23:24:56 -0400133 Client: kvClient,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700134 StoreType: backend,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400135 Host: Host,
136 Port: Port,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700137 Timeout: KvstoreTimeout,
138 PathPrefix: fmt.Sprintf(BasePathKvStore, DeviceID)}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530139
Girish Gowdru0c588b22019-04-23 23:24:56 -0400140 return kvbackend
Abhilash S.L7f17e402019-03-15 17:40:41 +0530141}
142
Gamze Abakafee36392019-10-03 11:17:24 +0000143// NewResourceMgr init a New resource manager instance which in turn instantiates pon resource manager
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700144// instances according to technology. Initializes the default resource ranges for all
145// the resources.
146func NewResourceMgr(deviceID string, KVStoreHostPort string, kvStoreType string, deviceType string, devInfo *openolt.DeviceInfo) *OpenOltResourceMgr {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400147 var ResourceMgr OpenOltResourceMgr
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700148 log.Debugf("Init new resource manager , host_port: %s, deviceid: %s", KVStoreHostPort, deviceID)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530149 ResourceMgr.HostAndPort = KVStoreHostPort
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700150 ResourceMgr.DeviceType = deviceType
151 ResourceMgr.DevInfo = devInfo
152 IPPort := strings.Split(KVStoreHostPort, ":")
153 ResourceMgr.Host = IPPort[0]
154 ResourceMgr.Port, _ = strconv.Atoi(IPPort[1])
Abhilash S.L7f17e402019-03-15 17:40:41 +0530155
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700156 Backend := kvStoreType
Girish Gowdru0c588b22019-04-23 23:24:56 -0400157 ResourceMgr.KVStore = SetKVClient(Backend, ResourceMgr.Host,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700158 ResourceMgr.Port, deviceID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400159 if ResourceMgr.KVStore == nil {
160 log.Error("Failed to setup KV store")
161 }
162 Ranges := make(map[string]*openolt.DeviceInfo_DeviceResourceRanges)
163 RsrcMgrsByTech := make(map[string]*ponrmgr.PONResourceManager)
164 ResourceMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530165
Girish Gowdru0c588b22019-04-23 23:24:56 -0400166 // TODO self.args = registry('main').get_args()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530167
Girish Gowdru0c588b22019-04-23 23:24:56 -0400168 /*
169 If a legacy driver returns protobuf without any ranges,s synthesize one from
Gamze Abakafee36392019-10-03 11:17:24 +0000170 the legacy global per-device information. This, in theory, is temporary until
Girish Gowdru0c588b22019-04-23 23:24:56 -0400171 the legacy drivers are upgrade to support pool ranges.
172 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700173 if devInfo.Ranges == nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400174 var ranges openolt.DeviceInfo_DeviceResourceRanges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700175 ranges.Technology = devInfo.GetTechnology()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530176
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700177 NumPONPorts := devInfo.GetPonPorts()
Girish Gowdru0c588b22019-04-23 23:24:56 -0400178 var index uint32
179 for index = 0; index < NumPONPorts; index++ {
180 ranges.IntfIds = append(ranges.IntfIds, index)
181 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530182
Abhilash S.L8ee90712019-04-29 16:24:22 +0530183 var Pool openolt.DeviceInfo_DeviceResourceRanges_Pool
Girish Gowdru0c588b22019-04-23 23:24:56 -0400184 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700185 Pool.Start = devInfo.OnuIdStart
186 Pool.End = devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400187 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
cbabuabf02352019-10-15 13:14:56 +0200188 onuPool := Pool
189 ranges.Pools = append(ranges.Pools, &onuPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530190
Girish Gowdru0c588b22019-04-23 23:24:56 -0400191 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700192 Pool.Start = devInfo.AllocIdStart
193 Pool.End = devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400194 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200195 allocPool := Pool
196 ranges.Pools = append(ranges.Pools, &allocPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530197
Girish Gowdru0c588b22019-04-23 23:24:56 -0400198 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700199 Pool.Start = devInfo.GemportIdStart
200 Pool.End = devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400201 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
cbabuabf02352019-10-15 13:14:56 +0200202 gemPool := Pool
203 ranges.Pools = append(ranges.Pools, &gemPool)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530204
Girish Gowdru0c588b22019-04-23 23:24:56 -0400205 Pool.Type = openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700206 Pool.Start = devInfo.FlowIdStart
207 Pool.End = devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400208 Pool.Sharing = openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH
Abhilash S.L8ee90712019-04-29 16:24:22 +0530209 ranges.Pools = append(ranges.Pools, &Pool)
210 // Add to device info
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700211 devInfo.Ranges = append(devInfo.Ranges, &ranges)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400212 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530213
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700214 // Create a separate Resource Manager instance for each range. This assumes that
Girish Gowdru0c588b22019-04-23 23:24:56 -0400215 // each technology is represented by only a single range
216 var GlobalPONRsrcMgr *ponrmgr.PONResourceManager
217 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700218 for _, TechRange := range devInfo.Ranges {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400219 technology := TechRange.Technology
220 log.Debugf("Device info technology %s", technology)
221 Ranges[technology] = TechRange
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700222 RsrcMgrsByTech[technology], err = ponrmgr.NewPONResourceManager(technology, deviceType, deviceID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400223 Backend, ResourceMgr.Host, ResourceMgr.Port)
224 if err != nil {
Gamze Abakafee36392019-10-03 11:17:24 +0000225 log.Errorf("Failed to create pon resource manager instance for technology %s", technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400226 return nil
227 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700228 // resource_mgrs_by_tech[technology] = resource_mgr
Girish Gowdru0c588b22019-04-23 23:24:56 -0400229 if GlobalPONRsrcMgr == nil {
230 GlobalPONRsrcMgr = RsrcMgrsByTech[technology]
231 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700232 for _, IntfID := range TechRange.IntfIds {
233 ResourceMgr.ResourceMgrs[uint32(IntfID)] = RsrcMgrsByTech[technology]
Girish Gowdru0c588b22019-04-23 23:24:56 -0400234 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700235 // self.initialize_device_resource_range_and_pool(resource_mgr, global_resource_mgr, arange)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400236 InitializeDeviceResourceRangeAndPool(RsrcMgrsByTech[technology], GlobalPONRsrcMgr,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700237 TechRange, devInfo)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400238 }
239 // After we have initialized resource ranges, initialize the
240 // resource pools accordingly.
241 for _, PONRMgr := range RsrcMgrsByTech {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700242 _ = PONRMgr.InitDeviceResourcePool()
Girish Gowdru0c588b22019-04-23 23:24:56 -0400243 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530244 log.Info("Initialization of resource manager success!")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400245 return &ResourceMgr
Abhilash S.L7f17e402019-03-15 17:40:41 +0530246}
247
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700248// InitializeDeviceResourceRangeAndPool initializes the resource range pool according to the sharing type, then apply
249// device specific information. If KV doesn't exist
250// or is broader than the device, the device's information will
251// dictate the range limits
252func InitializeDeviceResourceRangeAndPool(ponRMgr *ponrmgr.PONResourceManager, globalPONRMgr *ponrmgr.PONResourceManager,
253 techRange *openolt.DeviceInfo_DeviceResourceRanges, devInfo *openolt.DeviceInfo) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530254
Girish Gowdru0c588b22019-04-23 23:24:56 -0400255 // init the resource range pool according to the sharing type
Abhilash S.L7f17e402019-03-15 17:40:41 +0530256
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700257 log.Debugf("Resource range pool init for technology %s", ponRMgr.Technology)
258 // first load from KV profiles
259 status := ponRMgr.InitResourceRangesFromKVStore()
260 if !status {
261 log.Debugf("Failed to load resource ranges from KV store for tech %s", ponRMgr.Technology)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400262 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530263
Girish Gowdru0c588b22019-04-23 23:24:56 -0400264 /*
265 Then apply device specific information. If KV doesn't exist
Gamze Abakafee36392019-10-03 11:17:24 +0000266 or is broader than the device, the device's information will
Girish Gowdru0c588b22019-04-23 23:24:56 -0400267 dictate the range limits
268 */
Girish Kumar8f73fe02019-12-09 13:19:37 +0000269 log.Debugw("Using device info to init pon resource ranges", log.Fields{"Tech": ponRMgr.Technology})
Abhilash S.L7f17e402019-03-15 17:40:41 +0530270
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700271 ONUIDStart := devInfo.OnuIdStart
272 ONUIDEnd := devInfo.OnuIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400273 ONUIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF
274 ONUIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700275 AllocIDStart := devInfo.AllocIdStart
276 AllocIDEnd := devInfo.AllocIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400277 AllocIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
278 AllocIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700279 GEMPortIDStart := devInfo.GemportIdStart
280 GEMPortIDEnd := devInfo.GemportIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400281 GEMPortIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
282 GEMPortIDSharedPoolID := uint32(0)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700283 FlowIDStart := devInfo.FlowIdStart
284 FlowIDEnd := devInfo.FlowIdEnd
Girish Gowdru0c588b22019-04-23 23:24:56 -0400285 FlowIDShared := openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH // TODO EdgeCore/BAL limitation
286 FlowIDSharedPoolID := uint32(0)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530287
Girish Gowdru0c588b22019-04-23 23:24:56 -0400288 var FirstIntfPoolID uint32
289 var SharedPoolID uint32
Abhilash S.L7f17e402019-03-15 17:40:41 +0530290
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400291 /*
292 * 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 -0700293 * if resources are shared across interfaces then SharedPoolID is given a positive number.
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400294 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700295 for _, FirstIntfPoolID = range techRange.IntfIds {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400296 // skip the intf id 0
297 if FirstIntfPoolID == 0 {
298 continue
299 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400300 break
301 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530302
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700303 for _, RangePool := range techRange.Pools {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400304 if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400305 SharedPoolID = FirstIntfPoolID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400306 } else if RangePool.Sharing == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_SAME_TECH {
307 SharedPoolID = FirstIntfPoolID
308 } else {
309 SharedPoolID = 0
310 }
311 if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID {
312 ONUIDStart = RangePool.Start
313 ONUIDEnd = RangePool.End
314 ONUIDShared = RangePool.Sharing
315 ONUIDSharedPoolID = SharedPoolID
316 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID {
317 AllocIDStart = RangePool.Start
318 AllocIDEnd = RangePool.End
319 AllocIDShared = RangePool.Sharing
320 AllocIDSharedPoolID = SharedPoolID
321 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID {
322 GEMPortIDStart = RangePool.Start
323 GEMPortIDEnd = RangePool.End
324 GEMPortIDShared = RangePool.Sharing
325 GEMPortIDSharedPoolID = SharedPoolID
326 } else if RangePool.Type == openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID {
327 FlowIDStart = RangePool.Start
328 FlowIDEnd = RangePool.End
329 FlowIDShared = RangePool.Sharing
330 FlowIDSharedPoolID = SharedPoolID
331 }
332 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530333
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700334 log.Debugw("Device info init", log.Fields{"technology": techRange.Technology,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400335 "onu_id_start": ONUIDStart, "onu_id_end": ONUIDEnd, "onu_id_shared_pool_id": ONUIDSharedPoolID,
336 "alloc_id_start": AllocIDStart, "alloc_id_end": AllocIDEnd,
337 "alloc_id_shared_pool_id": AllocIDSharedPoolID,
338 "gemport_id_start": GEMPortIDStart, "gemport_id_end": GEMPortIDEnd,
339 "gemport_id_shared_pool_id": GEMPortIDSharedPoolID,
340 "flow_id_start": FlowIDStart,
341 "flow_id_end_idx": FlowIDEnd,
342 "flow_id_shared_pool_id": FlowIDSharedPoolID,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700343 "intf_ids": techRange.IntfIds,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400344 "uni_id_start": 0,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700345 "uni_id_end_idx": 1, /*MaxUNIIDperONU()*/
346 })
Abhilash S.L7f17e402019-03-15 17:40:41 +0530347
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700348 ponRMgr.InitDefaultPONResourceRanges(ONUIDStart, ONUIDEnd, ONUIDSharedPoolID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400349 AllocIDStart, AllocIDEnd, AllocIDSharedPoolID,
350 GEMPortIDStart, GEMPortIDEnd, GEMPortIDSharedPoolID,
351 FlowIDStart, FlowIDEnd, FlowIDSharedPoolID, 0, 1,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700352 devInfo.PonPorts, techRange.IntfIds)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530353
Girish Gowdru0c588b22019-04-23 23:24:56 -0400354 // For global sharing, make sure to refresh both local and global resource manager instances' range
Abhilash S.L7f17e402019-03-15 17:40:41 +0530355
Girish Gowdru0c588b22019-04-23 23:24:56 -0400356 if ONUIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700357 globalPONRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400358 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700359 ponRMgr.UpdateRanges(ponrmgr.ONU_ID_START_IDX, ONUIDStart, ponrmgr.ONU_ID_END_IDX, ONUIDEnd,
360 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400361 }
362 if AllocIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700363 globalPONRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400364 "", 0, nil)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530365
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700366 ponRMgr.UpdateRanges(ponrmgr.ALLOC_ID_START_IDX, AllocIDStart, ponrmgr.ALLOC_ID_END_IDX, AllocIDEnd,
367 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400368 }
369 if GEMPortIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700370 globalPONRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400371 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700372 ponRMgr.UpdateRanges(ponrmgr.GEMPORT_ID_START_IDX, GEMPortIDStart, ponrmgr.GEMPORT_ID_END_IDX, GEMPortIDEnd,
373 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400374 }
375 if FlowIDShared == openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_ALL_TECH {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700376 globalPONRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400377 "", 0, nil)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700378 ponRMgr.UpdateRanges(ponrmgr.FLOW_ID_START_IDX, FlowIDStart, ponrmgr.FLOW_ID_END_IDX, FlowIDEnd,
379 "", 0, globalPONRMgr)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400380 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530381
Girish Gowdru0c588b22019-04-23 23:24:56 -0400382 // Make sure loaded range fits the platform bit encoding ranges
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700383 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 +0530384}
385
Devmalya Paul495b94a2019-08-27 19:42:00 -0400386// Delete clears used resources for the particular olt device being deleted
387func (RsrcMgr *OpenOltResourceMgr) Delete() error {
388 /* TODO
389 def __del__(self):
390 self.log.info("clearing-device-resource-pool")
391 for key, resource_mgr in self.resource_mgrs.iteritems():
392 resource_mgr.clear_device_resource_pool()
Abhilash S.L7f17e402019-03-15 17:40:41 +0530393
Devmalya Paul495b94a2019-08-27 19:42:00 -0400394 def assert_pon_id_limit(self, pon_intf_id):
395 assert pon_intf_id in self.resource_mgrs
Abhilash S.L7f17e402019-03-15 17:40:41 +0530396
Devmalya Paul495b94a2019-08-27 19:42:00 -0400397 def assert_onu_id_limit(self, pon_intf_id, onu_id):
398 self.assert_pon_id_limit(pon_intf_id)
399 self.resource_mgrs[pon_intf_id].assert_resource_limits(onu_id, PONResourceManager.ONU_ID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530400
Devmalya Paul495b94a2019-08-27 19:42:00 -0400401 @property
402 def max_uni_id_per_onu(self):
403 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 +0530404
Devmalya Paul495b94a2019-08-27 19:42:00 -0400405 def assert_uni_id_limit(self, pon_intf_id, onu_id, uni_id):
406 self.assert_onu_id_limit(pon_intf_id, onu_id)
407 self.resource_mgrs[pon_intf_id].assert_resource_limits(uni_id, PONResourceManager.UNI_ID)
408 */
409 for _, rsrcMgr := range RsrcMgr.ResourceMgrs {
410 if err := rsrcMgr.ClearDeviceResourcePool(); err != nil {
411 log.Debug("Failed to clear device resource pool")
412 return err
413 }
414 }
415 log.Debug("Cleared device resource pool")
416 return nil
417}
Abhilash S.L7f17e402019-03-15 17:40:41 +0530418
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700419// GetONUID returns the available OnuID for the given pon-port
420func (RsrcMgr *OpenOltResourceMgr) GetONUID(ponIntfID uint32) (uint32, error) {
salmansiddiqui352a45c2019-08-19 10:15:36 +0000421 // Check if Pon Interface ID is present in Resource-manager-map
422 if _, ok := RsrcMgr.ResourceMgrs[ponIntfID]; !ok {
423 err := errors.New("invalid-pon-interface-" + strconv.Itoa(int(ponIntfID)))
424 return 0, err
425 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400426 // Get ONU id for a provided pon interface ID.
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700427 ONUID, err := RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400428 ponrmgr.ONU_ID, 1)
429 if err != nil {
430 log.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700431 ponIntfID, ponrmgr.ONU_ID)
cbabuabf02352019-10-15 13:14:56 +0200432 return 0, err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400433 }
434 if ONUID != nil {
Devmalya Paul495b94a2019-08-27 19:42:00 -0400435 RsrcMgr.ResourceMgrs[ponIntfID].InitResourceMap(fmt.Sprintf("%d,%d", ponIntfID, ONUID[0]))
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700436 return ONUID[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400437 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530438
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700439 return 0, err // return OnuID 0 on error
Abhilash S.L7f17e402019-03-15 17:40:41 +0530440}
441
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700442// GetFlowIDInfo returns the slice of flow info of the given pon-port
443// Note: For flows which trap from the NNI and not really associated with any particular
444// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530445func (RsrcMgr *OpenOltResourceMgr) GetFlowIDInfo(ponIntfID uint32, onuID int32, uniID int32, flowID uint32) *[]FlowInfo {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530446 var flows []FlowInfo
447
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700448 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
449 if err := RsrcMgr.ResourceMgrs[ponIntfID].GetFlowIDInfo(FlowPath, flowID, &flows); err != nil {
450 log.Errorw("Error while getting flows from KV store", log.Fields{"flowId": flowID})
Abhilash S.L8ee90712019-04-29 16:24:22 +0530451 return nil
452 }
453 if len(flows) == 0 {
454 log.Debugw("No flowInfo found in KV store", log.Fields{"flowPath": FlowPath})
455 return nil
456 }
457 return &flows
458}
459
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700460// GetCurrentFlowIDsForOnu fetches flow ID from the resource manager
461// Note: For flows which trap from the NNI and not really associated with any particular
462// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530463func (RsrcMgr *OpenOltResourceMgr) GetCurrentFlowIDsForOnu(PONIntfID uint32, ONUID int32, UNIID int32) []uint32 {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700464
Abhilash S.L8ee90712019-04-29 16:24:22 +0530465 FlowPath := fmt.Sprintf("%d,%d,%d", PONIntfID, ONUID, UNIID)
Serkant Uluderya89ff40c2019-10-17 16:02:25 -0700466 if mgrs, exist := RsrcMgr.ResourceMgrs[PONIntfID]; exist {
467 return mgrs.GetCurrentFlowIDsForOnu(FlowPath)
468 }
469 return nil
Abhilash S.L8ee90712019-04-29 16:24:22 +0530470}
471
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700472// UpdateFlowIDInfo updates flow info for the given pon interface, onu id, and uni id
473// Note: For flows which trap from the NNI and not really associated with any particular
474// ONU (like LLDP), the onu_id and uni_id is set as -1. The intf_id is the NNI intf_id.
475func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDInfo(ponIntfID int32, onuID int32, uniID int32,
476 flowID uint32, flowData *[]FlowInfo) error {
477 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
478 return RsrcMgr.ResourceMgrs[uint32(ponIntfID)].UpdateFlowIDInfoForOnu(FlowPath, flowID, *flowData)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530479}
480
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700481// GetFlowID return flow ID for a given pon interface id, onu id and uni id
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530482func (RsrcMgr *OpenOltResourceMgr) GetFlowID(ponIntfID uint32, ONUID int32, uniID int32,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400483 gemportID uint32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700484 flowStoreCookie uint64,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400485 flowCategory string, vlanPcp ...uint32) (uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530486
Girish Gowdru0c588b22019-04-23 23:24:56 -0400487 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700488 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, ONUID, uniID)
489 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(FlowPath)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400490 if FlowIDs != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700491 log.Debugw("Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "ONUID": ONUID, "uniID": uniID, "KVpath": FlowPath})
492 for _, flowID := range FlowIDs {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530493 FlowInfo := RsrcMgr.GetFlowIDInfo(ponIntfID, int32(ONUID), int32(uniID), uint32(flowID))
salmansiddiqui7ac62132019-08-22 03:58:50 +0000494 er := getFlowIDFromFlowInfo(FlowInfo, flowID, gemportID, flowStoreCookie, flowCategory, vlanPcp...)
495 if er == nil {
496 return flowID, er
Abhilash S.L8ee90712019-04-29 16:24:22 +0530497 }
498 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400499 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530500 log.Debug("No matching flows with flow cookie or flow category, allocating new flowid")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700501 FlowIDs, err = RsrcMgr.ResourceMgrs[ponIntfID].GetResourceID(ponIntfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400502 ponrmgr.FLOW_ID, 1)
503 if err != nil {
504 log.Errorf("Failed to get resource for interface %d for type %s",
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700505 ponIntfID, ponrmgr.FLOW_ID)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400506 return uint32(0), err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400507 }
508 if FlowIDs != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700509 _ = RsrcMgr.ResourceMgrs[ponIntfID].UpdateFlowIDForOnu(FlowPath, FlowIDs[0], true)
510 return FlowIDs[0], err
Girish Gowdru0c588b22019-04-23 23:24:56 -0400511 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530512
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700513 return 0, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530514}
515
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700516// GetAllocID return the first Alloc ID for a given pon interface id and onu id and then update the resource map on
517// the KV store with the list of alloc_ids allocated for the pon_intf_onu_id tuple
518// Currently of all the alloc_ids available, it returns the first alloc_id in the list for tha given ONU
519func (RsrcMgr *OpenOltResourceMgr) GetAllocID(intfID uint32, onuID uint32, uniID uint32) uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530520
Girish Gowdru0c588b22019-04-23 23:24:56 -0400521 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700522 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
523 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400524 if AllocID != nil {
525 // Since we support only one alloc_id for the ONU at the moment,
526 // return the first alloc_id in the list, if available, for that
527 // ONU.
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700528 log.Debugw("Retrieved alloc ID from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400529 return AllocID[0]
530 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700531 AllocID, err = RsrcMgr.ResourceMgrs[intfID].GetResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400532 ponrmgr.ALLOC_ID, 1)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530533
Girish Gowdru0c588b22019-04-23 23:24:56 -0400534 if AllocID == nil || err != nil {
535 log.Error("Failed to allocate alloc id")
536 return 0
537 }
538 // update the resource map on KV store with the list of alloc_id
539 // allocated for the pon_intf_onu_id tuple
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700540 err = RsrcMgr.ResourceMgrs[intfID].UpdateAllocIdsForOnu(IntfOnuIDUniID, AllocID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400541 if err != nil {
542 log.Error("Failed to update Alloc ID")
543 return 0
544 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530545 log.Debugw("Allocated new Tcont from pon resource mgr", log.Fields{"AllocID": AllocID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400546 return AllocID[0]
Abhilash S.L7f17e402019-03-15 17:40:41 +0530547}
548
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700549// UpdateAllocIdsForOnu updates alloc ids in kv store for a given pon interface id, onu id and uni id
550func (RsrcMgr *OpenOltResourceMgr) UpdateAllocIdsForOnu(ponPort uint32, onuID uint32, uniID uint32, allocID []uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530551
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700552 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
553 return RsrcMgr.ResourceMgrs[ponPort].UpdateAllocIdsForOnu(IntfOnuIDUniID,
554 allocID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530555}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700556
557// GetCurrentGEMPortIDsForOnu returns gem ports for given pon interface , onu id and uni id
558func (RsrcMgr *OpenOltResourceMgr) GetCurrentGEMPortIDsForOnu(intfID uint32, onuID uint32,
559 uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530560
Girish Gowdru0c588b22019-04-23 23:24:56 -0400561 /* Get gem ports for given pon interface , onu id and uni id. */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530562
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700563 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
564 return RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530565}
566
Gamze Abakafee36392019-10-03 11:17:24 +0000567// GetCurrentAllocIDsForOnu returns alloc ids for given pon interface and onu id
568func (RsrcMgr *OpenOltResourceMgr) GetCurrentAllocIDsForOnu(intfID uint32, onuID uint32, uniID uint32) []uint32 {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530569
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700570 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
571 AllocID := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400572 if AllocID != nil {
Gamze Abakafee36392019-10-03 11:17:24 +0000573 return AllocID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400574 }
Gamze Abakafee36392019-10-03 11:17:24 +0000575 return []uint32{}
576}
577
578// RemoveAllocIDForOnu removes the alloc id for given pon interface, onu id, uni id and alloc id
579func (RsrcMgr *OpenOltResourceMgr) RemoveAllocIDForOnu(intfID uint32, onuID uint32, uniID uint32, allocID uint32) {
580 allocIDs := RsrcMgr.GetCurrentAllocIDsForOnu(intfID, onuID, uniID)
581 for i := 0; i < len(allocIDs); i++ {
582 if allocIDs[i] == allocID {
583 allocIDs = append(allocIDs[:i], allocIDs[i+1:]...)
584 break
585 }
586 }
587 err := RsrcMgr.UpdateAllocIdsForOnu(intfID, onuID, uniID, allocIDs)
588 if err != nil {
589 log.Errorf("Failed to Remove Alloc Id For Onu. IntfID %d onuID %d uniID %d allocID %d",
590 intfID, onuID, uniID, allocID)
591 }
592}
593
594// RemoveGemPortIDForOnu removes the gem port id for given pon interface, onu id, uni id and gem port id
595func (RsrcMgr *OpenOltResourceMgr) RemoveGemPortIDForOnu(intfID uint32, onuID uint32, uniID uint32, gemPortID uint32) {
596 gemPortIDs := RsrcMgr.GetCurrentGEMPortIDsForOnu(intfID, onuID, uniID)
597 for i := 0; i < len(gemPortIDs); i++ {
598 if gemPortIDs[i] == gemPortID {
599 gemPortIDs = append(gemPortIDs[:i], gemPortIDs[i+1:]...)
600 break
601 }
602 }
603 err := RsrcMgr.UpdateGEMPortIDsForOnu(intfID, onuID, uniID, gemPortIDs)
604 if err != nil {
605 log.Errorf("Failed to Remove Gem Id For Onu. IntfID %d onuID %d uniID %d gemPortId %d",
606 intfID, onuID, uniID, gemPortID)
607 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530608}
609
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700610// UpdateGEMportsPonportToOnuMapOnKVStore updates onu and uni id associated with the gem port to the kv store
611// This stored information is used when packet_indication is received and we need to derive the ONU Id for which
612// the packet arrived based on the pon_intf and gemport available in the packet_indication
613func (RsrcMgr *OpenOltResourceMgr) UpdateGEMportsPonportToOnuMapOnKVStore(gemPorts []uint32, PonPort uint32,
614 onuID uint32, uniID uint32) error {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530615
Girish Gowdru0c588b22019-04-23 23:24:56 -0400616 /* Update onu and uni id associated with the gem port to the kv store. */
617 var IntfGEMPortPath string
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700618 Data := fmt.Sprintf("%d %d", onuID, uniID)
619 for _, GEM := range gemPorts {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400620 IntfGEMPortPath = fmt.Sprintf("%d,%d", PonPort, GEM)
621 Val, err := json.Marshal(Data)
622 if err != nil {
623 log.Error("failed to Marshal")
624 return err
625 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700626
Girish Gowdru0c588b22019-04-23 23:24:56 -0400627 if err = RsrcMgr.KVStore.Put(IntfGEMPortPath, Val); err != nil {
628 log.Errorf("Failed to update resource %s", IntfGEMPortPath)
629 return err
630 }
631 }
632 return nil
Abhilash S.L7f17e402019-03-15 17:40:41 +0530633}
634
Gamze Abakafee36392019-10-03 11:17:24 +0000635// RemoveGEMportPonportToOnuMapOnKVStore removes the relationship between the gem port and pon port
636func (RsrcMgr *OpenOltResourceMgr) RemoveGEMportPonportToOnuMapOnKVStore(GemPort uint32, PonPort uint32) {
637 IntfGEMPortPath := fmt.Sprintf("%d,%d", PonPort, GemPort)
638 err := RsrcMgr.KVStore.Delete(IntfGEMPortPath)
639 if err != nil {
640 log.Errorf("Failed to Remove Gem port-Pon port to onu map on kv store. Gem %d PonPort %d", GemPort, PonPort)
641 }
642}
643
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700644// GetGEMPortID gets gem port id for a particular pon port, onu id and uni id and then update the resource map on
645// the KV store with the list of gemport_id allocated for the pon_intf_onu_id tuple
646func (RsrcMgr *OpenOltResourceMgr) GetGEMPortID(ponPort uint32, onuID uint32,
647 uniID uint32, NumOfPorts uint32) ([]uint32, error) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530648
Girish Gowdru0c588b22019-04-23 23:24:56 -0400649 /* Get gem port id for a particular pon port, onu id
650 and uni id.
651 */
Abhilash S.L7f17e402019-03-15 17:40:41 +0530652
Girish Gowdru0c588b22019-04-23 23:24:56 -0400653 var err error
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700654 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530655
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700656 GEMPortList := RsrcMgr.ResourceMgrs[ponPort].GetCurrentGEMPortIDsForOnu(IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400657 if GEMPortList != nil {
658 return GEMPortList, nil
659 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530660
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700661 GEMPortList, err = RsrcMgr.ResourceMgrs[ponPort].GetResourceID(ponPort,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400662 ponrmgr.GEMPORT_ID, NumOfPorts)
663 if err != nil && GEMPortList == nil {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530664 log.Errorf("Failed to get gem port id for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400665 return nil, err
666 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530667
Girish Gowdru0c588b22019-04-23 23:24:56 -0400668 // update the resource map on KV store with the list of gemport_id
669 // allocated for the pon_intf_onu_id tuple
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700670 err = RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400671 GEMPortList)
672 if err != nil {
Abhilash S.L8ee90712019-04-29 16:24:22 +0530673 log.Errorf("Failed to update GEM ports to kv store for %s", IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400674 return nil, err
675 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700676 _ = RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(GEMPortList, ponPort,
677 onuID, uniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400678 return GEMPortList, err
Abhilash S.L7f17e402019-03-15 17:40:41 +0530679}
680
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700681// UpdateGEMPortIDsForOnu updates gemport ids on to the kv store for a given pon port, onu id and uni id
682func (RsrcMgr *OpenOltResourceMgr) UpdateGEMPortIDsForOnu(ponPort uint32, onuID uint32,
683 uniID uint32, GEMPortList []uint32) error {
684 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", ponPort, onuID, uniID)
685 return RsrcMgr.ResourceMgrs[ponPort].UpdateGEMPortIDsForOnu(IntfOnuIDUniID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400686 GEMPortList)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530687
688}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700689
690// FreeonuID releases(make free) onu id for a particular pon-port
691func (RsrcMgr *OpenOltResourceMgr) FreeonuID(intfID uint32, onuID []uint32) {
692
693 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID, ponrmgr.ONU_ID, onuID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530694
Girish Gowdru0c588b22019-04-23 23:24:56 -0400695 /* Free onu id for a particular interface.*/
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700696 var IntfonuID string
697 for _, onu := range onuID {
698 IntfonuID = fmt.Sprintf("%d,%d", intfID, onu)
699 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(IntfonuID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400700 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530701}
702
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700703// FreeFlowID returns the free flow id for a given interface, onu id and uni id
Devmalya Paul495b94a2019-08-27 19:42:00 -0400704func (RsrcMgr *OpenOltResourceMgr) FreeFlowID(IntfID uint32, onuID int32,
705 uniID int32, FlowID uint32) {
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400706 var IntfONUID string
707 var err error
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530708 FlowIds := make([]uint32, 0)
709
710 FlowIds = append(FlowIds, FlowID)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700711 IntfONUID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
712 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(IntfONUID, FlowID, false)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400713 if err != nil {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530714 log.Errorw("Failed to Update flow id for", log.Fields{"intf": IntfONUID})
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400715 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700716 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(IntfONUID, FlowID)
Abhilash Laxmeshwar83695912019-10-01 14:37:19 +0530717 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(IntfID, ponrmgr.FLOW_ID, FlowIds)
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400718}
719
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700720// FreeFlowIDs releases the flow Ids
721func (RsrcMgr *OpenOltResourceMgr) FreeFlowIDs(IntfID uint32, onuID uint32,
722 uniID uint32, FlowID []uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530723
Girish Gowdru0c588b22019-04-23 23:24:56 -0400724 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(IntfID, ponrmgr.FLOW_ID, FlowID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530725
Abhilash S.L8ee90712019-04-29 16:24:22 +0530726 var IntfOnuIDUniID string
Girish Gowdru0c588b22019-04-23 23:24:56 -0400727 var err error
728 for _, flow := range FlowID {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700729 IntfOnuIDUniID = fmt.Sprintf("%d,%d,%d", IntfID, onuID, uniID)
Abhilash S.L8ee90712019-04-29 16:24:22 +0530730 err = RsrcMgr.ResourceMgrs[IntfID].UpdateFlowIDForOnu(IntfOnuIDUniID, flow, false)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400731 if err != nil {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530732 log.Errorw("Failed to Update flow id for", log.Fields{"intf": IntfOnuIDUniID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400733 }
Abhilash S.L8ee90712019-04-29 16:24:22 +0530734 RsrcMgr.ResourceMgrs[IntfID].RemoveFlowIDInfo(IntfOnuIDUniID, flow)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400735 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530736}
737
Gamze Abakafee36392019-10-03 11:17:24 +0000738// FreeAllocID frees AllocID on the PON resource pool and also frees the allocID association
739// for the given OLT device.
740func (RsrcMgr *OpenOltResourceMgr) FreeAllocID(IntfID uint32, onuID uint32,
741 uniID uint32, allocID uint32) {
742 RsrcMgr.RemoveAllocIDForOnu(IntfID, onuID, uniID, allocID)
743 allocIDs := make([]uint32, 0)
744 allocIDs = append(allocIDs, allocID)
745 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(IntfID, ponrmgr.ALLOC_ID, allocIDs)
746}
747
748// FreeGemPortID frees GemPortID on the PON resource pool and also frees the gemPortID association
749// for the given OLT device.
750func (RsrcMgr *OpenOltResourceMgr) FreeGemPortID(IntfID uint32, onuID uint32,
751 uniID uint32, gemPortID uint32) {
752 RsrcMgr.RemoveGemPortIDForOnu(IntfID, onuID, uniID, gemPortID)
753 gemPortIDs := make([]uint32, 0)
754 gemPortIDs = append(gemPortIDs, gemPortID)
755 RsrcMgr.ResourceMgrs[IntfID].FreeResourceID(IntfID, ponrmgr.GEMPORT_ID, gemPortIDs)
756}
757
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700758// FreePONResourcesForONU make the pon resources free for a given pon interface and onu id, and the clears the
759// resource map and the onuID associated with (pon_intf_id, gemport_id) tuple,
760func (RsrcMgr *OpenOltResourceMgr) FreePONResourcesForONU(intfID uint32, onuID uint32, uniID uint32) {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530761
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700762 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530763
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700764 AllocIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentAllocIDForOnu(IntfOnuIDUniID)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530765
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700766 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400767 ponrmgr.ALLOC_ID,
768 AllocIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530769
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700770 GEMPortIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentGEMPortIDsForOnu(IntfOnuIDUniID)
771 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400772 ponrmgr.GEMPORT_ID,
773 GEMPortIDs)
Abhilash S.L7f17e402019-03-15 17:40:41 +0530774
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700775 FlowIDs := RsrcMgr.ResourceMgrs[intfID].GetCurrentFlowIDsForOnu(IntfOnuIDUniID)
776 RsrcMgr.ResourceMgrs[intfID].FreeResourceID(intfID,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400777 ponrmgr.FLOW_ID,
778 FlowIDs)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400779 // Clear resource map associated with (pon_intf_id, gemport_id) tuple.
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700780 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(IntfOnuIDUniID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400781 // Clear the ONU Id associated with the (pon_intf_id, gemport_id) tuple.
782 for _, GEM := range GEMPortIDs {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700783 _ = RsrcMgr.KVStore.Delete(fmt.Sprintf("%d,%d", intfID, GEM))
Girish Gowdru0c588b22019-04-23 23:24:56 -0400784 }
Abhilash S.L7f17e402019-03-15 17:40:41 +0530785}
786
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700787// IsFlowCookieOnKVStore checks if the given flow cookie is present on the kv store
788// Returns true if the flow cookie is found, otherwise it returns false
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530789func (RsrcMgr *OpenOltResourceMgr) IsFlowCookieOnKVStore(ponIntfID uint32, onuID int32, uniID int32,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700790 flowStoreCookie uint64) bool {
Abhilash S.L7f17e402019-03-15 17:40:41 +0530791
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700792 FlowPath := fmt.Sprintf("%d,%d,%d", ponIntfID, onuID, uniID)
793 FlowIDs := RsrcMgr.ResourceMgrs[ponIntfID].GetCurrentFlowIDsForOnu(FlowPath)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400794 if FlowIDs != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700795 log.Debugw("Found flowId(s) for this ONU", log.Fields{"pon": ponIntfID, "onuID": onuID, "uniID": uniID, "KVpath": FlowPath})
796 for _, flowID := range FlowIDs {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530797 FlowInfo := RsrcMgr.GetFlowIDInfo(ponIntfID, int32(onuID), int32(uniID), uint32(flowID))
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400798 if FlowInfo != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700799 log.Debugw("Found flows", log.Fields{"flows": *FlowInfo, "flowId": flowID})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400800 for _, Info := range *FlowInfo {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700801 if Info.FlowStoreCookie == flowStoreCookie {
802 log.Debug("Found flow matching with flowStore cookie", log.Fields{"flowId": flowID, "flowStoreCookie": flowStoreCookie})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400803 return true
804 }
805 }
806 }
807 }
808 }
809 return false
810}
Manikkaraj kb1d51442019-07-23 10:41:02 -0400811
salmansiddiqui7ac62132019-08-22 03:58:50 +0000812// GetTechProfileIDForOnu fetches Tech-Profile-ID from the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000813// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
814func (RsrcMgr *OpenOltResourceMgr) GetTechProfileIDForOnu(IntfID uint32, OnuID uint32, UniID uint32) []uint32 {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000815 Path := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000816 var Data []uint32
salmansiddiqui7ac62132019-08-22 03:58:50 +0000817 Value, err := RsrcMgr.KVStore.Get(Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400818 if err == nil {
819 if Value != nil {
820 Val, err := kvstore.ToByte(Value.Value)
821 if err != nil {
822 log.Errorw("Failed to convert into byte array", log.Fields{"error": err})
823 return Data
824 }
825 if err = json.Unmarshal(Val, &Data); err != nil {
826 log.Error("Failed to unmarshal", log.Fields{"error": err})
827 return Data
828 }
829 }
830 } else {
831 log.Errorf("Failed to get TP id from kvstore for path %s", Path)
832 }
833 log.Debugf("Getting TP id %d from path %s", Data, Path)
834 return Data
835
836}
837
Gamze Abakafee36392019-10-03 11:17:24 +0000838// RemoveTechProfileIDsForOnu deletes all tech profile ids from the KV-Store for the given onu based on the path
839// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
840func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDsForOnu(IntfID uint32, OnuID uint32, UniID uint32) error {
salmansiddiqui7ac62132019-08-22 03:58:50 +0000841 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
842 if err := RsrcMgr.KVStore.Delete(IntfOnuUniID); err != nil {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530843 log.Errorw("Failed to delete techprofile id resource in KV store", log.Fields{"path": IntfOnuUniID})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400844 return err
845 }
846 return nil
847}
848
Gamze Abakafee36392019-10-03 11:17:24 +0000849// RemoveTechProfileIDForOnu deletes a specific tech profile id from the KV-Store for the given onu based on the path
850// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
851func (RsrcMgr *OpenOltResourceMgr) RemoveTechProfileIDForOnu(IntfID uint32, OnuID uint32, UniID uint32, TpID uint32) error {
852 tpIDList := RsrcMgr.GetTechProfileIDForOnu(IntfID, OnuID, UniID)
853 for i, tpIDInList := range tpIDList {
854 if tpIDInList == TpID {
855 tpIDList = append(tpIDList[:i], tpIDList[i+1:]...)
856 }
857 }
858 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
859 Value, err := json.Marshal(tpIDList)
860 if err != nil {
861 log.Error("failed to Marshal")
862 return err
863 }
864 if err = RsrcMgr.KVStore.Put(IntfOnuUniID, Value); err != nil {
865 log.Errorf("Failed to update resource %s", IntfOnuUniID)
866 return err
867 }
868 return err
869}
870
871// UpdateTechProfileIDForOnu updates (put) already present tech-profile-id for the given onu based on the path
872// This path is formed as the following: {IntfID, OnuID, UniID}/tp_id
salmansiddiqui7ac62132019-08-22 03:58:50 +0000873func (RsrcMgr *OpenOltResourceMgr) UpdateTechProfileIDForOnu(IntfID uint32, OnuID uint32,
874 UniID uint32, TpID uint32) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400875 var Value []byte
876 var err error
877
salmansiddiqui7ac62132019-08-22 03:58:50 +0000878 IntfOnuUniID := fmt.Sprintf(TpIDPathSuffix, IntfID, OnuID, UniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000879
880 tpIDList := RsrcMgr.GetTechProfileIDForOnu(IntfID, OnuID, UniID)
881 for _, value := range tpIDList {
882 if value == TpID {
883 log.Debugf("TpID %d is already in tpIdList for the path %s", TpID, IntfOnuUniID)
884 return err
885 }
886 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000887 log.Debugf("updating tp id %d on path %s", TpID, IntfOnuUniID)
Gamze Abakafee36392019-10-03 11:17:24 +0000888 tpIDList = append(tpIDList, TpID)
889 Value, err = json.Marshal(tpIDList)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400890 if err != nil {
891 log.Error("failed to Marshal")
892 return err
893 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000894 if err = RsrcMgr.KVStore.Put(IntfOnuUniID, Value); err != nil {
895 log.Errorf("Failed to update resource %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400896 return err
897 }
898 return err
899}
900
salmansiddiqui7ac62132019-08-22 03:58:50 +0000901// UpdateMeterIDForOnu updates the meter id in the KV-Store for the given onu based on the path
Gamze Abakafee36392019-10-03 11:17:24 +0000902// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
salmansiddiqui7ac62132019-08-22 03:58:50 +0000903func (RsrcMgr *OpenOltResourceMgr) UpdateMeterIDForOnu(Direction string, IntfID uint32, OnuID uint32,
Gamze Abakafee36392019-10-03 11:17:24 +0000904 UniID uint32, TpID uint32, MeterConfig *ofp.OfpMeterConfig) error {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400905 var Value []byte
906 var err error
907
Gamze Abakafee36392019-10-03 11:17:24 +0000908 IntfOnuUniID := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400909 Value, err = json.Marshal(*MeterConfig)
910 if err != nil {
911 log.Error("failed to Marshal meter config")
912 return err
913 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000914 if err = RsrcMgr.KVStore.Put(IntfOnuUniID, Value); err != nil {
915 log.Errorf("Failed to store meter into KV store %s", IntfOnuUniID)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400916 return err
917 }
918 return err
919}
920
Gamze Abakafee36392019-10-03 11:17:24 +0000921// GetMeterIDForOnu fetches the meter id from the kv store for the given onu based on the path
922// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
923func (RsrcMgr *OpenOltResourceMgr) GetMeterIDForOnu(Direction string, IntfID uint32, OnuID uint32,
924 UniID uint32, TpID uint32) (*ofp.OfpMeterConfig, error) {
925 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400926 var meterConfig ofp.OfpMeterConfig
salmansiddiqui7ac62132019-08-22 03:58:50 +0000927 Value, err := RsrcMgr.KVStore.Get(Path)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400928 if err == nil {
929 if Value != nil {
930 log.Debug("Found meter in KV store", log.Fields{"Direction": Direction})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000931 Val, er := kvstore.ToByte(Value.Value)
932 if er != nil {
933 log.Errorw("Failed to convert into byte array", log.Fields{"error": er})
934 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400935 }
salmansiddiqui7ac62132019-08-22 03:58:50 +0000936 if er = json.Unmarshal(Val, &meterConfig); er != nil {
937 log.Error("Failed to unmarshal meterconfig", log.Fields{"error": er})
938 return nil, er
Manikkaraj kb1d51442019-07-23 10:41:02 -0400939 }
940 } else {
941 log.Debug("meter-does-not-exists-in-KVStore")
942 return nil, err
943 }
944 } else {
945 log.Errorf("Failed to get Meter config from kvstore for path %s", Path)
946
947 }
948 return &meterConfig, err
949}
950
Gamze Abakafee36392019-10-03 11:17:24 +0000951// RemoveMeterIDForOnu deletes the meter id from the kV-Store for the given onu based on the path
952// This path is formed as the following: <(pon_id, onu_id, uni_id)>/<tp_id>/meter_id/<direction>
953func (RsrcMgr *OpenOltResourceMgr) RemoveMeterIDForOnu(Direction string, IntfID uint32, OnuID uint32,
954 UniID uint32, TpID uint32) error {
955 Path := fmt.Sprintf(MeterIDPathSuffix, IntfID, OnuID, UniID, TpID, Direction)
salmansiddiqui7ac62132019-08-22 03:58:50 +0000956 if err := RsrcMgr.KVStore.Delete(Path); err != nil {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400957 log.Errorf("Failed to delete meter id %s from kvstore ", Path)
958 return err
959 }
960 return nil
961}
salmansiddiqui7ac62132019-08-22 03:58:50 +0000962
963func getFlowIDFromFlowInfo(FlowInfo *[]FlowInfo, flowID, gemportID uint32, flowStoreCookie uint64, flowCategory string, vlanPcp ...uint32) error {
964 if FlowInfo != nil {
965 for _, Info := range *FlowInfo {
966 if int32(gemportID) == Info.Flow.GemportId && flowCategory != "" && Info.FlowCategory == flowCategory {
967 log.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
968 if Info.FlowCategory == "HSIA_FLOW" && Info.Flow.Classifier.OPbits == vlanPcp[0] {
969 log.Debug("Found matching vlan pcp ", log.Fields{"flowId": flowID, "Vlanpcp": vlanPcp[0]})
970 return nil
971 }
972 }
973 if int32(gemportID) == Info.Flow.GemportId && flowStoreCookie != 0 && Info.FlowStoreCookie == flowStoreCookie {
974 if flowCategory != "" && Info.FlowCategory == flowCategory {
975 log.Debug("Found flow matching with flow category", log.Fields{"flowId": flowID, "FlowCategory": flowCategory})
976 return nil
977 }
978 }
979 }
980 }
Gamze Abakafee36392019-10-03 11:17:24 +0000981 log.Debugw("the flow can be related to a different service", log.Fields{"flow_info": FlowInfo})
salmansiddiqui7ac62132019-08-22 03:58:50 +0000982 return errors.New("invalid flow-info")
983}
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530984
985//AddGemToOnuGemInfo adds gemport to onugem info kvstore
986func (RsrcMgr *OpenOltResourceMgr) AddGemToOnuGemInfo(intfID uint32, onuID uint32, gemPort uint32) error {
987 var onuGemData []OnuGemInfo
988 var err error
989
990 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(intfID, &onuGemData); err != nil {
991 log.Errorf("failed to get onuifo for intfid %d", intfID)
992 return err
993 }
994 if len(onuGemData) == 0 {
995 log.Errorw("failed to ger Onuid info ", log.Fields{"intfid": intfID, "onuid": onuID})
996 return err
997 }
998
999 for idx, onugem := range onuGemData {
1000 if onugem.OnuID == onuID {
1001 for _, gem := range onuGemData[idx].GemPorts {
1002 if gem == gemPort {
1003 log.Debugw("Gem already present in onugem info, skpping addition", log.Fields{"gem": gem})
1004 return nil
1005 }
1006 }
1007 log.Debugw("Added gem to onugem info", log.Fields{"gem": gemPort})
1008 onuGemData[idx].GemPorts = append(onuGemData[idx].GemPorts, gemPort)
1009 break
1010 }
1011 }
1012 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(intfID, onuGemData)
1013 if err != nil {
1014 log.Error("Failed to add onugem to kv store")
1015 return err
1016 }
1017 return err
1018}
1019
1020//GetOnuGemInfo gets onu gem info from the kvstore per interface
1021func (RsrcMgr *OpenOltResourceMgr) GetOnuGemInfo(IntfID uint32) ([]OnuGemInfo, error) {
1022 var onuGemData []OnuGemInfo
1023
1024 if err := RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(IntfID, &onuGemData); err != nil {
1025 log.Errorf("failed to get onuifo for intfid %d", IntfID)
1026 return nil, err
1027 }
1028
1029 return onuGemData, nil
1030}
1031
1032// AddOnuInfo adds onu info on to the kvstore per interface
1033func (RsrcMgr *OpenOltResourceMgr) AddOnuInfo(IntfID uint32, onuGem OnuGemInfo) error {
1034 var onuGemData []OnuGemInfo
1035 var err error
1036
1037 if err = RsrcMgr.ResourceMgrs[IntfID].GetOnuGemInfo(IntfID, &onuGemData); err != nil {
1038 log.Errorf("failed to get onuifo for intfid %d", IntfID)
1039 return err
1040 }
1041 onuGemData = append(onuGemData, onuGem)
1042 err = RsrcMgr.ResourceMgrs[IntfID].AddOnuGemInfo(IntfID, onuGemData)
1043 if err != nil {
1044 log.Error("Failed to add onugem to kv store")
1045 return err
1046 }
1047
1048 log.Debugw("added onu to onugeminfo", log.Fields{"intf": IntfID, "onugem": onuGem})
1049 return err
1050}
1051
1052// UpdateOnuInfo updates Onuinfo on the kvstore per interface
1053func (RsrcMgr *OpenOltResourceMgr) UpdateOnuInfo(IntfID uint32, onuGem []OnuGemInfo) error {
1054 var onuGemData []OnuGemInfo
1055 var err error
1056
1057 err = RsrcMgr.ResourceMgrs[IntfID].AddOnuGemInfo(IntfID, onuGemData)
1058 if err != nil {
1059 log.Error("Failed to add onugem to kv store")
1060 return err
1061 }
1062
1063 log.Debugw("updated onugeminfo", log.Fields{"intf": IntfID, "onugem": onuGem})
1064 return err
1065}
1066
1067// AddUniPortToOnuInfo adds uni port to the onuinfo kvstore. check if the uni is already present if not update the kv store.
1068func (RsrcMgr *OpenOltResourceMgr) AddUniPortToOnuInfo(intfID uint32, onuID uint32, portNo uint32) {
1069 var onuGemData []OnuGemInfo
1070 var err error
1071
1072 if err = RsrcMgr.ResourceMgrs[intfID].GetOnuGemInfo(intfID, &onuGemData); err != nil {
1073 log.Errorf("failed to get onuifo for intfid %d", intfID)
1074 return
1075 }
1076 for idx, onu := range onuGemData {
1077 if onu.OnuID == onuID {
1078 for _, uni := range onu.UniPorts {
1079 if uni == portNo {
1080 log.Debugw("uni already present in onugem info", log.Fields{"uni": portNo})
1081 return
1082 }
1083 }
1084 onuGemData[idx].UniPorts = append(onuGemData[idx].UniPorts, portNo)
1085 break
1086 }
1087 }
1088 err = RsrcMgr.ResourceMgrs[intfID].AddOnuGemInfo(intfID, onuGemData)
1089 if err != nil {
1090 log.Errorw("Failed to add uin port in onugem to kv store", log.Fields{"uni": portNo})
1091 return
1092 }
1093 return
1094}
1095
1096//UpdateGemPortForPktIn updates gemport for pkt in path to kvstore, path being intfid, onuid, portno
1097func (RsrcMgr *OpenOltResourceMgr) UpdateGemPortForPktIn(pktIn PacketInInfoKey, gemPort uint32) {
1098
1099 path := fmt.Sprintf(OnuPacketINPath, pktIn.IntfID, pktIn.OnuID, pktIn.LogicalPort)
1100 Value, err := json.Marshal(gemPort)
1101 if err != nil {
1102 log.Error("Failed to marshal data")
1103 return
1104 }
1105 if err = RsrcMgr.KVStore.Put(path, Value); err != nil {
1106 log.Errorw("Failed to put to kvstore", log.Fields{"path": path, "value": gemPort})
1107 return
1108 }
1109 log.Debugw("added gem packet in successfully", log.Fields{"path": path, "gem": gemPort})
1110
1111 return
1112}
1113
1114// GetGemPortFromOnuPktIn gets the gem port from onu pkt in path, path being intfid, onuid, portno
1115func (RsrcMgr *OpenOltResourceMgr) GetGemPortFromOnuPktIn(intfID uint32, onuID uint32, logicalPort uint32) (uint32, error) {
1116
1117 var Val []byte
1118 var gemPort uint32
1119
1120 path := fmt.Sprintf(OnuPacketINPath, intfID, onuID, logicalPort)
1121
1122 value, err := RsrcMgr.KVStore.Get(path)
1123 if err != nil {
1124 log.Errorw("Failed to get from kv store", log.Fields{"path": path})
1125 return uint32(0), err
1126 } else if value == nil {
1127 log.Debugw("No pkt in gem found", log.Fields{"path": path})
1128 return uint32(0), nil
1129 }
1130
1131 if Val, err = kvstore.ToByte(value.Value); err != nil {
1132 log.Error("Failed to convert to byte array")
1133 return uint32(0), err
1134 }
1135 if err = json.Unmarshal(Val, &gemPort); err != nil {
1136 log.Error("Failed to unmarshall")
1137 return uint32(0), err
1138 }
1139 log.Debugw("found packein gemport from path", log.Fields{"path": path, "gem": gemPort})
1140
1141 return gemPort, nil
1142}
1143
1144// DelGemPortPktIn deletes the gemport from the pkt in path
1145func (RsrcMgr *OpenOltResourceMgr) DelGemPortPktIn(intfID uint32, onuID uint32, logicalPort uint32) error {
1146
1147 path := fmt.Sprintf(OnuPacketINPath, intfID, onuID, logicalPort)
1148 if err := RsrcMgr.KVStore.Delete(path); err != nil {
1149 log.Errorf("Falied to remove resource %s", path)
1150 return err
1151 }
1152 return nil
1153}
1154
1155// DelOnuGemInfoForIntf deletes the onugem info from kvstore per interface
1156func (RsrcMgr *OpenOltResourceMgr) DelOnuGemInfoForIntf(intfID uint32) error {
1157 if err := RsrcMgr.ResourceMgrs[intfID].DelOnuGemInfoForIntf(intfID); err != nil {
1158 log.Errorw("failed to delete onu gem info for", log.Fields{"intfid": intfID})
1159 return err
1160 }
1161 return nil
1162}
1163
1164//GetNNIFromKVStore gets NNi intfids from kvstore. path being per device
1165func (RsrcMgr *OpenOltResourceMgr) GetNNIFromKVStore() ([]uint32, error) {
1166
1167 var nni []uint32
1168 var Val []byte
1169
1170 path := fmt.Sprintf(NnniIntfID)
1171 value, err := RsrcMgr.KVStore.Get(path)
1172 if err != nil {
1173 log.Error("failed to get data from kv store")
1174 return nil, err
1175 }
1176 if value != nil {
1177 if Val, err = kvstore.ToByte(value.Value); err != nil {
1178 log.Error("Failed to convert to byte array")
1179 return nil, err
1180 }
1181 if err = json.Unmarshal(Val, &nni); err != nil {
1182 log.Error("Failed to unmarshall")
1183 return nil, err
1184 }
1185 }
1186 return nni, err
1187}
1188
1189// AddNNIToKVStore adds Nni interfaces to kvstore, path being per device.
1190func (RsrcMgr *OpenOltResourceMgr) AddNNIToKVStore(nniIntf uint32) error {
1191 var Value []byte
1192
1193 nni, err := RsrcMgr.GetNNIFromKVStore()
1194 if err != nil {
1195 log.Error("failed to fetch nni interfaces from kv store")
1196 return err
1197 }
1198
1199 path := fmt.Sprintf(NnniIntfID)
1200 nni = append(nni, nniIntf)
1201 Value, err = json.Marshal(nni)
1202 if err != nil {
1203 log.Error("Failed to marshal data")
1204 }
1205 if err = RsrcMgr.KVStore.Put(path, Value); err != nil {
1206 log.Errorw("Failed to put to kvstore", log.Fields{"path": path, "value": Value})
1207 return err
1208 }
1209 log.Debugw("added nni to kv successfully", log.Fields{"path": path, "nni": nniIntf})
1210 return nil
1211}
1212
1213// DelNNiFromKVStore deletes nni interface list from kv store.
1214func (RsrcMgr *OpenOltResourceMgr) DelNNiFromKVStore() error {
1215
1216 path := fmt.Sprintf(NnniIntfID)
1217
1218 if err := RsrcMgr.KVStore.Delete(path); err != nil {
1219 log.Errorw("Failed to delete nni interfaces from kv store", log.Fields{"path": path})
1220 return err
1221 }
1222 return nil
1223}
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301224
1225//UpdateFlowIDsForGem updates flow id per gemport
1226func (RsrcMgr *OpenOltResourceMgr) UpdateFlowIDsForGem(intf uint32, gem uint32, flowIDs []uint32) error {
1227 var val []byte
1228 path := fmt.Sprintf(FlowIDsForGem, intf)
1229
1230 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(intf)
1231 if err != nil {
1232 log.Error("Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
1233 return err
1234 }
1235 if flowsForGem == nil {
1236 flowsForGem = make(map[uint32][]uint32)
1237 }
1238 flowsForGem[gem] = flowIDs
1239 val, err = json.Marshal(flowsForGem)
1240 if err != nil {
1241 log.Error("Failed to marshal data", log.Fields{"error": err})
1242 return err
1243 }
1244 if err = RsrcMgr.KVStore.Put(path, val); err != nil {
1245 log.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
1246 return err
1247 }
1248 log.Debugw("added flowid list for gem to kv successfully", log.Fields{"path": path, "flowidlist": flowsForGem[gem]})
1249 return nil
1250}
1251
1252//DeleteFlowIDsForGem deletes the flowID list entry per gem from kvstore.
1253func (RsrcMgr *OpenOltResourceMgr) DeleteFlowIDsForGem(intf uint32, gem uint32) {
1254 path := fmt.Sprintf(FlowIDsForGem, intf)
1255 var val []byte
1256
1257 flowsForGem, err := RsrcMgr.GetFlowIDsGemMapForInterface(intf)
1258 if err != nil {
1259 log.Error("Failed to ger flowids for interface", log.Fields{"error": err, "intf": intf})
1260 return
1261 }
1262 if flowsForGem == nil {
1263 log.Error("No flowids found ", log.Fields{"intf": intf, "gemport": gem})
1264 return
1265 }
1266 // once we get the flows per gem map from kv , just delete the gem entry from the map
1267 delete(flowsForGem, gem)
1268 // once gem entry is deleted update the kv store.
1269 val, err = json.Marshal(flowsForGem)
1270 if err != nil {
1271 log.Error("Failed to marshal data", log.Fields{"error": err})
1272 return
1273 }
1274 if err = RsrcMgr.KVStore.Put(path, val); err != nil {
1275 log.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
1276 return
1277 }
1278 return
1279}
1280
1281//GetFlowIDsGemMapForInterface gets flowids per gemport and interface
1282func (RsrcMgr *OpenOltResourceMgr) GetFlowIDsGemMapForInterface(intf uint32) (map[uint32][]uint32, error) {
1283 path := fmt.Sprintf(FlowIDsForGem, intf)
1284 var flowsForGem map[uint32][]uint32
1285 var val []byte
1286
1287 value, err := RsrcMgr.KVStore.Get(path)
1288 if err != nil {
1289 log.Error("failed to get data from kv store")
1290 return nil, err
1291 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001292 if value != nil && value.Value != nil {
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301293 if val, err = kvstore.ToByte(value.Value); err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +00001294 log.Error("Failed to convert to byte array ", log.Fields{"error": err})
Abhilash Laxmeshwar275c0742019-11-25 16:47:02 +05301295 return nil, err
1296 }
1297 if err = json.Unmarshal(val, &flowsForGem); err != nil {
1298 log.Error("Failed to unmarshall", log.Fields{"error": err})
1299 return nil, err
1300 }
1301 }
1302 return flowsForGem, nil
1303}
Abhilash Laxmeshwar6d1acb92020-01-17 15:43:03 +05301304
1305//DeleteIntfIDGempMapPath deletes the intf id path used to store flow ids per gem to kvstore.
1306func (RsrcMgr *OpenOltResourceMgr) DeleteIntfIDGempMapPath(intf uint32) {
1307
1308 path := fmt.Sprintf(FlowIDsForGem, intf)
1309 if err := RsrcMgr.KVStore.Delete(path); err != nil {
1310 log.Errorw("Failed to delete nni interfaces from kv store", log.Fields{"path": path})
1311 return
1312 }
1313 return
1314}
1315
1316// RemoveResourceMap Clear resource map associated with (intfid, onuid, uniid) tuple.
1317func (RsrcMgr *OpenOltResourceMgr) RemoveResourceMap(intfID uint32, onuID int32, uniID int32) {
1318 IntfOnuIDUniID := fmt.Sprintf("%d,%d,%d", intfID, onuID, uniID)
1319 RsrcMgr.ResourceMgrs[intfID].RemoveResourceMap(IntfOnuIDUniID)
1320}
Esin Karamanccb714b2019-11-29 15:02:06 +00001321
1322//GetMcastQueuePerInterfaceMap gets multicast queue info per pon interface
1323func (RsrcMgr *OpenOltResourceMgr) GetMcastQueuePerInterfaceMap() (map[uint32][]uint32, error) {
1324 path := fmt.Sprintf(McastQueuesForIntf)
1325 var mcastQueueToIntfMap map[uint32][]uint32
1326 var val []byte
1327
1328 kvPair, err := RsrcMgr.KVStore.Get(path)
1329 if err != nil {
1330 log.Error("failed to get data from kv store")
1331 return nil, err
1332 }
1333 if kvPair != nil && kvPair.Value != nil {
1334 if val, err = kvstore.ToByte(kvPair.Value); err != nil {
1335 log.Error("Failed to convert to byte array ", log.Fields{"error": err})
1336 return nil, err
1337 }
1338 if err = json.Unmarshal(val, &mcastQueueToIntfMap); err != nil {
1339 log.Error("Failed to unmarshall ", log.Fields{"error": err})
1340 return nil, err
1341 }
1342 }
1343 return mcastQueueToIntfMap, nil
1344}
1345
1346//AddMcastQueueForIntf adds multicast queue for pon interface
1347func (RsrcMgr *OpenOltResourceMgr) AddMcastQueueForIntf(intf uint32, gem uint32, servicePriority uint32) error {
1348 var val []byte
1349 path := fmt.Sprintf(McastQueuesForIntf)
1350
1351 mcastQueues, err := RsrcMgr.GetMcastQueuePerInterfaceMap()
1352 if err != nil {
1353 log.Errorw("Failed to get multicast queue info for interface", log.Fields{"error": err, "intf": intf})
1354 return err
1355 }
1356 if mcastQueues == nil {
1357 mcastQueues = make(map[uint32][]uint32)
1358 }
1359 mcastQueues[intf] = []uint32{gem, servicePriority}
1360 if val, err = json.Marshal(mcastQueues); err != nil {
1361 log.Errorw("Failed to marshal data", log.Fields{"error": err})
1362 return err
1363 }
1364 if err = RsrcMgr.KVStore.Put(path, val); err != nil {
1365 log.Errorw("Failed to put to kvstore", log.Fields{"error": err, "path": path, "value": val})
1366 return err
1367 }
1368 log.Debugw("added multicast queue info to KV store successfully", log.Fields{"path": path, "mcastQueueInfo": mcastQueues[intf], "interfaceId": intf})
1369 return nil
1370}
1371
1372//AddFlowGroupToKVStore adds flow group into KV store
1373func (RsrcMgr *OpenOltResourceMgr) AddFlowGroupToKVStore(groupEntry *ofp.OfpGroupEntry, cached bool) error {
1374 var Value []byte
1375 var err error
1376 var path string
1377 if cached {
1378 path = fmt.Sprintf(FlowGroupCached, groupEntry.Desc.GroupId)
1379 } else {
1380 path = fmt.Sprintf(FlowGroup, groupEntry.Desc.GroupId)
1381 }
1382 //build group info object
1383 var outPorts []uint32
1384 for _, ofBucket := range groupEntry.Desc.Buckets {
1385 for _, ofAction := range ofBucket.Actions {
1386 if ofAction.Type == ofp.OfpActionType_OFPAT_OUTPUT {
1387 outPorts = append(outPorts, ofAction.GetOutput().Port)
1388 }
1389 }
1390 }
1391 groupInfo := GroupInfo{
1392 GroupID: groupEntry.Desc.GroupId,
1393 OutPorts: outPorts,
1394 }
1395
1396 Value, err = json.Marshal(groupInfo)
1397
1398 if err != nil {
1399 log.Error("failed to Marshal flow group object")
1400 return err
1401 }
1402
1403 if err = RsrcMgr.KVStore.Put(path, Value); err != nil {
1404 log.Errorf("Failed to update resource %s", path)
1405 return err
1406 }
1407 return nil
1408}
1409
1410//RemoveFlowGroupFromKVStore removes flow group from KV store
1411func (RsrcMgr *OpenOltResourceMgr) RemoveFlowGroupFromKVStore(groupID uint32, cached bool) bool {
1412 var path string
1413 if cached {
1414 path = fmt.Sprintf(FlowGroupCached, groupID)
1415 } else {
1416 path = fmt.Sprintf(FlowGroup, groupID)
1417 }
1418 if err := RsrcMgr.KVStore.Delete(path); err != nil {
1419 log.Errorf("Failed to remove resource %s due to %s", path, err)
1420 return false
1421 }
1422 return true
1423}
1424
1425//GetFlowGroupFromKVStore fetches flow group from the KV store. Returns (false, {} error) if any problem occurs during
1426//fetching the data. Returns (true, groupInfo, nil) if the group is fetched successfully.
1427// Returns (false, {}, nil) if the group does not exists in the KV store.
1428func (RsrcMgr *OpenOltResourceMgr) GetFlowGroupFromKVStore(groupID uint32, cached bool) (bool, GroupInfo, error) {
1429 var groupInfo GroupInfo
1430 var path string
1431 if cached {
1432 path = fmt.Sprintf(FlowGroupCached, groupID)
1433 } else {
1434 path = fmt.Sprintf(FlowGroup, groupID)
1435 }
1436 kvPair, err := RsrcMgr.KVStore.Get(path)
1437 if err != nil {
1438 return false, groupInfo, err
1439 }
1440 if kvPair != nil && kvPair.Value != nil {
1441 Val, err := kvstore.ToByte(kvPair.Value)
1442 if err != nil {
1443 log.Errorw("Failed to convert flow group into byte array", log.Fields{"error": err})
1444 return false, groupInfo, err
1445 }
1446 if err = json.Unmarshal(Val, &groupInfo); err != nil {
1447 log.Errorw("Failed to unmarshal", log.Fields{"error": err})
1448 return false, groupInfo, err
1449 }
1450 return true, groupInfo, nil
1451 }
1452 return false, groupInfo, nil
1453}