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
5 # http://www.apache.org/licenses/LICENSE-2.0
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
13 from toscaparser.elements.property_definition import PropertyDef
14 from toscaparser.elements.statefulentitytype import StatefulEntityType
17 class CapabilityTypeDef(StatefulEntityType):
18 '''TOSCA built-in capabilities type.'''
20 def __init__(self, name, ctype, ntype, custom_def=None):
22 super(CapabilityTypeDef, self).__init__(ctype, self.CAPABILITY_PREFIX,
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()
30 def get_properties_def_objects(self):
31 '''Return a list of property definition objects.'''
33 parent_properties = {}
34 if self.parent_capabilities:
35 for type, value in self.parent_capabilities.items():
36 parent_properties[type] = value.get('properties')
38 for prop, schema in self.properties.items():
39 properties.append(PropertyDef(prop, None, schema))
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.items():
46 properties.append(PropertyDef(prop, None, schema))
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()}
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
60 def _get_parent_capabilities(self):
62 parent_cap = self.parent_type
64 while parent_cap != 'tosca.capabilities.Root':
65 capabilities[parent_cap] = self.TOSCA_DEF[parent_cap]
66 parent_cap = capabilities[parent_cap]['derived_from']
70 def parent_type(self):
71 '''Return a capability this capability is derived from.'''
72 return self.derived_from(self.defs)