Improve handling 'empty'/null string in Service fields
[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 import org.yaml.snakeyaml.scanner.ScannerException;
83
84 @Service
85 public class PropertyConvertor {
86
87     private static final Logger log = Logger.getLogger(PropertyConvertor.class);
88
89     public Either<ToscaNodeType, ToscaError> convertProperties(Component component, ToscaNodeType toscaNodeType,
90                                                                Map<String, DataTypeDefinition> dataTypes) {
91         if (component instanceof Resource) {
92             Resource resource = (Resource) component;
93             List<PropertyDefinition> props = resource.getProperties();
94             if (props != null) {
95                 Map<String, ToscaProperty> properties = new HashMap<>();
96                 // take only the properties of this resource
97                 props.stream().filter(p -> p.getOwnerId() == null || p.getOwnerId().equals(component.getUniqueId())).forEach(property ->
98                     properties.put(property.getName(), convertProperty(dataTypes, property, PropertyType.PROPERTY))
99                 );
100                 if (!properties.isEmpty()) {
101                     toscaNodeType.setProperties(properties);
102                 }
103             }
104         }
105         return Either.left(toscaNodeType);
106     }
107
108     public ToscaProperty convertProperty(Map<String, DataTypeDefinition> dataTypes, PropertyDefinition property, PropertyType propertyType) {
109         ToscaProperty prop = new ToscaProperty();
110         log.trace("try to convert property {} from type {} with default value [{}]", property.getName(), property.getType(),
111             property.getDefaultValue());
112         SchemaDefinition schema = property.getSchema();
113         if (schema != null && schema.getProperty() != null && schema.getProperty().getType() != null && !schema.getProperty().getType().isEmpty()) {
114             final ToscaSchemaDefinition toscaSchemaDefinition = new ToscaSchemaDefinition();
115             toscaSchemaDefinition.setType(schema.getProperty().getType());
116             toscaSchemaDefinition.setDescription(schema.getProperty().getDescription());
117             prop.setEntry_schema(toscaSchemaDefinition);
118         }
119         String defaultValue = property.getDefaultValue();
120         if (Objects.isNull(defaultValue)) {
121             defaultValue = property.getValue();
122         }
123         Object convertedObj = convertToToscaObject(property, defaultValue, dataTypes, false);
124         if (convertedObj != null) {
125             prop.setDefaultp(convertedObj);
126         }
127         prop.setType(property.getType());
128         prop.setDescription(property.getDescription());
129         prop.setRequired(property.isRequired());
130         if (propertyType.equals(PropertyType.CAPABILITY)) {
131             prop.setStatus(property.getStatus());
132         }
133         prop.setMetadata(property.getMetadata());
134
135         if (CollectionUtils.isNotEmpty(property.getConstraints())) {
136             try {
137                 prop.setConstraints(convertConstraints(property.getConstraints(), property.getType(), property.getSchemaType()));
138             } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
139                 log.error(e.getMessage());
140             }
141         }
142         return prop;
143     }
144
145     private List<ToscaPropertyConstraint> convertConstraints(List<PropertyConstraint> constraints, String propertyType, String schemaType)
146         throws ConstraintValueDoNotMatchPropertyTypeException {
147         List<ToscaPropertyConstraint> convertedConstraints = new ArrayList<>();
148         for (PropertyConstraint constraint : constraints) {
149             if (constraint instanceof EqualConstraint) {
150                 EqualConstraint equalConstraint = ((EqualConstraint) constraint);
151
152                 if (doesTypeNeedConvertingToIntOrFloat(propertyType)) {
153                     equalConstraint.changeConstraintValueTypeTo(propertyType);
154                 }
155
156                 ToscaPropertyConstraintEqual prop = new ToscaPropertyConstraintEqual(equalConstraint.getEqual());
157                 convertedConstraints.add(prop);
158             }
159             if (constraint instanceof GreaterThanConstraint) {
160                 GreaterThanConstraint greaterThanConstraint = ((GreaterThanConstraint) constraint);
161
162                 if (doesTypeNeedConvertingToIntOrFloat(propertyType)) {
163                     greaterThanConstraint.changeConstraintValueTypeTo(propertyType);
164                 }
165
166                 ToscaPropertyConstraintGreaterThan prop = new ToscaPropertyConstraintGreaterThan(greaterThanConstraint.getGreaterThan());
167                 convertedConstraints.add(prop);
168             }
169             if (constraint instanceof GreaterOrEqualConstraint) {
170                 GreaterOrEqualConstraint greaterOrEqualConstraint = ((GreaterOrEqualConstraint) constraint);
171
172                 if (doesTypeNeedConvertingToIntOrFloat(propertyType)) {
173                     greaterOrEqualConstraint.changeConstraintValueTypeTo(propertyType);
174                 }
175
176                 ToscaPropertyConstraintGreaterOrEqual prop = new ToscaPropertyConstraintGreaterOrEqual(greaterOrEqualConstraint.getGreaterOrEqual());
177                 convertedConstraints.add(prop);
178             }
179             if (constraint instanceof LessThanConstraint) {
180                 LessThanConstraint lessThanConstraint = ((LessThanConstraint) constraint);
181
182                 if (doesTypeNeedConvertingToIntOrFloat(propertyType)) {
183                     lessThanConstraint.changeConstraintValueTypeTo(propertyType);
184                 }
185
186                 ToscaPropertyConstraintLessThan prop = new ToscaPropertyConstraintLessThan(lessThanConstraint.getLessThan());
187                 convertedConstraints.add(prop);
188             }
189             if (constraint instanceof LessOrEqualConstraint) {
190                 LessOrEqualConstraint lessOrEqualConstraint = ((LessOrEqualConstraint) constraint);
191
192                 if (doesTypeNeedConvertingToIntOrFloat(propertyType)) {
193                     lessOrEqualConstraint.changeConstraintValueTypeTo(propertyType);
194                 }
195
196                 ToscaPropertyConstraintLessOrEqual prop = new ToscaPropertyConstraintLessOrEqual(lessOrEqualConstraint.getLessOrEqual());
197                 convertedConstraints.add(prop);
198             }
199             if (constraint instanceof InRangeConstraint) {
200                 InRangeConstraint inRangeConstraint = (InRangeConstraint) constraint;
201
202                 if (doesTypeNeedConvertingToIntOrFloat(propertyType)) {
203                     inRangeConstraint.changeConstraintValueTypeTo(propertyType);
204                 }
205
206                 convertedConstraints.add(new ToscaPropertyConstraintInRange(inRangeConstraint.getInRange()));
207             }
208             if (constraint instanceof ValidValuesConstraint) {
209                 ValidValuesConstraint validValues = ((ValidValuesConstraint) constraint);
210
211                 if (isTypeMapOrList(propertyType) && doesTypeNeedConvertingToIntOrFloat(schemaType)) {
212                     validValues.changeConstraintValueTypeTo(schemaType);
213                 } else if (doesTypeNeedConvertingToIntOrFloat(propertyType)) {
214                     validValues.changeConstraintValueTypeTo(propertyType);
215                 }
216
217                 List prop = validValues.getValidValues();
218                 convertedConstraints.add(new ToscaPropertyConstraintValidValues(prop));
219             }
220             if (constraint instanceof LengthConstraint) {
221                 convertedConstraints.add(new ToscaPropertyConstraintLength(((LengthConstraint) constraint).getLength()));
222             }
223             if (constraint instanceof MinLengthConstraint) {
224                 convertedConstraints.add(new ToscaPropertyConstraintMinLength(((MinLengthConstraint) constraint).getMinLength()));
225             }
226             if (constraint instanceof MaxLengthConstraint) {
227                 convertedConstraints.add(new ToscaPropertyConstraintMaxLength(((MaxLengthConstraint) constraint).getMaxLength()));
228             }
229             if (constraint instanceof PatternConstraint) {
230                 convertedConstraints.add(new ToscaPropertyConstraintPattern(((PatternConstraint) constraint).getPattern()));
231             }
232         }
233         return convertedConstraints;
234     }
235
236     private boolean doesTypeNeedConvertingToIntOrFloat(String propertyType) {
237         return ToscaType.INTEGER.getType().equals(propertyType) || ToscaType.FLOAT.getType().equals(propertyType);
238     }
239
240     private boolean isTypeMapOrList (String type) {
241         return ToscaType.MAP.getType().equals(type) || ToscaType.LIST.getType().equals(type);
242     }
243
244     public Object convertToToscaObject(PropertyDataDefinition property, String value, Map<String, DataTypeDefinition> dataTypes,
245                                        boolean preserveEmptyValue) {
246         String propertyType = property.getType();
247         String innerType = property.getSchemaType();
248         log.trace("try to convert propertyType {} , value [{}], innerType {}", propertyType, value, innerType);
249         if (StringUtils.isEmpty(value)) {
250             value = DataTypePropertyConverter.getInstance().getDataTypePropertiesDefaultValuesRec(propertyType, dataTypes);
251             if (StringUtils.isEmpty(value)) {
252                 return null;
253             }
254         }
255         if (property.isToscaFunction() && property.getToscaFunction().getType() == ToscaFunctionType.YAML) {
256             return new Yaml().load(property.getValue());
257         }
258         try {
259             ToscaMapValueConverter mapConverterInst = ToscaMapValueConverter.getInstance();
260             ToscaValueConverter innerConverter = null;
261             boolean isScalar = true;
262             ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
263             if (type == null) {
264                 log.trace("isn't prederfined type, get from all data types");
265                 DataTypeDefinition dataTypeDefinition = dataTypes.get(propertyType);
266                 if (innerType == null) {
267                     innerType = propertyType;
268                 }
269                 if ((type = mapConverterInst.isScalarType(dataTypeDefinition)) != null) {
270                     log.trace("This is scalar type. get suitable converter for type {}", type);
271                     innerConverter = type.getValueConverter();
272                 } else {
273                     isScalar = false;
274                 }
275             } else {
276                 ToscaPropertyType typeIfScalar = ToscaPropertyType.getTypeIfScalar(type.getType());
277                 if (typeIfScalar == null) {
278                     isScalar = false;
279                 }
280                 innerConverter = type.getValueConverter();
281                 if (ToscaPropertyType.STRING == type && valueStartsWithNonJsonChar(value)) {
282                     return innerConverter.convertToToscaValue(value, innerType, dataTypes);
283                 }
284             }
285             JsonElement jsonElement = null;
286             StringReader reader = new StringReader(value);
287             JsonReader jsonReader = new JsonReader(reader);
288             jsonReader.setLenient(true);
289             jsonElement = JsonParser.parseReader(jsonReader);
290             if (value.equals("")) {
291                 return value;
292             }
293             if (jsonElement.isJsonPrimitive() && isScalar) {
294                 log.trace("It's well defined type. convert it");
295                 ToscaValueConverter converter = type.getValueConverter();
296                 return converter.convertToToscaValue(value, innerType, dataTypes);
297             }
298             log.trace("It's data type or inputs in primitive type. convert as map");
299             if (jsonElement.isJsonObject()) {
300                 JsonObject jsonObj = jsonElement.getAsJsonObject();
301                 // check if value is a get_input function
302                 if (jsonObj.entrySet().size() == 1 && jsonObj.has(ToscaFunctions.GET_INPUT.getFunctionName())) {
303                     Object obj = mapConverterInst.handleComplexJsonValue(jsonElement);
304                     log.debug("It's get_input function. obj={}", obj);
305                     return obj;
306                 }
307             }
308             Object convertedValue;
309             if (innerConverter != null && (ToscaPropertyType.MAP == type || ToscaPropertyType.LIST == type)) {
310                 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
311             } else if (isScalar) {
312                 // complex json for scalar type
313                 convertedValue = mapConverterInst.handleComplexJsonValue(jsonElement);
314             } else if (innerConverter != null) {
315                 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
316             } else {
317                 convertedValue = mapConverterInst
318                     .convertDataTypeToToscaObject(innerType, dataTypes, innerConverter, isScalar, jsonElement, preserveEmptyValue,
319                         property.isToscaFunction());
320             }
321             return convertedValue;
322
323         } catch (JsonParseException e) {
324             log.trace("{} not parsable as JSON. Convert as YAML instead", value);
325             try {
326                 return new Yaml().load(value);
327             } catch (ScannerException ex) {
328                 log.trace("{} not parsable as YAML. Returning as string", value);
329                 return value;
330             }
331
332         } catch (Exception e) {
333             log.debug("convertToToscaValue failed to parse json value :", e);
334             return null;
335         }
336     }
337
338     private boolean valueStartsWithNonJsonChar(String value) {
339         return value.startsWith("/") || value.startsWith(":");
340     }
341
342     public void convertAndAddValue(Map<String, DataTypeDefinition> dataTypes, Map<String, Object> props, PropertyDataDefinition prop,
343                                    Supplier<String> supplier) {
344         Object convertedValue = convertValue(dataTypes, prop, supplier);
345         if (!ToscaValueBaseConverter.isEmptyObjectValue(convertedValue)) {
346             props.put(prop.getName(), convertedValue);
347         }
348     }
349
350     private <T extends PropertyDataDefinition> Object convertValue(Map<String, DataTypeDefinition> dataTypes, T input, Supplier<String> supplier) {
351         return convertToToscaObject(input, supplier.get(), dataTypes, false);
352     }
353
354     public enum PropertyType {CAPABILITY, INPUT, PROPERTY}
355 }