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;
27 import java.util.Map.Entry;
30 import org.openecomp.sdc.be.config.BeEcompErrorManager;
31 import org.openecomp.sdc.be.model.DataTypeDefinition;
32 import org.openecomp.sdc.be.model.PropertyDefinition;
33 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import com.google.gson.JsonArray;
38 import com.google.gson.JsonElement;
39 import com.google.gson.JsonObject;
40 import com.google.gson.JsonParseException;
41 import com.google.gson.JsonParser;
42 import com.google.gson.JsonSyntaxException;
43 import com.google.gson.stream.JsonReader;
45 public class ToscaListValueConverter extends ToscaValueBaseConverter implements ToscaValueConverter {
46 private static ToscaListValueConverter listConverter = new ToscaListValueConverter();
47 private JsonParser jsonParser = new JsonParser();
48 private static Logger log = LoggerFactory.getLogger(ToscaListValueConverter.class.getName());
50 public static ToscaListValueConverter getInstance() {
54 private ToscaListValueConverter() {
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 if (innerToscaType != null) {
68 innerConverter = innerToscaType.getValueConverter();
70 DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType);
72 if (dataTypeDefinition != null) {
73 ToscaPropertyType toscaPropertyType = null;
74 if ((toscaPropertyType = isScalarType(dataTypeDefinition)) != null) {
75 innerConverter = toscaPropertyType.getValueConverter();
78 innerConverter = ToscaMapValueConverter.getInstance();
81 log.debug("inner Tosca Type is null");
85 JsonElement jsonElement = null;
87 StringReader reader = new StringReader(value);
88 JsonReader jsonReader = new JsonReader(reader);
89 jsonReader.setLenient(true);
91 jsonElement = jsonParser.parse(jsonReader);
92 } catch (JsonSyntaxException e) {
93 log.debug("convertToToscaValue failed to parse json value :", e);
96 if (jsonElement == null || true == jsonElement.isJsonNull()) {
97 log.debug("convertToToscaValue json element is null");
100 if (jsonElement.isJsonArray() == false) {
101 // get_input all array like get_input: qrouter_names
102 return handleComplexJsonValue(jsonElement);
104 JsonArray asJsonArray = jsonElement.getAsJsonArray();
106 ArrayList<Object> toscaList = new ArrayList<Object>();
107 final boolean isScalarF = isScalar;
108 final ToscaValueConverter innerConverterFinal = innerConverter;
109 asJsonArray.forEach(e -> {
110 Object convertedValue = null;
112 log.debug("try to convert scalar value {}", e.getAsString());
113 if (e.getAsString() == null) {
114 convertedValue = null;
116 JsonElement singleElement = jsonParser.parse(e.getAsString());
117 if (singleElement.isJsonPrimitive()) {
118 convertedValue = innerConverterFinal.convertToToscaValue(e.getAsString(), innerType,
121 convertedValue = handleComplexJsonValue(singleElement);
125 JsonObject asJsonObject = e.getAsJsonObject();
126 Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();
128 DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType);
129 Map<String, PropertyDefinition> allProperties = getAllProperties(dataTypeDefinition);
130 Map<String, Object> toscaObjectPresentation = new HashMap<>();
131 // log.debug("try to convert datatype value {}",
134 for (Entry<String, JsonElement> entry : entrySet) {
135 String propName = entry.getKey();
137 JsonElement elementValue = entry.getValue();
138 PropertyDefinition propertyDefinition = allProperties.get(propName);
139 if (propertyDefinition == null) {
140 log.debug("The property {} was not found under data type {}", propName, dataTypeDefinition.getName());
144 String type = propertyDefinition.getType();
145 ToscaPropertyType propertyType = ToscaPropertyType.isValidType(type);
147 if (propertyType != null) {
148 if (elementValue.isJsonPrimitive()) {
149 ToscaValueConverter valueConverter = propertyType.getValueConverter();
150 convValue = valueConverter.convertToToscaValue(elementValue.getAsString(), type,
153 if (ToscaPropertyType.MAP.equals(type) || ToscaPropertyType.LIST.equals(propertyType)) {
154 ToscaValueConverter valueConverter = propertyType.getValueConverter();
155 String json = gson.toJson(elementValue);
156 String innerTypeRecursive = propertyDefinition.getSchema().getProperty().getType();
157 convValue = valueConverter.convertToToscaValue(json, innerTypeRecursive, dataTypes);
159 convValue = handleComplexJsonValue(elementValue);
163 String json = gson.toJson(elementValue);
164 convValue = convertToToscaValue(json, type, dataTypes);
166 toscaObjectPresentation.put(propName, convValue);
168 convertedValue = toscaObjectPresentation;
170 toscaList.add(convertedValue);
175 JsonParseException e) {
176 log.debug("Failed to parse json : {}. {}", value, e);
177 BeEcompErrorManager.getInstance().logBeInvalidJsonInput("List Converter");