90a5161f7a312215c2294ab6d26c4fd27c98082c
[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.constraints.EqualConstraint;
47 import org.openecomp.sdc.be.model.tosca.constraints.GreaterOrEqualConstraint;
48 import org.openecomp.sdc.be.model.tosca.constraints.GreaterThanConstraint;
49 import org.openecomp.sdc.be.model.tosca.constraints.InRangeConstraint;
50 import org.openecomp.sdc.be.model.tosca.constraints.LengthConstraint;
51 import org.openecomp.sdc.be.model.tosca.constraints.LessOrEqualConstraint;
52 import org.openecomp.sdc.be.model.tosca.constraints.LessThanConstraint;
53 import org.openecomp.sdc.be.model.tosca.constraints.MaxLengthConstraint;
54 import org.openecomp.sdc.be.model.tosca.constraints.MinLengthConstraint;
55 import org.openecomp.sdc.be.model.tosca.constraints.ValidValuesConstraint;
56 import org.openecomp.sdc.be.model.tosca.converters.DataTypePropertyConverter;
57 import org.openecomp.sdc.be.model.tosca.converters.ToscaMapValueConverter;
58 import org.openecomp.sdc.be.model.tosca.converters.ToscaValueBaseConverter;
59 import org.openecomp.sdc.be.model.tosca.converters.ToscaValueConverter;
60 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
61 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
62 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraint;
63 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintEqual;
64 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintGreaterOrEqual;
65 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintGreaterThan;
66 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintInRange;
67 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintLength;
68 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintLessOrEqual;
69 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintLessThan;
70 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintMaxLength;
71 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintMinLength;
72 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintValidValues;
73 import org.openecomp.sdc.be.tosca.model.ToscaSchemaDefinition;
74 import org.openecomp.sdc.common.log.wrappers.Logger;
75 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
76 import org.springframework.stereotype.Service;
77 import org.yaml.snakeyaml.Yaml;
78
79
80 @Service
81 public class PropertyConvertor {
82
83     private static final Logger log = Logger.getLogger(PropertyConvertor.class);
84
85     public Either<ToscaNodeType, ToscaError> convertProperties(Component component, ToscaNodeType toscaNodeType,
86                                                                Map<String, DataTypeDefinition> dataTypes) {
87         if (component instanceof Resource) {
88             Resource resource = (Resource) component;
89             List<PropertyDefinition> props = resource.getProperties();
90             if (props != null) {
91                 Map<String, ToscaProperty> properties = new HashMap<>();
92                 // take only the properties of this resource
93                 props.stream().filter(p -> p.getOwnerId() == null || p.getOwnerId().equals(component.getUniqueId())).forEach(property -> {
94                     properties.put(property.getName(), convertProperty(dataTypes, property, PropertyType.PROPERTY));
95                 });
96                 if (!properties.isEmpty()) {
97                     toscaNodeType.setProperties(properties);
98                 }
99             }
100         }
101         return Either.left(toscaNodeType);
102     }
103
104     public ToscaProperty convertProperty(Map<String, DataTypeDefinition> dataTypes, PropertyDefinition property, PropertyType propertyType) {
105         ToscaProperty prop = new ToscaProperty();
106         log.trace("try to convert property {} from type {} with default value [{}]", property.getName(), property.getType(),
107             property.getDefaultValue());
108         SchemaDefinition schema = property.getSchema();
109         if (schema != null && schema.getProperty() != null && schema.getProperty().getType() != null && !schema.getProperty().getType().isEmpty()) {
110             final ToscaSchemaDefinition toscaSchemaDefinition = new ToscaSchemaDefinition();
111             toscaSchemaDefinition.setType(schema.getProperty().getType());
112             toscaSchemaDefinition.setDescription(schema.getProperty().getDescription());
113             prop.setEntry_schema(toscaSchemaDefinition);
114         }
115         String defaultValue = property.getDefaultValue();
116         if (Objects.isNull(defaultValue)) {
117             defaultValue = property.getValue();
118         }
119         Object convertedObj = convertToToscaObject(property, defaultValue, dataTypes, false);
120         if (convertedObj != null) {
121             prop.setDefaultp(convertedObj);
122         }
123         prop.setType(property.getType());
124         prop.setDescription(property.getDescription());
125         prop.setRequired(property.isRequired());
126         if (propertyType.equals(PropertyType.CAPABILITY)) {
127             prop.setStatus(property.getStatus());
128         }
129         prop.setMetadata(property.getMetadata());
130         
131         List<ToscaPropertyConstraint> constraints = new ArrayList<>();
132         if (CollectionUtils.isNotEmpty(property.getConstraints())) {
133             constraints = convertConstraints(property.getConstraints());
134             prop.setConstraints(constraints);
135         }
136         return prop;
137     }
138     
139     private List<ToscaPropertyConstraint> convertConstraints(List<PropertyConstraint> constraints) {
140         List<ToscaPropertyConstraint> convertedConstraints = new ArrayList<>();
141         for (PropertyConstraint constraint: constraints){
142             if (constraint instanceof EqualConstraint) {
143                 convertedConstraints.add(new ToscaPropertyConstraintEqual(((EqualConstraint) constraint).getEqual()));
144             }
145             if (constraint instanceof GreaterThanConstraint) {
146                 convertedConstraints.add(new ToscaPropertyConstraintGreaterThan(((GreaterThanConstraint) constraint).getGreaterThan()));
147             }
148             if (constraint instanceof GreaterOrEqualConstraint) {
149                 convertedConstraints.add(new ToscaPropertyConstraintGreaterOrEqual(((GreaterOrEqualConstraint) constraint).getGreaterOrEqual()));
150             }
151             if (constraint instanceof LessThanConstraint) {
152                 convertedConstraints.add(new ToscaPropertyConstraintLessThan(((LessThanConstraint) constraint).getLessThan()));
153             }
154             if (constraint instanceof LessOrEqualConstraint) {
155                 convertedConstraints.add(new ToscaPropertyConstraintLessOrEqual(((LessOrEqualConstraint) constraint).getLessOrEqual()));
156             }
157             if (constraint instanceof InRangeConstraint) {
158                 InRangeConstraint inRangeConstraint = (InRangeConstraint) constraint;
159                 List<String> range = new ArrayList<>();
160                 range.add(inRangeConstraint.getRangeMinValue());
161                 range.add(inRangeConstraint.getRangeMaxValue());
162                 convertedConstraints.add(new ToscaPropertyConstraintInRange(range));
163             }
164             if (constraint instanceof ValidValuesConstraint) {
165                 convertedConstraints.add(new ToscaPropertyConstraintValidValues(((ValidValuesConstraint) constraint).getValidValues()));
166             }
167             if (constraint instanceof LengthConstraint) {
168                 convertedConstraints.add(new ToscaPropertyConstraintLength(((LengthConstraint) constraint).getLength().toString()));
169             }
170             if (constraint instanceof MinLengthConstraint) {
171                 convertedConstraints.add(new ToscaPropertyConstraintMinLength(((MinLengthConstraint) constraint).getMinLength()));
172             }
173             if (constraint instanceof MaxLengthConstraint) {
174                 convertedConstraints.add(new ToscaPropertyConstraintMaxLength(((MaxLengthConstraint) constraint).getMaxLength()));
175             }
176         }
177         return convertedConstraints;
178     }
179
180     public Object convertToToscaObject(PropertyDataDefinition property, String value, Map<String, DataTypeDefinition> dataTypes,
181                                        boolean preserveEmptyValue) {
182         String propertyType = property.getType();
183         String innerType = property.getSchemaType();
184         log.trace("try to convert propertyType {} , value [{}], innerType {}", propertyType, value, innerType);
185         if (StringUtils.isEmpty(value)) {
186             value = DataTypePropertyConverter.getInstance().getDataTypePropertiesDefaultValuesRec(propertyType, dataTypes);
187             if (StringUtils.isEmpty(value)) {
188                 return null;
189             }
190         }
191         if (property.isToscaFunction() && property.getToscaFunction().getType() == ToscaFunctionType.YAML) {
192             return new Yaml().load(property.getValue());
193         }
194         try {
195             ToscaMapValueConverter mapConverterInst = ToscaMapValueConverter.getInstance();
196             ToscaValueConverter innerConverter = null;
197             boolean isScalar = true;
198             ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
199             if (type == null) {
200                 log.trace("isn't prederfined type, get from all data types");
201                 DataTypeDefinition dataTypeDefinition = dataTypes.get(propertyType);
202                 if (innerType == null) {
203                     innerType = propertyType;
204                 }
205                 if ((type = mapConverterInst.isScalarType(dataTypeDefinition)) != null) {
206                     log.trace("This is scalar type. get suitable converter for type {}", type);
207                     innerConverter = type.getValueConverter();
208                 } else {
209                     isScalar = false;
210                 }
211             } else {
212                 ToscaPropertyType typeIfScalar = ToscaPropertyType.getTypeIfScalar(type.getType());
213                 if (typeIfScalar == null) {
214                     isScalar = false;
215                 }
216                 innerConverter = type.getValueConverter();
217                 if (ToscaPropertyType.STRING == type && valueStartsWithNonJsonChar(value)) {
218                     return innerConverter.convertToToscaValue(value, innerType, dataTypes);
219                 }
220             }
221             JsonElement jsonElement = null;
222             StringReader reader = new StringReader(value);
223             JsonReader jsonReader = new JsonReader(reader);
224             jsonReader.setLenient(true);
225             jsonElement = JsonParser.parseReader(jsonReader);
226             if (value.equals("")) {
227                 return value;
228             }
229             if (jsonElement.isJsonPrimitive() && isScalar) {
230                 log.trace("It's well defined type. convert it");
231                 ToscaValueConverter converter = type.getValueConverter();
232                 return converter.convertToToscaValue(value, innerType, dataTypes);
233             }
234             log.trace("It's data type or inputs in primitive type. convert as map");
235             if (jsonElement.isJsonObject()) {
236                 JsonObject jsonObj = jsonElement.getAsJsonObject();
237                 // check if value is a get_input function
238                 if (jsonObj.entrySet().size() == 1 && jsonObj.has(ToscaFunctions.GET_INPUT.getFunctionName())) {
239                     Object obj = mapConverterInst.handleComplexJsonValue(jsonElement);
240                     log.debug("It's get_input function. obj={}", obj);
241                     return obj;
242                 }
243             }
244             Object convertedValue;
245             if (innerConverter != null && (ToscaPropertyType.MAP == type || ToscaPropertyType.LIST == type)) {
246                 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
247             } else if (isScalar) {
248                 // complex json for scalar type
249                 convertedValue = mapConverterInst.handleComplexJsonValue(jsonElement);
250             } else if (innerConverter != null) {
251                 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
252             } else {
253                 convertedValue = mapConverterInst
254                     .convertDataTypeToToscaObject(innerType, dataTypes, innerConverter, isScalar, jsonElement, preserveEmptyValue);
255             }
256             return convertedValue;
257         
258         } catch (JsonParseException e) {
259             log.trace("{} not parsable as JSON. Convert as YAML instead", value);
260             return  new Yaml().load(value);
261         } catch (Exception e) {
262             log.debug("convertToToscaValue failed to parse json value :", e);
263             return null;
264         }
265     }
266
267     private boolean valueStartsWithNonJsonChar(String value) {
268         return value.startsWith("/") || value.startsWith(":");
269     }
270
271     public void convertAndAddValue(Map<String, DataTypeDefinition> dataTypes, Map<String, Object> props, PropertyDataDefinition prop,
272                                    Supplier<String> supplier) {
273         Object convertedValue = convertValue(dataTypes, prop, supplier);
274         if (!ToscaValueBaseConverter.isEmptyObjectValue(convertedValue)) {
275             props.put(prop.getName(), convertedValue);
276         }
277     }
278
279     private <T extends PropertyDataDefinition> Object convertValue(Map<String, DataTypeDefinition> dataTypes, T input, Supplier<String> supplier) {
280         return convertToToscaObject(input, supplier.get(), dataTypes, false);
281     }
282
283     public enum PropertyType {CAPABILITY, INPUT, PROPERTY}
284 }