Bump upstreams for 2022.09 Chlorine
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / match / OxmSctpSrcSerializerTest.java
1 /*
2  * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowjava.protocol.impl.serialization.match;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.PooledByteBufAllocator;
15 import org.junit.Test;
16 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OpenflowBasicClass;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.SctpSrc;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.SctpSrcCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.sctp.src._case.SctpSrcBuilder;
23 import org.opendaylight.yangtools.yang.common.Uint16;
24
25 /**
26  * Unit tests for OxmSctpSrcSerializer.
27  *
28  * @author michal.polkorab
29  */
30 public class OxmSctpSrcSerializerTest {
31
32     OxmSctpSrcSerializer serializer = new OxmSctpSrcSerializer();
33
34     /**
35      * Test correct serialization.
36      */
37     @Test
38     public void testSerialize() {
39         MatchEntryBuilder builder = prepareMatchEntry(4096);
40
41         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
42         serializer.serialize(builder.build(), buffer);
43
44         checkHeader(buffer, false);
45         assertEquals("Wrong value", 4096, buffer.readUnsignedShort());
46         assertTrue("Unexpected data", buffer.readableBytes() == 0);
47     }
48
49     /**
50      * Test correct header serialization.
51      */
52     @Test
53     public void testSerializeHeader() {
54         MatchEntryBuilder builder = prepareHeader(false);
55
56         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
57         serializer.serializeHeader(builder.build(), buffer);
58
59         checkHeader(buffer, false);
60         assertTrue("Unexpected data", buffer.readableBytes() == 0);
61     }
62
63     /**
64      * Test correct oxm-class return value.
65      */
66     @Test
67     public void testGetOxmClassCode() {
68         assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());
69     }
70
71     /**
72      * Test correct oxm-field return value.
73      */
74     @Test
75     public void getOxmFieldCode() {
76         assertEquals("Wrong oxm-class", OxmMatchConstants.SCTP_SRC, serializer.getOxmFieldCode());
77     }
78
79     /**
80      * Test correct value length return value.
81      */
82     @Test
83     public void testGetValueLength() {
84         assertEquals("Wrong value length", Short.BYTES, serializer.getValueLength());
85     }
86
87     private static MatchEntryBuilder prepareMatchEntry(final int value) {
88         MatchEntryBuilder builder = prepareHeader(false);
89         SctpSrcCaseBuilder casebuilder = new SctpSrcCaseBuilder();
90         SctpSrcBuilder valueBuilder = new SctpSrcBuilder();
91         valueBuilder.setPort(new PortNumber(Uint16.valueOf(value)));
92         casebuilder.setSctpSrc(valueBuilder.build());
93         builder.setMatchEntryValue(casebuilder.build());
94         return builder;
95     }
96
97     private static MatchEntryBuilder prepareHeader(final boolean hasMask) {
98         MatchEntryBuilder builder = new MatchEntryBuilder();
99         builder.setOxmClass(OpenflowBasicClass.VALUE);
100         builder.setOxmMatchField(SctpSrc.VALUE);
101         builder.setHasMask(hasMask);
102         return builder;
103     }
104
105     private static void checkHeader(final ByteBuf buffer, final boolean hasMask) {
106         assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
107         short fieldAndMask = buffer.readUnsignedByte();
108         assertEquals("Wrong oxm-field", OxmMatchConstants.SCTP_SRC, fieldAndMask >>> 1);
109         assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
110         assertEquals("Wrong length", Short.BYTES, buffer.readUnsignedByte());
111     }
112 }