c7a2d7f26bccb48f85cdf85ca8bce58aed98d53d
[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.P_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 java.lang.reflect.Constructor;
21 import java.lang.reflect.InvocationTargetException;
22 import org.junit.Test;
23 import org.opendaylight.protocol.util.ByteArray;
24 import org.opendaylight.protocol.util.Ipv4Util;
25 import org.opendaylight.protocol.util.Ipv6Util;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev160812.PAddressPMulticastGroup;
28 import org.opendaylight.yangtools.yang.binding.DataContainer;
29
30 public class PAddressPMulticastGroupUtilTest {
31     private static final byte[] IPV4_ADDRESS_EXPECTED = {
32         (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01
33     };
34     private static final byte[] IPV6_ADDRESS_EXPECTED = {
35         0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
36     };
37
38     private static final byte[] SENDER_P_MULTICAST_GROUP_EXPECTED = {
39         (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01,
40         (byte) 0x17, (byte) 0x01, (byte) 0x01, (byte) 0x01
41     };
42
43     private static class MyPAddressPMulticastGroup implements PAddressPMulticastGroup {
44         @Override
45         public IpAddress getPAddress() {
46             return P_ADDRESS;
47         }
48
49         @Override
50         public IpAddress getPMulticastGroup() {
51             return P_MULTICAST;
52         }
53
54         @Override
55         public Class<? extends DataContainer> getImplementedInterface() {
56             return null;
57         }
58     }
59
60     @Test
61     public void parseIpAddress() throws Exception {
62         final ByteBuf ipv4Actual = Unpooled.buffer();
63         PAddressPMulticastGroupUtil.serializeIpAddress(P_ADDRESS, ipv4Actual);
64         assertArrayEquals(IPV4_ADDRESS_EXPECTED, ByteArray.readAllBytes(ipv4Actual));
65         final ByteBuf ipv6Actual = Unpooled.buffer();
66         PAddressPMulticastGroupUtil.serializeIpAddress(IPV6, ipv6Actual);
67         assertArrayEquals(IPV6_ADDRESS_EXPECTED, ByteArray.readAllBytes(ipv6Actual));
68         assertEquals(P_ADDRESS, PAddressPMulticastGroupUtil.parseIpAddress(Ipv4Util.IP4_LENGTH, Unpooled.wrappedBuffer(IPV4_ADDRESS_EXPECTED)));
69         assertEquals(IPV6, PAddressPMulticastGroupUtil.parseIpAddress(Ipv6Util.IPV6_LENGTH, Unpooled.wrappedBuffer(IPV6_ADDRESS_EXPECTED)));
70         assertNull(PAddressPMulticastGroupUtil.parseIpAddress(6, Unpooled.wrappedBuffer(IPV4_ADDRESS_EXPECTED)));
71     }
72
73     @Test
74     public void parseIpAddressPMulticastGroup() throws Exception {
75         final PAddressPMulticastGroup pAddressPMulticastGroup = new MyPAddressPMulticastGroup();
76         final ByteBuf pAddressPMulticastGroupActual = Unpooled.buffer();
77         PAddressPMulticastGroupUtil.serializeSenderPMulticastGroup(pAddressPMulticastGroup, pAddressPMulticastGroupActual);
78         assertArrayEquals(SENDER_P_MULTICAST_GROUP_EXPECTED, ByteArray.readAllBytes(pAddressPMulticastGroupActual));
79
80         final PAddressPMulticastGroup actual = PAddressPMulticastGroupUtil.parseSenderPMulticastGroup(Unpooled.wrappedBuffer
81             (SENDER_P_MULTICAST_GROUP_EXPECTED));
82         assertEquals(pAddressPMulticastGroup.getPAddress(), actual.getPAddress());
83         assertEquals(pAddressPMulticastGroup.getPMulticastGroup(), actual.getPMulticastGroup());
84     }
85
86     @Test(expected = UnsupportedOperationException.class)
87     public void testPrivateConstructor() throws Throwable {
88         final Constructor<PAddressPMulticastGroupUtil> constructor = PAddressPMulticastGroupUtil.class.getDeclaredConstructor();
89         constructor.setAccessible(true);
90         try {
91             constructor.newInstance();
92         } catch (final InvocationTargetException e) {
93             throw e.getCause();
94         }
95     }
96 }