From c98b136b3f8449cc219ce6fd9d09efa219eda9af Mon Sep 17 00:00:00 2001
From: mansoca <carlos.manso@cttc.es>
Date: Tue, 30 Apr 2024 16:10:51 +0000
Subject: [PATCH] update

---
 manifests/nbiservice.yaml                     | 14 +-----
 proto/context.proto                           |  1 +
 src/e2e_orchestrator/Dockerfile               |  1 +
 .../E2EOrchestratorServiceServicerImpl.py     | 22 +++++---
 .../nbi_plugins/tfs_api/Resources.py          | 28 ++++++++---
 .../rest_server/nbi_plugins/tfs_api/Tools.py  |  2 +-
 .../descriptors/emulated/dc-2-dc-service.json |  9 +++-
 .../descriptors/emulated/descriptor_e2e.json  | 20 --------
 .../descriptors/emulated/descriptor_ip.json   | 22 +++-----
 .../descriptors/emulated/link_mapping.json    | 34 +++++++++++++
 src/tests/ecoc24/tfs-ingress-e2e.yaml         |  7 +++
 src/tests/ecoc24/tfs-ingress-ip.yaml          |  7 +++
 .../service/VNTManagerServiceServicerImpl.py  | 50 ++++++++-----------
 13 files changed, 127 insertions(+), 90 deletions(-)
 delete mode 100644 src/tests/ecoc24/descriptors/emulated/descriptor_e2e.json
 create mode 100644 src/tests/ecoc24/descriptors/emulated/link_mapping.json

diff --git a/manifests/nbiservice.yaml b/manifests/nbiservice.yaml
index df3bde883..e77cb35c7 100644
--- a/manifests/nbiservice.yaml
+++ b/manifests/nbiservice.yaml
@@ -41,7 +41,7 @@ spec:
             - containerPort: 8762
           env:
             - name: LOG_LEVEL
-              value: "INFO"
+              value: "DEBUG"
           readinessProbe:
             exec:
               command: ["/bin/grpc_health_probe", "-addr=:9090"]
@@ -83,15 +83,3 @@ spec:
       protocol: TCP
       port: 8762
       targetPort: 8762
----
-apiVersion: v1
-kind: Service
-metadata:
-  name: remote-teraflow
-spec:
-  type: ExternalName
-  externalName: nbiservice.asdf.svc.cluster.local
-  ports:
-  - name: ws
-    protocol: TCP
-    port: 8762
diff --git a/proto/context.proto b/proto/context.proto
index a4cc4c6a7..ae5d77c67 100644
--- a/proto/context.proto
+++ b/proto/context.proto
@@ -259,6 +259,7 @@ message Link {
   string name = 2;
   repeated EndPointId link_endpoint_ids = 3;
   LinkAttributes attributes = 4;
+  bool virtual = 5;
 }
 
 message LinkIdList {
diff --git a/src/e2e_orchestrator/Dockerfile b/src/e2e_orchestrator/Dockerfile
index 85b7f1666..382424694 100644
--- a/src/e2e_orchestrator/Dockerfile
+++ b/src/e2e_orchestrator/Dockerfile
@@ -79,6 +79,7 @@ RUN python3 -m pip install -r e2e_orchestrator/requirements.txt
 # Add component files into working directory
 COPY --chown=teraflow:teraflow ./src/context/. context
 COPY --chown=teraflow:teraflow ./src/e2e_orchestrator/. e2e_orchestrator
+COPY --chown=teraflow:teraflow ./src/service/. service
 
 # Start the service
 ENTRYPOINT ["python", "-m", "e2e_orchestrator.service"]
diff --git a/src/e2e_orchestrator/service/E2EOrchestratorServiceServicerImpl.py b/src/e2e_orchestrator/service/E2EOrchestratorServiceServicerImpl.py
index fb285e50f..991ad149a 100644
--- a/src/e2e_orchestrator/service/E2EOrchestratorServiceServicerImpl.py
+++ b/src/e2e_orchestrator/service/E2EOrchestratorServiceServicerImpl.py
@@ -15,9 +15,10 @@
 import copy
 from common.method_wrappers.Decorator import MetricsPool, safe_and_metered_rpc_method
 from common.proto.e2eorchestrator_pb2 import E2EOrchestratorRequest, E2EOrchestratorReply
-from common.proto.context_pb2 import Empty, Connection, EndPointId, Link, LinkId, TopologyDetails, TopologyId, Device, Topology, Context
+from common.proto.context_pb2 import Empty, Connection, EndPointId, Link, LinkId, TopologyDetails, TopologyId, Device, Topology, Context, Service
 from common.proto.e2eorchestrator_pb2_grpc import E2EOrchestratorServiceServicer
 from context.client.ContextClient import ContextClient
+from service.client.ServiceClient import ServiceClient
 from context.service.database.uuids.EndPoint import endpoint_get_uuid
 from common.proto.vnt_manager_pb2 import VNTSubscriptionRequest
 from common.tools.grpc.Tools import grpc_message_to_json_string
@@ -37,7 +38,7 @@ METRICS_POOL = MetricsPool("E2EOrchestrator", "RPC")
 
 
 context_client: ContextClient = ContextClient()
-
+service_client: ServiceClient = ServiceClient()
 
 EXT_HOST = "nbiservice.tfs-ip.svc.cluster.local"
 EXT_PORT = "8762"
@@ -51,12 +52,17 @@ def _event_received(websocket):
         message_json = json.loads(message)
 
         if 'link_id' in message_json:
-            obj = Link(**message_json)
-            if _check_policies(obj):
-                pass
+            link = Link(**message_json)
+
+            service = Service()
+            service.service_id = link.link_id.link_uuid
+            service.serivice_type = 2 # Optical
+            service.service_status = 1
+            
+            # service_client.CreateService(service)
+
+            websocket.send(message)
 
-        elif 'link_uuid' in message_json:
-            obj = LinkId(**message_json)
 
         else:
             topology_details = TopologyDetails(**message_json)
@@ -72,9 +78,11 @@ def _event_received(websocket):
             context_client.SetTopology(topology)
 
             for device in topology_details.devices:
+                LOGGER.info('Setting Device: {}'.format(device))
                 context_client.SetDevice(device)
 
             for link in topology_details.links:
+                LOGGER.info('Setting Link: {}'.format(link))
                 context_client.SetLink(link)
 
 
diff --git a/src/nbi/service/rest_server/nbi_plugins/tfs_api/Resources.py b/src/nbi/service/rest_server/nbi_plugins/tfs_api/Resources.py
index abf240fb7..126885e70 100644
--- a/src/nbi/service/rest_server/nbi_plugins/tfs_api/Resources.py
+++ b/src/nbi/service/rest_server/nbi_plugins/tfs_api/Resources.py
@@ -20,11 +20,17 @@ from common.tools.grpc.Tools import grpc_message_to_json
 from context.client.ContextClient import ContextClient
 from service.client.ServiceClient import ServiceClient
 from vnt_manager.client.VNTManagerClient import VNTManagerClient
+import logging
+
+
 
 from .Tools import (
     format_grpc_to_json, grpc_connection_id, grpc_context_id, grpc_device_id, grpc_link, grpc_link_id, grpc_policy_rule_id,
     grpc_service_id, grpc_service, grpc_slice_id, grpc_topology_id)
 
+LOGGER = logging.getLogger(__name__)
+
+
 class _Resource(Resource):
     def __init__(self) -> None:
         super().__init__()
@@ -198,16 +204,26 @@ class VirtualLinks(_Resource):
         return format_grpc_to_json(self.vntmanager_client.ListVirtualLinks(Empty()))
 
 class VirtualLink(_Resource):
-    def get(self, link_uuid : str):
-        return format_grpc_to_json(self.vntmanager_client.GetVirtualLink(grpc_link_id(link_uuid)))
-    def post(self):
+    def get(self, virtual_link_uuid : str):
+        return format_grpc_to_json(self.vntmanager_client.GetVirtualLink(grpc_link_id(virtual_link_uuid)))
+    def post(self, virtual_link_uuid : str):  # pylint: disable=unused-argument
         link = request.get_json()
+        LOGGER.info('---------------------------LINK received------------------------------')
+        LOGGER.info(link)
+        LOGGER.info('----------------------------------------------------------------------')
+        LOGGER.info(link['link_id'])
+        LOGGER.info('type: {}'.format(type(link['link_id']))) 
+        LOGGER.info('---------------------------LINK received------------------------------')
+        link = grpc_link(link)
+
+
+
         return format_grpc_to_json(self.vntmanager_client.SetVirtualLink(grpc_link(link)))
-    def put(self):
+    def put(self, virtual_link_uuid : str):  # pylint: disable=unused-argument
         link = request.get_json()
         return format_grpc_to_json(self.vntmanager_client.SetVirtualLink(grpc_link(link)))
-    def delete(self, link_uuid : str):
-        return format_grpc_to_json(self.vntmanager_client.RemoveVirtualLink(grpc_link_id(link_uuid)))
+    def delete(self, virtual_link_uuid : str):
+        return format_grpc_to_json(self.vntmanager_client.RemoveVirtualLink(grpc_link_id(virtual_link_uuid)))
 
 class ConnectionIds(_Resource):
     def get(self, context_uuid : str, service_uuid : str):
diff --git a/src/nbi/service/rest_server/nbi_plugins/tfs_api/Tools.py b/src/nbi/service/rest_server/nbi_plugins/tfs_api/Tools.py
index d101a6569..773c31636 100644
--- a/src/nbi/service/rest_server/nbi_plugins/tfs_api/Tools.py
+++ b/src/nbi/service/rest_server/nbi_plugins/tfs_api/Tools.py
@@ -47,7 +47,7 @@ def grpc_link_id(link_uuid):
     return LinkId(**json_link_id(link_uuid))
 
 def grpc_link(link):
-    return Link(**json_link(link.link_id.uuid, link.link_endpoint_ids))
+    return Link(**json_link(link['link_id']['link_uuid']['uuid'], link['link_endpoint_ids']))
 
 def grpc_service_id(context_uuid, service_uuid):
     return ServiceId(**json_service_id(service_uuid, context_id=json_context_id(context_uuid)))
diff --git a/src/tests/ecoc24/descriptors/emulated/dc-2-dc-service.json b/src/tests/ecoc24/descriptors/emulated/dc-2-dc-service.json
index 7c3be015d..44c80ad46 100644
--- a/src/tests/ecoc24/descriptors/emulated/dc-2-dc-service.json
+++ b/src/tests/ecoc24/descriptors/emulated/dc-2-dc-service.json
@@ -2,7 +2,14 @@
     "services": [
         {
             "service_id": {
-                "context_id": {"context_uuid": {"uuid": "admin"}}, "service_uuid": {"uuid": "dc-2-dc-svc"}
+                "context_id": {
+                    "context_uuid": {
+                        "uuid": "admin"
+                        }
+                    },
+                "service_uuid": {
+                    "uuid": "dc-2-dc-svc"
+                }
             },
             "service_type": 2,
             "service_status": {"service_status": 1},
diff --git a/src/tests/ecoc24/descriptors/emulated/descriptor_e2e.json b/src/tests/ecoc24/descriptors/emulated/descriptor_e2e.json
deleted file mode 100644
index 93acb6faa..000000000
--- a/src/tests/ecoc24/descriptors/emulated/descriptor_e2e.json
+++ /dev/null
@@ -1,20 +0,0 @@
-{
-    "contexts": [
-        {"context_id": {"context_uuid": {"uuid": "admin"}}}
-    ],
-    "topologies": [
-        {"topology_id": {"context_id": {"context_uuid": {"uuid": "admin"}}, "topology_uuid": {"uuid": "admin"}}}
-    ],
-    "devices": [
-        {
-            "device_id": {"device_uuid": {"uuid": "TFS-IP"}}, "device_type": "teraflowsdn", "device_drivers": [7],
-            "device_endpoints": [], "device_operational_status": 0, "device_config": {"config_rules": [
-                {"action": 1, "custom": {"resource_key": "_connect/address", "resource_value": "127.0.0.1"}},
-                {"action": 1, "custom": {"resource_key": "_connect/port", "resource_value": "8002"}},
-                {"action": 1, "custom": {"resource_key": "_connect/settings", "resource_value": {
-                    "scheme": "http", "username": "admin", "password": "admin"
-                }}}
-            ]}
-        }
-    ]
-}
diff --git a/src/tests/ecoc24/descriptors/emulated/descriptor_ip.json b/src/tests/ecoc24/descriptors/emulated/descriptor_ip.json
index d0fd09350..516a8bdeb 100644
--- a/src/tests/ecoc24/descriptors/emulated/descriptor_ip.json
+++ b/src/tests/ecoc24/descriptors/emulated/descriptor_ip.json
@@ -7,32 +7,26 @@
     ],
     "devices": [
         {
-            "device_id": {"device_uuid": {"uuid": "PE1"}}, "device_type": "emu-packet-router", "device_drivers": [0],
+            "device_id": {"device_uuid": {"uuid": "IP1"}}, "device_type": "emu-packet-router", "device_drivers": [0],
             "device_endpoints": [], "device_operational_status": 0, "device_config": {"config_rules": [
                 {"action": 1, "custom": {"resource_key": "_connect/address", "resource_value": "127.0.0.1"}},
                 {"action": 1, "custom": {"resource_key": "_connect/port", "resource_value": "0"}},
                 {"action": 1, "custom": {"resource_key": "_connect/settings", "resource_value": {"endpoints": [
-                    {"sample_types": [], "type": "copper/internal", "uuid": "1/1"},
-                    {"sample_types": [], "type": "copper/internal", "uuid": "1/2"},
-                    {"sample_types": [], "type": "copper/internal", "uuid": "2/1"},
-                    {"sample_types": [], "type": "copper/internal", "uuid": "2/2"},
-                    {"sample_types": [], "type": "copper/internal", "uuid": "2/3"},
-                    {"sample_types": [], "type": "copper/internal", "uuid": "2/4"}
+                    {"sample_types": [], "type": "copper/internal", "uuid": "CTP1"},
+                    {"sample_types": [], "type": "copper/internal", "uuid": "CTP2"},
+                    {"sample_types": [], "type": "copper/internal", "uuid": "CTP3"}
                 ]}}}
             ]}
         },
         {
-            "device_id": {"device_uuid": {"uuid": "PE2"}}, "device_type": "emu-packet-router", "device_drivers": [0],
+            "device_id": {"device_uuid": {"uuid": "IP2"}}, "device_type": "emu-packet-router", "device_drivers": [0],
             "device_endpoints": [], "device_operational_status": 0, "device_config": {"config_rules": [
                 {"action": 1, "custom": {"resource_key": "_connect/address", "resource_value": "127.0.0.1"}},
                 {"action": 1, "custom": {"resource_key": "_connect/port", "resource_value": "0"}},
                 {"action": 1, "custom": {"resource_key": "_connect/settings", "resource_value": {"endpoints": [
-                    {"sample_types": [], "type": "copper/internal", "uuid": "1/1"},
-                    {"sample_types": [], "type": "copper/internal", "uuid": "1/2"},
-                    {"sample_types": [], "type": "copper/internal", "uuid": "2/1"},
-                    {"sample_types": [], "type": "copper/internal", "uuid": "2/2"},
-                    {"sample_types": [], "type": "copper/internal", "uuid": "2/3"},
-                    {"sample_types": [], "type": "copper/internal", "uuid": "2/4"}
+                    {"sample_types": [], "type": "copper/internal", "uuid": "CTP1"},
+                    {"sample_types": [], "type": "copper/internal", "uuid": "CTP2"},
+                    {"sample_types": [], "type": "copper/internal", "uuid": "CTP3"}
                 ]}}}
             ]}
         }
diff --git a/src/tests/ecoc24/descriptors/emulated/link_mapping.json b/src/tests/ecoc24/descriptors/emulated/link_mapping.json
new file mode 100644
index 000000000..9ac5a2599
--- /dev/null
+++ b/src/tests/ecoc24/descriptors/emulated/link_mapping.json
@@ -0,0 +1,34 @@
+{
+    "contexts": [
+        {"context_id": {"context_uuid": {"uuid": "admin"}}}
+    ],
+    "topologies": [
+        {"topology_id": {"context_id": {"context_uuid": {"uuid": "admin"}}, "topology_uuid": {"uuid": "admin"}}}
+    ],
+    "links": [
+        {"link_id": {"link_uuid": {"uuid": "IP1_CTP1-MGON1_OTP1"}}, "link_endpoint_ids": [
+            {"device_id": {"device_uuid": {"uuid": "IP1"}}, "endpoint_uuid": {"uuid": "CTP1"}},
+            {"device_id": {"device_uuid": {"uuid": "MG-ON1"}}, "endpoint_uuid": {"uuid": "OTP1"}}
+        ]},
+            {"link_id": {"link_uuid": {"uuid": "IP1_CTP2-MGON1_OTP2"}}, "link_endpoint_ids": [
+            {"device_id": {"device_uuid": {"uuid": "IP1"}}, "endpoint_uuid": {"uuid": "CTP2"}},
+            {"device_id": {"device_uuid": {"uuid": "MG-ON1"}}, "endpoint_uuid": {"uuid": "OTP2"}}
+        ]},
+        {"link_id": {"link_uuid": {"uuid": "IP1CTP3-MGON1_OTP3"}}, "link_endpoint_ids": [
+            {"device_id": {"device_uuid": {"uuid": "IP1"}}, "endpoint_uuid": {"uuid": "CTP3"}},
+            {"device_id": {"device_uuid": {"uuid": "MG-ON1"}}, "endpoint_uuid": {"uuid": "OTP3"}}
+        ]},            
+        {"link_id": {"link_uuid": {"uuid": "IP2_CTP1-MGON3_OTP1"}}, "link_endpoint_ids": [
+            {"device_id": {"device_uuid": {"uuid": "IP2"}}, "endpoint_uuid": {"uuid": "CTP1"}},
+            {"device_id": {"device_uuid": {"uuid": "MG-ON3"}}, "endpoint_uuid": {"uuid": "OTP1"}}
+        ]},
+            {"link_id": {"link_uuid": {"uuid": "IP2_CTP2-MGON3_OTP2"}}, "link_endpoint_ids": [
+            {"device_id": {"device_uuid": {"uuid": "IP2"}}, "endpoint_uuid": {"uuid": "CTP2"}},
+            {"device_id": {"device_uuid": {"uuid": "MG-ON3"}}, "endpoint_uuid": {"uuid": "OTP2"}}
+        ]},
+        {"link_id": {"link_uuid": {"uuid": "IP2_CTP3-MGON3_OTP3"}}, "link_endpoint_ids": [
+            {"device_id": {"device_uuid": {"uuid": "IP2"}}, "endpoint_uuid": {"uuid": "CTP3"}},
+            {"device_id": {"device_uuid": {"uuid": "MG-ON3"}}, "endpoint_uuid": {"uuid": "OTP3"}}
+        ]}
+    ]
+}
diff --git a/src/tests/ecoc24/tfs-ingress-e2e.yaml b/src/tests/ecoc24/tfs-ingress-e2e.yaml
index 0ba8a08d5..352d5effc 100644
--- a/src/tests/ecoc24/tfs-ingress-e2e.yaml
+++ b/src/tests/ecoc24/tfs-ingress-e2e.yaml
@@ -51,3 +51,10 @@ spec:
               name: nbiservice
               port:
                 number: 8080
+        - path: /()(tfs-api/.*)
+          pathType: Prefix
+          backend:
+            service:
+              name: nbiservice
+              port:
+                number: 8080
diff --git a/src/tests/ecoc24/tfs-ingress-ip.yaml b/src/tests/ecoc24/tfs-ingress-ip.yaml
index 56cec1360..2e8d30778 100644
--- a/src/tests/ecoc24/tfs-ingress-ip.yaml
+++ b/src/tests/ecoc24/tfs-ingress-ip.yaml
@@ -51,3 +51,10 @@ spec:
               name: nbiservice
               port:
                 number: 8080
+        - path: /()(tfs-api/.*)
+          pathType: Prefix
+          backend:
+            service:
+              name: nbiservice
+              port:
+                number: 8080
\ No newline at end of file
diff --git a/src/vnt_manager/service/VNTManagerServiceServicerImpl.py b/src/vnt_manager/service/VNTManagerServiceServicerImpl.py
index a4e84be6c..ce77bc9ee 100644
--- a/src/vnt_manager/service/VNTManagerServiceServicerImpl.py
+++ b/src/vnt_manager/service/VNTManagerServiceServicerImpl.py
@@ -42,6 +42,8 @@ from common.proto.dlt_gateway_pb2 import DltRecordEvent, DltRecordOperationEnum,
 from common.tools.grpc.Tools import grpc_message_to_json_string
 from common.tools.grpc.Tools import grpc_message_to_json
 import time
+import json
+
 
 LOGGER = logging.getLogger(__name__)
 
@@ -68,7 +70,6 @@ def send_msg(msg):
     LOGGER.info(msg)
     LOGGER.info('-------------------------------------------------------')
     WEBSOCKET.send(msg)
-    message = WEBSOCKET.recv()
 
 
 
@@ -123,17 +124,19 @@ class VNTMEventDispatcher(threading.Thread):
             else:
                 send_msg(grpc_message_to_json_string(topology_details))
 
-            LOGGER.info('aaaaaaaaaaaaaaaaa')
             while not self._terminate.is_set():
+
                 event = events_collector.get_event(block=True, timeout=GET_EVENT_TIMEOUT)
                 if event is None: continue
-                LOGGER.info('event!: {}'.format(event))
+                LOGGER.info('event!!!!!!!!!!!!!!!!!!!!!!: {}'.format(event))
                 topology_details = context_client.GetTopologyDetails(TopologyId(**topology_id))
+                LOGGER.info('topodetails..................................... ')
 
                 to_send = grpc_message_to_json_string(topology_details)
 
                 send_msg(to_send)
         
+            LOGGER.info('Exiting')
             events_collector.stop()
 
 
@@ -158,46 +161,37 @@ class VNTManagerServiceServicerImpl(VNTManagerServiceServicer):
 
         return reply
 
-    @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
-    def ListVirtualLinkIds(self, request : Empty, context : grpc.ServicerContext) -> LinkIdList:
-        return LinkIdList(link_ids=[link.link_id for link in self.links])
+    """    @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
+        def ListVirtualLinkIds(self, request : Empty, context : grpc.ServicerContext) -> LinkIdList:
+            return [link for link in context_client.ListLinks(Empty()) if link.virtual]
 
+            return LinkIdList(link_ids=[link.link_id for link in self.links])
+    """
     @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
     def ListVirtualLinks(self, request : Empty, context : grpc.ServicerContext) -> LinkList:
-        return LinkList(link=self.links)
+        return [link for link in context_client.ListLinks(Empty()).links if link.virtual]
 
     @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
     def GetVirtualLink(self, request : LinkId, context : grpc.ServicerContext) -> Link:
-        try:
-            send_msg(request)
-        except Exception as e:
-            LOGGER.error('Exception getting virtual link={}\n\t{}'.format(request.link_uuid.uuid, e))
-        else:
-            for link in  self.links:
-                if link.link_uuid.uuid == request.uuid:
-                    return link
-        return Empty()
+        link = context_client.GetLink(request)
+        return link if link.virtual else Empty()
+
 
     @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
     def SetVirtualLink(self, request : Link, context : grpc.ServicerContext) -> LinkId:
         try:
             send_msg(request)
+            message = WEBSOCKET.recv()
+            message_json = json.loads(message)
+            link = Link(**message_json)
+            context_client.SetLink(link)
+
         except Exception as e:
             LOGGER.error('Exception setting virtual link={}\n\t{}'.format(request.link_id.link_uuid.uuid, e))
-        else:
-            self.links.append(request)
-            return request.linkd_id
+        return request.linkd_id
 
     @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
     def RemoveVirtualLink(self, request : LinkId, context : grpc.ServicerContext) -> Empty:
-        try:
-            send_msg(request)
-        except Exception as e:
-            LOGGER.error('Exception removing virtual link={}\n\t{}'.format(request.link_id.link_uuid.uuid, e))
-        else:
-            for link in  self.links:
-                if link.link_uuid.uuid == request.uuid:
-                    self.links.remove(link)
-                    return Empty()
+        # TODO
         return Empty()
             
-- 
GitLab