Merge "Fixed run.sh to autodetect JAVA_HOME on Mac OS X"
[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.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;
26
27 public class MatchTest {
28     @Test
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);
33
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());
38
39         field = null;
40         field = new MatchField(MatchType.TP_SRC, Long.valueOf(23));
41         Assert.assertFalse(field.isValid());
42
43         field = null;
44         field = new MatchField(MatchType.TP_SRC, (long) 45);
45         Assert.assertFalse(field.isValid());
46
47         field = null;
48         field = new MatchField(MatchType.TP_SRC, 120000);
49         Assert.assertFalse(field.isValid());
50
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 };
53         field = null;
54         field = new MatchField(MatchType.DL_SRC, mac, mask);
55         Assert.assertFalse(field.getValue() == null);
56
57         field = null;
58         field = new MatchField(MatchType.NW_TOS, (byte) 0x22, (byte) 0x3);
59         Assert.assertFalse(field.getValue() == null);
60     }
61
62     @Test
63     public void testMatchSetGet() {
64         Match x = new Match();
65         short val = 2346;
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);
70     }
71
72     @Test
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 };
79
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());
86
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()));
90     }
91
92     @Test
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");
98
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));
102     }
103
104     @Test
105     public void testMatchSetGetEtherType() throws UnknownHostException {
106         Match x = new Match();
107
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));
112
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()));
116     }
117
118     @Test
119     public void testSetGetNwTos() {
120         Match x = new Match();
121         x.setField(MatchType.NW_TOS, (byte) 0xb, (byte) 0xf);
122
123         Byte t = new Byte((byte) 0xb);
124
125         Object o = x.getField(MatchType.NW_TOS).getValue();
126         Assert.assertTrue(o.equals(t));
127         Assert.assertTrue(o.equals((byte) 0xb));
128     }
129
130     @Test
131     public void testSetGetNwProto() {
132         Match x = new Match();
133         byte proto = (byte) 199;
134         x.setField(MatchType.NW_PROTO, proto, (byte) 0xff);
135
136         Object o = x.getField(MatchType.NW_PROTO).getValue();
137         Assert.assertTrue(o.equals(proto));
138     }
139
140     @Test
141     public void testSetTpSrc() {
142         // Minimum value validation.
143         Match match = new Match();
144         short tp_src = 0;
145         match.setField(MatchType.TP_SRC, tp_src);
146
147         Object o = match.getField(MatchType.TP_SRC).getValue();
148         Assert.assertTrue(o.equals(tp_src));
149
150         // Maximum value validation.
151         match = new Match();
152         tp_src = (short) 0xffff;
153         match.setField(MatchType.TP_SRC, tp_src);
154
155         o = match.getField(MatchType.TP_SRC).getValue();
156         Assert.assertTrue(o.equals(tp_src));
157     }
158
159     @Test
160     public void testSetTpDst() {
161         // Minimum value validation.
162         Match match = new Match();
163         short tp_dst = 0;
164         match.setField(MatchType.TP_DST, tp_dst);
165
166         Object o = match.getField(MatchType.TP_DST).getValue();
167         Assert.assertTrue(o.equals(tp_dst));
168
169         // Maximum value validation.
170         match = new Match();
171         tp_dst = (short) 0xffff;
172         match.setField(MatchType.TP_DST, tp_dst);
173
174         o = match.getField(MatchType.TP_DST).getValue();
175         Assert.assertTrue(o.equals(tp_dst));
176     }
177
178     @Test
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()));
186     }
187
188     @Test
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));
193
194         MatchField x = new MatchField(MatchType.IN_PORT, inPort);
195         Assert.assertTrue((x.getMask()) == null);
196
197         x = new MatchField(MatchType.DL_VLAN, (short) 255, (short) 0xff);
198         Assert.assertTrue(x.getBitMask() == 0xff);
199
200         x = new MatchField(MatchType.DL_SRC, mac, mask);
201         Assert.assertTrue(x.getMask().equals(mask));
202         Assert.assertTrue(x.getBitMask() == 0xffffffffff00L);
203     }
204
205     @Test
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));
209
210         MatchField x = new MatchField(MatchType.IN_PORT, inPort);
211         Assert.assertTrue(x.getBitMask() == 0);
212
213         x = new MatchField(MatchType.NW_PROTO, (byte) 17);
214         Assert.assertTrue(x.getBitMask() == 0xff);
215
216         x = new MatchField(MatchType.DL_VLAN, (short) 255);
217         Assert.assertTrue(x.getBitMask() == 0xfff);
218
219         x = new MatchField(MatchType.DL_SRC, mac);
220         Assert.assertTrue(x.getBitMask() == 0xffffffffffffL);
221     }
222
223     @Test
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;
248
249         /*
250          * Create a SAL Flow aFlow
251          */
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);
266
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);
279
280         Assert.assertTrue(match1.equals(match2));
281
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)));
286             }
287         }
288
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);
296             }
297         }
298     }
299
300     @Test
301     public void testEqualityNetMask() throws Exception {
302
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();
309
310         /*
311          * Create a SAL Flow aFlow
312          */
313         Match match1 = new Match();
314         Match match2 = new Match();
315
316         match1.setField(MatchType.DL_TYPE, ethertype);
317         match1.setField(MatchType.NW_SRC, srcIP, ipMask);
318
319         match2.setField(MatchType.DL_TYPE, ethertype2);
320         match2.setField(MatchType.NW_SRC, srcIP2, ipMask2);
321
322         Assert.assertTrue(match1.equals(match2));
323
324         ipMask2 = InetAddress.getByName("255.255.255.255");
325         match2.setField(MatchType.NW_SRC, srcIP2, ipMask2);
326
327         srcIP = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
328         srcIP2 = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
329         ipMask = null;
330         ipMask2 = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
331         ethertype = EtherTypes.IPv6.shortValue();
332         ethertype2 = EtherTypes.IPv6.shortValue();
333
334         match1.setField(MatchType.DL_TYPE, ethertype);
335         match1.setField(MatchType.NW_SRC, srcIP, ipMask);
336
337         match2.setField(MatchType.DL_TYPE, ethertype2);
338         match2.setField(MatchType.NW_SRC, srcIP2, ipMask2);
339
340         Assert.assertTrue(match1.equals(match2));
341     }
342
343     @Test
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");
355
356         Match match1 = new Match();
357         Match match2 = new Match();
358
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());
362
363         match1.setField(field1);
364         match2.setField(field2);
365         Assert.assertTrue(match1.hashCode() == match2.hashCode());
366
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());
370
371         match1.setField(field3);
372         match2.setField(field4);
373         Assert.assertTrue(match1.hashCode() == match2.hashCode());
374
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());
378
379         match1.setField(field5);
380         match2.setField(field6);
381         Assert.assertTrue(match1.hashCode() == match2 .hashCode());
382
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());
386
387         match1.setField(field7);
388         match2.setField(field8);
389         Assert.assertTrue(match1.hashCode() == match2.hashCode());
390
391     }
392
393     @Test
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;
406         Byte tos = 4;
407         byte proto = IPProtocols.UDP.byteValue();
408         short src = (short) 5500;
409         short dst = 80;
410
411         /*
412          * Create a SAL Flow aFlow
413          */
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);
427
428         Match cloned = match.clone();
429
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)));
435                 }
436             }
437         }
438
439         // Make sure none of the fields couples are pointing to the same
440         // reference
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);
447             }
448         }
449
450         Assert.assertTrue(match.equals(cloned));
451
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());
457     }
458
459     @Test
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;
472         Byte tos = 4;
473         byte proto = IPProtocols.UDP.byteValue();
474         short src = (short) 5500;
475         short dst = 80;
476
477         /*
478          * Create a SAL Flow aFlow
479          */
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);
493
494         Match flipped = match.reverse();
495
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)));
498
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());
502
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());
506
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());
510
511         Match flipflip = flipped.reverse().reverse();
512         Assert.assertTrue(flipflip.equals(flipped));
513
514     }
515
516     @Test
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);
521
522         Assert.assertTrue(field != null);
523         Assert.assertTrue(field.getValue().equals(new Short(vlan)));
524         Assert.assertTrue(field.isValid());
525     }
526
527     @Test
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
536         Match m1 = new Match();
537         m1.setField(MatchType.DL_TYPE, ethType);
538         m1.setField(MatchType.NW_SRC, ip1);
539
540         Match m2 = new Match();
541         m2.setField(MatchType.DL_TYPE, ethType);
542         m2.setField(MatchType.NW_SRC, ip2, ipm2);
543
544         Match m3 = new Match();
545         m3.setField(MatchType.DL_TYPE, ethType);
546         m3.setField(MatchType.NW_SRC, ip3, ipm3);
547         m3.setField(MatchType.NW_PROTO, IPProtocols.TCP.byteValue());
548
549         Match m3r = m3.reverse();
550         Assert.assertTrue(m3.intersetcs(m3r));
551
552         Assert.assertTrue(m1.intersetcs(m2));
553         Assert.assertTrue(m2.intersetcs(m1));
554         Assert.assertFalse(m1.intersetcs(m3));
555         Assert.assertTrue(m1.intersetcs(m3r));
556         Assert.assertFalse(m3.intersetcs(m1));
557         Assert.assertTrue(m3.intersetcs(m1.reverse()));
558         Assert.assertFalse(m2.intersetcs(m3));
559         Assert.assertFalse(m3.intersetcs(m2));
560         Assert.assertTrue(m2.intersetcs(m3r));
561
562
563         Match i = m1.getIntersection(m2);
564         Assert.assertTrue(((Short)i.getField(MatchType.DL_TYPE).getValue()).equals(ethType));
565         Assert.assertTrue(((InetAddress)i.getField(MatchType.NW_SRC).getValue()).equals(ip2));
566         Assert.assertTrue(((InetAddress)i.getField(MatchType.NW_SRC).getMask()).equals(ipm2));
567
568         i = m2.getIntersection(m3);
569         Assert.assertTrue(i.getMatches() == 0);
570
571         Match m4 = new Match();
572         m4.setField(MatchType.DL_TYPE, ethType);
573         m4.setField(MatchType.NW_PROTO, IPProtocols.TCP.byteValue());
574         Assert.assertTrue(m4.intersetcs(m3));
575
576         Match m5 = new Match();
577         m5.setField(MatchType.DL_TYPE, ethType);
578         m3.setField(MatchType.NW_SRC, ip3, ipm3);
579         m5.setField(MatchType.NW_PROTO, IPProtocols.UDP.byteValue());
580         Assert.assertFalse(m5.intersetcs(m3));
581         Assert.assertFalse(m5.intersetcs(m4));
582         Assert.assertTrue(m5.intersetcs(m5));
583         Assert.assertFalse(m3.intersetcs(m5));
584         Assert.assertFalse(m4.intersetcs(m5));
585
586
587         Match i2 = m4.getIntersection(m3);
588         Assert.assertFalse(i2.getMatches() == 0);
589         Assert.assertFalse(i2.getMatchesList().isEmpty());
590         Assert.assertTrue(((InetAddress)i2.getField(MatchType.NW_SRC).getValue()).equals(ip3));
591         Assert.assertTrue(((InetAddress)i2.getField(MatchType.NW_SRC).getMask()).equals(ipm3));
592         Assert.assertTrue(((Byte)i2.getField(MatchType.NW_PROTO).getValue()).equals(IPProtocols.TCP.byteValue()));
593
594         byte src[] = {(byte)0, (byte)0xab,(byte)0xbc,(byte)0xcd,(byte)0xde,(byte)0xef};
595         byte dst[] = {(byte)0x10, (byte)0x11,(byte)0x12,(byte)0x13,(byte)0x14,(byte)0x15};
596         Short srcPort = (short)1024;
597         Short dstPort = (short)80;
598
599         // Check identity
600         Match m6 = new Match();
601         m6.setField(MatchType.DL_SRC, src);
602         m6.setField(MatchType.DL_DST, dst);
603         m6.setField(MatchType.NW_SRC, ip2, ipm2);
604         m6.setField(MatchType.NW_DST, ip3, ipm3);
605         m6.setField(MatchType.NW_PROTO, IPProtocols.UDP.byteValue());
606         m6.setField(MatchType.TP_SRC, srcPort);
607         m6.setField(MatchType.TP_DST, dstPort);
608         Assert.assertTrue(m6.intersetcs(m6));
609         Assert.assertTrue(m6.getIntersection(m6).equals(m6));
610
611         // Empty match, represents the empty set
612         Match o = new Match();
613         Assert.assertTrue(m6.getIntersection(o).equals(o));
614         Assert.assertTrue(o.getIntersection(m6).equals(o));
615     }
616 }