Merge "Move adsal into its own subdirectory."
[controller.git] / opendaylight / adsal / protocol_plugins / openflow / src / test / java / org / opendaylight / controller / protocol_plugin / openflow / vendorextension / v6extension / V6MatchTest.java
1 /*
2  * Copyright (c) 2014 NEC Corporation
3  * 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
7  * distribution, and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.fail;
14 import static org.openflow.protocol.OFMatch.OFPFW_ALL;
15 import static org.openflow.protocol.OFMatch.OFPFW_DL_VLAN;
16 import static org.openflow.protocol.OFMatch.OFPFW_DL_VLAN_PCP;
17 import static org.openflow.protocol.OFMatch.OFPFW_IN_PORT;
18
19 import java.nio.ByteBuffer;
20 import org.junit.Test;
21
22 /**
23  * JUnit test for {@link V6Match}.
24  */
25 public class V6MatchTest {
26     /**
27      * Header of a match entry for input port field without a mask.
28      * The vendor-specific value is 0, and the length of value is 2.
29      */
30     private static int  HEADER_INPUT_PORT = (0 << 9) | 2;
31
32     /**
33      * Header of a match entry for VLAN TCI field without a mask.
34      * The vendor-specific value is 4, and the length of value is 2.
35      */
36     private static int  HEADER_VLAN_TCI= (4 << 9) | 2;
37
38     /**
39      * Header of a match entry for VLAN TCI field with a mask.
40      * The vendor-specific value is 4, and the length of value is 4.
41      */
42     private static int  HEADER_VLAN_TCI_W = (4 << 9) | (1 << 8) | 4;
43
44     /**
45      * Length of a match entry for input port field.
46      * Header (4 bytes) + value (2 bytes) = 6 bytes.
47      */
48     private static short  MATCH_LEN_INPUT_PORT = 6;
49
50     /**
51      * Length of a match entry for VLAN TCI field without a mask.
52      * Header (4 bytes) + value (2 bytes) = 6 bytes.
53      */
54     private static short  MATCH_LEN_VLAN_TCI = 6;
55
56     /**
57      * Length of a match entry for VLAN TCI field with a mask.
58      * Header (4 bytes) + value (2 bytes) + bitmask (2 bytes) = 8 bytes.
59      */
60     private static short  MATCH_LEN_VLAN_TCI_WITH_MASK = 8;
61
62     /**
63      * Value of OFP_VLAN_NONE defined by OpenFlow 1.0.
64      */
65     private static final short  OFP_VLAN_NONE = (short)0xffff;
66
67     /**
68      * CFI bit in VLAN TCI field.
69      */
70     private static final int  VLAN_TCI_CFI = 1 << 12;
71
72     /**
73      * Test case for {@link V6Match#fromString(String)} about VLAN TCI field.
74      * This test passes values to "dl_vlan" and "dl_vpcp".
75      */
76     @Test
77     public void testFromStringVlanTci() {
78         // Test for "dl_vlan" using non OFP_VLAN_NONE values.
79         short vlans[] = {1, 10, 1000, 4095};
80         short mask = 0;
81         for (short vlan: vlans) {
82             V6Match match = new V6Match();
83             match.fromString("dl_vlan=" + vlan);
84             assertEquals(MATCH_LEN_VLAN_TCI_WITH_MASK, match.getIPv6MatchLen());
85             assertEquals(vlan, match.getDataLayerVirtualLan());
86             int wildcards = OFPFW_ALL & ~OFPFW_DL_VLAN;
87             assertEquals(wildcards, match.getWildcards());
88         }
89
90         // Test for "dl_vpcp".
91         byte pcps[] = {1, 3, 7};
92         for (byte pcp: pcps) {
93             V6Match match = new V6Match();
94             match.fromString("dl_vpcp=" + pcp);
95             assertEquals(MATCH_LEN_VLAN_TCI_WITH_MASK, match.getIPv6MatchLen());
96             assertEquals(pcp, match.getDataLayerVirtualLanPriorityCodePoint());
97             int wildcards = OFPFW_ALL & ~OFPFW_DL_VLAN_PCP;
98             assertEquals(wildcards, match.getWildcards());
99         }
100
101         // Set "dl_vlan" field firstly, "dl_vpcp" field secondly.
102         for (short vlan: vlans) {
103             for (byte pcp: pcps) {
104                 V6Match match = new V6Match();
105                 match.fromString("dl_vlan=" + vlan);
106                 match.fromString("dl_vpcp=" + pcp);
107                 assertEquals(MATCH_LEN_VLAN_TCI, match.getIPv6MatchLen());
108                 assertEquals(vlan, match.getDataLayerVirtualLan());
109                 assertEquals(pcp,
110                         match.getDataLayerVirtualLanPriorityCodePoint());
111             }
112         }
113
114         // Set "dl_vpcp" field firstly, "dl_vlan" field secondly.
115         for (short vlan: vlans) {
116             for (byte pcp: pcps) {
117                 V6Match match = new V6Match();
118                 match.fromString("dl_vpcp=" + pcp);
119                 match.fromString("dl_vlan=" + vlan);
120                 assertEquals(MATCH_LEN_VLAN_TCI, match.getIPv6MatchLen());
121                 assertEquals(vlan, match.getDataLayerVirtualLan());
122                 assertEquals(pcp,
123                         match.getDataLayerVirtualLanPriorityCodePoint());
124             }
125         }
126
127         // Test for OFP_VLAN_NONE when VLAN PCP is not set.
128         V6Match match = new V6Match();
129         match.fromString("dl_vlan=" + OFP_VLAN_NONE);
130         assertEquals(MATCH_LEN_VLAN_TCI, match.getIPv6MatchLen());
131         assertEquals(OFP_VLAN_NONE, match.getDataLayerVirtualLan());
132
133         // Test for OFP_VLAN_NONE when VLAN PCP is set.
134         match = new V6Match();
135         match.fromString("dl_vpcp=" + 1);
136         try {
137             match.fromString("dl_vlan=" + OFP_VLAN_NONE);
138             fail("Throwing exception was expected.");
139         } catch (IllegalArgumentException e) {
140             // Throwing exception was expected.
141         }
142     }
143
144     /**
145      * Test case for {@link V6Match#writeTo(ByteBuffer)} for VLAN TCI field.
146      */
147     @Test
148     public void testWriteToVlanTci() {
149         byte mask = 0;
150
151         // Set only VLAN ID.
152         short vlans[] = {1, 10, 1000, 4095};
153         for (short vlan: vlans) {
154             V6Match match = new V6Match();
155             match.setDataLayerVirtualLan(vlan, mask);
156             ByteBuffer data = ByteBuffer.allocate(10);
157             match.writeTo(data);
158             assertEquals(MATCH_LEN_VLAN_TCI_WITH_MASK, data.position());
159             data.flip();
160             // Header
161             assertEquals(HEADER_VLAN_TCI_W, data.getInt());
162             // Value
163             short expectedTci = (short) (VLAN_TCI_CFI | vlan);
164             assertEquals(expectedTci, data.getShort());
165             // Mask
166             short expectedMask = 0x1fff;
167             assertEquals(expectedMask, data.getShort());
168         }
169
170         // Set only VLAN PCP.
171         byte pcps[] = {1, 3, 7};
172         for (byte pcp: pcps) {
173             V6Match match = new V6Match();
174             match.setDataLayerVirtualLanPriorityCodePoint(pcp, mask);
175             ByteBuffer data = ByteBuffer.allocate(10);
176             match.writeTo(data);
177             assertEquals(MATCH_LEN_VLAN_TCI_WITH_MASK, data.position());
178             data.flip();
179             // Header
180             assertEquals(HEADER_VLAN_TCI_W, data.getInt());
181             // Value
182             short expectedTci = (short) (pcp << 13 | VLAN_TCI_CFI);
183             assertEquals(expectedTci, data.getShort());
184             // Mask
185             short expectedMask = (short) 0xf000;
186             assertEquals(expectedMask, data.getShort());
187         }
188
189         // Set both VLAN ID and PCP.
190         for (short vlan: vlans) {
191             for (byte pcp: pcps) {
192                 V6Match match = new V6Match();
193                 match.setDataLayerVirtualLan(vlan, mask);
194                 match.setDataLayerVirtualLanPriorityCodePoint(pcp, mask);
195                 ByteBuffer data = ByteBuffer.allocate(10);
196                 match.writeTo(data);
197                 assertEquals(MATCH_LEN_VLAN_TCI, data.position());
198                 data.flip();
199                 // Header
200                 assertEquals(HEADER_VLAN_TCI, data.getInt());
201                 // Value
202                 short expectedTci = (short) (pcp << 13 | VLAN_TCI_CFI | vlan);
203                 assertEquals(expectedTci, data.getShort());
204             }
205         }
206
207         // Set OFP_VLAN_NONE.
208         V6Match match = new V6Match();
209         match.setDataLayerVirtualLan(OFP_VLAN_NONE, mask);
210         ByteBuffer data = ByteBuffer.allocate(10);
211         match.writeTo(data);
212         assertEquals(MATCH_LEN_VLAN_TCI, data.position());
213         data.flip();
214         // Header
215         assertEquals(HEADER_VLAN_TCI, data.getInt());
216         // Value
217         assertEquals(0, data.getShort());
218     }
219
220     /**
221      * Test case for {@link V6Match#writeTo(ByteBuffer)} for input port field.
222      */
223     @Test
224     public void testWriteToInputPort() {
225         // Set input port.
226         short ports[] = {1, 10, 100, 1000};
227         for (short port: ports) {
228             V6Match match = new V6Match();
229             match.setInputPort(port, (short) 0);
230             ByteBuffer data = ByteBuffer.allocate(10);
231             match.writeTo(data);
232             assertEquals(MATCH_LEN_INPUT_PORT, data.position());
233             data.flip();
234             // Header
235             assertEquals(HEADER_INPUT_PORT, data.getInt());
236             // Value
237             assertEquals(port, data.getShort());
238         }
239     }
240
241     /**
242      * Test case for {@link V6Match#readFrom(ByteBuffer)} for VLAN TCI field.
243      */
244     @Test
245     public void testReadFromVlanTci() {
246         // Test for an exact match a TCI value with CFI=1.
247         // It matches packets that have an 802.1Q header with a specified
248         // VID and PCP.
249         short vlans[] = {1, 10, 1000, 4095};
250         byte pcps[] = {1, 3, 7};
251         for (short vlan: vlans) {
252             for (byte pcp: pcps) {
253                 ByteBuffer data = ByteBuffer.allocate(MATCH_LEN_VLAN_TCI);
254                 data.putInt(HEADER_VLAN_TCI);
255                 short tci = (short) (pcp << 13 | VLAN_TCI_CFI | vlan);
256                 data.putShort(tci);
257                 data.flip();
258
259                 V6Match match = new V6Match();
260                 match.readFrom(data);
261                 assertEquals(MATCH_LEN_VLAN_TCI, match.getIPv6MatchLen());
262                 assertEquals(pcp,
263                         match.getDataLayerVirtualLanPriorityCodePoint());
264                 assertEquals(vlan, match.getDataLayerVirtualLan());
265                 int wildcards = OFPFW_ALL & ~OFPFW_DL_VLAN_PCP & ~OFPFW_DL_VLAN;
266                 assertEquals(wildcards, match.getWildcards());
267             }
268         }
269
270         // Test with a specific VID and CFI=1 with mask=0x1fff.
271         // It matches packets that have an 802.1Q header with that VID
272         // and any PCP.
273         for (short vlan: vlans) {
274             ByteBuffer data = ByteBuffer.allocate(MATCH_LEN_VLAN_TCI_WITH_MASK);
275             data.putInt(HEADER_VLAN_TCI_W);
276             short tci = (short) (VLAN_TCI_CFI | vlan);
277             data.putShort(tci);
278             short mask = (short) 0x1fff;
279             data.putShort(mask);
280             data.flip();
281
282             V6Match match = new V6Match();
283             match.readFrom(data);
284             assertEquals(MATCH_LEN_VLAN_TCI_WITH_MASK, match.getIPv6MatchLen());
285             assertEquals(vlan, match.getDataLayerVirtualLan());
286             int wildcards = OFPFW_ALL & ~OFPFW_DL_VLAN;
287             assertEquals(wildcards, match.getWildcards());
288         }
289
290         // Test with a specific PCP and CFI=1 with mask=0xf000.
291         // It matches packets that have an 802.1Q header with that PCP
292         // and any VID.
293         for (byte pcp: pcps) {
294             ByteBuffer data = ByteBuffer.allocate(MATCH_LEN_VLAN_TCI_WITH_MASK);
295             data.putInt(HEADER_VLAN_TCI_W);
296             short tci = (short) (pcp << 13| VLAN_TCI_CFI);
297             data.putShort(tci);
298             short mask = (short) 0xf000;
299             data.putShort(mask);
300             data.flip();
301
302             V6Match match = new V6Match();
303             match.readFrom(data);
304             assertEquals(MATCH_LEN_VLAN_TCI_WITH_MASK, match.getIPv6MatchLen());
305             assertEquals(pcp, match.getDataLayerVirtualLanPriorityCodePoint());
306             int wildcards = OFPFW_ALL & ~OFPFW_DL_VLAN_PCP;
307             assertEquals(wildcards, match.getWildcards());
308         }
309
310         // Test for an exact match with 0.
311         // It matches only packets without an 802.1Q header.
312         ByteBuffer data = ByteBuffer.allocate(MATCH_LEN_VLAN_TCI);
313         data.putInt(HEADER_VLAN_TCI);
314         short tci = 0;
315         data.putShort(tci);
316         data.flip();
317
318         V6Match match = new V6Match();
319         match.readFrom(data);
320         assertEquals(MATCH_LEN_VLAN_TCI, match.getIPv6MatchLen());
321         assertEquals(OFP_VLAN_NONE, match.getDataLayerVirtualLan());
322         int wildcards = OFPFW_ALL & ~OFPFW_DL_VLAN;
323         assertEquals(wildcards, match.getWildcards());
324     }
325
326     /**
327      * Test case for {@link V6Match#readFrom(ByteBuffer)} for input port field.
328      */
329     @Test
330     public void testReadFromInputPort() {
331         // Set input port.
332         short ports[] = {1, 10, 100, 1000};
333         for (short port: ports) {
334             ByteBuffer data = ByteBuffer.allocate(MATCH_LEN_INPUT_PORT);
335             data.putInt(HEADER_INPUT_PORT);
336             data.putShort(port);
337             data.flip();
338
339             V6Match match = new V6Match();
340             match.readFrom(data);
341             assertEquals(MATCH_LEN_INPUT_PORT, match.getIPv6MatchLen());
342             assertEquals(port, match.getInputPort());
343             int wildcards = OFPFW_ALL & ~OFPFW_IN_PORT;
344             assertEquals(wildcards, match.getWildcards());
345         }
346     }
347
348     /**
349      * Test case for {@link V6Match#setDataLayerVirtualLan(short, short)}.
350      */
351     @Test
352     public void testSetDataLayerVirtualLan() {
353         short vlans[] = {1, 10, 1000, 4095};
354         short mask = 0;
355         for (short vlan: vlans) {
356             V6Match match = new V6Match();
357             match.setDataLayerVirtualLan(vlan, mask);
358             assertEquals(MATCH_LEN_VLAN_TCI_WITH_MASK, match.getIPv6MatchLen());
359             assertEquals(vlan, match.getDataLayerVirtualLan());
360         }
361
362         // Test for OFP_VLAN_NONE.
363         V6Match match = new V6Match();
364         match.setDataLayerVirtualLan(OFP_VLAN_NONE, mask);
365         assertEquals(MATCH_LEN_VLAN_TCI, match.getIPv6MatchLen());
366         assertEquals(OFP_VLAN_NONE, match.getDataLayerVirtualLan());
367     }
368
369     /**
370      * Test case for
371      * {@link V6Match#setDataLayerVirtualLanPriorityCodePoint(byte, byte)}.
372      */
373     @Test
374     public void testSetDataLayerVirtualLanPriorityCodePoint() {
375         byte pcps[] = {1, 3, 7};
376         byte mask = 0;
377         for (byte pcp: pcps) {
378             V6Match match = new V6Match();
379             match.setDataLayerVirtualLanPriorityCodePoint(pcp, mask);
380             assertEquals(MATCH_LEN_VLAN_TCI_WITH_MASK, match.getIPv6MatchLen());
381             assertEquals(pcp, match.getDataLayerVirtualLanPriorityCodePoint());
382         }
383     }
384
385     /**
386      * Test case for setter methods for VLAN TCI field.
387      *
388      * This test case calls {@link V6Match#setDataLayerVirtualLan(short, short)}
389      * and {@link V6Match#setDataLayerVirtualLanPriorityCodePoint(byte, byte)}.
390      */
391     @Test
392     public void testSetVlanTCI() {
393         short vlans[] = {1, 10, 1000, 4095};
394         byte pcps[] = {1, 3, 7};
395         byte mask = 0;
396
397         // Call setDataLayerVirtualLan(short, short) firstly,
398         // and setDataLayerVirtualLanPriorityCodePoint(byte, byte) secondly,
399         for (short vlan: vlans) {
400             for (byte pcp: pcps) {
401                 V6Match match = new V6Match();
402                 match.setDataLayerVirtualLan(vlan, mask);
403                 match.setDataLayerVirtualLanPriorityCodePoint(pcp, mask);
404                 assertEquals(MATCH_LEN_VLAN_TCI, match.getIPv6MatchLen());
405                 assertEquals(vlan, match.getDataLayerVirtualLan());
406                 assertEquals(pcp,
407                              match.getDataLayerVirtualLanPriorityCodePoint());
408             }
409         }
410
411         // Call setDataLayerVirtualLanPriorityCodePoint(byte, byte) firstly,
412         // and setDataLayerVirtualLan(short, short) secondly.
413         for (short vlan: vlans) {
414             for (byte pcp: pcps) {
415                 V6Match match = new V6Match();
416                 match.setDataLayerVirtualLanPriorityCodePoint(pcp, mask);
417                 match.setDataLayerVirtualLan(vlan, mask);
418                 assertEquals(MATCH_LEN_VLAN_TCI, match.getIPv6MatchLen());
419                 assertEquals(vlan, match.getDataLayerVirtualLan());
420                 assertEquals(pcp,
421                              match.getDataLayerVirtualLanPriorityCodePoint());
422             }
423         }
424
425         // Test for setting OFP_VLAN_NONE when VLAN PCP is set.
426         for (byte pcp: pcps) {
427             V6Match match = new V6Match();
428             match.setDataLayerVirtualLanPriorityCodePoint(pcp, mask);
429             try {
430                 match.setDataLayerVirtualLan(OFP_VLAN_NONE, mask);
431             } catch (IllegalStateException e) {
432                 // Throwing exception was expected.
433             }
434         }
435
436         // Test for set VLAN PCP when OFP_VLAN_NONE is set to VLAN match.
437         for (byte pcp: pcps) {
438             V6Match match = new V6Match();
439             match.setDataLayerVirtualLan(OFP_VLAN_NONE, mask);
440             try {
441                 match.setDataLayerVirtualLanPriorityCodePoint(pcp, mask);
442             } catch (IllegalStateException e) {
443                 // Throwing exception was expected.
444             }
445         }
446     }
447
448     /**
449      * Test case for {@link V6Match#setInputPort(short, short)}.
450      */
451     @Test
452     public void testSetInputPort() {
453         short ports[] = {1, 10, 100, 1000};
454         for (short port: ports) {
455             V6Match match = new V6Match();
456             match.setInputPort(port, (short) 0);
457             assertEquals(MATCH_LEN_INPUT_PORT, match.getIPv6MatchLen());
458             assertEquals(port, match.getInputPort());
459         }
460     }
461 }