2 * ===============================LICENSE_START======================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===========================================
21 package org.openecomp.dcae.apod.analytics.cdap.tca.validator;
23 import org.openecomp.dcae.apod.analytics.cdap.common.validation.CDAPAppSettingsValidator;
24 import org.openecomp.dcae.apod.analytics.cdap.tca.settings.TCAPolicyPreferences;
25 import org.openecomp.dcae.apod.analytics.common.validation.GenericValidationResponse;
26 import org.openecomp.dcae.apod.analytics.model.domain.cef.EventSeverity;
27 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.ControlLoopEventStatus;
28 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.ControlLoopSchemaType;
29 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.Direction;
30 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.MetricsPerEventName;
31 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.Threshold;
32 import org.openecomp.dcae.apod.analytics.tca.utils.TCAUtils;
34 import java.util.List;
36 import static org.openecomp.dcae.apod.analytics.cdap.common.utils.ValidationUtils.isEmpty;
39 * Validates TCA Policy Preferences
42 * @author Rajiv Singla . Creation Date: 11/29/2016.
44 public class TCAPolicyPreferencesValidator implements CDAPAppSettingsValidator<TCAPolicyPreferences,
45 GenericValidationResponse<TCAPolicyPreferences>> {
47 private static final long serialVersionUID = 1L;
50 public GenericValidationResponse<TCAPolicyPreferences> validateAppSettings(
51 final TCAPolicyPreferences tcaPolicyPreferences) {
53 final GenericValidationResponse<TCAPolicyPreferences> validationResponse = new GenericValidationResponse<>();
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");
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");
68 final List<MetricsPerEventName> metricsPerEventNames =
69 tcaPolicyPreferences.getMetricsPerEventName();
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 metricsPerEventName:" + 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 metricsPerEventName:"
86 + metricsPerEventName);
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);
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 ControlLoopEventStatus 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);
113 return validationResponse;