Commit 1a85a101 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

PathComp component:

Common:
- temporarily added backend to pathcomp service for debug purposes

Proto:
- Added KDisjointPath Algorithm to pathcomp proto

Frontend:
- refactored Servicer to cope with multiple algorithms
- added implementation of ShortestPath algorithm
- added implementation of KShortestPath algorithm
- added implementation of KDisjointPath algorithm (partial)
- moved PathComp servicer tools to algorithms subfolder
- added new unitary test scenario (DC's with CellSiteGWs and Transport Network) to validate KDisjointPath algorithm
parent 9af0d9f3
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -93,3 +93,7 @@ spec:
    protocol: TCP
    port: 10020
    targetPort: 10020
  - name: http
    protocol: TCP
    port: 8081
    targetPort: 8081
+5 −0
Original line number Diff line number Diff line
@@ -28,11 +28,16 @@ message Algorithm_KShortestPath {
  uint32 k_return     = 2;
}

message Algorithm_KDisjointPath {
  uint32 num_disjoint = 1;
}

message PathCompRequest {
  repeated context.Service services = 1;
  oneof algorithm {
    Algorithm_ShortestPath  shortest_path   = 10;
    Algorithm_KShortestPath k_shortest_path = 11;
    Algorithm_KDisjointPath k_disjoint_path = 12;
  }
}

+22 −122
Original line number Diff line number Diff line
@@ -12,17 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import grpc, json, logging, requests, uuid
from typing import Dict, Tuple
from common.proto.context_pb2 import Empty, EndPointId, Service
import grpc, logging
from common.proto.context_pb2 import Empty
from common.proto.pathcomp_pb2 import PathCompReply, PathCompRequest
from common.proto.pathcomp_pb2_grpc import PathCompServiceServicer
from common.rpc_method_wrapper.Decorator import create_metrics, safe_and_metered_rpc_method
from common.tools.grpc.Tools import grpc_message_to_json_string
from context.client.ContextClient import ContextClient
from pathcomp.frontend.Config import BACKEND_URL
from pathcomp.frontend.service.tools.ComposeRequest import compose_device, compose_link, compose_service
#from pathcomp.frontend.service.tools.Constants import CapacityUnit
from pathcomp.frontend.service.algorithms.Factory import get_algorithm

LOGGER = logging.getLogger(__name__)

@@ -39,123 +36,26 @@ class PathCompServiceServicerImpl(PathCompServiceServicer):
    def Compute(self, request : PathCompRequest, context : grpc.ServicerContext) -> PathCompReply:
        LOGGER.info('[Compute] begin ; request = {:s}'.format(grpc_message_to_json_string(request)))

        algorithm = request.WhichOneof('algorithm')
        if algorithm == 'shortest_path':
            # no attributes
            pass
        elif algorithm == 'k_shortest_path':
            k_inspection = request.k_shortest_path.k_inspection
            k_return = request.k_shortest_path.k_return
        else:
            raise NotImplementedError('Unsupported Algorithm: {:s}'.format(str(algorithm)))

        context_client = ContextClient()

        algorithm = {'id': 'KSP', 'sync': False, 'k_paths': k_return}
        service_list = [
            compose_service(grpc_service, algorithm)
            for grpc_service in request.services
        ]
        get_service_key = lambda service_id: (service_id['contextId'], service_id['service_uuid'])
        service_dict : Dict[Tuple[str, str], Tuple[Dict, Service]] = {
            get_service_key(json_service['serviceId']): (json_service, grpc_service)
            for json_service,grpc_service in zip(service_list, request.services)
        }
        #LOGGER.info('service_dict = {:s}'.format(str(service_dict)))

        # TODO: consider filtering resources

        #grpc_contexts = context_client.ListContexts(Empty())
        #for grpc_context in grpc_contexts.contexts:
        #    # TODO: add context to request
        #    grpc_topologies = context_client.ListTopologies(grpc_context.context_id)
        #    for grpc_topology in grpc_topologies.topologies:    #pylint: disable=unused-variable
        #        # TODO: add topology to request
        #        pass

        grpc_devices = context_client.ListDevices(Empty())
        device_list = [
            compose_device(grpc_device)
            for grpc_device in grpc_devices.devices
        ]
        endpoint_dict : Dict[str, Dict[str, Tuple[Dict, EndPointId]]] = {
            json_device['device_Id']: {
                json_endpoint['endpoint_id']['endpoint_uuid']: (json_endpoint['endpoint_id'], grpc_endpoint.endpoint_id)
                for json_endpoint,grpc_endpoint in zip(json_device['device_endpoints'], grpc_device.device_endpoints)
            }
            for json_device,grpc_device in zip(device_list, grpc_devices.devices)
        }
        #LOGGER.info('endpoint_dict = {:s}'.format(str(endpoint_dict)))

        grpc_links = context_client.ListLinks(Empty())
        link_list = [
            compose_link(grpc_link)
            for grpc_link in grpc_links.links
        ]

        request = {
            'serviceList': service_list,
            'deviceList' : device_list,
            'linkList'   : link_list,
        }

        #with open('pc-req.json', 'w', encoding='UTF-8') as f:
        #    f.write(json.dumps(request, sort_keys=True, indent=4))

        reply = requests.post(BACKEND_URL, json=request)
        if reply.status_code not in {requests.codes.ok}:
            raise Exception('Backend error({:s}) for request({:s})'.format(
                str(reply.content.decode('UTF-8')), json.dumps(request, sort_keys=True)))
        LOGGER.info('status_code={:s} reply={:s}'.format(
            str(reply.status_code), str(reply.content.decode('UTF-8'))))

        json_reply = reply.json()
        response_list = json_reply.get('response-list', [])
        reply = PathCompReply()
        for response in response_list:
            service_key = get_service_key(response['serviceId'])
            tuple_service = service_dict.get(service_key)
            if tuple_service is None: raise Exception('ServiceKey({:s}) not found'.format(str(service_key)))
            json_service, grpc_service = tuple_service

            # TODO: implement support for multi-point services
            service_endpoint_ids = grpc_service.service_endpoint_ids
            if len(service_endpoint_ids) != 2: raise NotImplementedError('Service must have 2 endpoints')

            service = reply.services.add()
            service.CopyFrom(grpc_service)

            connection = reply.connections.add()
            connection.connection_id.connection_uuid.uuid = str(uuid.uuid4())
            connection.service_id.CopyFrom(service.service_id)

            no_path_issue = response.get('noPath', {}).get('issue')
            if no_path_issue is not None:
                # no path found: leave connection with no endpoints
                # no_path_issue == 1 => no path due to a constraint
                continue

            service_paths = response['path']

            for service_path in service_paths:
                # ... "path-capacity": {"total-size": {"value": 200, "unit": 0}},
                # ... "path-latency": {"fixed-latency-characteristic": "10.000000"},
                # ... "path-cost": {"cost-name": "", "cost-value": "5.000000", "cost-algorithm": "0.000000"},
                #path_capacity = service_path['path-capacity']['total-size']
                #path_capacity_value = path_capacity['value']
                #path_capacity_unit = CapacityUnit(path_capacity['unit'])
                #path_latency = service_path['path-latency']['fixed-latency-characteristic']
                #path_cost = service_path['path-cost']
                #path_cost_name = path_cost['cost-name']
                #path_cost_value = path_cost['cost-value']
                #path_cost_algorithm = path_cost['cost-algorithm']

                path_endpoints = service_path['devices']
                for endpoint in path_endpoints:
                    device_uuid = endpoint['device_id']
                    endpoint_uuid = endpoint['endpoint_uuid']
                    endpoint_id = connection.path_hops_endpoint_ids.add()
                    endpoint_id.CopyFrom(endpoint_dict[device_uuid][endpoint_uuid][1])

        # TODO: add filtering of devices and links
        # TODO: add contexts, topologies, and membership of devices/links in topologies
        algorithm = get_algorithm(request)
        algorithm.add_devices(context_client.ListDevices(Empty()))
        algorithm.add_links(context_client.ListLinks(Empty()))
        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))
        algorithm.execute()

        reply = algorithm.get_reply()
        LOGGER.info('[Compute] end ; reply = {:s}'.format(grpc_message_to_json_string(reply)))
        return reply
+33 −0
Original line number Diff line number Diff line
# 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.

from ._Algorithm import _Algorithm
from .KDisjointPathAlgorithm import KDisjointPathAlgorithm
from .KShortestPathAlgorithm import KShortestPathAlgorithm
from .ShortestPathAlgorithm import ShortestPathAlgorithm

ALGORITHMS = {
    'shortest_path'  : ShortestPathAlgorithm,
    'k_shortest_path': KShortestPathAlgorithm,
    'k_disjoint_path': KDisjointPathAlgorithm,
}

def get_algorithm(request) -> _Algorithm:
    algorithm_name = request.WhichOneof('algorithm')
    algorithm_class = ALGORITHMS.get(algorithm_name)
    if algorithm_class is None:
        raise Exception('Algorithm({:s}) not supported'.format(str(algorithm_name)))
    algorithm_settings = getattr(request, algorithm_name)
    algorithm_instance = algorithm_class(algorithm_settings)
    return algorithm_instance
+83 −0
Original line number Diff line number Diff line
# 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.

import copy
from typing import Optional
from common.proto.pathcomp_pb2 import Algorithm_KDisjointPath, Algorithm_KShortestPath, PathCompReply
from ._Algorithm import _Algorithm
from .KShortestPathAlgorithm import KShortestPathAlgorithm

class KDisjointPathAlgorithm(_Algorithm):
    def __init__(self, algorithm : Algorithm_KDisjointPath, class_name=__name__) -> None:
        super().__init__('KDP', False, class_name=class_name)
        self.num_disjoint = algorithm.num_disjoint

    def execute(self, dump_request_filename: Optional[str] = None, dump_reply_filename: Optional[str] = None) -> None:
        algorithm = KShortestPathAlgorithm(Algorithm_KShortestPath(k_inspection=0, k_return=1))
        algorithm.sync_paths = True
        algorithm.device_list = self.device_list
        algorithm.device_dict = self.device_dict
        algorithm.endpoint_dict = self.endpoint_dict
        algorithm.link_list = self.link_list
        algorithm.link_dict = self.link_dict
        algorithm.endpoint_to_link_dict = self.endpoint_to_link_dict
        algorithm.service_list = self.service_list
        algorithm.service_dict = self.service_dict

        disjoint_paths = dict()

        for num_path in range(self.num_disjoint):
            algorithm.execute('ksp-{:d}-request.json'.format(num_path), 'ksp-{:d}-reply.txt'.format(num_path))
            response_list = algorithm.json_reply.get('response-list', [])
            for response in response_list:
                service_id = response['serviceId']
                service_key = (service_id['contextId'], service_id['service_uuid'])
                disjoint_paths_service = disjoint_paths.setdefault(service_key, list())

                no_path_issue = response.get('noPath', {}).get('issue')
                if no_path_issue is not None:
                    disjoint_paths_service.append(None)
                    continue

                path_endpoints = response['path'][0]['devices']
                path_links = list()
                path_link_ids = set()
                for endpoint in path_endpoints:
                    device_uuid = endpoint['device_id']
                    endpoint_uuid = endpoint['endpoint_uuid']
                    item = algorithm.endpoint_to_link_dict.get((device_uuid, endpoint_uuid))
                    if item is None:
                        MSG = 'Link for Endpoint({:s}, {:s}) not found'
                        self.logger.warning(MSG.format(device_uuid, endpoint_uuid))
                        continue
                    json_link,_ = item
                    json_link_id = json_link['link_Id']
                    if len(path_links) == 0 or path_links[-1]['link_Id'] != json_link_id:
                        path_links.append(json_link)
                        path_link_ids.add(json_link_id)
                self.logger.info('path_links = {:s}'.format(str(path_links)))
                disjoint_paths_service.append(path_links)

                new_link_list = list(filter(lambda l: l['link_Id'] not in path_link_ids, algorithm.link_list))
                self.logger.info('algorithm.link_list = {:s}'.format(str(algorithm.link_list)))
                self.logger.info('new_link_list = {:s}'.format(str(new_link_list)))
                algorithm.link_list = new_link_list


            # TODO: find used links and remove them from algorithm.link_list
            # TODO: compose disjoint path found


        self.logger.info('disjoint_paths = {:s}'.format(str(disjoint_paths)))
        self.json_reply = {}
Loading