From 146e07b031f25cad9c9e2947aba61a67b6274f24 Mon Sep 17 00:00:00 2001
From: gifrerenom <lluis.gifre@cttc.es>
Date: Tue, 10 Dec 2024 17:56:30 +0000
Subject: [PATCH] Core components:

- Enabled gRPC Reflection
- Added scripts to inspect core gRPC components
---
 scripts/grpcurl_inspect_context.sh            | 44 +++++++++++++++++++
 scripts/grpcurl_inspect_device.sh             | 44 +++++++++++++++++++
 scripts/grpcurl_inspect_pathcomp_frontend.sh  | 44 +++++++++++++++++++
 scripts/grpcurl_inspect_service.sh            | 44 +++++++++++++++++++
 scripts/grpcurl_inspect_slice.sh              | 44 +++++++++++++++++++
 src/context/service/ContextService.py         |  5 +++
 src/device/service/DeviceService.py           |  6 ++-
 .../service/E2EOrchestratorService.py         | 12 +++--
 src/forecaster/service/ForecasterService.py   |  3 ++
 .../frontend/service/PathCompService.py       |  5 ++-
 src/service/service/ServiceService.py         |  3 ++
 src/slice/service/SliceService.py             |  5 ++-
 src/vnt_manager/service/VNTManagerService.py  | 12 +++--
 13 files changed, 254 insertions(+), 17 deletions(-)
 create mode 100755 scripts/grpcurl_inspect_context.sh
 create mode 100755 scripts/grpcurl_inspect_device.sh
 create mode 100755 scripts/grpcurl_inspect_pathcomp_frontend.sh
 create mode 100755 scripts/grpcurl_inspect_service.sh
 create mode 100755 scripts/grpcurl_inspect_slice.sh

diff --git a/scripts/grpcurl_inspect_context.sh b/scripts/grpcurl_inspect_context.sh
new file mode 100755
index 000000000..dda920a7a
--- /dev/null
+++ b/scripts/grpcurl_inspect_context.sh
@@ -0,0 +1,44 @@
+#!/bin/bash
+# Copyright 2022-2024 ETSI SDG TeraFlowSDN (TFS) (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.
+# 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.
+
+########################################################################################################################
+# Define your deployment settings here
+########################################################################################################################
+
+# If not already set, set the name of the Kubernetes namespace to deploy to.
+export TFS_K8S_NAMESPACE=${TFS_K8S_NAMESPACE:-"tfs"}
+
+########################################################################################################################
+# Automated steps start here
+########################################################################################################################
+
+# Ref: https://github.com/fullstorydev/grpcurl
+
+source tfs_runtime_env_vars.sh
+
+GRPC_ENDPOINT="$CONTEXTSERVICE_SERVICE_HOST:$CONTEXTSERVICE_SERVICE_PORT_GRPC"
+GRP_CURL_CMD="docker run fullstorydev/grpcurl --plaintext $GRPC_ENDPOINT"
+
+GRPC_SERVICES=`$GRP_CURL_CMD list`
+echo "gRPC Services found in $GRPC_ENDPOINT:"
+printf "\n"
+
+for GRPC_SERVICE in $GRPC_SERVICES; do
+    echo "gRPC Service: $GRPC_SERVICE"
+    $GRP_CURL_CMD describe $GRPC_SERVICE
+    printf "\n"
+done
+
+echo "Done!"
diff --git a/scripts/grpcurl_inspect_device.sh b/scripts/grpcurl_inspect_device.sh
new file mode 100755
index 000000000..0e1202fb6
--- /dev/null
+++ b/scripts/grpcurl_inspect_device.sh
@@ -0,0 +1,44 @@
+#!/bin/bash
+# Copyright 2022-2024 ETSI SDG TeraFlowSDN (TFS) (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.
+# 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.
+
+########################################################################################################################
+# Define your deployment settings here
+########################################################################################################################
+
+# If not already set, set the name of the Kubernetes namespace to deploy to.
+export TFS_K8S_NAMESPACE=${TFS_K8S_NAMESPACE:-"tfs"}
+
+########################################################################################################################
+# Automated steps start here
+########################################################################################################################
+
+# Ref: https://github.com/fullstorydev/grpcurl
+
+source tfs_runtime_env_vars.sh
+
+GRPC_ENDPOINT="$DEVICESERVICE_SERVICE_HOST:$DEVICESERVICE_SERVICE_PORT_GRPC"
+GRP_CURL_CMD="docker run fullstorydev/grpcurl --plaintext $GRPC_ENDPOINT"
+
+GRPC_SERVICES=`$GRP_CURL_CMD list`
+echo "gRPC Services found in $GRPC_ENDPOINT:"
+printf "\n"
+
+for GRPC_SERVICE in $GRPC_SERVICES; do
+    echo "gRPC Service: $GRPC_SERVICE"
+    $GRP_CURL_CMD describe $GRPC_SERVICE
+    printf "\n"
+done
+
+echo "Done!"
diff --git a/scripts/grpcurl_inspect_pathcomp_frontend.sh b/scripts/grpcurl_inspect_pathcomp_frontend.sh
new file mode 100755
index 000000000..686f7ae1e
--- /dev/null
+++ b/scripts/grpcurl_inspect_pathcomp_frontend.sh
@@ -0,0 +1,44 @@
+#!/bin/bash
+# Copyright 2022-2024 ETSI SDG TeraFlowSDN (TFS) (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.
+# 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.
+
+########################################################################################################################
+# Define your deployment settings here
+########################################################################################################################
+
+# If not already set, set the name of the Kubernetes namespace to deploy to.
+export TFS_K8S_NAMESPACE=${TFS_K8S_NAMESPACE:-"tfs"}
+
+########################################################################################################################
+# Automated steps start here
+########################################################################################################################
+
+# Ref: https://github.com/fullstorydev/grpcurl
+
+source tfs_runtime_env_vars.sh
+
+GRPC_ENDPOINT="$PATHCOMPSERVICE_SERVICE_HOST:$PATHCOMPSERVICE_SERVICE_PORT_GRPC"
+GRP_CURL_CMD="docker run fullstorydev/grpcurl --plaintext $GRPC_ENDPOINT"
+
+GRPC_SERVICES=`$GRP_CURL_CMD list`
+echo "gRPC Services found in $GRPC_ENDPOINT:"
+printf "\n"
+
+for GRPC_SERVICE in $GRPC_SERVICES; do
+    echo "gRPC Service: $GRPC_SERVICE"
+    $GRP_CURL_CMD describe $GRPC_SERVICE
+    printf "\n"
+done
+
+echo "Done!"
diff --git a/scripts/grpcurl_inspect_service.sh b/scripts/grpcurl_inspect_service.sh
new file mode 100755
index 000000000..f1b674ee5
--- /dev/null
+++ b/scripts/grpcurl_inspect_service.sh
@@ -0,0 +1,44 @@
+#!/bin/bash
+# Copyright 2022-2024 ETSI SDG TeraFlowSDN (TFS) (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.
+# 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.
+
+########################################################################################################################
+# Define your deployment settings here
+########################################################################################################################
+
+# If not already set, set the name of the Kubernetes namespace to deploy to.
+export TFS_K8S_NAMESPACE=${TFS_K8S_NAMESPACE:-"tfs"}
+
+########################################################################################################################
+# Automated steps start here
+########################################################################################################################
+
+# Ref: https://github.com/fullstorydev/grpcurl
+
+source tfs_runtime_env_vars.sh
+
+GRPC_ENDPOINT="$SERVICESERVICE_SERVICE_HOST:$SERVICESERVICE_SERVICE_PORT_GRPC"
+GRP_CURL_CMD="docker run fullstorydev/grpcurl --plaintext $GRPC_ENDPOINT"
+
+GRPC_SERVICES=`$GRP_CURL_CMD list`
+echo "gRPC Services found in $GRPC_ENDPOINT:"
+printf "\n"
+
+for GRPC_SERVICE in $GRPC_SERVICES; do
+    echo "gRPC Service: $GRPC_SERVICE"
+    $GRP_CURL_CMD describe $GRPC_SERVICE
+    printf "\n"
+done
+
+echo "Done!"
diff --git a/scripts/grpcurl_inspect_slice.sh b/scripts/grpcurl_inspect_slice.sh
new file mode 100755
index 000000000..170be7bf5
--- /dev/null
+++ b/scripts/grpcurl_inspect_slice.sh
@@ -0,0 +1,44 @@
+#!/bin/bash
+# Copyright 2022-2024 ETSI SDG TeraFlowSDN (TFS) (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.
+# 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.
+
+########################################################################################################################
+# Define your deployment settings here
+########################################################################################################################
+
+# If not already set, set the name of the Kubernetes namespace to deploy to.
+export TFS_K8S_NAMESPACE=${TFS_K8S_NAMESPACE:-"tfs"}
+
+########################################################################################################################
+# Automated steps start here
+########################################################################################################################
+
+# Ref: https://github.com/fullstorydev/grpcurl
+
+source tfs_runtime_env_vars.sh
+
+GRPC_ENDPOINT="$SLICESERVICE_SERVICE_HOST:$SLICESERVICE_SERVICE_PORT_GRPC"
+GRP_CURL_CMD="docker run fullstorydev/grpcurl --plaintext $GRPC_ENDPOINT"
+
+GRPC_SERVICES=`$GRP_CURL_CMD list`
+echo "gRPC Services found in $GRPC_ENDPOINT:"
+printf "\n"
+
+for GRPC_SERVICE in $GRPC_SERVICES; do
+    echo "gRPC Service: $GRPC_SERVICE"
+    $GRP_CURL_CMD describe $GRPC_SERVICE
+    printf "\n"
+done
+
+echo "Done!"
diff --git a/src/context/service/ContextService.py b/src/context/service/ContextService.py
index a385f4481..d633dea15 100644
--- a/src/context/service/ContextService.py
+++ b/src/context/service/ContextService.py
@@ -16,7 +16,9 @@ import logging, sqlalchemy
 from common.Constants import ServiceNameEnum
 from common.Settings import get_service_port_grpc
 from common.message_broker.MessageBroker import MessageBroker
+from common.proto.context_pb2 import DESCRIPTOR as CONTEXT_DESCRIPTOR
 from common.proto.context_pb2_grpc import add_ContextServiceServicer_to_server
+from common.proto.context_policy_pb2 import DESCRIPTOR as CONTEXT_POLICY_DESCRIPTOR
 from common.proto.context_policy_pb2_grpc import add_ContextPolicyServiceServicer_to_server
 from common.tools.service.GenericGrpcService import GenericGrpcService
 from .ContextServiceServicerImpl import ContextServiceServicerImpl
@@ -36,3 +38,6 @@ class ContextService(GenericGrpcService):
     def install_servicers(self):
         add_ContextServiceServicer_to_server(self.context_servicer, self.server)
         add_ContextPolicyServiceServicer_to_server(self.context_servicer, self.server)
+
+        self.add_reflection_service_name(CONTEXT_DESCRIPTOR, 'ContextService')
+        self.add_reflection_service_name(CONTEXT_POLICY_DESCRIPTOR, 'ContextPolicyService')
diff --git a/src/device/service/DeviceService.py b/src/device/service/DeviceService.py
index a94259471..a5a48e1bf 100644
--- a/src/device/service/DeviceService.py
+++ b/src/device/service/DeviceService.py
@@ -12,10 +12,11 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import os
 from common.Constants import ServiceNameEnum
 from common.Settings import get_service_port_grpc
+from common.proto.device_pb2 import DESCRIPTOR as DEVICE_DESCRIPTOR
 from common.proto.device_pb2_grpc import add_DeviceServiceServicer_to_server
+from common.proto.optical_device_pb2 import DESCRIPTOR as OPTICAL_DEVICE_DESCRIPTOR
 from common.proto.optical_device_pb2_grpc import add_OpenConfigServiceServicer_to_server
 from common.tools.service.GenericGrpcService import GenericGrpcService
 from device.Config import LOAD_ALL_DEVICE_DRIVERS
@@ -41,8 +42,11 @@ class DeviceService(GenericGrpcService):
     def install_servicers(self):
         self.monitoring_loops.start()
         add_DeviceServiceServicer_to_server(self.device_servicer, self.server)
+        self.add_reflection_service_name(DEVICE_DESCRIPTOR, 'DeviceService')
+
         if LOAD_ALL_DEVICE_DRIVERS:
             add_OpenConfigServiceServicer_to_server(self.openconfig_device_servicer,self.server)
+            self.add_reflection_service_name(OPTICAL_DEVICE_DESCRIPTOR, 'OpenConfigService')
 
     def stop(self):
         super().stop()
diff --git a/src/e2e_orchestrator/service/E2EOrchestratorService.py b/src/e2e_orchestrator/service/E2EOrchestratorService.py
index 9fa7bf4bd..3abef2777 100644
--- a/src/e2e_orchestrator/service/E2EOrchestratorService.py
+++ b/src/e2e_orchestrator/service/E2EOrchestratorService.py
@@ -12,19 +12,15 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import logging
-
 from common.Constants import ServiceNameEnum
-from common.proto.e2eorchestrator_pb2_grpc import add_E2EOrchestratorServiceServicer_to_server
 from common.Settings import get_service_port_grpc
+from common.proto.e2eorchestrator_pb2 import DESCRIPTOR as E2EORCHESTRATOR_DESCRIPTOR
+from common.proto.e2eorchestrator_pb2_grpc import add_E2EOrchestratorServiceServicer_to_server
 from common.tools.service.GenericGrpcService import GenericGrpcService
 from .E2EOrchestratorServiceServicerImpl import E2EOrchestratorServiceServicerImpl
 
-LOGGER = logging.getLogger(__name__)
-
-
 class E2EOrchestratorService(GenericGrpcService):
-    def __init__(self, cls_name: str = __name__):
+    def __init__(self, cls_name: str = __name__) -> None:
         port = get_service_port_grpc(ServiceNameEnum.E2EORCHESTRATOR)
         super().__init__(port, cls_name=cls_name)
         self.e2eorchestrator_servicer = E2EOrchestratorServiceServicerImpl()
@@ -33,3 +29,5 @@ class E2EOrchestratorService(GenericGrpcService):
         add_E2EOrchestratorServiceServicer_to_server(
             self.e2eorchestrator_servicer, self.server
         )
+
+        self.add_reflection_service_name(E2EORCHESTRATOR_DESCRIPTOR, 'E2EOrchestratorService')
diff --git a/src/forecaster/service/ForecasterService.py b/src/forecaster/service/ForecasterService.py
index 5f540cdc5..fedb5242d 100644
--- a/src/forecaster/service/ForecasterService.py
+++ b/src/forecaster/service/ForecasterService.py
@@ -14,6 +14,7 @@
 
 from common.Constants import ServiceNameEnum
 from common.Settings import get_service_port_grpc
+from common.proto.forecaster_pb2 import DESCRIPTOR as FORECASTER_DESCRIPTOR
 from common.proto.forecaster_pb2_grpc import add_ForecasterServiceServicer_to_server
 from common.tools.service.GenericGrpcService import GenericGrpcService
 from .ForecasterServiceServicerImpl import ForecasterServiceServicerImpl
@@ -26,3 +27,5 @@ class ForecasterService(GenericGrpcService):
 
     def install_servicers(self):
         add_ForecasterServiceServicer_to_server(self.forecaster_servicer, self.server)
+
+        self.add_reflection_service_name(FORECASTER_DESCRIPTOR, 'ForecasterService')
diff --git a/src/pathcomp/frontend/service/PathCompService.py b/src/pathcomp/frontend/service/PathCompService.py
index c19e59e77..6e413c14e 100644
--- a/src/pathcomp/frontend/service/PathCompService.py
+++ b/src/pathcomp/frontend/service/PathCompService.py
@@ -14,8 +14,9 @@
 
 from common.Constants import ServiceNameEnum
 from common.Settings import get_service_port_grpc
-from common.tools.service.GenericGrpcService import GenericGrpcService
+from common.proto.pathcomp_pb2 import DESCRIPTOR as PATHCOMP_DESCRIPTOR
 from common.proto.pathcomp_pb2_grpc import add_PathCompServiceServicer_to_server
+from common.tools.service.GenericGrpcService import GenericGrpcService
 from .PathCompServiceServicerImpl import PathCompServiceServicerImpl
 
 class PathCompService(GenericGrpcService):
@@ -26,3 +27,5 @@ class PathCompService(GenericGrpcService):
 
     def install_servicers(self):
         add_PathCompServiceServicer_to_server(self.pathcomp_servicer, self.server)
+
+        self.add_reflection_service_name(PATHCOMP_DESCRIPTOR, 'PathCompService')
diff --git a/src/service/service/ServiceService.py b/src/service/service/ServiceService.py
index b99826e5b..e088a99eb 100644
--- a/src/service/service/ServiceService.py
+++ b/src/service/service/ServiceService.py
@@ -14,6 +14,7 @@
 
 from common.Constants import ServiceNameEnum
 from common.Settings import get_service_port_grpc
+from common.proto.service_pb2 import DESCRIPTOR as SERVICE_DESCRIPTOR
 from common.proto.service_pb2_grpc import add_ServiceServiceServicer_to_server
 from common.tools.service.GenericGrpcService import GenericGrpcService
 from .ServiceServiceServicerImpl import ServiceServiceServicerImpl
@@ -27,3 +28,5 @@ class ServiceService(GenericGrpcService):
 
     def install_servicers(self):
         add_ServiceServiceServicer_to_server(self.service_servicer, self.server)
+
+        self.add_reflection_service_name(SERVICE_DESCRIPTOR, 'ServiceService')
diff --git a/src/slice/service/SliceService.py b/src/slice/service/SliceService.py
index dc2584f82..ac4e80976 100644
--- a/src/slice/service/SliceService.py
+++ b/src/slice/service/SliceService.py
@@ -14,9 +14,10 @@
 
 from common.Constants import ServiceNameEnum
 from common.Settings import get_service_port_grpc
+from common.proto.slice_pb2 import DESCRIPTOR as SLICE_DESCRIPTOR
 from common.proto.slice_pb2_grpc import add_SliceServiceServicer_to_server
 from common.tools.service.GenericGrpcService import GenericGrpcService
-from slice.service.SliceServiceServicerImpl import SliceServiceServicerImpl
+from .SliceServiceServicerImpl import SliceServiceServicerImpl
 
 class SliceService(GenericGrpcService):
     def __init__(self, cls_name: str = __name__) -> None:
@@ -26,3 +27,5 @@ class SliceService(GenericGrpcService):
 
     def install_servicers(self):
         add_SliceServiceServicer_to_server(self.slice_servicer, self.server)
+
+        self.add_reflection_service_name(SLICE_DESCRIPTOR, 'SliceService')
diff --git a/src/vnt_manager/service/VNTManagerService.py b/src/vnt_manager/service/VNTManagerService.py
index b95ad089a..3f44c4a51 100644
--- a/src/vnt_manager/service/VNTManagerService.py
+++ b/src/vnt_manager/service/VNTManagerService.py
@@ -12,19 +12,15 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import logging
-
 from common.Constants import ServiceNameEnum
-from common.proto.vnt_manager_pb2_grpc import add_VNTManagerServiceServicer_to_server
 from common.Settings import get_service_port_grpc
+from common.proto.vnt_manager_pb2 import DESCRIPTOR as VNT_MANAGER_DESCRIPTOR
+from common.proto.vnt_manager_pb2_grpc import add_VNTManagerServiceServicer_to_server
 from common.tools.service.GenericGrpcService import GenericGrpcService
 from .VNTManagerServiceServicerImpl import VNTManagerServiceServicerImpl
 
-LOGGER = logging.getLogger(__name__)
-
-
 class VNTManagerService(GenericGrpcService):
-    def __init__(self, cls_name: str = __name__):
+    def __init__(self, cls_name: str = __name__) -> None:
         port = get_service_port_grpc(ServiceNameEnum.VNTMANAGER)
         super().__init__(port, cls_name=cls_name)
         self.vntmanager_servicer = VNTManagerServiceServicerImpl()
@@ -33,3 +29,5 @@ class VNTManagerService(GenericGrpcService):
         add_VNTManagerServiceServicer_to_server(
             self.vntmanager_servicer, self.server
         )
+
+        self.add_reflection_service_name(VNT_MANAGER_DESCRIPTOR, 'VNTManagerService')
-- 
GitLab