2 * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.openflowjava.protocol.impl.serialization.match;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.PooledByteBufAllocator;
15 import org.junit.Assert;
16 import org.junit.Test;
17 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OpenflowBasicClass;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.TunnelId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.TunnelIdCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.tunnel.id._case.TunnelIdBuilder;
25 * Unit tests for OxmTunnelIdSerializer.
27 * @author michal.polkorab
29 public class OxmTunnelIdSerializerTest {
31 OxmTunnelIdSerializer serializer = new OxmTunnelIdSerializer();
34 * Test correct serialization.
37 public void testSerializeWithoutMask() {
38 MatchEntryBuilder builder = prepareMatchEntry(false, new byte[]{0, 1, 2, 3, 4, 5, 6, 7});
40 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
41 serializer.serialize(builder.build(), buffer);
43 checkHeader(buffer, false);
44 byte[] address = new byte[8];
45 buffer.readBytes(address);
46 Assert.assertArrayEquals("Wrong address", new byte[]{0, 1, 2, 3, 4, 5, 6, 7}, address);
47 assertTrue("Unexpected data", buffer.readableBytes() == 0);
51 * Test correct serialization.
54 public void testSerializeWithMask() {
55 MatchEntryBuilder builder = prepareMatchEntry(true, new byte[]{8, 9, 10, 11, 12, 13, 14, 15});
57 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
58 serializer.serialize(builder.build(), buffer);
60 checkHeader(buffer, true);
62 byte[] address = new byte[8];
63 buffer.readBytes(address);
64 Assert.assertArrayEquals("Wrong address", new byte[]{8, 9, 10, 11, 12, 13, 14, 15}, address);
65 byte[] tmp = new byte[8];
66 buffer.readBytes(tmp);
67 Assert.assertArrayEquals("Wrong mask", new byte[]{30, 30, 25, 25, 15, 15, 0, 0}, tmp);
68 assertTrue("Unexpected data", buffer.readableBytes() == 0);
72 * Test correct header serialization.
75 public void testSerializeHeaderWithoutMask() {
76 MatchEntryBuilder builder = prepareHeader(false);
78 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
79 serializer.serializeHeader(builder.build(), buffer);
81 checkHeader(buffer, false);
82 assertTrue("Unexpected data", buffer.readableBytes() == 0);
86 * Test correct header serialization.
89 public void testSerializeHeaderWithMask() {
90 MatchEntryBuilder builder = prepareHeader(true);
92 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
93 serializer.serializeHeader(builder.build(), buffer);
95 checkHeader(buffer, true);
96 assertTrue("Unexpected data", buffer.readableBytes() == 0);
100 * Test correct oxm-class return value.
103 public void testGetOxmClassCode() {
104 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());
108 * Test correct oxm-field return value.
111 public void getOxmFieldCode() {
112 assertEquals("Wrong oxm-class", OxmMatchConstants.TUNNEL_ID, serializer.getOxmFieldCode());
116 * Test correct value length return value.
119 public void testGetValueLength() {
120 assertEquals("Wrong value length", Long.BYTES, serializer.getValueLength());
123 private static MatchEntryBuilder prepareMatchEntry(final boolean hasMask, final byte[] value) {
124 final MatchEntryBuilder builder = prepareHeader(hasMask);
125 TunnelIdCaseBuilder casebuilder = new TunnelIdCaseBuilder();
126 TunnelIdBuilder valueBuilder = new TunnelIdBuilder();
128 valueBuilder.setMask(new byte[]{30, 30, 25, 25, 15, 15, 0, 0});
130 valueBuilder.setTunnelId(value);
131 casebuilder.setTunnelId(valueBuilder.build());
132 builder.setMatchEntryValue(casebuilder.build());
136 private static MatchEntryBuilder prepareHeader(final boolean hasMask) {
137 MatchEntryBuilder builder = new MatchEntryBuilder();
138 builder.setOxmClass(OpenflowBasicClass.VALUE);
139 builder.setOxmMatchField(TunnelId.VALUE);
140 builder.setHasMask(hasMask);
144 private static void checkHeader(final ByteBuf buffer, final boolean hasMask) {
145 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
146 short fieldAndMask = buffer.readUnsignedByte();
147 assertEquals("Wrong oxm-field", OxmMatchConstants.TUNNEL_ID, fieldAndMask >>> 1);
148 assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
150 assertEquals("Wrong length", 2 * Long.BYTES, buffer.readUnsignedByte());
152 assertEquals("Wrong length", Long.BYTES, buffer.readUnsignedByte());