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.augments.rev131002.MaskMatchEntry;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntryBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PseudoFieldMatchEntry;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PseudoFieldMatchEntryBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Ipv6ExthdrFlags;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Ipv6Exthdr;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OpenflowBasicClass;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntriesBuilder;
30 * @author michal.polkorab
33 public class OxmIpv6ExtHdrSerializerTest {
35 OxmIpv6ExtHdrSerializer serializer = new OxmIpv6ExtHdrSerializer();
38 * Test correct serialization
41 public void testSerializeWithMask() {
42 MatchEntriesBuilder builder = prepareIpv6ExtHdrMatchEntry(false,
43 new Ipv6ExthdrFlags(true, false, true, false, true, false, true, false, true));
45 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
46 serializer.serialize(builder.build(), buffer);
48 checkHeader(buffer, false);
49 assertEquals("Wrong value", 358, buffer.readUnsignedShort());
50 assertTrue("Unexpected data", buffer.readableBytes() == 0);
54 * Test correct serialization
57 public void testSerializeWithoutMask() {
58 MatchEntriesBuilder builder = prepareIpv6ExtHdrMatchEntry(true,
59 new Ipv6ExthdrFlags(false, true, false, true, false, true, false, true, false));
61 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
62 serializer.serialize(builder.build(), buffer);
64 checkHeader(buffer, true);
65 assertEquals("Wrong value", 153, buffer.readUnsignedShort());
66 byte[] tmp = new byte[2];
67 buffer.readBytes(tmp);
68 Assert.assertArrayEquals("Wrong mask", new byte[]{0, 15}, tmp);
69 assertTrue("Unexpected data", buffer.readableBytes() == 0);
73 * Test correct header serialization
76 public void testSerializeHeaderWithoutMask() {
77 MatchEntriesBuilder builder = prepareIpv6ExtHdrHeader(false);
79 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
80 serializer.serializeHeader(builder.build(), buffer);
82 checkHeader(buffer, false);
83 assertTrue("Unexpected data", buffer.readableBytes() == 0);
87 * Test correct header serialization
90 public void testSerializeHeaderWithMask() {
91 MatchEntriesBuilder builder = prepareIpv6ExtHdrHeader(true);
93 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
94 serializer.serializeHeader(builder.build(), buffer);
96 checkHeader(buffer, true);
97 assertTrue("Unexpected data", buffer.readableBytes() == 0);
101 * Test correct oxm-class return value
104 public void testGetOxmClassCode() {
105 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());
109 * Test correct oxm-field return value
112 public void getOxmFieldCode() {
113 assertEquals("Wrong oxm-class", OxmMatchConstants.IPV6_EXTHDR, serializer.getOxmFieldCode());
117 * Test correct value length return value
120 public void testGetValueLength() {
121 assertEquals("Wrong value length", EncodeConstants.SIZE_OF_SHORT_IN_BYTES, serializer.getValueLength());
125 private static MatchEntriesBuilder prepareIpv6ExtHdrMatchEntry(boolean hasMask, Ipv6ExthdrFlags flags) {
126 MatchEntriesBuilder builder = prepareIpv6ExtHdrHeader(hasMask);
128 MaskMatchEntryBuilder maskBuilder = new MaskMatchEntryBuilder();
129 maskBuilder.setMask(new byte[]{0, 15});
130 builder.addAugmentation(MaskMatchEntry.class, maskBuilder.build());
132 PseudoFieldMatchEntryBuilder pseudoBuilder = new PseudoFieldMatchEntryBuilder();
133 pseudoBuilder.setPseudoField(flags);
134 builder.addAugmentation(PseudoFieldMatchEntry.class, pseudoBuilder.build());
138 private static MatchEntriesBuilder prepareIpv6ExtHdrHeader(boolean hasMask) {
139 MatchEntriesBuilder builder = new MatchEntriesBuilder();
140 builder.setOxmClass(OpenflowBasicClass.class);
141 builder.setOxmMatchField(Ipv6Exthdr.class);
142 builder.setHasMask(hasMask);
146 private static void checkHeader(ByteBuf buffer, boolean hasMask) {
147 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
148 short fieldAndMask = buffer.readUnsignedByte();
149 assertEquals("Wrong oxm-field", OxmMatchConstants.IPV6_EXTHDR, fieldAndMask >>> 1);
150 assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
152 assertEquals("Wrong length", EncodeConstants.SIZE_OF_INT_IN_BYTES, buffer.readUnsignedByte());
154 assertEquals("Wrong length", EncodeConstants.SIZE_OF_SHORT_IN_BYTES, buffer.readUnsignedByte());