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;
14 import io.netty.buffer.ByteBuf;
15 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.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.EthDst;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OpenflowBasicClass;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.EthDstCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.eth.dst._case.EthDstBuilder;
28 * Unit tests for OxmEthDstSerializer.
30 * @author michal.polkorab
32 public class OxmEthDstSerializerTest {
34 OxmEthDstSerializer serializer = new OxmEthDstSerializer();
37 * Test correct serialization.
40 public void testSerializeWithoutMask() {
41 MatchEntryBuilder builder = prepareMatchEntry(false, "00:01:02:03:04:05");
43 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
44 serializer.serialize(builder.build(), buffer);
46 checkHeader(buffer, false);
47 byte[] address = new byte[6];
48 buffer.readBytes(address);
49 Assert.assertArrayEquals("Wrong address", new byte[]{0, 1, 2, 3, 4, 5}, address);
50 assertTrue("Unexpected data", buffer.readableBytes() == 0);
54 * Test correct serialization.
57 public void testSerializeWithMask() {
58 MatchEntryBuilder builder = prepareMatchEntry(true, "00:01:02:03:04:0A");
60 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
61 serializer.serialize(builder.build(), buffer);
63 checkHeader(buffer, true);
65 byte[] address = new byte[6];
66 buffer.readBytes(address);
67 Assert.assertArrayEquals("Wrong address", new byte[]{0, 1, 2, 3, 4, 10}, address);
68 byte[] tmp = new byte[6];
69 buffer.readBytes(tmp);
70 Assert.assertArrayEquals("Wrong mask", new byte[]{15, 15, 0, 0, 10, 10}, tmp);
71 assertTrue("Unexpected data", buffer.readableBytes() == 0);
75 * Test correct header serialization.
78 public void testSerializeHeaderWithoutMask() {
79 MatchEntryBuilder builder = prepareHeader(false);
81 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
82 serializer.serializeHeader(builder.build(), buffer);
84 checkHeader(buffer, false);
85 assertTrue("Unexpected data", buffer.readableBytes() == 0);
89 * Test correct header serialization.
92 public void testSerializeHeaderWithMask() {
93 MatchEntryBuilder builder = prepareHeader(true);
95 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
96 serializer.serializeHeader(builder.build(), buffer);
98 checkHeader(buffer, true);
99 assertTrue("Unexpected data", buffer.readableBytes() == 0);
103 * Test correct oxm-class return value.
106 public void testGetOxmClassCode() {
107 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());
111 * Test correct oxm-field return value.
114 public void getOxmFieldCode() {
115 assertEquals("Wrong oxm-class", OxmMatchConstants.ETH_DST, serializer.getOxmFieldCode());
119 * Test correct value length return value.
122 public void testGetValueLength() {
123 assertEquals("Wrong value length", EncodeConstants.MAC_ADDRESS_LENGTH, serializer.getValueLength());
126 private static MatchEntryBuilder prepareMatchEntry(final boolean hasMask, final String value) {
127 final MatchEntryBuilder builder = prepareHeader(hasMask);
128 EthDstCaseBuilder casebuilder = new EthDstCaseBuilder();
129 EthDstBuilder valueBuilder = new EthDstBuilder();
131 valueBuilder.setMask(new byte[]{15, 15, 0, 0, 10, 10});
133 valueBuilder.setMacAddress(new MacAddress(value));
134 casebuilder.setEthDst(valueBuilder.build());
135 builder.setMatchEntryValue(casebuilder.build());
139 private static MatchEntryBuilder prepareHeader(final boolean hasMask) {
140 MatchEntryBuilder builder = new MatchEntryBuilder();
141 builder.setOxmClass(OpenflowBasicClass.VALUE);
142 builder.setOxmMatchField(EthDst.VALUE);
143 builder.setHasMask(hasMask);
147 private static void checkHeader(final ByteBuf buffer, final boolean hasMask) {
148 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
149 short fieldAndMask = buffer.readUnsignedByte();
150 assertEquals("Wrong oxm-field", OxmMatchConstants.ETH_DST, fieldAndMask >>> 1);
151 assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
153 assertEquals("Wrong length", 2 * EncodeConstants.MAC_ADDRESS_LENGTH, buffer.readUnsignedByte());
155 assertEquals("Wrong length", EncodeConstants.MAC_ADDRESS_LENGTH, buffer.readUnsignedByte());