2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.models.base;
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;
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 num)) {
56 if (num.longValue() == annot.allowed()) {
57 // this value is always allowed
61 return verMin(result, fieldName, annot.value(), value);
65 * Invokes the value's {@link Validated#validate(String) validate()} method, if the
66 * value is of type {@link Validated}.
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) {
76 if (!result2.isClean()) {
77 result.addResult(result2);
80 return result2.isValid();
83 return super.verCascade(result, fieldName, value);
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
95 public boolean verKey(BeanValidationResult result, String fieldName, VerifyKey annot, Object value) {
96 if (!(value instanceof PfKey pfkey)) {
100 if (annot.keyNotNull() && pfkey.isNullKey()) {
101 result.addResult(fieldName, xlate(pfkey), ValidationStatus.INVALID, Validated.IS_A_NULL_KEY);
106 verCascade(result, fieldName, value);
109 if (!(pfkey instanceof PfKeyImpl keyimpl)) {
113 var result2 = new BeanValidationResult(fieldName, value);
115 if (annot.nameNotNull() && keyimpl.isNullName()) {
116 result2.addResult("name", pfkey.getName(), ValidationStatus.INVALID, Validated.IS_NULL);
119 if (annot.versionNotNull() && keyimpl.isNullVersion()) {
120 result2.addResult("version", pfkey.getVersion(), ValidationStatus.INVALID, Validated.IS_NULL);
123 if (!result2.isClean()) {
124 result.addResult(result2);
127 return result2.isValid();
131 public Object xlate(Object value) {
132 return (value instanceof PfKey ? ((PfKey) value).getId() : value);