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=========================================================
20 package org.openecomp.sdc.be.model.tosca.converters;
22 import com.google.gson.JsonArray;
23 import com.google.gson.JsonElement;
24 import com.google.gson.JsonObject;
25 import com.google.gson.JsonParseException;
26 import com.google.gson.JsonParser;
27 import com.google.gson.JsonSyntaxException;
28 import com.google.gson.stream.JsonReader;
29 import java.io.StringReader;
30 import java.util.ArrayList;
31 import java.util.HashMap;
33 import java.util.Map.Entry;
35 import org.jetbrains.annotations.Nullable;
36 import org.openecomp.sdc.be.config.BeEcompErrorManager;
37 import org.openecomp.sdc.be.model.DataTypeDefinition;
38 import org.openecomp.sdc.be.model.PropertyDefinition;
39 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
40 import org.openecomp.sdc.common.log.wrappers.Logger;
41 import org.openecomp.sdc.common.util.JsonUtils;
43 public class ToscaListValueConverter extends ToscaValueBaseConverter implements ToscaValueConverter {
45 private static final Logger log = Logger.getLogger(ToscaListValueConverter.class.getName());
46 private static final ToscaListValueConverter listConverter = new ToscaListValueConverter();
48 private ToscaListValueConverter() {
51 public static ToscaListValueConverter getInstance() {
56 public Object convertToToscaValue(String value, String innerType, Map<String, DataTypeDefinition> dataTypes) {
61 ToscaPropertyType innerToscaType = ToscaPropertyType.isValidType(innerType);
62 ToscaValueConverter innerConverter = null;
63 boolean isScalar = true;
64 if (innerToscaType != null) {
65 innerConverter = innerToscaType.getValueConverter();
67 DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType);
68 if (dataTypeDefinition != null) {
69 ToscaPropertyType toscaPropertyType = null;
70 if ((toscaPropertyType = isScalarType(dataTypeDefinition)) != null) {
71 innerConverter = toscaPropertyType.getValueConverter();
74 innerConverter = ToscaMapValueConverter.getInstance();
77 log.debug("inner Tosca Type is null");
81 JsonElement jsonElement;
82 jsonElement = parseToJson(value);
83 if (jsonElement == null || jsonElement.isJsonNull()) {
84 log.debug("convertToToscaValue json element is null");
87 if (!jsonElement.isJsonArray()) {
88 // get_input all array like get_input: qrouter_names
89 return handleComplexJsonValue(jsonElement);
91 JsonArray asJsonArray = jsonElement.getAsJsonArray();
92 ArrayList<Object> toscaList = new ArrayList<>();
93 final boolean isScalarF = isScalar;
94 final ToscaValueConverter innerConverterFinal = innerConverter;
95 asJsonArray.forEach(e -> {
96 Object convertedValue = null;
98 if (e.isJsonPrimitive()) {
99 String jsonAsString = e.getAsString();
100 log.debug("try to convert scalar value {}", jsonAsString);
101 convertedValue = innerConverterFinal.convertToToscaValue(jsonAsString, innerType, dataTypes);
103 convertedValue = handleComplexJsonValue(e);
106 JsonObject asJsonObject = e.getAsJsonObject();
107 Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();
108 DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType);
109 Map<String, PropertyDefinition> allProperties = getAllProperties(dataTypeDefinition);
110 Map<String, Object> toscaObjectPresentation = new HashMap<>();
111 for (Entry<String, JsonElement> entry : entrySet) {
112 String propName = entry.getKey();
113 JsonElement elementValue = entry.getValue();
114 PropertyDefinition propertyDefinition = allProperties.get(propName);
115 if (propertyDefinition == null) {
116 log.debug("The property {} was not found under data type {}", propName, dataTypeDefinition.getName());
119 String type = propertyDefinition.getType();
120 ToscaPropertyType propertyType = ToscaPropertyType.isValidType(type);
122 if (propertyType != null) {
123 if (elementValue.isJsonPrimitive()) {
124 ToscaValueConverter valueConverter = propertyType.getValueConverter();
125 convValue = valueConverter.convertToToscaValue(elementValue.getAsString(), type, dataTypes);
127 if (JsonUtils.isEmptyJson(elementValue)) {
130 if (ToscaPropertyType.MAP == propertyType || ToscaPropertyType.LIST == propertyType) {
131 ToscaValueConverter valueConverter = propertyType.getValueConverter();
132 String json = gson.toJson(elementValue);
133 String innerTypeRecursive = propertyDefinition.getSchema().getProperty().getType();
134 convValue = valueConverter.convertToToscaValue(json, innerTypeRecursive, dataTypes);
136 convValue = handleComplexJsonValue(elementValue);
141 String json = gson.toJson(elementValue);
142 convValue = convertToToscaValue(json, type, dataTypes);
144 toscaObjectPresentation.put(propName, convValue);
146 convertedValue = toscaObjectPresentation;
148 toscaList.add(convertedValue);
151 } catch (JsonParseException e) {
152 log.debug("Failed to parse json : {}", value, e);
153 BeEcompErrorManager.getInstance().logBeInvalidJsonInput("List Converter");
158 private JsonElement parseToJson(final String value) {
160 final StringReader reader = new StringReader(value);
161 final JsonReader jsonReader = new JsonReader(reader);
162 jsonReader.setLenient(true);
163 return JsonParser.parseReader(jsonReader);
164 } catch (final JsonSyntaxException e) {
165 log.debug("convertToToscaValue failed to parse json value :", e);