2 * Copyright (c) 2013 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.parser.spi;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.fail;
15 import com.google.common.primitives.UnsignedBytes;
17 import org.junit.Test;
18 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlriBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.CNextHop;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv6NextHopCase;
24 public class UtilsTest {
27 public void testCapabilityUtil() {
28 final byte[] result = new byte[] { 1, 2, 4, 8 };
29 assertArrayEquals(result, CapabilityUtil.formatCapability(1, new byte[] { 4, 8 }));
33 public void testMessageUtil() {
34 final byte[] result = new byte[] { UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE,
35 UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE,
36 UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE,
37 UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, 0, 23, 3, 32, 5, 14, 21 };
38 assertArrayEquals(result, MessageUtil.formatMessage(3, new byte[] { 32, 5, 14, 21 }));
42 public void testNlriUtil() {
43 final MpReachNlriBuilder builder = new MpReachNlriBuilder();
44 final byte[] ipv4 = new byte[] { 42, 42, 42, 42 };
46 NlriUtil.parseNextHop(ipv4, builder);
47 } catch (final BGPParsingException e) {
48 fail("This exception should not happen");
50 CNextHop hop = builder.getCNextHop();
51 assertEquals("42.42.42.42", ((Ipv4NextHopCase) hop).getIpv4NextHop().getGlobal().getValue());
53 final byte[] ipv6 = new byte[] { (byte) 0x20, (byte) 0x01, (byte) 0x0d, (byte) 0xb8, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 01 };
55 NlriUtil.parseNextHop(ipv6, builder);
56 } catch (final BGPParsingException e) {
57 fail("This exception should not happen");
59 hop = builder.getCNextHop();
60 assertEquals("2001:db8::1", ((Ipv6NextHopCase) hop).getIpv6NextHop().getGlobal().getValue());
61 assertNull(((Ipv6NextHopCase) hop).getIpv6NextHop().getLinkLocal());
63 final byte[] ipv6l = new byte[] { (byte) 0x20, (byte) 0x01, (byte) 0x0d, (byte) 0xb8, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00,
64 01, (byte) 0xfe, (byte) 0x80, 00, 00, 00, 00, 00, 00, (byte) 0xc0, 01, 0x0b, (byte) 0xff, (byte) 0xfe, 0x7e, 00, 00 };
66 NlriUtil.parseNextHop(ipv6l, builder);
67 } catch (final BGPParsingException e) {
68 fail("This exception should not happen");
70 hop = builder.getCNextHop();
71 assertEquals("2001:db8::1", ((Ipv6NextHopCase) hop).getIpv6NextHop().getGlobal().getValue());
72 assertEquals("fe80::c001:bff:fe7e:0", ((Ipv6NextHopCase) hop).getIpv6NextHop().getLinkLocal().getValue());
74 final byte[] wrong = new byte[] { (byte) 0x20, (byte) 0x01, (byte) 0x0d };
76 NlriUtil.parseNextHop(wrong, builder);
77 fail("Exception should happen");
78 } catch (final BGPParsingException e) {
79 assertEquals("Cannot parse NEXT_HOP attribute. Wrong bytes length: 3", e.getMessage());
84 public void testParameterUtil() {
85 final byte[] result = new byte[] { 1, 2, 4, 8 };
86 assertArrayEquals(result, ParameterUtil.formatParameter(1, new byte[] { 4, 8 }));