Update tosca lib to version 0.5
[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 description(self):
56         return self.group_description
57
58     @property
59     def version(self):
60         return self.group_version
61
62     @property
63     def interfaces(self):
64         return self.get_value(self.INTERFACES)
65
66     def _validate_fields(self):
67         if self.defs:
68             for name in self.defs.keys():
69                 if name not in self.SECTIONS:
70                     ExceptionCollector.appendException(
71                         UnknownFieldError(what='Group Type %s'
72                                           % self.grouptype, field=name))
73
74     def _validate_metadata(self, meta_data):
75         if not meta_data.get('type') in ['map', 'tosca:map']:
76             ExceptionCollector.appendException(
77                 InvalidTypeError(what='"%s" defined in group for '
78                                  'metadata' % (meta_data.get('type'))))
79         for entry_schema, entry_schema_type in meta_data.items():
80             if isinstance(entry_schema_type, dict) and not \
81                 entry_schema_type.get('type') == 'string':
82                 ExceptionCollector.appendException(
83                     InvalidTypeError(what='"%s" defined in group for '
84                                      'metadata "%s"'
85                                      % (entry_schema_type.get('type'),
86                                         entry_schema)))