Commit 941df77d authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Service component:

- store service in UpdateService to get UUIDs from given endpoint names and store constraints
- code cleanup
parent 9ccc623a
Loading
Loading
Loading
Loading
+19 −12
Original line number Diff line number Diff line
@@ -22,9 +22,9 @@ from common.proto.service_pb2_grpc import ServiceServiceServicer
from common.tools.grpc.Tools import grpc_message_to_json, grpc_message_to_json_string
from context.client.ContextClient import ContextClient
from pathcomp.frontend.client.PathCompClient import PathCompClient
from .tools.ContextGetters import get_service
from .service_handler_api.ServiceHandlerFactory import ServiceHandlerFactory
from .task_scheduler.TaskScheduler import TasksScheduler
from .tools.ContextGetters import get_service

LOGGER = logging.getLogger(__name__)

@@ -40,10 +40,6 @@ class ServiceServiceServicerImpl(ServiceServiceServicer):
    def CreateService(self, request : Service, context : grpc.ServicerContext) -> ServiceId:
        LOGGER.info('[CreateService] begin ; request = {:s}'.format(grpc_message_to_json_string(request)))

        service_id = request.service_id
        service_uuid = service_id.service_uuid.uuid
        service_context_uuid = service_id.context_id.context_uuid.uuid

        if len(request.service_endpoint_ids) > 0:
            unexpected_endpoints = []
            for service_endpoint_id in request.service_endpoint_ids:
@@ -97,8 +93,18 @@ class ServiceServiceServicerImpl(ServiceServiceServicer):
        _service : Optional[Service] = get_service(context_client, request.service_id)
        service = Service()
        service.CopyFrom(request if _service is None else _service)
        service.service_status.service_status = ServiceStatusEnum.SERVICESTATUS_PLANNED
        context_client.SetService(service)
        service.service_status.service_status = ServiceStatusEnum.SERVICESTATUS_PLANNED     # pylint: disable=no-member

        del service.service_endpoint_ids[:]                                     # pylint: disable=no-member
        for service_endpoint_id in request.service_endpoint_ids:
            service.service_endpoint_ids.add().CopyFrom(service_endpoint_id)    # pylint: disable=no-member

        del service.service_constraints[:]                                      # pylint: disable=no-member
        for service_constraint in request.service_constraints:
            service.service_constraints.add().CopyFrom(service_constraint)      # pylint: disable=no-member

        service_id_with_uuids = context_client.SetService(service)
        service_with_uuids = context_client.GetService(service_id_with_uuids)

        num_disjoint_paths = None
        for constraint in request.service_constraints:
@@ -107,14 +113,14 @@ class ServiceServiceServicerImpl(ServiceServiceServicer):
                break

        tasks_scheduler = TasksScheduler(self.service_handler_factory)
        if len(request.service_endpoint_ids) >= (2 if num_disjoint_paths is None else 4):
        if len(service_with_uuids.service_endpoint_ids) >= (2 if num_disjoint_paths is None else 4):
            pathcomp_request = PathCompRequest()
            pathcomp_request.services.append(request)
            pathcomp_request.services.append(service_with_uuids)    # pylint: disable=no-member

            if num_disjoint_paths is None:
                pathcomp_request.shortest_path.Clear()
                pathcomp_request.shortest_path.Clear()              # pylint: disable=no-member
            else:
                pathcomp_request.k_disjoint_path.num_disjoint = num_disjoint_paths
                pathcomp_request.k_disjoint_path.num_disjoint = num_disjoint_paths  # pylint: disable=no-member

            LOGGER.info('pathcomp_request={:s}'.format(grpc_message_to_json_string(pathcomp_request)))
            pathcomp = PathCompClient()
@@ -128,7 +134,7 @@ class ServiceServiceServicerImpl(ServiceServiceServicer):
            tasks_scheduler.compose_from_pathcompreply(pathcomp_reply, is_delete=False)

        tasks_scheduler.execute_all()
        return request.service_id
        return service_with_uuids.service_id

    @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
    def DeleteService(self, request : ServiceId, context : grpc.ServicerContext) -> Empty:
@@ -142,6 +148,7 @@ class ServiceServiceServicerImpl(ServiceServiceServicer):
        if _service is None: raise Exception('Service({:s}) not found'.format(grpc_message_to_json_string(request)))
        service = Service()
        service.CopyFrom(_service)
        # pylint: disable=no-member
        service.service_status.service_status = ServiceStatusEnum.SERVICESTATUS_PENDING_REMOVAL
        context_client.SetService(service)