1 # Copyright (c) 2018 Amdocs
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 from multicloud_azure.pub.aria import util
20 from aria.cli.core import aria
21 from aria.cli import utils
22 from aria.core import Core
23 from aria.cli import service_template_utils
24 from aria.storage import exceptions as storage_exceptions
25 from aria.utils import threading
26 from aria.orchestrator.workflow_runner import WorkflowRunner as Runner
28 LOG = logging.getLogger(__name__)
30 execution_state = util.SafeDict()
33 class AriaServiceImpl(object):
35 def deploy_service(self, template_name, template_body, inputs, logger):
37 service_template_name = template_name + "-template" + \
38 time.strftime('%Y%m%d%H%M%S')
39 status = self.install_template_private(service_template_name,
41 if (status[1] != 200):
42 logger.error("Error while installing the service-template")
43 return status[0], status[1]
45 logger.info("service template {0} valiadated and stored".format(
46 service_template_name))
47 status = self.create_service(
48 status, template_name + time.strftime('%Y%m%d%H%M%S'), inputs)
49 if (status[1] != 200):
50 return status[0], status[1]
51 execution_id = time.strftime('%Y%m%d%H%M%S')
52 thread = threading.ExceptionThread(target=self.start_execution,
53 args=(status[2].id, execution_id,
56 return execution_id, 200
58 @aria.pass_model_storage
59 @aria.pass_resource_storage
60 @aria.pass_plugin_manager
62 def install_template_private(self, service_template_name, template_body,
67 service_template_filename = "MainServiceTemplate.yaml"
68 fileSp = template_body
69 f = tempfile.NamedTemporaryFile(suffix='.csar',
71 f.write(fileSp.read())
72 f.seek(fileSp.tell(), 0)
73 service_template_path = f.name
75 file_path = service_template_utils.get(
76 service_template_path, service_template_filename)
78 core = Core(model_storage, resource_storage, plugin_manager)
79 logger.info("service-template file {}".format(file_path))
82 service_template_id = core.create_service_template(
84 os.path.dirname(file_path),
85 service_template_name)
86 except storage_exceptions.StorageError as e:
87 logger.error("storage exception")
88 utils.check_overriding_storage_exceptions(
89 e, 'service template', service_template_name)
91 except Exception as e:
92 logger.error("catchall exception")
94 return "service template installed", 200, service_template_id
96 @aria.pass_model_storage
97 @aria.pass_resource_storage
98 @aria.pass_plugin_manager
100 def create_service(self, template_id, service_name, input,
106 Creates a service from the specified service template
108 input = input['sdnc_directives'] if'sdnc_directives'in input else None
109 core = Core(model_storage, resource_storage, plugin_manager)
110 service = core.create_service(template_id, input, service_name)
111 logger.info("service {} created".format(service.name))
112 return "service {} created".format(service.name), 200, service
114 @aria.pass_model_storage
115 @aria.pass_resource_storage
116 @aria.pass_plugin_manager
118 def start_execution(self, service_id, execution_id, input, workflow_name,
124 Start an execution for the specified service
126 input = input['sdnc_directives'] if'sdnc_directives'in input else None
127 runner = Runner(model_storage, resource_storage, plugin_manager,
128 execution_id=execution_id,
129 service_id=service_id,
130 workflow_name=workflow_name,
133 service = model_storage.service.get(service_id)
134 tname = '{}_{}_{}'.format(service.name, workflow_name,
136 thread = threading.ExceptionThread(target=runner.execute,
139 execution_state[str(runner.execution_id)] = [runner, thread]
140 logger.info("execution {} started".format(runner.execution_id))
141 return json.dumps({"id": runner.execution_id}), 202
143 @aria.pass_model_storage
145 def show_execution(self, execution_id, model_storage, logger):
147 Return details of specified execution/Stack
150 execution = model_storage.execution.get(execution_id)
151 except BaseException:
152 return "Execution {} not found".format(execution_id), 404
153 logger.info("showing details of execution id {}".format(execution_id))
154 return json.dumps({"execution_id": execution_id,
155 "service_name": execution.service_name,
156 "service_template_name":
157 execution.service_template_name,
158 "workflow_name": execution.workflow_name,
159 "status": execution.status}), 200