Merge "Add verigraph code base"
[parser.git] / tosca2heat / tosca-parser / 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                  target=None, source=None):
31         super(RelationshipTemplate, self).__init__(name,
32                                                    relationship_template,
33                                                    'relationship_type',
34                                                    custom_def)
35         self.name = name.lower()
36         self.target = target
37         self.source = source
38
39     def get_properties_objects(self):
40         '''Return properties objects for this template.'''
41         if self._properties is None:
42             self._properties = self._create_relationship_properties()
43         return self._properties
44
45     def _create_relationship_properties(self):
46         props = []
47         properties = {}
48         relationship = self.entity_tpl.get('relationship')
49
50         if not relationship:
51             for value in self.entity_tpl.values():
52                 if isinstance(value, dict):
53                     relationship = value.get('relationship')
54                     break
55
56         if relationship:
57             properties = self.type_definition.get_value(self.PROPERTIES,
58                                                         relationship) or {}
59         if not properties:
60             properties = self.entity_tpl.get(self.PROPERTIES) or {}
61
62         if properties:
63             for name, value in properties.items():
64                 props_def = self.type_definition.get_properties_def()
65                 if props_def and name in props_def:
66                     if name in properties.keys():
67                         value = properties.get(name)
68                     prop = Property(name, value,
69                                     props_def[name].schema, self.custom_def)
70                     props.append(prop)
71         for p in self.type_definition.get_properties_def_objects():
72             if p.default is not None and p.name not in properties.keys():
73                 prop = Property(p.name, p.default, p.schema, self.custom_def)
74                 props.append(prop)
75         return props
76
77     def validate(self):
78         self._validate_properties(self.entity_tpl, self.type_definition)