blob: 6b113a2a4f96844d800a899b5159e010d637b522 [file] [log] [blame]
Ray Milkey33d90232014-11-04 10:49:00 -08001/*
2 * Copyright 2014 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 */
16package org.onlab.onos.net.flow.criteria;
17
18import org.junit.Test;
19import org.onlab.onos.net.PortNumber;
20import org.onlab.packet.IpPrefix;
21import org.onlab.packet.MacAddress;
22import org.onlab.packet.VlanId;
23
24import static org.hamcrest.MatcherAssert.assertThat;
25import static org.hamcrest.Matchers.containsString;
26import static org.hamcrest.Matchers.equalTo;
27import static org.hamcrest.Matchers.instanceOf;
28import static org.hamcrest.Matchers.is;
29import static org.hamcrest.Matchers.not;
30import static org.hamcrest.Matchers.notNullValue;
31import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
32import static org.onlab.junit.UtilityClassChecker.assertThatClassIsUtility;
33import static org.onlab.onos.net.PortNumber.portNumber;
34
35/**
36 * Unit tests for the Criteria class and its subclasses.
37 */
38public class CriteriaTest {
39
40 final PortNumber port1 = portNumber(1);
41 final PortNumber port2 = portNumber(2);
42
43 Criterion matchInPort1 = Criteria.matchInPort(port1);
44 Criterion sameAsMatchInPort1 = Criteria.matchInPort(port1);
45 Criterion matchInPort2 = Criteria.matchInPort(port2);
46
47 Criterion matchTcpPort1 = Criteria.matchTcpSrc((short) 1);
48 Criterion sameAsMatchTcpPort1 = Criteria.matchTcpSrc((short) 1);
49 Criterion matchTcpPort2 = Criteria.matchTcpDst((short) 2);
50
51 private static final String MAC1 = "00:00:00:00:00:01";
52 private static final String MAC2 = "00:00:00:00:00:02";
53 private MacAddress mac1 = new MacAddress(MAC1.getBytes());
54 private MacAddress mac2 = new MacAddress(MAC2.getBytes());
55 Criterion matchEth1 = Criteria.matchEthSrc(mac1);
56 Criterion sameAsMatchEth1 = Criteria.matchEthSrc(mac1);
57 Criterion matchEth2 = Criteria.matchEthDst(mac2);
58
59 short ethType1 = 1;
60 short ethType2 = 2;
61 Criterion matchEthType1 = Criteria.matchEthType(ethType1);
62 Criterion sameAsMatchEthType1 = Criteria.matchEthType(ethType1);
63 Criterion matchEthType2 = Criteria.matchEthType(ethType2);
64
65 short vlan1 = 1;
66 short vlan2 = 2;
67 VlanId vlanId1 = VlanId.vlanId(vlan1);
68 VlanId vlanId2 = VlanId.vlanId(vlan2);
69 Criterion matchVlanId1 = Criteria.matchVlanId(vlanId1);
70 Criterion sameAsMatchVlanId1 = Criteria.matchVlanId(vlanId1);
71 Criterion matchVlanId2 = Criteria.matchVlanId(vlanId2);
72
73 byte vlanPcp1 = 1;
74 byte vlanPcp2 = 2;
75 Criterion matchVlanPcp1 = Criteria.matchVlanPcp(vlanPcp1);
76 Criterion sameAsMatchVlanPcp1 = Criteria.matchVlanPcp(vlanPcp1);
77 Criterion matchVlanPcp2 = Criteria.matchVlanPcp(vlanPcp2);
78
79 byte protocol1 = 1;
80 byte protocol2 = 2;
81 Criterion matchIpProtocol1 = Criteria.matchIPProtocol(protocol1);
82 Criterion sameAsMatchIpProtocol1 = Criteria.matchIPProtocol(protocol1);
83 Criterion matchIpProtocol2 = Criteria.matchIPProtocol(protocol2);
84
85 private static final String IP1 = "1.2.3.4/24";
86 private static final String IP2 = "5.6.7.8/24";
87 private IpPrefix ip1 = IpPrefix.valueOf(IP1);
88 private IpPrefix ip2 = IpPrefix.valueOf(IP2);
89 Criterion matchIp1 = Criteria.matchIPSrc(ip1);
90 Criterion sameAsMatchIp1 = Criteria.matchIPSrc(ip1);
91 Criterion matchIp2 = Criteria.matchIPSrc(ip2);
92
93 short lambda1 = 1;
94 short lambda2 = 2;
95 Criterion matchLambda1 = Criteria.matchLambda(lambda1);
96 Criterion sameAsMatchLambda1 = Criteria.matchLambda(lambda1);
97 Criterion matchLambda2 = Criteria.matchLambda(lambda2);
98
Ray Milkey9d078652014-11-05 10:35:54 -080099 short signalLambda1 = 1;
100 short signalLambda2 = 2;
Ray Milkey33d90232014-11-04 10:49:00 -0800101 Criterion matchSignalLambda1 = Criteria.matchOpticalSignalType(signalLambda1);
102 Criterion sameAsMatchSignalLambda1 = Criteria.matchOpticalSignalType(signalLambda1);
103 Criterion matchSignalLambda2 = Criteria.matchOpticalSignalType(signalLambda2);
104
105 /**
106 * Checks that a Criterion object has the proper type, and then converts
107 * it to the proper type.
108 *
109 * @param criterion Criterion object to convert
110 * @param type Enumerated type value for the Criterion class
111 * @param clazz Desired Criterion class
112 * @param <T> The type the caller wants returned
113 * @return converted object
114 */
115 @SuppressWarnings("unchecked")
116 private <T> T checkAndConvert(Criterion criterion, Criterion.Type type, Class clazz) {
117 assertThat(criterion, is(notNullValue()));
118 assertThat(criterion.type(), is(equalTo(type)));
Ray Milkey78081052014-11-05 10:38:12 -0800119 assertThat(criterion, instanceOf(clazz));
Ray Milkey33d90232014-11-04 10:49:00 -0800120 return (T) criterion;
121 }
122
123 /**
124 * Checks the equals() and toString() methods of a Criterion class.
125 *
126 * @param c1 first object to compare
127 * @param c1match object that should be equal to the first
128 * @param c2 object that should be not equal to the first
129 * @param clazz Class object for the Criterion subclass
130 * @param <T> type of the arguments
131 */
132 private <T extends Criterion> void checkEqualsAndToString(T c1, T c1match,
133 T c2, Class clazz) {
Ray Milkey78081052014-11-05 10:38:12 -0800134 assertThat(c1, instanceOf(clazz));
135 assertThat(c1match, instanceOf(clazz));
136 assertThat(c2, instanceOf(clazz));
Ray Milkey33d90232014-11-04 10:49:00 -0800137
138 assertThat(c1, is(equalTo(c1match)));
139 assertThat(c1, is(not(equalTo(c2))));
140 assertThat(c1, is(not(equalTo(new Object()))));
141
Ray Milkey78081052014-11-05 10:38:12 -0800142 // Make sure the enumerated type appears in the toString() output and
143 // the toString output is unique.
Ray Milkey33d90232014-11-04 10:49:00 -0800144 assertThat(c1.toString(), containsString(c1.type().toString()));
Ray Milkey78081052014-11-05 10:38:12 -0800145 assertThat(c1.toString(), equalTo(c1match.toString()));
146 assertThat(c1.toString(), not(equalTo(c2.toString())));
Ray Milkey33d90232014-11-04 10:49:00 -0800147 }
148
149
150 /**
151 * Check that the Criteria class is a valid utility class.
152 */
153 @Test
154 public void testCriteriaUtility() {
155 assertThatClassIsUtility(Criteria.class);
156 }
157
158 /**
159 * Check that the Criteria implementations are immutable.
160 */
161 @Test
162 public void testCriteriaImmutability() {
163 assertThatClassIsImmutable(Criteria.PortCriterion.class);
164 assertThatClassIsImmutable(Criteria.EthCriterion.class);
165 assertThatClassIsImmutable(Criteria.EthTypeCriterion.class);
166 assertThatClassIsImmutable(Criteria.IPCriterion.class);
167 assertThatClassIsImmutable(Criteria.IPProtocolCriterion.class);
168 assertThatClassIsImmutable(Criteria.VlanPcpCriterion.class);
169 assertThatClassIsImmutable(Criteria.VlanIdCriterion.class);
170 assertThatClassIsImmutable(Criteria.TcpPortCriterion.class);
171 assertThatClassIsImmutable(Criteria.LambdaCriterion.class);
172 assertThatClassIsImmutable(Criteria.OpticalSignalTypeCriterion.class);
173 }
174
175 // PortCriterion class
176
177 /**
178 * Test the matchInPort method.
179 */
180 @Test
181 public void testMatchInPortMethod() {
182 PortNumber p1 = portNumber(1);
183 Criterion matchInPort = Criteria.matchInPort(p1);
184 Criteria.PortCriterion portCriterion =
185 checkAndConvert(matchInPort,
186 Criterion.Type.IN_PORT,
187 Criteria.PortCriterion.class);
188 assertThat(portCriterion.port(), is(equalTo(p1)));
189 }
190
191 /**
192 * Test the equals() method of the PortCriterion class.
193 */
194 @Test
195 public void testPortCriterionEquals() {
196 checkEqualsAndToString(matchInPort1, sameAsMatchInPort1, matchInPort2,
197 Criteria.PortCriterion.class);
198 }
199
200 /**
201 * Test the hashCode() method of the PortCriterion class.
202 */
203 @Test
204 public void testPortCriterionHashCode() {
205 assertThat(matchInPort1.hashCode(), is(equalTo(sameAsMatchInPort1.hashCode())));
206 assertThat(matchInPort1.hashCode(), is(not(equalTo(matchInPort2.hashCode()))));
207 }
208
209 // EthCriterion class
210
211 /**
212 * Test the matchEthSrc method.
213 */
214 @Test
215 public void testMatchEthSrcMethod() {
216 Criterion matchEthSrc = Criteria.matchEthSrc(mac1);
217 Criteria.EthCriterion ethCriterion =
218 checkAndConvert(matchEthSrc,
219 Criterion.Type.ETH_SRC,
220 Criteria.EthCriterion.class);
221 assertThat(ethCriterion.mac(), is(mac1));
222 }
223
224 /**
225 * Test the matchEthDst method.
226 */
227 @Test
228 public void testMatchEthDstMethod() {
229 Criterion matchTcpDst = Criteria.matchEthDst(mac1);
230 Criteria.EthCriterion ethCriterion =
231 checkAndConvert(matchTcpDst,
232 Criterion.Type.ETH_DST,
233 Criteria.EthCriterion.class);
234 assertThat(ethCriterion.mac(), is(equalTo(mac1)));
235 }
236
237 /**
238 * Test the equals() method of the EthCriterion class.
239 */
240 @Test
241 public void testEthCriterionEquals() {
242 checkEqualsAndToString(matchEth1, sameAsMatchEth1, matchEth2,
243 Criteria.EthCriterion.class);
244 }
245
246 /**
247 * Test the hashCode() method of the EthCriterion class.
248 */
249 @Test
250 public void testEthCriterionHashCode() {
251 assertThat(matchEth1.hashCode(), is(equalTo(sameAsMatchEth1.hashCode())));
252 assertThat(matchEth1.hashCode(), is(not(equalTo(matchEth2.hashCode()))));
253 }
254
255
256 // TcpPortCriterion class
257
258 /**
259 * Test the matchTcpSrc method.
260 */
261 @Test
262 public void testMatchTcpSrcMethod() {
263 Criterion matchTcpSrc = Criteria.matchTcpSrc((short) 1);
264 Criteria.TcpPortCriterion tcpPortCriterion =
265 checkAndConvert(matchTcpSrc,
266 Criterion.Type.TCP_SRC,
267 Criteria.TcpPortCriterion.class);
268 assertThat(tcpPortCriterion.tcpPort(), is(equalTo((short) 1)));
269 }
270
271 /**
272 * Test the matchTcpDst method.
273 */
274 @Test
275 public void testMatchTcpDstMethod() {
276 Criterion matchTcpDst = Criteria.matchTcpDst((short) 1);
277 Criteria.TcpPortCriterion tcpPortCriterion =
278 checkAndConvert(matchTcpDst,
279 Criterion.Type.TCP_DST,
280 Criteria.TcpPortCriterion.class);
281 assertThat(tcpPortCriterion.tcpPort(), is(equalTo((short) 1)));
282 }
283
284 /**
285 * Test the equals() method of the TcpPortCriterion class.
286 */
287 @Test
288 public void testTcpPortCriterionEquals() {
289 checkEqualsAndToString(matchTcpPort1, sameAsMatchTcpPort1, matchTcpPort2,
290 Criteria.TcpPortCriterion.class);
291 }
292
293 /**
294 * Test the hashCode() method of the TcpPortCriterion class.
295 */
296 @Test
297 public void testTcpPortCriterionHashCode() {
298 assertThat(matchTcpPort1.hashCode(), is(equalTo(sameAsMatchTcpPort1.hashCode())));
299 assertThat(matchTcpPort1.hashCode(), is(not(equalTo(matchTcpPort2.hashCode()))));
300 }
301
302
303 // EthTypeCriterion class
304
305 /**
306 * Test the matchEthType method.
307 */
308 @Test
309 public void testMatchEthTypeMethod() {
310 Short ethType = 12;
311 Criterion matchEthType = Criteria.matchEthType(ethType);
312 Criteria.EthTypeCriterion ethTypeCriterion =
313 checkAndConvert(matchEthType,
314 Criterion.Type.ETH_TYPE,
315 Criteria.EthTypeCriterion.class);
316 assertThat(ethTypeCriterion.ethType(), is(equalTo(ethType)));
317 }
318
319 /**
320 * Test the equals() method of the EthTypeCriterion class.
321 */
322 @Test
323 public void testEthTypeCriterionEquals() {
324 checkEqualsAndToString(matchEthType1, sameAsMatchEthType1, matchEthType2,
325 Criteria.EthTypeCriterion.class);
326 }
327
328 /**
329 * Test the hashCode() method of the EthTypeCriterion class.
330 */
331 @Test
332 public void testEthTypeCriterionHashCode() {
333 assertThat(matchInPort1.hashCode(), is(equalTo(sameAsMatchInPort1.hashCode())));
334 assertThat(matchInPort1.hashCode(), is(not(equalTo(matchInPort2.hashCode()))));
335 }
336
337
338 // VlanIdCriterion class
339
340 /**
341 * Test the matchVlanId method.
342 */
343 @Test
344 public void testMatchVlanIdMethod() {
345 Criterion matchVlanId = Criteria.matchVlanId(vlanId1);
346 Criteria.VlanIdCriterion vlanIdCriterion =
347 checkAndConvert(matchVlanId,
348 Criterion.Type.VLAN_VID,
349 Criteria.VlanIdCriterion.class);
350 assertThat(vlanIdCriterion.vlanId(), is(equalTo(vlanId1)));
351 }
352
353 /**
354 * Test the equals() method of the VlanIdCriterion class.
355 */
356 @Test
357 public void testVlanIdCriterionEquals() {
358 checkEqualsAndToString(matchVlanId1, sameAsMatchVlanId1, matchVlanId2,
359 Criteria.VlanIdCriterion.class);
360 }
361
362 /**
363 * Test the hashCode() method of the VlanIdCriterion class.
364 */
365 @Test
366 public void testVlanIdCriterionHashCode() {
367 assertThat(matchVlanId1.hashCode(), is(equalTo(sameAsMatchVlanId1.hashCode())));
368 assertThat(matchVlanId1.hashCode(), is(not(equalTo(matchVlanId2.hashCode()))));
369 }
370
371 // VlanPcpCriterion class
372
373 /**
374 * Test the matchVlanPcp method.
375 */
376 @Test
377 public void testMatchVlanPcpMethod() {
378 Criterion matchVlanPcp = Criteria.matchVlanPcp(vlanPcp1);
379 Criteria.VlanPcpCriterion vlanPcpCriterion =
380 checkAndConvert(matchVlanPcp,
381 Criterion.Type.VLAN_PCP,
382 Criteria.VlanPcpCriterion.class);
383 assertThat(vlanPcpCriterion.priority(), is(equalTo(vlanPcp1)));
384 }
385
386 /**
387 * Test the equals() method of the VlanPcpCriterion class.
388 */
389 @Test
390 public void testVlanPcpCriterionEquals() {
391 checkEqualsAndToString(matchVlanPcp1, sameAsMatchVlanPcp1, matchVlanPcp2,
392 Criteria.VlanPcpCriterion.class);
393 }
394
395 /**
396 * Test the hashCode() method of the VlnPcpCriterion class.
397 */
398 @Test
399 public void testVlanPcpCriterionHashCode() {
400 assertThat(matchVlanPcp1.hashCode(), is(equalTo(sameAsMatchVlanPcp1.hashCode())));
401 assertThat(matchVlanPcp1.hashCode(), is(not(equalTo(matchVlanPcp2.hashCode()))));
402 }
403
404 // IpProtocolCriterion class
405
406 /**
407 * Test the matchIpProtocol method.
408 */
409 @Test
410 public void testMatchIpProtocolMethod() {
411 Criterion matchIPProtocol = Criteria.matchIPProtocol(protocol1);
412 Criteria.IPProtocolCriterion ipProtocolCriterion =
413 checkAndConvert(matchIPProtocol,
414 Criterion.Type.IP_PROTO,
415 Criteria.IPProtocolCriterion.class);
416 assertThat(ipProtocolCriterion.protocol(), is(equalTo(protocol1)));
417 }
418
419 /**
420 * Test the equals() method of the IpProtocolCriterion class.
421 */
422 @Test
423 public void testIpProtocolCriterionEquals() {
424 checkEqualsAndToString(matchIpProtocol1, sameAsMatchIpProtocol1,
425 matchIpProtocol2, Criteria.IPProtocolCriterion.class);
426 }
427
428 /**
429 * Test the hashCode() method of the IpProtocolCriterion class.
430 */
431 @Test
432 public void testIpProtocolCriterionHashCode() {
433 assertThat(matchIpProtocol1.hashCode(), is(equalTo(sameAsMatchIpProtocol1.hashCode())));
434 assertThat(matchIpProtocol1.hashCode(), is(not(equalTo(matchIpProtocol2.hashCode()))));
435 }
436
437 // IPCriterion class
438
439 /**
440 * Test the matchIPSrc method.
441 */
442 @Test
443 public void testMatchIPSrcMethod() {
444 Criterion matchIpSrc = Criteria.matchIPSrc(ip1);
445 Criteria.IPCriterion ipCriterion =
446 checkAndConvert(matchIpSrc,
447 Criterion.Type.IPV4_SRC,
448 Criteria.IPCriterion.class);
449 assertThat(ipCriterion.ip(), is(ip1));
450 }
451
452 /**
453 * Test the matchIPDst method.
454 */
455 @Test
456 public void testMatchIPDstMethod() {
457 Criterion matchIPDst = Criteria.matchIPDst(ip1);
458 Criteria.IPCriterion ipCriterion =
459 checkAndConvert(matchIPDst,
460 Criterion.Type.IPV4_DST,
461 Criteria.IPCriterion.class);
462 assertThat(ipCriterion.ip(), is(equalTo(ip1)));
463 }
464
465 /**
466 * Test the equals() method of the IpCriterion class.
467 */
468 @Test
469 public void testIPCriterionEquals() {
470 checkEqualsAndToString(matchIp1, sameAsMatchIp1, matchIp2,
471 Criteria.IPCriterion.class);
472 }
473
474 /**
475 * Test the hashCode() method of the IpCriterion class.
476 */
477 @Test
478 public void testIPCriterionHashCode() {
479 assertThat(matchIp1.hashCode(), is(equalTo(sameAsMatchIp1.hashCode())));
480 assertThat(matchIp1.hashCode(), is(not(equalTo(matchIp2.hashCode()))));
481 }
482
483 // LambdaCriterion class
484
485 /**
486 * Test the matchLambda method.
487 */
488 @Test
489 public void testMatchLambdaMethod() {
490 Criterion matchLambda = Criteria.matchLambda(lambda1);
491 Criteria.LambdaCriterion lambdaCriterion =
492 checkAndConvert(matchLambda,
493 Criterion.Type.OCH_SIGID,
494 Criteria.LambdaCriterion.class);
495 assertThat(lambdaCriterion.lambda(), is(equalTo(lambda1)));
496 }
497
498 /**
499 * Test the equals() method of the LambdaCriterion class.
500 */
501 @Test
502 public void testLambdaCriterionEquals() {
503 checkEqualsAndToString(matchLambda1, sameAsMatchLambda1, matchLambda2,
504 Criteria.LambdaCriterion.class);
505 }
506
507 /**
508 * Test the hashCode() method of the LambdaCriterion class.
509 */
510 @Test
511 public void testLambdaCriterionHashCode() {
512 assertThat(matchLambda1.hashCode(), is(equalTo(sameAsMatchLambda1.hashCode())));
513 assertThat(matchLambda1.hashCode(), is(not(equalTo(matchLambda2.hashCode()))));
514 }
515
516 // OpticalSignalTypeCriterion class
517
518 /**
519 * Test the matchOpticalSignalType method.
520 */
521 @Test
522 public void testMatchOpticalSignalTypeMethod() {
523 Criterion matchLambda = Criteria.matchOpticalSignalType(signalLambda1);
524 Criteria.OpticalSignalTypeCriterion opticalSignalTypeCriterion =
525 checkAndConvert(matchLambda,
526 Criterion.Type.OCH_SIGTYPE,
527 Criteria.OpticalSignalTypeCriterion.class);
528 assertThat(opticalSignalTypeCriterion.signalType(), is(equalTo(signalLambda1)));
529 }
530
531 /**
532 * Test the equals() method of the OpticalSignalTypeCriterion class.
533 */
534 @Test
535 public void testOpticalSignalTypeCriterionEquals() {
536 checkEqualsAndToString(matchSignalLambda1, sameAsMatchSignalLambda1,
537 matchSignalLambda2,
538 Criteria.OpticalSignalTypeCriterion.class);
539 }
540
541 /**
542 * Test the hashCode() method of the OpticalSignalTypeCriterion class.
543 */
544 @Test
545 public void testOpticalSignalTypeCriterionHashCode() {
546 assertThat(matchSignalLambda1.hashCode(), is(equalTo(sameAsMatchSignalLambda1.hashCode())));
547 assertThat(matchSignalLambda1.hashCode(), is(not(equalTo(matchSignalLambda2.hashCode()))));
548 }
549
550}