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.EthTypeMatchEntry;
\r
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EthTypeMatchEntryBuilder;
\r
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.EtherType;
\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.EthType;
\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OpenflowBasicClass;
\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntriesBuilder;
\r
27 * @author michal.polkorab
\r
30 public class OxmEthTypeSerializerTest {
\r
32 OxmEthTypeSerializer serializer = new OxmEthTypeSerializer();
\r
35 * Test correct serialization
\r
38 public void testSerialize() {
\r
39 MatchEntriesBuilder builder = prepareEthTypeMatchEntry(65535);
\r
41 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
\r
42 serializer.serialize(builder.build(), buffer);
\r
44 checkHeader(buffer, false);
\r
45 assertEquals("Wrong value", 65535, buffer.readUnsignedShort());
\r
46 assertTrue("Unexpected data", buffer.readableBytes() == 0);
\r
50 * Test correct header serialization
\r
53 public void testSerializeHeader() {
\r
54 MatchEntriesBuilder builder = prepareEthTypeHeader(false);
\r
56 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
\r
57 serializer.serializeHeader(builder.build(), buffer);
\r
59 checkHeader(buffer, false);
\r
60 assertTrue("Unexpected data", buffer.readableBytes() == 0);
\r
64 * Test correct oxm-class return value
\r
67 public void testGetOxmClassCode() {
\r
68 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());
\r
72 * Test correct oxm-field return value
\r
75 public void getOxmFieldCode() {
\r
76 assertEquals("Wrong oxm-class", OxmMatchConstants.ETH_TYPE, serializer.getOxmFieldCode());
\r
80 * Test correct value length return value
\r
83 public void testGetValueLength() {
\r
84 assertEquals("Wrong value length", EncodeConstants.SIZE_OF_SHORT_IN_BYTES, serializer.getValueLength());
\r
88 private static MatchEntriesBuilder prepareEthTypeMatchEntry(int type) {
\r
89 MatchEntriesBuilder builder = prepareEthTypeHeader(false);
\r
90 EthTypeMatchEntryBuilder typeBuilder = new EthTypeMatchEntryBuilder();
\r
91 typeBuilder.setEthType(new EtherType(type));
\r
92 builder.addAugmentation(EthTypeMatchEntry.class, typeBuilder.build());
\r
96 private static MatchEntriesBuilder prepareEthTypeHeader(boolean hasMask) {
\r
97 MatchEntriesBuilder builder = new MatchEntriesBuilder();
\r
98 builder.setOxmClass(OpenflowBasicClass.class);
\r
99 builder.setOxmMatchField(EthType.class);
\r
100 builder.setHasMask(hasMask);
\r
104 private static void checkHeader(ByteBuf buffer, boolean hasMask) {
\r
105 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
\r
106 short fieldAndMask = buffer.readUnsignedByte();
\r
107 assertEquals("Wrong oxm-field", OxmMatchConstants.ETH_TYPE, fieldAndMask >>> 1);
\r
108 assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
\r
109 assertEquals("Wrong length", EncodeConstants.SIZE_OF_SHORT_IN_BYTES, buffer.readUnsignedByte());
\r