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.validators;
23 import com.google.gson.JsonArray;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonParser;
26 import com.google.gson.JsonSyntaxException;
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.common.util.JsonUtils;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
36 public class ListValidator implements PropertyTypeValidator {
38 private static ListValidator listValidator = new ListValidator();
40 private static Logger log = LoggerFactory.getLogger(ListValidator.class.getName());
42 private static JsonParser jsonParser = new JsonParser();
44 private static DataTypeValidatorConverter dataTypeValidatorConverter = DataTypeValidatorConverter.getInstance();
46 public static ListValidator getInstance() {
51 public boolean isValid(String value, String innerType, Map<String, DataTypeDefinition> allDataTypes) {
53 log.debug("Going to validate value {} with inner type {}", value, innerType);
55 if (value == null || value == "") {
58 if (innerType == null) {
62 PropertyTypeValidator innerValidator;
64 ToscaPropertyType innerToscaType = ToscaPropertyType.isValidType(innerType);
66 if (innerToscaType != null) {
67 switch (innerToscaType) {
69 innerValidator = ToscaPropertyType.STRING.getValidator();
72 innerValidator = ToscaPropertyType.INTEGER.getValidator();
75 innerValidator = ToscaPropertyType.FLOAT.getValidator();
78 innerValidator = ToscaPropertyType.BOOLEAN.getValidator();
81 innerValidator = ToscaPropertyType.JSON.getValidator();
84 log.debug("inner Tosca Type is unknown. {}", innerToscaType);
89 log.debug("inner Tosca Type is: {}", innerType);
91 boolean isValid = validateComplexInnerType(value, innerType, allDataTypes);
92 log.debug("Finish to validate value {} of list with inner type {}. result is {}",value,innerType,isValid);
97 JsonArray jo = (JsonArray) jsonParser.parse(value);
98 if(ToscaPropertyType.JSON == innerToscaType)
100 int size = jo.size();
101 for (int i = 0; i < size; i++) {
102 JsonElement currentValue = jo.get(i);
103 String element = JsonUtils.toString(currentValue);
104 if (!innerValidator.isValid(element, null, allDataTypes)) {
105 log.debug("validation of element : {} failed", element);
112 } catch (JsonSyntaxException e) {
113 log.debug("Failed to parse json : {}", value, e);
114 BeEcompErrorManager.getInstance().logBeInvalidJsonInput("List Validator");
122 public boolean isValid(String value, String innerType) {
123 return isValid(value, innerType, null);
126 private boolean validateComplexInnerType(String value, String innerType,
127 Map<String, DataTypeDefinition> allDataTypes) {
129 DataTypeDefinition innerDataTypeDefinition = allDataTypes.get(innerType);
130 if (innerDataTypeDefinition == null) {
131 log.debug("Data type {} cannot be found in our data types.", innerType);
137 JsonArray jo = (JsonArray) jsonParser.parse(value);
138 int size = jo.size();
139 for (int i = 0; i < size; i++) {
140 JsonElement currentValue = jo.get(i);
141 if (currentValue != null) {
142 String element = JsonUtils.toString(currentValue);
143 boolean isValid = dataTypeValidatorConverter.isValid(element, innerDataTypeDefinition,
145 if (isValid == false) {
146 log.debug("Cannot parse value {} from type {} in list parameter",currentValue,innerType);
152 } catch (Exception e) {
153 log.debug("Error when parsing JSON of object of type ", e);