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
10 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dcae.analytics.tca.web.validation;
24 import static org.onap.dcae.analytics.web.util.ValidationUtils.isEmpty;
26 import java.util.List;
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;
42 * TCA Policy Validator validates {@link TcaPolicy}
44 * @author Rajiv Singla
46 public class TcaPolicyValidator implements AnalyticsValidator<List<TcaPolicy>, GenericValidationResponse> {
48 private static final long serialVersionUID = 1L;
51 public GenericValidationResponse apply(final List<TcaPolicy> tcaPol) {
53 final GenericValidationResponse validationResponse = new GenericValidationResponse();
55 for( TcaPolicy tcaPolicy : tcaPol) {
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");
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()) {
67 .addErrorMessage("metricsPerEventName", "TCA Policy metricsPerEventName is empty");
68 return validationResponse;
71 // validate Metrics Per Event Name
72 for (MetricsPerEventName metricsPerEventName : metricsPerEventNames) {
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);
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);
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);
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);
116 return validationResponse;
120 public boolean supports(final Class<?> type) {
121 return type == TcaPolicy.class;
125 public void validate(@Nullable final Object target, final Errors errors) {
127 if (target == null) {
128 errors.rejectValue("tcaPolicy", "TCA Policy must not be null");
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());