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.gson.JsonElement;
23 import com.google.gson.JsonObject;
24 import com.google.gson.JsonParser;
25 import com.google.gson.JsonSyntaxException;
27 import java.util.Map.Entry;
29 import org.apache.commons.lang3.StringUtils;
30 import org.openecomp.sdc.be.config.BeEcompErrorManager;
31 import org.openecomp.sdc.be.model.DataTypeDefinition;
32 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
33 import org.openecomp.sdc.common.log.wrappers.Logger;
34 import org.openecomp.sdc.common.util.JsonUtils;
37 * Property Type Map correct usage:
38 * null key null value = Yaml reader error
39 valid key null value = key & value deleted
40 duplicated keys = last key is taken
41 mismatch between inner type and values type = returned mismatch in data type
42 validators and converters works the same as before
45 when written line by line :
48 key1 and val does not need " " , even if val1 is a string.
49 when written as one line : {"key1":val1 , "key2":val2}
50 Keys always need " " around them.
52 public class MapValidator implements PropertyTypeValidator {
54 private static final Logger log = Logger.getLogger(MapValidator.class.getName());
55 private static MapValidator mapValidator = new MapValidator();
56 private static DataTypeValidatorConverter dataTypeValidatorConverter = DataTypeValidatorConverter.getInstance();
57 private static JsonParser jsonParser = new JsonParser();
59 public static MapValidator getInstance() {
64 public boolean isValid(String value, String innerType, Map<String, DataTypeDefinition> allDataTypes) {
65 if (StringUtils.isEmpty(value)) {
68 if (innerType == null) {
71 PropertyTypeValidator innerValidator;
72 PropertyTypeValidator keyValidator = ToscaPropertyType.KEY.getValidator();
73 ToscaPropertyType innerToscaType = ToscaPropertyType.isValidType(innerType);
74 if (innerToscaType != null) {
75 switch (innerToscaType) {
77 innerValidator = ToscaPropertyType.STRING.getValidator();
80 innerValidator = ToscaPropertyType.INTEGER.getValidator();
83 innerValidator = ToscaPropertyType.FLOAT.getValidator();
86 innerValidator = ToscaPropertyType.BOOLEAN.getValidator();
89 innerValidator = ToscaPropertyType.JSON.getValidator();
92 log.debug("inner Tosca Type is unknown. {}", innerToscaType);
96 log.debug("inner Tosca Type is: {}", innerType);
97 boolean isValid = validateComplexInnerType(value, innerType, allDataTypes);
98 log.debug("Finish to validate value {} of map with inner type {}. result is {}", value, innerType, isValid);
102 JsonElement jsonObject = jsonParser.parse(value);
103 if (!jsonObject.isJsonObject()) {
106 JsonObject valueAsJson = jsonObject.getAsJsonObject();
107 return validateJsonObject(allDataTypes, innerValidator, keyValidator, valueAsJson);
108 } catch (JsonSyntaxException e) {
109 log.debug("Failed to parse json : {}", value, e);
110 BeEcompErrorManager.getInstance().logBeInvalidJsonInput("Map Validator");
115 private boolean validateJsonObject(Map<String, DataTypeDefinition> allDataTypes, PropertyTypeValidator innerValidator,
116 PropertyTypeValidator keyValidator, JsonObject asJsonObject) {
117 Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();
118 for (Entry<String, JsonElement> entry : entrySet) {
119 String currentKey = entry.getKey();
120 JsonElement jsonValue = entry.getValue();
121 String element = JsonUtils.toString(jsonValue);
122 if (!innerValidator.isValid(element, null, allDataTypes) || !keyValidator.isValid(entry.getKey(), null, allDataTypes)) {
123 log.debug("validation of key : {}, element : {} failed", currentKey, entry.getValue());
130 private boolean validateComplexInnerType(String value, String innerType, Map<String, DataTypeDefinition> allDataTypes) {
131 DataTypeDefinition innerDataTypeDefinition = allDataTypes.get(innerType);
132 if (innerDataTypeDefinition == null) {
133 log.debug("Data type {} cannot be found in our data types.", innerType);
137 JsonElement jsonObject = jsonParser.parse(value);
138 JsonObject asJsonObject = jsonObject.getAsJsonObject();
139 Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();
140 for (Entry<String, JsonElement> entry : entrySet) {
141 String currentKey = entry.getKey();
142 JsonElement currentValue = entry.getValue();
143 if (currentValue != null) {
144 String element = JsonUtils.toString(currentValue);
145 boolean isValid = dataTypeValidatorConverter.isValid(element, innerDataTypeDefinition, allDataTypes);
147 log.debug("Cannot parse value {} from type {} of key {}", currentValue, innerType, currentKey);
152 } catch (Exception e) {
153 log.debug("Cannot parse value {} of map from inner type {}", value, innerType, e);
160 public boolean isValid(String value, String innerType) {
161 return isValid(value, innerType, null);