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.openecomp.sdc.be.config.BeEcompErrorManager;
36 import org.openecomp.sdc.be.model.DataTypeDefinition;
37 import org.openecomp.sdc.be.model.PropertyDefinition;
38 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
39 import org.openecomp.sdc.common.log.wrappers.Logger;
41 public class ToscaListValueConverter extends ToscaValueBaseConverter implements ToscaValueConverter {
43 private static final Logger log = Logger.getLogger(ToscaListValueConverter.class.getName());
44 private static ToscaListValueConverter listConverter = new ToscaListValueConverter();
45 private JsonParser jsonParser = new JsonParser();
47 private ToscaListValueConverter() {
50 public static ToscaListValueConverter getInstance() {
55 public Object convertToToscaValue(String value, String innerType, Map<String, DataTypeDefinition> dataTypes) {
60 ToscaPropertyType innerToscaType = ToscaPropertyType.isValidType(innerType);
61 ToscaValueConverter innerConverter = null;
62 boolean isScalar = true;
63 if (innerToscaType != null) {
64 innerConverter = innerToscaType.getValueConverter();
66 DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType);
67 if (dataTypeDefinition != null) {
68 ToscaPropertyType toscaPropertyType = null;
69 if ((toscaPropertyType = isScalarType(dataTypeDefinition)) != null) {
70 innerConverter = toscaPropertyType.getValueConverter();
73 innerConverter = ToscaMapValueConverter.getInstance();
76 log.debug("inner Tosca Type is null");
80 JsonElement jsonElement = null;
82 StringReader reader = new StringReader(value);
83 JsonReader jsonReader = new JsonReader(reader);
84 jsonReader.setLenient(true);
85 jsonElement = jsonParser.parse(jsonReader);
86 } catch (JsonSyntaxException e) {
87 log.debug("convertToToscaValue failed to parse json value :", e);
90 if (jsonElement == null || jsonElement.isJsonNull()) {
91 log.debug("convertToToscaValue json element is null");
94 if (!jsonElement.isJsonArray()) {
95 // get_input all array like get_input: qrouter_names
96 return handleComplexJsonValue(jsonElement);
98 JsonArray asJsonArray = jsonElement.getAsJsonArray();
99 ArrayList<Object> toscaList = new ArrayList<>();
100 final boolean isScalarF = isScalar;
101 final ToscaValueConverter innerConverterFinal = innerConverter;
102 asJsonArray.forEach(e -> {
103 Object convertedValue = null;
105 if (e.isJsonPrimitive()) {
106 String jsonAsString = e.getAsString();
107 log.debug("try to convert scalar value {}", jsonAsString);
108 convertedValue = innerConverterFinal.convertToToscaValue(jsonAsString, innerType, dataTypes);
110 convertedValue = handleComplexJsonValue(e);
113 JsonObject asJsonObject = e.getAsJsonObject();
114 Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();
115 DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType);
116 Map<String, PropertyDefinition> allProperties = getAllProperties(dataTypeDefinition);
117 Map<String, Object> toscaObjectPresentation = new HashMap<>();
118 for (Entry<String, JsonElement> entry : entrySet) {
119 String propName = entry.getKey();
120 JsonElement elementValue = entry.getValue();
121 PropertyDefinition propertyDefinition = allProperties.get(propName);
122 if (propertyDefinition == null) {
123 log.debug("The property {} was not found under data type {}", propName, dataTypeDefinition.getName());
127 String type = propertyDefinition.getType();
128 ToscaPropertyType propertyType = ToscaPropertyType.isValidType(type);
130 if (propertyType != null) {
131 if (elementValue.isJsonPrimitive()) {
132 ToscaValueConverter valueConverter = propertyType.getValueConverter();
133 convValue = valueConverter.convertToToscaValue(elementValue.getAsString(), type, dataTypes);
135 if (ToscaPropertyType.MAP.equals(type) || ToscaPropertyType.LIST.equals(propertyType)) {
136 ToscaValueConverter valueConverter = propertyType.getValueConverter();
137 String json = gson.toJson(elementValue);
138 String innerTypeRecursive = propertyDefinition.getSchema().getProperty().getType();
139 convValue = valueConverter.convertToToscaValue(json, innerTypeRecursive, dataTypes);
141 convValue = handleComplexJsonValue(elementValue);
145 String json = gson.toJson(elementValue);
146 convValue = convertToToscaValue(json, type, dataTypes);
148 toscaObjectPresentation.put(propName, convValue);
150 convertedValue = toscaObjectPresentation;
152 toscaList.add(convertedValue);
155 } catch (JsonParseException e) {
156 log.debug("Failed to parse json : {}", value, e);
157 BeEcompErrorManager.getInstance().logBeInvalidJsonInput("List Converter");