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.Test;
16 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.MplsTc;
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.match.entries.grouping.MatchEntryBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.MplsTcCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.mpls.tc._case.MplsTcBuilder;
24 * Unit tests for OxmMplsTcSerializer.
26 * @author michal.polkorab
28 public class OxmMplsTcSerializerTest {
30 OxmMplsTcSerializer serializer = new OxmMplsTcSerializer();
33 * Test correct serialization.
36 public void testSerialize() {
37 MatchEntryBuilder builder = prepareMplsTcMatchEntry((short) 16);
39 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
40 serializer.serialize(builder.build(), buffer);
42 checkHeader(buffer, false);
43 assertEquals("Wrong value", 16, buffer.readUnsignedByte());
44 assertTrue("Unexpected data", buffer.readableBytes() == 0);
48 * Test correct header serialization.
51 public void testSerializeHeader() {
52 MatchEntryBuilder builder = prepareMplsTcHeader(false);
54 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
55 serializer.serializeHeader(builder.build(), buffer);
57 checkHeader(buffer, false);
58 assertTrue("Unexpected data", buffer.readableBytes() == 0);
62 * Test correct oxm-class return value.
65 public void testGetOxmClassCode() {
66 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());
70 * Test correct oxm-field return value.
73 public void getOxmFieldCode() {
74 assertEquals("Wrong oxm-class", OxmMatchConstants.MPLS_TC, serializer.getOxmFieldCode());
78 * Test correct value length return value.
81 public void testGetValueLength() {
82 assertEquals("Wrong value length", Byte.BYTES, serializer.getValueLength());
85 private static MatchEntryBuilder prepareMplsTcMatchEntry(short value) {
86 MatchEntryBuilder builder = prepareMplsTcHeader(false);
87 MplsTcCaseBuilder casebuilder = new MplsTcCaseBuilder();
88 MplsTcBuilder valueBuilder = new MplsTcBuilder();
89 valueBuilder.setTc(value);
90 casebuilder.setMplsTc(valueBuilder.build());
91 builder.setMatchEntryValue(casebuilder.build());
95 private static MatchEntryBuilder prepareMplsTcHeader(boolean hasMask) {
96 MatchEntryBuilder builder = new MatchEntryBuilder();
97 builder.setOxmClass(OpenflowBasicClass.class);
98 builder.setOxmMatchField(MplsTc.class);
99 builder.setHasMask(hasMask);
103 private static void checkHeader(ByteBuf buffer, boolean hasMask) {
104 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
105 short fieldAndMask = buffer.readUnsignedByte();
106 assertEquals("Wrong oxm-field", OxmMatchConstants.MPLS_TC, fieldAndMask >>> 1);
107 assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
108 assertEquals("Wrong length", Byte.BYTES, buffer.readUnsignedByte());