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" |
Girish Gowdra | 89c985b | 2020-10-14 15:02:09 -0700 | [diff] [blame] | 28 | "github.com/opencord/voltha-lib-go/v4/pkg/db" |
| 29 | "github.com/opencord/voltha-lib-go/v4/pkg/db/kvstore" |
| 30 | "github.com/opencord/voltha-lib-go/v4/pkg/log" |
| 31 | tp "github.com/opencord/voltha-lib-go/v4/pkg/techprofile" |
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" |
Matteo Scandolo | 29ff79c | 2020-11-06 13:03:17 -0800 | [diff] [blame] | 44 | |
| 45 | PATH_PREFIX = "%s/resource_manager/{%s}" |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 46 | |
| 47 | /*The path under which configuration data is stored is defined as technology/device agnostic. |
| 48 | That means the path does not include any specific technology/device variable. Using technology/device |
| 49 | agnostic path also makes northbound applications, that need to write to this path, |
| 50 | technology/device agnostic. |
| 51 | |
| 52 | Default kv client of PonResourceManager reads from/writes to PATH_PREFIX defined above. |
| 53 | That is why, an additional kv client (named KVStoreForConfig) is defined to read from the config path. |
| 54 | */ |
Matteo Scandolo | 29ff79c | 2020-11-06 13:03:17 -0800 | [diff] [blame] | 55 | |
| 56 | PATH_PREFIX_FOR_CONFIG = "%s/resource_manager/config" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 57 | /*The resource ranges for a given device model should be placed |
| 58 | at 'resource_manager/<technology>/resource_ranges/<olt_model_type>' |
| 59 | path on the KV store. |
| 60 | If Resource Range parameters are to be read from the external KV store, |
| 61 | they are expected to be stored in the following format. |
| 62 | Note: All parameters are MANDATORY for now. |
| 63 | constants used as keys to reference the resource range parameters from |
| 64 | and external KV store. |
| 65 | */ |
| 66 | UNI_ID_START_IDX = "uni_id_start" |
| 67 | UNI_ID_END_IDX = "uni_id_end" |
| 68 | ONU_ID_START_IDX = "onu_id_start" |
| 69 | ONU_ID_END_IDX = "onu_id_end" |
| 70 | ONU_ID_SHARED_IDX = "onu_id_shared" |
| 71 | ALLOC_ID_START_IDX = "alloc_id_start" |
| 72 | ALLOC_ID_END_IDX = "alloc_id_end" |
| 73 | ALLOC_ID_SHARED_IDX = "alloc_id_shared" |
| 74 | GEMPORT_ID_START_IDX = "gemport_id_start" |
| 75 | GEMPORT_ID_END_IDX = "gemport_id_end" |
| 76 | GEMPORT_ID_SHARED_IDX = "gemport_id_shared" |
| 77 | FLOW_ID_START_IDX = "flow_id_start" |
| 78 | FLOW_ID_END_IDX = "flow_id_end" |
| 79 | FLOW_ID_SHARED_IDX = "flow_id_shared" |
| 80 | NUM_OF_PON_PORT = "pon_ports" |
| 81 | |
| 82 | /* |
| 83 | The KV store backend is initialized with a path prefix and we need to |
| 84 | provide only the suffix. |
| 85 | */ |
| 86 | PON_RESOURCE_RANGE_CONFIG_PATH = "resource_ranges/%s" |
| 87 | |
| 88 | //resource path suffix |
| 89 | //Path on the KV store for storing alloc id ranges and resource pool for a given interface |
| 90 | //Format: <device_id>/alloc_id_pool/<pon_intf_id> |
| 91 | ALLOC_ID_POOL_PATH = "{%s}/alloc_id_pool/{%d}" |
| 92 | //Path on the KV store for storing gemport id ranges and resource pool for a given interface |
| 93 | //Format: <device_id>/gemport_id_pool/<pon_intf_id> |
| 94 | GEMPORT_ID_POOL_PATH = "{%s}/gemport_id_pool/{%d}" |
| 95 | //Path on the KV store for storing onu id ranges and resource pool for a given interface |
| 96 | //Format: <device_id>/onu_id_pool/<pon_intf_id> |
| 97 | ONU_ID_POOL_PATH = "{%s}/onu_id_pool/{%d}" |
| 98 | //Path on the KV store for storing flow id ranges and resource pool for a given interface |
| 99 | //Format: <device_id>/flow_id_pool/<pon_intf_id> |
| 100 | FLOW_ID_POOL_PATH = "{%s}/flow_id_pool/{%d}" |
| 101 | |
| 102 | //Path on the KV store for storing list of alloc IDs for a given ONU |
| 103 | //Format: <device_id>/<(pon_intf_id, onu_id)>/alloc_ids |
| 104 | ALLOC_ID_RESOURCE_MAP_PATH = "{%s}/{%s}/alloc_ids" |
| 105 | |
| 106 | //Path on the KV store for storing list of gemport IDs for a given ONU |
| 107 | //Format: <device_id>/<(pon_intf_id, onu_id)>/gemport_ids |
| 108 | GEMPORT_ID_RESOURCE_MAP_PATH = "{%s}/{%s}/gemport_ids" |
| 109 | |
| 110 | //Path on the KV store for storing list of Flow IDs for a given ONU |
| 111 | //Format: <device_id>/<(pon_intf_id, onu_id)>/flow_ids |
| 112 | FLOW_ID_RESOURCE_MAP_PATH = "{%s}/{%s}/flow_ids" |
| 113 | |
| 114 | //Flow Id info: Use to store more metadata associated with the flow_id |
Serkant Uluderya | 198de90 | 2020-11-16 20:29:17 +0300 | [diff] [blame] | 115 | FLOW_ID_INFO_PATH_PREFIX = "{%s}/flow_id_info" |
| 116 | //Format: <device_id>/flow_id_info/<(pon_intf_id, onu_id)> |
| 117 | FLOW_ID_INFO_PATH_INTF_ONU_PREFIX = "{%s}/flow_id_info/{%s}" |
| 118 | //Format: <device_id>/flow_id_info/<(pon_intf_id, onu_id)><flow_id> |
| 119 | FLOW_ID_INFO_PATH = FLOW_ID_INFO_PATH_PREFIX + "/{%s}/{%d}" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 120 | |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 121 | //path on the kvstore to store onugem info map |
| 122 | //format: <device-id>/onu_gem_info/<intfid> |
| 123 | ONU_GEM_INFO_PATH = "{%s}/onu_gem_info/{%d}" // onu_gem/<(intfid)> |
| 124 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 125 | //Constants for internal usage. |
| 126 | PON_INTF_ID = "pon_intf_id" |
| 127 | START_IDX = "start_idx" |
| 128 | END_IDX = "end_idx" |
| 129 | POOL = "pool" |
| 130 | NUM_OF_PON_INTF = 16 |
| 131 | |
Neha Sharma | 130ac6d | 2020-04-08 08:46:32 +0000 | [diff] [blame] | 132 | KVSTORE_RETRY_TIMEOUT = 5 * time.Second |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 133 | //Path on the KV store for storing reserved gem ports |
| 134 | //Format: reserved_gemport_ids |
| 135 | RESERVED_GEMPORT_IDS_PATH = "reserved_gemport_ids" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 136 | ) |
| 137 | |
| 138 | //type ResourceTypeIndex string |
| 139 | //type ResourceType string |
| 140 | |
| 141 | type PONResourceManager struct { |
| 142 | //Implements APIs to initialize/allocate/release alloc/gemport/onu IDs. |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 143 | Technology string |
| 144 | DeviceType string |
| 145 | DeviceID string |
serkant.uluderya | 2f2855e | 2021-01-30 12:43:40 +0300 | [diff] [blame] | 146 | Backend string // ETCD only currently |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 147 | Address string // address of the KV store |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 148 | OLTModel string |
| 149 | KVStore *db.Backend |
| 150 | KVStoreForConfig *db.Backend |
| 151 | TechProfileMgr tp.TechProfileIf // create object of *tp.TechProfileMgr |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 152 | |
| 153 | // Below attribute, pon_resource_ranges, should be initialized |
| 154 | // by reading from KV store. |
| 155 | PonResourceRanges map[string]interface{} |
| 156 | SharedResourceMgrs map[string]*PONResourceManager |
| 157 | SharedIdxByType map[string]string |
| 158 | IntfIDs []uint32 // list of pon interface IDs |
| 159 | Globalorlocal string |
| 160 | } |
| 161 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 162 | func newKVClient(ctx context.Context, storeType string, address string, timeout time.Duration) (kvstore.Client, error) { |
| 163 | logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 164 | switch storeType { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 165 | case "etcd": |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 166 | return kvstore.NewEtcdClient(ctx, address, timeout, log.WarnLevel) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 167 | } |
| 168 | return nil, errors.New("unsupported-kv-store") |
| 169 | } |
| 170 | |
Matteo Scandolo | 29ff79c | 2020-11-06 13:03:17 -0800 | [diff] [blame] | 171 | func SetKVClient(ctx context.Context, Technology string, Backend string, Addr string, configClient bool, basePathKvStore string) *db.Backend { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 172 | // TODO : Make sure direct call to NewBackend is working fine with backend , currently there is some |
| 173 | // issue between kv store and backend , core is not calling NewBackend directly |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 174 | kvClient, err := newKVClient(ctx, Backend, Addr, KVSTORE_RETRY_TIMEOUT) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 175 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 176 | logger.Fatalw(ctx, "Failed to init KV client\n", log.Fields{"err": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 177 | return nil |
| 178 | } |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 179 | |
| 180 | var pathPrefix string |
| 181 | if configClient { |
Matteo Scandolo | 29ff79c | 2020-11-06 13:03:17 -0800 | [diff] [blame] | 182 | pathPrefix = fmt.Sprintf(PATH_PREFIX_FOR_CONFIG, basePathKvStore) |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 183 | } else { |
Matteo Scandolo | 29ff79c | 2020-11-06 13:03:17 -0800 | [diff] [blame] | 184 | pathPrefix = fmt.Sprintf(PATH_PREFIX, basePathKvStore, Technology) |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 185 | } |
| 186 | |
sbarbari | 1e3e29c | 2019-11-05 10:06:50 -0500 | [diff] [blame] | 187 | kvbackend := &db.Backend{ |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 188 | Client: kvClient, |
| 189 | StoreType: Backend, |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 190 | Address: Addr, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 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. |
Matteo Scandolo | 29ff79c | 2020-11-06 13:03:17 -0800 | [diff] [blame] | 198 | func NewPONResourceManager(ctx context.Context, Technology string, DeviceType string, DeviceID string, Backend string, Address string, basePathKvStore string) (*PONResourceManager, error) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 199 | var PONMgr PONResourceManager |
| 200 | PONMgr.Technology = Technology |
| 201 | PONMgr.DeviceType = DeviceType |
| 202 | PONMgr.DeviceID = DeviceID |
| 203 | PONMgr.Backend = Backend |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 204 | PONMgr.Address = Address |
Matteo Scandolo | 29ff79c | 2020-11-06 13:03:17 -0800 | [diff] [blame] | 205 | PONMgr.KVStore = SetKVClient(ctx, Technology, Backend, Address, false, basePathKvStore) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 206 | if PONMgr.KVStore == nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 207 | logger.Error(ctx, "KV Client initilization failed") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 208 | return nil, errors.New("Failed to init KV client") |
| 209 | } |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 210 | // init kv client to read from the config path |
Matteo Scandolo | 29ff79c | 2020-11-06 13:03:17 -0800 | [diff] [blame] | 211 | PONMgr.KVStoreForConfig = SetKVClient(ctx, Technology, Backend, Address, true, basePathKvStore) |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 212 | if PONMgr.KVStoreForConfig == nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 213 | logger.Error(ctx, "KV Config Client initilization failed") |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 214 | return nil, errors.New("Failed to init KV Config client") |
| 215 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 216 | // Initialize techprofile for this technology |
Matteo Scandolo | 866ac58 | 2020-11-09 12:18:31 -0800 | [diff] [blame] | 217 | if PONMgr.TechProfileMgr, _ = tp.NewTechProfile(ctx, &PONMgr, Backend, Address, basePathKvStore); PONMgr.TechProfileMgr == nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 218 | logger.Error(ctx, "Techprofile initialization failed") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 219 | return nil, errors.New("Failed to init tech profile") |
| 220 | } |
| 221 | PONMgr.PonResourceRanges = make(map[string]interface{}) |
| 222 | PONMgr.SharedResourceMgrs = make(map[string]*PONResourceManager) |
| 223 | PONMgr.SharedIdxByType = make(map[string]string) |
| 224 | PONMgr.SharedIdxByType[ONU_ID] = ONU_ID_SHARED_IDX |
| 225 | PONMgr.SharedIdxByType[ALLOC_ID] = ALLOC_ID_SHARED_IDX |
| 226 | PONMgr.SharedIdxByType[GEMPORT_ID] = GEMPORT_ID_SHARED_IDX |
| 227 | PONMgr.SharedIdxByType[FLOW_ID] = FLOW_ID_SHARED_IDX |
| 228 | PONMgr.IntfIDs = make([]uint32, NUM_OF_PON_INTF) |
| 229 | PONMgr.OLTModel = DeviceType |
| 230 | return &PONMgr, nil |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | Initialize PON resource ranges with config fetched from kv store. |
| 235 | return boolean: True if PON resource ranges initialized else false |
| 236 | Try to initialize the PON Resource Ranges from KV store based on the |
| 237 | OLT model key, if available |
| 238 | */ |
| 239 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 240 | func (PONRMgr *PONResourceManager) InitResourceRangesFromKVStore(ctx context.Context) bool { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 241 | //Initialize PON resource ranges with config fetched from kv store. |
| 242 | //:return boolean: True if PON resource ranges initialized else false |
| 243 | // Try to initialize the PON Resource Ranges from KV store based on the |
| 244 | // OLT model key, if available |
| 245 | if PONRMgr.OLTModel == "" { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 246 | logger.Error(ctx, "Failed to get OLT model") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 247 | return false |
| 248 | } |
| 249 | Path := fmt.Sprintf(PON_RESOURCE_RANGE_CONFIG_PATH, PONRMgr.OLTModel) |
| 250 | //get resource from kv store |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 251 | Result, err := PONRMgr.KVStore.Get(ctx, Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 252 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 253 | logger.Debugf(ctx, "Error in fetching resource %s from KV strore", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 254 | return false |
| 255 | } |
| 256 | if Result == nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 257 | logger.Debug(ctx, "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] | 258 | return false |
| 259 | } |
| 260 | //update internal ranges from kv ranges. If there are missing |
| 261 | // values in the KV profile, continue to use the defaults |
| 262 | Value, err := ToByte(Result.Value) |
| 263 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 264 | logger.Error(ctx, "Failed to convert kvpair to byte string") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 265 | return false |
| 266 | } |
| 267 | if err := json.Unmarshal(Value, &PONRMgr.PonResourceRanges); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 268 | logger.Error(ctx, "Failed to Unmarshal json byte") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 269 | return false |
| 270 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 271 | logger.Debug(ctx, "Init resource ranges from kvstore success") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 272 | return true |
| 273 | } |
| 274 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 275 | func (PONRMgr *PONResourceManager) UpdateRanges(ctx context.Context, StartIDx string, StartID uint32, EndIDx string, EndID uint32, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 276 | SharedIDx string, SharedPoolID uint32, RMgr *PONResourceManager) { |
| 277 | /* |
| 278 | Update the ranges for all reosurce type in the intermnal maps |
| 279 | param: resource type start index |
| 280 | param: start ID |
| 281 | param: resource type end index |
| 282 | param: end ID |
| 283 | param: resource type shared index |
| 284 | param: shared pool id |
| 285 | param: global resource manager |
| 286 | */ |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 287 | logger.Debugf(ctx, "update ranges for %s, %d", StartIDx, StartID) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 288 | |
| 289 | if StartID != 0 { |
| 290 | if (PONRMgr.PonResourceRanges[StartIDx] == nil) || (PONRMgr.PonResourceRanges[StartIDx].(uint32) < StartID) { |
| 291 | PONRMgr.PonResourceRanges[StartIDx] = StartID |
| 292 | } |
| 293 | } |
| 294 | if EndID != 0 { |
| 295 | if (PONRMgr.PonResourceRanges[EndIDx] == nil) || (PONRMgr.PonResourceRanges[EndIDx].(uint32) > EndID) { |
| 296 | PONRMgr.PonResourceRanges[EndIDx] = EndID |
| 297 | } |
| 298 | } |
| 299 | //if SharedPoolID != 0 { |
| 300 | PONRMgr.PonResourceRanges[SharedIDx] = SharedPoolID |
| 301 | //} |
| 302 | if RMgr != nil { |
| 303 | PONRMgr.SharedResourceMgrs[SharedIDx] = RMgr |
| 304 | } |
| 305 | } |
| 306 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 307 | func (PONRMgr *PONResourceManager) InitDefaultPONResourceRanges(ctx context.Context, |
| 308 | ONUIDStart uint32, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 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 | */ |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 342 | PONRMgr.UpdateRanges(ctx, ONU_ID_START_IDX, ONUIDStart, ONU_ID_END_IDX, ONUIDEnd, ONU_ID_SHARED_IDX, ONUIDSharedPoolID, nil) |
| 343 | PONRMgr.UpdateRanges(ctx, ALLOC_ID_START_IDX, AllocIDStart, ALLOC_ID_END_IDX, AllocIDEnd, ALLOC_ID_SHARED_IDX, AllocIDSharedPoolID, nil) |
| 344 | PONRMgr.UpdateRanges(ctx, GEMPORT_ID_START_IDX, GEMPortIDStart, GEMPORT_ID_END_IDX, GEMPortIDEnd, GEMPORT_ID_SHARED_IDX, GEMPortIDSharedPoolID, nil) |
| 345 | PONRMgr.UpdateRanges(ctx, FLOW_ID_START_IDX, FlowIDStart, FLOW_ID_END_IDX, FlowIDEnd, FLOW_ID_SHARED_IDX, FlowIDSharedPoolID, nil) |
| 346 | PONRMgr.UpdateRanges(ctx, UNI_ID_START_IDX, UNIIDStart, UNI_ID_END_IDX, UNIIDEnd, "", 0, nil) |
| 347 | logger.Debug(ctx, "Initialize default range values") |
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 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 363 | logger.Debug(ctx, "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 { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 374 | logger.Error(ctx, "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 { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 390 | logger.Error(ctx, "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 { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 405 | logger.Error(ctx, "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 { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 421 | logger.Error(ctx, "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 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 435 | logger.Debug(ctx, "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 { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 443 | logger.Error(ctx, "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 { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 457 | logger.Error(ctx, "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 { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 470 | logger.Error(ctx, "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 { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 484 | logger.Error(ctx, "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 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 511 | Path := PONRMgr.GetPath(ctx, Intf, ResourceType) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 512 | if Path == "" { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 513 | logger.Errorf(ctx, "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) { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 521 | logger.Debugf(ctx, "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 |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 529 | logger.Debugw(ctx, "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 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 532 | FormatResult, err := PONRMgr.FormatResource(ctx, Intf, StartID, EndID, excluded) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 533 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 534 | logger.Errorf(ctx, "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 { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 540 | logger.Debug(ctx, "Successfuly posted to kv store") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 541 | return err |
| 542 | } |
| 543 | } |
| 544 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 545 | logger.Debug(ctx, "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 { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 555 | logger.Errorw(ctx, "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 { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 564 | logger.Errorw(ctx, "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 { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 568 | logger.Errorw(ctx, "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 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 574 | func (PONRMgr *PONResourceManager) FormatResource(ctx context.Context, IntfID uint32, StartIDx uint32, EndIDx uint32, |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 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 { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 596 | logger.Error(ctx, "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 { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 601 | logger.Warnf(ctx, "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 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 605 | PONRMgr.reserveID(ctx, TSData, StartIDx, excludedID) |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 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 { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 611 | logger.Errorf(ctx, "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) { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 631 | logger.Debugf(ctx, "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 { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 643 | logger.Error(ctx, "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 { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 653 | logger.Error(ctx, "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 { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 659 | logger.Error(ctx, "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 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 666 | func (PONRMgr *PONResourceManager) GetPath(ctx context.Context, IntfID uint32, ResourceType string) string { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 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 { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 692 | logger.Error(ctx, "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 | */ |
Matteo Scandolo | b85b2f0 | 2021-03-18 14:44:41 -0700 | [diff] [blame] | 706 | |
| 707 | logger.Debugw(ctx, "getting-resource-id", log.Fields{ |
| 708 | "intf-id": IntfID, |
| 709 | "resource-type": ResourceType, |
| 710 | "num": NumIDs, |
| 711 | }) |
| 712 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 713 | if NumIDs < 1 { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 714 | logger.Error(ctx, "Invalid number of resources requested") |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 715 | return nil, fmt.Errorf("Invalid number of resources requested %d", NumIDs) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 716 | } |
| 717 | // delegate to the master instance if sharing enabled across instances |
| 718 | |
| 719 | SharedResourceMgr := PONRMgr.SharedResourceMgrs[PONRMgr.SharedIdxByType[ResourceType]] |
| 720 | if SharedResourceMgr != nil && PONRMgr != SharedResourceMgr { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 721 | return SharedResourceMgr.GetResourceID(ctx, IntfID, ResourceType, NumIDs) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 722 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 723 | logger.Debugf(ctx, "Fetching resource from %s rsrc mgr for resource %s", PONRMgr.Globalorlocal, ResourceType) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 724 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 725 | Path := PONRMgr.GetPath(ctx, IntfID, ResourceType) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 726 | if Path == "" { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 727 | logger.Errorf(ctx, "Failed to get path for resource type %s", ResourceType) |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 728 | 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] | 729 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 730 | logger.Debugf(ctx, "Get resource for type %s on path %s", ResourceType, Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 731 | var Result []uint32 |
| 732 | var NextID uint32 |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 733 | Resource, err := PONRMgr.GetResource(ctx, Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 734 | if (err == nil) && (ResourceType == ONU_ID) || (ResourceType == FLOW_ID) { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 735 | if NextID, err = PONRMgr.GenerateNextID(ctx, Resource); err != nil { |
| 736 | logger.Error(ctx, "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 if (err == nil) && ((ResourceType == GEMPORT_ID) || (ResourceType == ALLOC_ID)) { |
| 741 | if NumIDs == 1 { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 742 | if NextID, err = PONRMgr.GenerateNextID(ctx, Resource); err != nil { |
| 743 | logger.Error(ctx, "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 | } else { |
| 748 | for NumIDs > 0 { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 749 | if NextID, err = PONRMgr.GenerateNextID(ctx, Resource); err != nil { |
| 750 | logger.Error(ctx, "Failed to Generate ID") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 751 | return Result, err |
| 752 | } |
| 753 | Result = append(Result, NextID) |
| 754 | NumIDs-- |
| 755 | } |
| 756 | } |
| 757 | } else { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 758 | logger.Error(ctx, "get resource failed") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 759 | return Result, err |
| 760 | } |
| 761 | |
| 762 | //Update resource in kv store |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 763 | if PONRMgr.UpdateResource(ctx, Path, Resource) != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 764 | logger.Errorf(ctx, "Failed to update resource %s", Path) |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 765 | return nil, fmt.Errorf("Failed to update resource %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 766 | } |
| 767 | return Result, nil |
| 768 | } |
| 769 | |
| 770 | func checkValidResourceType(ResourceType string) bool { |
| 771 | KnownResourceTypes := []string{ONU_ID, ALLOC_ID, GEMPORT_ID, FLOW_ID} |
| 772 | |
| 773 | for _, v := range KnownResourceTypes { |
| 774 | if v == ResourceType { |
| 775 | return true |
| 776 | } |
| 777 | } |
| 778 | return false |
| 779 | } |
| 780 | |
Matteo Scandolo | b85b2f0 | 2021-03-18 14:44:41 -0700 | [diff] [blame] | 781 | func (PONRMgr *PONResourceManager) FreeResourceID(ctx context.Context, IntfID uint32, ResourceType string, ReleaseContent []uint32) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 782 | /* |
Matteo Scandolo | b85b2f0 | 2021-03-18 14:44:41 -0700 | [diff] [blame] | 783 | Release alloc/gemport/onu/flow id for given OLT PON interface. |
| 784 | :param pon_intf_id: OLT PON interface id |
| 785 | :param resource_type: String to identify type of resource |
| 786 | :param release_content: required number of ids |
| 787 | :return boolean: True if all IDs in given release_content release else False |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 788 | */ |
Matteo Scandolo | b85b2f0 | 2021-03-18 14:44:41 -0700 | [diff] [blame] | 789 | |
| 790 | logger.Debugw(ctx, "freeing-resource-id", log.Fields{ |
| 791 | "intf-id": IntfID, |
| 792 | "resource-type": ResourceType, |
| 793 | "release-content": ReleaseContent, |
| 794 | }) |
| 795 | |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 796 | if !checkValidResourceType(ResourceType) { |
Matteo Scandolo | b85b2f0 | 2021-03-18 14:44:41 -0700 | [diff] [blame] | 797 | err := fmt.Errorf("Invalid resource type: %s", ResourceType) |
| 798 | logger.Error(ctx, err.Error()) |
| 799 | return err |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 800 | } |
| 801 | if ReleaseContent == nil { |
Matteo Scandolo | b85b2f0 | 2021-03-18 14:44:41 -0700 | [diff] [blame] | 802 | err := fmt.Errorf("Nothing to release") |
| 803 | logger.Debug(ctx, err.Error()) |
| 804 | return err |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 805 | } |
| 806 | // delegate to the master instance if sharing enabled across instances |
| 807 | SharedResourceMgr := PONRMgr.SharedResourceMgrs[PONRMgr.SharedIdxByType[ResourceType]] |
| 808 | if SharedResourceMgr != nil && PONRMgr != SharedResourceMgr { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 809 | return SharedResourceMgr.FreeResourceID(ctx, IntfID, ResourceType, ReleaseContent) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 810 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 811 | Path := PONRMgr.GetPath(ctx, IntfID, ResourceType) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 812 | if Path == "" { |
Matteo Scandolo | b85b2f0 | 2021-03-18 14:44:41 -0700 | [diff] [blame] | 813 | err := fmt.Errorf("Failed to get path for IntfId %d and ResourceType %s", IntfID, ResourceType) |
| 814 | logger.Error(ctx, err.Error()) |
| 815 | return err |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 816 | } |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 817 | Resource, err := PONRMgr.GetResource(ctx, Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 818 | if err != nil { |
Matteo Scandolo | b85b2f0 | 2021-03-18 14:44:41 -0700 | [diff] [blame] | 819 | logger.Error(ctx, err.Error()) |
| 820 | return err |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 821 | } |
| 822 | for _, Val := range ReleaseContent { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 823 | PONRMgr.ReleaseID(ctx, Resource, Val) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 824 | } |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 825 | if PONRMgr.UpdateResource(ctx, Path, Resource) != nil { |
Matteo Scandolo | b85b2f0 | 2021-03-18 14:44:41 -0700 | [diff] [blame] | 826 | err := fmt.Errorf("Free resource for %s failed", Path) |
| 827 | logger.Errorf(ctx, err.Error()) |
| 828 | return err |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 829 | } |
Matteo Scandolo | b85b2f0 | 2021-03-18 14:44:41 -0700 | [diff] [blame] | 830 | return nil |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 831 | } |
| 832 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 833 | 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] | 834 | /* |
| 835 | Update resource in resource kv store. |
| 836 | :param path: path to update resource |
| 837 | :param resource: resource need to be updated |
| 838 | :return boolean: True if resource updated in kv store else False |
| 839 | */ |
| 840 | // TODO resource[POOL] = resource[POOL].bin |
| 841 | Value, err := json.Marshal(Resource) |
| 842 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 843 | logger.Error(ctx, "failed to Marshal") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 844 | return err |
| 845 | } |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 846 | err = PONRMgr.KVStore.Put(ctx, Path, Value) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 847 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 848 | logger.Error(ctx, "failed to put data to kv store %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 849 | return err |
| 850 | } |
| 851 | return nil |
| 852 | } |
| 853 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 854 | func (PONRMgr *PONResourceManager) ClearResourceIDPool(ctx context.Context, contIntfID uint32, ResourceType string) bool { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 855 | /* |
| 856 | Clear Resource Pool for a given Resource Type on a given PON Port. |
| 857 | :return boolean: True if removed else False |
| 858 | */ |
| 859 | |
| 860 | // delegate to the master instance if sharing enabled across instances |
| 861 | SharedResourceMgr := PONRMgr.SharedResourceMgrs[PONRMgr.SharedIdxByType[ResourceType]] |
| 862 | if SharedResourceMgr != nil && PONRMgr != SharedResourceMgr { |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 863 | return SharedResourceMgr.ClearResourceIDPool(ctx, contIntfID, ResourceType) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 864 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 865 | Path := PONRMgr.GetPath(ctx, contIntfID, ResourceType) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 866 | if Path == "" { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 867 | logger.Error(ctx, "Failed to get path") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 868 | return false |
| 869 | } |
| 870 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 871 | if err := PONRMgr.KVStore.Delete(ctx, Path); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 872 | logger.Errorf(ctx, "Failed to delete resource %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 873 | return false |
| 874 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 875 | logger.Debugf(ctx, "Cleared resource %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 876 | return true |
| 877 | } |
| 878 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 879 | func (PONRMgr PONResourceManager) InitResourceMap(ctx context.Context, PONIntfONUID string) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 880 | /* |
| 881 | Initialize resource map |
| 882 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 883 | */ |
| 884 | // initialize pon_intf_onu_id tuple to alloc_ids map |
| 885 | AllocIDPath := fmt.Sprintf(ALLOC_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, PONIntfONUID) |
| 886 | var AllocIDs []byte |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 887 | Result := PONRMgr.KVStore.Put(ctx, AllocIDPath, AllocIDs) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 888 | if Result != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 889 | logger.Error(ctx, "Failed to update the KV store") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 890 | return |
| 891 | } |
| 892 | // initialize pon_intf_onu_id tuple to gemport_ids map |
| 893 | GEMPortIDPath := fmt.Sprintf(GEMPORT_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, PONIntfONUID) |
| 894 | var GEMPortIDs []byte |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 895 | Result = PONRMgr.KVStore.Put(ctx, GEMPortIDPath, GEMPortIDs) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 896 | if Result != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 897 | logger.Error(ctx, "Failed to update the KV store") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 898 | return |
| 899 | } |
| 900 | } |
| 901 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 902 | func (PONRMgr PONResourceManager) RemoveResourceMap(ctx context.Context, PONIntfONUID string) bool { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 903 | /* |
| 904 | Remove resource map |
| 905 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 906 | */ |
| 907 | // remove pon_intf_onu_id tuple to alloc_ids map |
| 908 | var err error |
| 909 | AllocIDPath := fmt.Sprintf(ALLOC_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, PONIntfONUID) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 910 | if err = PONRMgr.KVStore.Delete(ctx, AllocIDPath); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 911 | logger.Errorf(ctx, "Failed to remove resource %s", AllocIDPath) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 912 | return false |
| 913 | } |
| 914 | // remove pon_intf_onu_id tuple to gemport_ids map |
| 915 | GEMPortIDPath := fmt.Sprintf(GEMPORT_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, PONIntfONUID) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 916 | err = PONRMgr.KVStore.Delete(ctx, GEMPortIDPath) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 917 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 918 | logger.Errorf(ctx, "Failed to remove resource %s", GEMPortIDPath) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 919 | return false |
| 920 | } |
| 921 | |
| 922 | FlowIDPath := fmt.Sprintf(FLOW_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, PONIntfONUID) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 923 | if FlowIDs, err := PONRMgr.KVStore.List(ctx, FlowIDPath); err != nil { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 924 | for _, Flow := range FlowIDs { |
| 925 | FlowIDInfoPath := fmt.Sprintf(FLOW_ID_INFO_PATH, PONRMgr.DeviceID, PONIntfONUID, Flow.Value) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 926 | if err = PONRMgr.KVStore.Delete(ctx, FlowIDInfoPath); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 927 | logger.Errorf(ctx, "Failed to remove resource %s", FlowIDInfoPath) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 928 | return false |
| 929 | } |
| 930 | } |
| 931 | } |
| 932 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 933 | if err = PONRMgr.KVStore.Delete(ctx, FlowIDPath); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 934 | logger.Errorf(ctx, "Failed to remove resource %s", FlowIDPath) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 935 | return false |
| 936 | } |
| 937 | |
| 938 | return true |
| 939 | } |
| 940 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 941 | func (PONRMgr *PONResourceManager) GetCurrentAllocIDForOnu(ctx context.Context, IntfONUID string) []uint32 { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 942 | /* |
| 943 | Get currently configured alloc ids for given pon_intf_onu_id |
| 944 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 945 | :return list: List of alloc_ids if available, else None |
| 946 | */ |
| 947 | Path := fmt.Sprintf(ALLOC_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID) |
| 948 | |
| 949 | var Data []uint32 |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 950 | Value, err := PONRMgr.KVStore.Get(ctx, Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 951 | if err == nil { |
| 952 | if Value != nil { |
| 953 | Val, err := ToByte(Value.Value) |
| 954 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 955 | logger.Errorw(ctx, "Failed to convert into byte array", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 956 | return Data |
| 957 | } |
| 958 | if err = json.Unmarshal(Val, &Data); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 959 | logger.Error(ctx, "Failed to unmarshal", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 960 | return Data |
| 961 | } |
| 962 | } |
| 963 | } |
| 964 | return Data |
| 965 | } |
| 966 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 967 | func (PONRMgr *PONResourceManager) GetCurrentGEMPortIDsForOnu(ctx context.Context, IntfONUID string) []uint32 { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 968 | /* |
| 969 | Get currently configured gemport ids for given pon_intf_onu_id |
| 970 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 971 | :return list: List of gemport IDs if available, else None |
| 972 | */ |
| 973 | |
| 974 | Path := fmt.Sprintf(GEMPORT_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID) |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 975 | logger.Debugf(ctx, "Getting current gemports for %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 976 | var Data []uint32 |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 977 | Value, err := PONRMgr.KVStore.Get(ctx, Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 978 | if err == nil { |
| 979 | if Value != nil { |
| 980 | Val, _ := ToByte(Value.Value) |
| 981 | if err = json.Unmarshal(Val, &Data); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 982 | logger.Errorw(ctx, "Failed to unmarshal", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 983 | return Data |
| 984 | } |
| 985 | } |
| 986 | } else { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 987 | logger.Errorf(ctx, "Failed to get data from kvstore for %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 988 | } |
| 989 | return Data |
| 990 | } |
| 991 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 992 | func (PONRMgr *PONResourceManager) GetCurrentFlowIDsForOnu(ctx context.Context, IntfONUID string) []uint32 { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 993 | /* |
| 994 | Get currently configured flow ids for given pon_intf_onu_id |
| 995 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 996 | :return list: List of Flow IDs if available, else None |
| 997 | */ |
| 998 | |
| 999 | Path := fmt.Sprintf(FLOW_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID) |
| 1000 | |
| 1001 | var Data []uint32 |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1002 | Value, err := PONRMgr.KVStore.Get(ctx, Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1003 | if err == nil { |
| 1004 | if Value != nil { |
| 1005 | Val, _ := ToByte(Value.Value) |
| 1006 | if err = json.Unmarshal(Val, &Data); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1007 | logger.Error(ctx, "Failed to unmarshal") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1008 | return Data |
| 1009 | } |
| 1010 | } |
| 1011 | } |
| 1012 | return Data |
| 1013 | } |
| 1014 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1015 | 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] | 1016 | /* |
| 1017 | Get flow details configured for the ONU. |
| 1018 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 1019 | :param flow_id: Flow Id reference |
| 1020 | :param Data: Result |
| 1021 | :return error: nil if no error in getting from KV store |
| 1022 | */ |
| 1023 | |
| 1024 | Path := fmt.Sprintf(FLOW_ID_INFO_PATH, PONRMgr.DeviceID, IntfONUID, FlowID) |
| 1025 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1026 | Value, err := PONRMgr.KVStore.Get(ctx, Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1027 | if err == nil { |
| 1028 | if Value != nil { |
| 1029 | Val, err := ToByte(Value.Value) |
| 1030 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1031 | logger.Errorw(ctx, "Failed to convert flowinfo into byte array", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1032 | return err |
| 1033 | } |
| 1034 | if err = json.Unmarshal(Val, Data); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1035 | logger.Errorw(ctx, "Failed to unmarshal", log.Fields{"error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1036 | return err |
| 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | return err |
| 1041 | } |
| 1042 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1043 | func (PONRMgr *PONResourceManager) RemoveFlowIDInfo(ctx context.Context, IntfONUID string, FlowID uint32) bool { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1044 | /* |
| 1045 | Get flow_id details configured for the ONU. |
| 1046 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 1047 | :param flow_id: Flow Id reference |
| 1048 | */ |
| 1049 | Path := fmt.Sprintf(FLOW_ID_INFO_PATH, PONRMgr.DeviceID, IntfONUID, FlowID) |
| 1050 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1051 | if err := PONRMgr.KVStore.Delete(ctx, Path); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1052 | logger.Errorf(ctx, "Falied to remove resource %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1053 | return false |
| 1054 | } |
| 1055 | return true |
| 1056 | } |
| 1057 | |
Serkant Uluderya | 198de90 | 2020-11-16 20:29:17 +0300 | [diff] [blame] | 1058 | func (PONRMgr *PONResourceManager) RemoveAllFlowIDInfo(ctx context.Context, IntfONUID string) bool { |
| 1059 | /* |
| 1060 | Remove flow_id_info details configured for the ONU. |
| 1061 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 1062 | */ |
| 1063 | Path := fmt.Sprintf(FLOW_ID_INFO_PATH_INTF_ONU_PREFIX, PONRMgr.DeviceID, IntfONUID) |
| 1064 | |
| 1065 | if err := PONRMgr.KVStore.DeleteWithPrefix(ctx, Path); err != nil { |
| 1066 | logger.Errorf(ctx, "Falied to remove resource %s", Path) |
| 1067 | return false |
| 1068 | } |
| 1069 | return true |
| 1070 | } |
| 1071 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1072 | func (PONRMgr *PONResourceManager) UpdateAllocIdsForOnu(ctx context.Context, IntfONUID string, AllocIDs []uint32) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1073 | /* |
| 1074 | Update currently configured alloc ids for given pon_intf_onu_id |
| 1075 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 1076 | :param alloc_ids: list of alloc ids |
| 1077 | */ |
| 1078 | var Value []byte |
| 1079 | var err error |
| 1080 | Path := fmt.Sprintf(ALLOC_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID) |
| 1081 | Value, err = json.Marshal(AllocIDs) |
| 1082 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1083 | logger.Error(ctx, "failed to Marshal") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1084 | return err |
| 1085 | } |
| 1086 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1087 | if err = PONRMgr.KVStore.Put(ctx, Path, Value); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1088 | logger.Errorf(ctx, "Failed to update resource %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1089 | return err |
| 1090 | } |
| 1091 | return err |
| 1092 | } |
| 1093 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1094 | func (PONRMgr *PONResourceManager) UpdateGEMPortIDsForOnu(ctx context.Context, IntfONUID string, GEMPortIDs []uint32) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1095 | /* |
| 1096 | Update currently configured gemport ids for given pon_intf_onu_id |
| 1097 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 1098 | :param gemport_ids: list of gem port ids |
| 1099 | */ |
| 1100 | |
| 1101 | var Value []byte |
| 1102 | var err error |
| 1103 | Path := fmt.Sprintf(GEMPORT_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID) |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1104 | logger.Debugf(ctx, "Updating gemport ids for %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1105 | Value, err = json.Marshal(GEMPortIDs) |
| 1106 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1107 | logger.Error(ctx, "failed to Marshal") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1108 | return err |
| 1109 | } |
| 1110 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1111 | if err = PONRMgr.KVStore.Put(ctx, Path, Value); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1112 | logger.Errorf(ctx, "Failed to update resource %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1113 | return err |
| 1114 | } |
| 1115 | return err |
| 1116 | } |
| 1117 | |
| 1118 | func checkForFlowIDInList(FlowIDList []uint32, FlowID uint32) (bool, uint32) { |
| 1119 | /* |
| 1120 | Check for a flow id in a given list of flow IDs. |
| 1121 | :param FLowIDList: List of Flow IDs |
| 1122 | :param FlowID: Flowd to check in the list |
| 1123 | : return true and the index if present false otherwise. |
| 1124 | */ |
| 1125 | |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 1126 | for idx := range FlowIDList { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1127 | if FlowID == FlowIDList[idx] { |
| 1128 | return true, uint32(idx) |
| 1129 | } |
| 1130 | } |
| 1131 | return false, 0 |
| 1132 | } |
| 1133 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1134 | 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] | 1135 | /* |
| 1136 | Update the flow_id list of the ONU (add or remove flow_id from the list) |
| 1137 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 1138 | :param flow_id: flow ID |
| 1139 | :param add: Boolean flag to indicate whether the flow_id should be |
| 1140 | added or removed from the list. Defaults to adding the flow. |
| 1141 | */ |
| 1142 | var Value []byte |
| 1143 | var err error |
| 1144 | var RetVal bool |
| 1145 | var IDx uint32 |
| 1146 | Path := fmt.Sprintf(FLOW_ID_RESOURCE_MAP_PATH, PONRMgr.DeviceID, IntfONUID) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1147 | FlowIDs := PONRMgr.GetCurrentFlowIDsForOnu(ctx, IntfONUID) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1148 | |
| 1149 | if Add { |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 1150 | if RetVal, _ = checkForFlowIDInList(FlowIDs, FlowID); RetVal { |
| 1151 | return nil |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1152 | } |
| 1153 | FlowIDs = append(FlowIDs, FlowID) |
| 1154 | } else { |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 1155 | if RetVal, IDx = checkForFlowIDInList(FlowIDs, FlowID); !RetVal { |
| 1156 | return nil |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1157 | } |
| 1158 | // delete the index and shift |
| 1159 | FlowIDs = append(FlowIDs[:IDx], FlowIDs[IDx+1:]...) |
| 1160 | } |
| 1161 | Value, err = json.Marshal(FlowIDs) |
| 1162 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1163 | logger.Error(ctx, "Failed to Marshal") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1164 | return err |
| 1165 | } |
| 1166 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1167 | if err = PONRMgr.KVStore.Put(ctx, Path, Value); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1168 | logger.Errorf(ctx, "Failed to update resource %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1169 | return err |
| 1170 | } |
| 1171 | return err |
| 1172 | } |
| 1173 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1174 | 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] | 1175 | /* |
| 1176 | Update any metadata associated with the flow_id. The flow_data could be json |
| 1177 | or any of other data structure. The resource manager doesnt care |
| 1178 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 1179 | :param flow_id: Flow ID |
| 1180 | :param flow_data: Flow data blob |
| 1181 | */ |
| 1182 | var Value []byte |
| 1183 | var err error |
| 1184 | Path := fmt.Sprintf(FLOW_ID_INFO_PATH, PONRMgr.DeviceID, IntfONUID, FlowID) |
| 1185 | Value, err = json.Marshal(FlowData) |
| 1186 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1187 | logger.Error(ctx, "failed to Marshal") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1188 | return err |
| 1189 | } |
| 1190 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1191 | if err = PONRMgr.KVStore.Put(ctx, Path, Value); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1192 | logger.Errorf(ctx, "Failed to update resource %s", Path) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1193 | return err |
| 1194 | } |
| 1195 | return err |
| 1196 | } |
| 1197 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1198 | func (PONRMgr *PONResourceManager) GenerateNextID(ctx context.Context, Resource map[string]interface{}) (uint32, error) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1199 | /* |
| 1200 | Generate unique id having OFFSET as start |
| 1201 | :param resource: resource used to generate ID |
| 1202 | :return uint32: generated id |
| 1203 | */ |
| 1204 | ByteArray, err := ToByte(Resource[POOL]) |
| 1205 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1206 | logger.Error(ctx, "Failed to convert resource to byte array") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1207 | return 0, err |
| 1208 | } |
| 1209 | Data := bitmap.TSFromData(ByteArray, false) |
| 1210 | if Data == nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1211 | logger.Error(ctx, "Failed to get data from byte array") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1212 | return 0, errors.New("Failed to get data from byte array") |
| 1213 | } |
| 1214 | |
| 1215 | Len := Data.Len() |
| 1216 | var Idx int |
| 1217 | for Idx = 0; Idx < Len; Idx++ { |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 1218 | if !Data.Get(Idx) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1219 | break |
| 1220 | } |
| 1221 | } |
| 1222 | Data.Set(Idx, true) |
| 1223 | res := uint32(Resource[START_IDX].(float64)) |
| 1224 | Resource[POOL] = Data.Data(false) |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1225 | logger.Debugf(ctx, "Generated ID for %d", (uint32(Idx) + res)) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1226 | return (uint32(Idx) + res), err |
| 1227 | } |
| 1228 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1229 | func (PONRMgr *PONResourceManager) ReleaseID(ctx context.Context, Resource map[string]interface{}, Id uint32) bool { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1230 | /* |
| 1231 | Release unique id having OFFSET as start index. |
| 1232 | :param resource: resource used to release ID |
| 1233 | :param unique_id: id need to be released |
| 1234 | */ |
| 1235 | ByteArray, err := ToByte(Resource[POOL]) |
| 1236 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1237 | logger.Error(ctx, "Failed to convert resource to byte array") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1238 | return false |
| 1239 | } |
| 1240 | Data := bitmap.TSFromData(ByteArray, false) |
| 1241 | if Data == nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1242 | logger.Error(ctx, "Failed to get resource pool") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1243 | return false |
| 1244 | } |
David K. Bainbridge | 7c75cac | 2020-02-19 08:53:46 -0800 | [diff] [blame] | 1245 | Idx := Id - uint32(Resource[START_IDX].(float64)) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1246 | Data.Set(int(Idx), false) |
| 1247 | Resource[POOL] = Data.Data(false) |
| 1248 | |
| 1249 | return true |
| 1250 | } |
| 1251 | |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 1252 | /* Reserves a unique id in the specified resource pool. |
| 1253 | :param Resource: resource used to reserve ID |
| 1254 | :param Id: ID to be reserved |
| 1255 | */ |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1256 | func (PONRMgr *PONResourceManager) reserveID(ctx context.Context, TSData *bitmap.Threadsafe, StartIndex uint32, Id uint32) bool { |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 1257 | Data := bitmap.TSFromData(TSData.Data(false), false) |
| 1258 | if Data == nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1259 | logger.Error(ctx, "Failed to get resource pool") |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 1260 | return false |
| 1261 | } |
| 1262 | Idx := Id - StartIndex |
| 1263 | Data.Set(int(Idx), true) |
| 1264 | return true |
| 1265 | } |
| 1266 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1267 | func (PONRMgr *PONResourceManager) GetTechnology() string { |
| 1268 | return PONRMgr.Technology |
| 1269 | } |
| 1270 | |
| 1271 | func (PONRMgr *PONResourceManager) GetResourceTypeAllocID() string { |
| 1272 | return ALLOC_ID |
| 1273 | } |
| 1274 | |
| 1275 | func (PONRMgr *PONResourceManager) GetResourceTypeGemPortID() string { |
| 1276 | return GEMPORT_ID |
| 1277 | } |
| 1278 | |
Matteo Scandolo | b85b2f0 | 2021-03-18 14:44:41 -0700 | [diff] [blame] | 1279 | func (PONRMgr *PONResourceManager) GetResourceTypeOnuID() string { |
| 1280 | return ONU_ID |
| 1281 | } |
| 1282 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1283 | // ToByte converts an interface value to a []byte. The interface should either be of |
| 1284 | // a string type or []byte. Otherwise, an error is returned. |
| 1285 | func ToByte(value interface{}) ([]byte, error) { |
| 1286 | switch t := value.(type) { |
| 1287 | case []byte: |
| 1288 | return value.([]byte), nil |
| 1289 | case string: |
| 1290 | return []byte(value.(string)), nil |
| 1291 | default: |
| 1292 | return nil, fmt.Errorf("unexpected-type-%T", t) |
| 1293 | } |
| 1294 | } |
| 1295 | |
| 1296 | // ToString converts an interface value to a string. The interface should either be of |
| 1297 | // a string type or []byte. Otherwise, an error is returned. |
| 1298 | func ToString(value interface{}) (string, error) { |
| 1299 | switch t := value.(type) { |
| 1300 | case []byte: |
| 1301 | return string(value.([]byte)), nil |
| 1302 | case string: |
| 1303 | return value.(string), nil |
| 1304 | default: |
| 1305 | return "", fmt.Errorf("unexpected-type-%T", t) |
| 1306 | } |
| 1307 | } |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1308 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1309 | func (PONRMgr *PONResourceManager) AddOnuGemInfo(ctx context.Context, intfID uint32, onuGemData interface{}) error { |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1310 | /* |
| 1311 | Update onugem info map, |
| 1312 | :param pon_intf_id: reference of PON interface id |
| 1313 | :param onuegmdata: onugem info map |
| 1314 | */ |
| 1315 | var Value []byte |
| 1316 | var err error |
| 1317 | Path := fmt.Sprintf(ONU_GEM_INFO_PATH, PONRMgr.DeviceID, intfID) |
| 1318 | Value, err = json.Marshal(onuGemData) |
| 1319 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1320 | logger.Error(ctx, "failed to Marshal") |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1321 | return err |
| 1322 | } |
| 1323 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1324 | if err = PONRMgr.KVStore.Put(ctx, Path, Value); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1325 | logger.Errorf(ctx, "Failed to update resource %s", Path) |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1326 | return err |
| 1327 | } |
| 1328 | return err |
| 1329 | } |
| 1330 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1331 | func (PONRMgr *PONResourceManager) GetOnuGemInfo(ctx context.Context, IntfId uint32, onuGemInfo interface{}) error { |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1332 | /* |
| 1333 | Get onugeminfo map from kvstore |
| 1334 | :param intfid: refremce pon intfid |
| 1335 | :param onuGemInfo: onugem info to return from kv strore. |
| 1336 | */ |
| 1337 | var Val []byte |
| 1338 | |
| 1339 | path := fmt.Sprintf(ONU_GEM_INFO_PATH, PONRMgr.DeviceID, IntfId) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1340 | value, err := PONRMgr.KVStore.Get(ctx, path) |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1341 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1342 | logger.Errorw(ctx, "Failed to get from kv store", log.Fields{"path": path}) |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1343 | return err |
| 1344 | } else if value == nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1345 | logger.Debug(ctx, "No onuinfo for path", log.Fields{"path": path}) |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1346 | return nil // returning nil as this could happen if there are no onus for the interface yet |
| 1347 | } |
| 1348 | if Val, err = kvstore.ToByte(value.Value); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1349 | logger.Error(ctx, "Failed to convert to byte array") |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1350 | return err |
| 1351 | } |
| 1352 | |
| 1353 | if err = json.Unmarshal(Val, &onuGemInfo); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1354 | logger.Error(ctx, "Failed to unmarshall") |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1355 | return err |
| 1356 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1357 | logger.Debugw(ctx, "found onuinfo from path", log.Fields{"path": path, "onuinfo": onuGemInfo}) |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1358 | return err |
| 1359 | } |
| 1360 | |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1361 | func (PONRMgr *PONResourceManager) DelOnuGemInfoForIntf(ctx context.Context, intfId uint32) error { |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1362 | /* |
| 1363 | delete onugem info for an interface from kvstore |
| 1364 | :param intfid: refremce pon intfid |
| 1365 | */ |
| 1366 | |
| 1367 | path := fmt.Sprintf(ONU_GEM_INFO_PATH, PONRMgr.DeviceID, intfId) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 1368 | if err := PONRMgr.KVStore.Delete(ctx, path); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 1369 | logger.Errorf(ctx, "Falied to remove resource %s", path) |
Abhilash Laxmeshwar | bbb0b2b | 2019-10-31 17:04:29 +0530 | [diff] [blame] | 1370 | return err |
| 1371 | } |
| 1372 | return nil |
| 1373 | } |