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.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 * @author michal.polkorab
31 public class OxmEthDstSerializerTest {
33 OxmEthDstSerializer serializer = new OxmEthDstSerializer();
36 * Test correct serialization
39 public void testSerializeWithoutMask() {
40 MatchEntryBuilder builder = prepareMatchEntry(false, "00:01:02:03:04:05");
42 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
43 serializer.serialize(builder.build(), buffer);
45 checkHeader(buffer, false);
46 byte[] address = new byte[6];
47 buffer.readBytes(address);
48 Assert.assertArrayEquals("Wrong address", new byte[]{0, 1, 2, 3, 4, 5}, address);
49 assertTrue("Unexpected data", buffer.readableBytes() == 0);
53 * Test correct serialization
56 public void testSerializeWithMask() {
57 MatchEntryBuilder builder = prepareMatchEntry(true, "00:01:02:03:04:0A");
59 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
60 serializer.serialize(builder.build(), buffer);
62 checkHeader(buffer, true);
64 byte[] address = new byte[6];
65 buffer.readBytes(address);
66 Assert.assertArrayEquals("Wrong address", new byte[]{0, 1, 2, 3, 4, 10}, address);
67 byte[] tmp = new byte[6];
68 buffer.readBytes(tmp);
69 Assert.assertArrayEquals("Wrong mask", new byte[]{15, 15, 0, 0, 10, 10}, tmp);
70 assertTrue("Unexpected data", buffer.readableBytes() == 0);
74 * Test correct header serialization
77 public void testSerializeHeaderWithoutMask() {
78 MatchEntryBuilder builder = prepareHeader(false);
80 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
81 serializer.serializeHeader(builder.build(), buffer);
83 checkHeader(buffer, false);
84 assertTrue("Unexpected data", buffer.readableBytes() == 0);
88 * Test correct header serialization
91 public void testSerializeHeaderWithMask() {
92 MatchEntryBuilder builder = prepareHeader(true);
94 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
95 serializer.serializeHeader(builder.build(), buffer);
97 checkHeader(buffer, true);
98 assertTrue("Unexpected data", buffer.readableBytes() == 0);
102 * Test correct oxm-class return value
105 public void testGetOxmClassCode() {
106 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());
110 * Test correct oxm-field return value
113 public void getOxmFieldCode() {
114 assertEquals("Wrong oxm-class", OxmMatchConstants.ETH_DST, serializer.getOxmFieldCode());
118 * Test correct value length return value
121 public void testGetValueLength() {
122 assertEquals("Wrong value length", EncodeConstants.MAC_ADDRESS_LENGTH, serializer.getValueLength());
125 private static MatchEntryBuilder prepareMatchEntry(boolean hasMask, String value) {
126 MatchEntryBuilder builder = prepareHeader(hasMask);
127 EthDstCaseBuilder casebuilder = new EthDstCaseBuilder();
128 EthDstBuilder valueBuilder = new EthDstBuilder();
130 valueBuilder.setMask(new byte[]{15, 15, 0, 0, 10, 10});
132 valueBuilder.setMacAddress(new MacAddress(value));
133 casebuilder.setEthDst(valueBuilder.build());
134 builder.setMatchEntryValue(casebuilder.build());
138 private static MatchEntryBuilder prepareHeader(boolean hasMask) {
139 MatchEntryBuilder builder = new MatchEntryBuilder();
140 builder.setOxmClass(OpenflowBasicClass.class);
141 builder.setOxmMatchField(EthDst.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.ETH_DST, fieldAndMask >>> 1);
150 assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
152 assertEquals("Wrong length", 2 * EncodeConstants.MAC_ADDRESS_LENGTH, buffer.readUnsignedByte());
154 assertEquals("Wrong length", EncodeConstants.MAC_ADDRESS_LENGTH, buffer.readUnsignedByte());