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
|
/*
* Copyright © 2016-2018 European Support Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openecomp.sdc.be.datamodel.utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import fj.data.Either;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.openecomp.sdc.be.components.impl.ResponseFormatManager;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
import org.openecomp.sdc.be.model.ComponentInstanceInput;
import org.openecomp.sdc.be.model.DataTypeDefinition;
import org.openecomp.sdc.be.model.InputDefinition;
import org.openecomp.sdc.be.model.PropertyConstraint;
import org.openecomp.sdc.be.model.PropertyDefinition;
import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
import org.openecomp.sdc.be.model.tosca.ToscaType;
import org.openecomp.sdc.be.model.tosca.constraints.ConstraintUtil;
import org.openecomp.sdc.be.model.tosca.constraints.LengthConstraint;
import org.openecomp.sdc.be.model.tosca.constraints.MaxLengthConstraint;
import org.openecomp.sdc.be.model.tosca.constraints.MinLengthConstraint;
import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException;
import org.openecomp.sdc.exception.ResponseFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PropertyValueConstraintValidationUtil {
private static final String UNDERSCORE = "_";
private static final String VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY = "%nValue provided in invalid format for %s property";
private static final Logger logger = LoggerFactory.getLogger(PropertyValueConstraintValidationUtil.class);
private static final String IGNORE_PROPERTY_VALUE_START_WITH = "{\"get_input\":";
private Map<String, DataTypeDefinition> dataTypeDefinitionCache;
private final ObjectMapper objectMapper = new ObjectMapper();
private final List<String> errorMessages = new ArrayList<>();
private StringBuilder completePropertyName;
private String completeInputName;
public Either<Boolean, ResponseFormat> validatePropertyConstraints(final Collection<? extends PropertyDefinition> propertyDefinitionList,
final ApplicationDataTypeCache applicationDataTypeCache,
final String model) {
dataTypeDefinitionCache = applicationDataTypeCache.getAll(model).left().value();
CollectionUtils.emptyIfNull(propertyDefinitionList).stream()
.filter(this::isValuePresent)
.forEach(this::evaluatePropertyTypeForConstraintValidation);
if (CollectionUtils.isNotEmpty(errorMessages)) {
final String errorMsgAsString = String.join(",", errorMessages);
logger.debug("Properties with Invalid Data: {}", errorMsgAsString);
return Either.right(getResponseFormatManager().getResponseFormat(ActionStatus.INVALID_PROPERTY_VALUES, errorMsgAsString));
}
return Either.left(Boolean.TRUE);
}
private boolean isValuePresent(PropertyDefinition propertyDefinition) {
if (propertyDefinition instanceof ComponentInstanceInput) {
return StringUtils.isNotEmpty(propertyDefinition.getValue());
}
if (propertyDefinition instanceof InputDefinition) {
return StringUtils.isNotEmpty(propertyDefinition.getDefaultValue());
}
return StringUtils.isNotEmpty(propertyDefinition.getValue());
}
private void evaluatePropertyTypeForConstraintValidation(PropertyDefinition propertyDefinition) {
if (propertyDefinition == null || propertyDefinition.getType() == null || !dataTypeDefinitionCache.containsKey(
propertyDefinition.getType())) {
errorMessages.add("\nUnsupported datatype found for property " + getCompletePropertyName(propertyDefinition));
return;
}
completeInputName = "";
completePropertyName = new StringBuilder();
if (propertyDefinition instanceof ComponentInstanceInput) {
setCompletePropertyName(propertyDefinition);
evaluateComplexTypeProperties(propertyDefinition);
return;
}
if (propertyDefinition instanceof InputDefinition) {
completeInputName = propertyDefinition.getName();
propertyDefinition = getPropertyDefinitionObjectFromInputs(propertyDefinition);
}
if (propertyDefinition != null) {
List<PropertyConstraint> propertyConstraints =
dataTypeDefinitionCache.get(propertyDefinition.getType()).safeGetConstraints();
if (ToscaType.isPrimitiveType(propertyDefinition.getType())) {
propertyDefinition.setConstraints(org.openecomp.sdc.be.dao.utils.CollectionUtils.merge(propertyDefinition.safeGetConstraints(),
propertyConstraints.isEmpty() ? new ArrayList<>() : propertyConstraints));
evaluateConstraintsOnProperty(propertyDefinition);
} else if (ToscaType.isCollectionType(propertyDefinition.getType())) {
propertyDefinition.setConstraints(org.openecomp.sdc.be.dao.utils.CollectionUtils.merge(propertyDefinition.safeGetConstraints(),
propertyConstraints.isEmpty() ? new ArrayList<>() : propertyConstraints));
evaluateConstraintsOnProperty(propertyDefinition);
evaluateCollectionTypeProperties(propertyDefinition);
} else {
setCompletePropertyName(propertyDefinition);
evaluateComplexTypeProperties(propertyDefinition);
}
}
}
private void setCompletePropertyName(PropertyDefinition propertyDefinition) {
if (StringUtils.isNotBlank(propertyDefinition.getUniqueId())) {
completePropertyName.append(propertyDefinition.getUniqueId().substring(propertyDefinition.getUniqueId().lastIndexOf('.') + 1));
}
}
private void evaluateConstraintsOnProperty(PropertyDefinition propertyDefinition) {
ToscaType toscaType = ToscaType.isValidType(propertyDefinition.getType());
if (isPropertyNotMappedAsInput(propertyDefinition) && CollectionUtils.isNotEmpty(propertyDefinition.getConstraints())) {
for (PropertyConstraint propertyConstraint : propertyDefinition.getConstraints()) {
try {
propertyConstraint.initialize(toscaType);
propertyConstraint.validate(toscaType, propertyDefinition.getValue());
} catch (ConstraintValueDoNotMatchPropertyTypeException | ConstraintViolationException exception) {
errorMessages.add(propertyConstraint.getErrorMessage(toscaType, exception, getCompletePropertyName(propertyDefinition)));
} catch (IllegalArgumentException ie) {
errorMessages.add(ie.getMessage());
}
}
} else if (isPropertyNotMappedAsInput(propertyDefinition) && ToscaType.isPrimitiveType(propertyDefinition.getType()) && !toscaType
.isValidValue(propertyDefinition.getValue())) {
errorMessages.add(String.format("Unsupported value provided for %s property supported value type is %s.",
getCompletePropertyName(propertyDefinition), toscaType.getType()));
}
}
private boolean isPropertyNotMappedAsInput(PropertyDefinition propertyDefinition) {
return !propertyDefinition.getValue().startsWith(IGNORE_PROPERTY_VALUE_START_WITH);
}
private void checkAndEvaluatePrimitiveProperty(PropertyDefinition propertyDefinition, DataTypeDefinition dataTypeDefinition) {
if (ToscaType.isPrimitiveType(dataTypeDefinition.getName()) && CollectionUtils.isNotEmpty(dataTypeDefinition.getConstraints())) {
PropertyDefinition definition = new PropertyDefinition();
definition.setValue(propertyDefinition.getValue());
definition.setType(dataTypeDefinition.getName());
definition.setConstraints(dataTypeDefinition.getConstraints());
evaluateConstraintsOnProperty(propertyDefinition);
}
}
private void evaluateComplexTypeProperties(PropertyDefinition propertyDefinition) {
List<PropertyDefinition> propertyDefinitions = dataTypeDefinitionCache.get(propertyDefinition.getType()).getProperties();
try {
Map<String, Object> valueMap = MapUtils
.emptyIfNull(ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {
}));
if (CollectionUtils.isEmpty(propertyDefinitions)) {
checkAndEvaluatePrimitiveProperty(propertyDefinition, dataTypeDefinitionCache.get(propertyDefinition.getType()));
} else {
ListUtils.emptyIfNull(propertyDefinitions).forEach(prop -> evaluateRegularComplexType(propertyDefinition, prop, valueMap));
}
} catch (ConstraintValueDoNotMatchPropertyTypeException e) {
logger.debug(e.getMessage(), e);
errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
}
}
private void evaluateRegularComplexType(PropertyDefinition propertyDefinition, PropertyDefinition prop, Map<String, Object> valueMap) {
try {
PropertyDefinition newPropertyWithValue;
if (valueMap.containsKey(prop.getName())) {
if (ToscaType.isPrimitiveType(prop.getType())) {
newPropertyWithValue = copyPropertyWithNewValue(prop, String.valueOf(valueMap.get(prop.getName())));
if (isPropertyToEvaluate(newPropertyWithValue)) {
evaluateConstraintsOnProperty(newPropertyWithValue);
}
} else if (ToscaType.isCollectionType(prop.getType())) {
newPropertyWithValue =
copyPropertyWithNewValue(prop,
objectMapper.writeValueAsString(valueMap.get(prop.getName())));
if (isPropertyToEvaluate(newPropertyWithValue)) {
evaluateCollectionTypeProperties(newPropertyWithValue);
}
} else {
newPropertyWithValue =
copyPropertyWithNewValue(prop,
objectMapper.writeValueAsString(valueMap.get(prop.getName())));
if (isPropertyToEvaluate(newPropertyWithValue)) {
evaluateComplexTypeProperties(newPropertyWithValue);
}
}
}
} catch (IOException | ConstraintValueDoNotMatchPropertyTypeException e) {
logger.error(e.getMessage(), e);
errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
}
}
private boolean isPropertyToEvaluate(PropertyDefinition propertyDefinition) throws ConstraintValueDoNotMatchPropertyTypeException {
if (Boolean.FALSE.equals(propertyDefinition.isRequired())) {
if (!ToscaType.isCollectionType(propertyDefinition.getType())) {
return StringUtils.isNotEmpty(propertyDefinition.getValue()) &&
!"null".equals(propertyDefinition.getValue());
} else if (ToscaType.LIST == ToscaType.isValidType(propertyDefinition.getType())) {
Collection<Object> list = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {
});
return CollectionUtils.isNotEmpty(list);
} else {
Map<String, Object> valueMap = MapUtils
.emptyIfNull(ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {
}));
return MapUtils.isNotEmpty(valueMap);
}
} else {
return true;
}
}
private void evaluateCollectionTypeProperties(PropertyDefinition propertyDefinition) {
ToscaType toscaPropertyType = ToscaType.isValidType(propertyDefinition.getType());
try {
if (isPropertyToEvaluate(propertyDefinition)) {
evaluateCollectionConstraints(propertyDefinition, toscaPropertyType);
}
} catch (ConstraintValueDoNotMatchPropertyTypeException e) {
logger.error(e.getMessage(), e);
errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
}
if (ToscaType.LIST == toscaPropertyType) {
evaluateListType(propertyDefinition);
} else if (ToscaType.MAP == toscaPropertyType) {
evaluateMapType(propertyDefinition);
}
}
private void evaluateCollectionConstraints(PropertyDefinition propertyDefinition, ToscaType toscaPropertyType) {
List<PropertyConstraint> constraintsList = propertyDefinition.getConstraints();
if (CollectionUtils.isEmpty(constraintsList)) {
return;
}
ToscaType toscaPropertyType1;
if (null == toscaPropertyType) {
toscaPropertyType1 = ToscaType.isValidType(propertyDefinition.getType());
} else {
toscaPropertyType1 = toscaPropertyType;
}
constraintsList.stream()
.filter(this::isACollectionConstraint)
.forEach(propertyConstraint -> {
try {
if (ToscaType.LIST == toscaPropertyType1) {
Collection<Object> list = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {
});
propertyConstraint.validate(list);
} else if (ToscaType.MAP == toscaPropertyType1) {
final Map<String, Object> map = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {
});
propertyConstraint.validate(map);
}
} catch (ConstraintValueDoNotMatchPropertyTypeException | ConstraintViolationException exception) {
errorMessages.add("\n" + propertyConstraint.getErrorMessage(toscaPropertyType1, exception,
getCompletePropertyName(propertyDefinition)));
}
});
}
private boolean isACollectionConstraint(PropertyConstraint constraint) {
if (constraint instanceof MaxLengthConstraint) {
return true;
}
if (constraint instanceof MinLengthConstraint) {
return true;
}
return constraint instanceof LengthConstraint;
}
private void evaluateListType(PropertyDefinition propertyDefinition) {
try {
if (propertyDefinition.getSchemaType() == null) {
propertyDefinition.setSchema(createStringSchema());
}
Collection<Object> list = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {
});
evaluateCollectionType(propertyDefinition, list);
} catch (ConstraintValueDoNotMatchPropertyTypeException e) {
logger.debug(e.getMessage(), e);
errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
}
}
private SchemaDefinition createStringSchema() {
final SchemaDefinition schemaDefinition = new SchemaDefinition();
final PropertyDefinition schemaStringProperty = new PropertyDefinition();
schemaStringProperty.setType(ToscaType.STRING.getType());
schemaDefinition.setProperty(schemaStringProperty);
return schemaDefinition;
}
private void evaluateMapType(final PropertyDefinition propertyDefinition) {
try {
if (propertyDefinition.getSchemaType() == null) {
propertyDefinition.setSchema(createStringSchema());
}
final Map<String, Object> map = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {
});
evaluateCollectionType(propertyDefinition, map.values());
} catch (ConstraintValueDoNotMatchPropertyTypeException e) {
logger.debug(e.getMessage(), e);
errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
}
}
private void evaluateCollectionPrimitiveSchemaType(final PropertyDefinition propertyDefinition,
final String schemaType) throws JsonProcessingException {
if (propertyDefinition.getSchema() != null && propertyDefinition.getSchema().getProperty() instanceof PropertyDefinition) {
propertyDefinition.setConstraints(((PropertyDefinition) propertyDefinition.getSchema().getProperty()).getConstraints());
propertyDefinition.setValue(objectMapper.readValue(propertyDefinition.getValue(), String.class));
propertyDefinition.setType(schemaType);
evaluateConstraintsOnProperty(propertyDefinition);
}
}
private void evaluateCollectionType(final PropertyDefinition propertyDefinition, final Collection<Object> valueList) {
final String schemaType = propertyDefinition.getSchemaType();
for (final Object value : valueList) {
try {
final PropertyDefinition propertyCopyWithNewValue = copyPropertyWithNewValue(propertyDefinition,
objectMapper.writeValueAsString(value));
if (ToscaType.isPrimitiveType(schemaType)) {
evaluateCollectionPrimitiveSchemaType(propertyCopyWithNewValue, schemaType);
} else if (ToscaType.isCollectionType(schemaType)) {
propertyCopyWithNewValue.setType(schemaType);
propertyCopyWithNewValue.setSchemaType(propertyDefinition.getSchemaProperty().getSchemaType());
evaluateCollectionTypeProperties(propertyCopyWithNewValue);
} else {
propertyCopyWithNewValue.setType(schemaType);
completePropertyName.append(UNDERSCORE);
completePropertyName.append(propertyCopyWithNewValue.getName());
evaluateComplexTypeProperties(propertyCopyWithNewValue);
}
} catch (final Exception e) {
logger.debug(e.getMessage(), e);
errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
}
}
}
private String getCompletePropertyName(final PropertyDefinition propertyDefinition) {
if (StringUtils.isNotBlank(completeInputName)) {
return completeInputName;
}
final String propertyName = propertyDefinition == null ? "" : propertyDefinition.getName();
if (StringUtils.isNotBlank(completePropertyName)) {
return completePropertyName + UNDERSCORE + propertyName;
}
return propertyName;
}
private PropertyDefinition copyPropertyWithNewValue(final PropertyDefinition propertyToCopy, final String value) {
final var propertyDefinition = new PropertyDefinition(propertyToCopy);
propertyDefinition.setValue(value);
return propertyDefinition;
}
private PropertyDefinition getPropertyDefinitionObjectFromInputs(PropertyDefinition property) {
InputDefinition inputDefinition = (InputDefinition) property;
PropertyDefinition propertyDefinition = null;
if (CollectionUtils.isEmpty(inputDefinition.getProperties()) || ToscaType.isPrimitiveType(inputDefinition.getProperties().get(0).getType())) {
propertyDefinition = new PropertyDefinition();
propertyDefinition.setType(inputDefinition.getType());
propertyDefinition.setValue(inputDefinition.getDefaultValue());
propertyDefinition.setName(inputDefinition.getName());
} else if (Objects.nonNull(inputDefinition.getInputPath())) {
propertyDefinition = evaluateComplexTypeInputs(inputDefinition);
}
return propertyDefinition;
}
private PropertyDefinition evaluateComplexTypeInputs(InputDefinition inputDefinition) {
Map<String, Object> inputMap = new HashMap<>();
PropertyDefinition propertyDefinition = new PropertyDefinition();
String[] inputPathArr = inputDefinition.getInputPath().split("#");
if (inputPathArr.length > 1) {
inputPathArr = ArrayUtils.remove(inputPathArr, 0);
}
try {
Map<String, Object> presentMap = inputMap;
for (int i = 0; i < inputPathArr.length; i++) {
if (i == inputPathArr.length - 1) {
presentMap.computeIfAbsent(inputPathArr[i], k -> inputDefinition.getDefaultValue());
} else {
presentMap.computeIfAbsent(inputPathArr[i], k -> new HashMap<String, Object>());
presentMap = (Map<String, Object>) presentMap.get(inputPathArr[i]);
}
}
if (CollectionUtils.isNotEmpty(inputDefinition.getProperties())) {
propertyDefinition.setType(inputDefinition.getProperties().get(0).getType());
}
propertyDefinition.setName(inputDefinition.getName());
propertyDefinition.setValue(objectMapper.writeValueAsString(inputMap));
} catch (IOException e) {
logger.error(e.getMessage(), e);
errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, inputDefinition.getName()));
}
return propertyDefinition;
}
protected ResponseFormatManager getResponseFormatManager() {
return ResponseFormatManager.getInstance();
}
}
|