Mass-convert all compontents to use -no-zone addresses
[bgpcep.git] / bgp / extensions / 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 package org.opendaylight.protocol.bgp.inet.codec.nexthop;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13
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;
29
30 public class NextHopParserSerializerTest {
31
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};
34
35     private static final Ipv6AddressNoZone IPV6 = new Ipv6AddressNoZone("2001:db8::1");
36     private static final Ipv6AddressNoZone IPV6L = new Ipv6AddressNoZone("fe80::c001:bff:fe7e:0");
37
38     private Ipv4NextHopParserSerializer ipv4NextHopParserSerializer;
39     private Ipv6NextHopParserSerializer ipv6NextHopParserSerializer;
40     private CNextHop hop;
41     private ByteBuf buffer;
42
43     @Before
44     public final void setUp() {
45         this.ipv4NextHopParserSerializer = new Ipv4NextHopParserSerializer();
46         this.ipv6NextHopParserSerializer = new Ipv6NextHopParserSerializer();
47         this.buffer = Unpooled.buffer();
48     }
49
50     @Test
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();
55
56         this.ipv4NextHopParserSerializer.serializeNextHop(this.hop, this.buffer);
57         assertArrayEquals(ipv4B, ByteArray.readAllBytes(this.buffer));
58
59         final CNextHop parsedHop = this.ipv4NextHopParserSerializer.parseNextHop(Unpooled.wrappedBuffer(ipv4B));
60         assertTrue(this.hop instanceof Ipv4NextHopCase);
61         assertEquals(this.hop, parsedHop);
62     }
63
64     @Test
65     public void testSerializeIpv6LinkNextHopCase() throws BGPParsingException {
66         this.hop = new Ipv6NextHopCaseBuilder().setIpv6NextHop(new Ipv6NextHopBuilder()
67                 .setGlobal(IPV6).setLinkLocal(IPV6L).build()).build();
68         this.buffer.clear();
69         this.ipv6NextHopParserSerializer.serializeNextHop(this.hop, this.buffer);
70         assertArrayEquals(IPV6LB, ByteArray.readAllBytes(this.buffer));
71
72         final CNextHop parsedHop = this.ipv6NextHopParserSerializer.parseNextHop(Unpooled.wrappedBuffer(IPV6LB));
73         assertTrue(parsedHop instanceof Ipv6NextHopCase);
74         assertEquals(this.hop, parsedHop);
75     }
76
77     @Test
78     public void testSerializeIpv4NextHopEmpty() {
79         this.buffer.clear();
80         try {
81             this.ipv4NextHopParserSerializer.serializeNextHop(() -> null, this.buffer);
82         } catch (final IllegalArgumentException e) {
83             assertEquals("cNextHop is not a Ipv4 NextHop object.", e.getMessage());
84         }
85     }
86
87     @Test
88     public void testSerializeIpv6NextHopEmpty() {
89         this.buffer.clear();
90         try {
91             this.ipv6NextHopParserSerializer.serializeNextHop(() -> null, this.buffer);
92         } catch (final IllegalArgumentException e) {
93             assertEquals("cNextHop is not a Ipv6 NextHop object.", e.getMessage());
94         }
95     }
96 }