Commit 603428ec authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

PathComp component - FrontEnd:

- Fixed Sub-Service Composition to identify hierarchies of controllers and use the top-level one in the underlying hierarchy
- Code polishing
parent 2944c65f
Loading
Loading
Loading
Loading
+15 −6
Original line number Diff line number Diff line
@@ -13,12 +13,12 @@
# limitations under the License.


import json
from typing import Dict, Optional, Tuple
from common.DeviceTypes import DeviceTypeEnum
from common.proto.context_pb2 import Device
from common.tools.grpc.Tools import grpc_message_to_json_string


DEVICE_TYPE_TO_DEEPNESS = {
    DeviceTypeEnum.EMULATED_DATACENTER.value             : 90,
    DeviceTypeEnum.DATACENTER.value                      : 90,
@@ -62,9 +62,11 @@ DEVICE_TYPE_TO_DEEPNESS = {
    DeviceTypeEnum.NETWORK.value                         :  0, # network out of our control; always delegate
}


IGNORED_DEVICE_TYPES = {DeviceTypeEnum.EMULATED_OPTICAL_SPLITTER}
REMOTEDOMAIN_DEVICE_TYPES = {DeviceTypeEnum.NETWORK}


def get_device(
    device_dict : Dict[str, Tuple[Dict, Device]], device_uuid : str,
    fail_if_not_found : bool = True
@@ -75,18 +77,22 @@ def get_device(
        raise Exception(MSG.format(str(device_uuid)))
    return device_tuple


def get_device_controller_uuid(
    device : Device, device_dict : Dict[str, Tuple[Dict, Device]],
    retrieve_top_level_controller : bool = True
    device : Device, device_dict : Dict[str, Tuple[Dict, Device]]
) -> Optional[str]:
    last_controller_uuid = None
    while True:
        controller_uuid = device.controller_id.device_uuid.uuid
        if retrieve_top_level_controller: return controller_uuid
        if len(controller_uuid) == 0: return None
        if len(controller_uuid) == 0: return last_controller_uuid
        controller_tuple = get_device(device_dict, controller_uuid, fail_if_not_found=False)
        if controller_tuple is None: return controller_uuid
        if controller_tuple is None:
            MSG = 'Unable to find referenced Controller({:s})'
            raise Exception(MSG.format(str(controller_uuid)))
        last_controller_uuid = controller_uuid
        _, device = controller_tuple


def _map_device_type(device : Device) -> DeviceTypeEnum:
    device_type = DeviceTypeEnum._value2member_map_.get(device.device_type) # pylint: disable=no-member
    if device_type is None:
@@ -94,11 +100,13 @@ def _map_device_type(device : Device) -> DeviceTypeEnum:
        raise Exception(MSG.format(str(device.device_type), grpc_message_to_json_string(device)))
    return device_type


def _map_resource_to_deepness(device_type : DeviceTypeEnum) -> int:
    deepness = DEVICE_TYPE_TO_DEEPNESS.get(device_type.value)
    if deepness is None: raise Exception('Unsupported DeviceType({:s})'.format(str(device_type.value)))
    return deepness


def get_device_type(
    device : Device, device_dict : Dict[str, Tuple[Dict, Device]], device_controller_uuid : Optional[str]
) -> DeviceTypeEnum:
@@ -106,6 +114,7 @@ def get_device_type(
    _,device = get_device(device_dict, device_controller_uuid)
    return _map_device_type(device)


def get_resource_classification(
    device : Device, device_dict : Dict[str, Tuple[Dict, Device]]
) -> Tuple[int, DeviceTypeEnum, Optional[str]]: