ff2f18bed469b603fb04e9cccfb821977ecfe576
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-plugins / src / main / java / org / openecomp / dcae / apod / analytics / cdap / plugins / validator / JsonPathFilterPluginConfigValidator.java
1 /*
2  * ===============================LICENSE_START======================================
3  *  dcae-analytics
4  * ================================================================================
5  *    Copyright © 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.dcae.apod.analytics.cdap.plugins.validator;
22
23 import co.cask.cdap.api.data.schema.Schema;
24 import org.openecomp.dcae.apod.analytics.cdap.common.utils.ValidationUtils;
25 import org.openecomp.dcae.apod.analytics.cdap.common.validation.CDAPAppSettingsValidator;
26 import org.openecomp.dcae.apod.analytics.cdap.plugins.domain.config.filter.JsonPathFilterPluginConfig;
27 import org.openecomp.dcae.apod.analytics.cdap.plugins.utils.CDAPPluginUtils;
28 import org.openecomp.dcae.apod.analytics.common.validation.GenericValidationResponse;
29
30 /**
31  * Validator to validate {@link JsonPathFilterPluginConfig}
32  * <p>
33  * @author Rajiv Singla . Creation Date: 3/2/2017.
34  */
35 public class JsonPathFilterPluginConfigValidator implements CDAPAppSettingsValidator<JsonPathFilterPluginConfig,
36         GenericValidationResponse<JsonPathFilterPluginConfig>> {
37
38     private static final long serialVersionUID = 1L;
39
40     @Override
41     public GenericValidationResponse<JsonPathFilterPluginConfig> validateAppSettings(
42             final JsonPathFilterPluginConfig jsonPathFilterPluginConfig) {
43
44         final GenericValidationResponse<JsonPathFilterPluginConfig> validationResponse =
45                 new GenericValidationResponse<>();
46
47         final String jsonFilterMappings = jsonPathFilterPluginConfig.getJsonFilterMappings();
48         if (ValidationUtils.isEmpty(jsonFilterMappings)) {
49
50             validationResponse.addErrorMessage("JsonFilterMappings", "Json Filter Mappings must be present");
51         }
52
53
54         final String matchedField = jsonPathFilterPluginConfig.getOutputSchemaFieldName();
55         final String outputSchemaJson = jsonPathFilterPluginConfig.getSchema();
56
57         if (ValidationUtils.isEmpty(outputSchemaJson)) {
58
59             validationResponse.addErrorMessage("output schema", "Output schema is not present");
60
61         } else {
62
63             // validate matched output field type is boolean
64             if (matchedField != null &&
65                     !CDAPPluginUtils.validateSchemaFieldType(outputSchemaJson, matchedField, Schema.Type.BOOLEAN)) {
66                 validationResponse.addErrorMessage("OutputSchemaFieldName",
67                         String.format(
68                                 "OutputSchemaFieldName: %s must be marked as boolean type", matchedField));
69             }
70
71             // validate matched output field type is nullable
72             if (matchedField != null &&
73                     !CDAPPluginUtils.validateSchemaFieldType(outputSchemaJson, matchedField, Schema.Type.NULL)) {
74                 validationResponse.addErrorMessage("OutputSchemaFieldName",
75                         String.format(
76                                 "OutputSchemaFieldName: %s must be marked as nullable type", matchedField));
77             }
78
79         }
80
81         return validationResponse;
82     }
83 }