9d0d96e7acdb8038ebcc29efb7b576c6b79d782a
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / match / OxmIpDscpSerializerTest.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.Dscp;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.IpDscp;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OpenflowBasicClass;
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.IpDscpCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ip.dscp._case.IpDscpBuilder;
25
26 /**
27  * Unit tests for OxmIpDscpSerializer.
28  *
29  * @author michal.polkorab
30  */
31 public class OxmIpDscpSerializerTest {
32
33     OxmIpDscpSerializer serializer = new OxmIpDscpSerializer();
34
35     /**
36      * Test correct serialization.
37      */
38     @Test
39     public void testSerialize() {
40         MatchEntryBuilder builder = prepareIpDscpMatchEntry((short) 58);
41
42         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
43         serializer.serialize(builder.build(), buffer);
44
45         checkHeader(buffer, false);
46         assertEquals("Wrong value", 58, buffer.readUnsignedByte());
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 = prepareIpDscpHeader(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.IP_DSCP, 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_BYTE_IN_BYTES, serializer.getValueLength());
86     }
87
88     private static MatchEntryBuilder prepareIpDscpMatchEntry(short value) {
89         MatchEntryBuilder builder = prepareIpDscpHeader(false);
90         IpDscpCaseBuilder casebuilder = new IpDscpCaseBuilder();
91         IpDscpBuilder dscpBuilder = new IpDscpBuilder();
92         dscpBuilder.setDscp(new Dscp(value));
93         casebuilder.setIpDscp(dscpBuilder.build());
94         builder.setMatchEntryValue(casebuilder.build());
95         return builder;
96     }
97
98     private static MatchEntryBuilder prepareIpDscpHeader(boolean hasMask) {
99         MatchEntryBuilder builder = new MatchEntryBuilder();
100         builder.setOxmClass(OpenflowBasicClass.class);
101         builder.setOxmMatchField(IpDscp.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.IP_DSCP, fieldAndMask >>> 1);
110         assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
111         assertEquals("Wrong length", EncodeConstants.SIZE_OF_BYTE_IN_BYTES, buffer.readUnsignedByte());
112     }
113 }