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 Map<String, DataTypeDefinition> dataTypeDefinitionCache;
65 private final ObjectMapper objectMapper = new ObjectMapper();
66 private final List<String> errorMessages = new ArrayList<>();
67 private StringBuilder completePropertyName;
68 private String completeInputName;
70 public Either<Boolean, ResponseFormat> validatePropertyConstraints(final Collection<? extends PropertyDefinition> propertyDefinitionList,
71 final ApplicationDataTypeCache applicationDataTypeCache,
74 dataTypeDefinitionCache = applicationDataTypeCache.getAll(model).left().value();
75 CollectionUtils.emptyIfNull(propertyDefinitionList).stream()
76 .filter(this::isValuePresent)
77 .forEach(this::evaluatePropertyTypeForConstraintValidation);
78 if (CollectionUtils.isNotEmpty(errorMessages)) {
79 final String errorMsgAsString = String.join(",", errorMessages);
80 logger.debug("Properties with Invalid Data: {}", errorMsgAsString);
81 return Either.right(getResponseFormatManager().getResponseFormat(ActionStatus.INVALID_PROPERTY_VALUES, errorMsgAsString));
83 return Either.left(Boolean.TRUE);
86 private boolean isValuePresent(PropertyDefinition propertyDefinition) {
87 if (propertyDefinition instanceof ComponentInstanceInput) {
88 return StringUtils.isNotEmpty(propertyDefinition.getValue());
90 if (propertyDefinition instanceof InputDefinition) {
91 return StringUtils.isNotEmpty(propertyDefinition.getDefaultValue());
93 return StringUtils.isNotEmpty(propertyDefinition.getValue());
96 private void evaluatePropertyTypeForConstraintValidation(PropertyDefinition propertyDefinition) {
97 if (propertyDefinition == null || propertyDefinition.getType() == null || !dataTypeDefinitionCache.containsKey(
98 propertyDefinition.getType())) {
99 errorMessages.add("\nUnsupported datatype found for property " + getCompletePropertyName(propertyDefinition));
102 completeInputName = "";
103 completePropertyName = new StringBuilder();
104 if (propertyDefinition instanceof ComponentInstanceInput) {
105 setCompletePropertyName(propertyDefinition);
106 evaluateComplexTypeProperties(propertyDefinition);
109 if (propertyDefinition instanceof InputDefinition) {
110 completeInputName = propertyDefinition.getName();
111 propertyDefinition = getPropertyDefinitionObjectFromInputs(propertyDefinition);
113 if (propertyDefinition != null) {
114 List<PropertyConstraint> propertyConstraints =
115 dataTypeDefinitionCache.get(propertyDefinition.getType()).safeGetConstraints();
116 if (ToscaType.isPrimitiveType(propertyDefinition.getType())) {
117 propertyDefinition.setConstraints(org.openecomp.sdc.be.dao.utils.CollectionUtils.merge(propertyDefinition.safeGetConstraints(),
118 propertyConstraints.isEmpty() ? new ArrayList<>() : propertyConstraints));
119 evaluateConstraintsOnProperty(propertyDefinition);
120 } else if (ToscaType.isCollectionType(propertyDefinition.getType())) {
121 propertyDefinition.setConstraints(org.openecomp.sdc.be.dao.utils.CollectionUtils.merge(propertyDefinition.safeGetConstraints(),
122 propertyConstraints.isEmpty() ? new ArrayList<>() : propertyConstraints));
123 evaluateConstraintsOnProperty(propertyDefinition);
124 evaluateCollectionTypeProperties(propertyDefinition);
126 setCompletePropertyName(propertyDefinition);
127 evaluateComplexTypeProperties(propertyDefinition);
132 private void setCompletePropertyName(PropertyDefinition propertyDefinition) {
133 if (StringUtils.isNotBlank(propertyDefinition.getUniqueId())) {
134 completePropertyName.append(propertyDefinition.getUniqueId().substring(propertyDefinition.getUniqueId().lastIndexOf('.') + 1));
138 private void evaluateConstraintsOnProperty(PropertyDefinition propertyDefinition) {
139 ToscaType toscaType = ToscaType.isValidType(propertyDefinition.getType());
140 if (isPropertyNotMappedAsInput(propertyDefinition) && CollectionUtils.isNotEmpty(propertyDefinition.getConstraints())) {
141 for (PropertyConstraint propertyConstraint : propertyDefinition.getConstraints()) {
143 propertyConstraint.initialize(toscaType);
144 propertyConstraint.validate(toscaType, propertyDefinition.getValue());
145 } catch (ConstraintValueDoNotMatchPropertyTypeException | ConstraintViolationException exception) {
146 errorMessages.add(propertyConstraint.getErrorMessage(toscaType, exception, getCompletePropertyName(propertyDefinition)));
147 } catch (IllegalArgumentException ie) {
148 errorMessages.add(ie.getMessage());
151 } else if (isPropertyNotMappedAsInput(propertyDefinition) && ToscaType.isPrimitiveType(propertyDefinition.getType())
152 && !propertyDefinition.isToscaFunction() && !toscaType.isValidValue(propertyDefinition.getValue())) {
153 errorMessages.add(String.format("Unsupported value provided for %s property supported value type is %s.",
154 getCompletePropertyName(propertyDefinition), toscaType.getType()));
158 private boolean isPropertyNotMappedAsInput(PropertyDefinition propertyDefinition) {
159 return !propertyDefinition.getValue().startsWith(IGNORE_PROPERTY_VALUE_START_WITH_INPUT) && !propertyDefinition.getValue().startsWith(IGNORE_PROPERTY_VALUE_START_WITH_PROPERTY)
160 && !propertyDefinition.getValue().startsWith(IGNORE_PROPERTY_VALUE_START_WITH_ATTRIBUTE);
163 private void checkAndEvaluatePrimitiveProperty(PropertyDefinition propertyDefinition, DataTypeDefinition dataTypeDefinition) {
164 if (ToscaType.isPrimitiveType(dataTypeDefinition.getName()) && CollectionUtils.isNotEmpty(dataTypeDefinition.getConstraints())) {
165 PropertyDefinition definition = new PropertyDefinition();
166 definition.setValue(propertyDefinition.getValue());
167 definition.setType(dataTypeDefinition.getName());
168 definition.setConstraints(dataTypeDefinition.getConstraints());
169 evaluateConstraintsOnProperty(propertyDefinition);
173 private void evaluateComplexTypeProperties(PropertyDefinition propertyDefinition) {
174 List<PropertyDefinition> propertyDefinitions = dataTypeDefinitionCache.get(propertyDefinition.getType()).getProperties();
176 Map<String, Object> valueMap = MapUtils
177 .emptyIfNull(ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {
179 if (CollectionUtils.isEmpty(propertyDefinitions)) {
180 checkAndEvaluatePrimitiveProperty(propertyDefinition, dataTypeDefinitionCache.get(propertyDefinition.getType()));
182 ListUtils.emptyIfNull(propertyDefinitions).forEach(prop -> evaluateRegularComplexType(propertyDefinition, prop, valueMap));
184 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
185 logger.debug(e.getMessage(), e);
186 errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
190 private void evaluateRegularComplexType(PropertyDefinition propertyDefinition, PropertyDefinition prop, Map<String, Object> valueMap) {
192 PropertyDefinition newPropertyWithValue;
193 if (valueMap.containsKey(prop.getName())) {
194 if (propertyDefinition.getSubPropertyToscaFunctions() != null) {
195 for (SubPropertyToscaFunction subPropertyToscaFunction : propertyDefinition.getSubPropertyToscaFunctions()) {
196 final List<String> path = subPropertyToscaFunction.getSubPropertyPath();
197 if (path.size() == 1) {
198 if (path.get(0).equals(prop.getName())) {
202 if (path.size() > 1) {
203 if (path.get(0).equals(propertyDefinition.getToscaSubPath()) && path.get(1).equals(prop.getName())) {
209 if (ToscaType.isPrimitiveType(prop.getType())) {
210 newPropertyWithValue = copyPropertyWithNewValue(prop, String.valueOf(valueMap.get(prop.getName())), prop.getName());
211 if (isPropertyToEvaluate(newPropertyWithValue)) {
212 evaluateConstraintsOnProperty(newPropertyWithValue);
214 } else if (ToscaType.isCollectionType(prop.getType())) {
215 newPropertyWithValue =
216 copyPropertyWithNewValue(prop,
217 objectMapper.writeValueAsString(valueMap.get(prop.getName())), prop.getName());
218 if (isPropertyToEvaluate(newPropertyWithValue)) {
219 evaluateCollectionTypeProperties(newPropertyWithValue);
222 newPropertyWithValue =
223 copyPropertyWithNewValue(prop,
224 objectMapper.writeValueAsString(valueMap.get(prop.getName())), prop.getName());
225 if (isPropertyToEvaluate(newPropertyWithValue)) {
226 evaluateComplexTypeProperties(newPropertyWithValue);
230 } catch (IOException | ConstraintValueDoNotMatchPropertyTypeException e) {
231 logger.error(e.getMessage(), e);
232 errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
236 private boolean isPropertyToEvaluate(PropertyDefinition propertyDefinition) throws ConstraintValueDoNotMatchPropertyTypeException {
237 if (Boolean.FALSE.equals(propertyDefinition.isRequired())) {
238 if (!ToscaType.isCollectionType(propertyDefinition.getType())) {
239 return StringUtils.isNotEmpty(propertyDefinition.getValue()) &&
240 !"null".equals(propertyDefinition.getValue());
241 } else if (ToscaType.LIST == ToscaType.isValidType(propertyDefinition.getType())) {
242 Collection<Object> list = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {
244 return CollectionUtils.isNotEmpty(list);
246 Map<String, Object> valueMap = MapUtils
247 .emptyIfNull(ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {
249 return MapUtils.isNotEmpty(valueMap);
256 private void evaluateCollectionTypeProperties(PropertyDefinition propertyDefinition) {
257 ToscaType toscaPropertyType = ToscaType.isValidType(propertyDefinition.getType());
259 if (isPropertyToEvaluate(propertyDefinition)) {
260 evaluateCollectionConstraints(propertyDefinition, toscaPropertyType);
262 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
263 logger.error(e.getMessage(), e);
264 errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
266 if (ToscaType.LIST == toscaPropertyType) {
267 evaluateListType(propertyDefinition);
268 } else if (ToscaType.MAP == toscaPropertyType) {
269 evaluateMapType(propertyDefinition);
273 private void evaluateCollectionConstraints(PropertyDefinition propertyDefinition, ToscaType toscaPropertyType) {
274 List<PropertyConstraint> constraintsList = propertyDefinition.getConstraints();
276 if (CollectionUtils.isEmpty(constraintsList)) {
279 ToscaType toscaPropertyType1;
280 if (null == toscaPropertyType) {
281 toscaPropertyType1 = ToscaType.isValidType(propertyDefinition.getType());
283 toscaPropertyType1 = toscaPropertyType;
285 constraintsList.stream()
286 .filter(this::isACollectionConstraint)
287 .forEach(propertyConstraint -> {
289 if (ToscaType.LIST == toscaPropertyType1) {
290 Collection<Object> list = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {
292 propertyConstraint.validate(list);
293 } else if (ToscaType.MAP == toscaPropertyType1) {
294 final Map<String, Object> map = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {
296 propertyConstraint.validate(map);
298 } catch (ConstraintValueDoNotMatchPropertyTypeException | ConstraintViolationException exception) {
299 errorMessages.add("\n" + propertyConstraint.getErrorMessage(toscaPropertyType1, exception,
300 getCompletePropertyName(propertyDefinition)));
305 private boolean isACollectionConstraint(PropertyConstraint constraint) {
306 if (constraint instanceof MaxLengthConstraint) {
309 if (constraint instanceof MinLengthConstraint) {
312 return constraint instanceof LengthConstraint;
315 private void evaluateListType(PropertyDefinition propertyDefinition) {
317 if (propertyDefinition.getSchemaType() == null) {
318 propertyDefinition.setSchema(createStringSchema());
320 Collection<Object> list = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {});
321 final Map<String, Object> map = new HashMap<>();
323 for (Object obj : list) {
324 map.put(String.valueOf(index),obj);
327 evaluateCollectionType(propertyDefinition, map);
328 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
329 logger.debug(e.getMessage(), e);
330 errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
334 private SchemaDefinition createStringSchema() {
335 final SchemaDefinition schemaDefinition = new SchemaDefinition();
336 final PropertyDefinition schemaStringProperty = new PropertyDefinition();
337 schemaStringProperty.setType(ToscaType.STRING.getType());
338 schemaDefinition.setProperty(schemaStringProperty);
339 return schemaDefinition;
342 private void evaluateMapType(final PropertyDefinition propertyDefinition) {
344 if (propertyDefinition.getSchemaType() == null) {
345 propertyDefinition.setSchema(createStringSchema());
347 final Map<String, Object> map = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {
349 evaluateCollectionType(propertyDefinition, map);
350 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
351 logger.debug(e.getMessage(), e);
352 errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
356 private void evaluateCollectionPrimitiveSchemaType(final PropertyDefinition propertyDefinition,
357 final String schemaType) throws JsonProcessingException {
358 if (propertyDefinition.getSchema() != null && propertyDefinition.getSchema().getProperty() instanceof PropertyDefinition) {
359 propertyDefinition.setConstraints(((PropertyDefinition) propertyDefinition.getSchema().getProperty()).getConstraints());
360 propertyDefinition.setValue(objectMapper.readValue(propertyDefinition.getValue(), String.class));
361 propertyDefinition.setType(schemaType);
362 evaluateConstraintsOnProperty(propertyDefinition);
366 private void evaluateCollectionType(final PropertyDefinition propertyDefinition, final Map<String, Object> valueMap) {
367 final String schemaType = propertyDefinition.getSchemaType();
368 for (String mapKey : valueMap.keySet()) {
369 final Object value = valueMap.get(mapKey);
371 final PropertyDefinition propertyCopyWithNewValue = copyPropertyWithNewValue(propertyDefinition,
372 objectMapper.writeValueAsString(value),mapKey);
373 propertyCopyWithNewValue.setToscaSubPath(mapKey);
374 if (isPropertyNotMappedAsInput(propertyCopyWithNewValue)) {
375 if (ToscaType.isPrimitiveType(schemaType)) {
376 evaluateCollectionPrimitiveSchemaType(propertyCopyWithNewValue, schemaType);
377 } else if (ToscaType.isCollectionType(schemaType)) {
378 propertyCopyWithNewValue.setType(schemaType);
379 propertyCopyWithNewValue.setSchemaType(propertyDefinition.getSchemaProperty().getSchemaType());
380 evaluateCollectionTypeProperties(propertyCopyWithNewValue);
382 propertyCopyWithNewValue.setType(schemaType);
383 completePropertyName.append(UNDERSCORE);
384 completePropertyName.append(propertyCopyWithNewValue.getName());
385 evaluateComplexTypeProperties(propertyCopyWithNewValue);
388 } catch (final Exception e) {
389 logger.debug(e.getMessage(), e);
390 errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
395 private String getCompletePropertyName(final PropertyDefinition propertyDefinition) {
396 if (StringUtils.isNotBlank(completeInputName)) {
397 return completeInputName;
400 final String propertyName = propertyDefinition == null ? "" : propertyDefinition.getName();
401 if (StringUtils.isNotBlank(completePropertyName)) {
402 return completePropertyName + UNDERSCORE + propertyName;
408 private PropertyDefinition copyPropertyWithNewValue(final PropertyDefinition propertyToCopy, final String value, final String key) {
409 final var propertyDefinition = new PropertyDefinition(propertyToCopy);
410 if (key != null && propertyToCopy.getSubPropertyToscaFunctions() != null) {
411 propertyToCopy.getSubPropertyToscaFunctions().forEach(subPropertyToscaFunction -> {
412 final List<String> subPropertyPath = subPropertyToscaFunction.getSubPropertyPath();
413 if (subPropertyPath.get((subPropertyPath.size() - 1)).equals(key)) {
414 propertyDefinition.setToscaFunction(subPropertyToscaFunction.getToscaFunction());
418 propertyDefinition.setValue(value);
419 return propertyDefinition;
422 private PropertyDefinition getPropertyDefinitionObjectFromInputs(PropertyDefinition property) {
423 InputDefinition inputDefinition = (InputDefinition) property;
424 PropertyDefinition propertyDefinition = null;
425 if (CollectionUtils.isEmpty(inputDefinition.getProperties()) || ToscaType.isPrimitiveType(inputDefinition.getProperties().get(0).getType())) {
426 propertyDefinition = new PropertyDefinition();
427 propertyDefinition.setType(inputDefinition.getType());
428 propertyDefinition.setValue(inputDefinition.getDefaultValue());
429 propertyDefinition.setName(inputDefinition.getName());
430 propertyDefinition.setConstraints(inputDefinition.getConstraints());
431 } else if (Objects.nonNull(inputDefinition.getInputPath())) {
432 propertyDefinition = evaluateComplexTypeInputs(inputDefinition);
433 propertyDefinition.setConstraints(inputDefinition.getConstraints());
435 return propertyDefinition;
438 private PropertyDefinition evaluateComplexTypeInputs(InputDefinition inputDefinition) {
439 Map<String, Object> inputMap = new HashMap<>();
440 PropertyDefinition propertyDefinition = new PropertyDefinition();
441 String[] inputPathArr = inputDefinition.getInputPath().split("#");
442 if (inputPathArr.length > 1) {
443 inputPathArr = ArrayUtils.remove(inputPathArr, 0);
446 Map<String, Object> presentMap = inputMap;
447 for (int i = 0; i < inputPathArr.length; i++) {
448 if (i == inputPathArr.length - 1) {
449 presentMap.computeIfAbsent(inputPathArr[i], k -> inputDefinition.getDefaultValue());
451 presentMap.computeIfAbsent(inputPathArr[i], k -> new HashMap<String, Object>());
452 presentMap = (Map<String, Object>) presentMap.get(inputPathArr[i]);
455 if (CollectionUtils.isNotEmpty(inputDefinition.getProperties())) {
456 propertyDefinition.setType(inputDefinition.getProperties().get(0).getType());
458 propertyDefinition.setName(inputDefinition.getName());
459 propertyDefinition.setValue(objectMapper.writeValueAsString(inputMap));
460 } catch (IOException e) {
461 logger.error(e.getMessage(), e);
462 errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, inputDefinition.getName()));
464 return propertyDefinition;
467 protected ResponseFormatManager getResponseFormatManager() {
468 return ResponseFormatManager.getInstance();