diff options
author | Jim Hahn <jrh3@att.com> | 2019-03-22 14:12:03 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2019-03-25 10:33:34 -0400 |
commit | a8cd3141696d2a2258d90fc234eb6a2618b8731b (patch) | |
tree | be1fa429f936d68b3db9e8b69bed05fd072d52fb /models-base/src/main/java/org/onap | |
parent | 2a245ef80e39a101015efb164de53f1753fa5d47 (diff) |
Add PolicyIdentOptVersion
Added additional PolicyIdentXxx classes. Added PdpDeployPolicies, which
makes use of it, thus eliminating the need for PdpPolicies, which will be
deleted once the PAP has been modified to use the new code.
Added Validated class to facilitate field validation.
Added utility methods to Validated class.
Use new validator methods in PolicyIdentXxx classes.
Use addError() method in validator class.
Use parameter types instead of "?".
Use static Validator instead of local.
Get "result" from each call to a validateXxx() method.
Derived PolicyIdentOptVersion from PfConceptKey.
Moved PolicyIdent classes to models-pdp.
Added PolicyIdent classes to models-pap.
Also removed copy constructors from classes in models-pap, as those
are plain POJOs that will not be copied. Copy constructors will be
added to new classes that will be added to models-pdp in a separate
review.
Forgot to include the new Ident classes in models-pap.
Change-Id: I923132c464c7802ee3e9225685cde44f36c64620
Issue-ID: POLICY-1542
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'models-base/src/main/java/org/onap')
-rw-r--r-- | models-base/src/main/java/org/onap/policy/models/base/Validated.java | 253 | ||||
-rw-r--r-- | models-base/src/main/java/org/onap/policy/models/base/keys/PolicyTypeIdent.java | 42 |
2 files changed, 253 insertions, 42 deletions
diff --git a/models-base/src/main/java/org/onap/policy/models/base/Validated.java b/models-base/src/main/java/org/onap/policy/models/base/Validated.java new file mode 100644 index 000000000..7a0a8377b --- /dev/null +++ b/models-base/src/main/java/org/onap/policy/models/base/Validated.java @@ -0,0 +1,253 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.base; + +import java.util.Collection; +import java.util.Map; +import java.util.Map.Entry; +import lombok.NonNull; +import org.onap.policy.common.utils.validation.Assertions; +import org.onap.policy.models.base.PfValidationResult.ValidationResult; + +/** + * Classes that can be validated. This can be used as a super class or as a stand-alone + * utility class. + */ +public class Validated { + + /** + * Validates the fields of the object. The default method simply returns the result. + * + * @param result where to place the result + * @return the result + */ + public PfValidationResult validate(@NonNull PfValidationResult result) { + return result; + } + + /** + * Validates that a field value is not null. + * + * @param container the object that contains the field + * @param fieldName name of the field to be validated + * @param value value to be validated + * @param result where to place the result + * @return the result + */ + public PfValidationResult validateNotNull(@NonNull Object container, @NonNull String fieldName, Object value, + @NonNull PfValidationResult result) { + + if (value == null) { + addError(container, fieldName, result, "null"); + } + + return result; + } + + /** + * Validates that the name and version of a concept key do not have the null default + * values. + * + * @param value value to be validated + * @param result where to place the result + * @return the result + */ + public PfValidationResult validateNotNull(@NonNull PfConceptKey value, @NonNull PfValidationResult result) { + + if (PfConceptKey.NULL_KEY_NAME.equals(value.getName())) { + addError(value, "name", result, "null"); + } + + if (PfConceptKey.NULL_KEY_VERSION.equals(value.getVersion())) { + addError(value, "version", result, "null"); + } + + return result; + } + + /** + * Validates the contents of a field, verifying that it matches a pattern, if it is + * non-null. + * + * @param container the object that contains the field + * @param fieldName name of the field to be validated + * @param value value to be validated + * @param pattern pattern used to validate the value + * @param result where to place the result + * @return the result + */ + public PfValidationResult validateText(@NonNull Object container, @NonNull String fieldName, String value, + @NonNull String pattern, @NonNull PfValidationResult result) { + + if (value != null) { + addError(container, fieldName, result, + Assertions.getStringParameterValidationMessage(fieldName, value, pattern)); + } + + return result; + } + + /** + * Validates the contents of a property field, verifying that the keys ands values are + * non-null. + * + * @param container the object that contains the field + * @param fieldName name of the field to be validated + * @param properties properties to be validated + * @param resultIn where to place the result + * @return the result + */ + public <T> PfValidationResult validatePropertiesNotNull(@NonNull Object container, @NonNull String fieldName, + Map<String, T> properties, @NonNull PfValidationResult resultIn) { + + PfValidationResult result = resultIn; + + if (properties == null) { + return result; + } + + for (Entry<String, T> ent : properties.entrySet()) { + String key = ent.getKey(); + String keyName = fieldName + "." + key; + result = validateNotNull(container, keyName, key, result); + + result = validateNotNull(container, keyName, ent.getValue(), result); + } + + return result; + } + + /** + * Validates the items in a collection field are non-null. + * + * @param container the object that contains the field + * @param fieldName name of the field to be validated + * @param collection collection whose items are to be validated + * @param resultIn where to place the result + * @return the result + */ + public <T> PfValidationResult validateCollectionNotNull(@NonNull Object container, @NonNull String fieldName, + Collection<T> collection, @NonNull PfValidationResult resultIn) { + + PfValidationResult result = resultIn; + + if (collection == null) { + return result; + } + + String prefix = fieldName + "."; + int count = 0; + + for (T item : collection) { + result = validateNotNull(container, prefix + count, item, result); + ++count; + } + + return result; + } + + /** + * Invokes the "validate()" method on each item in a collection field, if the item is + * non-null. + * + * @param container the object that contains the field + * @param fieldName name of the field to be validated + * @param collection collection whose items are to be validated + * @param result where to place the result + * @return the result + */ + public <T extends Validated> PfValidationResult validateCollection(@NonNull Object container, + @NonNull String fieldName, Collection<T> collection, @NonNull PfValidationResult result) { + + if (collection == null) { + return result; + } + + for (T item : collection) { + if (item != null) { + result = item.validate(result); + } + } + + return result; + } + + /** + * Invokes the "validate()" method on each item in a concept collection field, if the + * item is non-null. + * + * @param container the object that contains the field + * @param fieldName name of the field to be validated + * @param collection collection whose items are to be validated + * @param result where to place the result + * @return the result + */ + public <T extends PfConcept> PfValidationResult validateConceptCollection(@NonNull Object container, + @NonNull String fieldName, Collection<T> collection, @NonNull PfValidationResult result) { + + if (collection == null) { + return result; + } + + for (T item : collection) { + if (item != null) { + result = item.validate(result); + } + } + + return result; + } + + /** + * Adds an error message to the validation result. + * + * @param container the object that contains the field + * @param fieldName name of the field to be validated + * @param result where to place the result + * @param errmsg the error message to be added, or {@code null} if nothing to add + */ + public void addError(@NonNull Object container, @NonNull String fieldName, @NonNull PfValidationResult result, + String errmsg) { + if (errmsg != null) { + result.addValidationMessage(new PfValidationMessage(makeKey(container), container.getClass(), + ValidationResult.INVALID, fieldName + " invalid-" + errmsg)); + } + } + + /** + * Makes a PfKey suitable for insertion into a validation message. Note: the + * "toString()" method of the key simply invokes container.toString(); + * + * @param container the container object for which the key should be made + * @return a key for the container + */ + public PfKey makeKey(@NonNull Object container) { + + return new PfConceptKey() { + private static final long serialVersionUID = 1L; + + @Override + public String toString() { + return container.toString(); + } + }; + } +} diff --git a/models-base/src/main/java/org/onap/policy/models/base/keys/PolicyTypeIdent.java b/models-base/src/main/java/org/onap/policy/models/base/keys/PolicyTypeIdent.java deleted file mode 100644 index 09e03816e..000000000 --- a/models-base/src/main/java/org/onap/policy/models/base/keys/PolicyTypeIdent.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP Policy Models - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.base.keys; - -import lombok.NoArgsConstructor; -import lombok.NonNull; -import org.onap.policy.models.base.PfConceptKey; - -/** - * Identifies a policy type. Both the name and version must be non-null. - */ -@NonNull -@NoArgsConstructor -public class PolicyTypeIdent extends PfConceptKey { - private static final long serialVersionUID = 1L; - - public PolicyTypeIdent(String name, String version) { - super(name, version); - } - - public PolicyTypeIdent(PolicyTypeIdent source) { - super(source); - } -} |