7505d2af8868d5325d3e8f1eaea0bc7a95351d38
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / tosca / converters / ToscaValueBaseConverter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.openecomp.sdc.be.model.tosca.converters;
21
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;
30 import java.util.Map;
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;
36
37 public class ToscaValueBaseConverter {
38
39     private static final Logger log = Logger.getLogger(ToscaValueBaseConverter.class.getName());
40     protected Gson gson = new Gson();
41
42     /**
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.
44      *
45      * @param convertedValue
46      * @return
47      */
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())) {
51             return true;
52         }
53         return false;
54     }
55
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));
62             }
63             dataTypeDefinition = dataTypeDefinition.getDerivedFrom();
64         }
65         return allParentsProps;
66     }
67
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;
76                 break;
77             }
78             dataType = dataType.getDerivedFrom();
79         }
80         return result;
81     }
82
83     public Object handleComplexJsonValue(final JsonElement jsonElement) {
84         if (jsonElement.isJsonNull()) {
85             return null;
86         }
87         if (jsonElement.isJsonObject()) {
88             return handleJsonObject(jsonElement);
89         }
90         if (jsonElement.isJsonArray()) {
91             return handleJsonArray(jsonElement);
92         }
93         if (jsonElement.isJsonPrimitive()) {
94             return json2JavaPrimitive(jsonElement.getAsJsonPrimitive());
95         }
96         log.debug("JSON type '{}' not supported", jsonElement);
97         return null;
98     }
99
100     private Map<String, Object> handleJsonObject(final JsonElement jsonElement) {
101         final JsonObject jsonObject = jsonElement.getAsJsonObject();
102         if (jsonObject.entrySet().isEmpty()) {
103             return null;
104         }
105         final Map<String, Object> jsonObjectAsMap = new HashMap<>();
106         for (final Entry<String, JsonElement> entry : jsonObject.entrySet()) {
107             final Object value = handleComplexJsonValue(entry.getValue());
108             if (value != null) {
109                 jsonObjectAsMap.put(entry.getKey(), value);
110             }
111
112         }
113         return jsonObjectAsMap.isEmpty() ? null : jsonObjectAsMap;
114     }
115
116     private List<Object> handleJsonArray(final JsonElement entry) {
117         final List<Object> jsonAsArray = new ArrayList<>();
118         final JsonArray jsonArray = entry.getAsJsonArray();
119         for (final JsonElement jsonElement : jsonArray) {
120             jsonAsArray.add(handleComplexJsonValue(jsonElement));
121         }
122         return jsonAsArray;
123     }
124
125     public Object json2JavaPrimitive(final JsonPrimitive jsonPrimitive) {
126         if (jsonPrimitive.isBoolean()) {
127             return jsonPrimitive.getAsBoolean();
128         }
129         if (jsonPrimitive.isString()) {
130             return jsonPrimitive.getAsString();
131         }
132         if (jsonPrimitive.isNumber()) {
133             if (jsonPrimitive.getAsString().contains(".")) {
134                 return jsonPrimitive.getAsDouble();
135             }
136             return jsonPrimitive.getAsInt();
137         }
138         throw new IllegalStateException(String.format("JSON primitive not supported: %s", jsonPrimitive));
139     }
140 }