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.Test;
\r
17 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
\r
18 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
\r
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ProtocolNumberMatchEntry;
\r
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ProtocolNumberMatchEntryBuilder;
\r
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.IpProto;
\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OpenflowBasicClass;
\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntriesBuilder;
\r
26 * @author michal.polkorab
\r
29 public class OxmIpProtoSerializerTest {
\r
31 OxmIpProtoSerializer serializer = new OxmIpProtoSerializer();
\r
34 * Test correct serialization
\r
37 public void testSerialize() {
\r
38 MatchEntriesBuilder builder = prepareMatchEntry((short) 123);
\r
40 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
\r
41 serializer.serialize(builder.build(), buffer);
\r
43 checkHeader(buffer, false);
\r
44 assertEquals("Wrong value", 123, buffer.readUnsignedByte());
\r
45 assertTrue("Unexpected data", buffer.readableBytes() == 0);
\r
49 * Test correct header serialization
\r
52 public void testSerializeHeader() {
\r
53 MatchEntriesBuilder builder = prepareHeader(false);
\r
55 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
\r
56 serializer.serializeHeader(builder.build(), buffer);
\r
58 checkHeader(buffer, false);
\r
59 assertTrue("Unexpected data", buffer.readableBytes() == 0);
\r
63 * Test correct oxm-class return value
\r
66 public void testGetOxmClassCode() {
\r
67 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());
\r
71 * Test correct oxm-field return value
\r
74 public void getOxmFieldCode() {
\r
75 assertEquals("Wrong oxm-class", OxmMatchConstants.IP_PROTO, serializer.getOxmFieldCode());
\r
79 * Test correct value length return value
\r
82 public void testGetValueLength() {
\r
83 assertEquals("Wrong value length", EncodeConstants.SIZE_OF_BYTE_IN_BYTES, serializer.getValueLength());
\r
86 private static MatchEntriesBuilder prepareMatchEntry(short value) {
\r
87 MatchEntriesBuilder builder = prepareHeader(false);
\r
88 ProtocolNumberMatchEntryBuilder protoBuilder = new ProtocolNumberMatchEntryBuilder();
\r
89 protoBuilder.setProtocolNumber(value);
\r
90 builder.addAugmentation(ProtocolNumberMatchEntry.class, protoBuilder.build());
\r
94 private static MatchEntriesBuilder prepareHeader(boolean hasMask) {
\r
95 MatchEntriesBuilder builder = new MatchEntriesBuilder();
\r
96 builder.setOxmClass(OpenflowBasicClass.class);
\r
97 builder.setOxmMatchField(IpProto.class);
\r
98 builder.setHasMask(hasMask);
\r
102 private static void checkHeader(ByteBuf buffer, boolean hasMask) {
\r
103 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
\r
104 short fieldAndMask = buffer.readUnsignedByte();
\r
105 assertEquals("Wrong oxm-field", OxmMatchConstants.IP_PROTO, fieldAndMask >>> 1);
\r
106 assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
\r
107 assertEquals("Wrong length", EncodeConstants.SIZE_OF_BYTE_IN_BYTES, buffer.readUnsignedByte());
\r