2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 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 java.util.Collection;
25 import java.util.Map.Entry;
26 import lombok.NonNull;
27 import org.onap.policy.common.utils.validation.Assertions;
28 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
31 * Classes that can be validated. This can be used as a super class or as a stand-alone
34 public class Validated {
37 * Validates the fields of the object. The default method simply returns the result.
39 * @param result where to place the result
42 public PfValidationResult validate(@NonNull PfValidationResult result) {
47 * Validates that a field value is not null.
49 * @param container the object that contains the field
50 * @param fieldName name of the field to be validated
51 * @param value value to be validated
52 * @param result where to place the result
55 public PfValidationResult validateNotNull(@NonNull Object container, @NonNull String fieldName, Object value,
56 @NonNull PfValidationResult result) {
59 addError(container, fieldName, result, "null");
66 * Validates that the name and version of a concept key do not have the null default
69 * @param value value to be validated
70 * @param result where to place the result
73 public PfValidationResult validateNotNull(@NonNull PfConceptKey value, @NonNull PfValidationResult result) {
75 if (PfKey.NULL_KEY_NAME.equals(value.getName())) {
76 addError(value, "name", result, "null");
79 if (PfKey.NULL_KEY_VERSION.equals(value.getVersion())) {
80 addError(value, "version", result, "null");
87 * Validates the contents of a field, verifying that it matches a pattern, if it is
90 * @param container the object that contains the field
91 * @param fieldName name of the field to be validated
92 * @param value value to be validated
93 * @param pattern pattern used to validate the value
94 * @param result where to place the result
97 public PfValidationResult validateText(@NonNull Object container, @NonNull String fieldName, String value,
98 @NonNull String pattern, @NonNull PfValidationResult result) {
101 addError(container, fieldName, result,
102 Assertions.getStringParameterValidationMessage(fieldName, value, pattern));
109 * Validates the contents of a property field, verifying that the keys ands values are
112 * @param container the object that contains the field
113 * @param fieldName name of the field to be validated
114 * @param properties properties to be validated
115 * @param resultIn where to place the result
118 public <T> PfValidationResult validatePropertiesNotNull(@NonNull Object container, @NonNull String fieldName,
119 Map<String, T> properties, @NonNull PfValidationResult resultIn) {
121 PfValidationResult result = resultIn;
123 if (properties == null) {
127 for (Entry<String, T> ent : properties.entrySet()) {
128 String key = ent.getKey();
129 String keyName = fieldName + "." + key;
130 result = validateNotNull(container, keyName, key, result);
132 result = validateNotNull(container, keyName, ent.getValue(), result);
139 * Validates the items in a collection field are non-null.
141 * @param container the object that contains the field
142 * @param fieldName name of the field to be validated
143 * @param collection collection whose items are to be validated
144 * @param resultIn where to place the result
147 public <T> PfValidationResult validateCollectionNotNull(@NonNull Object container, @NonNull String fieldName,
148 Collection<T> collection, @NonNull PfValidationResult resultIn) {
150 PfValidationResult result = resultIn;
152 if (collection == null) {
156 String prefix = fieldName + ".";
159 for (T item : collection) {
160 result = validateNotNull(container, prefix + count, item, result);
168 * Invokes the "validate()" method on each item in a collection field, if the item is
171 * @param container the object that contains the field
172 * @param fieldName name of the field to be validated
173 * @param collection collection whose items are to be validated
174 * @param result where to place the result
177 public <T extends Validated> PfValidationResult validateCollection(@NonNull Object container,
178 @NonNull String fieldName, Collection<T> collection, @NonNull PfValidationResult result) {
180 if (collection == null) {
184 for (T item : collection) {
186 result = item.validate(result);
194 * Invokes the "validate()" method on each item in a concept collection field, if the
197 * @param container the object that contains the field
198 * @param fieldName name of the field to be validated
199 * @param collection collection whose items are to be validated
200 * @param result where to place the result
203 public <T extends PfConcept> PfValidationResult validateConceptCollection(@NonNull Object container,
204 @NonNull String fieldName, Collection<T> collection, @NonNull PfValidationResult result) {
206 if (collection == null) {
210 for (T item : collection) {
212 result = item.validate(result);
220 * Adds an error message to the validation result.
222 * @param container the object that contains the field
223 * @param fieldName name of the field to be validated
224 * @param result where to place the result
225 * @param errmsg the error message to be added, or {@code null} if nothing to add
227 public void addError(@NonNull Object container, @NonNull String fieldName, @NonNull PfValidationResult result,
229 if (errmsg != null) {
230 result.addValidationMessage(new PfValidationMessage(makeKey(container), container.getClass(),
231 ValidationResult.INVALID, fieldName + " invalid-" + errmsg));
236 * Makes a PfKey suitable for insertion into a validation message. Note: the
237 * "toString()" method of the key simply invokes container.toString();
239 * @param container the container object for which the key should be made
240 * @return a key for the container
242 public PfKey makeKey(@NonNull Object container) {
244 return new PfConceptKey() {
245 private static final long serialVersionUID = 1L;
248 public String toString() {
249 return container.toString();