2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.sal.match;
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13 import java.util.Arrays;
15 import org.junit.Assert;
16 import org.junit.Test;
17 import org.opendaylight.controller.sal.core.Node;
18 import org.opendaylight.controller.sal.core.NodeConnector;
19 import org.opendaylight.controller.sal.match.Match;
20 import org.opendaylight.controller.sal.match.MatchField;
21 import org.opendaylight.controller.sal.match.MatchType;
22 import org.opendaylight.controller.sal.utils.EtherTypes;
23 import org.opendaylight.controller.sal.utils.IPProtocols;
24 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
25 import org.opendaylight.controller.sal.utils.NodeCreator;
27 public class MatchTest {
29 public void testMatchCreation() {
30 Node node = NodeCreator.createOFNode(7l);
31 NodeConnector port = NodeConnectorCreator.createOFNodeConnector((short) 6, node);
32 MatchField field = new MatchField(MatchType.IN_PORT, port);
34 Assert.assertTrue(field != null);
35 Assert.assertTrue(field.getType() == MatchType.IN_PORT);
36 Assert.assertTrue((NodeConnector) field.getValue() == port);
37 Assert.assertTrue(field.isValid());
40 field = new MatchField(MatchType.TP_SRC, Long.valueOf(23));
41 Assert.assertFalse(field.isValid());
44 field = new MatchField(MatchType.TP_SRC, (long) 45);
45 Assert.assertFalse(field.isValid());
48 field = new MatchField(MatchType.TP_SRC, 120000);
49 Assert.assertFalse(field.isValid());
51 byte mac[] = { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd, (byte) 11, (byte) 22 };
52 byte mask[] = { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff };
54 field = new MatchField(MatchType.DL_SRC, mac, mask);
55 Assert.assertFalse(field.getValue() == null);
58 field = new MatchField(MatchType.NW_TOS, (byte) 0x22, (byte) 0x3);
59 Assert.assertFalse(field.getValue() == null);
63 public void testMatchSetGet() {
64 Match x = new Match();
66 NodeConnector inPort = NodeConnectorCreator.createOFNodeConnector(val, NodeCreator.createOFNode(1l));
67 x.setField(MatchType.IN_PORT, inPort);
68 Assert.assertTrue(((NodeConnector) x.getField(MatchType.IN_PORT).getValue()).equals(inPort));
69 Assert.assertTrue((Short) ((NodeConnector) x.getField(MatchType.IN_PORT).getValue()).getID() == val);
73 public void testMatchSetGetMAC() {
74 Match x = new Match();
75 byte mac[] = { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd, (byte) 11, (byte) 22 };
76 byte mac2[] = { (byte) 0xaa, (byte) 0xbb, 0, 0, 0, (byte) 0xbb };
77 byte mask1[] = { (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66 };
78 byte mask2[] = { (byte) 0xff, (byte) 0xff, (byte) 0, (byte) 0, (byte) 0, (byte) 0xff };
80 x.setField(MatchType.DL_SRC, mac.clone(), mask1);
81 x.setField(MatchType.DL_DST, mac2.clone(), mask2);
82 Assert.assertTrue(Arrays.equals(mac, (byte[]) x.getField(MatchType.DL_SRC).getValue()));
83 Assert.assertFalse(Arrays.equals((byte[]) x.getField(MatchType.DL_SRC).getValue(),
84 (byte[]) x.getField(MatchType.DL_DST).getValue()));
85 Assert.assertFalse(x.getField(MatchType.DL_SRC).getBitMask() == x.getField(MatchType.DL_DST).getBitMask());
87 x.setField(new MatchField(MatchType.DL_DST, mac.clone(), mask1));
88 Assert.assertTrue(Arrays.equals((byte[]) x.getField(MatchType.DL_SRC).getValue(),
89 (byte[]) x.getField(MatchType.DL_DST).getValue()));
93 public void testMatchSetGetNWAddr() throws UnknownHostException {
94 Match x = new Match();
95 String ip = "172.20.231.23";
96 InetAddress address = InetAddress.getByName(ip);
97 InetAddress mask = InetAddress.getByName("255.255.0.0");
99 x.setField(MatchType.NW_SRC, address, mask);
100 Assert.assertTrue(ip.equals(((InetAddress) x.getField(MatchType.NW_SRC).getValue()).getHostAddress()));
101 Assert.assertTrue(x.getField(MatchType.NW_SRC).getMask().equals(mask));
105 public void testMatchSetGetEtherType() throws UnknownHostException {
106 Match x = new Match();
108 x.setField(MatchType.DL_TYPE, EtherTypes.QINQ.shortValue(), (short) 0xffff);
109 Assert.assertTrue(((Short) x.getField(MatchType.DL_TYPE).getValue()).equals(EtherTypes.QINQ.shortValue()));
110 Assert.assertFalse(x.getField(MatchType.DL_TYPE).getValue() == EtherTypes.QINQ);
111 Assert.assertFalse(x.getField(MatchType.DL_TYPE).getValue().equals(EtherTypes.QINQ));
113 x.setField(MatchType.DL_TYPE, EtherTypes.LLDP.shortValue(), (short) 0xffff);
114 Assert.assertTrue(((Short) x.getField(MatchType.DL_TYPE).getValue()).equals(EtherTypes.LLDP.shortValue()));
115 Assert.assertFalse(x.getField(MatchType.DL_TYPE).equals(EtherTypes.LLDP.intValue()));
119 public void testSetGetNwTos() {
120 Match x = new Match();
121 x.setField(MatchType.NW_TOS, (byte) 0xb, (byte) 0xf);
123 Byte t = new Byte((byte) 0xb);
125 Object o = x.getField(MatchType.NW_TOS).getValue();
126 Assert.assertTrue(o.equals(t));
127 Assert.assertTrue(o.equals((byte) 0xb));
131 public void testSetGetNwProto() {
132 Match x = new Match();
133 byte proto = (byte) 199;
134 x.setField(MatchType.NW_PROTO, proto, (byte) 0xff);
136 Object o = x.getField(MatchType.NW_PROTO).getValue();
137 Assert.assertTrue(o.equals(proto));
141 public void testSetTpSrc() {
142 // Minimum value validation.
143 Match match = new Match();
145 match.setField(MatchType.TP_SRC, tp_src);
147 Object o = match.getField(MatchType.TP_SRC).getValue();
148 Assert.assertTrue(o.equals(tp_src));
150 // Maximum value validation.
152 tp_src = (short) 0xffff;
153 match.setField(MatchType.TP_SRC, tp_src);
155 o = match.getField(MatchType.TP_SRC).getValue();
156 Assert.assertTrue(o.equals(tp_src));
160 public void testSetTpDst() {
161 // Minimum value validation.
162 Match match = new Match();
164 match.setField(MatchType.TP_DST, tp_dst);
166 Object o = match.getField(MatchType.TP_DST).getValue();
167 Assert.assertTrue(o.equals(tp_dst));
169 // Maximum value validation.
171 tp_dst = (short) 0xffff;
172 match.setField(MatchType.TP_DST, tp_dst);
174 o = match.getField(MatchType.TP_DST).getValue();
175 Assert.assertTrue(o.equals(tp_dst));
179 public void testMatchMask() {
180 Match x = new Match();
181 NodeConnector inPort = NodeConnectorCreator.createOFNodeConnector((short) 6, NodeCreator.createOFNode(3l));
182 x.setField(MatchType.IN_PORT, inPort);
183 x.setField(MatchType.DL_VLAN, (short) 28, (short) 0xfff);
184 Assert.assertFalse(x.getMatches() == 0);
185 Assert.assertTrue(x.getMatches() == (MatchType.IN_PORT.getIndex() | MatchType.DL_VLAN.getIndex()));
189 public void testMatchBitMask() {
190 byte mac[] = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 22, (byte) 12 };
191 byte mask[] = { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0 };
192 NodeConnector inPort = NodeConnectorCreator.createOFNodeConnector((short) 4095, NodeCreator.createOFNode(7l));
194 MatchField x = new MatchField(MatchType.IN_PORT, inPort);
195 Assert.assertTrue((x.getMask()) == null);
197 x = new MatchField(MatchType.DL_VLAN, (short) 255, (short) 0xff);
198 Assert.assertTrue(x.getBitMask() == 0xff);
200 x = new MatchField(MatchType.DL_SRC, mac, mask);
201 Assert.assertTrue(x.getMask().equals(mask));
202 Assert.assertTrue(x.getBitMask() == 0xffffffffff00L);
206 public void testNullMask() {
207 byte mac[] = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 22, (byte) 12 };
208 NodeConnector inPort = NodeConnectorCreator.createOFNodeConnector((short) 2000, NodeCreator.createOFNode(7l));
210 MatchField x = new MatchField(MatchType.IN_PORT, inPort);
211 Assert.assertTrue(x.getBitMask() == 0);
213 x = new MatchField(MatchType.NW_PROTO, (byte) 17);
214 Assert.assertTrue(x.getBitMask() == 0xff);
216 x = new MatchField(MatchType.DL_VLAN, (short) 255);
217 Assert.assertTrue(x.getBitMask() == 0xfff);
219 x = new MatchField(MatchType.DL_SRC, mac);
220 Assert.assertTrue(x.getBitMask() == 0xffffffffffffL);
224 public void testEquality() throws Exception {
225 Node node = NodeCreator.createOFNode(7l);
226 NodeConnector port = NodeConnectorCreator.createOFNodeConnector((short) 24, node);
227 NodeConnector port2 = NodeConnectorCreator.createOFNodeConnector((short) 24, node);
228 byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
229 byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
230 byte srcMac2[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
231 byte dstMac2[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
232 InetAddress srcIP = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
233 InetAddress dstIP = InetAddress.getByName("2001:420:281:1004:e123:e688:d655:a1b0");
234 InetAddress ipMask = InetAddress.getByName("ffff:ffff:ffff:ffff:0:0:0:0");
235 InetAddress ipMaskd = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
236 InetAddress srcIP2 = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
237 InetAddress dstIP2 = InetAddress.getByName("2001:420:281:1004:e123:e688:d655:a1b0");
238 InetAddress ipMask2 = InetAddress.getByName("ffff:ffff:ffff:ffff:0:0:0:0");
239 InetAddress ipMaskd2 = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
240 short ethertype = EtherTypes.IPv6.shortValue();
241 short ethertype2 = EtherTypes.IPv6.shortValue();
242 short vlan = (short) 27, vlan2 = (short) 27;
243 byte vlanPr = (byte) 3, vlanPr2 = (byte) 3;
244 Byte tos = 4, tos2 = 4;
245 byte proto = IPProtocols.UDP.byteValue(), proto2 = IPProtocols.UDP.byteValue();
246 short src = (short) 5500, src2 = (short) 5500;
247 short dst = 80, dst2 = 80;
250 * Create a SAL Flow aFlow
252 Match match1 = new Match();
253 Match match2 = new Match();
254 match1.setField(MatchType.IN_PORT, port);
255 match1.setField(MatchType.DL_SRC, srcMac);
256 match1.setField(MatchType.DL_DST, dstMac);
257 match1.setField(MatchType.DL_TYPE, ethertype);
258 match1.setField(MatchType.DL_VLAN, vlan);
259 match1.setField(MatchType.DL_VLAN_PR, vlanPr);
260 match1.setField(MatchType.NW_SRC, srcIP, ipMask);
261 match1.setField(MatchType.NW_DST, dstIP, ipMaskd);
262 match1.setField(MatchType.NW_TOS, tos);
263 match1.setField(MatchType.NW_PROTO, proto);
264 match1.setField(MatchType.TP_SRC, src);
265 match1.setField(MatchType.TP_DST, dst);
267 match2.setField(MatchType.IN_PORT, port2);
268 match2.setField(MatchType.DL_SRC, srcMac2);
269 match2.setField(MatchType.DL_DST, dstMac2);
270 match2.setField(MatchType.DL_TYPE, ethertype2);
271 match2.setField(MatchType.DL_VLAN, vlan2);
272 match2.setField(MatchType.DL_VLAN_PR, vlanPr2);
273 match2.setField(MatchType.NW_SRC, srcIP2, ipMask2);
274 match2.setField(MatchType.NW_DST, dstIP2, ipMaskd2);
275 match2.setField(MatchType.NW_TOS, tos2);
276 match2.setField(MatchType.NW_PROTO, proto2);
277 match2.setField(MatchType.TP_SRC, src2);
278 match2.setField(MatchType.TP_DST, dst2);
280 Assert.assertTrue(match1.equals(match2));
282 // Make sure all values are equals
283 for (MatchType type : MatchType.values()) {
284 if (match1.isPresent(type)) {
285 Assert.assertTrue(match1.getField(type).equals(match2.getField(type)));
289 // Make none of the fields couples are pointing to the same reference
290 MatchField a = null, b = null;
291 for (MatchType type : MatchType.values()) {
292 a = match1.getField(type);
293 b = match2.getField(type);
294 if (a != null && b != null) {
295 Assert.assertFalse(a == b);
301 public void testEqualityNetMask() throws Exception {
303 InetAddress srcIP = InetAddress.getByName("1.1.1.1");
304 InetAddress ipMask = InetAddress.getByName("255.255.255.255");
305 InetAddress srcIP2 = InetAddress.getByName("1.1.1.1");
306 InetAddress ipMask2 = null;
307 short ethertype = EtherTypes.IPv4.shortValue();
308 short ethertype2 = EtherTypes.IPv4.shortValue();
311 * Create a SAL Flow aFlow
313 Match match1 = new Match();
314 Match match2 = new Match();
316 match1.setField(MatchType.DL_TYPE, ethertype);
317 match1.setField(MatchType.NW_SRC, srcIP, ipMask);
319 match2.setField(MatchType.DL_TYPE, ethertype2);
320 match2.setField(MatchType.NW_SRC, srcIP2, ipMask2);
322 Assert.assertTrue(match1.equals(match2));
324 ipMask2 = InetAddress.getByName("255.255.255.255");
325 match2.setField(MatchType.NW_SRC, srcIP2, ipMask2);
327 srcIP = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
328 srcIP2 = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
330 ipMask2 = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
331 ethertype = EtherTypes.IPv6.shortValue();
332 ethertype2 = EtherTypes.IPv6.shortValue();
334 match1.setField(MatchType.DL_TYPE, ethertype);
335 match1.setField(MatchType.NW_SRC, srcIP, ipMask);
337 match2.setField(MatchType.DL_TYPE, ethertype2);
338 match2.setField(MatchType.NW_SRC, srcIP2, ipMask2);
340 Assert.assertTrue(match1.equals(match2));
344 public void testHashCode() throws Exception {
345 byte srcMac1[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
346 byte srcMac2[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
347 byte dstMac1[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
348 byte dstMac2[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
349 short ethertype = EtherTypes.IPv4.shortValue();
350 short ethertype2 = EtherTypes.IPv4.shortValue();
351 InetAddress srcIP1 = InetAddress.getByName("1.1.1.1");
352 InetAddress ipMask1 = InetAddress.getByName("255.255.255.255");
353 InetAddress srcIP2 = InetAddress.getByName("1.1.1.1");
354 InetAddress ipMask2 = InetAddress.getByName("255.255.255.255");
356 Match match1 = new Match();
357 Match match2 = new Match();
359 MatchField field1 = new MatchField(MatchType.DL_SRC, srcMac1);
360 MatchField field2 = new MatchField(MatchType.DL_SRC, srcMac2);
361 Assert.assertTrue(field1.hashCode() == field2.hashCode());
363 match1.setField(field1);
364 match2.setField(field2);
365 Assert.assertTrue(match1.hashCode() == match2.hashCode());
367 MatchField field3 = new MatchField(MatchType.DL_DST, dstMac1);
368 MatchField field4 = new MatchField(MatchType.DL_DST, dstMac2);
369 Assert.assertTrue(field3.hashCode() == field4.hashCode());
371 match1.setField(field3);
372 match2.setField(field4);
373 Assert.assertTrue(match1.hashCode() == match2.hashCode());
375 MatchField field5 = new MatchField(MatchType.DL_TYPE, ethertype);
376 MatchField field6 = new MatchField(MatchType.DL_TYPE, ethertype2);
377 Assert.assertTrue(field5.hashCode() == field6.hashCode());
379 match1.setField(field5);
380 match2.setField(field6);
381 Assert.assertTrue(match1.hashCode() == match2 .hashCode());
383 MatchField field7 = new MatchField(MatchType.NW_SRC, srcIP1, ipMask1);
384 MatchField field8 = new MatchField(MatchType.NW_SRC, srcIP2, ipMask2);
385 Assert.assertTrue(field7.hashCode() == field8.hashCode());
387 match1.setField(field7);
388 match2.setField(field8);
389 Assert.assertTrue(match1.hashCode() == match2.hashCode());
394 public void testCloning() throws Exception {
395 Node node = NodeCreator.createOFNode(7l);
396 NodeConnector port = NodeConnectorCreator.createOFNodeConnector((short) 24, node);
397 byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
398 byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
399 InetAddress srcIP = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
400 InetAddress dstIP = InetAddress.getByName("2001:420:281:1004:e123:e688:d655:a1b0");
401 InetAddress ipMasks = InetAddress.getByName("ffff:ffff:ffff:ffff:0:0:0:0");
402 InetAddress ipMaskd = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
403 short ethertype = EtherTypes.IPv6.shortValue();
404 short vlan = (short) 27;
405 byte vlanPr = (byte) 3;
407 byte proto = IPProtocols.UDP.byteValue();
408 short src = (short) 5500;
412 * Create a SAL Flow aFlow
414 Match match = new Match();
415 match.setField(MatchType.IN_PORT, port);
416 match.setField(MatchType.DL_SRC, srcMac);
417 match.setField(MatchType.DL_DST, dstMac);
418 match.setField(MatchType.DL_TYPE, ethertype);
419 match.setField(MatchType.DL_VLAN, vlan);
420 match.setField(MatchType.DL_VLAN_PR, vlanPr);
421 match.setField(MatchType.NW_SRC, srcIP, ipMasks);
422 match.setField(MatchType.NW_DST, dstIP, ipMaskd);
423 match.setField(MatchType.NW_TOS, tos);
424 match.setField(MatchType.NW_PROTO, proto);
425 match.setField(MatchType.TP_SRC, src);
426 match.setField(MatchType.TP_DST, dst);
428 Match cloned = match.clone();
430 // Make sure all values are equals
431 for (MatchType type : MatchType.values()) {
432 if (match.isPresent(type)) {
433 if (!match.getField(type).equals(cloned.getField(type))) {
434 Assert.assertTrue(match.getField(type).equals(cloned.getField(type)));
439 // Make sure none of the fields couples are pointing to the same
441 MatchField a = null, b = null;
442 for (MatchType type : MatchType.values()) {
443 a = match.getField(type);
444 b = cloned.getField(type);
445 if (a != null && b != null) {
446 Assert.assertFalse(a == b);
450 Assert.assertTrue(match.equals(cloned));
452 Assert.assertFalse(match.getField(MatchType.DL_SRC) == cloned.getField(MatchType.DL_SRC));
453 Assert.assertFalse(match.getField(MatchType.NW_DST) == cloned.getField(MatchType.NW_DST));
454 Assert.assertTrue(match.getField(MatchType.NW_DST).getMask()
455 .equals(cloned.getField(MatchType.NW_DST).getMask()));
456 Assert.assertTrue(match.hashCode() == cloned.hashCode());
460 public void testFlip() throws Exception {
461 Node node = NodeCreator.createOFNode(7l);
462 NodeConnector port = NodeConnectorCreator.createOFNodeConnector((short) 24, node);
463 byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
464 byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
465 InetAddress srcIP = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
466 InetAddress dstIP = InetAddress.getByName("2001:420:281:1004:e123:e688:d655:a1b0");
467 InetAddress ipMasks = InetAddress.getByName("ffff:ffff:ffff:ffff:0:0:0:0");
468 InetAddress ipMaskd = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
469 short ethertype = EtherTypes.IPv6.shortValue();
470 short vlan = (short) 27;
471 byte vlanPr = (byte) 3;
473 byte proto = IPProtocols.UDP.byteValue();
474 short src = (short) 5500;
478 * Create a SAL Flow aFlow
480 Match match = new Match();
481 match.setField(MatchType.IN_PORT, port);
482 match.setField(MatchType.DL_SRC, srcMac);
483 match.setField(MatchType.DL_DST, dstMac);
484 match.setField(MatchType.DL_TYPE, ethertype);
485 match.setField(MatchType.DL_VLAN, vlan);
486 match.setField(MatchType.DL_VLAN_PR, vlanPr);
487 match.setField(MatchType.NW_SRC, srcIP, ipMasks);
488 match.setField(MatchType.NW_DST, dstIP, ipMaskd);
489 match.setField(MatchType.NW_TOS, tos);
490 match.setField(MatchType.NW_PROTO, proto);
491 match.setField(MatchType.TP_SRC, src);
492 match.setField(MatchType.TP_DST, dst);
494 Match flipped = match.reverse();
496 Assert.assertTrue(match.getField(MatchType.DL_TYPE).equals(flipped.getField(MatchType.DL_TYPE)));
497 Assert.assertTrue(match.getField(MatchType.DL_VLAN).equals(flipped.getField(MatchType.DL_VLAN)));
499 Assert.assertTrue(match.getField(MatchType.DL_DST).getValue()
500 .equals(flipped.getField(MatchType.DL_SRC).getValue()));
501 Assert.assertTrue(match.getField(MatchType.DL_DST).getMask() == flipped.getField(MatchType.DL_SRC).getMask());
503 Assert.assertTrue(match.getField(MatchType.NW_DST).getValue()
504 .equals(flipped.getField(MatchType.NW_SRC).getValue()));
505 Assert.assertTrue(match.getField(MatchType.NW_DST).getMask() == flipped.getField(MatchType.NW_SRC).getMask());
507 Assert.assertTrue(match.getField(MatchType.TP_DST).getValue()
508 .equals(flipped.getField(MatchType.TP_SRC).getValue()));
509 Assert.assertTrue(match.getField(MatchType.TP_DST).getMask() == flipped.getField(MatchType.TP_SRC).getMask());
511 Match flipflip = flipped.reverse().reverse();
512 Assert.assertTrue(flipflip.equals(flipped));
517 public void testVlanNone() throws Exception {
518 // The value 0 is used to indicate that no VLAN ID is set
519 short vlan = (short) 0;
520 MatchField field = new MatchField(MatchType.DL_VLAN, vlan);
522 Assert.assertTrue(field != null);
523 Assert.assertTrue(field.getValue().equals(new Short(vlan)));
524 Assert.assertTrue(field.isValid());
528 public void testIntersection() throws UnknownHostException {
529 Short ethType = Short.valueOf((short)0x800);
530 InetAddress ip1 = InetAddress.getByName("1.1.1.1");
531 InetAddress ip2 = InetAddress.getByName("1.1.1.0");
532 InetAddress ipm2 = InetAddress.getByName("255.255.255.0");
533 InetAddress ip3 = InetAddress.getByName("1.3.0.0");
534 InetAddress ipm3 = InetAddress.getByName("255.255.0.0");
535 InetAddress ip4 = InetAddress.getByName("1.3.4.4");
536 InetAddress ipm4 = InetAddress.getByName("255.255.255.0");
538 Match m1 = new Match();
539 m1.setField(MatchType.DL_TYPE, ethType);
540 m1.setField(MatchType.NW_SRC, ip1);
542 Match m2 = new Match();
543 m2.setField(MatchType.DL_TYPE, ethType);
544 m2.setField(MatchType.NW_SRC, ip2, ipm2);
546 Match m3 = new Match();
547 m3.setField(MatchType.DL_TYPE, ethType);
548 m3.setField(MatchType.NW_SRC, ip3, ipm3);
549 m3.setField(MatchType.NW_PROTO, IPProtocols.TCP.byteValue());
551 Match m3r = m3.reverse();
552 Assert.assertTrue(m3.intersetcs(m3r));
554 Assert.assertTrue(m1.intersetcs(m2));
555 Assert.assertTrue(m2.intersetcs(m1));
556 Assert.assertFalse(m1.intersetcs(m3));
557 Assert.assertTrue(m1.intersetcs(m3r));
558 Assert.assertFalse(m3.intersetcs(m1));
559 Assert.assertTrue(m3.intersetcs(m1.reverse()));
560 Assert.assertFalse(m2.intersetcs(m3));
561 Assert.assertFalse(m3.intersetcs(m2));
562 Assert.assertTrue(m2.intersetcs(m3r));
565 Match i = m1.getIntersection(m2);
566 Assert.assertTrue(((Short)i.getField(MatchType.DL_TYPE).getValue()).equals(ethType));
567 // Verify intersection of IP addresses is correct
568 Assert.assertTrue(((InetAddress)i.getField(MatchType.NW_SRC).getValue()).equals(ip1));
569 Assert.assertNull(i.getField(MatchType.NW_SRC).getMask());
572 i = m2.getIntersection(m3);
573 Assert.assertNull(i);
575 Match m4 = new Match();
576 m4.setField(MatchType.DL_TYPE, ethType);
577 m4.setField(MatchType.NW_PROTO, IPProtocols.TCP.byteValue());
578 m3.setField(MatchType.NW_SRC, ip4, ipm4);
579 Assert.assertTrue(m4.intersetcs(m3));
581 // Verify intersection of IP and IP mask addresses is correct
582 Match ii = m3.getIntersection(m4);
583 Assert.assertTrue(((InetAddress)ii.getField(MatchType.NW_SRC).getValue()).equals(ip4));
584 Assert.assertTrue(((InetAddress)ii.getField(MatchType.NW_SRC).getMask()).equals(ipm4));
586 Match m5 = new Match();
587 m5.setField(MatchType.DL_TYPE, ethType);
588 m3.setField(MatchType.NW_SRC, ip3, ipm3);
589 m5.setField(MatchType.NW_PROTO, IPProtocols.UDP.byteValue());
590 Assert.assertFalse(m5.intersetcs(m3));
591 Assert.assertFalse(m5.intersetcs(m4));
592 Assert.assertTrue(m5.intersetcs(m5));
593 Assert.assertFalse(m3.intersetcs(m5));
594 Assert.assertFalse(m4.intersetcs(m5));
597 Match i2 = m4.getIntersection(m3);
598 Assert.assertFalse(i2.getMatches() == 0);
599 Assert.assertFalse(i2.getMatchesList().isEmpty());
600 Assert.assertTrue(((InetAddress)i2.getField(MatchType.NW_SRC).getValue()).equals(ip3));
601 Assert.assertTrue(((InetAddress)i2.getField(MatchType.NW_SRC).getMask()).equals(ipm3));
602 Assert.assertTrue(((Byte)i2.getField(MatchType.NW_PROTO).getValue()).equals(IPProtocols.TCP.byteValue()));
604 byte src[] = {(byte)0, (byte)0xab,(byte)0xbc,(byte)0xcd,(byte)0xde,(byte)0xef};
605 byte dst[] = {(byte)0x10, (byte)0x11,(byte)0x12,(byte)0x13,(byte)0x14,(byte)0x15};
606 Short srcPort = (short)1024;
607 Short dstPort = (short)80;
610 Match m6 = new Match();
611 m6.setField(MatchType.DL_SRC, src);
612 m6.setField(MatchType.DL_DST, dst);
613 m6.setField(MatchType.NW_SRC, ip2, ipm2);
614 m6.setField(MatchType.NW_DST, ip3, ipm3);
615 m6.setField(MatchType.NW_PROTO, IPProtocols.UDP.byteValue());
616 m6.setField(MatchType.TP_SRC, srcPort);
617 m6.setField(MatchType.TP_DST, dstPort);
618 Assert.assertTrue(m6.intersetcs(m6));
619 Assert.assertTrue(m6.getIntersection(m6).equals(m6));
621 // Empty match, represents the universal set (all packets)
622 Match u = new Match();
623 Assert.assertTrue(m6.getIntersection(u).equals(m6));
624 Assert.assertTrue(u.getIntersection(m6).equals(m6));
626 // No intersection with null match, empty set
627 Assert.assertNull(m6.getIntersection(null));