4a1a4d154ac627d3ad1003488ccc57c493216ef8
[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
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 from org.openecomp.sdc.toscaparser.jython.elements import JyGroupType
19
20 class GroupType(StatefulEntityType, JyGroupType):
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         
55     def getJyVersion(self):
56         return self.version       
57      
58     def getJyDescription(self):
59         return self.description     
60     
61     @property
62     def parent_type(self):
63         '''Return a group statefulentity of this entity is derived from.'''
64         if not hasattr(self, 'defs'):
65             return None
66         pgroup_entity = self.derived_from(self.defs)
67         if pgroup_entity:
68             return GroupType(pgroup_entity, self.custom_def)
69
70     @property
71     def description(self):
72         return self.group_description
73
74     @property
75     def version(self):
76         return self.group_version
77
78     @property
79     def interfaces(self):
80         return self.get_value(self.INTERFACES)
81
82     def _validate_fields(self):
83         if self.defs:
84             for name in self.defs.keys():
85                 if name not in self.SECTIONS:
86                     ExceptionCollector.appendException(
87                         UnknownFieldError(what='Group Type %s'
88                                           % self.grouptype, field=name))
89
90     def _validate_metadata(self, meta_data):
91         if not meta_data.get('type') in ['map', 'tosca:map']:
92             ExceptionCollector.appendException(
93                 InvalidTypeError(what='"%s" defined in group for '
94                                  'metadata' % (meta_data.get('type'))))
95         for entry_schema, entry_schema_type in meta_data.items():
96             if isinstance(entry_schema_type, dict) and not \
97                     entry_schema_type.get('type') == 'string':
98                 ExceptionCollector.appendException(
99                     InvalidTypeError(what='"%s" defined in group for '
100                                      'metadata "%s"'
101                                      % (entry_schema_type.get('type'),
102                                         entry_schema)))