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