7e2f8766ba07f9ac7582a2cbae156c84cfca4554
[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 java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Map.Entry;
29 import java.util.Set;
30
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;
36
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;
42
43 public class ToscaValueBaseConverter {
44         protected Gson gson = new Gson();
45         private static Logger log = LoggerFactory.getLogger(ToscaValueBaseConverter.class.getName());
46
47         protected Map<String, PropertyDefinition> getAllProperties(DataTypeDefinition dataTypeDefinition) {
48
49                 Map<String, PropertyDefinition> allParentsProps = new HashMap<>();
50
51                 while (dataTypeDefinition != null) {
52
53                         List<PropertyDefinition> currentParentsProps = dataTypeDefinition.getProperties();
54                         if (currentParentsProps != null) {
55                                 currentParentsProps.stream().forEach(p -> allParentsProps.put(p.getName(), p));
56                         }
57
58                         dataTypeDefinition = dataTypeDefinition.getDerivedFrom();
59                 }
60
61                 return allParentsProps;
62         }
63
64         public ToscaPropertyType isScalarType(DataTypeDefinition dataTypeDef) {
65
66                 ToscaPropertyType result = null;
67
68                 DataTypeDefinition dataType = dataTypeDef;
69
70                 while (dataType != null) {
71
72                         String name = dataType.getName();
73                         ToscaPropertyType typeIfScalar = ToscaPropertyType.getTypeIfScalar(name);
74                         if (typeIfScalar != null) {
75                                 result = typeIfScalar;
76                                 break;
77                         }
78
79                         dataType = dataType.getDerivedFrom();
80                 }
81
82                 return result;
83         }
84
85         public Object handleComplexJsonValue(JsonElement elementValue) {
86                 Object jsonValue = null;
87
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);
98                                 } else {
99                                         Object object;
100                                         if (entry.getValue().isJsonPrimitive()) {
101                                                 object = json2JavaPrimitive(entry.getValue().getAsJsonPrimitive());
102                                         } else {
103                                                 object = handleComplexJsonValue(entry.getValue());
104                                         }
105                                         value.put(entry.getKey(), object);
106                                 }
107                         }
108                         jsonValue = value;
109                 } else {
110                         if (elementValue.isJsonArray()) {
111                                 jsonValue = handleJsonArray(elementValue);
112                         } else {
113                                 if (elementValue.isJsonPrimitive()) {
114                                         jsonValue = json2JavaPrimitive(elementValue.getAsJsonPrimitive());
115                                 } else {
116                                         log.debug("not supported json type {} ", elementValue);
117                                 }
118                         }
119                 }
120
121                 return jsonValue;
122         }
123
124         private List<Object> handleJsonArray(JsonElement entry) {
125                 List<Object> array = new ArrayList<>();
126                 JsonArray jsonArray = entry.getAsJsonArray();
127                 Iterator<JsonElement> iterator = jsonArray.iterator();
128                 while (iterator.hasNext()) {
129                         Object object;
130                         JsonElement element = iterator.next();
131                         if (element.isJsonPrimitive()) {
132                                 object = json2JavaPrimitive(element.getAsJsonPrimitive());
133                         } else {
134                                 object = handleComplexJsonValue(element);
135                         }
136                         array.add(object);
137                 }
138                 return array;
139         }
140
141         public Object json2JavaPrimitive(JsonPrimitive prim) {
142                 if (prim.isBoolean()) {
143                         return prim.getAsBoolean();
144                 } else if (prim.isString()) {
145                         return prim.getAsString();
146                 } else if (prim.isNumber()) {
147                         String strRepesentation = prim.getAsString();
148                         if (strRepesentation.contains(".")) {
149                                 return prim.getAsDouble();
150                         } else {
151                                 return prim.getAsInt();
152                         }
153                 } else {
154                         throw new IllegalStateException();
155                 }
156         }
157
158         /**
159          * checks is received Object empty or equals null or not It is relevant only
160          * if received Object is instance of String, Map or List class.
161          * 
162          * @param convertedValue
163          * @return
164          */
165         static public boolean isEmptyObjectValue(Object convertedValue) {
166                 if (convertedValue == null) {
167                         return true;
168                 } else if (convertedValue instanceof String && ((String) convertedValue).isEmpty()) {
169                         return true;
170                 } else if (convertedValue instanceof Map && ((Map) convertedValue).isEmpty()) {
171                         return true;
172                 } else if (convertedValue instanceof List && ((List) convertedValue).isEmpty()) {
173                         return true;
174                 }
175                 return false;
176         }
177 }