Fix most bgp-parser-impl checkstyle
[bgpcep.git] / bgp / parser-impl / src / test / java / org / opendaylight / protocol / bgp / parser / impl / message / update / NextHopAttributeParserTest.java
1 /*
2  * Copyright (c) 2018 AT&T Intellectual Property. 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 package org.opendaylight.protocol.bgp.parser.impl.message.update;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.junit.Test;
16 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
17 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
18 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
19 import org.opendaylight.protocol.util.ByteArray;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv6NextHopCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.ipv4.next.hop._case.Ipv4NextHopBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.ipv6.next.hop._case.Ipv6NextHopBuilder;
28
29 public class NextHopAttributeParserTest {
30
31     private static final byte[] IPV4_NEXT_HOP_BYTES = {
32         (byte) 0x40, (byte) 0x03, (byte) 0x04, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
33     };
34     private static final byte[] IPV6_NEXT_HOP_BYTES = {
35         (byte) 0x40, (byte) 0x03, (byte) 0x20, (byte) 0xFF, (byte) 0xFF, (byte) 0x00, (byte) 0x00,
36         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
37         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0xFF, (byte) 0xFF,
38         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
39         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02
40     };
41
42     private static final Attributes IPV4_RESULT = new AttributesBuilder()
43             .setCNextHop(new Ipv4NextHopCaseBuilder().setIpv4NextHop(new Ipv4NextHopBuilder()
44                     .setGlobal(new Ipv4Address("255.255.255.255"))
45                     .build()).build()).build();
46     private static final Attributes IPV6_RESULT = new AttributesBuilder()
47             .setCNextHop(new Ipv6NextHopCaseBuilder().setIpv6NextHop(new Ipv6NextHopBuilder()
48                     .setGlobal(new Ipv6Address("ffff::1"))
49                     .setLinkLocal(new Ipv6Address("ffff::2"))
50                     .build()).build()).build();
51
52     @Test
53     public void testIpv4AttributeParser() throws BGPParsingException, BGPDocumentedException {
54         final ByteBuf actual = Unpooled.buffer();
55         ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry()
56                 .serializeAttribute(IPV4_RESULT, actual);
57         assertArrayEquals(IPV4_NEXT_HOP_BYTES, ByteArray.getAllBytes(actual));
58
59         final Attributes attributeOut = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance()
60                 .getAttributeRegistry().parseAttributes(actual, null).getAttributes();
61         assertEquals(IPV4_RESULT.getCNextHop(), attributeOut.getCNextHop());
62     }
63
64     @Test
65     public void testIpv6AttributeParser() throws BGPParsingException, BGPDocumentedException {
66         final ByteBuf actual = Unpooled.buffer();
67         ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry()
68                 .serializeAttribute(IPV6_RESULT, actual);
69         assertArrayEquals(IPV6_NEXT_HOP_BYTES, ByteArray.getAllBytes(actual));
70
71         final Attributes attributeOut = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance()
72                 .getAttributeRegistry().parseAttributes(actual, null).getAttributes();
73         assertEquals(IPV6_RESULT.getCNextHop(), attributeOut.getCNextHop());
74     }
75
76     @Test (expected = NullPointerException.class)
77     public void testParseEmptyIpv4Attribute() {
78         ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry()
79                 .serializeAttribute(new AttributesBuilder()
80                         .setCNextHop(new Ipv4NextHopCaseBuilder().build())
81                         .build(), Unpooled.buffer());
82     }
83
84     @Test (expected = NullPointerException.class)
85     public void testParseEmptyIpv6Attribute() {
86         ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry()
87                 .serializeAttribute(new AttributesBuilder()
88                         .setCNextHop(new Ipv6NextHopCaseBuilder().build())
89                         .build(), Unpooled.buffer());
90     }
91 }