X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=models-pdp%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fmodels%2Fpdp%2Fconcepts%2FPdpGroupFilter.java;h=a7ceaa9a52a55313a8d0e99bbd8d21de31c9ef4d;hb=f2b0318f53abf9f2345a5cdca74f3dd635aa9b60;hp=b49bedefe2afd059293ac1b998d3f6624b8db5ab;hpb=38cc81922273039d2d32979123bb83a15c49debf;p=policy%2Fmodels.git 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..a7ceaa9a5 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 @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2021 Nordix Foundation. + * Modifications Copyright (C) 2019-2020 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. @@ -22,53 +23,213 @@ 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.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; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; /** - * 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 static final String LATEST_VERSION = "LATEST"; - - // Regular expression +public class PdpGroupFilter implements PfObjectFilter { + // Name to find private String name; - // Regular Expression, set to LATEST_VERRSION to get the latest version - private String version; - + // State to find private PdpState groupState; - // Regular expression + // PDP type to find private String pdpType; // Set regular expressions on fields to match policy type names and versions - private ToscaPolicyTypeIdentifier policyType; + private List policyTypeList; + + // If set, only PDP groups where policy types are matched exactly are returned + @Builder.Default + private boolean matchPolicyTypesExactly = false; // Set regular expressions on fields to match policy names and versions - private ToscaPolicyIdentifier policy; + private List policyList; + + // If set, only PDP groups where policies are matched exactly are returned + @Builder.Default + private boolean matchPoliciesExactly = false; + + // If set, only PDP groups with PDPs in this state are returned + 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)) + .filter(p -> filterString(p.getName(), name)) + .filter(p -> groupState == null || ObjectUtils.compare(p.getPdpGroupState(), groupState) == 0) + .filter(p -> filterOnPdpType(p, pdpType)) + .filter(p -> filterOnPolicyTypeList(p, policyTypeList, matchPolicyTypesExactly)) + .filter(p -> filterOnPolicyList(p, policyList, matchPoliciesExactly)) + .filter(p -> filterOnPdpState(p, pdpState)) .collect(Collectors.toList()); - // @formatter:off + // @formatter:on + } + + /** + * 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 typeFilter the policy type regular expressions to check for + * @param matchPolicyTypesExactly if true, only PDP groups where policy types are matched exactly are returned + * @return true if the filter should let this PDP group through + */ + private boolean filterOnPolicyTypeList(final PdpGroup pdpGroup, final List typeFilter, + final boolean matchPolicyTypesExactly) { + if (typeFilter == null) { + return true; + } + + for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) { + if (matchPolicyTypesExactly) { + if (areListsIdentical(pdpSubGroup.getSupportedPolicyTypes(), typeFilter)) { + return true; + } + } else if (findSupportedPolicyType(pdpSubGroup.getSupportedPolicyTypes(), typeFilter)) { + return true; + } + } + + return false; + + } + + /** + * Find a single supported type. + * + * @param supportedPolicyTypes supported types + * @param typeFilter the list of types, one of which we wish to find supported by + * the list we are searching + * @return true if one element of the elements to find is supported by an element on + * the list we searched + */ + private boolean findSupportedPolicyType(List supportedPolicyTypes, + List typeFilter) { + for (ToscaConceptIdentifier supportedPolicyType : supportedPolicyTypes) { + String supName = supportedPolicyType.getName(); + if (supName.endsWith(".*")) { + String substr = supName.substring(0, supName.length() - 1); + if (typeFilter.stream().anyMatch(type -> type.getName().startsWith(substr))) { + return true; + } + } else if (typeFilter.contains(supportedPolicyType)) { + 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 + * @param matchPoliciesExactly if true, only PDP groups where ps are matched exactly are returned + * @return true if the filter should let this PDP group through + */ + private boolean filterOnPolicyList(final PdpGroup pdpGroup, final List policyFilter, + final boolean matchPoliciesExactly) { + if (policyFilter == null) { + return true; + } + + for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) { + if (matchPoliciesExactly) { + if (areListsIdentical(pdpSubGroup.getPolicies(), policyFilter)) { + return true; + } + } else if (findSingleElement(pdpSubGroup.getPolicies(), policyFilter)) { + return true; + } + } + + return false; + } + + /** + * Filter PDP groups on PDP state. + * + * @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 filterOnPdpState(final PdpGroup pdpGroup, final PdpState pdpState) { + if (pdpState == null) { + return true; + } + + for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) { + for (Pdp pdp : pdpSubGroup.getPdpInstances()) { + if (pdpState.equals(pdp.getPdpState())) { + return true; + } + } + } + + return false; + } + + /** + * Check if two lists have identical content. + * + * @param leftList the left list + * @param rightList the right list + * @return true if the lists are identical + */ + private boolean areListsIdentical(final List leftList, List rightList) { + return leftList.equals(rightList); + } + + /** + * Find a single element of a list in a list. + * + * @param listToSearch the list in which we are searching for elements + * @param listOfElementsToFind the list of elements, one of which we wish to find on the list we are searching + * @return true if one element of the elements to find is found on the list we searched + */ + private boolean findSingleElement(final List listToSearch, List listOfElementsToFind) { + for (Object elementToFind : listOfElementsToFind) { + if (listToSearch.contains(elementToFind)) { + return true; + } + } + + return false; } }