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) {
64 result.addResult(fieldName, getKeyId(value), ValidationStatus.INVALID, errorMessage);
68 * Makes a result that indicates a value is invalid, because it is null.
70 * @param fieldName name of the field containing the value
71 * @param value the field's value
72 * @return a result indicating the value is invalid
74 public static ValidationResult makeNullResult(@NonNull String fieldName, Object value) {
75 return new ObjectValidationResult(fieldName, getKeyId(value), ValidationStatus.INVALID, IS_NULL);
79 * Validates a key, ensuring that it isn't null and that it's structurally sound.
81 * @param result where to add the validation result
82 * @param fieldName name of the field containing the key
83 * @param key the field's value
85 public static void validateKeyNotNull(BeanValidationResult result, @NonNull String fieldName, PfKey key) {
87 result.addResult(fieldName, key, ValidationStatus.INVALID, IS_A_NULL_KEY);
91 if (key.isNullKey()) {
92 result.addResult(fieldName, key.getId(), ValidationStatus.INVALID, IS_A_NULL_KEY);
96 result.addResult(key.validate(fieldName));
100 * Validates a key's version, ensuring that it isn't null.
102 * @param result where to add the validation result
103 * @param fieldName name of the field containing the key
104 * @param key the field's value
106 public static void validateKeyVersionNotNull(BeanValidationResult result, @NonNull String fieldName,
108 if (key != null && key.isNullVersion()) {
109 BeanValidationResult result2 = new BeanValidationResult(fieldName, key);
110 result2.addResult(makeNullResult(PfKeyImpl.VERSION_TOKEN, key.getVersion()));
111 result.addResult(result2);
116 * Gets a key's ID, if the value is a {@link PfKey}.
118 * @param value value from which to get the ID
119 * @return the value's ID, if it's a key, the original value otherwise
121 private static Object getKeyId(Object value) {
122 return (value instanceof PfKey ? ((PfKey) value).getId() : value);