aboutsummaryrefslogtreecommitdiffstats
path: root/jtosca/src/main/java/org/openecomp/sdc/toscaparser/elements/GroupType.java
blob: 41dcd341bb9aa108aa5d5ef48a217884d1e65da8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package org.openecomp.sdc.toscaparser.elements;

import java.util.LinkedHashMap;

import org.openecomp.sdc.toscaparser.common.ExceptionCollector;

public class GroupType extends StatefulEntityType {

	private static final String DERIVED_FROM = "derived_from";
	private static final String VERSION = "version";
	private static final String METADATA = "metadata";
	private static final String DESCRIPTION = "description";
	private static final String PROPERTIES = "properties";
	private static final String MEMBERS = "members";
	private static final String INTERFACES = "interfaces";

	private static final String SECTIONS[] = {
			DERIVED_FROM, VERSION, METADATA, DESCRIPTION, PROPERTIES, MEMBERS, INTERFACES};
	
	private String groupType;
	private LinkedHashMap<String,Object> customDef;
	private String groupDescription;
	private String groupVersion;
	//private LinkedHashMap<String,Object> groupProperties;
	//private ArrayList<String> groupMembers;
	private LinkedHashMap<String,Object> metaData;

	@SuppressWarnings("unchecked")
	public GroupType(String _grouptype,LinkedHashMap<String,Object> _customDef) {
		super(_grouptype,GROUP_PREFIX,_customDef);
		
		groupType = _grouptype;
		customDef = _customDef;
		_validateFields();
		if(defs != null) {
			groupDescription = (String)defs.get(DESCRIPTION);
			groupVersion = (String)defs.get(VERSION);
			//groupProperties = (LinkedHashMap<String,Object>)defs.get(PROPERTIES);
			//groupMembers = (ArrayList<String>)defs.get(MEMBERS);
			Object mdo = defs.get(METADATA);
			if(mdo instanceof LinkedHashMap) {
				metaData = (LinkedHashMap<String,Object>)mdo;	
			}
			else {
				metaData = null;
			}
			
			if(metaData != null) {
				_validateMetadata(metaData);
			}
		}
	}
		
	public GroupType getParentType() {
        // Return a group statefulentity of this entity is derived from.
        if(defs == null) {
            return null;
        }
        String pgroupEntity = derivedFrom(defs);
        if(pgroupEntity != null) {
            return new GroupType(pgroupEntity,customDef);
        }
        return null;
	}
	
	public String getDescription() {
		return groupDescription;
	}

	public String getVersion() {
		return groupVersion;
	}
	
	@SuppressWarnings("unchecked")
	public LinkedHashMap<String,Object> getInterfaces() {
		Object ifo = getValue(INTERFACES,null,false);
		if(ifo instanceof LinkedHashMap) {
			return (LinkedHashMap<String, Object>)ifo;
		}
		return new LinkedHashMap<String,Object>();
	}
	
	private void _validateFields() {
		if(defs != null) {
			for(String name: defs.keySet()) {
				boolean bFound = false;
				for(String sect: SECTIONS) {
					if(name.equals(sect)) {
						bFound = true;
						break;
					}
				}
				if(!bFound) {
                    ExceptionCollector.appendException(String.format(
                        "UnknownFieldError: Group Type \"%s\" contains unknown field \"%s\"",
                        groupType,name));
				}
			}
		}
	}
	
	@SuppressWarnings("unchecked")
	private void _validateMetadata(LinkedHashMap<String,Object> metadata) {
		String mtt = (String) metadata.get("type");
		if(mtt != null && !mtt.equals("map") && !mtt.equals("tosca:map")) {
            ExceptionCollector.appendException(String.format(
                "InvalidTypeError: \"%s\" defined in group for metadata is invalid",
                mtt));
		}
		for(String entrySchema: metadata.keySet()) {
			Object estob = metadata.get(entrySchema);
			if(estob instanceof LinkedHashMap) {
				String est = (String)((LinkedHashMap<String,Object>)estob).get("type");
				if(!est.equals("string")) {
	                ExceptionCollector.appendException(String.format(
	                    "InvalidTypeError: \"%s\" defined in group for metadata \"%s\" is invalid",
	                    est,entrySchema));
				}
			}
		}
	}
 
	public String getType() {
		return groupType;
	}


}

/*python

from toscaparser.common.exception import ExceptionCollector
from toscaparser.common.exception import InvalidTypeError
from toscaparser.common.exception import UnknownFieldError
from toscaparser.elements.statefulentitytype import StatefulEntityType


class GroupType(StatefulEntityType):
    '''TOSCA built-in group type.'''

    SECTIONS = (DERIVED_FROM, VERSION, METADATA, DESCRIPTION, PROPERTIES,
                MEMBERS, INTERFACES) = \
               ("derived_from", "version", "metadata", "description",
                "properties", "members", "interfaces")

    def __init__(self, grouptype, custom_def=None):
        super(GroupType, self).__init__(grouptype, self.GROUP_PREFIX,
                                        custom_def)
        self.custom_def = custom_def
        self.grouptype = grouptype
        self._validate_fields()
        self.group_description = None
        if self.DESCRIPTION in self.defs:
            self.group_description = self.defs[self.DESCRIPTION]

        self.group_version = None
        if self.VERSION in self.defs:
            self.group_version = self.defs[self.VERSION]

        self.group_properties = None
        if self.PROPERTIES in self.defs:
            self.group_properties = self.defs[self.PROPERTIES]

        self.group_members = None
        if self.MEMBERS in self.defs:
            self.group_members = self.defs[self.MEMBERS]

        if self.METADATA in self.defs:
            self.meta_data = self.defs[self.METADATA]
            self._validate_metadata(self.meta_data)

    @property
    def parent_type(self):
        '''Return a group statefulentity of this entity is derived from.'''
        if not hasattr(self, 'defs'):
            return None
        pgroup_entity = self.derived_from(self.defs)
        if pgroup_entity:
            return GroupType(pgroup_entity, self.custom_def)

    @property
    def description(self):
        return self.group_description

    @property
    def version(self):
        return self.group_version

    @property
    def interfaces(self):
        return self.get_value(self.INTERFACES)

    def _validate_fields(self):
        if self.defs:
            for name in self.defs.keys():
                if name not in self.SECTIONS:
                    ExceptionCollector.appendException(
                        UnknownFieldError(what='Group Type %s'
                                          % self.grouptype, field=name))

    def _validate_metadata(self, meta_data):
        if not meta_data.get('type') in ['map', 'tosca:map']:
            ExceptionCollector.appendException(
                InvalidTypeError(what='"%s" defined in group for '
                                 'metadata' % (meta_data.get('type'))))
        for entry_schema, entry_schema_type in meta_data.items():
            if isinstance(entry_schema_type, dict) and not \
                    entry_schema_type.get('type') == 'string':
                ExceptionCollector.appendException(
                    InvalidTypeError(what='"%s" defined in group for '
                                     'metadata "%s"'
                                     % (entry_schema_type.get('type'),
                                        entry_schema)))
*/