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.Gson;
23 import com.google.gson.JsonArray;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonObject;
26 import com.google.gson.JsonPrimitive;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
31 import java.util.Map.Entry;
32 import org.openecomp.sdc.be.model.DataTypeDefinition;
33 import org.openecomp.sdc.be.model.PropertyDefinition;
34 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
35 import org.openecomp.sdc.common.log.wrappers.Logger;
37 public class ToscaValueBaseConverter {
39 private static final Logger log = Logger.getLogger(ToscaValueBaseConverter.class.getName());
40 protected Gson gson = new Gson();
43 * 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.
45 * @param convertedValue
48 public static boolean isEmptyObjectValue(Object convertedValue) {
49 if ((convertedValue == null) || (convertedValue instanceof String && ((String) convertedValue).isEmpty()) || (convertedValue instanceof Map
50 && ((Map) convertedValue).isEmpty()) || (convertedValue instanceof List && ((List) convertedValue).isEmpty())) {
56 protected Map<String, PropertyDefinition> getAllProperties(DataTypeDefinition dataTypeDefinition) {
57 Map<String, PropertyDefinition> allParentsProps = new HashMap<>();
58 while (dataTypeDefinition != null) {
59 List<PropertyDefinition> currentParentsProps = dataTypeDefinition.getProperties();
60 if (currentParentsProps != null) {
61 currentParentsProps.stream().forEach(p -> allParentsProps.put(p.getName(), p));
63 dataTypeDefinition = dataTypeDefinition.getDerivedFrom();
65 return allParentsProps;
68 public ToscaPropertyType isScalarType(DataTypeDefinition dataTypeDef) {
69 ToscaPropertyType result = null;
70 DataTypeDefinition dataType = dataTypeDef;
71 while (dataType != null) {
72 String name = dataType.getName();
73 ToscaPropertyType typeIfScalar = ToscaPropertyType.getTypeIfScalar(name);
74 if (typeIfScalar != null) {
75 result = typeIfScalar;
78 dataType = dataType.getDerivedFrom();
83 public Object handleComplexJsonValue(final JsonElement jsonElement) {
84 if (jsonElement.isJsonNull()) {
87 if (jsonElement.isJsonObject()) {
88 return handleJsonObject(jsonElement);
90 if (jsonElement.isJsonArray()) {
91 return handleJsonArray(jsonElement);
93 if (jsonElement.isJsonPrimitive()) {
94 return json2JavaPrimitive(jsonElement.getAsJsonPrimitive());
96 log.debug("JSON type '{}' not supported", jsonElement);
100 private Map<String, Object> handleJsonObject(final JsonElement jsonElement) {
101 final JsonObject jsonObject = jsonElement.getAsJsonObject();
102 if (jsonObject.entrySet().isEmpty()) {
105 final Map<String, Object> jsonObjectAsMap = new HashMap<>();
106 for (final Entry<String, JsonElement> entry : jsonObject.entrySet()) {
107 final Object value = handleComplexJsonValue(entry.getValue());
109 jsonObjectAsMap.put(entry.getKey(), value);
113 return jsonObjectAsMap.isEmpty() ? null : jsonObjectAsMap;
116 private List<Object> handleJsonArray(final JsonElement entry) {
117 final List<Object> jsonAsArray = new ArrayList<>();
118 final JsonArray jsonArray = entry.getAsJsonArray();
119 for (final JsonElement jsonElement : jsonArray) {
120 jsonAsArray.add(handleComplexJsonValue(jsonElement));
125 public Object json2JavaPrimitive(final JsonPrimitive jsonPrimitive) {
126 if (jsonPrimitive.isBoolean()) {
127 return jsonPrimitive.getAsBoolean();
129 if (jsonPrimitive.isString()) {
130 return jsonPrimitive.getAsString();
132 if (jsonPrimitive.isNumber()) {
133 if (jsonPrimitive.getAsString().contains(".")) {
134 return jsonPrimitive.getAsDouble();
136 return jsonPrimitive.getAsInt();
138 throw new IllegalStateException(String.format("JSON primitive not supported: %s", jsonPrimitive));