Bump upstreams for 2022.09 Chlorine
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / match / OxmIpv6NdTllSerializerTest.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.Ipv6NdTll;
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.Ipv6NdTllCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ipv6.nd.tll._case.Ipv6NdTllBuilder;
26
27 /**
28  * Unit tests for OxmIpv6NdTllSerializer.
29  *
30  * @author michal.polkorab
31  */
32 public class OxmIpv6NdTllSerializerTest {
33
34     OxmIpv6NdTllSerializer serializer = new OxmIpv6NdTllSerializer();
35
36     /**
37      * Test correct serialization.
38      */
39     @Test
40     public void testSerialize() {
41         MatchEntryBuilder builder = prepareMatchEntry("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 header serialization.
55      */
56     @Test
57     public void testSerializeHeader() {
58         MatchEntryBuilder builder = prepareHeader(false);
59
60         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
61         serializer.serializeHeader(builder.build(), buffer);
62
63         checkHeader(buffer, false);
64         assertTrue("Unexpected data", buffer.readableBytes() == 0);
65     }
66
67     /**
68      * Test correct oxm-class return value.
69      */
70     @Test
71     public void testGetOxmClassCode() {
72         assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());
73     }
74
75     /**
76      * Test correct oxm-field return value.
77      */
78     @Test
79     public void getOxmFieldCode() {
80         assertEquals("Wrong oxm-class", OxmMatchConstants.IPV6_ND_TLL, serializer.getOxmFieldCode());
81     }
82
83     /**
84      * Test correct value length return value.
85      */
86     @Test
87     public void testGetValueLength() {
88         assertEquals("Wrong value length", EncodeConstants.MAC_ADDRESS_LENGTH, serializer.getValueLength());
89     }
90
91     private static MatchEntryBuilder prepareMatchEntry(final String value) {
92         MatchEntryBuilder builder = prepareHeader(false);
93         Ipv6NdTllCaseBuilder casebuilder = new Ipv6NdTllCaseBuilder();
94         Ipv6NdTllBuilder valueBuilder = new Ipv6NdTllBuilder();
95         valueBuilder.setMacAddress(new MacAddress(value));
96         casebuilder.setIpv6NdTll(valueBuilder.build());
97         builder.setMatchEntryValue(casebuilder.build());
98         return builder;
99     }
100
101     private static MatchEntryBuilder prepareHeader(final boolean hasMask) {
102         MatchEntryBuilder builder = new MatchEntryBuilder();
103         builder.setOxmClass(OpenflowBasicClass.VALUE);
104         builder.setOxmMatchField(Ipv6NdTll.VALUE);
105         builder.setHasMask(hasMask);
106         return builder;
107     }
108
109     private static void checkHeader(final ByteBuf buffer, final boolean hasMask) {
110         assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
111         short fieldAndMask = buffer.readUnsignedByte();
112         assertEquals("Wrong oxm-field", OxmMatchConstants.IPV6_ND_TLL, fieldAndMask >>> 1);
113         assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
114         assertEquals("Wrong length", EncodeConstants.MAC_ADDRESS_LENGTH, buffer.readUnsignedByte());
115     }
116 }