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.IsidMatchEntry;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.IsidMatchEntryBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntry;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntryBuilder;
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.PbbIsid;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntriesBuilder;
29 * @author michal.polkorab
32 public class OxmPbbIsidSerializerTest {
34 OxmPbbIsidSerializer serializer = new OxmPbbIsidSerializer();
37 * Test correct serialization
40 public void testSerializeWithMask() {
41 MatchEntriesBuilder builder = preparePbbIsidMatchEntry(false, 12345);
43 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
44 serializer.serialize(builder.build(), buffer);
46 checkHeader(buffer, false);
47 assertEquals("Wrong value", 12345, buffer.readUnsignedMedium());
48 assertTrue("Unexpected data", buffer.readableBytes() == 0);
52 * Test correct serialization
55 public void testSerializeWithoutMask() {
56 MatchEntriesBuilder builder = preparePbbIsidMatchEntry(true, 6789);
58 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
59 serializer.serialize(builder.build(), buffer);
61 checkHeader(buffer, true);
62 assertEquals("Wrong value", 6789, buffer.readUnsignedMedium());
63 byte[] tmp = new byte[3];
64 buffer.readBytes(tmp);
65 Assert.assertArrayEquals("Wrong mask", new byte[]{0, 15, 10}, tmp);
66 assertTrue("Unexpected data", buffer.readableBytes() == 0);
70 * Test correct header serialization
73 public void testSerializeHeaderWithoutMask() {
74 MatchEntriesBuilder builder = preparePbbIsidHeader(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 = preparePbbIsidHeader(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.PBB_ISID, serializer.getOxmFieldCode());
114 * Test correct value length return value
117 public void testGetValueLength() {
118 assertEquals("Wrong value length", EncodeConstants.SIZE_OF_3_BYTES, serializer.getValueLength());
122 private static MatchEntriesBuilder preparePbbIsidMatchEntry(boolean hasMask, long value) {
123 MatchEntriesBuilder builder = preparePbbIsidHeader(hasMask);
125 MaskMatchEntryBuilder maskBuilder = new MaskMatchEntryBuilder();
126 maskBuilder.setMask(new byte[]{0, 15, 10});
127 builder.addAugmentation(MaskMatchEntry.class, maskBuilder.build());
129 IsidMatchEntryBuilder isidBuilder = new IsidMatchEntryBuilder();
130 isidBuilder.setIsid(value);
131 builder.addAugmentation(IsidMatchEntry.class, isidBuilder.build());
135 private static MatchEntriesBuilder preparePbbIsidHeader(boolean hasMask) {
136 MatchEntriesBuilder builder = new MatchEntriesBuilder();
137 builder.setOxmClass(OpenflowBasicClass.class);
138 builder.setOxmMatchField(PbbIsid.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.PBB_ISID, fieldAndMask >>> 1);
147 assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
149 assertEquals("Wrong length", 2 * EncodeConstants.SIZE_OF_3_BYTES, buffer.readUnsignedByte());
151 assertEquals("Wrong length", EncodeConstants.SIZE_OF_3_BYTES, buffer.readUnsignedByte());