VOL-948: Implementation of ResourceManager module and corresponding changes in OpenOlt adapter
Change-Id: Ie55ca23e975cf640cce094948a06ab5e12834895
diff --git a/common/pon_resource_manager/__init__.py b/common/pon_resource_manager/__init__.py
new file mode 100644
index 0000000..2d104e0
--- /dev/null
+++ b/common/pon_resource_manager/__init__.py
@@ -0,0 +1,13 @@
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
diff --git a/common/pon_resource_manager/resource_manager.py b/common/pon_resource_manager/resource_manager.py
new file mode 100644
index 0000000..4abead3
--- /dev/null
+++ b/common/pon_resource_manager/resource_manager.py
@@ -0,0 +1,460 @@
+#
+# Copyright 2018 the original author or authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+"""
+Resource Manager will be unique for each OLT device.
+
+It exposes APIs to create/free alloc_ids/onu_ids/gemport_ids. Resource Manager
+uses a KV store in backend to ensure resiliency of the data.
+"""
+import json
+import structlog
+from bitstring import BitArray
+from twisted.internet.defer import returnValue, inlineCallbacks
+
+from common.kvstore.kvstore import create_kv_client
+from common.utils.asleep import asleep
+
+
+class PONResourceManager(object):
+ """Implements APIs to initialize/allocate/release alloc/gemport/onu IDs."""
+
+ # Constants to identify resource pool
+ ONU_ID = 'ONU_ID'
+ ALLOC_ID = 'ALLOC_ID'
+ GEMPORT_ID = 'GEMPORT_ID'
+
+ # The resource ranges for a given device vendor_type should be placed
+ # at 'resource_manager/<technology>/resource_ranges/<olt_vendor_type>'
+ # path on the KV store.
+ # If Resource Range parameters are to be read from the external KV store,
+ # they are expected to be stored in the following format.
+ # Note: All parameters are MANDATORY for now.
+ '''
+ {
+ "onu_start_idx": 1,
+ "onu_end_idx": 127,
+ "alloc_id_start_idx": 1024,
+ "alloc_id_end_idx": 65534,
+ "gem_port_id_start_idx": 1024,
+ "gem_port_id_end_idx": 16383,
+ "num_of_pon_port": 16
+ }
+ '''
+ # constants used as keys to reference the resource range parameters from
+ # and external KV store.
+ ONU_START_IDX = "onu_start_idx"
+ ONU_END_IDX = "onu_end_idx"
+ ALLOC_ID_START_IDX = "alloc_id_start_idx"
+ ALLOC_ID_END_IDX = "alloc_id_end_idx"
+ GEM_PORT_ID_START_IDX = "gem_port_id_start_idx"
+ GEM_PORT_ID_END_IDX = "gem_port_id_end_idx"
+ NUM_OF_PON_PORT = "num_of_pon_port"
+
+ # PON Resource range configuration on the KV store.
+ # Format: 'resource_manager/<technology>/resource_ranges/<olt_vendor_type>'
+ PON_RESOURCE_RANGE_CONFIG_PATH = 'resource_manager/{}/resource_ranges/{}'
+
+ # resource path in kv store
+ ALLOC_ID_POOL_PATH = 'resource_manager/{}/{}/alloc_id_pool/{}'
+ GEMPORT_ID_POOL_PATH = 'resource_manager/{}/{}/gemport_id_pool/{}'
+ ONU_ID_POOL_PATH = 'resource_manager/{}/{}/onu_id_pool/{}'
+
+ # Constants for internal usage.
+ PON_INTF_ID = 'pon_intf_id'
+ START_IDX = 'start_idx'
+ END_IDX = 'end_idx'
+ POOL = 'pool'
+
+ def __init__(self, technology, olt_vendor_type, device_id,
+ backend, host, port):
+ """
+ Create PONResourceManager object.
+
+ :param technology: PON technology
+ :param: olt_vendor_type: This string defines the OLT vendor type
+ and is used as a Key to load the resource range configuration from
+ KV store location.
+ :param device_id: OLT device id
+ :param backend: backend store
+ :param host: ip of backend store
+ :param port: port on which backend store listens
+ :raises exception when invalid backend store passed as an argument
+ """
+ # logger
+ self._log = structlog.get_logger()
+
+ try:
+ self._kv_store = create_kv_client(backend, host, port)
+ self.technology = technology
+ self.olt_vendor_type = olt_vendor_type
+ self.device_id = device_id
+ # Below attribute, pon_resource_ranges, should be initialized
+ # by reading from KV store.
+ self.pon_resource_ranges = dict()
+ except Exception as e:
+ self._log.exception("exception-in-init")
+ raise Exception(e)
+
+ @inlineCallbacks
+ def init_pon_resource_ranges(self):
+ # Try to initialize the PON Resource Ranges from KV store if available
+ status = yield self.init_resource_ranges_from_kv_store()
+ # If reading from KV store fails, initialize to default values.
+ if not status:
+ self._log.error("failed-to-read-resource-ranges-from-kv-store")
+ self.init_default_pon_resource_ranges()
+
+ @inlineCallbacks
+ def init_resource_ranges_from_kv_store(self):
+ path = self.PON_RESOURCE_RANGE_CONFIG_PATH.format(
+ self.technology, self.olt_vendor_type)
+ # get resource from kv store
+ result = yield self._kv_store.get(path)
+ resource_range_config = result[0]
+
+ if resource_range_config is not None:
+ self.pon_resource_ranges = eval(resource_range_config.value)
+ self._log.debug("Init-resource-ranges-from-kvstore-success",
+ pon_resource_ranges=self.pon_resource_ranges,
+ path=path)
+ returnValue(True)
+
+ returnValue(False)
+
+ def init_default_pon_resource_ranges(self, onu_start_idx=1,
+ onu_end_idx=127,
+ alloc_id_start_idx=1024,
+ alloc_id_end_idx=65534,
+ gem_port_id_start_idx=1024,
+ gem_port_id_end_idx=16383,
+ num_of_pon_ports=16):
+ self._log.info("initialize-default-resource-range-values")
+ self.pon_resource_ranges[PONResourceManager.ONU_START_IDX] = onu_start_idx
+ self.pon_resource_ranges[PONResourceManager.ONU_END_IDX] = onu_end_idx
+ self.pon_resource_ranges[PONResourceManager.ALLOC_ID_START_IDX] = alloc_id_start_idx
+ self.pon_resource_ranges[PONResourceManager.ALLOC_ID_END_IDX] = alloc_id_end_idx
+ self.pon_resource_ranges[
+ PONResourceManager.GEM_PORT_ID_START_IDX] = gem_port_id_start_idx
+ self.pon_resource_ranges[
+ PONResourceManager.GEM_PORT_ID_END_IDX] = gem_port_id_end_idx
+ self.pon_resource_ranges[PONResourceManager.NUM_OF_PON_PORT] = num_of_pon_ports
+
+ def init_device_resource_pool(self):
+ i = 0
+ while i < self.pon_resource_ranges[PONResourceManager.NUM_OF_PON_PORT]:
+ self.init_resource_id_pool(
+ pon_intf_id=i,
+ resource_type=PONResourceManager.ONU_ID,
+ start_idx=self.pon_resource_ranges[
+ PONResourceManager.ONU_START_IDX],
+ end_idx=self.pon_resource_ranges[
+ PONResourceManager.ONU_END_IDX])
+
+ self.init_resource_id_pool(
+ pon_intf_id=i,
+ resource_type=PONResourceManager.ALLOC_ID,
+ start_idx=self.pon_resource_ranges[
+ PONResourceManager.ALLOC_ID_START_IDX],
+ end_idx=self.pon_resource_ranges[
+ PONResourceManager.ALLOC_ID_END_IDX])
+
+ self.init_resource_id_pool(
+ pon_intf_id=i,
+ resource_type=PONResourceManager.GEMPORT_ID,
+ start_idx=self.pon_resource_ranges[
+ PONResourceManager.GEM_PORT_ID_START_IDX],
+ end_idx=self.pon_resource_ranges[
+ PONResourceManager.GEM_PORT_ID_END_IDX])
+ i += 1
+
+ def clear_device_resource_pool(self):
+ i = 0
+ while i < self.pon_resource_ranges[PONResourceManager.NUM_OF_PON_PORT]:
+ self.clear_resource_id_pool(
+ pon_intf_id=i,
+ resource_type=PONResourceManager.ONU_ID,
+ )
+
+ self.clear_resource_id_pool(
+ pon_intf_id=i,
+ resource_type=PONResourceManager.ALLOC_ID,
+ )
+
+ self.clear_resource_id_pool(
+ pon_intf_id=i,
+ resource_type=PONResourceManager.GEMPORT_ID,
+ )
+ i += 1
+
+ @inlineCallbacks
+ def init_resource_id_pool(self, pon_intf_id, resource_type, start_idx,
+ end_idx):
+ """
+ Initialize Resource ID pool for a given Resource Type on a given PON Port
+
+ :param pon_intf_id: OLT PON interface id
+ :param resource_type: String to identify type of resource
+ :param start_idx: start index for onu id pool
+ :param end_idx: end index for onu id pool
+ :return boolean: True if resource id pool initialized else false
+ """
+ status = False
+ path = self._get_path(pon_intf_id, resource_type)
+ if path is None:
+ returnValue(status)
+
+ # In case of adapter reboot and reconciliation resource in kv store
+ # checked for its presence if not kv store update happens
+ resource = yield self._get_resource(path)
+
+ if resource is not None:
+ self._log.info("Resource-already-present-in-store", path=path)
+ status = True
+ else:
+ resource = self._format_resource(pon_intf_id, start_idx, end_idx)
+ self._log.info("Resource-initialized", path=path)
+
+ # Add resource as json in kv store.
+ result = yield self._kv_store.put(path, resource)
+ if result is None:
+ status = True
+ returnValue(status)
+
+ @inlineCallbacks
+ def get_resource_id(self, pon_intf_id, resource_type, num_of_id=1):
+ """
+ Create alloc/gemport/onu id for given OLT PON interface.
+
+ :param pon_intf_id: OLT PON interface id
+ :param resource_type: String to identify type of resource
+ :param num_of_id: required number of ids
+ :return list/int/None: list, int or None if resource type is
+ alloc_id/gemport_id, onu_id or invalid type
+ respectively
+ """
+ result = None
+ path = self._get_path(pon_intf_id, resource_type)
+ if path is None:
+ returnValue(result)
+
+ resource = yield self._get_resource(path)
+ try:
+ if resource is not None and resource_type == \
+ PONResourceManager.ONU_ID:
+ result = self._generate_next_id(resource)
+ elif resource is not None and (
+ resource_type == PONResourceManager.GEMPORT_ID or
+ resource_type == PONResourceManager.ALLOC_ID):
+ result = list()
+ while num_of_id > 0:
+ result.append(self._generate_next_id(resource))
+ num_of_id -= 1
+
+ # Update resource in kv store
+ self._update_resource(path, resource)
+
+ except BaseException:
+ self._log.exception("Get-" + resource_type + "-id-failed",
+ path=path)
+ self._log.debug("Get-" + resource_type + "-success", result=result,
+ path=path)
+ returnValue(result)
+
+ @inlineCallbacks
+ def free_resource_id(self, pon_intf_id, resource_type, release_content):
+ """
+ Release alloc/gemport/onu id for given OLT PON interface.
+
+ :param pon_intf_id: OLT PON interface id
+ :param resource_type: String to identify type of resource
+ :param release_content: required number of ids
+ :return boolean: True if all IDs in given release_content released
+ else False
+ """
+ status = False
+ path = self._get_path(pon_intf_id, resource_type)
+ if path is None:
+ returnValue(status)
+
+ resource = yield self._get_resource(path)
+ try:
+ if resource is not None and resource_type == \
+ PONResourceManager.ONU_ID:
+ self._release_id(resource, release_content)
+ elif resource is not None and (
+ resource_type == PONResourceManager.ALLOC_ID or
+ resource_type == PONResourceManager.GEMPORT_ID):
+ for content in release_content:
+ self._release_id(resource, content)
+ self._log.debug("Free-" + resource_type + "-success", path=path)
+
+ # Update resource in kv store
+ status = yield self._update_resource(path, resource)
+
+ except BaseException:
+ self._log.exception("Free-" + resource_type + "-failed", path=path)
+ returnValue(status)
+
+ @inlineCallbacks
+ def clear_resource_id_pool(self, pon_intf_id, resource_type):
+ """
+ Clear Resource Pool for a given Resource Type on a given PON Port.
+
+ :return boolean: True if removed else False
+ """
+ path = self._get_path(pon_intf_id, resource_type)
+ if path is None:
+ returnValue(False)
+
+ result = yield self._kv_store.delete(path)
+ if result is None:
+ self._log.debug("Resource-pool-cleared", device_id=self.device_id,
+ path=path)
+ returnValue(True)
+ self._log.error("Clear-resource-pool-failed", device_id=self.device_id,
+ path=path)
+ returnValue(False)
+
+ def _generate_next_id(self, resource):
+ """
+ Generate unique id having OFFSET as start index.
+
+ :param resource: resource used to generate ID
+ :return int: generated id
+ """
+ pos = resource[PONResourceManager.POOL].find('0b0')
+ resource[PONResourceManager.POOL].set(1, pos)
+ return pos[0] + resource[PONResourceManager.START_IDX]
+
+ def _release_id(self, resource, unique_id):
+ """
+ Release unique id having OFFSET as start index.
+
+ :param resource: resource used to release ID
+ :param unique_id: id need to be released
+ """
+ pos = ((int(unique_id)) - resource[PONResourceManager.START_IDX])
+ resource[PONResourceManager.POOL].set(0, pos)
+
+ def _get_path(self, pon_intf_id, resource_type):
+ """
+ Get path for given resource type.
+
+ :param pon_intf_id: OLT PON interface id
+ :param resource_type: String to identify type of resource
+ :return: path for given resource type
+ """
+ path = None
+ if resource_type == PONResourceManager.ONU_ID:
+ path = self._get_onu_id_resource_path(pon_intf_id)
+ elif resource_type == PONResourceManager.ALLOC_ID:
+ path = self._get_alloc_id_resource_path(pon_intf_id)
+ elif resource_type == PONResourceManager.GEMPORT_ID:
+ path = self._get_gemport_id_resource_path(pon_intf_id)
+ else:
+ self._log.error("invalid-resource-pool-identifier")
+ return path
+
+ def _get_alloc_id_resource_path(self, pon_intf_id):
+ """
+ Get alloc id resource path.
+
+ :param pon_intf_id: OLT PON interface id
+ :return: alloc id resource path
+ """
+ return PONResourceManager.ALLOC_ID_POOL_PATH.format(
+ self.technology, self.device_id, pon_intf_id)
+
+ def _get_gemport_id_resource_path(self, pon_intf_id):
+ """
+ Get gemport id resource path.
+
+ :param pon_intf_id: OLT PON interface id
+ :return: gemport id resource path
+ """
+ return PONResourceManager.GEMPORT_ID_POOL_PATH.format(
+ self.technology, self.device_id, pon_intf_id)
+
+ def _get_onu_id_resource_path(self, pon_intf_id):
+ """
+ Get onu id resource path.
+
+ :param pon_intf_id: OLT PON interface id
+ :return: onu id resource path
+ """
+ return PONResourceManager.ONU_ID_POOL_PATH.format(
+ self.technology, self.device_id, pon_intf_id)
+
+ @inlineCallbacks
+ def _update_resource(self, path, resource):
+ """
+ Update resource in resource kv store.
+
+ :param path: path to update resource
+ :param resource: resource need to be updated
+ :return boolean: True if resource updated in kv store else False
+ """
+ resource[PONResourceManager.POOL] = \
+ resource[PONResourceManager.POOL].bin
+ result = yield self._kv_store.put(path, json.dumps(resource))
+ if result is None:
+ returnValue(True)
+ returnValue(False)
+
+ @inlineCallbacks
+ def _get_resource(self, path):
+ """
+ Get resource from kv store.
+
+ :param path: path to get resource
+ :return: resource if resource present in kv store else None
+ """
+ # get resource from kv store
+ result = yield self._kv_store.get(path)
+ resource = result[0]
+
+ if resource is not None:
+ # decode resource fetched from backend store to dictionary
+ resource = eval(resource.value)
+
+ # resource pool in backend store stored as binary string whereas to
+ # access the pool to generate/release IDs it need to be converted
+ # as BitArray
+ resource[PONResourceManager.POOL] = \
+ BitArray('0b' + resource[PONResourceManager.POOL])
+
+ returnValue(resource)
+
+ def _format_resource(self, pon_intf_id, start_idx, end_idx):
+ """
+ Format resource as json.
+
+ :param pon_intf_id: OLT PON interface id
+ :param start_idx: start index for id pool
+ :param end_idx: end index for id pool
+ :return dictionary: resource formatted as dictionary
+ """
+ # Format resource as json to be stored in backend store
+ resource = dict()
+ resource[PONResourceManager.PON_INTF_ID] = pon_intf_id
+ resource[PONResourceManager.START_IDX] = start_idx
+ resource[PONResourceManager.END_IDX] = end_idx
+
+ # resource pool stored in backend store as binary string
+ resource[PONResourceManager.POOL] = BitArray(end_idx).bin
+
+ return json.dumps(resource)
diff --git a/tests/utests/common/test_pon_resource_manager.py b/tests/utests/common/test_pon_resource_manager.py
new file mode 100644
index 0000000..74c4736
--- /dev/null
+++ b/tests/utests/common/test_pon_resource_manager.py
@@ -0,0 +1,169 @@
+#
+# Copyright 2018 the original author or authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+import json
+from unittest import TestCase, main
+
+from bitstring import BitArray
+from common.kvstore.kv_client import KVPair
+from common.pon_resource_manager.resource_manager import PONResourceManager
+from mock import Mock
+from twisted.internet.defer import inlineCallbacks
+
+
+class TestResourceManager(TestCase):
+ def setUp(self):
+ self._rm = PONResourceManager('xgspon', 'default',
+ '0001c889ee7189fb', 'consul',
+ 'localhost', 8500)
+ self.default_resource_range = {
+ "onu_start_idx": 1,
+ "onu_end_idx": 127,
+ "alloc_id_start_idx": 1024,
+ "alloc_id_end_idx": 65534,
+ "gem_port_id_start_idx": 1024,
+ "gem_port_id_end_idx": 16383,
+ "num_of_pon_port": 16
+ }
+
+ def tearDown(self):
+ self._rm = None
+ self.default_resource_range = None
+
+ @inlineCallbacks
+ def test_init_pon_resource_ranges(self):
+ key = PONResourceManager.PON_RESOURCE_RANGE_CONFIG_PATH.format(
+ 'xgspon', 'default')
+ value = json.dumps(self.default_resource_range).encode('utf-8')
+ output = KVPair(key, value, None)
+ self._rm._kv_store.get = Mock(return_value=(output, None))
+
+ yield self._rm.init_pon_resource_ranges()
+ self.assertEqual(self._rm.pon_resource_ranges,
+ self.default_resource_range)
+
+ self._rm._kv_store.get = Mock(return_value=(None, None))
+
+ yield self._rm.init_pon_resource_ranges()
+ self.assertEqual(self._rm.pon_resource_ranges,
+ self.default_resource_range)
+
+ @inlineCallbacks
+ def test_init_resource_id_pool(self):
+ self._rm._kv_store.get = Mock(return_value=(None, None))
+ self._rm._kv_store.put = Mock(return_value=None)
+ status = yield self._rm.init_resource_id_pool(0, 'ONU_ID', 1, 127)
+ self.assertTrue(status)
+ status = yield self._rm.init_resource_id_pool(
+ 1, 'ALLOC_ID', 1024, 16383)
+ self.assertTrue(status)
+ status = yield self._rm.init_resource_id_pool(
+ 2, 'GEMPORT_ID', 1023, 65534)
+ self.assertTrue(status)
+
+ @inlineCallbacks
+ def test_get_resource_id(self):
+ # Get onu id test
+ onu_id_resource = self._rm._format_resource(0, 1, 127)
+ key = self._rm._get_path(0, PONResourceManager.ONU_ID)
+ value = onu_id_resource.encode('utf-8')
+ output = KVPair(key, value, None)
+ self._rm._kv_store.get = Mock(return_value=(output, None))
+ self._rm._kv_store.put = Mock(return_value=None)
+ result = yield self._rm.get_resource_id(0, 'ONU_ID')
+ self.assertEqual(result, 1)
+
+ # Get alloc id test
+ alloc_id_resource = self._rm._format_resource(1, 1024, 16383)
+ key = self._rm._get_path(1, PONResourceManager.ALLOC_ID)
+ value = alloc_id_resource.encode('utf-8')
+ output = KVPair(key, value, None)
+ self._rm._kv_store.get = Mock(return_value=(output, None))
+ result = yield self._rm.get_resource_id(1, 'ALLOC_ID', 1)
+ self.assertEqual(result[0], 1024)
+ result = yield self._rm.get_resource_id(1, 'ALLOC_ID', 4)
+ self.assertEqual(result, [1024, 1025, 1026, 1027])
+
+ # Get gemport id test
+ gemport_id_resource = self._rm._format_resource(2, 1023, 65534)
+ key = self._rm._get_path(2, PONResourceManager.GEMPORT_ID)
+ value = gemport_id_resource.encode('utf-8')
+ output = KVPair(key, value, None)
+ self._rm._kv_store.get = Mock(return_value=(output, None))
+ result = yield self._rm.get_resource_id(2, 'GEMPORT_ID', 1)
+ self.assertEqual(result[0], 1023)
+ result = yield self._rm.get_resource_id(2, 'GEMPORT_ID', 5)
+ self.assertEqual(result, [1023, 1024, 1025, 1026, 1027])
+
+ @inlineCallbacks
+ def test_free_resource_id(self):
+ # Free onu id test
+ self._rm._kv_store.put = Mock(return_value=None)
+ onu_id_resource = eval(self._rm._format_resource(0, 1, 127))
+ onu_id_resource['pool'] = BitArray('0b' + onu_id_resource['pool'])
+ self._rm._generate_next_id(onu_id_resource)
+ onu_id_resource['pool'] = onu_id_resource['pool'].bin
+ key = self._rm._get_path(0, PONResourceManager.ONU_ID)
+ value = json.dumps(onu_id_resource).encode('utf-8')
+ output = KVPair(key, value, None)
+ self._rm._kv_store.get = Mock(return_value=(output, None))
+ result = yield self._rm.free_resource_id(0, 'ONU_ID', 1)
+ self.assertTrue(result)
+
+ # Free alloc id test
+ alloc_id_resource = eval(self._rm._format_resource(1, 1024, 16383))
+ alloc_id_resource['pool'] = BitArray('0b' + alloc_id_resource['pool'])
+
+ for num in range(5):
+ self._rm._generate_next_id(alloc_id_resource)
+
+ alloc_id_resource['pool'] = alloc_id_resource['pool'].bin
+ key = self._rm._get_path(0, PONResourceManager.ALLOC_ID)
+ value = json.dumps(alloc_id_resource).encode('utf-8')
+ output = KVPair(key, value, None)
+ self._rm._kv_store.get = Mock(return_value=(output, None))
+ result = self._rm.free_resource_id(1, 'ALLOC_ID',
+ [1024, 1025, 1026, 1027, 1028])
+ self.assertTrue(result)
+
+ # Free gemport id test
+ gemport_id_resource = eval(self._rm._format_resource(2, 1023, 65534))
+ gemport_id_resource['pool'] = BitArray(
+ '0b' + gemport_id_resource['pool'])
+
+ for num in range(6):
+ self._rm._generate_next_id(gemport_id_resource)
+
+ gemport_id_resource['pool'] = gemport_id_resource['pool'].bin
+ key = self._rm._get_path(0, PONResourceManager.GEMPORT_ID)
+ value = json.dumps(gemport_id_resource).encode('utf-8')
+ output = KVPair(key, value, None)
+ self._rm._kv_store.get = Mock(return_value=(output, None))
+ result = self._rm.free_resource_id(2, 'GEMPORT_ID',
+ [1023, 1024, 1025, 1026, 1027, 1028])
+ self.assertTrue(result)
+
+ @inlineCallbacks
+ def test_clear_resource_id_pool(self):
+ self._rm._kv_store.delete = Mock(return_value=None)
+ status = yield self._rm.clear_resource_id_pool(0, 'ONU_ID')
+ self.assertTrue(status)
+ self._rm._kv_store.delete = Mock(return_value="error")
+ status = yield self._rm.clear_resource_id_pool(1, 'ALLOC_ID')
+ self.assertFalse(status)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/voltha/adapters/openolt/README.md b/voltha/adapters/openolt/README.md
new file mode 100644
index 0000000..5e174d3
--- /dev/null
+++ b/voltha/adapters/openolt/README.md
@@ -0,0 +1,65 @@
+# OpenOLT Device Adapter
+
+## Enable OpenOLT
+To preprovision and enable the OpenOLT use the below commands from the Voltha CLI.
+```bash
+ (voltha) preprovision_olt -t openolt -H YOUR_OLT_MGMT_IP:9191
+ (voltha) enable
+```
+
+### Additional Notes
+1. The `bal_core_dist` and openolt driver should be running on the OLT device, before enabling the device from VOLTHA CLI.
+2. 9191 is the TCP port that the OpenOLT driver uses for its gRPC channel
+3. In the commands above, you can either use the loopback IP address (127.0.0.1) or substitute all its occurrences with the management IP of your OLT
+
+## Using Resource Manager with Open OLT adapter
+Resource Manager is used to manage device PON resource pool and allocate PON resources
+from such pools. Resource Manager module currently manages assignment of ONU-ID, ALLOC-ID and GEM-PORT ID.
+The Resource Manager uses the KV store to back-up all the resource pool allocation data.
+
+The OpenOLT adapter interacts with Resource Manager module for PON resource assignments.
+The `openolt_resource_manager` module is responsible for interfacing with the Resource Manager.
+
+The Resource Manager optionally uses `olt_vendor_type` specific resource ranges to initialize the PON resource pools.
+In order to utilize this option, create an entry for `olt_vendor_type` specific PON resource ranges on the KV store.
+Please make sure to use the same KV store used by the VOLTHA core.
+
+### For example
+To specify ASFvOLT16 OLT device specific resource ranges, first create a JSON file `asfvolt16_resource_range.json` with the following entry
+```bash
+{
+ "onu_start_idx": 1,
+ "onu_end_idx": 127,
+ "alloc_id_start_idx": 1024,
+ "alloc_id_end_idx": 65534,
+ "gem_port_id_start_idx": 1024,
+ "gem_port_id_end_idx": 16383,
+ "num_of_pon_port": 16
+}
+```
+This data should be put on the KV store location `resource_manager/xgspon/resource_ranges/asfvolt16`
+
+The format of the KV store location is `resource_manager/<technology>/resource_ranges/<olt_vendor_type>`
+
+In the below example the KV store is assumed to be Consul. However the same is applicable to be etcd or any other KV store.
+Please make sure to use the same KV store used by the VOLTHA core.
+```bash
+curl -X PUT -H "Content-Type: application/json" http://127.0.0.1:8500/v1/kv/resource_manager/xgspon/resource_ranges/asfvolt16 -d @./asfvolt16_resource_range.json
+```
+
+The `olt_vendor_type` should be referred to during the preprovisiong step as shown below. The `olt_vendor_type` is an extra option and
+should be specified after `--`. The `-o` specifies the `olt_vendor_type`.
+
+```bash
+ (voltha) preprovision_olt -t openolt -H 192.168.50.100:9191 -- -o asfvolt16
+```
+
+Once the OLT device is enabled, any further PON Resource assignments will happen within the PON Resource ranges defined
+in `asfvolt16_resource_range.json` and placed on the KV store.
+
+#### Additional Notes
+If a `default` resource range profile should be used with all `olt_vendor_type`s, then place such Resource Range profile
+at the below path on the KV store.
+```bash
+resource_manager/xgspon/resource_ranges/default
+```
diff --git a/voltha/adapters/openolt/openolt_device.py b/voltha/adapters/openolt/openolt_device.py
index 2bf6047..20ea471 100644
--- a/voltha/adapters/openolt/openolt_device.py
+++ b/voltha/adapters/openolt/openolt_device.py
@@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
-
+import sys
import threading
import binascii
import grpc
@@ -22,6 +22,7 @@
from twisted.internet import reactor
from scapy.layers.l2 import Ether, Dot1Q
from transitions import Machine
+from twisted.internet.defer import inlineCallbacks
from voltha.protos.device_pb2 import Port, Device
from voltha.protos.common_pb2 import OperStatus, AdminState, ConnectStatus
@@ -44,7 +45,9 @@
DEFAULT_MGMT_VLAN
from voltha.adapters.openolt.openolt_alarms import OpenOltAlarmMgr
from voltha.adapters.openolt.openolt_bw import OpenOltBW
+from common.pon_resource_manager.resource_manager import PONResourceManager
from voltha.extensions.alarms.onu.onu_discovery_alarm import OnuDiscoveryAlarm
+from voltha.adapters.openolt.openolt_resource_manager import OpenOltResourceMgr
class OpenoltDevice(object):
@@ -96,6 +99,7 @@
is_reconciliation = kwargs.get('reconciliation', False)
self.device_id = device.id
self.host_and_port = device.host_and_port
+ self.extra_args = device.extra_args
self.log = structlog.get_logger(id=self.device_id,
ip=self.host_and_port)
self.proxy = registry('core').get_proxy('/')
@@ -180,6 +184,7 @@
except Exception as e:
self.log.exception('post_init failed', e=e)
+ @inlineCallbacks
def do_state_connected(self, event):
self.log.debug("do_state_connected")
@@ -195,10 +200,15 @@
device.hardware_version = device_info.hardware_version
device.firmware_version = device_info.firmware_version
+ self.resource_manager = OpenOltResourceMgr(self.device_id,
+ self.host_and_port,
+ self.extra_args,
+ device_info)
self.flow_mgr = OpenOltFlowMgr(self.log, self.stub, self.device_id,
- self.logical_device_id)
+ self.logical_device_id,
+ self.resource_manager)
- # TODO: use content of device_info for Resource manager (VOL-948)
+ yield self._initialize_resource_manager_resource_pools()
# TODO: check for uptime and reboot if too long (VOL-1192)
@@ -278,7 +288,6 @@
self.log.debug('post_down')
self.flow_mgr.reset_flows()
-
def indications_thread(self):
self.log.debug('starting-indications-thread')
self.log.debug('connecting to olt', device_id=self.device_id)
@@ -377,6 +386,7 @@
# FIXME - handle PON oper state change
pass
+ @inlineCallbacks
def onu_discovery_indication(self, onu_disc_indication):
intf_id = onu_disc_indication.intf_id
serial_number = onu_disc_indication.serial_number
@@ -400,14 +410,32 @@
serial_number=serial_number_str)
if onu_device is None:
- onu_id = self.new_onu_id(intf_id)
try:
+ onu_id = yield self.resource_manager.get_resource_id(
+ intf_id, PONResourceManager.ONU_ID)
+ if onu_id is None:
+ raise Exception("onu-id-unavailable")
+
+ pon_intf_onu_id = (intf_id, onu_id)
+ self.resource_manager.init_resource_store(pon_intf_onu_id)
+
+ alloc_id = yield self.resource_manager.get_alloc_id(
+ pon_intf_onu_id)
+ if alloc_id is None:
+ # Free up other PON resources if are unable to
+ # proceed ahead
+ self.resource_manager.free_resource_id(
+ intf_id, PONResourceManager.ONU_ID, onu_id
+ )
+ raise Exception("alloc-id-unavailable")
+
self.add_onu_device(
intf_id,
platform.intf_id_to_port_no(intf_id, Port.PON_OLT),
onu_id, serial_number)
+ # Use sched_id same as alloc_id for the ONU.
self.activate_onu(intf_id, onu_id, serial_number,
- serial_number_str)
+ serial_number_str, alloc_id, alloc_id)
except Exception as e:
self.log.exception('onu-activation-failed', e=e)
@@ -436,8 +464,12 @@
onu_device.oper_status = OperStatus.DISCOVERED
self.adapter_agent.update_device(onu_device)
try:
+ pon_intf_onu_id = (intf_id, onu_id)
+ alloc_id = yield self.resource_manager.get_alloc_id(
+ pon_intf_onu_id)
+ # Use sched_id same as alloc_id for the ONU.
self.activate_onu(intf_id, onu_id, serial_number,
- serial_number_str)
+ serial_number_str, alloc_id, alloc_id)
except Exception as e:
self.log.error('onu-activation-error',
serial_number=serial_number_str, error=e)
@@ -445,6 +477,7 @@
self.log.warn('unexpected state', onu_id=onu_id,
onu_device_oper_state=onu_device.oper_status)
+ @inlineCallbacks
def onu_indication(self, onu_indication):
self.log.debug("onu indication", intf_id=onu_indication.intf_id,
onu_id=onu_indication.onu_id,
@@ -473,6 +506,12 @@
onu_id=onu_indication.onu_id)
return
+
+ # We will use this alloc_id and gemport_id to pass on to the onu adapter
+ pon_intf_onu_id = (onu_indication.intf_id, onu_indication.onu_id)
+ alloc_id = yield self.resource_manager.get_alloc_id(pon_intf_onu_id)
+ gemport_id = yield self.resource_manager.get_gemport_id(pon_intf_onu_id)
+
if platform.intf_id_from_pon_port_no(onu_device.parent_port_no) \
!= onu_indication.intf_id:
self.log.warn('ONU-is-on-a-different-intf-id-now',
@@ -574,14 +613,11 @@
# tcont creation (onu)
tcont = TcontsConfigData()
- tcont.alloc_id = platform.mk_alloc_id(
- onu_indication.intf_id, onu_indication.onu_id)
+ tcont.alloc_id = alloc_id
# gem port creation
gem_port = GemportsConfigData()
- gem_port.gemport_id = platform.mk_gemport_id(
- onu_indication.intf_id,
- onu_indication.onu_id)
+ gem_port.gemport_id = gemport_id
# ports creation/update
def port_config():
@@ -625,14 +661,12 @@
# tcont creation (onu)
tcont = TcontsConfigData()
- tcont.alloc_id = platform.mk_alloc_id(
- onu_indication.intf_id, onu_indication.onu_id)
+ tcont.alloc_id = alloc_id
# gem port creation
gem_port = GemportsConfigData()
- gem_port.gemport_id = platform.mk_gemport_id(
- onu_indication.intf_id,
- onu_indication.onu_id)
+ gem_port.gemport_id = gemport_id
+
gem_port.tcont_ref = str(tcont.alloc_id)
self.log.info('inject-tcont-gem-data-onu-handler',
@@ -696,7 +730,7 @@
onu_device = self.adapter_agent.get_child_device(
self.device_id, onu_id=omci_indication.onu_id,
parent_port_no=platform.intf_id_to_port_no(
- omci_indication.intf_id, Port.PON_OLT),)
+ omci_indication.intf_id, Port.PON_OLT), )
self.adapter_agent.receive_proxied_message(onu_device.proxy_address,
omci_indication.pkt)
@@ -706,8 +740,17 @@
self.log.debug("packet indication", intf_id=pkt_indication.intf_id,
gemport_id=pkt_indication.gemport_id,
flow_id=pkt_indication.flow_id)
+ onu_id = None
- onu_id = platform.onu_id_from_gemport_id(pkt_indication.gemport_id)
+ pon_intf_gemport = (pkt_indication.intf_id, pkt_indication.gemport_id)
+ try:
+ onu_id = self.resource_manager.pon_intf_gemport_to_onu_id_map[
+ pon_intf_gemport]
+ except KeyError:
+ self.log.error("no-onu-reference-for-gem",
+ gemport_id=pkt_indication.gemport_id)
+ return
+
logical_port_num = platform.mk_uni_port_num(pkt_indication.intf_id,
onu_id)
@@ -773,10 +816,10 @@
def send_proxied_message(self, proxy_address, msg):
onu_device = self.adapter_agent.get_child_device(
- self.device_id,
- onu_id=proxy_address.onu_id,
- parent_port_no=platform.intf_id_to_port_no(
- proxy_address.channel_id, Port.PON_OLT))
+ self.device_id, onu_id=proxy_address.onu_id,
+ parent_port_no=platform.intf_id_to_port_no(
+ proxy_address.channel_id, Port.PON_OLT)
+ )
if onu_device.connect_status != ConnectStatus.REACHABLE:
self.log.debug('ONU is not reachable, cannot send OMCI',
serial_number=onu_device.serial_number,
@@ -900,19 +943,6 @@
self.adapter_agent.delete_port(self.device_id, port)
return
- def new_onu_id(self, intf_id):
- onu_devices = self.adapter_agent.get_child_devices(self.device_id)
- pon_onu_ids = [onu_device.proxy_address.onu_id
- for onu_device in onu_devices
- if onu_device.proxy_address.channel_id == intf_id]
- for i in range(1, platform.MAX_ONUS_PER_PON):
- if i not in pon_onu_ids:
- return i
-
- self.log.error('All available onu_ids taken on this pon',
- intf_id=intf_id, ids_taken=platform.MAX_ONUS_PER_PON)
- return None
-
def update_flow_table(self, flows):
self.log.debug('No updates here now, all is done in logical flows '
'update')
@@ -1005,6 +1035,10 @@
self.log.info('deleting-olt', device_id=self.device_id,
logical_device_id=self.logical_device_id)
+ # Removes all data from the resource manager KV store
+ # for the device.
+ self.resource_manager.clear_device_resource_pool()
+
try:
# Rebooting to reset the state
self.reboot()
@@ -1031,16 +1065,18 @@
self.log.info('openolt device reenabled')
def activate_onu(self, intf_id, onu_id, serial_number,
- serial_number_str):
+ serial_number_str, agg_port_id, sched_id):
pir = self.bw_mgr.pir(serial_number_str)
self.log.debug("activating-onu", intf_id=intf_id, onu_id=onu_id,
serial_number_str=serial_number_str,
serial_number=serial_number, pir=pir)
onu = openolt_pb2.Onu(intf_id=intf_id, onu_id=onu_id,
- serial_number=serial_number, pir=pir)
+ serial_number=serial_number, pir=pir,
+ agg_port_id=agg_port_id, sched_id=sched_id)
self.stub.ActivateOnu(onu)
self.log.info('onu-activated', serial_number=serial_number_str)
+ @inlineCallbacks
def delete_child_device(self, child_device):
self.log.debug('sending-deactivate-onu',
olt_device_id=self.device_id,
@@ -1062,11 +1098,25 @@
self.log.error('port delete error', error=e)
serial_number = self.destringify_serial_number(
child_device.serial_number)
+ pon_intf_id_onu_id = (child_device.proxy_address.channel_id,
+ child_device.proxy_address.onu_id)
+ alloc_id = yield self.resource_manager.get_alloc_id(pon_intf_id_onu_id)
+ # Use sched_id same as alloc_id for the Onu.
onu = openolt_pb2.Onu(intf_id=child_device.proxy_address.channel_id,
onu_id=child_device.proxy_address.onu_id,
- serial_number=serial_number)
+ serial_number=serial_number,
+ agg_port_id=alloc_id, sched_id=alloc_id)
self.stub.DeleteOnu(onu)
+ # Remove reference to (pon_intf_id, onu_id) from the dictionary
+ # when ONU is being removed.
+ del self.resource_manager.pon_intf_id_onu_id_to_resource_map[
+ pon_intf_id_onu_id]
+ # Free any PON resources that were reserved for the ONU
+ self.resource_manager.free_pon_resources_for_onu(
+ child_device.proxy_address.channel_id,
+ child_device.proxy_address.onu_id)
+
def reboot(self):
self.log.debug('rebooting openolt device', device_id=self.device_id)
try:
@@ -1087,3 +1137,14 @@
def simulate_alarm(self, alarm):
self.alarm_mgr.simulate_alarm(alarm)
+
+ @inlineCallbacks
+ def _initialize_resource_manager_resource_pools(self):
+ try:
+ yield self.resource_manager.initialize_device_resource_range_and_pool()
+ except Exception as e:
+ # When resource manager initialization fails we can't proceed
+ # further so, exiting without instantiating openolt adapter
+ self.log.exception("Resource-manager-initialization-failed", e=e)
+ sys.exit(1)
+
diff --git a/voltha/adapters/openolt/openolt_flow_mgr.py b/voltha/adapters/openolt/openolt_flow_mgr.py
index 4c40910..7325ed2 100644
--- a/voltha/adapters/openolt/openolt_flow_mgr.py
+++ b/voltha/adapters/openolt/openolt_flow_mgr.py
@@ -16,6 +16,7 @@
import copy
from twisted.internet import reactor
import grpc
+from twisted.internet.defer import inlineCallbacks, returnValue
from voltha.protos.openflow_13_pb2 import OFPXMC_OPENFLOW_BASIC, \
ofp_flow_stats, OFPMT_OXM, Flows, FlowGroups, OFPXMT_OFB_IN_PORT, \
@@ -23,6 +24,7 @@
from voltha.protos.device_pb2 import Port
import voltha.core.flow_decomposer as fd
import openolt_platform as platform
+from common.pon_resource_manager.resource_manager import PONResourceManager
from voltha.adapters.openolt.protos import openolt_pb2
from voltha.registry import registry
@@ -44,7 +46,7 @@
class OpenOltFlowMgr(object):
- def __init__(self, log, stub, device_id, logical_device_id):
+ def __init__(self, log, stub, device_id, logical_device_id, resource_mgr):
self.log = log
self.stub = stub
self.device_id = device_id
@@ -209,6 +211,7 @@
self.log.debug('no device flow to remove for this flow (normal '
'for multi table flows)', flow=flow)
+ @inlineCallbacks
def divide_and_add_flow(self, intf_id, onu_id, classifier,
action, flow):
@@ -218,7 +221,7 @@
if 'ip_proto' in classifier:
if classifier['ip_proto'] == 17:
self.log.debug('dhcp flow add')
- self.add_dhcp_trap(intf_id, onu_id, classifier,
+ yield self.add_dhcp_trap(intf_id, onu_id, classifier,
action, flow)
elif classifier['ip_proto'] == 2:
self.log.warn('igmp flow add ignored, not implemented yet')
@@ -229,48 +232,49 @@
elif 'eth_type' in classifier:
if classifier['eth_type'] == EAP_ETH_TYPE:
self.log.debug('eapol flow add')
- self.add_eapol_flow(intf_id, onu_id, flow)
+ yield self.add_eapol_flow(intf_id, onu_id, flow)
vlan_id = self.get_subscriber_vlan(fd.get_in_port(flow))
if vlan_id is not None:
- self.add_eapol_flow(
+ yield self.add_eapol_flow(
intf_id, onu_id, flow,
uplink_eapol_id=EAPOL_UPLINK_SECONDARY_FLOW_INDEX,
downlink_eapol_id=EAPOL_DOWNLINK_SECONDARY_FLOW_INDEX,
vlan_id=vlan_id)
if classifier['eth_type'] == LLDP_ETH_TYPE:
self.log.debug('lldp flow add')
- self.add_lldp_flow(intf_id, onu_id, flow, classifier,
+ yield self.add_lldp_flow(intf_id, onu_id, flow, classifier,
action)
elif 'push_vlan' in action:
- self.add_upstream_data_flow(intf_id, onu_id, classifier, action,
- flow)
+ yield self.add_upstream_data_flow(intf_id, onu_id, classifier, action,
+ flow)
elif 'pop_vlan' in action:
- self.add_downstream_data_flow(intf_id, onu_id, classifier,
+ yield self.add_downstream_data_flow(intf_id, onu_id, classifier,
action, flow)
else:
self.log.debug('Invalid-flow-type-to-handle',
classifier=classifier,
action=action, flow=flow)
+ @inlineCallbacks
def add_upstream_data_flow(self, intf_id, onu_id, uplink_classifier,
uplink_action, logical_flow):
uplink_classifier['pkt_tag_type'] = 'single_tag'
- self.add_hsia_flow(intf_id, onu_id, uplink_classifier,
+ yield self.add_hsia_flow(intf_id, onu_id, uplink_classifier,
uplink_action, 'upstream', HSIA_FLOW_INDEX,
logical_flow)
# Secondary EAP on the subscriber vlan
(eap_active, eap_logical_flow) = self.is_eap_enabled(intf_id, onu_id)
if eap_active:
- self.add_eapol_flow(
- intf_id, onu_id, eap_logical_flow,
+ yield self.add_eapol_flow(intf_id, onu_id, eap_logical_flow,
uplink_eapol_id=EAPOL_UPLINK_SECONDARY_FLOW_INDEX,
downlink_eapol_id=EAPOL_DOWNLINK_SECONDARY_FLOW_INDEX,
vlan_id=uplink_classifier['vlan_vid'])
+ @inlineCallbacks
def add_downstream_data_flow(self, intf_id, onu_id, downlink_classifier,
downlink_action, flow):
downlink_classifier['pkt_tag_type'] = 'double_tag'
@@ -278,7 +282,7 @@
downlink_action['pop_vlan'] = True
downlink_action['vlan_vid'] = downlink_classifier['vlan_vid']
- self.add_hsia_flow(intf_id, onu_id, downlink_classifier,
+ yield self.add_hsia_flow(intf_id, onu_id, downlink_classifier,
downlink_action, 'downstream', HSIA_FLOW_INDEX,
flow)
@@ -286,21 +290,31 @@
# will take care of handling all the p bits.
# We need to revisit when mulitple gem port per p bits is needed.
# Waiting for Technology profile
+ @inlineCallbacks
def add_hsia_flow(self, intf_id, onu_id, classifier, action,
direction, hsia_id, logical_flow):
- gemport_id = platform.mk_gemport_id(intf_id, onu_id)
+ pon_intf_onu_id = (intf_id, onu_id)
+ gemport_id = yield self.resource_mgr.get_gemport_id(
+ pon_intf_onu_id=pon_intf_onu_id
+ )
+ alloc_id = yield self.resource_mgr.get_alloc_id(
+ pon_intf_onu_id=pon_intf_onu_id
+ )
+
flow_id = platform.mk_flow_id(intf_id, onu_id, hsia_id)
flow = openolt_pb2.Flow(
onu_id=onu_id, flow_id=flow_id, flow_type=direction,
access_intf_id=intf_id, gemport_id=gemport_id,
+ alloc_id=alloc_id,
priority=logical_flow.priority,
classifier=self.mk_classifier(classifier),
action=self.mk_action(action))
self.add_flow_to_device(flow, logical_flow)
+ @inlineCallbacks
def add_dhcp_trap(self, intf_id, onu_id, classifier, action, logical_flow):
self.log.debug('add dhcp upstream trap', classifier=classifier,
@@ -311,12 +325,19 @@
classifier['pkt_tag_type'] = 'single_tag'
classifier.pop('vlan_vid', None)
- gemport_id = platform.mk_gemport_id(intf_id, onu_id)
+ pon_intf_onu_id = (intf_id, onu_id)
+ gemport_id = yield self.resource_mgr.get_gemport_id(
+ pon_intf_onu_id=pon_intf_onu_id
+ )
+ alloc_id = yield self.resource_mgr.get_alloc_id(
+ pon_intf_onu_id=pon_intf_onu_id
+ )
flow_id = platform.mk_flow_id(intf_id, onu_id, DHCP_FLOW_INDEX)
upstream_flow = openolt_pb2.Flow(
onu_id=onu_id, flow_id=flow_id, flow_type="upstream",
access_intf_id=intf_id, gemport_id=gemport_id,
+ alloc_id=alloc_id,
priority=logical_flow.priority,
classifier=self.mk_classifier(classifier),
action=self.mk_action(action))
@@ -349,6 +370,7 @@
self.add_flow_to_device(downstream_flow, downstream_logical_flow)
+ @inlineCallbacks
def add_eapol_flow(self, intf_id, onu_id, logical_flow,
uplink_eapol_id=EAPOL_FLOW_INDEX,
downlink_eapol_id=EAPOL_DOWNLINK_FLOW_INDEX,
@@ -364,7 +386,13 @@
uplink_action = {}
uplink_action['trap_to_host'] = True
- gemport_id = platform.mk_gemport_id(intf_id, onu_id)
+ pon_intf_onu_id = (intf_id, onu_id)
+ gemport_id = yield self.resource_mgr.get_gemport_id(
+ pon_intf_onu_id=pon_intf_onu_id
+ )
+ alloc_id = yield self.resource_mgr.get_alloc_id(
+ pon_intf_onu_id=pon_intf_onu_id
+ )
# Add Upstream EAPOL Flow.
@@ -373,6 +401,7 @@
upstream_flow = openolt_pb2.Flow(
onu_id=onu_id, flow_id=uplink_flow_id, flow_type="upstream",
access_intf_id=intf_id, gemport_id=gemport_id,
+ alloc_id=alloc_id,
priority=logical_flow.priority,
classifier=self.mk_classifier(uplink_classifier),
action=self.mk_action(uplink_action))
@@ -439,6 +468,7 @@
def reset_flows(self):
self.flows_proxy.update('/', Flows())
+ @inlineCallbacks
def add_lldp_flow(self, intf_id, onu_id, logical_flow, classifier, action):
self.log.debug('add lldp downstream trap', classifier=classifier,
@@ -448,12 +478,20 @@
action['trap_to_host'] = True
classifier['pkt_tag_type'] = 'untagged'
- gemport_id = platform.mk_gemport_id(onu_id)
+ pon_intf_onu_id = (intf_id, onu_id)
+ gemport_id = yield self.resource_mgr.get_gemport_id(
+ pon_intf_onu_id=pon_intf_onu_id
+ )
+ alloc_id = yield self.resource_mgr.get_alloc_id(
+ pon_intf_onu_id=pon_intf_onu_id
+ )
+
flow_id = platform.mk_flow_id(intf_id, onu_id, LLDP_FLOW_INDEX)
downstream_flow = openolt_pb2.Flow(
onu_id=onu_id, flow_id=flow_id, flow_type="downstream",
access_intf_id=3, network_intf_id=0, gemport_id=gemport_id,
+ alloc_id=alloc_id,
priority=logical_flow.priority,
classifier=self.mk_classifier(classifier),
action=self.mk_action(action))
diff --git a/voltha/adapters/openolt/openolt_platform.py b/voltha/adapters/openolt/openolt_platform.py
index a7f6904..b179d90 100644
--- a/voltha/adapters/openolt/openolt_platform.py
+++ b/voltha/adapters/openolt/openolt_platform.py
@@ -21,34 +21,6 @@
Encoding of identifiers
=======================
-GEM port ID
-
- GEM port id is unique per PON port
-
- 10 3 0
- +--+--------------+------+
- |1 | onu id | GEM |
- | | | idx |
- +--+--------------+------+
-
- GEM port id range (0, 1023) is reserved
- onu id = 7 bits = 128 ONUs per PON
- GEM index = 3 bits = 8 GEM ports per ONU
-
-Alloc ID
-
- Uniquely identifies a T-CONT
- Ranges from 0 to 4095
- Unique per PON interface
-
- 12 6 0
- +------------+------------+
- | onu id | Alloc idx |
- +------------+------------+
-
- onu id = 7 bits = 128 ONUs per PON
- Alloc index = 6 bits = 64 GEM ports per ONU
-
Flow id
Identifies a flow within a single OLT
@@ -109,20 +81,6 @@
def mk_uni_port_num(intf_id, onu_id):
return intf_id << 11 | onu_id << 4
-
-def mk_alloc_id(intf_id, onu_id, idx=0):
- # FIXME - driver should do prefixing 1 << 10 as it is Maple specific
- # return 1<<10 | onu_id<<6 | idx
- return 1023 + intf_id * MAX_ONUS_PER_PON + onu_id # FIXME
-
-
-def mk_gemport_id(intf_id, onu_id, idx=0):
- return 1024 + (((MAX_ONUS_PER_PON * intf_id + onu_id - 1) * 7) + idx)
-
-def onu_id_from_gemport_id(gemport_id):
- return (((gemport_id - 1024) // 7) % MAX_ONUS_PER_PON) + 1
-
-
def mk_flow_id(intf_id, onu_id, idx):
return intf_id << 11 | onu_id << 4 | idx
@@ -184,3 +142,4 @@
return True
return False
+
diff --git a/voltha/adapters/openolt/openolt_resource_manager.py b/voltha/adapters/openolt/openolt_resource_manager.py
new file mode 100644
index 0000000..a85cbc5
--- /dev/null
+++ b/voltha/adapters/openolt/openolt_resource_manager.py
@@ -0,0 +1,276 @@
+#
+# Copyright 2018 the original author or authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+import shlex
+from argparse import ArgumentParser, ArgumentError
+
+import structlog
+from twisted.internet.defer import inlineCallbacks, returnValue
+
+from common.pon_resource_manager.resource_manager import PONResourceManager
+from voltha.registry import registry
+
+
+# Used to parse extra arguments to OpenOlt adapter from the NBI
+class OpenOltArgumentParser(ArgumentParser):
+ # Must override the exit command to prevent it from
+ # calling sys.exit(). Return exception instead.
+ def exit(self, status=0, message=None):
+ raise Exception(message)
+
+
+class OpenOltResourceMgr(object):
+
+ GEMPORT_IDS = "gemport_ids"
+ ALLOC_IDS = "alloc_ids"
+
+ def __init__(self, device_id, host_and_port, extra_args, device_info):
+ self.log = structlog.get_logger(id=device_id,
+ ip=host_and_port)
+ self.device_id = device_id
+ self.host_and_port = host_and_port
+ self.extra_args = extra_args
+ self.device_info = device_info
+ # Below dictionary maintains a map of tuple (pon_intf_id, onu_id)
+ # to list of gemports and alloc_ids.
+ # Note: This is a stateful information and will be lost across
+ # VOLTHA reboots. When adapter is containerized, this information
+ # should be backed up in an external K/V store.
+ '''
+ Example:
+ {
+ (pon_intf_id_1, onu_id_1): {
+ "gemport_ids": [1024,1025],
+ "alloc_ids" : [1024]
+ },
+ (pon_intf_id_1, onu_id_2): {
+ "gemport_ids": [1026],
+ "alloc_ids" : [1025]
+ }
+ }
+ '''
+ self.pon_intf_id_onu_id_to_resource_map = dict()
+ self.pon_intf_gemport_to_onu_id_map = dict()
+ self.resource_mgr = self.\
+ _parse_extra_args_and_init_resource_manager_class()
+ # Flag to indicate whether information fetched from device should
+ # be used to intialize PON Resource Ranges
+ self.use_device_info = False
+
+ def _parse_extra_args_and_init_resource_manager_class(self):
+ self.args = registry('main').get_args()
+
+ # KV store's IP Address and PORT
+ host, port = '127.0.0.1', 8500
+ if self.args.backend == 'etcd':
+ host, port = self.args.etcd.split(':', 1)
+ elif self.args.backend == 'consul':
+ host, port = self.args.consul.split(':', 1)
+
+ if self.extra_args and len(self.extra_args) > 0:
+ parser = OpenOltArgumentParser(add_help=False)
+ parser.add_argument('--openolt_variant', '-o', action='store',
+ choices=['default', 'asfvolt16'],
+ default='default')
+ try:
+ args = parser.parse_args(shlex.split(self.extra_args))
+ self.log.debug('parsing-extra-arguments', args=args)
+
+ try:
+ resource_manager = PONResourceManager(
+ self.device_info.technology,
+ args.openolt_variant,
+ self.device_id, self.args.backend,
+ host, port
+ )
+ except Exception as e:
+ raise Exception(e)
+
+ except ArgumentError as e:
+ raise Exception('invalid-arguments: {}'.format(e.message))
+
+ except Exception as e:
+ raise Exception(
+ 'option-parsing-error: {}'.format(e.message))
+ else:
+ try:
+ # OLT Vendor type not available, use device information
+ # to initialize PON resource ranges.
+ self.use_device_info = True
+
+ resource_manager = PONResourceManager(
+ self.device_info.technology,
+ self.device_info.vendor,
+ self.device_id, self.args.backend,
+ host, port
+ )
+ except Exception as e:
+ raise Exception(e)
+
+ return resource_manager
+
+ def init_resource_store(self, pon_intf_onu_id):
+ # Initialize the map to store the (pon_intf_id, onu_id) to gemport
+ # list and alloc_id list
+ if pon_intf_onu_id not in \
+ self.pon_intf_id_onu_id_to_resource_map.keys():
+ self.pon_intf_id_onu_id_to_resource_map[pon_intf_onu_id] = dict()
+ self.pon_intf_id_onu_id_to_resource_map[pon_intf_onu_id] \
+ [OpenOltResourceMgr.GEMPORT_IDS] = list()
+ self.pon_intf_id_onu_id_to_resource_map[pon_intf_onu_id] \
+ [OpenOltResourceMgr.ALLOC_IDS] = list()
+
+ @inlineCallbacks
+ def get_resource_id(self, pon_intf_id, resource_type, num_of_id=1):
+ resource = yield self.resource_mgr.get_resource_id(
+ pon_intf_id, resource_type, num_of_id)
+ returnValue(resource)
+
+ @inlineCallbacks
+ def free_resource_id(self, pon_intf_id, resource_type, release_content):
+ result = yield self.resource_mgr.free_resource_id(
+ pon_intf_id, resource_type, release_content)
+ returnValue(result)
+
+ @inlineCallbacks
+ def get_alloc_id(self, pon_intf_onu_id):
+ # Derive the pon_intf from the pon_intf_onu_id tuple
+ pon_intf = pon_intf_onu_id[0]
+ alloc_id = None
+
+ # Since we support only one alloc_id for the ONU at the moment,
+ # return the first alloc_id in the list, if available, for that
+ # ONU.
+ if len(self.pon_intf_id_onu_id_to_resource_map[pon_intf_onu_id] \
+ [OpenOltResourceMgr.ALLOC_IDS]) > 0:
+ alloc_id = self.pon_intf_id_onu_id_to_resource_map[
+ pon_intf_onu_id][OpenOltResourceMgr.ALLOC_IDS][0]
+ returnValue(alloc_id)
+
+ # get_alloc_id returns a list of alloc_id.
+ alloc_id_list = yield self.resource_mgr.get_resource_id(
+ pon_intf_id=pon_intf,
+ resource_type=PONResourceManager.ALLOC_ID,
+ num_of_id=1
+ )
+ if alloc_id_list and len(alloc_id_list) == 0:
+ self.log.error("no-alloc-id-available")
+ returnValue(alloc_id)
+
+ # store the alloc id list per (pon_intf_id, onu_id) tuple
+ self.pon_intf_id_onu_id_to_resource_map[pon_intf_onu_id] \
+ [OpenOltResourceMgr.ALLOC_IDS].extend(alloc_id_list)
+
+ # Since we request only one alloc id, we refer the 0th
+ # index
+ alloc_id = alloc_id_list[0]
+
+ returnValue(alloc_id)
+
+ @inlineCallbacks
+ def get_gemport_id(self, pon_intf_onu_id):
+ # Derive the pon_intf and onu_id from the pon_intf_onu_id tuple
+ pon_intf = pon_intf_onu_id[0]
+ onu_id = pon_intf_onu_id[1]
+ gemport = None
+
+ if len(self.pon_intf_id_onu_id_to_resource_map[pon_intf_onu_id] \
+ [OpenOltResourceMgr.GEMPORT_IDS]) > 0:
+ # Since we support only one gemport_id on the ONU at the moment,
+ # return the first gemport_id in the list, if available, for that
+ # ONU.
+ gemport = self.pon_intf_id_onu_id_to_resource_map[pon_intf_onu_id] \
+ [OpenOltResourceMgr.GEMPORT_IDS][0]
+ returnValue(gemport)
+
+ # get_gem_id returns a list of gem_id.
+ gemport_id_list = yield self.resource_mgr.get_resource_id(
+ pon_intf_id=pon_intf,
+ resource_type=PONResourceManager.GEMPORT_ID,
+ num_of_id=1
+ )
+
+ if gemport_id_list and len(gemport_id_list) == 0:
+ self.log.error("no-gemport-id-available")
+ returnValue(gemport)
+
+ # store the gem port id list per (pon_intf_id, onu_id) tuple
+ self.pon_intf_id_onu_id_to_resource_map[pon_intf_onu_id] \
+ [OpenOltResourceMgr.GEMPORT_IDS].extend(gemport_id_list)
+
+ # We currently use only one gemport
+ gemport = gemport_id_list[0]
+
+ pon_intf_gemport = (pon_intf, gemport)
+ # This information is used when packet_indication is received and
+ # we need to derive the ONU Id for which the packet arrived based
+ # on the pon_intf and gemport available in the packet_indication
+ self.pon_intf_gemport_to_onu_id_map[pon_intf_gemport] = onu_id
+
+ returnValue(gemport)
+
+ def free_pon_resources_for_onu(self, pon_intf_id, onu_id):
+ # Frees Alloc Ids and Gemport Ids from Resource Manager for
+ # a given onu on a particular pon port
+
+ pon_intf_id_onu_id = (pon_intf_id, onu_id)
+ alloc_ids = \
+ self.pon_intf_id_onu_id_to_resource_map[pon_intf_id_onu_id] \
+ [OpenOltResourceMgr.ALLOC_IDS]
+ gemport_ids = \
+ self.pon_intf_id_onu_id_to_resource_map[pon_intf_id_onu_id] \
+ [OpenOltResourceMgr.GEMPORT_IDS]
+ self.resource_mgr.free_resource_id(pon_intf_id,
+ PONResourceManager.ONU_ID,
+ onu_id)
+ self.resource_mgr.free_resource_id(pon_intf_id,
+ PONResourceManager.ALLOC_ID,
+ alloc_ids)
+ self.resource_mgr.free_resource_id(pon_intf_id,
+ PONResourceManager.GEMPORT_ID,
+ gemport_ids)
+
+ # We need to clear the mapping of (pon_intf_id, gemport_id) to onu_id
+ for gemport_id in gemport_ids:
+ del self.pon_intf_gemport_to_onu_id_map[(pon_intf_id, gemport_id)]
+
+ @inlineCallbacks
+ def initialize_device_resource_range_and_pool(self):
+ if not self.use_device_info:
+ status = yield self.resource_mgr.init_resource_ranges_from_kv_store()
+ if not status:
+ self.log.error("failed-to-load-resource-range-from-kv-store")
+ # When we have failed to read the PON Resource ranges from KV
+ # store, use the information fetched from device.
+ self.use_device_info = True
+
+ if self.use_device_info:
+ self.log.info("using-device-info-to-init-pon-resource-ranges")
+ self.resource_mgr.init_default_pon_resource_ranges(
+ self.device_info.onu_id_start,
+ self.device_info.onu_id_end,
+ self.device_info.alloc_id_start,
+ self.device_info.alloc_id_end,
+ self.device_info.gemport_id_start,
+ self.device_info.gemport_id_end,
+ self.device_info.pon_ports
+ )
+
+ # After we have initialized resource ranges, initialize the
+ # resource pools accordingly.
+ self.resource_mgr.init_device_resource_pool()
+
+ def clear_device_resource_pool(self):
+ self.resource_mgr.clear_device_resource_pool()
diff --git a/voltha/adapters/openolt/protos/openolt.proto b/voltha/adapters/openolt/protos/openolt.proto
index 45ca664..c99814b 100644
--- a/voltha/adapters/openolt/protos/openolt.proto
+++ b/voltha/adapters/openolt/protos/openolt.proto
@@ -218,6 +218,9 @@
fixed32 onu_id = 2;
SerialNumber serial_number = 3;
fixed32 pir = 4; // peak information rate assigned to onu
+ fixed32 agg_port_id = 5;
+ fixed32 sched_id = 6;
+
}
message OmciMsg {
@@ -293,6 +296,7 @@
string flow_type = 4; // upstream, downstream, broadcast, multicast
fixed32 network_intf_id = 5;
fixed32 gemport_id = 6;
+ fixed32 alloc_id = 10;
Classifier classifier = 7;
Action action = 8;
fixed32 priority = 9;