Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package ponresourcemanager |
| 18 | |
| 19 | import ( |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 20 | "context" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 21 | "encoding/base64" |
| 22 | "encoding/json" |
| 23 | "errors" |
| 24 | "fmt" |
| 25 | "strconv" |
| 26 | |
serkant.uluderya | b38671c | 2019-11-01 09:35:38 -0700 | [diff] [blame] | 27 | bitmap "github.com/boljen/go-bitmap" |
| 28 | "github.com/opencord/voltha-lib-go/v3/pkg/db" |
| 29 | "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore" |
| 30 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 31 | tp "github.com/opencord/voltha-lib-go/v3/pkg/techprofile" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 32 | ) |
| 33 | |
| 34 | const ( |
| 35 | //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" |
| 41 | |
| 42 | //Constants for passing command line arugments |
| 43 | OLT_MODEL_ARG = "--olt_model" |
| 44 | PATH_PREFIX = "service/voltha/resource_manager/{%s}" |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 45 | |
| 46 | /*The path under which configuration data is stored is defined as technology/device agnostic. |
| 47 | That means the path does not include any specific technology/device variable. Using technology/device |
| 48 | agnostic path also makes northbound applications, that need to write to this path, |
| 49 | technology/device agnostic. |
| 50 | |
| 51 | Default kv client of PonResourceManager reads from/writes to PATH_PREFIX defined above. |
| 52 | That is why, an additional kv client (named KVStoreForConfig) is defined to read from the config path. |
| 53 | */ |
| 54 | PATH_PREFIX_FOR_CONFIG = "service/voltha/resource_manager/config" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 55 | /*The resource ranges for a given device model should be placed |
| 56 | at 'resource_manager/<technology>/resource_ranges/<olt_model_type>' |
| 57 | path on the KV store. |
| 58 | If Resource Range parameters are to be read from the external KV store, |
| 59 | they are expected to be stored in the following format. |
| 60 | Note: All parameters are MANDATORY for now. |
| 61 | constants used as keys to reference the resource range parameters from |
| 62 | and external KV store. |
| 63 | */ |
| 64 | UNI_ID_START_IDX = "uni_id_start" |
| 65 | UNI_ID_END_IDX = "uni_id_end" |
| 66 | ONU_ID_START_IDX = "onu_id_start" |
| 67 | ONU_ID_END_IDX = "onu_id_end" |
| 68 | ONU_ID_SHARED_IDX = "onu_id_shared" |
| 69 | ALLOC_ID_START_IDX = "alloc_id_start" |
| 70 | ALLOC_ID_END_IDX = "alloc_id_end" |
| 71 | ALLOC_ID_SHARED_IDX = "alloc_id_shared" |
| 72 | GEMPORT_ID_START_IDX = "gemport_id_start" |
| 73 | GEMPORT_ID_END_IDX = "gemport_id_end" |
| 74 | GEMPORT_ID_SHARED_IDX = "gemport_id_shared" |
| 75 | FLOW_ID_START_IDX = "flow_id_start" |
| 76 | FLOW_ID_END_IDX = "flow_id_end" |
| 77 | FLOW_ID_SHARED_IDX = "flow_id_shared" |
| 78 | NUM_OF_PON_PORT = "pon_ports" |
| 79 | |
| 80 | /* |
| 81 | The KV store backend is initialized with a path prefix and we need to |
| 82 | provide only the suffix. |
| 83 | */ |
| 84 | PON_RESOURCE_RANGE_CONFIG_PATH = "resource_ranges/%s" |
| 85 | |
| 86 | //resource path suffix |
| 87 | //Path on the KV store for storing alloc id ranges and resource pool for a given interface |
| 88 | //Format: <device_id>/alloc_id_pool/<pon_intf_id> |
| 89 | ALLOC_ID_POOL_PATH = "{%s}/alloc_id_pool/{%d}" |
| 90 | //Path on the KV store for storing gemport id ranges and resource pool for a given interface |
| 91 | //Format: <device_id>/gemport_id_pool/<pon_intf_id> |
| 92 | GEMPORT_ID_POOL_PATH = "{%s}/gemport_id_pool/{%d}" |
| 93 | //Path on the KV store for storing onu id ranges and resource pool for a given interface |
| 94 | //Format: <device_id>/onu_id_pool/<pon_intf_id> |
| 95 | ONU_ID_POOL_PATH = "{%s}/onu_id_pool/{%d}" |
| 96 | //Path on the KV store for storing flow id ranges and resource pool for a given interface |
| 97 | //Format: <device_id>/flow_id_pool/<pon_intf_id> |
| 98 | FLOW_ID_POOL_PATH = "{%s}/flow_id_pool/{%d}" |
| 99 | |
| 100 | //Path on the KV store for storing list of alloc IDs for a given ONU |
| 101 | //Format: <device_id>/<(pon_intf_id, onu_id)>/alloc_ids |
| 102 | ALLOC_ID_RESOURCE_MAP_PATH = "{%s}/{%s}/alloc_ids" |
| 103 | |
| 104 | //Path on the KV store for storing list of gemport IDs for a given ONU |
| 105 | //Format: <device_id>/<(pon_intf_id, onu_id)>/gemport_ids |
| 106 | GEMPORT_ID_RESOURCE_MAP_PATH = "{%s}/{%s}/gemport_ids" |
| 107 | |
| 108 | //Path on the KV store for storing list of Flow IDs for a given ONU |
| 109 | //Format: <device_id>/<(pon_intf_id, onu_id)>/flow_ids |
| 110 | FLOW_ID_RESOURCE_MAP_PATH = "{%s}/{%s}/flow_ids" |
| 111 | |
| 112 | //Flow Id info: Use to store more metadata associated with the flow_id |
| 113 | //Format: <device_id>/<(pon_intf_id, onu_id)>/flow_id_info/<flow_id> |
| 114 | FLOW_ID_INFO_PATH = "{%s}/{%s}/flow_id_info/{%d}" |
| 115 | |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 116 | //path on the kvstore to store onugem info map |
| 117 | //format: <device-id>/onu_gem_info/<intfid> |
| 118 | ONU_GEM_INFO_PATH = "{%s}/onu_gem_info/{%d}" // onu_gem/<(intfid)> |
| 119 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 120 | //Constants for internal usage. |
| 121 | PON_INTF_ID = "pon_intf_id" |
| 122 | START_IDX = "start_idx" |
| 123 | END_IDX = "end_idx" |
| 124 | POOL = "pool" |
| 125 | NUM_OF_PON_INTF = 16 |
| 126 | |
| 127 | KVSTORE_RETRY_TIMEOUT = 5 |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 128 | //Path on the KV store for storing reserved gem ports |
| 129 | //Format: reserved_gemport_ids |
| 130 | RESERVED_GEMPORT_IDS_PATH = "reserved_gemport_ids" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 131 | ) |
| 132 | |
| 133 | //type ResourceTypeIndex string |
| 134 | //type ResourceType string |
| 135 | |
| 136 | type PONResourceManager struct { |
| 137 | //Implements APIs to initialize/allocate/release alloc/gemport/onu IDs. |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 138 | Technology string |
| 139 | DeviceType string |
| 140 | DeviceID string |
| 141 | Backend string // ETCD, or consul |
| 142 | Host string // host ip of the KV store |
| 143 | Port int // port number for the KV store |
| 144 | OLTModel string |
| 145 | KVStore *db.Backend |
| 146 | KVStoreForConfig *db.Backend |
| 147 | TechProfileMgr tp.TechProfileIf // create object of *tp.TechProfileMgr |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 148 | |
| 149 | // Below attribute, pon_resource_ranges, should be initialized |
| 150 | // by reading from KV store. |
| 151 | PonResourceRanges map[string]interface{} |
| 152 | SharedResourceMgrs map[string]*PONResourceManager |
| 153 | SharedIdxByType map[string]string |
| 154 | IntfIDs []uint32 // list of pon interface IDs |
| 155 | Globalorlocal string |
| 156 | } |
| 157 | |
| 158 | func newKVClient(storeType string, address string, timeout int) (kvstore.Client, error) { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 159 | logger.Infow("kv-store-type", log.Fields{"store": storeType}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 160 | switch storeType { |
| 161 | case "consul": |
| 162 | return kvstore.NewConsulClient(address, timeout) |
| 163 | case "etcd": |
Rohan Agrawal | ee87e64 | 2020-04-14 10:22:18 +0000 | [diff] [blame] | 164 | return kvstore.NewEtcdClient(address, timeout, log.WarnLevel) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 165 | } |
| 166 | return nil, errors.New("unsupported-kv-store") |
| 167 | } |
| 168 | |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 169 | func SetKVClient(Technology string, Backend string, Host string, Port int, configClient bool) *db.Backend { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 170 | addr := Host + ":" + strconv.Itoa(Port) |
| 171 | // TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some |
| 172 | // issue between kv store and backend , core is not calling NewBackend directly |
| 173 | kvClient, err := newKVClient(Backend, addr, KVSTORE_RETRY_TIMEOUT) |
| 174 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 175 | logger.Fatalw("Failed to init KV client\n", log.Fields{"err": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 176 | return nil |
| 177 | } |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 178 | |
| 179 | var pathPrefix string |
| 180 | if configClient { |
| 181 | pathPrefix = PATH_PREFIX_FOR_CONFIG |
| 182 | } else { |
| 183 | pathPrefix = fmt.Sprintf(PATH_PREFIX, Technology) |
| 184 | } |
| 185 | |
sbarbari | 1e3e29c | 2019-11-05 10:06:50 -0500 | [diff] [blame] | 186 | kvbackend := &db.Backend{ |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 187 | Client: kvClient, |
| 188 | StoreType: Backend, |
| 189 | Host: Host, |
| 190 | Port: Port, |
| 191 | Timeout: KVSTORE_RETRY_TIMEOUT, |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 192 | PathPrefix: pathPrefix} |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 193 | |
| 194 | return kvbackend |
| 195 | } |
| 196 | |
| 197 | // NewPONResourceManager creates a new PON resource manager. |
| 198 | func NewPONResourceManager(Technology string, DeviceType string, DeviceID string, Backend string, Host string, Port int) (*PONResourceManager, error) { |
| 199 | var PONMgr PONResourceManager |
| 200 | PONMgr.Technology = Technology |
| 201 | PONMgr.DeviceType = DeviceType |
| 202 | PONMgr.DeviceID = DeviceID |
| 203 | PONMgr.Backend = Backend |
| 204 | PONMgr.Host = Host |
| 205 | PONMgr.Port = Port |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 206 | PONMgr.KVStore = SetKVClient(Technology, Backend, Host, Port, false) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 207 | if PONMgr.KVStore == nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 208 | logger.Error("KV Client initilization failed") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 209 | return nil, errors.New("Failed to init KV client") |
| 210 | } |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 211 | // init kv client to read from the config path |
| 212 | PONMgr.KVStoreForConfig = SetKVClient(Technology, Backend, Host, Port, true) |
| 213 | if PONMgr.KVStoreForConfig == nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 214 | logger.Error("KV Config Client initilization failed") |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 215 | return nil, errors.New("Failed to init KV Config client") |
| 216 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 217 | // Initialize techprofile for this technology |
| 218 | if PONMgr.TechProfileMgr, _ = tp.NewTechProfile(&PONMgr, Backend, Host, Port); PONMgr.TechProfileMgr == nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 219 | logger.Error("Techprofile initialization failed") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 220 | return nil, errors.New("Failed to init tech profile") |
| 221 | } |
| 222 | PONMgr.PonResourceRanges = make(map[string]interface{}) |
| 223 | PONMgr.SharedResourceMgrs = make(map[string]*PONResourceManager) |
| 224 | PONMgr.SharedIdxByType = make(map[string]string) |
| 225 | PONMgr.SharedIdxByType[ONU_ID] = ONU_ID_SHARED_IDX |
| 226 | PONMgr.SharedIdxByType[ALLOC_ID] = ALLOC_ID_SHARED_IDX |
| 227 | PONMgr.SharedIdxByType[GEMPORT_ID] = GEMPORT_ID_SHARED_IDX |
| 228 | PONMgr.SharedIdxByType[FLOW_ID] = FLOW_ID_SHARED_IDX |
| 229 | PONMgr.IntfIDs = make([]uint32, NUM_OF_PON_INTF) |
| 230 | PONMgr.OLTModel = DeviceType |
| 231 | return &PONMgr, nil |
| 232 | } |
| 233 | |
| 234 | /* |
| 235 | Initialize PON resource ranges with config fetched from kv store. |
| 236 | return boolean: True if PON resource ranges initialized else false |
| 237 | Try to initialize the PON Resource Ranges from KV store based on the |
| 238 | OLT model key, if available |
| 239 | */ |
| 240 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 241 | func (PONRMgr *PONResourceManager) InitResourceRangesFromKVStore(ctx context.Context) bool { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 242 | //Initialize PON resource ranges with config fetched from kv store. |
| 243 | //:return boolean: True if PON resource ranges initialized else false |
| 244 | // Try to initialize the PON Resource Ranges from KV store based on the |
| 245 | // OLT model key, if available |
| 246 | if PONRMgr.OLTModel == "" { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 247 | logger.Error("Failed to get OLT model") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 248 | return false |
| 249 | } |
| 250 | Path := fmt.Sprintf(PON_RESOURCE_RANGE_CONFIG_PATH, PONRMgr.OLTModel) |
| 251 | //get resource from kv store |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 252 | Result, err := PONRMgr.KVStore.Get(ctx, Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 253 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 254 | logger.Debugf("Error in fetching resource %s from KV strore", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 255 | return false |
| 256 | } |
| 257 | if Result == nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 258 | logger.Debug("There may be no resources in the KV store in case of fresh bootup, return true") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 259 | return false |
| 260 | } |
| 261 | //update internal ranges from kv ranges. If there are missing |
| 262 | // values in the KV profile, continue to use the defaults |
| 263 | Value, err := ToByte(Result.Value) |
| 264 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 265 | logger.Error("Failed to convert kvpair to byte string") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 266 | return false |
| 267 | } |
| 268 | if err := json.Unmarshal(Value, &PONRMgr.PonResourceRanges); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 269 | logger.Error("Failed to Unmarshal json byte") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 270 | return false |
| 271 | } |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 272 | logger.Debug("Init resource ranges from kvstore success") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 273 | return true |
| 274 | } |
| 275 | |
| 276 | func (PONRMgr *PONResourceManager) UpdateRanges(StartIDx string, StartID uint32, EndIDx string, EndID uint32, |
| 277 | SharedIDx string, SharedPoolID uint32, RMgr *PONResourceManager) { |
| 278 | /* |
| 279 | Update the ranges for all reosurce type in the intermnal maps |
| 280 | param: resource type start index |
| 281 | param: start ID |
| 282 | param: resource type end index |
| 283 | param: end ID |
| 284 | param: resource type shared index |
| 285 | param: shared pool id |
| 286 | param: global resource manager |
| 287 | */ |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 288 | logger.Debugf("update ranges for %s, %d", StartIDx, StartID) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 289 | |
| 290 | if StartID != 0 { |
| 291 | if (PONRMgr.PonResourceRanges[StartIDx] == nil) || (PONRMgr.PonResourceRanges[StartIDx].(uint32) < StartID) { |
| 292 | PONRMgr.PonResourceRanges[StartIDx] = StartID |
| 293 | } |
| 294 | } |
| 295 | if EndID != 0 { |
| 296 | if (PONRMgr.PonResourceRanges[EndIDx] == nil) || (PONRMgr.PonResourceRanges[EndIDx].(uint32) > EndID) { |
| 297 | PONRMgr.PonResourceRanges[EndIDx] = EndID |
| 298 | } |
| 299 | } |
| 300 | //if SharedPoolID != 0 { |
| 301 | PONRMgr.PonResourceRanges[SharedIDx] = SharedPoolID |
| 302 | //} |
| 303 | if RMgr != nil { |
| 304 | PONRMgr.SharedResourceMgrs[SharedIDx] = RMgr |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | func (PONRMgr *PONResourceManager) InitDefaultPONResourceRanges(ONUIDStart uint32, |
| 309 | 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 { |
| 324 | |
| 325 | /*Initialize default PON resource ranges |
| 326 | |
| 327 | :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 | */ |
| 342 | PONRMgr.UpdateRanges(ONU_ID_START_IDX, ONUIDStart, ONU_ID_END_IDX, ONUIDEnd, ONU_ID_SHARED_IDX, ONUIDSharedPoolID, nil) |
| 343 | PONRMgr.UpdateRanges(ALLOC_ID_START_IDX, AllocIDStart, ALLOC_ID_END_IDX, AllocIDEnd, ALLOC_ID_SHARED_IDX, AllocIDSharedPoolID, nil) |
| 344 | PONRMgr.UpdateRanges(GEMPORT_ID_START_IDX, GEMPortIDStart, GEMPORT_ID_END_IDX, GEMPortIDEnd, GEMPORT_ID_SHARED_IDX, GEMPortIDSharedPoolID, nil) |
| 345 | PONRMgr.UpdateRanges(FLOW_ID_START_IDX, FlowIDStart, FLOW_ID_END_IDX, FlowIDEnd, FLOW_ID_SHARED_IDX, FlowIDSharedPoolID, nil) |
| 346 | PONRMgr.UpdateRanges(UNI_ID_START_IDX, UNIIDStart, UNI_ID_END_IDX, UNIIDEnd, "", 0, nil) |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 347 | logger.Debug("Initialize default range values") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 348 | 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 |
| 357 | } |
| 358 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 359 | func (PONRMgr *PONResourceManager) InitDeviceResourcePool(ctx context.Context) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 360 | |
| 361 | //Initialize resource pool for all PON ports. |
| 362 | |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 363 | logger.Debug("Init resource ranges") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 364 | |
| 365 | 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 | } |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 371 | if err = PONRMgr.InitResourceIDPool(ctx, Intf, ONU_ID, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 372 | PONRMgr.PonResourceRanges[ONU_ID_START_IDX].(uint32), |
| 373 | PONRMgr.PonResourceRanges[ONU_ID_END_IDX].(uint32)); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 374 | logger.Error("Failed to init ONU ID resource pool") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 375 | return err |
| 376 | } |
| 377 | if SharedPoolID != 0 { |
| 378 | break |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | for _, Intf := range PONRMgr.IntfIDs { |
| 383 | SharedPoolID := PONRMgr.PonResourceRanges[ALLOC_ID_SHARED_IDX].(uint32) |
| 384 | if SharedPoolID != 0 { |
| 385 | Intf = SharedPoolID |
| 386 | } |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 387 | if err = PONRMgr.InitResourceIDPool(ctx, Intf, ALLOC_ID, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 388 | PONRMgr.PonResourceRanges[ALLOC_ID_START_IDX].(uint32), |
| 389 | PONRMgr.PonResourceRanges[ALLOC_ID_END_IDX].(uint32)); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 390 | logger.Error("Failed to init ALLOC ID resource pool ") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 391 | 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 | } |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 402 | if err = PONRMgr.InitResourceIDPool(ctx, Intf, GEMPORT_ID, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 403 | PONRMgr.PonResourceRanges[GEMPORT_ID_START_IDX].(uint32), |
| 404 | PONRMgr.PonResourceRanges[GEMPORT_ID_END_IDX].(uint32)); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 405 | logger.Error("Failed to init GEMPORT ID resource pool") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 406 | return err |
| 407 | } |
| 408 | if SharedPoolID != 0 { |
| 409 | break |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | for _, Intf := range PONRMgr.IntfIDs { |
| 414 | SharedPoolID := PONRMgr.PonResourceRanges[FLOW_ID_SHARED_IDX].(uint32) |
| 415 | if SharedPoolID != 0 { |
| 416 | Intf = SharedPoolID |
| 417 | } |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 418 | if err = PONRMgr.InitResourceIDPool(ctx, Intf, FLOW_ID, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 419 | PONRMgr.PonResourceRanges[FLOW_ID_START_IDX].(uint32), |
| 420 | PONRMgr.PonResourceRanges[FLOW_ID_END_IDX].(uint32)); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 421 | logger.Error("Failed to init FLOW ID resource pool") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 422 | return err |
| 423 | } |
| 424 | if SharedPoolID != 0 { |
| 425 | break |
| 426 | } |
| 427 | } |
| 428 | return err |
| 429 | } |
| 430 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 431 | func (PONRMgr *PONResourceManager) ClearDeviceResourcePool(ctx context.Context) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 432 | |
| 433 | //Clear resource pool for all PON ports. |
| 434 | |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 435 | logger.Debug("Clear resource ranges") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 436 | |
| 437 | for _, Intf := range PONRMgr.IntfIDs { |
| 438 | SharedPoolID := PONRMgr.PonResourceRanges[ONU_ID_SHARED_IDX].(uint32) |
| 439 | if SharedPoolID != 0 { |
| 440 | Intf = SharedPoolID |
| 441 | } |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 442 | if status := PONRMgr.ClearResourceIDPool(ctx, Intf, ONU_ID); !status { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 443 | logger.Error("Failed to clear ONU ID resource pool") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 444 | 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 | } |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 456 | if status := PONRMgr.ClearResourceIDPool(ctx, Intf, ALLOC_ID); !status { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 457 | logger.Error("Failed to clear ALLOC ID resource pool ") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 458 | 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 | } |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 469 | if status := PONRMgr.ClearResourceIDPool(ctx, Intf, GEMPORT_ID); !status { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 470 | logger.Error("Failed to clear GEMPORT ID resource pool") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 471 | 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 | } |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 483 | if status := PONRMgr.ClearResourceIDPool(ctx, Intf, FLOW_ID); !status { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 484 | logger.Error("Failed to clear FLOW ID resource pool") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 485 | return errors.New("Failed to clear FLOW ID resource pool") |
| 486 | } |
| 487 | if SharedPoolID != 0 { |
| 488 | break |
| 489 | } |
| 490 | } |
| 491 | return nil |
| 492 | } |
| 493 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 494 | func (PONRMgr *PONResourceManager) InitResourceIDPool(ctx context.Context, Intf uint32, ResourceType string, StartID uint32, EndID uint32) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 495 | |
| 496 | /*Initialize Resource ID pool for a given Resource Type on a given PON Port |
| 497 | |
| 498 | :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 | */ |
| 504 | |
| 505 | // delegate to the master instance if sharing enabled across instances |
| 506 | SharedResourceMgr := PONRMgr.SharedResourceMgrs[PONRMgr.SharedIdxByType[ResourceType]] |
| 507 | if SharedResourceMgr != nil && PONRMgr != SharedResourceMgr { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 508 | return SharedResourceMgr.InitResourceIDPool(ctx, Intf, ResourceType, StartID, EndID) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | Path := PONRMgr.GetPath(Intf, ResourceType) |
| 512 | if Path == "" { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 513 | logger.Errorf("Failed to get path for resource type %s", ResourceType) |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 514 | return fmt.Errorf("Failed to get path for resource type %s", ResourceType) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | //In case of adapter reboot and reconciliation resource in kv store |
| 518 | //checked for its presence if not kv store update happens |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 519 | Res, err := PONRMgr.GetResource(ctx, Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 520 | if (err == nil) && (Res != nil) { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 521 | logger.Debugf("Resource %s already present in store ", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 522 | return nil |
| 523 | } else { |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 524 | 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 |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 529 | logger.Debugw("Excluding some ports from GEM port id pool", log.Fields{"excluded gem ports": excluded}) |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | FormatResult, err := PONRMgr.FormatResource(Intf, StartID, EndID, excluded) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 533 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 534 | logger.Errorf("Failed to format resource") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 535 | return err |
| 536 | } |
| 537 | // Add resource as json in kv store. |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 538 | err = PONRMgr.KVStore.Put(ctx, Path, FormatResult) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 539 | if err == nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 540 | logger.Debug("Successfuly posted to kv store") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 541 | return err |
| 542 | } |
| 543 | } |
| 544 | |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 545 | logger.Debug("Error initializing pool") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 546 | |
| 547 | return err |
| 548 | } |
| 549 | |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 550 | func (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 { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 555 | logger.Errorw("Unable to get reserved GEM port ids from the kv store", log.Fields{"err": err}) |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 556 | 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 { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 564 | logger.Errorw("Failed to convert reserved gem port ids into byte array", log.Fields{"err": err}) |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 565 | return reservedGemPortIds, false |
| 566 | } |
| 567 | if err = json.Unmarshal(Val, &reservedGemPortIds); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 568 | logger.Errorw("Failed to unmarshal reservedGemPortIds", log.Fields{"err": err}) |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 569 | return reservedGemPortIds, false |
| 570 | } |
| 571 | return reservedGemPortIds, true |
| 572 | } |
| 573 | |
| 574 | func (PONRMgr *PONResourceManager) FormatResource(IntfID uint32, StartIDx uint32, EndIDx uint32, |
| 575 | Excluded []uint32) ([]byte, error) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 576 | /* |
| 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 |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 581 | :Id values to be Excluded from the pool |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 582 | :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 { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 596 | logger.Error("Failed to create a bitmap") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 597 | return nil, errors.New("Failed to create bitmap") |
| 598 | } |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 599 | for _, excludedID := range Excluded { |
| 600 | if excludedID < StartIDx || excludedID > EndIDx { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 601 | logger.Warnf("Cannot reserve %d. It must be in the range of [%d, %d]", excludedID, |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 602 | StartIDx, EndIDx) |
| 603 | continue |
| 604 | } |
| 605 | PONRMgr.reserveID(TSData, StartIDx, excludedID) |
| 606 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 607 | Resource[POOL] = TSData.Data(false) //we pass false so as the TSData lib api does not do a copy of the data and return |
| 608 | |
| 609 | Value, err := json.Marshal(Resource) |
| 610 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 611 | logger.Errorf("Failed to marshall resource") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 612 | return nil, err |
| 613 | } |
| 614 | return Value, err |
| 615 | } |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 616 | func (PONRMgr *PONResourceManager) GetResource(ctx context.Context, Path string) (map[string]interface{}, error) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 617 | /* |
| 618 | Get resource from kv store. |
| 619 | |
| 620 | :param path: path to get resource |
| 621 | :return: resource if resource present in kv store else None |
| 622 | */ |
| 623 | //get resource from kv store |
| 624 | |
| 625 | var Value []byte |
| 626 | Result := make(map[string]interface{}) |
| 627 | var Str string |
| 628 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 629 | Resource, err := PONRMgr.KVStore.Get(ctx, Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 630 | if (err != nil) || (Resource == nil) { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 631 | logger.Debugf("Resource unavailable at %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 632 | return nil, err |
| 633 | } |
| 634 | |
| 635 | Value, err = ToByte(Resource.Value) |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 636 | if err != nil { |
| 637 | return nil, err |
| 638 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 639 | |
| 640 | // decode resource fetched from backend store to dictionary |
| 641 | err = json.Unmarshal(Value, &Result) |
| 642 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 643 | logger.Error("Failed to decode resource") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 644 | 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 { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 653 | logger.Error("Failed to conver to kv pair to string") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 654 | return Result, err |
| 655 | } |
| 656 | Decode64, _ := base64.StdEncoding.DecodeString(Str) |
| 657 | Result[POOL], err = ToByte(Decode64) |
| 658 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 659 | logger.Error("Failed to convert resource pool to byte") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 660 | return Result, err |
| 661 | } |
| 662 | |
| 663 | return Result, err |
| 664 | } |
| 665 | |
| 666 | func (PONRMgr *PONResourceManager) GetPath(IntfID uint32, ResourceType string) string { |
| 667 | /* |
| 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 | */ |
| 673 | |
| 674 | /* |
| 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 { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 692 | logger.Error("Invalid resource pool identifier") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 693 | } |
| 694 | return Path |
| 695 | } |
| 696 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 697 | func (PONRMgr *PONResourceManager) GetResourceID(ctx context.Context, IntfID uint32, ResourceType string, NumIDs uint32) ([]uint32, error) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 698 | /* |
| 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 { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 707 | logger.Error("Invalid number of resources requested") |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 708 | return nil, fmt.Errorf("Invalid number of resources requested %d", NumIDs) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 709 | } |
| 710 | // delegate to the master instance if sharing enabled across instances |
| 711 | |
| 712 | SharedResourceMgr := PONRMgr.SharedResourceMgrs[PONRMgr.SharedIdxByType[ResourceType]] |
| 713 | if SharedResourceMgr != nil && PONRMgr != SharedResourceMgr { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 714 | return SharedResourceMgr.GetResourceID(ctx, IntfID, ResourceType, NumIDs) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 715 | } |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 716 | logger.Debugf("Fetching resource from %s rsrc mgr for resource %s", PONRMgr.Globalorlocal, ResourceType) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 717 | |
| 718 | Path := PONRMgr.GetPath(IntfID, ResourceType) |
| 719 | if Path == "" { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 720 | logger.Errorf("Failed to get path for resource type %s", ResourceType) |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 721 | return nil, fmt.Errorf("Failed to get path for resource type %s", ResourceType) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 722 | } |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 723 | logger.Debugf("Get resource for type %s on path %s", ResourceType, Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 724 | var Result []uint32 |
| 725 | var NextID uint32 |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 726 | Resource, err := PONRMgr.GetResource(ctx, Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 727 | if (err == nil) && (ResourceType == ONU_ID) || (ResourceType == FLOW_ID) { |
| 728 | if NextID, err = PONRMgr.GenerateNextID(Resource); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 729 | logger.Error("Failed to Generate ID") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 730 | return Result, err |
| 731 | } |
| 732 | Result = append(Result, NextID) |
| 733 | } else if (err == nil) && ((ResourceType == GEMPORT_ID) || (ResourceType == ALLOC_ID)) { |
| 734 | if NumIDs == 1 { |
| 735 | if NextID, err = PONRMgr.GenerateNextID(Resource); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 736 | logger.Error("Failed to Generate ID") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 737 | return Result, err |
| 738 | } |
| 739 | Result = append(Result, NextID) |
| 740 | } else { |
| 741 | for NumIDs > 0 { |
| 742 | if NextID, err = PONRMgr.GenerateNextID(Resource); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 743 | logger.Error("Failed to Generate ID") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 744 | return Result, err |
| 745 | } |
| 746 | Result = append(Result, NextID) |
| 747 | NumIDs-- |
| 748 | } |
| 749 | } |
| 750 | } else { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 751 | logger.Error("get resource failed") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 752 | return Result, err |
| 753 | } |
| 754 | |
| 755 | //Update resource in kv store |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 756 | if PONRMgr.UpdateResource(ctx, Path, Resource) != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 757 | logger.Errorf("Failed to update resource %s", Path) |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 758 | return nil, fmt.Errorf("Failed to update resource %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 759 | } |
| 760 | return Result, nil |
| 761 | } |
| 762 | |
| 763 | func checkValidResourceType(ResourceType string) bool { |
| 764 | KnownResourceTypes := []string{ONU_ID, ALLOC_ID, GEMPORT_ID, FLOW_ID} |
| 765 | |
| 766 | for _, v := range KnownResourceTypes { |
| 767 | if v == ResourceType { |
| 768 | return true |
| 769 | } |
| 770 | } |
| 771 | return false |
| 772 | } |
| 773 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 774 | func (PONRMgr *PONResourceManager) FreeResourceID(ctx context.Context, IntfID uint32, ResourceType string, ReleaseContent []uint32) bool { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 775 | /* |
| 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 | */ |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 782 | if !checkValidResourceType(ResourceType) { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 783 | logger.Error("Invalid resource type") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 784 | return false |
| 785 | } |
| 786 | if ReleaseContent == nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 787 | logger.Debug("Nothing to release") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 788 | 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 { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 793 | return SharedResourceMgr.FreeResourceID(ctx, IntfID, ResourceType, ReleaseContent) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 794 | } |
| 795 | Path := PONRMgr.GetPath(IntfID, ResourceType) |
| 796 | if Path == "" { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 797 | logger.Error("Failed to get path") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 798 | return false |
| 799 | } |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 800 | Resource, err := PONRMgr.GetResource(ctx, Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 801 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 802 | logger.Error("Failed to get resource") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 803 | return false |
| 804 | } |
| 805 | for _, Val := range ReleaseContent { |
| 806 | PONRMgr.ReleaseID(Resource, Val) |
| 807 | } |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 808 | if PONRMgr.UpdateResource(ctx, Path, Resource) != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 809 | logger.Errorf("Free resource for %s failed", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 810 | return false |
| 811 | } |
| 812 | return true |
| 813 | } |
| 814 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 815 | func (PONRMgr *PONResourceManager) UpdateResource(ctx context.Context, Path string, Resource map[string]interface{}) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 816 | /* |
| 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 { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 825 | logger.Error("failed to Marshal") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 826 | return err |
| 827 | } |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 828 | err = PONRMgr.KVStore.Put(ctx, Path, Value) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 829 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 830 | logger.Error("failed to put data to kv store %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 831 | return err |
| 832 | } |
| 833 | return nil |
| 834 | } |
| 835 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 836 | func (PONRMgr *PONResourceManager) ClearResourceIDPool(ctx context.Context, contIntfID uint32, ResourceType string) bool { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 837 | /* |
| 838 | Clear Resource Pool for a given Resource Type on a given PON Port. |
| 839 | :return boolean: True if removed else False |
| 840 | */ |
| 841 | |
| 842 | // delegate to the master instance if sharing enabled across instances |
| 843 | SharedResourceMgr := PONRMgr.SharedResourceMgrs[PONRMgr.SharedIdxByType[ResourceType]] |
| 844 | if SharedResourceMgr != nil && PONRMgr != SharedResourceMgr { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 845 | return SharedResourceMgr.ClearResourceIDPool(ctx, contIntfID, ResourceType) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 846 | } |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 847 | Path := PONRMgr.GetPath(contIntfID, ResourceType) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 848 | if Path == "" { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 849 | logger.Error("Failed to get path") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 850 | return false |
| 851 | } |
| 852 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 853 | if err := PONRMgr.KVStore.Delete(ctx, Path); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 854 | logger.Errorf("Failed to delete resource %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 855 | return false |
| 856 | } |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 857 | logger.Debugf("Cleared resource %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 858 | return true |
| 859 | } |
| 860 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 861 | func (PONRMgr PONResourceManager) InitResourceMap(ctx context.Context, PONIntfONUID string) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 862 | /* |
| 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 |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 869 | Result := PONRMgr.KVStore.Put(ctx, AllocIDPath, AllocIDs) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 870 | if Result != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 871 | logger.Error("Failed to update the KV store") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 872 | 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 |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 877 | Result = PONRMgr.KVStore.Put(ctx, GEMPortIDPath, GEMPortIDs) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 878 | if Result != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 879 | logger.Error("Failed to update the KV store") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 880 | return |
| 881 | } |
| 882 | } |
| 883 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 884 | func (PONRMgr PONResourceManager) RemoveResourceMap(ctx context.Context, PONIntfONUID string) bool { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 885 | /* |
| 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) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 892 | if err = PONRMgr.KVStore.Delete(ctx, AllocIDPath); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 893 | logger.Errorf("Failed to remove resource %s", AllocIDPath) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 894 | 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) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 898 | err = PONRMgr.KVStore.Delete(ctx, GEMPortIDPath) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 899 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 900 | logger.Errorf("Failed to remove resource %s", GEMPortIDPath) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 901 | return false |
| 902 | } |
| 903 | |
| 904 | FlowIDPath := fmt.Sprintf(FLOW_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, PONIntfONUID) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 905 | if FlowIDs, err := PONRMgr.KVStore.List(ctx, FlowIDPath); err != nil { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 906 | for _, Flow := range FlowIDs { |
| 907 | FlowIDInfoPath := fmt.Sprintf(FLOW_ID_INFO_PATH, PONRMgr.DeviceID, PONIntfONUID, Flow.Value) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 908 | if err = PONRMgr.KVStore.Delete(ctx, FlowIDInfoPath); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 909 | logger.Errorf("Failed to remove resource %s", FlowIDInfoPath) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 910 | return false |
| 911 | } |
| 912 | } |
| 913 | } |
| 914 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 915 | if err = PONRMgr.KVStore.Delete(ctx, FlowIDPath); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 916 | logger.Errorf("Failed to remove resource %s", FlowIDPath) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 917 | return false |
| 918 | } |
| 919 | |
| 920 | return true |
| 921 | } |
| 922 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 923 | func (PONRMgr *PONResourceManager) GetCurrentAllocIDForOnu(ctx context.Context, IntfONUID string) []uint32 { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 924 | /* |
| 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) |
| 930 | |
| 931 | var Data []uint32 |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 932 | Value, err := PONRMgr.KVStore.Get(ctx, Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 933 | if err == nil { |
| 934 | if Value != nil { |
| 935 | Val, err := ToByte(Value.Value) |
| 936 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 937 | logger.Errorw("Failed to convert into byte array", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 938 | return Data |
| 939 | } |
| 940 | if err = json.Unmarshal(Val, &Data); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 941 | logger.Error("Failed to unmarshal", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 942 | return Data |
| 943 | } |
| 944 | } |
| 945 | } |
| 946 | return Data |
| 947 | } |
| 948 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 949 | func (PONRMgr *PONResourceManager) GetCurrentGEMPortIDsForOnu(ctx context.Context, IntfONUID string) []uint32 { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 950 | /* |
| 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 | */ |
| 955 | |
| 956 | Path := fmt.Sprintf(GEMPORT_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID) |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 957 | logger.Debugf("Getting current gemports for %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 958 | var Data []uint32 |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 959 | Value, err := PONRMgr.KVStore.Get(ctx, Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 960 | if err == nil { |
| 961 | if Value != nil { |
| 962 | Val, _ := ToByte(Value.Value) |
| 963 | if err = json.Unmarshal(Val, &Data); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 964 | logger.Errorw("Failed to unmarshal", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 965 | return Data |
| 966 | } |
| 967 | } |
| 968 | } else { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 969 | logger.Errorf("Failed to get data from kvstore for %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 970 | } |
| 971 | return Data |
| 972 | } |
| 973 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 974 | func (PONRMgr *PONResourceManager) GetCurrentFlowIDsForOnu(ctx context.Context, IntfONUID string) []uint32 { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 975 | /* |
| 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 | */ |
| 980 | |
| 981 | Path := fmt.Sprintf(FLOW_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID) |
| 982 | |
| 983 | var Data []uint32 |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 984 | Value, err := PONRMgr.KVStore.Get(ctx, Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 985 | if err == nil { |
| 986 | if Value != nil { |
| 987 | Val, _ := ToByte(Value.Value) |
| 988 | if err = json.Unmarshal(Val, &Data); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 989 | logger.Error("Failed to unmarshal") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 990 | return Data |
| 991 | } |
| 992 | } |
| 993 | } |
| 994 | return Data |
| 995 | } |
| 996 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 997 | func (PONRMgr *PONResourceManager) GetFlowIDInfo(ctx context.Context, IntfONUID string, FlowID uint32, Data interface{}) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 998 | /* |
| 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 | */ |
| 1005 | |
| 1006 | Path := fmt.Sprintf(FLOW_ID_INFO_PATH, PONRMgr.DeviceID, IntfONUID, FlowID) |
| 1007 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1008 | Value, err := PONRMgr.KVStore.Get(ctx, Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1009 | if err == nil { |
| 1010 | if Value != nil { |
| 1011 | Val, err := ToByte(Value.Value) |
| 1012 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 1013 | logger.Errorw("Failed to convert flowinfo into byte array", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1014 | return err |
| 1015 | } |
| 1016 | if err = json.Unmarshal(Val, Data); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 1017 | logger.Errorw("Failed to unmarshal", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1018 | return err |
| 1019 | } |
| 1020 | } |
| 1021 | } |
| 1022 | return err |
| 1023 | } |
| 1024 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1025 | func (PONRMgr *PONResourceManager) RemoveFlowIDInfo(ctx context.Context, IntfONUID string, FlowID uint32) bool { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1026 | /* |
| 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) |
| 1032 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1033 | if err := PONRMgr.KVStore.Delete(ctx, Path); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 1034 | logger.Errorf("Falied to remove resource %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1035 | return false |
| 1036 | } |
| 1037 | return true |
| 1038 | } |
| 1039 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1040 | func (PONRMgr *PONResourceManager) UpdateAllocIdsForOnu(ctx context.Context, IntfONUID string, AllocIDs []uint32) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1041 | /* |
| 1042 | Update currently configured alloc ids for given pon_intf_onu_id |
| 1043 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 1044 | :param alloc_ids: list of alloc ids |
| 1045 | */ |
| 1046 | var Value []byte |
| 1047 | var err error |
| 1048 | Path := fmt.Sprintf(ALLOC_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID) |
| 1049 | Value, err = json.Marshal(AllocIDs) |
| 1050 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 1051 | logger.Error("failed to Marshal") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1052 | return err |
| 1053 | } |
| 1054 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1055 | if err = PONRMgr.KVStore.Put(ctx, Path, Value); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 1056 | logger.Errorf("Failed to update resource %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1057 | return err |
| 1058 | } |
| 1059 | return err |
| 1060 | } |
| 1061 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1062 | func (PONRMgr *PONResourceManager) UpdateGEMPortIDsForOnu(ctx context.Context, IntfONUID string, GEMPortIDs []uint32) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1063 | /* |
| 1064 | Update currently configured gemport ids for given pon_intf_onu_id |
| 1065 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 1066 | :param gemport_ids: list of gem port ids |
| 1067 | */ |
| 1068 | |
| 1069 | var Value []byte |
| 1070 | var err error |
| 1071 | Path := fmt.Sprintf(GEMPORT_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID) |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 1072 | logger.Debugf("Updating gemport ids for %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1073 | Value, err = json.Marshal(GEMPortIDs) |
| 1074 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 1075 | logger.Error("failed to Marshal") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1076 | return err |
| 1077 | } |
| 1078 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1079 | if err = PONRMgr.KVStore.Put(ctx, Path, Value); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 1080 | logger.Errorf("Failed to update resource %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1081 | return err |
| 1082 | } |
| 1083 | return err |
| 1084 | } |
| 1085 | |
| 1086 | func checkForFlowIDInList(FlowIDList []uint32, FlowID uint32) (bool, uint32) { |
| 1087 | /* |
| 1088 | Check for a flow id in a given list of flow IDs. |
| 1089 | :param FLowIDList: List of Flow IDs |
| 1090 | :param FlowID: Flowd to check in the list |
| 1091 | : return true and the index if present false otherwise. |
| 1092 | */ |
| 1093 | |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 1094 | for idx := range FlowIDList { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1095 | if FlowID == FlowIDList[idx] { |
| 1096 | return true, uint32(idx) |
| 1097 | } |
| 1098 | } |
| 1099 | return false, 0 |
| 1100 | } |
| 1101 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1102 | func (PONRMgr *PONResourceManager) UpdateFlowIDForOnu(ctx context.Context, IntfONUID string, FlowID uint32, Add bool) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1103 | /* |
| 1104 | Update the flow_id list of the ONU (add or remove flow_id from the list) |
| 1105 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 1106 | :param flow_id: flow ID |
| 1107 | :param add: Boolean flag to indicate whether the flow_id should be |
| 1108 | added or removed from the list. Defaults to adding the flow. |
| 1109 | */ |
| 1110 | var Value []byte |
| 1111 | var err error |
| 1112 | var RetVal bool |
| 1113 | var IDx uint32 |
| 1114 | Path := fmt.Sprintf(FLOW_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1115 | FlowIDs := PONRMgr.GetCurrentFlowIDsForOnu(ctx, IntfONUID) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1116 | |
| 1117 | if Add { |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 1118 | if RetVal, _ = checkForFlowIDInList(FlowIDs, FlowID); RetVal { |
| 1119 | return nil |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1120 | } |
| 1121 | FlowIDs = append(FlowIDs, FlowID) |
| 1122 | } else { |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 1123 | if RetVal, IDx = checkForFlowIDInList(FlowIDs, FlowID); !RetVal { |
| 1124 | return nil |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1125 | } |
| 1126 | // delete the index and shift |
| 1127 | FlowIDs = append(FlowIDs[:IDx], FlowIDs[IDx+1:]...) |
| 1128 | } |
| 1129 | Value, err = json.Marshal(FlowIDs) |
| 1130 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 1131 | logger.Error("Failed to Marshal") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1132 | return err |
| 1133 | } |
| 1134 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1135 | if err = PONRMgr.KVStore.Put(ctx, Path, Value); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 1136 | logger.Errorf("Failed to update resource %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1137 | return err |
| 1138 | } |
| 1139 | return err |
| 1140 | } |
| 1141 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1142 | func (PONRMgr *PONResourceManager) UpdateFlowIDInfoForOnu(ctx context.Context, IntfONUID string, FlowID uint32, FlowData interface{}) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1143 | /* |
| 1144 | Update any metadata associated with the flow_id. The flow_data could be json |
| 1145 | or any of other data structure. The resource manager doesnt care |
| 1146 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 1147 | :param flow_id: Flow ID |
| 1148 | :param flow_data: Flow data blob |
| 1149 | */ |
| 1150 | var Value []byte |
| 1151 | var err error |
| 1152 | Path := fmt.Sprintf(FLOW_ID_INFO_PATH, PONRMgr.DeviceID, IntfONUID, FlowID) |
| 1153 | Value, err = json.Marshal(FlowData) |
| 1154 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 1155 | logger.Error("failed to Marshal") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1156 | return err |
| 1157 | } |
| 1158 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1159 | if err = PONRMgr.KVStore.Put(ctx, Path, Value); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 1160 | logger.Errorf("Failed to update resource %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1161 | return err |
| 1162 | } |
| 1163 | return err |
| 1164 | } |
| 1165 | |
| 1166 | func (PONRMgr *PONResourceManager) GenerateNextID(Resource map[string]interface{}) (uint32, error) { |
| 1167 | /* |
| 1168 | Generate unique id having OFFSET as start |
| 1169 | :param resource: resource used to generate ID |
| 1170 | :return uint32: generated id |
| 1171 | */ |
| 1172 | ByteArray, err := ToByte(Resource[POOL]) |
| 1173 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 1174 | logger.Error("Failed to convert resource to byte array") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1175 | return 0, err |
| 1176 | } |
| 1177 | Data := bitmap.TSFromData(ByteArray, false) |
| 1178 | if Data == nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 1179 | logger.Error("Failed to get data from byte array") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1180 | return 0, errors.New("Failed to get data from byte array") |
| 1181 | } |
| 1182 | |
| 1183 | Len := Data.Len() |
| 1184 | var Idx int |
| 1185 | for Idx = 0; Idx < Len; Idx++ { |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 1186 | if !Data.Get(Idx) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1187 | break |
| 1188 | } |
| 1189 | } |
| 1190 | Data.Set(Idx, true) |
| 1191 | res := uint32(Resource[START_IDX].(float64)) |
| 1192 | Resource[POOL] = Data.Data(false) |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 1193 | logger.Debugf("Generated ID for %d", (uint32(Idx) + res)) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1194 | return (uint32(Idx) + res), err |
| 1195 | } |
| 1196 | |
| 1197 | func (PONRMgr *PONResourceManager) ReleaseID(Resource map[string]interface{}, Id uint32) bool { |
| 1198 | /* |
| 1199 | Release unique id having OFFSET as start index. |
| 1200 | :param resource: resource used to release ID |
| 1201 | :param unique_id: id need to be released |
| 1202 | */ |
| 1203 | ByteArray, err := ToByte(Resource[POOL]) |
| 1204 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 1205 | logger.Error("Failed to convert resource to byte array") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1206 | return false |
| 1207 | } |
| 1208 | Data := bitmap.TSFromData(ByteArray, false) |
| 1209 | if Data == nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 1210 | logger.Error("Failed to get resource pool") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1211 | return false |
| 1212 | } |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 1213 | Idx := Id - uint32(Resource[START_IDX].(float64)) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1214 | Data.Set(int(Idx), false) |
| 1215 | Resource[POOL] = Data.Data(false) |
| 1216 | |
| 1217 | return true |
| 1218 | } |
| 1219 | |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 1220 | /* Reserves a unique id in the specified resource pool. |
| 1221 | :param Resource: resource used to reserve ID |
| 1222 | :param Id: ID to be reserved |
| 1223 | */ |
| 1224 | func (PONRMgr *PONResourceManager) reserveID(TSData *bitmap.Threadsafe, StartIndex uint32, Id uint32) bool { |
| 1225 | Data := bitmap.TSFromData(TSData.Data(false), false) |
| 1226 | if Data == nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 1227 | logger.Error("Failed to get resource pool") |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 1228 | return false |
| 1229 | } |
| 1230 | Idx := Id - StartIndex |
| 1231 | Data.Set(int(Idx), true) |
| 1232 | return true |
| 1233 | } |
| 1234 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1235 | func (PONRMgr *PONResourceManager) GetTechnology() string { |
| 1236 | return PONRMgr.Technology |
| 1237 | } |
| 1238 | |
| 1239 | func (PONRMgr *PONResourceManager) GetResourceTypeAllocID() string { |
| 1240 | return ALLOC_ID |
| 1241 | } |
| 1242 | |
| 1243 | func (PONRMgr *PONResourceManager) GetResourceTypeGemPortID() string { |
| 1244 | return GEMPORT_ID |
| 1245 | } |
| 1246 | |
| 1247 | // ToByte converts an interface value to a []byte. The interface should either be of |
| 1248 | // a string type or []byte. Otherwise, an error is returned. |
| 1249 | func ToByte(value interface{}) ([]byte, error) { |
| 1250 | switch t := value.(type) { |
| 1251 | case []byte: |
| 1252 | return value.([]byte), nil |
| 1253 | case string: |
| 1254 | return []byte(value.(string)), nil |
| 1255 | default: |
| 1256 | return nil, fmt.Errorf("unexpected-type-%T", t) |
| 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | // ToString converts an interface value to a string. The interface should either be of |
| 1261 | // a string type or []byte. Otherwise, an error is returned. |
| 1262 | func ToString(value interface{}) (string, error) { |
| 1263 | switch t := value.(type) { |
| 1264 | case []byte: |
| 1265 | return string(value.([]byte)), nil |
| 1266 | case string: |
| 1267 | return value.(string), nil |
| 1268 | default: |
| 1269 | return "", fmt.Errorf("unexpected-type-%T", t) |
| 1270 | } |
| 1271 | } |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1272 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1273 | func (PONRMgr *PONResourceManager) AddOnuGemInfo(ctx context.Context, intfID uint32, onuGemData interface{}) error { |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1274 | /* |
| 1275 | Update onugem info map, |
| 1276 | :param pon_intf_id: reference of PON interface id |
| 1277 | :param onuegmdata: onugem info map |
| 1278 | */ |
| 1279 | var Value []byte |
| 1280 | var err error |
| 1281 | Path := fmt.Sprintf(ONU_GEM_INFO_PATH, PONRMgr.DeviceID, intfID) |
| 1282 | Value, err = json.Marshal(onuGemData) |
| 1283 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 1284 | logger.Error("failed to Marshal") |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1285 | return err |
| 1286 | } |
| 1287 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1288 | if err = PONRMgr.KVStore.Put(ctx, Path, Value); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 1289 | logger.Errorf("Failed to update resource %s", Path) |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1290 | return err |
| 1291 | } |
| 1292 | return err |
| 1293 | } |
| 1294 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1295 | func (PONRMgr *PONResourceManager) GetOnuGemInfo(ctx context.Context, IntfId uint32, onuGemInfo interface{}) error { |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1296 | /* |
| 1297 | Get onugeminfo map from kvstore |
| 1298 | :param intfid: refremce pon intfid |
| 1299 | :param onuGemInfo: onugem info to return from kv strore. |
| 1300 | */ |
| 1301 | var Val []byte |
| 1302 | |
| 1303 | path := fmt.Sprintf(ONU_GEM_INFO_PATH, PONRMgr.DeviceID, IntfId) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1304 | value, err := PONRMgr.KVStore.Get(ctx, path) |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1305 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 1306 | logger.Errorw("Failed to get from kv store", log.Fields{"path": path}) |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1307 | return err |
| 1308 | } else if value == nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 1309 | logger.Debug("No onuinfo for path", log.Fields{"path": path}) |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1310 | return nil // returning nil as this could happen if there are no onus for the interface yet |
| 1311 | } |
| 1312 | if Val, err = kvstore.ToByte(value.Value); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 1313 | logger.Error("Failed to convert to byte array") |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1314 | return err |
| 1315 | } |
| 1316 | |
| 1317 | if err = json.Unmarshal(Val, &onuGemInfo); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 1318 | logger.Error("Failed to unmarshall") |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1319 | return err |
| 1320 | } |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 1321 | logger.Debugw("found onuinfo from path", log.Fields{"path": path, "onuinfo": onuGemInfo}) |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1322 | return err |
| 1323 | } |
| 1324 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1325 | func (PONRMgr *PONResourceManager) DelOnuGemInfoForIntf(ctx context.Context, intfId uint32) error { |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1326 | /* |
| 1327 | delete onugem info for an interface from kvstore |
| 1328 | :param intfid: refremce pon intfid |
| 1329 | */ |
| 1330 | |
| 1331 | path := fmt.Sprintf(ONU_GEM_INFO_PATH, PONRMgr.DeviceID, intfId) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1332 | if err := PONRMgr.KVStore.Delete(ctx, path); err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 1333 | logger.Errorf("Falied to remove resource %s", path) |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1334 | return err |
| 1335 | } |
| 1336 | return nil |
| 1337 | } |