blob: d1f30e125decf24523020f45ac48ce6b5e8c889f [file] [log] [blame]
Frank Wange33e4ed2017-06-28 10:01:07 +08001/*
2 * Copyright 2017-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.net.flow.criteria;
18
19import com.google.common.collect.Maps;
20import com.google.common.testing.EqualsTester;
21import org.apache.commons.collections.CollectionUtils;
22import org.junit.Test;
23import org.onosproject.net.pi.runtime.PiExactFieldMatch;
24import org.onosproject.net.pi.runtime.PiFieldMatch;
25import org.onosproject.net.pi.runtime.PiHeaderFieldId;
26import org.onosproject.net.pi.runtime.PiLpmFieldMatch;
27import org.onosproject.net.pi.runtime.PiRangeFieldMatch;
28import org.onosproject.net.pi.runtime.PiTernaryFieldMatch;
29import org.onosproject.net.pi.runtime.PiValidFieldMatch;
30
31import java.util.Map;
32
33import static org.hamcrest.MatcherAssert.assertThat;
34import static org.hamcrest.Matchers.equalTo;
35import static org.hamcrest.Matchers.instanceOf;
36import static org.hamcrest.Matchers.is;
37import static org.hamcrest.Matchers.notNullValue;
38import static org.onlab.util.ImmutableByteSequence.copyFrom;
39
40/**
41 * Unit tests for the PiCriteria class.
42 */
43public class PiCriteriaTest {
44
45 PiHeaderFieldId piEthHeaderFieldId = PiHeaderFieldId.of("ethernet_t", "etherType");
46 byte[] matchExactBytes1 = {0x08, 0x00};
47 byte[] matchExactBytes2 = {0x08, 0x06};
48 Criterion matchPiExactByte1 = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactBytes1).build();
49 Criterion sameAsMatchPiExactByte1 = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactBytes1).build();
50 Criterion matchPiExactByte2 = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactBytes2).build();
51
52 short matchExactShort1 = 0x800;
53 short matchExactShort2 = 0x806;
54 Criterion matchPiExactShort1 = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactShort1).build();
55 Criterion sameAsMatchPiExactShort1 = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactShort1).build();
56 Criterion matchPiExactShort2 = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactShort2).build();
57
58 int matchExactInt1 = 0x800;
59 int matchExactInt2 = 0x806;
60 Criterion matchPiExactInt1 = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactInt1).build();
61 Criterion sameAsMatchPiExactInt1 = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactInt1).build();
62 Criterion matchPiExactInt2 = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactInt2).build();
63
64 long matchExactLong1 = 0x800;
65 long matchExactLong2 = 0x806;
66 Criterion matchPiExactLong1 = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactLong1).build();
67 Criterion sameAsMatchPiExactLong1 = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactLong1).build();
68 Criterion matchPiExactLong2 = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactLong2).build();
69
70 PiHeaderFieldId piIpv4HeaderFieldId = PiHeaderFieldId.of("ipv4_t", "dstAddr");
71 int mask = 0x00ffffff;
72 byte[] matchLpmBytes1 = {0x0a, 0x01, 0x01, 0x01};
73 byte[] matchLpmBytes2 = {0x0a, 0x01, 0x01, 0x02};
74 Criterion matchPiLpmByte1 = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId, matchLpmBytes1, mask).build();
75 Criterion sameAsMatchPiLpmByte1 = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId, matchLpmBytes1, mask).build();
76 Criterion matchPiLpmByte2 = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId, matchLpmBytes2, mask).build();
77
78 short matchLpmShort1 = 0x0a0a;
79 short matchLpmShort2 = 0x0a0b;
80 Criterion matchPiLpmShort1 = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId, matchLpmShort1, mask).build();
81 Criterion sameAsMatchPiLpmShort1 = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId,
82 matchLpmShort1, mask)
83 .build();
84 Criterion matchPiLpmShort2 = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId, matchLpmShort2, mask).build();
85
86 int matchLpmInt1 = 0x0a010101;
87 int matchLpmInt2 = 0x0a010102;
88 Criterion matchPiLpmInt1 = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId, matchLpmInt1, mask).build();
89 Criterion sameAsMatchPiLpmInt1 = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId, matchLpmInt1, mask).build();
90 Criterion matchPiLpmInt2 = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId, matchLpmInt2, mask).build();
91
92 long matchLpmLong1 = 0x0a010101;
93 long matchLpmLong2 = 0x0a010102;
94 Criterion matchPiLpmLong1 = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId, matchLpmLong1, mask).build();
95 Criterion sameAsMatchPiLpmLong1 = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId, matchLpmLong1, mask).build();
96 Criterion matchPiLpmLong2 = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId, matchLpmLong2, mask).build();
97
98
99 byte[] matchTernaryBytes1 = {0x0a, 0x01, 0x01, 0x01};
100 byte[] matchTernaryBytes2 = {0x0a, 0x01, 0x01, 0x02};
101 byte[] matchTernaryMaskBytes = {0x7f, 0x7f, 0x7f, 0x00};
102 Criterion matchPiTernaryByte1 = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId,
103 matchTernaryBytes1,
104 matchTernaryMaskBytes)
105 .build();
106 Criterion sameAsMatchPiTernaryByte1 = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId,
107 matchTernaryBytes1,
108 matchTernaryMaskBytes)
109 .build();
110 Criterion matchPiTernaryByte2 = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId,
111 matchTernaryBytes2,
112 matchTernaryMaskBytes)
113 .build();
114
115 short matchTernaryShort1 = 0x0a0a;
116 short matchTernaryShort2 = 0x0a0b;
117 short matchTernaryMaskShort = 0xff0;
118 Criterion matchPiTernaryShort1 = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId,
119 matchTernaryShort1,
120 matchTernaryMaskShort)
121 .build();
122 Criterion sameAsMatchPiTernaryShort1 = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId,
123 matchTernaryShort1,
124 matchTernaryMaskShort)
125 .build();
126 Criterion matchPiTernaryShort2 = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId,
127 matchTernaryShort2,
128 matchTernaryMaskShort)
129 .build();
130
131 int matchTernaryInt1 = 0x0a010101;
132 int matchTernaryInt2 = 0x0a010102;
133 int matchTernaryMaskInt = 0xffff;
134 Criterion matchPiTernaryInt1 = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId,
135 matchTernaryInt1,
136 matchTernaryMaskInt)
137 .build();
138 Criterion sameAsMatchPiTernaryInt1 = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId,
139 matchTernaryInt1,
140 matchTernaryMaskInt)
141 .build();
142 Criterion matchPiTernaryInt2 = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId,
143 matchTernaryInt2,
144 matchTernaryMaskInt)
145 .build();
146
147 long matchTernaryLong1 = 0x0a010101;
148 long matchTernaryLong2 = 0x0a010102;
149 long matchTernaryMaskLong = 0xffff;
150 Criterion matchPiTernaryLong1 = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId,
151 matchTernaryLong1,
152 matchTernaryMaskLong)
153 .build();
154 Criterion sameAsMatchPiTernaryLong1 = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId,
155 matchTernaryLong1,
156 matchTernaryMaskLong)
157 .build();
158 Criterion matchPiTernaryLong2 = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId,
159 matchTernaryLong2,
160 matchTernaryMaskLong)
161 .build();
162
163 Criterion matchPiValid1 = PiCriterion.builder().matchValid(piIpv4HeaderFieldId, false).build();
164 Criterion sameAsMatchPiValid1 = PiCriterion.builder().matchValid(piIpv4HeaderFieldId, false).build();
165 Criterion matchPiValid2 = PiCriterion.builder().matchValid(piIpv4HeaderFieldId, true).build();
166
167 byte[] matchRangeBytes1 = {0x10};
168 byte[] matchRangeBytes2 = {0x20};
169 byte[] matchRangeHighBytes = {0x30};
170 Criterion matchPiRangeByte1 = PiCriterion.builder().matchRange(piIpv4HeaderFieldId,
171 matchRangeBytes1,
172 matchRangeHighBytes)
173 .build();
174 Criterion sameAsMatchPiRangeByte1 = PiCriterion.builder().matchRange(piIpv4HeaderFieldId,
175 matchRangeBytes1,
176 matchRangeHighBytes)
177 .build();
178 Criterion matchPiRangeByte2 = PiCriterion.builder().matchRange(piIpv4HeaderFieldId,
179 matchRangeBytes2,
180 matchRangeHighBytes)
181 .build();
182
183 short matchRangeShort1 = 0x100;
184 short matchRangeShort2 = 0x200;
185 short matchRangeHighShort = 0x300;
186 Criterion matchPiRangeShort1 = PiCriterion.builder().matchRange(piIpv4HeaderFieldId,
187 matchRangeShort1,
188 matchRangeHighShort)
189 .build();
190 Criterion sameAsMatchPiRangeShort1 = PiCriterion.builder().matchRange(piIpv4HeaderFieldId,
191 matchRangeShort1,
192 matchRangeHighShort)
193 .build();
194 Criterion matchPiRangeShort2 = PiCriterion.builder().matchRange(piIpv4HeaderFieldId,
195 matchRangeShort2,
196 matchRangeHighShort)
197 .build();
198
199 int matchRangeInt1 = 0x100;
200 int matchRangeInt2 = 0x200;
201 int matchRangeHighInt = 0x300;
202 Criterion matchPiRangeInt1 = PiCriterion.builder().matchRange(piIpv4HeaderFieldId,
203 matchRangeInt1,
204 matchRangeHighInt)
205 .build();
206 Criterion sameAsMatchPiRangeInt1 = PiCriterion.builder().matchRange(piIpv4HeaderFieldId,
207 matchRangeInt1,
208 matchRangeHighInt)
209 .build();
210 Criterion matchPiRangeInt2 = PiCriterion.builder().matchRange(piIpv4HeaderFieldId,
211 matchRangeInt2,
212 matchRangeHighInt)
213 .build();
214
215 long matchRangeLong1 = 0x100;
216 long matchRangeLong2 = 0x200;
217 long matchRangeHighLong = 0x300;
218 Criterion matchPiRangeLong1 = PiCriterion.builder().matchRange(piIpv4HeaderFieldId,
219 matchRangeLong1,
220 matchRangeHighLong)
221 .build();
222 Criterion sameAsMatchPiRangeLong1 = PiCriterion.builder().matchRange(piIpv4HeaderFieldId,
223 matchRangeLong1,
224 matchRangeHighLong)
225 .build();
226 Criterion matchPiRangeLong2 = PiCriterion.builder().matchRange(piIpv4HeaderFieldId,
227 matchRangeLong2,
228 matchRangeHighLong)
229 .build();
230
231 /**
232 * Checks that a Criterion object has the proper type, and then converts
233 * it to the proper type.
234 *
235 * @param criterion Criterion object to convert
236 * @param type Enumerated type value for the Criterion class
237 * @param clazz Desired Criterion class
238 * @param <T> The type the caller wants returned
239 * @return converted object
240 */
241 @SuppressWarnings("unchecked")
242 private <T> T checkAndConvert(Criterion criterion, Criterion.Type type, Class clazz) {
243 assertThat(criterion, is(notNullValue()));
244 assertThat(criterion.type(), is(equalTo(type)));
245 assertThat(criterion, instanceOf(clazz));
246 return (T) criterion;
247 }
248
249 /**
250 * Test the ExactMatchPi method.
251 */
252 @Test
253 public void testExactMatchPiMethod() {
254
255 Map<PiHeaderFieldId, PiFieldMatch> fieldMatchesExactBytes = Maps.newHashMap();
256 Map<PiHeaderFieldId, PiFieldMatch> fieldMatchesExactShort = Maps.newHashMap();
257 Map<PiHeaderFieldId, PiFieldMatch> fieldMatchesExactInt = Maps.newHashMap();
258 Map<PiHeaderFieldId, PiFieldMatch> fieldMatchesExactLong = Maps.newHashMap();
259
260 Criterion matchPiBytes = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactBytes1).build();
261 PiCriterion piCriterionBytes =
262 checkAndConvert(matchPiBytes, Criterion.Type.PROTOCOL_INDEPENDENT, PiCriterion.class);
263 fieldMatchesExactBytes.put(piEthHeaderFieldId, new PiExactFieldMatch(piEthHeaderFieldId,
264 copyFrom(matchExactBytes1)));
265 assertThat("Incorrect match param value",
266 CollectionUtils.isEqualCollection(piCriterionBytes.fieldMatches(), fieldMatchesExactBytes.values()));
267 assertThat(piCriterionBytes.type(), is(equalTo(Criterion.Type.PROTOCOL_INDEPENDENT)));
268
269
270 Criterion matchPiShort = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactShort1).build();
271 PiCriterion piCriterionShort =
272 checkAndConvert(matchPiShort, Criterion.Type.PROTOCOL_INDEPENDENT, PiCriterion.class);
273 fieldMatchesExactShort.put(piEthHeaderFieldId, new PiExactFieldMatch(piEthHeaderFieldId,
274 copyFrom(matchExactShort1)));
275 assertThat("Incorrect match param value",
276 CollectionUtils.isEqualCollection(piCriterionShort.fieldMatches(), fieldMatchesExactShort.values()));
277 assertThat(piCriterionShort.type(), is(equalTo(Criterion.Type.PROTOCOL_INDEPENDENT)));
278
279
280 Criterion matchPiInt = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactInt1).build();
281 PiCriterion piCriterionInt =
282 checkAndConvert(matchPiInt, Criterion.Type.PROTOCOL_INDEPENDENT, PiCriterion.class);
283 fieldMatchesExactInt.put(piEthHeaderFieldId, new PiExactFieldMatch(piEthHeaderFieldId,
284 copyFrom(matchExactInt1)));
285 assertThat("Incorrect match param value",
286 CollectionUtils.isEqualCollection(piCriterionInt.fieldMatches(), fieldMatchesExactInt.values()));
287 assertThat(piCriterionInt.type(), is(equalTo(Criterion.Type.PROTOCOL_INDEPENDENT)));
288
289
290 Criterion matchPiLong = PiCriterion.builder().matchExact(piEthHeaderFieldId, matchExactLong1).build();
291 PiCriterion piCriterionLong =
292 checkAndConvert(matchPiLong, Criterion.Type.PROTOCOL_INDEPENDENT, PiCriterion.class);
293 fieldMatchesExactLong.put(piEthHeaderFieldId, new PiExactFieldMatch(piEthHeaderFieldId,
294 copyFrom(matchExactLong1)));
295 assertThat("Incorrect match param value",
296 CollectionUtils.isEqualCollection(piCriterionLong.fieldMatches(), fieldMatchesExactLong.values()));
297 assertThat(piCriterionLong.type(), is(equalTo(Criterion.Type.PROTOCOL_INDEPENDENT)));
298 }
299
300 /**
301 * Test the LpmMatchPi method.
302 */
303 @Test
304 public void testLpmMatchPiMethod() {
305
306 Map<PiHeaderFieldId, PiFieldMatch> fieldMatchesLpmBytes = Maps.newHashMap();
307 Map<PiHeaderFieldId, PiFieldMatch> fieldMatchesLpmShort = Maps.newHashMap();
308 Map<PiHeaderFieldId, PiFieldMatch> fieldMatchesLpmInt = Maps.newHashMap();
309 Map<PiHeaderFieldId, PiFieldMatch> fieldMatchesLpmLong = Maps.newHashMap();
310
311 Criterion matchPiBytes = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId, matchLpmBytes1, mask).build();
312 PiCriterion piCriterionBytes =
313 checkAndConvert(matchPiBytes, Criterion.Type.PROTOCOL_INDEPENDENT, PiCriterion.class);
314 fieldMatchesLpmBytes.put(piIpv4HeaderFieldId, new PiLpmFieldMatch(piIpv4HeaderFieldId,
315 copyFrom(matchLpmBytes1), mask));
316 assertThat("Incorrect match param value",
317 CollectionUtils.isEqualCollection(piCriterionBytes.fieldMatches(), fieldMatchesLpmBytes.values()));
318 assertThat(piCriterionBytes.type(), is(equalTo(Criterion.Type.PROTOCOL_INDEPENDENT)));
319
320
321 Criterion matchPiShort = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId, matchLpmShort1, mask).build();
322 PiCriterion piCriterionShort =
323 checkAndConvert(matchPiShort, Criterion.Type.PROTOCOL_INDEPENDENT, PiCriterion.class);
324 fieldMatchesLpmShort.put(piIpv4HeaderFieldId, new PiLpmFieldMatch(piIpv4HeaderFieldId,
325 copyFrom(matchLpmShort1), mask));
326 assertThat("Incorrect match param value",
327 CollectionUtils.isEqualCollection(piCriterionShort.fieldMatches(), fieldMatchesLpmShort.values()));
328 assertThat(piCriterionShort.type(), is(equalTo(Criterion.Type.PROTOCOL_INDEPENDENT)));
329
330
331 Criterion matchPiInt = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId, matchLpmInt1, mask).build();
332 PiCriterion piCriterionInt =
333 checkAndConvert(matchPiInt, Criterion.Type.PROTOCOL_INDEPENDENT, PiCriterion.class);
334 fieldMatchesLpmInt.put(piIpv4HeaderFieldId, new PiLpmFieldMatch(piIpv4HeaderFieldId,
335 copyFrom(matchLpmInt1), mask));
336 assertThat("Incorrect match param value",
337 CollectionUtils.isEqualCollection(piCriterionInt.fieldMatches(), fieldMatchesLpmInt.values()));
338 assertThat(piCriterionInt.type(), is(equalTo(Criterion.Type.PROTOCOL_INDEPENDENT)));
339
340
341 Criterion matchPiLong = PiCriterion.builder().matchLpm(piIpv4HeaderFieldId, matchLpmLong1, mask).build();
342 PiCriterion piCriterionLong =
343 checkAndConvert(matchPiLong, Criterion.Type.PROTOCOL_INDEPENDENT, PiCriterion.class);
344 fieldMatchesLpmLong.put(piIpv4HeaderFieldId, new PiLpmFieldMatch(piIpv4HeaderFieldId,
345 copyFrom(matchLpmLong1), mask));
346 assertThat("Incorrect match param value",
347 CollectionUtils.isEqualCollection(piCriterionLong.fieldMatches(), fieldMatchesLpmLong.values()));
348 assertThat(piCriterionLong.type(), is(equalTo(Criterion.Type.PROTOCOL_INDEPENDENT)));
349 }
350
351 /**
352 * Test the TernaryMatchPi method.
353 */
354 @Test
355 public void testTernaryMatchPiMethod() {
356 Map<PiHeaderFieldId, PiFieldMatch> fieldMatchesTernaryBytes = Maps.newHashMap();
357 Map<PiHeaderFieldId, PiFieldMatch> fieldMatchesTernaryShort = Maps.newHashMap();
358 Map<PiHeaderFieldId, PiFieldMatch> fieldMatchesTernaryInt = Maps.newHashMap();
359 Map<PiHeaderFieldId, PiFieldMatch> fieldMatchesTernaryLong = Maps.newHashMap();
360
361 Criterion matchPiBytes = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId, matchTernaryBytes1,
362 matchTernaryMaskBytes)
363 .build();
364 PiCriterion piCriterionBytes =
365 checkAndConvert(matchPiBytes, Criterion.Type.PROTOCOL_INDEPENDENT, PiCriterion.class);
366 fieldMatchesTernaryBytes.put(piIpv4HeaderFieldId, new PiTernaryFieldMatch(piIpv4HeaderFieldId,
367 copyFrom(matchTernaryBytes1),
368 copyFrom(matchTernaryMaskBytes)));
369 assertThat("Incorrect match param value",
370 CollectionUtils.isEqualCollection(piCriterionBytes.fieldMatches(), fieldMatchesTernaryBytes.values()));
371 assertThat(piCriterionBytes.type(), is(equalTo(Criterion.Type.PROTOCOL_INDEPENDENT)));
372
373 Criterion matchPiShort = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId, matchTernaryShort1,
374 matchTernaryMaskShort)
375 .build();
376 PiCriterion piCriterionShort =
377 checkAndConvert(matchPiShort, Criterion.Type.PROTOCOL_INDEPENDENT, PiCriterion.class);
378 fieldMatchesTernaryShort.put(piIpv4HeaderFieldId, new PiTernaryFieldMatch(piIpv4HeaderFieldId,
379 copyFrom(matchTernaryShort1),
380 copyFrom(matchTernaryMaskShort)));
381 assertThat("Incorrect match param value",
382 CollectionUtils.isEqualCollection(piCriterionShort.fieldMatches(), fieldMatchesTernaryShort.values()));
383 assertThat(piCriterionShort.type(), is(equalTo(Criterion.Type.PROTOCOL_INDEPENDENT)));
384
385 Criterion matchPiInt = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId, matchTernaryInt1,
386 matchTernaryMaskInt)
387 .build();
388 PiCriterion piCriterionInt =
389 checkAndConvert(matchPiInt, Criterion.Type.PROTOCOL_INDEPENDENT, PiCriterion.class);
390 fieldMatchesTernaryInt.put(piIpv4HeaderFieldId, new PiTernaryFieldMatch(piIpv4HeaderFieldId,
391 copyFrom(matchTernaryInt1),
392 copyFrom(matchTernaryMaskInt)));
393 assertThat("Incorrect match param value",
394 CollectionUtils.isEqualCollection(piCriterionInt.fieldMatches(), fieldMatchesTernaryInt.values()));
395 assertThat(piCriterionInt.type(), is(equalTo(Criterion.Type.PROTOCOL_INDEPENDENT)));
396
397 Criterion matchPiLong = PiCriterion.builder().matchTernary(piIpv4HeaderFieldId, matchTernaryLong1,
398 matchTernaryMaskLong)
399 .build();
400 PiCriterion piCriterionLong =
401 checkAndConvert(matchPiLong, Criterion.Type.PROTOCOL_INDEPENDENT, PiCriterion.class);
402 fieldMatchesTernaryLong.put(piIpv4HeaderFieldId, new PiTernaryFieldMatch(piIpv4HeaderFieldId,
403 copyFrom(matchTernaryLong1),
404 copyFrom(matchTernaryMaskLong)));
405 assertThat("Incorrect match param value",
406 CollectionUtils.isEqualCollection(piCriterionLong.fieldMatches(), fieldMatchesTernaryLong.values()));
407 assertThat(piCriterionLong.type(), is(equalTo(Criterion.Type.PROTOCOL_INDEPENDENT)));
408 }
409
410 /**
411 * Test the ValidMatchPi method.
412 */
413 @Test
414 public void testValidMatchPiMethod() {
415 Map<PiHeaderFieldId, PiFieldMatch> fieldMatchesValid = Maps.newHashMap();
416 Criterion matchPiBytes = PiCriterion.builder().matchValid(piIpv4HeaderFieldId, true).build();
417 PiCriterion piCriterionBytes = checkAndConvert(matchPiBytes, Criterion.Type.PROTOCOL_INDEPENDENT,
418 PiCriterion.class);
419 fieldMatchesValid.put(piIpv4HeaderFieldId, new PiValidFieldMatch(piIpv4HeaderFieldId, true));
420
421 assertThat("Incorrect match param value",
422 CollectionUtils.isEqualCollection(piCriterionBytes.fieldMatches(), fieldMatchesValid.values()));
423 assertThat(piCriterionBytes.type(), is(equalTo(Criterion.Type.PROTOCOL_INDEPENDENT)));
424 }
425
426 /**
427 * Test the RangeMatchPi method.
428 */
429 @Test
430 public void testRangeMatchPiMethod() {
431 Map<PiHeaderFieldId, PiFieldMatch> fieldMatchesRangeBytes = Maps.newHashMap();
432 Map<PiHeaderFieldId, PiFieldMatch> fieldMatchesRangeShort = Maps.newHashMap();
433 Map<PiHeaderFieldId, PiFieldMatch> fieldMatchesRangeInt = Maps.newHashMap();
434 Map<PiHeaderFieldId, PiFieldMatch> fieldMatchesRangeLong = Maps.newHashMap();
435
436 Criterion matchPiBytes = PiCriterion.builder().matchRange(piIpv4HeaderFieldId, matchRangeBytes1,
437 matchRangeHighBytes)
438 .build();
439 PiCriterion piCriterionBytes =
440 checkAndConvert(matchPiBytes, Criterion.Type.PROTOCOL_INDEPENDENT, PiCriterion.class);
441 fieldMatchesRangeBytes.put(piIpv4HeaderFieldId, new PiRangeFieldMatch(piIpv4HeaderFieldId,
442 copyFrom(matchRangeBytes1),
443 copyFrom(matchRangeHighBytes)));
444 assertThat("Incorrect match param value",
445 CollectionUtils.isEqualCollection(piCriterionBytes.fieldMatches(), fieldMatchesRangeBytes.values()));
446 assertThat(piCriterionBytes.type(), is(equalTo(Criterion.Type.PROTOCOL_INDEPENDENT)));
447
448 Criterion matchPiShort = PiCriterion.builder().matchRange(piIpv4HeaderFieldId, matchRangeShort1,
449 matchRangeHighShort)
450 .build();
451 PiCriterion piCriterionShort =
452 checkAndConvert(matchPiShort, Criterion.Type.PROTOCOL_INDEPENDENT, PiCriterion.class);
453 fieldMatchesRangeShort.put(piIpv4HeaderFieldId, new PiRangeFieldMatch(piIpv4HeaderFieldId,
454 copyFrom(matchRangeShort1),
455 copyFrom(matchRangeHighShort)));
456 assertThat("Incorrect match param value",
457 CollectionUtils.isEqualCollection(piCriterionShort.fieldMatches(), fieldMatchesRangeShort.values()));
458 assertThat(piCriterionShort.type(), is(equalTo(Criterion.Type.PROTOCOL_INDEPENDENT)));
459
460 Criterion matchPiInt = PiCriterion.builder().matchRange(piIpv4HeaderFieldId, matchRangeInt1,
461 matchRangeHighInt)
462 .build();
463 PiCriterion piCriterionInt =
464 checkAndConvert(matchPiInt, Criterion.Type.PROTOCOL_INDEPENDENT, PiCriterion.class);
465 fieldMatchesRangeInt.put(piIpv4HeaderFieldId, new PiRangeFieldMatch(piIpv4HeaderFieldId,
466 copyFrom(matchRangeInt1),
467 copyFrom(matchRangeHighInt)));
468 assertThat("Incorrect match param value",
469 CollectionUtils.isEqualCollection(piCriterionInt.fieldMatches(), fieldMatchesRangeInt.values()));
470 assertThat(piCriterionInt.type(), is(equalTo(Criterion.Type.PROTOCOL_INDEPENDENT)));
471
472 Criterion matchPiLong = PiCriterion.builder().matchRange(piIpv4HeaderFieldId, matchRangeLong1,
473 matchRangeHighLong)
474 .build();
475 PiCriterion piCriterionLong =
476 checkAndConvert(matchPiLong, Criterion.Type.PROTOCOL_INDEPENDENT, PiCriterion.class);
477 fieldMatchesRangeLong.put(piIpv4HeaderFieldId, new PiRangeFieldMatch(piIpv4HeaderFieldId,
478 copyFrom(matchRangeLong1),
479 copyFrom(matchRangeHighLong)));
480 assertThat("Incorrect match param value",
481 CollectionUtils.isEqualCollection(piCriterionLong.fieldMatches(), fieldMatchesRangeLong.values()));
482 assertThat(piCriterionLong.type(), is(equalTo(Criterion.Type.PROTOCOL_INDEPENDENT)));
483 }
484
485 /**
486 * Test the equals() method of the PiCriterion class.
487 */
488 @Test
489 public void testPiExactCriterionEquals() {
490 new EqualsTester()
491 .addEqualityGroup(matchPiExactByte1)
492 .addEqualityGroup(matchPiExactByte2)
493 .testEquals();
494
495 new EqualsTester()
496 .addEqualityGroup(matchPiExactShort1)
497 .addEqualityGroup(matchPiExactShort2)
498 .testEquals();
499
500 new EqualsTester()
501 .addEqualityGroup(matchPiExactInt1)
502 .addEqualityGroup(matchPiExactInt2)
503 .testEquals();
504
505 new EqualsTester()
506 .addEqualityGroup(matchPiExactLong1)
507 .addEqualityGroup(matchPiExactLong2)
508 .testEquals();
509 }
510
511 /**
512 * Test the equals() method of the PiCriterion class.
513 */
514 @Test
515 public void testPiLpmCriterionEquals() {
516 new EqualsTester()
517 .addEqualityGroup(matchPiLpmByte1)
518 .addEqualityGroup(matchPiLpmByte2)
519 .testEquals();
520
521 new EqualsTester()
522 .addEqualityGroup(matchPiLpmShort1)
523 .addEqualityGroup(matchPiLpmShort2)
524 .testEquals();
525
526 new EqualsTester()
527 .addEqualityGroup(matchPiLpmInt1)
528 .addEqualityGroup(matchPiLpmInt2)
529 .testEquals();
530
531 new EqualsTester()
532 .addEqualityGroup(matchPiLpmLong1)
533 .addEqualityGroup(matchPiLpmLong2)
534 .testEquals();
535 }
536
537 /**
538 * Test the equals() method of the PiCriterion class.
539 */
540 @Test
541 public void testPiTernaryCriterionEquals() {
542 new EqualsTester()
543 .addEqualityGroup(matchPiTernaryByte1)
544 .addEqualityGroup(matchPiTernaryByte2)
545 .testEquals();
546
547 new EqualsTester()
548 .addEqualityGroup(matchPiTernaryShort1)
549 .addEqualityGroup(matchPiTernaryShort2)
550 .testEquals();
551
552 new EqualsTester()
553 .addEqualityGroup(matchPiTernaryInt1)
554 .addEqualityGroup(matchPiTernaryInt2)
555 .testEquals();
556
557 new EqualsTester()
558 .addEqualityGroup(matchPiTernaryLong1)
559 .addEqualityGroup(matchPiTernaryLong2)
560 .testEquals();
561 }
562
563 /**
564 * Test the equals() method of the PiCriterion class.
565 */
566 @Test
567 public void testPiValidCriterionEquals() {
568 new EqualsTester()
569 .addEqualityGroup(matchPiValid1)
570 .addEqualityGroup(matchPiValid2)
571 .testEquals();
572 }
573
574 /**
575 * Test the equals() method of the PiCriterion class.
576 */
577 @Test
578 public void testPiRangeCriterionEquals() {
579 new EqualsTester()
580 .addEqualityGroup(matchPiRangeByte1)
581 .addEqualityGroup(matchPiRangeByte2)
582 .testEquals();
583
584 new EqualsTester()
585 .addEqualityGroup(matchPiRangeShort1)
586 .addEqualityGroup(matchPiRangeShort2)
587 .testEquals();
588
589 new EqualsTester()
590 .addEqualityGroup(matchPiRangeInt1)
591 .addEqualityGroup(matchPiRangeInt2)
592 .testEquals();
593
594 new EqualsTester()
595 .addEqualityGroup(matchPiRangeLong1)
596 .addEqualityGroup(matchPiRangeLong2)
597 .testEquals();
598 }
599}