blob: 415ce217b2f7d71996920097137fba80f3e5a2da [file] [log] [blame]
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001/*
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
17package ponresourcemanager
18
19import (
npujarec5762e2020-01-01 14:08:48 +053020 "context"
Matt Jeanneret384d8c92019-05-06 14:27:31 -040021 "encoding/base64"
22 "encoding/json"
23 "errors"
24 "fmt"
Neha Sharmacc656962020-04-14 14:26:11 +000025 "time"
Matt Jeanneretcab955f2019-04-10 15:45:57 -040026
Esin Karamanccb714b2019-11-29 15:02:06 +000027 bitmap "github.com/boljen/go-bitmap"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070028 "github.com/opencord/voltha-lib-go/v4/pkg/db"
29 "github.com/opencord/voltha-lib-go/v4/pkg/db/kvstore"
30 "github.com/opencord/voltha-lib-go/v4/pkg/log"
31 tp "github.com/opencord/voltha-lib-go/v4/pkg/techprofile"
Matt Jeanneretcab955f2019-04-10 15:45:57 -040032)
33
34const (
Matt Jeanneret384d8c92019-05-06 14:27:31 -040035 //Constants to identify resource pool
36 UNI_ID = "UNI_ID"
37 ONU_ID = "ONU_ID"
38 ALLOC_ID = "ALLOC_ID"
39 GEMPORT_ID = "GEMPORT_ID"
40 FLOW_ID = "FLOW_ID"
Matt Jeanneretcab955f2019-04-10 15:45:57 -040041
Matt Jeanneret384d8c92019-05-06 14:27:31 -040042 //Constants for passing command line arugments
43 OLT_MODEL_ARG = "--olt_model"
Matteo Scandolodfa7a972020-11-06 13:03:40 -080044
45 PATH_PREFIX = "%s/resource_manager/{%s}"
divyadesaid26f6b12020-03-19 06:30:28 +000046
47 /*The path under which configuration data is stored is defined as technology/device agnostic.
48 That means the path does not include any specific technology/device variable. Using technology/device
49 agnostic path also makes northbound applications, that need to write to this path,
50 technology/device agnostic.
51
52 Default kv client of PonResourceManager reads from/writes to PATH_PREFIX defined above.
53 That is why, an additional kv client (named KVStoreForConfig) is defined to read from the config path.
54 */
Matteo Scandolodfa7a972020-11-06 13:03:40 -080055
56 PATH_PREFIX_FOR_CONFIG = "%s/resource_manager/config"
Matt Jeanneret384d8c92019-05-06 14:27:31 -040057 /*The resource ranges for a given device model should be placed
58 at 'resource_manager/<technology>/resource_ranges/<olt_model_type>'
59 path on the KV store.
60 If Resource Range parameters are to be read from the external KV store,
61 they are expected to be stored in the following format.
62 Note: All parameters are MANDATORY for now.
63 constants used as keys to reference the resource range parameters from
64 and external KV store.
65 */
66 UNI_ID_START_IDX = "uni_id_start"
67 UNI_ID_END_IDX = "uni_id_end"
68 ONU_ID_START_IDX = "onu_id_start"
69 ONU_ID_END_IDX = "onu_id_end"
70 ONU_ID_SHARED_IDX = "onu_id_shared"
71 ALLOC_ID_START_IDX = "alloc_id_start"
72 ALLOC_ID_END_IDX = "alloc_id_end"
73 ALLOC_ID_SHARED_IDX = "alloc_id_shared"
74 GEMPORT_ID_START_IDX = "gemport_id_start"
75 GEMPORT_ID_END_IDX = "gemport_id_end"
76 GEMPORT_ID_SHARED_IDX = "gemport_id_shared"
77 FLOW_ID_START_IDX = "flow_id_start"
78 FLOW_ID_END_IDX = "flow_id_end"
79 FLOW_ID_SHARED_IDX = "flow_id_shared"
80 NUM_OF_PON_PORT = "pon_ports"
Matt Jeanneretcab955f2019-04-10 15:45:57 -040081
Matt Jeanneret384d8c92019-05-06 14:27:31 -040082 /*
83 The KV store backend is initialized with a path prefix and we need to
84 provide only the suffix.
85 */
86 PON_RESOURCE_RANGE_CONFIG_PATH = "resource_ranges/%s"
Matt Jeanneretcab955f2019-04-10 15:45:57 -040087
Matt Jeanneret384d8c92019-05-06 14:27:31 -040088 //resource path suffix
89 //Path on the KV store for storing alloc id ranges and resource pool for a given interface
90 //Format: <device_id>/alloc_id_pool/<pon_intf_id>
91 ALLOC_ID_POOL_PATH = "{%s}/alloc_id_pool/{%d}"
92 //Path on the KV store for storing gemport id ranges and resource pool for a given interface
93 //Format: <device_id>/gemport_id_pool/<pon_intf_id>
94 GEMPORT_ID_POOL_PATH = "{%s}/gemport_id_pool/{%d}"
95 //Path on the KV store for storing onu id ranges and resource pool for a given interface
96 //Format: <device_id>/onu_id_pool/<pon_intf_id>
97 ONU_ID_POOL_PATH = "{%s}/onu_id_pool/{%d}"
98 //Path on the KV store for storing flow id ranges and resource pool for a given interface
99 //Format: <device_id>/flow_id_pool/<pon_intf_id>
100 FLOW_ID_POOL_PATH = "{%s}/flow_id_pool/{%d}"
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400101
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400102 //Path on the KV store for storing list of alloc IDs for a given ONU
103 //Format: <device_id>/<(pon_intf_id, onu_id)>/alloc_ids
104 ALLOC_ID_RESOURCE_MAP_PATH = "{%s}/{%s}/alloc_ids"
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400105
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400106 //Path on the KV store for storing list of gemport IDs for a given ONU
107 //Format: <device_id>/<(pon_intf_id, onu_id)>/gemport_ids
108 GEMPORT_ID_RESOURCE_MAP_PATH = "{%s}/{%s}/gemport_ids"
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400109
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400110 //Path on the KV store for storing list of Flow IDs for a given ONU
111 //Format: <device_id>/<(pon_intf_id, onu_id)>/flow_ids
112 FLOW_ID_RESOURCE_MAP_PATH = "{%s}/{%s}/flow_ids"
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400113
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400114 //Flow Id info: Use to store more metadata associated with the flow_id
serkant.uluderya7b8211e2021-02-24 16:39:18 +0300115 FLOW_ID_INFO_PATH_PREFIX = "{%s}/flow_id_info"
116 //Format: <device_id>/flow_id_info/<(pon_intf_id, onu_id)>
117 FLOW_ID_INFO_PATH_INTF_ONU_PREFIX = "{%s}/flow_id_info/{%s}"
118 //Format: <device_id>/flow_id_info/<(pon_intf_id, onu_id)><flow_id>
119 FLOW_ID_INFO_PATH = FLOW_ID_INFO_PATH_PREFIX + "/{%s}/{%d}"
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400120
Scott Bakerc6e54cb2019-11-04 09:31:25 -0800121 //path on the kvstore to store onugem info map
122 //format: <device-id>/onu_gem_info/<intfid>
123 ONU_GEM_INFO_PATH = "{%s}/onu_gem_info/{%d}" // onu_gem/<(intfid)>
124
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400125 //Constants for internal usage.
126 PON_INTF_ID = "pon_intf_id"
127 START_IDX = "start_idx"
128 END_IDX = "end_idx"
129 POOL = "pool"
130 NUM_OF_PON_INTF = 16
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400131
Neha Sharmacc656962020-04-14 14:26:11 +0000132 KVSTORE_RETRY_TIMEOUT = 5 * time.Second
divyadesaid26f6b12020-03-19 06:30:28 +0000133 //Path on the KV store for storing reserved gem ports
134 //Format: reserved_gemport_ids
135 RESERVED_GEMPORT_IDS_PATH = "reserved_gemport_ids"
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400136)
137
138//type ResourceTypeIndex string
139//type ResourceType string
140
141type PONResourceManager struct {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400142 //Implements APIs to initialize/allocate/release alloc/gemport/onu IDs.
divyadesaid26f6b12020-03-19 06:30:28 +0000143 Technology string
144 DeviceType string
145 DeviceID string
serkant.uluderya7b8211e2021-02-24 16:39:18 +0300146 Backend string // ETCD only currently
Neha Sharma3f221ae2020-04-29 19:02:12 +0000147 Address string // address of the KV store
divyadesaid26f6b12020-03-19 06:30:28 +0000148 OLTModel string
149 KVStore *db.Backend
150 KVStoreForConfig *db.Backend
151 TechProfileMgr tp.TechProfileIf // create object of *tp.TechProfileMgr
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400152
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400153 // Below attribute, pon_resource_ranges, should be initialized
154 // by reading from KV store.
155 PonResourceRanges map[string]interface{}
156 SharedResourceMgrs map[string]*PONResourceManager
157 SharedIdxByType map[string]string
158 IntfIDs []uint32 // list of pon interface IDs
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400159 Globalorlocal string
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400160}
161
Neha Sharma96b7bf22020-06-15 10:37:32 +0000162func newKVClient(ctx context.Context, storeType string, address string, timeout time.Duration) (kvstore.Client, error) {
163 logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType})
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400164 switch storeType {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400165 case "etcd":
Neha Sharma96b7bf22020-06-15 10:37:32 +0000166 return kvstore.NewEtcdClient(ctx, address, timeout, log.WarnLevel)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400167 }
168 return nil, errors.New("unsupported-kv-store")
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400169}
170
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800171func SetKVClient(ctx context.Context, Technology string, Backend string, Addr string, configClient bool, basePathKvStore string) *db.Backend {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400172 // TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some
173 // issue between kv store and backend , core is not calling NewBackend directly
Neha Sharma96b7bf22020-06-15 10:37:32 +0000174 kvClient, err := newKVClient(ctx, Backend, Addr, KVSTORE_RETRY_TIMEOUT)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400175 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000176 logger.Fatalw(ctx, "Failed to init KV client\n", log.Fields{"err": err})
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400177 return nil
178 }
divyadesaid26f6b12020-03-19 06:30:28 +0000179
180 var pathPrefix string
181 if configClient {
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800182 pathPrefix = fmt.Sprintf(PATH_PREFIX_FOR_CONFIG, basePathKvStore)
divyadesaid26f6b12020-03-19 06:30:28 +0000183 } else {
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800184 pathPrefix = fmt.Sprintf(PATH_PREFIX, basePathKvStore, Technology)
divyadesaid26f6b12020-03-19 06:30:28 +0000185 }
186
sbarbaria8910ba2019-11-05 10:12:23 -0500187 kvbackend := &db.Backend{
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400188 Client: kvClient,
189 StoreType: Backend,
Neha Sharma3f221ae2020-04-29 19:02:12 +0000190 Address: Addr,
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400191 Timeout: KVSTORE_RETRY_TIMEOUT,
divyadesaid26f6b12020-03-19 06:30:28 +0000192 PathPrefix: pathPrefix}
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400193
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400194 return kvbackend
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400195}
196
197// NewPONResourceManager creates a new PON resource manager.
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800198func NewPONResourceManager(ctx context.Context, Technology string, DeviceType string, DeviceID string, Backend string, Address string, basePathKvStore string) (*PONResourceManager, error) {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400199 var PONMgr PONResourceManager
200 PONMgr.Technology = Technology
201 PONMgr.DeviceType = DeviceType
202 PONMgr.DeviceID = DeviceID
203 PONMgr.Backend = Backend
Neha Sharma3f221ae2020-04-29 19:02:12 +0000204 PONMgr.Address = Address
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800205 PONMgr.KVStore = SetKVClient(ctx, Technology, Backend, Address, false, basePathKvStore)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400206 if PONMgr.KVStore == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000207 logger.Error(ctx, "KV Client initilization failed")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400208 return nil, errors.New("Failed to init KV client")
209 }
divyadesaid26f6b12020-03-19 06:30:28 +0000210 // init kv client to read from the config path
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800211 PONMgr.KVStoreForConfig = SetKVClient(ctx, Technology, Backend, Address, true, basePathKvStore)
divyadesaid26f6b12020-03-19 06:30:28 +0000212 if PONMgr.KVStoreForConfig == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000213 logger.Error(ctx, "KV Config Client initilization failed")
divyadesaid26f6b12020-03-19 06:30:28 +0000214 return nil, errors.New("Failed to init KV Config client")
215 }
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400216 // Initialize techprofile for this technology
Matteo Scandoloc858ab72020-11-16 12:44:51 -0800217 if PONMgr.TechProfileMgr, _ = tp.NewTechProfile(ctx, &PONMgr, Backend, Address, basePathKvStore); PONMgr.TechProfileMgr == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000218 logger.Error(ctx, "Techprofile initialization failed")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400219 return nil, errors.New("Failed to init tech profile")
220 }
221 PONMgr.PonResourceRanges = make(map[string]interface{})
222 PONMgr.SharedResourceMgrs = make(map[string]*PONResourceManager)
223 PONMgr.SharedIdxByType = make(map[string]string)
224 PONMgr.SharedIdxByType[ONU_ID] = ONU_ID_SHARED_IDX
225 PONMgr.SharedIdxByType[ALLOC_ID] = ALLOC_ID_SHARED_IDX
226 PONMgr.SharedIdxByType[GEMPORT_ID] = GEMPORT_ID_SHARED_IDX
227 PONMgr.SharedIdxByType[FLOW_ID] = FLOW_ID_SHARED_IDX
228 PONMgr.IntfIDs = make([]uint32, NUM_OF_PON_INTF)
229 PONMgr.OLTModel = DeviceType
230 return &PONMgr, nil
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400231}
232
233/*
234 Initialize PON resource ranges with config fetched from kv store.
235 return boolean: True if PON resource ranges initialized else false
236 Try to initialize the PON Resource Ranges from KV store based on the
237 OLT model key, if available
238*/
239
npujarec5762e2020-01-01 14:08:48 +0530240func (PONRMgr *PONResourceManager) InitResourceRangesFromKVStore(ctx context.Context) bool {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400241 //Initialize PON resource ranges with config fetched from kv store.
242 //:return boolean: True if PON resource ranges initialized else false
243 // Try to initialize the PON Resource Ranges from KV store based on the
244 // OLT model key, if available
245 if PONRMgr.OLTModel == "" {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000246 logger.Error(ctx, "Failed to get OLT model")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400247 return false
248 }
249 Path := fmt.Sprintf(PON_RESOURCE_RANGE_CONFIG_PATH, PONRMgr.OLTModel)
250 //get resource from kv store
npujarec5762e2020-01-01 14:08:48 +0530251 Result, err := PONRMgr.KVStore.Get(ctx, Path)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400252 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000253 logger.Debugf(ctx, "Error in fetching resource %s from KV strore", Path)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400254 return false
255 }
256 if Result == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000257 logger.Debug(ctx, "There may be no resources in the KV store in case of fresh bootup, return true")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400258 return false
259 }
260 //update internal ranges from kv ranges. If there are missing
261 // values in the KV profile, continue to use the defaults
262 Value, err := ToByte(Result.Value)
263 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000264 logger.Error(ctx, "Failed to convert kvpair to byte string")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400265 return false
266 }
267 if err := json.Unmarshal(Value, &PONRMgr.PonResourceRanges); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000268 logger.Error(ctx, "Failed to Unmarshal json byte")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400269 return false
270 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000271 logger.Debug(ctx, "Init resource ranges from kvstore success")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400272 return true
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400273}
274
Neha Sharma96b7bf22020-06-15 10:37:32 +0000275func (PONRMgr *PONResourceManager) UpdateRanges(ctx context.Context, StartIDx string, StartID uint32, EndIDx string, EndID uint32,
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400276 SharedIDx string, SharedPoolID uint32, RMgr *PONResourceManager) {
277 /*
278 Update the ranges for all reosurce type in the intermnal maps
279 param: resource type start index
280 param: start ID
281 param: resource type end index
282 param: end ID
283 param: resource type shared index
284 param: shared pool id
285 param: global resource manager
286 */
Neha Sharma96b7bf22020-06-15 10:37:32 +0000287 logger.Debugf(ctx, "update ranges for %s, %d", StartIDx, StartID)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400288
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400289 if StartID != 0 {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400290 if (PONRMgr.PonResourceRanges[StartIDx] == nil) || (PONRMgr.PonResourceRanges[StartIDx].(uint32) < StartID) {
291 PONRMgr.PonResourceRanges[StartIDx] = StartID
292 }
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400293 }
294 if EndID != 0 {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400295 if (PONRMgr.PonResourceRanges[EndIDx] == nil) || (PONRMgr.PonResourceRanges[EndIDx].(uint32) > EndID) {
296 PONRMgr.PonResourceRanges[EndIDx] = EndID
297 }
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400298 }
299 //if SharedPoolID != 0 {
300 PONRMgr.PonResourceRanges[SharedIDx] = SharedPoolID
301 //}
302 if RMgr != nil {
303 PONRMgr.SharedResourceMgrs[SharedIDx] = RMgr
304 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400305}
306
Neha Sharma96b7bf22020-06-15 10:37:32 +0000307func (PONRMgr *PONResourceManager) InitDefaultPONResourceRanges(ctx context.Context,
308 ONUIDStart uint32,
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400309 ONUIDEnd uint32,
310 ONUIDSharedPoolID uint32,
311 AllocIDStart uint32,
312 AllocIDEnd uint32,
313 AllocIDSharedPoolID uint32,
314 GEMPortIDStart uint32,
315 GEMPortIDEnd uint32,
316 GEMPortIDSharedPoolID uint32,
317 FlowIDStart uint32,
318 FlowIDEnd uint32,
319 FlowIDSharedPoolID uint32,
320 UNIIDStart uint32,
321 UNIIDEnd uint32,
322 NoOfPONPorts uint32,
323 IntfIDs []uint32) bool {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400324
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400325 /*Initialize default PON resource ranges
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400326
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400327 :param onu_id_start_idx: onu id start index
328 :param onu_id_end_idx: onu id end index
329 :param onu_id_shared_pool_id: pool idx for id shared by all intfs or None for no sharing
330 :param alloc_id_start_idx: alloc id start index
331 :param alloc_id_end_idx: alloc id end index
332 :param alloc_id_shared_pool_id: pool idx for alloc id shared by all intfs or None for no sharing
333 :param gemport_id_start_idx: gemport id start index
334 :param gemport_id_end_idx: gemport id end index
335 :param gemport_id_shared_pool_id: pool idx for gemport id shared by all intfs or None for no sharing
336 :param flow_id_start_idx: flow id start index
337 :param flow_id_end_idx: flow id end index
338 :param flow_id_shared_pool_id: pool idx for flow id shared by all intfs or None for no sharing
339 :param num_of_pon_ports: number of PON ports
340 :param intf_ids: interfaces serviced by this manager
341 */
Neha Sharma96b7bf22020-06-15 10:37:32 +0000342 PONRMgr.UpdateRanges(ctx, ONU_ID_START_IDX, ONUIDStart, ONU_ID_END_IDX, ONUIDEnd, ONU_ID_SHARED_IDX, ONUIDSharedPoolID, nil)
343 PONRMgr.UpdateRanges(ctx, ALLOC_ID_START_IDX, AllocIDStart, ALLOC_ID_END_IDX, AllocIDEnd, ALLOC_ID_SHARED_IDX, AllocIDSharedPoolID, nil)
344 PONRMgr.UpdateRanges(ctx, GEMPORT_ID_START_IDX, GEMPortIDStart, GEMPORT_ID_END_IDX, GEMPortIDEnd, GEMPORT_ID_SHARED_IDX, GEMPortIDSharedPoolID, nil)
345 PONRMgr.UpdateRanges(ctx, FLOW_ID_START_IDX, FlowIDStart, FLOW_ID_END_IDX, FlowIDEnd, FLOW_ID_SHARED_IDX, FlowIDSharedPoolID, nil)
346 PONRMgr.UpdateRanges(ctx, UNI_ID_START_IDX, UNIIDStart, UNI_ID_END_IDX, UNIIDEnd, "", 0, nil)
347 logger.Debug(ctx, "Initialize default range values")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400348 var i uint32
349 if IntfIDs == nil {
350 for i = 0; i < NoOfPONPorts; i++ {
351 PONRMgr.IntfIDs = append(PONRMgr.IntfIDs, i)
352 }
353 } else {
354 PONRMgr.IntfIDs = IntfIDs
355 }
356 return true
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400357}
358
npujarec5762e2020-01-01 14:08:48 +0530359func (PONRMgr *PONResourceManager) InitDeviceResourcePool(ctx context.Context) error {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400360
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400361 //Initialize resource pool for all PON ports.
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400362
Neha Sharma96b7bf22020-06-15 10:37:32 +0000363 logger.Debug(ctx, "Init resource ranges")
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400364
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400365 var err error
366 for _, Intf := range PONRMgr.IntfIDs {
367 SharedPoolID := PONRMgr.PonResourceRanges[ONU_ID_SHARED_IDX].(uint32)
368 if SharedPoolID != 0 {
369 Intf = SharedPoolID
370 }
npujarec5762e2020-01-01 14:08:48 +0530371 if err = PONRMgr.InitResourceIDPool(ctx, Intf, ONU_ID,
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400372 PONRMgr.PonResourceRanges[ONU_ID_START_IDX].(uint32),
373 PONRMgr.PonResourceRanges[ONU_ID_END_IDX].(uint32)); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000374 logger.Error(ctx, "Failed to init ONU ID resource pool")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400375 return err
376 }
377 if SharedPoolID != 0 {
378 break
379 }
380 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400381
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400382 for _, Intf := range PONRMgr.IntfIDs {
383 SharedPoolID := PONRMgr.PonResourceRanges[ALLOC_ID_SHARED_IDX].(uint32)
384 if SharedPoolID != 0 {
385 Intf = SharedPoolID
386 }
npujarec5762e2020-01-01 14:08:48 +0530387 if err = PONRMgr.InitResourceIDPool(ctx, Intf, ALLOC_ID,
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400388 PONRMgr.PonResourceRanges[ALLOC_ID_START_IDX].(uint32),
389 PONRMgr.PonResourceRanges[ALLOC_ID_END_IDX].(uint32)); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000390 logger.Error(ctx, "Failed to init ALLOC ID resource pool ")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400391 return err
392 }
393 if SharedPoolID != 0 {
394 break
395 }
396 }
397 for _, Intf := range PONRMgr.IntfIDs {
398 SharedPoolID := PONRMgr.PonResourceRanges[GEMPORT_ID_SHARED_IDX].(uint32)
399 if SharedPoolID != 0 {
400 Intf = SharedPoolID
401 }
npujarec5762e2020-01-01 14:08:48 +0530402 if err = PONRMgr.InitResourceIDPool(ctx, Intf, GEMPORT_ID,
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400403 PONRMgr.PonResourceRanges[GEMPORT_ID_START_IDX].(uint32),
404 PONRMgr.PonResourceRanges[GEMPORT_ID_END_IDX].(uint32)); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000405 logger.Error(ctx, "Failed to init GEMPORT ID resource pool")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400406 return err
407 }
408 if SharedPoolID != 0 {
409 break
410 }
411 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400412
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400413 for _, Intf := range PONRMgr.IntfIDs {
414 SharedPoolID := PONRMgr.PonResourceRanges[FLOW_ID_SHARED_IDX].(uint32)
415 if SharedPoolID != 0 {
416 Intf = SharedPoolID
417 }
npujarec5762e2020-01-01 14:08:48 +0530418 if err = PONRMgr.InitResourceIDPool(ctx, Intf, FLOW_ID,
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400419 PONRMgr.PonResourceRanges[FLOW_ID_START_IDX].(uint32),
420 PONRMgr.PonResourceRanges[FLOW_ID_END_IDX].(uint32)); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000421 logger.Error(ctx, "Failed to init FLOW ID resource pool")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400422 return err
423 }
424 if SharedPoolID != 0 {
425 break
426 }
427 }
428 return err
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400429}
430
npujarec5762e2020-01-01 14:08:48 +0530431func (PONRMgr *PONResourceManager) ClearDeviceResourcePool(ctx context.Context) error {
Devmalya Paul495b94a2019-08-27 19:42:00 -0400432
433 //Clear resource pool for all PON ports.
434
Neha Sharma96b7bf22020-06-15 10:37:32 +0000435 logger.Debug(ctx, "Clear resource ranges")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400436
437 for _, Intf := range PONRMgr.IntfIDs {
438 SharedPoolID := PONRMgr.PonResourceRanges[ONU_ID_SHARED_IDX].(uint32)
439 if SharedPoolID != 0 {
440 Intf = SharedPoolID
441 }
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000442 if status := PONRMgr.ClearResourceIDPool(ctx, Intf, ONU_ID); !status {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000443 logger.Error(ctx, "Failed to clear ONU ID resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400444 return errors.New("Failed to clear ONU ID resource pool")
445 }
446 if SharedPoolID != 0 {
447 break
448 }
449 }
450
451 for _, Intf := range PONRMgr.IntfIDs {
452 SharedPoolID := PONRMgr.PonResourceRanges[ALLOC_ID_SHARED_IDX].(uint32)
453 if SharedPoolID != 0 {
454 Intf = SharedPoolID
455 }
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000456 if status := PONRMgr.ClearResourceIDPool(ctx, Intf, ALLOC_ID); !status {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000457 logger.Error(ctx, "Failed to clear ALLOC ID resource pool ")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400458 return errors.New("Failed to clear ALLOC ID resource pool")
459 }
460 if SharedPoolID != 0 {
461 break
462 }
463 }
464 for _, Intf := range PONRMgr.IntfIDs {
465 SharedPoolID := PONRMgr.PonResourceRanges[GEMPORT_ID_SHARED_IDX].(uint32)
466 if SharedPoolID != 0 {
467 Intf = SharedPoolID
468 }
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000469 if status := PONRMgr.ClearResourceIDPool(ctx, Intf, GEMPORT_ID); !status {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000470 logger.Error(ctx, "Failed to clear GEMPORT ID resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400471 return errors.New("Failed to clear GEMPORT ID resource pool")
472 }
473 if SharedPoolID != 0 {
474 break
475 }
476 }
477
478 for _, Intf := range PONRMgr.IntfIDs {
479 SharedPoolID := PONRMgr.PonResourceRanges[FLOW_ID_SHARED_IDX].(uint32)
480 if SharedPoolID != 0 {
481 Intf = SharedPoolID
482 }
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000483 if status := PONRMgr.ClearResourceIDPool(ctx, Intf, FLOW_ID); !status {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000484 logger.Error(ctx, "Failed to clear FLOW ID resource pool")
Devmalya Paul495b94a2019-08-27 19:42:00 -0400485 return errors.New("Failed to clear FLOW ID resource pool")
486 }
487 if SharedPoolID != 0 {
488 break
489 }
490 }
491 return nil
492}
493
npujarec5762e2020-01-01 14:08:48 +0530494func (PONRMgr *PONResourceManager) InitResourceIDPool(ctx context.Context, Intf uint32, ResourceType string, StartID uint32, EndID uint32) error {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400495
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400496 /*Initialize Resource ID pool for a given Resource Type on a given PON Port
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400497
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400498 :param pon_intf_id: OLT PON interface id
499 :param resource_type: String to identify type of resource
500 :param start_idx: start index for onu id pool
501 :param end_idx: end index for onu id pool
502 :return boolean: True if resource id pool initialized else false
503 */
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400504
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400505 // delegate to the master instance if sharing enabled across instances
506 SharedResourceMgr := PONRMgr.SharedResourceMgrs[PONRMgr.SharedIdxByType[ResourceType]]
507 if SharedResourceMgr != nil && PONRMgr != SharedResourceMgr {
npujarec5762e2020-01-01 14:08:48 +0530508 return SharedResourceMgr.InitResourceIDPool(ctx, Intf, ResourceType, StartID, EndID)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400509 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400510
Neha Sharma96b7bf22020-06-15 10:37:32 +0000511 Path := PONRMgr.GetPath(ctx, Intf, ResourceType)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400512 if Path == "" {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000513 logger.Errorf(ctx, "Failed to get path for resource type %s", ResourceType)
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000514 return fmt.Errorf("Failed to get path for resource type %s", ResourceType)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400515 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400516
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400517 //In case of adapter reboot and reconciliation resource in kv store
518 //checked for its presence if not kv store update happens
npujarec5762e2020-01-01 14:08:48 +0530519 Res, err := PONRMgr.GetResource(ctx, Path)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400520 if (err == nil) && (Res != nil) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000521 logger.Debugf(ctx, "Resource %s already present in store ", Path)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400522 return nil
523 } else {
divyadesaid26f6b12020-03-19 06:30:28 +0000524 var excluded []uint32
525 if ResourceType == GEMPORT_ID {
526 //get gem port ids defined in the KV store, if any, and exclude them from the gem port id pool
527 if reservedGemPortIds, defined := PONRMgr.getReservedGemPortIdsFromKVStore(ctx); defined {
528 excluded = reservedGemPortIds
Neha Sharma96b7bf22020-06-15 10:37:32 +0000529 logger.Debugw(ctx, "Excluding some ports from GEM port id pool", log.Fields{"excluded gem ports": excluded})
divyadesaid26f6b12020-03-19 06:30:28 +0000530 }
531 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000532 FormatResult, err := PONRMgr.FormatResource(ctx, Intf, StartID, EndID, excluded)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400533 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000534 logger.Errorf(ctx, "Failed to format resource")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400535 return err
536 }
537 // Add resource as json in kv store.
npujarec5762e2020-01-01 14:08:48 +0530538 err = PONRMgr.KVStore.Put(ctx, Path, FormatResult)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400539 if err == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000540 logger.Debug(ctx, "Successfuly posted to kv store")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400541 return err
542 }
543 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400544
Neha Sharma96b7bf22020-06-15 10:37:32 +0000545 logger.Debug(ctx, "Error initializing pool")
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400546
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400547 return err
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400548}
549
divyadesaid26f6b12020-03-19 06:30:28 +0000550func (PONRMgr *PONResourceManager) getReservedGemPortIdsFromKVStore(ctx context.Context) ([]uint32, bool) {
551 var reservedGemPortIds []uint32
552 // read reserved gem ports from the config path
553 KvPair, err := PONRMgr.KVStoreForConfig.Get(ctx, RESERVED_GEMPORT_IDS_PATH)
554 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000555 logger.Errorw(ctx, "Unable to get reserved GEM port ids from the kv store", log.Fields{"err": err})
divyadesaid26f6b12020-03-19 06:30:28 +0000556 return reservedGemPortIds, false
557 }
558 if KvPair == nil || KvPair.Value == nil {
559 //no reserved gem port defined in the store
560 return reservedGemPortIds, false
561 }
562 Val, err := kvstore.ToByte(KvPair.Value)
563 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000564 logger.Errorw(ctx, "Failed to convert reserved gem port ids into byte array", log.Fields{"err": err})
divyadesaid26f6b12020-03-19 06:30:28 +0000565 return reservedGemPortIds, false
566 }
567 if err = json.Unmarshal(Val, &reservedGemPortIds); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000568 logger.Errorw(ctx, "Failed to unmarshal reservedGemPortIds", log.Fields{"err": err})
divyadesaid26f6b12020-03-19 06:30:28 +0000569 return reservedGemPortIds, false
570 }
571 return reservedGemPortIds, true
572}
573
Neha Sharma96b7bf22020-06-15 10:37:32 +0000574func (PONRMgr *PONResourceManager) FormatResource(ctx context.Context, IntfID uint32, StartIDx uint32, EndIDx uint32,
divyadesaid26f6b12020-03-19 06:30:28 +0000575 Excluded []uint32) ([]byte, error) {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400576 /*
577 Format resource as json.
578 :param pon_intf_id: OLT PON interface id
579 :param start_idx: start index for id pool
580 :param end_idx: end index for id pool
divyadesaid26f6b12020-03-19 06:30:28 +0000581 :Id values to be Excluded from the pool
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400582 :return dictionary: resource formatted as map
583 */
584 // Format resource as json to be stored in backend store
585 Resource := make(map[string]interface{})
586 Resource[PON_INTF_ID] = IntfID
587 Resource[START_IDX] = StartIDx
588 Resource[END_IDX] = EndIDx
589 /*
590 Resource pool stored in backend store as binary string.
591 Tracking the resource allocation will be done by setting the bits \
592 in the byte array. The index set will be the resource number allocated.
593 */
594 var TSData *bitmap.Threadsafe
595 if TSData = bitmap.NewTS(int(EndIDx)); TSData == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000596 logger.Error(ctx, "Failed to create a bitmap")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400597 return nil, errors.New("Failed to create bitmap")
598 }
divyadesaid26f6b12020-03-19 06:30:28 +0000599 for _, excludedID := range Excluded {
600 if excludedID < StartIDx || excludedID > EndIDx {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000601 logger.Warnf(ctx, "Cannot reserve %d. It must be in the range of [%d, %d]", excludedID,
divyadesaid26f6b12020-03-19 06:30:28 +0000602 StartIDx, EndIDx)
603 continue
604 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000605 PONRMgr.reserveID(ctx, TSData, StartIDx, excludedID)
divyadesaid26f6b12020-03-19 06:30:28 +0000606 }
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400607 Resource[POOL] = TSData.Data(false) //we pass false so as the TSData lib api does not do a copy of the data and return
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400608
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400609 Value, err := json.Marshal(Resource)
610 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000611 logger.Errorf(ctx, "Failed to marshall resource")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400612 return nil, err
613 }
614 return Value, err
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400615}
npujarec5762e2020-01-01 14:08:48 +0530616func (PONRMgr *PONResourceManager) GetResource(ctx context.Context, Path string) (map[string]interface{}, error) {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400617 /*
618 Get resource from kv store.
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400619
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400620 :param path: path to get resource
621 :return: resource if resource present in kv store else None
622 */
623 //get resource from kv store
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400624
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400625 var Value []byte
626 Result := make(map[string]interface{})
627 var Str string
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400628
npujarec5762e2020-01-01 14:08:48 +0530629 Resource, err := PONRMgr.KVStore.Get(ctx, Path)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400630 if (err != nil) || (Resource == nil) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000631 logger.Debugf(ctx, "Resource unavailable at %s", Path)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400632 return nil, err
633 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400634
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400635 Value, err = ToByte(Resource.Value)
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000636 if err != nil {
637 return nil, err
638 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400639
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400640 // decode resource fetched from backend store to dictionary
641 err = json.Unmarshal(Value, &Result)
642 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000643 logger.Error(ctx, "Failed to decode resource")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400644 return Result, err
645 }
646 /*
647 resource pool in backend store stored as binary string whereas to
648 access the pool to generate/release IDs it need to be converted
649 as BitArray
650 */
651 Str, err = ToString(Result[POOL])
652 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000653 logger.Error(ctx, "Failed to conver to kv pair to string")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400654 return Result, err
655 }
656 Decode64, _ := base64.StdEncoding.DecodeString(Str)
657 Result[POOL], err = ToByte(Decode64)
658 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000659 logger.Error(ctx, "Failed to convert resource pool to byte")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400660 return Result, err
661 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400662
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400663 return Result, err
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400664}
665
Neha Sharma96b7bf22020-06-15 10:37:32 +0000666func (PONRMgr *PONResourceManager) GetPath(ctx context.Context, IntfID uint32, ResourceType string) string {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400667 /*
668 Get path for given resource type.
669 :param pon_intf_id: OLT PON interface id
670 :param resource_type: String to identify type of resource
671 :return: path for given resource type
672 */
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400673
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400674 /*
675 Get the shared pool for the given resource type.
676 all the resource ranges and the shared resource maps are initialized during the init.
677 */
678 SharedPoolID := PONRMgr.PonResourceRanges[PONRMgr.SharedIdxByType[ResourceType]].(uint32)
679 if SharedPoolID != 0 {
680 IntfID = SharedPoolID
681 }
682 var Path string
683 if ResourceType == ONU_ID {
684 Path = fmt.Sprintf(ONU_ID_POOL_PATH, PONRMgr.DeviceID, IntfID)
685 } else if ResourceType == ALLOC_ID {
686 Path = fmt.Sprintf(ALLOC_ID_POOL_PATH, PONRMgr.DeviceID, IntfID)
687 } else if ResourceType == GEMPORT_ID {
688 Path = fmt.Sprintf(GEMPORT_ID_POOL_PATH, PONRMgr.DeviceID, IntfID)
689 } else if ResourceType == FLOW_ID {
690 Path = fmt.Sprintf(FLOW_ID_POOL_PATH, PONRMgr.DeviceID, IntfID)
691 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000692 logger.Error(ctx, "Invalid resource pool identifier")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400693 }
694 return Path
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400695}
696
npujarec5762e2020-01-01 14:08:48 +0530697func (PONRMgr *PONResourceManager) GetResourceID(ctx context.Context, IntfID uint32, ResourceType string, NumIDs uint32) ([]uint32, error) {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400698 /*
699 Create alloc/gemport/onu/flow id for given OLT PON interface.
700 :param pon_intf_id: OLT PON interface id
701 :param resource_type: String to identify type of resource
702 :param num_of_id: required number of ids
703 :return list/uint32/None: list, uint32 or None if resource type is
704 alloc_id/gemport_id, onu_id or invalid type respectively
705 */
706 if NumIDs < 1 {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000707 logger.Error(ctx, "Invalid number of resources requested")
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000708 return nil, fmt.Errorf("Invalid number of resources requested %d", NumIDs)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400709 }
710 // delegate to the master instance if sharing enabled across instances
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400711
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400712 SharedResourceMgr := PONRMgr.SharedResourceMgrs[PONRMgr.SharedIdxByType[ResourceType]]
713 if SharedResourceMgr != nil && PONRMgr != SharedResourceMgr {
npujarec5762e2020-01-01 14:08:48 +0530714 return SharedResourceMgr.GetResourceID(ctx, IntfID, ResourceType, NumIDs)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400715 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000716 logger.Debugf(ctx, "Fetching resource from %s rsrc mgr for resource %s", PONRMgr.Globalorlocal, ResourceType)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400717
Neha Sharma96b7bf22020-06-15 10:37:32 +0000718 Path := PONRMgr.GetPath(ctx, IntfID, ResourceType)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400719 if Path == "" {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000720 logger.Errorf(ctx, "Failed to get path for resource type %s", ResourceType)
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000721 return nil, fmt.Errorf("Failed to get path for resource type %s", ResourceType)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400722 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000723 logger.Debugf(ctx, "Get resource for type %s on path %s", ResourceType, Path)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400724 var Result []uint32
725 var NextID uint32
npujarec5762e2020-01-01 14:08:48 +0530726 Resource, err := PONRMgr.GetResource(ctx, Path)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400727 if (err == nil) && (ResourceType == ONU_ID) || (ResourceType == FLOW_ID) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000728 if NextID, err = PONRMgr.GenerateNextID(ctx, Resource); err != nil {
729 logger.Error(ctx, "Failed to Generate ID")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400730 return Result, err
731 }
732 Result = append(Result, NextID)
733 } else if (err == nil) && ((ResourceType == GEMPORT_ID) || (ResourceType == ALLOC_ID)) {
734 if NumIDs == 1 {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000735 if NextID, err = PONRMgr.GenerateNextID(ctx, Resource); err != nil {
736 logger.Error(ctx, "Failed to Generate ID")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400737 return Result, err
738 }
739 Result = append(Result, NextID)
740 } else {
741 for NumIDs > 0 {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000742 if NextID, err = PONRMgr.GenerateNextID(ctx, Resource); err != nil {
743 logger.Error(ctx, "Failed to Generate ID")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400744 return Result, err
745 }
746 Result = append(Result, NextID)
747 NumIDs--
748 }
749 }
750 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000751 logger.Error(ctx, "get resource failed")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400752 return Result, err
753 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400754
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400755 //Update resource in kv store
npujarec5762e2020-01-01 14:08:48 +0530756 if PONRMgr.UpdateResource(ctx, Path, Resource) != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000757 logger.Errorf(ctx, "Failed to update resource %s", Path)
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000758 return nil, fmt.Errorf("Failed to update resource %s", Path)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400759 }
760 return Result, nil
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400761}
762
763func checkValidResourceType(ResourceType string) bool {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400764 KnownResourceTypes := []string{ONU_ID, ALLOC_ID, GEMPORT_ID, FLOW_ID}
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400765
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400766 for _, v := range KnownResourceTypes {
767 if v == ResourceType {
768 return true
769 }
770 }
771 return false
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400772}
773
npujarec5762e2020-01-01 14:08:48 +0530774func (PONRMgr *PONResourceManager) FreeResourceID(ctx context.Context, IntfID uint32, ResourceType string, ReleaseContent []uint32) bool {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400775 /*
776 Release alloc/gemport/onu/flow id for given OLT PON interface.
777 :param pon_intf_id: OLT PON interface id
778 :param resource_type: String to identify type of resource
779 :param release_content: required number of ids
780 :return boolean: True if all IDs in given release_content release else False
781 */
Rohan Agrawal02f784d2020-02-14 09:34:02 +0000782 if !checkValidResourceType(ResourceType) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000783 logger.Error(ctx, "Invalid resource type")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400784 return false
785 }
786 if ReleaseContent == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000787 logger.Debug(ctx, "Nothing to release")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400788 return true
789 }
790 // delegate to the master instance if sharing enabled across instances
791 SharedResourceMgr := PONRMgr.SharedResourceMgrs[PONRMgr.SharedIdxByType[ResourceType]]
792 if SharedResourceMgr != nil && PONRMgr != SharedResourceMgr {
npujarec5762e2020-01-01 14:08:48 +0530793 return SharedResourceMgr.FreeResourceID(ctx, IntfID, ResourceType, ReleaseContent)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400794 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000795 Path := PONRMgr.GetPath(ctx, IntfID, ResourceType)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400796 if Path == "" {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000797 logger.Error(ctx, "Failed to get path")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400798 return false
799 }
npujarec5762e2020-01-01 14:08:48 +0530800 Resource, err := PONRMgr.GetResource(ctx, Path)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400801 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000802 logger.Error(ctx, "Failed to get resource")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400803 return false
804 }
805 for _, Val := range ReleaseContent {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000806 PONRMgr.ReleaseID(ctx, Resource, Val)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400807 }
npujarec5762e2020-01-01 14:08:48 +0530808 if PONRMgr.UpdateResource(ctx, Path, Resource) != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000809 logger.Errorf(ctx, "Free resource for %s failed", Path)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400810 return false
811 }
812 return true
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400813}
814
npujarec5762e2020-01-01 14:08:48 +0530815func (PONRMgr *PONResourceManager) UpdateResource(ctx context.Context, Path string, Resource map[string]interface{}) error {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400816 /*
817 Update resource in resource kv store.
818 :param path: path to update resource
819 :param resource: resource need to be updated
820 :return boolean: True if resource updated in kv store else False
821 */
822 // TODO resource[POOL] = resource[POOL].bin
823 Value, err := json.Marshal(Resource)
824 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000825 logger.Error(ctx, "failed to Marshal")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400826 return err
827 }
npujarec5762e2020-01-01 14:08:48 +0530828 err = PONRMgr.KVStore.Put(ctx, Path, Value)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400829 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000830 logger.Error(ctx, "failed to put data to kv store %s", Path)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400831 return err
832 }
833 return nil
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400834}
835
npujarec5762e2020-01-01 14:08:48 +0530836func (PONRMgr *PONResourceManager) ClearResourceIDPool(ctx context.Context, contIntfID uint32, ResourceType string) bool {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400837 /*
838 Clear Resource Pool for a given Resource Type on a given PON Port.
839 :return boolean: True if removed else False
840 */
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400841
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400842 // delegate to the master instance if sharing enabled across instances
843 SharedResourceMgr := PONRMgr.SharedResourceMgrs[PONRMgr.SharedIdxByType[ResourceType]]
844 if SharedResourceMgr != nil && PONRMgr != SharedResourceMgr {
npujarec5762e2020-01-01 14:08:48 +0530845 return SharedResourceMgr.ClearResourceIDPool(ctx, contIntfID, ResourceType)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400846 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000847 Path := PONRMgr.GetPath(ctx, contIntfID, ResourceType)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400848 if Path == "" {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000849 logger.Error(ctx, "Failed to get path")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400850 return false
851 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400852
npujarec5762e2020-01-01 14:08:48 +0530853 if err := PONRMgr.KVStore.Delete(ctx, Path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000854 logger.Errorf(ctx, "Failed to delete resource %s", Path)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400855 return false
856 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000857 logger.Debugf(ctx, "Cleared resource %s", Path)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400858 return true
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400859}
860
npujarec5762e2020-01-01 14:08:48 +0530861func (PONRMgr PONResourceManager) InitResourceMap(ctx context.Context, PONIntfONUID string) {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400862 /*
863 Initialize resource map
864 :param pon_intf_onu_id: reference of PON interface id and onu id
865 */
866 // initialize pon_intf_onu_id tuple to alloc_ids map
867 AllocIDPath := fmt.Sprintf(ALLOC_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, PONIntfONUID)
868 var AllocIDs []byte
npujarec5762e2020-01-01 14:08:48 +0530869 Result := PONRMgr.KVStore.Put(ctx, AllocIDPath, AllocIDs)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400870 if Result != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000871 logger.Error(ctx, "Failed to update the KV store")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400872 return
873 }
874 // initialize pon_intf_onu_id tuple to gemport_ids map
875 GEMPortIDPath := fmt.Sprintf(GEMPORT_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, PONIntfONUID)
876 var GEMPortIDs []byte
npujarec5762e2020-01-01 14:08:48 +0530877 Result = PONRMgr.KVStore.Put(ctx, GEMPortIDPath, GEMPortIDs)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400878 if Result != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000879 logger.Error(ctx, "Failed to update the KV store")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400880 return
881 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400882}
883
npujarec5762e2020-01-01 14:08:48 +0530884func (PONRMgr PONResourceManager) RemoveResourceMap(ctx context.Context, PONIntfONUID string) bool {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400885 /*
886 Remove resource map
887 :param pon_intf_onu_id: reference of PON interface id and onu id
888 */
889 // remove pon_intf_onu_id tuple to alloc_ids map
890 var err error
891 AllocIDPath := fmt.Sprintf(ALLOC_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, PONIntfONUID)
npujarec5762e2020-01-01 14:08:48 +0530892 if err = PONRMgr.KVStore.Delete(ctx, AllocIDPath); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000893 logger.Errorf(ctx, "Failed to remove resource %s", AllocIDPath)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400894 return false
895 }
896 // remove pon_intf_onu_id tuple to gemport_ids map
897 GEMPortIDPath := fmt.Sprintf(GEMPORT_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, PONIntfONUID)
npujarec5762e2020-01-01 14:08:48 +0530898 err = PONRMgr.KVStore.Delete(ctx, GEMPortIDPath)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400899 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000900 logger.Errorf(ctx, "Failed to remove resource %s", GEMPortIDPath)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400901 return false
902 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400903
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400904 FlowIDPath := fmt.Sprintf(FLOW_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, PONIntfONUID)
npujarec5762e2020-01-01 14:08:48 +0530905 if FlowIDs, err := PONRMgr.KVStore.List(ctx, FlowIDPath); err != nil {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400906 for _, Flow := range FlowIDs {
907 FlowIDInfoPath := fmt.Sprintf(FLOW_ID_INFO_PATH, PONRMgr.DeviceID, PONIntfONUID, Flow.Value)
npujarec5762e2020-01-01 14:08:48 +0530908 if err = PONRMgr.KVStore.Delete(ctx, FlowIDInfoPath); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000909 logger.Errorf(ctx, "Failed to remove resource %s", FlowIDInfoPath)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400910 return false
911 }
912 }
913 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400914
npujarec5762e2020-01-01 14:08:48 +0530915 if err = PONRMgr.KVStore.Delete(ctx, FlowIDPath); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000916 logger.Errorf(ctx, "Failed to remove resource %s", FlowIDPath)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400917 return false
918 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400919
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400920 return true
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400921}
922
npujarec5762e2020-01-01 14:08:48 +0530923func (PONRMgr *PONResourceManager) GetCurrentAllocIDForOnu(ctx context.Context, IntfONUID string) []uint32 {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400924 /*
925 Get currently configured alloc ids for given pon_intf_onu_id
926 :param pon_intf_onu_id: reference of PON interface id and onu id
927 :return list: List of alloc_ids if available, else None
928 */
929 Path := fmt.Sprintf(ALLOC_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400930
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400931 var Data []uint32
npujarec5762e2020-01-01 14:08:48 +0530932 Value, err := PONRMgr.KVStore.Get(ctx, Path)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400933 if err == nil {
934 if Value != nil {
935 Val, err := ToByte(Value.Value)
936 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000937 logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"error": err})
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400938 return Data
939 }
940 if err = json.Unmarshal(Val, &Data); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000941 logger.Error(ctx, "Failed to unmarshal", log.Fields{"error": err})
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400942 return Data
943 }
944 }
945 }
946 return Data
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400947}
948
npujarec5762e2020-01-01 14:08:48 +0530949func (PONRMgr *PONResourceManager) GetCurrentGEMPortIDsForOnu(ctx context.Context, IntfONUID string) []uint32 {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400950 /*
951 Get currently configured gemport ids for given pon_intf_onu_id
952 :param pon_intf_onu_id: reference of PON interface id and onu id
953 :return list: List of gemport IDs if available, else None
954 */
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400955
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400956 Path := fmt.Sprintf(GEMPORT_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID)
Neha Sharma96b7bf22020-06-15 10:37:32 +0000957 logger.Debugf(ctx, "Getting current gemports for %s", Path)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400958 var Data []uint32
npujarec5762e2020-01-01 14:08:48 +0530959 Value, err := PONRMgr.KVStore.Get(ctx, Path)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400960 if err == nil {
961 if Value != nil {
962 Val, _ := ToByte(Value.Value)
963 if err = json.Unmarshal(Val, &Data); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000964 logger.Errorw(ctx, "Failed to unmarshal", log.Fields{"error": err})
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400965 return Data
966 }
967 }
968 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000969 logger.Errorf(ctx, "Failed to get data from kvstore for %s", Path)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400970 }
971 return Data
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400972}
973
npujarec5762e2020-01-01 14:08:48 +0530974func (PONRMgr *PONResourceManager) GetCurrentFlowIDsForOnu(ctx context.Context, IntfONUID string) []uint32 {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400975 /*
976 Get currently configured flow ids for given pon_intf_onu_id
977 :param pon_intf_onu_id: reference of PON interface id and onu id
978 :return list: List of Flow IDs if available, else None
979 */
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400980
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400981 Path := fmt.Sprintf(FLOW_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400982
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400983 var Data []uint32
npujarec5762e2020-01-01 14:08:48 +0530984 Value, err := PONRMgr.KVStore.Get(ctx, Path)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400985 if err == nil {
986 if Value != nil {
987 Val, _ := ToByte(Value.Value)
988 if err = json.Unmarshal(Val, &Data); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000989 logger.Error(ctx, "Failed to unmarshal")
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400990 return Data
991 }
992 }
993 }
994 return Data
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400995}
996
npujarec5762e2020-01-01 14:08:48 +0530997func (PONRMgr *PONResourceManager) GetFlowIDInfo(ctx context.Context, IntfONUID string, FlowID uint32, Data interface{}) error {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400998 /*
999 Get flow details configured for the ONU.
1000 :param pon_intf_onu_id: reference of PON interface id and onu id
1001 :param flow_id: Flow Id reference
1002 :param Data: Result
1003 :return error: nil if no error in getting from KV store
1004 */
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001005
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001006 Path := fmt.Sprintf(FLOW_ID_INFO_PATH, PONRMgr.DeviceID, IntfONUID, FlowID)
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001007
npujarec5762e2020-01-01 14:08:48 +05301008 Value, err := PONRMgr.KVStore.Get(ctx, Path)
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001009 if err == nil {
1010 if Value != nil {
1011 Val, err := ToByte(Value.Value)
1012 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001013 logger.Errorw(ctx, "Failed to convert flowinfo into byte array", log.Fields{"error": err})
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001014 return err
1015 }
1016 if err = json.Unmarshal(Val, Data); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001017 logger.Errorw(ctx, "Failed to unmarshal", log.Fields{"error": err})
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001018 return err
1019 }
1020 }
1021 }
1022 return err
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001023}
1024
npujarec5762e2020-01-01 14:08:48 +05301025func (PONRMgr *PONResourceManager) RemoveFlowIDInfo(ctx context.Context, IntfONUID string, FlowID uint32) bool {
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001026 /*
1027 Get flow_id details configured for the ONU.
1028 :param pon_intf_onu_id: reference of PON interface id and onu id
1029 :param flow_id: Flow Id reference
1030 */
1031 Path := fmt.Sprintf(FLOW_ID_INFO_PATH, PONRMgr.DeviceID, IntfONUID, FlowID)
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001032
npujarec5762e2020-01-01 14:08:48 +05301033 if err := PONRMgr.KVStore.Delete(ctx, Path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001034 logger.Errorf(ctx, "Falied to remove resource %s", Path)
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001035 return false
1036 }
1037 return true
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001038}
1039
serkant.uluderya7b8211e2021-02-24 16:39:18 +03001040func (PONRMgr *PONResourceManager) RemoveAllFlowIDInfo(ctx context.Context, IntfONUID string) bool {
1041 /*
1042 Remove flow_id_info details configured for the ONU.
1043 :param pon_intf_onu_id: reference of PON interface id and onu id
1044 */
1045 Path := fmt.Sprintf(FLOW_ID_INFO_PATH_INTF_ONU_PREFIX, PONRMgr.DeviceID, IntfONUID)
1046
1047 if err := PONRMgr.KVStore.DeleteWithPrefix(ctx, Path); err != nil {
1048 logger.Errorf(ctx, "Falied to remove resource %s", Path)
1049 return false
1050 }
1051 return true
1052}
1053
npujarec5762e2020-01-01 14:08:48 +05301054func (PONRMgr *PONResourceManager) UpdateAllocIdsForOnu(ctx context.Context, IntfONUID string, AllocIDs []uint32) error {
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001055 /*
1056 Update currently configured alloc ids for given pon_intf_onu_id
1057 :param pon_intf_onu_id: reference of PON interface id and onu id
1058 :param alloc_ids: list of alloc ids
1059 */
1060 var Value []byte
1061 var err error
1062 Path := fmt.Sprintf(ALLOC_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID)
1063 Value, err = json.Marshal(AllocIDs)
1064 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001065 logger.Error(ctx, "failed to Marshal")
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001066 return err
1067 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001068
npujarec5762e2020-01-01 14:08:48 +05301069 if err = PONRMgr.KVStore.Put(ctx, Path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001070 logger.Errorf(ctx, "Failed to update resource %s", Path)
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001071 return err
1072 }
1073 return err
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001074}
1075
npujarec5762e2020-01-01 14:08:48 +05301076func (PONRMgr *PONResourceManager) UpdateGEMPortIDsForOnu(ctx context.Context, IntfONUID string, GEMPortIDs []uint32) error {
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001077 /*
1078 Update currently configured gemport ids for given pon_intf_onu_id
1079 :param pon_intf_onu_id: reference of PON interface id and onu id
1080 :param gemport_ids: list of gem port ids
1081 */
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001082
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001083 var Value []byte
1084 var err error
1085 Path := fmt.Sprintf(GEMPORT_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID)
Neha Sharma96b7bf22020-06-15 10:37:32 +00001086 logger.Debugf(ctx, "Updating gemport ids for %s", Path)
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001087 Value, err = json.Marshal(GEMPortIDs)
1088 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001089 logger.Error(ctx, "failed to Marshal")
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001090 return err
1091 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001092
npujarec5762e2020-01-01 14:08:48 +05301093 if err = PONRMgr.KVStore.Put(ctx, Path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001094 logger.Errorf(ctx, "Failed to update resource %s", Path)
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001095 return err
1096 }
1097 return err
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001098}
1099
1100func checkForFlowIDInList(FlowIDList []uint32, FlowID uint32) (bool, uint32) {
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001101 /*
1102 Check for a flow id in a given list of flow IDs.
1103 :param FLowIDList: List of Flow IDs
1104 :param FlowID: Flowd to check in the list
1105 : return true and the index if present false otherwise.
1106 */
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001107
Rohan Agrawal02f784d2020-02-14 09:34:02 +00001108 for idx := range FlowIDList {
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001109 if FlowID == FlowIDList[idx] {
1110 return true, uint32(idx)
1111 }
1112 }
1113 return false, 0
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001114}
1115
npujarec5762e2020-01-01 14:08:48 +05301116func (PONRMgr *PONResourceManager) UpdateFlowIDForOnu(ctx context.Context, IntfONUID string, FlowID uint32, Add bool) error {
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001117 /*
1118 Update the flow_id list of the ONU (add or remove flow_id from the list)
1119 :param pon_intf_onu_id: reference of PON interface id and onu id
1120 :param flow_id: flow ID
1121 :param add: Boolean flag to indicate whether the flow_id should be
1122 added or removed from the list. Defaults to adding the flow.
1123 */
1124 var Value []byte
1125 var err error
1126 var RetVal bool
1127 var IDx uint32
1128 Path := fmt.Sprintf(FLOW_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID)
npujarec5762e2020-01-01 14:08:48 +05301129 FlowIDs := PONRMgr.GetCurrentFlowIDsForOnu(ctx, IntfONUID)
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001130
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001131 if Add {
Rohan Agrawal02f784d2020-02-14 09:34:02 +00001132 if RetVal, _ = checkForFlowIDInList(FlowIDs, FlowID); RetVal {
1133 return nil
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001134 }
1135 FlowIDs = append(FlowIDs, FlowID)
1136 } else {
Rohan Agrawal02f784d2020-02-14 09:34:02 +00001137 if RetVal, IDx = checkForFlowIDInList(FlowIDs, FlowID); !RetVal {
1138 return nil
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001139 }
1140 // delete the index and shift
1141 FlowIDs = append(FlowIDs[:IDx], FlowIDs[IDx+1:]...)
1142 }
1143 Value, err = json.Marshal(FlowIDs)
1144 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001145 logger.Error(ctx, "Failed to Marshal")
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001146 return err
1147 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001148
npujarec5762e2020-01-01 14:08:48 +05301149 if err = PONRMgr.KVStore.Put(ctx, Path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001150 logger.Errorf(ctx, "Failed to update resource %s", Path)
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001151 return err
1152 }
1153 return err
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001154}
1155
npujarec5762e2020-01-01 14:08:48 +05301156func (PONRMgr *PONResourceManager) UpdateFlowIDInfoForOnu(ctx context.Context, IntfONUID string, FlowID uint32, FlowData interface{}) error {
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001157 /*
1158 Update any metadata associated with the flow_id. The flow_data could be json
1159 or any of other data structure. The resource manager doesnt care
1160 :param pon_intf_onu_id: reference of PON interface id and onu id
1161 :param flow_id: Flow ID
1162 :param flow_data: Flow data blob
1163 */
1164 var Value []byte
1165 var err error
1166 Path := fmt.Sprintf(FLOW_ID_INFO_PATH, PONRMgr.DeviceID, IntfONUID, FlowID)
1167 Value, err = json.Marshal(FlowData)
1168 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001169 logger.Error(ctx, "failed to Marshal")
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001170 return err
1171 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001172
npujarec5762e2020-01-01 14:08:48 +05301173 if err = PONRMgr.KVStore.Put(ctx, Path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001174 logger.Errorf(ctx, "Failed to update resource %s", Path)
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001175 return err
1176 }
1177 return err
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001178}
1179
Neha Sharma96b7bf22020-06-15 10:37:32 +00001180func (PONRMgr *PONResourceManager) GenerateNextID(ctx context.Context, Resource map[string]interface{}) (uint32, error) {
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001181 /*
1182 Generate unique id having OFFSET as start
1183 :param resource: resource used to generate ID
1184 :return uint32: generated id
1185 */
1186 ByteArray, err := ToByte(Resource[POOL])
1187 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001188 logger.Error(ctx, "Failed to convert resource to byte array")
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001189 return 0, err
1190 }
1191 Data := bitmap.TSFromData(ByteArray, false)
1192 if Data == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001193 logger.Error(ctx, "Failed to get data from byte array")
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001194 return 0, errors.New("Failed to get data from byte array")
1195 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001196
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001197 Len := Data.Len()
1198 var Idx int
1199 for Idx = 0; Idx < Len; Idx++ {
Rohan Agrawal02f784d2020-02-14 09:34:02 +00001200 if !Data.Get(Idx) {
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001201 break
1202 }
1203 }
1204 Data.Set(Idx, true)
1205 res := uint32(Resource[START_IDX].(float64))
1206 Resource[POOL] = Data.Data(false)
Neha Sharma96b7bf22020-06-15 10:37:32 +00001207 logger.Debugf(ctx, "Generated ID for %d", (uint32(Idx) + res))
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001208 return (uint32(Idx) + res), err
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001209}
1210
Neha Sharma96b7bf22020-06-15 10:37:32 +00001211func (PONRMgr *PONResourceManager) ReleaseID(ctx context.Context, Resource map[string]interface{}, Id uint32) bool {
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001212 /*
1213 Release unique id having OFFSET as start index.
1214 :param resource: resource used to release ID
1215 :param unique_id: id need to be released
1216 */
1217 ByteArray, err := ToByte(Resource[POOL])
1218 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001219 logger.Error(ctx, "Failed to convert resource to byte array")
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001220 return false
1221 }
1222 Data := bitmap.TSFromData(ByteArray, false)
1223 if Data == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001224 logger.Error(ctx, "Failed to get resource pool")
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001225 return false
1226 }
Rohan Agrawal02f784d2020-02-14 09:34:02 +00001227 Idx := Id - uint32(Resource[START_IDX].(float64))
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001228 Data.Set(int(Idx), false)
1229 Resource[POOL] = Data.Data(false)
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001230
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001231 return true
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001232}
1233
divyadesaid26f6b12020-03-19 06:30:28 +00001234/* Reserves a unique id in the specified resource pool.
1235:param Resource: resource used to reserve ID
1236:param Id: ID to be reserved
1237*/
Neha Sharma96b7bf22020-06-15 10:37:32 +00001238func (PONRMgr *PONResourceManager) reserveID(ctx context.Context, TSData *bitmap.Threadsafe, StartIndex uint32, Id uint32) bool {
divyadesaid26f6b12020-03-19 06:30:28 +00001239 Data := bitmap.TSFromData(TSData.Data(false), false)
1240 if Data == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001241 logger.Error(ctx, "Failed to get resource pool")
divyadesaid26f6b12020-03-19 06:30:28 +00001242 return false
1243 }
1244 Idx := Id - StartIndex
1245 Data.Set(int(Idx), true)
1246 return true
1247}
1248
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001249func (PONRMgr *PONResourceManager) GetTechnology() string {
1250 return PONRMgr.Technology
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001251}
1252
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001253func (PONRMgr *PONResourceManager) GetResourceTypeAllocID() string {
1254 return ALLOC_ID
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001255}
1256
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001257func (PONRMgr *PONResourceManager) GetResourceTypeGemPortID() string {
1258 return GEMPORT_ID
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001259}
1260
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001261// ToByte converts an interface value to a []byte. The interface should either be of
1262// a string type or []byte. Otherwise, an error is returned.
1263func ToByte(value interface{}) ([]byte, error) {
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001264 switch t := value.(type) {
1265 case []byte:
1266 return value.([]byte), nil
1267 case string:
1268 return []byte(value.(string)), nil
1269 default:
1270 return nil, fmt.Errorf("unexpected-type-%T", t)
1271 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001272}
1273
1274// ToString converts an interface value to a string. The interface should either be of
1275// a string type or []byte. Otherwise, an error is returned.
1276func ToString(value interface{}) (string, error) {
Matt Jeanneret384d8c92019-05-06 14:27:31 -04001277 switch t := value.(type) {
1278 case []byte:
1279 return string(value.([]byte)), nil
1280 case string:
1281 return value.(string), nil
1282 default:
1283 return "", fmt.Errorf("unexpected-type-%T", t)
1284 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001285}
Scott Bakerc6e54cb2019-11-04 09:31:25 -08001286
npujarec5762e2020-01-01 14:08:48 +05301287func (PONRMgr *PONResourceManager) AddOnuGemInfo(ctx context.Context, intfID uint32, onuGemData interface{}) error {
Scott Bakerc6e54cb2019-11-04 09:31:25 -08001288 /*
1289 Update onugem info map,
1290 :param pon_intf_id: reference of PON interface id
1291 :param onuegmdata: onugem info map
1292 */
1293 var Value []byte
1294 var err error
1295 Path := fmt.Sprintf(ONU_GEM_INFO_PATH, PONRMgr.DeviceID, intfID)
1296 Value, err = json.Marshal(onuGemData)
1297 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001298 logger.Error(ctx, "failed to Marshal")
Scott Bakerc6e54cb2019-11-04 09:31:25 -08001299 return err
1300 }
1301
npujarec5762e2020-01-01 14:08:48 +05301302 if err = PONRMgr.KVStore.Put(ctx, Path, Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001303 logger.Errorf(ctx, "Failed to update resource %s", Path)
Scott Bakerc6e54cb2019-11-04 09:31:25 -08001304 return err
1305 }
1306 return err
1307}
1308
npujarec5762e2020-01-01 14:08:48 +05301309func (PONRMgr *PONResourceManager) GetOnuGemInfo(ctx context.Context, IntfId uint32, onuGemInfo interface{}) error {
Scott Bakerc6e54cb2019-11-04 09:31:25 -08001310 /*
1311 Get onugeminfo map from kvstore
1312 :param intfid: refremce pon intfid
1313 :param onuGemInfo: onugem info to return from kv strore.
1314 */
1315 var Val []byte
1316
1317 path := fmt.Sprintf(ONU_GEM_INFO_PATH, PONRMgr.DeviceID, IntfId)
npujarec5762e2020-01-01 14:08:48 +05301318 value, err := PONRMgr.KVStore.Get(ctx, path)
Scott Bakerc6e54cb2019-11-04 09:31:25 -08001319 if err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001320 logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path})
Scott Bakerc6e54cb2019-11-04 09:31:25 -08001321 return err
1322 } else if value == nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001323 logger.Debug(ctx, "No onuinfo for path", log.Fields{"path": path})
Scott Bakerc6e54cb2019-11-04 09:31:25 -08001324 return nil // returning nil as this could happen if there are no onus for the interface yet
1325 }
1326 if Val, err = kvstore.ToByte(value.Value); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001327 logger.Error(ctx, "Failed to convert to byte array")
Scott Bakerc6e54cb2019-11-04 09:31:25 -08001328 return err
1329 }
1330
1331 if err = json.Unmarshal(Val, &onuGemInfo); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001332 logger.Error(ctx, "Failed to unmarshall")
Scott Bakerc6e54cb2019-11-04 09:31:25 -08001333 return err
1334 }
Neha Sharma96b7bf22020-06-15 10:37:32 +00001335 logger.Debugw(ctx, "found onuinfo from path", log.Fields{"path": path, "onuinfo": onuGemInfo})
Scott Bakerc6e54cb2019-11-04 09:31:25 -08001336 return err
1337}
1338
npujarec5762e2020-01-01 14:08:48 +05301339func (PONRMgr *PONResourceManager) DelOnuGemInfoForIntf(ctx context.Context, intfId uint32) error {
Scott Bakerc6e54cb2019-11-04 09:31:25 -08001340 /*
1341 delete onugem info for an interface from kvstore
1342 :param intfid: refremce pon intfid
1343 */
1344
1345 path := fmt.Sprintf(ONU_GEM_INFO_PATH, PONRMgr.DeviceID, intfId)
npujarec5762e2020-01-01 14:08:48 +05301346 if err := PONRMgr.KVStore.Delete(ctx, path); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001347 logger.Errorf(ctx, "Falied to remove resource %s", path)
Scott Bakerc6e54cb2019-11-04 09:31:25 -08001348 return err
1349 }
1350 return nil
1351}