From ff80019215a73b6c2e86ba04d41dbaa8a7e322f0 Mon Sep 17 00:00:00 2001
From: gifrerenom <lluis.gifre@cttc.es>
Date: Tue, 8 Nov 2022 07:50:17 +0000
Subject: [PATCH] PathComp FrontEnd:

- extended to support inter-domain path computation
---
 .../service/PathCompServiceServicerImpl.py    | 25 +++++++++++++++----
 .../frontend/service/algorithms/_Algorithm.py | 12 +++++----
 .../algorithms/tools/ConstantsMappings.py     |  1 +
 3 files changed, 28 insertions(+), 10 deletions(-)

diff --git a/src/pathcomp/frontend/service/PathCompServiceServicerImpl.py b/src/pathcomp/frontend/service/PathCompServiceServicerImpl.py
index 1d55646ab..3c3703649 100644
--- a/src/pathcomp/frontend/service/PathCompServiceServicerImpl.py
+++ b/src/pathcomp/frontend/service/PathCompServiceServicerImpl.py
@@ -13,13 +13,19 @@
 # limitations under the License.
 
 import grpc, logging
-from common.proto.context_pb2 import Empty
+from common.Constants import DEFAULT_CONTEXT_UUID, INTERDOMAIN_TOPOLOGY_UUID
+from common.proto.context_pb2 import ContextId, 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.context_queries.Device import get_devices_in_topology
+from common.tools.context_queries.Link import get_links_in_topology
+from common.tools.context_queries.InterDomain import is_multi_domain
 from common.tools.grpc.Tools import grpc_message_to_json_string
+from common.tools.object_factory.Context import json_context_id
 from context.client.ContextClient import ContextClient
 from pathcomp.frontend.service.algorithms.Factory import get_algorithm
+from pathcomp.frontend.service.algorithms.ShortestPathAlgorithm import ShortestPathAlgorithm
 
 LOGGER = logging.getLogger(__name__)
 
@@ -27,6 +33,8 @@ SERVICE_NAME = 'PathComp'
 METHOD_NAMES = ['Compute']
 METRICS = create_metrics(SERVICE_NAME, METHOD_NAMES)
 
+ADMIN_CONTEXT_ID = ContextId(**json_context_id(DEFAULT_CONTEXT_UUID))
+
 class PathCompServiceServicerImpl(PathCompServiceServicer):
     def __init__(self) -> None:
         LOGGER.debug('Creating Servicer...')
@@ -38,11 +46,18 @@ class PathCompServiceServicerImpl(PathCompServiceServicer):
 
         context_client = ContextClient()
 
-        # TODO: add filtering of devices and links
-        # TODO: add contexts, topologies, and membership of devices/links in topologies
+        if (len(request.services) == 1) and is_multi_domain(context_client, request.services[0].service_endpoint_ids):
+            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())
+
         algorithm = get_algorithm(request)
-        algorithm.add_devices(context_client.ListDevices(Empty()))
-        algorithm.add_links(context_client.ListLinks(Empty()))
+        algorithm.add_devices(devices)
+        algorithm.add_links(links)
         algorithm.add_service_requests(request)
 
         #LOGGER.debug('device_list = {:s}'  .format(str(algorithm.device_list  )))
diff --git a/src/pathcomp/frontend/service/algorithms/_Algorithm.py b/src/pathcomp/frontend/service/algorithms/_Algorithm.py
index b798813a8..dfd657467 100644
--- a/src/pathcomp/frontend/service/algorithms/_Algorithm.py
+++ b/src/pathcomp/frontend/service/algorithms/_Algorithm.py
@@ -13,7 +13,7 @@
 # limitations under the License.
 
 import json, logging, requests
-from typing import Dict, List, Optional, Tuple
+from typing import Dict, List, Optional, Tuple, Union
 from common.proto.context_pb2 import (
     ConfigRule, Connection, Device, DeviceList, EndPointId, Link, LinkList, Service, ServiceStatusEnum,
     ServiceTypeEnum)
@@ -46,8 +46,9 @@ class _Algorithm:
         self.service_list : List[Dict] = list()
         self.service_dict : Dict[Tuple[str, str], Tuple[Dict, Service]] = dict()
 
-    def add_devices(self, grpc_devices : DeviceList) -> None:
-        for grpc_device in grpc_devices.devices:
+    def add_devices(self, grpc_devices : Union[List[Device], DeviceList]) -> None:
+        if isinstance(grpc_devices, DeviceList): grpc_devices = grpc_devices.devices
+        for grpc_device in grpc_devices:
             json_device = compose_device(grpc_device)
             self.device_list.append(json_device)
 
@@ -62,8 +63,9 @@ class _Algorithm:
 
             self.endpoint_dict[device_uuid] = device_endpoint_dict
 
-    def add_links(self, grpc_links : LinkList) -> None:
-        for grpc_link in grpc_links.links:
+    def add_links(self, grpc_links : Union[List[Link], LinkList]) -> None:
+        if isinstance(grpc_links, LinkList): grpc_links = grpc_links.links
+        for grpc_link in grpc_links:
             json_link = compose_link(grpc_link)
             self.link_list.append(json_link)
 
diff --git a/src/pathcomp/frontend/service/algorithms/tools/ConstantsMappings.py b/src/pathcomp/frontend/service/algorithms/tools/ConstantsMappings.py
index 8561ab110..5d1ce4b4e 100644
--- a/src/pathcomp/frontend/service/algorithms/tools/ConstantsMappings.py
+++ b/src/pathcomp/frontend/service/algorithms/tools/ConstantsMappings.py
@@ -80,6 +80,7 @@ class DeviceLayerEnum(IntEnum):
 DEVICE_TYPE_TO_LAYER = {
     DeviceTypeEnum.EMULATED_DATACENTER.value      : DeviceLayerEnum.APPLICATION_DEVICE,
     DeviceTypeEnum.DATACENTER.value               : DeviceLayerEnum.APPLICATION_DEVICE,
+    DeviceTypeEnum.NETWORK.value                  : DeviceLayerEnum.APPLICATION_DEVICE,
 
     DeviceTypeEnum.EMULATED_PACKET_ROUTER.value   : DeviceLayerEnum.PACKET_DEVICE,
     DeviceTypeEnum.PACKET_ROUTER.value            : DeviceLayerEnum.PACKET_DEVICE,
-- 
GitLab