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