806f13cc46f360f7e97f47e6c68597bef83fd673
[bgpcep.git] / bgp / inet / src / test / java / org / opendaylight / protocol / bgp / inet / codec / nexthop / NextHopParserSerializerTest.java
1 /*
2  * Copyright (c) 2015 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.inet.codec.nexthop;
10
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertTrue;
14
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.inet.codec.nexthop.Ipv4NextHopParserSerializer;
20 import org.opendaylight.protocol.bgp.inet.codec.nexthop.Ipv6NextHopParserSerializer;
21 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
22 import org.opendaylight.protocol.util.ByteArray;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.CNextHop;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv6NextHopCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv6NextHopCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.ipv4.next.hop._case.Ipv4NextHopBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.ipv6.next.hop._case.Ipv6NextHopBuilder;
32 import org.opendaylight.yangtools.yang.binding.DataContainer;
33
34 public class NextHopParserSerializerTest {
35
36     public static final byte[] ipv6lB = {0x20, 1, 0x0d, (byte) 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
37         1, (byte) 0xfe, (byte) 0x80, 0, 0, 0, 0, 0, 0, (byte) 0xc0, 1, 0x0b, (byte) 0xff, (byte) 0xfe, 0x7e, 0, 0};
38
39     public static final Ipv6Address ipv6 = new Ipv6Address("2001:db8::1");
40     public static final Ipv6Address ipv6l = new Ipv6Address("fe80::c001:bff:fe7e:0");
41
42     Ipv4NextHopParserSerializer ipv4NextHopParserSerializer;
43     Ipv6NextHopParserSerializer ipv6NextHopParserSerializer;
44     CNextHop hop;
45     ByteBuf buffer;
46
47     @Before
48     public final void setUp() {
49         this.ipv4NextHopParserSerializer = new Ipv4NextHopParserSerializer();
50         this.ipv6NextHopParserSerializer = new Ipv6NextHopParserSerializer();
51         this.buffer = Unpooled.buffer();
52     }
53
54     @Test
55     public void testSerializeIpv4NextHopCase() throws BGPParsingException {
56         final byte[] ipv4B = {42, 42, 42, 42};
57         this.hop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(new Ipv4NextHopBuilder()
58             .setGlobal(new Ipv4Address("42.42.42.42")).build()).build();
59
60         this.ipv4NextHopParserSerializer.serializeNextHop(this.hop, this.buffer);
61         assertArrayEquals(ipv4B, ByteArray.readAllBytes(this.buffer));
62
63         final CNextHop parsedHop = this.ipv4NextHopParserSerializer.parseNextHop(Unpooled.wrappedBuffer(ipv4B));
64         assertTrue(this.hop instanceof Ipv4NextHopCase);
65         assertEquals(this.hop, parsedHop);
66     }
67
68     @Test
69     public void testSerializeIpv6LinkNextHopCase() throws BGPParsingException {
70         this.hop = new Ipv6NextHopCaseBuilder().setIpv6NextHop(new Ipv6NextHopBuilder().setGlobal(ipv6).setLinkLocal(ipv6l).build()).build();
71         this.buffer.clear();
72         this.ipv6NextHopParserSerializer.serializeNextHop(this.hop, this.buffer);
73         assertArrayEquals(ipv6lB, ByteArray.readAllBytes(this.buffer));
74
75         final CNextHop parsedHop = this.ipv6NextHopParserSerializer.parseNextHop(Unpooled.wrappedBuffer(ipv6lB));
76         assertTrue(parsedHop instanceof Ipv6NextHopCase);
77         assertEquals(this.hop, parsedHop);
78     }
79
80     @Test
81     public void testSerializeIpv4NextHopEmpty() {
82         this.buffer.clear();
83         try {
84             this.ipv4NextHopParserSerializer.serializeNextHop(() -> null, this.buffer);
85         } catch (final IllegalArgumentException e) {
86             assertEquals("cNextHop is not a Ipv4 NextHop object.", e.getMessage());
87         }
88     }
89
90     @Test
91     public void testSerializeIpv6NextHopEmpty() {
92         this.buffer.clear();
93         try {
94             this.ipv6NextHopParserSerializer.serializeNextHop(() -> null, this.buffer);
95         } catch (final IllegalArgumentException e) {
96             assertEquals("cNextHop is not a Ipv6 NextHop object.", e.getMessage());
97         }
98     }
99 }