vFW and vDNS support added to azure-plugin
[multicloud/azure.git] / azure / multicloud_azure / pub / aria / service.py
1 # Copyright (c) 2018 Amdocs
2 #
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:
6 #
7 #       http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
12
13 import logging
14 import json
15 import tempfile
16 import time
17 import os
18
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
27
28 LOG = logging.getLogger(__name__)
29
30 execution_state = util.SafeDict()
31
32
33 class AriaServiceImpl(object):
34
35     def deploy_service(self, template_name, template_body, inputs, logger):
36
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,
40                                                template_body)
41         if (status[1] != 200):
42             logger.error("Error while installing the service-template")
43             return status[0], status[1]
44         else:
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,
54                                                  inputs, 'install'))
55         thread.start()
56         return execution_id, 200
57
58     @aria.pass_model_storage
59     @aria.pass_resource_storage
60     @aria.pass_plugin_manager
61     @aria.pass_logger
62     def install_template_private(self, service_template_name, template_body,
63                                  model_storage,
64                                  resource_storage,
65                                  plugin_manager,
66                                  logger):
67         service_template_filename = "MainServiceTemplate.yaml"
68         fileSp = template_body
69         f = tempfile.NamedTemporaryFile(suffix='.csar',
70                                         delete=False)
71         f.write(fileSp.read())
72         f.seek(fileSp.tell(), 0)
73         service_template_path = f.name
74         fileSp.close()
75         file_path = service_template_utils.get(
76             service_template_path, service_template_filename)
77
78         core = Core(model_storage, resource_storage, plugin_manager)
79         logger.info("service-template file {}".format(file_path))
80
81         try:
82             service_template_id = core.create_service_template(
83                 file_path,
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)
90             return e.message, 500
91         except Exception as e:
92             logger.error("catchall exception")
93             return e.message, 500
94         return "service template installed", 200, service_template_id
95
96     @aria.pass_model_storage
97     @aria.pass_resource_storage
98     @aria.pass_plugin_manager
99     @aria.pass_logger
100     def create_service(self, template_id, service_name, input,
101                        model_storage,
102                        resource_storage,
103                        plugin_manager,
104                        logger):
105         """
106         Creates a service from the specified service template
107         """
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
113
114     @aria.pass_model_storage
115     @aria.pass_resource_storage
116     @aria.pass_plugin_manager
117     @aria.pass_logger
118     def start_execution(self, service_id, execution_id, input, workflow_name,
119                         model_storage,
120                         resource_storage,
121                         plugin_manager,
122                         logger):
123         """
124         Start an execution for the specified service
125         """
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,
131                         inputs=input)
132
133         service = model_storage.service.get(service_id)
134         tname = '{}_{}_{}'.format(service.name, workflow_name,
135                                   runner.execution_id)
136         thread = threading.ExceptionThread(target=runner.execute,
137                                            name=tname)
138         thread.start()
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
142
143     @aria.pass_model_storage
144     @aria.pass_logger
145     def show_execution(self, execution_id, model_storage, logger):
146         """
147         Return details of specified execution/Stack
148         """
149         try:
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