0f86c689009d922a8bdd5f09aa3ad26a86a19f00
[policy/models.git] / models-pdp / src / main / java / org / onap / policy / models / pdp / concepts / PdpGroupFilter.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.pdp.concepts;
22
23 import java.util.List;
24 import java.util.stream.Collectors;
25
26 import lombok.Builder;
27 import lombok.Data;
28 import lombok.NonNull;
29
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;
35
36 /**
37  * Filter class for searches for {@link PdpGroup} instances. If any fields are null, they are ignored.
38  *
39  * @author Liam Fallon (liam.fallon@est.tech)
40  */
41 @Builder
42 @Data
43 public class PdpGroupFilter implements PfObjectFilter<PdpGroup> {
44     public static final String LATEST_VERSION = "LATEST";
45
46     // Name to find
47     private String name;
48
49     // Version to find, set to LATEST_VERSION to get the latest version
50     private String version;
51
52     // State to find
53     private PdpState groupState;
54
55     // PDP type to find
56     private String pdpType;
57
58     // Set regular expressions on fields to match policy type names and versions
59     private List<ToscaPolicyTypeIdentifier> policyTypeList;
60
61     // If set, only PDP groups where policy types are matched exactly are returned
62     @Builder.Default
63     private boolean matchPolicyTypesExactly = false;
64
65     // Set regular expressions on fields to match policy names and versions
66     private List<ToscaPolicyIdentifier> policyList;
67
68     // If set, only PDP groups where policies are matched exactly are returned
69     @Builder.Default
70     private boolean matchPoliciesExactly = false;
71
72     // If set, only PDP groups with PDPs in this state are returned
73     private PdpState pdpState;
74
75     @Override
76     public List<PdpGroup> filter(@NonNull final List<PdpGroup> originalList) {
77
78         // @formatter:off
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());
89         // @formatter:on
90
91         if (LATEST_VERSION.equals(version)) {
92             returnList = this.latestVersionFilter(returnList);
93         }
94
95         return returnList;
96     }
97
98     /**
99      * Filter PDP groups on PDP type.
100      *
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
104      */
105     private boolean filterOnPdpType(final PdpGroup pdpGroup, final String pdpType) {
106         if (pdpType == null) {
107             return true;
108         }
109
110         for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
111             if (pdpSubGroup.getPdpType().equals(pdpType)) {
112                 return true;
113             }
114         }
115
116         return false;
117     }
118
119     /**
120      * Filter PDP groups on policy type.
121      *
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
126      */
127     private boolean filterOnPolicyTypeList(final PdpGroup pdpGroup, final List<ToscaPolicyTypeIdentifier> typeFilter,
128             final boolean matchPolicyTypesExactly) {
129         if (typeFilter == null) {
130             return true;
131         }
132
133         for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
134             if (matchPolicyTypesExactly && areListsIdentical(pdpSubGroup.getSupportedPolicyTypes(), typeFilter)) {
135                 return true;
136             } else if (!matchPolicyTypesExactly
137                     && findSingleElement(pdpSubGroup.getSupportedPolicyTypes(), typeFilter)) {
138                 return true;
139             }
140         }
141
142         return false;
143
144     }
145
146     /**
147      * Filter PDP groups on policy.
148      *
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
153      */
154     private boolean filterOnPolicyList(final PdpGroup pdpGroup, final List<ToscaPolicyIdentifier> policyFilter,
155             final boolean matchPoliciesExactly) {
156         if (policyFilter == null) {
157             return true;
158         }
159
160         for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
161             if (matchPoliciesExactly && areListsIdentical(pdpSubGroup.getPolicies(), policyFilter)) {
162                 return true;
163             } else if (!matchPoliciesExactly && findSingleElement(pdpSubGroup.getPolicies(), policyFilter)) {
164                 return true;
165             }
166         }
167
168         return false;
169     }
170
171     /**
172      * Filter PDP groups on PDP state.
173      *
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
177      */
178     private boolean filterOnPdpState(final PdpGroup pdpGroup, final PdpState pdpState) {
179         if (pdpState == null) {
180             return true;
181         }
182
183         for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
184             for (Pdp pdp : pdpSubGroup.getPdpInstances()) {
185                 if (pdpState.equals(pdp.getPdpState())) {
186                     return true;
187                 }
188             }
189         }
190
191         return false;
192     }
193
194     /**
195      * Check if two lists have identical content.
196      *
197      * @param leftList the left list
198      * @param rightList the right list
199      * @return true if the lists are identical
200      */
201     private <T> boolean areListsIdentical(final List<T> leftList, List<T> rightList) {
202         return leftList.equals(rightList);
203     }
204
205     /**
206      * Find a single element of a list in a list.
207      *
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
211      */
212     private <T> boolean findSingleElement(final List<T> listToSearch, List<T> listOfElementsToFind) {
213         for (Object elementToFind : listOfElementsToFind) {
214             if (listToSearch.contains(elementToFind)) {
215                 return true;
216             }
217         }
218
219         return false;
220     }
221 }