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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 TSOCA normative lifecycle workflows.
20 from ... import workflow
21 from ..api import task
24 NORMATIVE_STANDARD_INTERFACE = 'Standard' # 'tosca.interfaces.node.lifecycle.Standard'
25 NORMATIVE_CONFIGURE_INTERFACE = 'Configure' # 'tosca.interfaces.relationship.Configure'
27 NORMATIVE_CREATE = 'create'
28 NORMATIVE_CONFIGURE = 'configure'
29 NORMATIVE_START = 'start'
30 NORMATIVE_STOP = 'stop'
31 NORMATIVE_DELETE = 'delete'
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'
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'
46 'NORMATIVE_STANDARD_INTERFACE',
47 'NORMATIVE_CONFIGURE_INTERFACE',
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',
69 @workflow(suffix_template='{node.name}')
70 def install_node(graph, node, **kwargs):
72 sequence = [task.create_task(node, NORMATIVE_STANDARD_INTERFACE, NORMATIVE_CREATE)]
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)
85 sequence += _create_start_tasks(node)
87 graph.sequence(*sequence)
90 @workflow(suffix_template='{node.name}')
91 def uninstall_node(graph, node, **kwargs):
93 sequence = _create_stop_tasks(node)
96 sequence.append(task.create_task(node, NORMATIVE_STANDARD_INTERFACE, NORMATIVE_DELETE))
98 graph.sequence(*sequence)
101 @workflow(suffix_template='{node.name}')
102 def start_node(graph, node, **kwargs):
103 graph.sequence(*_create_start_tasks(node))
106 @workflow(suffix_template='{node.name}')
107 def stop_node(graph, node, **kwargs):
108 graph.sequence(*_create_stop_tasks(node))
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)
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)
127 def create_node_task_dependencies(graph, tasks_and_nodes, reverse=False):
129 Creates dependencies between tasks if there is a relationship (outbound) between their nodes.
132 def get_task(node_name):
133 for api_task, task_node in tasks_and_nodes:
134 if task_node.name == node_name:
138 for api_task, node in tasks_and_nodes:
140 for relationship in node.outbound_relationships:
141 dependency = get_task(relationship.target_node.name)
143 dependencies.append(dependency)
146 for dependency in dependencies:
147 graph.add_dependency(dependency, api_task)
149 graph.add_dependency(api_task, dependencies)