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
9 package org.opendaylight.protocol.bgp.inet.codec.nexthop;
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertTrue;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
20 import org.opendaylight.protocol.util.ByteArray;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.CNextHop;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv4NextHopCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv6NextHopCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv6NextHopCaseBuilder;
28 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;
29 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;
31 public class NextHopParserSerializerTest {
33 private static final byte[] IPV6LB = {0x20, 1, 0x0d, (byte) 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
34 1, (byte) 0xfe, (byte) 0x80, 0, 0, 0, 0, 0, 0, (byte) 0xc0, 1, 0x0b, (byte) 0xff, (byte) 0xfe, 0x7e, 0, 0};
36 private static final Ipv6Address IPV6 = new Ipv6Address("2001:db8::1");
37 private static final Ipv6Address IPV6L = new Ipv6Address("fe80::c001:bff:fe7e:0");
39 private Ipv4NextHopParserSerializer ipv4NextHopParserSerializer;
40 private Ipv6NextHopParserSerializer ipv6NextHopParserSerializer;
42 private ByteBuf buffer;
45 public final void setUp() {
46 this.ipv4NextHopParserSerializer = new Ipv4NextHopParserSerializer();
47 this.ipv6NextHopParserSerializer = new Ipv6NextHopParserSerializer();
48 this.buffer = Unpooled.buffer();
52 public void testSerializeIpv4NextHopCase() throws BGPParsingException {
53 final byte[] ipv4B = {42, 42, 42, 42};
54 this.hop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(new Ipv4NextHopBuilder()
55 .setGlobal(new Ipv4Address("42.42.42.42")).build()).build();
57 this.ipv4NextHopParserSerializer.serializeNextHop(this.hop, this.buffer);
58 assertArrayEquals(ipv4B, ByteArray.readAllBytes(this.buffer));
60 final CNextHop parsedHop = this.ipv4NextHopParserSerializer.parseNextHop(Unpooled.wrappedBuffer(ipv4B));
61 assertTrue(this.hop instanceof Ipv4NextHopCase);
62 assertEquals(this.hop, parsedHop);
66 public void testSerializeIpv6LinkNextHopCase() throws BGPParsingException {
67 this.hop = new Ipv6NextHopCaseBuilder().setIpv6NextHop(new Ipv6NextHopBuilder()
68 .setGlobal(IPV6).setLinkLocal(IPV6L).build()).build();
70 this.ipv6NextHopParserSerializer.serializeNextHop(this.hop, this.buffer);
71 assertArrayEquals(IPV6LB, ByteArray.readAllBytes(this.buffer));
73 final CNextHop parsedHop = this.ipv6NextHopParserSerializer.parseNextHop(Unpooled.wrappedBuffer(IPV6LB));
74 assertTrue(parsedHop instanceof Ipv6NextHopCase);
75 assertEquals(this.hop, parsedHop);
79 public void testSerializeIpv4NextHopEmpty() {
82 this.ipv4NextHopParserSerializer.serializeNextHop(() -> null, this.buffer);
83 } catch (final IllegalArgumentException e) {
84 assertEquals("cNextHop is not a Ipv4 NextHop object.", e.getMessage());
89 public void testSerializeIpv6NextHopEmpty() {
92 this.ipv6NextHopParserSerializer.serializeNextHop(() -> null, this.buffer);
93 } catch (final IllegalArgumentException e) {
94 assertEquals("cNextHop is not a Ipv6 NextHop object.", e.getMessage());