2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.model.tosca.converters;
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;
35 import java.util.Map.Entry;
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;
44 public class ToscaMapValueConverter extends ToscaValueBaseConverter implements ToscaValueConverter {
45 private static ToscaMapValueConverter mapConverter = new ToscaMapValueConverter();
47 private JsonParser jsonParser = new JsonParser();
48 private static final Logger log = Logger.getLogger(ToscaMapValueConverter.class.getName());
50 public static ToscaMapValueConverter getInstance() {
54 private ToscaMapValueConverter() {
59 public Object convertToToscaValue(String value, String innerType, Map<String, DataTypeDefinition> dataTypes) {
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();
72 DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType);
73 if (dataTypeDefinition != null) {
74 ToscaPropertyType toscaPropertyType = null;
75 if ((toscaPropertyType = isScalarType(dataTypeDefinition)) != null) {
76 innerConverter = toscaPropertyType.getValueConverter();
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();
87 log.debug("inner Tosca Type is null");
92 JsonElement jsonElement = null;
94 StringReader reader = new StringReader(value);
95 JsonReader jsonReader = new JsonReader(reader);
96 jsonReader.setLenient(true);
98 jsonElement = jsonParser.parse(jsonReader);
100 } catch (JsonSyntaxException e) {
101 log.debug("convertToToscaValue failed to parse json value :", e);
104 if (jsonElement == null || jsonElement.isJsonNull()) {
105 log.debug("convertToToscaValue json element is null");
108 JsonObject asJsonObject = jsonElement.getAsJsonObject();
109 Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();
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);
118 } catch (JsonParseException e) {
119 log.debug("Failed to parse json : {}", value, e);
120 BeEcompErrorManager.getInstance().logBeInvalidJsonInput("List Converter");
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;
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) {
142 propType = toscaPropType.getType();
143 innerConverterProp = toscaPropType.getValueConverter();
149 final Object convertedValue =
150 convertDataTypeToToscaObject(propType, dataTypes, innerConverterProp, scalar, e.getValue(), false);
151 toscaMap.put(e.getKey(), convertedValue);
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;
161 convertedValue = innerConverter.convertToToscaValue(entryValue.getAsString(), innerType, dataTypes);
164 if ( entryValue.isJsonPrimitive() ){
165 return handleComplexJsonValue(entryValue);
168 // ticket 228696523 created / DE272734 / Bug 154492 Fix
169 if(entryValue instanceof JsonArray) {
170 ArrayList<Object> toscaObjectPresentationArray = new ArrayList<>();
171 JsonArray jsonArray = entryValue.getAsJsonArray();
173 for (JsonElement jsonElement : jsonArray) {
174 Object convertedDataTypeToToscaMap = convertDataTypeToToscaMap(innerType, dataTypes, isScalarF, jsonElement, preserveEmptyValue);
175 toscaObjectPresentationArray.add(convertedDataTypeToToscaMap);
177 convertedValue = toscaObjectPresentationArray;
179 convertedValue = convertDataTypeToToscaMap(innerType, dataTypes, isScalarF, entryValue, preserveEmptyValue);
182 return convertedValue;
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());
191 JsonObject asJsonObjectIn = entryValue.getAsJsonObject();
193 DataTypePropertyConverter.getInstance().mergeDataTypeDefaultValuesWithPropertyValue(asJsonObjectIn, innerType, dataTypes);
194 Map<String, Object> toscaObjectPresentation = new HashMap<>();
195 Set<Entry<String, JsonElement>> entrySetIn = asJsonObjectIn.entrySet();
197 for (Entry<String, JsonElement> entry : entrySetIn) {
198 String propName = entry.getKey();
200 JsonElement elementValue = entry.getValue();
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();
211 convValue = handleComplexJsonValue(elementValue);
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);
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);
227 convValue = handleComplexJsonValue(elementValue);
231 convValue = convertToToscaValue(elementValue.toString(), type, dataTypes);
235 if (elementValue.isJsonPrimitive()) {
236 convValue = json2JavaPrimitive(elementValue.getAsJsonPrimitive());
238 convValue = handleComplexJsonValue(elementValue);
241 if(preserveEmptyValue || !isEmptyObjectValue(convValue) || isGetPolicyValue(propName)){
242 toscaObjectPresentation.put(propName, convValue);
245 convertedValue = toscaObjectPresentation;
246 return convertedValue;
249 private boolean isGetPolicyValue(String key) {
250 return key.equals(ToscaFunctions.GET_POLICY.getFunctionName());