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 // Regular Expression, set to LATEST_VERRSION to get the latest version
50 private String version;
52 private PdpState groupState;
55 private String pdpType;
57 // Set regular expressions on fields to match policy type names and versions
58 private ToscaPolicyTypeIdentifier policyType;
60 // Set regular expressions on fields to match policy names and versions
61 private ToscaPolicyIdentifier policy;
63 private PdpState pdpState;
66 public List<PdpGroup> filter(@NonNull final List<PdpGroup> originalList) {
69 List<PdpGroup> returnList = originalList.stream()
70 .filter(p -> filterOnRegexp(p.getName(), name))
71 .filter(p -> version.equals(LATEST_VERSION) || filterOnRegexp(p.getVersion(), version))
72 .filter(p -> ObjectUtils.compare(p.getPdpGroupState(), groupState) == 0)
73 .filter(p -> filterOnPdpType(p, pdpType))
74 .filter(p -> filterOnPolicyType(p, policyType))
75 .filter(p -> filterOnPolicy(p, policy))
76 .filter(p -> filterOnPdpState(p, pdpState))
77 .collect(Collectors.toList());
80 if (LATEST_VERSION.equals(version)) {
81 returnList = this.latestVersionFilter(returnList);
88 * Filter PDP groups on PDP type.
90 * @param pdpGroup the PDP group to check
91 * @param pdpType the PDP type to check for
92 * @return true if the filter should let this PDP group through
94 private boolean filterOnPdpType(final PdpGroup pdpGroup, final String pdpType) {
95 if (pdpType == null) {
99 for (PdpSubGroup pdpSubGroup: pdpGroup.getPdpSubgroups()) {
100 if (pdpSubGroup.getPdpType().equals(pdpType)) {
109 * Filter PDP groups on policy type.
111 * @param pdpGroup the PDP group to check
112 * @param policyTypeFilter the policy type regular expressions to check for
113 * @return true if the filter should let this PDP group through
115 private boolean filterOnPolicyType(final PdpGroup pdpGroup, final ToscaPolicyTypeIdentifier policyTypeFiler) {
116 if (policyTypeFiler == null) {
120 for (PdpSubGroup pdpSubGroup: pdpGroup.getPdpSubgroups()) {
121 for (ToscaPolicyTypeIdentifier foundPolicyType : pdpSubGroup.getSupportedPolicyTypes()) {
122 if (foundPolicyType.getName().matches(policyTypeFiler.getName())
123 && foundPolicyType.getVersion().matches(policyTypeFiler.getVersion())) {
133 * Filter PDP groups on policy.
135 * @param pdpGroup the PDP group to check
136 * @param policyFilter the policy regular expressions to check for
137 * @return true if the filter should let this PDP group through
139 private boolean filterOnPolicy(final PdpGroup pdpGroup, final ToscaPolicyIdentifier policyFiler) {
140 if (policyFiler == null) {
144 for (PdpSubGroup pdpSubGroup: pdpGroup.getPdpSubgroups()) {
145 for (ToscaPolicyIdentifier foundPolicy : pdpSubGroup.getPolicies()) {
146 if (foundPolicy.getName().matches(policyFiler.getName())
147 && foundPolicy.getVersion().matches(policyFiler.getVersion())) {
157 * Filter PDP groups on PDP state.
159 * @param pdpGroup the PDP group to check
160 * @param policyFilter the policy regular expressions to check for
161 * @return true if the filter should let this PDP group through
163 private boolean filterOnPdpState(final PdpGroup pdpGroup, final PdpState pdpState) {
164 if (pdpState == null) {
168 for (PdpSubGroup pdpSubGroup: pdpGroup.getPdpSubgroups()) {
169 for (Pdp pdp : pdpSubGroup.getPdpInstances()) {
170 if (pdpState.equals(pdp.getPdpState())) {