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.utils.EtherTypes;
20 import org.opendaylight.controller.sal.utils.IPProtocols;
21 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
22 import org.opendaylight.controller.sal.utils.NodeCreator;
24 public class MatchTest {
26 public void testMatchCreation() {
27 Node node = NodeCreator.createOFNode(7L);
28 NodeConnector port = NodeConnectorCreator.createOFNodeConnector((short) 6, node);
29 MatchField field = new MatchField(MatchType.IN_PORT, port);
31 Assert.assertTrue(field != null);
32 Assert.assertTrue(field.getType() == MatchType.IN_PORT);
33 Assert.assertTrue((NodeConnector) field.getValue() == port);
34 Assert.assertTrue(field.isValid());
37 field = new MatchField(MatchType.TP_SRC, Long.valueOf(23));
38 Assert.assertFalse(field.isValid());
41 field = new MatchField(MatchType.TP_SRC, (long) 45);
42 Assert.assertFalse(field.isValid());
45 field = new MatchField(MatchType.TP_SRC, 120000);
46 Assert.assertFalse(field.isValid());
48 byte mac[] = { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd, (byte) 11, (byte) 22 };
49 byte mask[] = { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff };
51 field = new MatchField(MatchType.DL_SRC, mac, mask);
52 Assert.assertFalse(field.getValue() == null);
55 field = new MatchField(MatchType.NW_TOS, (byte) 0x22, (byte) 0x3);
56 Assert.assertFalse(field.getValue() == null);
60 public void testMatchSetGet() {
61 Match x = new Match();
63 NodeConnector inPort = NodeConnectorCreator.createOFNodeConnector(val, NodeCreator.createOFNode(1L));
64 x.setField(MatchType.IN_PORT, inPort);
65 Assert.assertTrue(((NodeConnector) x.getField(MatchType.IN_PORT).getValue()).equals(inPort));
66 Assert.assertTrue((Short) ((NodeConnector) x.getField(MatchType.IN_PORT).getValue()).getID() == val);
70 public void testMatchSetGetMAC() {
71 Match x = new Match();
72 byte mac[] = { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd, (byte) 11, (byte) 22 };
73 byte mac2[] = { (byte) 0xaa, (byte) 0xbb, 0, 0, 0, (byte) 0xbb };
74 byte mask1[] = { (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66 };
75 byte mask2[] = { (byte) 0xff, (byte) 0xff, (byte) 0, (byte) 0, (byte) 0, (byte) 0xff };
77 x.setField(MatchType.DL_SRC, mac.clone(), mask1);
78 x.setField(MatchType.DL_DST, mac2.clone(), mask2);
79 Assert.assertTrue(Arrays.equals(mac, (byte[]) x.getField(MatchType.DL_SRC).getValue()));
80 Assert.assertFalse(Arrays.equals((byte[]) x.getField(MatchType.DL_SRC).getValue(),
81 (byte[]) x.getField(MatchType.DL_DST).getValue()));
82 Assert.assertFalse(x.getField(MatchType.DL_SRC).getBitMask() == x.getField(MatchType.DL_DST).getBitMask());
84 x.setField(new MatchField(MatchType.DL_DST, mac.clone(), mask1));
85 Assert.assertTrue(Arrays.equals((byte[]) x.getField(MatchType.DL_SRC).getValue(),
86 (byte[]) x.getField(MatchType.DL_DST).getValue()));
90 public void testMatchSetGetNWAddr() throws UnknownHostException {
91 Match x = new Match();
92 String ip = "172.20.231.23";
93 InetAddress address = InetAddress.getByName(ip);
94 InetAddress mask = InetAddress.getByName("255.255.0.0");
96 x.setField(MatchType.NW_SRC, address, mask);
97 Assert.assertTrue(ip.equals(((InetAddress) x.getField(MatchType.NW_SRC).getValue()).getHostAddress()));
98 Assert.assertTrue(x.getField(MatchType.NW_SRC).getMask().equals(mask));
102 public void testMatchSetGetEtherType() throws UnknownHostException {
103 Match x = new Match();
105 x.setField(MatchType.DL_TYPE, EtherTypes.QINQ.shortValue(), (short) 0xffff);
106 Assert.assertTrue(((Short) x.getField(MatchType.DL_TYPE).getValue()).equals(EtherTypes.QINQ.shortValue()));
107 Assert.assertFalse(x.getField(MatchType.DL_TYPE).getValue() == EtherTypes.QINQ);
108 Assert.assertFalse(x.getField(MatchType.DL_TYPE).getValue().equals(EtherTypes.QINQ));
110 x.setField(MatchType.DL_TYPE, EtherTypes.LLDP.shortValue(), (short) 0xffff);
111 Assert.assertTrue(((Short) x.getField(MatchType.DL_TYPE).getValue()).equals(EtherTypes.LLDP.shortValue()));
112 Assert.assertFalse(x.getField(MatchType.DL_TYPE).equals(EtherTypes.LLDP.intValue()));
116 public void testSetGetNwTos() {
117 Match x = new Match();
118 x.setField(MatchType.NW_TOS, (byte) 0xb, (byte) 0xf);
120 Byte t = new Byte((byte) 0xb);
122 Object o = x.getField(MatchType.NW_TOS).getValue();
123 Assert.assertTrue(o.equals(t));
124 Assert.assertTrue(o.equals((byte) 0xb));
128 public void testSetGetNwProto() {
129 Match x = new Match();
130 byte proto = (byte) 199;
131 x.setField(MatchType.NW_PROTO, proto, (byte) 0xff);
133 Object o = x.getField(MatchType.NW_PROTO).getValue();
134 Assert.assertTrue(o.equals(proto));
138 public void testSetTpSrc() {
139 // Minimum value validation.
140 Match match = new Match();
142 match.setField(MatchType.TP_SRC, tp_src);
144 Object o = match.getField(MatchType.TP_SRC).getValue();
145 Assert.assertTrue(o.equals(tp_src));
147 // Maximum value validation.
149 tp_src = (short) 0xffff;
150 match.setField(MatchType.TP_SRC, tp_src);
152 o = match.getField(MatchType.TP_SRC).getValue();
153 Assert.assertTrue(o.equals(tp_src));
157 public void testSetTpDst() {
158 // Minimum value validation.
159 Match match = new Match();
161 match.setField(MatchType.TP_DST, tp_dst);
163 Object o = match.getField(MatchType.TP_DST).getValue();
164 Assert.assertTrue(o.equals(tp_dst));
166 // Maximum value validation.
168 tp_dst = (short) 0xffff;
169 match.setField(MatchType.TP_DST, tp_dst);
171 o = match.getField(MatchType.TP_DST).getValue();
172 Assert.assertTrue(o.equals(tp_dst));
176 public void testMatchMask() {
177 Match x = new Match();
178 NodeConnector inPort = NodeConnectorCreator.createOFNodeConnector((short) 6, NodeCreator.createOFNode(3L));
179 x.setField(MatchType.IN_PORT, inPort);
180 x.setField(MatchType.DL_VLAN, (short) 28, (short) 0xfff);
181 Assert.assertFalse(x.getMatches() == 0);
182 Assert.assertTrue(x.getMatches() == (MatchType.IN_PORT.getIndex() | MatchType.DL_VLAN.getIndex()));
186 public void testMatchBitMask() {
187 byte mac[] = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 22, (byte) 12 };
188 byte mask[] = { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0 };
189 NodeConnector inPort = NodeConnectorCreator.createOFNodeConnector((short) 4095, NodeCreator.createOFNode(7L));
191 MatchField x = new MatchField(MatchType.IN_PORT, inPort);
192 Assert.assertTrue((x.getMask()) == null);
194 x = new MatchField(MatchType.DL_VLAN, (short) 255, (short) 0xff);
195 Assert.assertTrue(x.getBitMask() == 0xff);
197 x = new MatchField(MatchType.DL_SRC, mac, mask);
198 Assert.assertTrue(x.getMask().equals(mask));
199 Assert.assertTrue(x.getBitMask() == 0xffffffffff00L);
203 public void testNullMask() {
204 byte mac[] = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 22, (byte) 12 };
205 NodeConnector inPort = NodeConnectorCreator.createOFNodeConnector((short) 2000, NodeCreator.createOFNode(7L));
207 MatchField x = new MatchField(MatchType.IN_PORT, inPort);
208 Assert.assertTrue(x.getBitMask() == 0);
210 x = new MatchField(MatchType.NW_PROTO, (byte) 17);
211 Assert.assertTrue(x.getBitMask() == 0xff);
213 x = new MatchField(MatchType.DL_VLAN, (short) 255);
214 Assert.assertTrue(x.getBitMask() == 0xfff);
216 x = new MatchField(MatchType.DL_SRC, mac);
217 Assert.assertTrue(x.getBitMask() == 0xffffffffffffL);
221 public void testEquality() throws Exception {
222 Node node = NodeCreator.createOFNode(7L);
223 NodeConnector port = NodeConnectorCreator.createOFNodeConnector((short) 24, node);
224 NodeConnector port2 = NodeConnectorCreator.createOFNodeConnector((short) 24, node);
225 byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
226 byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
227 byte srcMac2[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
228 byte dstMac2[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
229 InetAddress srcIP = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
230 InetAddress dstIP = InetAddress.getByName("2001:420:281:1004:e123:e688:d655:a1b0");
231 InetAddress ipMask = InetAddress.getByName("ffff:ffff:ffff:ffff:0:0:0:0");
232 InetAddress ipMaskd = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
233 InetAddress srcIP2 = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
234 InetAddress dstIP2 = InetAddress.getByName("2001:420:281:1004:e123:e688:d655:a1b0");
235 InetAddress ipMask2 = InetAddress.getByName("ffff:ffff:ffff:ffff:0:0:0:0");
236 InetAddress ipMaskd2 = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
237 short ethertype = EtherTypes.IPv6.shortValue();
238 short ethertype2 = EtherTypes.IPv6.shortValue();
239 short vlan = (short) 27, vlan2 = (short) 27;
240 byte vlanPr = (byte) 3, vlanPr2 = (byte) 3;
241 Byte tos = 4, tos2 = 4;
242 byte proto = IPProtocols.UDP.byteValue(), proto2 = IPProtocols.UDP.byteValue();
243 short src = (short) 5500, src2 = (short) 5500;
244 short dst = 80, dst2 = 80;
247 * Create a SAL Flow aFlow
249 Match match1 = new Match();
250 Match match2 = new Match();
251 match1.setField(MatchType.IN_PORT, port);
252 match1.setField(MatchType.DL_SRC, srcMac);
253 match1.setField(MatchType.DL_DST, dstMac);
254 match1.setField(MatchType.DL_TYPE, ethertype);
255 match1.setField(MatchType.DL_VLAN, vlan);
256 match1.setField(MatchType.DL_VLAN_PR, vlanPr);
257 match1.setField(MatchType.NW_SRC, srcIP, ipMask);
258 match1.setField(MatchType.NW_DST, dstIP, ipMaskd);
259 match1.setField(MatchType.NW_TOS, tos);
260 match1.setField(MatchType.NW_PROTO, proto);
261 match1.setField(MatchType.TP_SRC, src);
262 match1.setField(MatchType.TP_DST, dst);
264 match2.setField(MatchType.IN_PORT, port2);
265 match2.setField(MatchType.DL_SRC, srcMac2);
266 match2.setField(MatchType.DL_DST, dstMac2);
267 match2.setField(MatchType.DL_TYPE, ethertype2);
268 match2.setField(MatchType.DL_VLAN, vlan2);
269 match2.setField(MatchType.DL_VLAN_PR, vlanPr2);
270 match2.setField(MatchType.NW_SRC, srcIP2, ipMask2);
271 match2.setField(MatchType.NW_DST, dstIP2, ipMaskd2);
272 match2.setField(MatchType.NW_TOS, tos2);
273 match2.setField(MatchType.NW_PROTO, proto2);
274 match2.setField(MatchType.TP_SRC, src2);
275 match2.setField(MatchType.TP_DST, dst2);
277 Assert.assertTrue(match1.equals(match2));
279 // Make sure all values are equals
280 for (MatchType type : MatchType.values()) {
281 if (match1.isPresent(type)) {
282 Assert.assertTrue(match1.getField(type).equals(match2.getField(type)));
286 // Make none of the fields couples are pointing to the same reference
287 MatchField a = null, b = null;
288 for (MatchType type : MatchType.values()) {
289 a = match1.getField(type);
290 b = match2.getField(type);
291 if (a != null && b != null) {
292 Assert.assertFalse(a == b);
298 public void testEqualityNetMask() throws Exception {
300 InetAddress srcIP = InetAddress.getByName("1.1.1.1");
301 InetAddress ipMask = InetAddress.getByName("255.255.255.255");
302 InetAddress srcIP2 = InetAddress.getByName("1.1.1.1");
303 InetAddress ipMask2 = null;
304 short ethertype = EtherTypes.IPv4.shortValue();
305 short ethertype2 = EtherTypes.IPv4.shortValue();
308 * Create a SAL Flow aFlow
310 Match match1 = new Match();
311 Match match2 = new Match();
313 match1.setField(MatchType.DL_TYPE, ethertype);
314 match1.setField(MatchType.NW_SRC, srcIP, ipMask);
316 match2.setField(MatchType.DL_TYPE, ethertype2);
317 match2.setField(MatchType.NW_SRC, srcIP2, ipMask2);
319 Assert.assertTrue(match1.equals(match2));
321 ipMask2 = InetAddress.getByName("255.255.255.255");
322 match2.setField(MatchType.NW_SRC, srcIP2, ipMask2);
324 srcIP = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
325 srcIP2 = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
327 ipMask2 = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
328 ethertype = EtherTypes.IPv6.shortValue();
329 ethertype2 = EtherTypes.IPv6.shortValue();
331 match1.setField(MatchType.DL_TYPE, ethertype);
332 match1.setField(MatchType.NW_SRC, srcIP, ipMask);
334 match2.setField(MatchType.DL_TYPE, ethertype2);
335 match2.setField(MatchType.NW_SRC, srcIP2, ipMask2);
337 Assert.assertTrue(match1.equals(match2));
341 public void testHashCodeWithReverseMatch() throws Exception {
342 InetAddress srcIP1 = InetAddress.getByName("1.1.1.1");
343 InetAddress ipMask1 = InetAddress.getByName("255.255.255.255");
344 InetAddress srcIP2 = InetAddress.getByName("2.2.2.2");
345 InetAddress ipMask2 = InetAddress.getByName("255.255.255.255");
346 MatchField field1 = new MatchField(MatchType.NW_SRC, srcIP1, ipMask1);
347 MatchField field2 = new MatchField(MatchType.NW_DST, srcIP2, ipMask2);
348 Match match1 = new Match();
349 match1.setField(field1);
350 match1.setField(field2);
351 Match match2 = match1.reverse();
352 Assert.assertFalse(match1.hashCode() == match2.hashCode());
356 public void testHashCode() throws Exception {
357 byte srcMac1[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
358 byte srcMac2[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
359 byte dstMac1[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
360 byte dstMac2[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
361 short ethertype = EtherTypes.IPv4.shortValue();
362 short ethertype2 = EtherTypes.IPv4.shortValue();
363 InetAddress srcIP1 = InetAddress.getByName("1.1.1.1");
364 InetAddress ipMask1 = InetAddress.getByName("255.255.255.255");
365 InetAddress srcIP2 = InetAddress.getByName("1.1.1.1");
366 InetAddress ipMask2 = InetAddress.getByName("255.255.255.255");
368 Match match1 = new Match();
369 Match match2 = new Match();
371 MatchField field1 = new MatchField(MatchType.DL_SRC, srcMac1);
372 MatchField field2 = new MatchField(MatchType.DL_SRC, srcMac2);
373 Assert.assertTrue(field1.hashCode() == field2.hashCode());
375 match1.setField(field1);
376 match2.setField(field2);
377 Assert.assertTrue(match1.hashCode() == match2.hashCode());
379 MatchField field3 = new MatchField(MatchType.DL_DST, dstMac1);
380 MatchField field4 = new MatchField(MatchType.DL_DST, dstMac2);
381 Assert.assertTrue(field3.hashCode() == field4.hashCode());
383 match1.setField(field3);
384 match2.setField(field4);
385 Assert.assertTrue(match1.hashCode() == match2.hashCode());
387 MatchField field5 = new MatchField(MatchType.DL_TYPE, ethertype);
388 MatchField field6 = new MatchField(MatchType.DL_TYPE, ethertype2);
389 Assert.assertTrue(field5.hashCode() == field6.hashCode());
391 match1.setField(field5);
392 match2.setField(field6);
393 Assert.assertTrue(match1.hashCode() == match2 .hashCode());
395 MatchField field7 = new MatchField(MatchType.NW_SRC, srcIP1, ipMask1);
396 MatchField field8 = new MatchField(MatchType.NW_SRC, srcIP2, ipMask2);
397 Assert.assertTrue(field7.hashCode() == field8.hashCode());
399 match1.setField(field7);
400 match2.setField(field8);
401 Assert.assertTrue(match1.hashCode() == match2.hashCode());
406 public void testCloning() throws Exception {
407 Node node = NodeCreator.createOFNode(7L);
408 NodeConnector port = NodeConnectorCreator.createOFNodeConnector((short) 24, node);
409 byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
410 byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
411 InetAddress srcIP = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
412 InetAddress dstIP = InetAddress.getByName("2001:420:281:1004:e123:e688:d655:a1b0");
413 InetAddress ipMasks = InetAddress.getByName("ffff:ffff:ffff:ffff:0:0:0:0");
414 InetAddress ipMaskd = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
415 short ethertype = EtherTypes.IPv6.shortValue();
416 short vlan = (short) 27;
417 byte vlanPr = (byte) 3;
419 byte proto = IPProtocols.UDP.byteValue();
420 short src = (short) 5500;
424 * Create a SAL Flow aFlow
426 Match match = new Match();
427 match.setField(MatchType.IN_PORT, port);
428 match.setField(MatchType.DL_SRC, srcMac);
429 match.setField(MatchType.DL_DST, dstMac);
430 match.setField(MatchType.DL_TYPE, ethertype);
431 match.setField(MatchType.DL_VLAN, vlan);
432 match.setField(MatchType.DL_VLAN_PR, vlanPr);
433 match.setField(MatchType.NW_SRC, srcIP, ipMasks);
434 match.setField(MatchType.NW_DST, dstIP, ipMaskd);
435 match.setField(MatchType.NW_TOS, tos);
436 match.setField(MatchType.NW_PROTO, proto);
437 match.setField(MatchType.TP_SRC, src);
438 match.setField(MatchType.TP_DST, dst);
440 Match cloned = match.clone();
442 // Make sure all values are equals
443 for (MatchType type : MatchType.values()) {
444 if (match.isPresent(type)) {
445 if (!match.getField(type).equals(cloned.getField(type))) {
446 Assert.assertTrue(match.getField(type).equals(cloned.getField(type)));
451 // Make sure none of the fields couples are pointing to the same
453 MatchField a = null, b = null;
454 for (MatchType type : MatchType.values()) {
455 a = match.getField(type);
456 b = cloned.getField(type);
457 if (a != null && b != null) {
458 Assert.assertFalse(a == b);
462 Assert.assertTrue(match.equals(cloned));
464 Assert.assertFalse(match.getField(MatchType.DL_SRC) == cloned.getField(MatchType.DL_SRC));
465 Assert.assertFalse(match.getField(MatchType.NW_DST) == cloned.getField(MatchType.NW_DST));
466 Assert.assertTrue(match.getField(MatchType.NW_DST).getMask()
467 .equals(cloned.getField(MatchType.NW_DST).getMask()));
468 Assert.assertTrue(match.hashCode() == cloned.hashCode());
472 public void testFlip() throws Exception {
473 Node node = NodeCreator.createOFNode(7L);
474 NodeConnector port = NodeConnectorCreator.createOFNodeConnector((short) 24, node);
475 byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
476 byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
477 InetAddress srcIP = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
478 InetAddress dstIP = InetAddress.getByName("2001:420:281:1004:e123:e688:d655:a1b0");
479 InetAddress ipMasks = InetAddress.getByName("ffff:ffff:ffff:ffff:0:0:0:0");
480 InetAddress ipMaskd = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
481 short ethertype = EtherTypes.IPv6.shortValue();
482 short vlan = (short) 27;
483 byte vlanPr = (byte) 3;
485 byte proto = IPProtocols.UDP.byteValue();
486 short src = (short) 5500;
490 * Create a SAL Flow aFlow
492 Match match = new Match();
493 match.setField(MatchType.IN_PORT, port);
494 match.setField(MatchType.DL_SRC, srcMac);
495 match.setField(MatchType.DL_DST, dstMac);
496 match.setField(MatchType.DL_TYPE, ethertype);
497 match.setField(MatchType.DL_VLAN, vlan);
498 match.setField(MatchType.DL_VLAN_PR, vlanPr);
499 match.setField(MatchType.NW_SRC, srcIP, ipMasks);
500 match.setField(MatchType.NW_DST, dstIP, ipMaskd);
501 match.setField(MatchType.NW_TOS, tos);
502 match.setField(MatchType.NW_PROTO, proto);
503 match.setField(MatchType.TP_SRC, src);
504 match.setField(MatchType.TP_DST, dst);
506 Match flipped = match.reverse();
508 Assert.assertTrue(match.getField(MatchType.DL_TYPE).equals(flipped.getField(MatchType.DL_TYPE)));
509 Assert.assertTrue(match.getField(MatchType.DL_VLAN).equals(flipped.getField(MatchType.DL_VLAN)));
511 Assert.assertTrue(match.getField(MatchType.DL_DST).getValue()
512 .equals(flipped.getField(MatchType.DL_SRC).getValue()));
513 Assert.assertTrue(match.getField(MatchType.DL_DST).getMask() == flipped.getField(MatchType.DL_SRC).getMask());
515 Assert.assertTrue(match.getField(MatchType.NW_DST).getValue()
516 .equals(flipped.getField(MatchType.NW_SRC).getValue()));
517 Assert.assertTrue(match.getField(MatchType.NW_DST).getMask() == flipped.getField(MatchType.NW_SRC).getMask());
519 Assert.assertTrue(match.getField(MatchType.TP_DST).getValue()
520 .equals(flipped.getField(MatchType.TP_SRC).getValue()));
521 Assert.assertTrue(match.getField(MatchType.TP_DST).getMask() == flipped.getField(MatchType.TP_SRC).getMask());
523 Match flipflip = flipped.reverse().reverse();
524 Assert.assertTrue(flipflip.equals(flipped));
529 public void testVlanNone() throws Exception {
530 // The value 0 is used to indicate that no VLAN ID is set
531 short vlan = (short) 0;
532 MatchField field = new MatchField(MatchType.DL_VLAN, vlan);
534 Assert.assertTrue(field != null);
535 Assert.assertTrue(field.getValue().equals(new Short(vlan)));
536 Assert.assertTrue(field.isValid());
540 public void testIntersection() throws UnknownHostException {
541 Short ethType = Short.valueOf((short)0x800);
542 InetAddress ip1 = InetAddress.getByName("1.1.1.1");
543 InetAddress ip2 = InetAddress.getByName("1.1.1.0");
544 InetAddress ipm2 = InetAddress.getByName("255.255.255.0");
545 InetAddress ip3 = InetAddress.getByName("1.3.0.0");
546 InetAddress ipm3 = InetAddress.getByName("255.255.0.0");
547 InetAddress ip4 = InetAddress.getByName("1.3.4.4");
548 InetAddress ipm4 = InetAddress.getByName("255.255.255.0");
550 Match m1 = new Match();
551 m1.setField(MatchType.DL_TYPE, ethType);
552 m1.setField(MatchType.NW_SRC, ip1);
554 Match m2 = new Match();
555 m2.setField(MatchType.DL_TYPE, ethType);
556 m2.setField(MatchType.NW_SRC, ip2, ipm2);
558 Match m3 = new Match();
559 m3.setField(MatchType.DL_TYPE, ethType);
560 m3.setField(MatchType.NW_SRC, ip3, ipm3);
561 m3.setField(MatchType.NW_PROTO, IPProtocols.TCP.byteValue());
563 Match m3r = m3.reverse();
564 Assert.assertTrue(m3.intersetcs(m3r));
566 Assert.assertTrue(m1.intersetcs(m2));
567 Assert.assertTrue(m2.intersetcs(m1));
568 Assert.assertFalse(m1.intersetcs(m3));
569 Assert.assertTrue(m1.intersetcs(m3r));
570 Assert.assertFalse(m3.intersetcs(m1));
571 Assert.assertTrue(m3.intersetcs(m1.reverse()));
572 Assert.assertFalse(m2.intersetcs(m3));
573 Assert.assertFalse(m3.intersetcs(m2));
574 Assert.assertTrue(m2.intersetcs(m3r));
577 Match i = m1.getIntersection(m2);
578 Assert.assertTrue(((Short)i.getField(MatchType.DL_TYPE).getValue()).equals(ethType));
579 // Verify intersection of IP addresses is correct
580 Assert.assertTrue(((InetAddress)i.getField(MatchType.NW_SRC).getValue()).equals(ip1));
581 Assert.assertNull(i.getField(MatchType.NW_SRC).getMask());
584 i = m2.getIntersection(m3);
585 Assert.assertNull(i);
587 Match m4 = new Match();
588 m4.setField(MatchType.DL_TYPE, ethType);
589 m4.setField(MatchType.NW_PROTO, IPProtocols.TCP.byteValue());
590 m3.setField(MatchType.NW_SRC, ip4, ipm4);
591 Assert.assertTrue(m4.intersetcs(m3));
593 // Verify intersection of IP and IP mask addresses is correct
594 Match ii = m3.getIntersection(m4);
595 Assert.assertTrue(((InetAddress)ii.getField(MatchType.NW_SRC).getValue()).equals(ip4));
596 Assert.assertTrue(((InetAddress)ii.getField(MatchType.NW_SRC).getMask()).equals(ipm4));
598 Match m5 = new Match();
599 m5.setField(MatchType.DL_TYPE, ethType);
600 m3.setField(MatchType.NW_SRC, ip3, ipm3);
601 m5.setField(MatchType.NW_PROTO, IPProtocols.UDP.byteValue());
602 Assert.assertFalse(m5.intersetcs(m3));
603 Assert.assertFalse(m5.intersetcs(m4));
604 Assert.assertTrue(m5.intersetcs(m5));
605 Assert.assertFalse(m3.intersetcs(m5));
606 Assert.assertFalse(m4.intersetcs(m5));
609 Match i2 = m4.getIntersection(m3);
610 Assert.assertFalse(i2.getMatches() == 0);
611 Assert.assertFalse(i2.getMatchesList().isEmpty());
612 Assert.assertTrue(((InetAddress)i2.getField(MatchType.NW_SRC).getValue()).equals(ip3));
613 Assert.assertTrue(((InetAddress)i2.getField(MatchType.NW_SRC).getMask()).equals(ipm3));
614 Assert.assertTrue(((Byte)i2.getField(MatchType.NW_PROTO).getValue()).equals(IPProtocols.TCP.byteValue()));
616 byte src[] = {(byte)0, (byte)0xab,(byte)0xbc,(byte)0xcd,(byte)0xde,(byte)0xef};
617 byte dst[] = {(byte)0x10, (byte)0x11,(byte)0x12,(byte)0x13,(byte)0x14,(byte)0x15};
618 Short srcPort = (short)1024;
619 Short dstPort = (short)80;
622 Match m6 = new Match();
623 m6.setField(MatchType.DL_SRC, src);
624 m6.setField(MatchType.DL_DST, dst);
625 m6.setField(MatchType.NW_SRC, ip2, ipm2);
626 m6.setField(MatchType.NW_DST, ip3, ipm3);
627 m6.setField(MatchType.NW_PROTO, IPProtocols.UDP.byteValue());
628 m6.setField(MatchType.TP_SRC, srcPort);
629 m6.setField(MatchType.TP_DST, dstPort);
630 Assert.assertTrue(m6.intersetcs(m6));
631 Assert.assertTrue(m6.getIntersection(m6).equals(m6));
633 // Empty match, represents the universal set (all packets)
634 Match u = new Match();
635 Assert.assertTrue(m6.getIntersection(u).equals(m6));
636 Assert.assertTrue(u.getIntersection(m6).equals(m6));
638 // No intersection with null match, empty set
639 Assert.assertNull(m6.getIntersection(null));