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 final MapValidator mapValidator = new MapValidator();
56 private static final DataTypeValidatorConverter dataTypeValidatorConverter = DataTypeValidatorConverter.getInstance();
58 public static MapValidator getInstance() {
63 public boolean isValid(String value, String innerType, Map<String, DataTypeDefinition> allDataTypes) {
64 if (StringUtils.isEmpty(value)) {
67 if (innerType == null) {
70 PropertyTypeValidator innerValidator;
71 PropertyTypeValidator keyValidator = ToscaPropertyType.KEY.getValidator();
72 ToscaPropertyType innerToscaType = ToscaPropertyType.isValidType(innerType);
73 if (innerToscaType != null) {
74 switch (innerToscaType) {
76 innerValidator = ToscaPropertyType.STRING.getValidator();
79 innerValidator = ToscaPropertyType.INTEGER.getValidator();
82 innerValidator = ToscaPropertyType.FLOAT.getValidator();
85 innerValidator = ToscaPropertyType.BOOLEAN.getValidator();
88 innerValidator = ToscaPropertyType.JSON.getValidator();
91 log.debug("inner Tosca Type is unknown. {}", innerToscaType);
95 log.debug("inner Tosca Type is: {}", innerType);
96 boolean isValid = validateComplexInnerType(value, innerType, allDataTypes);
97 log.debug("Finish to validate value {} of map with inner type {}. result is {}", value, innerType, isValid);
101 JsonElement jsonObject = JsonParser.parseString(value);
102 if (!jsonObject.isJsonObject()) {
105 JsonObject valueAsJson = jsonObject.getAsJsonObject();
106 return validateJsonObject(allDataTypes, innerValidator, keyValidator, valueAsJson);
107 } catch (JsonSyntaxException e) {
108 log.debug("Failed to parse json : {}", value, e);
109 BeEcompErrorManager.getInstance().logBeInvalidJsonInput("Map Validator");
114 private boolean validateJsonObject(Map<String, DataTypeDefinition> allDataTypes, PropertyTypeValidator innerValidator,
115 PropertyTypeValidator keyValidator, JsonObject asJsonObject) {
116 Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();
117 for (Entry<String, JsonElement> entry : entrySet) {
118 String currentKey = entry.getKey();
119 JsonElement jsonValue = entry.getValue();
120 String element = JsonUtils.toString(jsonValue);
121 if (!innerValidator.isValid(element, null, allDataTypes) || !keyValidator.isValid(entry.getKey(), null, allDataTypes)) {
122 log.debug("validation of key : {}, element : {} failed", currentKey, entry.getValue());
129 private boolean validateComplexInnerType(String value, String innerType, Map<String, DataTypeDefinition> allDataTypes) {
130 DataTypeDefinition innerDataTypeDefinition = allDataTypes.get(innerType);
131 if (innerDataTypeDefinition == null) {
132 log.debug("Data type {} cannot be found in our data types.", innerType);
136 JsonElement jsonObject = JsonParser.parseString(value);
137 JsonObject asJsonObject = jsonObject.getAsJsonObject();
138 Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();
139 for (Entry<String, JsonElement> entry : entrySet) {
140 String currentKey = entry.getKey();
141 JsonElement currentValue = entry.getValue();
142 if (currentValue != null) {
143 String element = JsonUtils.toString(currentValue);
144 boolean isValid = dataTypeValidatorConverter.isValid(element, innerDataTypeDefinition, allDataTypes);
146 log.debug("Cannot parse value {} from type {} of key {}", currentValue, innerType, currentKey);
151 } catch (Exception e) {
152 log.debug("Cannot parse value {} of map from inner type {}", value, innerType, e);
159 public boolean isValid(String value, String innerType) {
160 return isValid(value, innerType, null);