ecddff67db605e956d4a2432eb807955983ed1c1
[multicloud/azure.git] / azure / aria / aria-rest-server / src / main / python / aria-rest / aria_rest / rest.py
1 #
2 # ============LICENSE_START===================================================
3 # Copyright (c) 2018 Amdocs. All rights reserved.
4 # ===================================================================
5 # Licensed under the Apache License, Version 2.0 (the "License"); you may not
6 # use this file except in compliance with the License. You may obtain a copy
7 # 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, WITHOUT
13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 # License for the specific language governing permissions and limitations under
15 # the License.
16 # ============LICENSE_END====================================================
17 #
18
19 from flask import Flask, request, jsonify
20 from flask_autodoc.autodoc import Autodoc
21 from aria import install_aria_extensions
22 from aria.cli.core import aria
23 from aria.utils import threading
24 from aria.orchestrator.workflow_runner import WorkflowRunner
25 from aria.orchestrator.workflows.executor.dry import DryExecutor
26 import util
27
28 version_id = "v0"
29 route_base = "/api/multicloud-azure/" + version_id + "/"
30 app = Flask("onap-aria-rest")
31 auto = Autodoc(app)
32
33 execution_state = util.SafeDict()
34
35
36 def main():
37     install_aria_extensions()
38     app.run(host='0.0.0.0', port=5000, threaded=True)
39
40
41 # start execution
42 @app.route(
43     route_base +
44     "services/<service_id>/executions/<workflow_name>",
45     methods=['POST'])
46 @auto.doc()
47 @aria.pass_model_storage
48 @aria.pass_resource_storage
49 @aria.pass_plugin_manager
50 @aria.pass_logger
51 def start_execution(
52         service_id,
53         workflow_name,
54         model_storage,
55         resource_storage,
56         plugin_manager,
57         logger):
58     """
59     Start an execution for the specified service
60     """
61     body = request.json or {}
62     executor = DryExecutor(
63         ) if 'executor' in body and body['executor'] == 'dry' else None
64
65     inputs = body['inputs'] if 'inputs' in body else None
66     task_max_attempts = (body['task_max_attempts']
67                          if 'task_max_attempts' in body else 30)
68     task_retry_interval = (body['task_retry_interval']
69                            if 'task_retry_interval' in body else 30)
70
71     runner = WorkflowRunner(model_storage, resource_storage, plugin_manager,
72                             service_id=service_id,
73                             workflow_name=workflow_name,
74                             inputs=inputs,
75                             executor=executor,
76                             task_max_attempts=task_max_attempts,
77                             task_retry_interval=task_retry_interval)
78
79     service = model_storage.service.get(service_id)
80     tname = '{}_{}_{}'.format(service.name, workflow_name, runner.execution_id)
81     thread = threading.ExceptionThread(target=runner.execute,
82                                        name=tname)
83     thread.start()
84     execution_state[str(runner.execution_id)] = [runner, thread]
85     logger.info("execution {} started".format(runner.execution_id))
86     return jsonify({"id": runner.execution_id}), 202
87
88
89 if __name__ == "__main__":
90     app.run(host='0.0.0.0', port=5000, threaded=True)