vFW and vDNS support added to azure-plugin
[multicloud/azure.git] / azure / aria / aria-extension-cloudify / src / aria / extensions / aria_extension_tosca / simple_v1_0 / misc.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 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,
24                                       type_validator)
25
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,
35                                             data_type_validator)
36 from .presentation.types import (convert_name_to_full_type_name, get_type_by_name)
37
38
39
40 @implements_specification('3.5.1', 'tosca-simple-1.0')
41 class Description(AsIsPresentation):
42     """
43     Human-readable description.
44
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>`__
48     """
49
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)
52
53     def _dump(self, context):
54         value = as_raw(self.value)
55         puts(context.style.meta_style(value))
56
57
58 @allow_unknown_fields
59 @has_fields
60 @implements_specification('3.9.3.2', 'tosca-simple-1.0')
61 class MetaData(ExtensiblePresentation):
62     """
63     Meta data.
64     """
65
66     @primitive_field(str)
67     @implements_specification('3.9.3.3', 'tosca-simple-1.0')
68     def template_name(self):
69         """
70         This optional metadata keyname can be used to declare the name of service template as a
71         single-line string value.
72         """
73
74     @primitive_field(str)
75     @implements_specification('3.9.3.4', 'tosca-simple-1.0')
76     def template_author(self):
77         """
78         This optional metadata keyname can be used to declare the author(s) of the service template
79         as a single-line string value.
80         """
81
82     @primitive_field(str)
83     @implements_specification('3.9.3.5', 'tosca-simple-1.0')
84     def template_version(self):
85         """
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.
88         """
89
90     @primitive_dict_unknown_fields()
91     def custom(self):
92         """
93         :type: dict
94         """
95
96
97 @short_form_field('url')
98 @has_fields
99 @implements_specification('3.5.5', 'tosca-simple-1.0')
100 class Repository(ExtensiblePresentation):
101     """
102     A repository definition defines a named external repository which contains deployment and
103     implementation artifacts that are referenced within the TOSCA Service Template.
104
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>`__
108     """
109
110     @object_field(Description)
111     def description(self):
112         """
113         The optional description for the repository.
114
115         :type: :class:`Description`
116         """
117
118     @primitive_field(str, required=True)
119     def url(self):
120         """
121         The required URL or network address used to access the repository.
122
123         :type: :obj:`basestring`
124         """
125
126     @primitive_field()
127     def credential(self):
128         """
129         The optional Credential used to authorize access to the repository.
130
131         :type: tosca.datatypes.Credential
132         """
133
134     @cachedmethod
135     def _get_credential(self, context):
136         return get_data_type_value(context, self, 'credential', 'tosca.datatypes.Credential')
137
138
139 @short_form_field('file')
140 @has_fields
141 @implements_specification('3.5.7', 'tosca-simple-1.0')
142 class Import(ExtensiblePresentation):
143     """
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.
147
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>`__
151     """
152
153     @primitive_field(str, required=True)
154     def file(self):
155         """
156         The required symbolic name for the imported file.
157
158         :type: :obj:`basestring`
159         """
160
161     @primitive_field(str)
162     def repository(self):
163         """
164         The optional symbolic name of the repository definition where the imported file can be found
165         as a string.
166
167         :type: :obj:`basestring`
168         """
169
170     @primitive_field(str)
171     def namespace_uri(self):
172         """
173         The optional namespace URI to that will be applied to type definitions found within the
174         imported file as a string.
175
176         :type: :obj:`basestring`
177         """
178
179     @primitive_field(str)
180     def namespace_prefix(self):
181         """
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
184         file.
185
186         :type: :obj:`basestring`
187         """
188
189
190 @has_fields
191 @implements_specification('3.5.2-1', 'tosca-simple-1.0')
192 class ConstraintClause(ExtensiblePresentation):
193     """
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.
197
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>`__
201     """
202
203     @field_validator(constraint_clause_field_validator)
204     @primitive_field()
205     def equal(self):
206         """
207         Constrains a property or parameter to a value equal to ('=') the value declared.
208         """
209
210     @field_validator(constraint_clause_field_validator)
211     @primitive_field()
212     def greater_than(self):
213         """
214         Constrains a property or parameter to a value greater than ('>') the value declared.
215         """
216
217     @field_validator(constraint_clause_field_validator)
218     @primitive_field()
219     def greater_or_equal(self):
220         """
221         Constrains a property or parameter to a value greater than or equal to ('>=') the value
222         declared.
223         """
224
225     @field_validator(constraint_clause_field_validator)
226     @primitive_field()
227     def less_than(self):
228         """
229         Constrains a property or parameter to a value less than ('<') the value declared.
230         """
231
232     @field_validator(constraint_clause_field_validator)
233     @primitive_field()
234     def less_or_equal(self):
235         """
236         Constrains a property or parameter to a value less than or equal to ('<=') the value
237         declared.
238         """
239
240     @field_validator(constraint_clause_in_range_validator)
241     @primitive_list_field()
242     def in_range(self):
243         """
244         Constrains a property or parameter to a value in range of (inclusive) the two values
245         declared.
246
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.
249         """
250
251     @field_validator(constraint_clause_valid_values_validator)
252     @primitive_list_field()
253     def valid_values(self):
254         """
255         Constrains a property or parameter to a value that is in the list of declared values.
256         """
257
258     @primitive_field(int)
259     def length(self):
260         """
261         Constrains the property or parameter to a value of a given length.
262         """
263
264     @primitive_field(int)
265     def min_length(self):
266         """
267         Constrains the property or parameter to a value to a minimum length.
268         """
269
270     @primitive_field(int)
271     def max_length(self):
272         """
273         Constrains the property or parameter to a value to a maximum length.
274         """
275
276     @field_validator(constraint_clause_pattern_validator)
277     @primitive_field(str)
278     def pattern(self):
279         """
280         Constrains the property or parameter to a value that is allowed by the provided regular
281         expression.
282
283         Note: Future drafts of this specification will detail the use of regular expressions and
284         reference an appropriate standardized grammar.
285         """
286
287     @cachedmethod
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'):
293             # Properties
294             return self._container._get_type(context)
295         else:
296             # DataType (the DataType itself is our type)
297             return self._container
298
299     def _apply_to_value(self, context, presentation, value):
300         return apply_constraint_to_value(context, presentation, self, value)
301
302
303 @short_form_field('type')
304 @has_fields
305 class EntrySchema(ExtensiblePresentation):
306     """
307     ARIA NOTE: The specification does not properly explain this type, however it is implied by
308     examples.
309     """
310
311     @field_validator(data_type_validator('entry schema data type'))
312     @primitive_field(str, required=True)
313     def type(self):
314         """
315         :type: :obj:`basestring`
316         """
317
318     @object_field(Description)
319     def description(self):
320         """
321         :type: :class:`Description`
322         """
323
324     @object_list_field(ConstraintClause)
325     def constraints(self):
326         """
327         :type: list of (str, :class:`ConstraintClause`)
328         """
329
330     @cachedmethod
331     def _get_type(self, context):
332         return get_data_type(context, self, 'type')
333
334     @cachedmethod
335     def _get_constraints(self, context):
336         return get_property_constraints(context, self)
337
338
339 @short_form_field('primary')
340 @has_fields
341 class OperationImplementation(ExtensiblePresentation):
342     """
343     Operation implementation.
344     """
345
346     @primitive_field(str)
347     def primary(self):
348         """
349         The optional implementation artifact name (i.e., the primary script file name within a
350         TOSCA CSAR file).
351
352         :type: :obj:`basestring`
353         """
354
355     @primitive_list_field(str)
356     def dependencies(self):
357         """
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).
361
362         :type: [:obj:`basestring`]
363         """
364
365
366 class SubstitutionMappingsRequirement(AsIsPresentation):
367     """
368     Substitution mapping for requirement.
369     """
370
371     @property
372     @cachedmethod
373     def node_template(self):
374         return str(self._raw[0])
375
376     @property
377     @cachedmethod
378     def requirement(self):
379         return str(self._raw[1])
380
381     def _validate(self, context):
382         super(SubstitutionMappingsRequirement, self)._validate(context)
383         validate_substitution_mappings_requirement(context, self)
384
385
386 class SubstitutionMappingsCapability(AsIsPresentation):
387     """
388     Substitution mapping for capability.
389     """
390
391     @property
392     @cachedmethod
393     def node_template(self):
394         return str(self._raw[0])
395
396     @property
397     @cachedmethod
398     def capability(self):
399         return str(self._raw[1])
400
401     def _validate(self, context):
402         super(SubstitutionMappingsCapability, self)._validate(context)
403         validate_substitution_mappings_capability(context, self)
404
405
406 @has_fields
407 @implements_specification('2.10', 'tosca-simple-1.0')
408 class SubstitutionMappings(ExtensiblePresentation):
409     """
410     Substitution mappings.
411     """
412
413     @field_validator(type_validator('node type', convert_name_to_full_type_name, 'node_types'))
414     @primitive_field(str, required=True)
415     def node_type(self):
416         """
417         :type: :obj:`basestring`
418         """
419
420     @object_dict_field(SubstitutionMappingsRequirement)
421     def requirements(self):
422         """
423         :type: {:obj:`basestring`: :class:`SubstitutionMappingsRequirement`}
424         """
425
426     @object_dict_field(SubstitutionMappingsCapability)
427     def capabilities(self):
428         """
429         :type: {:obj:`basestring`: :class:`SubstitutionMappingsCapability`}
430         """
431
432     @cachedmethod
433     def _get_type(self, context):
434         return get_type_by_name(context, self.node_type, 'node_types')
435
436     def _validate(self, context):
437         super(SubstitutionMappings, self)._validate(context)
438         self._get_type(context)
439
440     def _dump(self, context):
441         self._dump_content(context, (
442             'node_type',
443             'requirements',
444             'capabilities'))