Support TOSCA functions in Node Filters
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / tosca / validators / MapValidator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.openecomp.sdc.be.model.tosca.validators;
21
22 import com.google.gson.JsonElement;
23 import com.google.gson.JsonObject;
24 import com.google.gson.JsonParser;
25 import com.google.gson.JsonSyntaxException;
26 import java.util.Map;
27 import java.util.Map.Entry;
28 import java.util.Set;
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;
35
36 /*
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
43
44 Types:
45 when written line by line :
46                     key1 : val1
47                     key2 : val2
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.
51 */
52 public class MapValidator implements PropertyTypeValidator {
53
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();
57
58     public static MapValidator getInstance() {
59         return mapValidator;
60     }
61
62     @Override
63     public boolean isValid(String value, String innerType, Map<String, DataTypeDefinition> allDataTypes) {
64         if (StringUtils.isEmpty(value)) {
65             return true;
66         }
67         if (innerType == null) {
68             return false;
69         }
70         PropertyTypeValidator innerValidator;
71         PropertyTypeValidator keyValidator = ToscaPropertyType.KEY.getValidator();
72         ToscaPropertyType innerToscaType = ToscaPropertyType.isValidType(innerType);
73         if (innerToscaType != null) {
74             switch (innerToscaType) {
75                 case STRING:
76                     innerValidator = ToscaPropertyType.STRING.getValidator();
77                     break;
78                 case INTEGER:
79                     innerValidator = ToscaPropertyType.INTEGER.getValidator();
80                     break;
81                 case FLOAT:
82                     innerValidator = ToscaPropertyType.FLOAT.getValidator();
83                     break;
84                 case BOOLEAN:
85                     innerValidator = ToscaPropertyType.BOOLEAN.getValidator();
86                     break;
87                 case JSON:
88                     innerValidator = ToscaPropertyType.JSON.getValidator();
89                     break;
90                 default:
91                     log.debug("inner Tosca Type is unknown. {}", innerToscaType);
92                     return false;
93             }
94         } else {
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);
98             return isValid;
99         }
100         try {
101             JsonElement jsonObject = JsonParser.parseString(value);
102             if (!jsonObject.isJsonObject()) {
103                 return false;
104             }
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");
110         }
111         return false;
112     }
113
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());
123                 return false;
124             }
125         }
126         return true;
127     }
128
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);
133             return false;
134         }
135         try {
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);
145                     if (!isValid) {
146                         log.debug("Cannot parse value {} from type {} of key {}", currentValue, innerType, currentKey);
147                         return false;
148                     }
149                 }
150             }
151         } catch (Exception e) {
152             log.debug("Cannot parse value {} of map from inner type {}", value, innerType, e);
153             return false;
154         }
155         return true;
156     }
157
158     @Override
159     public boolean isValid(String value, String innerType) {
160         return isValid(value, innerType, null);
161     }
162 }