e886327481d3aa869802f22b0534e7db5285a91a
[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                                 log.debug("not supported json type {} ",elementValue);
114                         }
115                 }
116                 
117                 return jsonValue;
118         }
119
120         private List<Object> handleJsonArray(JsonElement entry) {
121                 List<Object> array = new ArrayList<>();
122                 JsonArray jsonArray = entry.getAsJsonArray();
123                 Iterator<JsonElement> iterator = jsonArray.iterator();
124                 while (iterator.hasNext()) {
125                         Object object;
126                         JsonElement element = iterator.next();
127                         if (element.isJsonPrimitive()) {
128                                 object = json2JavaPrimitive(element.getAsJsonPrimitive());
129                         } else {
130                                 object = handleComplexJsonValue(element);
131                         }
132                         array.add(object);
133                 }
134                 return array;
135         }
136
137         public Object json2JavaPrimitive(JsonPrimitive prim) {
138                 if (prim.isBoolean()) {
139                         return prim.getAsBoolean();
140                 } else if (prim.isString()) {
141                         return prim.getAsString();
142                 } else if (prim.isNumber()) {
143                         String strRepesentation = prim.getAsString();
144                         if (strRepesentation.contains(".")) {
145                                 return prim.getAsDouble();
146                         } else {
147                                 return prim.getAsInt();
148                         }
149                 } else {
150                         throw new IllegalStateException();
151                 }
152         }
153 }