Fix StatefulEntityType when entitytype is not define
[parser.git] / tosca2heat / tosca-parser / toscaparser / elements / grouptype.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
14 from toscaparser.common.exception import ExceptionCollector
15 from toscaparser.common.exception import InvalidTypeError
16 from toscaparser.common.exception import UnknownFieldError
17 from toscaparser.elements.statefulentitytype import StatefulEntityType
18
19
20 class GroupType(StatefulEntityType):
21     '''TOSCA built-in group type.'''
22
23     SECTIONS = (DERIVED_FROM, VERSION, METADATA, DESCRIPTION, PROPERTIES,
24                 MEMBERS, INTERFACES) = \
25                ("derived_from", "version", "metadata", "description",
26                 "properties", "members", "interfaces")
27
28     def __init__(self, grouptype, custom_def=None):
29         super(GroupType, self).__init__(grouptype, self.GROUP_PREFIX,
30                                         custom_def)
31         self.custom_def = custom_def
32         self.grouptype = grouptype
33         self._validate_fields()
34         self.group_description = None
35         if self.DESCRIPTION in self.defs:
36             self.group_description = self.defs[self.DESCRIPTION]
37
38         self.group_version = None
39         if self.VERSION in self.defs:
40             self.group_version = self.defs[self.VERSION]
41
42         self.group_properties = None
43         if self.PROPERTIES in self.defs:
44             self.group_properties = self.defs[self.PROPERTIES]
45
46         self.group_members = None
47         if self.MEMBERS in self.defs:
48             self.group_members = self.defs[self.MEMBERS]
49
50         if self.METADATA in self.defs:
51             self.meta_data = self.defs[self.METADATA]
52             self._validate_metadata(self.meta_data)
53
54     @property
55     def parent_type(self):
56         '''Return a group statefulentity of this entity is derived from.'''
57         if not hasattr(self, 'defs'):
58             return None
59         pgroup_entity = self.derived_from(self.defs)
60         if pgroup_entity:
61             return GroupType(pgroup_entity, self.custom_def)
62
63     @property
64     def description(self):
65         return self.group_description
66
67     @property
68     def version(self):
69         return self.group_version
70
71     @property
72     def interfaces(self):
73         return self.get_value(self.INTERFACES)
74
75     def _validate_fields(self):
76         if self.defs:
77             for name in self.defs.keys():
78                 if name not in self.SECTIONS:
79                     ExceptionCollector.appendException(
80                         UnknownFieldError(what='Group Type %s'
81                                           % self.grouptype, field=name))
82
83     def _validate_metadata(self, meta_data):
84         if not meta_data.get('type') in ['map', 'tosca:map']:
85             ExceptionCollector.appendException(
86                 InvalidTypeError(what='"%s" defined in group for '
87                                  'metadata' % (meta_data.get('type'))))
88         for entry_schema, entry_schema_type in meta_data.items():
89             if isinstance(entry_schema_type, dict) and not \
90                     entry_schema_type.get('type') == 'string':
91                 ExceptionCollector.appendException(
92                     InvalidTypeError(what='"%s" defined in group for '
93                                      'metadata "%s"'
94                                      % (entry_schema_type.get('type'),
95                                         entry_schema)))