Mass replace CRLF->LF
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / match / OxmVlanVidSerializerTest.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.MaskMatchEntry;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntryBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanVidMatchEntry;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanVidMatchEntryBuilder;
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.VlanVid;
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 OxmVlanVidSerializerTest {
33
34     OxmVlanVidSerializer serializer = new OxmVlanVidSerializer();
35
36     /**
37      * Test correct serialization
38      */
39     @Test
40     public void testSerializeWithCfiBitSet() {
41         MatchEntriesBuilder builder = prepareVlanVidMatchEntry(false, true);
42         
43         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
44         serializer.serialize(builder.build(), buffer);
45
46         checkHeader(buffer, false);
47         assertEquals("Wrong value", 4596, buffer.readUnsignedShort());
48         assertTrue("Unexpected data", buffer.readableBytes() == 0);
49     }
50
51     /**
52      * Test correct serialization
53      */
54     @Test
55     public void testSerializeWithoutCfiBitSet() {
56         MatchEntriesBuilder builder = prepareVlanVidMatchEntry(true, false);
57         
58         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
59         serializer.serialize(builder.build(), buffer);
60
61         checkHeader(buffer, true);
62         assertEquals("Wrong value", 500, buffer.readUnsignedShort());
63         byte[] tmp = new byte[2];
64         buffer.readBytes(tmp);
65         Assert.assertArrayEquals("Wrong mask", new byte[]{15, 15}, 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 = prepareVlanVidHeader(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 = prepareVlanVidHeader(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.VLAN_VID, 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_SHORT_IN_BYTES, serializer.getValueLength());
119     }
120
121     private static MatchEntriesBuilder prepareVlanVidMatchEntry(boolean hasMask, boolean cfiBit) {
122         MatchEntriesBuilder builder = prepareVlanVidHeader(hasMask);
123         if (hasMask) {
124             MaskMatchEntryBuilder maskBuilder = new MaskMatchEntryBuilder();
125             maskBuilder.setMask(new byte[]{15, 15});
126             builder.addAugmentation(MaskMatchEntry.class, maskBuilder.build());
127         }
128         VlanVidMatchEntryBuilder vlanBuilder = new VlanVidMatchEntryBuilder();
129         vlanBuilder.setVlanVid(500);
130         vlanBuilder.setCfiBit(cfiBit);
131         builder.addAugmentation(VlanVidMatchEntry.class, vlanBuilder.build());
132         return builder;
133     }
134
135     private static MatchEntriesBuilder prepareVlanVidHeader(boolean hasMask) {
136         MatchEntriesBuilder builder = new MatchEntriesBuilder();
137         builder.setOxmClass(OpenflowBasicClass.class);
138         builder.setOxmMatchField(VlanVid.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.VLAN_VID, fieldAndMask >>> 1);
147         assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
148         if (hasMask) {
149             assertEquals("Wrong length", EncodeConstants.SIZE_OF_INT_IN_BYTES, buffer.readUnsignedByte());
150         } else {
151             assertEquals("Wrong length", EncodeConstants.SIZE_OF_SHORT_IN_BYTES, buffer.readUnsignedByte());
152         }
153     }
154 }