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