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