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.Gson;
24 import com.google.gson.JsonArray;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonObject;
27 import com.google.gson.JsonParser;
28 import com.google.gson.JsonPrimitive;
29 import com.google.gson.JsonSyntaxException;
30 import com.google.gson.stream.JsonReader;
31 import java.io.StringReader;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
36 import java.util.Map.Entry;
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;
42 public class ToscaValueBaseConverter {
44 private static final Logger log = Logger.getLogger(ToscaValueBaseConverter.class.getName());
45 protected Gson gson = new Gson();
48 * checks is received Object empty or equals null or not It is relevant only if received Object is instance of String, Map or List class.
50 * @param convertedValue
53 public static boolean isEmptyObjectValue(Object convertedValue) {
54 if ((convertedValue == null) || (convertedValue instanceof String && ((String) convertedValue).isEmpty()) || (convertedValue instanceof Map
55 && ((Map) convertedValue).isEmpty()) || (convertedValue instanceof List && ((List) convertedValue).isEmpty())) {
61 protected Map<String, PropertyDefinition> getAllProperties(DataTypeDefinition dataTypeDefinition) {
62 Map<String, PropertyDefinition> allParentsProps = new HashMap<>();
63 while (dataTypeDefinition != null) {
64 List<PropertyDefinition> currentParentsProps = dataTypeDefinition.getProperties();
65 if (currentParentsProps != null) {
66 currentParentsProps.stream().forEach(p -> allParentsProps.put(p.getName(), p));
68 dataTypeDefinition = dataTypeDefinition.getDerivedFrom();
70 return allParentsProps;
73 public ToscaPropertyType isScalarType(DataTypeDefinition dataTypeDef) {
74 ToscaPropertyType result = null;
75 DataTypeDefinition dataType = dataTypeDef;
76 while (dataType != null) {
77 String name = dataType.getName();
78 ToscaPropertyType typeIfScalar = ToscaPropertyType.getTypeIfScalar(name);
79 if (typeIfScalar != null) {
80 result = typeIfScalar;
83 dataType = dataType.getDerivedFrom();
88 public Object handleComplexJsonValue(final JsonElement jsonElement) {
89 if (jsonElement.isJsonNull()) {
92 if (jsonElement.isJsonObject()) {
93 return handleJsonObject(jsonElement);
95 if (jsonElement.isJsonArray()) {
96 return handleJsonArray(jsonElement);
98 if (jsonElement.isJsonPrimitive()) {
99 if (!isJsonElementAJsonPrimitive(jsonElement)) {
100 return handleComplexJsonValue(parseToJson(jsonElement.getAsString()));
102 return json2JavaPrimitive(jsonElement.getAsJsonPrimitive());
104 log.debug("JSON type '{}' not supported", jsonElement);
108 private Map<String, Object> handleJsonObject(final JsonElement jsonElement) {
109 final JsonObject jsonObject = jsonElement.getAsJsonObject();
110 if (jsonObject.entrySet().isEmpty()) {
113 final Map<String, Object> jsonObjectAsMap = new HashMap<>();
114 for (final Entry<String, JsonElement> entry : jsonObject.entrySet()) {
115 final Object value = handleComplexJsonValue(entry.getValue());
117 jsonObjectAsMap.put(entry.getKey(), value);
121 return jsonObjectAsMap.isEmpty() ? null : jsonObjectAsMap;
124 private List<Object> handleJsonArray(final JsonElement entry) {
125 final List<Object> jsonAsArray = new ArrayList<>();
126 final JsonArray jsonArray = entry.getAsJsonArray();
127 for (final JsonElement jsonElement : jsonArray) {
128 jsonAsArray.add(handleComplexJsonValue(jsonElement));
133 public Object json2JavaPrimitive(final JsonPrimitive jsonPrimitive) {
134 if (jsonPrimitive.isBoolean()) {
135 return jsonPrimitive.getAsBoolean();
137 if (jsonPrimitive.isString()) {
138 return jsonPrimitive.getAsString();
140 if (jsonPrimitive.isNumber()) {
141 if (jsonPrimitive.getAsString().contains(".")) {
142 return jsonPrimitive.getAsDouble();
144 return jsonPrimitive.getAsInt();
146 throw new IllegalStateException(String.format("JSON primitive not supported: %s", jsonPrimitive));
149 public JsonElement parseToJson(final String value) {
151 final StringReader reader = new StringReader(value);
152 final JsonReader jsonReader = new JsonReader(reader);
153 jsonReader.setLenient(true);
154 return JsonParser.parseReader(jsonReader);
155 } catch (final JsonSyntaxException e) {
156 log.debug("convertToToscaValue failed to parse json value :", e);
161 public boolean isJsonElementAJsonPrimitive(JsonElement jsonElement) {
162 if (!jsonElement.isJsonPrimitive()) {
165 String elementAsString = jsonElement.getAsString();
166 JsonElement elementAsJson = parseToJson(elementAsString);
167 if (elementAsJson.isJsonPrimitive() || elementAsJson.isJsonNull()) {