From 2f925fb69dfc65bf25870fe89c004a009a73e476 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Thu, 4 Apr 2019 17:27:50 +0000 Subject: [PATCH] Add filters on policy objects I just raised this review to shwo where I'm going with the filters. They are not complete yet. Issue-ID: POLICY-1095 Change-Id: I7b602a32bb67159b893f3b3cefea5d88038c4e5f Signed-off-by: liamfallon --- .../org/onap/policy/models/base/PfObjectFiler.java | 40 ++++++++++++++++++++ .../policy/models/pdp/concepts/PdpGroupFilter.java | 44 +++++++++++++++++++++- .../authorative/concepts/ToscaPolicyFilter.java | 39 ++++++++++++++++++- .../concepts/ToscaPolicyTypeFilter.java | 31 ++++++++++++++- .../tosca/authorative/concepts/TestPojos.java | 22 +++++++++-- 5 files changed, 169 insertions(+), 7 deletions(-) create mode 100644 models-base/src/main/java/org/onap/policy/models/base/PfObjectFiler.java 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 new file mode 100644 index 000000000..f1481bf3c --- /dev/null +++ b/models-base/src/main/java/org/onap/policy/models/base/PfObjectFiler.java @@ -0,0 +1,40 @@ +/*- + * ============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-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroupFilter.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroupFilter.java index f06a34dbb..f7a47ccd7 100644 --- a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroupFilter.java +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroupFilter.java @@ -20,11 +20,53 @@ package org.onap.policy.models.pdp.concepts; +import java.util.List; +import java.util.stream.Collectors; + +import lombok.Builder; +import lombok.Data; +import lombok.NonNull; + +import org.onap.policy.models.base.PfObjectFiler; +import org.onap.policy.models.pdp.enums.PdpState; + /** * Filter class for searches for {@link PdpGroup} instances. + * If any fields are null, they are ignored. * * @author Liam Fallon (liam.fallon@est.tech) */ -public class PdpGroupFilter { +@Builder +@Data +public class PdpGroupFilter implements PfObjectFiler { + public static final String LATEST_VERSION = "LATEST"; + + // Regular expression + private String name; + + // Regular Expression, set to LATEST_VERRSION to get the latest version + private String version; + + private PdpState groupState; + + // Regular expression + private String pdpType; + + // Set regular expressions on fields to match policy type names and versions + private ToscaPolicyTypeIdentifier policyType; + + // Set regular expressions on fields to match policy names and versions + private ToscaPolicyIdentifier policy; + + @Override + public List filter(@NonNull final List originalList) { + // @formatter:off + return originalList.stream() + .filter(p -> name != null && p.getName() .matches(name)) + .filter(p -> version != null && p.getVersion().matches(version)) + .filter(p -> groupState != null && p.getPdpGroupState().equals(groupState)) + .collect(Collectors.toList()); + // @formatter:off + } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilter.java index 496c62677..7781af236 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilter.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilter.java @@ -20,11 +20,48 @@ package org.onap.policy.models.tosca.authorative.concepts; +import java.util.List; +import java.util.stream.Collectors; + +import lombok.Builder; +import lombok.Data; +import lombok.NonNull; + +import org.onap.policy.models.base.PfObjectFiler; + /** * Filter class for searches for {@link ToscaPolicy} instances. + * If any fields are null, they are ignored. * * @author Liam Fallon (liam.fallon@est.tech) */ -public class ToscaPolicyFilter { +@Builder +@Data +public class ToscaPolicyFilter implements PfObjectFiler { + public static final String LATEST_VERSION = "LATEST"; + + // Regular expression + private String name; + + // Regular Expression, set to LATEST_VERRSION to get the latest version + private String version; + + // Regular expression + private String policyTypeName; + + // Regular Expression, set to LATEST_VERRSION to get the latest version + private String policyTypeVersion; + + @Override + public List filter(@NonNull final List originalList) { + // @formatter:off + return originalList.stream() + .filter(p -> name != null && p.getName() .matches(name)) + .filter(p -> version != null && p.getVersion() .matches(version)) + .filter(p -> policyTypeName != null && p.getType() .matches(policyTypeName)) + .filter(p -> policyTypeVersion != null && p.getTypeVersion().matches(policyTypeVersion)) + .collect(Collectors.toList()); + // @formatter:off + } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilter.java index a77e1856b..baa95045c 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilter.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilter.java @@ -20,11 +20,40 @@ package org.onap.policy.models.tosca.authorative.concepts; +import java.util.List; +import java.util.stream.Collectors; + +import lombok.Builder; +import lombok.Data; +import lombok.NonNull; + +import org.onap.policy.models.base.PfObjectFiler; + /** * Filter class for searches for {@link ToscaPolicyType} instances. + * If any fields are null, they are ignored. * * @author Liam Fallon (liam.fallon@est.tech) */ -public class ToscaPolicyTypeFilter { +@Builder +@Data +public class ToscaPolicyTypeFilter implements PfObjectFiler { + public static final String LATEST_VERSION = "LATEST"; + + // Regular expression + private String name; + + // Regular Expression, set to LATEST_VERRSION to get the latest version + private String version; + + @Override + public List filter(@NonNull final List originalList) { + // @formatter:off + return originalList.stream() + .filter(p -> name != null && p.getName() .matches(name)) + .filter(p -> version != null && p.getVersion().matches(version)) + .collect(Collectors.toList()); + // @formatter:off + } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestPojos.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestPojos.java index 7c813a625..96d82e7b3 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestPojos.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestPojos.java @@ -22,6 +22,7 @@ package org.onap.policy.models.tosca.authorative.concepts; +import com.openpojo.reflection.filters.FilterClassName; import com.openpojo.reflection.filters.FilterPackageInfo; import com.openpojo.validation.Validator; import com.openpojo.validation.ValidatorBuilder; @@ -44,9 +45,22 @@ public class TestPojos { @Test public void testPojos() { - final Validator validator = ValidatorBuilder.create().with(new ToStringTester()) - .with(new SetterMustExistRule()).with(new GetterMustExistRule()).with(new SetterTester()) - .with(new GetterTester()).build(); - validator.validate(POJO_PACKAGE, new FilterPackageInfo()); + // @formatter:off + final Validator validator = ValidatorBuilder + .create() + .with(new ToStringTester()) + .with(new SetterMustExistRule()) + .with(new GetterMustExistRule()) + .with(new SetterTester()) + .with(new GetterTester()) + .build(); + validator.validate(POJO_PACKAGE, + new FilterPackageInfo(), + new FilterClassName( + org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter.class.getName()), + new FilterClassName( + org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter.class.getName()) + ); + // @formatter:on } } -- 2.16.6