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