vFW and vDNS support added to azure-plugin
[multicloud/azure.git] / azure / aria / aria-extension-cloudify / src / aria / aria / parser / consumption / modeling.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 from .consumer import Consumer, ConsumerChain
17 from ...utils.formatting import json_dumps, yaml_dumps
18
19
20 class DeriveServiceTemplate(Consumer):
21     """
22     Derives the service template from the presenter.
23     """
24
25     def consume(self):
26         if self.context.presentation.presenter is None:
27             self.context.validation.report('DeriveServiceTemplate consumer: missing presenter')
28             return
29
30         if not hasattr(self.context.presentation.presenter, '_get_model'):
31             self.context.validation.report('DeriveServiceTemplate consumer: presenter does not'
32                                            ' support "_get_model"')
33             return
34
35         self.context.modeling.template = \
36             self.context.presentation.presenter._get_model(self.context)
37
38
39 class CoerceServiceTemplateValues(Consumer):
40     """
41     Coerces values in the service template.
42     """
43
44     def consume(self):
45         self.topology.coerce(self.context.modeling.template, report_issues=True)
46
47
48 class ValidateServiceTemplate(Consumer):
49     """
50     Validates the service template.
51     """
52
53     def consume(self):
54         self.topology.validate(self.context.modeling.template)
55
56
57 class ServiceTemplate(ConsumerChain):
58     """
59     Generates the service template from the presenter.
60     """
61
62     def __init__(self, context):
63         super(ServiceTemplate, self).__init__(context, (DeriveServiceTemplate,
64                                                         CoerceServiceTemplateValues,
65                                                         ValidateServiceTemplate))
66
67     def dump(self):
68         if self.context.has_arg_switch('yaml'):
69             indent = self.context.get_arg_value_int('indent', 2)
70             raw = self.context.modeling.template_as_raw
71             self.context.write(yaml_dumps(raw, indent=indent))
72         elif self.context.has_arg_switch('json'):
73             indent = self.context.get_arg_value_int('indent', 2)
74             raw = self.context.modeling.template_as_raw
75             self.context.write(json_dumps(raw, indent=indent))
76         else:
77             self.context.write(self.topology.dump(self.context.modeling.template))
78
79
80 class Types(Consumer):
81     """
82     Used to just dump the types.
83     """
84
85     def dump(self):
86         if self.context.has_arg_switch('yaml'):
87             indent = self.context.get_arg_value_int('indent', 2)
88             raw = self.context.modeling.types_as_raw
89             self.context.write(yaml_dumps(raw, indent=indent))
90         elif self.context.has_arg_switch('json'):
91             indent = self.context.get_arg_value_int('indent', 2)
92             raw = self.context.modeling.types_as_raw
93             self.context.write(json_dumps(raw, indent=indent))
94         else:
95             self.topology.dump_types(self.context, self.context.modeling.template)
96
97
98 class InstantiateServiceInstance(Consumer):
99     """
100     Instantiates the service template into a service instance.
101     """
102
103     def consume(self):
104         if self.context.modeling.template is None:
105             self.context.validation.report('InstantiateServiceInstance consumer: missing service '
106                                            'template')
107             return
108         self.context.modeling.instance = self.topology.instantiate(
109             self.context.modeling.template,
110             inputs=dict(self.context.modeling.inputs)
111         )
112
113
114 class CoerceServiceInstanceValues(Consumer):
115     """
116     Coerces values in the service instance.
117     """
118
119     def consume(self):
120         self.topology.coerce(self.context.modeling.instance, report_issues=True)
121
122
123 class ValidateServiceInstance(Consumer):
124     """
125     Validates the service instance.
126     """
127
128     def consume(self):
129         self.topology.validate(self.context.modeling.instance)
130
131
132 class SatisfyRequirements(Consumer):
133     """
134     Satisfies node requirements in the service instance.
135     """
136
137     def consume(self):
138         self.topology.satisfy_requirements(self.context.modeling.instance)
139
140
141 class ValidateCapabilities(Consumer):
142     """
143     Validates capabilities in the service instance.
144     """
145
146     def consume(self):
147         self.topology.validate_capabilities(self.context.modeling.instance)
148
149
150 class FindHosts(Consumer):
151     """
152     Find hosts for all nodes in the service instance.
153     """
154
155     def consume(self):
156         self.topology.assign_hosts(self.context.modeling.instance)
157
158
159 class ConfigureOperations(Consumer):
160     """
161     Configures all operations in the service instance.
162     """
163
164     def consume(self):
165         self.topology.configure_operations(self.context.modeling.instance)
166
167
168 class ServiceInstance(ConsumerChain):
169     """
170     Generates the service instance by instantiating the service template.
171     """
172
173     def __init__(self, context):
174         super(ServiceInstance, self).__init__(context, (InstantiateServiceInstance,
175                                                         CoerceServiceInstanceValues,
176                                                         ValidateServiceInstance,
177                                                         CoerceServiceInstanceValues,
178                                                         SatisfyRequirements,
179                                                         CoerceServiceInstanceValues,
180                                                         ValidateCapabilities,
181                                                         FindHosts,
182                                                         ConfigureOperations,
183                                                         CoerceServiceInstanceValues))
184
185     def dump(self):
186         if self.context.has_arg_switch('graph'):
187             self.context.modeling.instance.dump_graph()
188         elif self.context.has_arg_switch('yaml'):
189             indent = self.context.get_arg_value_int('indent', 2)
190             raw = self.context.modeling.instance_as_raw
191             self.context.write(yaml_dumps(raw, indent=indent))
192         elif self.context.has_arg_switch('json'):
193             indent = self.context.get_arg_value_int('indent', 2)
194             raw = self.context.modeling.instance_as_raw
195             self.context.write(json_dumps(raw, indent=indent))
196         else:
197             str_rep = self.topology.dump(self.context.modeling.instance)
198             self.context.write(str_rep)