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 --- .../onap/policy/models/pdp/concepts/PdpGroup.java | 7 +- .../policy/models/pdp/concepts/PdpGroupFilter.java | 96 ++++++++++++++++++++-- .../policy/models/pdp/concepts/TestPdpGroup.java | 4 +- 3 files changed, 96 insertions(+), 11 deletions(-) (limited to 'models-pdp') diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroup.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroup.java index a59d1af5a..9665fd472 100644 --- a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroup.java +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroup.java @@ -40,7 +40,7 @@ import org.onap.policy.models.pdp.enums.PdpState; */ @Data @NoArgsConstructor -public class PdpGroup implements PfNameVersion { +public class PdpGroup implements PfNameVersion, Comparable { private String name; private String version; private String description; @@ -67,4 +67,9 @@ public class PdpGroup implements PfNameVersion { this.properties = (source.properties == null ? null : new LinkedHashMap<>(source.properties)); this.pdpSubgroups = PfUtils.mapList(source.pdpSubgroups, PdpSubGroup::new); } + + @Override + public int compareTo(final PdpGroup other) { + return compareNameVersion(this, other); + } } 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 b49bedefe..c5c0bc541 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 @@ -27,20 +27,20 @@ import lombok.Builder; import lombok.Data; import lombok.NonNull; -import org.onap.policy.models.base.PfObjectFiler; +import org.apache.commons.lang3.ObjectUtils; +import org.onap.policy.models.base.PfObjectFilter; import org.onap.policy.models.pdp.enums.PdpState; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; /** - * Filter class for searches for {@link PdpGroup} instances. - * If any fields are null, they are ignored. + * Filter class for searches for {@link PdpGroup} instances. If any fields are null, they are ignored. * * @author Liam Fallon (liam.fallon@est.tech) */ @Builder @Data -public class PdpGroupFilter implements PfObjectFiler { +public class PdpGroupFilter implements PfObjectFilter { public static final String LATEST_VERSION = "LATEST"; // Regular expression @@ -60,15 +60,95 @@ public class PdpGroupFilter implements PfObjectFiler { // Set regular expressions on fields to match policy names and versions private ToscaPolicyIdentifier policy; + private PdpState pdpState; + @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)) + List returnList = originalList.stream() + .filter(p -> filterOnRegexp(p.getName(), name)) + .filter(p -> filterOnRegexp(p.getVersion(), version)) + .filter(p -> ObjectUtils.compare(p.getPdpGroupState(), groupState) == 0) + .filter(p -> filterOnPdpType(p, pdpType)) + .filter(p -> filterOnPolicyType(p, policyType)) + .filter(p -> filterOnPolicy(p, policy)) .collect(Collectors.toList()); // @formatter:off + + if (LATEST_VERSION.equals(version)) { + returnList = this.latestVersionFilter(returnList); + } + + return returnList; + } + + /** + * Filter PDP groups on PDP type. + * + * @param pdpGroup the PDP group to check + * @param pdpType the PDP type to check for + * @return true if the filter should let this PDP group through + */ + private boolean filterOnPdpType(final PdpGroup pdpGroup, final String pdpType) { + if (pdpType == null) { + return true; + } + + for (PdpSubGroup pdpSubGroup: pdpGroup.getPdpSubgroups()) { + if (pdpSubGroup.getPdpType().equals(pdpType)) { + return true; + } + } + + return false; + } + + /** + * Filter PDP groups on policy type. + * + * @param pdpGroup the PDP group to check + * @param policyTypeFilter the policy type regular expressions to check for + * @return true if the filter should let this PDP group through + */ + private boolean filterOnPolicyType(final PdpGroup pdpGroup, final ToscaPolicyTypeIdentifier policyTypeFiler) { + if (policyTypeFiler == null) { + return true; + } + + for (PdpSubGroup pdpSubGroup: pdpGroup.getPdpSubgroups()) { + for (ToscaPolicyTypeIdentifier foundPolicyType : pdpSubGroup.getSupportedPolicyTypes()) { + if (foundPolicyType.getName().matches(policyTypeFiler.getName()) + && foundPolicyType.getVersion().matches(policyTypeFiler.getVersion())) { + return true; + } + } + } + + return false; + } + + /** + * Filter PDP groups on policy. + * + * @param pdpGroup the PDP group to check + * @param policyFilter the policy regular expressions to check for + * @return true if the filter should let this PDP group through + */ + private boolean filterOnPolicy(final PdpGroup pdpGroup, final ToscaPolicyIdentifier policyFiler) { + if (policyFiler == null) { + return true; + } + + for (PdpSubGroup pdpSubGroup: pdpGroup.getPdpSubgroups()) { + for (ToscaPolicyIdentifier foundPolicy : pdpSubGroup.getPolicies()) { + if (foundPolicy.getName().matches(policyFiler.getName()) + && foundPolicy.getVersion().matches(policyFiler.getVersion())) { + return true; + } + } + } + + return false; } } diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/TestPdpGroup.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/TestPdpGroup.java index cac506acc..4de1f2ee3 100644 --- a/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/TestPdpGroup.java +++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/TestPdpGroup.java @@ -49,7 +49,7 @@ public class TestPdpGroup { // verify with all values orig.setDescription("my-descript"); orig.setName("my-name"); - orig.setVersion("my-version"); + orig.setVersion("1.2.3"); orig.setDescription("my-description"); orig.setPdpGroupState(PdpState.SAFE); @@ -64,7 +64,7 @@ public class TestPdpGroup { props.put("key-B", "value-B"); orig.setProperties(props); - assertEquals("PdpGroup(name=my-name, version=my-version, description=my-description, " + assertEquals("PdpGroup(name=my-name, version=1.2.3, description=my-description, " + "pdpGroupState=SAFE, properties={key-A=value-A, key-B=value-B}, " + "pdpSubgroups=[PdpSubGroup(pdpType=null, supportedPolicyTypes=[], policies=[], " + "currentInstanceCount=10, desiredInstanceCount=0, properties=null, pdpInstances=[]), " -- cgit 1.2.3-korg