vFW and vDNS support added to azure-plugin
[multicloud/azure.git] / azure / aria / aria-extension-cloudify / src / aria / aria / orchestrator / workflows / builtin / workflows.py
1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements.  See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License.  You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 """
17 TSOCA normative lifecycle workflows.
18 """
19
20 from ... import workflow
21 from ..api import task
22
23
24 NORMATIVE_STANDARD_INTERFACE = 'Standard' # 'tosca.interfaces.node.lifecycle.Standard'
25 NORMATIVE_CONFIGURE_INTERFACE = 'Configure' # 'tosca.interfaces.relationship.Configure'
26
27 NORMATIVE_CREATE = 'create'
28 NORMATIVE_CONFIGURE = 'configure'
29 NORMATIVE_START = 'start'
30 NORMATIVE_STOP = 'stop'
31 NORMATIVE_DELETE = 'delete'
32
33 NORMATIVE_PRE_CONFIGURE_SOURCE = 'pre_configure_source'
34 NORMATIVE_PRE_CONFIGURE_TARGET = 'pre_configure_target'
35 NORMATIVE_POST_CONFIGURE_SOURCE = 'post_configure_source'
36 NORMATIVE_POST_CONFIGURE_TARGET = 'post_configure_target'
37
38 NORMATIVE_ADD_SOURCE = 'add_source'
39 NORMATIVE_ADD_TARGET = 'add_target'
40 NORMATIVE_REMOVE_TARGET = 'remove_target'
41 NORMATIVE_REMOVE_SOURCE = 'remove_source'
42 NORMATIVE_TARGET_CHANGED = 'target_changed'
43
44
45 __all__ = (
46     'NORMATIVE_STANDARD_INTERFACE',
47     'NORMATIVE_CONFIGURE_INTERFACE',
48     'NORMATIVE_CREATE',
49     'NORMATIVE_START',
50     'NORMATIVE_STOP',
51     'NORMATIVE_DELETE',
52     'NORMATIVE_CONFIGURE',
53     'NORMATIVE_PRE_CONFIGURE_SOURCE',
54     'NORMATIVE_PRE_CONFIGURE_TARGET',
55     'NORMATIVE_POST_CONFIGURE_SOURCE',
56     'NORMATIVE_POST_CONFIGURE_TARGET',
57     'NORMATIVE_ADD_SOURCE',
58     'NORMATIVE_ADD_TARGET',
59     'NORMATIVE_REMOVE_SOURCE',
60     'NORMATIVE_REMOVE_TARGET',
61     'NORMATIVE_TARGET_CHANGED',
62     'install_node',
63     'uninstall_node',
64     'start_node',
65     'stop_node',
66 )
67
68
69 @workflow(suffix_template='{node.name}')
70 def install_node(graph, node, **kwargs):
71     # Create
72     sequence = [task.create_task(node, NORMATIVE_STANDARD_INTERFACE, NORMATIVE_CREATE)]
73
74     # Configure
75     sequence += task.create_relationships_tasks(node,
76                                                 NORMATIVE_CONFIGURE_INTERFACE,
77                                                 NORMATIVE_PRE_CONFIGURE_SOURCE,
78                                                 NORMATIVE_PRE_CONFIGURE_TARGET)
79     sequence.append(task.create_task(node, NORMATIVE_STANDARD_INTERFACE, NORMATIVE_CONFIGURE))
80     sequence += task.create_relationships_tasks(node,
81                                                 NORMATIVE_CONFIGURE_INTERFACE,
82                                                 NORMATIVE_POST_CONFIGURE_SOURCE,
83                                                 NORMATIVE_POST_CONFIGURE_TARGET)
84     # Start
85     sequence += _create_start_tasks(node)
86
87     graph.sequence(*sequence)
88
89
90 @workflow(suffix_template='{node.name}')
91 def uninstall_node(graph, node, **kwargs):
92     # Stop
93     sequence = _create_stop_tasks(node)
94
95     # Delete
96     sequence.append(task.create_task(node, NORMATIVE_STANDARD_INTERFACE, NORMATIVE_DELETE))
97
98     graph.sequence(*sequence)
99
100
101 @workflow(suffix_template='{node.name}')
102 def start_node(graph, node, **kwargs):
103     graph.sequence(*_create_start_tasks(node))
104
105
106 @workflow(suffix_template='{node.name}')
107 def stop_node(graph, node, **kwargs):
108     graph.sequence(*_create_stop_tasks(node))
109
110
111 def _create_start_tasks(node):
112     sequence = [task.create_task(node, NORMATIVE_STANDARD_INTERFACE, NORMATIVE_START)]
113     sequence += task.create_relationships_tasks(node,
114                                                 NORMATIVE_CONFIGURE_INTERFACE,
115                                                 NORMATIVE_ADD_SOURCE, NORMATIVE_ADD_TARGET)
116     return sequence
117
118
119 def _create_stop_tasks(node):
120     sequence = [task.create_task(node, NORMATIVE_STANDARD_INTERFACE, NORMATIVE_STOP)]
121     sequence += task.create_relationships_tasks(node,
122                                                 NORMATIVE_CONFIGURE_INTERFACE,
123                                                 NORMATIVE_REMOVE_SOURCE, NORMATIVE_REMOVE_TARGET)
124     return sequence
125
126
127 def create_node_task_dependencies(graph, tasks_and_nodes, reverse=False):
128     """
129     Creates dependencies between tasks if there is a relationship (outbound) between their nodes.
130     """
131
132     def get_task(node_name):
133         for api_task, task_node in tasks_and_nodes:
134             if task_node.name == node_name:
135                 return api_task
136         return None
137
138     for api_task, node in tasks_and_nodes:
139         dependencies = []
140         for relationship in node.outbound_relationships:
141             dependency = get_task(relationship.target_node.name)
142             if dependency:
143                 dependencies.append(dependency)
144         if dependencies:
145             if reverse:
146                 for dependency in dependencies:
147                     graph.add_dependency(dependency, api_task)
148             else:
149                 graph.add_dependency(api_task, dependencies)