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