Initial opendaylight infrastructure commit!!
[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 testCloning() throws Exception {
309         Node node = NodeCreator.createOFNode(7l);
310         NodeConnector port = NodeConnectorCreator.createOFNodeConnector(
311                 (short) 24, node);
312         byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
313                 (byte) 0x9a, (byte) 0xbc };
314         byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d,
315                 (byte) 0x5e, (byte) 0x6f };
316         InetAddress srcIP = InetAddress
317                 .getByName("2001:420:281:1004:407a:57f4:4d15:c355");
318         InetAddress dstIP = InetAddress
319                 .getByName("2001:420:281:1004:e123:e688:d655:a1b0");
320         InetAddress ipMasks = InetAddress
321                 .getByName("ffff:ffff:ffff:ffff:0:0:0:0");
322         InetAddress ipMaskd = InetAddress
323                 .getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
324         short ethertype = EtherTypes.IPv6.shortValue();
325         short vlan = (short) 27;
326         byte vlanPr = (byte) 3;
327         Byte tos = 4;
328         byte proto = IPProtocols.UDP.byteValue();
329         short src = (short) 5500;
330         short dst = 80;
331
332         /*
333          * Create a SAL Flow aFlow
334          */
335         Match match = new Match();
336         match.setField(MatchType.IN_PORT, port);
337         match.setField(MatchType.DL_SRC, srcMac);
338         match.setField(MatchType.DL_DST, dstMac);
339         match.setField(MatchType.DL_TYPE, ethertype);
340         match.setField(MatchType.DL_VLAN, vlan);
341         match.setField(MatchType.DL_VLAN_PR, vlanPr);
342         match.setField(MatchType.NW_SRC, srcIP, ipMasks);
343         match.setField(MatchType.NW_DST, dstIP, ipMaskd);
344         match.setField(MatchType.NW_TOS, tos);
345         match.setField(MatchType.NW_PROTO, proto);
346         match.setField(MatchType.TP_SRC, src);
347         match.setField(MatchType.TP_DST, dst);
348
349         Match cloned = match.clone();
350
351         // Make sure all values are equals
352         for (MatchType type : MatchType.values()) {
353             if (match.isPresent(type)) {
354                 if (!match.getField(type).equals(cloned.getField(type))) {
355                     Assert.assertTrue(match.getField(type).equals(
356                             cloned.getField(type)));
357                 }
358             }
359         }
360
361         // Make sure none of the fields couples are pointing to the same reference
362         MatchField a = null, b = null;
363         for (MatchType type : MatchType.values()) {
364             a = match.getField(type);
365             b = cloned.getField(type);
366             if (a != null && b != null) {
367                 Assert.assertFalse(a == b);
368             }
369         }
370
371         Assert.assertTrue(match.equals(cloned));
372
373         Assert.assertFalse(match.getField(MatchType.DL_SRC) == cloned
374                 .getField(MatchType.DL_SRC));
375         Assert.assertFalse(match.getField(MatchType.NW_DST) == cloned
376                 .getField(MatchType.NW_DST));
377         Assert.assertTrue(match.getField(MatchType.NW_DST).getMask().equals(
378                 cloned.getField(MatchType.NW_DST).getMask()));
379     }
380
381     @Test
382     public void testFlip() throws Exception {
383         Node node = NodeCreator.createOFNode(7l);
384         NodeConnector port = NodeConnectorCreator.createOFNodeConnector(
385                 (short) 24, node);
386         byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
387                 (byte) 0x9a, (byte) 0xbc };
388         byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d,
389                 (byte) 0x5e, (byte) 0x6f };
390         InetAddress srcIP = InetAddress
391                 .getByName("2001:420:281:1004:407a:57f4:4d15:c355");
392         InetAddress dstIP = InetAddress
393                 .getByName("2001:420:281:1004:e123:e688:d655:a1b0");
394         InetAddress ipMasks = InetAddress
395                 .getByName("ffff:ffff:ffff:ffff:0:0:0:0");
396         InetAddress ipMaskd = InetAddress
397                 .getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
398         short ethertype = EtherTypes.IPv6.shortValue();
399         short vlan = (short) 27;
400         byte vlanPr = (byte) 3;
401         Byte tos = 4;
402         byte proto = IPProtocols.UDP.byteValue();
403         short src = (short) 5500;
404         short dst = 80;
405
406         /*
407          * Create a SAL Flow aFlow
408          */
409         Match match = new Match();
410         match.setField(MatchType.IN_PORT, port);
411         match.setField(MatchType.DL_SRC, srcMac);
412         match.setField(MatchType.DL_DST, dstMac);
413         match.setField(MatchType.DL_TYPE, ethertype);
414         match.setField(MatchType.DL_VLAN, vlan);
415         match.setField(MatchType.DL_VLAN_PR, vlanPr);
416         match.setField(MatchType.NW_SRC, srcIP, ipMasks);
417         match.setField(MatchType.NW_DST, dstIP, ipMaskd);
418         match.setField(MatchType.NW_TOS, tos);
419         match.setField(MatchType.NW_PROTO, proto);
420         match.setField(MatchType.TP_SRC, src);
421         match.setField(MatchType.TP_DST, dst);
422
423         Match flipped = match.reverse();
424
425         Assert.assertTrue(match.getField(MatchType.DL_TYPE).equals(
426                 flipped.getField(MatchType.DL_TYPE)));
427         Assert.assertTrue(match.getField(MatchType.DL_VLAN).equals(
428                 flipped.getField(MatchType.DL_VLAN)));
429
430         Assert.assertTrue(match.getField(MatchType.DL_DST).getValue().equals(
431                 flipped.getField(MatchType.DL_SRC).getValue()));
432         Assert.assertTrue(match.getField(MatchType.DL_DST).getMask() == flipped
433                 .getField(MatchType.DL_SRC).getMask());
434
435         Assert.assertTrue(match.getField(MatchType.NW_DST).getValue().equals(
436                 flipped.getField(MatchType.NW_SRC).getValue()));
437         Assert.assertTrue(match.getField(MatchType.NW_DST).getMask() == flipped
438                 .getField(MatchType.NW_SRC).getMask());
439
440         Assert.assertTrue(match.getField(MatchType.TP_DST).getValue().equals(
441                 flipped.getField(MatchType.TP_SRC).getValue()));
442         Assert.assertTrue(match.getField(MatchType.TP_DST).getMask() == flipped
443                 .getField(MatchType.TP_SRC).getMask());
444
445         Match flipflip = flipped.reverse().reverse();
446         Assert.assertTrue(flipflip.equals(flipped));
447
448     }
449 }