1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package gov.nist.secauto.oscal.lib.profile.resolver.selection;
28
29 import gov.nist.secauto.metaschema.model.common.util.ObjectUtils;
30 import gov.nist.secauto.oscal.lib.model.IncludeAll;
31 import gov.nist.secauto.oscal.lib.model.ProfileImport;
32 import gov.nist.secauto.oscal.lib.model.control.catalog.IControl;
33 import gov.nist.secauto.oscal.lib.model.control.profile.IProfileSelectControlById;
34
35 import org.apache.commons.lang3.tuple.Pair;
36
37 import java.util.List;
38
39 import edu.umd.cs.findbugs.annotations.NonNull;
40
41 public interface IControlFilter {
42 @NonNull
43 IControlFilter ALWAYS_MATCH = new IControlFilter() {
44 @Override
45 public @NonNull Pair<Boolean, Boolean> match(@NonNull IControl control, boolean defaultMatch) {
46 return IControlSelectionFilter.MATCH;
47 }
48
49 @Override
50 public @NonNull IControlSelectionFilter getInclusionFilter() {
51 return IControlSelectionFilter.ALL_MATCH;
52 }
53
54 @Override
55 public @NonNull IControlSelectionFilter getExclusionFilter() {
56 return IControlSelectionFilter.NONE_MATCH;
57 }
58 };
59
60 @NonNull
61 IControlFilter NONE_MATCH = new IControlFilter() {
62
63 @Override
64 public @NonNull Pair<Boolean, Boolean> match(@NonNull IControl control, boolean defaultMatch) {
65 return IControlSelectionFilter.NON_MATCH;
66 }
67
68 @Override
69 public @NonNull IControlSelectionFilter getInclusionFilter() {
70 return IControlSelectionFilter.NONE_MATCH;
71 }
72
73 @Override
74 public @NonNull IControlSelectionFilter getExclusionFilter() {
75 return IControlSelectionFilter.NONE_MATCH;
76 }
77 };
78
79
80
81
82
83
84
85
86
87 @NonNull
88 static IControlFilter newInstance(@NonNull ProfileImport profileImport) {
89 return new Filter(profileImport);
90 }
91
92 @NonNull
93 static IControlFilter newInstance(@NonNull IControlSelectionFilter includes,
94 @NonNull IControlSelectionFilter excludes) {
95 return new Filter(includes, excludes);
96 }
97
98
99
100
101
102
103
104
105
106
107
108
109 @NonNull
110 default Pair<Boolean, Boolean> match(@NonNull IControl control) {
111 return match(control, false);
112 }
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127 @NonNull
128 Pair<Boolean, Boolean> match(@NonNull IControl control, boolean defaultMatch);
129
130 @NonNull
131 IControlSelectionFilter getInclusionFilter();
132
133 @NonNull
134 IControlSelectionFilter getExclusionFilter();
135
136 class Filter implements IControlFilter {
137 @NonNull
138 private final IControlSelectionFilter inclusionFilter;
139 @NonNull
140 private final IControlSelectionFilter exclusionFilter;
141
142 public Filter(@NonNull ProfileImport profileImport) {
143 IncludeAll includeAll = profileImport.getIncludeAll();
144
145 if (includeAll == null) {
146 List<? extends IProfileSelectControlById> selections = profileImport.getIncludeControls();
147 if (selections == null) {
148 this.inclusionFilter = IControlSelectionFilter.NONE_MATCH;
149 } else {
150 this.inclusionFilter = new DefaultControlSelectionFilter(selections);
151 }
152 } else {
153 this.inclusionFilter = IControlSelectionFilter.ALL_MATCH;
154 }
155
156 List<? extends IProfileSelectControlById> selections = profileImport.getExcludeControls();
157 if (selections == null) {
158 this.exclusionFilter = IControlSelectionFilter.NONE_MATCH;
159 } else {
160 this.exclusionFilter = new DefaultControlSelectionFilter(selections);
161 }
162
163 }
164
165 public Filter(@NonNull IControlSelectionFilter includes, @NonNull IControlSelectionFilter excludes) {
166 this.inclusionFilter = includes;
167 this.exclusionFilter = excludes;
168 }
169
170 @Override
171 @NonNull
172 public IControlSelectionFilter getInclusionFilter() {
173 return inclusionFilter;
174 }
175
176 @Override
177 @NonNull
178 public IControlSelectionFilter getExclusionFilter() {
179 return exclusionFilter;
180 }
181
182 @Override
183 public Pair<Boolean, Boolean> match(@NonNull IControl control, boolean defaultMatch) {
184 @NonNull Pair<Boolean, Boolean> result = getInclusionFilter().apply(control);
185 boolean left = ObjectUtils.notNull(result.getLeft());
186 if (left) {
187
188 Pair<Boolean, Boolean> excluded = getExclusionFilter().apply(control);
189 if (ObjectUtils.notNull(excluded.getLeft())) {
190
191 result = IControlSelectionFilter.NON_MATCH;
192 }
193 } else {
194 result = defaultMatch ? IControlSelectionFilter.MATCH : IControlSelectionFilter.NON_MATCH;
195 }
196 return result;
197 }
198
199 }
200
201 }