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