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.JsonElement;
24 import com.google.gson.JsonObject;
25 import com.google.gson.JsonParser;
26 import com.google.gson.JsonSyntaxException;
27 import org.apache.commons.lang.StringUtils;
28 import org.openecomp.sdc.be.config.BeEcompErrorManager;
29 import org.openecomp.sdc.be.model.DataTypeDefinition;
30 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
31 import org.openecomp.sdc.common.log.wrappers.Logger;
32 import org.openecomp.sdc.common.util.JsonUtils;
35 import java.util.Map.Entry;
39 * Property Type Map correct usage:
40 * null key null value = Yaml reader error
41 valid key null value = key & value deleted
42 duplicated keys = last key is taken
43 mismatch between inner type and values type = returned mismatch in data type
44 validators and converters works the same as before
47 when written line by line :
50 key1 and val does not need " " , even if val1 is a string.
51 when written as one line : {"key1":val1 , "key2":val2}
52 Keys always need " " around them.
54 public class MapValidator implements PropertyTypeValidator {
56 private static MapValidator mapValidator = new MapValidator();
58 private static final Logger log = Logger.getLogger(MapValidator.class.getName());
60 private static DataTypeValidatorConverter dataTypeValidatorConverter = DataTypeValidatorConverter.getInstance();
62 private static JsonParser jsonParser = new JsonParser();
64 public static MapValidator getInstance() {
69 public boolean isValid(String value, String innerType, Map<String, DataTypeDefinition> allDataTypes) {
71 if (StringUtils.isEmpty(value)) {
74 if (innerType == null) {
78 PropertyTypeValidator innerValidator;
79 PropertyTypeValidator keyValidator = ToscaPropertyType.KEY.getValidator();
80 ToscaPropertyType innerToscaType = ToscaPropertyType.isValidType(innerType);
82 if (innerToscaType != null) {
83 switch (innerToscaType) {
85 innerValidator = ToscaPropertyType.STRING.getValidator();
88 innerValidator = ToscaPropertyType.INTEGER.getValidator();
91 innerValidator = ToscaPropertyType.FLOAT.getValidator();
94 innerValidator = ToscaPropertyType.BOOLEAN.getValidator();
97 innerValidator = ToscaPropertyType.JSON.getValidator();
100 log.debug("inner Tosca Type is unknown. {}", innerToscaType);
105 log.debug("inner Tosca Type is: {}", innerType);
107 boolean isValid = validateComplexInnerType(value, innerType, allDataTypes);
108 log.debug("Finish to validate value {} of map with inner type {}. result is {}",value,innerType,isValid);
114 JsonElement jsonObject = jsonParser.parse(value);
115 if (!jsonObject.isJsonObject()) {
118 JsonObject valueAsJson = jsonObject.getAsJsonObject();
119 return validateJsonObject(allDataTypes, innerValidator, keyValidator, valueAsJson);
120 } catch (JsonSyntaxException e) {
121 log.debug("Failed to parse json : {}", value, e);
122 BeEcompErrorManager.getInstance().logBeInvalidJsonInput("Map Validator");
129 private boolean validateJsonObject(Map<String, DataTypeDefinition> allDataTypes, PropertyTypeValidator innerValidator, PropertyTypeValidator keyValidator, JsonObject asJsonObject) {
130 Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();
131 for (Entry<String, JsonElement> entry : entrySet) {
132 String currentKey = entry.getKey();
133 JsonElement jsonValue = entry.getValue();
135 String element = JsonUtils.toString(jsonValue);
137 if (!innerValidator.isValid(element, null, allDataTypes)
138 || !keyValidator.isValid(entry.getKey(), null, allDataTypes)) {
139 log.debug("validation of key : {}, element : {} failed", currentKey, entry.getValue());
147 private boolean validateComplexInnerType(String value, String innerType,
148 Map<String, DataTypeDefinition> allDataTypes) {
150 DataTypeDefinition innerDataTypeDefinition = allDataTypes.get(innerType);
151 if (innerDataTypeDefinition == null) {
152 log.debug("Data type {} cannot be found in our data types.", innerType);
157 JsonElement jsonObject = jsonParser.parse(value);
158 JsonObject asJsonObject = jsonObject.getAsJsonObject();
159 Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();
160 for (Entry<String, JsonElement> entry : entrySet) {
161 String currentKey = entry.getKey();
162 JsonElement currentValue = entry.getValue();
164 if (currentValue != null) {
165 String element = JsonUtils.toString(currentValue);
166 boolean isValid = dataTypeValidatorConverter.isValid(element, innerDataTypeDefinition,
169 log.debug("Cannot parse value {} from type {} of key {}",currentValue,innerType,currentKey);
175 } catch (Exception e) {
176 log.debug("Cannot parse value {} of map from inner type {}", value, innerType, e);
184 public boolean isValid(String value, String innerType) {
185 return isValid(value, innerType, null);