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.
16 from datetime import datetime
18 from aria.modeling import models
19 from aria.orchestrator import decorators
20 from aria.orchestrator.workflows.builtin.workflows import (
21 NORMATIVE_STANDARD_INTERFACE,
28 NORMATIVE_CONFIGURE_INTERFACE,
29 NORMATIVE_PRE_CONFIGURE_SOURCE,
30 NORMATIVE_PRE_CONFIGURE_TARGET,
31 NORMATIVE_POST_CONFIGURE_SOURCE,
32 NORMATIVE_POST_CONFIGURE_TARGET,
36 NORMATIVE_REMOVE_TARGET,
37 NORMATIVE_REMOVE_SOURCE
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
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'
54 def create_service_template(name=SERVICE_TEMPLATE_NAME, description=None, inputs=None):
57 return models.ServiceTemplate(
59 description=description,
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')
74 def create_service(service_template, name=SERVICE_NAME, inputs=None):
75 now = datetime.utcnow()
77 return models.Service(
80 service_template=service_template,
87 def create_service_with_dependencies(include_execution=False,
91 service_template = create_service_template()
92 service = create_service(service_template=service_template)
94 execution = create_execution(service=service, status=models.Execution.STARTED)
95 service.executions = [execution]
98 input = create_input(name='input1', value='value1')
99 service.inputs = {'input1': input}
101 output = create_output(name='output1', value='value1')
102 service.outputs = {'output1': output}
104 node_template = create_node_template(service_template=service_template)
105 node = create_node(node_template, service, state=models.Node.STARTED)
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)
114 service = create_service(service_template=service_template)
115 create_node(dependency_node_template=node_template, service=service)
117 node_template.properties = {'prop1': create_property(name='prop1', value='value1')}
121 def create_node_with_dependencies(include_attribute=False):
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
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(
143 capability_templates=capability_templates,
144 requirement_templates=requirement_templates,
145 interface_templates=interface_templates,
146 service_template=service_template)
148 service_template.node_templates[node_template.name] = node_template
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')
156 capability_template = models.CapabilityTemplate(
160 return create_node_template(
161 service_template=service_template,
164 capability_templates=_dictify(capability_template)
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')
172 requirement_template = models.RequirementTemplate(
174 target_node_template=dependency_node_template
176 return create_node_template(
177 service_template=service_template,
180 interface_templates=_dictify(get_standard_interface_template(service_template)),
181 requirement_templates=[requirement_template],
185 def create_node(dependency_node_template, service, name=NODE_NAME, state=models.Node.INITIAL):
188 type=dependency_node_template.type,
190 node_template=dependency_node_template,
193 interfaces=get_standard_interface(service),
195 service.nodes[node.name] = node
199 def create_relationship(source, target):
200 return models.Relationship(
203 interfaces=get_configure_interfaces(service=source.service),
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(
212 **(operation_kwargs or {})
214 return models.InterfaceTemplate(
216 operation_templates=_dictify(operation_template),
218 **(interface_kwargs or {})
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)
229 return models.Operation(
231 **(operation_kwargs or {})
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)
240 return models.Interface(
242 operations=_dictify(operation),
244 **(interface_kwargs or {})
248 def create_execution(service, status=models.Execution.PENDING):
249 return models.Execution(
252 workflow_name=WORKFLOW_NAME,
253 created_at=datetime.utcnow(),
254 started_at=datetime.utcnow(),
259 def create_plugin(name='test_plugin', package_version='0.1'):
260 return models.Plugin(
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(),
276 def create_plugin_specification(name='test_plugin', version='0.1'):
277 return models.PluginSpecification(
283 def _create_parameter(name, value, model_cls):
284 return model_cls.wrap(name, value)
287 def create_property(name, value):
288 return _create_parameter(name, value, model_cls=models.Property)
291 def create_input(name, value):
292 return _create_parameter(name, value, model_cls=models.Input)
295 def create_output(name, value):
296 return _create_parameter(name, value, model_cls=models.Output)
300 return dict(((item.name, item),))
303 def get_standard_interface_template(service_template):
304 the_type = service_template.interface_types.get_descendant('test_interface_type')
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)
312 return models.InterfaceTemplate(name=NORMATIVE_STANDARD_INTERFACE,
313 operation_templates=op_templates,
317 def get_standard_interface(service):
318 the_type = service.service_template.interface_types.get_descendant('test_interface_type')
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)
327 NORMATIVE_STANDARD_INTERFACE:
328 models.Interface(name=NORMATIVE_STANDARD_INTERFACE, operations=ops, type=the_type)
332 def get_configure_interfaces(service):
333 the_type = service.service_template.interface_types.get_descendant('test_interface_type')
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,
343 NORMATIVE_PRE_CONFIGURE_TARGET,
344 NORMATIVE_POST_CONFIGURE_TARGET,
345 NORMATIVE_ADD_TARGET,
346 NORMATIVE_REMOVE_TARGET)
349 NORMATIVE_CONFIGURE_INTERFACE: models.Interface(
350 name=NORMATIVE_CONFIGURE_INTERFACE, operations=operations, type=the_type)
356 @decorators.operation
357 def mock_operation(*args, **kwargs):