Commit 6903fd7f authored by Waleed Akbar's avatar Waleed Akbar
Browse files

feat(tools): Device Server - enhance endpoint population with default context and topology handling

parent 715d82d6
Loading
Loading
Loading
Loading
+22 −6
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@
import grpc, logging, os, time
from typing import Dict, List
from prometheus_client import Histogram
from common.Constants import ServiceNameEnum
from common.Constants import DEFAULT_CONTEXT_NAME, DEFAULT_TOPOLOGY_NAME, ServiceNameEnum
from common.Settings import ENVVAR_SUFIX_SERVICE_HOST, get_env_var_name
from common.method_wrappers.Decorator import MetricTypeEnum, MetricsPool, safe_and_metered_rpc_method
from common.method_wrappers.ServiceExceptions import NotFoundException, OperationFailedException
@@ -35,7 +35,7 @@ from .monitoring.MonitoringLoops import MonitoringLoops
from .ErrorMessages import ERROR_MISSING_DRIVER, ERROR_MISSING_KPI
from .Tools import (
    check_connect_rules, check_no_endpoints, compute_rules_to_add_delete, configure_rules,
    deconfigure_rules, get_device_controller_uuid, populate_config_rules,
    deconfigure_rules, get_connect_rules, get_device_controller_uuid, populate_config_rules,
    populate_endpoint_monitoring_resources, populate_endpoints, populate_initial_config_rules,
    subscribe_kpi, unsubscribe_kpi, update_endpoints, update_sap_id, _raw_config_rules_to_grpc
)
@@ -48,6 +48,21 @@ METRICS_POOL_DETAILS = MetricsPool('Device', 'execution', labels={
    'driver': '', 'operation': '', 'step': '',
})


def _connect_rule_or_default(connection_config_rules: Dict[str, str], key: str, default: str) -> str:
    value = str(connection_config_rules.get(key, default)).strip()
    return value or default


def _endpoint_topology_defaults(connection_config_rules: Dict[str, str]) -> Dict[str, str]:
    return {
        'default_context_uuid': _connect_rule_or_default(
            connection_config_rules, 'context_uuid', DEFAULT_CONTEXT_NAME),
        'default_topology_uuid': _connect_rule_or_default(
            connection_config_rules, 'topology_uuid', DEFAULT_TOPOLOGY_NAME),
    }


class DeviceServiceServicerImpl(DeviceServiceServicer):
    def __init__(self, driver_instance_cache : DriverInstanceCache, monitoring_loops : MonitoringLoops) -> None:
        LOGGER.debug('Creating Servicer...')
@@ -124,7 +139,8 @@ class DeviceServiceServicerImpl(DeviceServiceServicer):
                # created from request, populate endpoints using driver
                errors.extend(populate_endpoints(
                    device, driver, self.monitoring_loops, new_sub_devices, sorted_sub_device_uuids,
                    new_sub_links, new_optical_configs
                    new_sub_links, new_optical_configs,
                    **_endpoint_topology_defaults(connection_config_rules)
                ))
                t6 = time.time()
                t_pop_endpoints = t6 - t5
@@ -470,9 +486,11 @@ class DeviceServiceServicerImpl(DeviceServiceServicer):
            new_sub_links = dict()
            sorted_sub_device_uuids = list()
            new_optical_configs = dict()
            connection_config_rules = get_connect_rules(device.device_config)
            errors = populate_endpoints(
                device, driver, self.monitoring_loops, new_sub_devices, sorted_sub_device_uuids,
                new_sub_links, new_optical_configs
                new_sub_links, new_optical_configs,
                **_endpoint_topology_defaults(connection_config_rules)
            )
            if len(errors) > 0:
                raise OperationFailedException('UpdateDeviceInventory', extra_details=errors)
@@ -505,5 +523,3 @@ class DeviceServiceServicerImpl(DeviceServiceServicer):
        except Exception as e:
            LOGGER.exception('Error updating inventory of device {:s}'.format(str(device_uuid)))
            raise e

+11 −9
Original line number Diff line number Diff line
@@ -107,7 +107,9 @@ def get_device_controller_uuid(device : Device) -> Optional[str]:
def populate_endpoints(
    device : Device, driver : _Driver, monitoring_loops : MonitoringLoops,
    new_sub_devices : Dict[str, Device], sorted_sub_device_uuids : List[str],
    new_sub_links : Dict[str, Link], new_optical_configs : Dict[str, OpticalConfig]
    new_sub_links : Dict[str, Link], new_optical_configs : Dict[str, OpticalConfig],
    default_context_uuid : str = DEFAULT_CONTEXT_NAME,
    default_topology_uuid : str = DEFAULT_TOPOLOGY_NAME
) -> List[str]:
    device_uuid = device.device_id.device_uuid.uuid
    device_name = device.name
@@ -131,8 +133,8 @@ def populate_endpoints(
    if add_mgmt_port:
        # add mgmt port to main device
        device_mgmt_endpoint = device.device_endpoints.add()
        device_mgmt_endpoint.endpoint_id.topology_id.context_id.context_uuid.uuid = DEFAULT_CONTEXT_NAME
        device_mgmt_endpoint.endpoint_id.topology_id.topology_uuid.uuid = DEFAULT_TOPOLOGY_NAME
        device_mgmt_endpoint.endpoint_id.topology_id.context_id.context_uuid.uuid = default_context_uuid
        device_mgmt_endpoint.endpoint_id.topology_id.topology_uuid.uuid = default_topology_uuid
        device_mgmt_endpoint.endpoint_id.device_id.device_uuid.uuid = device_uuid
        device_mgmt_endpoint.endpoint_id.endpoint_uuid.uuid = 'mgmt'
        device_mgmt_endpoint.name = 'mgmt'
@@ -192,8 +194,8 @@ def populate_endpoints(
                _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
                _sub_device_mgmt_endpoint.endpoint_id.topology_id.context_id.context_uuid.uuid = default_context_uuid
                _sub_device_mgmt_endpoint.endpoint_id.topology_id.topology_uuid.uuid = default_topology_uuid
                _sub_device_mgmt_endpoint.endpoint_id.device_id.device_uuid.uuid = _sub_device_uuid
                _sub_device_mgmt_endpoint.endpoint_id.endpoint_uuid.uuid = 'mgmt'
                _sub_device_mgmt_endpoint.name = 'mgmt'
@@ -243,10 +245,10 @@ def populate_endpoints(

            device_endpoint.endpoint_id.endpoint_uuid.uuid = endpoint_uuid

            endpoint_context_uuid = resource_value.get('context_uuid', DEFAULT_CONTEXT_NAME)
            endpoint_context_uuid = resource_value.get('context_uuid', default_context_uuid) or default_context_uuid
            device_endpoint.endpoint_id.topology_id.context_id.context_uuid.uuid = endpoint_context_uuid

            endpoint_topology_uuid = resource_value.get('topology_uuid', DEFAULT_TOPOLOGY_NAME)
            endpoint_topology_uuid = resource_value.get('topology_uuid', default_topology_uuid) or default_topology_uuid
            device_endpoint.endpoint_id.topology_id.topology_uuid.uuid = endpoint_topology_uuid

            endpoint_name = resource_value.get('name')
@@ -294,8 +296,8 @@ def populate_endpoints(

            for _device_uuid,_endpoint_uuid in resource_value['endpoints']:
                _sub_link_endpoint_id = _sub_link.link_endpoint_ids.add()      # pylint: disable=no-member
                _sub_link_endpoint_id.topology_id.context_id.context_uuid.uuid = DEFAULT_CONTEXT_NAME
                _sub_link_endpoint_id.topology_id.topology_uuid.uuid = DEFAULT_TOPOLOGY_NAME
                _sub_link_endpoint_id.topology_id.context_id.context_uuid.uuid = default_context_uuid
                _sub_link_endpoint_id.topology_id.topology_uuid.uuid = default_topology_uuid
                _sub_link_endpoint_id.device_id.device_uuid.uuid = _device_uuid
                _sub_link_endpoint_id.endpoint_uuid.uuid = _endpoint_uuid