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 java.io.StringReader;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
28 import java.util.Map.Entry;
31 import org.openecomp.sdc.be.config.BeEcompErrorManager;
32 import org.openecomp.sdc.be.model.DataTypeDefinition;
33 import org.openecomp.sdc.be.model.PropertyDefinition;
34 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import com.google.gson.JsonArray;
39 import com.google.gson.JsonElement;
40 import com.google.gson.JsonObject;
41 import com.google.gson.JsonParseException;
42 import com.google.gson.JsonParser;
43 import com.google.gson.JsonSyntaxException;
44 import com.google.gson.stream.JsonReader;
46 public class ToscaMapValueConverter extends ToscaValueBaseConverter implements ToscaValueConverter {
47 private static ToscaMapValueConverter mapConverter = new ToscaMapValueConverter();
49 private JsonParser jsonParser = new JsonParser();
50 private static Logger log = LoggerFactory.getLogger(ToscaMapValueConverter.class.getName());
52 public static ToscaMapValueConverter getInstance() {
56 private ToscaMapValueConverter() {
61 public Object convertToToscaValue(String value, String innerType, Map<String, DataTypeDefinition> dataTypes) {
66 ToscaPropertyType innerToscaType = ToscaPropertyType.isValidType(innerType);
67 ToscaValueConverter innerConverter = null;
68 boolean isScalar = true;
69 List<PropertyDefinition> allPropertiesRecursive = new ArrayList<>();
70 if (innerToscaType != null) {
71 innerConverter = innerToscaType.getValueConverter();
74 DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType);
75 if (dataTypeDefinition != null) {
76 ToscaPropertyType toscaPropertyType = null;
77 if ((toscaPropertyType = isScalarType(dataTypeDefinition)) != null) {
78 innerConverter = toscaPropertyType.getValueConverter();
81 allPropertiesRecursive.addAll(dataTypeDefinition.getProperties());
82 DataTypeDefinition derivedFrom = dataTypeDefinition.getDerivedFrom();
83 while ( !derivedFrom.getName().equals("tosca.datatypes.Root") ){
84 allPropertiesRecursive.addAll(derivedFrom.getProperties());
85 derivedFrom = derivedFrom.getDerivedFrom();
89 log.debug("inner Tosca Type is null");
94 JsonElement jsonElement = null;
96 StringReader reader = new StringReader(value);
97 JsonReader jsonReader = new JsonReader(reader);
98 jsonReader.setLenient(true);
100 jsonElement = jsonParser.parse(jsonReader);
102 } catch (JsonSyntaxException e) {
103 log.debug("convertToToscaValue failed to parse json value :", e);
106 if (jsonElement == null || true == jsonElement.isJsonNull()) {
107 log.debug("convertToToscaValue json element is null");
110 JsonObject asJsonObject = jsonElement.getAsJsonObject();
111 Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();
113 Map<String, Object> toscaMap = new HashMap<>();
114 final boolean isScalarF = isScalar;
115 final ToscaValueConverter innerConverterFinal = innerConverter;
116 entrySet.forEach(e -> {
117 convertEntry(innerType, dataTypes, allPropertiesRecursive, toscaMap, isScalarF, innerConverterFinal, e);
120 } catch (JsonParseException e) {
121 log.debug("Failed to parse json : {}", value, e);
122 BeEcompErrorManager.getInstance().logBeInvalidJsonInput("List Converter");
127 private void convertEntry(String innerType, Map<String, DataTypeDefinition> dataTypes, List<PropertyDefinition> allPropertiesRecursive, Map<String, Object> toscaMap, final boolean isScalarF, final ToscaValueConverter innerConverterFinal,
128 Entry<String, JsonElement> e) {
129 log.debug("try convert element {}", e.getValue());
130 boolean scalar = false;
131 String propType = null;
132 ToscaValueConverter innerConverterProp = innerConverterFinal;
135 propType = innerType;
137 for ( PropertyDefinition pd : allPropertiesRecursive ){
138 if ( pd.getName().equals(e.getKey()) ){
139 propType = pd.getType();
140 DataTypeDefinition pdDataType = dataTypes.get(propType);
141 ToscaPropertyType toscaPropType = isScalarType(pdDataType);
142 if ( toscaPropType == null ){
146 propType = toscaPropType.getType();
147 innerConverterProp = toscaPropType.getValueConverter();
153 Object convertedValue = convertDataTypeToToscaObject(propType, dataTypes, innerConverterProp, scalar, e.getValue());
154 toscaMap.put(e.getKey(), convertedValue);
157 public Object convertDataTypeToToscaObject(String innerType, Map<String, DataTypeDefinition> dataTypes, ToscaValueConverter innerConverter, final boolean isScalarF, JsonElement entryValue) {
158 Object convertedValue = null;
159 if (isScalarF && entryValue.isJsonPrimitive()) {
160 log.debug("try convert scalar value {}", entryValue.getAsString());
161 if (entryValue.getAsString() == null) {
162 convertedValue = null;
164 convertedValue = innerConverter.convertToToscaValue(entryValue.getAsString(), innerType, dataTypes);
167 if ( entryValue.isJsonPrimitive() ){
168 return handleComplexJsonValue(entryValue);
171 // ticket 228696523 created / DE272734 / Bug 154492 Fix
172 if(entryValue instanceof JsonArray) {
173 ArrayList<Object> toscaObjectPresentationArray = new ArrayList<>();
174 JsonArray jsonArray = entryValue.getAsJsonArray();
176 for (JsonElement jsonElement : jsonArray) {
177 Object convertedDataTypeToToscaMap = convertDataTypeToToscaMap(innerType, dataTypes, isScalarF, jsonElement);
178 toscaObjectPresentationArray.add(convertedDataTypeToToscaMap);
180 convertedValue = toscaObjectPresentationArray;
182 convertedValue = convertDataTypeToToscaMap(innerType, dataTypes, isScalarF, entryValue);
185 return convertedValue;
188 private Object convertDataTypeToToscaMap(String innerType, Map<String, DataTypeDefinition> dataTypes,
189 final boolean isScalarF, JsonElement entryValue) {
190 Object convertedValue;
191 JsonObject asJsonObjectIn = entryValue.getAsJsonObject();
192 Set<Entry<String, JsonElement>> entrySetIn = asJsonObjectIn.entrySet();
194 DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType);
195 Map<String, PropertyDefinition> allProperties = getAllProperties(dataTypeDefinition);
196 Map<String, Object> toscaObjectPresentation = new HashMap<>();
198 for (Entry<String, JsonElement> entry : entrySetIn) {
199 String propName = entry.getKey();
201 JsonElement elementValue = entry.getValue();
203 if (isScalarF == false) {
204 PropertyDefinition propertyDefinition = allProperties.get(propName);
205 if (propertyDefinition == null) {
206 log.trace("The property {} was not found under data type . Parse as map", propName);
207 if (elementValue.isJsonPrimitive()) {
208 convValue = elementValue.getAsString();
210 convValue = handleComplexJsonValue(elementValue);
213 String type = propertyDefinition.getType();
214 ToscaPropertyType propertyType = ToscaPropertyType.isValidType(type);
215 if (propertyType != null) {
216 if (elementValue.isJsonPrimitive()) {
217 ToscaValueConverter valueConverter = propertyType.getValueConverter();
218 convValue = valueConverter.convertToToscaValue(elementValue.getAsString(), type, dataTypes);
220 if (ToscaPropertyType.MAP.equals(type) || ToscaPropertyType.LIST.equals(propertyType)) {
221 ToscaValueConverter valueConverter = propertyType.getValueConverter();
222 String json = gson.toJson(elementValue);
223 String innerTypeRecursive = propertyDefinition.getSchema().getProperty().getType();
224 convValue = valueConverter.convertToToscaValue(json, innerTypeRecursive, dataTypes);
226 convValue = handleComplexJsonValue(elementValue);
230 convValue = convertToToscaValue(elementValue.toString(), type, dataTypes);
234 if (elementValue.isJsonPrimitive()) {
235 convValue = json2JavaPrimitive(elementValue.getAsJsonPrimitive());
237 convValue = handleComplexJsonValue(elementValue);
240 if(!isEmptyObjectValue(convValue)){
241 toscaObjectPresentation.put(propName, convValue);
244 convertedValue = toscaObjectPresentation;
245 return convertedValue;