Enforce findbug & checkstyle under evon module
[bgpcep.git] / bgp / evpn / src / test / java / org / opendaylight / protocol / bgp / evpn / impl / attributes / tunnel / identifier / PAddressPMulticastGroupUtilTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.protocol.bgp.evpn.impl.attributes.tunnel.identifier;
10
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNull;
14 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.IPV6;
15 import static org.opendaylight.protocol.bgp.evpn.impl.attributes.tunnel.identifier.PMSITunnelAttributeHandlerTestUtil.IP_ADDRESS;
16 import static org.opendaylight.protocol.bgp.evpn.impl.attributes.tunnel.identifier.PMSITunnelAttributeHandlerTestUtil.P_MULTICAST;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.buffer.Unpooled;
20 import org.junit.Test;
21 import org.opendaylight.protocol.util.ByteArray;
22 import org.opendaylight.protocol.util.Ipv4Util;
23 import org.opendaylight.protocol.util.Ipv6Util;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev160812.PAddressPMulticastGroup;
26 import org.opendaylight.yangtools.yang.binding.DataContainer;
27
28 public class PAddressPMulticastGroupUtilTest {
29     private static final byte[] IPV4_ADDRESS_EXPECTED = {
30         (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01
31     };
32     private static final byte[] IPV6_ADDRESS_EXPECTED = {
33         0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
34     };
35
36     private static final byte[] SENDER_P_MULTICAST_GROUP_EXPECTED = {
37         (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01,
38         (byte) 0x17, (byte) 0x01, (byte) 0x01, (byte) 0x01
39     };
40
41     private static class MyPAddressPMulticastGroup implements PAddressPMulticastGroup {
42         @Override
43         public IpAddress getPAddress() {
44             return IP_ADDRESS;
45         }
46
47         @Override
48         public IpAddress getPMulticastGroup() {
49             return P_MULTICAST;
50         }
51
52         @Override
53         public Class<? extends DataContainer> getImplementedInterface() {
54             return null;
55         }
56     }
57
58     @Test
59     public void parseIpAddress() throws Exception {
60         final ByteBuf ipv4Actual = Unpooled.buffer();
61         PAddressPMulticastGroupUtil.serializeIpAddress(IP_ADDRESS, ipv4Actual);
62         assertArrayEquals(IPV4_ADDRESS_EXPECTED, ByteArray.readAllBytes(ipv4Actual));
63         final ByteBuf ipv6Actual = Unpooled.buffer();
64         PAddressPMulticastGroupUtil.serializeIpAddress(IPV6, ipv6Actual);
65         assertArrayEquals(IPV6_ADDRESS_EXPECTED, ByteArray.readAllBytes(ipv6Actual));
66         assertEquals(IP_ADDRESS, PAddressPMulticastGroupUtil.parseIpAddress(Ipv4Util.IP4_LENGTH,
67                 Unpooled.wrappedBuffer(IPV4_ADDRESS_EXPECTED)));
68         assertEquals(IPV6, PAddressPMulticastGroupUtil.parseIpAddress(Ipv6Util.IPV6_LENGTH,
69                 Unpooled.wrappedBuffer(IPV6_ADDRESS_EXPECTED)));
70         assertNull(PAddressPMulticastGroupUtil.parseIpAddress(6,
71                 Unpooled.wrappedBuffer(IPV4_ADDRESS_EXPECTED)));
72     }
73
74     @Test
75     public void parseIpAddressPMulticastGroup() throws Exception {
76         final PAddressPMulticastGroup pAddressPMulticastGroup = new MyPAddressPMulticastGroup();
77         final ByteBuf pAddressPMulticastGroupActual = Unpooled.buffer();
78         PAddressPMulticastGroupUtil.serializeSenderPMulticastGroup(pAddressPMulticastGroup,
79                 pAddressPMulticastGroupActual);
80         assertArrayEquals(SENDER_P_MULTICAST_GROUP_EXPECTED, ByteArray.readAllBytes(pAddressPMulticastGroupActual));
81
82         final PAddressPMulticastGroup actual = PAddressPMulticastGroupUtil
83                 .parseSenderPMulticastGroup(Unpooled.wrappedBuffer(SENDER_P_MULTICAST_GROUP_EXPECTED));
84         assertEquals(IP_ADDRESS, actual.getPAddress());
85         assertEquals(P_MULTICAST, actual.getPMulticastGroup());
86     }
87 }