7b5c9cf03dae78ee918c9e19f93bca41d5f5cac6
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-tca / src / main / java / org / openecomp / dcae / apod / analytics / cdap / tca / validator / TCAPolicyPreferencesValidator.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.tca.validator;\r
22 \r
23 import org.openecomp.dcae.apod.analytics.cdap.common.validation.CDAPAppSettingsValidator;\r
24 import org.openecomp.dcae.apod.analytics.cdap.tca.settings.TCAPolicyPreferences;\r
25 import org.openecomp.dcae.apod.analytics.common.validation.GenericValidationResponse;\r
26 import org.openecomp.dcae.apod.analytics.model.domain.cef.EventSeverity;\r
27 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.ClosedLoopEventStatus;\r
28 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.ControlLoopSchemaType;\r
29 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.Direction;\r
30 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.MetricsPerEventName;\r
31 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.Threshold;\r
32 import org.openecomp.dcae.apod.analytics.tca.utils.TCAUtils;\r
33 \r
34 import java.util.List;\r
35 \r
36 import static org.openecomp.dcae.apod.analytics.cdap.common.utils.ValidationUtils.isEmpty;\r
37 \r
38 /**\r
39  * Validates TCA Policy Preferences\r
40  * <p>\r
41  *\r
42  * @author Rajiv Singla . Creation Date: 11/29/2016.\r
43  */\r
44 public class TCAPolicyPreferencesValidator implements CDAPAppSettingsValidator<TCAPolicyPreferences,\r
45         GenericValidationResponse<TCAPolicyPreferences>> {\r
46 \r
47     private static final long serialVersionUID = 1L;\r
48 \r
49     @Override\r
50     public GenericValidationResponse<TCAPolicyPreferences> validateAppSettings(\r
51             final TCAPolicyPreferences tcaPolicyPreferences) {\r
52 \r
53         final GenericValidationResponse<TCAPolicyPreferences> validationResponse = new GenericValidationResponse<>();\r
54 \r
55         // validate TCA Policy must domain present\r
56         final String domain = tcaPolicyPreferences.getDomain();\r
57         if (isEmpty(domain)) {\r
58             validationResponse.addErrorMessage("domain", "TCA Policy must have only one domain present");\r
59         }\r
60 \r
61         // validate TCA Policy must have at least one event name\r
62         final List<String> policyEventNames = TCAUtils.getPolicyEventNames(tcaPolicyPreferences);\r
63         if (policyEventNames.isEmpty()) {\r
64             validationResponse.addErrorMessage("metricsPerEventNames",\r
65                     "TCA Policy must have at least one or more event names");\r
66         }\r
67 \r
68         final List<MetricsPerEventName> metricsPerEventNames =\r
69                 tcaPolicyPreferences.getMetricsPerEventName();\r
70 \r
71         // validate Metrics Per Event Name\r
72         for (MetricsPerEventName metricsPerEventName : metricsPerEventNames) {\r
73 \r
74             // event name must be present\r
75             final String eventName = metricsPerEventName.getEventName();\r
76             if (isEmpty(eventName)) {\r
77                 validationResponse.addErrorMessage("eventName",\r
78                         "TCA Policy eventName is not present for metricsPerEventName:" + metricsPerEventName);\r
79             }\r
80 \r
81             // control Loop Schema type must be present\r
82             final ControlLoopSchemaType controlLoopSchemaType = metricsPerEventName.getControlLoopSchemaType();\r
83             if (controlLoopSchemaType == null) {\r
84                 validationResponse.addErrorMessage("controlLoopEventType",\r
85                         "TCA Policy controlLoopSchemaType is not present for metricsPerEventName:"\r
86                                 + metricsPerEventName);\r
87             }\r
88 \r
89             // must have at least 1 threshold defined\r
90             if (metricsPerEventName.getThresholds() == null || metricsPerEventName.getThresholds().isEmpty()) {\r
91                 validationResponse.addErrorMessage("thresholds",\r
92                         "TCA Policy event Name must have at least one threshold. " +\r
93                                 "Event Name causing this validation error:" + metricsPerEventName);\r
94             } else {\r
95                 // validate each threshold must have non null - fieldPath, thresholdValue, direction and severity\r
96                 final List<Threshold> eventNameThresholds = metricsPerEventName.getThresholds();\r
97                 for (Threshold eventNameThreshold : eventNameThresholds) {\r
98                     final String fieldPath = eventNameThreshold.getFieldPath();\r
99                     final Long thresholdValue = eventNameThreshold.getThresholdValue();\r
100                     final Direction direction = eventNameThreshold.getDirection();\r
101                     final EventSeverity severity = eventNameThreshold.getSeverity();\r
102                     final ClosedLoopEventStatus closedLoopEventStatus = eventNameThreshold.getClosedLoopEventStatus();\r
103                     if (isEmpty(fieldPath) || thresholdValue == null || direction == null || severity == null ||\r
104                             closedLoopEventStatus == null) {\r
105                         validationResponse.addErrorMessage("threshold",\r
106                                 "TCA Policy threshold must have fieldPath,thresholdValue,direction, " +\r
107                                         "closedLoopEventStatus and severity defined." +\r
108                                         "Threshold causing this validation error:" + eventNameThreshold);\r
109                     }\r
110                 }\r
111             }\r
112         }\r
113         return validationResponse;\r
114     }\r
115 }\r