Support 7.2.1 VES in TCAGEN2
[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  * ==========LICENSE_START======================================================================
3  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
4  * Copyright (c) 2022 Wipro Limited Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  * ============LICENSE_END=========================================================
18  *
19  */
20
21 package org.onap.dcae.analytics.tca.web.validation;
22
23
24 import static org.onap.dcae.analytics.web.util.ValidationUtils.isEmpty;
25
26 import java.util.List;
27
28 import org.onap.dcae.analytics.model.cef.EventSeverity;
29 import org.onap.dcae.analytics.tca.model.policy.ClosedLoopEventStatus;
30 import org.onap.dcae.analytics.tca.model.policy.ControlLoopSchemaType;
31 import org.onap.dcae.analytics.tca.model.policy.Direction;
32 import org.onap.dcae.analytics.tca.model.policy.MetricsPerEventName;
33 import org.onap.dcae.analytics.tca.model.policy.TcaPolicy;
34 import org.onap.dcae.analytics.tca.model.policy.Threshold;
35 import org.onap.dcae.analytics.web.validation.AnalyticsValidator;
36 import org.onap.dcae.analytics.web.validation.GenericValidationResponse;
37 import org.springframework.lang.Nullable;
38 import org.springframework.validation.Errors;
39
40
41 /**
42  * TCA Policy Validator validates {@link TcaPolicy}
43  *
44  * @author Rajiv Singla
45  */
46 public class TcaPolicyValidator implements AnalyticsValidator<List<TcaPolicy>, GenericValidationResponse> {
47
48     private static final long serialVersionUID = 1L;
49
50     @Override
51     public GenericValidationResponse apply(final List<TcaPolicy> tcaPol) {
52
53         final GenericValidationResponse validationResponse = new GenericValidationResponse();
54         
55         for( TcaPolicy tcaPolicy : tcaPol) {
56            
57            // validate TCA Policy must domain present
58            final String domain = tcaPolicy.getDomain();
59            if (isEmpty(domain)) {
60               validationResponse.addErrorMessage("domain", "TCA Policy must have only one domain present");
61            }
62
63            // validate TCA Policy must have at lease one metrics per event name
64            final List<MetricsPerEventName> metricsPerEventNames = tcaPolicy.getMetricsPerEventName();
65            if (metricsPerEventNames == null || metricsPerEventNames.isEmpty()) {
66               validationResponse
67                     .addErrorMessage("metricsPerEventName", "TCA Policy metricsPerEventName is empty");
68               return validationResponse;
69            }
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 metricsPerEventNames:" + 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 metricsPerEventNames:"
86                                 + metricsPerEventName);
87               }
88
89               // must have at least 1 threshold defined
90               final List<Threshold> thresholds = metricsPerEventName.getThresholds();
91               if (thresholds == null || thresholds.isEmpty()) {
92                  validationResponse.addErrorMessage("thresholds",
93                         "TCA Policy event Name must have at least one threshold. " +
94                                 "Event Name causing this validation error:" + metricsPerEventName);
95               } else {
96                   // validate each threshold must have non null - fieldPath, thresholdValue, direction and severity
97                   for (Threshold eventNameThreshold : thresholds) {
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            }
114         }     
115
116         return validationResponse;
117     }
118
119     @Override
120     public boolean supports(final Class<?> type) {
121         return type == TcaPolicy.class;
122     }
123
124     @Override
125     public void validate(@Nullable final Object target, final Errors errors) {
126
127         if (target == null) {
128             errors.rejectValue("tcaPolicy", "TCA Policy must not be null");
129             return;
130         }
131
132         final List<TcaPolicy> tcaPolicy = (List<TcaPolicy>) target;
133         final GenericValidationResponse validationResponse = apply(tcaPolicy);
134         if (validationResponse.hasErrors()) {
135             errors.rejectValue("tca policy", validationResponse.getAllErrorMessage());
136         }
137     }
138 }