2d3ef8548822f079734f7c10970af840be138574
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.openecomp.sdc.be.datamodel.utils;
17
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;
27 import java.util.Map;
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;
51
52 public class PropertyValueConstraintValidationUtil {
53
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;
63
64     public Either<Boolean, ResponseFormat> validatePropertyConstraints(final Collection<? extends PropertyDefinition> propertyDefinitionList,
65                                                                        final ApplicationDataTypeCache applicationDataTypeCache,
66                                                                        final String model) {
67
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));
76         }
77         return Either.left(Boolean.TRUE);
78     }
79
80     private boolean isValuePresent(PropertyDefinition propertyDefinition) {
81         if (propertyDefinition instanceof ComponentInstanceInput) {
82             return StringUtils.isNotEmpty(propertyDefinition.getValue());
83         }
84         if (propertyDefinition instanceof InputDefinition) {
85             return StringUtils.isNotEmpty(propertyDefinition.getDefaultValue());
86         }
87         return StringUtils.isNotEmpty(propertyDefinition.getValue());
88     }
89
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));
93             return;
94         }
95         completeInputName = "";
96         completePropertyName = new StringBuilder();
97         if (propertyDefinition instanceof ComponentInstanceInput) {
98             setCompletePropertyName(propertyDefinition);
99             evaluateComplexTypeProperties(propertyDefinition);
100             return;
101         }
102         if (propertyDefinition instanceof InputDefinition) {
103             completeInputName = propertyDefinition.getName();
104             propertyDefinition = getPropertyDefinitionObjectFromInputs(propertyDefinition);
105         }
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);
116             } else {
117                 setCompletePropertyName(propertyDefinition);
118                 evaluateComplexTypeProperties(propertyDefinition);
119             }
120         }
121     }
122
123     private void setCompletePropertyName(PropertyDefinition propertyDefinition) {
124         if (StringUtils.isNotBlank(propertyDefinition.getUniqueId())) {
125             completePropertyName.append(propertyDefinition.getUniqueId().substring(propertyDefinition.getUniqueId().lastIndexOf('.') + 1));
126         }
127     }
128
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()) {
134                 try {
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)));
139                 }
140             }
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()));
146         }
147     }
148
149     private boolean isPropertyNotMappedAsInput(PropertyDefinition propertyDefinition) {
150         return !propertyDefinition.getValue().startsWith(IGNORE_PROPERTY_VALUE_START_WITH);
151     }
152
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);
160         }
161     }
162
163     private void evaluateComplexTypeProperties(PropertyDefinition propertyDefinition) {
164         List<PropertyDefinition> propertyDefinitions = dataTypeDefinitionCache.get(propertyDefinition.getType()).getProperties();
165         try {
166             Map<String, Object> valueMap = MapUtils
167                 .emptyIfNull(ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {
168                 }));
169             if (CollectionUtils.isEmpty(propertyDefinitions)) {
170                 checkAndEvaluatePrimitiveProperty(propertyDefinition, dataTypeDefinitionCache.get(propertyDefinition.getType()));
171             } else {
172                 ListUtils.emptyIfNull(propertyDefinitions).forEach(prop -> evaluateRegularComplexType(propertyDefinition, prop, valueMap));
173             }
174         } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
175             logger.debug(e.getMessage(), e);
176             errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
177         }
178     }
179
180     private void evaluateRegularComplexType(PropertyDefinition propertyDefinition, PropertyDefinition prop, Map<String, Object> valueMap) {
181         try {
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()))));
187                 } else {
188                     completePropertyName.append(UNDERSCORE);
189                     completePropertyName.append(prop.getName());
190                     evaluateComplexTypeProperties(copyPropertyWithNewValue(prop, objectMapper.writeValueAsString(valueMap.get(prop.getName()))));
191                 }
192             }
193         } catch (IOException e) {
194             logger.error(e.getMessage(), e);
195             errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
196         }
197     }
198
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);
205         }
206     }
207
208     private void evaluateListType(PropertyDefinition propertyDefinition) {
209         try {
210             if (propertyDefinition.getSchemaType() == null) {
211                 propertyDefinition.setSchema(createStringSchema());
212             }
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)));
218         }
219     }
220
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;
227     }
228
229     private void evaluateMapType(final PropertyDefinition propertyDefinition) {
230         try {
231             if (propertyDefinition.getSchemaType() == null) {
232                 propertyDefinition.setSchema(createStringSchema());
233             }
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)));
239         }
240     }
241
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);
249         }
250     }
251
252     private void evaluateCollectionType(final PropertyDefinition propertyDefinition, final Collection<Object> valueList) {
253         final String schemaType = propertyDefinition.getSchemaType();
254         for (final Object value : valueList) {
255             try {
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);
263                 } else {
264                     propertyCopyWithNewValue.setType(schemaType);
265                     completePropertyName.append(UNDERSCORE);
266                     completePropertyName.append(propertyCopyWithNewValue.getName());
267                     evaluateComplexTypeProperties(propertyCopyWithNewValue);
268                 }
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)));
272             }
273         }
274     }
275
276     private String getCompletePropertyName(final PropertyDefinition propertyDefinition) {
277         if (StringUtils.isNotBlank(completeInputName)) {
278             return completeInputName;
279         }
280
281         final String propertyName = propertyDefinition == null ? "" : propertyDefinition.getName();
282         if (StringUtils.isNotBlank(completePropertyName)) {
283             return completePropertyName + UNDERSCORE + propertyName;
284         }
285
286         return propertyName;
287     }
288
289     private PropertyDefinition copyPropertyWithNewValue(final PropertyDefinition propertyToCopy, final String value) {
290         final var propertyDefinition = new PropertyDefinition(propertyToCopy);
291         propertyDefinition.setValue(value);
292         return propertyDefinition;
293     }
294
295     private boolean isValidValueConstraintPresent(List<PropertyConstraint> propertyConstraints) {
296         return propertyConstraints != null && propertyConstraints.stream().anyMatch(ValidValuesConstraint.class::isInstance);
297     }
298
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);
309         }
310         return propertyDefinition;
311     }
312
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);
319         }
320         try {
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());
325                 } else {
326                     presentMap.computeIfAbsent(inputPathArr[i], k -> new HashMap<String, Object>());
327                     presentMap = (Map<String, Object>) presentMap.get(inputPathArr[i]);
328                 }
329             }
330             if (CollectionUtils.isNotEmpty(inputDefinition.getProperties())) {
331                 propertyDefinition.setType(inputDefinition.getProperties().get(0).getType());
332             }
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()));
338         }
339         return propertyDefinition;
340     }
341
342     protected ResponseFormatManager getResponseFormatManager() {
343         return ResponseFormatManager.getInstance();
344     }
345 }