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.oxm.rev150225.OpenflowBasicClass;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.VlanVid;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.VlanVidCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.vlan.vid._case.VlanVidBuilder;
27 * @author michal.polkorab
30 public class OxmVlanVidSerializerTest {
32 OxmVlanVidSerializer serializer = new OxmVlanVidSerializer();
35 * Test correct serialization
38 public void testSerializeWithCfiBitSet() {
39 MatchEntryBuilder builder = prepareVlanVidMatchEntry(false, true);
41 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
42 serializer.serialize(builder.build(), buffer);
44 checkHeader(buffer, false);
45 assertEquals("Wrong value", 4596, buffer.readUnsignedShort());
46 assertTrue("Unexpected data", buffer.readableBytes() == 0);
50 * Test correct serialization
53 public void testSerializeWithoutCfiBitSet() {
54 MatchEntryBuilder builder = prepareVlanVidMatchEntry(true, false);
56 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
57 serializer.serialize(builder.build(), buffer);
59 checkHeader(buffer, true);
60 assertEquals("Wrong value", 500, buffer.readUnsignedShort());
61 byte[] tmp = new byte[2];
62 buffer.readBytes(tmp);
63 Assert.assertArrayEquals("Wrong mask", new byte[]{15, 15}, tmp);
64 assertTrue("Unexpected data", buffer.readableBytes() == 0);
68 * Test correct header serialization
71 public void testSerializeHeaderWithoutMask() {
72 MatchEntryBuilder builder = prepareVlanVidHeader(false);
74 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
75 serializer.serializeHeader(builder.build(), buffer);
77 checkHeader(buffer, false);
78 assertTrue("Unexpected data", buffer.readableBytes() == 0);
82 * Test correct header serialization
85 public void testSerializeHeaderWithMask() {
86 MatchEntryBuilder builder = prepareVlanVidHeader(true);
88 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
89 serializer.serializeHeader(builder.build(), buffer);
91 checkHeader(buffer, true);
92 assertTrue("Unexpected data", buffer.readableBytes() == 0);
96 * Test correct oxm-class return value
99 public void testGetOxmClassCode() {
100 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());
104 * Test correct oxm-field return value
107 public void getOxmFieldCode() {
108 assertEquals("Wrong oxm-class", OxmMatchConstants.VLAN_VID, serializer.getOxmFieldCode());
112 * Test correct value length return value
115 public void testGetValueLength() {
116 assertEquals("Wrong value length", EncodeConstants.SIZE_OF_SHORT_IN_BYTES, serializer.getValueLength());
119 private static MatchEntryBuilder prepareVlanVidMatchEntry(boolean hasMask, boolean cfiBit) {
120 MatchEntryBuilder builder = prepareVlanVidHeader(hasMask);
121 VlanVidCaseBuilder casebuilder = new VlanVidCaseBuilder();
122 VlanVidBuilder valueBuilder = new VlanVidBuilder();
124 valueBuilder.setMask(new byte[]{15, 15});
126 valueBuilder.setVlanVid(500);
127 valueBuilder.setCfiBit(cfiBit);
128 casebuilder.setVlanVid(valueBuilder.build());
129 builder.setMatchEntryValue(casebuilder.build());
133 private static MatchEntryBuilder prepareVlanVidHeader(boolean hasMask) {
134 MatchEntryBuilder builder = new MatchEntryBuilder();
135 builder.setOxmClass(OpenflowBasicClass.class);
136 builder.setOxmMatchField(VlanVid.class);
137 builder.setHasMask(hasMask);
141 private static void checkHeader(ByteBuf buffer, boolean hasMask) {
142 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
143 short fieldAndMask = buffer.readUnsignedByte();
144 assertEquals("Wrong oxm-field", OxmMatchConstants.VLAN_VID, fieldAndMask >>> 1);
145 assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
147 assertEquals("Wrong length", EncodeConstants.SIZE_OF_INT_IN_BYTES, buffer.readUnsignedByte());
149 assertEquals("Wrong length", EncodeConstants.SIZE_OF_SHORT_IN_BYTES, buffer.readUnsignedByte());