043446e783d6ecbbafa58882e1c68318c0bd695e
[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                                         log.debug("try to convert scalar value {}", e.getAsString());
113                                         if (e.getAsString() == null) {
114                                                 convertedValue = null;
115                                         } else {
116                                                 JsonElement singleElement = jsonParser.parse(e.getAsString());
117                                                 if (singleElement.isJsonPrimitive()) {
118                                                         convertedValue = innerConverterFinal.convertToToscaValue(e.getAsString(), innerType,
119                                                                         dataTypes);
120                                                 } else {
121                                                         convertedValue = handleComplexJsonValue(singleElement);
122                                                 }
123                                         }
124                                 } else {
125                                         JsonObject asJsonObject = e.getAsJsonObject();
126                                         Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();
127
128                                         DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType);
129                                         Map<String, PropertyDefinition> allProperties = getAllProperties(dataTypeDefinition);
130                                         Map<String, Object> toscaObjectPresentation = new HashMap<>();
131                                         // log.debug("try to convert datatype value {}",
132                                         // e.getAsString());
133
134                                         for (Entry<String, JsonElement> entry : entrySet) {
135                                                 String propName = entry.getKey();
136
137                                                 JsonElement elementValue = entry.getValue();
138                                                 PropertyDefinition propertyDefinition = allProperties.get(propName);
139                                                 if (propertyDefinition == null) {
140                                                         log.debug("The property {} was not found under data type {}", propName, dataTypeDefinition.getName());
141                                                         continue;
142                                                         // return null;
143                                                 }
144                                                 String type = propertyDefinition.getType();
145                                                 ToscaPropertyType propertyType = ToscaPropertyType.isValidType(type);
146                                                 Object convValue;
147                                                 if (propertyType != null) {
148                                                         if (elementValue.isJsonPrimitive()) {
149                                                                 ToscaValueConverter valueConverter = propertyType.getValueConverter();
150                                                                 convValue = valueConverter.convertToToscaValue(elementValue.getAsString(), type,
151                                                                                 dataTypes);
152                                                         } else {
153                                                                 if (ToscaPropertyType.MAP.equals(type) || ToscaPropertyType.LIST.equals(propertyType)) {
154                                                                         ToscaValueConverter valueConverter = propertyType.getValueConverter();
155                                                                         String json = gson.toJson(elementValue);
156                                                                         String innerTypeRecursive = propertyDefinition.getSchema().getProperty().getType();
157                                                                         convValue = valueConverter.convertToToscaValue(json, innerTypeRecursive, dataTypes);
158                                                                 } else {
159                                                                         convValue = handleComplexJsonValue(elementValue);
160                                                                 }
161                                                         }
162                                                 } else {
163                                                         String json = gson.toJson(elementValue);
164                                                         convValue = convertToToscaValue(json, type, dataTypes);
165                                                 }
166                                                 toscaObjectPresentation.put(propName, convValue);
167                                         }
168                                         convertedValue = toscaObjectPresentation;
169                                 }
170                                 toscaList.add(convertedValue);
171                         });
172                         return toscaList;
173                 } catch (
174
175                 JsonParseException e) {
176                         log.debug("Failed to parse json : {}. {}", value, e);
177                         BeEcompErrorManager.getInstance().logBeInvalidJsonInput("List Converter");
178                         return null;
179                 }
180         }
181
182 }