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.apache.commons.lang3.math.NumberUtils;
38 import org.openecomp.sdc.be.model.DataTypeDefinition;
39 import org.openecomp.sdc.be.model.PropertyDefinition;
40 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
41 import org.openecomp.sdc.common.log.wrappers.Logger;
43 public class ToscaValueBaseConverter {
45 private static final Logger log = Logger.getLogger(ToscaValueBaseConverter.class.getName());
46 protected Gson gson = new Gson();
49 * 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.
51 * @param convertedValue
54 public static boolean isEmptyObjectValue(Object convertedValue) {
55 if ((convertedValue == null) || (convertedValue instanceof String && ((String) convertedValue).isEmpty()) || (convertedValue instanceof Map
56 && ((Map) convertedValue).isEmpty()) || (convertedValue instanceof List && ((List) convertedValue).isEmpty())) {
62 protected Map<String, PropertyDefinition> getAllProperties(DataTypeDefinition dataTypeDefinition) {
63 Map<String, PropertyDefinition> allParentsProps = new HashMap<>();
64 while (dataTypeDefinition != null) {
65 List<PropertyDefinition> currentParentsProps = dataTypeDefinition.getProperties();
66 if (currentParentsProps != null) {
67 currentParentsProps.stream().forEach(p -> allParentsProps.put(p.getName(), p));
69 dataTypeDefinition = dataTypeDefinition.getDerivedFrom();
71 return allParentsProps;
74 public ToscaPropertyType isScalarType(DataTypeDefinition dataTypeDef) {
75 ToscaPropertyType result = null;
76 DataTypeDefinition dataType = dataTypeDef;
77 while (dataType != null) {
78 String name = dataType.getName();
79 ToscaPropertyType typeIfScalar = ToscaPropertyType.getTypeIfScalar(name);
80 if (typeIfScalar != null) {
81 result = typeIfScalar;
84 dataType = dataType.getDerivedFrom();
89 public Object handleComplexJsonValue(final JsonElement jsonElement) {
90 if (jsonElement.isJsonNull()) {
93 if (jsonElement.isJsonObject()) {
94 return handleJsonObject(jsonElement);
96 if (jsonElement.isJsonArray()) {
97 return handleJsonArray(jsonElement);
99 if (jsonElement.isJsonPrimitive()) {
100 if (!isJsonElementAJsonPrimitive(jsonElement)) {
101 return handleComplexJsonValue(parseToJson(jsonElement.getAsString()));
103 return json2JavaPrimitive(jsonElement.getAsJsonPrimitive());
105 log.debug("JSON type '{}' not supported", jsonElement);
109 private Map<String, Object> handleJsonObject(final JsonElement jsonElement) {
110 final JsonObject jsonObject = jsonElement.getAsJsonObject();
111 if (jsonObject.entrySet().isEmpty()) {
114 final Map<String, Object> jsonObjectAsMap = new HashMap<>();
115 for (final Entry<String, JsonElement> entry : jsonObject.entrySet()) {
116 final Object value = handleComplexJsonValue(entry.getValue());
118 jsonObjectAsMap.put(entry.getKey(), value);
122 return jsonObjectAsMap.isEmpty() ? null : jsonObjectAsMap;
125 private List<Object> handleJsonArray(final JsonElement entry) {
126 final List<Object> jsonAsArray = new ArrayList<>();
127 final JsonArray jsonArray = entry.getAsJsonArray();
128 for (final JsonElement jsonElement : jsonArray) {
129 jsonAsArray.add(handleComplexJsonValue(jsonElement));
134 public Object json2JavaPrimitive(final JsonPrimitive jsonPrimitive) {
135 if (jsonPrimitive.isBoolean()) {
136 return jsonPrimitive.getAsBoolean();
138 if (jsonPrimitive.isString() && !NumberUtils.isCreatable(jsonPrimitive.getAsString())) {
139 return jsonPrimitive.getAsString();
141 if (jsonPrimitive.isNumber() ||
142 (jsonPrimitive.isString() && NumberUtils.isCreatable(jsonPrimitive.getAsString()))) {
143 if (jsonPrimitive.getAsString().contains(".")) {
144 return jsonPrimitive.getAsDouble();
146 return jsonPrimitive.getAsInt();
148 throw new IllegalStateException(String.format("JSON primitive not supported: %s", jsonPrimitive));
151 public JsonElement parseToJson(final String value) {
153 final StringReader reader = new StringReader(value);
154 final JsonReader jsonReader = new JsonReader(reader);
155 jsonReader.setLenient(true);
156 return JsonParser.parseReader(jsonReader);
157 } catch (final JsonSyntaxException e) {
158 log.debug("convertToToscaValue failed to parse json value :", e);
163 public boolean isJsonElementAJsonPrimitive(JsonElement jsonElement) {
164 if (!jsonElement.isJsonPrimitive()) {
167 String elementAsString = jsonElement.getAsString();
168 JsonElement elementAsJson = parseToJson(elementAsString);
169 if (elementAsJson.isJsonPrimitive() || elementAsJson.isJsonNull()) {