Reformat catalog-model
[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.Iterator;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Map.Entry;
33 import java.util.Set;
34 import org.openecomp.sdc.be.model.DataTypeDefinition;
35 import org.openecomp.sdc.be.model.PropertyDefinition;
36 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
37 import org.openecomp.sdc.common.log.wrappers.Logger;
38
39 public class ToscaValueBaseConverter {
40
41     private static final Logger log = Logger.getLogger(ToscaValueBaseConverter.class.getName());
42     protected Gson gson = new Gson();
43
44     /**
45      * 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.
46      *
47      * @param convertedValue
48      * @return
49      */
50     public static boolean isEmptyObjectValue(Object convertedValue) {
51         if ((convertedValue == null) || (convertedValue instanceof String && ((String) convertedValue).isEmpty()) || (convertedValue instanceof Map
52             && ((Map) convertedValue).isEmpty()) || (convertedValue instanceof List && ((List) convertedValue).isEmpty())) {
53             return true;
54         }
55         return false;
56     }
57
58     protected Map<String, PropertyDefinition> getAllProperties(DataTypeDefinition dataTypeDefinition) {
59         Map<String, PropertyDefinition> allParentsProps = new HashMap<>();
60         while (dataTypeDefinition != null) {
61             List<PropertyDefinition> currentParentsProps = dataTypeDefinition.getProperties();
62             if (currentParentsProps != null) {
63                 currentParentsProps.stream().forEach(p -> allParentsProps.put(p.getName(), p));
64             }
65             dataTypeDefinition = dataTypeDefinition.getDerivedFrom();
66         }
67         return allParentsProps;
68     }
69
70     public ToscaPropertyType isScalarType(DataTypeDefinition dataTypeDef) {
71         ToscaPropertyType result = null;
72         DataTypeDefinition dataType = dataTypeDef;
73         while (dataType != null) {
74             String name = dataType.getName();
75             ToscaPropertyType typeIfScalar = ToscaPropertyType.getTypeIfScalar(name);
76             if (typeIfScalar != null) {
77                 result = typeIfScalar;
78                 break;
79             }
80             dataType = dataType.getDerivedFrom();
81         }
82         return result;
83     }
84
85     public Object handleComplexJsonValue(JsonElement elementValue) {
86         Object jsonValue = null;
87         Map<String, Object> value = new HashMap<>();
88         if (elementValue.isJsonObject()) {
89             JsonObject jsonOb = elementValue.getAsJsonObject();
90             Set<Entry<String, JsonElement>> entrySet = jsonOb.entrySet();
91             Iterator<Entry<String, JsonElement>> iteratorEntry = entrySet.iterator();
92             while (iteratorEntry.hasNext()) {
93                 Entry<String, JsonElement> entry = iteratorEntry.next();
94                 if (entry.getValue().isJsonArray()) {
95                     List<Object> array = handleJsonArray(entry.getValue());
96                     value.put(entry.getKey(), array);
97                 } else {
98                     Object object;
99                     if (entry.getValue().isJsonPrimitive()) {
100                         object = json2JavaPrimitive(entry.getValue().getAsJsonPrimitive());
101                     } else {
102                         object = handleComplexJsonValue(entry.getValue());
103                     }
104                     value.put(entry.getKey(), object);
105                 }
106             }
107             jsonValue = value;
108         } else {
109             if (elementValue.isJsonArray()) {
110                 jsonValue = handleJsonArray(elementValue);
111             } else {
112                 if (elementValue.isJsonPrimitive()) {
113                     jsonValue = json2JavaPrimitive(elementValue.getAsJsonPrimitive());
114                 } else {
115                     log.debug("not supported json type ");
116                 }
117             }
118         }
119         return jsonValue;
120     }
121
122     private List<Object> handleJsonArray(JsonElement entry) {
123         List<Object> array = new ArrayList<>();
124         JsonArray jsonArray = entry.getAsJsonArray();
125         Iterator<JsonElement> iterator = jsonArray.iterator();
126         while (iterator.hasNext()) {
127             Object object;
128             JsonElement element = iterator.next();
129             if (element.isJsonPrimitive()) {
130                 object = json2JavaPrimitive(element.getAsJsonPrimitive());
131             } else {
132                 object = handleComplexJsonValue(element);
133             }
134             array.add(object);
135         }
136         return array;
137     }
138
139     public Object json2JavaPrimitive(JsonPrimitive prim) {
140         if (prim.isBoolean()) {
141             return prim.getAsBoolean();
142         } else if (prim.isString()) {
143             return prim.getAsString();
144         } else if (prim.isNumber()) {
145             String strRepesentation = prim.getAsString();
146             if (strRepesentation.contains(".")) {
147                 return prim.getAsDouble();
148             } else {
149                 return prim.getAsInt();
150             }
151         } else {
152             throw new IllegalStateException();
153         }
154     }
155 }