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> {
44 public static final String LATEST_VERSION = "LATEST";
49 // Version to find, set to LATEST_VERSION to get the latest version
50 private String version;
53 private PdpState groupState;
56 private String pdpType;
58 // Set regular expressions on fields to match policy type names and versions
59 private List<ToscaPolicyTypeIdentifier> policyTypeList;
61 // If set, only PDP groups where policy types are matched exactly are returned
63 private boolean matchPolicyTypesExactly = false;
65 // Set regular expressions on fields to match policy names and versions
66 private List<ToscaPolicyIdentifier> policyList;
68 // If set, only PDP groups where policies are matched exactly are returned
70 private boolean matchPoliciesExactly = false;
72 // If set, only PDP groups with PDPs in this state are returned
73 private PdpState pdpState;
76 public List<PdpGroup> filter(@NonNull final List<PdpGroup> originalList) {
79 List<PdpGroup> returnList = originalList.stream()
80 .filter(p -> filterString(p.getName(), name))
81 .filter(p -> LATEST_VERSION.equals(version)
82 || filterString(p.getVersion(), version))
83 .filter(p -> groupState == null || ObjectUtils.compare(p.getPdpGroupState(), groupState) == 0)
84 .filter(p -> filterOnPdpType(p, pdpType))
85 .filter(p -> filterOnPolicyTypeList(p, policyTypeList, matchPolicyTypesExactly))
86 .filter(p -> filterOnPolicyList(p, policyList, matchPoliciesExactly))
87 .filter(p -> filterOnPdpState(p, pdpState))
88 .collect(Collectors.toList());
91 if (LATEST_VERSION.equals(version)) {
92 returnList = this.latestVersionFilter(returnList);
99 * Filter PDP groups on PDP type.
101 * @param pdpGroup the PDP group to check
102 * @param pdpType the PDP type to check for
103 * @return true if the filter should let this PDP group through
105 private boolean filterOnPdpType(final PdpGroup pdpGroup, final String pdpType) {
106 if (pdpType == null) {
110 for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
111 if (pdpSubGroup.getPdpType().equals(pdpType)) {
120 * Filter PDP groups on policy type.
122 * @param pdpGroup the PDP group to check
123 * @param typeFilter the policy type regular expressions to check for
124 * @param matchPolicyTypesExactly if true, only PDP groups where policy types are matched exactly are returned
125 * @return true if the filter should let this PDP group through
127 private boolean filterOnPolicyTypeList(final PdpGroup pdpGroup, final List<ToscaPolicyTypeIdentifier> typeFilter,
128 final boolean matchPolicyTypesExactly) {
129 if (typeFilter == null) {
133 for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
134 if (matchPolicyTypesExactly && areListsIdentical(pdpSubGroup.getSupportedPolicyTypes(), typeFilter)) {
136 } else if (!matchPolicyTypesExactly
137 && findSingleElement(pdpSubGroup.getSupportedPolicyTypes(), typeFilter)) {
147 * Filter PDP groups on policy.
149 * @param pdpGroup the PDP group to check
150 * @param policyFilter the policy regular expressions to check for
151 * @param matchPoliciesExactly if true, only PDP groups where ps are matched exactly are returned
152 * @return true if the filter should let this PDP group through
154 private boolean filterOnPolicyList(final PdpGroup pdpGroup, final List<ToscaPolicyIdentifier> policyFilter,
155 final boolean matchPoliciesExactly) {
156 if (policyFilter == null) {
160 for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
161 if (matchPoliciesExactly && areListsIdentical(pdpSubGroup.getPolicies(), policyFilter)) {
163 } else if (!matchPoliciesExactly && findSingleElement(pdpSubGroup.getPolicies(), policyFilter)) {
172 * Filter PDP groups on PDP state.
174 * @param pdpGroup the PDP group to check
175 * @param policyFilter the policy regular expressions to check for
176 * @return true if the filter should let this PDP group through
178 private boolean filterOnPdpState(final PdpGroup pdpGroup, final PdpState pdpState) {
179 if (pdpState == null) {
183 for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
184 for (Pdp pdp : pdpSubGroup.getPdpInstances()) {
185 if (pdpState.equals(pdp.getPdpState())) {
195 * Check if two lists have identical content.
197 * @param leftList the left list
198 * @param rightList the right list
199 * @return true if the lists are identical
201 private <T> boolean areListsIdentical(final List<T> leftList, List<T> rightList) {
202 return leftList.equals(rightList);
206 * Find a single element of a list in a list.
208 * @param listToSearch the list in which we are searching for elements
209 * @param listOfElementsToFind the list of elements, one of which we wish to find on the list we are searching
210 * @return true if one element of the elements to find is found on the list we searched
212 private <T> boolean findSingleElement(final List<T> listToSearch, List<T> listOfElementsToFind) {
213 for (Object elementToFind : listOfElementsToFind) {
214 if (listToSearch.contains(elementToFind)) {