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.caching import cachedmethod
17 from aria.utils.console import puts
18 from aria.utils.formatting import as_raw
19 from aria.parser import implements_specification
20 from aria.parser.presentation import (AsIsPresentation, has_fields, allow_unknown_fields,
21 short_form_field, primitive_field, primitive_list_field,
22 primitive_dict_unknown_fields, object_field,
23 object_list_field, object_dict_field, field_validator,
26 from .modeling.data_types import (get_data_type, get_data_type_value, get_property_constraints,
27 apply_constraint_to_value)
28 from .modeling.substitution_mappings import (validate_substitution_mappings_requirement,
29 validate_substitution_mappings_capability)
30 from .presentation.extensible import ExtensiblePresentation
31 from .presentation.field_validators import (constraint_clause_field_validator,
32 constraint_clause_in_range_validator,
33 constraint_clause_valid_values_validator,
34 constraint_clause_pattern_validator,
36 from .presentation.types import (convert_name_to_full_type_name, get_type_by_name)
40 @implements_specification('3.5.1', 'tosca-simple-1.0')
41 class Description(AsIsPresentation):
43 Human-readable description.
45 See the `TOSCA Simple Profile v1.0 cos01 specification <http://docs.oasis-open.org/tosca
46 /TOSCA-Simple-Profile-YAML/v1.0/cos01/TOSCA-Simple-Profile-YAML-v1.0-cos01.html
47 #DEFN_ELEMENT_DESCRIPTION>`__
50 def __init__(self, name=None, raw=None, container=None, cls=None): # pylint: disable=unused-argument
51 super(Description, self).__init__(name, raw, container, cls=unicode)
53 def _dump(self, context):
54 value = as_raw(self.value)
55 puts(context.style.meta_style(value))
60 @implements_specification('3.9.3.2', 'tosca-simple-1.0')
61 class MetaData(ExtensiblePresentation):
67 @implements_specification('3.9.3.3', 'tosca-simple-1.0')
68 def template_name(self):
70 This optional metadata keyname can be used to declare the name of service template as a
71 single-line string value.
75 @implements_specification('3.9.3.4', 'tosca-simple-1.0')
76 def template_author(self):
78 This optional metadata keyname can be used to declare the author(s) of the service template
79 as a single-line string value.
83 @implements_specification('3.9.3.5', 'tosca-simple-1.0')
84 def template_version(self):
86 This optional metadata keyname can be used to declare a domain specific version of the
87 service template as a single-line string value.
90 @primitive_dict_unknown_fields()
97 @short_form_field('url')
99 @implements_specification('3.5.5', 'tosca-simple-1.0')
100 class Repository(ExtensiblePresentation):
102 A repository definition defines a named external repository which contains deployment and
103 implementation artifacts that are referenced within the TOSCA Service Template.
105 See the `TOSCA Simple Profile v1.0 cos01 specification <http://docs.oasis-open.org/tosca
106 /TOSCA-Simple-Profile-YAML/v1.0/cos01/TOSCA-Simple-Profile-YAML-v1.0-cos01.html
107 #DEFN_ELEMENT_REPOSITORY_DEF>`__
110 @object_field(Description)
111 def description(self):
113 The optional description for the repository.
115 :type: :class:`Description`
118 @primitive_field(str, required=True)
121 The required URL or network address used to access the repository.
123 :type: :obj:`basestring`
127 def credential(self):
129 The optional Credential used to authorize access to the repository.
131 :type: tosca.datatypes.Credential
135 def _get_credential(self, context):
136 return get_data_type_value(context, self, 'credential', 'tosca.datatypes.Credential')
139 @short_form_field('file')
141 @implements_specification('3.5.7', 'tosca-simple-1.0')
142 class Import(ExtensiblePresentation):
144 An import definition is used within a TOSCA Service Template to locate and uniquely name another
145 TOSCA Service Template file which has type and template definitions to be imported (included)
146 and referenced within another Service Template.
148 See the `TOSCA Simple Profile v1.0 cos01 specification <http://docs.oasis-open.org/tosca
149 /TOSCA-Simple-Profile-YAML/v1.0/cos01/TOSCA-Simple-Profile-YAML-v1.0-cos01.html
150 #DEFN_ELEMENT_IMPORT_DEF>`__
153 @primitive_field(str, required=True)
156 The required symbolic name for the imported file.
158 :type: :obj:`basestring`
161 @primitive_field(str)
162 def repository(self):
164 The optional symbolic name of the repository definition where the imported file can be found
167 :type: :obj:`basestring`
170 @primitive_field(str)
171 def namespace_uri(self):
173 The optional namespace URI to that will be applied to type definitions found within the
174 imported file as a string.
176 :type: :obj:`basestring`
179 @primitive_field(str)
180 def namespace_prefix(self):
182 The optional namespace prefix (alias) that will be used to indicate the namespace_uri when
183 forming a qualified name (i.e., qname) when referencing type definitions from the imported
186 :type: :obj:`basestring`
191 @implements_specification('3.5.2-1', 'tosca-simple-1.0')
192 class ConstraintClause(ExtensiblePresentation):
194 A constraint clause defines an operation along with one or more compatible values that can be
195 used to define a constraint on a property or parameter's allowed values when it is defined in a
196 TOSCA Service Template or one of its entities.
198 See the `TOSCA Simple Profile v1.0 cos01 specification <http://docs.oasis-open.org/tosca
199 /TOSCA-Simple-Profile-YAML/v1.0/cos01/TOSCA-Simple-Profile-YAML-v1.0-cos01.html
200 #DEFN_ELEMENT_CONSTRAINTS_CLAUSE>`__
203 @field_validator(constraint_clause_field_validator)
207 Constrains a property or parameter to a value equal to ('=') the value declared.
210 @field_validator(constraint_clause_field_validator)
212 def greater_than(self):
214 Constrains a property or parameter to a value greater than ('>') the value declared.
217 @field_validator(constraint_clause_field_validator)
219 def greater_or_equal(self):
221 Constrains a property or parameter to a value greater than or equal to ('>=') the value
225 @field_validator(constraint_clause_field_validator)
229 Constrains a property or parameter to a value less than ('<') the value declared.
232 @field_validator(constraint_clause_field_validator)
234 def less_or_equal(self):
236 Constrains a property or parameter to a value less than or equal to ('<=') the value
240 @field_validator(constraint_clause_in_range_validator)
241 @primitive_list_field()
244 Constrains a property or parameter to a value in range of (inclusive) the two values
247 Note: subclasses or templates of types that declare a property with the ``in_range``
248 constraint MAY only further restrict the range specified by the parent type.
251 @field_validator(constraint_clause_valid_values_validator)
252 @primitive_list_field()
253 def valid_values(self):
255 Constrains a property or parameter to a value that is in the list of declared values.
258 @primitive_field(int)
261 Constrains the property or parameter to a value of a given length.
264 @primitive_field(int)
265 def min_length(self):
267 Constrains the property or parameter to a value to a minimum length.
270 @primitive_field(int)
271 def max_length(self):
273 Constrains the property or parameter to a value to a maximum length.
276 @field_validator(constraint_clause_pattern_validator)
277 @primitive_field(str)
280 Constrains the property or parameter to a value that is allowed by the provided regular
283 Note: Future drafts of this specification will detail the use of regular expressions and
284 reference an appropriate standardized grammar.
288 def _get_type(self, context):
289 if hasattr(self._container, '_get_type_for_name'):
290 # NodeFilter or CapabilityFilter
291 return self._container._get_type_for_name(context, self._name)
292 elif hasattr(self._container, '_get_type'):
294 return self._container._get_type(context)
296 # DataType (the DataType itself is our type)
297 return self._container
299 def _apply_to_value(self, context, presentation, value):
300 return apply_constraint_to_value(context, presentation, self, value)
303 @short_form_field('type')
305 class EntrySchema(ExtensiblePresentation):
307 ARIA NOTE: The specification does not properly explain this type, however it is implied by
311 @field_validator(data_type_validator('entry schema data type'))
312 @primitive_field(str, required=True)
315 :type: :obj:`basestring`
318 @object_field(Description)
319 def description(self):
321 :type: :class:`Description`
324 @object_list_field(ConstraintClause)
325 def constraints(self):
327 :type: list of (str, :class:`ConstraintClause`)
331 def _get_type(self, context):
332 return get_data_type(context, self, 'type')
335 def _get_constraints(self, context):
336 return get_property_constraints(context, self)
339 @short_form_field('primary')
341 class OperationImplementation(ExtensiblePresentation):
343 Operation implementation.
346 @primitive_field(str)
349 The optional implementation artifact name (i.e., the primary script file name within a
352 :type: :obj:`basestring`
355 @primitive_list_field(str)
356 def dependencies(self):
358 The optional ordered list of one or more dependent or secondary implementation artifact name
359 which are referenced by the primary implementation artifact (e.g., a library the script
360 installs or a secondary script).
362 :type: [:obj:`basestring`]
366 class SubstitutionMappingsRequirement(AsIsPresentation):
368 Substitution mapping for requirement.
373 def node_template(self):
374 return str(self._raw[0])
378 def requirement(self):
379 return str(self._raw[1])
381 def _validate(self, context):
382 super(SubstitutionMappingsRequirement, self)._validate(context)
383 validate_substitution_mappings_requirement(context, self)
386 class SubstitutionMappingsCapability(AsIsPresentation):
388 Substitution mapping for capability.
393 def node_template(self):
394 return str(self._raw[0])
398 def capability(self):
399 return str(self._raw[1])
401 def _validate(self, context):
402 super(SubstitutionMappingsCapability, self)._validate(context)
403 validate_substitution_mappings_capability(context, self)
407 @implements_specification('2.10', 'tosca-simple-1.0')
408 class SubstitutionMappings(ExtensiblePresentation):
410 Substitution mappings.
413 @field_validator(type_validator('node type', convert_name_to_full_type_name, 'node_types'))
414 @primitive_field(str, required=True)
417 :type: :obj:`basestring`
420 @object_dict_field(SubstitutionMappingsRequirement)
421 def requirements(self):
423 :type: {:obj:`basestring`: :class:`SubstitutionMappingsRequirement`}
426 @object_dict_field(SubstitutionMappingsCapability)
427 def capabilities(self):
429 :type: {:obj:`basestring`: :class:`SubstitutionMappingsCapability`}
433 def _get_type(self, context):
434 return get_type_by_name(context, self.node_type, 'node_types')
436 def _validate(self, context):
437 super(SubstitutionMappings, self)._validate(context)
438 self._get_type(context)
440 def _dump(self, context):
441 self._dump_content(context, (