From 11b4748c60ca1dd0526ab31857ac6d4b05e9d0d6 Mon Sep 17 00:00:00 2001
From: jimenezquesa <manuel.jimenez@eviden.com>
Date: Thu, 19 Dec 2024 11:57:22 +0100
Subject: [PATCH] Second Draft

---
 proto/ztp_server.proto                        | 24 +++++++++++++--
 src/ztp_server/client/ZtpClient.py            |  4 +--
 src/ztp_server/service/ZtpServerService.py    |  2 +-
 .../service/ZtpServerServiceServicerImpl.py   | 29 ++++++++++++++-----
 src/ztp_server/service/__main__.py            |  1 +
 5 files changed, 46 insertions(+), 14 deletions(-)

diff --git a/proto/ztp_server.proto b/proto/ztp_server.proto
index 37ccc71d3..6c575df11 100755
--- a/proto/ztp_server.proto
+++ b/proto/ztp_server.proto
@@ -15,9 +15,27 @@
 syntax = "proto3";
 package ztpServer;
 
-import "context.proto";
+//import "context.proto";
 
 service ZtpServerService {
-  rpc GetProvisioningScript     (context.ProvisioningScriptName         ) returns (context.ProvisioningScript       ) {}
-  rpc GetZtpProvisioning        (context.ZtpFileName                    ) returns (context.ZtpFile       ) {}
+  rpc GetProvisioningScript     (ProvisioningScriptName         ) returns (ProvisioningScript       ) {}
+  rpc GetZtpProvisioning        (ZtpFileName                    ) returns (ZtpFile       ) {}
+}
+
+
+// Define the request message for both methods
+message ProvisioningScriptName {
+  string input = 1;
+}
+
+message ZtpFileName {
+  string input = 1;
+}
+
+message ProvisioningScript {
+  string script = 1;
+}
+
+message ZtpFile {
+  string json = 1;
 }
diff --git a/src/ztp_server/client/ZtpClient.py b/src/ztp_server/client/ZtpClient.py
index a790b76e2..5e5737857 100755
--- a/src/ztp_server/client/ZtpClient.py
+++ b/src/ztp_server/client/ZtpClient.py
@@ -15,7 +15,7 @@
 import grpc, logging
 from common.Constants import ServiceNameEnum
 from common.Settings import get_service_host, get_service_port_grpc
-from common.proto.ztpServer_pb2_grpc import ztpServerServiceStub    #TODO
+from common.proto.ztp_server_pb2_grpc import ztpServerServiceStub
 from common.proto.context_pb2 import (
     ZtpFileName, ZtpFile, ProvisioningScriptName, ProvisioningScript)
 from common.tools.client.RetryDecorator import retry, delay_exponential
@@ -39,7 +39,7 @@ class ZtpClient:
 
     def connect(self):
         self.channel = grpc.insecure_channel(self.endpoint)
-        self.stub = ztpServerServiceStub(self.channel) #TODO
+        self.stub = ztpServerServiceStub(self.channel)
 
     def close(self):
         if self.channel is not None: self.channel.close()
diff --git a/src/ztp_server/service/ZtpServerService.py b/src/ztp_server/service/ZtpServerService.py
index 2b4da4f93..aba4aee94 100755
--- a/src/ztp_server/service/ZtpServerService.py
+++ b/src/ztp_server/service/ZtpServerService.py
@@ -14,7 +14,7 @@
 
 from common.Constants import ServiceNameEnum
 from common.Settings import get_service_port_grpc
-from common.proto.ztpServer_pb2_grpc import add_Ztp_ServerServiceServicer_to_server 
+from common.proto.ztp_server_pb2_grpc import add_Ztp_ServerServiceServicer_to_server 
 from common.tools.service.GenericGrpcService import GenericGrpcService
 from ztp_server.service.ZtpServerServiceServicerImpl import ZtpServerServiceServicerImpl
 
diff --git a/src/ztp_server/service/ZtpServerServiceServicerImpl.py b/src/ztp_server/service/ZtpServerServiceServicerImpl.py
index 009cda922..015f958f4 100755
--- a/src/ztp_server/service/ZtpServerServiceServicerImpl.py
+++ b/src/ztp_server/service/ZtpServerServiceServicerImpl.py
@@ -12,11 +12,11 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import grpc, logging
+import grpc, logging, json
 from common.method_wrappers.Decorator import MetricsPool, safe_and_metered_rpc_method
 from common.proto.context_pb2 import (
     ZtpFileName, ZtpFile, ProvisioningScriptName, ProvisioningScript)
-from common.proto.ztpServer_pb2_grpc import ztpServerServiceServicer
+from common.proto.ztp_server_pb2_grpc import ztpServerServiceServicer
 
 LOGGER = logging.getLogger(__name__)
 
@@ -29,12 +29,25 @@ class ZtpServerServiceServicerImpl(ztpServerServiceServicer):
 
     @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
     def GetZtpProvisioning(self, request : ProvisioningScriptName, context : grpc.ServicerContext) -> ProvisioningScript:
-        LOGGER.warning('NOT IMPLEMENTED')
-        return ProvisioningScript()
+        try:
+            filePath = '../data/' + ProvisioningScriptName
+            with open(filePath, 'r') as provisioning_file:
+                provisioning_content = provisioning_file.read()
+            return ztpServerServiceServicer.ProvisioningScript(script=provisioning_content)
+        except FileNotFoundError:
+            context.set_code(grpc.StatusCode.NOT_FOUND)
+            context.set_details('File not found')
+            return ztpServerServiceServicer.ProvisioningScript()
+
     
     @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
     def GetZtpProvisioning(self, request : ZtpFileName, context : grpc.ServicerContext) -> ZtpFile:
-        LOGGER.warning('NOT IMPLEMENTED')
-        return ZtpFile()
-    
-
+        try:
+            filePath = '../data/' + ZtpFileName
+            with open(filePath, 'r') as json_file:
+                json_content = json_file.read()
+            return ztpServerServiceServicer.ZtpFile(json=json_content)
+        except FileNotFoundError:
+            context.set_code(grpc.StatusCode.NOT_FOUND)
+            context.set_details('File not found')
+            return ztpServerServiceServicer.ZtpFile(json=json_content)
\ No newline at end of file
diff --git a/src/ztp_server/service/__main__.py b/src/ztp_server/service/__main__.py
index 25e8605d3..55bd5f187 100755
--- a/src/ztp_server/service/__main__.py
+++ b/src/ztp_server/service/__main__.py
@@ -21,6 +21,7 @@ from common.Settings import (
 )
 from .ZtpServerService import ZtpServerService
 from .rest_server.RestServer import RestServer
+from .rest_server.ztpServer_plugins.tfs_api import register_tfs_api
 
 from .context_subscription import register_context_subscription
 
-- 
GitLab