Use annotations on parameterized types
[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  * ================================================================================
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.onap.policy.models.base;
22
23 import org.onap.policy.common.parameters.BeanValidationResult;
24 import org.onap.policy.common.parameters.BeanValidator;
25 import org.onap.policy.common.parameters.ObjectValidationResult;
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)) {
53             return true;
54         }
55
56         Number num = (Number) value;
57         if (num.longValue() == annot.allowed()) {
58             // this value is always allowed
59             return true;
60         }
61
62         return verMin(result, fieldName, annot.value(), value);
63     }
64
65     /**
66      * Invokes the value's {@link Validated#validate(String) validate()} method, if the
67      * value is of type {@link Validated}.
68      */
69     @Override
70     public boolean verCascade(BeanValidationResult result, String fieldName, Object value) {
71         if (value instanceof Validated) {
72             ValidationResult result2 = ((Validated) value).validate(fieldName);
73             if (result2 == null) {
74                 return true;
75             }
76
77             if (!result2.isClean()) {
78                 result.addResult(result2);
79             }
80
81             return result2.isValid();
82         }
83
84         return super.verCascade(result, fieldName, value);
85     }
86
87     /**
88      * Validates a key.
89      *
90      * @param result where to add the validation result
91      * @param fieldName name of the field containing the key
92      * @param annot validation annotations for the key
93      * @param value value to be verified
94      * @return {@code true} if the next check should be performed, {@code false} otherwise
95      */
96     public boolean verKey(BeanValidationResult result, String fieldName, VerifyKey annot, Object value) {
97         if (!(value instanceof PfKey)) {
98             return true;
99         }
100
101         PfKey pfkey = (PfKey) value;
102         if (annot.keyNotNull() && pfkey.isNullKey()) {
103             result.addResult(new ObjectValidationResult(fieldName, xlate(pfkey), ValidationStatus.INVALID,
104                             Validated.IS_A_NULL_KEY));
105             return false;
106         }
107
108         if (annot.valid()) {
109             verCascade(result, fieldName, value);
110         }
111
112         if (!(pfkey instanceof PfKeyImpl)) {
113             return true;
114         }
115
116         BeanValidationResult result2 = new BeanValidationResult(fieldName, value);
117
118         PfKeyImpl keyimpl = (PfKeyImpl) pfkey;
119
120         if (annot.nameNotNull() && keyimpl.isNullName()) {
121             result2.addResult(new ObjectValidationResult("name", pfkey.getName(), ValidationStatus.INVALID,
122                             Validated.IS_NULL));
123         }
124
125         if (annot.versionNotNull() && keyimpl.isNullVersion()) {
126             result2.addResult(new ObjectValidationResult("version", pfkey.getVersion(), ValidationStatus.INVALID,
127                             Validated.IS_NULL));
128         }
129
130         if (!result2.isClean()) {
131             result.addResult(result2);
132         }
133
134         return result2.isValid();
135     }
136
137     @Override
138     public Object xlate(Object value) {
139         return (value instanceof PfKey ? ((PfKey) value).getId() : value);
140     }
141 }