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=========================================================
20 package org.openecomp.sdc.be.model.tosca.validators;
22 import com.google.common.base.Strings;
23 import com.google.gson.JsonArray;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonParser;
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.log.wrappers.Logger;
31 import org.openecomp.sdc.common.util.JsonUtils;
33 public class ListValidator implements PropertyTypeValidator {
35 private static final Logger log = Logger.getLogger(ListValidator.class.getName());
36 private static ListValidator listValidator = new ListValidator();
37 private static JsonParser jsonParser = new JsonParser();
38 private static DataTypeValidatorConverter dataTypeValidatorConverter = DataTypeValidatorConverter.getInstance();
40 public static ListValidator getInstance() {
45 public boolean isValid(String value, String innerType, Map<String, DataTypeDefinition> allDataTypes) {
46 log.debug("Going to validate value {} with inner type {}", value, innerType);
47 if (Strings.isNullOrEmpty(value)) {
50 if (innerType == null) {
53 PropertyTypeValidator innerValidator;
54 ToscaPropertyType innerToscaType = ToscaPropertyType.isValidType(innerType);
55 if (innerToscaType != null) {
56 switch (innerToscaType) {
58 innerValidator = ToscaPropertyType.STRING.getValidator();
61 innerValidator = ToscaPropertyType.INTEGER.getValidator();
64 innerValidator = ToscaPropertyType.FLOAT.getValidator();
67 innerValidator = ToscaPropertyType.BOOLEAN.getValidator();
70 innerValidator = ToscaPropertyType.JSON.getValidator();
73 innerValidator = ToscaPropertyType.MAP.getValidator();
76 log.debug("inner Tosca Type is unknown. {}", innerToscaType);
80 log.debug("inner Tosca Type is: {}", innerType);
81 boolean isValid = validateComplexInnerType(value, innerType, allDataTypes);
82 log.debug("Finish to validate value {} of list with inner type {}. result is {}", value, innerType, isValid);
86 JsonArray jo = (JsonArray) jsonParser.parse(value);
87 if (ToscaPropertyType.JSON == innerToscaType) {
91 for (int i = 0; i < size; i++) {
92 JsonElement currentValue = jo.get(i);
93 String element = JsonUtils.toString(currentValue);
94 if (!innerValidator.isValid(element, innerToscaType.equals(ToscaPropertyType.MAP)? ToscaPropertyType.STRING.getType(): null, allDataTypes)) {
95 log.debug("validation of element : {} failed", element);
100 } catch (Exception e) {
101 log.debug("Failed to parse json : {}", value, e);
102 BeEcompErrorManager.getInstance().logBeInvalidJsonInput("List Validator");
108 public boolean isValid(String value, String innerType) {
109 return isValid(value, innerType, null);
112 private boolean validateComplexInnerType(String value, String innerType, Map<String, DataTypeDefinition> allDataTypes) {
113 DataTypeDefinition innerDataTypeDefinition = allDataTypes.get(innerType);
114 if (innerDataTypeDefinition == null) {
115 log.debug("Data type {} cannot be found in our data types.", innerType);
119 JsonArray jo = (JsonArray) jsonParser.parse(value);
120 int size = jo.size();
121 for (int i = 0; i < size; i++) {
122 JsonElement currentValue = jo.get(i);
123 if (currentValue != null) {
124 String element = JsonUtils.toString(currentValue);
125 boolean isValid = dataTypeValidatorConverter.isValid(element, innerDataTypeDefinition, allDataTypes);
127 log.debug("Cannot parse value {} from type {} in list parameter", currentValue, innerType);
132 } catch (Exception e) {
133 log.debug("Error when parsing JSON of object of type ", e);