diff --git a/proto/ztp_server.proto b/proto/ztp_server.proto index 37ccc71d36d87938b55dc1a53ddb8acaa7287116..6c575df1163cdbf77adee8ff1f5e9dbb57ca1b43 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 a790b76e29fbc4697ea0ee7094d327064b54fa23..5e5737857996672650d5fb95dcda5537552699eb 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 2b4da4f93c9126bd706ee288e7191153a482c64e..aba4aee94196b350b268502af785d3d712cd81dd 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 009cda9227a1652f21c67ebddfa60c057327bd20..015f958f489b0ffb250b7a7436477e27fbd6b49b 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 25e8605d300f3b26261ba3cbb10818b8671bdf6d..55bd5f1874a89ab0ba56ae9c604bf42d272f83fa 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