Support python3 uploaded to pypi websit
[parser.git] / tosca2heat / tosca-parser / toscaparser / elements / relationshiptype.py
1 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
2 #    not use this file except in compliance with the License. You may obtain
3 #    a copy of the License at
4 #
5 #         http://www.apache.org/licenses/LICENSE-2.0
6 #
7 #    Unless required by applicable law or agreed to in writing, software
8 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 #    License for the specific language governing permissions and limitations
11 #    under the License.
12
13 from toscaparser.common.exception import ExceptionCollector
14 from toscaparser.common.exception import UnknownFieldError
15 from toscaparser.elements.statefulentitytype import StatefulEntityType
16
17
18 class RelationshipType(StatefulEntityType):
19     '''TOSCA built-in relationship type.'''
20     SECTIONS = (DERIVED_FROM, VALID_TARGET_TYPES, INTERFACES,
21                 ATTRIBUTES, PROPERTIES, DESCRIPTION, VERSION,
22                 CREDENTIAL) = ('derived_from', 'valid_target_types',
23                                'interfaces', 'attributes', 'properties',
24                                'description', 'version', 'credential')
25
26     def __init__(self, type, capability_name=None, custom_def=None):
27         super(RelationshipType, self).__init__(type, self.RELATIONSHIP_PREFIX,
28                                                custom_def)
29         self.capability_name = capability_name
30         self.custom_def = custom_def
31         self._validate_keys()
32
33     @property
34     def parent_type(self):
35         '''Return a relationship this reletionship is derived from.'''
36         prel = self.derived_from(self.defs)
37         if prel:
38             return RelationshipType(prel, self.custom_def)
39
40     @property
41     def valid_target_types(self):
42         return self.entity_value(self.defs, 'valid_target_types')
43
44     def _validate_keys(self):
45         for key in self.defs.keys():
46             if key not in self.SECTIONS:
47                 ExceptionCollector.appendException(
48                     UnknownFieldError(what='Relationshiptype "%s"' % self.type,
49                                       field=key))