Support TOSCA functions in operation inputs
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / tosca / converters / ToscaMapValueConverter.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.JsonArray;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonObject;
26 import com.google.gson.JsonParseException;
27 import com.google.gson.JsonParser;
28 import com.google.gson.JsonSyntaxException;
29 import com.google.gson.stream.JsonReader;
30 import java.io.StringReader;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Map.Entry;
36 import java.util.Set;
37 import org.openecomp.sdc.be.config.BeEcompErrorManager;
38 import org.openecomp.sdc.be.model.DataTypeDefinition;
39 import org.openecomp.sdc.be.model.PropertyDefinition;
40 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
41 import org.openecomp.sdc.common.log.wrappers.Logger;
42 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
43
44 public class ToscaMapValueConverter extends ToscaValueBaseConverter implements ToscaValueConverter {
45
46     private static final Logger log = Logger.getLogger(ToscaMapValueConverter.class.getName());
47     private static final ToscaMapValueConverter mapConverter = new ToscaMapValueConverter();
48
49     private ToscaMapValueConverter() {
50     }
51
52     public static ToscaMapValueConverter getInstance() {
53         return mapConverter;
54     }
55
56     @Override
57     public Object convertToToscaValue(String value, String innerType, Map<String, DataTypeDefinition> dataTypes) {
58         if (value == null) {
59             return null;
60         }
61         try {
62             ToscaPropertyType innerToscaType = ToscaPropertyType.isValidType(innerType);
63             ToscaValueConverter innerConverter = null;
64             boolean isScalar = true;
65             List<PropertyDefinition> allPropertiesRecursive = new ArrayList<>();
66             if (innerToscaType != null) {
67                 innerConverter = innerToscaType.getValueConverter();
68             } else {
69                 DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType);
70                 if (dataTypeDefinition != null) {
71                     ToscaPropertyType toscaPropertyType;
72                     if ((toscaPropertyType = isScalarType(dataTypeDefinition)) != null) {
73                         innerConverter = toscaPropertyType.getValueConverter();
74                     } else {
75                         isScalar = false;
76                         allPropertiesRecursive.addAll(dataTypeDefinition.getProperties());
77                         DataTypeDefinition derivedFrom = dataTypeDefinition.getDerivedFrom();
78                         while (!derivedFrom.getName().equals("tosca.datatypes.Root")) {
79                             allPropertiesRecursive.addAll(derivedFrom.getProperties());
80                             derivedFrom = derivedFrom.getDerivedFrom();
81                         }
82                     }
83                 } else {
84                     log.debug("inner Tosca Type is null");
85                     return value;
86                 }
87             }
88             JsonElement jsonElement;
89             jsonElement = parseToJson(value);
90             if (jsonElement == null || jsonElement.isJsonNull()) {
91                 log.debug("convertToToscaValue json element is null");
92                 return null;
93             }
94             JsonObject asJsonObject = jsonElement.getAsJsonObject();
95             Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();
96             Map<String, Object> toscaMap = new HashMap<>();
97             final boolean isScalarF = isScalar;
98             final ToscaValueConverter innerConverterFinal = innerConverter;
99             entrySet.forEach(e -> {
100                 if (!e.getValue().isJsonNull()) {
101                     convertEntry(innerType, dataTypes, allPropertiesRecursive, toscaMap, isScalarF, innerConverterFinal, e);
102                 }
103             });
104             return toscaMap;
105         } catch (JsonParseException e) {
106             log.debug("Failed to parse json : {}", value, e);
107             BeEcompErrorManager.getInstance().logBeInvalidJsonInput("List Converter");
108             return null;
109         }
110     }
111
112     private void convertEntry(final String innerType, final Map<String, DataTypeDefinition> dataTypes,
113                               final List<PropertyDefinition> allPropertiesRecursive, final Map<String, Object> toscaMap, final boolean isScalarF,
114                               final ToscaValueConverter innerConverterFinal, final Entry<String, JsonElement> e) {
115         log.debug("try convert element ");
116         boolean scalar = isScalarF;
117         String propType = innerType;
118         ToscaValueConverter innerConverterProp = innerConverterFinal;
119         if (!scalar) {
120             for (PropertyDefinition pd : allPropertiesRecursive) {
121                 if (pd.getName().equals(e.getKey())) {
122                     propType = pd.getType();
123                     final DataTypeDefinition pdDataType = dataTypes.get(propType);
124                     final ToscaPropertyType toscaPropType = isScalarType(pdDataType);
125                     if (toscaPropType != null) {
126                         scalar = true;
127                         propType = toscaPropType.getType();
128                         innerConverterProp = toscaPropType.getValueConverter();
129                     }
130                     break;
131                 }
132             }
133         }
134         final Object convertedValue = convertDataTypeToToscaObject(propType, dataTypes, innerConverterProp, scalar, e.getValue(), false, false);
135         toscaMap.put(e.getKey(), convertedValue);
136     }
137
138     public Object convertDataTypeToToscaObject(String innerType, Map<String, DataTypeDefinition> dataTypes, ToscaValueConverter innerConverter,
139                                                final boolean isScalarF, JsonElement entryValue, boolean preserveEmptyValue,
140                                                final boolean isToscaFunction) {
141         Object convertedValue;
142         if (isScalarF && isJsonElementAJsonPrimitive(entryValue)) {
143             return innerConverter.convertToToscaValue(entryValue.getAsString(), innerType, dataTypes);
144         } else {
145             if (entryValue.isJsonPrimitive()) {
146                 return handleComplexJsonValue(entryValue);
147             }
148             // ticket 228696523 created   / DE272734 / Bug 154492 Fix
149             if (entryValue instanceof JsonArray) {
150                 ArrayList<Object> toscaObjectPresentationArray = new ArrayList<>();
151                 JsonArray jsonArray = entryValue.getAsJsonArray();
152                 for (JsonElement jsonElement : jsonArray) {
153                     Object convertedDataTypeToToscaMap = convertDataTypeToToscaMap(innerType, dataTypes, isScalarF, jsonElement, preserveEmptyValue,
154                         isToscaFunction);
155                     toscaObjectPresentationArray.add(convertedDataTypeToToscaMap);
156                 }
157                 convertedValue = toscaObjectPresentationArray;
158             } else {
159                 convertedValue = convertDataTypeToToscaMap(innerType, dataTypes, isScalarF, entryValue, preserveEmptyValue, isToscaFunction);
160             }
161         }
162         return convertedValue;
163     }
164
165     private Object convertDataTypeToToscaMap(String innerType, Map<String, DataTypeDefinition> dataTypes, final boolean isScalarF,
166                                              JsonElement entryValue, boolean preserveEmptyValue, final boolean isToscaFunction) {
167         Object convertedValue;
168         if (entryValue.isJsonPrimitive()) {
169             return json2JavaPrimitive(entryValue.getAsJsonPrimitive());
170         }
171         JsonObject asJsonObjectIn = entryValue.getAsJsonObject();
172         if (!isToscaFunction) {
173             DataTypePropertyConverter.getInstance()
174                 .mergeDataTypeDefaultValuesWithPropertyValue(asJsonObjectIn, innerType, dataTypes);
175         }
176         Map<String, Object> toscaObjectPresentation = new HashMap<>();
177         Set<Entry<String, JsonElement>> entrySetIn = asJsonObjectIn.entrySet();
178         for (Entry<String, JsonElement> entry : entrySetIn) {
179             String propName = entry.getKey();
180             JsonElement elementValue = entry.getValue();
181             Object convValue;
182             if (!isScalarF) {
183                 DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType);
184                 Map<String, PropertyDefinition> allProperties = getAllProperties(dataTypeDefinition);
185                 PropertyDefinition propertyDefinition = allProperties.get(propName);
186                 if (propertyDefinition == null) {
187                     log.trace("The property {} was not found under data type . Parse as map", propName);
188                     if (elementValue.isJsonPrimitive()) {
189                         convValue = elementValue.getAsJsonPrimitive();
190                     } else {
191                         convValue = handleComplexJsonValue(elementValue);
192                     }
193                 } else {
194                     String type = propertyDefinition.getType();
195                     ToscaPropertyType propertyType = ToscaPropertyType.isValidType(type);
196                     if (propertyType != null) {
197                         if (isJsonElementAJsonPrimitive(elementValue)) {
198                             ToscaValueConverter valueConverter = propertyType.getValueConverter();
199                             convValue = valueConverter.convertToToscaValue(elementValue.getAsString(), type, dataTypes);
200                         } else {
201                             if (ToscaPropertyType.MAP.equals(propertyType) || ToscaPropertyType.LIST.equals(propertyType)) {
202                                 ToscaValueConverter valueConverter = propertyType.getValueConverter();
203                                 String json = gson.toJson(elementValue);
204                                 String innerTypeRecursive = propertyDefinition.getSchema().getProperty().getType();
205                                 convValue = valueConverter.convertToToscaValue(json, innerTypeRecursive, dataTypes);
206                             } else {
207                                 convValue = handleComplexJsonValue(elementValue);
208                             }
209                         }
210                     } else {
211                         convValue = convertToToscaValue(elementValue.toString(), type, dataTypes);
212                     }
213                 }
214             } else {
215                 if (isJsonElementAJsonPrimitive(elementValue)) {
216                     convValue = json2JavaPrimitive(elementValue.getAsJsonPrimitive());
217                 } else {
218                     convValue = handleComplexJsonValue(elementValue);
219                 }
220             }
221             if (preserveEmptyValue || !isEmptyObjectValue(convValue) || isGetPolicyValue(propName)) {
222                 toscaObjectPresentation.put(propName, convValue);
223             }
224         }
225         convertedValue = toscaObjectPresentation;
226         return convertedValue;
227     }
228
229     private boolean isGetPolicyValue(String key) {
230         return key.equals(ToscaFunctions.GET_POLICY.getFunctionName());
231     }
232 }