Initial TCA commit into DCAEGEN2
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-tca / src / main / java / org / openecomp / dcae / apod / analytics / cdap / tca / validator / TCAPolicyPreferencesValidator.java
1 /*
2  * ===============================LICENSE_START======================================
3  *  dcae-analytics
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
10  *
11  *          http://www.apache.org/licenses/LICENSE-2.0
12  *
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===========================================
19  */
20
21 package org.openecomp.dcae.apod.analytics.cdap.tca.validator;
22
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.Direction;
28 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.MetricsPerFunctionalRole;
29 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.Threshold;
30 import org.openecomp.dcae.apod.analytics.tca.utils.TCAUtils;
31
32 import java.util.List;
33
34 import static org.openecomp.dcae.apod.analytics.cdap.common.utils.ValidationUtils.isEmpty;
35
36 /**
37  * Validates TCA Policy Preferences
38  * <p>
39  * @author Rajiv Singla . Creation Date: 11/29/2016.
40  */
41 public class TCAPolicyPreferencesValidator implements CDAPAppSettingsValidator<TCAPolicyPreferences,
42         GenericValidationResponse<TCAPolicyPreferences>> {
43
44     private static final long serialVersionUID = 1L;
45
46     @Override
47     public GenericValidationResponse<TCAPolicyPreferences> validateAppSettings(
48             final TCAPolicyPreferences tcaPolicyPreferences) {
49
50         final GenericValidationResponse<TCAPolicyPreferences> validationResponse = new GenericValidationResponse<>();
51
52         // validate TCA Policy must domain present
53         final String domain = tcaPolicyPreferences.getDomain();
54         if (isEmpty(domain)) {
55             validationResponse.addErrorMessage("domain", "TCA Policy must have only one domain present");
56         }
57
58         // validate TCA Policy must have at least one functional role
59         final List<String> policyFunctionalRoles = TCAUtils.getPolicyFunctionalRoles(tcaPolicyPreferences);
60         if (policyFunctionalRoles.isEmpty()) {
61             validationResponse.addErrorMessage("metricsPerFunctionalRoles",
62                     "TCA Policy must have at least one or more functional roles");
63         }
64
65         final List<MetricsPerFunctionalRole> metricsPerFunctionalRoles =
66                 tcaPolicyPreferences.getMetricsPerFunctionalRole();
67
68         // validate each Functional Role must have at least one threshold
69         for (MetricsPerFunctionalRole metricsPerFunctionalRole : metricsPerFunctionalRoles) {
70             if (metricsPerFunctionalRole.getThresholds().isEmpty()) {
71                 validationResponse.addErrorMessage("thresholds",
72                         "TCA Policy Functional Role must have at least one threshold. " +
73                                 "Functional Role causing this validation error:" + metricsPerFunctionalRole);
74             }
75         }
76
77         // validate each threshold must have non null - fieldPath, thresholdValue, direction and severity
78         for (MetricsPerFunctionalRole metricsPerFunctionalRole : metricsPerFunctionalRoles) {
79             final List<Threshold> functionalRoleThresholds = metricsPerFunctionalRole.getThresholds();
80             for (Threshold functionalRoleThreshold : functionalRoleThresholds) {
81                 final String fieldPath = functionalRoleThreshold.getFieldPath();
82                 final Long thresholdValue = functionalRoleThreshold.getThresholdValue();
83                 final Direction direction = functionalRoleThreshold.getDirection();
84                 final EventSeverity severity = functionalRoleThreshold.getSeverity();
85                 if (isEmpty(fieldPath) || thresholdValue == null || direction == null || severity == null) {
86                     validationResponse.addErrorMessage("threshold",
87                             "TCA Policy threshold must have fieldPath, thresholdValue, direction and severity present."
88                                     + "Threshold causing this validation error:" + functionalRoleThreshold);
89                 }
90             }
91         }
92
93
94         return validationResponse;
95     }
96 }