Fix 'in_range constraints missing from TOSCA template'-bug
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / tosca / PropertyConvertor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.be.tosca;
21
22 import com.google.gson.JsonElement;
23 import com.google.gson.JsonObject;
24 import com.google.gson.JsonParseException;
25 import com.google.gson.JsonParser;
26 import com.google.gson.stream.JsonReader;
27 import fj.data.Either;
28 import java.io.StringReader;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Objects;
34 import java.util.function.Supplier;
35 import org.apache.commons.collections.CollectionUtils;
36 import org.apache.commons.lang3.StringUtils;
37 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
38 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
39 import org.openecomp.sdc.be.datatypes.elements.ToscaFunctionType;
40 import org.openecomp.sdc.be.model.Component;
41 import org.openecomp.sdc.be.model.DataTypeDefinition;
42 import org.openecomp.sdc.be.model.PropertyConstraint;
43 import org.openecomp.sdc.be.model.PropertyDefinition;
44 import org.openecomp.sdc.be.model.Resource;
45 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
46 import org.openecomp.sdc.be.model.tosca.ToscaType;
47 import org.openecomp.sdc.be.model.tosca.constraints.EqualConstraint;
48 import org.openecomp.sdc.be.model.tosca.constraints.GreaterOrEqualConstraint;
49 import org.openecomp.sdc.be.model.tosca.constraints.GreaterThanConstraint;
50 import org.openecomp.sdc.be.model.tosca.constraints.InRangeConstraint;
51 import org.openecomp.sdc.be.model.tosca.constraints.LengthConstraint;
52 import org.openecomp.sdc.be.model.tosca.constraints.LessOrEqualConstraint;
53 import org.openecomp.sdc.be.model.tosca.constraints.LessThanConstraint;
54 import org.openecomp.sdc.be.model.tosca.constraints.MaxLengthConstraint;
55 import org.openecomp.sdc.be.model.tosca.constraints.MinLengthConstraint;
56 import org.openecomp.sdc.be.model.tosca.constraints.PatternConstraint;
57 import org.openecomp.sdc.be.model.tosca.constraints.ValidValuesConstraint;
58 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
59 import org.openecomp.sdc.be.model.tosca.converters.DataTypePropertyConverter;
60 import org.openecomp.sdc.be.model.tosca.converters.ToscaMapValueConverter;
61 import org.openecomp.sdc.be.model.tosca.converters.ToscaValueBaseConverter;
62 import org.openecomp.sdc.be.model.tosca.converters.ToscaValueConverter;
63 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
64 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
65 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraint;
66 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintEqual;
67 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintGreaterOrEqual;
68 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintGreaterThan;
69 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintInRange;
70 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintLength;
71 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintLessOrEqual;
72 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintLessThan;
73 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintMaxLength;
74 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintMinLength;
75 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintPattern;
76 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintValidValues;
77 import org.openecomp.sdc.be.tosca.model.ToscaSchemaDefinition;
78 import org.openecomp.sdc.common.log.wrappers.Logger;
79 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
80 import org.springframework.stereotype.Service;
81 import org.yaml.snakeyaml.Yaml;
82
83 @Service
84 public class PropertyConvertor {
85
86     private static final Logger log = Logger.getLogger(PropertyConvertor.class);
87
88     public Either<ToscaNodeType, ToscaError> convertProperties(Component component, ToscaNodeType toscaNodeType,
89                                                                Map<String, DataTypeDefinition> dataTypes) {
90         if (component instanceof Resource) {
91             Resource resource = (Resource) component;
92             List<PropertyDefinition> props = resource.getProperties();
93             if (props != null) {
94                 Map<String, ToscaProperty> properties = new HashMap<>();
95                 // take only the properties of this resource
96                 props.stream().filter(p -> p.getOwnerId() == null || p.getOwnerId().equals(component.getUniqueId())).forEach(property ->
97                     properties.put(property.getName(), convertProperty(dataTypes, property, PropertyType.PROPERTY))
98                 );
99                 if (!properties.isEmpty()) {
100                     toscaNodeType.setProperties(properties);
101                 }
102             }
103         }
104         return Either.left(toscaNodeType);
105     }
106
107     public ToscaProperty convertProperty(Map<String, DataTypeDefinition> dataTypes, PropertyDefinition property, PropertyType propertyType) {
108         ToscaProperty prop = new ToscaProperty();
109         log.trace("try to convert property {} from type {} with default value [{}]", property.getName(), property.getType(),
110             property.getDefaultValue());
111         SchemaDefinition schema = property.getSchema();
112         if (schema != null && schema.getProperty() != null && schema.getProperty().getType() != null && !schema.getProperty().getType().isEmpty()) {
113             final ToscaSchemaDefinition toscaSchemaDefinition = new ToscaSchemaDefinition();
114             toscaSchemaDefinition.setType(schema.getProperty().getType());
115             toscaSchemaDefinition.setDescription(schema.getProperty().getDescription());
116             prop.setEntry_schema(toscaSchemaDefinition);
117         }
118         String defaultValue = property.getDefaultValue();
119         if (Objects.isNull(defaultValue)) {
120             defaultValue = property.getValue();
121         }
122         Object convertedObj = convertToToscaObject(property, defaultValue, dataTypes, false);
123         if (convertedObj != null) {
124             prop.setDefaultp(convertedObj);
125         }
126         prop.setType(property.getType());
127         prop.setDescription(property.getDescription());
128         prop.setRequired(property.isRequired());
129         if (propertyType.equals(PropertyType.CAPABILITY)) {
130             prop.setStatus(property.getStatus());
131         }
132         prop.setMetadata(property.getMetadata());
133
134         if (CollectionUtils.isNotEmpty(property.getConstraints())) {
135             try {
136                 prop.setConstraints(convertConstraints(property.getConstraints(), property.getType()));
137             } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
138                 log.error(e.getMessage());
139             }
140         }
141         return prop;
142     }
143
144     private List<ToscaPropertyConstraint> convertConstraints(List<PropertyConstraint> constraints, String propertyType)
145         throws ConstraintValueDoNotMatchPropertyTypeException {
146         List<ToscaPropertyConstraint> convertedConstraints = new ArrayList<>();
147         for (PropertyConstraint constraint : constraints) {
148             if (constraint instanceof EqualConstraint) {
149                 EqualConstraint equalConstraint = ((EqualConstraint) constraint);
150
151                 if (doesPropertyTypeNeedConverted(propertyType)) {
152                     equalConstraint.changeConstraintValueTypeTo(propertyType);
153                 }
154
155                 ToscaPropertyConstraintEqual prop = new ToscaPropertyConstraintEqual(equalConstraint.getEqual());
156                 convertedConstraints.add(prop);
157             }
158             if (constraint instanceof GreaterThanConstraint) {
159                 GreaterThanConstraint greaterThanConstraint = ((GreaterThanConstraint) constraint);
160
161                 if (doesPropertyTypeNeedConverted(propertyType)) {
162                     greaterThanConstraint.changeConstraintValueTypeTo(propertyType);
163                 }
164
165                 ToscaPropertyConstraintGreaterThan prop = new ToscaPropertyConstraintGreaterThan(greaterThanConstraint.getGreaterThan());
166                 convertedConstraints.add(prop);
167             }
168             if (constraint instanceof GreaterOrEqualConstraint) {
169                 GreaterOrEqualConstraint greaterOrEqualConstraint = ((GreaterOrEqualConstraint) constraint);
170
171                 if (doesPropertyTypeNeedConverted(propertyType)) {
172                     greaterOrEqualConstraint.changeConstraintValueTypeTo(propertyType);
173                 }
174
175                 ToscaPropertyConstraintGreaterOrEqual prop = new ToscaPropertyConstraintGreaterOrEqual(greaterOrEqualConstraint.getGreaterOrEqual());
176                 convertedConstraints.add(prop);
177             }
178             if (constraint instanceof LessThanConstraint) {
179                 LessThanConstraint lessThanConstraint = ((LessThanConstraint) constraint);
180
181                 if (doesPropertyTypeNeedConverted(propertyType)) {
182                     lessThanConstraint.changeConstraintValueTypeTo(propertyType);
183                 }
184
185                 ToscaPropertyConstraintLessThan prop = new ToscaPropertyConstraintLessThan(lessThanConstraint.getLessThan());
186                 convertedConstraints.add(prop);
187             }
188             if (constraint instanceof LessOrEqualConstraint) {
189                 LessOrEqualConstraint lessOrEqualConstraint = ((LessOrEqualConstraint) constraint);
190
191                 if (doesPropertyTypeNeedConverted(propertyType)) {
192                     lessOrEqualConstraint.changeConstraintValueTypeTo(propertyType);
193                 }
194
195                 ToscaPropertyConstraintLessOrEqual prop = new ToscaPropertyConstraintLessOrEqual(lessOrEqualConstraint.getLessOrEqual());
196                 convertedConstraints.add(prop);
197             }
198             if (constraint instanceof InRangeConstraint) {
199                 InRangeConstraint inRangeConstraint = (InRangeConstraint) constraint;
200
201                 if (doesPropertyTypeNeedConverted(propertyType)) {
202                     inRangeConstraint.changeConstraintValueTypeTo(propertyType);
203                 }
204
205                 convertedConstraints.add(new ToscaPropertyConstraintInRange(inRangeConstraint.getInRange()));
206             }
207             if (constraint instanceof ValidValuesConstraint) {
208                 ValidValuesConstraint validValues = ((ValidValuesConstraint) constraint);
209
210                 if (doesPropertyTypeNeedConverted(propertyType)) {
211                     validValues.changeConstraintValueTypeTo(propertyType);
212                 }
213
214                 List prop = validValues.getValidValues();
215                 convertedConstraints.add(new ToscaPropertyConstraintValidValues(prop));
216             }
217             if (constraint instanceof LengthConstraint) {
218                 convertedConstraints.add(new ToscaPropertyConstraintLength(((LengthConstraint) constraint).getLength()));
219             }
220             if (constraint instanceof MinLengthConstraint) {
221                 convertedConstraints.add(new ToscaPropertyConstraintMinLength(((MinLengthConstraint) constraint).getMinLength()));
222             }
223             if (constraint instanceof MaxLengthConstraint) {
224                 convertedConstraints.add(new ToscaPropertyConstraintMaxLength(((MaxLengthConstraint) constraint).getMaxLength()));
225             }
226             if (constraint instanceof PatternConstraint) {
227                 convertedConstraints.add(new ToscaPropertyConstraintPattern(((PatternConstraint) constraint).getPattern()));
228             }
229         }
230         return convertedConstraints;
231     }
232
233     private boolean doesPropertyTypeNeedConverted(String propertyType) {
234         return ToscaType.INTEGER.getType().equals(propertyType) || ToscaType.FLOAT.getType().equals(propertyType);
235     }
236
237     public Object convertToToscaObject(PropertyDataDefinition property, String value, Map<String, DataTypeDefinition> dataTypes,
238                                        boolean preserveEmptyValue) {
239         String propertyType = property.getType();
240         String innerType = property.getSchemaType();
241         log.trace("try to convert propertyType {} , value [{}], innerType {}", propertyType, value, innerType);
242         if (StringUtils.isEmpty(value)) {
243             value = DataTypePropertyConverter.getInstance().getDataTypePropertiesDefaultValuesRec(propertyType, dataTypes);
244             if (StringUtils.isEmpty(value)) {
245                 return null;
246             }
247         }
248         if (property.isToscaFunction() && property.getToscaFunction().getType() == ToscaFunctionType.YAML) {
249             return new Yaml().load(property.getValue());
250         }
251         try {
252             ToscaMapValueConverter mapConverterInst = ToscaMapValueConverter.getInstance();
253             ToscaValueConverter innerConverter = null;
254             boolean isScalar = true;
255             ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
256             if (type == null) {
257                 log.trace("isn't prederfined type, get from all data types");
258                 DataTypeDefinition dataTypeDefinition = dataTypes.get(propertyType);
259                 if (innerType == null) {
260                     innerType = propertyType;
261                 }
262                 if ((type = mapConverterInst.isScalarType(dataTypeDefinition)) != null) {
263                     log.trace("This is scalar type. get suitable converter for type {}", type);
264                     innerConverter = type.getValueConverter();
265                 } else {
266                     isScalar = false;
267                 }
268             } else {
269                 ToscaPropertyType typeIfScalar = ToscaPropertyType.getTypeIfScalar(type.getType());
270                 if (typeIfScalar == null) {
271                     isScalar = false;
272                 }
273                 innerConverter = type.getValueConverter();
274                 if (ToscaPropertyType.STRING == type && valueStartsWithNonJsonChar(value)) {
275                     return innerConverter.convertToToscaValue(value, innerType, dataTypes);
276                 }
277             }
278             JsonElement jsonElement = null;
279             StringReader reader = new StringReader(value);
280             JsonReader jsonReader = new JsonReader(reader);
281             jsonReader.setLenient(true);
282             jsonElement = JsonParser.parseReader(jsonReader);
283             if (value.equals("")) {
284                 return value;
285             }
286             if (jsonElement.isJsonPrimitive() && isScalar) {
287                 log.trace("It's well defined type. convert it");
288                 ToscaValueConverter converter = type.getValueConverter();
289                 return converter.convertToToscaValue(value, innerType, dataTypes);
290             }
291             log.trace("It's data type or inputs in primitive type. convert as map");
292             if (jsonElement.isJsonObject()) {
293                 JsonObject jsonObj = jsonElement.getAsJsonObject();
294                 // check if value is a get_input function
295                 if (jsonObj.entrySet().size() == 1 && jsonObj.has(ToscaFunctions.GET_INPUT.getFunctionName())) {
296                     Object obj = mapConverterInst.handleComplexJsonValue(jsonElement);
297                     log.debug("It's get_input function. obj={}", obj);
298                     return obj;
299                 }
300             }
301             Object convertedValue;
302             if (innerConverter != null && (ToscaPropertyType.MAP == type || ToscaPropertyType.LIST == type)) {
303                 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
304             } else if (isScalar) {
305                 // complex json for scalar type
306                 convertedValue = mapConverterInst.handleComplexJsonValue(jsonElement);
307             } else if (innerConverter != null) {
308                 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
309             } else {
310                 convertedValue = mapConverterInst
311                     .convertDataTypeToToscaObject(innerType, dataTypes, innerConverter, isScalar, jsonElement, preserveEmptyValue);
312             }
313             return convertedValue;
314
315         } catch (JsonParseException e) {
316             log.trace("{} not parsable as JSON. Convert as YAML instead", value);
317             return new Yaml().load(value);
318         } catch (Exception e) {
319             log.debug("convertToToscaValue failed to parse json value :", e);
320             return null;
321         }
322     }
323
324     private boolean valueStartsWithNonJsonChar(String value) {
325         return value.startsWith("/") || value.startsWith(":");
326     }
327
328     public void convertAndAddValue(Map<String, DataTypeDefinition> dataTypes, Map<String, Object> props, PropertyDataDefinition prop,
329                                    Supplier<String> supplier) {
330         Object convertedValue = convertValue(dataTypes, prop, supplier);
331         if (!ToscaValueBaseConverter.isEmptyObjectValue(convertedValue)) {
332             props.put(prop.getName(), convertedValue);
333         }
334     }
335
336     private <T extends PropertyDataDefinition> Object convertValue(Map<String, DataTypeDefinition> dataTypes, T input, Supplier<String> supplier) {
337         return convertToToscaObject(input, supplier.get(), dataTypes, false);
338     }
339
340     public enum PropertyType {CAPABILITY, INPUT, PROPERTY}
341 }