2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.model.tosca.converters;
23 import com.google.common.base.Strings;
24 import com.google.gson.*;
25 import fj.data.Either;
26 import org.apache.commons.lang3.tuple.ImmutablePair;
27 import org.openecomp.sdc.be.config.BeEcompErrorManager;
28 import org.openecomp.sdc.be.model.DataTypeDefinition;
29 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
30 import org.openecomp.sdc.be.model.tosca.validators.DataTypeValidatorConverter;
31 import org.openecomp.sdc.be.model.tosca.validators.ListValidator;
32 import org.openecomp.sdc.common.log.wrappers.Logger;
33 import org.openecomp.sdc.common.util.GsonFactory;
34 import org.openecomp.sdc.common.util.JsonUtils;
36 import java.util.HashMap;
38 import java.util.Map.Entry;
41 public class MapConverter implements PropertyValueConverter {
43 private static MapConverter mapConverter = new MapConverter();
44 private static Gson gson = GsonFactory.getGson();
45 private static final Logger log = Logger.getLogger(ListValidator.class.getName());
47 DataTypeValidatorConverter dataTypeValidatorConverter = DataTypeValidatorConverter.getInstance();
49 private static JsonParser jsonParser = new JsonParser();
51 public static MapConverter getInstance() {
55 public String convert(String value, String innerType, Map<String, DataTypeDefinition> dataTypes) {
57 Either<String, Boolean> convertWithErrorResult = this.convertWithErrorResult(value, innerType, dataTypes);
58 if (convertWithErrorResult.isRight()) {
62 return convertWithErrorResult.left().value();
65 public Either<String, Boolean> convertWithErrorResult(String value, String innerType,
66 Map<String, DataTypeDefinition> dataTypes) {
68 if (Strings.isNullOrEmpty(value) || innerType == null) {
69 return Either.left(value);
72 PropertyValueConverter innerConverter;
73 PropertyValueConverter keyConverter = ToscaPropertyType.STRING.getConverter();
74 ToscaPropertyType innerToscaType = ToscaPropertyType.isValidType(innerType);
76 if (innerToscaType != null) {
77 switch (innerToscaType) {
79 innerConverter = ToscaPropertyType.STRING.getConverter();
82 innerConverter = ToscaPropertyType.INTEGER.getConverter();
85 innerConverter = ToscaPropertyType.FLOAT.getConverter();
88 innerConverter = ToscaPropertyType.BOOLEAN.getConverter();
91 innerConverter = ToscaPropertyType.JSON.getConverter();
94 log.debug("inner Tosca Type is unknown");
95 return Either.left(value);
100 log.debug("inner Tosca Type {} ia a complex data type.", innerType);
102 return convertComplexInnerType(value, innerType, keyConverter,
108 Map<String, String> newMap = new HashMap<>();
110 JsonElement jsonObject = jsonParser.parse(value);
111 JsonObject asJsonObject = jsonObject.getAsJsonObject();
112 Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();
113 for (Entry<String, JsonElement> entry : entrySet) {
114 String key = entry.getKey();
115 JsonElement jsonValue = entry.getValue();
117 key = keyConverter.convert(entry.getKey(), null, dataTypes);
119 String element = JsonUtils.toString(jsonValue);
121 String val = innerConverter.convert(element, null, dataTypes);
122 newMap.put(key, val);
126 switch (innerToscaType) {
128 value = gson.toJson(newMap);
132 Map<String, Integer> intMap = new HashMap<>();
133 for (Map.Entry<String, String> entry : newMap.entrySet()) {
134 objVal = entry.getValue();
135 key = entry.getKey();
136 if (objVal != null) {
137 intMap.put(key, Integer.valueOf(objVal.toString()));
139 intMap.put(key, null);
143 value = gson.toJson(intMap);
147 for (Map.Entry<String, String> entry : newMap.entrySet()) {
148 objVal = entry.getValue();
149 if (objVal == null) {
152 key = entry.getKey();
153 value += "\"" + key + "\":" + objVal.toString() + ",";
155 value = value.substring(0, value.length() - 1);
159 Map<String, Boolean> boolMap = new HashMap<>();
160 for (Map.Entry<String, String> entry : newMap.entrySet()) {
161 objVal = entry.getValue();
162 key = entry.getKey();
163 if (objVal != null) {
164 boolMap.put(key, Boolean.valueOf(objVal.toString()));
166 boolMap.put(key, null);
169 value = gson.toJson(boolMap);
172 value = gson.toJson(newMap);
173 log.debug("inner Tosca Type unknown : {}", innerToscaType);
175 } catch (JsonParseException e) {
176 log.debug("Failed to parse json : {}", value, e);
177 BeEcompErrorManager.getInstance().logBeInvalidJsonInput("Map Converter");
178 return Either.right(false);
181 return Either.left(value);
186 * convert the json value of map when the inner type is a complex data type
190 * @param keyConverter
191 * @param allDataTypes
194 private Either<String, Boolean> convertComplexInnerType(String value, String innerType,
195 PropertyValueConverter keyConverter, Map<String, DataTypeDefinition> allDataTypes) {
197 DataTypeDefinition dataTypeDefinition = allDataTypes.get(innerType);
198 if (dataTypeDefinition == null) {
199 log.debug("Cannot find data type {}", innerType);
200 return Either.right(false);
203 Map<String, JsonElement> newMap = new HashMap<>();
207 JsonElement jsonObject = jsonParser.parse(value);
208 JsonObject asJsonObject = jsonObject.getAsJsonObject();
209 Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();
210 for (Entry<String, JsonElement> entry : entrySet) {
211 String currentKey = keyConverter.convert(entry.getKey(), null, allDataTypes);
213 JsonElement currentValue = entry.getValue();
215 if (currentValue != null) {
217 String element = JsonUtils.toString(currentValue);
219 ImmutablePair<JsonElement, Boolean> validateAndUpdate = dataTypeValidatorConverter
220 .validateAndUpdate(element, dataTypeDefinition, allDataTypes);
221 if (!validateAndUpdate.right.booleanValue()) {
222 log.debug("Cannot parse value {} from type {} of key {}",currentValue,innerType,currentKey);
223 return Either.right(false);
225 JsonElement newValue = validateAndUpdate.left;
226 newMap.put(currentKey, newValue);
228 newMap.put(currentKey, null);
232 } catch (Exception e) {
233 log.debug("Cannot parse value {} of map from inner type {}", value, innerType);
234 return Either.right(false);
237 value = gson.toJson(newMap);
238 return Either.left(value);