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" |
Neha Sharma | 130ac6d | 2020-04-08 08:46:32 +0000 | [diff] [blame] | 25 | "time" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 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 | |
Neha Sharma | 130ac6d | 2020-04-08 08:46:32 +0000 | [diff] [blame] | 127 | KVSTORE_RETRY_TIMEOUT = 5 * time.Second |
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 |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 142 | Address string // address of the KV store |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 143 | OLTModel string |
| 144 | KVStore *db.Backend |
| 145 | KVStoreForConfig *db.Backend |
| 146 | TechProfileMgr tp.TechProfileIf // create object of *tp.TechProfileMgr |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 147 | |
| 148 | // Below attribute, pon_resource_ranges, should be initialized |
| 149 | // by reading from KV store. |
| 150 | PonResourceRanges map[string]interface{} |
| 151 | SharedResourceMgrs map[string]*PONResourceManager |
| 152 | SharedIdxByType map[string]string |
| 153 | IntfIDs []uint32 // list of pon interface IDs |
| 154 | Globalorlocal string |
| 155 | } |
| 156 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 157 | func newKVClient(storeType string, address string, timeout time.Duration) (kvstore.Client, error) { |
| 158 | logger.Infow("kv-store-type", log.Fields{"store": storeType}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 159 | switch storeType { |
| 160 | case "consul": |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 161 | return kvstore.NewConsulClient(address, timeout) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 162 | case "etcd": |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 163 | return kvstore.NewEtcdClient(address, timeout, log.WarnLevel) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 164 | } |
| 165 | return nil, errors.New("unsupported-kv-store") |
| 166 | } |
| 167 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 168 | func SetKVClient(Technology string, Backend string, Addr string, configClient bool) *db.Backend { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 169 | // TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some |
| 170 | // issue between kv store and backend , core is not calling NewBackend directly |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 171 | kvClient, err := newKVClient(Backend, Addr, KVSTORE_RETRY_TIMEOUT) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 172 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 173 | logger.Fatalw("Failed to init KV client\n", log.Fields{"err": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 174 | return nil |
| 175 | } |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 176 | |
| 177 | var pathPrefix string |
| 178 | if configClient { |
| 179 | pathPrefix = PATH_PREFIX_FOR_CONFIG |
| 180 | } else { |
| 181 | pathPrefix = fmt.Sprintf(PATH_PREFIX, Technology) |
| 182 | } |
| 183 | |
sbarbari | 1e3e29c | 2019-11-05 10:06:50 -0500 | [diff] [blame] | 184 | kvbackend := &db.Backend{ |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 185 | Client: kvClient, |
| 186 | StoreType: Backend, |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 187 | Address: Addr, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 188 | Timeout: KVSTORE_RETRY_TIMEOUT, |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 189 | PathPrefix: pathPrefix} |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 190 | |
| 191 | return kvbackend |
| 192 | } |
| 193 | |
| 194 | // NewPONResourceManager creates a new PON resource manager. |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 195 | func NewPONResourceManager(Technology string, DeviceType string, DeviceID string, Backend string, Address string) (*PONResourceManager, error) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 196 | var PONMgr PONResourceManager |
| 197 | PONMgr.Technology = Technology |
| 198 | PONMgr.DeviceType = DeviceType |
| 199 | PONMgr.DeviceID = DeviceID |
| 200 | PONMgr.Backend = Backend |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 201 | PONMgr.Address = Address |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 202 | PONMgr.KVStore = SetKVClient(Technology, Backend, Address, false) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 203 | if PONMgr.KVStore == nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 204 | logger.Error("KV Client initilization failed") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 205 | return nil, errors.New("Failed to init KV client") |
| 206 | } |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 207 | // init kv client to read from the config path |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 208 | PONMgr.KVStoreForConfig = SetKVClient(Technology, Backend, Address, true) |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 209 | if PONMgr.KVStoreForConfig == nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 210 | logger.Error("KV Config Client initilization failed") |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 211 | return nil, errors.New("Failed to init KV Config client") |
| 212 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 213 | // Initialize techprofile for this technology |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 214 | if PONMgr.TechProfileMgr, _ = tp.NewTechProfile(&PONMgr, Backend, Address); PONMgr.TechProfileMgr == nil { |
| 215 | logger.Error("Techprofile initialization failed") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 216 | return nil, errors.New("Failed to init tech profile") |
| 217 | } |
| 218 | PONMgr.PonResourceRanges = make(map[string]interface{}) |
| 219 | PONMgr.SharedResourceMgrs = make(map[string]*PONResourceManager) |
| 220 | PONMgr.SharedIdxByType = make(map[string]string) |
| 221 | PONMgr.SharedIdxByType[ONU_ID] = ONU_ID_SHARED_IDX |
| 222 | PONMgr.SharedIdxByType[ALLOC_ID] = ALLOC_ID_SHARED_IDX |
| 223 | PONMgr.SharedIdxByType[GEMPORT_ID] = GEMPORT_ID_SHARED_IDX |
| 224 | PONMgr.SharedIdxByType[FLOW_ID] = FLOW_ID_SHARED_IDX |
| 225 | PONMgr.IntfIDs = make([]uint32, NUM_OF_PON_INTF) |
| 226 | PONMgr.OLTModel = DeviceType |
| 227 | return &PONMgr, nil |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | Initialize PON resource ranges with config fetched from kv store. |
| 232 | return boolean: True if PON resource ranges initialized else false |
| 233 | Try to initialize the PON Resource Ranges from KV store based on the |
| 234 | OLT model key, if available |
| 235 | */ |
| 236 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 237 | func (PONRMgr *PONResourceManager) InitResourceRangesFromKVStore(ctx context.Context) bool { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 238 | //Initialize PON resource ranges with config fetched from kv store. |
| 239 | //:return boolean: True if PON resource ranges initialized else false |
| 240 | // Try to initialize the PON Resource Ranges from KV store based on the |
| 241 | // OLT model key, if available |
| 242 | if PONRMgr.OLTModel == "" { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 243 | logger.Error("Failed to get OLT model") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 244 | return false |
| 245 | } |
| 246 | Path := fmt.Sprintf(PON_RESOURCE_RANGE_CONFIG_PATH, PONRMgr.OLTModel) |
| 247 | //get resource from kv store |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 248 | Result, err := PONRMgr.KVStore.Get(ctx, Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 249 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 250 | logger.Debugf("Error in fetching resource %s from KV strore", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 251 | return false |
| 252 | } |
| 253 | if Result == nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 254 | 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] | 255 | return false |
| 256 | } |
| 257 | //update internal ranges from kv ranges. If there are missing |
| 258 | // values in the KV profile, continue to use the defaults |
| 259 | Value, err := ToByte(Result.Value) |
| 260 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 261 | logger.Error("Failed to convert kvpair to byte string") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 262 | return false |
| 263 | } |
| 264 | if err := json.Unmarshal(Value, &PONRMgr.PonResourceRanges); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 265 | logger.Error("Failed to Unmarshal json byte") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 266 | return false |
| 267 | } |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 268 | logger.Debug("Init resource ranges from kvstore success") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 269 | return true |
| 270 | } |
| 271 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 272 | func (PONRMgr *PONResourceManager) UpdateRanges(StartIDx string, StartID uint32, EndIDx string, EndID uint32, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 273 | SharedIDx string, SharedPoolID uint32, RMgr *PONResourceManager) { |
| 274 | /* |
| 275 | Update the ranges for all reosurce type in the intermnal maps |
| 276 | param: resource type start index |
| 277 | param: start ID |
| 278 | param: resource type end index |
| 279 | param: end ID |
| 280 | param: resource type shared index |
| 281 | param: shared pool id |
| 282 | param: global resource manager |
| 283 | */ |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 284 | logger.Debugf("update ranges for %s, %d", StartIDx, StartID) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 285 | |
| 286 | if StartID != 0 { |
| 287 | if (PONRMgr.PonResourceRanges[StartIDx] == nil) || (PONRMgr.PonResourceRanges[StartIDx].(uint32) < StartID) { |
| 288 | PONRMgr.PonResourceRanges[StartIDx] = StartID |
| 289 | } |
| 290 | } |
| 291 | if EndID != 0 { |
| 292 | if (PONRMgr.PonResourceRanges[EndIDx] == nil) || (PONRMgr.PonResourceRanges[EndIDx].(uint32) > EndID) { |
| 293 | PONRMgr.PonResourceRanges[EndIDx] = EndID |
| 294 | } |
| 295 | } |
| 296 | //if SharedPoolID != 0 { |
| 297 | PONRMgr.PonResourceRanges[SharedIDx] = SharedPoolID |
| 298 | //} |
| 299 | if RMgr != nil { |
| 300 | PONRMgr.SharedResourceMgrs[SharedIDx] = RMgr |
| 301 | } |
| 302 | } |
| 303 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 304 | func (PONRMgr *PONResourceManager) InitDefaultPONResourceRanges(ONUIDStart uint32, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 305 | ONUIDEnd uint32, |
| 306 | ONUIDSharedPoolID uint32, |
| 307 | AllocIDStart uint32, |
| 308 | AllocIDEnd uint32, |
| 309 | AllocIDSharedPoolID uint32, |
| 310 | GEMPortIDStart uint32, |
| 311 | GEMPortIDEnd uint32, |
| 312 | GEMPortIDSharedPoolID uint32, |
| 313 | FlowIDStart uint32, |
| 314 | FlowIDEnd uint32, |
| 315 | FlowIDSharedPoolID uint32, |
| 316 | UNIIDStart uint32, |
| 317 | UNIIDEnd uint32, |
| 318 | NoOfPONPorts uint32, |
| 319 | IntfIDs []uint32) bool { |
| 320 | |
| 321 | /*Initialize default PON resource ranges |
| 322 | |
| 323 | :param onu_id_start_idx: onu id start index |
| 324 | :param onu_id_end_idx: onu id end index |
| 325 | :param onu_id_shared_pool_id: pool idx for id shared by all intfs or None for no sharing |
| 326 | :param alloc_id_start_idx: alloc id start index |
| 327 | :param alloc_id_end_idx: alloc id end index |
| 328 | :param alloc_id_shared_pool_id: pool idx for alloc id shared by all intfs or None for no sharing |
| 329 | :param gemport_id_start_idx: gemport id start index |
| 330 | :param gemport_id_end_idx: gemport id end index |
| 331 | :param gemport_id_shared_pool_id: pool idx for gemport id shared by all intfs or None for no sharing |
| 332 | :param flow_id_start_idx: flow id start index |
| 333 | :param flow_id_end_idx: flow id end index |
| 334 | :param flow_id_shared_pool_id: pool idx for flow id shared by all intfs or None for no sharing |
| 335 | :param num_of_pon_ports: number of PON ports |
| 336 | :param intf_ids: interfaces serviced by this manager |
| 337 | */ |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 338 | PONRMgr.UpdateRanges(ONU_ID_START_IDX, ONUIDStart, ONU_ID_END_IDX, ONUIDEnd, ONU_ID_SHARED_IDX, ONUIDSharedPoolID, nil) |
| 339 | PONRMgr.UpdateRanges(ALLOC_ID_START_IDX, AllocIDStart, ALLOC_ID_END_IDX, AllocIDEnd, ALLOC_ID_SHARED_IDX, AllocIDSharedPoolID, nil) |
| 340 | PONRMgr.UpdateRanges(GEMPORT_ID_START_IDX, GEMPortIDStart, GEMPORT_ID_END_IDX, GEMPortIDEnd, GEMPORT_ID_SHARED_IDX, GEMPortIDSharedPoolID, nil) |
| 341 | PONRMgr.UpdateRanges(FLOW_ID_START_IDX, FlowIDStart, FLOW_ID_END_IDX, FlowIDEnd, FLOW_ID_SHARED_IDX, FlowIDSharedPoolID, nil) |
| 342 | PONRMgr.UpdateRanges(UNI_ID_START_IDX, UNIIDStart, UNI_ID_END_IDX, UNIIDEnd, "", 0, nil) |
| 343 | logger.Debug("Initialize default range values") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 344 | var i uint32 |
| 345 | if IntfIDs == nil { |
| 346 | for i = 0; i < NoOfPONPorts; i++ { |
| 347 | PONRMgr.IntfIDs = append(PONRMgr.IntfIDs, i) |
| 348 | } |
| 349 | } else { |
| 350 | PONRMgr.IntfIDs = IntfIDs |
| 351 | } |
| 352 | return true |
| 353 | } |
| 354 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 355 | func (PONRMgr *PONResourceManager) InitDeviceResourcePool(ctx context.Context) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 356 | |
| 357 | //Initialize resource pool for all PON ports. |
| 358 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 359 | logger.Debug("Init resource ranges") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 360 | |
| 361 | var err error |
| 362 | for _, Intf := range PONRMgr.IntfIDs { |
| 363 | SharedPoolID := PONRMgr.PonResourceRanges[ONU_ID_SHARED_IDX].(uint32) |
| 364 | if SharedPoolID != 0 { |
| 365 | Intf = SharedPoolID |
| 366 | } |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 367 | if err = PONRMgr.InitResourceIDPool(ctx, Intf, ONU_ID, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 368 | PONRMgr.PonResourceRanges[ONU_ID_START_IDX].(uint32), |
| 369 | PONRMgr.PonResourceRanges[ONU_ID_END_IDX].(uint32)); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 370 | logger.Error("Failed to init ONU ID resource pool") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 371 | return err |
| 372 | } |
| 373 | if SharedPoolID != 0 { |
| 374 | break |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | for _, Intf := range PONRMgr.IntfIDs { |
| 379 | SharedPoolID := PONRMgr.PonResourceRanges[ALLOC_ID_SHARED_IDX].(uint32) |
| 380 | if SharedPoolID != 0 { |
| 381 | Intf = SharedPoolID |
| 382 | } |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 383 | if err = PONRMgr.InitResourceIDPool(ctx, Intf, ALLOC_ID, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 384 | PONRMgr.PonResourceRanges[ALLOC_ID_START_IDX].(uint32), |
| 385 | PONRMgr.PonResourceRanges[ALLOC_ID_END_IDX].(uint32)); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 386 | logger.Error("Failed to init ALLOC ID resource pool ") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 387 | return err |
| 388 | } |
| 389 | if SharedPoolID != 0 { |
| 390 | break |
| 391 | } |
| 392 | } |
| 393 | for _, Intf := range PONRMgr.IntfIDs { |
| 394 | SharedPoolID := PONRMgr.PonResourceRanges[GEMPORT_ID_SHARED_IDX].(uint32) |
| 395 | if SharedPoolID != 0 { |
| 396 | Intf = SharedPoolID |
| 397 | } |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 398 | if err = PONRMgr.InitResourceIDPool(ctx, Intf, GEMPORT_ID, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 399 | PONRMgr.PonResourceRanges[GEMPORT_ID_START_IDX].(uint32), |
| 400 | PONRMgr.PonResourceRanges[GEMPORT_ID_END_IDX].(uint32)); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 401 | logger.Error("Failed to init GEMPORT ID resource pool") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 402 | return err |
| 403 | } |
| 404 | if SharedPoolID != 0 { |
| 405 | break |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | for _, Intf := range PONRMgr.IntfIDs { |
| 410 | SharedPoolID := PONRMgr.PonResourceRanges[FLOW_ID_SHARED_IDX].(uint32) |
| 411 | if SharedPoolID != 0 { |
| 412 | Intf = SharedPoolID |
| 413 | } |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 414 | if err = PONRMgr.InitResourceIDPool(ctx, Intf, FLOW_ID, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 415 | PONRMgr.PonResourceRanges[FLOW_ID_START_IDX].(uint32), |
| 416 | PONRMgr.PonResourceRanges[FLOW_ID_END_IDX].(uint32)); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 417 | logger.Error("Failed to init FLOW ID resource pool") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 418 | return err |
| 419 | } |
| 420 | if SharedPoolID != 0 { |
| 421 | break |
| 422 | } |
| 423 | } |
| 424 | return err |
| 425 | } |
| 426 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 427 | func (PONRMgr *PONResourceManager) ClearDeviceResourcePool(ctx context.Context) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 428 | |
| 429 | //Clear resource pool for all PON ports. |
| 430 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 431 | logger.Debug("Clear resource ranges") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 432 | |
| 433 | for _, Intf := range PONRMgr.IntfIDs { |
| 434 | SharedPoolID := PONRMgr.PonResourceRanges[ONU_ID_SHARED_IDX].(uint32) |
| 435 | if SharedPoolID != 0 { |
| 436 | Intf = SharedPoolID |
| 437 | } |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 438 | if status := PONRMgr.ClearResourceIDPool(ctx, Intf, ONU_ID); !status { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 439 | logger.Error("Failed to clear ONU ID resource pool") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 440 | return errors.New("Failed to clear ONU ID resource pool") |
| 441 | } |
| 442 | if SharedPoolID != 0 { |
| 443 | break |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | for _, Intf := range PONRMgr.IntfIDs { |
| 448 | SharedPoolID := PONRMgr.PonResourceRanges[ALLOC_ID_SHARED_IDX].(uint32) |
| 449 | if SharedPoolID != 0 { |
| 450 | Intf = SharedPoolID |
| 451 | } |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 452 | if status := PONRMgr.ClearResourceIDPool(ctx, Intf, ALLOC_ID); !status { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 453 | logger.Error("Failed to clear ALLOC ID resource pool ") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 454 | return errors.New("Failed to clear ALLOC ID resource pool") |
| 455 | } |
| 456 | if SharedPoolID != 0 { |
| 457 | break |
| 458 | } |
| 459 | } |
| 460 | for _, Intf := range PONRMgr.IntfIDs { |
| 461 | SharedPoolID := PONRMgr.PonResourceRanges[GEMPORT_ID_SHARED_IDX].(uint32) |
| 462 | if SharedPoolID != 0 { |
| 463 | Intf = SharedPoolID |
| 464 | } |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 465 | if status := PONRMgr.ClearResourceIDPool(ctx, Intf, GEMPORT_ID); !status { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 466 | logger.Error("Failed to clear GEMPORT ID resource pool") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 467 | return errors.New("Failed to clear GEMPORT ID resource pool") |
| 468 | } |
| 469 | if SharedPoolID != 0 { |
| 470 | break |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | for _, Intf := range PONRMgr.IntfIDs { |
| 475 | SharedPoolID := PONRMgr.PonResourceRanges[FLOW_ID_SHARED_IDX].(uint32) |
| 476 | if SharedPoolID != 0 { |
| 477 | Intf = SharedPoolID |
| 478 | } |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 479 | if status := PONRMgr.ClearResourceIDPool(ctx, Intf, FLOW_ID); !status { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 480 | logger.Error("Failed to clear FLOW ID resource pool") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 481 | return errors.New("Failed to clear FLOW ID resource pool") |
| 482 | } |
| 483 | if SharedPoolID != 0 { |
| 484 | break |
| 485 | } |
| 486 | } |
| 487 | return nil |
| 488 | } |
| 489 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 490 | 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] | 491 | |
| 492 | /*Initialize Resource ID pool for a given Resource Type on a given PON Port |
| 493 | |
| 494 | :param pon_intf_id: OLT PON interface id |
| 495 | :param resource_type: String to identify type of resource |
| 496 | :param start_idx: start index for onu id pool |
| 497 | :param end_idx: end index for onu id pool |
| 498 | :return boolean: True if resource id pool initialized else false |
| 499 | */ |
| 500 | |
| 501 | // delegate to the master instance if sharing enabled across instances |
| 502 | SharedResourceMgr := PONRMgr.SharedResourceMgrs[PONRMgr.SharedIdxByType[ResourceType]] |
| 503 | if SharedResourceMgr != nil && PONRMgr != SharedResourceMgr { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 504 | return SharedResourceMgr.InitResourceIDPool(ctx, Intf, ResourceType, StartID, EndID) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 507 | Path := PONRMgr.GetPath(Intf, ResourceType) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 508 | if Path == "" { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 509 | logger.Errorf("Failed to get path for resource type %s", ResourceType) |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 510 | return fmt.Errorf("Failed to get path for resource type %s", ResourceType) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | //In case of adapter reboot and reconciliation resource in kv store |
| 514 | //checked for its presence if not kv store update happens |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 515 | Res, err := PONRMgr.GetResource(ctx, Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 516 | if (err == nil) && (Res != nil) { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 517 | logger.Debugf("Resource %s already present in store ", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 518 | return nil |
| 519 | } else { |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 520 | var excluded []uint32 |
| 521 | if ResourceType == GEMPORT_ID { |
| 522 | //get gem port ids defined in the KV store, if any, and exclude them from the gem port id pool |
| 523 | if reservedGemPortIds, defined := PONRMgr.getReservedGemPortIdsFromKVStore(ctx); defined { |
| 524 | excluded = reservedGemPortIds |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 525 | 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] | 526 | } |
| 527 | } |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 528 | FormatResult, err := PONRMgr.FormatResource(Intf, StartID, EndID, excluded) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 529 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 530 | logger.Errorf("Failed to format resource") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 531 | return err |
| 532 | } |
| 533 | // Add resource as json in kv store. |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 534 | err = PONRMgr.KVStore.Put(ctx, Path, FormatResult) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 535 | if err == nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 536 | logger.Debug("Successfuly posted to kv store") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 537 | return err |
| 538 | } |
| 539 | } |
| 540 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 541 | logger.Debug("Error initializing pool") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 542 | |
| 543 | return err |
| 544 | } |
| 545 | |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 546 | func (PONRMgr *PONResourceManager) getReservedGemPortIdsFromKVStore(ctx context.Context) ([]uint32, bool) { |
| 547 | var reservedGemPortIds []uint32 |
| 548 | // read reserved gem ports from the config path |
| 549 | KvPair, err := PONRMgr.KVStoreForConfig.Get(ctx, RESERVED_GEMPORT_IDS_PATH) |
| 550 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 551 | 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] | 552 | return reservedGemPortIds, false |
| 553 | } |
| 554 | if KvPair == nil || KvPair.Value == nil { |
| 555 | //no reserved gem port defined in the store |
| 556 | return reservedGemPortIds, false |
| 557 | } |
| 558 | Val, err := kvstore.ToByte(KvPair.Value) |
| 559 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 560 | 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] | 561 | return reservedGemPortIds, false |
| 562 | } |
| 563 | if err = json.Unmarshal(Val, &reservedGemPortIds); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 564 | logger.Errorw("Failed to unmarshal reservedGemPortIds", log.Fields{"err": err}) |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 565 | return reservedGemPortIds, false |
| 566 | } |
| 567 | return reservedGemPortIds, true |
| 568 | } |
| 569 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 570 | func (PONRMgr *PONResourceManager) FormatResource(IntfID uint32, StartIDx uint32, EndIDx uint32, |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 571 | Excluded []uint32) ([]byte, error) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 572 | /* |
| 573 | Format resource as json. |
| 574 | :param pon_intf_id: OLT PON interface id |
| 575 | :param start_idx: start index for id pool |
| 576 | :param end_idx: end index for id pool |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 577 | :Id values to be Excluded from the pool |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 578 | :return dictionary: resource formatted as map |
| 579 | */ |
| 580 | // Format resource as json to be stored in backend store |
| 581 | Resource := make(map[string]interface{}) |
| 582 | Resource[PON_INTF_ID] = IntfID |
| 583 | Resource[START_IDX] = StartIDx |
| 584 | Resource[END_IDX] = EndIDx |
| 585 | /* |
| 586 | Resource pool stored in backend store as binary string. |
| 587 | Tracking the resource allocation will be done by setting the bits \ |
| 588 | in the byte array. The index set will be the resource number allocated. |
| 589 | */ |
| 590 | var TSData *bitmap.Threadsafe |
| 591 | if TSData = bitmap.NewTS(int(EndIDx)); TSData == nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 592 | logger.Error("Failed to create a bitmap") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 593 | return nil, errors.New("Failed to create bitmap") |
| 594 | } |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 595 | for _, excludedID := range Excluded { |
| 596 | if excludedID < StartIDx || excludedID > EndIDx { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 597 | 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] | 598 | StartIDx, EndIDx) |
| 599 | continue |
| 600 | } |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 601 | PONRMgr.reserveID(TSData, StartIDx, excludedID) |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 602 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 603 | Resource[POOL] = TSData.Data(false) //we pass false so as the TSData lib api does not do a copy of the data and return |
| 604 | |
| 605 | Value, err := json.Marshal(Resource) |
| 606 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 607 | logger.Errorf("Failed to marshall resource") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 608 | return nil, err |
| 609 | } |
| 610 | return Value, err |
| 611 | } |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 612 | 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] | 613 | /* |
| 614 | Get resource from kv store. |
| 615 | |
| 616 | :param path: path to get resource |
| 617 | :return: resource if resource present in kv store else None |
| 618 | */ |
| 619 | //get resource from kv store |
| 620 | |
| 621 | var Value []byte |
| 622 | Result := make(map[string]interface{}) |
| 623 | var Str string |
| 624 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 625 | Resource, err := PONRMgr.KVStore.Get(ctx, Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 626 | if (err != nil) || (Resource == nil) { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 627 | logger.Debugf("Resource unavailable at %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 628 | return nil, err |
| 629 | } |
| 630 | |
| 631 | Value, err = ToByte(Resource.Value) |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 632 | if err != nil { |
| 633 | return nil, err |
| 634 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 635 | |
| 636 | // decode resource fetched from backend store to dictionary |
| 637 | err = json.Unmarshal(Value, &Result) |
| 638 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 639 | logger.Error("Failed to decode resource") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 640 | return Result, err |
| 641 | } |
| 642 | /* |
| 643 | resource pool in backend store stored as binary string whereas to |
| 644 | access the pool to generate/release IDs it need to be converted |
| 645 | as BitArray |
| 646 | */ |
| 647 | Str, err = ToString(Result[POOL]) |
| 648 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 649 | logger.Error("Failed to conver to kv pair to string") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 650 | return Result, err |
| 651 | } |
| 652 | Decode64, _ := base64.StdEncoding.DecodeString(Str) |
| 653 | Result[POOL], err = ToByte(Decode64) |
| 654 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 655 | logger.Error("Failed to convert resource pool to byte") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 656 | return Result, err |
| 657 | } |
| 658 | |
| 659 | return Result, err |
| 660 | } |
| 661 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 662 | func (PONRMgr *PONResourceManager) GetPath(IntfID uint32, ResourceType string) string { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 663 | /* |
| 664 | Get path for given resource type. |
| 665 | :param pon_intf_id: OLT PON interface id |
| 666 | :param resource_type: String to identify type of resource |
| 667 | :return: path for given resource type |
| 668 | */ |
| 669 | |
| 670 | /* |
| 671 | Get the shared pool for the given resource type. |
| 672 | all the resource ranges and the shared resource maps are initialized during the init. |
| 673 | */ |
| 674 | SharedPoolID := PONRMgr.PonResourceRanges[PONRMgr.SharedIdxByType[ResourceType]].(uint32) |
| 675 | if SharedPoolID != 0 { |
| 676 | IntfID = SharedPoolID |
| 677 | } |
| 678 | var Path string |
| 679 | if ResourceType == ONU_ID { |
| 680 | Path = fmt.Sprintf(ONU_ID_POOL_PATH, PONRMgr.DeviceID, IntfID) |
| 681 | } else if ResourceType == ALLOC_ID { |
| 682 | Path = fmt.Sprintf(ALLOC_ID_POOL_PATH, PONRMgr.DeviceID, IntfID) |
| 683 | } else if ResourceType == GEMPORT_ID { |
| 684 | Path = fmt.Sprintf(GEMPORT_ID_POOL_PATH, PONRMgr.DeviceID, IntfID) |
| 685 | } else if ResourceType == FLOW_ID { |
| 686 | Path = fmt.Sprintf(FLOW_ID_POOL_PATH, PONRMgr.DeviceID, IntfID) |
| 687 | } else { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 688 | logger.Error("Invalid resource pool identifier") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 689 | } |
| 690 | return Path |
| 691 | } |
| 692 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 693 | 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] | 694 | /* |
| 695 | Create alloc/gemport/onu/flow id for given OLT PON interface. |
| 696 | :param pon_intf_id: OLT PON interface id |
| 697 | :param resource_type: String to identify type of resource |
| 698 | :param num_of_id: required number of ids |
| 699 | :return list/uint32/None: list, uint32 or None if resource type is |
| 700 | alloc_id/gemport_id, onu_id or invalid type respectively |
| 701 | */ |
| 702 | if NumIDs < 1 { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 703 | logger.Error("Invalid number of resources requested") |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 704 | return nil, fmt.Errorf("Invalid number of resources requested %d", NumIDs) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 705 | } |
| 706 | // delegate to the master instance if sharing enabled across instances |
| 707 | |
| 708 | SharedResourceMgr := PONRMgr.SharedResourceMgrs[PONRMgr.SharedIdxByType[ResourceType]] |
| 709 | if SharedResourceMgr != nil && PONRMgr != SharedResourceMgr { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 710 | return SharedResourceMgr.GetResourceID(ctx, IntfID, ResourceType, NumIDs) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 711 | } |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 712 | 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] | 713 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 714 | Path := PONRMgr.GetPath(IntfID, ResourceType) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 715 | if Path == "" { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 716 | logger.Errorf("Failed to get path for resource type %s", ResourceType) |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 717 | 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] | 718 | } |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 719 | logger.Debugf("Get resource for type %s on path %s", ResourceType, Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 720 | var Result []uint32 |
| 721 | var NextID uint32 |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 722 | Resource, err := PONRMgr.GetResource(ctx, Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 723 | if (err == nil) && (ResourceType == ONU_ID) || (ResourceType == FLOW_ID) { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 724 | if NextID, err = PONRMgr.GenerateNextID(Resource); err != nil { |
| 725 | logger.Error("Failed to Generate ID") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 726 | return Result, err |
| 727 | } |
| 728 | Result = append(Result, NextID) |
| 729 | } else if (err == nil) && ((ResourceType == GEMPORT_ID) || (ResourceType == ALLOC_ID)) { |
| 730 | if NumIDs == 1 { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 731 | if NextID, err = PONRMgr.GenerateNextID(Resource); err != nil { |
| 732 | logger.Error("Failed to Generate ID") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 733 | return Result, err |
| 734 | } |
| 735 | Result = append(Result, NextID) |
| 736 | } else { |
| 737 | for NumIDs > 0 { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 738 | if NextID, err = PONRMgr.GenerateNextID(Resource); err != nil { |
| 739 | logger.Error("Failed to Generate ID") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 740 | return Result, err |
| 741 | } |
| 742 | Result = append(Result, NextID) |
| 743 | NumIDs-- |
| 744 | } |
| 745 | } |
| 746 | } else { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 747 | logger.Error("get resource failed") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 748 | return Result, err |
| 749 | } |
| 750 | |
| 751 | //Update resource in kv store |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 752 | if PONRMgr.UpdateResource(ctx, Path, Resource) != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 753 | logger.Errorf("Failed to update resource %s", Path) |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 754 | return nil, fmt.Errorf("Failed to update resource %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 755 | } |
| 756 | return Result, nil |
| 757 | } |
| 758 | |
| 759 | func checkValidResourceType(ResourceType string) bool { |
| 760 | KnownResourceTypes := []string{ONU_ID, ALLOC_ID, GEMPORT_ID, FLOW_ID} |
| 761 | |
| 762 | for _, v := range KnownResourceTypes { |
| 763 | if v == ResourceType { |
| 764 | return true |
| 765 | } |
| 766 | } |
| 767 | return false |
| 768 | } |
| 769 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 770 | 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] | 771 | /* |
| 772 | Release alloc/gemport/onu/flow id for given OLT PON interface. |
| 773 | :param pon_intf_id: OLT PON interface id |
| 774 | :param resource_type: String to identify type of resource |
| 775 | :param release_content: required number of ids |
| 776 | :return boolean: True if all IDs in given release_content release else False |
| 777 | */ |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 778 | if !checkValidResourceType(ResourceType) { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 779 | logger.Error("Invalid resource type") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 780 | return false |
| 781 | } |
| 782 | if ReleaseContent == nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 783 | logger.Debug("Nothing to release") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 784 | return true |
| 785 | } |
| 786 | // delegate to the master instance if sharing enabled across instances |
| 787 | SharedResourceMgr := PONRMgr.SharedResourceMgrs[PONRMgr.SharedIdxByType[ResourceType]] |
| 788 | if SharedResourceMgr != nil && PONRMgr != SharedResourceMgr { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 789 | return SharedResourceMgr.FreeResourceID(ctx, IntfID, ResourceType, ReleaseContent) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 790 | } |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 791 | Path := PONRMgr.GetPath(IntfID, ResourceType) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 792 | if Path == "" { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 793 | logger.Error("Failed to get path") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 794 | return false |
| 795 | } |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 796 | Resource, err := PONRMgr.GetResource(ctx, Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 797 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 798 | logger.Error("Failed to get resource") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 799 | return false |
| 800 | } |
| 801 | for _, Val := range ReleaseContent { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 802 | PONRMgr.ReleaseID(Resource, Val) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 803 | } |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 804 | if PONRMgr.UpdateResource(ctx, Path, Resource) != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 805 | logger.Errorf("Free resource for %s failed", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 806 | return false |
| 807 | } |
| 808 | return true |
| 809 | } |
| 810 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 811 | 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] | 812 | /* |
| 813 | Update resource in resource kv store. |
| 814 | :param path: path to update resource |
| 815 | :param resource: resource need to be updated |
| 816 | :return boolean: True if resource updated in kv store else False |
| 817 | */ |
| 818 | // TODO resource[POOL] = resource[POOL].bin |
| 819 | Value, err := json.Marshal(Resource) |
| 820 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 821 | logger.Error("failed to Marshal") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 822 | return err |
| 823 | } |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 824 | err = PONRMgr.KVStore.Put(ctx, Path, Value) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 825 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 826 | logger.Error("failed to put data to kv store %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 827 | return err |
| 828 | } |
| 829 | return nil |
| 830 | } |
| 831 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 832 | func (PONRMgr *PONResourceManager) ClearResourceIDPool(ctx context.Context, contIntfID uint32, ResourceType string) bool { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 833 | /* |
| 834 | Clear Resource Pool for a given Resource Type on a given PON Port. |
| 835 | :return boolean: True if removed else False |
| 836 | */ |
| 837 | |
| 838 | // delegate to the master instance if sharing enabled across instances |
| 839 | SharedResourceMgr := PONRMgr.SharedResourceMgrs[PONRMgr.SharedIdxByType[ResourceType]] |
| 840 | if SharedResourceMgr != nil && PONRMgr != SharedResourceMgr { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 841 | return SharedResourceMgr.ClearResourceIDPool(ctx, contIntfID, ResourceType) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 842 | } |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 843 | Path := PONRMgr.GetPath(contIntfID, ResourceType) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 844 | if Path == "" { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 845 | logger.Error("Failed to get path") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 846 | return false |
| 847 | } |
| 848 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 849 | if err := PONRMgr.KVStore.Delete(ctx, Path); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 850 | logger.Errorf("Failed to delete resource %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 851 | return false |
| 852 | } |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 853 | logger.Debugf("Cleared resource %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 854 | return true |
| 855 | } |
| 856 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 857 | func (PONRMgr PONResourceManager) InitResourceMap(ctx context.Context, PONIntfONUID string) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 858 | /* |
| 859 | Initialize resource map |
| 860 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 861 | */ |
| 862 | // initialize pon_intf_onu_id tuple to alloc_ids map |
| 863 | AllocIDPath := fmt.Sprintf(ALLOC_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, PONIntfONUID) |
| 864 | var AllocIDs []byte |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 865 | Result := PONRMgr.KVStore.Put(ctx, AllocIDPath, AllocIDs) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 866 | if Result != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 867 | logger.Error("Failed to update the KV store") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 868 | return |
| 869 | } |
| 870 | // initialize pon_intf_onu_id tuple to gemport_ids map |
| 871 | GEMPortIDPath := fmt.Sprintf(GEMPORT_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, PONIntfONUID) |
| 872 | var GEMPortIDs []byte |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 873 | Result = PONRMgr.KVStore.Put(ctx, GEMPortIDPath, GEMPortIDs) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 874 | if Result != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 875 | logger.Error("Failed to update the KV store") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 876 | return |
| 877 | } |
| 878 | } |
| 879 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 880 | func (PONRMgr PONResourceManager) RemoveResourceMap(ctx context.Context, PONIntfONUID string) bool { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 881 | /* |
| 882 | Remove resource map |
| 883 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 884 | */ |
| 885 | // remove pon_intf_onu_id tuple to alloc_ids map |
| 886 | var err error |
| 887 | AllocIDPath := fmt.Sprintf(ALLOC_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, PONIntfONUID) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 888 | if err = PONRMgr.KVStore.Delete(ctx, AllocIDPath); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 889 | logger.Errorf("Failed to remove resource %s", AllocIDPath) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 890 | return false |
| 891 | } |
| 892 | // remove pon_intf_onu_id tuple to gemport_ids map |
| 893 | GEMPortIDPath := fmt.Sprintf(GEMPORT_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, PONIntfONUID) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 894 | err = PONRMgr.KVStore.Delete(ctx, GEMPortIDPath) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 895 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 896 | logger.Errorf("Failed to remove resource %s", GEMPortIDPath) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 897 | return false |
| 898 | } |
| 899 | |
| 900 | FlowIDPath := fmt.Sprintf(FLOW_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, PONIntfONUID) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 901 | if FlowIDs, err := PONRMgr.KVStore.List(ctx, FlowIDPath); err != nil { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 902 | for _, Flow := range FlowIDs { |
| 903 | FlowIDInfoPath := fmt.Sprintf(FLOW_ID_INFO_PATH, PONRMgr.DeviceID, PONIntfONUID, Flow.Value) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 904 | if err = PONRMgr.KVStore.Delete(ctx, FlowIDInfoPath); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 905 | logger.Errorf("Failed to remove resource %s", FlowIDInfoPath) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 906 | return false |
| 907 | } |
| 908 | } |
| 909 | } |
| 910 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 911 | if err = PONRMgr.KVStore.Delete(ctx, FlowIDPath); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 912 | logger.Errorf("Failed to remove resource %s", FlowIDPath) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 913 | return false |
| 914 | } |
| 915 | |
| 916 | return true |
| 917 | } |
| 918 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 919 | func (PONRMgr *PONResourceManager) GetCurrentAllocIDForOnu(ctx context.Context, IntfONUID string) []uint32 { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 920 | /* |
| 921 | Get currently configured alloc ids for given pon_intf_onu_id |
| 922 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 923 | :return list: List of alloc_ids if available, else None |
| 924 | */ |
| 925 | Path := fmt.Sprintf(ALLOC_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID) |
| 926 | |
| 927 | var Data []uint32 |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 928 | Value, err := PONRMgr.KVStore.Get(ctx, Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 929 | if err == nil { |
| 930 | if Value != nil { |
| 931 | Val, err := ToByte(Value.Value) |
| 932 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 933 | logger.Errorw("Failed to convert into byte array", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 934 | return Data |
| 935 | } |
| 936 | if err = json.Unmarshal(Val, &Data); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 937 | logger.Error("Failed to unmarshal", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 938 | return Data |
| 939 | } |
| 940 | } |
| 941 | } |
| 942 | return Data |
| 943 | } |
| 944 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 945 | func (PONRMgr *PONResourceManager) GetCurrentGEMPortIDsForOnu(ctx context.Context, IntfONUID string) []uint32 { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 946 | /* |
| 947 | Get currently configured gemport ids for given pon_intf_onu_id |
| 948 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 949 | :return list: List of gemport IDs if available, else None |
| 950 | */ |
| 951 | |
| 952 | Path := fmt.Sprintf(GEMPORT_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID) |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 953 | logger.Debugf("Getting current gemports for %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 954 | var Data []uint32 |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 955 | Value, err := PONRMgr.KVStore.Get(ctx, Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 956 | if err == nil { |
| 957 | if Value != nil { |
| 958 | Val, _ := ToByte(Value.Value) |
| 959 | if err = json.Unmarshal(Val, &Data); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 960 | logger.Errorw("Failed to unmarshal", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 961 | return Data |
| 962 | } |
| 963 | } |
| 964 | } else { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 965 | logger.Errorf("Failed to get data from kvstore for %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 966 | } |
| 967 | return Data |
| 968 | } |
| 969 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 970 | func (PONRMgr *PONResourceManager) GetCurrentFlowIDsForOnu(ctx context.Context, IntfONUID string) []uint32 { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 971 | /* |
| 972 | Get currently configured flow ids for given pon_intf_onu_id |
| 973 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 974 | :return list: List of Flow IDs if available, else None |
| 975 | */ |
| 976 | |
| 977 | Path := fmt.Sprintf(FLOW_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID) |
| 978 | |
| 979 | var Data []uint32 |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 980 | Value, err := PONRMgr.KVStore.Get(ctx, Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 981 | if err == nil { |
| 982 | if Value != nil { |
| 983 | Val, _ := ToByte(Value.Value) |
| 984 | if err = json.Unmarshal(Val, &Data); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 985 | logger.Error("Failed to unmarshal") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 986 | return Data |
| 987 | } |
| 988 | } |
| 989 | } |
| 990 | return Data |
| 991 | } |
| 992 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 993 | 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] | 994 | /* |
| 995 | Get flow details configured for the ONU. |
| 996 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 997 | :param flow_id: Flow Id reference |
| 998 | :param Data: Result |
| 999 | :return error: nil if no error in getting from KV store |
| 1000 | */ |
| 1001 | |
| 1002 | Path := fmt.Sprintf(FLOW_ID_INFO_PATH, PONRMgr.DeviceID, IntfONUID, FlowID) |
| 1003 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1004 | Value, err := PONRMgr.KVStore.Get(ctx, Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1005 | if err == nil { |
| 1006 | if Value != nil { |
| 1007 | Val, err := ToByte(Value.Value) |
| 1008 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 1009 | 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] | 1010 | return err |
| 1011 | } |
| 1012 | if err = json.Unmarshal(Val, Data); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 1013 | logger.Errorw("Failed to unmarshal", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1014 | return err |
| 1015 | } |
| 1016 | } |
| 1017 | } |
| 1018 | return err |
| 1019 | } |
| 1020 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1021 | func (PONRMgr *PONResourceManager) RemoveFlowIDInfo(ctx context.Context, IntfONUID string, FlowID uint32) bool { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1022 | /* |
| 1023 | Get flow_id details configured for the ONU. |
| 1024 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 1025 | :param flow_id: Flow Id reference |
| 1026 | */ |
| 1027 | Path := fmt.Sprintf(FLOW_ID_INFO_PATH, PONRMgr.DeviceID, IntfONUID, FlowID) |
| 1028 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1029 | if err := PONRMgr.KVStore.Delete(ctx, Path); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 1030 | logger.Errorf("Falied to remove resource %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1031 | return false |
| 1032 | } |
| 1033 | return true |
| 1034 | } |
| 1035 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1036 | func (PONRMgr *PONResourceManager) UpdateAllocIdsForOnu(ctx context.Context, IntfONUID string, AllocIDs []uint32) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1037 | /* |
| 1038 | Update currently configured alloc ids for given pon_intf_onu_id |
| 1039 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 1040 | :param alloc_ids: list of alloc ids |
| 1041 | */ |
| 1042 | var Value []byte |
| 1043 | var err error |
| 1044 | Path := fmt.Sprintf(ALLOC_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID) |
| 1045 | Value, err = json.Marshal(AllocIDs) |
| 1046 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 1047 | logger.Error("failed to Marshal") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1048 | return err |
| 1049 | } |
| 1050 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1051 | if err = PONRMgr.KVStore.Put(ctx, Path, Value); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 1052 | logger.Errorf("Failed to update resource %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1053 | return err |
| 1054 | } |
| 1055 | return err |
| 1056 | } |
| 1057 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1058 | func (PONRMgr *PONResourceManager) UpdateGEMPortIDsForOnu(ctx context.Context, IntfONUID string, GEMPortIDs []uint32) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1059 | /* |
| 1060 | Update currently configured gemport ids for given pon_intf_onu_id |
| 1061 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 1062 | :param gemport_ids: list of gem port ids |
| 1063 | */ |
| 1064 | |
| 1065 | var Value []byte |
| 1066 | var err error |
| 1067 | Path := fmt.Sprintf(GEMPORT_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID) |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 1068 | logger.Debugf("Updating gemport ids for %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1069 | Value, err = json.Marshal(GEMPortIDs) |
| 1070 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 1071 | logger.Error("failed to Marshal") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1072 | return err |
| 1073 | } |
| 1074 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1075 | if err = PONRMgr.KVStore.Put(ctx, Path, Value); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 1076 | logger.Errorf("Failed to update resource %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1077 | return err |
| 1078 | } |
| 1079 | return err |
| 1080 | } |
| 1081 | |
| 1082 | func checkForFlowIDInList(FlowIDList []uint32, FlowID uint32) (bool, uint32) { |
| 1083 | /* |
| 1084 | Check for a flow id in a given list of flow IDs. |
| 1085 | :param FLowIDList: List of Flow IDs |
| 1086 | :param FlowID: Flowd to check in the list |
| 1087 | : return true and the index if present false otherwise. |
| 1088 | */ |
| 1089 | |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 1090 | for idx := range FlowIDList { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1091 | if FlowID == FlowIDList[idx] { |
| 1092 | return true, uint32(idx) |
| 1093 | } |
| 1094 | } |
| 1095 | return false, 0 |
| 1096 | } |
| 1097 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1098 | 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] | 1099 | /* |
| 1100 | Update the flow_id list of the ONU (add or remove flow_id from the list) |
| 1101 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 1102 | :param flow_id: flow ID |
| 1103 | :param add: Boolean flag to indicate whether the flow_id should be |
| 1104 | added or removed from the list. Defaults to adding the flow. |
| 1105 | */ |
| 1106 | var Value []byte |
| 1107 | var err error |
| 1108 | var RetVal bool |
| 1109 | var IDx uint32 |
| 1110 | Path := fmt.Sprintf(FLOW_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1111 | FlowIDs := PONRMgr.GetCurrentFlowIDsForOnu(ctx, IntfONUID) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1112 | |
| 1113 | if Add { |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 1114 | if RetVal, _ = checkForFlowIDInList(FlowIDs, FlowID); RetVal { |
| 1115 | return nil |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1116 | } |
| 1117 | FlowIDs = append(FlowIDs, FlowID) |
| 1118 | } else { |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 1119 | if RetVal, IDx = checkForFlowIDInList(FlowIDs, FlowID); !RetVal { |
| 1120 | return nil |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1121 | } |
| 1122 | // delete the index and shift |
| 1123 | FlowIDs = append(FlowIDs[:IDx], FlowIDs[IDx+1:]...) |
| 1124 | } |
| 1125 | Value, err = json.Marshal(FlowIDs) |
| 1126 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 1127 | logger.Error("Failed to Marshal") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1128 | return err |
| 1129 | } |
| 1130 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1131 | if err = PONRMgr.KVStore.Put(ctx, Path, Value); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 1132 | logger.Errorf("Failed to update resource %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1133 | return err |
| 1134 | } |
| 1135 | return err |
| 1136 | } |
| 1137 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1138 | 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] | 1139 | /* |
| 1140 | Update any metadata associated with the flow_id. The flow_data could be json |
| 1141 | or any of other data structure. The resource manager doesnt care |
| 1142 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 1143 | :param flow_id: Flow ID |
| 1144 | :param flow_data: Flow data blob |
| 1145 | */ |
| 1146 | var Value []byte |
| 1147 | var err error |
| 1148 | Path := fmt.Sprintf(FLOW_ID_INFO_PATH, PONRMgr.DeviceID, IntfONUID, FlowID) |
| 1149 | Value, err = json.Marshal(FlowData) |
| 1150 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 1151 | logger.Error("failed to Marshal") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1152 | return err |
| 1153 | } |
| 1154 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1155 | if err = PONRMgr.KVStore.Put(ctx, Path, Value); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 1156 | logger.Errorf("Failed to update resource %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1157 | return err |
| 1158 | } |
| 1159 | return err |
| 1160 | } |
| 1161 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 1162 | func (PONRMgr *PONResourceManager) GenerateNextID(Resource map[string]interface{}) (uint32, error) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1163 | /* |
| 1164 | Generate unique id having OFFSET as start |
| 1165 | :param resource: resource used to generate ID |
| 1166 | :return uint32: generated id |
| 1167 | */ |
| 1168 | ByteArray, err := ToByte(Resource[POOL]) |
| 1169 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 1170 | logger.Error("Failed to convert resource to byte array") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1171 | return 0, err |
| 1172 | } |
| 1173 | Data := bitmap.TSFromData(ByteArray, false) |
| 1174 | if Data == nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 1175 | logger.Error("Failed to get data from byte array") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1176 | return 0, errors.New("Failed to get data from byte array") |
| 1177 | } |
| 1178 | |
| 1179 | Len := Data.Len() |
| 1180 | var Idx int |
| 1181 | for Idx = 0; Idx < Len; Idx++ { |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 1182 | if !Data.Get(Idx) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1183 | break |
| 1184 | } |
| 1185 | } |
| 1186 | Data.Set(Idx, true) |
| 1187 | res := uint32(Resource[START_IDX].(float64)) |
| 1188 | Resource[POOL] = Data.Data(false) |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 1189 | logger.Debugf("Generated ID for %d", (uint32(Idx) + res)) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1190 | return (uint32(Idx) + res), err |
| 1191 | } |
| 1192 | |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 1193 | func (PONRMgr *PONResourceManager) ReleaseID(Resource map[string]interface{}, Id uint32) bool { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1194 | /* |
| 1195 | Release unique id having OFFSET as start index. |
| 1196 | :param resource: resource used to release ID |
| 1197 | :param unique_id: id need to be released |
| 1198 | */ |
| 1199 | ByteArray, err := ToByte(Resource[POOL]) |
| 1200 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 1201 | logger.Error("Failed to convert resource to byte array") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1202 | return false |
| 1203 | } |
| 1204 | Data := bitmap.TSFromData(ByteArray, false) |
| 1205 | if Data == nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 1206 | logger.Error("Failed to get resource pool") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1207 | return false |
| 1208 | } |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 1209 | Idx := Id - uint32(Resource[START_IDX].(float64)) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1210 | Data.Set(int(Idx), false) |
| 1211 | Resource[POOL] = Data.Data(false) |
| 1212 | |
| 1213 | return true |
| 1214 | } |
| 1215 | |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 1216 | /* Reserves a unique id in the specified resource pool. |
| 1217 | :param Resource: resource used to reserve ID |
| 1218 | :param Id: ID to be reserved |
| 1219 | */ |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 1220 | func (PONRMgr *PONResourceManager) reserveID(TSData *bitmap.Threadsafe, StartIndex uint32, Id uint32) bool { |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 1221 | Data := bitmap.TSFromData(TSData.Data(false), false) |
| 1222 | if Data == nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 1223 | logger.Error("Failed to get resource pool") |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 1224 | return false |
| 1225 | } |
| 1226 | Idx := Id - StartIndex |
| 1227 | Data.Set(int(Idx), true) |
| 1228 | return true |
| 1229 | } |
| 1230 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1231 | func (PONRMgr *PONResourceManager) GetTechnology() string { |
| 1232 | return PONRMgr.Technology |
| 1233 | } |
| 1234 | |
| 1235 | func (PONRMgr *PONResourceManager) GetResourceTypeAllocID() string { |
| 1236 | return ALLOC_ID |
| 1237 | } |
| 1238 | |
| 1239 | func (PONRMgr *PONResourceManager) GetResourceTypeGemPortID() string { |
| 1240 | return GEMPORT_ID |
| 1241 | } |
| 1242 | |
| 1243 | // ToByte converts an interface value to a []byte. The interface should either be of |
| 1244 | // a string type or []byte. Otherwise, an error is returned. |
| 1245 | func ToByte(value interface{}) ([]byte, error) { |
| 1246 | switch t := value.(type) { |
| 1247 | case []byte: |
| 1248 | return value.([]byte), nil |
| 1249 | case string: |
| 1250 | return []byte(value.(string)), nil |
| 1251 | default: |
| 1252 | return nil, fmt.Errorf("unexpected-type-%T", t) |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | // ToString converts an interface value to a string. The interface should either be of |
| 1257 | // a string type or []byte. Otherwise, an error is returned. |
| 1258 | func ToString(value interface{}) (string, error) { |
| 1259 | switch t := value.(type) { |
| 1260 | case []byte: |
| 1261 | return string(value.([]byte)), nil |
| 1262 | case string: |
| 1263 | return value.(string), nil |
| 1264 | default: |
| 1265 | return "", fmt.Errorf("unexpected-type-%T", t) |
| 1266 | } |
| 1267 | } |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1268 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1269 | func (PONRMgr *PONResourceManager) AddOnuGemInfo(ctx context.Context, intfID uint32, onuGemData interface{}) error { |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1270 | /* |
| 1271 | Update onugem info map, |
| 1272 | :param pon_intf_id: reference of PON interface id |
| 1273 | :param onuegmdata: onugem info map |
| 1274 | */ |
| 1275 | var Value []byte |
| 1276 | var err error |
| 1277 | Path := fmt.Sprintf(ONU_GEM_INFO_PATH, PONRMgr.DeviceID, intfID) |
| 1278 | Value, err = json.Marshal(onuGemData) |
| 1279 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 1280 | logger.Error("failed to Marshal") |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1281 | return err |
| 1282 | } |
| 1283 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1284 | if err = PONRMgr.KVStore.Put(ctx, Path, Value); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 1285 | logger.Errorf("Failed to update resource %s", Path) |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1286 | return err |
| 1287 | } |
| 1288 | return err |
| 1289 | } |
| 1290 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1291 | func (PONRMgr *PONResourceManager) GetOnuGemInfo(ctx context.Context, IntfId uint32, onuGemInfo interface{}) error { |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1292 | /* |
| 1293 | Get onugeminfo map from kvstore |
| 1294 | :param intfid: refremce pon intfid |
| 1295 | :param onuGemInfo: onugem info to return from kv strore. |
| 1296 | */ |
| 1297 | var Val []byte |
| 1298 | |
| 1299 | path := fmt.Sprintf(ONU_GEM_INFO_PATH, PONRMgr.DeviceID, IntfId) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1300 | value, err := PONRMgr.KVStore.Get(ctx, path) |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1301 | if err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 1302 | logger.Errorw("Failed to get from kv store", log.Fields{"path": path}) |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1303 | return err |
| 1304 | } else if value == nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 1305 | logger.Debug("No onuinfo for path", log.Fields{"path": path}) |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1306 | return nil // returning nil as this could happen if there are no onus for the interface yet |
| 1307 | } |
| 1308 | if Val, err = kvstore.ToByte(value.Value); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 1309 | logger.Error("Failed to convert to byte array") |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1310 | return err |
| 1311 | } |
| 1312 | |
| 1313 | if err = json.Unmarshal(Val, &onuGemInfo); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 1314 | logger.Error("Failed to unmarshall") |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1315 | return err |
| 1316 | } |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 1317 | logger.Debugw("found onuinfo from path", log.Fields{"path": path, "onuinfo": onuGemInfo}) |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1318 | return err |
| 1319 | } |
| 1320 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1321 | func (PONRMgr *PONResourceManager) DelOnuGemInfoForIntf(ctx context.Context, intfId uint32) error { |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1322 | /* |
| 1323 | delete onugem info for an interface from kvstore |
| 1324 | :param intfid: refremce pon intfid |
| 1325 | */ |
| 1326 | |
| 1327 | path := fmt.Sprintf(ONU_GEM_INFO_PATH, PONRMgr.DeviceID, intfId) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1328 | if err := PONRMgr.KVStore.Delete(ctx, path); err != nil { |
Scott Baker | e668595 | 2020-06-23 04:05:39 +0000 | [diff] [blame] | 1329 | logger.Errorf("Falied to remove resource %s", path) |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1330 | return err |
| 1331 | } |
| 1332 | return nil |
| 1333 | } |