Merge "Add bug fixes and tests for filters"
[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     // Regular expression
47     private String name;
48
49     // Regular Expression, set to to get the latest version
50     private String version;
51
52     private PdpState groupState;
53
54     // Regular expression
55     private String pdpType;
56
57     // Set regular expressions on fields to match policy type names and versions
58     private ToscaPolicyTypeIdentifier policyType;
59
60     // Set regular expressions on fields to match policy names and versions
61     private ToscaPolicyIdentifier policy;
62
63     private PdpState pdpState;
64
65     @Override
66     public List<PdpGroup> filter(@NonNull final List<PdpGroup> originalList) {
67
68         // @formatter:off
69         List<PdpGroup> returnList = originalList.stream()
70                 .filter(p -> filterString(p.getName(), name))
71                 .filter(p -> (version != null && LATEST_VERSION.equals(version))
72                         || filterString(p.getVersion(), version))
73                 .filter(p -> groupState == null || ObjectUtils.compare(p.getPdpGroupState(), groupState) == 0)
74                 .filter(p -> filterOnPdpType(p, pdpType))
75                 .filter(p -> filterOnPolicyType(p, policyType))
76                 .filter(p -> filterOnPolicy(p, policy))
77                 .filter(p -> filterOnPdpState(p, pdpState))
78                 .collect(Collectors.toList());
79         // @formatter:off
80
81         if (LATEST_VERSION.equals(version)) {
82             returnList = this.latestVersionFilter(returnList);
83         }
84
85         return returnList;
86     }
87
88     /**
89      * Filter PDP groups on PDP type.
90      *
91      * @param pdpGroup the PDP group to check
92      * @param pdpType the PDP type to check for
93      * @return true if the filter should let this PDP group through
94      */
95     private boolean filterOnPdpType(final PdpGroup pdpGroup, final String pdpType) {
96         if (pdpType == null) {
97             return true;
98         }
99
100         for (PdpSubGroup pdpSubGroup: pdpGroup.getPdpSubgroups()) {
101             if (pdpSubGroup.getPdpType().equals(pdpType)) {
102                 return true;
103             }
104         }
105
106         return false;
107     }
108
109     /**
110      * Filter PDP groups on policy type.
111      *
112      * @param pdpGroup the PDP group to check
113      * @param policyTypeFilter the policy type regular expressions to check for
114      * @return true if the filter should let this PDP group through
115      */
116     private boolean filterOnPolicyType(final PdpGroup pdpGroup, final ToscaPolicyTypeIdentifier policyTypeFiler) {
117         if (policyTypeFiler == null) {
118             return true;
119         }
120
121         for (PdpSubGroup pdpSubGroup: pdpGroup.getPdpSubgroups()) {
122             for (ToscaPolicyTypeIdentifier foundPolicyType : pdpSubGroup.getSupportedPolicyTypes()) {
123                 if (foundPolicyType.getName().matches(policyTypeFiler.getName())
124                         && foundPolicyType.getVersion().matches(policyTypeFiler.getVersion())) {
125                     return true;
126                 }
127             }
128         }
129
130         return false;
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      * @return true if the filter should let this PDP group through
139      */
140     private boolean filterOnPolicy(final PdpGroup pdpGroup, final ToscaPolicyIdentifier policyFiler) {
141         if (policyFiler == null) {
142             return true;
143         }
144
145         for (PdpSubGroup pdpSubGroup: pdpGroup.getPdpSubgroups()) {
146             for (ToscaPolicyIdentifier foundPolicy : pdpSubGroup.getPolicies()) {
147                 if (foundPolicy.getName().matches(policyFiler.getName())
148                         && foundPolicy.getVersion().matches(policyFiler.getVersion())) {
149                     return true;
150                 }
151             }
152         }
153
154         return false;
155     }
156
157     /**
158      * Filter PDP groups on PDP state.
159      *
160      * @param pdpGroup the PDP group to check
161      * @param policyFilter the policy regular expressions to check for
162      * @return true if the filter should let this PDP group through
163      */
164     private boolean filterOnPdpState(final PdpGroup pdpGroup, final PdpState pdpState) {
165         if (pdpState == null) {
166             return true;
167         }
168
169         for (PdpSubGroup pdpSubGroup: pdpGroup.getPdpSubgroups()) {
170             for (Pdp pdp : pdpSubGroup.getPdpInstances()) {
171                 if (pdpState.equals(pdp.getPdpState())) {
172                     return true;
173                 }
174             }
175         }
176
177         return false;
178     }
179 }