Fix policy property type issue
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / tosca / converters / ToscaMapValueConverter.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.model.tosca.converters;
22
23 import com.google.gson.JsonArray;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonObject;
26 import com.google.gson.JsonParseException;
27 import com.google.gson.JsonParser;
28 import com.google.gson.JsonSyntaxException;
29 import com.google.gson.stream.JsonReader;
30 import java.io.StringReader;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Map.Entry;
36 import java.util.Set;
37 import org.openecomp.sdc.be.config.BeEcompErrorManager;
38 import org.openecomp.sdc.be.model.DataTypeDefinition;
39 import org.openecomp.sdc.be.model.PropertyDefinition;
40 import org.openecomp.sdc.be.model.tosca.ToscaFunctions;
41 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
42 import org.openecomp.sdc.common.log.wrappers.Logger;
43
44 public class ToscaMapValueConverter extends ToscaValueBaseConverter implements ToscaValueConverter {
45     private static ToscaMapValueConverter mapConverter = new ToscaMapValueConverter();
46
47     private JsonParser jsonParser = new JsonParser();
48     private static final Logger log = Logger.getLogger(ToscaMapValueConverter.class.getName());
49
50     public static ToscaMapValueConverter getInstance() {
51         return mapConverter;
52     }
53
54     private ToscaMapValueConverter() {
55
56     }
57
58     @Override
59     public Object convertToToscaValue(String value, String innerType, Map<String, DataTypeDefinition> dataTypes) {
60         if (value == null) {
61             return value;
62         }
63         try {
64             ToscaPropertyType innerToscaType = ToscaPropertyType.isValidType(innerType);
65             ToscaValueConverter innerConverter = null;
66             boolean isScalar = true;
67             List<PropertyDefinition> allPropertiesRecursive = new ArrayList<>();
68             if (innerToscaType != null) {
69                 innerConverter = innerToscaType.getValueConverter();
70             } else {
71
72                 DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType);
73                 if (dataTypeDefinition != null) {
74                     ToscaPropertyType toscaPropertyType = null;
75                     if ((toscaPropertyType = isScalarType(dataTypeDefinition)) != null) {
76                         innerConverter = toscaPropertyType.getValueConverter();
77                     } else {
78                         isScalar = false;
79                         allPropertiesRecursive.addAll(dataTypeDefinition.getProperties());
80                         DataTypeDefinition derivedFrom = dataTypeDefinition.getDerivedFrom();
81                         while ( !derivedFrom.getName().equals("tosca.datatypes.Root") ){
82                             allPropertiesRecursive.addAll(derivedFrom.getProperties());
83                             derivedFrom = derivedFrom.getDerivedFrom();
84                         }
85                     }
86                 } else {
87                     log.debug("inner Tosca Type is null");
88                     return value;
89                 }
90
91             }
92             JsonElement jsonElement = null;
93             try {
94                 StringReader reader = new StringReader(value);
95                 JsonReader jsonReader = new JsonReader(reader);
96                 jsonReader.setLenient(true);
97
98                 jsonElement = jsonParser.parse(jsonReader);
99
100             } catch (JsonSyntaxException e) {
101                 log.debug("convertToToscaValue failed to parse json value :", e);
102                 return null;
103             }
104             if (jsonElement == null || jsonElement.isJsonNull()) {
105                 log.debug("convertToToscaValue json element is null");
106                 return null;
107             }
108             JsonObject asJsonObject = jsonElement.getAsJsonObject();
109             Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();
110
111             Map<String, Object> toscaMap = new HashMap<>();
112             final boolean isScalarF = isScalar;
113             final ToscaValueConverter innerConverterFinal = innerConverter;
114             entrySet.forEach(e -> {
115                 convertEntry(innerType, dataTypes, allPropertiesRecursive, toscaMap, isScalarF, innerConverterFinal, e);
116             });
117             return toscaMap;
118         } catch (JsonParseException e) {
119             log.debug("Failed to parse json : {}", value, e);
120             BeEcompErrorManager.getInstance().logBeInvalidJsonInput("List Converter");
121             return null;
122         }
123     }
124
125     private void convertEntry(final String innerType, final Map<String, DataTypeDefinition> dataTypes,
126             final List<PropertyDefinition> allPropertiesRecursive, final Map<String, Object> toscaMap, final boolean isScalarF,
127             final ToscaValueConverter innerConverterFinal, final Entry<String, JsonElement> e) {
128         log.debug("try convert element ");
129         boolean scalar = isScalarF;
130         String propType = innerType;
131         ToscaValueConverter innerConverterProp = innerConverterFinal;
132         if (!scalar) {
133             for (PropertyDefinition pd : allPropertiesRecursive) {
134                 if (pd.getName().equals(e.getKey())) {
135                     propType = pd.getType();
136                     final DataTypeDefinition pdDataType = dataTypes.get(propType);
137                     final ToscaPropertyType toscaPropType = isScalarType(pdDataType);
138                     if (toscaPropType == null) {
139                         scalar = false;
140                     } else {
141                         scalar = true;
142                         propType = toscaPropType.getType();
143                         innerConverterProp = toscaPropType.getValueConverter();
144                     }
145                     break;
146                 }
147             }
148         }
149         final Object convertedValue =
150                 convertDataTypeToToscaObject(propType, dataTypes, innerConverterProp, scalar, e.getValue(), false);
151         toscaMap.put(e.getKey(), convertedValue);
152     }
153
154     public Object convertDataTypeToToscaObject(String innerType, Map<String, DataTypeDefinition> dataTypes, ToscaValueConverter innerConverter, final boolean isScalarF, JsonElement entryValue, boolean preserveEmptyValue) {
155         Object convertedValue = null;
156         if (isScalarF && entryValue.isJsonPrimitive()) {
157             log.debug("try convert scalar value ");
158             if (entryValue.getAsString() == null) {
159                 convertedValue = null;
160             } else {
161                 convertedValue = innerConverter.convertToToscaValue(entryValue.getAsString(), innerType, dataTypes);
162             }
163         } else {
164             if ( entryValue.isJsonPrimitive() ){
165                 return handleComplexJsonValue(entryValue);
166             }
167
168             // ticket 228696523 created   / DE272734 / Bug 154492 Fix
169             if(entryValue instanceof JsonArray) {
170                 ArrayList<Object> toscaObjectPresentationArray = new ArrayList<>();
171                 JsonArray jsonArray = entryValue.getAsJsonArray();
172
173                 for (JsonElement jsonElement : jsonArray) {
174                     Object convertedDataTypeToToscaMap = convertDataTypeToToscaMap(innerType, dataTypes, isScalarF, jsonElement, preserveEmptyValue);
175                     toscaObjectPresentationArray.add(convertedDataTypeToToscaMap);
176                 }
177                 convertedValue = toscaObjectPresentationArray;
178             } else {
179                 convertedValue = convertDataTypeToToscaMap(innerType, dataTypes, isScalarF, entryValue, preserveEmptyValue);
180             }
181         }
182         return convertedValue;
183     }
184
185     private Object convertDataTypeToToscaMap(String innerType, Map<String, DataTypeDefinition> dataTypes,
186             final boolean isScalarF, JsonElement entryValue, boolean preserveEmptyValue) {
187         Object convertedValue;
188         if (entryValue.isJsonPrimitive()) {
189             return json2JavaPrimitive(entryValue.getAsJsonPrimitive());
190         }
191         JsonObject asJsonObjectIn = entryValue.getAsJsonObject();
192
193         DataTypePropertyConverter.getInstance().mergeDataTypeDefaultValuesWithPropertyValue(asJsonObjectIn, innerType, dataTypes);
194         Map<String, Object> toscaObjectPresentation = new HashMap<>();
195         Set<Entry<String, JsonElement>> entrySetIn = asJsonObjectIn.entrySet();
196
197         for (Entry<String, JsonElement> entry : entrySetIn) {
198             String propName = entry.getKey();
199
200             JsonElement elementValue = entry.getValue();
201             Object convValue;
202             if (!isScalarF) {
203                 DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType);
204                 Map<String, PropertyDefinition> allProperties = getAllProperties(dataTypeDefinition);
205                 PropertyDefinition propertyDefinition = allProperties.get(propName);
206                 if (propertyDefinition == null) {
207                     log.trace("The property {} was not found under data type . Parse as map", propName);
208                     if (elementValue.isJsonPrimitive()) {
209                         convValue = elementValue.getAsString();
210                     } else {
211                         convValue = handleComplexJsonValue(elementValue);
212                     }
213                 } else {
214                     String type = propertyDefinition.getType();
215                     ToscaPropertyType propertyType = ToscaPropertyType.isValidType(type);
216                     if (propertyType != null) {
217                         if (elementValue.isJsonPrimitive()) {
218                             ToscaValueConverter valueConverter = propertyType.getValueConverter();
219                             convValue = valueConverter.convertToToscaValue(elementValue.getAsString(), type, dataTypes);
220                         } else {
221                             if (ToscaPropertyType.MAP.equals(type) || ToscaPropertyType.LIST.equals(propertyType)) {
222                                 ToscaValueConverter valueConverter = propertyType.getValueConverter();
223                                 String json = gson.toJson(elementValue);
224                                 String innerTypeRecursive = propertyDefinition.getSchema().getProperty().getType();
225                                 convValue = valueConverter.convertToToscaValue(json, innerTypeRecursive, dataTypes);
226                             } else {
227                                 convValue = handleComplexJsonValue(elementValue);
228                             }
229                         }
230                     } else {
231                         convValue = convertToToscaValue(elementValue.toString(), type, dataTypes);
232                     }
233                 }
234             } else {
235                 if (elementValue.isJsonPrimitive()) {
236                     convValue = json2JavaPrimitive(elementValue.getAsJsonPrimitive());
237                 } else {
238                     convValue = handleComplexJsonValue(elementValue);
239                 }
240             }
241             if(preserveEmptyValue || !isEmptyObjectValue(convValue) || isGetPolicyValue(propName)){
242                 toscaObjectPresentation.put(propName, convValue);
243             }
244         }
245         convertedValue = toscaObjectPresentation;
246         return convertedValue;
247     }
248
249     private boolean isGetPolicyValue(String key) {
250         return key.equals(ToscaFunctions.GET_POLICY.getFunctionName());
251     }
252 }