diff --git a/src/nbi/service/rest_server/nbi_plugins/ietf_network_slice/ietf_slice_handler.py b/src/nbi/service/rest_server/nbi_plugins/ietf_network_slice/ietf_slice_handler.py index 408aeeb01f0f539bf7b728d613cb67f677766bb7..46e2423c4a12ae785cb8bc610f9d76dea03ee662 100644 --- a/src/nbi/service/rest_server/nbi_plugins/ietf_network_slice/ietf_slice_handler.py +++ b/src/nbi/service/rest_server/nbi_plugins/ietf_network_slice/ietf_slice_handler.py @@ -8,6 +8,7 @@ from common.proto.context_pb2 import ( ConfigRule, Constraint, DeviceId, + Device, Empty, EndPointId, ServiceConfig, @@ -16,6 +17,8 @@ from common.proto.context_pb2 import ( ) from common.tools.context_queries.Slice import get_slice_by_defualt_name from common.tools.grpc.ConfigRules import update_config_rule_custom +from common.tools.object_factory.Device import json_device_id +from common.DeviceTypes import DeviceTypeEnum from context.client import ContextClient from .YangValidator import YangValidator @@ -29,6 +32,19 @@ ADDRESS_PREFIX = 24 RAISE_IF_DIFFERS = False +def get_endpoint_controller_type( + endpoint: EndPointId, context_client: ContextClient +) -> str: + endpoint_device: Device = context_client.GetDevice(endpoint.device_id) + if endpoint_device.controller_id == DeviceId(): + return "" + controller = context_client.GetDevice(endpoint_device.controller_id) + if controller is None: + controller_uuid = endpoint_device.controller_id.device_uuid.uuid + raise Exception("Device({:s}) not found".format(str(controller_uuid))) + return controller.device_type + + def get_custom_config_rule( service_config: ServiceConfig, resource_key: str ) -> Optional[ConfigRule]: @@ -40,12 +56,29 @@ def get_custom_config_rule( return cr -def sort_endpoints(endpoinst_list: List, sdps: List, connection_group: Dict) -> List: - src_sdp_id = connection_group["connectivity-construct"][0]["p2p-sender-sdp"] - sdp_id_name_mapping = {sdp["id"]: sdp["node-id"] for sdp in sdps} - if endpoinst_list[0].device_id.device_uuid.uuid == sdp_id_name_mapping[src_sdp_id]: +def sort_endpoints( + endpoinst_list: List[EndPointId], + sdps: List, + connection_group: Dict, + context_client: ContextClient, +) -> List[EndPointId]: + first_ep = endpoinst_list[0] + first_controller_type = get_endpoint_controller_type(first_ep, context_client) + last_ep = endpoinst_list[-1] + last_controller_type = get_endpoint_controller_type(last_ep, context_client) + if first_controller_type == DeviceTypeEnum.NCE.value: return endpoinst_list - return endpoinst_list[::-1] + elif last_controller_type == DeviceTypeEnum.NCE.value: + return endpoinst_list[::-1] + else: + src_sdp_id = connection_group["connectivity-construct"][0]["p2p-sender-sdp"] + sdp_id_name_mapping = {sdp["id"]: sdp["node-id"] for sdp in sdps} + if ( + endpoinst_list[0].device_id.device_uuid.uuid + == sdp_id_name_mapping[src_sdp_id] + ): + return endpoinst_list + return endpoinst_list[::-1] def replace_ont_endpoint_with_emu_dc( @@ -191,7 +224,7 @@ class IETFSliceHandler: break else: raise Exception("connection group not found") - list_endpoints = sort_endpoints(list_endpoints, sdps, cg) + list_endpoints = sort_endpoints(list_endpoints, sdps, cg, context_client) list_endpoints = replace_ont_endpoint_with_emu_dc( list_endpoints, context_client ) @@ -327,16 +360,14 @@ class IETFSliceHandler: slice_services = candidate_ietf_data["network-slice-services"]["slice-service"] slice_service = slice_services[0] slice_connection_groups = slice_service["connection-groups"]["connection-group"] - connection_group_id = updated_connection_group["connection-group"][0]["id"] + connection_group_id = updated_connection_group["id"] cg_idx = list( ( slice_cg["id"] == connection_group_id for slice_cg in slice_connection_groups ) ).index(True) - slice_connection_groups[cg_idx] = updated_connection_group["connection-group"][ - 0 - ] + slice_connection_groups[cg_idx] = updated_connection_group fields = { name: (value, RAISE_IF_DIFFERS) for name, value in candidate_ietf_data.items() @@ -462,12 +493,6 @@ class IETFSliceHandler: else: raise Exception("connection group not found") del slice_request.slice_constraints[:] - # del slice_request.slice_endpoint_ids[:] - # list_endpoints = sort_endpoints(list_endpoints, sdps, cg) - # list_endpoints = replace_ont_endpoint_with_emu_dc( - # list_endpoints, context_client - # ) - # slice_request.slice_endpoint_ids.extend(list_endpoints) slice_request.slice_constraints.extend(list_constraints) fields = {name: (value, RAISE_IF_DIFFERS) for name, value in ietf_data.items()} update_config_rule_custom(