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