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