Add bi-directional GRPC python executor.
[ccsdk/cds.git] / ms / py-executor / blueprints_grpc / blueprint_processing_server.py
1 #!/usr/bin/python
2 #
3 #  Copyright © 2018-2019 AT&T Intellectual Property.
4 #
5 #  Licensed under the Apache License, Version 2.0 (the "License");
6 #  you may not use this file except in compliance with the License.
7 #  You may obtain a copy of the License at
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
11 #  Unless required by applicable law or agreed to in writing, software
12 #  distributed under the License is distributed on an "AS IS" BASIS,
13 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 #  See the License for the specific language governing permissions and
15 #  limitations under the License.
16
17 import logging
18 from google.protobuf.json_format import MessageToJson
19 from .proto import BluePrintProcessing_pb2_grpc as BluePrintProcessing_pb2_grpc
20 from .script_executor_configuration import ScriptExecutorConfiguration
21 from .executor_utils import instance_for_input
22
23
24 class AbstractScriptFunction:
25
26     def set_context(self, context):
27         self.context = context
28
29     def process(self, request):
30         pass
31
32     def recover(self, runtime_exception, execution_request):
33         pass
34
35
36 class BluePrintProcessingServer(BluePrintProcessing_pb2_grpc.BluePrintProcessingServiceServicer):
37
38     def __init__(self, configuration: ScriptExecutorConfiguration):
39         self.logger = logging.getLogger(self.__class__.__name__)
40         self.configuration = configuration
41
42     def process(self, request_iterator, context):
43         for request in request_iterator:
44             jsonObj = MessageToJson(request.payload)
45             self.logger.info(jsonObj)
46             # Get the Dynamic Process Instance based on request
47             instance: AbstractScriptFunction = instance_for_input(self.configuration, request)
48             instance.set_context(context)
49             return instance.process(request)