Java 17 Upgrade
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfValidator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2023 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.base;
23
24 import org.onap.policy.common.parameters.BeanValidationResult;
25 import org.onap.policy.common.parameters.BeanValidator;
26 import org.onap.policy.common.parameters.ValidationResult;
27 import org.onap.policy.common.parameters.ValidationStatus;
28 import org.onap.policy.common.parameters.ValueValidator;
29 import org.onap.policy.models.base.validation.annotations.PfMin;
30 import org.onap.policy.models.base.validation.annotations.VerifyKey;
31
32 public class PfValidator extends BeanValidator {
33
34     @Override
35     protected void addValidators(ValueValidator validator) {
36         super.addValidators(validator);
37
38         validator.addAnnotation(VerifyKey.class, this::verKey);
39         validator.addAnnotation(PfMin.class, this::verPfMin);
40     }
41
42     /**
43      * Verifies that the value is >= the minimum value.
44      *
45      * @param result where to add the validation result
46      * @param fieldName field whose value is being verified
47      * @param annot annotation against which the value is being verified
48      * @param value value to be verified
49      * @return {@code true} if the next check should be performed, {@code false} otherwise
50      */
51     public boolean verPfMin(BeanValidationResult result, String fieldName, PfMin annot, Object value) {
52         if (!(value instanceof Number num)) {
53             return true;
54         }
55
56         if (num.longValue() == annot.allowed()) {
57             // this value is always allowed
58             return true;
59         }
60
61         return verMin(result, fieldName, annot.value(), value);
62     }
63
64     /**
65      * Invokes the value's {@link Validated#validate(String) validate()} method, if the
66      * value is of type {@link Validated}.
67      */
68     @Override
69     public boolean verCascade(BeanValidationResult result, String fieldName, Object value) {
70         if (value instanceof Validated) {
71             ValidationResult result2 = ((Validated) value).validate(fieldName);
72             if (result2 == null) {
73                 return true;
74             }
75
76             if (!result2.isClean()) {
77                 result.addResult(result2);
78             }
79
80             return result2.isValid();
81         }
82
83         return super.verCascade(result, fieldName, value);
84     }
85
86     /**
87      * Validates a key.
88      *
89      * @param result where to add the validation result
90      * @param fieldName name of the field containing the key
91      * @param annot validation annotations for the key
92      * @param value value to be verified
93      * @return {@code true} if the next check should be performed, {@code false} otherwise
94      */
95     public boolean verKey(BeanValidationResult result, String fieldName, VerifyKey annot, Object value) {
96         if (!(value instanceof PfKey pfkey)) {
97             return true;
98         }
99
100         if (annot.keyNotNull() && pfkey.isNullKey()) {
101             result.addResult(fieldName, xlate(pfkey), ValidationStatus.INVALID, Validated.IS_A_NULL_KEY);
102             return false;
103         }
104
105         if (annot.valid()) {
106             verCascade(result, fieldName, value);
107         }
108
109         if (!(pfkey instanceof PfKeyImpl keyimpl)) {
110             return true;
111         }
112
113         var result2 = new BeanValidationResult(fieldName, value);
114
115         if (annot.nameNotNull() && keyimpl.isNullName()) {
116             result2.addResult("name", pfkey.getName(), ValidationStatus.INVALID, Validated.IS_NULL);
117         }
118
119         if (annot.versionNotNull() && keyimpl.isNullVersion()) {
120             result2.addResult("version", pfkey.getVersion(), ValidationStatus.INVALID, Validated.IS_NULL);
121         }
122
123         if (!result2.isClean()) {
124             result.addResult(result2);
125         }
126
127         return result2.isValid();
128     }
129
130     @Override
131     public Object xlate(Object value) {
132         return (value instanceof PfKey ? ((PfKey) value).getId() : value);
133     }
134 }