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.model.ComponentInstanceInput;
38 import org.openecomp.sdc.be.model.DataTypeDefinition;
39 import org.openecomp.sdc.be.model.InputDefinition;
40 import org.openecomp.sdc.be.model.PropertyConstraint;
41 import org.openecomp.sdc.be.model.PropertyDefinition;
42 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
43 import org.openecomp.sdc.be.model.tosca.ToscaType;
44 import org.openecomp.sdc.be.model.tosca.constraints.ConstraintUtil;
45 import org.openecomp.sdc.be.model.tosca.constraints.ValidValuesConstraint;
46 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
47 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException;
48 import org.openecomp.sdc.exception.ResponseFormat;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 public class PropertyValueConstraintValidationUtil {
54 private static final String UNDERSCORE = "_";
55 private static final String VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY = "%nValue provided in invalid format for %s property";
56 private static final Logger logger = LoggerFactory.getLogger(PropertyValueConstraintValidationUtil.class);
57 private static final String IGNORE_PROPERTY_VALUE_START_WITH = "{\"get_input\":";
58 private Map<String, DataTypeDefinition> dataTypeDefinitionCache;
59 private final ObjectMapper objectMapper = new ObjectMapper();
60 private final List<String> errorMessages = new ArrayList<>();
61 private StringBuilder completePropertyName;
62 private String completeInputName;
64 public Either<Boolean, ResponseFormat> validatePropertyConstraints(final Collection<? extends PropertyDefinition> propertyDefinitionList,
65 final ApplicationDataTypeCache applicationDataTypeCache,
68 dataTypeDefinitionCache = applicationDataTypeCache.getAll(model).left().value();
69 CollectionUtils.emptyIfNull(propertyDefinitionList).stream()
70 .filter(this::isValuePresent)
71 .forEach(this::evaluatePropertyTypeForConstraintValidation);
72 if (CollectionUtils.isNotEmpty(errorMessages)) {
73 final String errorMsgAsString = String.join(",", errorMessages);
74 logger.debug("Properties with Invalid Data: {}", errorMsgAsString);
75 return Either.right(getResponseFormatManager().getResponseFormat(ActionStatus.INVALID_PROPERTY_VALUES, errorMsgAsString));
77 return Either.left(Boolean.TRUE);
80 private boolean isValuePresent(PropertyDefinition propertyDefinition) {
81 if (propertyDefinition instanceof ComponentInstanceInput) {
82 return StringUtils.isNotEmpty(propertyDefinition.getValue());
84 if (propertyDefinition instanceof InputDefinition) {
85 return StringUtils.isNotEmpty(propertyDefinition.getDefaultValue());
87 return StringUtils.isNotEmpty(propertyDefinition.getValue());
90 private void evaluatePropertyTypeForConstraintValidation(PropertyDefinition propertyDefinition) {
91 if (propertyDefinition == null || propertyDefinition.getType() == null || !dataTypeDefinitionCache.containsKey(propertyDefinition.getType())) {
92 errorMessages.add("\nUnsupported datatype found for property " + getCompletePropertyName(propertyDefinition));
95 completeInputName = "";
96 completePropertyName = new StringBuilder();
97 if (propertyDefinition instanceof ComponentInstanceInput) {
98 setCompletePropertyName(propertyDefinition);
99 evaluateComplexTypeProperties(propertyDefinition);
102 if (propertyDefinition instanceof InputDefinition) {
103 completeInputName = propertyDefinition.getName();
104 propertyDefinition = getPropertyDefinitionObjectFromInputs(propertyDefinition);
106 if (propertyDefinition != null) {
107 if (ToscaType.isPrimitiveType(propertyDefinition.getType())) {
108 propertyDefinition.setConstraints(org.openecomp.sdc.be.dao.utils.CollectionUtils.merge(propertyDefinition.safeGetConstraints(),
109 dataTypeDefinitionCache.get(propertyDefinition.getType()).safeGetConstraints()));
110 evaluateConstraintsOnProperty(propertyDefinition);
111 } else if (ToscaType.isCollectionType(propertyDefinition.getType())) {
112 propertyDefinition.setConstraints(org.openecomp.sdc.be.dao.utils.CollectionUtils.merge(propertyDefinition.safeGetConstraints(),
113 dataTypeDefinitionCache.get(propertyDefinition.getType()).safeGetConstraints()));
114 evaluateConstraintsOnProperty(propertyDefinition);
115 evaluateCollectionTypeProperties(propertyDefinition);
117 setCompletePropertyName(propertyDefinition);
118 evaluateComplexTypeProperties(propertyDefinition);
123 private void setCompletePropertyName(PropertyDefinition propertyDefinition) {
124 if (StringUtils.isNotBlank(propertyDefinition.getUniqueId())) {
125 completePropertyName.append(propertyDefinition.getUniqueId().substring(propertyDefinition.getUniqueId().lastIndexOf('.') + 1));
129 private void evaluateConstraintsOnProperty(PropertyDefinition propertyDefinition) {
130 ToscaType toscaType = ToscaType.isValidType(propertyDefinition.getType());
131 if (isPropertyNotMappedAsInput(propertyDefinition) && CollectionUtils.isNotEmpty(propertyDefinition.getConstraints())
132 && isValidValueConstraintPresent(propertyDefinition.getConstraints())) {
133 for (PropertyConstraint propertyConstraint : propertyDefinition.getConstraints()) {
135 propertyConstraint.initialize(toscaType);
136 propertyConstraint.validate(toscaType, propertyDefinition.getValue());
137 } catch (ConstraintValueDoNotMatchPropertyTypeException | ConstraintViolationException exception) {
138 errorMessages.add("\n" + propertyConstraint.getErrorMessage(toscaType, exception, getCompletePropertyName(propertyDefinition)));
141 } else if (isPropertyNotMappedAsInput(propertyDefinition) && ToscaType.isPrimitiveType(propertyDefinition.getType()) && !toscaType
142 .isValidValue(propertyDefinition.getValue())) {
143 errorMessages.add(String
144 .format("\nUnsupported value provided for %s property supported value " + "type is %s.", getCompletePropertyName(propertyDefinition),
145 toscaType.getType()));
149 private boolean isPropertyNotMappedAsInput(PropertyDefinition propertyDefinition) {
150 return !propertyDefinition.getValue().startsWith(IGNORE_PROPERTY_VALUE_START_WITH);
153 private void checkAndEvaluatePrimitiveProperty(PropertyDefinition propertyDefinition, DataTypeDefinition dataTypeDefinition) {
154 if (ToscaType.isPrimitiveType(dataTypeDefinition.getName()) && CollectionUtils.isNotEmpty(dataTypeDefinition.getConstraints())) {
155 PropertyDefinition definition = new PropertyDefinition();
156 definition.setValue(propertyDefinition.getValue());
157 definition.setType(dataTypeDefinition.getName());
158 definition.setConstraints(dataTypeDefinition.getConstraints());
159 evaluateConstraintsOnProperty(propertyDefinition);
163 private void evaluateComplexTypeProperties(PropertyDefinition propertyDefinition) {
164 List<PropertyDefinition> propertyDefinitions = dataTypeDefinitionCache.get(propertyDefinition.getType()).getProperties();
166 Map<String, Object> valueMap = MapUtils
167 .emptyIfNull(ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {
169 if (CollectionUtils.isEmpty(propertyDefinitions)) {
170 checkAndEvaluatePrimitiveProperty(propertyDefinition, dataTypeDefinitionCache.get(propertyDefinition.getType()));
172 ListUtils.emptyIfNull(propertyDefinitions).forEach(prop -> evaluateRegularComplexType(propertyDefinition, prop, valueMap));
174 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
175 logger.debug(e.getMessage(), e);
176 errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
180 private void evaluateRegularComplexType(PropertyDefinition propertyDefinition, PropertyDefinition prop, Map<String, Object> valueMap) {
182 if (valueMap.containsKey(prop.getName())) {
183 if (ToscaType.isPrimitiveType(prop.getType())) {
184 evaluateConstraintsOnProperty(copyPropertyWithNewValue(prop, String.valueOf(valueMap.get(prop.getName()))));
185 } else if (ToscaType.isCollectionType(prop.getType())) {
186 evaluateCollectionTypeProperties(copyPropertyWithNewValue(prop, objectMapper.writeValueAsString(valueMap.get(prop.getName()))));
188 completePropertyName.append(UNDERSCORE);
189 completePropertyName.append(prop.getName());
190 evaluateComplexTypeProperties(copyPropertyWithNewValue(prop, objectMapper.writeValueAsString(valueMap.get(prop.getName()))));
193 } catch (IOException e) {
194 logger.error(e.getMessage(), e);
195 errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
199 private void evaluateCollectionTypeProperties(PropertyDefinition propertyDefinition) {
200 ToscaType toscaPropertyType = ToscaType.isValidType(propertyDefinition.getType());
201 if (ToscaType.LIST == toscaPropertyType) {
202 evaluateListType(propertyDefinition);
203 } else if (ToscaType.MAP == toscaPropertyType) {
204 evaluateMapType(propertyDefinition);
208 private void evaluateListType(PropertyDefinition propertyDefinition) {
210 if (propertyDefinition.getSchemaType() == null) {
211 propertyDefinition.setSchema(createStringSchema());
213 List<Object> list = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {});
214 evaluateCollectionType(propertyDefinition, list);
215 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
216 logger.debug(e.getMessage(), e);
217 errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
221 private SchemaDefinition createStringSchema() {
222 final SchemaDefinition schemaDefinition = new SchemaDefinition();
223 final PropertyDefinition schemaStringProperty = new PropertyDefinition();
224 schemaStringProperty.setType(ToscaType.STRING.getType());
225 schemaDefinition.setProperty(schemaStringProperty);
226 return schemaDefinition;
229 private void evaluateMapType(final PropertyDefinition propertyDefinition) {
231 if (propertyDefinition.getSchemaType() == null) {
232 propertyDefinition.setSchema(createStringSchema());
234 final Map<String, Object> map = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {});
235 evaluateCollectionType(propertyDefinition, map.values());
236 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
237 logger.debug(e.getMessage(), e);
238 errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
242 private void evaluateCollectionPrimitiveSchemaType(final PropertyDefinition propertyDefinition,
243 final String schemaType) throws JsonProcessingException {
244 if (propertyDefinition.getSchema() != null && propertyDefinition.getSchema().getProperty() instanceof PropertyDefinition) {
245 propertyDefinition.setConstraints(((PropertyDefinition) propertyDefinition.getSchema().getProperty()).getConstraints());
246 propertyDefinition.setValue(objectMapper.readValue(propertyDefinition.getValue(), String.class));
247 propertyDefinition.setType(schemaType);
248 evaluateConstraintsOnProperty(propertyDefinition);
252 private void evaluateCollectionType(final PropertyDefinition propertyDefinition, final Collection<Object> valueList) {
253 final String schemaType = propertyDefinition.getSchemaType();
254 for (final Object value : valueList) {
256 final PropertyDefinition propertyCopyWithNewValue = copyPropertyWithNewValue(propertyDefinition, objectMapper.writeValueAsString(value));
257 if (ToscaType.isPrimitiveType(schemaType)) {
258 evaluateCollectionPrimitiveSchemaType(propertyCopyWithNewValue, schemaType);
259 } else if (ToscaType.isCollectionType(schemaType)) {
260 propertyCopyWithNewValue.setType(schemaType);
261 propertyCopyWithNewValue.setSchemaType(propertyDefinition.getSchemaProperty().getSchemaType());
262 evaluateCollectionTypeProperties(propertyCopyWithNewValue);
264 propertyCopyWithNewValue.setType(schemaType);
265 completePropertyName.append(UNDERSCORE);
266 completePropertyName.append(propertyCopyWithNewValue.getName());
267 evaluateComplexTypeProperties(propertyCopyWithNewValue);
269 } catch (final Exception e) {
270 logger.debug(e.getMessage(), e);
271 errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
276 private String getCompletePropertyName(final PropertyDefinition propertyDefinition) {
277 if (StringUtils.isNotBlank(completeInputName)) {
278 return completeInputName;
281 final String propertyName = propertyDefinition == null ? "" : propertyDefinition.getName();
282 if (StringUtils.isNotBlank(completePropertyName)) {
283 return completePropertyName + UNDERSCORE + propertyName;
289 private PropertyDefinition copyPropertyWithNewValue(final PropertyDefinition propertyToCopy, final String value) {
290 final var propertyDefinition = new PropertyDefinition(propertyToCopy);
291 propertyDefinition.setValue(value);
292 return propertyDefinition;
295 private boolean isValidValueConstraintPresent(List<PropertyConstraint> propertyConstraints) {
296 return propertyConstraints != null && propertyConstraints.stream().anyMatch(ValidValuesConstraint.class::isInstance);
299 private PropertyDefinition getPropertyDefinitionObjectFromInputs(PropertyDefinition property) {
300 InputDefinition inputDefinition = (InputDefinition) property;
301 PropertyDefinition propertyDefinition = null;
302 if (CollectionUtils.isEmpty(inputDefinition.getProperties()) || ToscaType.isPrimitiveType(inputDefinition.getProperties().get(0).getType())) {
303 propertyDefinition = new PropertyDefinition();
304 propertyDefinition.setType(inputDefinition.getType());
305 propertyDefinition.setValue(inputDefinition.getDefaultValue());
306 propertyDefinition.setName(inputDefinition.getName());
307 } else if (Objects.nonNull(inputDefinition.getInputPath())) {
308 propertyDefinition = evaluateComplexTypeInputs(inputDefinition);
310 return propertyDefinition;
313 private PropertyDefinition evaluateComplexTypeInputs(InputDefinition inputDefinition) {
314 Map<String, Object> inputMap = new HashMap<>();
315 PropertyDefinition propertyDefinition = new PropertyDefinition();
316 String[] inputPathArr = inputDefinition.getInputPath().split("#");
317 if (inputPathArr.length > 1) {
318 inputPathArr = ArrayUtils.remove(inputPathArr, 0);
321 Map<String, Object> presentMap = inputMap;
322 for (int i = 0; i < inputPathArr.length; i++) {
323 if (i == inputPathArr.length - 1) {
324 presentMap.computeIfAbsent(inputPathArr[i], k -> inputDefinition.getDefaultValue());
326 presentMap.computeIfAbsent(inputPathArr[i], k -> new HashMap<String, Object>());
327 presentMap = (Map<String, Object>) presentMap.get(inputPathArr[i]);
330 if (CollectionUtils.isNotEmpty(inputDefinition.getProperties())) {
331 propertyDefinition.setType(inputDefinition.getProperties().get(0).getType());
333 propertyDefinition.setName(inputDefinition.getName());
334 propertyDefinition.setValue(objectMapper.writeValueAsString(inputMap));
335 } catch (IOException e) {
336 logger.error(e.getMessage(), e);
337 errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, inputDefinition.getName()));
339 return propertyDefinition;
342 protected ResponseFormatManager getResponseFormatManager() {
343 return ResponseFormatManager.getInstance();