Merge "BUG-2329 Add test for anyxmls inside rpc resonse for netcfon-connector"
[controller.git] / opendaylight / adsal / sal / api / src / test / java / org / opendaylight / controller / sal / match / MatchExtensibleTest.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 import java.util.HashSet;
15 import java.util.Set;
16
17
18 import org.junit.Assert;
19 import org.junit.Test;
20 import org.opendaylight.controller.sal.core.Node;
21 import org.opendaylight.controller.sal.core.NodeConnector;
22 import org.opendaylight.controller.sal.match.extensible.DlDst;
23 import org.opendaylight.controller.sal.match.extensible.DlSrc;
24 import org.opendaylight.controller.sal.match.extensible.DlType;
25 import org.opendaylight.controller.sal.match.extensible.DlVlan;
26 import org.opendaylight.controller.sal.match.extensible.DlVlanPriority;
27 import org.opendaylight.controller.sal.match.extensible.InPort;
28 import org.opendaylight.controller.sal.match.extensible.Match;
29 import org.opendaylight.controller.sal.match.extensible.MatchField;
30 import org.opendaylight.controller.sal.match.extensible.NwDst;
31 import org.opendaylight.controller.sal.match.extensible.NwProtocol;
32 import org.opendaylight.controller.sal.match.extensible.NwSrc;
33 import org.opendaylight.controller.sal.match.extensible.NwTos;
34 import org.opendaylight.controller.sal.match.extensible.TpDst;
35 import org.opendaylight.controller.sal.match.extensible.TpSrc;
36 import org.opendaylight.controller.sal.utils.EtherTypes;
37 import org.opendaylight.controller.sal.utils.IPProtocols;
38 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
39 import org.opendaylight.controller.sal.utils.NodeCreator;
40
41 public class MatchExtensibleTest {
42     @Test
43     public void testMatchCreation() {
44         Node node = NodeCreator.createOFNode(7L);
45         NodeConnector port = NodeConnectorCreator.createOFNodeConnector((short) 6, node);
46         MatchField<?> field = new InPort(port);
47
48         Assert.assertTrue(field != null);
49         Assert.assertEquals(field.getType(), InPort.TYPE);
50         Assert.assertEquals(field.getValue(), port);
51         Assert.assertTrue(field.isValid());
52
53
54         byte mac[] = { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd, (byte) 11, (byte) 22 };
55         field = null;
56         field = new DlSrc(mac);
57         Assert.assertNotNull(field.getValue());
58
59         field = null;
60         field = new NwTos((byte) 0x22);
61         Assert.assertNotNull(field.getValue());
62     }
63
64     @Test
65     public void testMatchSetGet() {
66         Match x = new Match();
67         short val = 2346;
68         NodeConnector inPort = NodeConnectorCreator.createOFNodeConnector(val, NodeCreator.createOFNode(1L));
69         x.setField(new InPort(inPort));
70         Assert.assertEquals(x.getField(InPort.TYPE).getValue(), inPort);
71         Assert.assertTrue((Short) ((NodeConnector) x.getField(InPort.TYPE).getValue()).getID() == val);
72     }
73
74     @Test
75     public void testMatchSetGetMAC() {
76         Match x = new Match();
77         byte mac[] = { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd, (byte) 11, (byte) 22 };
78         byte mac2[] = { (byte) 0xaa, (byte) 0xbb, 0, 0, 0, (byte) 0xbb };
79
80         x.setField(new DlSrc(mac));
81         x.setField(new DlDst(mac2));
82         Assert.assertArrayEquals(mac, (byte[]) x.getField(DlSrc.TYPE).getValue());
83         Assert.assertFalse(Arrays.equals((byte[]) x.getField(DlSrc.TYPE).getValue(), (byte[]) x.getField(DlDst.TYPE)
84                 .getValue()));
85
86         x.setField(new DlDst(mac.clone()));
87         Assert.assertArrayEquals((byte[]) x.getField(DlSrc.TYPE).getValue(), (byte[]) x.getField(DlDst.TYPE).getValue());
88     }
89
90     @Test
91     public void testMatchSetGetNWAddr() throws UnknownHostException {
92         Match x = new Match();
93         String ip = "172.20.231.23";
94         InetAddress address = InetAddress.getByName(ip);
95         InetAddress mask = InetAddress.getByName("255.255.0.0");
96
97         x.setField(new NwSrc(address, mask));
98         Assert.assertEquals(address, x.getField(NwSrc.TYPE).getValue());
99         Assert.assertEquals(x.getField(NwSrc.TYPE).getMask(), mask);
100     }
101
102     @Test
103     public void testMatchSetGetEtherType() throws UnknownHostException {
104         Match x = new Match();
105
106         x.setField(new DlType(EtherTypes.QINQ.shortValue()));
107         Assert.assertEquals(x.getField(DlType.TYPE).getValue(), EtherTypes.QINQ.shortValue());
108
109         x.setField(new DlType(EtherTypes.LLDP.shortValue()));
110         Assert.assertEquals(x.getField(DlType.TYPE).getValue(), EtherTypes.LLDP.shortValue());
111         Assert.assertFalse(x.getField(DlType.TYPE).equals(EtherTypes.LLDP.intValue()));
112     }
113
114     @Test
115     public void testSetGetNwTos() {
116         Match x = new Match();
117         x.setField(new NwTos((byte) 0xb));
118
119         Byte t = new Byte((byte) 0xb);
120
121         Object o = x.getField(NwTos.TYPE).getValue();
122         Assert.assertEquals(o, t);
123         Assert.assertEquals(o, Byte.valueOf((byte)0xb));
124     }
125
126     @Test
127     public void testSetGetNwProto() {
128         Match x = new Match();
129         Byte proto = (byte) 199;
130         x.setField(new NwProtocol(proto));
131
132         Byte o = (Byte) x.getField(NwProtocol.TYPE).getValue();
133         Assert.assertEquals(o, proto);
134     }
135
136     @Test
137     public void testSetTpSrc() {
138         // Minimum value validation.
139         Match match = new Match();
140         short tp_src = 0;
141         match.setField(new TpSrc(tp_src));
142
143         Object o = match.getField(TpSrc.TYPE).getValue();
144         Assert.assertEquals(o, tp_src);
145
146         // Maximum value validation.
147         match = new Match();
148         tp_src = (short) 0xffff;
149         match.setField(new TpSrc(tp_src));
150
151         o = match.getField(TpSrc.TYPE).getValue();
152         Assert.assertEquals(o, tp_src);
153     }
154
155     @Test
156     public void testSetTpDst() {
157         // Minimum value validation.
158         Match match = new Match();
159         short tp_dst = 0;
160         match.setField(new TpDst(tp_dst));
161
162         Object o = match.getField(TpDst.TYPE).getValue();
163         Assert.assertTrue(o.equals(tp_dst));
164
165         // Maximum value validation.
166         match = new Match();
167         tp_dst = (short) 0xffff;
168         match.setField(new TpDst(tp_dst));
169
170         o = match.getField(TpDst.TYPE).getValue();
171         Assert.assertEquals(o, tp_dst);
172     }
173
174     @Test
175     public void testEquality() throws Exception {
176         Node node = NodeCreator.createOFNode(7L);
177         NodeConnector port = NodeConnectorCreator.createOFNodeConnector((short) 24, node);
178         NodeConnector port2 = NodeConnectorCreator.createOFNodeConnector((short) 24, node);
179         byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
180         byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
181         byte srcMac2[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
182         byte dstMac2[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
183         InetAddress srcIP = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
184         InetAddress dstIP = InetAddress.getByName("2001:420:281:1004:e123:e688:d655:a1b0");
185         InetAddress ipMask = InetAddress.getByName("ffff:ffff:ffff:ffff:0:0:0:0");
186         InetAddress ipMaskd = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
187         InetAddress ipMask2 = InetAddress.getByName("ffff:ffff:ffff:ffff:0:0:0:0");
188         InetAddress ipMaskd2 = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
189         short ethertype = EtherTypes.IPv6.shortValue();
190         short ethertype2 = EtherTypes.IPv6.shortValue();
191         short vlan = (short) 27, vlan2 = (short) 27;
192         byte vlanPr = (byte) 3, vlanPr2 = (byte) 3;
193         Byte tos = 4, tos2 = 4;
194         byte proto = IPProtocols.UDP.byteValue(), proto2 = IPProtocols.UDP.byteValue();
195         short src = (short) 5500, src2 = (short) 5500;
196         short dst = 80, dst2 = 80;
197
198         /*
199          * Create a SAL Flow aFlow
200          */
201         Match match1 = new Match();
202         Match match2 = new Match();
203         match1.setField(new InPort(port));
204         match1.setField(new DlSrc(srcMac));
205         match1.setField(new DlDst(dstMac));
206         match1.setField(new DlType(ethertype));
207         match1.setField(new DlVlan(vlan));
208         match1.setField(new DlVlanPriority(vlanPr));
209         match1.setField(new NwSrc(srcIP, ipMask));
210         match1.setField(new NwDst(dstIP, ipMaskd));
211         match1.setField(new NwTos(tos));
212         match1.setField(new NwProtocol(proto));
213         match1.setField(new TpSrc(src));
214         match1.setField(new TpDst(dst));
215
216         match2.setField(new InPort(port2));
217         match2.setField(new DlSrc(srcMac2));
218         match2.setField(new DlDst(dstMac2));
219         match2.setField(new DlType(ethertype2));
220         match2.setField(new DlVlan(vlan2));
221         match2.setField(new DlVlanPriority(vlanPr2));
222         match2.setField(new NwSrc(srcIP, ipMask2));
223         match2.setField(new NwDst(dstIP, ipMaskd2));
224         match2.setField(new NwTos(tos2));
225         match2.setField(new NwProtocol(proto2));
226         match2.setField(new TpSrc(src2));
227         match2.setField(new TpDst(dst2));
228
229         Assert.assertTrue(match1.equals(match2));
230
231         Set<String> allFields = new HashSet<String>(match1.getMatchesList());
232         allFields.addAll(match2.getMatchesList());
233         // Make sure all values are equals
234         for (String type : allFields) {
235             if (match1.isPresent(type)) {
236                 Assert.assertEquals(match1.getField(type), match2.getField(type));
237             }
238         }
239
240         // Make none of the fields couples are pointing to the same reference
241         MatchField<?> a = null, b = null;
242         for (String type : allFields) {
243             a = match1.getField(type);
244             b = match2.getField(type);
245             if (a != null && b != null) {
246                 Assert.assertFalse(a == b);
247             }
248         }
249     }
250
251     @Test
252     public void testEqualityNetMask() throws Exception {
253
254         InetAddress srcIP = InetAddress.getByName("1.1.1.1");
255         InetAddress ipMask = InetAddress.getByName("255.255.255.255");
256         InetAddress srcIP2 = InetAddress.getByName("1.1.1.1");
257         InetAddress ipMask2 = null;
258         short ethertype = EtherTypes.IPv4.shortValue();
259         short ethertype2 = EtherTypes.IPv4.shortValue();
260
261         /*
262          * Create a SAL Flow aFlow
263          */
264         Match match1 = new Match();
265         Match match2 = new Match();
266
267         match1.setField(new DlType(ethertype));
268         match1.setField(new NwSrc(srcIP, ipMask));
269
270         match2.setField(new DlType(ethertype2));
271         match2.setField(new NwSrc(srcIP2, ipMask2));
272
273         Assert.assertTrue(match1.equals(match2));
274
275         ipMask2 = InetAddress.getByName("255.255.255.255");
276         match2.setField(new NwSrc(srcIP2, ipMask2));
277
278         srcIP = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
279         srcIP2 = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
280         ipMask = null;
281         ipMask2 = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
282         ethertype = EtherTypes.IPv6.shortValue();
283         ethertype2 = EtherTypes.IPv6.shortValue();
284
285         match1.setField(new DlType(ethertype));
286         match1.setField(new NwSrc(srcIP, ipMask));
287
288         match2.setField(new DlType(ethertype2));
289         match2.setField(new NwSrc(srcIP2, ipMask2));
290
291         Assert.assertEquals(match1, match2);
292     }
293
294     @Test
295     public void testHashCodeWithReverseMatch() throws Exception {
296         InetAddress srcIP1 = InetAddress.getByName("1.1.1.1");
297         InetAddress ipMask1 = InetAddress.getByName("255.255.255.255");
298         InetAddress srcIP2 = InetAddress.getByName("2.2.2.2");
299         InetAddress ipMask2 = InetAddress.getByName("255.255.255.255");
300         MatchField<?> field1 = new NwSrc(srcIP1, ipMask1);
301         MatchField<?> field2 = new NwDst(srcIP2, ipMask2);
302         Match match1 = new Match();
303         match1.setField(field1);
304         match1.setField(field2);
305         Match match2 = match1.reverse();
306         Assert.assertFalse(match1.hashCode() == match2.hashCode());
307     }
308
309     @Test
310     public void testHashCode() throws Exception {
311         byte srcMac1[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
312         byte srcMac2[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
313         byte dstMac1[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
314         byte dstMac2[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
315         short ethertype = EtherTypes.IPv4.shortValue();
316         short ethertype2 = EtherTypes.IPv4.shortValue();
317         InetAddress srcIP1 = InetAddress.getByName("1.1.1.1");
318         InetAddress ipMask1 = InetAddress.getByName("255.255.255.255");
319         InetAddress srcIP2 = InetAddress.getByName("1.1.1.1");
320         InetAddress ipMask2 = InetAddress.getByName("255.255.255.255");
321
322         Match match1 = new Match();
323         Match match2 = new Match();
324
325         MatchField<?> field1 = new DlSrc(srcMac1);
326         MatchField<?> field2 = new DlSrc(srcMac2);
327         Assert.assertTrue(field1.hashCode() == field2.hashCode());
328
329         match1.setField(field1);
330         match2.setField(field2);
331         Assert.assertTrue(match1.hashCode() == match2.hashCode());
332
333         MatchField<?> field3 = new DlDst(dstMac1);
334         MatchField<?> field4 = new DlDst(dstMac2);
335         Assert.assertTrue(field3.hashCode() == field4.hashCode());
336
337         match1.setField(field3);
338         match2.setField(field4);
339         Assert.assertTrue(match1.hashCode() == match2.hashCode());
340
341         MatchField<?> field5 = new DlType(ethertype);
342         MatchField<?> field6 = new DlType(ethertype2);
343         Assert.assertTrue(field5.hashCode() == field6.hashCode());
344
345         match1.setField(field5);
346         match2.setField(field6);
347         Assert.assertTrue(match1.hashCode() == match2 .hashCode());
348
349         MatchField<?> field7 = new NwSrc(srcIP1, ipMask1);
350         MatchField<?> field8 = new NwSrc(srcIP2, ipMask2);
351         Assert.assertTrue(field7.hashCode() == field8.hashCode());
352
353         match1.setField(field7);
354         match2.setField(field8);
355         Assert.assertTrue(match1.hashCode() == match2.hashCode());
356
357     }
358
359     @Test
360     public void testCloning() throws Exception {
361         Node node = NodeCreator.createOFNode(7L);
362         NodeConnector port = NodeConnectorCreator.createOFNodeConnector((short) 24, node);
363         byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
364         byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
365         InetAddress srcIP = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
366         InetAddress dstIP = InetAddress.getByName("2001:420:281:1004:e123:e688:d655:a1b0");
367         InetAddress ipMasks = InetAddress.getByName("ffff:ffff:ffff:ffff:0:0:0:0");
368         InetAddress ipMaskd = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
369         short ethertype = EtherTypes.IPv6.shortValue();
370         short vlan = (short) 27;
371         byte vlanPr = (byte) 3;
372         Byte tos = 4;
373         byte proto = IPProtocols.UDP.byteValue();
374         short src = (short) 5500;
375         short dst = 80;
376
377         /*
378          * Create a SAL Flow aFlow
379          */
380         Match match = new Match();
381         match.setField(new InPort(port));
382         match.setField(new DlSrc(srcMac));
383         match.setField(new DlDst(dstMac));
384         match.setField(new DlType(ethertype));
385         match.setField(new DlVlan(vlan));
386         match.setField(new DlVlanPriority(vlanPr));
387         match.setField(new NwSrc(srcIP, ipMasks));
388         match.setField(new NwDst(dstIP, ipMaskd));
389         match.setField(new NwTos(tos));
390         match.setField(new NwProtocol(proto));
391         match.setField(new TpSrc(src));
392         match.setField(new TpDst(dst));
393
394         Match cloned = match.clone();
395
396         // Make sure all values are equals
397         for (String type : match.getMatchesList()) {
398             if (match.isPresent(type)) {
399                 if (!match.getField(type).equals(cloned.getField(type))) {
400                     Assert.assertEquals(match.getField(type), cloned.getField(type));
401                 }
402             }
403         }
404
405         // Make sure none of the fields couples are pointing to the same
406         // reference
407         MatchField<?> a = null, b = null;
408         for (String type : match.getMatchesList()) {
409             a = match.getField(type);
410             b = cloned.getField(type);
411             if (a != null && b != null) {
412                 Assert.assertFalse(a == b);
413             }
414         }
415
416         Assert.assertTrue(match.equals(cloned));
417
418         Assert.assertEquals(match.getField(DlSrc.TYPE), cloned.getField(DlSrc.TYPE));
419         Assert.assertEquals(match.getField(NwDst.TYPE), cloned.getField(NwDst.TYPE));
420         Assert.assertEquals(match.getField(NwDst.TYPE).getMask(), cloned.getField(NwDst.TYPE).getMask());
421         Assert.assertEquals(match.hashCode(), cloned.hashCode());
422     }
423
424     @Test
425     public void testFlip() throws Exception {
426         Node node = NodeCreator.createOFNode(7L);
427         NodeConnector port = NodeConnectorCreator.createOFNodeConnector((short) 24, node);
428         byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
429         byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
430         InetAddress srcIP = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
431         InetAddress dstIP = InetAddress.getByName("2001:420:281:1004:e123:e688:d655:a1b0");
432         InetAddress ipMasks = InetAddress.getByName("ffff:ffff:ffff:ffff:0:0:0:0");
433         InetAddress ipMaskd = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
434         short ethertype = EtherTypes.IPv6.shortValue();
435         short vlan = (short) 27;
436         byte vlanPr = (byte) 3;
437         Byte tos = 4;
438         byte proto = IPProtocols.UDP.byteValue();
439         short src = (short) 5500;
440         short dst = 80;
441
442         /*
443          * Create a SAL Flow aFlow
444          */
445         Match match = new Match();
446         match.setField(new InPort(port));
447         match.setField(new DlSrc(srcMac));
448         match.setField(new DlDst(dstMac));
449         match.setField(new DlType(ethertype));
450         match.setField(new DlVlan(vlan));
451         match.setField(new DlVlanPriority(vlanPr));
452         match.setField(new NwSrc(srcIP, ipMasks));
453         match.setField(new NwDst(dstIP, ipMaskd));
454         match.setField(new NwTos(tos));
455         match.setField(new NwProtocol(proto));
456         match.setField(new TpSrc(src));
457         match.setField(new TpDst(dst));
458
459         Match flipped = match.reverse();
460
461         Assert.assertEquals(match.getField(DlType.TYPE), flipped.getField(DlType.TYPE));
462         Assert.assertEquals(match.getField(DlVlan.TYPE), flipped.getField(DlVlan.TYPE));
463
464         Assert.assertArrayEquals((byte[]) match.getField(DlDst.TYPE).getValue(), (byte[]) flipped.getField(DlSrc.TYPE)
465                 .getValue());
466
467         Assert.assertEquals(match.getField(NwDst.TYPE).getValue(), flipped.getField(NwSrc.TYPE).getValue());
468
469         Assert.assertEquals(match.getField(TpDst.TYPE).getValue(), flipped.getField(TpSrc.TYPE).getValue());
470
471         Match flipflip = flipped.reverse().reverse();
472         Assert.assertEquals(flipflip, flipped);
473
474     }
475
476     @Test
477     public void testVlanNone() throws Exception {
478         // The value 0 is used to indicate that no VLAN ID is set
479         short vlan = (short) 0;
480         MatchField<?> field = new DlVlan(vlan);
481
482         Assert.assertTrue(field != null);
483         Assert.assertEquals(field.getValue(), new Short(vlan));
484         Assert.assertTrue(field.isValid());
485     }
486
487     @Test
488     public void testIntersection() throws UnknownHostException {
489         Short ethType = Short.valueOf((short)0x800);
490         InetAddress ip1 = InetAddress.getByName("1.1.1.1");
491         InetAddress ip2 = InetAddress.getByName("1.1.1.0");
492         InetAddress ipm2 = InetAddress.getByName("255.255.255.0");
493         InetAddress ip3 = InetAddress.getByName("1.3.0.0");
494         InetAddress ipm3 = InetAddress.getByName("255.255.0.0");
495         InetAddress ip4 = InetAddress.getByName("1.3.4.4");
496         InetAddress ipm4 = InetAddress.getByName("255.255.255.0");
497
498         Match m1 = new Match();
499         m1.setField(new DlType(ethType));
500         m1.setField(new NwSrc(ip1));
501
502         Match m2 = new Match();
503         m2.setField(new DlType(ethType));
504         m2.setField(new NwSrc(ip2, ipm2));
505
506         Match m3 = new Match();
507         m3.setField(new DlType(ethType));
508         m3.setField(new NwSrc(ip3, ipm3));
509         m3.setField(new NwProtocol(IPProtocols.TCP.byteValue()));
510
511         Match m3r = m3.reverse();
512         Assert.assertTrue(m3.intersetcs(m3r));
513
514         Assert.assertTrue(m1.intersetcs(m2));
515         Assert.assertTrue(m2.intersetcs(m1));
516         Assert.assertFalse(m1.intersetcs(m3));
517         Assert.assertTrue(m1.intersetcs(m3r));
518         Assert.assertFalse(m3.intersetcs(m1));
519         Assert.assertTrue(m3.intersetcs(m1.reverse()));
520         Assert.assertFalse(m2.intersetcs(m3));
521         Assert.assertFalse(m3.intersetcs(m2));
522         Assert.assertTrue(m2.intersetcs(m3r));
523
524
525         Match i = m1.getIntersection(m2);
526         Assert.assertTrue(((Short)i.getField(DlType.TYPE).getValue()).equals(ethType));
527         // Verify intersection of IP addresses is correct
528         Assert.assertTrue(((InetAddress)i.getField(NwSrc.TYPE).getValue()).equals(ip1));
529         Assert.assertNull(i.getField(NwSrc.TYPE).getMask());
530
531         // Empty set
532         i = m2.getIntersection(m3);
533         Assert.assertNull(i);
534
535         Match m4 = new Match();
536         m4.setField(new DlType(ethType));
537         m4.setField(new NwProtocol(IPProtocols.TCP.byteValue()));
538         m3.setField(new NwSrc(ip4, ipm4));
539         Assert.assertTrue(m4.intersetcs(m3));
540
541         // Verify intersection of IP and IP mask addresses is correct
542         Match ii = m3.getIntersection(m4);
543         Assert.assertTrue(((InetAddress)ii.getField(NwSrc.TYPE).getValue()).equals(ip4));
544         Assert.assertTrue(((InetAddress)ii.getField(NwSrc.TYPE).getMask()).equals(ipm4));
545
546         Match m5 = new Match();
547         m5.setField(new DlType(ethType));
548         m3.setField(new NwSrc(ip3, ipm3));
549         m5.setField(new NwProtocol(IPProtocols.UDP.byteValue()));
550         Assert.assertFalse(m5.intersetcs(m3));
551         Assert.assertFalse(m5.intersetcs(m4));
552         Assert.assertTrue(m5.intersetcs(m5));
553         Assert.assertFalse(m3.intersetcs(m5));
554         Assert.assertFalse(m4.intersetcs(m5));
555
556
557         Match i2 = m4.getIntersection(m3);
558         Assert.assertFalse(i2.isEmpty());
559         Assert.assertFalse(i2.getMatchesList().isEmpty());
560         Assert.assertTrue(((InetAddress)i2.getField(NwSrc.TYPE).getValue()).equals(ip3));
561         Assert.assertTrue(((InetAddress)i2.getField(NwSrc.TYPE).getMask()).equals(ipm3));
562         Assert.assertTrue(((Byte)i2.getField(NwProtocol.TYPE).getValue()).equals(IPProtocols.TCP.byteValue()));
563
564         byte src[] = {(byte)0, (byte)0xab,(byte)0xbc,(byte)0xcd,(byte)0xde,(byte)0xef};
565         byte dst[] = {(byte)0x10, (byte)0x11,(byte)0x12,(byte)0x13,(byte)0x14,(byte)0x15};
566         Short srcPort = (short)1024;
567         Short dstPort = (short)80;
568
569         // Check identity
570         Match m6 = new Match();
571         m6.setField(new DlSrc(src));
572         m6.setField(new DlDst(dst));
573         m6.setField(new NwSrc(ip2, ipm2));
574         m6.setField(new NwDst(ip3, ipm3));
575         m6.setField(new NwProtocol(IPProtocols.UDP.byteValue()));
576         m6.setField(new TpSrc(srcPort));
577         m6.setField(new TpDst(dstPort));
578         Assert.assertTrue(m6.intersetcs(m6));
579         Assert.assertTrue(m6.getIntersection(m6).equals(m6));
580
581         // Empty match, represents the universal set (all packets)
582         Match u = new Match();
583         Assert.assertEquals(m6.getIntersection(u), m6);
584         Assert.assertEquals(u.getIntersection(m6), m6);
585
586         // No intersection with null match, empty set
587         Assert.assertNull(m6.getIntersection(null));
588     }
589 }