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 java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.List;
28 import java.util.Map.Entry;
31 import org.openecomp.sdc.be.model.DataTypeDefinition;
32 import org.openecomp.sdc.be.model.PropertyDefinition;
33 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import com.google.gson.Gson;
38 import com.google.gson.JsonArray;
39 import com.google.gson.JsonElement;
40 import com.google.gson.JsonObject;
41 import com.google.gson.JsonPrimitive;
43 public class ToscaValueBaseConverter {
44 protected Gson gson = new Gson();
45 private static Logger log = LoggerFactory.getLogger(ToscaValueBaseConverter.class.getName());
47 protected Map<String, PropertyDefinition> getAllProperties(DataTypeDefinition dataTypeDefinition) {
49 Map<String, PropertyDefinition> allParentsProps = new HashMap<>();
51 while (dataTypeDefinition != null) {
53 List<PropertyDefinition> currentParentsProps = dataTypeDefinition.getProperties();
54 if (currentParentsProps != null) {
55 currentParentsProps.stream().forEach(p -> allParentsProps.put(p.getName(), p));
58 dataTypeDefinition = dataTypeDefinition.getDerivedFrom();
61 return allParentsProps;
64 public ToscaPropertyType isScalarType(DataTypeDefinition dataTypeDef) {
66 ToscaPropertyType result = null;
68 DataTypeDefinition dataType = dataTypeDef;
70 while (dataType != null) {
72 String name = dataType.getName();
73 ToscaPropertyType typeIfScalar = ToscaPropertyType.getTypeIfScalar(name);
74 if (typeIfScalar != null) {
75 result = typeIfScalar;
79 dataType = dataType.getDerivedFrom();
85 public Object handleComplexJsonValue(JsonElement elementValue) {
86 Object jsonValue = null;
88 Map<String, Object> value = new HashMap<String, Object>();
89 if (elementValue.isJsonObject()) {
90 JsonObject jsonOb = elementValue.getAsJsonObject();
91 Set<Entry<String, JsonElement>> entrySet = jsonOb.entrySet();
92 Iterator<Entry<String, JsonElement>> iteratorEntry = entrySet.iterator();
93 while (iteratorEntry.hasNext()) {
94 Entry<String, JsonElement> entry = iteratorEntry.next();
95 if (entry.getValue().isJsonArray()) {
96 List<Object> array = handleJsonArray(entry.getValue());
97 value.put(entry.getKey(), array);
100 if (entry.getValue().isJsonPrimitive()) {
101 object = json2JavaPrimitive(entry.getValue().getAsJsonPrimitive());
103 object = handleComplexJsonValue(entry.getValue());
105 value.put(entry.getKey(), object);
110 if (elementValue.isJsonArray()) {
111 jsonValue = handleJsonArray(elementValue);
113 if (elementValue.isJsonPrimitive()) {
114 jsonValue = json2JavaPrimitive(elementValue.getAsJsonPrimitive());
116 log.debug("not supported json type {} ", elementValue);
124 private List<Object> handleJsonArray(JsonElement entry) {
125 List<Object> array = new ArrayList<>();
126 JsonArray jsonArray = entry.getAsJsonArray();
127 Iterator<JsonElement> iterator = jsonArray.iterator();
128 while (iterator.hasNext()) {
130 JsonElement element = iterator.next();
131 if (element.isJsonPrimitive()) {
132 object = json2JavaPrimitive(element.getAsJsonPrimitive());
134 object = handleComplexJsonValue(element);
141 public Object json2JavaPrimitive(JsonPrimitive prim) {
142 if (prim.isBoolean()) {
143 return prim.getAsBoolean();
144 } else if (prim.isString()) {
145 return prim.getAsString();
146 } else if (prim.isNumber()) {
147 String strRepesentation = prim.getAsString();
148 if (strRepesentation.contains(".")) {
149 return prim.getAsDouble();
151 return prim.getAsInt();
154 throw new IllegalStateException();
159 * checks is received Object empty or equals null or not It is relevant only
160 * if received Object is instance of String, Map or List class.
162 * @param convertedValue
165 static public boolean isEmptyObjectValue(Object convertedValue) {
166 if (convertedValue == null) {
168 } else if (convertedValue instanceof String && ((String) convertedValue).isEmpty()) {
170 } else if (convertedValue instanceof Map && ((Map) convertedValue).isEmpty()) {
172 } else if (convertedValue instanceof List && ((List) convertedValue).isEmpty()) {