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