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.*;
24 import org.openecomp.sdc.be.model.DataTypeDefinition;
25 import org.openecomp.sdc.be.model.PropertyDefinition;
26 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
27 import org.openecomp.sdc.common.log.wrappers.Logger;
30 import java.util.Map.Entry;
32 public class ToscaValueBaseConverter {
33 protected Gson gson = new Gson();
34 private static final Logger log = Logger.getLogger(ToscaValueBaseConverter.class.getName());
36 protected Map<String, PropertyDefinition> getAllProperties(DataTypeDefinition dataTypeDefinition) {
38 Map<String, PropertyDefinition> allParentsProps = new HashMap<>();
40 while (dataTypeDefinition != null) {
42 List<PropertyDefinition> currentParentsProps = dataTypeDefinition.getProperties();
43 if (currentParentsProps != null) {
44 currentParentsProps.stream().forEach(p -> allParentsProps.put(p.getName(), p));
47 dataTypeDefinition = dataTypeDefinition.getDerivedFrom();
50 return allParentsProps;
53 public ToscaPropertyType isScalarType(DataTypeDefinition dataTypeDef) {
55 ToscaPropertyType result = null;
57 DataTypeDefinition dataType = dataTypeDef;
59 while (dataType != null) {
61 String name = dataType.getName();
62 ToscaPropertyType typeIfScalar = ToscaPropertyType.getTypeIfScalar(name);
63 if (typeIfScalar != null) {
64 result = typeIfScalar;
68 dataType = dataType.getDerivedFrom();
74 public Object handleComplexJsonValue(JsonElement elementValue) {
75 Object jsonValue = null;
77 Map<String, Object> value = new HashMap<>();
78 if (elementValue.isJsonObject()) {
79 JsonObject jsonOb = elementValue.getAsJsonObject();
80 Set<Entry<String, JsonElement>> entrySet = jsonOb.entrySet();
81 Iterator<Entry<String, JsonElement>> iteratorEntry = entrySet.iterator();
82 while (iteratorEntry.hasNext()) {
83 Entry<String, JsonElement> entry = iteratorEntry.next();
84 if (entry.getValue().isJsonArray()) {
85 List<Object> array = handleJsonArray(entry.getValue());
86 value.put(entry.getKey(), array);
89 if (entry.getValue().isJsonPrimitive()) {
90 object = json2JavaPrimitive(entry.getValue().getAsJsonPrimitive());
92 object = handleComplexJsonValue(entry.getValue());
94 value.put(entry.getKey(), object);
99 if (elementValue.isJsonArray()) {
100 jsonValue = handleJsonArray(elementValue);
102 if (elementValue.isJsonPrimitive()) {
103 jsonValue = json2JavaPrimitive(elementValue.getAsJsonPrimitive());
105 log.debug("not supported json type ");
113 private List<Object> handleJsonArray(JsonElement entry) {
114 List<Object> array = new ArrayList<>();
115 JsonArray jsonArray = entry.getAsJsonArray();
116 Iterator<JsonElement> iterator = jsonArray.iterator();
117 while (iterator.hasNext()) {
119 JsonElement element = iterator.next();
120 if (element.isJsonPrimitive()) {
121 object = json2JavaPrimitive(element.getAsJsonPrimitive());
123 object = handleComplexJsonValue(element);
130 public Object json2JavaPrimitive(JsonPrimitive prim) {
131 if (prim.isBoolean()) {
132 return prim.getAsBoolean();
133 } else if (prim.isString()) {
134 return prim.getAsString();
135 } else if (prim.isNumber()) {
136 String strRepesentation = prim.getAsString();
137 if (strRepesentation.contains(".")) {
138 return prim.getAsDouble();
140 return prim.getAsInt();
143 throw new IllegalStateException();
148 * checks is received Object empty or equals null or not It is relevant only
149 * if received Object is instance of String, Map or List class.
151 * @param convertedValue
154 public static boolean isEmptyObjectValue(Object convertedValue) {
155 if ((convertedValue == null)
156 || (convertedValue instanceof String && ((String) convertedValue).isEmpty())
157 || (convertedValue instanceof Map && ((Map) convertedValue).isEmpty())
158 || (convertedValue instanceof List && ((List) convertedValue).isEmpty()))