Remove version from PdpGroup
[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     // Name to find
45     private String name;
46
47     // State to find
48     private PdpState groupState;
49
50     // PDP type to find
51     private String pdpType;
52
53     // Set regular expressions on fields to match policy type names and versions
54     private List<ToscaPolicyTypeIdentifier> policyTypeList;
55
56     // If set, only PDP groups where policy types are matched exactly are returned
57     @Builder.Default
58     private boolean matchPolicyTypesExactly = false;
59
60     // Set regular expressions on fields to match policy names and versions
61     private List<ToscaPolicyIdentifier> policyList;
62
63     // If set, only PDP groups where policies are matched exactly are returned
64     @Builder.Default
65     private boolean matchPoliciesExactly = false;
66
67     // If set, only PDP groups with PDPs in this state are returned
68     private PdpState pdpState;
69
70     @Override
71     public List<PdpGroup> filter(@NonNull final List<PdpGroup> originalList) {
72
73         // @formatter:off
74         return originalList.stream()
75                 .filter(p -> filterString(p.getName(), name))
76                 .filter(p -> groupState == null || ObjectUtils.compare(p.getPdpGroupState(), groupState) == 0)
77                 .filter(p -> filterOnPdpType(p, pdpType))
78                 .filter(p -> filterOnPolicyTypeList(p, policyTypeList, matchPolicyTypesExactly))
79                 .filter(p -> filterOnPolicyList(p, policyList, matchPoliciesExactly))
80                 .filter(p -> filterOnPdpState(p, pdpState))
81                 .collect(Collectors.toList());
82         // @formatter:on
83     }
84
85     /**
86      * Filter PDP groups on PDP type.
87      *
88      * @param pdpGroup the PDP group to check
89      * @param pdpType the PDP type to check for
90      * @return true if the filter should let this PDP group through
91      */
92     private boolean filterOnPdpType(final PdpGroup pdpGroup, final String pdpType) {
93         if (pdpType == null) {
94             return true;
95         }
96
97         for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
98             if (pdpSubGroup.getPdpType().equals(pdpType)) {
99                 return true;
100             }
101         }
102
103         return false;
104     }
105
106     /**
107      * Filter PDP groups on policy type.
108      *
109      * @param pdpGroup the PDP group to check
110      * @param typeFilter the policy type regular expressions to check for
111      * @param matchPolicyTypesExactly if true, only PDP groups where policy types are matched exactly are returned
112      * @return true if the filter should let this PDP group through
113      */
114     private boolean filterOnPolicyTypeList(final PdpGroup pdpGroup, final List<ToscaPolicyTypeIdentifier> typeFilter,
115             final boolean matchPolicyTypesExactly) {
116         if (typeFilter == null) {
117             return true;
118         }
119
120         for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
121             if (matchPolicyTypesExactly && areListsIdentical(pdpSubGroup.getSupportedPolicyTypes(), typeFilter)) {
122                 return true;
123             } else if (!matchPolicyTypesExactly
124                     && findSingleElement(pdpSubGroup.getSupportedPolicyTypes(), typeFilter)) {
125                 return true;
126             }
127         }
128
129         return false;
130
131     }
132
133     /**
134      * Filter PDP groups on policy.
135      *
136      * @param pdpGroup the PDP group to check
137      * @param policyFilter the policy regular expressions to check for
138      * @param matchPoliciesExactly if true, only PDP groups where ps are matched exactly are returned
139      * @return true if the filter should let this PDP group through
140      */
141     private boolean filterOnPolicyList(final PdpGroup pdpGroup, final List<ToscaPolicyIdentifier> policyFilter,
142             final boolean matchPoliciesExactly) {
143         if (policyFilter == null) {
144             return true;
145         }
146
147         for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
148             if (matchPoliciesExactly && areListsIdentical(pdpSubGroup.getPolicies(), policyFilter)) {
149                 return true;
150             } else if (!matchPoliciesExactly && findSingleElement(pdpSubGroup.getPolicies(), policyFilter)) {
151                 return true;
152             }
153         }
154
155         return false;
156     }
157
158     /**
159      * Filter PDP groups on PDP state.
160      *
161      * @param pdpGroup the PDP group to check
162      * @param policyFilter the policy regular expressions to check for
163      * @return true if the filter should let this PDP group through
164      */
165     private boolean filterOnPdpState(final PdpGroup pdpGroup, final PdpState pdpState) {
166         if (pdpState == null) {
167             return true;
168         }
169
170         for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
171             for (Pdp pdp : pdpSubGroup.getPdpInstances()) {
172                 if (pdpState.equals(pdp.getPdpState())) {
173                     return true;
174                 }
175             }
176         }
177
178         return false;
179     }
180
181     /**
182      * Check if two lists have identical content.
183      *
184      * @param leftList the left list
185      * @param rightList the right list
186      * @return true if the lists are identical
187      */
188     private <T> boolean areListsIdentical(final List<T> leftList, List<T> rightList) {
189         return leftList.equals(rightList);
190     }
191
192     /**
193      * Find a single element of a list in a list.
194      *
195      * @param listToSearch the list in which we are searching for elements
196      * @param listOfElementsToFind the list of elements, one of which we wish to find on the list we are searching
197      * @return true if one element of the elements to find is found on the list we searched
198      */
199     private <T> boolean findSingleElement(final List<T> listToSearch, List<T> listOfElementsToFind) {
200         for (Object elementToFind : listOfElementsToFind) {
201             if (listToSearch.contains(elementToFind)) {
202                 return true;
203             }
204         }
205
206         return false;
207     }
208 }