2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.base;
23 import lombok.NonNull;
24 import org.onap.policy.common.parameters.BeanValidationResult;
25 import org.onap.policy.common.parameters.ObjectValidationResult;
26 import org.onap.policy.common.parameters.ValidationResult;
27 import org.onap.policy.common.parameters.ValidationStatus;
30 * Classes that can be validated. This can be used as a super class or as a stand-alone
33 public class Validated {
34 public static final String IS_BLANK = "is blank";
35 public static final String IS_A_NULL_KEY = "is a null key";
36 public static final String IS_NULL = "is null";
37 public static final String NOT_DEFINED = "not defined";
38 public static final String NOT_FOUND = "not found";
40 public static final String KEY_TOKEN = "key";
41 public static final String VALUE_TOKEN = "value";
44 * Validates the fields of the object. The default method uses a {@link PfValidator}
45 * to validate the object.
47 * @param fieldName name of the field containing this
48 * @return the result, or {@code null}
50 public BeanValidationResult validate(@NonNull String fieldName) {
51 return new PfValidator().validateTop(fieldName, this);
55 * Adds a result indicating that a value is invalid.
57 * @param result where to put the result
58 * @param fieldName name of the field containing the value
59 * @param value the field's value
60 * @param errorMessage the error message
62 public static void addResult(@NonNull BeanValidationResult result, @NonNull String fieldName, Object value,
63 @NonNull String errorMessage) {
65 new ObjectValidationResult(fieldName, getKeyId(value), ValidationStatus.INVALID, errorMessage));
69 * Makes a result that indicates a value is invalid, because it is null.
71 * @param fieldName name of the field containing the value
72 * @param value the field's value
73 * @return a result indicating the value is invalid
75 public static ValidationResult makeNullResult(@NonNull String fieldName, Object value) {
76 return new ObjectValidationResult(fieldName, getKeyId(value), ValidationStatus.INVALID, IS_NULL);
80 * Validates a key, ensuring that it isn't null and that it's structurally sound.
82 * @param result where to add the validation result
83 * @param fieldName name of the field containing the key
84 * @param key the field's value
86 public static void validateKeyNotNull(BeanValidationResult result, @NonNull String fieldName, PfKey key) {
88 result.addResult(new ObjectValidationResult(fieldName, key, ValidationStatus.INVALID, IS_A_NULL_KEY));
92 if (key.isNullKey()) {
93 result.addResult(new ObjectValidationResult(fieldName, key.getId(), ValidationStatus.INVALID,
98 result.addResult(key.validate(fieldName));
102 * Validates a key's version, ensuring that it isn't null.
104 * @param result where to add the validation result
105 * @param fieldName name of the field containing the key
106 * @param key the field's value
108 public static void validateKeyVersionNotNull(BeanValidationResult result, @NonNull String fieldName,
110 if (key != null && key.isNullVersion()) {
111 BeanValidationResult result2 = new BeanValidationResult(fieldName, key);
112 result2.addResult(makeNullResult(PfKeyImpl.VERSION_TOKEN, key.getVersion()));
113 result.addResult(result2);
118 * Gets a key's ID, if the value is a {@link PfKey}.
120 * @param value value from which to get the ID
121 * @return the value's ID, if it's a key, the original value otherwise
123 private static Object getKeyId(Object value) {
124 return (value instanceof PfKey ? ((PfKey) value).getId() : value);