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