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