Avoiding hash collisions of a match with its reverse
[controller.git] / opendaylight / sal / api / src / test / java / org / opendaylight / controller / sal / match / MatchTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.sal.match;
10
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13 import java.util.Arrays;
14
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;
23
24 public class MatchTest {
25     @Test
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);
30
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());
35
36         field = null;
37         field = new MatchField(MatchType.TP_SRC, Long.valueOf(23));
38         Assert.assertFalse(field.isValid());
39
40         field = null;
41         field = new MatchField(MatchType.TP_SRC, (long) 45);
42         Assert.assertFalse(field.isValid());
43
44         field = null;
45         field = new MatchField(MatchType.TP_SRC, 120000);
46         Assert.assertFalse(field.isValid());
47
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 };
50         field = null;
51         field = new MatchField(MatchType.DL_SRC, mac, mask);
52         Assert.assertFalse(field.getValue() == null);
53
54         field = null;
55         field = new MatchField(MatchType.NW_TOS, (byte) 0x22, (byte) 0x3);
56         Assert.assertFalse(field.getValue() == null);
57     }
58
59     @Test
60     public void testMatchSetGet() {
61         Match x = new Match();
62         short val = 2346;
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);
67     }
68
69     @Test
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 };
76
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());
83
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()));
87     }
88
89     @Test
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");
95
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));
99     }
100
101     @Test
102     public void testMatchSetGetEtherType() throws UnknownHostException {
103         Match x = new Match();
104
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));
109
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()));
113     }
114
115     @Test
116     public void testSetGetNwTos() {
117         Match x = new Match();
118         x.setField(MatchType.NW_TOS, (byte) 0xb, (byte) 0xf);
119
120         Byte t = new Byte((byte) 0xb);
121
122         Object o = x.getField(MatchType.NW_TOS).getValue();
123         Assert.assertTrue(o.equals(t));
124         Assert.assertTrue(o.equals((byte) 0xb));
125     }
126
127     @Test
128     public void testSetGetNwProto() {
129         Match x = new Match();
130         byte proto = (byte) 199;
131         x.setField(MatchType.NW_PROTO, proto, (byte) 0xff);
132
133         Object o = x.getField(MatchType.NW_PROTO).getValue();
134         Assert.assertTrue(o.equals(proto));
135     }
136
137     @Test
138     public void testSetTpSrc() {
139         // Minimum value validation.
140         Match match = new Match();
141         short tp_src = 0;
142         match.setField(MatchType.TP_SRC, tp_src);
143
144         Object o = match.getField(MatchType.TP_SRC).getValue();
145         Assert.assertTrue(o.equals(tp_src));
146
147         // Maximum value validation.
148         match = new Match();
149         tp_src = (short) 0xffff;
150         match.setField(MatchType.TP_SRC, tp_src);
151
152         o = match.getField(MatchType.TP_SRC).getValue();
153         Assert.assertTrue(o.equals(tp_src));
154     }
155
156     @Test
157     public void testSetTpDst() {
158         // Minimum value validation.
159         Match match = new Match();
160         short tp_dst = 0;
161         match.setField(MatchType.TP_DST, tp_dst);
162
163         Object o = match.getField(MatchType.TP_DST).getValue();
164         Assert.assertTrue(o.equals(tp_dst));
165
166         // Maximum value validation.
167         match = new Match();
168         tp_dst = (short) 0xffff;
169         match.setField(MatchType.TP_DST, tp_dst);
170
171         o = match.getField(MatchType.TP_DST).getValue();
172         Assert.assertTrue(o.equals(tp_dst));
173     }
174
175     @Test
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()));
183     }
184
185     @Test
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));
190
191         MatchField x = new MatchField(MatchType.IN_PORT, inPort);
192         Assert.assertTrue((x.getMask()) == null);
193
194         x = new MatchField(MatchType.DL_VLAN, (short) 255, (short) 0xff);
195         Assert.assertTrue(x.getBitMask() == 0xff);
196
197         x = new MatchField(MatchType.DL_SRC, mac, mask);
198         Assert.assertTrue(x.getMask().equals(mask));
199         Assert.assertTrue(x.getBitMask() == 0xffffffffff00L);
200     }
201
202     @Test
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));
206
207         MatchField x = new MatchField(MatchType.IN_PORT, inPort);
208         Assert.assertTrue(x.getBitMask() == 0);
209
210         x = new MatchField(MatchType.NW_PROTO, (byte) 17);
211         Assert.assertTrue(x.getBitMask() == 0xff);
212
213         x = new MatchField(MatchType.DL_VLAN, (short) 255);
214         Assert.assertTrue(x.getBitMask() == 0xfff);
215
216         x = new MatchField(MatchType.DL_SRC, mac);
217         Assert.assertTrue(x.getBitMask() == 0xffffffffffffL);
218     }
219
220     @Test
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;
245
246         /*
247          * Create a SAL Flow aFlow
248          */
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);
263
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);
276
277         Assert.assertTrue(match1.equals(match2));
278
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)));
283             }
284         }
285
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);
293             }
294         }
295     }
296
297     @Test
298     public void testEqualityNetMask() throws Exception {
299
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();
306
307         /*
308          * Create a SAL Flow aFlow
309          */
310         Match match1 = new Match();
311         Match match2 = new Match();
312
313         match1.setField(MatchType.DL_TYPE, ethertype);
314         match1.setField(MatchType.NW_SRC, srcIP, ipMask);
315
316         match2.setField(MatchType.DL_TYPE, ethertype2);
317         match2.setField(MatchType.NW_SRC, srcIP2, ipMask2);
318
319         Assert.assertTrue(match1.equals(match2));
320
321         ipMask2 = InetAddress.getByName("255.255.255.255");
322         match2.setField(MatchType.NW_SRC, srcIP2, ipMask2);
323
324         srcIP = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
325         srcIP2 = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
326         ipMask = null;
327         ipMask2 = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
328         ethertype = EtherTypes.IPv6.shortValue();
329         ethertype2 = EtherTypes.IPv6.shortValue();
330
331         match1.setField(MatchType.DL_TYPE, ethertype);
332         match1.setField(MatchType.NW_SRC, srcIP, ipMask);
333
334         match2.setField(MatchType.DL_TYPE, ethertype2);
335         match2.setField(MatchType.NW_SRC, srcIP2, ipMask2);
336
337         Assert.assertTrue(match1.equals(match2));
338     }
339
340     @Test
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());
353     }
354
355     @Test
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");
367
368         Match match1 = new Match();
369         Match match2 = new Match();
370
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());
374
375         match1.setField(field1);
376         match2.setField(field2);
377         Assert.assertTrue(match1.hashCode() == match2.hashCode());
378
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());
382
383         match1.setField(field3);
384         match2.setField(field4);
385         Assert.assertTrue(match1.hashCode() == match2.hashCode());
386
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());
390
391         match1.setField(field5);
392         match2.setField(field6);
393         Assert.assertTrue(match1.hashCode() == match2 .hashCode());
394
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());
398
399         match1.setField(field7);
400         match2.setField(field8);
401         Assert.assertTrue(match1.hashCode() == match2.hashCode());
402
403     }
404
405     @Test
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;
418         Byte tos = 4;
419         byte proto = IPProtocols.UDP.byteValue();
420         short src = (short) 5500;
421         short dst = 80;
422
423         /*
424          * Create a SAL Flow aFlow
425          */
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);
439
440         Match cloned = match.clone();
441
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)));
447                 }
448             }
449         }
450
451         // Make sure none of the fields couples are pointing to the same
452         // reference
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);
459             }
460         }
461
462         Assert.assertTrue(match.equals(cloned));
463
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());
469     }
470
471     @Test
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;
484         Byte tos = 4;
485         byte proto = IPProtocols.UDP.byteValue();
486         short src = (short) 5500;
487         short dst = 80;
488
489         /*
490          * Create a SAL Flow aFlow
491          */
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);
505
506         Match flipped = match.reverse();
507
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)));
510
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());
514
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());
518
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());
522
523         Match flipflip = flipped.reverse().reverse();
524         Assert.assertTrue(flipflip.equals(flipped));
525
526     }
527
528     @Test
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);
533
534         Assert.assertTrue(field != null);
535         Assert.assertTrue(field.getValue().equals(new Short(vlan)));
536         Assert.assertTrue(field.isValid());
537     }
538
539     @Test
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");
549
550         Match m1 = new Match();
551         m1.setField(MatchType.DL_TYPE, ethType);
552         m1.setField(MatchType.NW_SRC, ip1);
553
554         Match m2 = new Match();
555         m2.setField(MatchType.DL_TYPE, ethType);
556         m2.setField(MatchType.NW_SRC, ip2, ipm2);
557
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());
562
563         Match m3r = m3.reverse();
564         Assert.assertTrue(m3.intersetcs(m3r));
565
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));
575
576
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());
582
583         // Empty set
584         i = m2.getIntersection(m3);
585         Assert.assertNull(i);
586
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));
592
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));
597
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));
607
608
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()));
615
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;
620
621         // Check identity
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));
632
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));
637
638         // No intersection with null match, empty set
639         Assert.assertNull(m6.getIntersection(null));
640     }
641 }