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