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