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