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;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.PooledByteBufAllocator;
16 import org.junit.Test;
17 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
18 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OpenflowBasicClass;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.VlanPcp;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.VlanPcpCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.vlan.pcp._case.VlanPcpBuilder;
26 * Unit tests for OxmVlanPcpSerializer.
28 * @author michal.polkorab
30 public class OxmVlanPcpSerializerTest {
32 OxmVlanPcpSerializer serializer = new OxmVlanPcpSerializer();
35 * Test correct serialization.
38 public void testSerialize() {
39 MatchEntryBuilder builder = prepareVlanPcpMatchEntry((short) 42);
41 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
42 serializer.serialize(builder.build(), buffer);
44 checkHeader(buffer, false);
45 assertEquals("Wrong value", 42, buffer.readUnsignedByte());
46 assertTrue("Unexpected data", buffer.readableBytes() == 0);
50 * Test correct header serialization.
53 public void testSerializeHeader() {
54 MatchEntryBuilder builder = prepareVlanPcpHeader(false);
56 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
57 serializer.serializeHeader(builder.build(), buffer);
59 checkHeader(buffer, false);
60 assertTrue("Unexpected data", buffer.readableBytes() == 0);
64 * Test correct oxm-class return value.
67 public void testGetOxmClassCode() {
68 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());
72 * Test correct oxm-field return value.
75 public void getOxmFieldCode() {
76 assertEquals("Wrong oxm-class", OxmMatchConstants.VLAN_PCP, serializer.getOxmFieldCode());
80 * Test correct value length return value.
83 public void testGetValueLength() {
84 assertEquals("Wrong value length", EncodeConstants.SIZE_OF_BYTE_IN_BYTES, serializer.getValueLength());
87 private static MatchEntryBuilder prepareVlanPcpMatchEntry(short value) {
88 MatchEntryBuilder builder = prepareVlanPcpHeader(false);
89 VlanPcpCaseBuilder casebuilder = new VlanPcpCaseBuilder();
90 VlanPcpBuilder valueBuilder = new VlanPcpBuilder();
91 valueBuilder.setVlanPcp(value);
92 casebuilder.setVlanPcp(valueBuilder.build());
93 builder.setMatchEntryValue(casebuilder.build());
97 private static MatchEntryBuilder prepareVlanPcpHeader(boolean hasMask) {
98 MatchEntryBuilder builder = new MatchEntryBuilder();
99 builder.setOxmClass(OpenflowBasicClass.class);
100 builder.setOxmMatchField(VlanPcp.class);
101 builder.setHasMask(hasMask);
105 private static void checkHeader(ByteBuf buffer, boolean hasMask) {
106 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
107 short fieldAndMask = buffer.readUnsignedByte();
108 assertEquals("Wrong oxm-field", OxmMatchConstants.VLAN_PCP, fieldAndMask >>> 1);
109 assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
110 assertEquals("Wrong length", EncodeConstants.SIZE_OF_BYTE_IN_BYTES, buffer.readUnsignedByte());