aboutsummaryrefslogtreecommitdiffstats
path: root/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/DataEntity.java
blob: a081f89b63532630389a02c97b75f03397b72fe3 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package org.openecomp.sdc.toscaparser.api;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;

import org.openecomp.sdc.toscaparser.api.common.ExceptionCollector;
import org.openecomp.sdc.toscaparser.api.elements.*;
import org.openecomp.sdc.toscaparser.api.elements.constraints.Constraint;
import org.openecomp.sdc.toscaparser.api.elements.constraints.Schema;
import org.openecomp.sdc.toscaparser.api.functions.Function;
import org.openecomp.sdc.toscaparser.api.utils.TOSCAVersionProperty;
import org.openecomp.sdc.toscaparser.api.utils.ValidateUtils;

public class DataEntity {
    // A complex data value entity
	
	private LinkedHashMap<String,Object> customDef;
	private DataType dataType;
	private LinkedHashMap<String,PropertyDef> schema;
	private Object value;
	private String propertyName;
	
	public DataEntity(String _dataTypeName,Object _valueDict,
					  LinkedHashMap<String,Object> _customDef,String _propName) {
		
        customDef = _customDef;
        dataType = new DataType(_dataTypeName,_customDef);
        schema = dataType.getAllProperties();
        value = _valueDict;
        propertyName = _propName;
	}
	
	@SuppressWarnings("unchecked")
	public Object validate() {
		// Validate the value by the definition of the datatype

        // A datatype can not have both 'type' and 'properties' definitions.
        // If the datatype has 'type' definition
        if(dataType.getValueType() != null) {
            value = DataEntity.validateDatatype(dataType.getValueType(),value,null,customDef,null);
            Schema schemaCls = new Schema(propertyName,dataType.getDefs());
            for(Constraint constraint: schemaCls.getConstraints()) {
                constraint.validate(value);
            }
        }
        // If the datatype has 'properties' definition
        else {
            if(!(value instanceof LinkedHashMap)) {
            	//ERROR under investigation
                ExceptionCollector.appendWarning(String.format(
                    "TypeMismatchError: \"%s\" is not a map. The type is \"%s\"",
                    value.toString(),dataType.getType()));
                
				if (value instanceof List && ((List) value).size() > 0)  {
					value = ((List) value).get(0);
				}

				if (!(value instanceof LinkedHashMap))  {
					return value;
				}
			}



			LinkedHashMap<String,Object> valueDict = (LinkedHashMap<String,Object>)value;
            ArrayList<String> allowedProps = new ArrayList<>();
            ArrayList<String> requiredProps = new ArrayList<>();
            LinkedHashMap<String,Object> defaultProps = new LinkedHashMap<>();
            if(schema != null) {
            	allowedProps.addAll(schema.keySet());
            	for(String name: schema.keySet()) {
            		PropertyDef propDef = schema.get(name);
            		if(propDef.isRequired()) {
            			requiredProps.add(name);
            		}
            		if(propDef.getDefault() != null) {
            			defaultProps.put(name,propDef.getDefault());
            		}
            	}
            }
            
            // check allowed field
            for(String valueKey: valueDict.keySet()) {
            	//1710 devlop JSON validation
            	if(!("json").equals(dataType.getType()) && !allowedProps.contains(valueKey)) {
                    ExceptionCollector.appendException(String.format(
                        "UnknownFieldError: Data value of type \"%s\" contains unknown field \"%s\"",
                        dataType.getType(),valueKey));
            	}
            }

            // check default field
            for(String defKey: defaultProps.keySet()) {
            	Object defValue = defaultProps.get(defKey);
            	if(valueDict.get(defKey) == null) {
            		valueDict.put(defKey, defValue);            		
            	}
            	
            }
            
            // check missing field
            ArrayList<String> missingProp = new ArrayList<>();
            for(String reqKey: requiredProps) {
                if(!valueDict.keySet().contains(reqKey)) {
                    missingProp.add(reqKey);
                }
            }
            if(missingProp.size() > 0) {
                ExceptionCollector.appendWarning(String.format(
                    "MissingRequiredFieldError: Data value of type \"%s\" is missing required field(s) \"%s\"",
                    dataType.getType(),missingProp.toString()));
            }
            
            // check every field
            for(String vname: valueDict.keySet()) {
            	Object vvalue = valueDict.get(vname);
            	LinkedHashMap<String,Object> schemaName = _findSchema(vname);
            	if(schemaName == null) {
            		continue;
            	}
            	Schema propSchema = new Schema(vname,schemaName);
                // check if field value meets type defined
                DataEntity.validateDatatype(propSchema.getType(), 
                							vvalue,
                                            propSchema.getEntrySchema(),
                                            customDef,
                                            null);
            	
                // check if field value meets constraints defined
                if(propSchema.getConstraints() != null) {
                    for(Constraint constraint: propSchema.getConstraints()) {
                        if(vvalue instanceof ArrayList) {
                            for(Object val: (ArrayList<Object>)vvalue) {
                                constraint.validate(val);
                            }
                        }
                        else {
                            constraint.validate(vvalue);
                        }
                    }
                }
            }
        }
        return value;
	}

	private LinkedHashMap<String,Object> _findSchema(String name) {
		if(schema != null && schema.get(name) != null) {
			return schema.get(name).getSchema();
		}
		return null;
	}
	
	public static Object validateDatatype(String type, 
										  Object value, 
										  LinkedHashMap<String,Object> entrySchema, 
										  LinkedHashMap<String,Object> customDef,
										  String propName) {
		// Validate value with given type

        // If type is list or map, validate its entry by entry_schema(if defined)
        // If type is a user-defined complex datatype, custom_def is required.

		if(Function.isFunction(value)) {
			return value;
		}
		else if (type == null)  {
			//NOT ANALYZED
			 ExceptionCollector.appendWarning(String.format(
	                    "MissingType: Type is missing for value \"%s\"",
	                    value.toString()));
			 return value;
		}
		else if(type.equals(Schema.STRING)) {
            return ValidateUtils.validateString(value);
		}
		else if(type.equals(Schema.INTEGER)) {
            return ValidateUtils.validateInteger(value);
		}
		else if(type.equals(Schema.FLOAT)) {
            return ValidateUtils.validateFloat(value);
		}
		else if(type.equals(Schema.NUMBER)) {
            return ValidateUtils.validateNumeric(value);
		}
		else if(type.equals(Schema.BOOLEAN)) {
            return ValidateUtils.validateBoolean(value);
		}
		else if(type.equals(Schema.RANGE)) {
            return ValidateUtils.validateRange(value);
		}
		else if(type.equals(Schema.TIMESTAMP)) {
            ValidateUtils.validateTimestamp(value);
            return value;
		}
		else if(type.equals(Schema.LIST)) {
            ValidateUtils.validateList(value);
            if(entrySchema != null) {
            	DataEntity.validateEntry(value,entrySchema,customDef);
            }
            return value;
		}
		else if(type.equals(Schema.SCALAR_UNIT_SIZE)) {
            return (new ScalarUnitSize(value)).validateScalarUnit();
		}
		else if(type.equals(Schema.SCALAR_UNIT_FREQUENCY)) {
            return (new ScalarUnitFrequency(value)).validateScalarUnit();
		}
		else if(type.equals(Schema.SCALAR_UNIT_TIME)) {
            return (new ScalarUnitTime(value)).validateScalarUnit();
		}
		else if(type.equals(Schema.VERSION)) {
            return (new TOSCAVersionProperty(value)).getVersion();
		}
		else if(type.equals(Schema.MAP)) {
            ValidateUtils.validateMap(value);
            if(entrySchema != null) {
            	DataEntity.validateEntry(value,entrySchema,customDef);
            }
            return value;
		}
		else if(type.equals(Schema.PORTSPEC)) {
            // tODO(TBD) bug 1567063, validate source & target as PortDef type
            // as complex types not just as integers
            PortSpec.validateAdditionalReq(value,propName,customDef);
		}
        else {
            DataEntity data = new DataEntity(type,value,customDef,null);
            return data.validate();
        }
        
		return value;
	}
	
	@SuppressWarnings("unchecked")
	public static Object validateEntry(Object value,
			  						   LinkedHashMap<String,Object> entrySchema,
									   LinkedHashMap<String,Object> customDef) {
		
        // Validate entries for map and list
        Schema schema = new Schema(null,entrySchema);
        Object valueob = value;
        ArrayList<Object> valueList = null;
        if(valueob  instanceof LinkedHashMap) {
            valueList = new ArrayList<Object>(((LinkedHashMap<String,Object>)valueob).values());
        }
        else if(valueob instanceof ArrayList) {
        	valueList = (ArrayList<Object>)valueob;
        }
        if(valueList != null) {
	        for(Object v: valueList) {
	            DataEntity.validateDatatype(schema.getType(),v,schema.getEntrySchema(),customDef,null);
	            if(schema.getConstraints() !=  null) {
	                for(Constraint constraint: schema.getConstraints()) {
	                    constraint.validate(v);
	                }
	            }
	        }
        }
		return value;
	}

	@Override
	public String toString() {
		return "DataEntity{" +
				"customDef=" + customDef +
				", dataType=" + dataType +
				", schema=" + schema +
				", value=" + value +
				", propertyName='" + propertyName + '\'' +
				'}';
	}
}

/*python

from toscaparser.common.exception import ExceptionCollector
from toscaparser.common.exception import MissingRequiredFieldError
from toscaparser.common.exception import TypeMismatchError
from toscaparser.common.exception import UnknownFieldError
from toscaparser.elements.constraints import Schema
from toscaparser.elements.datatype import DataType
from toscaparser.elements.portspectype import PortSpec
from toscaparser.elements.scalarunit import ScalarUnit_Frequency
from toscaparser.elements.scalarunit import ScalarUnit_Size
from toscaparser.elements.scalarunit import ScalarUnit_Time
from toscaparser.utils.gettextutils import _
from toscaparser.utils import validateutils


class DataEntity(object):
    '''A complex data value entity.'''

    def __init__(self, datatypename, value_dict, custom_def=None,
                 prop_name=None):
        self.custom_def = custom_def
        self.datatype = DataType(datatypename, custom_def)
        self.schema = self.datatype.get_all_properties()
        self.value = value_dict
        self.property_name = prop_name

    def validate(self):
        '''Validate the value by the definition of the datatype.'''

        # A datatype can not have both 'type' and 'properties' definitions.
        # If the datatype has 'type' definition
        if self.datatype.value_type:
            self.value = DataEntity.validate_datatype(self.datatype.value_type,
                                                      self.value,
                                                      None,
                                                      self.custom_def)
            schema = Schema(self.property_name, self.datatype.defs)
            for constraint in schema.constraints:
                constraint.validate(self.value)
        # If the datatype has 'properties' definition
        else:
            if not isinstance(self.value, dict):
                ExceptionCollector.appendException(
                    TypeMismatchError(what=self.value,
                                      type=self.datatype.type))
            allowed_props = []
            required_props = []
            default_props = {}
            if self.schema:
                allowed_props = self.schema.keys()
                for name, prop_def in self.schema.items():
                    if prop_def.required:
                        required_props.append(name)
                    if prop_def.default:
                        default_props[name] = prop_def.default

            # check allowed field
            for value_key in list(self.value.keys()):
                if value_key not in allowed_props:
                    ExceptionCollector.appendException(
                        UnknownFieldError(what=(_('Data value of type "%s"')
                                                % self.datatype.type),
                                          field=value_key))

            # check default field
            for def_key, def_value in list(default_props.items()):
                if def_key not in list(self.value.keys()):
                    self.value[def_key] = def_value

            # check missing field
            missingprop = []
            for req_key in required_props:
                if req_key not in list(self.value.keys()):
                    missingprop.append(req_key)
            if missingprop:
                ExceptionCollector.appendException(
                    MissingRequiredFieldError(
                        what=(_('Data value of type "%s"')
                              % self.datatype.type), required=missingprop))

            # check every field
            for name, value in list(self.value.items()):
                schema_name = self._find_schema(name)
                if not schema_name:
                    continue
                prop_schema = Schema(name, schema_name)
                # check if field value meets type defined
                DataEntity.validate_datatype(prop_schema.type, value,
                                             prop_schema.entry_schema,
                                             self.custom_def)
                # check if field value meets constraints defined
                if prop_schema.constraints:
                    for constraint in prop_schema.constraints:
                        if isinstance(value, list):
                            for val in value:
                                constraint.validate(val)
                        else:
                            constraint.validate(value)

        return self.value

    def _find_schema(self, name):
        if self.schema and name in self.schema.keys():
            return self.schema[name].schema

    @staticmethod
    def validate_datatype(type, value, entry_schema=None, custom_def=None,
                          prop_name=None):
        '''Validate value with given type.

        If type is list or map, validate its entry by entry_schema(if defined)
        If type is a user-defined complex datatype, custom_def is required.
        '''
        from toscaparser.functions import is_function
        if is_function(value):
            return value
        if type == Schema.STRING:
            return validateutils.validate_string(value)
        elif type == Schema.INTEGER:
            return validateutils.validate_integer(value)
        elif type == Schema.FLOAT:
            return validateutils.validate_float(value)
        elif type == Schema.NUMBER:
            return validateutils.validate_numeric(value)
        elif type == Schema.BOOLEAN:
            return validateutils.validate_boolean(value)
        elif type == Schema.RANGE:
            return validateutils.validate_range(value)
        elif type == Schema.TIMESTAMP:
            validateutils.validate_timestamp(value)
            return value
        elif type == Schema.LIST:
            validateutils.validate_list(value)
            if entry_schema:
                DataEntity.validate_entry(value, entry_schema, custom_def)
            return value
        elif type == Schema.SCALAR_UNIT_SIZE:
            return ScalarUnit_Size(value).validate_scalar_unit()
        elif type == Schema.SCALAR_UNIT_FREQUENCY:
            return ScalarUnit_Frequency(value).validate_scalar_unit()
        elif type == Schema.SCALAR_UNIT_TIME:
            return ScalarUnit_Time(value).validate_scalar_unit()
        elif type == Schema.VERSION:
            return validateutils.TOSCAVersionProperty(value).get_version()
        elif type == Schema.MAP:
            validateutils.validate_map(value)
            if entry_schema:
                DataEntity.validate_entry(value, entry_schema, custom_def)
            return value
        elif type == Schema.PORTSPEC:
            # tODO(TBD) bug 1567063, validate source & target as PortDef type
            # as complex types not just as integers
            PortSpec.validate_additional_req(value, prop_name, custom_def)
        else:
            data = DataEntity(type, value, custom_def)
            return data.validate()

    @staticmethod
    def validate_entry(value, entry_schema, custom_def=None):
        '''Validate entries for map and list.'''
        schema = Schema(None, entry_schema)
        valuelist = value
        if isinstance(value, dict):
            valuelist = list(value.values())
        for v in valuelist:
            DataEntity.validate_datatype(schema.type, v, schema.entry_schema,
                                         custom_def)
            if schema.constraints:
                for constraint in schema.constraints:
                    constraint.validate(v)
        return value
*/