Add method to register listener for unknown msg
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / match / OxmIpv6SrcSerializerTest.java
1 /*
2  * Copyright (c) 2015 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.inet.types.rev130715.Ipv6Address;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Ipv6Src;
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.Ipv6SrcCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ipv6.src._case.Ipv6SrcBuilder;
26
27 /**
28  * @author michal.polkorab
29  *
30  */
31 public class OxmIpv6SrcSerializerTest {
32
33     OxmIpv6SrcSerializer serializer = new OxmIpv6SrcSerializer();
34
35     /**
36      * Test correct serialization
37      */
38     @Test
39     public void testSerializeWithoutMask() {
40         MatchEntryBuilder builder = prepareMatchEntry(false, "aaaa:bbbb:1111:2222::");
41
42         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
43         serializer.serialize(builder.build(), buffer);
44
45         checkHeader(buffer, false);
46         Assert.assertEquals("Wrong ipv6 address", 43690, buffer.readUnsignedShort());
47         Assert.assertEquals("Wrong ipv6 address", 48059, buffer.readUnsignedShort());
48         Assert.assertEquals("Wrong ipv6 address", 4369, buffer.readUnsignedShort());
49         Assert.assertEquals("Wrong ipv6 address", 8738, buffer.readUnsignedShort());
50         Assert.assertEquals("Wrong ipv6 address", 0, buffer.readUnsignedShort());
51         Assert.assertEquals("Wrong ipv6 address", 0, buffer.readUnsignedShort());
52         Assert.assertEquals("Wrong ipv6 address", 0, buffer.readUnsignedShort());
53         Assert.assertEquals("Wrong ipv6 address", 0, buffer.readUnsignedShort());
54         assertTrue("Unexpected data", buffer.readableBytes() == 0);
55     }
56
57     /**
58      * Test correct serialization
59      */
60     @Test
61     public void testSerialize() {
62         MatchEntryBuilder builder = prepareMatchEntry(false, "::aaaa:bbbb:1111:2222");
63
64         ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
65         serializer.serialize(builder.build(), buffer);
66
67         checkHeader(buffer, false);
68         Assert.assertEquals("Wrong ipv6 address", 0, buffer.readUnsignedShort());
69         Assert.assertEquals("Wrong ipv6 address", 0, buffer.readUnsignedShort());
70         Assert.assertEquals("Wrong ipv6 address", 0, buffer.readUnsignedShort());
71         Assert.assertEquals("Wrong ipv6 address", 0, buffer.readUnsignedShort());
72         Assert.assertEquals("Wrong ipv6 address", 43690, buffer.readUnsignedShort());
73         Assert.assertEquals("Wrong ipv6 address", 48059, buffer.readUnsignedShort());
74         Assert.assertEquals("Wrong ipv6 address", 4369, buffer.readUnsignedShort());
75         Assert.assertEquals("Wrong ipv6 address", 8738, buffer.readUnsignedShort());
76         assertTrue("Unexpected data", buffer.readableBytes() == 0);
77     }
78
79     private static MatchEntryBuilder prepareMatchEntry(boolean hasMask, String value) {
80         MatchEntryBuilder builder = prepareHeader(hasMask);
81         Ipv6SrcCaseBuilder caseBuilder = new Ipv6SrcCaseBuilder();
82         Ipv6SrcBuilder srcBuilder = new Ipv6SrcBuilder();
83         srcBuilder.setIpv6Address(new Ipv6Address(value));
84         if (hasMask) {
85             srcBuilder.setMask(new byte[]{15, 15, 0, 0});
86         }
87         caseBuilder.setIpv6Src(srcBuilder.build());
88         builder.setMatchEntryValue(caseBuilder.build());
89         return builder;
90     }
91
92     private static MatchEntryBuilder prepareHeader(boolean hasMask) {
93         MatchEntryBuilder builder = new MatchEntryBuilder();
94         builder.setOxmClass(OpenflowBasicClass.class);
95         builder.setOxmMatchField(Ipv6Src.class);
96         builder.setHasMask(hasMask);
97         return builder;
98     }
99
100     private static void checkHeader(ByteBuf buffer, boolean hasMask) {
101         assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
102         short fieldAndMask = buffer.readUnsignedByte();
103         assertEquals("Wrong oxm-field", OxmMatchConstants.IPV6_SRC, fieldAndMask >>> 1);
104         assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
105         if (hasMask) {
106             assertEquals("Wrong length", 2 * EncodeConstants.SIZE_OF_IPV6_ADDRESS_IN_BYTES,
107                     buffer.readUnsignedByte());
108         } else {
109             assertEquals("Wrong length", EncodeConstants.SIZE_OF_IPV6_ADDRESS_IN_BYTES, buffer.readUnsignedByte());
110         }
111     }
112 }