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
9 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.dcae.analytics.tca.web.validation;
23 import static org.onap.dcae.analytics.web.util.ValidationUtils.isEmpty;
25 import java.util.List;
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;
41 * TCA Policy Validator validates {@link TcaPolicy}
43 * @author Rajiv Singla
45 public class TcaPolicyValidator implements AnalyticsValidator<TcaPolicy, GenericValidationResponse> {
47 private static final long serialVersionUID = 1L;
50 public GenericValidationResponse apply(final TcaPolicy tcaPolicy) {
52 final GenericValidationResponse validationResponse = new GenericValidationResponse();
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");
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()) {
64 .addErrorMessage("metricsPerEventName", "TCA Policy metricsPerEventName is empty");
65 return validationResponse;
68 // validate Metrics Per Event Name
69 for (MetricsPerEventName metricsPerEventName : metricsPerEventNames) {
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);
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);
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);
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);
111 return validationResponse;
115 public boolean supports(final Class<?> type) {
116 return type == TcaPolicy.class;
120 public void validate(@Nullable final Object target, final Errors errors) {
122 if (target == null) {
123 errors.rejectValue("tcaPolicy", "TCA Policy must not be null");
127 final TcaPolicy tcaPolicy = (TcaPolicy) target;
128 final GenericValidationResponse validationResponse = apply(tcaPolicy);
129 if (validationResponse.hasErrors()) {
130 errors.rejectValue("tca policy", validationResponse.getAllErrorMessage());