2 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.parser.impl.message.update;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
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.Ipv4AddressNoZone;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
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;
29 public class NextHopAttributeParserTest {
31 private static final byte[] IPV4_NEXT_HOP_BYTES = {
32 (byte) 0x40, (byte) 0x03, (byte) 0x04, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
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
42 private static final Attributes IPV4_RESULT = new AttributesBuilder()
43 .setCNextHop(new Ipv4NextHopCaseBuilder().setIpv4NextHop(new Ipv4NextHopBuilder()
44 .setGlobal(new Ipv4AddressNoZone("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 Ipv6AddressNoZone("ffff::1"))
49 .setLinkLocal(new Ipv6AddressNoZone("ffff::2"))
50 .build()).build()).build();
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));
59 final Attributes attributeOut = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance()
60 .getAttributeRegistry().parseAttributes(actual, null).getAttributes();
61 assertEquals(IPV4_RESULT.getCNextHop(), attributeOut.getCNextHop());
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));
71 final Attributes attributeOut = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance()
72 .getAttributeRegistry().parseAttributes(actual, null).getAttributes();
73 assertEquals(IPV6_RESULT.getCNextHop(), attributeOut.getCNextHop());
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());
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());