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.rev100924.MacAddress;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MacAddressMatchEntry;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MacAddressMatchEntryBuilder;
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.Ipv6NdSll;
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 OxmIpv6NdSllSerializerTest {
35 OxmIpv6NdSllSerializer serializer = new OxmIpv6NdSllSerializer();
38 * Test correct serialization
41 public void testSerialize() {
42 MatchEntriesBuilder builder = prepareMatchEntry(false, "00:01:02:03:04:05");
44 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
45 serializer.serialize(builder.build(), buffer);
47 checkHeader(buffer, false);
48 byte[] address = new byte[6];
49 buffer.readBytes(address);
50 Assert.assertArrayEquals("Wrong address", new byte[]{0, 1, 2, 3, 4, 5}, address);
51 assertTrue("Unexpected data", buffer.readableBytes() == 0);
55 * Test correct header serialization
58 public void testSerializeHeader() {
59 MatchEntriesBuilder builder = prepareHeader(false);
61 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
62 serializer.serializeHeader(builder.build(), buffer);
64 checkHeader(buffer, false);
65 assertTrue("Unexpected data", buffer.readableBytes() == 0);
69 * Test correct oxm-class return value
72 public void testGetOxmClassCode() {
73 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());
77 * Test correct oxm-field return value
80 public void getOxmFieldCode() {
81 assertEquals("Wrong oxm-class", OxmMatchConstants.IPV6_ND_SLL, serializer.getOxmFieldCode());
85 * Test correct value length return value
88 public void testGetValueLength() {
89 assertEquals("Wrong value length", EncodeConstants.MAC_ADDRESS_LENGTH, serializer.getValueLength());
92 private static MatchEntriesBuilder prepareMatchEntry(boolean hasMask, String value) {
93 MatchEntriesBuilder builder = prepareHeader(hasMask);
95 MaskMatchEntryBuilder maskBuilder = new MaskMatchEntryBuilder();
96 maskBuilder.setMask(new byte[]{15, 15, 0, 0, 10, 10});
97 builder.addAugmentation(MaskMatchEntry.class, maskBuilder.build());
99 MacAddressMatchEntryBuilder macBuilder = new MacAddressMatchEntryBuilder();
100 macBuilder.setMacAddress(new MacAddress(value));
101 builder.addAugmentation(MacAddressMatchEntry.class, macBuilder.build());
105 private static MatchEntriesBuilder prepareHeader(boolean hasMask) {
106 MatchEntriesBuilder builder = new MatchEntriesBuilder();
107 builder.setOxmClass(OpenflowBasicClass.class);
108 builder.setOxmMatchField(Ipv6NdSll.class);
109 builder.setHasMask(hasMask);
113 private static void checkHeader(ByteBuf buffer, boolean hasMask) {
114 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
115 short fieldAndMask = buffer.readUnsignedByte();
116 assertEquals("Wrong oxm-field", OxmMatchConstants.IPV6_ND_SLL, fieldAndMask >>> 1);
117 assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
118 assertEquals("Wrong length", EncodeConstants.MAC_ADDRESS_LENGTH, buffer.readUnsignedByte());