Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2018 the original author or authors. |
| 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 | """ |
| 18 | Resource Manager will be unique for each OLT device. |
| 19 | |
| 20 | It exposes APIs to create/free alloc_ids/onu_ids/gemport_ids. Resource Manager |
| 21 | uses a KV store in backend to ensure resiliency of the data. |
| 22 | """ |
| 23 | import json |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 24 | import ast |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 25 | import structlog |
| 26 | from bitstring import BitArray |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 27 | import shlex |
| 28 | from argparse import ArgumentParser, ArgumentError |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 29 | |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 30 | from common.pon_resource_manager.resource_kv_store import ResourceKvStore |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 31 | from common.tech_profile.tech_profile import TechProfile |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 32 | |
| 33 | |
| 34 | # Used to parse extra arguments to OpenOlt adapter from the NBI |
| 35 | class OltVendorArgumentParser(ArgumentParser): |
| 36 | # Must override the exit command to prevent it from |
| 37 | # calling sys.exit(). Return exception instead. |
| 38 | def exit(self, status=0, message=None): |
| 39 | raise Exception(message) |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 40 | |
| 41 | |
| 42 | class PONResourceManager(object): |
| 43 | """Implements APIs to initialize/allocate/release alloc/gemport/onu IDs.""" |
| 44 | |
| 45 | # Constants to identify resource pool |
Craig Lutgen | abd9c84 | 2018-11-15 23:58:27 +0000 | [diff] [blame] | 46 | UNI_ID = 'UNI_ID' |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 47 | ONU_ID = 'ONU_ID' |
| 48 | ALLOC_ID = 'ALLOC_ID' |
| 49 | GEMPORT_ID = 'GEMPORT_ID' |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 50 | FLOW_ID = 'FLOW_ID' |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 51 | |
Craig Lutgen | f760147 | 2018-11-02 13:27:41 +0000 | [diff] [blame] | 52 | # Constants for passing command line arugments |
| 53 | OLT_MODEL_ARG = '--olt_model' |
| 54 | |
| 55 | # The resource ranges for a given device model should be placed |
| 56 | # at 'resource_manager/<technology>/resource_ranges/<olt_model_type>' |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 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 | ''' |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 62 | { |
| 63 | "onu_id_start": 1, |
| 64 | "onu_id_end": 127, |
| 65 | "alloc_id_start": 1024, |
| 66 | "alloc_id_end": 2816, |
| 67 | "gemport_id_start": 1024, |
| 68 | "gemport_id_end": 8960, |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 69 | "flow_id_start": 1, |
| 70 | "flow_id_end": 16383, |
Craig Lutgen | abd9c84 | 2018-11-15 23:58:27 +0000 | [diff] [blame] | 71 | "uni_id_start": 0, |
| 72 | "uni_id_end": 0, |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 73 | "pon_ports": 16 |
| 74 | } |
| 75 | |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 76 | ''' |
| 77 | # constants used as keys to reference the resource range parameters from |
| 78 | # and external KV store. |
Craig Lutgen | abd9c84 | 2018-11-15 23:58:27 +0000 | [diff] [blame] | 79 | UNI_ID_START_IDX = "uni_id_start" |
| 80 | UNI_ID_END_IDX = "uni_id_end" |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 81 | ONU_ID_START_IDX = "onu_id_start" |
| 82 | ONU_ID_END_IDX = "onu_id_end" |
| 83 | ONU_ID_SHARED_IDX = "onu_id_shared" |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 84 | ALLOC_ID_START_IDX = "alloc_id_start" |
| 85 | ALLOC_ID_END_IDX = "alloc_id_end" |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 86 | ALLOC_ID_SHARED_IDX = "alloc_id_shared" |
| 87 | GEMPORT_ID_START_IDX = "gemport_id_start" |
| 88 | GEMPORT_ID_END_IDX = "gemport_id_end" |
| 89 | GEMPORT_ID_SHARED_IDX = "gemport_id_shared" |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 90 | FLOW_ID_START_IDX = "flow_id_start" |
| 91 | FLOW_ID_END_IDX = "flow_id_end" |
| 92 | FLOW_ID_SHARED_IDX = "flow_id_shared" |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 93 | NUM_OF_PON_PORT = "pon_ports" |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 94 | |
| 95 | # PON Resource range configuration on the KV store. |
Craig Lutgen | f760147 | 2018-11-02 13:27:41 +0000 | [diff] [blame] | 96 | # Format: 'resource_manager/<technology>/resource_ranges/<olt_model_type>' |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 97 | # The KV store backend is initialized with a path prefix and we need to |
| 98 | # provide only the suffix. |
| 99 | PON_RESOURCE_RANGE_CONFIG_PATH = 'resource_ranges/{}' |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 100 | |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 101 | # resource path suffix |
| 102 | ALLOC_ID_POOL_PATH = '{}/alloc_id_pool/{}' |
| 103 | GEMPORT_ID_POOL_PATH = '{}/gemport_id_pool/{}' |
| 104 | ONU_ID_POOL_PATH = '{}/onu_id_pool/{}' |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 105 | FLOW_ID_POOL_PATH = '{}/flow_id_pool/{}' |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 106 | |
| 107 | # Path on the KV store for storing list of alloc IDs for a given ONU |
| 108 | # Format: <device_id>/<(pon_intf_id, onu_id)>/alloc_ids |
| 109 | ALLOC_ID_RESOURCE_MAP_PATH = '{}/{}/alloc_ids' |
| 110 | |
| 111 | # Path on the KV store for storing list of gemport IDs for a given ONU |
| 112 | # Format: <device_id>/<(pon_intf_id, onu_id)>/gemport_ids |
| 113 | GEMPORT_ID_RESOURCE_MAP_PATH = '{}/{}/gemport_ids' |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 114 | |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 115 | # Path on the KV store for storing list of Flow IDs for a given ONU |
| 116 | # Format: <device_id>/<(pon_intf_id, onu_id)>/flow_ids |
| 117 | FLOW_ID_RESOURCE_MAP_PATH = '{}/{}/flow_ids' |
| 118 | |
| 119 | # Flow Id info: Use to store more metadata associated with the flow_id |
| 120 | # Format: <device_id>/<(pon_intf_id, onu_id)>/flow_id_info/<flow_id> |
| 121 | FLOW_ID_INFO_PATH = '{}/{}/flow_id_info/{}' |
| 122 | |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 123 | # Constants for internal usage. |
| 124 | PON_INTF_ID = 'pon_intf_id' |
| 125 | START_IDX = 'start_idx' |
| 126 | END_IDX = 'end_idx' |
| 127 | POOL = 'pool' |
| 128 | |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 129 | def __init__(self, technology, extra_args, device_id, |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 130 | backend, host, port): |
| 131 | """ |
| 132 | Create PONResourceManager object. |
| 133 | |
| 134 | :param technology: PON technology |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 135 | :param: extra_args: This string contains extra arguments passed during |
| 136 | pre-provisioning of OLT and specifies the OLT Vendor type |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 137 | :param device_id: OLT device id |
| 138 | :param backend: backend store |
| 139 | :param host: ip of backend store |
| 140 | :param port: port on which backend store listens |
| 141 | :raises exception when invalid backend store passed as an argument |
| 142 | """ |
| 143 | # logger |
| 144 | self._log = structlog.get_logger() |
| 145 | |
| 146 | try: |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 147 | self.technology = technology |
Craig Lutgen | f760147 | 2018-11-02 13:27:41 +0000 | [diff] [blame] | 148 | self.extra_args = extra_args |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 149 | self.device_id = device_id |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 150 | self.backend = backend |
| 151 | self.host = host |
| 152 | self.port = port |
Craig Lutgen | f760147 | 2018-11-02 13:27:41 +0000 | [diff] [blame] | 153 | self.olt_model = None |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 154 | |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 155 | self._kv_store = ResourceKvStore(technology, device_id, backend, |
| 156 | host, port) |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 157 | self.tech_profile = TechProfile(self) |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 158 | |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 159 | # Below attribute, pon_resource_ranges, should be initialized |
| 160 | # by reading from KV store. |
| 161 | self.pon_resource_ranges = dict() |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 162 | self.pon_resource_ranges[PONResourceManager.ONU_ID_SHARED_IDX] = None |
| 163 | self.pon_resource_ranges[PONResourceManager.ALLOC_ID_SHARED_IDX] = None |
| 164 | self.pon_resource_ranges[PONResourceManager.GEMPORT_ID_SHARED_IDX] = None |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 165 | self.pon_resource_ranges[PONResourceManager.FLOW_ID_SHARED_IDX] = None |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 166 | |
| 167 | self.shared_resource_mgrs = dict() |
| 168 | self.shared_resource_mgrs[PONResourceManager.ONU_ID_SHARED_IDX] = None |
| 169 | self.shared_resource_mgrs[PONResourceManager.ALLOC_ID_SHARED_IDX] = None |
| 170 | self.shared_resource_mgrs[PONResourceManager.GEMPORT_ID_SHARED_IDX] = None |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 171 | self.shared_resource_mgrs[PONResourceManager.FLOW_ID_SHARED_IDX] = None |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 172 | |
| 173 | self.shared_idx_by_type = dict() |
| 174 | self.shared_idx_by_type[PONResourceManager.ONU_ID] = PONResourceManager.ONU_ID_SHARED_IDX |
| 175 | self.shared_idx_by_type[PONResourceManager.ALLOC_ID] = PONResourceManager.ALLOC_ID_SHARED_IDX |
| 176 | self.shared_idx_by_type[PONResourceManager.GEMPORT_ID] = PONResourceManager.GEMPORT_ID_SHARED_IDX |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 177 | self.shared_idx_by_type[PONResourceManager.FLOW_ID] = PONResourceManager.FLOW_ID_SHARED_IDX |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 178 | |
Craig Lutgen | 65423ce | 2018-10-29 23:24:21 -0500 | [diff] [blame] | 179 | self.intf_ids = None |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 180 | |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 181 | except Exception as e: |
| 182 | self._log.exception("exception-in-init") |
| 183 | raise Exception(e) |
| 184 | |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 185 | def init_resource_ranges_from_kv_store(self): |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 186 | """ |
| 187 | Initialize PON resource ranges with config fetched from kv store. |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 188 | |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 189 | :return boolean: True if PON resource ranges initialized else false |
| 190 | """ |
Craig Lutgen | f760147 | 2018-11-02 13:27:41 +0000 | [diff] [blame] | 191 | self.olt_model = self._get_olt_model() |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 192 | # Try to initialize the PON Resource Ranges from KV store based on the |
Craig Lutgen | f760147 | 2018-11-02 13:27:41 +0000 | [diff] [blame] | 193 | # OLT model key, if available |
| 194 | if self.olt_model is None: |
| 195 | self._log.info("device-model-unavailable--not-reading-from-kv-store") |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 196 | return False |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 197 | |
Craig Lutgen | f760147 | 2018-11-02 13:27:41 +0000 | [diff] [blame] | 198 | path = self.PON_RESOURCE_RANGE_CONFIG_PATH.format(self.olt_model) |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 199 | try: |
| 200 | # get resource from kv store |
| 201 | result = self._kv_store.get_from_kv_store(path) |
| 202 | |
| 203 | if result is None: |
| 204 | self._log.debug("resource-range-config-unavailable-on-kvstore") |
| 205 | return False |
| 206 | |
| 207 | resource_range_config = result |
| 208 | |
| 209 | if resource_range_config is not None: |
Craig Lutgen | abd9c84 | 2018-11-15 23:58:27 +0000 | [diff] [blame] | 210 | # update internal ranges from kv ranges. If there are missing |
| 211 | # values in the KV profile, continue to use the defaults |
| 212 | for key,value in json.loads(resource_range_config): self.pon_resource_ranges[key] = value |
| 213 | |
| 214 | # initialize optional elements that may not be in the profile |
| 215 | if self.pon_resource_ranges[PONResourceManager.UNI_ID_START_IDX] is None: |
| 216 | self.pon_resource_ranges[PONResourceManager.UNI_ID_START_IDX] = 0 |
| 217 | if self.pon_resource_ranges[PONResourceManager.UNI_ID_END_IDX] is None: |
| 218 | self.pon_resource_ranges[PONResourceManager.UNI_ID_END_IDX] = 0 |
| 219 | |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 220 | self._log.debug("Init-resource-ranges-from-kvstore-success", |
| 221 | pon_resource_ranges=self.pon_resource_ranges, |
| 222 | path=path) |
| 223 | return True |
| 224 | |
| 225 | except Exception as e: |
| 226 | self._log.exception("error-initializing-resource-range-from-kv-store", |
| 227 | e=e) |
| 228 | return False |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 229 | |
Craig Lutgen | abd9c84 | 2018-11-15 23:58:27 +0000 | [diff] [blame] | 230 | def update_range_(self, start_idx, start, end_idx, end, shared_idx = None, shared_pool_id = None, |
| 231 | shared_resource_mgr = None): |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 232 | if (start is not None) and \ |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 233 | (start_idx not in self.pon_resource_ranges or self.pon_resource_ranges[start_idx] < start): |
| 234 | self.pon_resource_ranges[start_idx] = start |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 235 | if (end is not None) and \ |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 236 | (end_idx not in self.pon_resource_ranges or self.pon_resource_ranges[end_idx] > end): |
| 237 | self.pon_resource_ranges[end_idx] = end |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 238 | if (shared_pool_id is not None) and \ |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 239 | (shared_idx not in self.pon_resource_ranges or self.pon_resource_ranges[shared_idx] is None): |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 240 | self.pon_resource_ranges[shared_idx] = shared_pool_id |
| 241 | if (shared_resource_mgr is not None) and \ |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 242 | (shared_idx not in self.shared_resource_mgrs or self.shared_resource_mgrs[shared_idx] is None): |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 243 | self.shared_resource_mgrs[shared_idx] = shared_resource_mgr |
| 244 | |
| 245 | def update_ranges(self, |
| 246 | onu_id_start_idx=None, |
| 247 | onu_id_end_idx=None, |
| 248 | onu_id_shared_pool_id=None, |
| 249 | onu_id_shared_resource_mgr=None, |
| 250 | alloc_id_start_idx=None, |
| 251 | alloc_id_end_idx=None, |
| 252 | alloc_id_shared_pool_id=None, |
| 253 | alloc_id_shared_resource_mgr=None, |
| 254 | gemport_id_start_idx=None, |
| 255 | gemport_id_end_idx=None, |
| 256 | gemport_id_shared_pool_id=None, |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 257 | gemport_id_shared_resource_mgr=None, |
| 258 | flow_id_start_idx=None, |
| 259 | flow_id_end_idx=None, |
| 260 | flow_id_shared_pool_id=None, |
Craig Lutgen | abd9c84 | 2018-11-15 23:58:27 +0000 | [diff] [blame] | 261 | flow_id_shared_resource_mgr=None, |
| 262 | uni_id_start_idx=None, |
| 263 | uni_id_end_idx=None): |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 264 | |
| 265 | self.update_range_(PONResourceManager.ONU_ID_START_IDX, onu_id_start_idx, |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 266 | PONResourceManager.ONU_ID_END_IDX, onu_id_end_idx, |
| 267 | PONResourceManager.ONU_ID_SHARED_IDX, onu_id_shared_pool_id, |
| 268 | onu_id_shared_resource_mgr) |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 269 | |
| 270 | self.update_range_(PONResourceManager.ALLOC_ID_START_IDX, alloc_id_start_idx, |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 271 | PONResourceManager.ALLOC_ID_END_IDX, alloc_id_end_idx, |
| 272 | PONResourceManager.ALLOC_ID_SHARED_IDX, alloc_id_shared_pool_id, |
| 273 | alloc_id_shared_resource_mgr) |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 274 | |
| 275 | self.update_range_(PONResourceManager.GEMPORT_ID_START_IDX, gemport_id_start_idx, |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 276 | PONResourceManager.GEMPORT_ID_END_IDX, gemport_id_end_idx, |
| 277 | PONResourceManager.GEMPORT_ID_SHARED_IDX, gemport_id_shared_pool_id, |
| 278 | gemport_id_shared_resource_mgr) |
| 279 | |
| 280 | self.update_range_(PONResourceManager.FLOW_ID_START_IDX, flow_id_start_idx, |
| 281 | PONResourceManager.FLOW_ID_END_IDX, flow_id_end_idx, |
| 282 | PONResourceManager.FLOW_ID_SHARED_IDX, flow_id_shared_pool_id, |
| 283 | flow_id_shared_resource_mgr) |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 284 | |
Craig Lutgen | abd9c84 | 2018-11-15 23:58:27 +0000 | [diff] [blame] | 285 | self.update_range_(PONResourceManager.UNI_ID_START_IDX, uni_id_start_idx, |
| 286 | PONResourceManager.UNI_ID_END_IDX, uni_id_end_idx) |
| 287 | |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 288 | def init_default_pon_resource_ranges(self, |
| 289 | onu_id_start_idx=1, |
| 290 | onu_id_end_idx=127, |
| 291 | onu_id_shared_pool_id=None, |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 292 | alloc_id_start_idx=1024, |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 293 | alloc_id_end_idx=2816, |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 294 | alloc_id_shared_pool_id=None, |
| 295 | gemport_id_start_idx=1024, |
| 296 | gemport_id_end_idx=8960, |
| 297 | gemport_id_shared_pool_id=None, |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 298 | flow_id_start_idx=1, |
| 299 | flow_id_end_idx=16383, |
| 300 | flow_id_shared_pool_id=None, |
Craig Lutgen | abd9c84 | 2018-11-15 23:58:27 +0000 | [diff] [blame] | 301 | uni_id_start_idx=0, |
| 302 | uni_id_end_idx=0, |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 303 | num_of_pon_ports=16, |
| 304 | intf_ids=None): |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 305 | """ |
| 306 | Initialize default PON resource ranges |
| 307 | |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 308 | :param onu_id_start_idx: onu id start index |
| 309 | :param onu_id_end_idx: onu id end index |
| 310 | :param onu_id_shared_pool_id: pool idx for id shared by all intfs or None for no sharing |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 311 | :param alloc_id_start_idx: alloc id start index |
| 312 | :param alloc_id_end_idx: alloc id end index |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 313 | :param alloc_id_shared_pool_id: pool idx for alloc id shared by all intfs or None for no sharing |
| 314 | :param gemport_id_start_idx: gemport id start index |
| 315 | :param gemport_id_end_idx: gemport id end index |
| 316 | :param gemport_id_shared_pool_id: pool idx for gemport id shared by all intfs or None for no sharing |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 317 | :param flow_id_start_idx: flow id start index |
| 318 | :param flow_id_end_idx: flow id end index |
| 319 | :param flow_id_shared_pool_id: pool idx for flow id shared by all intfs or None for no sharing |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 320 | :param num_of_pon_ports: number of PON ports |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 321 | :param intf_ids: interfaces serviced by this manager |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 322 | """ |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 323 | self._log.info("initialize-default-resource-range-values") |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 324 | |
| 325 | self.update_ranges(onu_id_start_idx, onu_id_end_idx, onu_id_shared_pool_id, None, |
| 326 | alloc_id_start_idx, alloc_id_end_idx, alloc_id_shared_pool_id, None, |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 327 | gemport_id_start_idx, gemport_id_end_idx, gemport_id_shared_pool_id, None, |
Craig Lutgen | abd9c84 | 2018-11-15 23:58:27 +0000 | [diff] [blame] | 328 | flow_id_start_idx, flow_id_end_idx, flow_id_shared_pool_id, None, |
| 329 | uni_id_start_idx, uni_id_end_idx) |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 330 | |
| 331 | if intf_ids is None: |
| 332 | intf_ids = range(0, num_of_pon_ports) |
| 333 | |
| 334 | self.intf_ids = intf_ids |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 335 | |
| 336 | def init_device_resource_pool(self): |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 337 | """ |
| 338 | Initialize resource pool for all PON ports. |
| 339 | """ |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 340 | |
| 341 | self._log.info("init-device-resource-pool", technology=self.technology, |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 342 | pon_resource_ranges=self.pon_resource_ranges) |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 343 | |
| 344 | for i in self.intf_ids: |
| 345 | shared_pool_id = self.pon_resource_ranges[PONResourceManager.ONU_ID_SHARED_IDX] |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 346 | if shared_pool_id is not None: |
| 347 | i = shared_pool_id |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 348 | self.init_resource_id_pool( |
| 349 | pon_intf_id=i, |
| 350 | resource_type=PONResourceManager.ONU_ID, |
| 351 | start_idx=self.pon_resource_ranges[ |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 352 | PONResourceManager.ONU_ID_START_IDX], |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 353 | end_idx=self.pon_resource_ranges[ |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 354 | PONResourceManager.ONU_ID_END_IDX]) |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 355 | if shared_pool_id is not None: |
| 356 | break |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 357 | |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 358 | for i in self.intf_ids: |
| 359 | shared_pool_id = self.pon_resource_ranges[PONResourceManager.ALLOC_ID_SHARED_IDX] |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 360 | if shared_pool_id is not None: |
| 361 | i = shared_pool_id |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 362 | self.init_resource_id_pool( |
| 363 | pon_intf_id=i, |
| 364 | resource_type=PONResourceManager.ALLOC_ID, |
| 365 | start_idx=self.pon_resource_ranges[ |
| 366 | PONResourceManager.ALLOC_ID_START_IDX], |
| 367 | end_idx=self.pon_resource_ranges[ |
| 368 | PONResourceManager.ALLOC_ID_END_IDX]) |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 369 | if shared_pool_id is not None: |
| 370 | break |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 371 | |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 372 | for i in self.intf_ids: |
| 373 | shared_pool_id = self.pon_resource_ranges[PONResourceManager.GEMPORT_ID_SHARED_IDX] |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 374 | if shared_pool_id is not None: |
| 375 | i = shared_pool_id |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 376 | self.init_resource_id_pool( |
| 377 | pon_intf_id=i, |
| 378 | resource_type=PONResourceManager.GEMPORT_ID, |
| 379 | start_idx=self.pon_resource_ranges[ |
| 380 | PONResourceManager.GEMPORT_ID_START_IDX], |
| 381 | end_idx=self.pon_resource_ranges[ |
| 382 | PONResourceManager.GEMPORT_ID_END_IDX]) |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 383 | if shared_pool_id is not None: |
| 384 | break |
| 385 | |
| 386 | for i in self.intf_ids: |
| 387 | shared_pool_id = self.pon_resource_ranges[PONResourceManager.FLOW_ID_SHARED_IDX] |
| 388 | if shared_pool_id is not None: |
| 389 | i = shared_pool_id |
| 390 | self.init_resource_id_pool( |
| 391 | pon_intf_id=i, |
| 392 | resource_type=PONResourceManager.FLOW_ID, |
| 393 | start_idx=self.pon_resource_ranges[ |
| 394 | PONResourceManager.FLOW_ID_START_IDX], |
| 395 | end_idx=self.pon_resource_ranges[ |
| 396 | PONResourceManager.FLOW_ID_END_IDX]) |
| 397 | if shared_pool_id is not None: |
| 398 | break |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 399 | |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 400 | def clear_device_resource_pool(self): |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 401 | """ |
| 402 | Clear resource pool of all PON ports. |
| 403 | """ |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 404 | for i in self.intf_ids: |
| 405 | shared_pool_id = self.pon_resource_ranges[PONResourceManager.ONU_ID_SHARED_IDX] |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 406 | if shared_pool_id is not None: |
| 407 | i = shared_pool_id |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 408 | self.clear_resource_id_pool( |
| 409 | pon_intf_id=i, |
| 410 | resource_type=PONResourceManager.ONU_ID, |
| 411 | ) |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 412 | if shared_pool_id is not None: |
| 413 | break |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 414 | |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 415 | for i in self.intf_ids: |
| 416 | shared_pool_id = self.pon_resource_ranges[PONResourceManager.ALLOC_ID_SHARED_IDX] |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 417 | if shared_pool_id is not None: |
| 418 | i = shared_pool_id |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 419 | self.clear_resource_id_pool( |
| 420 | pon_intf_id=i, |
| 421 | resource_type=PONResourceManager.ALLOC_ID, |
| 422 | ) |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 423 | if shared_pool_id is not None: |
| 424 | break |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 425 | |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 426 | for i in self.intf_ids: |
| 427 | shared_pool_id = self.pon_resource_ranges[PONResourceManager.GEMPORT_ID_SHARED_IDX] |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 428 | if shared_pool_id is not None: |
| 429 | i = shared_pool_id |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 430 | self.clear_resource_id_pool( |
| 431 | pon_intf_id=i, |
| 432 | resource_type=PONResourceManager.GEMPORT_ID, |
| 433 | ) |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 434 | if shared_pool_id is not None: |
| 435 | break |
| 436 | |
| 437 | for i in self.intf_ids: |
| 438 | shared_pool_id = self.pon_resource_ranges[PONResourceManager.FLOW_ID_SHARED_IDX] |
| 439 | if shared_pool_id is not None: |
| 440 | i = shared_pool_id |
| 441 | self.clear_resource_id_pool( |
| 442 | pon_intf_id=i, |
| 443 | resource_type=PONResourceManager.FLOW_ID, |
| 444 | ) |
| 445 | if shared_pool_id is not None: |
| 446 | break |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 447 | |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 448 | def init_resource_id_pool(self, pon_intf_id, resource_type, start_idx, |
| 449 | end_idx): |
| 450 | """ |
| 451 | Initialize Resource ID pool for a given Resource Type on a given PON Port |
| 452 | |
| 453 | :param pon_intf_id: OLT PON interface id |
| 454 | :param resource_type: String to identify type of resource |
| 455 | :param start_idx: start index for onu id pool |
| 456 | :param end_idx: end index for onu id pool |
| 457 | :return boolean: True if resource id pool initialized else false |
| 458 | """ |
| 459 | status = False |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 460 | |
| 461 | # delegate to the master instance if sharing enabled across instances |
| 462 | shared_resource_mgr = self.shared_resource_mgrs[self.shared_idx_by_type[resource_type]] |
| 463 | if shared_resource_mgr is not None and shared_resource_mgr is not self: |
| 464 | return shared_resource_mgr.init_resource_id_pool(pon_intf_id, resource_type, |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 465 | start_idx, end_idx) |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 466 | |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 467 | path = self._get_path(pon_intf_id, resource_type) |
| 468 | if path is None: |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 469 | return status |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 470 | |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 471 | try: |
| 472 | # In case of adapter reboot and reconciliation resource in kv store |
| 473 | # checked for its presence if not kv store update happens |
| 474 | resource = self._get_resource(path) |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 475 | |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 476 | if resource is not None: |
| 477 | self._log.info("Resource-already-present-in-store", path=path) |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 478 | status = True |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 479 | else: |
| 480 | resource = self._format_resource(pon_intf_id, start_idx, |
| 481 | end_idx) |
| 482 | self._log.info("Resource-initialized", path=path) |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 483 | |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 484 | # Add resource as json in kv store. |
| 485 | result = self._kv_store.update_to_kv_store(path, resource) |
| 486 | if result is True: |
| 487 | status = True |
| 488 | |
| 489 | except Exception as e: |
| 490 | self._log.exception("error-initializing-resource-pool", e=e) |
| 491 | |
| 492 | return status |
| 493 | |
Craig Lutgen | abd9c84 | 2018-11-15 23:58:27 +0000 | [diff] [blame] | 494 | def assert_resource_limits(self, id, resource_type): |
| 495 | """ |
| 496 | Assert the specified id value is in the limit bounds of he requested resource type. |
| 497 | |
| 498 | :param id: The value to assert is in limits |
| 499 | :param resource_type: String to identify type of resource |
| 500 | """ |
| 501 | start_idx = PONResourceManager.ONU_ID_START_IDX if resource_type == PONResourceManager.ONU_ID \ |
| 502 | else PONResourceManager.ALLOC_ID_START_IDX if resource_type == PONResourceManager.ALLOC_ID \ |
| 503 | else PONResourceManager.GEMPORT_ID_START_IDX if resource_type == PONResourceManager.GEMPORT_ID \ |
| 504 | else PONResourceManager.FLOW_ID_START_IDX if resource_type == PONResourceManager.FLOW_ID \ |
| 505 | else PONResourceManager.UNI_ID_START_IDX if resource_type == PONResourceManager.UNI_ID \ |
| 506 | else None |
| 507 | end_idx = PONResourceManager.ONU_ID_END_IDX if resource_type == PONResourceManager.ONU_ID \ |
| 508 | else PONResourceManager.ALLOC_ID_END_IDX if resource_type == PONResourceManager.ALLOC_ID \ |
| 509 | else PONResourceManager.GEMPORT_ID_END_IDX if resource_type == PONResourceManager.GEMPORT_ID \ |
| 510 | else PONResourceManager.FLOW_ID_END_IDX if resource_type == PONResourceManager.FLOW_ID \ |
| 511 | else PONResourceManager.UNI_ID_END_IDX if resource_type == PONResourceManager.UNI_ID \ |
| 512 | else None |
| 513 | assert id >= self.pon_resource_ranges[start_idx] and id <= self.pon_resource_ranges[end_idx] |
| 514 | |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 515 | def get_resource_id(self, pon_intf_id, resource_type, num_of_id=1): |
| 516 | """ |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 517 | Create alloc/gemport/onu/flow id for given OLT PON interface. |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 518 | |
| 519 | :param pon_intf_id: OLT PON interface id |
| 520 | :param resource_type: String to identify type of resource |
| 521 | :param num_of_id: required number of ids |
| 522 | :return list/int/None: list, int or None if resource type is |
| 523 | alloc_id/gemport_id, onu_id or invalid type |
| 524 | respectively |
| 525 | """ |
| 526 | result = None |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 527 | |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 528 | if num_of_id < 1: |
| 529 | self._log.error("invalid-num-of-resources-requested") |
| 530 | return result |
| 531 | |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 532 | # delegate to the master instance if sharing enabled across instances |
| 533 | shared_resource_mgr = self.shared_resource_mgrs[self.shared_idx_by_type[resource_type]] |
| 534 | if shared_resource_mgr is not None and shared_resource_mgr is not self: |
Craig Lutgen | 66dbf62 | 2018-11-18 12:24:54 -0600 | [diff] [blame] | 535 | return shared_resource_mgr.get_resource_id(pon_intf_id, resource_type, num_of_id) |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 536 | |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 537 | path = self._get_path(pon_intf_id, resource_type) |
| 538 | if path is None: |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 539 | return result |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 540 | |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 541 | try: |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 542 | resource = self._get_resource(path) |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 543 | if resource is not None and \ |
| 544 | (resource_type == PONResourceManager.ONU_ID or |
| 545 | resource_type == PONResourceManager.FLOW_ID): |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 546 | result = self._generate_next_id(resource) |
| 547 | elif resource is not None and ( |
| 548 | resource_type == PONResourceManager.GEMPORT_ID or |
| 549 | resource_type == PONResourceManager.ALLOC_ID): |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 550 | if num_of_id == 1: |
| 551 | result = self._generate_next_id(resource) |
| 552 | else: |
| 553 | result = list() |
| 554 | while num_of_id > 0: |
| 555 | result.append(self._generate_next_id(resource)) |
| 556 | num_of_id -= 1 |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 557 | else: |
| 558 | raise Exception("get-resource-failed") |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 559 | |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 560 | self._log.debug("Get-" + resource_type + "-success", result=result, |
| 561 | path=path) |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 562 | # Update resource in kv store |
| 563 | self._update_resource(path, resource) |
| 564 | |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 565 | except Exception as e: |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 566 | self._log.exception("Get-" + resource_type + "-id-failed", |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 567 | path=path, e=e) |
| 568 | return result |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 569 | |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 570 | def free_resource_id(self, pon_intf_id, resource_type, release_content): |
| 571 | """ |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 572 | Release alloc/gemport/onu/flow id for given OLT PON interface. |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 573 | |
| 574 | :param pon_intf_id: OLT PON interface id |
| 575 | :param resource_type: String to identify type of resource |
| 576 | :param release_content: required number of ids |
| 577 | :return boolean: True if all IDs in given release_content released |
| 578 | else False |
| 579 | """ |
| 580 | status = False |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 581 | |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 582 | # delegate to the master instance if sharing enabled across instances |
| 583 | shared_resource_mgr = self.shared_resource_mgrs[self.shared_idx_by_type[resource_type]] |
| 584 | if shared_resource_mgr is not None and shared_resource_mgr is not self: |
| 585 | return shared_resource_mgr.free_resource_id(pon_intf_id, resource_type) |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 586 | |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 587 | path = self._get_path(pon_intf_id, resource_type) |
| 588 | if path is None: |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 589 | return status |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 590 | |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 591 | try: |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 592 | resource = self._get_resource(path) |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 593 | if resource is not None and ( |
| 594 | resource_type == PONResourceManager.ONU_ID or |
| 595 | resource_type == PONResourceManager.FLOW_ID): |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 596 | self._release_id(resource, release_content) |
| 597 | elif resource is not None and ( |
| 598 | resource_type == PONResourceManager.ALLOC_ID or |
| 599 | resource_type == PONResourceManager.GEMPORT_ID): |
| 600 | for content in release_content: |
| 601 | self._release_id(resource, content) |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 602 | else: |
| 603 | raise Exception("get-resource-failed") |
| 604 | |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 605 | self._log.debug("Free-" + resource_type + "-success", path=path) |
| 606 | |
| 607 | # Update resource in kv store |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 608 | status = self._update_resource(path, resource) |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 609 | |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 610 | except Exception as e: |
| 611 | self._log.exception("Free-" + resource_type + "-failed", |
| 612 | path=path, e=e) |
| 613 | return status |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 614 | |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 615 | def clear_resource_id_pool(self, pon_intf_id, resource_type): |
| 616 | """ |
| 617 | Clear Resource Pool for a given Resource Type on a given PON Port. |
| 618 | |
| 619 | :return boolean: True if removed else False |
| 620 | """ |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 621 | |
| 622 | # delegate to the master instance if sharing enabled across instances |
| 623 | shared_resource_mgr = self.shared_resource_mgrs[self.shared_idx_by_type[resource_type]] |
| 624 | if shared_resource_mgr is not None and shared_resource_mgr is not self: |
| 625 | return shared_resource_mgr.clear_resource_id_pool(pon_intf_id, resource_type) |
| 626 | |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 627 | path = self._get_path(pon_intf_id, resource_type) |
| 628 | if path is None: |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 629 | return False |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 630 | |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 631 | try: |
| 632 | result = self._kv_store.remove_from_kv_store(path) |
| 633 | if result is True: |
| 634 | self._log.debug("Resource-pool-cleared", |
| 635 | device_id=self.device_id, |
| 636 | path=path) |
| 637 | return True |
| 638 | except Exception as e: |
| 639 | self._log.exception("error-clearing-resource-pool", e=e) |
| 640 | |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 641 | self._log.error("Clear-resource-pool-failed", device_id=self.device_id, |
| 642 | path=path) |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 643 | return False |
| 644 | |
| 645 | def init_resource_map(self, pon_intf_onu_id): |
| 646 | """ |
| 647 | Initialize resource map |
| 648 | |
| 649 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 650 | """ |
| 651 | # initialize pon_intf_onu_id tuple to alloc_ids map |
| 652 | alloc_id_path = PONResourceManager.ALLOC_ID_RESOURCE_MAP_PATH.format( |
| 653 | self.device_id, str(pon_intf_onu_id) |
| 654 | ) |
| 655 | alloc_ids = list() |
| 656 | self._kv_store.update_to_kv_store( |
| 657 | alloc_id_path, json.dumps(alloc_ids) |
| 658 | ) |
| 659 | |
| 660 | # initialize pon_intf_onu_id tuple to gemport_ids map |
| 661 | gemport_id_path = PONResourceManager.GEMPORT_ID_RESOURCE_MAP_PATH.format( |
| 662 | self.device_id, str(pon_intf_onu_id) |
| 663 | ) |
| 664 | gemport_ids = list() |
| 665 | self._kv_store.update_to_kv_store( |
| 666 | gemport_id_path, json.dumps(gemport_ids) |
| 667 | ) |
| 668 | |
| 669 | def remove_resource_map(self, pon_intf_onu_id): |
| 670 | """ |
| 671 | Remove resource map |
| 672 | |
| 673 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 674 | """ |
| 675 | # remove pon_intf_onu_id tuple to alloc_ids map |
| 676 | alloc_id_path = PONResourceManager.ALLOC_ID_RESOURCE_MAP_PATH.format( |
| 677 | self.device_id, str(pon_intf_onu_id) |
| 678 | ) |
| 679 | self._kv_store.remove_from_kv_store(alloc_id_path) |
| 680 | |
| 681 | # remove pon_intf_onu_id tuple to gemport_ids map |
| 682 | gemport_id_path = PONResourceManager.GEMPORT_ID_RESOURCE_MAP_PATH.format( |
| 683 | self.device_id, str(pon_intf_onu_id) |
| 684 | ) |
| 685 | self._kv_store.remove_from_kv_store(gemport_id_path) |
| 686 | |
| 687 | def get_current_alloc_ids_for_onu(self, pon_intf_onu_id): |
| 688 | """ |
| 689 | Get currently configured alloc ids for given pon_intf_onu_id |
| 690 | |
| 691 | :param pon_intf_onu_id: reference of PON interface id and onu id |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 692 | |
| 693 | :return list: List of alloc_ids if available, else None |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 694 | """ |
| 695 | path = PONResourceManager.ALLOC_ID_RESOURCE_MAP_PATH.format( |
| 696 | self.device_id, |
| 697 | str(pon_intf_onu_id)) |
| 698 | value = self._kv_store.get_from_kv_store(path) |
| 699 | if value is not None: |
| 700 | alloc_id_list = json.loads(value) |
| 701 | if len(alloc_id_list) > 0: |
| 702 | return alloc_id_list |
| 703 | |
| 704 | return None |
| 705 | |
| 706 | def get_current_gemport_ids_for_onu(self, pon_intf_onu_id): |
| 707 | """ |
| 708 | Get currently configured gemport ids for given pon_intf_onu_id |
| 709 | |
| 710 | :param pon_intf_onu_id: reference of PON interface id and onu id |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 711 | |
| 712 | :return list: List of gemport IDs if available, else None |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 713 | """ |
| 714 | |
| 715 | path = PONResourceManager.GEMPORT_ID_RESOURCE_MAP_PATH.format( |
| 716 | self.device_id, |
| 717 | str(pon_intf_onu_id)) |
| 718 | value = self._kv_store.get_from_kv_store(path) |
| 719 | if value is not None: |
| 720 | gemport_id_list = json.loads(value) |
| 721 | if len(gemport_id_list) > 0: |
| 722 | return gemport_id_list |
| 723 | |
| 724 | return None |
| 725 | |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 726 | def get_current_flow_ids_for_onu(self, pon_intf_onu_id): |
| 727 | """ |
| 728 | Get currently configured flow ids for given pon_intf_onu_id |
| 729 | |
| 730 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 731 | |
| 732 | :return list: List of Flow IDs if available, else None |
| 733 | """ |
| 734 | |
| 735 | path = PONResourceManager.FLOW_ID_RESOURCE_MAP_PATH.format( |
| 736 | self.device_id, |
| 737 | str(pon_intf_onu_id)) |
| 738 | value = self._kv_store.get_from_kv_store(path) |
| 739 | if value is not None: |
| 740 | flow_id_list = json.loads(value) |
| 741 | assert(isinstance(flow_id_list, list)) |
| 742 | if len(flow_id_list) > 0: |
| 743 | return flow_id_list |
| 744 | |
| 745 | return None |
| 746 | |
| 747 | def get_flow_id_info(self, pon_intf_onu_id, flow_id): |
| 748 | """ |
| 749 | Get flow_id details configured for the ONU. |
| 750 | |
| 751 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 752 | :param flow_id: Flow Id reference |
| 753 | |
| 754 | :return blob: Flow data blob if available, else None |
| 755 | """ |
| 756 | |
| 757 | path = PONResourceManager.FLOW_ID_INFO_PATH.format( |
| 758 | self.device_id, |
| 759 | str(pon_intf_onu_id), |
| 760 | flow_id) |
| 761 | value = self._kv_store.get_from_kv_store(path) |
| 762 | if value is not None: |
| 763 | return ast.literal_eval(value) |
| 764 | |
| 765 | return None |
| 766 | |
| 767 | def remove_flow_id_info(self, pon_intf_onu_id, flow_id): |
| 768 | """ |
| 769 | Get flow_id details configured for the ONU. |
| 770 | |
| 771 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 772 | :param flow_id: Flow Id reference |
| 773 | |
| 774 | """ |
| 775 | |
| 776 | path = PONResourceManager.FLOW_ID_INFO_PATH.format( |
| 777 | self.device_id, |
| 778 | str(pon_intf_onu_id), |
| 779 | flow_id) |
| 780 | self._kv_store.remove_from_kv_store(path) |
| 781 | |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 782 | def update_alloc_ids_for_onu(self, pon_intf_onu_id, alloc_ids): |
| 783 | """ |
| 784 | Update currently configured alloc ids for given pon_intf_onu_id |
| 785 | |
| 786 | :param pon_intf_onu_id: reference of PON interface id and onu id |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 787 | :param alloc_ids: list of alloc ids |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 788 | """ |
| 789 | path = PONResourceManager.ALLOC_ID_RESOURCE_MAP_PATH.format( |
| 790 | self.device_id, str(pon_intf_onu_id) |
| 791 | ) |
| 792 | self._kv_store.update_to_kv_store( |
| 793 | path, json.dumps(alloc_ids) |
| 794 | ) |
| 795 | |
| 796 | def update_gemport_ids_for_onu(self, pon_intf_onu_id, gemport_ids): |
| 797 | """ |
| 798 | Update currently configured gemport ids for given pon_intf_onu_id |
| 799 | |
| 800 | :param pon_intf_onu_id: reference of PON interface id and onu id |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 801 | :param gemport_ids: list of gem port ids |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 802 | """ |
| 803 | path = PONResourceManager.GEMPORT_ID_RESOURCE_MAP_PATH.format( |
| 804 | self.device_id, str(pon_intf_onu_id) |
| 805 | ) |
| 806 | self._kv_store.update_to_kv_store( |
| 807 | path, json.dumps(gemport_ids) |
| 808 | ) |
| 809 | |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 810 | def update_flow_id_for_onu(self, pon_intf_onu_id, flow_id, add=True): |
| 811 | """ |
| 812 | Update the flow_id list of the ONU (add or remove flow_id from the list) |
| 813 | |
| 814 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 815 | :param flow_id: flow ID |
| 816 | :param add: Boolean flag to indicate whether the flow_id should be |
| 817 | added or removed from the list. Defaults to adding the flow. |
| 818 | """ |
| 819 | path = PONResourceManager.FLOW_ID_RESOURCE_MAP_PATH.format( |
| 820 | self.device_id, str(pon_intf_onu_id) |
| 821 | ) |
| 822 | current_flow_ids = self.get_current_flow_ids_for_onu(pon_intf_onu_id) |
| 823 | if not isinstance(current_flow_ids, list): |
| 824 | # When the first flow_id is being added, the current_flow_ids is None |
| 825 | current_flow_ids = list() |
| 826 | |
| 827 | if add: |
| 828 | if flow_id not in current_flow_ids: |
| 829 | current_flow_ids.append(flow_id) |
| 830 | else: |
| 831 | if flow_id in current_flow_ids: |
| 832 | current_flow_ids.remove(flow_id) |
| 833 | |
| 834 | self._kv_store.update_to_kv_store(path, current_flow_ids) |
| 835 | |
| 836 | def update_flow_id_info_for_onu(self, pon_intf_onu_id, flow_id, flow_data): |
| 837 | """ |
| 838 | Update any metadata associated with the flow_id. The flow_data could be json |
| 839 | or any of other data structure. The resource manager doesnt care |
| 840 | |
| 841 | :param pon_intf_onu_id: reference of PON interface id and onu id |
| 842 | :param flow_id: Flow ID |
| 843 | :param flow_data: Flow data blob |
| 844 | """ |
| 845 | path = PONResourceManager.FLOW_ID_INFO_PATH.format( |
| 846 | self.device_id, str(pon_intf_onu_id), flow_id |
| 847 | ) |
| 848 | |
| 849 | if not self._kv_store.update_to_kv_store(path, flow_data): |
| 850 | self._log.error("flow-info-update-failed", path=path, flow_id=flow_id) |
| 851 | |
Craig Lutgen | f760147 | 2018-11-02 13:27:41 +0000 | [diff] [blame] | 852 | def _get_olt_model(self): |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 853 | """ |
Craig Lutgen | f760147 | 2018-11-02 13:27:41 +0000 | [diff] [blame] | 854 | Get olt model variant |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 855 | |
Craig Lutgen | f760147 | 2018-11-02 13:27:41 +0000 | [diff] [blame] | 856 | :return: type of olt model |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 857 | """ |
Craig Lutgen | f760147 | 2018-11-02 13:27:41 +0000 | [diff] [blame] | 858 | olt_model = None |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 859 | if self.extra_args and len(self.extra_args) > 0: |
| 860 | parser = OltVendorArgumentParser(add_help=False) |
Craig Lutgen | f760147 | 2018-11-02 13:27:41 +0000 | [diff] [blame] | 861 | parser.add_argument(PONResourceManager.OLT_MODEL_ARG, '-m', action='store', default='default') |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 862 | try: |
| 863 | args = parser.parse_args(shlex.split(self.extra_args)) |
| 864 | self._log.debug('parsing-extra-arguments', args=args) |
Craig Lutgen | f760147 | 2018-11-02 13:27:41 +0000 | [diff] [blame] | 865 | olt_model = args.olt_model |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 866 | except ArgumentError as e: |
| 867 | self._log.exception('invalid-arguments: {}', e=e) |
| 868 | except Exception as e: |
| 869 | self._log.exception('option-parsing-error: {}', e=e) |
| 870 | |
Craig Lutgen | f760147 | 2018-11-02 13:27:41 +0000 | [diff] [blame] | 871 | self._log.debug('olt-model', olt_model=olt_model) |
| 872 | return olt_model |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 873 | |
| 874 | def _generate_next_id(self, resource): |
| 875 | """ |
| 876 | Generate unique id having OFFSET as start index. |
| 877 | |
| 878 | :param resource: resource used to generate ID |
| 879 | :return int: generated id |
| 880 | """ |
| 881 | pos = resource[PONResourceManager.POOL].find('0b0') |
| 882 | resource[PONResourceManager.POOL].set(1, pos) |
| 883 | return pos[0] + resource[PONResourceManager.START_IDX] |
| 884 | |
| 885 | def _release_id(self, resource, unique_id): |
| 886 | """ |
| 887 | Release unique id having OFFSET as start index. |
| 888 | |
| 889 | :param resource: resource used to release ID |
| 890 | :param unique_id: id need to be released |
| 891 | """ |
| 892 | pos = ((int(unique_id)) - resource[PONResourceManager.START_IDX]) |
| 893 | resource[PONResourceManager.POOL].set(0, pos) |
| 894 | |
| 895 | def _get_path(self, pon_intf_id, resource_type): |
| 896 | """ |
| 897 | Get path for given resource type. |
| 898 | |
| 899 | :param pon_intf_id: OLT PON interface id |
| 900 | :param resource_type: String to identify type of resource |
| 901 | :return: path for given resource type |
| 902 | """ |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 903 | |
| 904 | shared_pool_id = self.pon_resource_ranges[self.shared_idx_by_type[resource_type]] |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 905 | if shared_pool_id is not None: |
| 906 | pon_intf_id = shared_pool_id |
Craig Lutgen | 1dd4708 | 2018-10-23 13:12:26 -0500 | [diff] [blame] | 907 | |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 908 | path = None |
| 909 | if resource_type == PONResourceManager.ONU_ID: |
| 910 | path = self._get_onu_id_resource_path(pon_intf_id) |
| 911 | elif resource_type == PONResourceManager.ALLOC_ID: |
| 912 | path = self._get_alloc_id_resource_path(pon_intf_id) |
| 913 | elif resource_type == PONResourceManager.GEMPORT_ID: |
| 914 | path = self._get_gemport_id_resource_path(pon_intf_id) |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 915 | elif resource_type == PONResourceManager.FLOW_ID: |
| 916 | path = self._get_flow_id_resource_path(pon_intf_id) |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 917 | else: |
| 918 | self._log.error("invalid-resource-pool-identifier") |
| 919 | return path |
| 920 | |
Girish Gowdru | ab836e9 | 2018-10-25 01:17:57 -0700 | [diff] [blame] | 921 | def _get_flow_id_resource_path(self, pon_intf_id): |
| 922 | """ |
| 923 | Get flow id resource path. |
| 924 | |
| 925 | :param pon_intf_id: OLT PON interface id |
| 926 | :return: flow id resource path |
| 927 | """ |
| 928 | return PONResourceManager.FLOW_ID_POOL_PATH.format( |
| 929 | self.device_id, pon_intf_id) |
| 930 | |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 931 | def _get_alloc_id_resource_path(self, pon_intf_id): |
| 932 | """ |
| 933 | Get alloc id resource path. |
| 934 | |
| 935 | :param pon_intf_id: OLT PON interface id |
| 936 | :return: alloc id resource path |
| 937 | """ |
| 938 | return PONResourceManager.ALLOC_ID_POOL_PATH.format( |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 939 | self.device_id, pon_intf_id) |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 940 | |
| 941 | def _get_gemport_id_resource_path(self, pon_intf_id): |
| 942 | """ |
| 943 | Get gemport id resource path. |
| 944 | |
| 945 | :param pon_intf_id: OLT PON interface id |
| 946 | :return: gemport id resource path |
| 947 | """ |
| 948 | return PONResourceManager.GEMPORT_ID_POOL_PATH.format( |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 949 | self.device_id, pon_intf_id) |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 950 | |
| 951 | def _get_onu_id_resource_path(self, pon_intf_id): |
| 952 | """ |
| 953 | Get onu id resource path. |
| 954 | |
| 955 | :param pon_intf_id: OLT PON interface id |
| 956 | :return: onu id resource path |
| 957 | """ |
| 958 | return PONResourceManager.ONU_ID_POOL_PATH.format( |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 959 | self.device_id, pon_intf_id) |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 960 | |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 961 | def _update_resource(self, path, resource): |
| 962 | """ |
| 963 | Update resource in resource kv store. |
| 964 | |
| 965 | :param path: path to update resource |
| 966 | :param resource: resource need to be updated |
| 967 | :return boolean: True if resource updated in kv store else False |
| 968 | """ |
| 969 | resource[PONResourceManager.POOL] = \ |
| 970 | resource[PONResourceManager.POOL].bin |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 971 | result = self._kv_store.update_to_kv_store(path, json.dumps(resource)) |
| 972 | if result is True: |
| 973 | return True |
| 974 | return False |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 975 | |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 976 | def _get_resource(self, path): |
| 977 | """ |
| 978 | Get resource from kv store. |
| 979 | |
| 980 | :param path: path to get resource |
| 981 | :return: resource if resource present in kv store else None |
| 982 | """ |
| 983 | # get resource from kv store |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 984 | result = self._kv_store.get_from_kv_store(path) |
| 985 | if result is None: |
| 986 | return result |
| 987 | self._log.info("dumping resource", result=result) |
| 988 | resource = result |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 989 | |
| 990 | if resource is not None: |
| 991 | # decode resource fetched from backend store to dictionary |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 992 | resource = json.loads(resource) |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 993 | |
| 994 | # resource pool in backend store stored as binary string whereas to |
| 995 | # access the pool to generate/release IDs it need to be converted |
| 996 | # as BitArray |
| 997 | resource[PONResourceManager.POOL] = \ |
| 998 | BitArray('0b' + resource[PONResourceManager.POOL]) |
| 999 | |
Girish Gowdru | 1e77ea0 | 2018-09-24 09:10:35 -0700 | [diff] [blame] | 1000 | return resource |
Girish Gowdru | 141ced8 | 2018-09-17 20:19:14 -0700 | [diff] [blame] | 1001 | |
| 1002 | def _format_resource(self, pon_intf_id, start_idx, end_idx): |
| 1003 | """ |
| 1004 | Format resource as json. |
| 1005 | |
| 1006 | :param pon_intf_id: OLT PON interface id |
| 1007 | :param start_idx: start index for id pool |
| 1008 | :param end_idx: end index for id pool |
| 1009 | :return dictionary: resource formatted as dictionary |
| 1010 | """ |
| 1011 | # Format resource as json to be stored in backend store |
| 1012 | resource = dict() |
| 1013 | resource[PONResourceManager.PON_INTF_ID] = pon_intf_id |
| 1014 | resource[PONResourceManager.START_IDX] = start_idx |
| 1015 | resource[PONResourceManager.END_IDX] = end_idx |
| 1016 | |
| 1017 | # resource pool stored in backend store as binary string |
| 1018 | resource[PONResourceManager.POOL] = BitArray(end_idx).bin |
| 1019 | |
| 1020 | return json.dumps(resource) |