Bump upstreams
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / protocol / serialization / match / Ipv6DestinationEntrySerializerTest.java
1 /*
2  * Copyright (c) 2016 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.openflowplugin.impl.protocol.serialization.match;
10
11 import static org.junit.Assert.assertArrayEquals;
12
13 import org.junit.Test;
14 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
15 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
16 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.IpConversionUtil;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6MatchBuilder;
22
23 public class Ipv6DestinationEntrySerializerTest extends AbstractMatchEntrySerializerTest {
24
25     @Test
26     public void testSerialize() {
27         final Ipv6Prefix ipv6Address = new Ipv6Prefix("2001:db8::/32");
28
29         final Match ipv6abmMatch = new MatchBuilder()
30                 .setLayer3Match(new Ipv6MatchBuilder()
31                         .setIpv6Destination(ipv6Address)
32                         .build())
33                 .build();
34
35         assertMatch(ipv6abmMatch, true, (out) -> {
36             byte[] address = new byte[16];
37             out.readBytes(address);
38             assertArrayEquals(address,
39                     IetfInetUtil.ipv6AddressBytes(IpConversionUtil.extractIpv6Address(ipv6Address)));
40
41             byte[] mask = new byte[16];
42             out.readBytes(mask);
43             assertArrayEquals(mask,
44                     IpConversionUtil.convertIpv6PrefixToByteArray(IpConversionUtil.extractIpv6Prefix(ipv6Address)));
45         });
46     }
47
48     @Test
49     public void testSerializeWithoutMask() {
50         final Ipv6Prefix ipv6Address = new Ipv6Prefix("2001:db8::123/128");
51
52         final Match ipv6abmMatch = new MatchBuilder()
53                 .setLayer3Match(new Ipv6MatchBuilder()
54                         .setIpv6Destination(ipv6Address)
55                         .build())
56                 .build();
57
58         assertMatch(ipv6abmMatch, false, (out) -> {
59             byte[] address = new byte[16];
60             out.readBytes(address);
61             assertArrayEquals(address,
62                     IetfInetUtil.ipv6AddressBytes(IpConversionUtil.extractIpv6Address(ipv6Address)));
63         });
64     }
65
66     @Override
67     protected short getLength() {
68         return EncodeConstants.SIZE_OF_IPV6_ADDRESS_IN_BYTES;
69     }
70
71     @Override
72     protected int getOxmFieldCode() {
73         return OxmMatchConstants.IPV6_DST;
74     }
75
76     @Override
77     protected int getOxmClassCode() {
78         return OxmMatchConstants.OPENFLOW_BASIC_CLASS;
79     }
80
81 }