docs: fix issues
[parser.git] / tosca2heat / tosca-parser-0.3.0 / toscaparser / relationship_template.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
14 import logging
15
16 from toscaparser.entity_template import EntityTemplate
17 from toscaparser.properties import Property
18
19 SECTIONS = (DERIVED_FROM, PROPERTIES, REQUIREMENTS,
20             INTERFACES, CAPABILITIES, TYPE) = \
21            ('derived_from', 'properties', 'requirements', 'interfaces',
22             'capabilities', 'type')
23
24 log = logging.getLogger('tosca')
25
26
27 class RelationshipTemplate(EntityTemplate):
28     '''Relationship template.'''
29     def __init__(self, relationship_template, name, custom_def=None):
30         super(RelationshipTemplate, self).__init__(name,
31                                                    relationship_template,
32                                                    'relationship_type',
33                                                    custom_def)
34         self.name = name.lower()
35
36     def get_properties_objects(self):
37         '''Return properties objects for this template.'''
38         if self._properties is None:
39             self._properties = self._create_relationship_properties()
40         return self._properties
41
42     def _create_relationship_properties(self):
43         props = []
44         properties = {}
45         relationship = self.entity_tpl.get('relationship')
46         if relationship:
47             properties = self.type_definition.get_value(self.PROPERTIES,
48                                                         relationship) or {}
49         if not properties:
50             properties = self.entity_tpl.get(self.PROPERTIES) or {}
51
52         if properties:
53             for name, value in properties.items():
54                 props_def = self.type_definition.get_properties_def()
55                 if props_def and name in props_def:
56                     if name in properties.keys():
57                         value = properties.get(name)
58                     prop = Property(name, value,
59                                     props_def[name].schema, self.custom_def)
60                     props.append(prop)
61         for p in self.type_definition.get_properties_def_objects():
62             if p.default is not None and p.name not in properties.keys():
63                 prop = Property(p.name, p.default, p.schema, self.custom_def)
64                 props.append(prop)
65         return props
66
67     def validate(self):
68         self._validate_properties(self.entity_tpl, self.type_definition)