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.inet.types.rev100924.Ipv4Address;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv4AddressMatchEntry;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv4AddressMatchEntryBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntry;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntryBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ArpSpa;
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 OxmArpSpaSerializerTest {
35 OxmArpSpaSerializer serializer = new OxmArpSpaSerializer();
38 * Test correct serialization
41 public void testSerializeWithoutMask() {
42 MatchEntriesBuilder builder = prepareMatchEntry(false, "10.0.0.1");
44 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
45 serializer.serialize(builder.build(), buffer);
47 checkHeader(buffer, false);
48 byte[] address = new byte[4];
49 buffer.readBytes(address);
50 Assert.assertArrayEquals("Wrong address", new byte[]{10, 0, 0, 1}, address);
51 assertTrue("Unexpected data", buffer.readableBytes() == 0);
55 * Test correct serialization
58 public void testSerializeWithMask() {
59 MatchEntriesBuilder builder = prepareMatchEntry(true, "120.121.122.0");
61 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
62 serializer.serialize(builder.build(), buffer);
64 checkHeader(buffer, true);
66 byte[] address = new byte[4];
67 buffer.readBytes(address);
68 Assert.assertArrayEquals("Wrong address", new byte[]{120, 121, 122, 0}, address);
69 byte[] tmp = new byte[4];
70 buffer.readBytes(tmp);
71 Assert.assertArrayEquals("Wrong mask", new byte[]{15, 15, 0, 0}, tmp);
72 assertTrue("Unexpected data", buffer.readableBytes() == 0);
76 * Test correct header serialization
79 public void testSerializeHeaderWithoutMask() {
80 MatchEntriesBuilder builder = prepareHeader(false);
82 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
83 serializer.serializeHeader(builder.build(), buffer);
85 checkHeader(buffer, false);
86 assertTrue("Unexpected data", buffer.readableBytes() == 0);
90 * Test correct header serialization
93 public void testSerializeHeaderWithMask() {
94 MatchEntriesBuilder builder = prepareHeader(true);
96 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
97 serializer.serializeHeader(builder.build(), buffer);
99 checkHeader(buffer, true);
100 assertTrue("Unexpected data", buffer.readableBytes() == 0);
104 * Test correct oxm-class return value
107 public void testGetOxmClassCode() {
108 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());
112 * Test correct oxm-field return value
115 public void getOxmFieldCode() {
116 assertEquals("Wrong oxm-class", OxmMatchConstants.ARP_SPA, serializer.getOxmFieldCode());
120 * Test correct value length return value
123 public void testGetValueLength() {
124 assertEquals("Wrong value length", EncodeConstants.SIZE_OF_INT_IN_BYTES, serializer.getValueLength());
127 private static MatchEntriesBuilder prepareMatchEntry(boolean hasMask, String value) {
128 MatchEntriesBuilder builder = prepareHeader(hasMask);
130 MaskMatchEntryBuilder maskBuilder = new MaskMatchEntryBuilder();
131 maskBuilder.setMask(new byte[]{15, 15, 0, 0});
132 builder.addAugmentation(MaskMatchEntry.class, maskBuilder.build());
134 Ipv4AddressMatchEntryBuilder addressBuilder = new Ipv4AddressMatchEntryBuilder();
135 addressBuilder.setIpv4Address(new Ipv4Address(value));
136 builder.addAugmentation(Ipv4AddressMatchEntry.class, addressBuilder.build());
140 private static MatchEntriesBuilder prepareHeader(boolean hasMask) {
141 MatchEntriesBuilder builder = new MatchEntriesBuilder();
142 builder.setOxmClass(OpenflowBasicClass.class);
143 builder.setOxmMatchField(ArpSpa.class);
144 builder.setHasMask(hasMask);
148 private static void checkHeader(ByteBuf buffer, boolean hasMask) {
149 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
150 short fieldAndMask = buffer.readUnsignedByte();
151 assertEquals("Wrong oxm-field", OxmMatchConstants.ARP_SPA, fieldAndMask >>> 1);
152 assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
154 assertEquals("Wrong length", EncodeConstants.SIZE_OF_LONG_IN_BYTES, buffer.readUnsignedByte());
156 assertEquals("Wrong length", EncodeConstants.SIZE_OF_INT_IN_BYTES, buffer.readUnsignedByte());