From 4c4946e339942863e73e20726dd95aaacfcfb5a6 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Mon, 7 Dec 2020 16:25:14 +0000 Subject: JPA concepts for TOSCA Implement the Database persistence parts for the new TOSCA concepts so that those concepts can be written to and read from the database. First batch of comment fixes Issue-ID: POLICY-2900 Change-Id: I45191c1ff57cf1ea8563399db6cc8e51b2724813 Signed-off-by: liamfallon --- .../policy/models/base/PfConceptContainer.java | 36 ++++++------- .../java/org/onap/policy/models/base/PfUtils.java | 34 +++++++++++++ .../policy/models/base/PfValidationResult.java | 59 +++++++++++++--------- 3 files changed, 85 insertions(+), 44 deletions(-) (limited to 'models-base/src/main/java') diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java b/models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java index 77ef939bd..863a3ef02 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java @@ -66,7 +66,7 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult; @EqualsAndHashCode(callSuper = false) public class PfConceptContainer extends PfConcept - implements PfConceptGetter, PfAuthorative>> { + implements PfConceptGetter, PfAuthorative>> { private static final long serialVersionUID = -324211738823208318L; private static final Pattern KEY_ID_PATTERN = Pattern.compile(PfKey.KEY_ID_REGEXP); @@ -193,9 +193,9 @@ public class PfConceptContainer ex } incomingConceptEntry.getValue().setName(findConceptField(conceptKey, conceptKey.getName(), - incomingConceptEntry.getValue(), PfNameVersion::getName)); + incomingConceptEntry.getValue(), PfNameVersion::getName)); incomingConceptEntry.getValue().setVersion(findConceptField(conceptKey, conceptKey.getVersion(), - incomingConceptEntry.getValue(), PfNameVersion::getVersion)); + incomingConceptEntry.getValue(), PfNameVersion::getVersion)); C jpaConcept = getConceptNewInstance(); // This cast allows us to call the fromAuthorative method @@ -212,7 +212,7 @@ public class PfConceptContainer ex if (conceptMap.isEmpty()) { throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, - "An incoming list of concepts must have at least one entry"); + "An incoming list of concepts must have at least one entry"); } } @@ -246,7 +246,7 @@ public class PfConceptContainer ex if (key.equals(PfConceptKey.getNullKey())) { result.addValidationMessage( - new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key")); + new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key")); } result = key.validate(result); @@ -270,14 +270,14 @@ public class PfConceptContainer ex for (final Entry conceptEntry : conceptMap.entrySet()) { if (conceptEntry.getKey().equals(PfConceptKey.getNullKey())) { result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, - "key on concept entry " + conceptEntry.getKey() + " may not be the null key")); + "key on concept entry " + conceptEntry.getKey() + " may not be the null key")); } else if (conceptEntry.getValue() == null) { result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, - "value on concept entry " + conceptEntry.getKey() + " may not be null")); + "value on concept entry " + conceptEntry.getKey() + " may not be null")); } else if (!conceptEntry.getKey().equals(conceptEntry.getValue().getKey())) { result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, - "key on concept entry key " + conceptEntry.getKey() + " does not equal concept value key " - + conceptEntry.getValue().getKey())); + "key on concept entry key " + conceptEntry.getKey() + " does not equal concept value key " + + conceptEntry.getValue().getKey())); result = conceptEntry.getValue().validate(result); } else { result = conceptEntry.getValue().validate(result); @@ -305,11 +305,7 @@ public class PfConceptContainer ex return retVal; } - if (!conceptMap.equals(other.conceptMap)) { - return (conceptMap.hashCode() - other.conceptMap.hashCode()); - } - - return 0; + return PfUtils.compareMaps(conceptMap, other.conceptMap); } /** @@ -349,7 +345,7 @@ public class PfConceptContainer ex @Override public C get(final String conceptKeyName, final String conceptKeyVersion) { return new PfConceptGetterImpl<>((NavigableMap) conceptMap).get(conceptKeyName, - conceptKeyVersion); + conceptKeyVersion); } @Override @@ -360,7 +356,7 @@ public class PfConceptContainer ex @Override public Set getAll(final String conceptKeyName, final String conceptKeyVersion) { return new PfConceptGetterImpl<>((NavigableMap) conceptMap).getAll(conceptKeyName, - conceptKeyVersion); + conceptKeyVersion); } /** @@ -372,16 +368,16 @@ public class PfConceptContainer ex private C getConceptNewInstance() { try { String conceptClassName = - ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0].getTypeName(); + ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0].getTypeName(); return (C) Class.forName(conceptClassName).getDeclaredConstructor().newInstance(); } catch (Exception ex) { throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, - "failed to instantiate instance of container concept class", ex); + "failed to instantiate instance of container concept class", ex); } } private String findConceptField(final PfConceptKey conceptKey, final String keyFieldValue, - final PfNameVersion concept, final Function fieldGetterFunction) { + final PfNameVersion concept, final Function fieldGetterFunction) { String conceptField = fieldGetterFunction.apply(concept); @@ -389,7 +385,7 @@ public class PfConceptContainer ex return keyFieldValue; } else { throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, "Key " + conceptKey.getId() + " field " - + keyFieldValue + " does not match the value " + conceptField + " in the concept field"); + + keyFieldValue + " does not match the value " + conceptField + " in the concept field"); } } } diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfUtils.java b/models-base/src/main/java/org/onap/policy/models/base/PfUtils.java index d5b70d78a..57d23450a 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfUtils.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfUtils.java @@ -22,6 +22,7 @@ package org.onap.policy.models.base; import java.lang.reflect.InvocationTargetException; +import java.util.Collection; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -29,6 +30,8 @@ import java.util.Map.Entry; import java.util.function.Function; import java.util.stream.Collectors; import javax.ws.rs.core.Response; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.collections4.MapUtils; /** * Utility class for Policy Framework concept utilities. @@ -67,6 +70,37 @@ public final class PfUtils { return 0; } + /** + * Compare two collections of the same type, nulls are allowed. + * + * @param leftCollection the first collection + * @param rightCollection the second collection + * @return a measure of the comparison + */ + public static int compareCollections(final Collection leftCollection, final Collection rightCollection) { + if (CollectionUtils.isEmpty(leftCollection) && CollectionUtils.isEmpty(rightCollection)) { + return 0; + } + + return compareObjects(leftCollection, rightCollection); + } + + + /** + * Compare two maps of the same type, nulls are allowed. + * + * @param leftMap the first map + * @param rightMap the second map + * @return a measure of the comparison + */ + public static int compareMaps(final Map leftMap, final Map rightMap) { + if (MapUtils.isEmpty(leftMap) && MapUtils.isEmpty(rightMap)) { + return 0; + } + + return compareObjects(leftMap, rightMap); + } + /** * Convenience method to apply a mapping function to all of the elements of a list, generating a new list. * diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfValidationResult.java b/models-base/src/main/java/org/onap/policy/models/base/PfValidationResult.java index 6f4633612..bbac3b7e5 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfValidationResult.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfValidationResult.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,23 +33,23 @@ public class PfValidationResult { * The ValidationResult enumeration describes the severity of a validation result. */ public enum ValidationResult { - /** No problems or observations were detected during validation. */ - VALID, - /** - * Observations were made on a concept (such as blank descriptions) of a nature that will not - * affect the use of the concept. - */ - OBSERVATION, - /** - * Warnings were made on a concept (such as defined but unused concepts) of a nature that may - * affect the use of the concept. - */ - WARNING, - /** - * Errors were detected on a concept (such as referenced but undefined concepts) of a nature - * that will affect the use of the concept. - */ - INVALID + /** No problems or observations were detected during validation. */ + VALID, + /** + * Observations were made on a concept (such as blank descriptions) of a nature that will not affect the use of + * the concept. + */ + OBSERVATION, + /** + * Warnings were made on a concept (such as defined but unused concepts) of a nature that may affect the use of + * the concept. + */ + WARNING, + /** + * Errors were detected on a concept (such as referenced but undefined concepts) of a nature that will affect + * the use of the concept. + */ + INVALID } // The actual verification result @@ -59,8 +59,8 @@ public class PfValidationResult { private final List messageList = new LinkedList<>(); /** - * Check if a validation reported a valid concept, returns true if the model is usable (that is, - * even if the model has warnings or observations). + * Check if a validation reported a valid concept, returns true if the model is usable (that is, even if the model + * has warnings or observations). * * @return true, if the concept is reported as valid and can be used */ @@ -69,8 +69,7 @@ public class PfValidationResult { } /** - * Check if a validation reported a concept with no errors or warnings, returns true if the - * model is OK to use. + * Check if a validation reported a concept with no errors or warnings, returns true if the model is OK to use. * * @return true, if the concept has no warnings or errors */ @@ -78,9 +77,21 @@ public class PfValidationResult { return validationResult == ValidationResult.VALID || validationResult == ValidationResult.OBSERVATION; } + + /** + * Append a validation result to another validation result. + * + * @param result2Append the result to append to the current validation result + */ + public void append(final PfValidationResult result2Append) { + for (PfValidationMessage message : result2Append.getMessageList()) { + addValidationMessage(message); + } + } + /** - * Adds a validation message to the validation result, used by validate() implementations on - * {@link PfConcept} subclasses to report validaiton observations. + * Adds a validation message to the validation result, used by validate() implementations on {@link PfConcept} + * subclasses to report validaiton observations. * * @param validationMessage the validation message */ -- cgit 1.2.3-korg