Clean all unused and redundant imports in controller.
[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 testHashCode() throws Exception {
342         byte srcMac1[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
343         byte srcMac2[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
344         byte dstMac1[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
345         byte dstMac2[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
346         short ethertype = EtherTypes.IPv4.shortValue();
347         short ethertype2 = EtherTypes.IPv4.shortValue();
348         InetAddress srcIP1 = InetAddress.getByName("1.1.1.1");
349         InetAddress ipMask1 = InetAddress.getByName("255.255.255.255");
350         InetAddress srcIP2 = InetAddress.getByName("1.1.1.1");
351         InetAddress ipMask2 = InetAddress.getByName("255.255.255.255");
352
353         Match match1 = new Match();
354         Match match2 = new Match();
355
356         MatchField field1 = new MatchField(MatchType.DL_SRC, srcMac1);
357         MatchField field2 = new MatchField(MatchType.DL_SRC, srcMac2);
358         Assert.assertTrue(field1.hashCode() == field2.hashCode());
359
360         match1.setField(field1);
361         match2.setField(field2);
362         Assert.assertTrue(match1.hashCode() == match2.hashCode());
363
364         MatchField field3 = new MatchField(MatchType.DL_DST, dstMac1);
365         MatchField field4 = new MatchField(MatchType.DL_DST, dstMac2);
366         Assert.assertTrue(field3.hashCode() == field4.hashCode());
367
368         match1.setField(field3);
369         match2.setField(field4);
370         Assert.assertTrue(match1.hashCode() == match2.hashCode());
371
372         MatchField field5 = new MatchField(MatchType.DL_TYPE, ethertype);
373         MatchField field6 = new MatchField(MatchType.DL_TYPE, ethertype2);
374         Assert.assertTrue(field5.hashCode() == field6.hashCode());
375
376         match1.setField(field5);
377         match2.setField(field6);
378         Assert.assertTrue(match1.hashCode() == match2 .hashCode());
379
380         MatchField field7 = new MatchField(MatchType.NW_SRC, srcIP1, ipMask1);
381         MatchField field8 = new MatchField(MatchType.NW_SRC, srcIP2, ipMask2);
382         Assert.assertTrue(field7.hashCode() == field8.hashCode());
383
384         match1.setField(field7);
385         match2.setField(field8);
386         Assert.assertTrue(match1.hashCode() == match2.hashCode());
387
388     }
389
390     @Test
391     public void testCloning() throws Exception {
392         Node node = NodeCreator.createOFNode(7l);
393         NodeConnector port = NodeConnectorCreator.createOFNodeConnector((short) 24, node);
394         byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
395         byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
396         InetAddress srcIP = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
397         InetAddress dstIP = InetAddress.getByName("2001:420:281:1004:e123:e688:d655:a1b0");
398         InetAddress ipMasks = InetAddress.getByName("ffff:ffff:ffff:ffff:0:0:0:0");
399         InetAddress ipMaskd = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
400         short ethertype = EtherTypes.IPv6.shortValue();
401         short vlan = (short) 27;
402         byte vlanPr = (byte) 3;
403         Byte tos = 4;
404         byte proto = IPProtocols.UDP.byteValue();
405         short src = (short) 5500;
406         short dst = 80;
407
408         /*
409          * Create a SAL Flow aFlow
410          */
411         Match match = new Match();
412         match.setField(MatchType.IN_PORT, port);
413         match.setField(MatchType.DL_SRC, srcMac);
414         match.setField(MatchType.DL_DST, dstMac);
415         match.setField(MatchType.DL_TYPE, ethertype);
416         match.setField(MatchType.DL_VLAN, vlan);
417         match.setField(MatchType.DL_VLAN_PR, vlanPr);
418         match.setField(MatchType.NW_SRC, srcIP, ipMasks);
419         match.setField(MatchType.NW_DST, dstIP, ipMaskd);
420         match.setField(MatchType.NW_TOS, tos);
421         match.setField(MatchType.NW_PROTO, proto);
422         match.setField(MatchType.TP_SRC, src);
423         match.setField(MatchType.TP_DST, dst);
424
425         Match cloned = match.clone();
426
427         // Make sure all values are equals
428         for (MatchType type : MatchType.values()) {
429             if (match.isPresent(type)) {
430                 if (!match.getField(type).equals(cloned.getField(type))) {
431                     Assert.assertTrue(match.getField(type).equals(cloned.getField(type)));
432                 }
433             }
434         }
435
436         // Make sure none of the fields couples are pointing to the same
437         // reference
438         MatchField a = null, b = null;
439         for (MatchType type : MatchType.values()) {
440             a = match.getField(type);
441             b = cloned.getField(type);
442             if (a != null && b != null) {
443                 Assert.assertFalse(a == b);
444             }
445         }
446
447         Assert.assertTrue(match.equals(cloned));
448
449         Assert.assertFalse(match.getField(MatchType.DL_SRC) == cloned.getField(MatchType.DL_SRC));
450         Assert.assertFalse(match.getField(MatchType.NW_DST) == cloned.getField(MatchType.NW_DST));
451         Assert.assertTrue(match.getField(MatchType.NW_DST).getMask()
452                 .equals(cloned.getField(MatchType.NW_DST).getMask()));
453         Assert.assertTrue(match.hashCode() == cloned.hashCode());
454     }
455
456     @Test
457     public void testFlip() throws Exception {
458         Node node = NodeCreator.createOFNode(7l);
459         NodeConnector port = NodeConnectorCreator.createOFNodeConnector((short) 24, node);
460         byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
461         byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
462         InetAddress srcIP = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
463         InetAddress dstIP = InetAddress.getByName("2001:420:281:1004:e123:e688:d655:a1b0");
464         InetAddress ipMasks = InetAddress.getByName("ffff:ffff:ffff:ffff:0:0:0:0");
465         InetAddress ipMaskd = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
466         short ethertype = EtherTypes.IPv6.shortValue();
467         short vlan = (short) 27;
468         byte vlanPr = (byte) 3;
469         Byte tos = 4;
470         byte proto = IPProtocols.UDP.byteValue();
471         short src = (short) 5500;
472         short dst = 80;
473
474         /*
475          * Create a SAL Flow aFlow
476          */
477         Match match = new Match();
478         match.setField(MatchType.IN_PORT, port);
479         match.setField(MatchType.DL_SRC, srcMac);
480         match.setField(MatchType.DL_DST, dstMac);
481         match.setField(MatchType.DL_TYPE, ethertype);
482         match.setField(MatchType.DL_VLAN, vlan);
483         match.setField(MatchType.DL_VLAN_PR, vlanPr);
484         match.setField(MatchType.NW_SRC, srcIP, ipMasks);
485         match.setField(MatchType.NW_DST, dstIP, ipMaskd);
486         match.setField(MatchType.NW_TOS, tos);
487         match.setField(MatchType.NW_PROTO, proto);
488         match.setField(MatchType.TP_SRC, src);
489         match.setField(MatchType.TP_DST, dst);
490
491         Match flipped = match.reverse();
492
493         Assert.assertTrue(match.getField(MatchType.DL_TYPE).equals(flipped.getField(MatchType.DL_TYPE)));
494         Assert.assertTrue(match.getField(MatchType.DL_VLAN).equals(flipped.getField(MatchType.DL_VLAN)));
495
496         Assert.assertTrue(match.getField(MatchType.DL_DST).getValue()
497                 .equals(flipped.getField(MatchType.DL_SRC).getValue()));
498         Assert.assertTrue(match.getField(MatchType.DL_DST).getMask() == flipped.getField(MatchType.DL_SRC).getMask());
499
500         Assert.assertTrue(match.getField(MatchType.NW_DST).getValue()
501                 .equals(flipped.getField(MatchType.NW_SRC).getValue()));
502         Assert.assertTrue(match.getField(MatchType.NW_DST).getMask() == flipped.getField(MatchType.NW_SRC).getMask());
503
504         Assert.assertTrue(match.getField(MatchType.TP_DST).getValue()
505                 .equals(flipped.getField(MatchType.TP_SRC).getValue()));
506         Assert.assertTrue(match.getField(MatchType.TP_DST).getMask() == flipped.getField(MatchType.TP_SRC).getMask());
507
508         Match flipflip = flipped.reverse().reverse();
509         Assert.assertTrue(flipflip.equals(flipped));
510
511     }
512
513     @Test
514     public void testVlanNone() throws Exception {
515         // The value 0 is used to indicate that no VLAN ID is set
516         short vlan = (short) 0;
517         MatchField field = new MatchField(MatchType.DL_VLAN, vlan);
518
519         Assert.assertTrue(field != null);
520         Assert.assertTrue(field.getValue().equals(new Short(vlan)));
521         Assert.assertTrue(field.isValid());
522     }
523
524     @Test
525     public void testIntersection() throws UnknownHostException {
526         Short ethType = Short.valueOf((short)0x800);
527         InetAddress ip1 = InetAddress.getByName("1.1.1.1");
528         InetAddress ip2 = InetAddress.getByName("1.1.1.0");
529         InetAddress ipm2 = InetAddress.getByName("255.255.255.0");
530         InetAddress ip3 = InetAddress.getByName("1.3.0.0");
531         InetAddress ipm3 = InetAddress.getByName("255.255.0.0");
532         InetAddress ip4 = InetAddress.getByName("1.3.4.4");
533         InetAddress ipm4 = InetAddress.getByName("255.255.255.0");
534
535         Match m1 = new Match();
536         m1.setField(MatchType.DL_TYPE, ethType);
537         m1.setField(MatchType.NW_SRC, ip1);
538
539         Match m2 = new Match();
540         m2.setField(MatchType.DL_TYPE, ethType);
541         m2.setField(MatchType.NW_SRC, ip2, ipm2);
542
543         Match m3 = new Match();
544         m3.setField(MatchType.DL_TYPE, ethType);
545         m3.setField(MatchType.NW_SRC, ip3, ipm3);
546         m3.setField(MatchType.NW_PROTO, IPProtocols.TCP.byteValue());
547
548         Match m3r = m3.reverse();
549         Assert.assertTrue(m3.intersetcs(m3r));
550
551         Assert.assertTrue(m1.intersetcs(m2));
552         Assert.assertTrue(m2.intersetcs(m1));
553         Assert.assertFalse(m1.intersetcs(m3));
554         Assert.assertTrue(m1.intersetcs(m3r));
555         Assert.assertFalse(m3.intersetcs(m1));
556         Assert.assertTrue(m3.intersetcs(m1.reverse()));
557         Assert.assertFalse(m2.intersetcs(m3));
558         Assert.assertFalse(m3.intersetcs(m2));
559         Assert.assertTrue(m2.intersetcs(m3r));
560
561
562         Match i = m1.getIntersection(m2);
563         Assert.assertTrue(((Short)i.getField(MatchType.DL_TYPE).getValue()).equals(ethType));
564         // Verify intersection of IP addresses is correct
565         Assert.assertTrue(((InetAddress)i.getField(MatchType.NW_SRC).getValue()).equals(ip1));
566         Assert.assertNull(i.getField(MatchType.NW_SRC).getMask());
567
568         // Empty set
569         i = m2.getIntersection(m3);
570         Assert.assertNull(i);
571
572         Match m4 = new Match();
573         m4.setField(MatchType.DL_TYPE, ethType);
574         m4.setField(MatchType.NW_PROTO, IPProtocols.TCP.byteValue());
575         m3.setField(MatchType.NW_SRC, ip4, ipm4);
576         Assert.assertTrue(m4.intersetcs(m3));
577
578         // Verify intersection of IP and IP mask addresses is correct
579         Match ii = m3.getIntersection(m4);
580         Assert.assertTrue(((InetAddress)ii.getField(MatchType.NW_SRC).getValue()).equals(ip4));
581         Assert.assertTrue(((InetAddress)ii.getField(MatchType.NW_SRC).getMask()).equals(ipm4));
582
583         Match m5 = new Match();
584         m5.setField(MatchType.DL_TYPE, ethType);
585         m3.setField(MatchType.NW_SRC, ip3, ipm3);
586         m5.setField(MatchType.NW_PROTO, IPProtocols.UDP.byteValue());
587         Assert.assertFalse(m5.intersetcs(m3));
588         Assert.assertFalse(m5.intersetcs(m4));
589         Assert.assertTrue(m5.intersetcs(m5));
590         Assert.assertFalse(m3.intersetcs(m5));
591         Assert.assertFalse(m4.intersetcs(m5));
592
593
594         Match i2 = m4.getIntersection(m3);
595         Assert.assertFalse(i2.getMatches() == 0);
596         Assert.assertFalse(i2.getMatchesList().isEmpty());
597         Assert.assertTrue(((InetAddress)i2.getField(MatchType.NW_SRC).getValue()).equals(ip3));
598         Assert.assertTrue(((InetAddress)i2.getField(MatchType.NW_SRC).getMask()).equals(ipm3));
599         Assert.assertTrue(((Byte)i2.getField(MatchType.NW_PROTO).getValue()).equals(IPProtocols.TCP.byteValue()));
600
601         byte src[] = {(byte)0, (byte)0xab,(byte)0xbc,(byte)0xcd,(byte)0xde,(byte)0xef};
602         byte dst[] = {(byte)0x10, (byte)0x11,(byte)0x12,(byte)0x13,(byte)0x14,(byte)0x15};
603         Short srcPort = (short)1024;
604         Short dstPort = (short)80;
605
606         // Check identity
607         Match m6 = new Match();
608         m6.setField(MatchType.DL_SRC, src);
609         m6.setField(MatchType.DL_DST, dst);
610         m6.setField(MatchType.NW_SRC, ip2, ipm2);
611         m6.setField(MatchType.NW_DST, ip3, ipm3);
612         m6.setField(MatchType.NW_PROTO, IPProtocols.UDP.byteValue());
613         m6.setField(MatchType.TP_SRC, srcPort);
614         m6.setField(MatchType.TP_DST, dstPort);
615         Assert.assertTrue(m6.intersetcs(m6));
616         Assert.assertTrue(m6.getIntersection(m6).equals(m6));
617
618         // Empty match, represents the universal set (all packets)
619         Match u = new Match();
620         Assert.assertTrue(m6.getIntersection(u).equals(m6));
621         Assert.assertTrue(u.getIntersection(m6).equals(m6));
622
623         // No intersection with null match, empty set
624         Assert.assertNull(m6.getIntersection(null));
625     }
626 }