Address more sonar issues in policy-models
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / Validated.java
1 /*
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
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     public PfValidationResult validateNotNull(@NonNull PfConceptKey value, @NonNull PfValidationResult result) {
74
75         if (PfKey.NULL_KEY_NAME.equals(value.getName())) {
76             addError(value, "name", result, "null");
77         }
78
79         if (PfKey.NULL_KEY_VERSION.equals(value.getVersion())) {
80             addError(value, "version", result, "null");
81         }
82
83         return result;
84     }
85
86     /**
87      * Validates the contents of a field, verifying that it matches a pattern, if it is
88      * non-null.
89      *
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
95      * @return the result
96      */
97     public PfValidationResult validateText(@NonNull Object container, @NonNull String fieldName, String value,
98                     @NonNull String pattern, @NonNull PfValidationResult result) {
99
100         if (value != null) {
101             addError(container, fieldName, result,
102                             Assertions.getStringParameterValidationMessage(fieldName, value, pattern));
103         }
104
105         return result;
106     }
107
108     /**
109      * Validates the contents of a property field, verifying that the keys ands values are
110      * non-null.
111      *
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
116      * @return the result
117      */
118     public <T> PfValidationResult validatePropertiesNotNull(@NonNull Object container, @NonNull String fieldName,
119                     Map<String, T> properties, @NonNull PfValidationResult resultIn) {
120
121         PfValidationResult result = resultIn;
122
123         if (properties == null) {
124             return result;
125         }
126
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);
131
132             result = validateNotNull(container, keyName, ent.getValue(), result);
133         }
134
135         return result;
136     }
137
138     /**
139      * Validates the items in a collection field are non-null.
140      *
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
145      * @return the result
146      */
147     public <T> PfValidationResult validateCollectionNotNull(@NonNull Object container, @NonNull String fieldName,
148                     Collection<T> collection, @NonNull PfValidationResult resultIn) {
149
150         PfValidationResult result = resultIn;
151
152         if (collection == null) {
153             return result;
154         }
155
156         String prefix = fieldName + ".";
157         int count = 0;
158
159         for (T item : collection) {
160             result = validateNotNull(container, prefix + count, item, result);
161             ++count;
162         }
163
164         return result;
165     }
166
167     /**
168      * Invokes the "validate()" method on each item in a collection field, if the item is
169      * non-null.
170      *
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
175      * @return the result
176      */
177     public <T extends Validated> PfValidationResult validateCollection(@NonNull Object container,
178                     @NonNull String fieldName, Collection<T> collection, @NonNull PfValidationResult result) {
179
180         if (collection == null) {
181             return result;
182         }
183
184         for (T item : collection) {
185             if (item != null) {
186                 result = item.validate(result);
187             }
188         }
189
190         return result;
191     }
192
193     /**
194      * Invokes the "validate()" method on each item in a concept collection field, if the
195      * item is non-null.
196      *
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
201      * @return the result
202      */
203     public <T extends PfConcept> PfValidationResult validateConceptCollection(@NonNull Object container,
204                     @NonNull String fieldName, Collection<T> collection, @NonNull PfValidationResult result) {
205
206         if (collection == null) {
207             return result;
208         }
209
210         for (T item : collection) {
211             if (item != null) {
212                 result = item.validate(result);
213             }
214         }
215
216         return result;
217     }
218
219     /**
220      * Adds an error message to the validation result.
221      *
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
226      */
227     public void addError(@NonNull Object container, @NonNull String fieldName, @NonNull PfValidationResult result,
228                     String errmsg) {
229         if (errmsg != null) {
230             result.addValidationMessage(new PfValidationMessage(makeKey(container), container.getClass(),
231                             ValidationResult.INVALID, fieldName + " invalid-" + errmsg));
232         }
233     }
234
235     /**
236      * Makes a PfKey suitable for insertion into a validation message. Note: the
237      * "toString()" method of the key simply invokes container.toString();
238      *
239      * @param container the container object for which the key should be made
240      * @return a key for the container
241      */
242     public PfKey makeKey(@NonNull Object container) {
243
244         return new PfConceptKey() {
245             private static final long serialVersionUID = 1L;
246
247             @Override
248             public String toString() {
249                 return container.toString();
250             }
251         };
252     }
253 }