Merge "The grouptype's parent_type definition is error"
[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         else:
63             return None
64
65     @property
66     def description(self):
67         return self.group_description
68
69     @property
70     def version(self):
71         return self.group_version
72
73     @property
74     def interfaces(self):
75         return self.get_value(self.INTERFACES)
76
77     def _validate_fields(self):
78         if self.defs:
79             for name in self.defs.keys():
80                 if name not in self.SECTIONS:
81                     ExceptionCollector.appendException(
82                         UnknownFieldError(what='Group Type %s'
83                                           % self.grouptype, field=name))
84
85     def _validate_metadata(self, meta_data):
86         if not meta_data.get('type') in ['map', 'tosca:map']:
87             ExceptionCollector.appendException(
88                 InvalidTypeError(what='"%s" defined in group for '
89                                  'metadata' % (meta_data.get('type'))))
90         for entry_schema, entry_schema_type in meta_data.items():
91             if isinstance(entry_schema_type, dict) and not \
92                 entry_schema_type.get('type') == 'string':
93                 ExceptionCollector.appendException(
94                     InvalidTypeError(what='"%s" defined in group for '
95                                      'metadata "%s"'
96                                      % (entry_schema_type.get('type'),
97                                         entry_schema)))