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.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 * @author michal.polkorab
29 public class OxmVlanPcpSerializerTest {
31 OxmVlanPcpSerializer serializer = new OxmVlanPcpSerializer();
34 * Test correct serialization
37 public void testSerialize() {
38 MatchEntryBuilder builder = prepareVlanPcpMatchEntry((short) 42);
40 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
41 serializer.serialize(builder.build(), buffer);
43 checkHeader(buffer, false);
44 assertEquals("Wrong value", 42, buffer.readUnsignedByte());
45 assertTrue("Unexpected data", buffer.readableBytes() == 0);
49 * Test correct header serialization
52 public void testSerializeHeader() {
53 MatchEntryBuilder builder = prepareVlanPcpHeader(false);
55 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
56 serializer.serializeHeader(builder.build(), buffer);
58 checkHeader(buffer, false);
59 assertTrue("Unexpected data", buffer.readableBytes() == 0);
63 * Test correct oxm-class return value
66 public void testGetOxmClassCode() {
67 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());
71 * Test correct oxm-field return value
74 public void getOxmFieldCode() {
75 assertEquals("Wrong oxm-class", OxmMatchConstants.VLAN_PCP, serializer.getOxmFieldCode());
79 * Test correct value length return value
82 public void testGetValueLength() {
83 assertEquals("Wrong value length", EncodeConstants.SIZE_OF_BYTE_IN_BYTES, serializer.getValueLength());
86 private static MatchEntryBuilder prepareVlanPcpMatchEntry(short value) {
87 MatchEntryBuilder builder = prepareVlanPcpHeader(false);
88 VlanPcpCaseBuilder casebuilder = new VlanPcpCaseBuilder();
89 VlanPcpBuilder valueBuilder = new VlanPcpBuilder();
90 valueBuilder.setVlanPcp(value);
91 casebuilder.setVlanPcp(valueBuilder.build());
92 builder.setMatchEntryValue(casebuilder.build());
96 private static MatchEntryBuilder prepareVlanPcpHeader(boolean hasMask) {
97 MatchEntryBuilder builder = new MatchEntryBuilder();
98 builder.setOxmClass(OpenflowBasicClass.class);
99 builder.setOxmMatchField(VlanPcp.class);
100 builder.setHasMask(hasMask);
104 private static void checkHeader(ByteBuf buffer, boolean hasMask) {
105 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
106 short fieldAndMask = buffer.readUnsignedByte();
107 assertEquals("Wrong oxm-field", OxmMatchConstants.VLAN_PCP, fieldAndMask >>> 1);
108 assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
109 assertEquals("Wrong length", EncodeConstants.SIZE_OF_BYTE_IN_BYTES, buffer.readUnsignedByte());