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.Icmpv6CodeMatchEntry;
\r
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv6CodeMatchEntryBuilder;
\r
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Icmpv6Code;
\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 OxmIcmpv6CodeSerializerTest {
\r
31 OxmIcmpv6CodeSerializer serializer = new OxmIcmpv6CodeSerializer();
\r
34 * Test correct serialization
\r
37 public void testSerialize() {
\r
38 MatchEntriesBuilder builder = prepareIcmpv6CodeMatchEntry((short) 101);
\r
40 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
\r
41 serializer.serialize(builder.build(), buffer);
\r
43 checkHeader(buffer, false);
\r
44 assertEquals("Wrong value", 101, 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 = prepareIcmpv6CodeHeader(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.ICMPV6_CODE, 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 prepareIcmpv6CodeMatchEntry(short value) {
\r
87 MatchEntriesBuilder builder = prepareIcmpv6CodeHeader(false);
\r
88 Icmpv6CodeMatchEntryBuilder icmpv6Builder = new Icmpv6CodeMatchEntryBuilder();
\r
89 icmpv6Builder.setIcmpv6Code(value);
\r
90 builder.addAugmentation(Icmpv6CodeMatchEntry.class, icmpv6Builder.build());
\r
94 private static MatchEntriesBuilder prepareIcmpv6CodeHeader(boolean hasMask) {
\r
95 MatchEntriesBuilder builder = new MatchEntriesBuilder();
\r
96 builder.setOxmClass(OpenflowBasicClass.class);
\r
97 builder.setOxmMatchField(Icmpv6Code.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.ICMPV6_CODE, 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