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 Map<String, Object> jsonObjectAsMap = new HashMap<>();
102 final JsonObject jsonObject = jsonElement.getAsJsonObject();
103 for (final Entry<String, JsonElement> entry : jsonObject.entrySet()) {
104 jsonObjectAsMap.put(entry.getKey(), handleComplexJsonValue(entry.getValue()));
106 return jsonObjectAsMap;
109 private List<Object> handleJsonArray(final JsonElement entry) {
110 final List<Object> jsonAsArray = new ArrayList<>();
111 final JsonArray jsonArray = entry.getAsJsonArray();
112 for (final JsonElement jsonElement : jsonArray) {
113 jsonAsArray.add(handleComplexJsonValue(jsonElement));
118 public Object json2JavaPrimitive(final JsonPrimitive jsonPrimitive) {
119 if (jsonPrimitive.isBoolean()) {
120 return jsonPrimitive.getAsBoolean();
122 if (jsonPrimitive.isString()) {
123 return jsonPrimitive.getAsString();
125 if (jsonPrimitive.isNumber()) {
126 if (jsonPrimitive.getAsString().contains(".")) {
127 return jsonPrimitive.getAsDouble();
129 return jsonPrimitive.getAsInt();
131 throw new IllegalStateException(String.format("JSON primitive not supported: %s", jsonPrimitive));