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 aria.utils.collections import (FrozenDict, FrozenList)
17 from aria.utils.caching import cachedmethod
18 from aria.parser import implements_specification
19 from aria.parser.presentation import (has_fields, primitive_field, primitive_list_field,
20 object_field, object_list_field, object_dict_field,
21 object_sequenced_list_field, field_validator,
22 type_validator, list_type_validator)
24 from .assignments import (PropertyAssignment, AttributeAssignment, RequirementAssignment,
25 CapabilityAssignment, InterfaceAssignment, ArtifactAssignment)
26 from .definitions import ParameterDefinition
27 from .filters import NodeFilter
28 from .misc import (Description, MetaData, Repository, Import, SubstitutionMappings)
29 from .modeling.parameters import (get_assigned_and_defined_parameter_values, get_parameter_values)
30 from .modeling.interfaces import get_template_interfaces
31 from .modeling.requirements import get_template_requirements
32 from .modeling.capabilities import get_template_capabilities
33 from .modeling.artifacts import get_inherited_artifact_definitions
34 from .modeling.policies import get_policy_targets
35 from .modeling.copy import get_default_raw_from_copy
36 from .presentation.extensible import ExtensiblePresentation
37 from .presentation.field_validators import copy_validator, policy_targets_validator
38 from .presentation.types import (convert_name_to_full_type_name, get_type_by_name)
39 from .types import (ArtifactType, DataType, CapabilityType, InterfaceType, RelationshipType,
40 NodeType, GroupType, PolicyType)
44 @implements_specification('3.7.3', 'tosca-simple-1.0')
45 class NodeTemplate(ExtensiblePresentation):
47 A Node Template specifies the occurrence of a manageable software component as part of an
48 application's topology model which is defined in a TOSCA Service Template. A Node template is an
49 instance of a specified Node Type and can provide customized properties, constraints or
50 operations which override the defaults provided by its Node Type and its implementations.
52 See the `TOSCA Simple Profile v1.0 cos01 specification <http://docs.oasis-open.org/tosca
53 /TOSCA-Simple-Profile-YAML/v1.0/cos01/TOSCA-Simple-Profile-YAML-v1.0-cos01.html
54 #DEFN_ENTITY_NODE_TEMPLATE>`__
57 @field_validator(type_validator('node type', convert_name_to_full_type_name, 'node_types'))
58 @primitive_field(str, required=True)
61 The required name of the Node Type the Node Template is based upon.
63 :type: :obj:`basestring`
66 @object_field(Description)
67 def description(self):
69 An optional description for the Node Template.
71 :type: :class:`Description`
74 @primitive_list_field(str)
77 An optional list of directive values to provide processing instructions to orchestrators and
80 :type: [:obj:`basestring`]
83 @object_dict_field(PropertyAssignment)
86 An optional list of property value assignments for the Node Template.
88 :type: {:obj:`basestring`: :class:`PropertyAssignment`}
91 @object_dict_field(AttributeAssignment)
94 An optional list of attribute value assignments for the Node Template.
96 :type: {:obj:`basestring`: :class:`AttributeAssignment`}
99 @object_sequenced_list_field(RequirementAssignment)
100 def requirements(self):
102 An optional sequenced list of requirement assignments for the Node Template.
104 :type: list of (str, :class:`RequirementAssignment`)
107 @object_dict_field(CapabilityAssignment)
108 def capabilities(self):
110 An optional list of capability assignments for the Node Template.
112 :type: {:obj:`basestring`: :class:`CapabilityAssignment`}
115 @object_dict_field(InterfaceAssignment)
116 def interfaces(self):
118 An optional list of named interface definitions for the Node Template.
120 :type: {:obj:`basestring`: :class:`InterfaceAssignment`}
123 @object_dict_field(ArtifactAssignment)
126 An optional list of named artifact definitions for the Node Template.
128 :type: {:obj:`basestring`: :class:`ArtifactAssignment`}
131 @object_field(NodeFilter)
132 def node_filter(self):
134 The optional filter definition that TOSCA orchestrators would use to select the correct
135 target node. This keyname is only valid if the directive has the value of "selectable" set.
137 :type: :class:`NodeFilter`
140 @field_validator(copy_validator('node template', 'node_templates'))
141 @primitive_field(str)
144 The optional (symbolic) name of another node template to copy into (all keynames and values)
145 and use as a basis for this node template.
147 :type: :obj:`basestring`
151 def _get_default_raw(self):
152 return get_default_raw_from_copy(self, 'node_templates')
155 def _get_type(self, context):
156 return get_type_by_name(context, self.type, 'node_types')
159 def _get_property_values(self, context):
160 return FrozenDict(get_assigned_and_defined_parameter_values(context, self, 'property'))
163 def _get_attribute_default_values(self, context):
164 return FrozenDict(get_assigned_and_defined_parameter_values(context, self, 'attribute'))
167 def _get_requirements(self, context):
168 return FrozenList(get_template_requirements(context, self))
171 def _get_capabilities(self, context):
172 return FrozenDict(get_template_capabilities(context, self))
175 def _get_interfaces(self, context):
176 return FrozenDict(get_template_interfaces(context, self, 'node template'))
179 def _get_artifacts(self, context):
180 return FrozenDict(get_inherited_artifact_definitions(context, self))
182 def _validate(self, context):
183 super(NodeTemplate, self)._validate(context)
184 self._get_property_values(context)
185 self._get_requirements(context)
186 self._get_capabilities(context)
187 self._get_interfaces(context)
188 self._get_artifacts(context)
190 def _dump(self, context):
191 self._dump_content(context, (
206 @implements_specification('3.7.4', 'tosca-simple-1.0')
207 class RelationshipTemplate(ExtensiblePresentation):
209 A Relationship Template specifies the occurrence of a manageable relationship between node
210 templates as part of an application's topology model that is defined in a TOSCA Service
211 Template. A Relationship template is an instance of a specified Relationship Type and can
212 provide customized properties, constraints or operations which override the defaults provided by
213 its Relationship Type and its implementations.
215 See the `TOSCA Simple Profile v1.0 cos01 specification <http://docs.oasis-open.org/tosca
216 /TOSCA-Simple-Profile-YAML/v1.0/cos01/TOSCA-Simple-Profile-YAML-v1.0-cos01.html
217 #DEFN_ENTITY_RELATIONSHIP_TEMPLATE>`__
220 @field_validator(type_validator('relationship type', convert_name_to_full_type_name,
221 'relationship_types'))
222 @primitive_field(str, required=True)
225 The required name of the Relationship Type the Relationship Template is based upon.
227 :type: :obj:`basestring`
230 @object_field(Description)
231 def description(self):
233 An optional description for the Relationship Template.
235 :type: :class:`Description`
238 @object_dict_field(PropertyAssignment)
239 def properties(self):
241 An optional list of property assignments for the Relationship Template.
243 :type: {:obj:`basestring`: :class:`PropertyAssignment`}
246 @object_dict_field(AttributeAssignment)
247 def attributes(self):
249 An optional list of attribute assignments for the Relationship Template.
251 :type: {:obj:`basestring`: :class:`AttributeAssignment`}
254 @object_dict_field(InterfaceAssignment)
255 def interfaces(self):
257 An optional list of named interface definitions for the Node Template.
259 ARIA NOTE: Spec is wrong here, should be Relationship Template.
261 :type: {:obj:`basestring`: :class:`InterfaceAssignment`}
264 @field_validator(copy_validator('relationship template', 'relationship_templates'))
265 @primitive_field(str)
268 The optional (symbolic) name of another relationship template to copy into (all keynames and
269 values) and use as a basis for this relationship template.
271 :type: :obj:`basestring`
275 def _get_default_raw(self):
276 return get_default_raw_from_copy(self, 'relationship_templates')
279 def _get_type(self, context):
280 return get_type_by_name(context, self.type, 'relationship_types')
283 def _get_property_values(self, context):
284 return FrozenDict(get_assigned_and_defined_parameter_values(context, self, 'property'))
287 def _get_interfaces(self, context):
288 return FrozenDict(get_template_interfaces(context, self, 'relationship template'))
290 def _validate(self, context):
291 super(RelationshipTemplate, self)._validate(context)
292 self._get_property_values(context)
293 self._get_interfaces(context)
295 def _dump(self, context):
296 self._dump_content(context, (
306 @implements_specification('3.7.5', 'tosca-simple-1.0')
307 class GroupTemplate(ExtensiblePresentation):
309 A group definition defines a logical grouping of node templates, typically for management
310 purposes, but is separate from the application's topology template.
312 See the `TOSCA Simple Profile v1.0 cos01 specification <http://docs.oasis-open.org/tosca
313 /TOSCA-Simple-Profile-YAML/v1.0/cos01/TOSCA-Simple-Profile-YAML-v1.0-cos01.html
314 #DEFN_ELEMENT_GROUP_DEF>`__
317 @field_validator(type_validator('group type', convert_name_to_full_type_name,
319 @primitive_field(str, required=True)
322 The required name of the group type the group definition is based upon.
324 :type: :obj:`basestring`
327 @object_field(Description)
328 def description(self):
330 The optional description for the group definition.
332 :type: :class:`Description`
335 @object_dict_field(PropertyAssignment)
336 def properties(self):
338 An optional list of property value assignments for the group definition.
340 :type: {:obj:`basestring`: :class:`PropertyAssignment`}
343 @field_validator(list_type_validator('node template', 'topology_template', 'node_templates'))
344 @primitive_list_field(str)
347 The optional list of one or more node template names that are members of this group
350 :type: [:obj:`basestring`]
353 @object_dict_field(InterfaceAssignment)
354 def interfaces(self):
356 An optional list of named interface definitions for the group definition.
358 :type: {:obj:`basestring`: :class:`InterfaceDefinition`}
362 def _get_type(self, context):
363 return get_type_by_name(context, self.type, 'group_types')
366 def _get_property_values(self, context):
367 return FrozenDict(get_assigned_and_defined_parameter_values(context, self, 'property'))
370 def _get_interfaces(self, context):
371 return FrozenDict(get_template_interfaces(context, self, 'group definition'))
373 def _validate(self, context):
374 super(GroupTemplate, self)._validate(context)
375 self._get_property_values(context)
376 self._get_interfaces(context)
380 @implements_specification('3.7.6', 'tosca-simple-1.0')
381 class PolicyTemplate(ExtensiblePresentation):
383 A policy definition defines a policy that can be associated with a TOSCA topology or top-level
384 entity definition (e.g., group definition, node template, etc.).
386 See the `TOSCA Simple Profile v1.0 cos01 specification <http://docs.oasis-open.org/tosca
387 /TOSCA-Simple-Profile-YAML/v1.0/cos01/TOSCA-Simple-Profile-YAML-v1.0-cos01.html
388 #DEFN_ELEMENT_POLICY_DEF>`__
391 @field_validator(type_validator('policy type', convert_name_to_full_type_name, 'policy_types'))
392 @primitive_field(str, required=True)
395 The required name of the policy type the policy definition is based upon.
397 :type: :obj:`basestring`
400 @object_field(Description)
401 def description(self):
403 The optional description for the policy definition.
405 :type: :class:`Description`
408 @object_dict_field(PropertyAssignment)
409 def properties(self):
411 An optional list of property value assignments for the policy definition.
413 :type: {:obj:`basestring`: :class:`PropertyAssignment`
416 @field_validator(policy_targets_validator)
417 @primitive_list_field(str)
420 An optional list of valid Node Templates or Groups the Policy can be applied to.
422 :type: [:obj:`basestring`]
426 def _get_type(self, context):
427 return get_type_by_name(context, self.type, 'policy_types')
430 def _get_property_values(self, context):
431 return FrozenDict(get_assigned_and_defined_parameter_values(context, self, 'property'))
434 def _get_targets(self, context):
435 node_templates, groups = get_policy_targets(context, self)
436 return FrozenList(node_templates), FrozenList(groups)
438 def _validate(self, context):
439 super(PolicyTemplate, self)._validate(context)
440 self._get_property_values(context)
444 @implements_specification('3.8', 'tosca-simple-1.0')
445 class TopologyTemplate(ExtensiblePresentation):
447 This section defines the topology template of a cloud application. The main ingredients of the
448 topology template are node templates representing components of the application and relationship
449 templates representing links between the components. These elements are defined in the nested
450 ``node_templates`` section and the nested relationship_templates sections, respectively.
451 Furthermore, a topology template allows for defining input parameters, output parameters as well
452 as grouping of node templates.
454 See the `TOSCA Simple Profile v1.0 cos01 specification <http://docs.oasis-open.org/tosca
455 /TOSCA-Simple-Profile-YAML/v1.0/cos01/TOSCA-Simple-Profile-YAML-v1.0-cos01.html
456 #DEFN_ENTITY_TOPOLOGY_TEMPLATE>`__
459 @object_field(Description)
460 def description(self):
462 The optional description for the Topology Template.
464 :type: :class:`Description`
467 @object_dict_field(ParameterDefinition)
470 An optional list of input parameters (i.e., as parameter definitions) for the Topology
473 :type: {:obj:`basestring`: :class:`ParameterDefinition`}
476 @object_dict_field(NodeTemplate)
477 def node_templates(self):
479 An optional list of node template definitions for the Topology Template.
481 :type: {:obj:`basestring`: :class:`NodeTemplate`}
484 @object_dict_field(RelationshipTemplate)
485 def relationship_templates(self):
487 An optional list of relationship templates for the Topology Template.
489 :type: {:obj:`basestring`: :class:`RelationshipTemplate`}
492 @object_dict_field(GroupTemplate)
495 An optional list of Group definitions whose members are node templates defined within this
496 same Topology Template.
498 :class:`GroupTemplate`
501 @object_dict_field(PolicyTemplate)
504 An optional list of Policy definitions for the Topology Template.
506 :type: {:obj:`basestring`: :class:`PolicyTemplate`}
509 @object_dict_field(ParameterDefinition)
512 An optional list of output parameters (i.e., as parameter definitions) for the Topology
515 :type: {:obj:`basestring`: :class:`ParameterDefinition`}
518 @object_field(SubstitutionMappings)
519 def substitution_mappings(self):
521 An optional declaration that exports the topology template as an implementation of a Node
524 This also includes the mappings between the external Node Types named capabilities and
525 requirements to existing implementations of those capabilities and requirements on Node
526 templates declared within the topology template.
530 def _get_input_values(self, context):
531 return FrozenDict(get_parameter_values(context, self, 'inputs'))
534 def _get_output_values(self, context):
535 return FrozenDict(get_parameter_values(context, self, 'outputs'))
537 def _validate(self, context):
538 super(TopologyTemplate, self)._validate(context)
539 self._get_input_values(context)
540 self._get_output_values(context)
542 def _dump(self, context):
543 self._dump_content(context, (
547 'relationship_templates',
551 'substitution_mappings'))
555 @implements_specification('3.9', 'tosca-simple-1.0')
556 class ServiceTemplate(ExtensiblePresentation):
560 See the `TOSCA Simple Profile v1.0 cos01 specification <http://docs.oasis-open.org/tosca
561 /TOSCA-Simple-Profile-YAML/v1.0/cos01/TOSCA-Simple-Profile-YAML-v1.0-cos01.html
562 #DEFN_ELEMENT_SERVICE_TEMPLATE>`__.
565 @primitive_field(str)
566 @implements_specification('3.9.3.1', 'tosca-simple-1.0')
567 def tosca_definitions_version(self):
569 Defines the version of the TOSCA Simple Profile specification the template (grammar)
572 See the `TOSCA Simple Profile v1.0 cos01 specification <http://docs.oasis-open.org/tosca
573 /TOSCA-Simple-Profile-YAML/v1.0/cos01/TOSCA-Simple-Profile-YAML-v1.0-cos01.html
576 :type: :obj:`basestring`
579 @object_field(MetaData)
582 Defines a section used to declare additional metadata information. Domain-specific TOSCA
583 profile specifications may define keynames that are required for their implementations.
585 See the `TOSCA Simple Profile v1.0 cos01 specification <http://docs.oasis-open.org/tosca
586 /TOSCA-Simple-Profile-YAML/v1.0/cos01/TOSCA-Simple-Profile-YAML-v1.0-cos01.html
589 :type: :class:`MetaData`
592 @object_field(Description)
593 @implements_specification('3.9.3.6', 'tosca-simple-1.0')
594 def description(self):
596 Declares a description for this Service Template and its contents.
598 :type: :class:`Description`
602 @implements_specification('3.9.3.7', 'tosca-simple-1.0')
603 def dsl_definitions(self):
605 Declares optional DSL-specific definitions and conventions. For example, in YAML, this
606 allows defining reusable YAML macros (i.e., YAML alias anchors) for use throughout the TOSCA
609 See the `TOSCA Simple Profile v1.0 cos01 specification <http://docs.oasis-open.org/tosca
610 /TOSCA-Simple-Profile-YAML/v1.0/cos01/TOSCA-Simple-Profile-YAML-v1.0-cos01.html
614 @object_dict_field(Repository)
615 @implements_specification('3.9.3.8', 'tosca-simple-1.0')
616 def repositories(self):
618 Declares the list of external repositories which contain artifacts that are referenced in
619 the service template along with their addresses and necessary credential information used to
620 connect to them in order to retrieve the artifacts.
622 :type: {:obj:`basestring`: :class:`Repository`}
625 @object_list_field(Import)
626 @implements_specification('3.9.3.9', 'tosca-simple-1.0')
629 Declares import statements external TOSCA Definitions documents. For example, these may be
630 file location or URIs relative to the service template file within the same TOSCA CSAR file.
632 :type: list of :class:`Import`
635 @object_dict_field(ArtifactType)
636 @implements_specification('3.9.3.10', 'tosca-simple-1.0')
637 def artifact_types(self):
639 This section contains an optional list of artifact type definitions for use in the service
642 :type: {:obj:`basestring`: :class:`ArtifactType`}
645 @object_dict_field(DataType)
646 @implements_specification('3.9.3.11', 'tosca-simple-1.0')
647 def data_types(self):
649 Declares a list of optional TOSCA Data Type definitions.
651 :type: {:obj:`basestring`: :class:`DataType`}
654 @object_dict_field(CapabilityType)
655 @implements_specification('3.9.3.12', 'tosca-simple-1.0')
656 def capability_types(self):
658 This section contains an optional list of capability type definitions for use in the service
661 :type: {:obj:`basestring`: :class:`CapabilityType`}
664 @object_dict_field(InterfaceType)
665 @implements_specification('3.9.3.13', 'tosca-simple-1.0')
666 def interface_types(self):
668 This section contains an optional list of interface type definitions for use in the service
671 :type: {:obj:`basestring`: :class:`InterfaceType`}
674 @object_dict_field(RelationshipType)
675 @implements_specification('3.9.3.14', 'tosca-simple-1.0')
676 def relationship_types(self):
678 This section contains a set of relationship type definitions for use in the service
681 :type: {:obj:`basestring`: :class:`RelationshipType`}
684 @object_dict_field(NodeType)
685 @implements_specification('3.9.3.15', 'tosca-simple-1.0')
686 def node_types(self):
688 This section contains a set of node type definitions for use in the service template.
690 :type: {:obj:`basestring`: :class:`NodeType`}
693 @object_dict_field(GroupType)
694 @implements_specification('3.9.3.16', 'tosca-simple-1.0')
695 def group_types(self):
697 This section contains a list of group type definitions for use in the service template.
699 :type: {:obj:`basestring`: :class:`GroupType`}
702 @object_dict_field(PolicyType)
703 @implements_specification('3.9.3.17', 'tosca-simple-1.0')
704 def policy_types(self):
706 This section contains a list of policy type definitions for use in the service template.
708 :type: {:obj:`basestring`: :class:`PolicyType`}
711 @object_field(TopologyTemplate)
712 def topology_template(self):
714 Defines the topology template of an application or service, consisting of node templates
715 that represent the application's or service's components, as well as relationship templates
716 representing relations between the components.
718 :type: :class:`TopologyTemplate`
721 def _dump(self, context):
722 self._dump_content(context, (
724 'tosca_definitions_version',
732 'relationship_types',
736 'topology_template'))