Merge "Add missing convertors for OF1.0 actions"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / protocol / serialization / match / Ipv6SourceEntrySerializerTest.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 Ipv6SourceEntrySerializerTest extends AbstractMatchEntrySerializerTest {
24
25     @Test
26     public void testSerialize() throws Exception {
27         final Ipv6Prefix ipv6Address = new Ipv6Prefix("2001:db8::/32");
28
29         final Match ipv6abmMatch = new MatchBuilder()
30                 .setLayer3Match(new Ipv6MatchBuilder()
31                         .setIpv6Source(ipv6Address)
32                         .build())
33                 .build();
34
35         assertMatch(ipv6abmMatch, true, (out) -> {
36             byte[] address = new byte[16];
37             out.readBytes(address);
38             assertArrayEquals(address, IetfInetUtil.INSTANCE.ipv6AddressBytes(IpConversionUtil.extractIpv6Address(ipv6Address)));
39
40             byte[] mask = new byte[16];
41             out.readBytes(mask);
42             assertArrayEquals(mask, IpConversionUtil.convertIpv6PrefixToByteArray(IpConversionUtil.extractIpv6Prefix(ipv6Address)));
43         });
44     }
45
46     @Test
47     public void testSerializeWithoutMask() throws Exception {
48         final Ipv6Prefix ipv6Address = new Ipv6Prefix("2001:db8::123/128");
49
50         final Match ipv6abmMatch = new MatchBuilder()
51                 .setLayer3Match(new Ipv6MatchBuilder()
52                         .setIpv6Source(ipv6Address)
53                         .build())
54                 .build();
55
56         assertMatch(ipv6abmMatch, false, (out) -> {
57             byte[] address = new byte[16];
58             out.readBytes(address);
59             assertArrayEquals(address, IetfInetUtil.INSTANCE.ipv6AddressBytes(IpConversionUtil.extractIpv6Address(ipv6Address)));
60         });
61     }
62
63     @Override
64     protected short getLength() {
65         return EncodeConstants.SIZE_OF_IPV6_ADDRESS_IN_BYTES;
66     }
67
68     @Override
69     protected int getOxmFieldCode() {
70         return OxmMatchConstants.IPV6_SRC;
71     }
72
73     @Override
74     protected int getOxmClassCode() {
75         return OxmMatchConstants.OPENFLOW_BASIC_CLASS;
76     }
77
78 }