c9b5db8e870eeaf8304ce658f9b65033b1117ed1
[sdc.git] /
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
21 package org.openecomp.sdc.be.model.tosca.converters;
22
23 import com.google.gson.Gson;
24 import com.google.gson.JsonArray;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonObject;
27 import com.google.gson.JsonParser;
28 import com.google.gson.JsonPrimitive;
29 import com.google.gson.JsonSyntaxException;
30 import com.google.gson.stream.JsonReader;
31 import java.io.StringReader;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Map.Entry;
37 import org.openecomp.sdc.be.model.DataTypeDefinition;
38 import org.openecomp.sdc.be.model.PropertyDefinition;
39 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
40 import org.openecomp.sdc.common.log.wrappers.Logger;
41
42 public class ToscaValueBaseConverter {
43
44     private static final Logger log = Logger.getLogger(ToscaValueBaseConverter.class.getName());
45     protected Gson gson = new Gson();
46
47     /**
48      * 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.
49      *
50      * @param convertedValue
51      * @return
52      */
53     public static boolean isEmptyObjectValue(Object convertedValue) {
54         if ((convertedValue == null) || (convertedValue instanceof String && ((String) convertedValue).isEmpty()) || (convertedValue instanceof Map
55             && ((Map) convertedValue).isEmpty()) || (convertedValue instanceof List && ((List) convertedValue).isEmpty())) {
56             return true;
57         }
58         return false;
59     }
60
61     protected Map<String, PropertyDefinition> getAllProperties(DataTypeDefinition dataTypeDefinition) {
62         Map<String, PropertyDefinition> allParentsProps = new HashMap<>();
63         while (dataTypeDefinition != null) {
64             List<PropertyDefinition> currentParentsProps = dataTypeDefinition.getProperties();
65             if (currentParentsProps != null) {
66                 currentParentsProps.stream().forEach(p -> allParentsProps.put(p.getName(), p));
67             }
68             dataTypeDefinition = dataTypeDefinition.getDerivedFrom();
69         }
70         return allParentsProps;
71     }
72
73     public ToscaPropertyType isScalarType(DataTypeDefinition dataTypeDef) {
74         ToscaPropertyType result = null;
75         DataTypeDefinition dataType = dataTypeDef;
76         while (dataType != null) {
77             String name = dataType.getName();
78             ToscaPropertyType typeIfScalar = ToscaPropertyType.getTypeIfScalar(name);
79             if (typeIfScalar != null) {
80                 result = typeIfScalar;
81                 break;
82             }
83             dataType = dataType.getDerivedFrom();
84         }
85         return result;
86     }
87
88     public Object handleComplexJsonValue(final JsonElement jsonElement) {
89         if (jsonElement.isJsonNull()) {
90             return null;
91         }
92         if (jsonElement.isJsonObject()) {
93             return handleJsonObject(jsonElement);
94         }
95         if (jsonElement.isJsonArray()) {
96             return handleJsonArray(jsonElement);
97         }
98         if (jsonElement.isJsonPrimitive()) {
99             if (!isJsonElementAJsonPrimitive(jsonElement)) {
100                 return handleComplexJsonValue(parseToJson(jsonElement.getAsString()));
101             }
102             return json2JavaPrimitive(jsonElement.getAsJsonPrimitive());
103         }
104         log.debug("JSON type '{}' not supported", jsonElement);
105         return null;
106     }
107
108     private Map<String, Object> handleJsonObject(final JsonElement jsonElement) {
109         final JsonObject jsonObject = jsonElement.getAsJsonObject();
110         if (jsonObject.entrySet().isEmpty()) {
111             return null;
112         }
113         final Map<String, Object> jsonObjectAsMap = new HashMap<>();
114         for (final Entry<String, JsonElement> entry : jsonObject.entrySet()) {
115             final Object value = handleComplexJsonValue(entry.getValue());
116             if (value != null) {
117                 jsonObjectAsMap.put(entry.getKey(), value);
118             }
119
120         }
121         return jsonObjectAsMap.isEmpty() ? null : jsonObjectAsMap;
122     }
123
124     private List<Object> handleJsonArray(final JsonElement entry) {
125         final List<Object> jsonAsArray = new ArrayList<>();
126         final JsonArray jsonArray = entry.getAsJsonArray();
127         for (final JsonElement jsonElement : jsonArray) {
128             jsonAsArray.add(handleComplexJsonValue(jsonElement));
129         }
130         return jsonAsArray;
131     }
132
133     public Object json2JavaPrimitive(final JsonPrimitive jsonPrimitive) {
134         if (jsonPrimitive.isBoolean()) {
135             return jsonPrimitive.getAsBoolean();
136         }
137         if (jsonPrimitive.isString()) {
138             return jsonPrimitive.getAsString();
139         }
140         if (jsonPrimitive.isNumber()) {
141             if (jsonPrimitive.getAsString().contains(".")) {
142                 return jsonPrimitive.getAsDouble();
143             }
144             return jsonPrimitive.getAsInt();
145         }
146         throw new IllegalStateException(String.format("JSON primitive not supported: %s", jsonPrimitive));
147     }
148
149     public JsonElement parseToJson(final String value) {
150         try {
151             final StringReader reader = new StringReader(value);
152             final JsonReader jsonReader = new JsonReader(reader);
153             jsonReader.setLenient(true);
154             return JsonParser.parseReader(jsonReader);
155         } catch (final JsonSyntaxException e) {
156             log.debug("convertToToscaValue failed to parse json value :", e);
157             return null;
158         }
159     }
160
161     public boolean isJsonElementAJsonPrimitive(JsonElement jsonElement) {
162         if (!jsonElement.isJsonPrimitive()) {
163             return false;
164         }
165         String elementAsString = jsonElement.getAsString();
166         JsonElement elementAsJson = parseToJson(elementAsString);
167         if (elementAsJson.isJsonPrimitive() || elementAsJson.isJsonNull()) {
168             return true;
169         }
170         return false;
171     }
172 }