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