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