2 * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
\r
4 * This program and the accompanying materials are made available under the
\r
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
\r
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
\r
9 package org.opendaylight.openflowjava.protocol.impl.serialization.match;
\r
11 import static org.junit.Assert.assertEquals;
\r
12 import static org.junit.Assert.assertTrue;
\r
13 import io.netty.buffer.ByteBuf;
\r
14 import io.netty.buffer.PooledByteBufAllocator;
\r
16 import org.junit.Assert;
\r
17 import org.junit.Test;
\r
18 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
\r
19 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
\r
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntry;
\r
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntryBuilder;
\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanVidMatchEntry;
\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanVidMatchEntryBuilder;
\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OpenflowBasicClass;
\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.VlanVid;
\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntriesBuilder;
\r
29 * @author michal.polkorab
\r
32 public class OxmVlanVidSerializerTest {
\r
34 OxmVlanVidSerializer serializer = new OxmVlanVidSerializer();
\r
37 * Test correct serialization
\r
40 public void testSerializeWithCfiBitSet() {
\r
41 MatchEntriesBuilder builder = prepareVlanVidMatchEntry(false, true);
\r
43 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
\r
44 serializer.serialize(builder.build(), buffer);
\r
46 checkHeader(buffer, false);
\r
47 assertEquals("Wrong value", 4596, buffer.readUnsignedShort());
\r
48 assertTrue("Unexpected data", buffer.readableBytes() == 0);
\r
52 * Test correct serialization
\r
55 public void testSerializeWithoutCfiBitSet() {
\r
56 MatchEntriesBuilder builder = prepareVlanVidMatchEntry(true, false);
\r
58 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
\r
59 serializer.serialize(builder.build(), buffer);
\r
61 checkHeader(buffer, true);
\r
62 assertEquals("Wrong value", 500, buffer.readUnsignedShort());
\r
63 byte[] tmp = new byte[2];
\r
64 buffer.readBytes(tmp);
\r
65 Assert.assertArrayEquals("Wrong mask", new byte[]{15, 15}, tmp);
\r
66 assertTrue("Unexpected data", buffer.readableBytes() == 0);
\r
70 * Test correct header serialization
\r
73 public void testSerializeHeaderWithoutMask() {
\r
74 MatchEntriesBuilder builder = prepareVlanVidHeader(false);
\r
76 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
\r
77 serializer.serializeHeader(builder.build(), buffer);
\r
79 checkHeader(buffer, false);
\r
80 assertTrue("Unexpected data", buffer.readableBytes() == 0);
\r
84 * Test correct header serialization
\r
87 public void testSerializeHeaderWithMask() {
\r
88 MatchEntriesBuilder builder = prepareVlanVidHeader(true);
\r
90 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
\r
91 serializer.serializeHeader(builder.build(), buffer);
\r
93 checkHeader(buffer, true);
\r
94 assertTrue("Unexpected data", buffer.readableBytes() == 0);
\r
98 * Test correct oxm-class return value
\r
101 public void testGetOxmClassCode() {
\r
102 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());
\r
106 * Test correct oxm-field return value
\r
109 public void getOxmFieldCode() {
\r
110 assertEquals("Wrong oxm-class", OxmMatchConstants.VLAN_VID, serializer.getOxmFieldCode());
\r
114 * Test correct value length return value
\r
117 public void testGetValueLength() {
\r
118 assertEquals("Wrong value length", EncodeConstants.SIZE_OF_SHORT_IN_BYTES, serializer.getValueLength());
\r
121 private static MatchEntriesBuilder prepareVlanVidMatchEntry(boolean hasMask, boolean cfiBit) {
\r
122 MatchEntriesBuilder builder = prepareVlanVidHeader(hasMask);
\r
124 MaskMatchEntryBuilder maskBuilder = new MaskMatchEntryBuilder();
\r
125 maskBuilder.setMask(new byte[]{15, 15});
\r
126 builder.addAugmentation(MaskMatchEntry.class, maskBuilder.build());
\r
128 VlanVidMatchEntryBuilder vlanBuilder = new VlanVidMatchEntryBuilder();
\r
129 vlanBuilder.setVlanVid(500);
\r
130 vlanBuilder.setCfiBit(cfiBit);
\r
131 builder.addAugmentation(VlanVidMatchEntry.class, vlanBuilder.build());
\r
135 private static MatchEntriesBuilder prepareVlanVidHeader(boolean hasMask) {
\r
136 MatchEntriesBuilder builder = new MatchEntriesBuilder();
\r
137 builder.setOxmClass(OpenflowBasicClass.class);
\r
138 builder.setOxmMatchField(VlanVid.class);
\r
139 builder.setHasMask(hasMask);
\r
143 private static void checkHeader(ByteBuf buffer, boolean hasMask) {
\r
144 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
\r
145 short fieldAndMask = buffer.readUnsignedByte();
\r
146 assertEquals("Wrong oxm-field", OxmMatchConstants.VLAN_VID, fieldAndMask >>> 1);
\r
147 assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
\r
149 assertEquals("Wrong length", EncodeConstants.SIZE_OF_INT_IN_BYTES, buffer.readUnsignedByte());
\r
151 assertEquals("Wrong length", EncodeConstants.SIZE_OF_SHORT_IN_BYTES, buffer.readUnsignedByte());
\r