2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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 @SuppressWarnings("java:S3252") // squelch sonar warning for using PkConceptKey instead of PfKeyImpl
74 public PfValidationResult validateNotNull(@NonNull PfConceptKey value, @NonNull PfValidationResult result) {
76 if (PfConceptKey.NULL_KEY_NAME.equals(value.getName())) {
77 addError(value, "name", result, "null");
80 if (PfConceptKey.NULL_KEY_VERSION.equals(value.getVersion())) {
81 addError(value, "version", result, "null");
88 * Validates the contents of a field, verifying that it matches a pattern, if it is
91 * @param container the object that contains the field
92 * @param fieldName name of the field to be validated
93 * @param value value to be validated
94 * @param pattern pattern used to validate the value
95 * @param result where to place the result
98 public PfValidationResult validateText(@NonNull Object container, @NonNull String fieldName, String value,
99 @NonNull String pattern, @NonNull PfValidationResult result) {
102 addError(container, fieldName, result,
103 Assertions.getStringParameterValidationMessage(fieldName, value, pattern));
110 * Validates the contents of a property field, verifying that the keys ands values are
113 * @param container the object that contains the field
114 * @param fieldName name of the field to be validated
115 * @param properties properties to be validated
116 * @param resultIn where to place the result
119 public <T> PfValidationResult validatePropertiesNotNull(@NonNull Object container, @NonNull String fieldName,
120 Map<String, T> properties, @NonNull PfValidationResult resultIn) {
122 PfValidationResult result = resultIn;
124 if (properties == null) {
128 for (Entry<String, T> ent : properties.entrySet()) {
129 String key = ent.getKey();
130 String keyName = fieldName + "." + key;
131 result = validateNotNull(container, keyName, key, result);
133 result = validateNotNull(container, keyName, ent.getValue(), result);
140 * Validates the items in a collection field are non-null.
142 * @param container the object that contains the field
143 * @param fieldName name of the field to be validated
144 * @param collection collection whose items are to be validated
145 * @param resultIn where to place the result
148 public <T> PfValidationResult validateCollectionNotNull(@NonNull Object container, @NonNull String fieldName,
149 Collection<T> collection, @NonNull PfValidationResult resultIn) {
151 PfValidationResult result = resultIn;
153 if (collection == null) {
157 String prefix = fieldName + ".";
160 for (T item : collection) {
161 result = validateNotNull(container, prefix + count, item, result);
169 * Invokes the "validate()" method on each item in a collection field, if the item is
172 * @param container the object that contains the field
173 * @param fieldName name of the field to be validated
174 * @param collection collection whose items are to be validated
175 * @param result where to place the result
178 public <T extends Validated> PfValidationResult validateCollection(@NonNull Object container,
179 @NonNull String fieldName, Collection<T> collection, @NonNull PfValidationResult result) {
181 if (collection == null) {
185 for (T item : collection) {
187 result = item.validate(result);
195 * Invokes the "validate()" method on each item in a concept collection field, if the
198 * @param container the object that contains the field
199 * @param fieldName name of the field to be validated
200 * @param collection collection whose items are to be validated
201 * @param result where to place the result
204 public <T extends PfConcept> PfValidationResult validateConceptCollection(@NonNull Object container,
205 @NonNull String fieldName, Collection<T> collection, @NonNull PfValidationResult result) {
207 if (collection == null) {
211 for (T item : collection) {
213 result = item.validate(result);
221 * Adds an error message to the validation result.
223 * @param container the object that contains the field
224 * @param fieldName name of the field to be validated
225 * @param result where to place the result
226 * @param errmsg the error message to be added, or {@code null} if nothing to add
228 public void addError(@NonNull Object container, @NonNull String fieldName, @NonNull PfValidationResult result,
230 if (errmsg != null) {
231 result.addValidationMessage(new PfValidationMessage(makeKey(container), container.getClass(),
232 ValidationResult.INVALID, fieldName + " invalid-" + errmsg));
237 * Makes a PfKey suitable for insertion into a validation message. Note: the
238 * "toString()" method of the key simply invokes container.toString();
240 * @param container the container object for which the key should be made
241 * @return a key for the container
243 public PfKey makeKey(@NonNull Object container) {
245 return new PfConceptKey() {
246 private static final long serialVersionUID = 1L;
249 public String toString() {
250 return container.toString();