f3189ee968453b35fd19de2c1cebd560dbac5251
[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.io.StringReader;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Map.Entry;
28 import java.util.Set;
29
30 import org.openecomp.sdc.be.config.BeEcompErrorManager;
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.JsonArray;
38 import com.google.gson.JsonElement;
39 import com.google.gson.JsonObject;
40 import com.google.gson.JsonParseException;
41 import com.google.gson.JsonParser;
42 import com.google.gson.JsonSyntaxException;
43 import com.google.gson.stream.JsonReader;
44
45 public class ToscaListValueConverter extends ToscaValueBaseConverter implements ToscaValueConverter {
46         private static ToscaListValueConverter listConverter = new ToscaListValueConverter();
47         private JsonParser jsonParser = new JsonParser();
48         private static Logger log = LoggerFactory.getLogger(ToscaListValueConverter.class.getName());
49
50         public static ToscaListValueConverter getInstance() {
51                 return listConverter;
52         }
53
54         private ToscaListValueConverter() {
55
56         }
57
58         @Override
59         public Object convertToToscaValue(String value, String innerType, Map<String, DataTypeDefinition> dataTypes) {
60                 if (value == null) {
61                         return null;
62                 }
63                 try {
64                         ToscaPropertyType innerToscaType = ToscaPropertyType.isValidType(innerType);
65                         ToscaValueConverter innerConverter = null;
66                         boolean isScalar = true;
67                         if (innerToscaType != null) {
68                                 innerConverter = innerToscaType.getValueConverter();
69                         } else {
70                                 DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType);
71
72                                 if (dataTypeDefinition != null) {
73                                         ToscaPropertyType toscaPropertyType = null;
74                                         if ((toscaPropertyType = isScalarType(dataTypeDefinition)) != null) {
75                                                 innerConverter = toscaPropertyType.getValueConverter();
76                                         } else {
77                                                 isScalar = false;
78                                                 innerConverter = ToscaMapValueConverter.getInstance();
79                                         }
80                                 } else {
81                                         log.debug("inner Tosca Type is null");
82                                         return value;
83                                 }
84                         }
85                         JsonElement jsonElement = null;
86                         try {
87                                 StringReader reader = new StringReader(value);
88                                 JsonReader jsonReader = new JsonReader(reader);
89                                 jsonReader.setLenient(true);
90
91                                 jsonElement = jsonParser.parse(jsonReader);
92                         } catch (JsonSyntaxException e) {
93                                 log.debug("convertToToscaValue failed to parse json value :", e);
94                                 return null;
95                         }
96                         if (jsonElement == null || true == jsonElement.isJsonNull()) {
97                                 log.debug("convertToToscaValue json element is null");
98                                 return null;
99                         }
100                         if (jsonElement.isJsonArray() == false) {
101                                 // get_input all array like get_input: qrouter_names
102                                 return handleComplexJsonValue(jsonElement);
103                         }
104                         JsonArray asJsonArray = jsonElement.getAsJsonArray();
105
106                         ArrayList<Object> toscaList = new ArrayList<Object>();
107                         final boolean isScalarF = isScalar;
108                         final ToscaValueConverter innerConverterFinal = innerConverter;
109                         asJsonArray.forEach(e -> {
110                                 Object convertedValue = null;
111                                 if (isScalarF) {
112                                         if (e.isJsonPrimitive()) {
113                                                 String jsonAsString = e.getAsString();
114                                                 log.debug("try to convert scalar value {}", jsonAsString);
115                                                 convertedValue = innerConverterFinal.convertToToscaValue(jsonAsString, innerType,
116                                                                 dataTypes);
117                                         } else {
118                                                 convertedValue = handleComplexJsonValue(e);
119                                         }
120
121                                 } else {
122                                         JsonObject asJsonObject = e.getAsJsonObject();
123                                         Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();
124
125                                         DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType);
126                                         Map<String, PropertyDefinition> allProperties = getAllProperties(dataTypeDefinition);
127                                         Map<String, Object> toscaObjectPresentation = new HashMap<>();
128                                         
129                                         for (Entry<String, JsonElement> entry : entrySet) {
130                                                 String propName = entry.getKey();
131
132                                                 JsonElement elementValue = entry.getValue();
133                                                 PropertyDefinition propertyDefinition = allProperties.get(propName);
134                                                 if (propertyDefinition == null) {
135                                                         log.debug("The property {} was not found under data type {}",propName,dataTypeDefinition.getName());
136                                                         continue;
137                                                         // return null;
138                                                 }
139                                                 String type = propertyDefinition.getType();
140                                                 ToscaPropertyType propertyType = ToscaPropertyType.isValidType(type);
141                                                 Object convValue;
142                                                 if (propertyType != null) {
143                                                         if (elementValue.isJsonPrimitive()) {
144                                                                 ToscaValueConverter valueConverter = propertyType.getValueConverter();
145                                                                 convValue = valueConverter.convertToToscaValue(elementValue.getAsString(), type,
146                                                                                 dataTypes);
147                                                         } else {
148                                                                 if (ToscaPropertyType.MAP.equals(type) || ToscaPropertyType.LIST.equals(propertyType)) {
149                                                                         ToscaValueConverter valueConverter = propertyType.getValueConverter();
150                                                                         String json = gson.toJson(elementValue);
151                                                                         String innerTypeRecursive = propertyDefinition.getSchema().getProperty().getType();
152                                                                         convValue = valueConverter.convertToToscaValue(json, innerTypeRecursive, dataTypes);
153                                                                 } else {
154                                                                         convValue = handleComplexJsonValue(elementValue);
155                                                                 }
156                                                         }
157                                                 } else {
158                                                         String json = gson.toJson(elementValue);
159                                                         convValue = convertToToscaValue(json, type, dataTypes);
160                                                 }
161                                                 toscaObjectPresentation.put(propName, convValue);
162                                         }
163                                         convertedValue = toscaObjectPresentation;
164                                 }
165                                 toscaList.add(convertedValue);
166                         });
167                         return toscaList;
168                 } catch (
169
170                 JsonParseException e) {
171                         log.debug("Failed to parse json : {}", value, e);
172                         BeEcompErrorManager.getInstance().logBeInvalidJsonInput("List Converter");
173                         return null;
174                 }
175         }
176
177 }