summaryrefslogtreecommitdiffstats
path: root/azure/aria/aria-extension-cloudify/src/aria/aria/parser/consumption/modeling.py
blob: 221b3086559d201765352121896ddaa36580b506 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# 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.

from .consumer import Consumer, ConsumerChain
from ...utils.formatting import json_dumps, yaml_dumps


class DeriveServiceTemplate(Consumer):
    """
    Derives the service template from the presenter.
    """

    def consume(self):
        if self.context.presentation.presenter is None:
            self.context.validation.report('DeriveServiceTemplate consumer: missing presenter')
            return

        if not hasattr(self.context.presentation.presenter, '_get_model'):
            self.context.validation.report('DeriveServiceTemplate consumer: presenter does not'
                                           ' support "_get_model"')
            return

        self.context.modeling.template = \
            self.context.presentation.presenter._get_model(self.context)


class CoerceServiceTemplateValues(Consumer):
    """
    Coerces values in the service template.
    """

    def consume(self):
        self.topology.coerce(self.context.modeling.template, report_issues=True)


class ValidateServiceTemplate(Consumer):
    """
    Validates the service template.
    """

    def consume(self):
        self.topology.validate(self.context.modeling.template)


class ServiceTemplate(ConsumerChain):
    """
    Generates the service template from the presenter.
    """

    def __init__(self, context):
        super(ServiceTemplate, self).__init__(context, (DeriveServiceTemplate,
                                                        CoerceServiceTemplateValues,
                                                        ValidateServiceTemplate))

    def dump(self):
        if self.context.has_arg_switch('yaml'):
            indent = self.context.get_arg_value_int('indent', 2)
            raw = self.context.modeling.template_as_raw
            self.context.write(yaml_dumps(raw, indent=indent))
        elif self.context.has_arg_switch('json'):
            indent = self.context.get_arg_value_int('indent', 2)
            raw = self.context.modeling.template_as_raw
            self.context.write(json_dumps(raw, indent=indent))
        else:
            self.context.write(self.topology.dump(self.context.modeling.template))


class Types(Consumer):
    """
    Used to just dump the types.
    """

    def dump(self):
        if self.context.has_arg_switch('yaml'):
            indent = self.context.get_arg_value_int('indent', 2)
            raw = self.context.modeling.types_as_raw
            self.context.write(yaml_dumps(raw, indent=indent))
        elif self.context.has_arg_switch('json'):
            indent = self.context.get_arg_value_int('indent', 2)
            raw = self.context.modeling.types_as_raw
            self.context.write(json_dumps(raw, indent=indent))
        else:
            self.topology.dump_types(self.context, self.context.modeling.template)


class InstantiateServiceInstance(Consumer):
    """
    Instantiates the service template into a service instance.
    """

    def consume(self):
        if self.context.modeling.template is None:
            self.context.validation.report('InstantiateServiceInstance consumer: missing service '
                                           'template')
            return
        self.context.modeling.instance = self.topology.instantiate(
            self.context.modeling.template,
            inputs=dict(self.context.modeling.inputs)
        )


class CoerceServiceInstanceValues(Consumer):
    """
    Coerces values in the service instance.
    """

    def consume(self):
        self.topology.coerce(self.context.modeling.instance, report_issues=True)


class ValidateServiceInstance(Consumer):
    """
    Validates the service instance.
    """

    def consume(self):
        self.topology.validate(self.context.modeling.instance)


class SatisfyRequirements(Consumer):
    """
    Satisfies node requirements in the service instance.
    """

    def consume(self):
        self.topology.satisfy_requirements(self.context.modeling.instance)


class ValidateCapabilities(Consumer):
    """
    Validates capabilities in the service instance.
    """

    def consume(self):
        self.topology.validate_capabilities(self.context.modeling.instance)


class FindHosts(Consumer):
    """
    Find hosts for all nodes in the service instance.
    """

    def consume(self):
        self.topology.assign_hosts(self.context.modeling.instance)


class ConfigureOperations(Consumer):
    """
    Configures all operations in the service instance.
    """

    def consume(self):
        self.topology.configure_operations(self.context.modeling.instance)


class ServiceInstance(ConsumerChain):
    """
    Generates the service instance by instantiating the service template.
    """

    def __init__(self, context):
        super(ServiceInstance, self).__init__(context, (InstantiateServiceInstance,
                                                        CoerceServiceInstanceValues,
                                                        ValidateServiceInstance,
                                                        CoerceServiceInstanceValues,
                                                        SatisfyRequirements,
                                                        CoerceServiceInstanceValues,
                                                        ValidateCapabilities,
                                                        FindHosts,
                                                        ConfigureOperations,
                                                        CoerceServiceInstanceValues))

    def dump(self):
        if self.context.has_arg_switch('graph'):
            self.context.modeling.instance.dump_graph()
        elif self.context.has_arg_switch('yaml'):
            indent = self.context.get_arg_value_int('indent', 2)
            raw = self.context.modeling.instance_as_raw
            self.context.write(yaml_dumps(raw, indent=indent))
        elif self.context.has_arg_switch('json'):
            indent = self.context.get_arg_value_int('indent', 2)
            raw = self.context.modeling.instance_as_raw
            self.context.write(json_dumps(raw, indent=indent))
        else:
            str_rep = self.topology.dump(self.context.modeling.instance)
            self.context.write(str_rep)