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.Iterator;
30 import java.util.List;
32 import java.util.Map.Entry;
34 import org.openecomp.sdc.be.model.DataTypeDefinition;
35 import org.openecomp.sdc.be.model.PropertyDefinition;
36 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
37 import org.openecomp.sdc.common.log.wrappers.Logger;
39 public class ToscaValueBaseConverter {
41 private static final Logger log = Logger.getLogger(ToscaValueBaseConverter.class.getName());
42 protected Gson gson = new Gson();
45 * 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.
47 * @param convertedValue
50 public static boolean isEmptyObjectValue(Object convertedValue) {
51 if ((convertedValue == null) || (convertedValue instanceof String && ((String) convertedValue).isEmpty()) || (convertedValue instanceof Map
52 && ((Map) convertedValue).isEmpty()) || (convertedValue instanceof List && ((List) convertedValue).isEmpty())) {
58 protected Map<String, PropertyDefinition> getAllProperties(DataTypeDefinition dataTypeDefinition) {
59 Map<String, PropertyDefinition> allParentsProps = new HashMap<>();
60 while (dataTypeDefinition != null) {
61 List<PropertyDefinition> currentParentsProps = dataTypeDefinition.getProperties();
62 if (currentParentsProps != null) {
63 currentParentsProps.stream().forEach(p -> allParentsProps.put(p.getName(), p));
65 dataTypeDefinition = dataTypeDefinition.getDerivedFrom();
67 return allParentsProps;
70 public ToscaPropertyType isScalarType(DataTypeDefinition dataTypeDef) {
71 ToscaPropertyType result = null;
72 DataTypeDefinition dataType = dataTypeDef;
73 while (dataType != null) {
74 String name = dataType.getName();
75 ToscaPropertyType typeIfScalar = ToscaPropertyType.getTypeIfScalar(name);
76 if (typeIfScalar != null) {
77 result = typeIfScalar;
80 dataType = dataType.getDerivedFrom();
85 public Object handleComplexJsonValue(JsonElement elementValue) {
86 Object jsonValue = null;
87 Map<String, Object> value = new HashMap<>();
88 if (elementValue.isJsonObject()) {
89 JsonObject jsonOb = elementValue.getAsJsonObject();
90 Set<Entry<String, JsonElement>> entrySet = jsonOb.entrySet();
91 Iterator<Entry<String, JsonElement>> iteratorEntry = entrySet.iterator();
92 while (iteratorEntry.hasNext()) {
93 Entry<String, JsonElement> entry = iteratorEntry.next();
94 if (entry.getValue().isJsonArray()) {
95 List<Object> array = handleJsonArray(entry.getValue());
96 value.put(entry.getKey(), array);
99 if (entry.getValue().isJsonPrimitive()) {
100 object = json2JavaPrimitive(entry.getValue().getAsJsonPrimitive());
102 object = handleComplexJsonValue(entry.getValue());
104 value.put(entry.getKey(), object);
109 if (elementValue.isJsonArray()) {
110 jsonValue = handleJsonArray(elementValue);
112 if (elementValue.isJsonPrimitive()) {
113 jsonValue = json2JavaPrimitive(elementValue.getAsJsonPrimitive());
115 log.debug("not supported json type ");
122 private List<Object> handleJsonArray(JsonElement entry) {
123 List<Object> array = new ArrayList<>();
124 JsonArray jsonArray = entry.getAsJsonArray();
125 Iterator<JsonElement> iterator = jsonArray.iterator();
126 while (iterator.hasNext()) {
128 JsonElement element = iterator.next();
129 if (element.isJsonPrimitive()) {
130 object = json2JavaPrimitive(element.getAsJsonPrimitive());
132 object = handleComplexJsonValue(element);
139 public Object json2JavaPrimitive(JsonPrimitive prim) {
140 if (prim.isBoolean()) {
141 return prim.getAsBoolean();
142 } else if (prim.isString()) {
143 return prim.getAsString();
144 } else if (prim.isNumber()) {
145 String strRepesentation = prim.getAsString();
146 if (strRepesentation.contains(".")) {
147 return prim.getAsDouble();
149 return prim.getAsInt();
152 throw new IllegalStateException();