Support additional operands for node filters
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / tosca / validators / ListValidator.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.common.base.Strings;
23 import com.google.gson.JsonArray;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonParser;
26 import java.util.Map;
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;
32
33 public class ListValidator implements PropertyTypeValidator {
34
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();
39
40     public static ListValidator getInstance() {
41         return listValidator;
42     }
43
44     @Override
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)) {
48             return true;
49         }
50         if (innerType == null) {
51             return false;
52         }
53         PropertyTypeValidator innerValidator;
54         ToscaPropertyType innerToscaType = ToscaPropertyType.isValidType(innerType);
55         if (innerToscaType != null) {
56             switch (innerToscaType) {
57                 case STRING:
58                     innerValidator = ToscaPropertyType.STRING.getValidator();
59                     break;
60                 case INTEGER:
61                     innerValidator = ToscaPropertyType.INTEGER.getValidator();
62                     break;
63                 case FLOAT:
64                     innerValidator = ToscaPropertyType.FLOAT.getValidator();
65                     break;
66                 case BOOLEAN:
67                     innerValidator = ToscaPropertyType.BOOLEAN.getValidator();
68                     break;
69                 case JSON:
70                     innerValidator = ToscaPropertyType.JSON.getValidator();
71                     break;
72                 case MAP:
73                         innerValidator = ToscaPropertyType.MAP.getValidator();
74                         break;
75                 case RANGE:
76                     innerValidator = ToscaPropertyType.RANGE.getValidator();
77                     break;
78                 case SCALAR_UNIT_BITRATE:
79                     innerValidator = ToscaPropertyType.SCALAR_UNIT_BITRATE.getValidator();
80                     break;
81                 case SCALAR_UNIT_FREQUENCY:
82                     innerValidator = ToscaPropertyType.SCALAR_UNIT_FREQUENCY.getValidator();
83                     break;
84                 case SCALAR_UNIT_TIME:
85                     innerValidator = ToscaPropertyType.SCALAR_UNIT_TIME.getValidator();
86                     break;
87                 case SCALAR_UNIT_SIZE:
88                     innerValidator = ToscaPropertyType.SCALAR_UNIT_SIZE.getValidator();
89                     break;
90                 case SCALAR_UNIT:
91                     innerValidator = ToscaPropertyType.SCALAR_UNIT.getValidator();
92                     break;
93                 case TIMESTAMP:
94                     innerValidator = ToscaPropertyType.TIMESTAMP.getValidator();
95                     break;
96                 default:
97                     log.debug("inner Tosca Type is unknown. {}", innerToscaType);
98                     return false;
99             }
100         } else {
101             log.debug("inner Tosca Type is: {}", innerType);
102             boolean isValid = validateComplexInnerType(value, innerType, allDataTypes);
103             log.debug("Finish to validate value {} of list with inner type {}. result is {}", value, innerType, isValid);
104             return isValid;
105         }
106         try {
107             JsonArray jo = (JsonArray) jsonParser.parse(value);
108             if (ToscaPropertyType.JSON == innerToscaType) {
109                 return true;
110             }
111             int size = jo.size();
112             for (int i = 0; i < size; i++) {
113                 JsonElement currentValue = jo.get(i);
114                 String element = JsonUtils.toString(currentValue);
115                 if (!innerValidator.isValid(element, innerToscaType.equals(ToscaPropertyType.MAP)? ToscaPropertyType.STRING.getType(): null, allDataTypes)) {
116                     log.debug("validation of element : {} failed", element);
117                     return false;
118                 }
119             }
120             return true;
121         } catch (Exception e) {
122             log.debug("Failed to parse json : {}", value, e);
123             BeEcompErrorManager.getInstance().logBeInvalidJsonInput("List Validator");
124         }
125         return false;
126     }
127
128     @Override
129     public boolean isValid(String value, String innerType) {
130         return isValid(value, innerType, null);
131     }
132
133     private boolean validateComplexInnerType(String value, String innerType, Map<String, DataTypeDefinition> allDataTypes) {
134         DataTypeDefinition innerDataTypeDefinition = allDataTypes.get(innerType);
135         if (innerDataTypeDefinition == null) {
136             log.debug("Data type {} cannot be found in our data types.", innerType);
137             return false;
138         }
139         try {
140             JsonArray jo = (JsonArray) jsonParser.parse(value);
141             int size = jo.size();
142             for (int i = 0; i < size; i++) {
143                 JsonElement currentValue = jo.get(i);
144                 if (currentValue != null) {
145                     String element = JsonUtils.toString(currentValue);
146                     boolean isValid = dataTypeValidatorConverter.isValid(element, innerDataTypeDefinition, allDataTypes);
147                     if (!isValid) {
148                         log.debug("Cannot parse value {} from type {} in list parameter", currentValue, innerType);
149                         return false;
150                     }
151                 }
152             }
153         } catch (Exception e) {
154             log.debug("Error when parsing JSON of object of type ", e);
155             return false;
156         }
157         return true;
158     }
159 }