Merge "Incorrect inheritance of requirements in NodeType"
[parser.git] / tosca2heat / tosca-parser / toscaparser / elements / capabilitytype.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.elements.property_definition import PropertyDef
14 from toscaparser.elements.statefulentitytype import StatefulEntityType
15
16
17 class CapabilityTypeDef(StatefulEntityType):
18     '''TOSCA built-in capabilities type.'''
19
20     def __init__(self, name, ctype, ntype, custom_def=None):
21         self.name = name
22         super(CapabilityTypeDef, self).__init__(ctype, self.CAPABILITY_PREFIX,
23                                                 custom_def)
24         self.nodetype = ntype
25         self.properties = None
26         if self.PROPERTIES in self.defs:
27             self.properties = self.defs[self.PROPERTIES]
28         self.parent_capabilities = self._get_parent_capabilities(custom_def)
29
30     def get_properties_def_objects(self):
31         '''Return a list of property definition objects.'''
32         properties = []
33         parent_properties = {}
34         if self.parent_capabilities:
35             for type, value in self.parent_capabilities.items():
36                 parent_properties[type] = value.get('properties')
37         if self.properties:
38             for prop, schema in self.properties.items():
39                 properties.append(PropertyDef(prop, None, schema))
40         if parent_properties:
41             for parent, props in parent_properties.items():
42                 for prop, schema in props.items():
43                     # add parent property if not overridden by children type
44                     if not self.properties or \
45                         prop not in self.properties.keys():
46                         properties.append(PropertyDef(prop, None, schema))
47         return properties
48
49     def get_properties_def(self):
50         '''Return a dictionary of property definition name-object pairs.'''
51         return {prop.name: prop
52                 for prop in self.get_properties_def_objects()}
53
54     def get_property_def_value(self, name):
55         '''Return the definition of a given property name.'''
56         props_def = self.get_properties_def()
57         if props_def and name in props_def:
58             return props_def[name].value
59
60     def _get_parent_capabilities(self, custom_def=None):
61         capabilities = {}
62         parent_cap = self.parent_type
63         if parent_cap:
64             while parent_cap != 'tosca.capabilities.Root':
65                 if parent_cap in self.TOSCA_DEF.keys():
66                     capabilities[parent_cap] = self.TOSCA_DEF[parent_cap]
67                 elif custom_def and parent_cap in custom_def.keys():
68                     capabilities[parent_cap] = custom_def[parent_cap]
69                 parent_cap = capabilities[parent_cap]['derived_from']
70         return capabilities
71
72     @property
73     def parent_type(self):
74         '''Return a capability this capability is derived from.'''
75         return self.derived_from(self.defs)