Commit 7201adf6 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Device component:

- Added support for underlying controller-managed devices
parent d3e79ddf
Loading
Loading
Loading
Loading
+65 −19
Original line number Diff line number Diff line
@@ -12,8 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import json, logging
from typing import Any, Dict, List, Optional, Tuple, Union
import json, logging, re
from typing import Any, Dict, List, Optional, Set, Tuple, Union
from common.Constants import DEFAULT_CONTEXT_NAME, DEFAULT_TOPOLOGY_NAME
from common.DeviceTypes import DeviceTypeEnum
from common.method_wrappers.ServiceExceptions import InvalidArgumentException, NotFoundException
@@ -123,6 +123,9 @@ def populate_endpoints(
            add_mgmt_port = True
            break

    devices_with_mgmt_endpoints : Set[str] = set()
    mgmt_links : Set[Tuple[str, str]] = set()

    if add_mgmt_port:
        # add mgmt port to main device
        device_mgmt_endpoint = device.device_endpoints.add()
@@ -133,6 +136,9 @@ def populate_endpoints(
        device_mgmt_endpoint.name = 'mgmt'
        device_mgmt_endpoint.endpoint_type = 'mgmt'

        devices_with_mgmt_endpoints.add(device_uuid)
        devices_with_mgmt_endpoints.add(device_name)

    errors : List[str] = list()
    for resource_data in results_getconfig:
        if len(resource_data) != 2:
@@ -150,9 +156,11 @@ def populate_endpoints(
        if resource_key.startswith('/devices/device'):
            # create sub-device
            _sub_device_uuid = resource_value['uuid']
            _sub_device_name = resource_value['name']
            _sub_device_ctrl = resource_value.get('ctrl_uuid')
            _sub_device = Device()
            _sub_device.device_id.device_uuid.uuid = _sub_device_uuid           # pylint: disable=no-member
            _sub_device.name = resource_value['name']
            _sub_device.name = _sub_device_name
            _sub_device.device_type = resource_value['type']
            _sub_device.device_operational_status = resource_value['status']
            
@@ -167,12 +175,20 @@ def populate_endpoints(
                    MSG = 'Unsupported drivers definition in sub-device({:s}, {:s})'
                    raise Exception(MSG.format(str(resource_key), str(resource_value)))

            if _sub_device_ctrl is not None:
                # Sub-device is managed by an underlying controller
                _sub_device.controller_id.device_uuid.uuid = _sub_device_ctrl
            else:
                # Sub-devices should always have a controller associated.
                _sub_device.controller_id.device_uuid.uuid = device_uuid

            new_sub_devices[_sub_device_uuid] = _sub_device

            # add mgmt port to sub-device
            if (
                _sub_device_uuid not in devices_with_mgmt_endpoints and
                _sub_device_name not in devices_with_mgmt_endpoints
            ):
                _sub_device_mgmt_endpoint = _sub_device.device_endpoints.add()      # pylint: disable=no-member
                _sub_device_mgmt_endpoint.endpoint_id.topology_id.context_id.context_uuid.uuid = DEFAULT_CONTEXT_NAME
                _sub_device_mgmt_endpoint.endpoint_id.topology_id.topology_uuid.uuid = DEFAULT_TOPOLOGY_NAME
@@ -182,7 +198,8 @@ def populate_endpoints(
                _sub_device_mgmt_endpoint.endpoint_type = 'mgmt'

            # add mgmt link
            _mgmt_link_uuid = '{:s}/{:s}=={:s}/{:s}'.format(device_name, 'mgmt', _sub_device.name, 'mgmt')
            if (device_name, _sub_device_name) not in mgmt_links:
                _mgmt_link_uuid = '{:s}/{:s}=={:s}/{:s}'.format(device_name, 'mgmt', _sub_device_name, 'mgmt')
                _mgmt_link = Link()
                _mgmt_link.link_id.link_uuid.uuid = _mgmt_link_uuid                         # pylint: disable=no-member
                _mgmt_link.name = _mgmt_link_uuid
@@ -190,9 +207,20 @@ def populate_endpoints(
                _mgmt_link.link_endpoint_ids.append(_sub_device_mgmt_endpoint.endpoint_id)  # pylint: disable=no-member
                new_sub_links[_mgmt_link_uuid] = _mgmt_link

                mgmt_links.add((device_name, _sub_device_name))

        elif resource_key.startswith('/endpoints/endpoint'):
            endpoint_uuid = resource_value['uuid']
            _device_uuid = resource_value.get('device_uuid')
            endpoint_name = resource_value.get('name')

            if endpoint_uuid == 'mgmt' or endpoint_name == 'mgmt':
                if _device_uuid is None:
                    if device_uuid in devices_with_mgmt_endpoints:
                        continue
                else:
                    if _device_uuid in devices_with_mgmt_endpoints:
                        continue

            if _device_uuid is None:
                # add endpoint to current device
@@ -225,7 +253,22 @@ def populate_endpoints(
            if location is not None:
                device_endpoint.endpoint_location.MergeFrom(Location(**location))

            if endpoint_uuid == 'mgmt' or endpoint_name == 'mgmt':
                if _device_uuid is None:
                    devices_with_mgmt_endpoints.add(device_uuid)
                    devices_with_mgmt_endpoints.add(device_name)
                else:
                    devices_with_mgmt_endpoints.add(_device_uuid)
                    devices_with_mgmt_endpoints.add(new_sub_devices[_device_uuid].name)

        elif resource_key.startswith('/links/link'):
            link_name = resource_value['name']
            mgmt_match = re.match(r'^([^\/]+)\/mgmt\=\=([^\/]+)\/mgmt$', link_name)
            if mgmt_match is not None:
                # is management link
                src_dev_name, dst_dev_name = mgmt_match.groups()
                if (src_dev_name, dst_dev_name) in mgmt_links: continue

            # create sub-link
            _sub_link_uuid = resource_value['uuid']
            _sub_link = Link()
@@ -240,6 +283,9 @@ def populate_endpoints(
                _sub_link_endpoint_id.device_id.device_uuid.uuid = device_uuid
                _sub_link_endpoint_id.endpoint_uuid.uuid = endpoint_uuid

            if mgmt_match is not None:
                mgmt_links.add((src_dev_name, dst_dev_name))

        # ----------Experimental --------------
        elif resource_key.startswith('/opticalconfigs/opticalconfig/'):
            new_optical_configs["new_optical_config"]=resource_value