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.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 * @author michal.polkorab
30 public class OxmPbbIsidSerializerTest {
32 OxmPbbIsidSerializer serializer = new OxmPbbIsidSerializer();
35 * Test correct serialization
38 public void testSerializeWithMask() {
39 MatchEntryBuilder builder = preparePbbIsidMatchEntry(false, 12345);
41 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
42 serializer.serialize(builder.build(), buffer);
44 checkHeader(buffer, false);
45 assertEquals("Wrong value", 12345, buffer.readUnsignedMedium());
46 assertTrue("Unexpected data", buffer.readableBytes() == 0);
50 * Test correct serialization
53 public void testSerializeWithoutMask() {
54 MatchEntryBuilder builder = preparePbbIsidMatchEntry(true, 6789);
56 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
57 serializer.serialize(builder.build(), buffer);
59 checkHeader(buffer, true);
60 assertEquals("Wrong value", 6789, buffer.readUnsignedMedium());
61 byte[] tmp = new byte[3];
62 buffer.readBytes(tmp);
63 Assert.assertArrayEquals("Wrong mask", new byte[]{0, 15, 10}, tmp);
64 assertTrue("Unexpected data", buffer.readableBytes() == 0);
68 * Test correct header serialization
71 public void testSerializeHeaderWithoutMask() {
72 MatchEntryBuilder builder = preparePbbIsidHeader(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 = preparePbbIsidHeader(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.PBB_ISID, serializer.getOxmFieldCode());
112 * Test correct value length return value
115 public void testGetValueLength() {
116 assertEquals("Wrong value length", EncodeConstants.SIZE_OF_3_BYTES, serializer.getValueLength());
119 private static MatchEntryBuilder preparePbbIsidMatchEntry(boolean hasMask, long value) {
120 MatchEntryBuilder builder = preparePbbIsidHeader(hasMask);
121 PbbIsidCaseBuilder casebuilder = new PbbIsidCaseBuilder();
122 PbbIsidBuilder valueBuilder = new PbbIsidBuilder();
124 valueBuilder.setMask(new byte[]{0, 15, 10});
126 valueBuilder.setIsid(value);
127 casebuilder.setPbbIsid(valueBuilder.build());
128 builder.setMatchEntryValue(casebuilder.build());
132 private static MatchEntryBuilder preparePbbIsidHeader(boolean hasMask) {
133 MatchEntryBuilder builder = new MatchEntryBuilder();
134 builder.setOxmClass(OpenflowBasicClass.class);
135 builder.setOxmMatchField(PbbIsid.class);
136 builder.setHasMask(hasMask);
140 private static void checkHeader(ByteBuf buffer, boolean hasMask) {
141 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
142 short fieldAndMask = buffer.readUnsignedByte();
143 assertEquals("Wrong oxm-field", OxmMatchConstants.PBB_ISID, fieldAndMask >>> 1);
144 assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
146 assertEquals("Wrong length", 2 * EncodeConstants.SIZE_OF_3_BYTES, buffer.readUnsignedByte());
148 assertEquals("Wrong length", EncodeConstants.SIZE_OF_3_BYTES, buffer.readUnsignedByte());