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.ToscaPropertyType;
41 import org.openecomp.sdc.common.log.wrappers.Logger;
42 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
44 public class ToscaMapValueConverter extends ToscaValueBaseConverter implements ToscaValueConverter {
46 private static final Logger log = Logger.getLogger(ToscaMapValueConverter.class.getName());
47 private static final ToscaMapValueConverter mapConverter = new ToscaMapValueConverter();
49 private ToscaMapValueConverter() {
52 public static ToscaMapValueConverter getInstance() {
57 public Object convertToToscaValue(String value, String innerType, Map<String, DataTypeDefinition> dataTypes) {
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();
69 DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType);
70 if (dataTypeDefinition != null) {
71 ToscaPropertyType toscaPropertyType;
72 if ((toscaPropertyType = isScalarType(dataTypeDefinition)) != null) {
73 innerConverter = toscaPropertyType.getValueConverter();
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();
84 log.debug("inner Tosca Type is null");
88 JsonElement jsonElement;
89 jsonElement = parseToJson(value);
90 if (jsonElement == null || jsonElement.isJsonNull()) {
91 log.debug("convertToToscaValue json element is null");
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);
105 } catch (JsonParseException e) {
106 log.debug("Failed to parse json : {}", value, e);
107 BeEcompErrorManager.getInstance().logBeInvalidJsonInput("List Converter");
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;
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) {
127 propType = toscaPropType.getType();
128 innerConverterProp = toscaPropType.getValueConverter();
134 final Object convertedValue = convertDataTypeToToscaObject(propType, dataTypes, innerConverterProp, scalar, e.getValue(), false);
135 toscaMap.put(e.getKey(), convertedValue);
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);
144 if (entryValue.isJsonPrimitive()) {
145 return handleComplexJsonValue(entryValue);
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);
155 convertedValue = toscaObjectPresentationArray;
157 convertedValue = convertDataTypeToToscaMap(innerType, dataTypes, isScalarF, entryValue, preserveEmptyValue);
160 return convertedValue;
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());
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();
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();
186 convValue = handleComplexJsonValue(elementValue);
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);
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);
202 convValue = handleComplexJsonValue(elementValue);
206 convValue = convertToToscaValue(elementValue.toString(), type, dataTypes);
210 if (isJsonElementAJsonPrimitive(elementValue)) {
211 convValue = json2JavaPrimitive(elementValue.getAsJsonPrimitive());
213 convValue = handleComplexJsonValue(elementValue);
216 if (preserveEmptyValue || !isEmptyObjectValue(convValue) || isGetPolicyValue(propName)) {
217 toscaObjectPresentation.put(propName, convValue);
220 convertedValue = toscaObjectPresentation;
221 return convertedValue;
224 private boolean isGetPolicyValue(String key) {
225 return key.equals(ToscaFunctions.GET_POLICY.getFunctionName());