974ecb76c457b13fe6540ad05bef144cf73a0608
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / match / OxmPbbIsidSerializerTest.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.Assert;\r
17 import org.junit.Test;\r
18 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;\r
19 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;\r
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.IsidMatchEntry;\r
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.IsidMatchEntryBuilder;\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntry;\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntryBuilder;\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OpenflowBasicClass;\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.PbbIsid;\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntriesBuilder;\r
27 \r
28 /**\r
29  * @author michal.polkorab\r
30  *\r
31  */\r
32 public class OxmPbbIsidSerializerTest {\r
33 \r
34     OxmPbbIsidSerializer serializer = new OxmPbbIsidSerializer();\r
35 \r
36     /**\r
37      * Test correct serialization\r
38      */\r
39     @Test\r
40     public void testSerializeWithMask() {\r
41         MatchEntriesBuilder builder = preparePbbIsidMatchEntry(false, 12345);\r
42         \r
43         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();\r
44         serializer.serialize(builder.build(), buffer);\r
45 \r
46         checkHeader(buffer, false);\r
47         assertEquals("Wrong value", 12345, buffer.readUnsignedMedium());\r
48         assertTrue("Unexpected data", buffer.readableBytes() == 0);\r
49     }\r
50 \r
51     /**\r
52      * Test correct serialization\r
53      */\r
54     @Test\r
55     public void testSerializeWithoutMask() {\r
56         MatchEntriesBuilder builder = preparePbbIsidMatchEntry(true, 6789);\r
57         \r
58         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();\r
59         serializer.serialize(builder.build(), buffer);\r
60 \r
61         checkHeader(buffer, true);\r
62         assertEquals("Wrong value", 6789, buffer.readUnsignedMedium());\r
63         byte[] tmp = new byte[3];\r
64         buffer.readBytes(tmp);\r
65         Assert.assertArrayEquals("Wrong mask", new byte[]{0, 15, 10}, tmp);\r
66         assertTrue("Unexpected data", buffer.readableBytes() == 0);\r
67     }\r
68 \r
69     /**\r
70      * Test correct header serialization\r
71      */\r
72     @Test\r
73     public void testSerializeHeaderWithoutMask() {\r
74         MatchEntriesBuilder builder = preparePbbIsidHeader(false);\r
75         \r
76         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();\r
77         serializer.serializeHeader(builder.build(), buffer);\r
78 \r
79         checkHeader(buffer, false);\r
80         assertTrue("Unexpected data", buffer.readableBytes() == 0);\r
81     }\r
82 \r
83     /**\r
84      * Test correct header serialization\r
85      */\r
86     @Test\r
87     public void testSerializeHeaderWithMask() {\r
88         MatchEntriesBuilder builder = preparePbbIsidHeader(true);\r
89         \r
90         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();\r
91         serializer.serializeHeader(builder.build(), buffer);\r
92 \r
93         checkHeader(buffer, true);\r
94         assertTrue("Unexpected data", buffer.readableBytes() == 0);\r
95     }\r
96 \r
97     /**\r
98      * Test correct oxm-class return value\r
99      */\r
100     @Test\r
101     public void testGetOxmClassCode() {\r
102         assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());\r
103     }\r
104 \r
105     /**\r
106      * Test correct oxm-field return value\r
107      */\r
108     @Test\r
109     public void getOxmFieldCode() {\r
110         assertEquals("Wrong oxm-class", OxmMatchConstants.PBB_ISID, serializer.getOxmFieldCode());\r
111     }\r
112 \r
113     /**\r
114      * Test correct value length return value\r
115      */\r
116     @Test\r
117     public void testGetValueLength() {\r
118         assertEquals("Wrong value length", EncodeConstants.SIZE_OF_3_BYTES, serializer.getValueLength());\r
119     }\r
120 \r
121     \r
122     private static MatchEntriesBuilder preparePbbIsidMatchEntry(boolean hasMask, long value) {\r
123         MatchEntriesBuilder builder = preparePbbIsidHeader(hasMask);\r
124         if (hasMask) {\r
125             MaskMatchEntryBuilder maskBuilder = new MaskMatchEntryBuilder();\r
126             maskBuilder.setMask(new byte[]{0, 15, 10});\r
127             builder.addAugmentation(MaskMatchEntry.class, maskBuilder.build());\r
128         }\r
129         IsidMatchEntryBuilder isidBuilder = new IsidMatchEntryBuilder();\r
130         isidBuilder.setIsid(value);\r
131         builder.addAugmentation(IsidMatchEntry.class, isidBuilder.build());\r
132         return builder;\r
133     }\r
134 \r
135     private static MatchEntriesBuilder preparePbbIsidHeader(boolean hasMask) {\r
136         MatchEntriesBuilder builder = new MatchEntriesBuilder();\r
137         builder.setOxmClass(OpenflowBasicClass.class);\r
138         builder.setOxmMatchField(PbbIsid.class);\r
139         builder.setHasMask(hasMask);\r
140         return builder;\r
141     }\r
142 \r
143     private static void checkHeader(ByteBuf buffer, boolean hasMask) {\r
144         assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());\r
145         short fieldAndMask = buffer.readUnsignedByte();\r
146         assertEquals("Wrong oxm-field", OxmMatchConstants.PBB_ISID, fieldAndMask >>> 1);\r
147         assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);\r
148         if (hasMask) {\r
149             assertEquals("Wrong length", 2 * EncodeConstants.SIZE_OF_3_BYTES, buffer.readUnsignedByte());\r
150         } else {\r
151             assertEquals("Wrong length", EncodeConstants.SIZE_OF_3_BYTES, buffer.readUnsignedByte());\r
152         }\r
153     }\r
154 }