db7b9d63ec34d6f97c287198bc3700cca7eac107
[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.ValidationResult;
26 import org.onap.policy.common.parameters.ValidationStatus;
27 import org.onap.policy.common.parameters.ValueValidator;
28 import org.onap.policy.models.base.validation.annotations.PfMin;
29 import org.onap.policy.models.base.validation.annotations.VerifyKey;
30
31 public class PfValidator extends BeanValidator {
32
33     @Override
34     protected void addValidators(ValueValidator validator) {
35         super.addValidators(validator);
36
37         validator.addAnnotation(VerifyKey.class, this::verKey);
38         validator.addAnnotation(PfMin.class, this::verPfMin);
39     }
40
41     /**
42      * Verifies that the value is >= the minimum value.
43      *
44      * @param result where to add the validation result
45      * @param fieldName field whose value is being verified
46      * @param annot annotation against which the value is being verified
47      * @param value value to be verified
48      * @return {@code true} if the next check should be performed, {@code false} otherwise
49      */
50     public boolean verPfMin(BeanValidationResult result, String fieldName, PfMin annot, Object value) {
51         if (!(value instanceof Number)) {
52             return true;
53         }
54
55         Number num = (Number) value;
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)) {
97             return true;
98         }
99
100         var pfkey = (PfKey) value;
101         if (annot.keyNotNull() && pfkey.isNullKey()) {
102             result.addResult(fieldName, xlate(pfkey), ValidationStatus.INVALID, Validated.IS_A_NULL_KEY);
103             return false;
104         }
105
106         if (annot.valid()) {
107             verCascade(result, fieldName, value);
108         }
109
110         if (!(pfkey instanceof PfKeyImpl)) {
111             return true;
112         }
113
114         var result2 = new BeanValidationResult(fieldName, value);
115
116         PfKeyImpl keyimpl = (PfKeyImpl) pfkey;
117
118         if (annot.nameNotNull() && keyimpl.isNullName()) {
119             result2.addResult("name", pfkey.getName(), ValidationStatus.INVALID, Validated.IS_NULL);
120         }
121
122         if (annot.versionNotNull() && keyimpl.isNullVersion()) {
123             result2.addResult("version", pfkey.getVersion(), ValidationStatus.INVALID, Validated.IS_NULL);
124         }
125
126         if (!result2.isClean()) {
127             result.addResult(result2);
128         }
129
130         return result2.isValid();
131     }
132
133     @Override
134     public Object xlate(Object value) {
135         return (value instanceof PfKey ? ((PfKey) value).getId() : value);
136     }
137 }