Checkstyle enforcer
[controller.git] / opendaylight / sal / api / src / test / java / org / opendaylight / controller / sal / match / MatchTest.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.sal.match;
11
12 import java.net.InetAddress;
13 import java.net.UnknownHostException;
14 import java.util.Arrays;
15
16 import org.junit.Assert;
17 import org.junit.Test;
18 import org.opendaylight.controller.sal.core.Node;
19 import org.opendaylight.controller.sal.core.NodeConnector;
20 import org.opendaylight.controller.sal.match.Match;
21 import org.opendaylight.controller.sal.match.MatchField;
22 import org.opendaylight.controller.sal.match.MatchType;
23 import org.opendaylight.controller.sal.utils.EtherTypes;
24 import org.opendaylight.controller.sal.utils.IPProtocols;
25 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
26 import org.opendaylight.controller.sal.utils.NodeCreator;
27
28 public class MatchTest {
29     @Test
30     public void testMatchCreation() {
31         Node node = NodeCreator.createOFNode(7l);
32         NodeConnector port = NodeConnectorCreator.createOFNodeConnector(
33                 (short) 6, node);
34         MatchField field = new MatchField(MatchType.IN_PORT, port);
35
36         Assert.assertTrue(field != null);
37         Assert.assertTrue(field.getType() == MatchType.IN_PORT);
38         Assert.assertTrue((NodeConnector) field.getValue() == port);
39         Assert.assertTrue(field.isValid());
40
41         field = null;
42         field = new MatchField(MatchType.TP_SRC, Long.valueOf(23));
43         Assert.assertFalse(field.isValid());
44
45         field = null;
46         field = new MatchField(MatchType.TP_SRC, (long) 45);
47         Assert.assertFalse(field.isValid());
48
49         field = null;
50         field = new MatchField(MatchType.TP_SRC, 120000);
51         Assert.assertFalse(field.isValid());
52
53         byte mac[] = { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd,
54                 (byte) 11, (byte) 22 };
55         byte mask[] = { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
56                 (byte) 0xff, (byte) 0xff };
57         field = null;
58         field = new MatchField(MatchType.DL_SRC, mac, mask);
59         Assert.assertFalse(field.getValue() == null);
60
61         field = null;
62         field = new MatchField(MatchType.NW_TOS, (byte) 0x22, (byte) 0x3);
63         Assert.assertFalse(field.getValue() == null);
64     }
65
66     @Test
67     public void testMatchSetGet() {
68         Match x = new Match();
69         short val = 2346;
70         NodeConnector inPort = NodeConnectorCreator.createOFNodeConnector(val,
71                 NodeCreator.createOFNode(1l));
72         x.setField(MatchType.IN_PORT, inPort);
73         Assert.assertTrue(((NodeConnector) x.getField(MatchType.IN_PORT)
74                 .getValue()).equals(inPort));
75         Assert
76                 .assertTrue((Short) ((NodeConnector) x.getField(
77                         MatchType.IN_PORT).getValue()).getID() == val);
78     }
79
80     @Test
81     public void testMatchSetGetMAC() {
82         Match x = new Match();
83         byte mac[] = { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd,
84                 (byte) 11, (byte) 22 };
85         byte mac2[] = { (byte) 0xaa, (byte) 0xbb, 0, 0, 0, (byte) 0xbb };
86         byte mask1[] = { (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44,
87                 (byte) 0x55, (byte) 0x66 };
88         byte mask2[] = { (byte) 0xff, (byte) 0xff, (byte) 0, (byte) 0,
89                 (byte) 0, (byte) 0xff };
90
91         x.setField(MatchType.DL_SRC, mac.clone(), mask1);
92         x.setField(MatchType.DL_DST, mac2.clone(), mask2);
93         Assert.assertTrue(Arrays.equals(mac, (byte[]) x.getField(
94                 MatchType.DL_SRC).getValue()));
95         Assert.assertFalse(Arrays.equals((byte[]) x.getField(MatchType.DL_SRC)
96                 .getValue(), (byte[]) x.getField(MatchType.DL_DST).getValue()));
97         Assert.assertFalse(x.getField(MatchType.DL_SRC).getBitMask() == x
98                 .getField(MatchType.DL_DST).getBitMask());
99
100         x.setField(new MatchField(MatchType.DL_DST, mac.clone(), mask1));
101         Assert.assertTrue(Arrays.equals((byte[]) x.getField(MatchType.DL_SRC)
102                 .getValue(), (byte[]) x.getField(MatchType.DL_DST).getValue()));
103     }
104
105     @Test
106     public void testMatchSetGetNWAddr() throws UnknownHostException {
107         Match x = new Match();
108         String ip = "172.20.231.23";
109         InetAddress address = InetAddress.getByName(ip);
110         InetAddress mask = InetAddress.getByName("255.255.0.0");
111
112         x.setField(MatchType.NW_SRC, address, mask);
113         Assert.assertTrue(ip.equals(((InetAddress) x.getField(MatchType.NW_SRC)
114                 .getValue()).getHostAddress()));
115         Assert.assertTrue(x.getField(MatchType.NW_SRC).getMask().equals(mask));
116     }
117
118     @Test
119     public void testMatchSetGetEtherType() throws UnknownHostException {
120         Match x = new Match();
121
122         x.setField(MatchType.DL_TYPE, EtherTypes.QINQ.shortValue(),
123                 (short) 0xffff);
124         Assert.assertTrue(((Short) x.getField(MatchType.DL_TYPE).getValue())
125                 .equals(EtherTypes.QINQ.shortValue()));
126         Assert
127                 .assertFalse(x.getField(MatchType.DL_TYPE).getValue() == EtherTypes.QINQ);
128         Assert.assertFalse(x.getField(MatchType.DL_TYPE).getValue().equals(
129                 EtherTypes.QINQ));
130
131         x.setField(MatchType.DL_TYPE, EtherTypes.LLDP.shortValue(),
132                 (short) 0xffff);
133         Assert.assertTrue(((Short) x.getField(MatchType.DL_TYPE).getValue())
134                 .equals(EtherTypes.LLDP.shortValue()));
135         Assert.assertFalse(x.getField(MatchType.DL_TYPE).equals(
136                 EtherTypes.LLDP.intValue()));
137     }
138
139     @Test
140     public void testSetGetNwTos() {
141         Match x = new Match();
142         x.setField(MatchType.NW_TOS, (byte) 0xb, (byte) 0xf);
143
144         Byte t = new Byte((byte) 0xb);
145
146         Object o = x.getField(MatchType.NW_TOS).getValue();
147         Assert.assertTrue(o.equals(t));
148         Assert.assertTrue(o.equals((byte) 0xb));
149     }
150
151     @Test
152     public void testSetGetNwProto() {
153         Match x = new Match();
154         byte proto = (byte) 199;
155         x.setField(MatchType.NW_PROTO, proto, (byte) 0xff);
156
157         Object o = x.getField(MatchType.NW_PROTO).getValue();
158         Assert.assertTrue(o.equals(proto));
159     }
160
161     @Test
162     public void testMatchMask() {
163         Match x = new Match();
164         NodeConnector inPort = NodeConnectorCreator.createOFNodeConnector(
165                 (short) 6, NodeCreator.createOFNode(3l));
166         x.setField(MatchType.IN_PORT, inPort);
167         x.setField(MatchType.DL_VLAN, (short) 28, (short) 0xfff);
168         Assert.assertFalse(x.getMatches() == 0);
169         Assert
170                 .assertTrue(x.getMatches() == (MatchType.IN_PORT.getIndex() | MatchType.DL_VLAN
171                         .getIndex()));
172     }
173
174     @Test
175     public void testMatchBitMask() {
176         byte mac[] = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
177                 (byte) 22, (byte) 12 };
178         byte mask[] = { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
179                 (byte) 0xff, (byte) 0 };
180         NodeConnector inPort = NodeConnectorCreator.createOFNodeConnector(
181                 (short) 4095, NodeCreator.createOFNode(7l));
182
183         MatchField x = new MatchField(MatchType.IN_PORT, inPort);
184         Assert.assertTrue((x.getMask()) == null);
185
186         x = new MatchField(MatchType.DL_VLAN, (short) 255, (short) 0xff);
187         Assert.assertTrue(x.getBitMask() == 0xff);
188
189         x = new MatchField(MatchType.DL_SRC, mac, mask);
190         Assert.assertTrue(x.getMask().equals(mask));
191         Assert.assertTrue(x.getBitMask() == 0xffffffffff00L);
192     }
193
194     @Test
195     public void testNullMask() {
196         byte mac[] = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
197                 (byte) 22, (byte) 12 };
198         NodeConnector inPort = NodeConnectorCreator.createOFNodeConnector(
199                 (short) 2000, NodeCreator.createOFNode(7l));
200
201         MatchField x = new MatchField(MatchType.IN_PORT, inPort);
202         Assert.assertTrue(x.getBitMask() == 0);
203
204         x = new MatchField(MatchType.NW_PROTO, (byte) 17);
205         Assert.assertTrue(x.getBitMask() == 0xff);
206
207         x = new MatchField(MatchType.DL_VLAN, (short) 255);
208         Assert.assertTrue(x.getBitMask() == 0xfff);
209
210         x = new MatchField(MatchType.DL_SRC, mac);
211         Assert.assertTrue(x.getBitMask() == 0xffffffffffffL);
212     }
213
214     @Test
215     public void testEquality() throws Exception {
216         Node node = NodeCreator.createOFNode(7l);
217         NodeConnector port = NodeConnectorCreator.createOFNodeConnector(
218                 (short) 24, node);
219         NodeConnector port2 = NodeConnectorCreator.createOFNodeConnector(
220                 (short) 24, node);
221         byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
222                 (byte) 0x9a, (byte) 0xbc };
223         byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d,
224                 (byte) 0x5e, (byte) 0x6f };
225         byte srcMac2[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
226                 (byte) 0x9a, (byte) 0xbc };
227         byte dstMac2[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d,
228                 (byte) 0x5e, (byte) 0x6f };
229         InetAddress srcIP = InetAddress
230                 .getByName("2001:420:281:1004:407a:57f4:4d15:c355");
231         InetAddress dstIP = InetAddress
232                 .getByName("2001:420:281:1004:e123:e688:d655:a1b0");
233         InetAddress ipMask = InetAddress
234                 .getByName("ffff:ffff:ffff:ffff:0:0:0:0");
235         InetAddress ipMaskd = InetAddress
236                 .getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
237         InetAddress srcIP2 = InetAddress
238                 .getByName("2001:420:281:1004:407a:57f4:4d15:c355");
239         InetAddress dstIP2 = InetAddress
240                 .getByName("2001:420:281:1004:e123:e688:d655:a1b0");
241         InetAddress ipMask2 = InetAddress
242                 .getByName("ffff:ffff:ffff:ffff:0:0:0:0");
243         InetAddress ipMaskd2 = InetAddress
244                 .getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
245         short ethertype = EtherTypes.IPv6.shortValue();
246         short ethertype2 = EtherTypes.IPv6.shortValue();
247         short vlan = (short) 27, vlan2 = (short) 27;
248         byte vlanPr = (byte) 3, vlanPr2 = (byte) 3;
249         Byte tos = 4, tos2 = 4;
250         byte proto = IPProtocols.UDP.byteValue(), proto2 = IPProtocols.UDP
251                 .byteValue();
252         short src = (short) 5500, src2 = (short) 5500;
253         short dst = 80, dst2 = 80;
254
255         /*
256          * Create a SAL Flow aFlow
257          */
258         Match match1 = new Match();
259         Match match2 = new Match();
260         match1.setField(MatchType.IN_PORT, port);
261         match1.setField(MatchType.DL_SRC, srcMac);
262         match1.setField(MatchType.DL_DST, dstMac);
263         match1.setField(MatchType.DL_TYPE, ethertype);
264         match1.setField(MatchType.DL_VLAN, vlan);
265         match1.setField(MatchType.DL_VLAN_PR, vlanPr);
266         match1.setField(MatchType.NW_SRC, srcIP, ipMask);
267         match1.setField(MatchType.NW_DST, dstIP, ipMaskd);
268         match1.setField(MatchType.NW_TOS, tos);
269         match1.setField(MatchType.NW_PROTO, proto);
270         match1.setField(MatchType.TP_SRC, src);
271         match1.setField(MatchType.TP_DST, dst);
272
273         match2.setField(MatchType.IN_PORT, port2);
274         match2.setField(MatchType.DL_SRC, srcMac2);
275         match2.setField(MatchType.DL_DST, dstMac2);
276         match2.setField(MatchType.DL_TYPE, ethertype2);
277         match2.setField(MatchType.DL_VLAN, vlan2);
278         match2.setField(MatchType.DL_VLAN_PR, vlanPr2);
279         match2.setField(MatchType.NW_SRC, srcIP2, ipMask2);
280         match2.setField(MatchType.NW_DST, dstIP2, ipMaskd2);
281         match2.setField(MatchType.NW_TOS, tos2);
282         match2.setField(MatchType.NW_PROTO, proto2);
283         match2.setField(MatchType.TP_SRC, src2);
284         match2.setField(MatchType.TP_DST, dst2);
285
286         Assert.assertTrue(match1.equals(match2));
287
288         // Make sure all values are equals
289         for (MatchType type : MatchType.values()) {
290             if (match1.isPresent(type)) {
291                 Assert.assertTrue(match1.getField(type).equals(
292                         match2.getField(type)));
293             }
294         }
295
296         // Make none of the fields couples are pointing to the same reference
297         MatchField a = null, b = null;
298         for (MatchType type : MatchType.values()) {
299             a = match1.getField(type);
300             b = match2.getField(type);
301             if (a != null && b != null) {
302                 Assert.assertFalse(a == b);
303             }
304         }
305     }
306
307     @Test
308     public void testEqualityNetMask() throws Exception {
309
310         InetAddress srcIP = InetAddress.getByName("1.1.1.1");
311         InetAddress ipMask = InetAddress.getByName("255.255.255.255");
312         InetAddress srcIP2 = InetAddress.getByName("1.1.1.1");
313         InetAddress ipMask2 = null;
314         short ethertype = EtherTypes.IPv4.shortValue();
315         short ethertype2 = EtherTypes.IPv4.shortValue();
316
317         /*
318          * Create a SAL Flow aFlow
319          */
320         Match match1 = new Match();
321         Match match2 = new Match();
322
323         match1.setField(MatchType.DL_TYPE, ethertype);
324         match1.setField(MatchType.NW_SRC, srcIP, ipMask);
325
326         match2.setField(MatchType.DL_TYPE, ethertype2);
327         match2.setField(MatchType.NW_SRC, srcIP2, ipMask2);
328
329         Assert.assertTrue(match1.equals(match2));
330
331         ipMask2 = InetAddress.getByName("255.255.255.255");
332         match2.setField(MatchType.NW_SRC, srcIP2, ipMask2);
333
334         srcIP = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
335         srcIP2 = InetAddress.getByName("2001:420:281:1004:407a:57f4:4d15:c355");
336         ipMask = null;
337         ipMask2 = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
338         ethertype = EtherTypes.IPv6.shortValue();
339         ethertype2 = EtherTypes.IPv6.shortValue();
340
341         match1.setField(MatchType.DL_TYPE, ethertype);
342         match1.setField(MatchType.NW_SRC, srcIP, ipMask);
343
344         match2.setField(MatchType.DL_TYPE, ethertype2);
345         match2.setField(MatchType.NW_SRC, srcIP2, ipMask2);
346
347         Assert.assertTrue(match1.equals(match2));
348     }
349
350     @Test
351     public void testCloning() throws Exception {
352         Node node = NodeCreator.createOFNode(7l);
353         NodeConnector port = NodeConnectorCreator.createOFNodeConnector(
354                 (short) 24, node);
355         byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
356                 (byte) 0x9a, (byte) 0xbc };
357         byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d,
358                 (byte) 0x5e, (byte) 0x6f };
359         InetAddress srcIP = InetAddress
360                 .getByName("2001:420:281:1004:407a:57f4:4d15:c355");
361         InetAddress dstIP = InetAddress
362                 .getByName("2001:420:281:1004:e123:e688:d655:a1b0");
363         InetAddress ipMasks = InetAddress
364                 .getByName("ffff:ffff:ffff:ffff:0:0:0:0");
365         InetAddress ipMaskd = InetAddress
366                 .getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
367         short ethertype = EtherTypes.IPv6.shortValue();
368         short vlan = (short) 27;
369         byte vlanPr = (byte) 3;
370         Byte tos = 4;
371         byte proto = IPProtocols.UDP.byteValue();
372         short src = (short) 5500;
373         short dst = 80;
374
375         /*
376          * Create a SAL Flow aFlow
377          */
378         Match match = new Match();
379         match.setField(MatchType.IN_PORT, port);
380         match.setField(MatchType.DL_SRC, srcMac);
381         match.setField(MatchType.DL_DST, dstMac);
382         match.setField(MatchType.DL_TYPE, ethertype);
383         match.setField(MatchType.DL_VLAN, vlan);
384         match.setField(MatchType.DL_VLAN_PR, vlanPr);
385         match.setField(MatchType.NW_SRC, srcIP, ipMasks);
386         match.setField(MatchType.NW_DST, dstIP, ipMaskd);
387         match.setField(MatchType.NW_TOS, tos);
388         match.setField(MatchType.NW_PROTO, proto);
389         match.setField(MatchType.TP_SRC, src);
390         match.setField(MatchType.TP_DST, dst);
391
392         Match cloned = match.clone();
393
394         // Make sure all values are equals
395         for (MatchType type : MatchType.values()) {
396             if (match.isPresent(type)) {
397                 if (!match.getField(type).equals(cloned.getField(type))) {
398                     Assert.assertTrue(match.getField(type).equals(
399                             cloned.getField(type)));
400                 }
401             }
402         }
403
404         // Make sure none of the fields couples are pointing to the same reference
405         MatchField a = null, b = null;
406         for (MatchType type : MatchType.values()) {
407             a = match.getField(type);
408             b = cloned.getField(type);
409             if (a != null && b != null) {
410                 Assert.assertFalse(a == b);
411             }
412         }
413
414         Assert.assertTrue(match.equals(cloned));
415
416         Assert.assertFalse(match.getField(MatchType.DL_SRC) == cloned
417                 .getField(MatchType.DL_SRC));
418         Assert.assertFalse(match.getField(MatchType.NW_DST) == cloned
419                 .getField(MatchType.NW_DST));
420         Assert.assertTrue(match.getField(MatchType.NW_DST).getMask().equals(
421                 cloned.getField(MatchType.NW_DST).getMask()));
422     }
423
424     @Test
425     public void testFlip() throws Exception {
426         Node node = NodeCreator.createOFNode(7l);
427         NodeConnector port = NodeConnectorCreator.createOFNodeConnector(
428                 (short) 24, node);
429         byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
430                 (byte) 0x9a, (byte) 0xbc };
431         byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d,
432                 (byte) 0x5e, (byte) 0x6f };
433         InetAddress srcIP = InetAddress
434                 .getByName("2001:420:281:1004:407a:57f4:4d15:c355");
435         InetAddress dstIP = InetAddress
436                 .getByName("2001:420:281:1004:e123:e688:d655:a1b0");
437         InetAddress ipMasks = InetAddress
438                 .getByName("ffff:ffff:ffff:ffff:0:0:0:0");
439         InetAddress ipMaskd = InetAddress
440                 .getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
441         short ethertype = EtherTypes.IPv6.shortValue();
442         short vlan = (short) 27;
443         byte vlanPr = (byte) 3;
444         Byte tos = 4;
445         byte proto = IPProtocols.UDP.byteValue();
446         short src = (short) 5500;
447         short dst = 80;
448
449         /*
450          * Create a SAL Flow aFlow
451          */
452         Match match = new Match();
453         match.setField(MatchType.IN_PORT, port);
454         match.setField(MatchType.DL_SRC, srcMac);
455         match.setField(MatchType.DL_DST, dstMac);
456         match.setField(MatchType.DL_TYPE, ethertype);
457         match.setField(MatchType.DL_VLAN, vlan);
458         match.setField(MatchType.DL_VLAN_PR, vlanPr);
459         match.setField(MatchType.NW_SRC, srcIP, ipMasks);
460         match.setField(MatchType.NW_DST, dstIP, ipMaskd);
461         match.setField(MatchType.NW_TOS, tos);
462         match.setField(MatchType.NW_PROTO, proto);
463         match.setField(MatchType.TP_SRC, src);
464         match.setField(MatchType.TP_DST, dst);
465
466         Match flipped = match.reverse();
467
468         Assert.assertTrue(match.getField(MatchType.DL_TYPE).equals(
469                 flipped.getField(MatchType.DL_TYPE)));
470         Assert.assertTrue(match.getField(MatchType.DL_VLAN).equals(
471                 flipped.getField(MatchType.DL_VLAN)));
472
473         Assert.assertTrue(match.getField(MatchType.DL_DST).getValue().equals(
474                 flipped.getField(MatchType.DL_SRC).getValue()));
475         Assert.assertTrue(match.getField(MatchType.DL_DST).getMask() == flipped
476                 .getField(MatchType.DL_SRC).getMask());
477
478         Assert.assertTrue(match.getField(MatchType.NW_DST).getValue().equals(
479                 flipped.getField(MatchType.NW_SRC).getValue()));
480         Assert.assertTrue(match.getField(MatchType.NW_DST).getMask() == flipped
481                 .getField(MatchType.NW_SRC).getMask());
482
483         Assert.assertTrue(match.getField(MatchType.TP_DST).getValue().equals(
484                 flipped.getField(MatchType.TP_SRC).getValue()));
485         Assert.assertTrue(match.getField(MatchType.TP_DST).getMask() == flipped
486                 .getField(MatchType.TP_SRC).getMask());
487
488         Match flipflip = flipped.reverse().reverse();
489         Assert.assertTrue(flipflip.equals(flipped));
490
491     }
492 }