2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.models.base;
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;
32 public class PfValidator extends BeanValidator {
35 protected void addValidators(ValueValidator validator) {
36 super.addValidators(validator);
38 validator.addAnnotation(VerifyKey.class, this::verKey);
39 validator.addAnnotation(PfMin.class, this::verPfMin);
43 * Verifies that the value is >= the minimum value.
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
51 public boolean verPfMin(BeanValidationResult result, String fieldName, PfMin annot, Object value) {
52 if (!(value instanceof Number)) {
56 Number num = (Number) value;
57 if (num.longValue() == annot.allowed()) {
58 // this value is always allowed
62 return verMin(result, fieldName, annot.value(), value);
66 * Invokes the value's {@link Validated#validate(String) validate()} method, if the
67 * value is of type {@link Validated}.
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) {
77 if (!result2.isClean()) {
78 result.addResult(result2);
81 return result2.isValid();
84 return super.verCascade(result, fieldName, value);
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
96 public boolean verKey(BeanValidationResult result, String fieldName, VerifyKey annot, Object value) {
97 if (!(value instanceof PfKey)) {
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));
109 verCascade(result, fieldName, value);
112 if (!(pfkey instanceof PfKeyImpl)) {
116 BeanValidationResult result2 = new BeanValidationResult(fieldName, value);
118 PfKeyImpl keyimpl = (PfKeyImpl) pfkey;
120 if (annot.nameNotNull() && keyimpl.isNullName()) {
121 result2.addResult(new ObjectValidationResult("name", pfkey.getName(), ValidationStatus.INVALID,
125 if (annot.versionNotNull() && keyimpl.isNullVersion()) {
126 result2.addResult(new ObjectValidationResult("version", pfkey.getVersion(), ValidationStatus.INVALID,
130 if (!result2.isClean()) {
131 result.addResult(result2);
134 return result2.isValid();
138 public Object xlate(Object value) {
139 return (value instanceof PfKey ? ((PfKey) value).getId() : value);