cecb131caa91da497a0fdf969a96ad8a92015670
[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                 List<Object> range = new ArrayList<>();
206                 range.add(inRangeConstraint.getMin());
207                 range.add(inRangeConstraint.getMax());
208                 convertedConstraints.add(new ToscaPropertyConstraintInRange(range));
209             }
210             if (constraint instanceof ValidValuesConstraint) {
211                 ValidValuesConstraint validValues = ((ValidValuesConstraint) constraint);
212
213                 if (propertyType.equals(ToscaType.INTEGER.toString()) || propertyType.equals(ToscaType.FLOAT.toString())) {
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 doesPropertyTypeNeedConverted(String propertyType) {
237         return propertyType.equals(ToscaType.INTEGER.getType()) || propertyType.equals(ToscaType.FLOAT.getType());
238     }
239
240     public Object convertToToscaObject(PropertyDataDefinition property, String value, Map<String, DataTypeDefinition> dataTypes,
241                                        boolean preserveEmptyValue) {
242         String propertyType = property.getType();
243         String innerType = property.getSchemaType();
244         log.trace("try to convert propertyType {} , value [{}], innerType {}", propertyType, value, innerType);
245         if (StringUtils.isEmpty(value)) {
246             value = DataTypePropertyConverter.getInstance().getDataTypePropertiesDefaultValuesRec(propertyType, dataTypes);
247             if (StringUtils.isEmpty(value)) {
248                 return null;
249             }
250         }
251         if (property.isToscaFunction() && property.getToscaFunction().getType() == ToscaFunctionType.YAML) {
252             return new Yaml().load(property.getValue());
253         }
254         try {
255             ToscaMapValueConverter mapConverterInst = ToscaMapValueConverter.getInstance();
256             ToscaValueConverter innerConverter = null;
257             boolean isScalar = true;
258             ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
259             if (type == null) {
260                 log.trace("isn't prederfined type, get from all data types");
261                 DataTypeDefinition dataTypeDefinition = dataTypes.get(propertyType);
262                 if (innerType == null) {
263                     innerType = propertyType;
264                 }
265                 if ((type = mapConverterInst.isScalarType(dataTypeDefinition)) != null) {
266                     log.trace("This is scalar type. get suitable converter for type {}", type);
267                     innerConverter = type.getValueConverter();
268                 } else {
269                     isScalar = false;
270                 }
271             } else {
272                 ToscaPropertyType typeIfScalar = ToscaPropertyType.getTypeIfScalar(type.getType());
273                 if (typeIfScalar == null) {
274                     isScalar = false;
275                 }
276                 innerConverter = type.getValueConverter();
277                 if (ToscaPropertyType.STRING == type && valueStartsWithNonJsonChar(value)) {
278                     return innerConverter.convertToToscaValue(value, innerType, dataTypes);
279                 }
280             }
281             JsonElement jsonElement = null;
282             StringReader reader = new StringReader(value);
283             JsonReader jsonReader = new JsonReader(reader);
284             jsonReader.setLenient(true);
285             jsonElement = JsonParser.parseReader(jsonReader);
286             if (value.equals("")) {
287                 return value;
288             }
289             if (jsonElement.isJsonPrimitive() && isScalar) {
290                 log.trace("It's well defined type. convert it");
291                 ToscaValueConverter converter = type.getValueConverter();
292                 return converter.convertToToscaValue(value, innerType, dataTypes);
293             }
294             log.trace("It's data type or inputs in primitive type. convert as map");
295             if (jsonElement.isJsonObject()) {
296                 JsonObject jsonObj = jsonElement.getAsJsonObject();
297                 // check if value is a get_input function
298                 if (jsonObj.entrySet().size() == 1 && jsonObj.has(ToscaFunctions.GET_INPUT.getFunctionName())) {
299                     Object obj = mapConverterInst.handleComplexJsonValue(jsonElement);
300                     log.debug("It's get_input function. obj={}", obj);
301                     return obj;
302                 }
303             }
304             Object convertedValue;
305             if (innerConverter != null && (ToscaPropertyType.MAP == type || ToscaPropertyType.LIST == type)) {
306                 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
307             } else if (isScalar) {
308                 // complex json for scalar type
309                 convertedValue = mapConverterInst.handleComplexJsonValue(jsonElement);
310             } else if (innerConverter != null) {
311                 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
312             } else {
313                 convertedValue = mapConverterInst
314                     .convertDataTypeToToscaObject(innerType, dataTypes, innerConverter, isScalar, jsonElement, preserveEmptyValue);
315             }
316             return convertedValue;
317
318         } catch (JsonParseException e) {
319             log.trace("{} not parsable as JSON. Convert as YAML instead", value);
320             return new Yaml().load(value);
321         } catch (Exception e) {
322             log.debug("convertToToscaValue failed to parse json value :", e);
323             return null;
324         }
325     }
326
327     private boolean valueStartsWithNonJsonChar(String value) {
328         return value.startsWith("/") || value.startsWith(":");
329     }
330
331     public void convertAndAddValue(Map<String, DataTypeDefinition> dataTypes, Map<String, Object> props, PropertyDataDefinition prop,
332                                    Supplier<String> supplier) {
333         Object convertedValue = convertValue(dataTypes, prop, supplier);
334         if (!ToscaValueBaseConverter.isEmptyObjectValue(convertedValue)) {
335             props.put(prop.getName(), convertedValue);
336         }
337     }
338
339     private <T extends PropertyDataDefinition> Object convertValue(Map<String, DataTypeDefinition> dataTypes, T input, Supplier<String> supplier) {
340         return convertToToscaObject(input, supplier.get(), dataTypes, false);
341     }
342
343     public enum PropertyType {CAPABILITY, INPUT, PROPERTY}
344 }