Support complex types in interface 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.JsonParser;
25 import com.google.gson.stream.JsonReader;
26 import fj.data.Either;
27 import java.io.StringReader;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Objects;
32 import java.util.function.Supplier;
33 import org.apache.commons.lang3.StringUtils;
34 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
35 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
36 import org.openecomp.sdc.be.model.Component;
37 import org.openecomp.sdc.be.model.DataTypeDefinition;
38 import org.openecomp.sdc.be.model.PropertyDefinition;
39 import org.openecomp.sdc.be.model.Resource;
40 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
41 import org.openecomp.sdc.be.model.tosca.converters.DataTypePropertyConverter;
42 import org.openecomp.sdc.be.model.tosca.converters.ToscaMapValueConverter;
43 import org.openecomp.sdc.be.model.tosca.converters.ToscaValueBaseConverter;
44 import org.openecomp.sdc.be.model.tosca.converters.ToscaValueConverter;
45 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
46 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
47 import org.openecomp.sdc.be.tosca.model.ToscaSchemaDefinition;
48 import org.openecomp.sdc.common.log.wrappers.Logger;
49 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
50 import org.springframework.stereotype.Service;
51
52 @Service
53 public class PropertyConvertor {
54
55     private static final Logger log = Logger.getLogger(PropertyConvertor.class);
56
57     public Either<ToscaNodeType, ToscaError> convertProperties(Component component, ToscaNodeType toscaNodeType,
58                                                                Map<String, DataTypeDefinition> dataTypes) {
59         if (component instanceof Resource) {
60             Resource resource = (Resource) component;
61             List<PropertyDefinition> props = resource.getProperties();
62             if (props != null) {
63                 Map<String, ToscaProperty> properties = new HashMap<>();
64                 // take only the properties of this resource
65                 props.stream().filter(p -> p.getOwnerId() == null || p.getOwnerId().equals(component.getUniqueId())).forEach(property -> {
66                     properties.put(property.getName(), convertProperty(dataTypes, property, PropertyType.PROPERTY));
67                 });
68                 if (!properties.isEmpty()) {
69                     toscaNodeType.setProperties(properties);
70                 }
71             }
72         }
73         return Either.left(toscaNodeType);
74     }
75
76     public ToscaProperty convertProperty(Map<String, DataTypeDefinition> dataTypes, PropertyDefinition property, PropertyType propertyType) {
77         ToscaProperty prop = new ToscaProperty();
78         log.trace("try to convert property {} from type {} with default value [{}]", property.getName(), property.getType(),
79             property.getDefaultValue());
80         SchemaDefinition schema = property.getSchema();
81         if (schema != null && schema.getProperty() != null && schema.getProperty().getType() != null && !schema.getProperty().getType().isEmpty()) {
82             final ToscaSchemaDefinition toscaSchemaDefinition = new ToscaSchemaDefinition();
83             toscaSchemaDefinition.setType(schema.getProperty().getType());
84             toscaSchemaDefinition.setDescription(schema.getProperty().getDescription());
85             prop.setEntry_schema(toscaSchemaDefinition);
86         }
87         String defaultValue = property.getDefaultValue();
88         if (Objects.isNull(defaultValue)) {
89             defaultValue = property.getValue();
90         }
91         Object convertedObj = convertToToscaObject(property, defaultValue, dataTypes, false);
92         if (convertedObj != null) {
93             prop.setDefaultp(convertedObj);
94         }
95         prop.setType(property.getType());
96         prop.setDescription(property.getDescription());
97         prop.setRequired(property.isRequired());
98         if (propertyType.equals(PropertyType.CAPABILITY)) {
99             prop.setStatus(property.getStatus());
100         }
101         prop.setMetadata(property.getMetadata());
102         return prop;
103     }
104
105     public Object convertToToscaObject(PropertyDataDefinition property, String value, Map<String, DataTypeDefinition> dataTypes,
106                                        boolean preserveEmptyValue) {
107         String propertyType = property.getType();
108         String innerType = property.getSchemaType();
109         log.trace("try to convert propertyType {} , value [{}], innerType {}", propertyType, value, innerType);
110         if (StringUtils.isEmpty(value)) {
111             value = DataTypePropertyConverter.getInstance().getDataTypePropertiesDefaultValuesRec(propertyType, dataTypes);
112             if (StringUtils.isEmpty(value)) {
113                 return null;
114             }
115         }
116         try {
117             ToscaMapValueConverter mapConverterInst = ToscaMapValueConverter.getInstance();
118             ToscaValueConverter innerConverter = null;
119             boolean isScalar = true;
120             ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
121             if (type == null) {
122                 log.trace("isn't prederfined type, get from all data types");
123                 DataTypeDefinition dataTypeDefinition = dataTypes.get(propertyType);
124                 if (innerType == null) {
125                     innerType = propertyType;
126                 }
127                 if ((type = mapConverterInst.isScalarType(dataTypeDefinition)) != null) {
128                     log.trace("This is scalar type. get suitable converter for type {}", type);
129                     innerConverter = type.getValueConverter();
130                 } else {
131                     isScalar = false;
132                 }
133             } else {
134                 ToscaPropertyType typeIfScalar = ToscaPropertyType.getTypeIfScalar(type.getType());
135                 if (typeIfScalar == null) {
136                     isScalar = false;
137                 }
138                 innerConverter = type.getValueConverter();
139                 if (ToscaPropertyType.STRING == type && valueStartsWithNonJsonChar(value)) {
140                     return innerConverter.convertToToscaValue(value, innerType, dataTypes);
141                 }
142             }
143             JsonElement jsonElement = null;
144             StringReader reader = new StringReader(value);
145             JsonReader jsonReader = new JsonReader(reader);
146             jsonReader.setLenient(true);
147             jsonElement = JsonParser.parseReader(jsonReader);
148             if (value.equals("")) {
149                 return value;
150             }
151             if (jsonElement.isJsonPrimitive() && isScalar) {
152                 log.trace("It's well defined type. convert it");
153                 ToscaValueConverter converter = type.getValueConverter();
154                 return converter.convertToToscaValue(value, innerType, dataTypes);
155             }
156             log.trace("It's data type or inputs in primitive type. convert as map");
157             if (jsonElement.isJsonObject()) {
158                 JsonObject jsonObj = jsonElement.getAsJsonObject();
159                 // check if value is a get_input function
160                 if (jsonObj.entrySet().size() == 1 && jsonObj.has(ToscaFunctions.GET_INPUT.getFunctionName())) {
161                     Object obj = mapConverterInst.handleComplexJsonValue(jsonElement);
162                     log.debug("It's get_input function. obj={}", obj);
163                     return obj;
164                 }
165             }
166             Object convertedValue;
167             if (innerConverter != null && (ToscaPropertyType.MAP == type || ToscaPropertyType.LIST == type)) {
168                 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
169             } else if (isScalar) {
170                 // complex json for scalar type
171                 convertedValue = mapConverterInst.handleComplexJsonValue(jsonElement);
172             } else if (innerConverter != null) {
173                 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
174             } else {
175                 convertedValue = mapConverterInst
176                     .convertDataTypeToToscaObject(innerType, dataTypes, innerConverter, isScalar, jsonElement, preserveEmptyValue);
177             }
178             return convertedValue;
179         } catch (Exception e) {
180             log.debug("convertToToscaValue failed to parse json value :", e);
181             return null;
182         }
183     }
184
185     private boolean valueStartsWithNonJsonChar(String value) {
186         return value.startsWith("/") || value.startsWith(":");
187     }
188
189     public void convertAndAddValue(Map<String, DataTypeDefinition> dataTypes, Map<String, Object> props, PropertyDataDefinition prop,
190                                    Supplier<String> supplier) {
191         Object convertedValue = convertValue(dataTypes, prop, supplier);
192         if (!ToscaValueBaseConverter.isEmptyObjectValue(convertedValue)) {
193             props.put(prop.getName(), convertedValue);
194         }
195     }
196
197     private <T extends PropertyDataDefinition> Object convertValue(Map<String, DataTypeDefinition> dataTypes, T input, Supplier<String> supplier) {
198         return convertToToscaObject(input, supplier.get(), dataTypes, false);
199     }
200
201     public enum PropertyType {CAPABILITY, INPUT, PROPERTY}
202 }