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