Bug 2756 - Match model update
[openflowjava.git] / 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 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.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.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  * @author michal.polkorab
29  *
30  */
31 public class OxmIpv6NdTllSerializerTest {
32
33     OxmIpv6NdTllSerializer serializer = new OxmIpv6NdTllSerializer();
34
35     /**
36      * Test correct serialization
37      */
38     @Test
39     public void testSerialize() {
40         MatchEntryBuilder builder = prepareMatchEntry("00:01:02:03:04:05");
41
42         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
43         serializer.serialize(builder.build(), buffer);
44
45         checkHeader(buffer, false);
46         byte[] address = new byte[6];
47         buffer.readBytes(address);
48         Assert.assertArrayEquals("Wrong address", new byte[]{0, 1, 2, 3, 4, 5}, address);
49         assertTrue("Unexpected data", buffer.readableBytes() == 0);
50     }
51
52     /**
53      * Test correct header serialization
54      */
55     @Test
56     public void testSerializeHeader() {
57         MatchEntryBuilder builder = prepareHeader(false);
58
59         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
60         serializer.serializeHeader(builder.build(), buffer);
61
62         checkHeader(buffer, false);
63         assertTrue("Unexpected data", buffer.readableBytes() == 0);
64     }
65
66     /**
67      * Test correct oxm-class return value
68      */
69     @Test
70     public void testGetOxmClassCode() {
71         assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, serializer.getOxmClassCode());
72     }
73
74     /**
75      * Test correct oxm-field return value
76      */
77     @Test
78     public void getOxmFieldCode() {
79         assertEquals("Wrong oxm-class", OxmMatchConstants.IPV6_ND_TLL, serializer.getOxmFieldCode());
80     }
81
82     /**
83      * Test correct value length return value
84      */
85     @Test
86     public void testGetValueLength() {
87         assertEquals("Wrong value length", EncodeConstants.MAC_ADDRESS_LENGTH, serializer.getValueLength());
88     }
89
90     private static MatchEntryBuilder prepareMatchEntry(String value) {
91         MatchEntryBuilder builder = prepareHeader(false);
92         Ipv6NdTllCaseBuilder casebuilder = new Ipv6NdTllCaseBuilder();
93         Ipv6NdTllBuilder valueBuilder = new Ipv6NdTllBuilder();
94         valueBuilder.setMacAddress(new MacAddress(value));
95         casebuilder.setIpv6NdTll(valueBuilder.build());
96         builder.setMatchEntryValue(casebuilder.build());
97         return builder;
98     }
99
100     private static MatchEntryBuilder prepareHeader(boolean hasMask) {
101         MatchEntryBuilder builder = new MatchEntryBuilder();
102         builder.setOxmClass(OpenflowBasicClass.class);
103         builder.setOxmMatchField(Ipv6NdTll.class);
104         builder.setHasMask(hasMask);
105         return builder;
106     }
107
108     private static void checkHeader(ByteBuf buffer, boolean hasMask) {
109         assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
110         short fieldAndMask = buffer.readUnsignedByte();
111         assertEquals("Wrong oxm-field", OxmMatchConstants.IPV6_ND_TLL, fieldAndMask >>> 1);
112         assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
113         assertEquals("Wrong length", EncodeConstants.MAC_ADDRESS_LENGTH, buffer.readUnsignedByte());
114     }
115 }