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.Test;
17 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
18 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
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.SctpSrc;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.SctpSrcCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.sctp.src._case.SctpSrcBuilder;
27 * Unit tests for OxmSctpSrcSerializer.
29 * @author michal.polkorab
31 public class OxmSctpSrcSerializerTest {
33 OxmSctpSrcSerializer serializer = new OxmSctpSrcSerializer();
36 * Test correct serialization.
39 public void testSerialize() {
40 MatchEntryBuilder builder = prepareMatchEntry(4096);
42 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
43 serializer.serialize(builder.build(), buffer);
45 checkHeader(buffer, false);
46 assertEquals("Wrong value", 4096, buffer.readUnsignedShort());
47 assertTrue("Unexpected data", buffer.readableBytes() == 0);
51 * Test correct header serialization.
54 public void testSerializeHeader() {
55 MatchEntryBuilder builder = prepareHeader(false);
57 ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
58 serializer.serializeHeader(builder.build(), buffer);
60 checkHeader(buffer, false);
61 assertTrue("Unexpected data", buffer.readableBytes() == 0);
65 * Test correct oxm-class return value.
68 public void testGetOxmClassCode() {
69 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());
73 * Test correct oxm-field return value.
76 public void getOxmFieldCode() {
77 assertEquals("Wrong oxm-class", OxmMatchConstants.SCTP_SRC, serializer.getOxmFieldCode());
81 * Test correct value length return value.
84 public void testGetValueLength() {
85 assertEquals("Wrong value length", EncodeConstants.SIZE_OF_SHORT_IN_BYTES, serializer.getValueLength());
88 private static MatchEntryBuilder prepareMatchEntry(int value) {
89 MatchEntryBuilder builder = prepareHeader(false);
90 SctpSrcCaseBuilder casebuilder = new SctpSrcCaseBuilder();
91 SctpSrcBuilder valueBuilder = new SctpSrcBuilder();
92 valueBuilder.setPort(new PortNumber(value));
93 casebuilder.setSctpSrc(valueBuilder.build());
94 builder.setMatchEntryValue(casebuilder.build());
98 private static MatchEntryBuilder prepareHeader(boolean hasMask) {
99 MatchEntryBuilder builder = new MatchEntryBuilder();
100 builder.setOxmClass(OpenflowBasicClass.class);
101 builder.setOxmMatchField(SctpSrc.class);
102 builder.setHasMask(hasMask);
106 private static void checkHeader(ByteBuf buffer, boolean hasMask) {
107 assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
108 short fieldAndMask = buffer.readUnsignedByte();
109 assertEquals("Wrong oxm-field", OxmMatchConstants.SCTP_SRC, fieldAndMask >>> 1);
110 assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
111 assertEquals("Wrong length", EncodeConstants.SIZE_OF_SHORT_IN_BYTES, buffer.readUnsignedByte());