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
8 package org.opendaylight.openflowjava.protocol.impl.serialization.match;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.PooledByteBufAllocator;
15 import org.junit.Assert;
16 import org.junit.Test;
17 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.ArpTpa;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OpenflowBasicClass;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ArpTpaCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.arp.tpa._case.ArpTpaBuilder;
26 * Unit tests for OxmArpTpaSerializer.
28 * @author michal.polkorab
30 public class OxmArpTpaSerializerTest {
32 OxmArpTpaSerializer serializer = new OxmArpTpaSerializer();
35 * Test correct serialization.
38 public void testSerializeWithoutMask() {
39 MatchEntryBuilder builder = prepareMatchEntry(false, "10.0.0.1");
41 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
42 serializer.serialize(builder.build(), buffer);
44 checkHeader(buffer, false);
45 byte[] address = new byte[4];
46 buffer.readBytes(address);
47 Assert.assertArrayEquals("Wrong address", new byte[]{10, 0, 0, 1}, address);
48 assertTrue("Unexpected data", buffer.readableBytes() == 0);
52 * Test correct serialization.
55 public void testSerializeWithMask() {
56 MatchEntryBuilder builder = prepareMatchEntry(true, "120.121.122.0");
58 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
59 serializer.serialize(builder.build(), buffer);
61 checkHeader(buffer, true);
63 byte[] address = new byte[4];
64 buffer.readBytes(address);
65 Assert.assertArrayEquals("Wrong address", new byte[]{120, 121, 122, 0}, address);
66 byte[] tmp = new byte[4];
67 buffer.readBytes(tmp);
68 Assert.assertArrayEquals("Wrong mask", new byte[]{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.ARP_TPA, serializer.getOxmFieldCode());
117 * Test correct value length return value.
120 public void testGetValueLength() {
121 assertEquals("Wrong value length", Integer.BYTES, serializer.getValueLength());
124 private static MatchEntryBuilder prepareMatchEntry(final boolean hasMask, final String value) {
125 final MatchEntryBuilder builder = prepareHeader(hasMask);
126 ArpTpaCaseBuilder casebuilder = new ArpTpaCaseBuilder();
127 ArpTpaBuilder valueBuilder = new ArpTpaBuilder();
129 valueBuilder.setMask(new byte[]{15, 15, 0, 0});
131 valueBuilder.setIpv4Address(new Ipv4Address(value));
132 casebuilder.setArpTpa(valueBuilder.build());
133 builder.setMatchEntryValue(casebuilder.build());
137 private static MatchEntryBuilder prepareHeader(final boolean hasMask) {
138 MatchEntryBuilder builder = new MatchEntryBuilder();
139 builder.setOxmClass(OpenflowBasicClass.VALUE);
140 builder.setOxmMatchField(ArpTpa.VALUE);
141 builder.setHasMask(hasMask);
145 private static void checkHeader(final ByteBuf buffer, final boolean hasMask) {
146 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
147 short fieldAndMask = buffer.readUnsignedByte();
148 assertEquals("Wrong oxm-field", OxmMatchConstants.ARP_TPA, fieldAndMask >>> 1);
149 assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
151 assertEquals("Wrong length", Long.BYTES, buffer.readUnsignedByte());
153 assertEquals("Wrong length", Integer.BYTES, buffer.readUnsignedByte());