Remove EncodeConstants.SIZE_OF_{BYTE,SHORT,INT,LONG}_IN_BYTES
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / match / OxmTcpDstSerializerTest.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.TcpDst;
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.TcpDstCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.tcp.dst._case.TcpDstBuilder;
23
24 /**
25  * Unit tests for OxmTcpDstSerializer.
26  *
27  * @author michal.polkorab
28  */
29 public class OxmTcpDstSerializerTest {
30
31     OxmTcpDstSerializer serializer = new OxmTcpDstSerializer();
32
33     /**
34      * Test correct serialization.
35      */
36     @Test
37     public void testSerialize() {
38         MatchEntryBuilder builder = prepareMatchEntry(4096);
39
40         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
41         serializer.serialize(builder.build(), buffer);
42
43         checkHeader(buffer, false);
44         assertEquals("Wrong value", 4096, buffer.readUnsignedShort());
45         assertTrue("Unexpected data", buffer.readableBytes() == 0);
46     }
47
48     /**
49      * Test correct header serialization.
50      */
51     @Test
52     public void testSerializeHeader() {
53         MatchEntryBuilder builder = prepareHeader(false);
54
55         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
56         serializer.serializeHeader(builder.build(), buffer);
57
58         checkHeader(buffer, false);
59         assertTrue("Unexpected data", buffer.readableBytes() == 0);
60     }
61
62     /**
63      * Test correct oxm-class return value.
64      */
65     @Test
66     public void testGetOxmClassCode() {
67         assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());
68     }
69
70     /**
71      * Test correct oxm-field return value.
72      */
73     @Test
74     public void getOxmFieldCode() {
75         assertEquals("Wrong oxm-class", OxmMatchConstants.TCP_DST, serializer.getOxmFieldCode());
76     }
77
78     /**
79      * Test correct value length return value.
80      */
81     @Test
82     public void testGetValueLength() {
83         assertEquals("Wrong value length", Short.BYTES, serializer.getValueLength());
84     }
85
86     private static MatchEntryBuilder prepareMatchEntry(int value) {
87         MatchEntryBuilder builder = prepareHeader(false);
88         TcpDstCaseBuilder casebuilder = new TcpDstCaseBuilder();
89         TcpDstBuilder valueBuilder = new TcpDstBuilder();
90         valueBuilder.setPort(new PortNumber(value));
91         casebuilder.setTcpDst(valueBuilder.build());
92         builder.setMatchEntryValue(casebuilder.build());
93         return builder;
94     }
95
96     private static MatchEntryBuilder prepareHeader(boolean hasMask) {
97         MatchEntryBuilder builder = new MatchEntryBuilder();
98         builder.setOxmClass(OpenflowBasicClass.class);
99         builder.setOxmMatchField(TcpDst.class);
100         builder.setHasMask(hasMask);
101         return builder;
102     }
103
104     private static void checkHeader(ByteBuf buffer, boolean hasMask) {
105         assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
106         short fieldAndMask = buffer.readUnsignedByte();
107         assertEquals("Wrong oxm-field", OxmMatchConstants.TCP_DST, fieldAndMask >>> 1);
108         assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
109         assertEquals("Wrong length", Short.BYTES, buffer.readUnsignedByte());
110     }
111 }