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 log.debug("not supported json type {} ",elementValue);
120 private List<Object> handleJsonArray(JsonElement entry) {
121 List<Object> array = new ArrayList<>();
122 JsonArray jsonArray = entry.getAsJsonArray();
123 Iterator<JsonElement> iterator = jsonArray.iterator();
124 while (iterator.hasNext()) {
126 JsonElement element = iterator.next();
127 if (element.isJsonPrimitive()) {
128 object = json2JavaPrimitive(element.getAsJsonPrimitive());
130 object = handleComplexJsonValue(element);
137 public Object json2JavaPrimitive(JsonPrimitive prim) {
138 if (prim.isBoolean()) {
139 return prim.getAsBoolean();
140 } else if (prim.isString()) {
141 return prim.getAsString();
142 } else if (prim.isNumber()) {
143 String strRepesentation = prim.getAsString();
144 if (strRepesentation.contains(".")) {
145 return prim.getAsDouble();
147 return prim.getAsInt();
150 throw new IllegalStateException();