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.augments.rev131002.MaskMatchEntry;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntryBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanVidMatchEntry;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanVidMatchEntryBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OpenflowBasicClass;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.VlanVid;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntriesBuilder;
29 * @author michal.polkorab
32 public class OxmVlanVidSerializerTest {
34 OxmVlanVidSerializer serializer = new OxmVlanVidSerializer();
37 * Test correct serialization
40 public void testSerializeWithCfiBitSet() {
41 MatchEntriesBuilder builder = prepareVlanVidMatchEntry(false, true);
43 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
44 serializer.serialize(builder.build(), buffer);
46 checkHeader(buffer, false);
47 assertEquals("Wrong value", 4596, buffer.readUnsignedShort());
48 assertTrue("Unexpected data", buffer.readableBytes() == 0);
52 * Test correct serialization
55 public void testSerializeWithoutCfiBitSet() {
56 MatchEntriesBuilder builder = prepareVlanVidMatchEntry(true, false);
58 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
59 serializer.serialize(builder.build(), buffer);
61 checkHeader(buffer, true);
62 assertEquals("Wrong value", 500, buffer.readUnsignedShort());
63 byte[] tmp = new byte[2];
64 buffer.readBytes(tmp);
65 Assert.assertArrayEquals("Wrong mask", new byte[]{15, 15}, tmp);
66 assertTrue("Unexpected data", buffer.readableBytes() == 0);
70 * Test correct header serialization
73 public void testSerializeHeaderWithoutMask() {
74 MatchEntriesBuilder builder = prepareVlanVidHeader(false);
76 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
77 serializer.serializeHeader(builder.build(), buffer);
79 checkHeader(buffer, false);
80 assertTrue("Unexpected data", buffer.readableBytes() == 0);
84 * Test correct header serialization
87 public void testSerializeHeaderWithMask() {
88 MatchEntriesBuilder builder = prepareVlanVidHeader(true);
90 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
91 serializer.serializeHeader(builder.build(), buffer);
93 checkHeader(buffer, true);
94 assertTrue("Unexpected data", buffer.readableBytes() == 0);
98 * Test correct oxm-class return value
101 public void testGetOxmClassCode() {
102 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());
106 * Test correct oxm-field return value
109 public void getOxmFieldCode() {
110 assertEquals("Wrong oxm-class", OxmMatchConstants.VLAN_VID, serializer.getOxmFieldCode());
114 * Test correct value length return value
117 public void testGetValueLength() {
118 assertEquals("Wrong value length", EncodeConstants.SIZE_OF_SHORT_IN_BYTES, serializer.getValueLength());
121 private static MatchEntriesBuilder prepareVlanVidMatchEntry(boolean hasMask, boolean cfiBit) {
122 MatchEntriesBuilder builder = prepareVlanVidHeader(hasMask);
124 MaskMatchEntryBuilder maskBuilder = new MaskMatchEntryBuilder();
125 maskBuilder.setMask(new byte[]{15, 15});
126 builder.addAugmentation(MaskMatchEntry.class, maskBuilder.build());
128 VlanVidMatchEntryBuilder vlanBuilder = new VlanVidMatchEntryBuilder();
129 vlanBuilder.setVlanVid(500);
130 vlanBuilder.setCfiBit(cfiBit);
131 builder.addAugmentation(VlanVidMatchEntry.class, vlanBuilder.build());
135 private static MatchEntriesBuilder prepareVlanVidHeader(boolean hasMask) {
136 MatchEntriesBuilder builder = new MatchEntriesBuilder();
137 builder.setOxmClass(OpenflowBasicClass.class);
138 builder.setOxmMatchField(VlanVid.class);
139 builder.setHasMask(hasMask);
143 private static void checkHeader(ByteBuf buffer, boolean hasMask) {
144 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
145 short fieldAndMask = buffer.readUnsignedByte();
146 assertEquals("Wrong oxm-field", OxmMatchConstants.VLAN_VID, fieldAndMask >>> 1);
147 assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
149 assertEquals("Wrong length", EncodeConstants.SIZE_OF_INT_IN_BYTES, buffer.readUnsignedByte());
151 assertEquals("Wrong length", EncodeConstants.SIZE_OF_SHORT_IN_BYTES, buffer.readUnsignedByte());