Bump upstreams for 2022.09 Chlorine
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / match / OxmIpv4SrcSerializerTest.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.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Ipv4Src;
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.match.entries.grouping.MatchEntryBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Ipv4SrcCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ipv4.src._case.Ipv4SrcBuilder;
24
25 /**
26  * Unit tests for OxmIpv4SrcSerializer.
27  *
28  * @author michal.polkorab
29  */
30 public class OxmIpv4SrcSerializerTest {
31
32     OxmIpv4SrcSerializer serializer = new OxmIpv4SrcSerializer();
33
34     /**
35      * Test correct serialization.
36      */
37     @Test
38     public void testSerializeWithoutMask() {
39         MatchEntryBuilder builder = prepareMatchEntry(false, "10.0.0.1");
40
41         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
42         serializer.serialize(builder.build(), buffer);
43
44         checkHeader(buffer, false);
45         byte[] address = new byte[4];
46         buffer.readBytes(address);
47         Assert.assertArrayEquals("Wrong address", new byte[]{10, 0, 0, 1}, 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, "120.121.122.0");
57
58         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
59         serializer.serialize(builder.build(), buffer);
60
61         checkHeader(buffer, true);
62
63         byte[] address = new byte[4];
64         buffer.readBytes(address);
65         Assert.assertArrayEquals("Wrong address", new byte[]{120, 121, 122, 0}, address);
66         byte[] tmp = new byte[4];
67         buffer.readBytes(tmp);
68         Assert.assertArrayEquals("Wrong mask", new byte[]{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.IPV4_SRC, serializer.getOxmFieldCode());
114     }
115
116     /**
117      * Test correct value length return value.
118      */
119     @Test
120     public void testGetValueLength() {
121         assertEquals("Wrong value length", Integer.BYTES, serializer.getValueLength());
122     }
123
124     private static MatchEntryBuilder prepareMatchEntry(final boolean hasMask, final String value) {
125         final MatchEntryBuilder builder = prepareHeader(hasMask);
126         Ipv4SrcCaseBuilder casebuilder = new Ipv4SrcCaseBuilder();
127         Ipv4SrcBuilder valueBuilder = new Ipv4SrcBuilder();
128         if (hasMask) {
129             valueBuilder.setMask(new byte[]{15, 15, 0, 0});
130         }
131         valueBuilder.setIpv4Address(new Ipv4Address(value));
132         casebuilder.setIpv4Src(valueBuilder.build());
133         builder.setMatchEntryValue(casebuilder.build());
134         return builder;
135     }
136
137     private static MatchEntryBuilder prepareHeader(final boolean hasMask) {
138         MatchEntryBuilder builder = new MatchEntryBuilder();
139         builder.setOxmClass(OpenflowBasicClass.VALUE);
140         builder.setOxmMatchField(Ipv4Src.VALUE);
141         builder.setHasMask(hasMask);
142         return builder;
143     }
144
145     private static void checkHeader(final ByteBuf buffer, final boolean hasMask) {
146         assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
147         short fieldAndMask = buffer.readUnsignedByte();
148         assertEquals("Wrong oxm-field", OxmMatchConstants.IPV4_SRC, fieldAndMask >>> 1);
149         assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
150         if (hasMask) {
151             assertEquals("Wrong length", Long.BYTES, buffer.readUnsignedByte());
152         } else {
153             assertEquals("Wrong length", Integer.BYTES, buffer.readUnsignedByte());
154         }
155     }
156 }