2 * Copyright (c) 2015 Cisco Systems, Inc. and others. 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.inet.codec.nexthop;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
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.types.rev180329.next.hop.CNextHop;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv4NextHopCase;
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.Ipv6NextHopCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv6NextHopCaseBuilder;
27 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;
28 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;
30 public class NextHopParserSerializerTest {
32 private static final byte[] IPV6LB = {0x20, 1, 0x0d, (byte) 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
33 1, (byte) 0xfe, (byte) 0x80, 0, 0, 0, 0, 0, 0, (byte) 0xc0, 1, 0x0b, (byte) 0xff, (byte) 0xfe, 0x7e, 0, 0};
35 private static final Ipv6AddressNoZone IPV6 = new Ipv6AddressNoZone("2001:db8::1");
36 private static final Ipv6AddressNoZone IPV6L = new Ipv6AddressNoZone("fe80::c001:bff:fe7e:0");
38 private Ipv4NextHopParserSerializer ipv4NextHopParserSerializer;
39 private Ipv6NextHopParserSerializer ipv6NextHopParserSerializer;
41 private ByteBuf buffer;
44 public final void setUp() {
45 this.ipv4NextHopParserSerializer = new Ipv4NextHopParserSerializer();
46 this.ipv6NextHopParserSerializer = new Ipv6NextHopParserSerializer();
47 this.buffer = Unpooled.buffer();
51 public void testSerializeIpv4NextHopCase() throws BGPParsingException {
52 final byte[] ipv4B = {42, 42, 42, 42};
53 this.hop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(new Ipv4NextHopBuilder()
54 .setGlobal(new Ipv4AddressNoZone("42.42.42.42")).build()).build();
56 this.ipv4NextHopParserSerializer.serializeNextHop(this.hop, this.buffer);
57 assertArrayEquals(ipv4B, ByteArray.readAllBytes(this.buffer));
59 final CNextHop parsedHop = this.ipv4NextHopParserSerializer.parseNextHop(Unpooled.wrappedBuffer(ipv4B));
60 assertTrue(this.hop instanceof Ipv4NextHopCase);
61 assertEquals(this.hop, parsedHop);
65 public void testSerializeIpv6LinkNextHopCase() throws BGPParsingException {
66 this.hop = new Ipv6NextHopCaseBuilder().setIpv6NextHop(new Ipv6NextHopBuilder()
67 .setGlobal(IPV6).setLinkLocal(IPV6L).build()).build();
69 this.ipv6NextHopParserSerializer.serializeNextHop(this.hop, this.buffer);
70 assertArrayEquals(IPV6LB, ByteArray.readAllBytes(this.buffer));
72 final CNextHop parsedHop = this.ipv6NextHopParserSerializer.parseNextHop(Unpooled.wrappedBuffer(IPV6LB));
73 assertTrue(parsedHop instanceof Ipv6NextHopCase);
74 assertEquals(this.hop, parsedHop);
78 public void testSerializeIpv4NextHopEmpty() {
81 this.ipv4NextHopParserSerializer.serializeNextHop(() -> null, this.buffer);
82 } catch (final IllegalArgumentException e) {
83 assertEquals("cNextHop is not a Ipv4 NextHop object.", e.getMessage());
88 public void testSerializeIpv6NextHopEmpty() {
91 this.ipv6NextHopParserSerializer.serializeNextHop(() -> null, this.buffer);
92 } catch (final IllegalArgumentException e) {
93 assertEquals("cNextHop is not a Ipv6 NextHop object.", e.getMessage());