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;
14 import io.netty.buffer.ByteBuf;
15 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.PbbIsid;
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.PbbIsidCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.pbb.isid._case.PbbIsidBuilder;
27 * Unit tests for OxmPbbIsidSerializer.
29 * @author michal.polkorab
31 public class OxmPbbIsidSerializerTest {
33 OxmPbbIsidSerializer serializer = new OxmPbbIsidSerializer();
36 * Test correct serialization.
39 public void testSerializeWithMask() {
40 MatchEntryBuilder builder = preparePbbIsidMatchEntry(false, 12345);
42 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
43 serializer.serialize(builder.build(), buffer);
45 checkHeader(buffer, false);
46 assertEquals("Wrong value", 12345, buffer.readUnsignedMedium());
47 assertTrue("Unexpected data", buffer.readableBytes() == 0);
51 * Test correct serialization.
54 public void testSerializeWithoutMask() {
55 MatchEntryBuilder builder = preparePbbIsidMatchEntry(true, 6789);
57 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
58 serializer.serialize(builder.build(), buffer);
60 checkHeader(buffer, true);
61 assertEquals("Wrong value", 6789, buffer.readUnsignedMedium());
62 byte[] tmp = new byte[3];
63 buffer.readBytes(tmp);
64 Assert.assertArrayEquals("Wrong mask", new byte[]{0, 15, 10}, tmp);
65 assertTrue("Unexpected data", buffer.readableBytes() == 0);
69 * Test correct header serialization.
72 public void testSerializeHeaderWithoutMask() {
73 MatchEntryBuilder builder = preparePbbIsidHeader(false);
75 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
76 serializer.serializeHeader(builder.build(), buffer);
78 checkHeader(buffer, false);
79 assertTrue("Unexpected data", buffer.readableBytes() == 0);
83 * Test correct header serialization.
86 public void testSerializeHeaderWithMask() {
87 MatchEntryBuilder builder = preparePbbIsidHeader(true);
89 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
90 serializer.serializeHeader(builder.build(), buffer);
92 checkHeader(buffer, true);
93 assertTrue("Unexpected data", buffer.readableBytes() == 0);
97 * Test correct oxm-class return value.
100 public void testGetOxmClassCode() {
101 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());
105 * Test correct oxm-field return value.
108 public void getOxmFieldCode() {
109 assertEquals("Wrong oxm-class", OxmMatchConstants.PBB_ISID, serializer.getOxmFieldCode());
113 * Test correct value length return value.
116 public void testGetValueLength() {
117 assertEquals("Wrong value length", EncodeConstants.SIZE_OF_3_BYTES, serializer.getValueLength());
120 private static MatchEntryBuilder preparePbbIsidMatchEntry(boolean hasMask, long value) {
121 final MatchEntryBuilder builder = preparePbbIsidHeader(hasMask);
122 PbbIsidCaseBuilder casebuilder = new PbbIsidCaseBuilder();
123 PbbIsidBuilder valueBuilder = new PbbIsidBuilder();
125 valueBuilder.setMask(new byte[]{0, 15, 10});
127 valueBuilder.setIsid(value);
128 casebuilder.setPbbIsid(valueBuilder.build());
129 builder.setMatchEntryValue(casebuilder.build());
133 private static MatchEntryBuilder preparePbbIsidHeader(boolean hasMask) {
134 MatchEntryBuilder builder = new MatchEntryBuilder();
135 builder.setOxmClass(OpenflowBasicClass.class);
136 builder.setOxmMatchField(PbbIsid.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.PBB_ISID, fieldAndMask >>> 1);
145 assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
147 assertEquals("Wrong length", 2 * EncodeConstants.SIZE_OF_3_BYTES, buffer.readUnsignedByte());
149 assertEquals("Wrong length", EncodeConstants.SIZE_OF_3_BYTES, buffer.readUnsignedByte());