2 * Copyright (c) 2014 NEC Corporation
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
10 package org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension;
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;
19 import java.nio.ByteBuffer;
20 import org.junit.Test;
23 * JUnit test for {@link V6Match}.
25 public class V6MatchTest {
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.
30 private static int HEADER_INPUT_PORT = (0 << 9) | 2;
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.
36 private static int HEADER_VLAN_TCI= (4 << 9) | 2;
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.
42 private static int HEADER_VLAN_TCI_W = (4 << 9) | (1 << 8) | 4;
45 * Length of a match entry for input port field.
46 * Header (4 bytes) + value (2 bytes) = 6 bytes.
48 private static short MATCH_LEN_INPUT_PORT = 6;
51 * Length of a match entry for VLAN TCI field without a mask.
52 * Header (4 bytes) + value (2 bytes) = 6 bytes.
54 private static short MATCH_LEN_VLAN_TCI = 6;
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.
60 private static short MATCH_LEN_VLAN_TCI_WITH_MASK = 8;
63 * Value of OFP_VLAN_NONE defined by OpenFlow 1.0.
65 private static final short OFP_VLAN_NONE = (short)0xffff;
68 * CFI bit in VLAN TCI field.
70 private static final int VLAN_TCI_CFI = 1 << 12;
73 * Test case for {@link V6Match#fromString(String)} about VLAN TCI field.
74 * This test passes values to "dl_vlan" and "dl_vpcp".
77 public void testFromStringVlanTci() {
78 // Test for "dl_vlan" using non OFP_VLAN_NONE values.
79 short vlans[] = {1, 10, 1000, 4095};
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());
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());
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());
110 match.getDataLayerVirtualLanPriorityCodePoint());
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());
123 match.getDataLayerVirtualLanPriorityCodePoint());
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());
133 // Test for OFP_VLAN_NONE when VLAN PCP is set.
134 match = new V6Match();
135 match.fromString("dl_vpcp=" + 1);
137 match.fromString("dl_vlan=" + OFP_VLAN_NONE);
138 fail("Throwing exception was expected.");
139 } catch (IllegalArgumentException e) {
140 // Throwing exception was expected.
145 * Test case for {@link V6Match#writeTo(ByteBuffer)} for VLAN TCI field.
148 public void testWriteToVlanTci() {
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);
158 assertEquals(MATCH_LEN_VLAN_TCI_WITH_MASK, data.position());
161 assertEquals(HEADER_VLAN_TCI_W, data.getInt());
163 short expectedTci = (short) (VLAN_TCI_CFI | vlan);
164 assertEquals(expectedTci, data.getShort());
166 short expectedMask = 0x1fff;
167 assertEquals(expectedMask, data.getShort());
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);
177 assertEquals(MATCH_LEN_VLAN_TCI_WITH_MASK, data.position());
180 assertEquals(HEADER_VLAN_TCI_W, data.getInt());
182 short expectedTci = (short) (pcp << 13 | VLAN_TCI_CFI);
183 assertEquals(expectedTci, data.getShort());
185 short expectedMask = (short) 0xf000;
186 assertEquals(expectedMask, data.getShort());
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);
197 assertEquals(MATCH_LEN_VLAN_TCI, data.position());
200 assertEquals(HEADER_VLAN_TCI, data.getInt());
202 short expectedTci = (short) (pcp << 13 | VLAN_TCI_CFI | vlan);
203 assertEquals(expectedTci, data.getShort());
207 // Set OFP_VLAN_NONE.
208 V6Match match = new V6Match();
209 match.setDataLayerVirtualLan(OFP_VLAN_NONE, mask);
210 ByteBuffer data = ByteBuffer.allocate(10);
212 assertEquals(MATCH_LEN_VLAN_TCI, data.position());
215 assertEquals(HEADER_VLAN_TCI, data.getInt());
217 assertEquals(0, data.getShort());
221 * Test case for {@link V6Match#writeTo(ByteBuffer)} for input port field.
224 public void testWriteToInputPort() {
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);
232 assertEquals(MATCH_LEN_INPUT_PORT, data.position());
235 assertEquals(HEADER_INPUT_PORT, data.getInt());
237 assertEquals(port, data.getShort());
242 * Test case for {@link V6Match#readFrom(ByteBuffer)} for VLAN TCI field.
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
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);
259 V6Match match = new V6Match();
260 match.readFrom(data);
261 assertEquals(MATCH_LEN_VLAN_TCI, match.getIPv6MatchLen());
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());
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
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);
278 short mask = (short) 0x1fff;
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());
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
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);
298 short mask = (short) 0xf000;
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());
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);
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());
327 * Test case for {@link V6Match#readFrom(ByteBuffer)} for input port field.
330 public void testReadFromInputPort() {
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);
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());
349 * Test case for {@link V6Match#setDataLayerVirtualLan(short, short)}.
352 public void testSetDataLayerVirtualLan() {
353 short vlans[] = {1, 10, 1000, 4095};
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());
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());
371 * {@link V6Match#setDataLayerVirtualLanPriorityCodePoint(byte, byte)}.
374 public void testSetDataLayerVirtualLanPriorityCodePoint() {
375 byte pcps[] = {1, 3, 7};
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());
386 * Test case for setter methods for VLAN TCI field.
388 * This test case calls {@link V6Match#setDataLayerVirtualLan(short, short)}
389 * and {@link V6Match#setDataLayerVirtualLanPriorityCodePoint(byte, byte)}.
392 public void testSetVlanTCI() {
393 short vlans[] = {1, 10, 1000, 4095};
394 byte pcps[] = {1, 3, 7};
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());
407 match.getDataLayerVirtualLanPriorityCodePoint());
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());
421 match.getDataLayerVirtualLanPriorityCodePoint());
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);
430 match.setDataLayerVirtualLan(OFP_VLAN_NONE, mask);
431 } catch (IllegalStateException e) {
432 // Throwing exception was expected.
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);
441 match.setDataLayerVirtualLanPriorityCodePoint(pcp, mask);
442 } catch (IllegalStateException e) {
443 // Throwing exception was expected.
449 * Test case for {@link V6Match#setInputPort(short, short)}.
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());