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 / OxmMplsTcSerializerTest.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.opendaylight.openflow.oxm.rev150225.MplsTc;
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.match.entries.grouping.MatchEntryBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.MplsTcCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.mpls.tc._case.MplsTcBuilder;
22
23 /**
24  * Unit tests for OxmMplsTcSerializer.
25  *
26  * @author michal.polkorab
27  */
28 public class OxmMplsTcSerializerTest {
29
30     OxmMplsTcSerializer serializer = new OxmMplsTcSerializer();
31
32     /**
33      * Test correct serialization.
34      */
35     @Test
36     public void testSerialize() {
37         MatchEntryBuilder builder = prepareMplsTcMatchEntry((short) 16);
38
39         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
40         serializer.serialize(builder.build(), buffer);
41
42         checkHeader(buffer, false);
43         assertEquals("Wrong value", 16, buffer.readUnsignedByte());
44         assertTrue("Unexpected data", buffer.readableBytes() == 0);
45     }
46
47     /**
48      * Test correct header serialization.
49      */
50     @Test
51     public void testSerializeHeader() {
52         MatchEntryBuilder builder = prepareMplsTcHeader(false);
53
54         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
55         serializer.serializeHeader(builder.build(), buffer);
56
57         checkHeader(buffer, false);
58         assertTrue("Unexpected data", buffer.readableBytes() == 0);
59     }
60
61     /**
62      * Test correct oxm-class return value.
63      */
64     @Test
65     public void testGetOxmClassCode() {
66         assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());
67     }
68
69     /**
70      * Test correct oxm-field return value.
71      */
72     @Test
73     public void getOxmFieldCode() {
74         assertEquals("Wrong oxm-class", OxmMatchConstants.MPLS_TC, serializer.getOxmFieldCode());
75     }
76
77     /**
78      * Test correct value length return value.
79      */
80     @Test
81     public void testGetValueLength() {
82         assertEquals("Wrong value length", Byte.BYTES, serializer.getValueLength());
83     }
84
85     private static MatchEntryBuilder prepareMplsTcMatchEntry(short value) {
86         MatchEntryBuilder builder = prepareMplsTcHeader(false);
87         MplsTcCaseBuilder casebuilder = new MplsTcCaseBuilder();
88         MplsTcBuilder valueBuilder = new MplsTcBuilder();
89         valueBuilder.setTc(value);
90         casebuilder.setMplsTc(valueBuilder.build());
91         builder.setMatchEntryValue(casebuilder.build());
92         return builder;
93     }
94
95     private static MatchEntryBuilder prepareMplsTcHeader(boolean hasMask) {
96         MatchEntryBuilder builder = new MatchEntryBuilder();
97         builder.setOxmClass(OpenflowBasicClass.class);
98         builder.setOxmMatchField(MplsTc.class);
99         builder.setHasMask(hasMask);
100         return builder;
101     }
102
103     private static void checkHeader(ByteBuf buffer, boolean hasMask) {
104         assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
105         short fieldAndMask = buffer.readUnsignedByte();
106         assertEquals("Wrong oxm-field", OxmMatchConstants.MPLS_TC, fieldAndMask >>> 1);
107         assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
108         assertEquals("Wrong length", Byte.BYTES, buffer.readUnsignedByte());
109     }
110 }