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