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