Standalone TCA with EELF Logger
[dcaegen2/analytics/tca-gen2.git] / dcae-analytics / dcae-analytics-tca-web / src / main / java / org / onap / dcae / analytics / tca / web / validation / TcaPolicyValidator.java
1 /*
2  * ================================================================================
3  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=========================================================
17  *
18  */
19
20 package org.onap.dcae.analytics.tca.web.validation;
21
22
23 import static org.onap.dcae.analytics.web.util.ValidationUtils.isEmpty;
24
25 import java.util.List;
26
27 import org.onap.dcae.analytics.model.cef.EventSeverity;
28 import org.onap.dcae.analytics.tca.model.policy.ClosedLoopEventStatus;
29 import org.onap.dcae.analytics.tca.model.policy.ControlLoopSchemaType;
30 import org.onap.dcae.analytics.tca.model.policy.Direction;
31 import org.onap.dcae.analytics.tca.model.policy.MetricsPerEventName;
32 import org.onap.dcae.analytics.tca.model.policy.TcaPolicy;
33 import org.onap.dcae.analytics.tca.model.policy.Threshold;
34 import org.onap.dcae.analytics.web.validation.AnalyticsValidator;
35 import org.onap.dcae.analytics.web.validation.GenericValidationResponse;
36 import org.springframework.lang.Nullable;
37 import org.springframework.validation.Errors;
38
39
40 /**
41  * TCA Policy Validator validates {@link TcaPolicy}
42  *
43  * @author Rajiv Singla
44  */
45 public class TcaPolicyValidator implements AnalyticsValidator<TcaPolicy, GenericValidationResponse> {
46
47     private static final long serialVersionUID = 1L;
48
49     @Override
50     public GenericValidationResponse apply(final TcaPolicy tcaPolicy) {
51
52         final GenericValidationResponse validationResponse = new GenericValidationResponse();
53
54         // validate TCA Policy must domain present
55         final String domain = tcaPolicy.getDomain();
56         if (isEmpty(domain)) {
57             validationResponse.addErrorMessage("domain", "TCA Policy must have only one domain present");
58         }
59
60         // validate TCA Policy must have at lease one metrics per event name
61         final List<MetricsPerEventName> metricsPerEventNames = tcaPolicy.getMetricsPerEventName();
62         if (metricsPerEventNames == null || metricsPerEventNames.isEmpty()) {
63             validationResponse
64                     .addErrorMessage("metricsPerEventName", "TCA Policy metricsPerEventName is empty");
65             return validationResponse;
66         }
67
68         // validate Metrics Per Event Name
69         for (MetricsPerEventName metricsPerEventName : metricsPerEventNames) {
70
71             // event name must be present
72             final String eventName = metricsPerEventName.getEventName();
73             if (isEmpty(eventName)) {
74                 validationResponse.addErrorMessage("eventName",
75                         "TCA Policy eventName is not present for metricsPerEventNames:" + metricsPerEventName);
76             }
77
78             // control Loop Schema type must be present
79             final ControlLoopSchemaType controlLoopSchemaType = metricsPerEventName.getControlLoopSchemaType();
80             if (controlLoopSchemaType == null) {
81                 validationResponse.addErrorMessage("controlLoopEventType",
82                         "TCA Policy controlLoopSchemaType is not present for metricsPerEventNames:"
83                                 + metricsPerEventName);
84             }
85
86             // must have at least 1 threshold defined
87             final List<Threshold> thresholds = metricsPerEventName.getThresholds();
88             if (thresholds == null || thresholds.isEmpty()) {
89                 validationResponse.addErrorMessage("thresholds",
90                         "TCA Policy event Name must have at least one threshold. " +
91                                 "Event Name causing this validation error:" + metricsPerEventName);
92             } else {
93                 // validate each threshold must have non null - fieldPath, thresholdValue, direction and severity
94                 for (Threshold eventNameThreshold : thresholds) {
95                     final String fieldPath = eventNameThreshold.getFieldPath();
96                     final Long thresholdValue = eventNameThreshold.getThresholdValue();
97                     final Direction direction = eventNameThreshold.getDirection();
98                     final EventSeverity severity = eventNameThreshold.getSeverity();
99                     final ClosedLoopEventStatus closedLoopEventStatus = eventNameThreshold.getClosedLoopEventStatus();
100                     if (isEmpty(fieldPath) || thresholdValue == null || direction == null || severity == null ||
101                             closedLoopEventStatus == null) {
102                         validationResponse.addErrorMessage("threshold",
103                                 "TCA Policy threshold must have fieldPath,thresholdValue,direction, " +
104                                         "closedLoopEventStatus and severity defined." +
105                                         "Threshold causing this validation error:" + eventNameThreshold);
106                     }
107                 }
108             }
109         }
110
111         return validationResponse;
112     }
113
114     @Override
115     public boolean supports(final Class<?> type) {
116         return type == TcaPolicy.class;
117     }
118
119     @Override
120     public void validate(@Nullable final Object target, final Errors errors) {
121
122         if (target == null) {
123             errors.rejectValue("tcaPolicy", "TCA Policy must not be null");
124             return;
125         }
126
127         final TcaPolicy tcaPolicy = (TcaPolicy) target;
128         final GenericValidationResponse validationResponse = apply(tcaPolicy);
129         if (validationResponse.hasErrors()) {
130             errors.rejectValue("tca policy", validationResponse.getAllErrorMessage());
131         }
132     }
133 }