Merge "Update tosca lib to version 0.5"
[parser.git] / tosca2heat / tosca-parser / toscaparser / elements / statefulentitytype.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 InvalidTypeError
15 from toscaparser.elements.attribute_definition import AttributeDef
16 from toscaparser.elements.entity_type import EntityType
17 from toscaparser.elements.property_definition import PropertyDef
18
19
20 class StatefulEntityType(EntityType):
21     '''Class representing TOSCA states.'''
22
23     interfaces_node_lifecycle_operations = ['create',
24                                             'configure', 'start',
25                                             'stop', 'delete']
26
27     interfaces_relationship_configure_operations = ['post_configure_source',
28                                                     'post_configure_target',
29                                                     'add_target',
30                                                     'remove_target']
31
32     def __init__(self, entitytype, prefix, custom_def=None):
33         entire_entitytype = entitytype
34         if not entitytype.startswith(self.TOSCA):
35             entire_entitytype = prefix + entitytype
36         if entire_entitytype in list(self.TOSCA_DEF.keys()):
37             self.defs = self.TOSCA_DEF[entire_entitytype]
38             entitytype = entire_entitytype
39         elif custom_def and entitytype in list(custom_def.keys()):
40             self.defs = custom_def[entitytype]
41         else:
42             self.defs = None
43             ExceptionCollector.appendException(
44                 InvalidTypeError(what=entitytype))
45         self.type = entitytype
46
47     def get_properties_def_objects(self):
48         '''Return a list of property definition objects.'''
49         properties = []
50         props = self.get_definition(self.PROPERTIES)
51         if props:
52             for prop, schema in props.items():
53                 properties.append(PropertyDef(prop, None, schema))
54         return properties
55
56     def get_properties_def(self):
57         '''Return a dictionary of property definition name-object pairs.'''
58         return {prop.name: prop
59                 for prop in self.get_properties_def_objects()}
60
61     def get_property_def_value(self, name):
62         '''Return the property definition associated with a given name.'''
63         props_def = self.get_properties_def()
64         if props_def and name in props_def.keys():
65             return props_def[name].value
66
67     def get_attributes_def_objects(self):
68         '''Return a list of attribute definition objects.'''
69         attrs = self.get_value(self.ATTRIBUTES, parent=True)
70         if attrs:
71             return [AttributeDef(attr, None, schema)
72                     for attr, schema in attrs.items()]
73         return []
74
75     def get_attributes_def(self):
76         '''Return a dictionary of attribute definition name-object pairs.'''
77         return {attr.name: attr
78                 for attr in self.get_attributes_def_objects()}
79
80     def get_attribute_def_value(self, name):
81         '''Return the attribute definition associated with a given name.'''
82         attrs_def = self.get_attributes_def()
83         if attrs_def and name in attrs_def.keys():
84             return attrs_def[name].value