Sonar Critical Fix
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-plugins / src / main / java / org / onap / dcae / apod / analytics / cdap / plugins / validator / SimpleTCAPluginConfigValidator.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.onap.dcae.apod.analytics.cdap.plugins.validator;
22
23 import co.cask.cdap.api.data.schema.Schema;
24 import org.onap.dcae.apod.analytics.cdap.common.utils.ValidationUtils;
25 import org.onap.dcae.apod.analytics.cdap.common.validation.CDAPAppSettingsValidator;
26 import org.onap.dcae.apod.analytics.cdap.plugins.domain.config.tca.SimpleTCAPluginConfig;
27 import org.onap.dcae.apod.analytics.cdap.plugins.utils.CDAPPluginUtils;
28 import org.onap.dcae.apod.analytics.common.validation.GenericValidationResponse;
29
30 /**
31  * Validator that validate {@link SimpleTCAPluginConfig}
32  * <p>
33  * @author Rajiv Singla . Creation Date: 2/21/2017.
34  */
35 public class SimpleTCAPluginConfigValidator implements CDAPAppSettingsValidator<SimpleTCAPluginConfig,
36         GenericValidationResponse<SimpleTCAPluginConfig>> {
37
38     private static final long serialVersionUID = 1L;
39
40     @Override
41     public GenericValidationResponse<SimpleTCAPluginConfig> validateAppSettings(
42             final SimpleTCAPluginConfig tcaPluginConfig) {
43
44         final GenericValidationResponse<SimpleTCAPluginConfig> validationResponse = new GenericValidationResponse<>();
45
46         if (ValidationUtils.isEmpty(tcaPluginConfig.getVesMessageFieldName())) {
47             validationResponse.addErrorMessage("vesMessageFieldName",
48                     "Missing VES Message Field Name from plugin incoming schema");
49         }
50
51         if (ValidationUtils.isEmpty(tcaPluginConfig.getPolicyJson())) {
52             validationResponse.addErrorMessage("policyJson",
53                     "Missing tca Policy Json");
54         }
55
56         final String alertFieldValue = tcaPluginConfig.getAlertFieldName();
57         final String alertFieldName = "alertFieldName";
58         if (ValidationUtils.isEmpty(alertFieldValue)) {
59             validationResponse.addErrorMessage(alertFieldName,
60                     "Missing alert Field Name that will be placed in plugin outgoing schema");
61         }
62
63         if (ValidationUtils.isEmpty(tcaPluginConfig.getMessageTypeFieldName())) {
64             validationResponse.addErrorMessage("messageTypeField",
65                     "Missing message Type Field Name that will be placed in plugin outgoing schema");
66         }
67
68
69         final String outputSchemaJson = tcaPluginConfig.getSchema();
70         if (ValidationUtils.isEmpty(outputSchemaJson)) {
71             validationResponse.addErrorMessage("output schema", "Output schema is not present");
72         } else {
73             // validate output schema - alert field name is of type string
74             if (alertFieldValue != null &&
75                     !CDAPPluginUtils.validateSchemaFieldType(outputSchemaJson, alertFieldValue, Schema.Type.STRING)) {
76                 validationResponse.addErrorMessage(alertFieldName,
77                         String.format(
78                                 "Alert Field Name: %s must be String type", alertFieldValue));
79             }
80             // validate output schema - alert field name is nullable
81             if (alertFieldValue != null &&
82                     !CDAPPluginUtils.validateSchemaFieldType(outputSchemaJson, alertFieldValue, Schema.Type.NULL)) {
83                 validationResponse.addErrorMessage(alertFieldName,
84                         String.format(
85                                 "Alert Field Name: %s must be marked as nullable type", alertFieldValue));
86             }
87         }
88
89         return validationResponse;
90     }
91 }