428fedbd9961b10f9a3ae8ba9fb07a744521553e
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-plugins / src / main / java / org / openecomp / dcae / apod / analytics / cdap / plugins / validator / JsonPathFilterPluginConfigValidator.java
1 /*\r
2  * ===============================LICENSE_START======================================\r
3  *  dcae-analytics\r
4  * ================================================================================\r
5  *    Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  *  Licensed under the Apache License, Version 2.0 (the "License");\r
8  *  you may not use this file except in compliance with the License.\r
9  *   You may obtain a copy of the License at\r
10  *\r
11  *          http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  *  Unless required by applicable law or agreed to in writing, software\r
14  *  distributed under the License is distributed on an "AS IS" BASIS,\r
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  *  See the License for the specific language governing permissions and\r
17  *  limitations under the License.\r
18  *  ============================LICENSE_END===========================================\r
19  */\r
20 \r
21 package org.openecomp.dcae.apod.analytics.cdap.plugins.validator;\r
22 \r
23 import co.cask.cdap.api.data.schema.Schema;\r
24 import org.openecomp.dcae.apod.analytics.cdap.common.utils.ValidationUtils;\r
25 import org.openecomp.dcae.apod.analytics.cdap.common.validation.CDAPAppSettingsValidator;\r
26 import org.openecomp.dcae.apod.analytics.cdap.plugins.domain.config.filter.JsonPathFilterPluginConfig;\r
27 import org.openecomp.dcae.apod.analytics.cdap.plugins.utils.CDAPPluginUtils;\r
28 import org.openecomp.dcae.apod.analytics.common.validation.GenericValidationResponse;\r
29 \r
30 /**\r
31  * Validator to validate {@link JsonPathFilterPluginConfig}\r
32  * <p>\r
33  * @author Rajiv Singla . Creation Date: 3/2/2017.\r
34  */\r
35 public class JsonPathFilterPluginConfigValidator implements CDAPAppSettingsValidator<JsonPathFilterPluginConfig,\r
36         GenericValidationResponse<JsonPathFilterPluginConfig>> {\r
37 \r
38     private static final long serialVersionUID = 1L;\r
39 \r
40     @Override\r
41     public GenericValidationResponse<JsonPathFilterPluginConfig> validateAppSettings(\r
42             final JsonPathFilterPluginConfig jsonPathFilterPluginConfig) {\r
43 \r
44         final GenericValidationResponse<JsonPathFilterPluginConfig> validationResponse =\r
45                 new GenericValidationResponse<>();\r
46 \r
47         final String jsonFilterMappings = jsonPathFilterPluginConfig.getJsonFilterMappings();\r
48         if (ValidationUtils.isEmpty(jsonFilterMappings)) {\r
49 \r
50             validationResponse.addErrorMessage("JsonFilterMappings", "Json Filter Mappings must be present");\r
51         }\r
52 \r
53 \r
54         final String matchedField = jsonPathFilterPluginConfig.getOutputSchemaFieldName();\r
55         final String outputSchemaJson = jsonPathFilterPluginConfig.getSchema();\r
56 \r
57         if (ValidationUtils.isEmpty(outputSchemaJson)) {\r
58 \r
59             validationResponse.addErrorMessage("output schema", "Output schema is not present");\r
60 \r
61         } else {\r
62 \r
63             // validate matched output field type is boolean\r
64             if (matchedField != null &&\r
65                     !CDAPPluginUtils.validateSchemaFieldType(outputSchemaJson, matchedField, Schema.Type.BOOLEAN)) {\r
66                 validationResponse.addErrorMessage("OutputSchemaFieldName",\r
67                         String.format(\r
68                                 "OutputSchemaFieldName: %s must be marked as boolean type", matchedField));\r
69             }\r
70 \r
71             // validate matched output field type is nullable\r
72             if (matchedField != null &&\r
73                     !CDAPPluginUtils.validateSchemaFieldType(outputSchemaJson, matchedField, Schema.Type.NULL)) {\r
74                 validationResponse.addErrorMessage("OutputSchemaFieldName",\r
75                         String.format(\r
76                                 "OutputSchemaFieldName: %s must be marked as nullable type", matchedField));\r
77             }\r
78 \r
79         }\r
80 \r
81         return validationResponse;\r
82     }\r
83 }\r