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