From a36f8613c298a8391dbddac9babf5f28889329c1 Mon Sep 17 00:00:00 2001
From: Carlos Natalino <carlos.natalino@chalmers.se>
Date: Wed, 10 May 2023 15:34:56 +0200
Subject: [PATCH] Updating ports in the constants and using it in the clients.

---
 src/common/Constants.py                       | 27 ++++++++++---------
 .../client/DbscanServingClient.py             | 14 +++++-----
 .../client/OpticalAttackDetectorClient.py     | 21 +++++----------
 .../client/OpticalAttackMitigatorClient.py    | 21 +++++----------
 4 files changed, 36 insertions(+), 47 deletions(-)

diff --git a/src/common/Constants.py b/src/common/Constants.py
index 480e2a231..c5a49746e 100644
--- a/src/common/Constants.py
+++ b/src/common/Constants.py
@@ -61,18 +61,21 @@ class ServiceNameEnum(Enum):
 
 # Default gRPC service ports
 DEFAULT_SERVICE_GRPC_PORTS = {
-    ServiceNameEnum.CONTEXT      .value :  1010,
-    ServiceNameEnum.DEVICE       .value :  2020,
-    ServiceNameEnum.SERVICE      .value :  3030,
-    ServiceNameEnum.SLICE        .value :  4040,
-    ServiceNameEnum.AUTOMATION   .value :  5050,
-    ServiceNameEnum.POLICY       .value :  6060,
-    ServiceNameEnum.MONITORING   .value :  7070,
-    ServiceNameEnum.DLT          .value :  8080,
-    ServiceNameEnum.COMPUTE      .value :  9090,
-    ServiceNameEnum.CYBERSECURITY.value : 10000,
-    ServiceNameEnum.INTERDOMAIN  .value : 10010,
-    ServiceNameEnum.PATHCOMP     .value : 10020,
+    ServiceNameEnum.CONTEXT                .value :  1010,
+    ServiceNameEnum.DEVICE                 .value :  2020,
+    ServiceNameEnum.SERVICE                .value :  3030,
+    ServiceNameEnum.SLICE                  .value :  4040,
+    ServiceNameEnum.AUTOMATION             .value :  5050,
+    ServiceNameEnum.POLICY                 .value :  6060,
+    ServiceNameEnum.MONITORING             .value :  7070,
+    ServiceNameEnum.DLT                    .value :  8080,
+    ServiceNameEnum.COMPUTE                .value :  9090,
+    ServiceNameEnum.DBSCANSERVING          .value : 10008,
+    ServiceNameEnum.OPTICALATTACKDETECTOR  .value : 10006,
+    ServiceNameEnum.OPTICALATTACKMITIGATOR .value : 10007,
+    ServiceNameEnum.OPTICALATTACKMANAGER   .value : 10005,
+    ServiceNameEnum.INTERDOMAIN            .value : 10010,
+    ServiceNameEnum.PATHCOMP               .value : 10020,
 
     # Used for test and debugging only
     ServiceNameEnum.DLT_GATEWAY   .value : 50051,
diff --git a/src/dbscanserving/client/DbscanServingClient.py b/src/dbscanserving/client/DbscanServingClient.py
index c8944f90a..15081ab3a 100644
--- a/src/dbscanserving/client/DbscanServingClient.py
+++ b/src/dbscanserving/client/DbscanServingClient.py
@@ -1,4 +1,4 @@
-# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/)
+# Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -15,10 +15,11 @@
 import grpc, logging
 from typing import Counter
 
+from common.Constants import ServiceNameEnum
+from common.Settings import get_service_host, get_service_port_grpc
+from common.tools.client.RetryDecorator import delay_exponential, retry
 from common.proto.dbscanserving_pb2 import DetectionRequest, DetectionResponse
 from common.proto.dbscanserving_pb2_grpc import DetectorStub
-from common.Settings import get_setting
-from common.tools.client.RetryDecorator import delay_exponential, retry
 
 LOGGER = logging.getLogger(__name__)
 MAX_RETRIES = 15
@@ -32,10 +33,9 @@ RETRY_DECORATOR = retry(
 
 class DbscanServingClient:
     def __init__(self, host=None, port=None):
-        if not host:
-            host = get_setting("DBSCANSERVINGSERVICE_SERVICE_HOST")
-        if not port:
-            port = get_setting("DBSCANSERVINGSERVICE_SERVICE_PORT_GRPC")
+        if not host: host = get_service_host(ServiceNameEnum.DBSCANSERVING)
+        if not port: port = get_service_port_grpc(ServiceNameEnum.DBSCANSERVING)
+
         self.endpoint = "{:s}:{:s}".format(str(host), str(port))
         LOGGER.debug("Creating channel to {:s}...".format(str(self.endpoint)))
         self.channel = None
diff --git a/src/opticalattackdetector/client/OpticalAttackDetectorClient.py b/src/opticalattackdetector/client/OpticalAttackDetectorClient.py
index eb84e5155..50f303b5d 100644
--- a/src/opticalattackdetector/client/OpticalAttackDetectorClient.py
+++ b/src/opticalattackdetector/client/OpticalAttackDetectorClient.py
@@ -12,16 +12,15 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import logging
-
-import grpc
+import grpc, logging
 
+from common.Constants import ServiceNameEnum
+from common.Settings import get_service_host, get_service_port_grpc
+from common.tools.client.RetryDecorator import delay_exponential, retry
+from common.tools.grpc.Tools import grpc_message_to_json
 from common.proto.context_pb2 import Empty
 from common.proto.optical_attack_detector_pb2_grpc import \
     OpticalAttackDetectorServiceStub
-from common.Settings import get_setting
-from common.tools.client.RetryDecorator import delay_exponential, retry
-from common.tools.grpc.Tools import grpc_message_to_json
 
 LOGGER = logging.getLogger(__name__)
 MAX_RETRIES = 15
@@ -35,14 +34,8 @@ RETRY_DECORATOR = retry(
 
 class OpticalAttackDetectorClient:
     def __init__(self, host=None, port=None):
-        if not host:
-            host = get_setting(
-                "OPTICALATTACKDETECTORSERVICE_SERVICE_HOST", default="DBSCANSERVING"
-            )
-        if not port:
-            port = get_setting(
-                "OPTICALATTACKDETECTORSERVICE_SERVICE_PORT_GRPC", default=10007
-            )
+        if not host: host = get_service_host(ServiceNameEnum.OPTICALATTACKDETECTOR)
+        if not port: port = get_service_port_grpc(ServiceNameEnum.OPTICALATTACKDETECTOR)
         self.endpoint = "{:s}:{:s}".format(str(host), str(port))
         LOGGER.debug("Creating channel to {:s}...".format(str(self.endpoint)))
         self.channel = None
diff --git a/src/opticalattackmitigator/client/OpticalAttackMitigatorClient.py b/src/opticalattackmitigator/client/OpticalAttackMitigatorClient.py
index 214c82eef..6851d3492 100644
--- a/src/opticalattackmitigator/client/OpticalAttackMitigatorClient.py
+++ b/src/opticalattackmitigator/client/OpticalAttackMitigatorClient.py
@@ -12,16 +12,15 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import logging
-
-import grpc
+import grpc, logging
 
+from common.Constants import ServiceNameEnum
+from common.Settings import get_service_host, get_service_port_grpc
+from common.tools.client.RetryDecorator import delay_exponential, retry
+from common.tools.grpc.Tools import grpc_message_to_json
 from common.proto.optical_attack_mitigator_pb2 import (AttackDescription,
                                                        AttackResponse)
 from common.proto.optical_attack_mitigator_pb2_grpc import AttackMitigatorStub
-from common.Settings import get_setting
-from common.tools.client.RetryDecorator import delay_exponential, retry
-from common.tools.grpc.Tools import grpc_message_to_json
 
 LOGGER = logging.getLogger(__name__)
 MAX_RETRIES = 15
@@ -35,14 +34,8 @@ RETRY_DECORATOR = retry(
 
 class OpticalAttackMitigatorClient:
     def __init__(self, host=None, port=None):
-        if not host:
-            host = get_setting(
-                "OPTICALATTACKMITIGATORSERVICE_SERVICE_HOST", default="DBSCANSERVING"
-            )
-        if not port:
-            port = get_setting(
-                "OPTICALATTACKMITIGATORSERVICE_SERVICE_PORT_GRPC", default=10007
-            )
+        if not host: host = get_service_host(ServiceNameEnum.OPTICALATTACKMITIGATOR)
+        if not port: port = get_service_port_grpc(ServiceNameEnum.OPTICALATTACKMITIGATOR)
         self.endpoint = "{:s}:{:s}".format(str(host), str(port))
         LOGGER.debug("Creating channel to {:s}...".format(str(self.endpoint)))
         self.channel = None
-- 
GitLab