2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.pdp.concepts;
23 import java.util.List;
24 import java.util.stream.Collectors;
26 import lombok.Builder;
28 import lombok.NonNull;
30 import org.apache.commons.lang3.ObjectUtils;
31 import org.onap.policy.models.base.PfObjectFilter;
32 import org.onap.policy.models.pdp.enums.PdpState;
33 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
37 * Filter class for searches for {@link PdpGroup} instances. If any fields are null, they are ignored.
39 * @author Liam Fallon (liam.fallon@est.tech)
43 public class PdpGroupFilter implements PfObjectFilter<PdpGroup> {
48 private PdpState groupState;
51 private String pdpType;
53 // Set regular expressions on fields to match policy type names and versions
54 private List<ToscaPolicyTypeIdentifier> policyTypeList;
56 // If set, only PDP groups where policy types are matched exactly are returned
58 private boolean matchPolicyTypesExactly = false;
60 // Set regular expressions on fields to match policy names and versions
61 private List<ToscaPolicyIdentifier> policyList;
63 // If set, only PDP groups where policies are matched exactly are returned
65 private boolean matchPoliciesExactly = false;
67 // If set, only PDP groups with PDPs in this state are returned
68 private PdpState pdpState;
71 public List<PdpGroup> filter(@NonNull final List<PdpGroup> originalList) {
74 return originalList.stream()
75 .filter(p -> filterString(p.getName(), name))
76 .filter(p -> groupState == null || ObjectUtils.compare(p.getPdpGroupState(), groupState) == 0)
77 .filter(p -> filterOnPdpType(p, pdpType))
78 .filter(p -> filterOnPolicyTypeList(p, policyTypeList, matchPolicyTypesExactly))
79 .filter(p -> filterOnPolicyList(p, policyList, matchPoliciesExactly))
80 .filter(p -> filterOnPdpState(p, pdpState))
81 .collect(Collectors.toList());
86 * Filter PDP groups on PDP type.
88 * @param pdpGroup the PDP group to check
89 * @param pdpType the PDP type to check for
90 * @return true if the filter should let this PDP group through
92 private boolean filterOnPdpType(final PdpGroup pdpGroup, final String pdpType) {
93 if (pdpType == null) {
97 for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
98 if (pdpSubGroup.getPdpType().equals(pdpType)) {
107 * Filter PDP groups on policy type.
109 * @param pdpGroup the PDP group to check
110 * @param typeFilter the policy type regular expressions to check for
111 * @param matchPolicyTypesExactly if true, only PDP groups where policy types are matched exactly are returned
112 * @return true if the filter should let this PDP group through
114 private boolean filterOnPolicyTypeList(final PdpGroup pdpGroup, final List<ToscaPolicyTypeIdentifier> typeFilter,
115 final boolean matchPolicyTypesExactly) {
116 if (typeFilter == null) {
120 for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
121 if (matchPolicyTypesExactly && areListsIdentical(pdpSubGroup.getSupportedPolicyTypes(), typeFilter)) {
123 } else if (!matchPolicyTypesExactly
124 && findSingleElement(pdpSubGroup.getSupportedPolicyTypes(), typeFilter)) {
134 * Filter PDP groups on policy.
136 * @param pdpGroup the PDP group to check
137 * @param policyFilter the policy regular expressions to check for
138 * @param matchPoliciesExactly if true, only PDP groups where ps are matched exactly are returned
139 * @return true if the filter should let this PDP group through
141 private boolean filterOnPolicyList(final PdpGroup pdpGroup, final List<ToscaPolicyIdentifier> policyFilter,
142 final boolean matchPoliciesExactly) {
143 if (policyFilter == null) {
147 for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
148 if (matchPoliciesExactly && areListsIdentical(pdpSubGroup.getPolicies(), policyFilter)) {
150 } else if (!matchPoliciesExactly && findSingleElement(pdpSubGroup.getPolicies(), policyFilter)) {
159 * Filter PDP groups on PDP state.
161 * @param pdpGroup the PDP group to check
162 * @param policyFilter the policy regular expressions to check for
163 * @return true if the filter should let this PDP group through
165 private boolean filterOnPdpState(final PdpGroup pdpGroup, final PdpState pdpState) {
166 if (pdpState == null) {
170 for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
171 for (Pdp pdp : pdpSubGroup.getPdpInstances()) {
172 if (pdpState.equals(pdp.getPdpState())) {
182 * Check if two lists have identical content.
184 * @param leftList the left list
185 * @param rightList the right list
186 * @return true if the lists are identical
188 private <T> boolean areListsIdentical(final List<T> leftList, List<T> rightList) {
189 return leftList.equals(rightList);
193 * Find a single element of a list in a list.
195 * @param listToSearch the list in which we are searching for elements
196 * @param listOfElementsToFind the list of elements, one of which we wish to find on the list we are searching
197 * @return true if one element of the elements to find is found on the list we searched
199 private <T> boolean findSingleElement(final List<T> listToSearch, List<T> listOfElementsToFind) {
200 for (Object elementToFind : listOfElementsToFind) {
201 if (listToSearch.contains(elementToFind)) {