ed3089c0514f5e39697f0828f71cac9ed2ffe729
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / match / OxmIcmpv6CodeSerializerTest.java
1 /*\r
2  * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 \r
9 package org.opendaylight.openflowjava.protocol.impl.serialization.match;\r
10 \r
11 import static org.junit.Assert.assertEquals;\r
12 import static org.junit.Assert.assertTrue;\r
13 import io.netty.buffer.ByteBuf;\r
14 import io.netty.buffer.PooledByteBufAllocator;\r
15 \r
16 import org.junit.Test;\r
17 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;\r
18 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;\r
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv6CodeMatchEntry;\r
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Icmpv6CodeMatchEntryBuilder;\r
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.Icmpv6Code;\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OpenflowBasicClass;\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntriesBuilder;\r
24 \r
25 /**\r
26  * @author michal.polkorab\r
27  *\r
28  */\r
29 public class OxmIcmpv6CodeSerializerTest {\r
30 \r
31     OxmIcmpv6CodeSerializer serializer = new OxmIcmpv6CodeSerializer();\r
32 \r
33     /**\r
34      * Test correct serialization\r
35      */\r
36     @Test\r
37     public void testSerialize() {\r
38         MatchEntriesBuilder builder = prepareIcmpv6CodeMatchEntry((short) 101);\r
39         \r
40         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();\r
41         serializer.serialize(builder.build(), buffer);\r
42 \r
43         checkHeader(buffer, false);\r
44         assertEquals("Wrong value", 101, buffer.readUnsignedByte());\r
45         assertTrue("Unexpected data", buffer.readableBytes() == 0);\r
46     }\r
47 \r
48     /**\r
49      * Test correct header serialization\r
50      */\r
51     @Test\r
52     public void testSerializeHeader() {\r
53         MatchEntriesBuilder builder = prepareIcmpv6CodeHeader(false);\r
54         \r
55         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();\r
56         serializer.serializeHeader(builder.build(), buffer);\r
57 \r
58         checkHeader(buffer, false);\r
59         assertTrue("Unexpected data", buffer.readableBytes() == 0);\r
60     }\r
61 \r
62     /**\r
63      * Test correct oxm-class return value\r
64      */\r
65     @Test\r
66     public void testGetOxmClassCode() {\r
67         assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());\r
68     }\r
69 \r
70     /**\r
71      * Test correct oxm-field return value\r
72      */\r
73     @Test\r
74     public void getOxmFieldCode() {\r
75         assertEquals("Wrong oxm-class", OxmMatchConstants.ICMPV6_CODE, serializer.getOxmFieldCode());\r
76     }\r
77 \r
78     /**\r
79      * Test correct value length return value\r
80      */\r
81     @Test\r
82     public void testGetValueLength() {\r
83         assertEquals("Wrong value length", EncodeConstants.SIZE_OF_BYTE_IN_BYTES, serializer.getValueLength());\r
84     }\r
85 \r
86     private static MatchEntriesBuilder prepareIcmpv6CodeMatchEntry(short value) {\r
87         MatchEntriesBuilder builder = prepareIcmpv6CodeHeader(false);\r
88         Icmpv6CodeMatchEntryBuilder icmpv6Builder = new Icmpv6CodeMatchEntryBuilder();\r
89         icmpv6Builder.setIcmpv6Code(value);\r
90         builder.addAugmentation(Icmpv6CodeMatchEntry.class, icmpv6Builder.build());\r
91         return builder;\r
92     }\r
93 \r
94     private static MatchEntriesBuilder prepareIcmpv6CodeHeader(boolean hasMask) {\r
95         MatchEntriesBuilder builder = new MatchEntriesBuilder();\r
96         builder.setOxmClass(OpenflowBasicClass.class);\r
97         builder.setOxmMatchField(Icmpv6Code.class);\r
98         builder.setHasMask(hasMask);\r
99         return builder;\r
100     }\r
101 \r
102     private static void checkHeader(ByteBuf buffer, boolean hasMask) {\r
103         assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());\r
104         short fieldAndMask = buffer.readUnsignedByte();\r
105         assertEquals("Wrong oxm-field", OxmMatchConstants.ICMPV6_CODE, fieldAndMask >>> 1);\r
106         assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);\r
107         assertEquals("Wrong length", EncodeConstants.SIZE_OF_BYTE_IN_BYTES, buffer.readUnsignedByte());\r
108     }\r
109 }