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::isNonToscaFunctionValuePresent)
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 isNonToscaFunctionValuePresent(PropertyDefinition propertyDefinition) {
87 if (isValueAToscaFunction(propertyDefinition)) {
90 if (propertyDefinition instanceof ComponentInstanceInput) {
91 return StringUtils.isNotEmpty(propertyDefinition.getValue());
93 if (propertyDefinition instanceof InputDefinition) {
94 return StringUtils.isNotEmpty(propertyDefinition.getDefaultValue());
96 return StringUtils.isNotEmpty(propertyDefinition.getValue());
99 private void evaluatePropertyTypeForConstraintValidation(PropertyDefinition propertyDefinition) {
100 if (propertyDefinition == null || propertyDefinition.getType() == null || !dataTypeDefinitionCache.containsKey(
101 propertyDefinition.getType())) {
102 errorMessages.add("\nUnsupported datatype found for property " + getCompletePropertyName(propertyDefinition));
105 completeInputName = "";
106 completePropertyName = new StringBuilder();
107 if (propertyDefinition instanceof ComponentInstanceInput) {
108 setCompletePropertyName(propertyDefinition);
109 evaluateComplexTypeProperties(propertyDefinition);
112 if (propertyDefinition instanceof InputDefinition) {
113 completeInputName = propertyDefinition.getName();
114 propertyDefinition = getPropertyDefinitionObjectFromInputs(propertyDefinition);
116 if (propertyDefinition != null) {
117 List<PropertyConstraint> propertyConstraints =
118 dataTypeDefinitionCache.get(propertyDefinition.getType()).safeGetConstraints();
119 if (ToscaType.isPrimitiveType(propertyDefinition.getType())) {
120 propertyDefinition.setConstraints(org.openecomp.sdc.be.dao.utils.CollectionUtils.merge(propertyDefinition.safeGetConstraints(),
121 propertyConstraints.isEmpty() ? new ArrayList<>() : propertyConstraints));
122 evaluateConstraintsOnProperty(propertyDefinition);
123 } else if (ToscaType.isCollectionType(propertyDefinition.getType())) {
124 propertyDefinition.setConstraints(org.openecomp.sdc.be.dao.utils.CollectionUtils.merge(propertyDefinition.safeGetConstraints(),
125 propertyConstraints.isEmpty() ? new ArrayList<>() : propertyConstraints));
126 evaluateConstraintsOnProperty(propertyDefinition);
127 evaluateCollectionTypeProperties(propertyDefinition);
129 setCompletePropertyName(propertyDefinition);
130 evaluateComplexTypeProperties(propertyDefinition);
135 private void setCompletePropertyName(PropertyDefinition propertyDefinition) {
136 if (StringUtils.isNotBlank(propertyDefinition.getUniqueId())) {
137 completePropertyName.append(propertyDefinition.getUniqueId().substring(propertyDefinition.getUniqueId().lastIndexOf('.') + 1));
141 private void evaluateConstraintsOnProperty(PropertyDefinition propertyDefinition) {
142 ToscaType toscaType = ToscaType.isValidType(propertyDefinition.getType());
143 if (!isValueAToscaFunction(propertyDefinition) && CollectionUtils.isNotEmpty(propertyDefinition.getConstraints())) {
144 for (PropertyConstraint propertyConstraint : propertyDefinition.getConstraints()) {
146 propertyConstraint.initialize(toscaType, propertyDefinition.getSchema());
147 propertyConstraint.validate(toscaType, propertyDefinition.getSchema(), propertyDefinition.getValue());
148 } catch (ConstraintValueDoNotMatchPropertyTypeException | ConstraintViolationException exception) {
149 errorMessages.add(propertyConstraint.getErrorMessage(toscaType, exception, getCompletePropertyName(propertyDefinition)));
150 } catch (IllegalArgumentException ie) {
151 errorMessages.add(ie.getMessage());
154 } else if (!isValueAToscaFunction(propertyDefinition) && ToscaType.isPrimitiveType(propertyDefinition.getType())
155 && !propertyDefinition.isToscaFunction() && !toscaType.isValidValue(propertyDefinition.getValue())) {
156 errorMessages.add(String.format("Unsupported value provided for %s property supported value type is %s.",
157 getCompletePropertyName(propertyDefinition), toscaType.getType()));
161 private boolean isValueAToscaFunction(PropertyDefinition propertyDefinition) {
162 return propertyDefinition.getToscaFunction() != null || propertyDefinition.getValue() != null && (propertyDefinition.getValue().startsWith(IGNORE_PROPERTY_VALUE_START_WITH_INPUT) || propertyDefinition.getValue().startsWith(IGNORE_PROPERTY_VALUE_START_WITH_PROPERTY)
163 || propertyDefinition.getValue().startsWith(IGNORE_PROPERTY_VALUE_START_WITH_ATTRIBUTE));
166 private void checkAndEvaluatePrimitiveProperty(PropertyDefinition propertyDefinition, DataTypeDefinition dataTypeDefinition) {
167 if (ToscaType.isPrimitiveType(dataTypeDefinition.getName()) && CollectionUtils.isNotEmpty(dataTypeDefinition.getConstraints())) {
168 PropertyDefinition definition = new PropertyDefinition();
169 definition.setValue(propertyDefinition.getValue());
170 definition.setType(dataTypeDefinition.getName());
171 definition.setConstraints(dataTypeDefinition.getConstraints());
172 evaluateConstraintsOnProperty(propertyDefinition);
176 private void evaluateComplexTypeProperties(PropertyDefinition propertyDefinition) {
177 List<PropertyDefinition> propertyDefinitions = dataTypeDefinitionCache.get(propertyDefinition.getType()).getProperties();
179 Map<String, Object> valueMap = MapUtils
180 .emptyIfNull(ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {
182 if (CollectionUtils.isEmpty(propertyDefinitions)) {
183 checkAndEvaluatePrimitiveProperty(propertyDefinition, dataTypeDefinitionCache.get(propertyDefinition.getType()));
185 ListUtils.emptyIfNull(propertyDefinitions).forEach(prop -> evaluateRegularComplexType(propertyDefinition, prop, valueMap));
187 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
188 logger.debug(e.getMessage(), e);
189 errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
193 private void evaluateRegularComplexType(PropertyDefinition propertyDefinition, PropertyDefinition prop, Map<String, Object> valueMap) {
195 PropertyDefinition newPropertyWithValue;
196 if (valueMap.containsKey(prop.getName())) {
197 if (propertyDefinition.getSubPropertyToscaFunctions() != null) {
198 for (SubPropertyToscaFunction subPropertyToscaFunction : propertyDefinition.getSubPropertyToscaFunctions()) {
199 final List<String> path = subPropertyToscaFunction.getSubPropertyPath();
200 if (path.size() == 1) {
201 if (path.get(0).equals(prop.getName())) {
205 if (path.size() > 1) {
206 if (path.get(0).equals(propertyDefinition.getToscaSubPath()) && path.get(1).equals(prop.getName())) {
212 if (ToscaType.isPrimitiveType(prop.getType())) {
213 newPropertyWithValue = copyPropertyWithNewValue(prop, String.valueOf(valueMap.get(prop.getName())), prop.getName());
214 if (isPropertyToEvaluate(newPropertyWithValue)) {
215 evaluateConstraintsOnProperty(newPropertyWithValue);
217 } else if (ToscaType.isCollectionType(prop.getType())) {
218 newPropertyWithValue =
219 copyPropertyWithNewValue(prop,
220 objectMapper.writeValueAsString(valueMap.get(prop.getName())), prop.getName());
221 if (isPropertyToEvaluate(newPropertyWithValue)) {
222 evaluateCollectionTypeProperties(newPropertyWithValue);
225 newPropertyWithValue =
226 copyPropertyWithNewValue(prop,
227 objectMapper.writeValueAsString(valueMap.get(prop.getName())), prop.getName());
228 if (isPropertyToEvaluate(newPropertyWithValue)) {
229 evaluateComplexTypeProperties(newPropertyWithValue);
233 } catch (IOException | ConstraintValueDoNotMatchPropertyTypeException e) {
234 logger.error(e.getMessage(), e);
235 errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
239 private boolean isPropertyToEvaluate(PropertyDefinition propertyDefinition) throws ConstraintValueDoNotMatchPropertyTypeException {
240 if (Boolean.FALSE.equals(propertyDefinition.isRequired())) {
241 if (!ToscaType.isCollectionType(propertyDefinition.getType())) {
242 return StringUtils.isNotEmpty(propertyDefinition.getValue()) &&
243 !"null".equals(propertyDefinition.getValue());
244 } else if (ToscaType.LIST == ToscaType.isValidType(propertyDefinition.getType())) {
245 Collection<Object> list = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {
247 return CollectionUtils.isNotEmpty(list);
249 Map<String, Object> valueMap = MapUtils
250 .emptyIfNull(ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {
252 return MapUtils.isNotEmpty(valueMap);
259 private void evaluateCollectionTypeProperties(PropertyDefinition propertyDefinition) {
260 ToscaType toscaPropertyType = ToscaType.isValidType(propertyDefinition.getType());
262 if (isPropertyToEvaluate(propertyDefinition)) {
263 evaluateCollectionConstraints(propertyDefinition, toscaPropertyType);
265 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
266 logger.error(e.getMessage(), e);
267 errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
269 if (ToscaType.LIST == toscaPropertyType) {
270 evaluateListType(propertyDefinition);
271 } else if (ToscaType.MAP == toscaPropertyType) {
272 evaluateMapType(propertyDefinition);
276 private void evaluateCollectionConstraints(PropertyDefinition propertyDefinition, ToscaType toscaPropertyType) {
277 List<PropertyConstraint> constraintsList = propertyDefinition.getConstraints();
279 if (CollectionUtils.isEmpty(constraintsList)) {
282 ToscaType toscaPropertyType1;
283 if (null == toscaPropertyType) {
284 toscaPropertyType1 = ToscaType.isValidType(propertyDefinition.getType());
286 toscaPropertyType1 = toscaPropertyType;
288 constraintsList.stream()
289 .filter(this::isACollectionConstraint)
290 .forEach(propertyConstraint -> {
292 if (ToscaType.LIST == toscaPropertyType1) {
293 Collection<Object> list = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {
295 propertyConstraint.validate(list);
296 } else if (ToscaType.MAP == toscaPropertyType1) {
297 final Map<String, Object> map = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {
299 propertyConstraint.validate(map);
301 } catch (ConstraintValueDoNotMatchPropertyTypeException | ConstraintViolationException exception) {
302 errorMessages.add("\n" + propertyConstraint.getErrorMessage(toscaPropertyType1, exception,
303 getCompletePropertyName(propertyDefinition)));
308 private boolean isACollectionConstraint(PropertyConstraint constraint) {
309 if (constraint instanceof MaxLengthConstraint) {
312 if (constraint instanceof MinLengthConstraint) {
315 return constraint instanceof LengthConstraint;
318 private void evaluateListType(PropertyDefinition propertyDefinition) {
320 if (propertyDefinition.getSchemaType() == null) {
321 propertyDefinition.setSchema(createStringSchema());
323 Collection<Object> list = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {});
324 final Map<String, Object> map = new HashMap<>();
326 for (Object obj : list) {
327 map.put(String.valueOf(index),obj);
330 evaluateCollectionType(propertyDefinition, map);
331 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
332 logger.debug(e.getMessage(), e);
333 errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
337 private SchemaDefinition createStringSchema() {
338 final SchemaDefinition schemaDefinition = new SchemaDefinition();
339 final PropertyDefinition schemaStringProperty = new PropertyDefinition();
340 schemaStringProperty.setType(ToscaType.STRING.getType());
341 schemaDefinition.setProperty(schemaStringProperty);
342 return schemaDefinition;
345 private void evaluateMapType(final PropertyDefinition propertyDefinition) {
347 if (propertyDefinition.getSchemaType() == null) {
348 propertyDefinition.setSchema(createStringSchema());
350 final Map<String, Object> map = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {
352 evaluateCollectionType(propertyDefinition, map);
353 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
354 logger.debug(e.getMessage(), e);
355 errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
359 private void evaluateCollectionPrimitiveSchemaType(final PropertyDefinition propertyDefinition,
360 final String schemaType) throws JsonProcessingException {
361 if (propertyDefinition.getSchema() != null && propertyDefinition.getSchema().getProperty() instanceof PropertyDefinition) {
362 propertyDefinition.setConstraints(((PropertyDefinition) propertyDefinition.getSchema().getProperty()).getConstraints());
363 propertyDefinition.setValue(objectMapper.readValue(propertyDefinition.getValue(), String.class));
364 propertyDefinition.setType(schemaType);
365 evaluateConstraintsOnProperty(propertyDefinition);
369 private void evaluateCollectionType(final PropertyDefinition propertyDefinition, final Map<String, Object> valueMap) {
370 final String schemaType = propertyDefinition.getSchemaType();
371 for (String mapKey : valueMap.keySet()) {
372 final Object value = valueMap.get(mapKey);
374 final PropertyDefinition propertyCopyWithNewValue = copyPropertyWithNewValue(propertyDefinition,
375 objectMapper.writeValueAsString(value),mapKey);
376 propertyCopyWithNewValue.setToscaSubPath(mapKey);
377 if (isPropertyNotMappedAsInput(propertyCopyWithNewValue)) {
378 if (ToscaType.isPrimitiveType(schemaType)) {
379 evaluateCollectionPrimitiveSchemaType(propertyCopyWithNewValue, schemaType);
380 } else if (ToscaType.isCollectionType(schemaType)) {
381 propertyCopyWithNewValue.setType(schemaType);
382 propertyCopyWithNewValue.setSchemaType(propertyDefinition.getSchemaProperty().getSchemaType());
383 evaluateCollectionTypeProperties(propertyCopyWithNewValue);
385 propertyCopyWithNewValue.setType(schemaType);
386 completePropertyName.append(UNDERSCORE);
387 completePropertyName.append(propertyCopyWithNewValue.getName());
388 evaluateComplexTypeProperties(propertyCopyWithNewValue);
391 } catch (final Exception e) {
392 logger.debug(e.getMessage(), e);
393 errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
398 private String getCompletePropertyName(final PropertyDefinition propertyDefinition) {
399 if (StringUtils.isNotBlank(completeInputName)) {
400 return completeInputName;
403 final String propertyName = propertyDefinition == null ? "" : propertyDefinition.getName();
404 if (StringUtils.isNotBlank(completePropertyName)) {
405 return completePropertyName + UNDERSCORE + propertyName;
411 private PropertyDefinition copyPropertyWithNewValue(final PropertyDefinition propertyToCopy, final String value, final String key) {
412 final var propertyDefinition = new PropertyDefinition(propertyToCopy);
413 if (key != null && propertyToCopy.getSubPropertyToscaFunctions() != null) {
414 propertyToCopy.getSubPropertyToscaFunctions().forEach(subPropertyToscaFunction -> {
415 final List<String> subPropertyPath = subPropertyToscaFunction.getSubPropertyPath();
416 if (subPropertyPath.get((subPropertyPath.size() - 1)).equals(key)) {
417 propertyDefinition.setToscaFunction(subPropertyToscaFunction.getToscaFunction());
421 propertyDefinition.setValue(value);
422 return propertyDefinition;
425 private PropertyDefinition getPropertyDefinitionObjectFromInputs(PropertyDefinition property) {
426 InputDefinition inputDefinition = (InputDefinition) property;
427 PropertyDefinition propertyDefinition = null;
428 if (CollectionUtils.isEmpty(inputDefinition.getProperties()) || ToscaType.isPrimitiveType(inputDefinition.getProperties().get(0).getType())) {
429 propertyDefinition = new PropertyDefinition();
430 propertyDefinition.setType(inputDefinition.getType());
431 propertyDefinition.setValue(inputDefinition.getDefaultValue());
432 propertyDefinition.setName(inputDefinition.getName());
433 propertyDefinition.setConstraints(inputDefinition.getConstraints());
434 } else if (Objects.nonNull(inputDefinition.getInputPath())) {
435 propertyDefinition = evaluateComplexTypeInputs(inputDefinition);
436 propertyDefinition.setConstraints(inputDefinition.getConstraints());
438 return propertyDefinition;
441 private PropertyDefinition evaluateComplexTypeInputs(InputDefinition inputDefinition) {
442 Map<String, Object> inputMap = new HashMap<>();
443 PropertyDefinition propertyDefinition = new PropertyDefinition();
444 String[] inputPathArr = inputDefinition.getInputPath().split("#");
445 if (inputPathArr.length > 1) {
446 inputPathArr = ArrayUtils.remove(inputPathArr, 0);
449 Map<String, Object> presentMap = inputMap;
450 for (int i = 0; i < inputPathArr.length; i++) {
451 if (i == inputPathArr.length - 1) {
452 presentMap.computeIfAbsent(inputPathArr[i], k -> inputDefinition.getDefaultValue());
454 presentMap.computeIfAbsent(inputPathArr[i], k -> new HashMap<String, Object>());
455 presentMap = (Map<String, Object>) presentMap.get(inputPathArr[i]);
458 if (CollectionUtils.isNotEmpty(inputDefinition.getProperties())) {
459 propertyDefinition.setType(inputDefinition.getProperties().get(0).getType());
461 propertyDefinition.setName(inputDefinition.getName());
462 propertyDefinition.setValue(objectMapper.writeValueAsString(inputMap));
463 } catch (IOException e) {
464 logger.error(e.getMessage(), e);
465 errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, inputDefinition.getName()));
467 return propertyDefinition;
470 protected ResponseFormatManager getResponseFormatManager() {
471 return ResponseFormatManager.getInstance();