Skip to content
Snippets Groups Projects
PathCompServiceServicerImpl.py 3.65 KiB
Newer Older
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
import grpc, logging, threading
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from common.Constants import DEFAULT_CONTEXT_UUID, INTERDOMAIN_TOPOLOGY_UUID
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from common.method_wrappers.Decorator import MetricsPool, safe_and_metered_rpc_method
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from common.proto.context_pb2 import ContextId, Empty
from common.proto.pathcomp_pb2 import PathCompReply, PathCompRequest
from common.proto.pathcomp_pb2_grpc import PathCompServiceServicer
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from common.tools.context_queries.Device import get_devices_in_topology
from common.tools.context_queries.Link import get_links_in_topology
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from common.tools.context_queries.InterDomain import is_inter_domain
from common.tools.grpc.Tools import grpc_message_to_json_string
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from common.tools.object_factory.Context import json_context_id
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from context.client.ContextClient import ContextClient
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from pathcomp.frontend.service.algorithms.Factory import get_algorithm
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

LOGGER = logging.getLogger(__name__)

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
METRICS_POOL = MetricsPool('PathComp', 'RPC')
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
ADMIN_CONTEXT_ID = ContextId(**json_context_id(DEFAULT_CONTEXT_UUID))

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
class PathCompServiceServicerImpl(PathCompServiceServicer):
    def __init__(self) -> None:
        LOGGER.debug('Creating Servicer...')
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        self._lock = threading.Lock()
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        LOGGER.debug('Servicer Created')

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def Compute(self, request : PathCompRequest, context : grpc.ServicerContext) -> PathCompReply:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        LOGGER.debug('[Compute] begin ; request = {:s}'.format(grpc_message_to_json_string(request)))
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

        context_client = ContextClient()

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        if (len(request.services) == 1) and is_inter_domain(context_client, request.services[0].service_endpoint_ids):
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
            devices = get_devices_in_topology(context_client, ADMIN_CONTEXT_ID, INTERDOMAIN_TOPOLOGY_UUID)
            links = get_links_in_topology(context_client, ADMIN_CONTEXT_ID, INTERDOMAIN_TOPOLOGY_UUID)
        else:
            # TODO: improve filtering of devices and links
            # TODO: add contexts, topologies, and membership of devices/links in topologies
            devices = context_client.ListDevices(Empty())
            links = context_client.ListLinks(Empty())

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        algorithm = get_algorithm(request)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        algorithm.add_devices(devices)
        algorithm.add_links(links)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        algorithm.add_service_requests(request)

        #LOGGER.debug('device_list = {:s}'  .format(str(algorithm.device_list  )))
        #LOGGER.debug('endpoint_dict = {:s}'.format(str(algorithm.endpoint_dict)))
        #LOGGER.debug('link_list = {:s}'    .format(str(algorithm.link_list    )))
        #LOGGER.debug('service_list = {:s}' .format(str(algorithm.service_list )))
        #LOGGER.debug('service_dict = {:s}' .format(str(algorithm.service_dict )))

        #import time
        #ts = time.time()
        #algorithm.execute('request-{:f}.json'.format(ts), 'reply-{:f}.json'.format(ts))
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        with self._lock:
            # ensure backend receives requests one at a time
            algorithm.execute()
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

        reply = algorithm.get_reply()
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        LOGGER.debug('[Compute] end ; reply = {:s}'.format(grpc_message_to_json_string(reply)))
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        return reply