Implement YAML Validator
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / datamodel / utils / PropertyValueConstraintValidationUtil.java
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.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;
55
56 public class PropertyValueConstraintValidationUtil {
57
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;
72
73     public Either<Boolean, ResponseFormat> validatePropertyConstraints(final Collection<? extends PropertyDefinition> propertyDefinitionList,
74                                                                        final ApplicationDataTypeCache applicationDataTypeCache,
75                                                                        final String model) {
76
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));
85         }
86         return Either.left(Boolean.TRUE);
87     }
88
89     private boolean isNonToscaFunctionValuePresent(PropertyDefinition propertyDefinition) {
90         if (isValueAToscaFunction(propertyDefinition)) {
91             return false;
92         }
93         if (propertyDefinition instanceof ComponentInstanceInput) {
94             return StringUtils.isNotEmpty(propertyDefinition.getValue());
95         }
96         if (propertyDefinition instanceof InputDefinition) {
97             return StringUtils.isNotEmpty(propertyDefinition.getDefaultValue());
98         }
99         return StringUtils.isNotEmpty(propertyDefinition.getValue() != null ? propertyDefinition.getValue() : propertyDefinition.getDefaultValue());
100     }
101
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));
106             return;
107         }
108         completeInputName = "";
109         completePropertyName = new StringBuilder();
110         if (propertyDefinition instanceof ComponentInstanceInput) {
111             setCompletePropertyName(propertyDefinition);
112             evaluateComplexTypeProperties(propertyDefinition);
113             return;
114         }
115         if (propertyDefinition instanceof InputDefinition) {
116             completeInputName = propertyDefinition.getName();
117             propertyDefinition = getPropertyDefinitionObjectFromInputs(propertyDefinition);
118         }
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);
131             } else {
132                 setCompletePropertyName(propertyDefinition);
133                 evaluateComplexTypeProperties(propertyDefinition);
134             }
135         }
136     }
137
138     private void setCompletePropertyName(PropertyDefinition propertyDefinition) {
139         if (StringUtils.isNotBlank(propertyDefinition.getUniqueId())) {
140             completePropertyName.append(propertyDefinition.getUniqueId().substring(propertyDefinition.getUniqueId().lastIndexOf('.') + 1));
141         }
142     }
143
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()) {
148                 try {
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());
155                 }
156             }
157         } else if (!isValueAToscaFunction(propertyDefinition) && ToscaType.isPrimitiveType(propertyDefinition.getType())
158                 && !propertyDefinition.isToscaFunction() && !toscaType.isValidValue(
159                     propertyDefinition.getValue() != null ? propertyDefinition.getValue() : propertyDefinition.getDefaultValue())) {
160             errorMessages.add(String.format("Unsupported value provided for %s property supported value type is %s.",
161                 getCompletePropertyName(propertyDefinition), toscaType.getType()));
162         }
163     }
164
165     private boolean isValueAToscaFunction(PropertyDefinition propertyDefinition) {
166         return (propertyDefinition.getToscaFunction() != null)  || (propertyDefinition.getValue() != null
167             && ((propertyDefinition.getValue().startsWith(IGNORE_PROPERTY_VALUE_START_WITH_INPUT) || propertyDefinition.getValue().startsWith(IGNORE_PROPERTY_VALUE_START_WITH_PROPERTY)
168             || propertyDefinition.getValue().startsWith(IGNORE_PROPERTY_VALUE_START_WITH_ATTRIBUTE) || propertyDefinition.getValue().startsWith(IGNORE_PROPERTY_VALUE_ATTRIBUTE)
169             || propertyDefinition.getValue().startsWith(IGNORE_PROPERTY_VALUE_PROPERTY) || propertyDefinition.getValue().startsWith(IGNORE_PROPERTY_VALUE_INPUT))));
170     }
171
172     private void checkAndEvaluatePrimitiveProperty(PropertyDefinition propertyDefinition, DataTypeDefinition dataTypeDefinition) {
173         if (ToscaType.isPrimitiveType(dataTypeDefinition.getName()) && CollectionUtils.isNotEmpty(dataTypeDefinition.getConstraints())) {
174             PropertyDefinition definition = new PropertyDefinition();
175             definition.setValue(propertyDefinition.getValue());
176             definition.setType(dataTypeDefinition.getName());
177             definition.setConstraints(dataTypeDefinition.getConstraints());
178             evaluateConstraintsOnProperty(propertyDefinition);
179         }
180     }
181
182     private void evaluateComplexTypeProperties(PropertyDefinition propertyDefinition) {
183         List<PropertyDefinition> propertyDefinitions = dataTypeDefinitionCache.get(propertyDefinition.getType()).getProperties();
184         try {
185             Map<String, Object> valueMap = MapUtils
186                 .emptyIfNull(ConstraintUtil.parseToCollection(null != propertyDefinition.getValue() ?
187                     propertyDefinition.getValue() : propertyDefinition.getDefaultValue(), new TypeReference<>() {
188                 }));
189             if (CollectionUtils.isEmpty(propertyDefinitions)) {
190                 checkAndEvaluatePrimitiveProperty(propertyDefinition, dataTypeDefinitionCache.get(propertyDefinition.getType()));
191             } else {
192                 ListUtils.emptyIfNull(propertyDefinitions).forEach(prop -> evaluateRegularComplexType(propertyDefinition, prop, valueMap));
193             }
194         } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
195             logger.debug(e.getMessage(), e);
196             errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
197         }
198     }
199
200     private void evaluateRegularComplexType(PropertyDefinition propertyDefinition, PropertyDefinition prop, Map<String, Object> valueMap) {
201         try {
202             PropertyDefinition newPropertyWithValue;
203             if (valueMap.containsKey(prop.getName())) {
204                 if (propertyDefinition.getSubPropertyToscaFunctions() != null) {
205                     for (SubPropertyToscaFunction subPropertyToscaFunction : propertyDefinition.getSubPropertyToscaFunctions()) {
206                         final List<String> path = subPropertyToscaFunction.getSubPropertyPath();
207                         if (path.size() == 1 && path.get(0).equals(prop.getName())) {
208                             return;
209                         }
210                         if (path.size() > 1) {
211                             if (path.get(0).equals(propertyDefinition.getToscaSubPath()) && path.get(1).equals(prop.getName())) {
212                                 return;
213                             }
214                         }
215                     }
216                 }
217                 if (ToscaType.isPrimitiveType(prop.getType())) {
218                     newPropertyWithValue = copyPropertyWithNewValue(prop, String.valueOf(valueMap.get(prop.getName())), prop.getName());
219                     if (isPropertyToEvaluate(newPropertyWithValue)) {
220                         evaluateConstraintsOnProperty(newPropertyWithValue);
221                     }
222                 } else if (ToscaType.isCollectionType(prop.getType())) {
223                     newPropertyWithValue =
224                         copyPropertyWithNewValue(prop,
225                             objectMapper.writeValueAsString(valueMap.get(prop.getName())), prop.getName());
226                     if (isPropertyToEvaluate(newPropertyWithValue)) {
227                         evaluateCollectionTypeProperties(newPropertyWithValue);
228                     }
229                 } else {
230                     newPropertyWithValue =
231                         copyPropertyWithNewValue(prop,
232                             objectMapper.writeValueAsString(valueMap.get(prop.getName())), prop.getName());
233                     if (isPropertyToEvaluate(newPropertyWithValue)) {
234                         evaluateComplexTypeProperties(newPropertyWithValue);
235                     }
236                 }
237             }
238         } catch (IOException | ConstraintValueDoNotMatchPropertyTypeException e) {
239             logger.error(e.getMessage(), e);
240             errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
241         }
242     }
243
244     private boolean isPropertyToEvaluate(PropertyDefinition propertyDefinition) throws ConstraintValueDoNotMatchPropertyTypeException {
245         if (Boolean.FALSE.equals(propertyDefinition.isRequired())) {
246             if (!ToscaType.isCollectionType(propertyDefinition.getType())) {
247                 return StringUtils.isNotEmpty(propertyDefinition.getValue()) &&
248                     !"null".equals(propertyDefinition.getValue());
249             } else if (ToscaType.LIST == ToscaType.isValidType(propertyDefinition.getType())) {
250                 Collection<?> list = ConstraintUtil.parseToCollection(null != propertyDefinition.getValue() ?
251                     propertyDefinition.getValue() : propertyDefinition.getDefaultValue(), new TypeReference<List<?>>() {
252                 });
253                 return CollectionUtils.isNotEmpty(list);
254             } else {
255                 Map<String, Object> valueMap = MapUtils
256                     .emptyIfNull(ConstraintUtil.parseToCollection(null != propertyDefinition.getValue() ?
257                         propertyDefinition.getValue() : propertyDefinition.getDefaultValue(), new TypeReference<>() {
258                     }));
259                 return MapUtils.isNotEmpty(valueMap);
260             }
261         } else {
262             return true;
263         }
264     }
265
266     private void evaluateCollectionTypeProperties(PropertyDefinition propertyDefinition) {
267         ToscaType toscaPropertyType = ToscaType.isValidType(propertyDefinition.getType());
268         try {
269             if (isPropertyToEvaluate(propertyDefinition)) {
270                 evaluateCollectionConstraints(propertyDefinition, toscaPropertyType);
271             }
272         } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
273             logger.error(e.getMessage(), e);
274             errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
275         }
276         if (ToscaType.LIST == toscaPropertyType) {
277             evaluateListType(propertyDefinition);
278         } else if (ToscaType.MAP == toscaPropertyType) {
279             evaluateMapType(propertyDefinition);
280         }
281     }
282
283     private void evaluateCollectionConstraints(PropertyDefinition propertyDefinition, ToscaType toscaPropertyType) {
284         List<PropertyConstraint> constraintsList = propertyDefinition.getConstraints();
285
286         if (CollectionUtils.isEmpty(constraintsList)) {
287             return;
288         }
289         ToscaType toscaPropertyType1;
290         if (null == toscaPropertyType) {
291             toscaPropertyType1 = ToscaType.isValidType(propertyDefinition.getType());
292         } else {
293             toscaPropertyType1 = toscaPropertyType;
294         }
295         constraintsList.stream()
296             .filter(this::isACollectionConstraint)
297             .forEach(propertyConstraint -> {
298                 try {
299                     if (ToscaType.LIST == toscaPropertyType1) {
300                         Collection<Object> list = ConstraintUtil.parseToCollection(null != propertyDefinition.getValue() ? propertyDefinition.getValue() : propertyDefinition.getDefaultValue(), new TypeReference<>() {
301                         });
302                         propertyConstraint.validate(list);
303                     } else if (ToscaType.MAP == toscaPropertyType1) {
304                         final Map<String, Object> map = ConstraintUtil.parseToCollection(null != propertyDefinition.getValue() ? propertyDefinition.getValue() : propertyDefinition.getDefaultValue(), new TypeReference<>() {
305                         });
306                         propertyConstraint.validate(map);
307                     }
308                 } catch (ConstraintValueDoNotMatchPropertyTypeException | ConstraintViolationException exception) {
309                     errorMessages.add("\n" + propertyConstraint.getErrorMessage(toscaPropertyType1, exception,
310                         getCompletePropertyName(propertyDefinition)));
311                 }
312             });
313     }
314
315     private boolean isACollectionConstraint(PropertyConstraint constraint) {
316         if (constraint instanceof MaxLengthConstraint) {
317             return true;
318         }
319         if (constraint instanceof MinLengthConstraint) {
320             return true;
321         }
322         return constraint instanceof LengthConstraint;
323     }
324
325     private void evaluateListType(PropertyDefinition propertyDefinition) {
326         try {
327             if (propertyDefinition.getSchemaType() == null) {
328                 propertyDefinition.setSchema(createStringSchema());
329             }
330             Collection<?> list = ConstraintUtil.parseToCollection(null != propertyDefinition.getValue() ?
331                 propertyDefinition.getValue() : propertyDefinition.getDefaultValue(), new TypeReference<List<?>>() {});
332             final Map<String, Object> map = new HashMap<>();
333             int index = 0;
334             for (Object obj : list) {
335                 map.put(String.valueOf(index),obj);
336                 index++;
337             }
338             evaluateCollectionType(propertyDefinition, map);
339         } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
340             logger.debug(e.getMessage(), e);
341             errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
342         }
343     }
344
345     private SchemaDefinition createStringSchema() {
346         final SchemaDefinition schemaDefinition = new SchemaDefinition();
347         final PropertyDefinition schemaStringProperty = new PropertyDefinition();
348         schemaStringProperty.setType(ToscaType.STRING.getType());
349         schemaDefinition.setProperty(schemaStringProperty);
350         return schemaDefinition;
351     }
352
353     private void evaluateMapType(final PropertyDefinition propertyDefinition) {
354         try {
355             if (propertyDefinition.getSchemaType() == null) {
356                 propertyDefinition.setSchema(createStringSchema());
357             }
358             final Map<String, Object> map = ConstraintUtil.parseToCollection(null != propertyDefinition.getValue() ?
359                 propertyDefinition.getValue() : propertyDefinition.getDefaultValue(), new TypeReference<>() {
360             });
361             evaluateCollectionType(propertyDefinition, map);
362         } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
363             logger.debug(e.getMessage(), e);
364             errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
365         }
366     }
367
368     private void evaluateCollectionPrimitiveSchemaType(final PropertyDefinition propertyDefinition,
369                                                        final String schemaType) throws JsonProcessingException {
370         if (propertyDefinition.getSchema() != null && propertyDefinition.getSchema().getProperty() instanceof PropertyDefinition) {
371             propertyDefinition.setConstraints(((PropertyDefinition) propertyDefinition.getSchema().getProperty()).getConstraints());
372             propertyDefinition.setValue(objectMapper.readValue(propertyDefinition.getValue(), String.class));
373             propertyDefinition.setType(schemaType);
374             evaluateConstraintsOnProperty(propertyDefinition);
375         }
376     }
377
378     private void evaluateCollectionType(final PropertyDefinition propertyDefinition, final Map<String, Object> valueMap) {
379         final String schemaType = propertyDefinition.getSchemaType();
380         for (String mapKey : valueMap.keySet()) {
381             final Object value = valueMap.get(mapKey);
382             try {
383                 final PropertyDefinition propertyCopyWithNewValue = copyPropertyWithNewValue(propertyDefinition,
384                     objectMapper.writeValueAsString(value),mapKey);
385                 propertyCopyWithNewValue.setToscaSubPath(mapKey);
386                 if (!isValueAToscaFunction(propertyCopyWithNewValue)) {
387                     if (ToscaType.isPrimitiveType(schemaType)) {
388                         evaluateCollectionPrimitiveSchemaType(propertyCopyWithNewValue, schemaType);
389                     } else if (ToscaType.isCollectionType(schemaType)) {
390                         propertyCopyWithNewValue.setType(schemaType);
391                         propertyCopyWithNewValue.setSchemaType(propertyDefinition.getSchemaProperty().getSchemaType());
392                         evaluateCollectionTypeProperties(propertyCopyWithNewValue);
393                     } else {
394                         propertyCopyWithNewValue.setType(schemaType);
395                         completePropertyName.append(UNDERSCORE);
396                         completePropertyName.append(propertyCopyWithNewValue.getName());
397                         evaluateComplexTypeProperties(propertyCopyWithNewValue);
398                     }
399                 }
400             } catch (final Exception e) {
401                 logger.debug(e.getMessage(), e);
402                 errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition)));
403             }
404         }
405     }
406
407     private String getCompletePropertyName(final PropertyDefinition propertyDefinition) {
408         if (StringUtils.isNotBlank(completeInputName)) {
409             return completeInputName;
410         }
411
412         final String propertyName = propertyDefinition == null ? "" : propertyDefinition.getName();
413         if (StringUtils.isNotBlank(completePropertyName)) {
414             return completePropertyName + UNDERSCORE + propertyName;
415         }
416
417         return propertyName;
418     }
419
420     private PropertyDefinition copyPropertyWithNewValue(final PropertyDefinition propertyToCopy, final String value, final String key) {
421         final var propertyDefinition = new PropertyDefinition(propertyToCopy);
422         if (key != null && propertyToCopy.getSubPropertyToscaFunctions() != null) {
423             propertyToCopy.getSubPropertyToscaFunctions().forEach(subPropertyToscaFunction -> {
424                 final List<String> subPropertyPath = subPropertyToscaFunction.getSubPropertyPath();
425                 if (subPropertyPath.get((subPropertyPath.size() - 1)).equals(key)) {
426                     propertyDefinition.setToscaFunction(subPropertyToscaFunction.getToscaFunction());
427                 }
428             });
429         }
430         propertyDefinition.setValue(value);
431         return propertyDefinition;
432     }
433
434     private PropertyDefinition getPropertyDefinitionObjectFromInputs(PropertyDefinition property) {
435         InputDefinition inputDefinition = (InputDefinition) property;
436         PropertyDefinition propertyDefinition = null;
437         if (CollectionUtils.isEmpty(inputDefinition.getProperties()) || ToscaType.isPrimitiveType(inputDefinition.getProperties().get(0).getType())) {
438             propertyDefinition = new PropertyDefinition();
439             propertyDefinition.setType(inputDefinition.getType());
440             propertyDefinition.setValue(inputDefinition.getDefaultValue());
441             propertyDefinition.setName(inputDefinition.getName());
442             propertyDefinition.setConstraints(inputDefinition.getConstraints());
443         } else if (Objects.nonNull(inputDefinition.getInputPath())) {
444             propertyDefinition = evaluateComplexTypeInputs(inputDefinition);
445             propertyDefinition.setConstraints(inputDefinition.getConstraints());
446         }
447         return propertyDefinition;
448     }
449
450     private PropertyDefinition evaluateComplexTypeInputs(InputDefinition inputDefinition) {
451         Map<String, Object> inputMap = new HashMap<>();
452         PropertyDefinition propertyDefinition = new PropertyDefinition();
453         String[] inputPathArr = inputDefinition.getInputPath().split("#");
454         if (inputPathArr.length > 1) {
455             inputPathArr = ArrayUtils.remove(inputPathArr, 0);
456         }
457         try {
458             Map<String, Object> presentMap = inputMap;
459             for (int i = 0; i < inputPathArr.length; i++) {
460                 if (i == inputPathArr.length - 1) {
461                     presentMap.computeIfAbsent(inputPathArr[i], k -> inputDefinition.getDefaultValue());
462                 } else {
463                     presentMap.computeIfAbsent(inputPathArr[i], k -> new HashMap<String, Object>());
464                     presentMap = (Map<String, Object>) presentMap.get(inputPathArr[i]);
465                 }
466             }
467             if (CollectionUtils.isNotEmpty(inputDefinition.getProperties())) {
468                 propertyDefinition.setType(inputDefinition.getProperties().get(0).getType());
469             }
470             propertyDefinition.setName(inputDefinition.getName());
471             propertyDefinition.setValue(objectMapper.writeValueAsString(inputMap));
472         } catch (IOException e) {
473             logger.error(e.getMessage(), e);
474             errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, inputDefinition.getName()));
475         }
476         return propertyDefinition;
477     }
478
479     protected ResponseFormatManager getResponseFormatManager() {
480         return ResponseFormatManager.getInstance();
481     }
482 }