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;
14 import java.util.List;
16 import org.junit.Assert;
17 import org.junit.Test;
18 import org.opendaylight.controller.sal.core.Node;
19 import org.opendaylight.controller.sal.core.NodeConnector;
20 import org.opendaylight.controller.sal.core.Property;
21 import org.opendaylight.controller.sal.core.Tables;
22 import org.opendaylight.controller.sal.core.Tier;
23 import org.opendaylight.controller.sal.utils.EtherTypes;
24 import org.opendaylight.controller.sal.utils.IPProtocols;
25 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
26 import org.opendaylight.controller.sal.utils.NodeCreator;
28 public class MatchTest {
30 public void testMatchCreation() {
31 Node node = NodeCreator.createOFNode(7L);
32 NodeConnector port = NodeConnectorCreator.createOFNodeConnector((short) 6, node);
33 MatchField field = new MatchField(MatchType.IN_PORT, port);
35 Assert.assertTrue(field != null);
36 Assert.assertTrue(field.getType() == MatchType.IN_PORT);
37 Assert.assertTrue((NodeConnector) field.getValue() == port);
38 Assert.assertTrue(field.isValid());
41 field = new MatchField(MatchType.TP_SRC, Long.valueOf(23));
42 Assert.assertFalse(field.isValid());
45 field = new MatchField(MatchType.TP_SRC, (long) 45);
46 Assert.assertFalse(field.isValid());
49 field = new MatchField(MatchType.TP_SRC, 120000);
50 Assert.assertFalse(field.isValid());
52 byte mac[] = { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd, (byte) 11, (byte) 22 };
53 byte mask[] = { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff };
55 field = new MatchField(MatchType.DL_SRC, mac, mask);
56 Assert.assertFalse(field.getValue() == null);
59 field = new MatchField(MatchType.NW_TOS, (byte) 0x22, (byte) 0x3);
60 Assert.assertFalse(field.getValue() == null);
64 public void testMatchSetGet() {
65 Match x = new Match();
67 NodeConnector inPort = NodeConnectorCreator.createOFNodeConnector(val, NodeCreator.createOFNode(1L));
68 x.setField(MatchType.IN_PORT, inPort);
69 Assert.assertTrue(((NodeConnector) x.getField(MatchType.IN_PORT).getValue()).equals(inPort));
70 Assert.assertTrue((Short) ((NodeConnector) x.getField(MatchType.IN_PORT).getValue()).getID() == val);
74 public void testMatchSetGetMAC() {
75 Match x = new Match();
76 byte mac[] = { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd, (byte) 11, (byte) 22 };
77 byte mac2[] = { (byte) 0xaa, (byte) 0xbb, 0, 0, 0, (byte) 0xbb };
78 byte mask1[] = { (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66 };
79 byte mask2[] = { (byte) 0xff, (byte) 0xff, (byte) 0, (byte) 0, (byte) 0, (byte) 0xff };
81 x.setField(MatchType.DL_SRC, mac.clone(), mask1);
82 x.setField(MatchType.DL_DST, mac2.clone(), mask2);
83 Assert.assertTrue(Arrays.equals(mac, (byte[]) x.getField(MatchType.DL_SRC).getValue()));
84 Assert.assertFalse(Arrays.equals((byte[]) x.getField(MatchType.DL_SRC).getValue(),
85 (byte[]) x.getField(MatchType.DL_DST).getValue()));
86 Assert.assertFalse(x.getField(MatchType.DL_SRC).getBitMask() == x.getField(MatchType.DL_DST).getBitMask());
88 x.setField(new MatchField(MatchType.DL_DST, mac.clone(), mask1));
89 Assert.assertTrue(Arrays.equals((byte[]) x.getField(MatchType.DL_SRC).getValue(),
90 (byte[]) x.getField(MatchType.DL_DST).getValue()));
94 public void testMatchSetGetNWAddr() throws UnknownHostException {
95 Match x = new Match();
96 String ip = "172.20.231.23";
97 InetAddress address = InetAddress.getByName(ip);
98 InetAddress mask = InetAddress.getByName("255.255.0.0");
100 x.setField(MatchType.NW_SRC, address, mask);
101 Assert.assertTrue(ip.equals(((InetAddress) x.getField(MatchType.NW_SRC).getValue()).getHostAddress()));
102 Assert.assertTrue(x.getField(MatchType.NW_SRC).getMask().equals(mask));
106 public void testMatchSetGetEtherType() throws UnknownHostException {
107 Match x = new Match();
109 x.setField(MatchType.DL_TYPE, EtherTypes.QINQ.shortValue(), (short) 0xffff);
110 Assert.assertTrue(((Short) x.getField(MatchType.DL_TYPE).getValue()).equals(EtherTypes.QINQ.shortValue()));
111 Assert.assertFalse(x.getField(MatchType.DL_TYPE).getValue() == EtherTypes.QINQ);
112 Assert.assertFalse(x.getField(MatchType.DL_TYPE).getValue().equals(EtherTypes.QINQ));
114 x.setField(MatchType.DL_TYPE, EtherTypes.LLDP.shortValue(), (short) 0xffff);
115 Assert.assertTrue(((Short) x.getField(MatchType.DL_TYPE).getValue()).equals(EtherTypes.LLDP.shortValue()));
116 Assert.assertFalse(x.getField(MatchType.DL_TYPE).equals(EtherTypes.LLDP.intValue()));
120 public void testSetGetNwTos() {
121 Match x = new Match();
122 x.setField(MatchType.NW_TOS, (byte) 0xb, (byte) 0xf);
124 Byte t = new Byte((byte) 0xb);
126 Object o = x.getField(MatchType.NW_TOS).getValue();
127 Assert.assertTrue(o.equals(t));
128 Assert.assertTrue(o.equals((byte) 0xb));
132 public void testSetGetNwProto() {
133 Match x = new Match();
134 byte proto = (byte) 199;
135 x.setField(MatchType.NW_PROTO, proto, (byte) 0xff);
137 Object o = x.getField(MatchType.NW_PROTO).getValue();
138 Assert.assertTrue(o.equals(proto));
142 public void testSetTpSrc() {
143 // Minimum value validation.
144 Match match = new Match();
146 match.setField(MatchType.TP_SRC, tp_src);
148 Object o = match.getField(MatchType.TP_SRC).getValue();
149 Assert.assertTrue(o.equals(tp_src));
151 // Maximum value validation.
153 tp_src = (short) 0xffff;
154 match.setField(MatchType.TP_SRC, tp_src);
156 o = match.getField(MatchType.TP_SRC).getValue();
157 Assert.assertTrue(o.equals(tp_src));
161 public void testSetTpDst() {
162 // Minimum value validation.
163 Match match = new Match();
165 match.setField(MatchType.TP_DST, tp_dst);
167 Object o = match.getField(MatchType.TP_DST).getValue();
168 Assert.assertTrue(o.equals(tp_dst));
170 // Maximum value validation.
172 tp_dst = (short) 0xffff;
173 match.setField(MatchType.TP_DST, tp_dst);
175 o = match.getField(MatchType.TP_DST).getValue();
176 Assert.assertTrue(o.equals(tp_dst));
180 public void testMatchMask() {
181 Match x = new Match();
182 NodeConnector inPort = NodeConnectorCreator.createOFNodeConnector((short) 6, NodeCreator.createOFNode(3L));
183 x.setField(MatchType.IN_PORT, inPort);
184 x.setField(MatchType.DL_VLAN, (short) 28, (short) 0xfff);
185 Assert.assertFalse(x.getMatches() == 0);
186 Assert.assertTrue(x.getMatches() == (MatchType.IN_PORT.getIndex() | MatchType.DL_VLAN.getIndex()));
190 public void testMatchBitMask() {
191 byte mac[] = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 22, (byte) 12 };
192 byte mask[] = { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0 };
193 NodeConnector inPort = NodeConnectorCreator.createOFNodeConnector((short) 4095, NodeCreator.createOFNode(7L));
195 MatchField x = new MatchField(MatchType.IN_PORT, inPort);
196 Assert.assertTrue((x.getMask()) == null);
198 x = new MatchField(MatchType.DL_VLAN, (short) 255, (short) 0xff);
199 Assert.assertTrue(x.getBitMask() == 0xff);
201 x = new MatchField(MatchType.DL_SRC, mac, mask);
202 Assert.assertTrue(x.getMask().equals(mask));
203 Assert.assertTrue(x.getBitMask() == 0xffffffffff00L);
207 public void testNullMask() {
208 byte mac[] = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 22, (byte) 12 };
209 NodeConnector inPort = NodeConnectorCreator.createOFNodeConnector((short) 2000, NodeCreator.createOFNode(7L));
211 MatchField x = new MatchField(MatchType.IN_PORT, inPort);
212 Assert.assertTrue(x.getBitMask() == 0);
214 x = new MatchField(MatchType.NW_PROTO, (byte) 17);
215 Assert.assertTrue(x.getBitMask() == 0xff);
217 x = new MatchField(MatchType.DL_VLAN, (short) 255);
218 Assert.assertTrue(x.getBitMask() == 0xfff);
220 x = new MatchField(MatchType.DL_SRC, mac);
221 Assert.assertTrue(x.getBitMask() == 0xffffffffffffL);
225 public void testEquality() throws Exception {
226 Node node = NodeCreator.createOFNode(7L);
227 NodeConnector port = NodeConnectorCreator.createOFNodeConnector((short) 24, node);
228 NodeConnector port2 = NodeConnectorCreator.createOFNodeConnector((short) 24, node);
229 byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
230 byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
231 byte srcMac2[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
232 byte dstMac2[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
233 InetAddress srcIP = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
234 InetAddress dstIP = InetAddress.getByName("2001:420:281:1004:e123:e688:d655:a1b0");
235 InetAddress ipMask = InetAddress.getByName("ffff:ffff:ffff:ffff:0:0:0:0");
236 InetAddress ipMaskd = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
237 InetAddress srcIP2 = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
238 InetAddress dstIP2 = InetAddress.getByName("2001:420:281:1004:e123:e688:d655:a1b0");
239 InetAddress ipMask2 = InetAddress.getByName("ffff:ffff:ffff:ffff:0:0:0:0");
240 InetAddress ipMaskd2 = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
241 short ethertype = EtherTypes.IPv6.shortValue();
242 short ethertype2 = EtherTypes.IPv6.shortValue();
243 short vlan = (short) 27, vlan2 = (short) 27;
244 byte vlanPr = (byte) 3, vlanPr2 = (byte) 3;
245 Byte tos = 4, tos2 = 4;
246 byte proto = IPProtocols.UDP.byteValue(), proto2 = IPProtocols.UDP.byteValue();
247 short src = (short) 5500, src2 = (short) 5500;
248 short dst = 80, dst2 = 80;
251 * Create a SAL Flow aFlow
253 Match match1 = new Match();
254 Match match2 = new Match();
255 match1.setField(MatchType.IN_PORT, port);
256 match1.setField(MatchType.DL_SRC, srcMac);
257 match1.setField(MatchType.DL_DST, dstMac);
258 match1.setField(MatchType.DL_TYPE, ethertype);
259 match1.setField(MatchType.DL_VLAN, vlan);
260 match1.setField(MatchType.DL_VLAN_PR, vlanPr);
261 match1.setField(MatchType.NW_SRC, srcIP, ipMask);
262 match1.setField(MatchType.NW_DST, dstIP, ipMaskd);
263 match1.setField(MatchType.NW_TOS, tos);
264 match1.setField(MatchType.NW_PROTO, proto);
265 match1.setField(MatchType.TP_SRC, src);
266 match1.setField(MatchType.TP_DST, dst);
268 match2.setField(MatchType.IN_PORT, port2);
269 match2.setField(MatchType.DL_SRC, srcMac2);
270 match2.setField(MatchType.DL_DST, dstMac2);
271 match2.setField(MatchType.DL_TYPE, ethertype2);
272 match2.setField(MatchType.DL_VLAN, vlan2);
273 match2.setField(MatchType.DL_VLAN_PR, vlanPr2);
274 match2.setField(MatchType.NW_SRC, srcIP2, ipMask2);
275 match2.setField(MatchType.NW_DST, dstIP2, ipMaskd2);
276 match2.setField(MatchType.NW_TOS, tos2);
277 match2.setField(MatchType.NW_PROTO, proto2);
278 match2.setField(MatchType.TP_SRC, src2);
279 match2.setField(MatchType.TP_DST, dst2);
281 Assert.assertTrue(match1.equals(match2));
283 // Make sure all values are equals
284 for (MatchType type : MatchType.values()) {
285 if (match1.isPresent(type)) {
286 Assert.assertTrue(match1.getField(type).equals(match2.getField(type)));
290 // Make none of the fields couples are pointing to the same reference
291 MatchField a = null, b = null;
292 for (MatchType type : MatchType.values()) {
293 a = match1.getField(type);
294 b = match2.getField(type);
295 if (a != null && b != null) {
296 Assert.assertFalse(a == b);
302 public void testEqualityNetMask() throws Exception {
304 InetAddress srcIP = InetAddress.getByName("1.1.1.1");
305 InetAddress ipMask = InetAddress.getByName("255.255.255.255");
306 InetAddress srcIP2 = InetAddress.getByName("1.1.1.1");
307 InetAddress ipMask2 = null;
308 short ethertype = EtherTypes.IPv4.shortValue();
309 short ethertype2 = EtherTypes.IPv4.shortValue();
312 * Create a SAL Flow aFlow
314 Match match1 = new Match();
315 Match match2 = new Match();
317 match1.setField(MatchType.DL_TYPE, ethertype);
318 match1.setField(MatchType.NW_SRC, srcIP, ipMask);
320 match2.setField(MatchType.DL_TYPE, ethertype2);
321 match2.setField(MatchType.NW_SRC, srcIP2, ipMask2);
323 Assert.assertTrue(match1.equals(match2));
325 ipMask2 = InetAddress.getByName("255.255.255.255");
326 match2.setField(MatchType.NW_SRC, srcIP2, ipMask2);
328 srcIP = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
329 srcIP2 = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
331 ipMask2 = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
332 ethertype = EtherTypes.IPv6.shortValue();
333 ethertype2 = EtherTypes.IPv6.shortValue();
335 match1.setField(MatchType.DL_TYPE, ethertype);
336 match1.setField(MatchType.NW_SRC, srcIP, ipMask);
338 match2.setField(MatchType.DL_TYPE, ethertype2);
339 match2.setField(MatchType.NW_SRC, srcIP2, ipMask2);
341 Assert.assertTrue(match1.equals(match2));
345 public void testHashCodeWithReverseMatch() throws Exception {
346 InetAddress srcIP1 = InetAddress.getByName("1.1.1.1");
347 InetAddress ipMask1 = InetAddress.getByName("255.255.255.255");
348 InetAddress srcIP2 = InetAddress.getByName("2.2.2.2");
349 InetAddress ipMask2 = InetAddress.getByName("255.255.255.255");
350 MatchField field1 = new MatchField(MatchType.NW_SRC, srcIP1, ipMask1);
351 MatchField field2 = new MatchField(MatchType.NW_DST, srcIP2, ipMask2);
352 Match match1 = new Match();
353 match1.setField(field1);
354 match1.setField(field2);
355 Match match2 = match1.reverse();
356 Assert.assertFalse(match1.hashCode() == match2.hashCode());
360 public void testHashCode() throws Exception {
361 byte srcMac1[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
362 byte srcMac2[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
363 byte dstMac1[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
364 byte dstMac2[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
365 short ethertype = EtherTypes.IPv4.shortValue();
366 short ethertype2 = EtherTypes.IPv4.shortValue();
367 InetAddress srcIP1 = InetAddress.getByName("1.1.1.1");
368 InetAddress ipMask1 = InetAddress.getByName("255.255.255.255");
369 InetAddress srcIP2 = InetAddress.getByName("1.1.1.1");
370 InetAddress ipMask2 = InetAddress.getByName("255.255.255.255");
372 Match match1 = new Match();
373 Match match2 = new Match();
375 MatchField field1 = new MatchField(MatchType.DL_SRC, srcMac1);
376 MatchField field2 = new MatchField(MatchType.DL_SRC, srcMac2);
377 Assert.assertTrue(field1.hashCode() == field2.hashCode());
379 match1.setField(field1);
380 match2.setField(field2);
381 Assert.assertTrue(match1.hashCode() == match2.hashCode());
383 MatchField field3 = new MatchField(MatchType.DL_DST, dstMac1);
384 MatchField field4 = new MatchField(MatchType.DL_DST, dstMac2);
385 Assert.assertTrue(field3.hashCode() == field4.hashCode());
387 match1.setField(field3);
388 match2.setField(field4);
389 Assert.assertTrue(match1.hashCode() == match2.hashCode());
391 MatchField field5 = new MatchField(MatchType.DL_TYPE, ethertype);
392 MatchField field6 = new MatchField(MatchType.DL_TYPE, ethertype2);
393 Assert.assertTrue(field5.hashCode() == field6.hashCode());
395 match1.setField(field5);
396 match2.setField(field6);
397 Assert.assertTrue(match1.hashCode() == match2 .hashCode());
399 MatchField field7 = new MatchField(MatchType.NW_SRC, srcIP1, ipMask1);
400 MatchField field8 = new MatchField(MatchType.NW_SRC, srcIP2, ipMask2);
401 Assert.assertTrue(field7.hashCode() == field8.hashCode());
403 match1.setField(field7);
404 match2.setField(field8);
405 Assert.assertTrue(match1.hashCode() == match2.hashCode());
410 public void testCloning() throws Exception {
411 Node node = NodeCreator.createOFNode(7L);
412 NodeConnector port = NodeConnectorCreator.createOFNodeConnector((short) 24, node);
413 byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
414 byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
415 InetAddress srcIP = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
416 InetAddress dstIP = InetAddress.getByName("2001:420:281:1004:e123:e688:d655:a1b0");
417 InetAddress ipMasks = InetAddress.getByName("ffff:ffff:ffff:ffff:0:0:0:0");
418 InetAddress ipMaskd = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
419 short ethertype = EtherTypes.IPv6.shortValue();
420 short vlan = (short) 27;
421 byte vlanPr = (byte) 3;
423 byte proto = IPProtocols.UDP.byteValue();
424 short src = (short) 5500;
428 * Create a SAL Flow aFlow
430 Match match = new Match();
431 match.setField(MatchType.IN_PORT, port);
432 match.setField(MatchType.DL_SRC, srcMac);
433 match.setField(MatchType.DL_DST, dstMac);
434 match.setField(MatchType.DL_TYPE, ethertype);
435 match.setField(MatchType.DL_VLAN, vlan);
436 match.setField(MatchType.DL_VLAN_PR, vlanPr);
437 match.setField(MatchType.NW_SRC, srcIP, ipMasks);
438 match.setField(MatchType.NW_DST, dstIP, ipMaskd);
439 match.setField(MatchType.NW_TOS, tos);
440 match.setField(MatchType.NW_PROTO, proto);
441 match.setField(MatchType.TP_SRC, src);
442 match.setField(MatchType.TP_DST, dst);
444 Match cloned = match.clone();
446 // Make sure all values are equals
447 for (MatchType type : MatchType.values()) {
448 if (match.isPresent(type)) {
449 if (!match.getField(type).equals(cloned.getField(type))) {
450 Assert.assertTrue(match.getField(type).equals(cloned.getField(type)));
455 // Make sure none of the fields couples are pointing to the same
457 MatchField a = null, b = null;
458 for (MatchType type : MatchType.values()) {
459 a = match.getField(type);
460 b = cloned.getField(type);
461 if (a != null && b != null) {
462 Assert.assertFalse(a == b);
466 Assert.assertTrue(match.equals(cloned));
468 Assert.assertFalse(match.getField(MatchType.DL_SRC) == cloned.getField(MatchType.DL_SRC));
469 Assert.assertFalse(match.getField(MatchType.NW_DST) == cloned.getField(MatchType.NW_DST));
470 Assert.assertTrue(match.getField(MatchType.NW_DST).getMask()
471 .equals(cloned.getField(MatchType.NW_DST).getMask()));
472 Assert.assertTrue(match.hashCode() == cloned.hashCode());
476 public void testFlip() throws Exception {
477 Node node = NodeCreator.createOFNode(7L);
478 NodeConnector port = NodeConnectorCreator.createOFNodeConnector((short) 24, node);
479 byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
480 byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
481 InetAddress srcIP = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
482 InetAddress dstIP = InetAddress.getByName("2001:420:281:1004:e123:e688:d655:a1b0");
483 InetAddress ipMasks = InetAddress.getByName("ffff:ffff:ffff:ffff:0:0:0:0");
484 InetAddress ipMaskd = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
485 short ethertype = EtherTypes.IPv6.shortValue();
486 short vlan = (short) 27;
487 byte vlanPr = (byte) 3;
489 byte proto = IPProtocols.UDP.byteValue();
490 short src = (short) 5500;
494 * Create a SAL Flow aFlow
496 Match match = new Match();
497 match.setField(MatchType.IN_PORT, port);
498 match.setField(MatchType.DL_SRC, srcMac);
499 match.setField(MatchType.DL_DST, dstMac);
500 match.setField(MatchType.DL_TYPE, ethertype);
501 match.setField(MatchType.DL_VLAN, vlan);
502 match.setField(MatchType.DL_VLAN_PR, vlanPr);
503 match.setField(MatchType.NW_SRC, srcIP, ipMasks);
504 match.setField(MatchType.NW_DST, dstIP, ipMaskd);
505 match.setField(MatchType.NW_TOS, tos);
506 match.setField(MatchType.NW_PROTO, proto);
507 match.setField(MatchType.TP_SRC, src);
508 match.setField(MatchType.TP_DST, dst);
510 Match flipped = match.reverse();
512 Assert.assertTrue(match.getField(MatchType.DL_TYPE).equals(flipped.getField(MatchType.DL_TYPE)));
513 Assert.assertTrue(match.getField(MatchType.DL_VLAN).equals(flipped.getField(MatchType.DL_VLAN)));
515 Assert.assertTrue(match.getField(MatchType.DL_DST).getValue()
516 .equals(flipped.getField(MatchType.DL_SRC).getValue()));
517 Assert.assertTrue(match.getField(MatchType.DL_DST).getMask() == flipped.getField(MatchType.DL_SRC).getMask());
519 Assert.assertTrue(match.getField(MatchType.NW_DST).getValue()
520 .equals(flipped.getField(MatchType.NW_SRC).getValue()));
521 Assert.assertTrue(match.getField(MatchType.NW_DST).getMask() == flipped.getField(MatchType.NW_SRC).getMask());
523 Assert.assertTrue(match.getField(MatchType.TP_DST).getValue()
524 .equals(flipped.getField(MatchType.TP_SRC).getValue()));
525 Assert.assertTrue(match.getField(MatchType.TP_DST).getMask() == flipped.getField(MatchType.TP_SRC).getMask());
527 Match flipflip = flipped.reverse().reverse();
528 Assert.assertTrue(flipflip.equals(flipped));
533 public void testVlanNone() throws Exception {
534 // The value 0 is used to indicate that no VLAN ID is set
535 short vlan = (short) 0;
536 MatchField field = new MatchField(MatchType.DL_VLAN, vlan);
538 Assert.assertTrue(field != null);
539 Assert.assertTrue(field.getValue().equals(new Short(vlan)));
540 Assert.assertTrue(field.isValid());
544 public void testIntersection() throws UnknownHostException {
545 Short ethType = Short.valueOf((short)0x800);
546 InetAddress ip1 = InetAddress.getByName("1.1.1.1");
547 InetAddress ip2 = InetAddress.getByName("1.1.1.0");
548 InetAddress ipm2 = InetAddress.getByName("255.255.255.0");
549 InetAddress ip3 = InetAddress.getByName("1.3.0.0");
550 InetAddress ipm3 = InetAddress.getByName("255.255.0.0");
551 InetAddress ip4 = InetAddress.getByName("1.3.4.4");
552 InetAddress ipm4 = InetAddress.getByName("255.255.255.0");
554 Match m1 = new Match();
555 m1.setField(MatchType.DL_TYPE, ethType);
556 m1.setField(MatchType.NW_SRC, ip1);
558 Match m2 = new Match();
559 m2.setField(MatchType.DL_TYPE, ethType);
560 m2.setField(MatchType.NW_SRC, ip2, ipm2);
562 Match m3 = new Match();
563 m3.setField(MatchType.DL_TYPE, ethType);
564 m3.setField(MatchType.NW_SRC, ip3, ipm3);
565 m3.setField(MatchType.NW_PROTO, IPProtocols.TCP.byteValue());
567 Match m3r = m3.reverse();
568 Assert.assertTrue(m3.intersetcs(m3r));
570 Assert.assertTrue(m1.intersetcs(m2));
571 Assert.assertTrue(m2.intersetcs(m1));
572 Assert.assertFalse(m1.intersetcs(m3));
573 Assert.assertTrue(m1.intersetcs(m3r));
574 Assert.assertFalse(m3.intersetcs(m1));
575 Assert.assertTrue(m3.intersetcs(m1.reverse()));
576 Assert.assertFalse(m2.intersetcs(m3));
577 Assert.assertFalse(m3.intersetcs(m2));
578 Assert.assertTrue(m2.intersetcs(m3r));
581 Match i = m1.getIntersection(m2);
582 Assert.assertTrue(((Short)i.getField(MatchType.DL_TYPE).getValue()).equals(ethType));
583 // Verify intersection of IP addresses is correct
584 Assert.assertTrue(((InetAddress)i.getField(MatchType.NW_SRC).getValue()).equals(ip1));
585 Assert.assertNull(i.getField(MatchType.NW_SRC).getMask());
588 i = m2.getIntersection(m3);
589 Assert.assertNull(i);
591 Match m4 = new Match();
592 m4.setField(MatchType.DL_TYPE, ethType);
593 m4.setField(MatchType.NW_PROTO, IPProtocols.TCP.byteValue());
594 m3.setField(MatchType.NW_SRC, ip4, ipm4);
595 Assert.assertTrue(m4.intersetcs(m3));
597 // Verify intersection of IP and IP mask addresses is correct
598 Match ii = m3.getIntersection(m4);
599 Assert.assertTrue(((InetAddress)ii.getField(MatchType.NW_SRC).getValue()).equals(ip4));
600 Assert.assertTrue(((InetAddress)ii.getField(MatchType.NW_SRC).getMask()).equals(ipm4));
602 Match m5 = new Match();
603 m5.setField(MatchType.DL_TYPE, ethType);
604 m3.setField(MatchType.NW_SRC, ip3, ipm3);
605 m5.setField(MatchType.NW_PROTO, IPProtocols.UDP.byteValue());
606 Assert.assertFalse(m5.intersetcs(m3));
607 Assert.assertFalse(m5.intersetcs(m4));
608 Assert.assertTrue(m5.intersetcs(m5));
609 Assert.assertFalse(m3.intersetcs(m5));
610 Assert.assertFalse(m4.intersetcs(m5));
613 Match i2 = m4.getIntersection(m3);
614 Assert.assertFalse(i2.getMatches() == 0);
615 Assert.assertFalse(i2.getMatchesList().isEmpty());
616 Assert.assertTrue(((InetAddress)i2.getField(MatchType.NW_SRC).getValue()).equals(ip3));
617 Assert.assertTrue(((InetAddress)i2.getField(MatchType.NW_SRC).getMask()).equals(ipm3));
618 Assert.assertTrue(((Byte)i2.getField(MatchType.NW_PROTO).getValue()).equals(IPProtocols.TCP.byteValue()));
620 byte src[] = {(byte)0, (byte)0xab,(byte)0xbc,(byte)0xcd,(byte)0xde,(byte)0xef};
621 byte dst[] = {(byte)0x10, (byte)0x11,(byte)0x12,(byte)0x13,(byte)0x14,(byte)0x15};
622 Short srcPort = (short)1024;
623 Short dstPort = (short)80;
626 Match m6 = new Match();
627 m6.setField(MatchType.DL_SRC, src);
628 m6.setField(MatchType.DL_DST, dst);
629 m6.setField(MatchType.NW_SRC, ip2, ipm2);
630 m6.setField(MatchType.NW_DST, ip3, ipm3);
631 m6.setField(MatchType.NW_PROTO, IPProtocols.UDP.byteValue());
632 m6.setField(MatchType.TP_SRC, srcPort);
633 m6.setField(MatchType.TP_DST, dstPort);
634 Assert.assertTrue(m6.intersetcs(m6));
635 Assert.assertTrue(m6.getIntersection(m6).equals(m6));
637 // Empty match, represents the universal set (all packets)
638 Match u = new Match();
639 Assert.assertTrue(m6.getIntersection(u).equals(m6));
640 Assert.assertTrue(u.getIntersection(m6).equals(m6));
642 // No intersection with null match, empty set
643 Assert.assertNull(m6.getIntersection(null));
647 public void testMetadata() {
648 Property tier1 = new Tier(1);
649 Property tier2 = new Tier(2);
650 Property table1 = new Tables((byte)0x7f);
651 Match m1 = new Match();
652 List<Property> resprops = null;
653 resprops = m1.getMetadatas();
654 // This should be null
655 Assert.assertTrue(resprops.isEmpty());
656 m1.setMetadata("tier1", tier1);
657 m1.setMetadata("tier2", tier2);
658 m1.setMetadata("table1", table1);
659 resprops = m1.getMetadatas();
660 // Check for the number of elements in it
661 Assert.assertTrue(resprops.size() == 3);
662 // Check if the elements are in it
663 Assert.assertTrue(resprops.contains(tier1));
664 Assert.assertTrue(resprops.contains(tier2));
665 Assert.assertTrue(resprops.contains(table1));
666 // Check for single elements retrieve
667 Assert.assertTrue(m1.getMetadata("tier1").equals(tier1));
668 Assert.assertTrue(m1.getMetadata("tier2").equals(tier2));
669 Assert.assertTrue(m1.getMetadata("table1").equals(table1));
670 // Now remove an element and make sure the remaining are
672 m1.removeMetadata("tier1");
674 resprops = m1.getMetadatas();
675 // Check for the number of elements in it
676 Assert.assertTrue(resprops.size() == 2);
677 // Check if the elements are in it
678 Assert.assertFalse(resprops.contains(tier1));
679 Assert.assertTrue(resprops.contains(tier2));
680 Assert.assertTrue(resprops.contains(table1));
681 // Check for single elements retrieve
682 Assert.assertTrue(m1.getMetadata("table1").equals(table1));
683 Assert.assertTrue(m1.getMetadata("tier2").equals(tier2));
684 Assert.assertNull(m1.getMetadata("tier1"));
686 // Check for an element never existed
687 Assert.assertNull(m1.getMetadata("table100"));
690 m1.removeMetadata("tier2");
691 m1.removeMetadata("table1");
693 // Remove also a non-existent one
694 m1.removeMetadata("table100");
696 resprops = m1.getMetadatas();
697 // Check there are no elements left
698 Assert.assertTrue(resprops.size() == 0);
700 // Now check for exception on setting null values
702 m1.setMetadata("foo", null);
703 // The line below should never be reached
704 Assert.assertTrue(false);
705 } catch (NullPointerException nue) {
706 // NPE should be raised for null value
707 Assert.assertTrue(true);
710 // Now check on using null key
712 m1.setMetadata(null, table1);
713 // The line below should never be reached
714 Assert.assertTrue(false);
715 } catch (NullPointerException nue) {
716 // NPE should be raised for null value
717 Assert.assertTrue(true);
720 // Now check on using null key and null value
722 m1.setMetadata(null, null);
723 // The line below should never be reached
724 Assert.assertTrue(false);
725 } catch (NullPointerException nue) {
726 // NPE should be raised for null value
727 Assert.assertTrue(true);