vFW and vDNS support added to azure-plugin
[multicloud/azure.git] / azure / aria / aria-extension-cloudify / src / aria / tests / mock / models.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 datetime import datetime
17
18 from aria.modeling import models
19 from aria.orchestrator import decorators
20 from aria.orchestrator.workflows.builtin.workflows import (
21     NORMATIVE_STANDARD_INTERFACE,
22     NORMATIVE_CREATE,
23     NORMATIVE_START,
24     NORMATIVE_STOP,
25     NORMATIVE_DELETE,
26     NORMATIVE_CONFIGURE,
27
28     NORMATIVE_CONFIGURE_INTERFACE,
29     NORMATIVE_PRE_CONFIGURE_SOURCE,
30     NORMATIVE_PRE_CONFIGURE_TARGET,
31     NORMATIVE_POST_CONFIGURE_SOURCE,
32     NORMATIVE_POST_CONFIGURE_TARGET,
33
34     NORMATIVE_ADD_SOURCE,
35     NORMATIVE_ADD_TARGET,
36     NORMATIVE_REMOVE_TARGET,
37     NORMATIVE_REMOVE_SOURCE
38 )
39
40 SERVICE_TEMPLATE_NAME = 'test_service_template'
41 SERVICE_NAME = 'test_service1'
42 NODE_TEMPLATE_NAME = 'test_node_template'
43 NODE_NAME = 'test_node1'
44 WORKFLOW_NAME = 'test_workflow'
45 TASK_RETRY_INTERVAL = 1
46 TASK_MAX_ATTEMPTS = 1
47
48 DEPENDENCY_NODE_TEMPLATE_NAME = 'dependency_node_template'
49 DEPENDENCY_NODE_NAME = 'dependency_node'
50 DEPENDENT_NODE_TEMPLATE_NAME = 'dependent_node_template'
51 DEPENDENT_NODE_NAME = 'dependent_node'
52
53
54 def create_service_template(name=SERVICE_TEMPLATE_NAME, description=None, inputs=None):
55     now = datetime.now()
56     inputs = inputs or {}
57     return models.ServiceTemplate(
58         name=name,
59         description=description,
60         inputs=inputs,
61         created_at=now,
62         updated_at=now,
63         main_file_name='main_file_name',
64         node_types=models.Type(variant='node', name='test_node_type'),
65         group_types=models.Type(variant='group', name='test_group_type'),
66         policy_types=models.Type(variant='policy', name='test_policy_type'),
67         relationship_types=models.Type(variant='relationship', name='test_relationship_type'),
68         capability_types=models.Type(variant='capability', name='test_capability_type'),
69         artifact_types=models.Type(variant='artifact', name='test_artifact_type'),
70         interface_types=models.Type(variant='interface', name='test_interface_type')
71     )
72
73
74 def create_service(service_template, name=SERVICE_NAME, inputs=None):
75     now = datetime.utcnow()
76     inputs = inputs or {}
77     return models.Service(
78         name=name,
79         inputs=inputs,
80         service_template=service_template,
81         description='',
82         created_at=now,
83         updated_at=now,
84     )
85
86
87 def create_service_with_dependencies(include_execution=False,
88                                      include_input=False,
89                                      include_output=False,
90                                      include_node=False):
91     service_template = create_service_template()
92     service = create_service(service_template=service_template)
93     if include_execution:
94         execution = create_execution(service=service, status=models.Execution.STARTED)
95         service.executions = [execution]
96         execution.id = '1'
97     if include_input:
98         input = create_input(name='input1', value='value1')
99         service.inputs = {'input1': input}
100     if include_output:
101         output = create_output(name='output1', value='value1')
102         service.outputs = {'output1': output}
103     if include_node:
104         node_template = create_node_template(service_template=service_template)
105         node = create_node(node_template, service, state=models.Node.STARTED)
106         node.id = '1'
107     return service
108
109
110 def create_node_template_with_dependencies(include_node=False, include_property=False):
111     service_template = create_service_template()
112     node_template = create_node_template(service_template=service_template)
113     if include_node:
114         service = create_service(service_template=service_template)
115         create_node(dependency_node_template=node_template, service=service)
116     if include_property:
117         node_template.properties = {'prop1': create_property(name='prop1', value='value1')}
118     return node_template
119
120
121 def create_node_with_dependencies(include_attribute=False):
122
123     node_template = create_node_template_with_dependencies()
124     node_template.service_template.services[0] = create_service(node_template.service_template)
125     node = create_node(node_template, node_template.service_template.services[0])
126     if include_attribute:
127         node.attributes['attribute1'] = models.Attribute.wrap('attribute1', 'value1')               # pylint: disable=unsubscriptable-object
128     return node
129
130
131 def create_node_template(service_template,
132                          name=NODE_TEMPLATE_NAME,
133                          type=models.Type(variant='node', name='test_node_type'),
134                          capability_templates=None,
135                          requirement_templates=None,
136                          interface_templates=None):
137     capability_templates = capability_templates or {}
138     requirement_templates = requirement_templates or []
139     interface_templates = interface_templates or {}
140     node_template = models.NodeTemplate(
141         name=name,
142         type=type,
143         capability_templates=capability_templates,
144         requirement_templates=requirement_templates,
145         interface_templates=interface_templates,
146         service_template=service_template)
147
148     service_template.node_templates[node_template.name] = node_template
149     return node_template
150
151
152 def create_dependency_node_template(service_template, name=DEPENDENCY_NODE_TEMPLATE_NAME):
153     node_type = service_template.node_types.get_descendant('test_node_type')
154     capability_type = service_template.capability_types.get_descendant('test_capability_type')
155
156     capability_template = models.CapabilityTemplate(
157         name='capability',
158         type=capability_type
159     )
160     return create_node_template(
161         service_template=service_template,
162         name=name,
163         type=node_type,
164         capability_templates=_dictify(capability_template)
165     )
166
167
168 def create_dependent_node_template(
169         service_template, dependency_node_template, name=DEPENDENT_NODE_TEMPLATE_NAME):
170     the_type = service_template.node_types.get_descendant('test_node_type')
171
172     requirement_template = models.RequirementTemplate(
173         name='requirement',
174         target_node_template=dependency_node_template
175     )
176     return create_node_template(
177         service_template=service_template,
178         name=name,
179         type=the_type,
180         interface_templates=_dictify(get_standard_interface_template(service_template)),
181         requirement_templates=[requirement_template],
182     )
183
184
185 def create_node(dependency_node_template, service, name=NODE_NAME, state=models.Node.INITIAL):
186     node = models.Node(
187         name=name,
188         type=dependency_node_template.type,
189         version=None,
190         node_template=dependency_node_template,
191         state=state,
192         service=service,
193         interfaces=get_standard_interface(service),
194     )
195     service.nodes[node.name] = node
196     return node
197
198
199 def create_relationship(source, target):
200     return models.Relationship(
201         source_node=source,
202         target_node=target,
203         interfaces=get_configure_interfaces(service=source.service),
204     )
205
206
207 def create_interface_template(service_template, interface_name, operation_name,
208                               operation_kwargs=None, interface_kwargs=None):
209     the_type = service_template.interface_types.get_descendant('test_interface_type')
210     operation_template = models.OperationTemplate(
211         name=operation_name,
212         **(operation_kwargs or {})
213     )
214     return models.InterfaceTemplate(
215         type=the_type,
216         operation_templates=_dictify(operation_template),
217         name=interface_name,
218         **(interface_kwargs or {})
219     )
220
221
222 def create_operation(operation_name, operation_kwargs=None):
223     if operation_kwargs and operation_kwargs.get('arguments'):
224         operation_kwargs['arguments'] = dict(
225             (argument_name, models.Argument.wrap(argument_name, argument_value))
226             for argument_name, argument_value in operation_kwargs['arguments'].iteritems()
227             if argument_value is not None)
228
229     return models.Operation(
230         name=operation_name,
231         **(operation_kwargs or {})
232     )
233
234
235 def create_interface(service, interface_name, operation_name, operation_kwargs=None,
236                      interface_kwargs=None):
237     the_type = service.service_template.interface_types.get_descendant('test_interface_type')
238     operation = create_operation(operation_name, operation_kwargs)
239
240     return models.Interface(
241         type=the_type,
242         operations=_dictify(operation),
243         name=interface_name,
244         **(interface_kwargs or {})
245     )
246
247
248 def create_execution(service, status=models.Execution.PENDING):
249     return models.Execution(
250         service=service,
251         status=status,
252         workflow_name=WORKFLOW_NAME,
253         created_at=datetime.utcnow(),
254         started_at=datetime.utcnow(),
255         inputs={}
256     )
257
258
259 def create_plugin(name='test_plugin', package_version='0.1'):
260     return models.Plugin(
261         name=name,
262         archive_name='archive_name',
263         distribution='distribution',
264         distribution_release='dist_release',
265         distribution_version='dist_version',
266         package_name='package',
267         package_source='source',
268         package_version=package_version,
269         supported_platform='any',
270         supported_py_versions=['python27'],
271         uploaded_at=datetime.now(),
272         wheels=[],
273     )
274
275
276 def create_plugin_specification(name='test_plugin', version='0.1'):
277     return models.PluginSpecification(
278         name=name,
279         version=version
280     )
281
282
283 def _create_parameter(name, value, model_cls):
284     return model_cls.wrap(name, value)
285
286
287 def create_property(name, value):
288     return _create_parameter(name, value, model_cls=models.Property)
289
290
291 def create_input(name, value):
292     return _create_parameter(name, value, model_cls=models.Input)
293
294
295 def create_output(name, value):
296     return _create_parameter(name, value, model_cls=models.Output)
297
298
299 def _dictify(item):
300     return dict(((item.name, item),))
301
302
303 def get_standard_interface_template(service_template):
304     the_type = service_template.interface_types.get_descendant('test_interface_type')
305
306     op_templates = dict(
307         (op_name, models.OperationTemplate(
308             name=op_name, implementation='{0}.{1}'.format(__file__, mock_operation.__name__)))
309         for op_name in (NORMATIVE_CREATE, NORMATIVE_CONFIGURE, NORMATIVE_START,
310                         NORMATIVE_STOP, NORMATIVE_DELETE)
311     )
312     return models.InterfaceTemplate(name=NORMATIVE_STANDARD_INTERFACE,
313                                     operation_templates=op_templates,
314                                     type=the_type)
315
316
317 def get_standard_interface(service):
318     the_type = service.service_template.interface_types.get_descendant('test_interface_type')
319
320     ops = dict(
321         (op_name, models.Operation(
322             name=op_name, implementation='{0}.{1}'.format(__file__, mock_operation.__name__)))
323         for op_name in (NORMATIVE_CREATE, NORMATIVE_CONFIGURE, NORMATIVE_START,
324                         NORMATIVE_STOP, NORMATIVE_DELETE)
325     )
326     return {
327         NORMATIVE_STANDARD_INTERFACE:
328             models.Interface(name=NORMATIVE_STANDARD_INTERFACE, operations=ops, type=the_type)
329     }
330
331
332 def get_configure_interfaces(service):
333     the_type = service.service_template.interface_types.get_descendant('test_interface_type')
334
335     operations = dict(
336         (op_name, models.Operation(
337             name=op_name, implementation='{0}.{1}'.format(__file__, mock_operation.__name__)))
338         for op_name in (NORMATIVE_PRE_CONFIGURE_SOURCE,
339                         NORMATIVE_POST_CONFIGURE_SOURCE,
340                         NORMATIVE_ADD_SOURCE,
341                         NORMATIVE_REMOVE_SOURCE,
342
343                         NORMATIVE_PRE_CONFIGURE_TARGET,
344                         NORMATIVE_POST_CONFIGURE_TARGET,
345                         NORMATIVE_ADD_TARGET,
346                         NORMATIVE_REMOVE_TARGET)
347     )
348     interface = {
349         NORMATIVE_CONFIGURE_INTERFACE: models.Interface(
350             name=NORMATIVE_CONFIGURE_INTERFACE, operations=operations, type=the_type)
351     }
352
353     return interface
354
355
356 @decorators.operation
357 def mock_operation(*args, **kwargs):
358     pass