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