ee254811b1a32500e69e02091318a5d110d8ab3c
[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 Map<String, Object> jsonObjectAsMap = new HashMap<>();
102         final JsonObject jsonObject = jsonElement.getAsJsonObject();
103         for (final Entry<String, JsonElement> entry : jsonObject.entrySet()) {
104             jsonObjectAsMap.put(entry.getKey(), handleComplexJsonValue(entry.getValue()));
105         }
106         return jsonObjectAsMap;
107     }
108
109     private List<Object> handleJsonArray(final JsonElement entry) {
110         final List<Object> jsonAsArray = new ArrayList<>();
111         final JsonArray jsonArray = entry.getAsJsonArray();
112         for (final JsonElement jsonElement : jsonArray) {
113             jsonAsArray.add(handleComplexJsonValue(jsonElement));
114         }
115         return jsonAsArray;
116     }
117
118     public Object json2JavaPrimitive(final JsonPrimitive jsonPrimitive) {
119         if (jsonPrimitive.isBoolean()) {
120             return jsonPrimitive.getAsBoolean();
121         }
122         if (jsonPrimitive.isString()) {
123             return jsonPrimitive.getAsString();
124         }
125         if (jsonPrimitive.isNumber()) {
126             if (jsonPrimitive.getAsString().contains(".")) {
127                 return jsonPrimitive.getAsDouble();
128             }
129             return jsonPrimitive.getAsInt();
130         }
131         throw new IllegalStateException(String.format("JSON primitive not supported: %s", jsonPrimitive));
132     }
133 }