summaryrefslogtreecommitdiffstats
path: root/azure/aria/aria-extension-cloudify/src/aria/aria/orchestrator/workflows/builtin/workflows.py
blob: b286e98978c32fb13b07d9eaa78a7bd3d6b41a2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
TSOCA normative lifecycle workflows.
"""

from ... import workflow
from ..api import task


NORMATIVE_STANDARD_INTERFACE = 'Standard' # 'tosca.interfaces.node.lifecycle.Standard'
NORMATIVE_CONFIGURE_INTERFACE = 'Configure' # 'tosca.interfaces.relationship.Configure'

NORMATIVE_CREATE = 'create'
NORMATIVE_CONFIGURE = 'configure'
NORMATIVE_START = 'start'
NORMATIVE_STOP = 'stop'
NORMATIVE_DELETE = 'delete'

NORMATIVE_PRE_CONFIGURE_SOURCE = 'pre_configure_source'
NORMATIVE_PRE_CONFIGURE_TARGET = 'pre_configure_target'
NORMATIVE_POST_CONFIGURE_SOURCE = 'post_configure_source'
NORMATIVE_POST_CONFIGURE_TARGET = 'post_configure_target'

NORMATIVE_ADD_SOURCE = 'add_source'
NORMATIVE_ADD_TARGET = 'add_target'
NORMATIVE_REMOVE_TARGET = 'remove_target'
NORMATIVE_REMOVE_SOURCE = 'remove_source'
NORMATIVE_TARGET_CHANGED = 'target_changed'


__all__ = (
    'NORMATIVE_STANDARD_INTERFACE',
    'NORMATIVE_CONFIGURE_INTERFACE',
    'NORMATIVE_CREATE',
    'NORMATIVE_START',
    'NORMATIVE_STOP',
    'NORMATIVE_DELETE',
    'NORMATIVE_CONFIGURE',
    'NORMATIVE_PRE_CONFIGURE_SOURCE',
    'NORMATIVE_PRE_CONFIGURE_TARGET',
    'NORMATIVE_POST_CONFIGURE_SOURCE',
    'NORMATIVE_POST_CONFIGURE_TARGET',
    'NORMATIVE_ADD_SOURCE',
    'NORMATIVE_ADD_TARGET',
    'NORMATIVE_REMOVE_SOURCE',
    'NORMATIVE_REMOVE_TARGET',
    'NORMATIVE_TARGET_CHANGED',
    'install_node',
    'uninstall_node',
    'start_node',
    'stop_node',
)


@workflow(suffix_template='{node.name}')
def install_node(graph, node, **kwargs):
    # Create
    sequence = [task.create_task(node, NORMATIVE_STANDARD_INTERFACE, NORMATIVE_CREATE)]

    # Configure
    sequence += task.create_relationships_tasks(node,
                                                NORMATIVE_CONFIGURE_INTERFACE,
                                                NORMATIVE_PRE_CONFIGURE_SOURCE,
                                                NORMATIVE_PRE_CONFIGURE_TARGET)
    sequence.append(task.create_task(node, NORMATIVE_STANDARD_INTERFACE, NORMATIVE_CONFIGURE))
    sequence += task.create_relationships_tasks(node,
                                                NORMATIVE_CONFIGURE_INTERFACE,
                                                NORMATIVE_POST_CONFIGURE_SOURCE,
                                                NORMATIVE_POST_CONFIGURE_TARGET)
    # Start
    sequence += _create_start_tasks(node)

    graph.sequence(*sequence)


@workflow(suffix_template='{node.name}')
def uninstall_node(graph, node, **kwargs):
    # Stop
    sequence = _create_stop_tasks(node)

    # Delete
    sequence.append(task.create_task(node, NORMATIVE_STANDARD_INTERFACE, NORMATIVE_DELETE))

    graph.sequence(*sequence)


@workflow(suffix_template='{node.name}')
def start_node(graph, node, **kwargs):
    graph.sequence(*_create_start_tasks(node))


@workflow(suffix_template='{node.name}')
def stop_node(graph, node, **kwargs):
    graph.sequence(*_create_stop_tasks(node))


def _create_start_tasks(node):
    sequence = [task.create_task(node, NORMATIVE_STANDARD_INTERFACE, NORMATIVE_START)]
    sequence += task.create_relationships_tasks(node,
                                                NORMATIVE_CONFIGURE_INTERFACE,
                                                NORMATIVE_ADD_SOURCE, NORMATIVE_ADD_TARGET)
    return sequence


def _create_stop_tasks(node):
    sequence = [task.create_task(node, NORMATIVE_STANDARD_INTERFACE, NORMATIVE_STOP)]
    sequence += task.create_relationships_tasks(node,
                                                NORMATIVE_CONFIGURE_INTERFACE,
                                                NORMATIVE_REMOVE_SOURCE, NORMATIVE_REMOVE_TARGET)
    return sequence


def create_node_task_dependencies(graph, tasks_and_nodes, reverse=False):
    """
    Creates dependencies between tasks if there is a relationship (outbound) between their nodes.
    """

    def get_task(node_name):
        for api_task, task_node in tasks_and_nodes:
            if task_node.name == node_name:
                return api_task
        return None

    for api_task, node in tasks_and_nodes:
        dependencies = []
        for relationship in node.outbound_relationships:
            dependency = get_task(relationship.target_node.name)
            if dependency:
                dependencies.append(dependency)
        if dependencies:
            if reverse:
                for dependency in dependencies:
                    graph.add_dependency(dependency, api_task)
            else:
                graph.add_dependency(api_task, dependencies)