Bug 2756 - Match model update
[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.oxm.rev150225.OpenflowBasicClass;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.VlanVid;
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.VlanVidCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.vlan.vid._case.VlanVidBuilder;
25
26 /**
27  * @author michal.polkorab
28  *
29  */
30 public class OxmVlanVidSerializerTest {
31
32     OxmVlanVidSerializer serializer = new OxmVlanVidSerializer();
33
34     /**
35      * Test correct serialization
36      */
37     @Test
38     public void testSerializeWithCfiBitSet() {
39         MatchEntryBuilder builder = prepareVlanVidMatchEntry(false, true);
40
41         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
42         serializer.serialize(builder.build(), buffer);
43
44         checkHeader(buffer, false);
45         assertEquals("Wrong value", 4596, buffer.readUnsignedShort());
46         assertTrue("Unexpected data", buffer.readableBytes() == 0);
47     }
48
49     /**
50      * Test correct serialization
51      */
52     @Test
53     public void testSerializeWithoutCfiBitSet() {
54         MatchEntryBuilder builder = prepareVlanVidMatchEntry(true, false);
55
56         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
57         serializer.serialize(builder.build(), buffer);
58
59         checkHeader(buffer, true);
60         assertEquals("Wrong value", 500, buffer.readUnsignedShort());
61         byte[] tmp = new byte[2];
62         buffer.readBytes(tmp);
63         Assert.assertArrayEquals("Wrong mask", new byte[]{15, 15}, tmp);
64         assertTrue("Unexpected data", buffer.readableBytes() == 0);
65     }
66
67     /**
68      * Test correct header serialization
69      */
70     @Test
71     public void testSerializeHeaderWithoutMask() {
72         MatchEntryBuilder builder = prepareVlanVidHeader(false);
73
74         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
75         serializer.serializeHeader(builder.build(), buffer);
76
77         checkHeader(buffer, false);
78         assertTrue("Unexpected data", buffer.readableBytes() == 0);
79     }
80
81     /**
82      * Test correct header serialization
83      */
84     @Test
85     public void testSerializeHeaderWithMask() {
86         MatchEntryBuilder builder = prepareVlanVidHeader(true);
87
88         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
89         serializer.serializeHeader(builder.build(), buffer);
90
91         checkHeader(buffer, true);
92         assertTrue("Unexpected data", buffer.readableBytes() == 0);
93     }
94
95     /**
96      * Test correct oxm-class return value
97      */
98     @Test
99     public void testGetOxmClassCode() {
100         assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());
101     }
102
103     /**
104      * Test correct oxm-field return value
105      */
106     @Test
107     public void getOxmFieldCode() {
108         assertEquals("Wrong oxm-class", OxmMatchConstants.VLAN_VID, serializer.getOxmFieldCode());
109     }
110
111     /**
112      * Test correct value length return value
113      */
114     @Test
115     public void testGetValueLength() {
116         assertEquals("Wrong value length", EncodeConstants.SIZE_OF_SHORT_IN_BYTES, serializer.getValueLength());
117     }
118
119     private static MatchEntryBuilder prepareVlanVidMatchEntry(boolean hasMask, boolean cfiBit) {
120         MatchEntryBuilder builder = prepareVlanVidHeader(hasMask);
121         VlanVidCaseBuilder casebuilder = new VlanVidCaseBuilder();
122         VlanVidBuilder valueBuilder = new VlanVidBuilder();
123         if (hasMask) {
124             valueBuilder.setMask(new byte[]{15, 15});
125         }
126         valueBuilder.setVlanVid(500);
127         valueBuilder.setCfiBit(cfiBit);
128         casebuilder.setVlanVid(valueBuilder.build());
129         builder.setMatchEntryValue(casebuilder.build());
130         return builder;
131     }
132
133     private static MatchEntryBuilder prepareVlanVidHeader(boolean hasMask) {
134         MatchEntryBuilder builder = new MatchEntryBuilder();
135         builder.setOxmClass(OpenflowBasicClass.class);
136         builder.setOxmMatchField(VlanVid.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.VLAN_VID, fieldAndMask >>> 1);
145         assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
146         if (hasMask) {
147             assertEquals("Wrong length", EncodeConstants.SIZE_OF_INT_IN_BYTES, buffer.readUnsignedByte());
148         } else {
149             assertEquals("Wrong length", EncodeConstants.SIZE_OF_SHORT_IN_BYTES, buffer.readUnsignedByte());
150         }
151     }
152 }