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