blob: 6a2387855f22ccd6ee9fb9f5eb5d961d3a37bc31 [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)));
119 assertThat(criterion, is(instanceOf(clazz)));
120 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) {
134 assertThat(c1, is(instanceOf(clazz)));
135 assertThat(c1match, is(instanceOf(clazz)));
136 assertThat(c2, is(instanceOf(clazz)));
137
138 assertThat(c1, is(equalTo(c1match)));
139 assertThat(c1, is(not(equalTo(c2))));
140 assertThat(c1, is(not(equalTo(new Object()))));
141
142 // Make sure the enumerated type appears in the toString() output.
143 assertThat(c1.toString(), containsString(c1.type().toString()));
144 }
145
146
147 /**
148 * Check that the Criteria class is a valid utility class.
149 */
150 @Test
151 public void testCriteriaUtility() {
152 assertThatClassIsUtility(Criteria.class);
153 }
154
155 /**
156 * Check that the Criteria implementations are immutable.
157 */
158 @Test
159 public void testCriteriaImmutability() {
160 assertThatClassIsImmutable(Criteria.PortCriterion.class);
161 assertThatClassIsImmutable(Criteria.EthCriterion.class);
162 assertThatClassIsImmutable(Criteria.EthTypeCriterion.class);
163 assertThatClassIsImmutable(Criteria.IPCriterion.class);
164 assertThatClassIsImmutable(Criteria.IPProtocolCriterion.class);
165 assertThatClassIsImmutable(Criteria.VlanPcpCriterion.class);
166 assertThatClassIsImmutable(Criteria.VlanIdCriterion.class);
167 assertThatClassIsImmutable(Criteria.TcpPortCriterion.class);
168 assertThatClassIsImmutable(Criteria.LambdaCriterion.class);
169 assertThatClassIsImmutable(Criteria.OpticalSignalTypeCriterion.class);
170 }
171
172 // PortCriterion class
173
174 /**
175 * Test the matchInPort method.
176 */
177 @Test
178 public void testMatchInPortMethod() {
179 PortNumber p1 = portNumber(1);
180 Criterion matchInPort = Criteria.matchInPort(p1);
181 Criteria.PortCriterion portCriterion =
182 checkAndConvert(matchInPort,
183 Criterion.Type.IN_PORT,
184 Criteria.PortCriterion.class);
185 assertThat(portCriterion.port(), is(equalTo(p1)));
186 }
187
188 /**
189 * Test the equals() method of the PortCriterion class.
190 */
191 @Test
192 public void testPortCriterionEquals() {
193 checkEqualsAndToString(matchInPort1, sameAsMatchInPort1, matchInPort2,
194 Criteria.PortCriterion.class);
195 }
196
197 /**
198 * Test the hashCode() method of the PortCriterion class.
199 */
200 @Test
201 public void testPortCriterionHashCode() {
202 assertThat(matchInPort1.hashCode(), is(equalTo(sameAsMatchInPort1.hashCode())));
203 assertThat(matchInPort1.hashCode(), is(not(equalTo(matchInPort2.hashCode()))));
204 }
205
206 // EthCriterion class
207
208 /**
209 * Test the matchEthSrc method.
210 */
211 @Test
212 public void testMatchEthSrcMethod() {
213 Criterion matchEthSrc = Criteria.matchEthSrc(mac1);
214 Criteria.EthCriterion ethCriterion =
215 checkAndConvert(matchEthSrc,
216 Criterion.Type.ETH_SRC,
217 Criteria.EthCriterion.class);
218 assertThat(ethCriterion.mac(), is(mac1));
219 }
220
221 /**
222 * Test the matchEthDst method.
223 */
224 @Test
225 public void testMatchEthDstMethod() {
226 Criterion matchTcpDst = Criteria.matchEthDst(mac1);
227 Criteria.EthCriterion ethCriterion =
228 checkAndConvert(matchTcpDst,
229 Criterion.Type.ETH_DST,
230 Criteria.EthCriterion.class);
231 assertThat(ethCriterion.mac(), is(equalTo(mac1)));
232 }
233
234 /**
235 * Test the equals() method of the EthCriterion class.
236 */
237 @Test
238 public void testEthCriterionEquals() {
239 checkEqualsAndToString(matchEth1, sameAsMatchEth1, matchEth2,
240 Criteria.EthCriterion.class);
241 }
242
243 /**
244 * Test the hashCode() method of the EthCriterion class.
245 */
246 @Test
247 public void testEthCriterionHashCode() {
248 assertThat(matchEth1.hashCode(), is(equalTo(sameAsMatchEth1.hashCode())));
249 assertThat(matchEth1.hashCode(), is(not(equalTo(matchEth2.hashCode()))));
250 }
251
252
253 // TcpPortCriterion class
254
255 /**
256 * Test the matchTcpSrc method.
257 */
258 @Test
259 public void testMatchTcpSrcMethod() {
260 Criterion matchTcpSrc = Criteria.matchTcpSrc((short) 1);
261 Criteria.TcpPortCriterion tcpPortCriterion =
262 checkAndConvert(matchTcpSrc,
263 Criterion.Type.TCP_SRC,
264 Criteria.TcpPortCriterion.class);
265 assertThat(tcpPortCriterion.tcpPort(), is(equalTo((short) 1)));
266 }
267
268 /**
269 * Test the matchTcpDst method.
270 */
271 @Test
272 public void testMatchTcpDstMethod() {
273 Criterion matchTcpDst = Criteria.matchTcpDst((short) 1);
274 Criteria.TcpPortCriterion tcpPortCriterion =
275 checkAndConvert(matchTcpDst,
276 Criterion.Type.TCP_DST,
277 Criteria.TcpPortCriterion.class);
278 assertThat(tcpPortCriterion.tcpPort(), is(equalTo((short) 1)));
279 }
280
281 /**
282 * Test the equals() method of the TcpPortCriterion class.
283 */
284 @Test
285 public void testTcpPortCriterionEquals() {
286 checkEqualsAndToString(matchTcpPort1, sameAsMatchTcpPort1, matchTcpPort2,
287 Criteria.TcpPortCriterion.class);
288 }
289
290 /**
291 * Test the hashCode() method of the TcpPortCriterion class.
292 */
293 @Test
294 public void testTcpPortCriterionHashCode() {
295 assertThat(matchTcpPort1.hashCode(), is(equalTo(sameAsMatchTcpPort1.hashCode())));
296 assertThat(matchTcpPort1.hashCode(), is(not(equalTo(matchTcpPort2.hashCode()))));
297 }
298
299
300 // EthTypeCriterion class
301
302 /**
303 * Test the matchEthType method.
304 */
305 @Test
306 public void testMatchEthTypeMethod() {
307 Short ethType = 12;
308 Criterion matchEthType = Criteria.matchEthType(ethType);
309 Criteria.EthTypeCriterion ethTypeCriterion =
310 checkAndConvert(matchEthType,
311 Criterion.Type.ETH_TYPE,
312 Criteria.EthTypeCriterion.class);
313 assertThat(ethTypeCriterion.ethType(), is(equalTo(ethType)));
314 }
315
316 /**
317 * Test the equals() method of the EthTypeCriterion class.
318 */
319 @Test
320 public void testEthTypeCriterionEquals() {
321 checkEqualsAndToString(matchEthType1, sameAsMatchEthType1, matchEthType2,
322 Criteria.EthTypeCriterion.class);
323 }
324
325 /**
326 * Test the hashCode() method of the EthTypeCriterion class.
327 */
328 @Test
329 public void testEthTypeCriterionHashCode() {
330 assertThat(matchInPort1.hashCode(), is(equalTo(sameAsMatchInPort1.hashCode())));
331 assertThat(matchInPort1.hashCode(), is(not(equalTo(matchInPort2.hashCode()))));
332 }
333
334
335 // VlanIdCriterion class
336
337 /**
338 * Test the matchVlanId method.
339 */
340 @Test
341 public void testMatchVlanIdMethod() {
342 Criterion matchVlanId = Criteria.matchVlanId(vlanId1);
343 Criteria.VlanIdCriterion vlanIdCriterion =
344 checkAndConvert(matchVlanId,
345 Criterion.Type.VLAN_VID,
346 Criteria.VlanIdCriterion.class);
347 assertThat(vlanIdCriterion.vlanId(), is(equalTo(vlanId1)));
348 }
349
350 /**
351 * Test the equals() method of the VlanIdCriterion class.
352 */
353 @Test
354 public void testVlanIdCriterionEquals() {
355 checkEqualsAndToString(matchVlanId1, sameAsMatchVlanId1, matchVlanId2,
356 Criteria.VlanIdCriterion.class);
357 }
358
359 /**
360 * Test the hashCode() method of the VlanIdCriterion class.
361 */
362 @Test
363 public void testVlanIdCriterionHashCode() {
364 assertThat(matchVlanId1.hashCode(), is(equalTo(sameAsMatchVlanId1.hashCode())));
365 assertThat(matchVlanId1.hashCode(), is(not(equalTo(matchVlanId2.hashCode()))));
366 }
367
368 // VlanPcpCriterion class
369
370 /**
371 * Test the matchVlanPcp method.
372 */
373 @Test
374 public void testMatchVlanPcpMethod() {
375 Criterion matchVlanPcp = Criteria.matchVlanPcp(vlanPcp1);
376 Criteria.VlanPcpCriterion vlanPcpCriterion =
377 checkAndConvert(matchVlanPcp,
378 Criterion.Type.VLAN_PCP,
379 Criteria.VlanPcpCriterion.class);
380 assertThat(vlanPcpCriterion.priority(), is(equalTo(vlanPcp1)));
381 }
382
383 /**
384 * Test the equals() method of the VlanPcpCriterion class.
385 */
386 @Test
387 public void testVlanPcpCriterionEquals() {
388 checkEqualsAndToString(matchVlanPcp1, sameAsMatchVlanPcp1, matchVlanPcp2,
389 Criteria.VlanPcpCriterion.class);
390 }
391
392 /**
393 * Test the hashCode() method of the VlnPcpCriterion class.
394 */
395 @Test
396 public void testVlanPcpCriterionHashCode() {
397 assertThat(matchVlanPcp1.hashCode(), is(equalTo(sameAsMatchVlanPcp1.hashCode())));
398 assertThat(matchVlanPcp1.hashCode(), is(not(equalTo(matchVlanPcp2.hashCode()))));
399 }
400
401 // IpProtocolCriterion class
402
403 /**
404 * Test the matchIpProtocol method.
405 */
406 @Test
407 public void testMatchIpProtocolMethod() {
408 Criterion matchIPProtocol = Criteria.matchIPProtocol(protocol1);
409 Criteria.IPProtocolCriterion ipProtocolCriterion =
410 checkAndConvert(matchIPProtocol,
411 Criterion.Type.IP_PROTO,
412 Criteria.IPProtocolCriterion.class);
413 assertThat(ipProtocolCriterion.protocol(), is(equalTo(protocol1)));
414 }
415
416 /**
417 * Test the equals() method of the IpProtocolCriterion class.
418 */
419 @Test
420 public void testIpProtocolCriterionEquals() {
421 checkEqualsAndToString(matchIpProtocol1, sameAsMatchIpProtocol1,
422 matchIpProtocol2, Criteria.IPProtocolCriterion.class);
423 }
424
425 /**
426 * Test the hashCode() method of the IpProtocolCriterion class.
427 */
428 @Test
429 public void testIpProtocolCriterionHashCode() {
430 assertThat(matchIpProtocol1.hashCode(), is(equalTo(sameAsMatchIpProtocol1.hashCode())));
431 assertThat(matchIpProtocol1.hashCode(), is(not(equalTo(matchIpProtocol2.hashCode()))));
432 }
433
434 // IPCriterion class
435
436 /**
437 * Test the matchIPSrc method.
438 */
439 @Test
440 public void testMatchIPSrcMethod() {
441 Criterion matchIpSrc = Criteria.matchIPSrc(ip1);
442 Criteria.IPCriterion ipCriterion =
443 checkAndConvert(matchIpSrc,
444 Criterion.Type.IPV4_SRC,
445 Criteria.IPCriterion.class);
446 assertThat(ipCriterion.ip(), is(ip1));
447 }
448
449 /**
450 * Test the matchIPDst method.
451 */
452 @Test
453 public void testMatchIPDstMethod() {
454 Criterion matchIPDst = Criteria.matchIPDst(ip1);
455 Criteria.IPCriterion ipCriterion =
456 checkAndConvert(matchIPDst,
457 Criterion.Type.IPV4_DST,
458 Criteria.IPCriterion.class);
459 assertThat(ipCriterion.ip(), is(equalTo(ip1)));
460 }
461
462 /**
463 * Test the equals() method of the IpCriterion class.
464 */
465 @Test
466 public void testIPCriterionEquals() {
467 checkEqualsAndToString(matchIp1, sameAsMatchIp1, matchIp2,
468 Criteria.IPCriterion.class);
469 }
470
471 /**
472 * Test the hashCode() method of the IpCriterion class.
473 */
474 @Test
475 public void testIPCriterionHashCode() {
476 assertThat(matchIp1.hashCode(), is(equalTo(sameAsMatchIp1.hashCode())));
477 assertThat(matchIp1.hashCode(), is(not(equalTo(matchIp2.hashCode()))));
478 }
479
480 // LambdaCriterion class
481
482 /**
483 * Test the matchLambda method.
484 */
485 @Test
486 public void testMatchLambdaMethod() {
487 Criterion matchLambda = Criteria.matchLambda(lambda1);
488 Criteria.LambdaCriterion lambdaCriterion =
489 checkAndConvert(matchLambda,
490 Criterion.Type.OCH_SIGID,
491 Criteria.LambdaCriterion.class);
492 assertThat(lambdaCriterion.lambda(), is(equalTo(lambda1)));
493 }
494
495 /**
496 * Test the equals() method of the LambdaCriterion class.
497 */
498 @Test
499 public void testLambdaCriterionEquals() {
500 checkEqualsAndToString(matchLambda1, sameAsMatchLambda1, matchLambda2,
501 Criteria.LambdaCriterion.class);
502 }
503
504 /**
505 * Test the hashCode() method of the LambdaCriterion class.
506 */
507 @Test
508 public void testLambdaCriterionHashCode() {
509 assertThat(matchLambda1.hashCode(), is(equalTo(sameAsMatchLambda1.hashCode())));
510 assertThat(matchLambda1.hashCode(), is(not(equalTo(matchLambda2.hashCode()))));
511 }
512
513 // OpticalSignalTypeCriterion class
514
515 /**
516 * Test the matchOpticalSignalType method.
517 */
518 @Test
519 public void testMatchOpticalSignalTypeMethod() {
520 Criterion matchLambda = Criteria.matchOpticalSignalType(signalLambda1);
521 Criteria.OpticalSignalTypeCriterion opticalSignalTypeCriterion =
522 checkAndConvert(matchLambda,
523 Criterion.Type.OCH_SIGTYPE,
524 Criteria.OpticalSignalTypeCriterion.class);
525 assertThat(opticalSignalTypeCriterion.signalType(), is(equalTo(signalLambda1)));
526 }
527
528 /**
529 * Test the equals() method of the OpticalSignalTypeCriterion class.
530 */
531 @Test
532 public void testOpticalSignalTypeCriterionEquals() {
533 checkEqualsAndToString(matchSignalLambda1, sameAsMatchSignalLambda1,
534 matchSignalLambda2,
535 Criteria.OpticalSignalTypeCriterion.class);
536 }
537
538 /**
539 * Test the hashCode() method of the OpticalSignalTypeCriterion class.
540 */
541 @Test
542 public void testOpticalSignalTypeCriterionHashCode() {
543 assertThat(matchSignalLambda1.hashCode(), is(equalTo(sameAsMatchSignalLambda1.hashCode())));
544 assertThat(matchSignalLambda1.hashCode(), is(not(equalTo(matchSignalLambda2.hashCode()))));
545 }
546
547}