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