From 69bc7db0edc751d3936b92c4bdf1ee74dfa4da57 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Fri, 5 Apr 2019 15:40:15 +0000 Subject: Complete filters for Database Fetches This review completes the implementaiton of the filters for fetching policy types, policies, and PDP groups from the database. It also fixes bugs in Policy type creation. Yaml in some of the policy type examples modified so that it is syntatically correct. Proeprties now stored as a blob in DB as they can be big. Issue-ID: POLICY-1095 Change-Id: I6aef88ee2905afa58d778d82832f2b55d794fe9c Signed-off-by: liamfallon --- .../org/onap/policy/models/base/PfNameVersion.java | 42 +++++++++++ .../org/onap/policy/models/base/PfObjectFiler.java | 40 ----------- .../onap/policy/models/base/PfObjectFilter.java | 84 ++++++++++++++++++++++ 3 files changed, 126 insertions(+), 40 deletions(-) delete mode 100644 models-base/src/main/java/org/onap/policy/models/base/PfObjectFiler.java create mode 100644 models-base/src/main/java/org/onap/policy/models/base/PfObjectFilter.java (limited to 'models-base/src/main/java/org') diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfNameVersion.java b/models-base/src/main/java/org/onap/policy/models/base/PfNameVersion.java index 47238fc40..7bdced77b 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfNameVersion.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfNameVersion.java @@ -20,6 +20,9 @@ package org.onap.policy.models.base; +import org.apache.commons.lang3.ObjectUtils; +import org.onap.policy.common.utils.validation.Version; + /** * An interface that forces a POJO to have getName() and getVersion() methods. * @@ -33,4 +36,43 @@ public interface PfNameVersion { public String getVersion(); public void setVersion(final String version); + + /** + * COmpare two name version implementation objects. + * + * @param left the left name/version implementation + * @param right the right name/version implementation + * @return the comparison resilt + */ + public default int compareNameVersion(final PfNameVersion left, final PfNameVersion right) { + if (left == null && right == null) { + return 0; + } + + if (left == null) { + return 1; + } + + if (right == null) { + return -1; + } + + int result = ObjectUtils.compare(left.getName(), right.getName()); + if (result != 0) { + return result; + } + + if (left.getVersion() == null && right.getVersion() == null) { + return 0; + } + + if (left.getVersion() == null) { + return 1; + } + + if (right.getVersion() == null) { + return -1; + } + return new Version(left.getVersion()).compareTo(new Version(right.getVersion())); + } } diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfObjectFiler.java b/models-base/src/main/java/org/onap/policy/models/base/PfObjectFiler.java deleted file mode 100644 index f1481bf3c..000000000 --- a/models-base/src/main/java/org/onap/policy/models/base/PfObjectFiler.java +++ /dev/null @@ -1,40 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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.List; - -/** - * Interface for filtering a list of concepts. - * - * @author Liam Fallon (liam.fallon@est.tech) - */ -@FunctionalInterface -public interface PfObjectFiler { - /** - * Filter an incoming list, removing items that do not match the filter. - * - * @param originalList the original list - * @return the filtered list - */ - public List filter(final List originalList); - -} diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfObjectFilter.java b/models-base/src/main/java/org/onap/policy/models/base/PfObjectFilter.java new file mode 100644 index 000000000..a377c24be --- /dev/null +++ b/models-base/src/main/java/org/onap/policy/models/base/PfObjectFilter.java @@ -0,0 +1,84 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.ArrayList; +import java.util.Collections; +import java.util.List; + +import lombok.NonNull; + +/** + * Interface for filtering a list of concepts. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +@FunctionalInterface +public interface PfObjectFilter> { + /** + * Filter an incoming list, removing items that do not match the filter. + * + * @param originalList the original list + * @return the filtered list + */ + public List filter(final List originalList); + + /** + * Check if a value matches a regular expression. + * + * @param value the incoming value to check + * @param regexp the regular expression to check against + * @return match or not + */ + public default boolean filterOnRegexp(@NonNull final String value, @NonNull final String regexp) { + return value.matches(regexp); + } + + /** + * Sort an incoming list and remove all but the latest version of each concept. + * + * @param originalList the incoming list + * @return the filtered list + */ + public default List latestVersionFilter(final List originalList) { + List filteredList = new ArrayList<>(); + Collections.sort(filteredList); + + List filteredOutList = new ArrayList<>(); + + for (int i = 1; i < filteredList.size(); i++) { + // Get the current and last element + T thisElement = filteredList.get(i); + T lastElement = filteredList.get(i - 1); + + // The list is sorted so if the last element name is the same as the current element name, the last element + // should be removed + if (((PfNameVersion)thisElement).getName().equals(((PfNameVersion)lastElement).getName())) { + filteredOutList.add(lastElement); + } + } + + // We can now remove these elements + filteredList.removeAll(filteredOutList); + + return filteredList; + } +} -- cgit 1.2.3-korg