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
9 package org.opendaylight.openflowjava.protocol.impl.serialization.match;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.PooledByteBufAllocator;
16 import org.junit.Assert;
17 import org.junit.Test;
18 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
19 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntry;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntryBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataMatchEntry;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataMatchEntryBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OpenflowBasicClass;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.TunnelId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntriesBuilder;
29 * @author michal.polkorab
32 public class OxmTunnelIdSerializerTest {
34 OxmTunnelIdSerializer serializer = new OxmTunnelIdSerializer();
37 * Test correct serialization
40 public void testSerializeWithoutMask() {
41 MatchEntriesBuilder builder = prepareMatchEntry(false, new byte[]{0, 1, 2, 3, 4, 5, 6, 7});
43 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
44 serializer.serialize(builder.build(), buffer);
46 checkHeader(buffer, false);
47 byte[] address = new byte[8];
48 buffer.readBytes(address);
49 Assert.assertArrayEquals("Wrong address", new byte[]{0, 1, 2, 3, 4, 5, 6, 7}, address);
50 assertTrue("Unexpected data", buffer.readableBytes() == 0);
54 * Test correct serialization
57 public void testSerializeWithMask() {
58 MatchEntriesBuilder builder = prepareMatchEntry(true, new byte[]{8, 9, 10, 11, 12, 13, 14, 15});
60 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
61 serializer.serialize(builder.build(), buffer);
63 checkHeader(buffer, true);
65 byte[] address = new byte[8];
66 buffer.readBytes(address);
67 Assert.assertArrayEquals("Wrong address", new byte[]{8, 9, 10, 11, 12, 13, 14, 15}, address);
68 byte[] tmp = new byte[8];
69 buffer.readBytes(tmp);
70 Assert.assertArrayEquals("Wrong mask", new byte[]{30, 30, 25, 25, 15, 15, 0, 0}, tmp);
71 assertTrue("Unexpected data", buffer.readableBytes() == 0);
75 * Test correct header serialization
78 public void testSerializeHeaderWithoutMask() {
79 MatchEntriesBuilder builder = prepareHeader(false);
81 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
82 serializer.serializeHeader(builder.build(), buffer);
84 checkHeader(buffer, false);
85 assertTrue("Unexpected data", buffer.readableBytes() == 0);
89 * Test correct header serialization
92 public void testSerializeHeaderWithMask() {
93 MatchEntriesBuilder builder = prepareHeader(true);
95 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
96 serializer.serializeHeader(builder.build(), buffer);
98 checkHeader(buffer, true);
99 assertTrue("Unexpected data", buffer.readableBytes() == 0);
103 * Test correct oxm-class return value
106 public void testGetOxmClassCode() {
107 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());
111 * Test correct oxm-field return value
114 public void getOxmFieldCode() {
115 assertEquals("Wrong oxm-class", OxmMatchConstants.TUNNEL_ID, serializer.getOxmFieldCode());
119 * Test correct value length return value
122 public void testGetValueLength() {
123 assertEquals("Wrong value length", EncodeConstants.SIZE_OF_LONG_IN_BYTES, serializer.getValueLength());
126 private static MatchEntriesBuilder prepareMatchEntry(boolean hasMask, byte[] value) {
127 MatchEntriesBuilder builder = prepareHeader(hasMask);
129 MaskMatchEntryBuilder maskBuilder = new MaskMatchEntryBuilder();
130 maskBuilder.setMask(new byte[]{30, 30, 25, 25, 15, 15, 0, 0});
131 builder.addAugmentation(MaskMatchEntry.class, maskBuilder.build());
133 MetadataMatchEntryBuilder metadataBuilder = new MetadataMatchEntryBuilder();
134 metadataBuilder.setMetadata(value);
135 builder.addAugmentation(MetadataMatchEntry.class, metadataBuilder.build());
139 private static MatchEntriesBuilder prepareHeader(boolean hasMask) {
140 MatchEntriesBuilder builder = new MatchEntriesBuilder();
141 builder.setOxmClass(OpenflowBasicClass.class);
142 builder.setOxmMatchField(TunnelId.class);
143 builder.setHasMask(hasMask);
147 private static void checkHeader(ByteBuf buffer, boolean hasMask) {
148 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
149 short fieldAndMask = buffer.readUnsignedByte();
150 assertEquals("Wrong oxm-field", OxmMatchConstants.TUNNEL_ID, fieldAndMask >>> 1);
151 assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
153 assertEquals("Wrong length", 2 * EncodeConstants.SIZE_OF_LONG_IN_BYTES, buffer.readUnsignedByte());
155 assertEquals("Wrong length", EncodeConstants.SIZE_OF_LONG_IN_BYTES, buffer.readUnsignedByte());