26a8a526078e742d8f88f95433276c40e55de0e0
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / Validated.java
1 /*
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.base;
22
23 import java.util.Collection;
24 import java.util.Map;
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;
29
30 /**
31  * Classes that can be validated. This can be used as a super class or as a stand-alone
32  * utility class.
33  */
34 public class Validated {
35
36     /**
37      * Validates the fields of the object. The default method simply returns the result.
38      *
39      * @param result where to place the result
40      * @return the result
41      */
42     public PfValidationResult validate(@NonNull PfValidationResult result) {
43         return result;
44     }
45
46     /**
47      * Validates that a field value is not null.
48      *
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
53      * @return the result
54      */
55     public PfValidationResult validateNotNull(@NonNull Object container, @NonNull String fieldName, Object value,
56                     @NonNull PfValidationResult result) {
57
58         if (value == null) {
59             addError(container, fieldName, result, "null");
60         }
61
62         return result;
63     }
64
65     /**
66      * Validates that the name and version of a concept key do not have the null default
67      * values.
68      *
69      * @param value value to be validated
70      * @param result where to place the result
71      * @return the result
72      */
73     @SuppressWarnings("java:S3252") // squelch sonar warning for using PkConceptKey instead of PfKeyImpl
74     public PfValidationResult validateNotNull(@NonNull PfConceptKey value, @NonNull PfValidationResult result) {
75
76         if (PfConceptKey.NULL_KEY_NAME.equals(value.getName())) {
77             addError(value, "name", result, "null");
78         }
79
80         if (PfConceptKey.NULL_KEY_VERSION.equals(value.getVersion())) {
81             addError(value, "version", result, "null");
82         }
83
84         return result;
85     }
86
87     /**
88      * Validates the contents of a field, verifying that it matches a pattern, if it is
89      * non-null.
90      *
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
96      * @return the result
97      */
98     public PfValidationResult validateText(@NonNull Object container, @NonNull String fieldName, String value,
99                     @NonNull String pattern, @NonNull PfValidationResult result) {
100
101         if (value != null) {
102             addError(container, fieldName, result,
103                             Assertions.getStringParameterValidationMessage(fieldName, value, pattern));
104         }
105
106         return result;
107     }
108
109     /**
110      * Validates the contents of a property field, verifying that the keys ands values are
111      * non-null.
112      *
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
117      * @return the result
118      */
119     public <T> PfValidationResult validatePropertiesNotNull(@NonNull Object container, @NonNull String fieldName,
120                     Map<String, T> properties, @NonNull PfValidationResult resultIn) {
121
122         PfValidationResult result = resultIn;
123
124         if (properties == null) {
125             return result;
126         }
127
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);
132
133             result = validateNotNull(container, keyName, ent.getValue(), result);
134         }
135
136         return result;
137     }
138
139     /**
140      * Validates the items in a collection field are non-null.
141      *
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
146      * @return the result
147      */
148     public <T> PfValidationResult validateCollectionNotNull(@NonNull Object container, @NonNull String fieldName,
149                     Collection<T> collection, @NonNull PfValidationResult resultIn) {
150
151         PfValidationResult result = resultIn;
152
153         if (collection == null) {
154             return result;
155         }
156
157         String prefix = fieldName + ".";
158         int count = 0;
159
160         for (T item : collection) {
161             result = validateNotNull(container, prefix + count, item, result);
162             ++count;
163         }
164
165         return result;
166     }
167
168     /**
169      * Invokes the "validate()" method on each item in a collection field, if the item is
170      * non-null.
171      *
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
176      * @return the result
177      */
178     public <T extends Validated> PfValidationResult validateCollection(@NonNull Object container,
179                     @NonNull String fieldName, Collection<T> collection, @NonNull PfValidationResult result) {
180
181         if (collection == null) {
182             return result;
183         }
184
185         for (T item : collection) {
186             if (item != null) {
187                 result = item.validate(result);
188             }
189         }
190
191         return result;
192     }
193
194     /**
195      * Invokes the "validate()" method on each item in a concept collection field, if the
196      * item is non-null.
197      *
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
202      * @return the result
203      */
204     public <T extends PfConcept> PfValidationResult validateConceptCollection(@NonNull Object container,
205                     @NonNull String fieldName, Collection<T> collection, @NonNull PfValidationResult result) {
206
207         if (collection == null) {
208             return result;
209         }
210
211         for (T item : collection) {
212             if (item != null) {
213                 result = item.validate(result);
214             }
215         }
216
217         return result;
218     }
219
220     /**
221      * Adds an error message to the validation result.
222      *
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
227      */
228     public void addError(@NonNull Object container, @NonNull String fieldName, @NonNull PfValidationResult result,
229                     String errmsg) {
230         if (errmsg != null) {
231             result.addValidationMessage(new PfValidationMessage(makeKey(container), container.getClass(),
232                             ValidationResult.INVALID, fieldName + " invalid-" + errmsg));
233         }
234     }
235
236     /**
237      * Makes a PfKey suitable for insertion into a validation message. Note: the
238      * "toString()" method of the key simply invokes container.toString();
239      *
240      * @param container the container object for which the key should be made
241      * @return a key for the container
242      */
243     public PfKey makeKey(@NonNull Object container) {
244
245         return new PfConceptKey() {
246             private static final long serialVersionUID = 1L;
247
248             @Override
249             public String toString() {
250                 return container.toString();
251             }
252         };
253     }
254 }