8f647a4eee874497ae06d8ab48b1379ba335703b
[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  *  Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.pdp.concepts;
23
24 import java.util.List;
25 import java.util.stream.Collectors;
26
27 import lombok.Builder;
28 import lombok.Data;
29 import lombok.NonNull;
30
31 import org.apache.commons.lang3.ObjectUtils;
32 import org.onap.policy.models.base.PfObjectFilter;
33 import org.onap.policy.models.pdp.enums.PdpState;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
36
37 /**
38  * Filter class for searches for {@link PdpGroup} instances. If any fields are null, they are ignored.
39  *
40  * @author Liam Fallon (liam.fallon@est.tech)
41  */
42 @Builder
43 @Data
44 public class PdpGroupFilter implements PfObjectFilter<PdpGroup> {
45     // Name to find
46     private String name;
47
48     // State to find
49     private PdpState groupState;
50
51     // PDP type to find
52     private String pdpType;
53
54     // Set regular expressions on fields to match policy type names and versions
55     private List<ToscaPolicyTypeIdentifier> policyTypeList;
56
57     // If set, only PDP groups where policy types are matched exactly are returned
58     @Builder.Default
59     private boolean matchPolicyTypesExactly = false;
60
61     // Set regular expressions on fields to match policy names and versions
62     private List<ToscaPolicyIdentifier> policyList;
63
64     // If set, only PDP groups where policies are matched exactly are returned
65     @Builder.Default
66     private boolean matchPoliciesExactly = false;
67
68     // If set, only PDP groups with PDPs in this state are returned
69     private PdpState pdpState;
70
71     @Override
72     public List<PdpGroup> filter(@NonNull final List<PdpGroup> originalList) {
73
74         // @formatter:off
75         return originalList.stream()
76                 .filter(p -> filterString(p.getName(), name))
77                 .filter(p -> groupState == null || ObjectUtils.compare(p.getPdpGroupState(), groupState) == 0)
78                 .filter(p -> filterOnPdpType(p, pdpType))
79                 .filter(p -> filterOnPolicyTypeList(p, policyTypeList, matchPolicyTypesExactly))
80                 .filter(p -> filterOnPolicyList(p, policyList, matchPoliciesExactly))
81                 .filter(p -> filterOnPdpState(p, pdpState))
82                 .collect(Collectors.toList());
83         // @formatter:on
84     }
85
86     /**
87      * Filter PDP groups on PDP type.
88      *
89      * @param pdpGroup the PDP group to check
90      * @param pdpType the PDP type to check for
91      * @return true if the filter should let this PDP group through
92      */
93     private boolean filterOnPdpType(final PdpGroup pdpGroup, final String pdpType) {
94         if (pdpType == null) {
95             return true;
96         }
97
98         for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
99             if (pdpSubGroup.getPdpType().equals(pdpType)) {
100                 return true;
101             }
102         }
103
104         return false;
105     }
106
107     /**
108      * Filter PDP groups on policy type.
109      *
110      * @param pdpGroup the PDP group to check
111      * @param typeFilter the policy type regular expressions to check for
112      * @param matchPolicyTypesExactly if true, only PDP groups where policy types are matched exactly are returned
113      * @return true if the filter should let this PDP group through
114      */
115     private boolean filterOnPolicyTypeList(final PdpGroup pdpGroup, final List<ToscaPolicyTypeIdentifier> typeFilter,
116             final boolean matchPolicyTypesExactly) {
117         if (typeFilter == null) {
118             return true;
119         }
120
121         for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
122             if (matchPolicyTypesExactly) {
123                 if (areListsIdentical(pdpSubGroup.getSupportedPolicyTypes(), typeFilter)) {
124                     return true;
125                 }
126             } else if (findSupportedPolicyType(pdpSubGroup.getSupportedPolicyTypes(), typeFilter)) {
127                 return true;
128             }
129         }
130
131         return false;
132
133     }
134
135     /**
136      * Find a single supported type.
137      *
138      * @param supportedPolicyTypes supported types
139      * @param typeFilter the list of types, one of which we wish to find supported by
140      *        the list we are searching
141      * @return true if one element of the elements to find is supported by an element on
142      *         the list we searched
143      */
144     private boolean findSupportedPolicyType(List<ToscaPolicyTypeIdentifier> supportedPolicyTypes,
145                     List<ToscaPolicyTypeIdentifier> typeFilter) {
146         for (ToscaPolicyTypeIdentifier supportedPolicyType : supportedPolicyTypes) {
147             String supName = supportedPolicyType.getName();
148             if (supName.endsWith(".*")) {
149                 String substr = supName.substring(0, supName.length() - 1);
150                 if (typeFilter.stream().anyMatch(type -> type.getName().startsWith(substr))) {
151                     return true;
152                 }
153             } else if (typeFilter.contains(supportedPolicyType)) {
154                 return true;
155             }
156         }
157
158         return false;
159     }
160
161     /**
162      * Filter PDP groups on policy.
163      *
164      * @param pdpGroup the PDP group to check
165      * @param policyFilter the policy regular expressions to check for
166      * @param matchPoliciesExactly if true, only PDP groups where ps are matched exactly are returned
167      * @return true if the filter should let this PDP group through
168      */
169     private boolean filterOnPolicyList(final PdpGroup pdpGroup, final List<ToscaPolicyIdentifier> policyFilter,
170             final boolean matchPoliciesExactly) {
171         if (policyFilter == null) {
172             return true;
173         }
174
175         for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
176             if (matchPoliciesExactly) {
177                 if (areListsIdentical(pdpSubGroup.getPolicies(), policyFilter)) {
178                     return true;
179                 }
180             } else if (findSingleElement(pdpSubGroup.getPolicies(), policyFilter)) {
181                 return true;
182             }
183         }
184
185         return false;
186     }
187
188     /**
189      * Filter PDP groups on PDP state.
190      *
191      * @param pdpGroup the PDP group to check
192      * @param policyFilter the policy regular expressions to check for
193      * @return true if the filter should let this PDP group through
194      */
195     private boolean filterOnPdpState(final PdpGroup pdpGroup, final PdpState pdpState) {
196         if (pdpState == null) {
197             return true;
198         }
199
200         for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
201             for (Pdp pdp : pdpSubGroup.getPdpInstances()) {
202                 if (pdpState.equals(pdp.getPdpState())) {
203                     return true;
204                 }
205             }
206         }
207
208         return false;
209     }
210
211     /**
212      * Check if two lists have identical content.
213      *
214      * @param leftList the left list
215      * @param rightList the right list
216      * @return true if the lists are identical
217      */
218     private <T> boolean areListsIdentical(final List<T> leftList, List<T> rightList) {
219         return leftList.equals(rightList);
220     }
221
222     /**
223      * Find a single element of a list in a list.
224      *
225      * @param listToSearch the list in which we are searching for elements
226      * @param listOfElementsToFind the list of elements, one of which we wish to find on the list we are searching
227      * @return true if one element of the elements to find is found on the list we searched
228      */
229     private <T> boolean findSingleElement(final List<T> listToSearch, List<T> listOfElementsToFind) {
230         for (Object elementToFind : listOfElementsToFind) {
231             if (listToSearch.contains(elementToFind)) {
232                 return true;
233             }
234         }
235
236         return false;
237     }
238 }