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.gson.*;
24 import fj.data.Either;
25 import org.apache.commons.lang3.tuple.ImmutablePair;
26 import org.openecomp.sdc.be.config.BeEcompErrorManager;
27 import org.openecomp.sdc.be.model.DataTypeDefinition;
28 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
29 import org.openecomp.sdc.be.model.tosca.validators.DataTypeValidatorConverter;
30 import org.openecomp.sdc.be.model.tosca.validators.ListValidator;
31 import org.openecomp.sdc.common.log.wrappers.Logger;
32 import org.openecomp.sdc.common.util.GsonFactory;
33 import org.openecomp.sdc.common.util.JsonUtils;
35 import java.math.BigInteger;
36 import java.util.ArrayList;
37 import java.util.List;
40 public class ListConverter implements PropertyValueConverter {
42 private static ListConverter listConverter = new ListConverter();
43 private static Gson gson = GsonFactory.getGson();
44 private static final Logger log = Logger.getLogger(ListValidator.class.getName());
46 DataTypeValidatorConverter dataTypeValidatorConverter = DataTypeValidatorConverter.getInstance();
48 private static JsonParser jsonParser = new JsonParser();
50 public static ListConverter getInstance() {
55 public String convert(String value, String innerType, Map<String, DataTypeDefinition> dataTypes) {
56 Either<String, Boolean> convertWithErrorResult = this.convertWithErrorResult(value, innerType, dataTypes);
57 if (convertWithErrorResult.isRight()) {
61 return convertWithErrorResult.left().value();
64 public Either<String, Boolean> convertWithErrorResult(String value, String innerType,
65 Map<String, DataTypeDefinition> dataTypes) {
66 if (value == null || innerType == null) {
67 return Either.left(value);
70 PropertyValueConverter innerConverter;
71 ToscaPropertyType innerToscaType = ToscaPropertyType.isValidType(innerType);
73 if (innerToscaType != null) {
74 PropertyValueConverter innerConverter1;
75 switch (innerToscaType) {
77 innerConverter1 = ToscaPropertyType.STRING.getConverter();
80 innerConverter1 = ToscaPropertyType.INTEGER.getConverter();
83 innerConverter1 = ToscaPropertyType.FLOAT.getConverter();
86 innerConverter1 = ToscaPropertyType.BOOLEAN.getConverter();
89 innerConverter1 = ToscaPropertyType.JSON.getConverter();
92 log.debug("inner Tosca Type is unknown");
93 return Either.left(value);
95 innerConverter = innerConverter1;
97 log.debug("inner Tosca Type {} ia a complex data type.", innerType);
99 return convertComplexInnerType(value, innerType, dataTypes);
103 ArrayList<String> newList = new ArrayList<>();
105 JsonArray jo = (JsonArray) jsonParser.parse(value);
106 if(ToscaPropertyType.JSON == innerToscaType)
107 return Either.left(value);
108 int size = jo.size();
109 for (int i = 0; i < size; i++) {
110 JsonElement currentValue = jo.get(i);
111 String element = JsonUtils.toString(currentValue);
113 if (element == null || element.isEmpty()) {
116 element = innerConverter.convert(element, null, dataTypes);
117 newList.add(element);
120 switch (innerToscaType) {
122 value = gson.toJson(newList);
125 List<BigInteger> intList = new ArrayList<>();
127 for (String str : newList) {
129 if (str.contains("0x")) {
130 str = str.replaceFirst("0x", "");
133 if (str.contains("0o")) {
134 str = str.replaceFirst("0o", "");
137 intList.add(new BigInteger(str, base));
139 value = gson.toJson(intList);
143 for (String str : newList) {
146 value = value.substring(0, value.length() - 1);
150 List<Boolean> boolList = new ArrayList<>();
151 for (String str : newList) {
152 boolList.add(Boolean.valueOf(str));
154 value = gson.toJson(boolList);
157 value = gson.toJson(newList);
158 log.debug("inner Tosca Type unknown : {}", innerToscaType);
161 } catch (JsonParseException e) {
162 log.debug("Failed to parse json : {}", value, e);
163 BeEcompErrorManager.getInstance().logBeInvalidJsonInput("List Converter");
164 return Either.right(false);
167 return Either.left(value);
170 private Either<String, Boolean> convertComplexInnerType(String value, String innerType,
171 Map<String, DataTypeDefinition> allDataTypes) {
173 DataTypeDefinition dataTypeDefinition = allDataTypes.get(innerType);
174 if (dataTypeDefinition == null) {
175 log.debug("Cannot find data type {}", innerType);
176 return Either.right(false);
179 List<JsonElement> newList = new ArrayList<>();
183 JsonArray jo = (JsonArray) jsonParser.parse(value);
184 int size = jo.size();
185 for (int i = 0; i < size; i++) {
186 JsonElement currentValue = jo.get(i);
188 if (currentValue != null) {
190 String element = JsonUtils.toString(currentValue);
192 ImmutablePair<JsonElement, Boolean> validateAndUpdate = dataTypeValidatorConverter
193 .validateAndUpdate(element, dataTypeDefinition, allDataTypes);
194 if (!validateAndUpdate.right.booleanValue()) {
195 log.debug("Cannot parse value {} from type {} in list position {}",currentValue,innerType,i);
196 return Either.right(false);
198 JsonElement newValue = validateAndUpdate.left;
199 newList.add(newValue);
202 } catch (Exception e) {
203 log.debug("Failed to parse the value {} of list parameter.", value);
204 return Either.right(false);
206 value = gson.toJson(newList);
207 return Either.left(value);