27918975d083d041ab88aa86e7a32ff2e96ee3d4
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / match / OxmIcmpv4CodeSerializerTest.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.Test;
16 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Icmpv4Code;
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.match.entries.grouping.MatchEntryBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Icmpv4CodeCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.icmpv4.code._case.Icmpv4CodeBuilder;
22
23 /**
24  * Unit tests for OxmIcmpv4CodeSerializer.
25  *
26  * @author michal.polkorab
27  */
28 public class OxmIcmpv4CodeSerializerTest {
29
30     OxmIcmpv4CodeSerializer serializer = new OxmIcmpv4CodeSerializer();
31
32     /**
33      * Test correct serialization.
34      */
35     @Test
36     public void testSerialize() {
37         MatchEntryBuilder builder = prepareIcmpv4CodeMatchEntry((short) 200);
38
39         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
40         serializer.serialize(builder.build(), buffer);
41
42         checkHeader(buffer, false);
43         assertEquals("Wrong value", 200, buffer.readUnsignedByte());
44         assertTrue("Unexpected data", buffer.readableBytes() == 0);
45     }
46
47     /**
48      * Test correct header serialization.
49      */
50     @Test
51     public void testSerializeHeader() {
52         MatchEntryBuilder builder = prepareIcmpv4CodeHeader(false);
53
54         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
55         serializer.serializeHeader(builder.build(), buffer);
56
57         checkHeader(buffer, false);
58         assertTrue("Unexpected data", buffer.readableBytes() == 0);
59     }
60
61     /**
62      * Test correct oxm-class return value.
63      */
64     @Test
65     public void testGetOxmClassCode() {
66         assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());
67     }
68
69     /**
70      * Test correct oxm-field return value.
71      */
72     @Test
73     public void getOxmFieldCode() {
74         assertEquals("Wrong oxm-class", OxmMatchConstants.ICMPV4_CODE, serializer.getOxmFieldCode());
75     }
76
77     /**
78      * Test correct value length return value.
79      */
80     @Test
81     public void testGetValueLength() {
82         assertEquals("Wrong value length", Byte.BYTES, serializer.getValueLength());
83     }
84
85     private static MatchEntryBuilder prepareIcmpv4CodeMatchEntry(short value) {
86         MatchEntryBuilder builder = prepareIcmpv4CodeHeader(false);
87         Icmpv4CodeCaseBuilder casebuilder = new Icmpv4CodeCaseBuilder();
88         Icmpv4CodeBuilder valueBuilder = new Icmpv4CodeBuilder();
89         valueBuilder.setIcmpv4Code(value);
90         casebuilder.setIcmpv4Code(valueBuilder.build());
91         builder.setMatchEntryValue(casebuilder.build());
92         return builder;
93     }
94
95     private static MatchEntryBuilder prepareIcmpv4CodeHeader(boolean hasMask) {
96         MatchEntryBuilder builder = new MatchEntryBuilder();
97         builder.setOxmClass(OpenflowBasicClass.class);
98         builder.setOxmMatchField(Icmpv4Code.class);
99         builder.setHasMask(hasMask);
100         return builder;
101     }
102
103     private static void checkHeader(ByteBuf buffer, boolean hasMask) {
104         assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
105         short fieldAndMask = buffer.readUnsignedByte();
106         assertEquals("Wrong oxm-field", OxmMatchConstants.ICMPV4_CODE, fieldAndMask >>> 1);
107         assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
108         assertEquals("Wrong length", Byte.BYTES, buffer.readUnsignedByte());
109     }
110 }