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.Metadata;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OpenflowBasicClass;
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.MetadataCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.metadata._case.MetadataBuilder;
27 * @author michal.polkorab
30 public class OxmMetadataSerializerTest {
32 OxmMetadataSerializer serializer = new OxmMetadataSerializer();
35 * Test correct serialization
38 public void testSerializeWithoutMask() {
39 MatchEntryBuilder builder = prepareMatchEntry(false, new byte[]{0, 1, 2, 3, 4, 5, 6, 7});
41 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
42 serializer.serialize(builder.build(), buffer);
44 checkHeader(buffer, false);
45 byte[] address = new byte[8];
46 buffer.readBytes(address);
47 Assert.assertArrayEquals("Wrong address", new byte[]{0, 1, 2, 3, 4, 5, 6, 7}, address);
48 assertTrue("Unexpected data", buffer.readableBytes() == 0);
52 * Test correct serialization
55 public void testSerializeWithMask() {
56 MatchEntryBuilder builder = prepareMatchEntry(true, new byte[]{8, 9, 10, 11, 12, 13, 14, 15});
58 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
59 serializer.serialize(builder.build(), buffer);
61 checkHeader(buffer, true);
63 byte[] address = new byte[8];
64 buffer.readBytes(address);
65 Assert.assertArrayEquals("Wrong address", new byte[]{8, 9, 10, 11, 12, 13, 14, 15}, address);
66 byte[] tmp = new byte[8];
67 buffer.readBytes(tmp);
68 Assert.assertArrayEquals("Wrong mask", new byte[]{30, 30, 25, 25, 15, 15, 0, 0}, tmp);
69 assertTrue("Unexpected data", buffer.readableBytes() == 0);
73 * Test correct header serialization
76 public void testSerializeHeaderWithoutMask() {
77 MatchEntryBuilder builder = prepareHeader(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 MatchEntryBuilder builder = prepareHeader(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.METADATA, serializer.getOxmFieldCode());
117 * Test correct value length return value
120 public void testGetValueLength() {
121 assertEquals("Wrong value length", EncodeConstants.SIZE_OF_LONG_IN_BYTES, serializer.getValueLength());
124 private static MatchEntryBuilder prepareMatchEntry(boolean hasMask, byte[] value) {
125 MatchEntryBuilder builder = prepareHeader(hasMask);
126 MetadataCaseBuilder casebuilder = new MetadataCaseBuilder();
127 MetadataBuilder valueBuilder = new MetadataBuilder();
129 valueBuilder.setMask(new byte[]{30, 30, 25, 25, 15, 15, 0, 0});
131 valueBuilder.setMetadata(value);
132 casebuilder.setMetadata(valueBuilder.build());
133 builder.setMatchEntryValue(casebuilder.build());
137 private static MatchEntryBuilder prepareHeader(boolean hasMask) {
138 MatchEntryBuilder builder = new MatchEntryBuilder();
139 builder.setOxmClass(OpenflowBasicClass.class);
140 builder.setOxmMatchField(Metadata.class);
141 builder.setHasMask(hasMask);
145 private static void checkHeader(ByteBuf buffer, boolean hasMask) {
146 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
147 short fieldAndMask = buffer.readUnsignedByte();
148 assertEquals("Wrong oxm-field", OxmMatchConstants.METADATA, fieldAndMask >>> 1);
149 assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
151 assertEquals("Wrong length", 2 * EncodeConstants.SIZE_OF_LONG_IN_BYTES, buffer.readUnsignedByte());
153 assertEquals("Wrong length", EncodeConstants.SIZE_OF_LONG_IN_BYTES, buffer.readUnsignedByte());