2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.openecomp.sdc.be.datamodel.utils;
18 import com.fasterxml.jackson.core.JsonProcessingException;
19 import com.fasterxml.jackson.core.type.TypeReference;
20 import com.fasterxml.jackson.databind.ObjectMapper;
21 import fj.data.Either;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.List;
28 import java.util.Objects;
29 import org.apache.commons.collections4.CollectionUtils;
30 import org.apache.commons.collections4.ListUtils;
31 import org.apache.commons.collections4.MapUtils;
32 import org.apache.commons.lang3.ArrayUtils;
33 import org.apache.commons.lang3.StringUtils;
34 import org.openecomp.sdc.be.components.impl.ResponseFormatManager;
35 import org.openecomp.sdc.be.dao.api.ActionStatus;
36 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
37 import org.openecomp.sdc.be.datatypes.elements.SubPropertyToscaFunction;
38 import org.openecomp.sdc.be.model.ComponentInstanceInput;
39 import org.openecomp.sdc.be.model.DataTypeDefinition;
40 import org.openecomp.sdc.be.model.InputDefinition;
41 import org.openecomp.sdc.be.model.PropertyConstraint;
42 import org.openecomp.sdc.be.model.PropertyDefinition;
43 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
44 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
45 import org.openecomp.sdc.be.model.tosca.ToscaType;
46 import org.openecomp.sdc.be.model.tosca.constraints.ConstraintUtil;
47 import org.openecomp.sdc.be.model.tosca.constraints.LengthConstraint;
48 import org.openecomp.sdc.be.model.tosca.constraints.MaxLengthConstraint;
49 import org.openecomp.sdc.be.model.tosca.constraints.MinLengthConstraint;
50 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
51 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException;
52 import org.openecomp.sdc.exception.ResponseFormat;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
56 public class PropertyValueConstraintValidationUtil {
58 private static final String UNDERSCORE = "_";
59 private static final String VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY = "%nValue provided in invalid format for %s property";
60 private static final Logger logger = LoggerFactory.getLogger(PropertyValueConstraintValidationUtil.class);
61 private static final String IGNORE_PROPERTY_VALUE_START_WITH_INPUT = "{\"get_input\":";
62 private static final String IGNORE_PROPERTY_VALUE_START_WITH_PROPERTY = "{\"get_property\":";
63 private static final String IGNORE_PROPERTY_VALUE_START_WITH_ATTRIBUTE = "{\"get_attribute\":";
64 private static final String IGNORE_PROPERTY_VALUE_INPUT = "{get_input=";
65 private static final String IGNORE_PROPERTY_VALUE_PROPERTY = "{get_property=";
66 private static final String IGNORE_PROPERTY_VALUE_ATTRIBUTE = "{get_attribute=";
67 private Map<String, DataTypeDefinition> dataTypeDefinitionCache;
68 private final ObjectMapper objectMapper = new ObjectMapper();
69 private final List<String> errorMessages = new ArrayList<>();
70 private StringBuilder completePropertyName;
71 private String completeInputName;
73 public Either<Boolean, ResponseFormat> validatePropertyConstraints(final Collection<? extends PropertyDefinition> propertyDefinitionList,
74 final ApplicationDataTypeCache applicationDataTypeCache,
77 dataTypeDefinitionCache = applicationDataTypeCache.getAll(model).left().value();
78 CollectionUtils.emptyIfNull(propertyDefinitionList).stream()
79 .filter(this::isNonToscaFunctionValuePresent)
80 .forEach(this::evaluatePropertyTypeForConstraintValidation);
81 if (CollectionUtils.isNotEmpty(errorMessages)) {
82 final String errorMsgAsString = String.join(",", errorMessages);
83 logger.debug("Properties with Invalid Data: {}", errorMsgAsString);
84 return Either.right(getResponseFormatManager().getResponseFormat(ActionStatus.INVALID_PROPERTY_VALUES, errorMsgAsString));
86 return Either.left(Boolean.TRUE);
89 private boolean isNonToscaFunctionValuePresent(PropertyDefinition propertyDefinition) {
90 if (isValueAToscaFunction(propertyDefinition)) {
93 if (propertyDefinition instanceof ComponentInstanceInput) {
94 return StringUtils.isNotEmpty(propertyDefinition.getValue());
96 if (propertyDefinition instanceof InputDefinition) {
97 return StringUtils.isNotEmpty(propertyDefinition.getDefaultValue());
99 return StringUtils.isNotEmpty(propertyDefinition.getValue());
102 private void evaluatePropertyTypeForConstraintValidation(PropertyDefinition propertyDefinition) {
103 if (propertyDefinition == null || propertyDefinition.getType() == null || !dataTypeDefinitionCache.containsKey(
104 propertyDefinition.getType())) {
105 errorMessages.add("\nUnsupported datatype found for property " + getCompletePropertyName(propertyDefinition));
108 completeInputName = "";
109 completePropertyName = new StringBuilder();
110 if (propertyDefinition instanceof ComponentInstanceInput) {
111 setCompletePropertyName(propertyDefinition);
112 evaluateComplexTypeProperties(propertyDefinition);
115 if (propertyDefinition instanceof InputDefinition) {
116 completeInputName = propertyDefinition.getName();
117 propertyDefinition = getPropertyDefinitionObjectFromInputs(propertyDefinition);
119 if (propertyDefinition != null) {
120 List<PropertyConstraint> propertyConstraints =
121 dataTypeDefinitionCache.get(propertyDefinition.getType()).safeGetConstraints();
122 if (ToscaType.isPrimitiveType(propertyDefinition.getType())) {
123 propertyDefinition.setConstraints(org.openecomp.sdc.be.dao.utils.CollectionUtils.merge(propertyDefinition.safeGetConstraints(),
124 propertyConstraints.isEmpty() ? new ArrayList<>() : propertyConstraints));
125 evaluateConstraintsOnProperty(propertyDefinition);
126 } else if (ToscaType.isCollectionType(propertyDefinition.getType())) {
127 propertyDefinition.setConstraints(org.openecomp.sdc.be.dao.utils.CollectionUtils.merge(propertyDefinition.safeGetConstraints(),
128 propertyConstraints.isEmpty() ? new ArrayList<>() : propertyConstraints));
129 evaluateConstraintsOnProperty(propertyDefinition);
130 evaluateCollectionTypeProperties(propertyDefinition);
132 setCompletePropertyName(propertyDefinition);
133 evaluateComplexTypeProperties(propertyDefinition);
138 private void setCompletePropertyName(PropertyDefinition propertyDefinition) {
139 if (StringUtils.isNotBlank(propertyDefinition.getUniqueId())) {
140 completePropertyName.append(propertyDefinition.getUniqueId().substring(propertyDefinition.getUniqueId().lastIndexOf('.') + 1));
144 private void evaluateConstraintsOnProperty(PropertyDefinition propertyDefinition) {
145 ToscaType toscaType = ToscaType.isValidType(propertyDefinition.getType());
146 if (!isValueAToscaFunction(propertyDefinition) && CollectionUtils.isNotEmpty(propertyDefinition.getConstraints())) {
147 for (PropertyConstraint propertyConstraint : propertyDefinition.getConstraints()) {
149 propertyConstraint.initialize(toscaType, propertyDefinition.getSchema());
150 propertyConstraint.validate(propertyDefinition);
151 } catch (ConstraintValueDoNotMatchPropertyTypeException | ConstraintViolationException exception) {
152 errorMessages.add(propertyConstraint.getErrorMessage(toscaType, exception, getCompletePropertyName(propertyDefinition)));
153 } catch (IllegalArgumentException ie) {
154 errorMessages.add(ie.getMessage());
157 } else if (!isValueAToscaFunction(propertyDefinition) && ToscaType.isPrimitiveType(propertyDefinition.getType())
158 && !propertyDefinition.isToscaFunction() && !toscaType.isValidValue(propertyDefinition.getValue())) {
159 errorMessages.add(String.format("Unsupported value provided for %s property supported value type is %s.",
160 getCompletePropertyName(propertyDefinition), toscaType.getType()));
164 private boolean isValueAToscaFunction(PropertyDefinition propertyDefinition) {
165 return (propertyDefinition.getToscaFunction() != null) || (propertyDefinition.getValue() != null
166 && ((propertyDefinition.getValue().startsWith(IGNORE_PROPERTY_VALUE_START_WITH_INPUT) || propertyDefinition.getValue().startsWith(IGNORE_PROPERTY_VALUE_START_WITH_PROPERTY)
167 || propertyDefinition.getValue().startsWith(IGNORE_PROPERTY_VALUE_START_WITH_ATTRIBUTE) || propertyDefinition.getValue().startsWith(IGNORE_PROPERTY_VALUE_ATTRIBUTE)
168 || propertyDefinition.getValue().startsWith(IGNORE_PROPERTY_VALUE_PROPERTY) || propertyDefinition.getValue().startsWith(IGNORE_PROPERTY_VALUE_INPUT))));
171 private void checkAndEvaluatePrimitiveProperty(PropertyDefinition propertyDefinition, DataTypeDefinition dataTypeDefinition) {
172 if (ToscaType.isPrimitiveType(dataTypeDefinition.getName()) && CollectionUtils.isNotEmpty(dataTypeDefinition.getConstraints())) {
173 PropertyDefinition definition = new PropertyDefinition();
174 definition.setValue(propertyDefinition.getValue());
175 definition.setType(dataTypeDefinition.getName());
176 definition.setConstraints(dataTypeDefinition.getConstraints());
177 evaluateConstraintsOnProperty(propertyDefinition);
181 private void evaluateComplexTypeProperties(PropertyDefinition propertyDefinition) {
182 List<PropertyDefinition> propertyDefinitions = dataTypeDefinitionCache.get(propertyDefinition.getType()).getProperties();
184 Map<String, Object> valueMap = MapUtils
185 .emptyIfNull(ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {
187 if (CollectionUtils.isEmpty(propertyDefinitions)) {
188 checkAndEvaluatePrimitiveProperty(propertyDefinition, dataTypeDefinitionCache.get(propertyDefinition.getType()));
190 ListUtils.emptyIfNull(propertyDefinitions).forEach(prop -> evaluateRegularComplexType(propertyDefinition, prop, valueMap));
192 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
193 logger.debug(e.getMessage(), e);
194 errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
198 private void evaluateRegularComplexType(PropertyDefinition propertyDefinition, PropertyDefinition prop, Map<String, Object> valueMap) {
200 PropertyDefinition newPropertyWithValue;
201 if (valueMap.containsKey(prop.getName())) {
202 if (propertyDefinition.getSubPropertyToscaFunctions() != null) {
203 for (SubPropertyToscaFunction subPropertyToscaFunction : propertyDefinition.getSubPropertyToscaFunctions()) {
204 final List<String> path = subPropertyToscaFunction.getSubPropertyPath();
205 if (path.size() == 1 && path.get(0).equals(prop.getName())) {
208 if (path.size() > 1) {
209 if (path.get(0).equals(propertyDefinition.getToscaSubPath()) && path.get(1).equals(prop.getName())) {
215 if (ToscaType.isPrimitiveType(prop.getType())) {
216 newPropertyWithValue = copyPropertyWithNewValue(prop, String.valueOf(valueMap.get(prop.getName())), prop.getName());
217 if (isPropertyToEvaluate(newPropertyWithValue)) {
218 evaluateConstraintsOnProperty(newPropertyWithValue);
220 } else if (ToscaType.isCollectionType(prop.getType())) {
221 newPropertyWithValue =
222 copyPropertyWithNewValue(prop,
223 objectMapper.writeValueAsString(valueMap.get(prop.getName())), prop.getName());
224 if (isPropertyToEvaluate(newPropertyWithValue)) {
225 evaluateCollectionTypeProperties(newPropertyWithValue);
228 newPropertyWithValue =
229 copyPropertyWithNewValue(prop,
230 objectMapper.writeValueAsString(valueMap.get(prop.getName())), prop.getName());
231 if (isPropertyToEvaluate(newPropertyWithValue)) {
232 evaluateComplexTypeProperties(newPropertyWithValue);
236 } catch (IOException | ConstraintValueDoNotMatchPropertyTypeException e) {
237 logger.error(e.getMessage(), e);
238 errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
242 private boolean isPropertyToEvaluate(PropertyDefinition propertyDefinition) throws ConstraintValueDoNotMatchPropertyTypeException {
243 if (Boolean.FALSE.equals(propertyDefinition.isRequired())) {
244 if (!ToscaType.isCollectionType(propertyDefinition.getType())) {
245 return StringUtils.isNotEmpty(propertyDefinition.getValue()) &&
246 !"null".equals(propertyDefinition.getValue());
247 } else if (ToscaType.LIST == ToscaType.isValidType(propertyDefinition.getType())) {
248 Collection<Object> list = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {
250 return CollectionUtils.isNotEmpty(list);
252 Map<String, Object> valueMap = MapUtils
253 .emptyIfNull(ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {
255 return MapUtils.isNotEmpty(valueMap);
262 private void evaluateCollectionTypeProperties(PropertyDefinition propertyDefinition) {
263 ToscaType toscaPropertyType = ToscaType.isValidType(propertyDefinition.getType());
265 if (isPropertyToEvaluate(propertyDefinition)) {
266 evaluateCollectionConstraints(propertyDefinition, toscaPropertyType);
268 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
269 logger.error(e.getMessage(), e);
270 errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
272 if (ToscaType.LIST == toscaPropertyType) {
273 evaluateListType(propertyDefinition);
274 } else if (ToscaType.MAP == toscaPropertyType) {
275 evaluateMapType(propertyDefinition);
279 private void evaluateCollectionConstraints(PropertyDefinition propertyDefinition, ToscaType toscaPropertyType) {
280 List<PropertyConstraint> constraintsList = propertyDefinition.getConstraints();
282 if (CollectionUtils.isEmpty(constraintsList)) {
285 ToscaType toscaPropertyType1;
286 if (null == toscaPropertyType) {
287 toscaPropertyType1 = ToscaType.isValidType(propertyDefinition.getType());
289 toscaPropertyType1 = toscaPropertyType;
291 constraintsList.stream()
292 .filter(this::isACollectionConstraint)
293 .forEach(propertyConstraint -> {
295 if (ToscaType.LIST == toscaPropertyType1) {
296 Collection<Object> list = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {
298 propertyConstraint.validate(list);
299 } else if (ToscaType.MAP == toscaPropertyType1) {
300 final Map<String, Object> map = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {
302 propertyConstraint.validate(map);
304 } catch (ConstraintValueDoNotMatchPropertyTypeException | ConstraintViolationException exception) {
305 errorMessages.add("\n" + propertyConstraint.getErrorMessage(toscaPropertyType1, exception,
306 getCompletePropertyName(propertyDefinition)));
311 private boolean isACollectionConstraint(PropertyConstraint constraint) {
312 if (constraint instanceof MaxLengthConstraint) {
315 if (constraint instanceof MinLengthConstraint) {
318 return constraint instanceof LengthConstraint;
321 private void evaluateListType(PropertyDefinition propertyDefinition) {
323 if (propertyDefinition.getSchemaType() == null) {
324 propertyDefinition.setSchema(createStringSchema());
326 Collection<Object> list = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {});
327 final Map<String, Object> map = new HashMap<>();
329 for (Object obj : list) {
330 map.put(String.valueOf(index),obj);
333 evaluateCollectionType(propertyDefinition, map);
334 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
335 logger.debug(e.getMessage(), e);
336 errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
340 private SchemaDefinition createStringSchema() {
341 final SchemaDefinition schemaDefinition = new SchemaDefinition();
342 final PropertyDefinition schemaStringProperty = new PropertyDefinition();
343 schemaStringProperty.setType(ToscaType.STRING.getType());
344 schemaDefinition.setProperty(schemaStringProperty);
345 return schemaDefinition;
348 private void evaluateMapType(final PropertyDefinition propertyDefinition) {
350 if (propertyDefinition.getSchemaType() == null) {
351 propertyDefinition.setSchema(createStringSchema());
353 final Map<String, Object> map = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {
355 evaluateCollectionType(propertyDefinition, map);
356 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
357 logger.debug(e.getMessage(), e);
358 errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
362 private void evaluateCollectionPrimitiveSchemaType(final PropertyDefinition propertyDefinition,
363 final String schemaType) throws JsonProcessingException {
364 if (propertyDefinition.getSchema() != null && propertyDefinition.getSchema().getProperty() instanceof PropertyDefinition) {
365 propertyDefinition.setConstraints(((PropertyDefinition) propertyDefinition.getSchema().getProperty()).getConstraints());
366 propertyDefinition.setValue(objectMapper.readValue(propertyDefinition.getValue(), String.class));
367 propertyDefinition.setType(schemaType);
368 evaluateConstraintsOnProperty(propertyDefinition);
372 private void evaluateCollectionType(final PropertyDefinition propertyDefinition, final Map<String, Object> valueMap) {
373 final String schemaType = propertyDefinition.getSchemaType();
374 for (String mapKey : valueMap.keySet()) {
375 final Object value = valueMap.get(mapKey);
377 final PropertyDefinition propertyCopyWithNewValue = copyPropertyWithNewValue(propertyDefinition,
378 objectMapper.writeValueAsString(value),mapKey);
379 propertyCopyWithNewValue.setToscaSubPath(mapKey);
380 if (!isValueAToscaFunction(propertyCopyWithNewValue)) {
381 if (ToscaType.isPrimitiveType(schemaType)) {
382 evaluateCollectionPrimitiveSchemaType(propertyCopyWithNewValue, schemaType);
383 } else if (ToscaType.isCollectionType(schemaType)) {
384 propertyCopyWithNewValue.setType(schemaType);
385 propertyCopyWithNewValue.setSchemaType(propertyDefinition.getSchemaProperty().getSchemaType());
386 evaluateCollectionTypeProperties(propertyCopyWithNewValue);
388 propertyCopyWithNewValue.setType(schemaType);
389 completePropertyName.append(UNDERSCORE);
390 completePropertyName.append(propertyCopyWithNewValue.getName());
391 evaluateComplexTypeProperties(propertyCopyWithNewValue);
394 } catch (final Exception e) {
395 logger.debug(e.getMessage(), e);
396 errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
401 private String getCompletePropertyName(final PropertyDefinition propertyDefinition) {
402 if (StringUtils.isNotBlank(completeInputName)) {
403 return completeInputName;
406 final String propertyName = propertyDefinition == null ? "" : propertyDefinition.getName();
407 if (StringUtils.isNotBlank(completePropertyName)) {
408 return completePropertyName + UNDERSCORE + propertyName;
414 private PropertyDefinition copyPropertyWithNewValue(final PropertyDefinition propertyToCopy, final String value, final String key) {
415 final var propertyDefinition = new PropertyDefinition(propertyToCopy);
416 if (key != null && propertyToCopy.getSubPropertyToscaFunctions() != null) {
417 propertyToCopy.getSubPropertyToscaFunctions().forEach(subPropertyToscaFunction -> {
418 final List<String> subPropertyPath = subPropertyToscaFunction.getSubPropertyPath();
419 if (subPropertyPath.get((subPropertyPath.size() - 1)).equals(key)) {
420 propertyDefinition.setToscaFunction(subPropertyToscaFunction.getToscaFunction());
424 propertyDefinition.setValue(value);
425 return propertyDefinition;
428 private PropertyDefinition getPropertyDefinitionObjectFromInputs(PropertyDefinition property) {
429 InputDefinition inputDefinition = (InputDefinition) property;
430 PropertyDefinition propertyDefinition = null;
431 if (CollectionUtils.isEmpty(inputDefinition.getProperties()) || ToscaType.isPrimitiveType(inputDefinition.getProperties().get(0).getType())) {
432 propertyDefinition = new PropertyDefinition();
433 propertyDefinition.setType(inputDefinition.getType());
434 propertyDefinition.setValue(inputDefinition.getDefaultValue());
435 propertyDefinition.setName(inputDefinition.getName());
436 propertyDefinition.setConstraints(inputDefinition.getConstraints());
437 } else if (Objects.nonNull(inputDefinition.getInputPath())) {
438 propertyDefinition = evaluateComplexTypeInputs(inputDefinition);
439 propertyDefinition.setConstraints(inputDefinition.getConstraints());
441 return propertyDefinition;
444 private PropertyDefinition evaluateComplexTypeInputs(InputDefinition inputDefinition) {
445 Map<String, Object> inputMap = new HashMap<>();
446 PropertyDefinition propertyDefinition = new PropertyDefinition();
447 String[] inputPathArr = inputDefinition.getInputPath().split("#");
448 if (inputPathArr.length > 1) {
449 inputPathArr = ArrayUtils.remove(inputPathArr, 0);
452 Map<String, Object> presentMap = inputMap;
453 for (int i = 0; i < inputPathArr.length; i++) {
454 if (i == inputPathArr.length - 1) {
455 presentMap.computeIfAbsent(inputPathArr[i], k -> inputDefinition.getDefaultValue());
457 presentMap.computeIfAbsent(inputPathArr[i], k -> new HashMap<String, Object>());
458 presentMap = (Map<String, Object>) presentMap.get(inputPathArr[i]);
461 if (CollectionUtils.isNotEmpty(inputDefinition.getProperties())) {
462 propertyDefinition.setType(inputDefinition.getProperties().get(0).getType());
464 propertyDefinition.setName(inputDefinition.getName());
465 propertyDefinition.setValue(objectMapper.writeValueAsString(inputMap));
466 } catch (IOException e) {
467 logger.error(e.getMessage(), e);
468 errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, inputDefinition.getName()));
470 return propertyDefinition;
473 protected ResponseFormatManager getResponseFormatManager() {
474 return ResponseFormatManager.getInstance();