Standalone TCA with EELF Logger
[dcaegen2/analytics/tca-gen2.git] / dcae-analytics / dcae-analytics-tca-web / src / main / java / org / onap / dcae / analytics / tca / web / validation / ConfigPropertiesAaiValidator.java
1 /*
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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=========================================================
17  *
18  */
19
20 package org.onap.dcae.analytics.tca.web.validation;
21
22
23 import org.onap.dcae.analytics.tca.web.TcaAppProperties;
24 import org.onap.dcae.analytics.web.util.ValidationUtils;
25 import org.onap.dcae.analytics.web.validation.AnalyticsValidator;
26 import org.onap.dcae.analytics.web.validation.GenericValidationResponse;
27 import org.springframework.lang.Nullable;
28 import org.springframework.validation.Errors;
29
30 /**
31  * @author Rajiv Singla
32  */
33 public class ConfigPropertiesAaiValidator implements
34         AnalyticsValidator<TcaAppProperties.Aai, GenericValidationResponse> {
35
36     private static final long serialVersionUID = 1L;
37
38     @Override
39     public GenericValidationResponse apply(final TcaAppProperties.Aai aai) {
40
41         final GenericValidationResponse validationResponse = new GenericValidationResponse();
42
43         final String genericVnfPath = aai.getGenericVnfPath();
44         if (ValidationUtils.isEmpty(genericVnfPath)) {
45             validationResponse.addErrorMessage("generic_vnf_path", "AAI Generic vnf path must be present");
46         }
47
48         final String nodeQueryPath = aai.getNodeQueryPath();
49         if (ValidationUtils.isEmpty(nodeQueryPath)) {
50             validationResponse.addErrorMessage("node_query_path", "AAI Node Query Path must be present");
51         }
52
53
54         final String url = aai.getUrl();
55         if (ValidationUtils.isEmpty(url)) {
56             validationResponse.addErrorMessage("url", "AAI url must be present");
57         }
58
59         return validationResponse;
60     }
61
62     @Override
63     public boolean supports(final Class<?> type) {
64         return type == TcaAppProperties.Aai.class;
65     }
66
67     @Override
68     public void validate(@Nullable final Object target, final Errors errors) {
69
70         // if aai is not present - assuming no aai enrichment is required
71         if (target == null) {
72             return;
73         }
74         final TcaAppProperties.Aai aai = (TcaAppProperties.Aai) target;
75
76         // skip validation if aai enrichment is not enabled
77         if (aai.getEnableEnrichment() == null || !aai.getEnableEnrichment()) {
78             return;
79         }
80
81         final GenericValidationResponse validationResponse = apply(aai);
82
83         if (validationResponse.hasErrors()) {
84             errors.rejectValue("aai", validationResponse.getAllErrorMessage());
85         }
86
87     }
88 }