2 * Copyright (c) 2016 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.bgp.concepts;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.junit.Test;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.RouteDistinguisher;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.RouteDistinguisherBuilder;
19 public class RouteDistinguisherUtilTest {
21 private static final String IP_ADDRESS = "1.2.3.4";
22 private static final String IP_PORT = "10";
23 private static final String ADMIN = "55";
24 private static final String ASSIGNED_NUMBER = "65535";
25 private static final String ASSIGNED_NUMBER_BIG = "4294967295";
26 private static final byte[] AS_2B_BYTES = {0, 0, 0, 55, 0, 0, (byte) 0xff, (byte) 0xff};
27 private static final byte[] AS_2B_BYTES_BIG = {0, 0, 0, 55, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff};
28 private static final byte[] IP_BYTES = {0, 1, 1, 2, 3, 4, 0, 10};
29 private static final byte[] AS_4B_BYTES = {0, 2, 0, 0, 0, 55, (byte) 0xff, (byte) 0xff};
30 private static final byte[] INVALID_RD_TYPE_BYTES = {0, 3, 0, 0, 0, 55, (byte) 0xff, (byte) 0xff};
31 private static final char SEPARATOR = ':';
34 * Create 4-octet AS RD or IPv4 RD, 2-octet AS RD cannot be created with this function.
36 private static RouteDistinguisher createRouteDistinguisher(final int type, final String administratorSubfield,
37 final String assignedNumberSubfield) {
38 final StringBuilder routeDistiguisher = new StringBuilder();
40 routeDistiguisher.append(type).append(SEPARATOR);
42 routeDistiguisher.append(administratorSubfield);
43 routeDistiguisher.append(SEPARATOR);
44 routeDistiguisher.append(assignedNumberSubfield);
45 return RouteDistinguisherBuilder.getDefaultInstance(routeDistiguisher.toString());
49 public void testAs2BRouteDistinguisher() {
50 final RouteDistinguisher expected = createRouteDistinguisher(0, ADMIN, ASSIGNED_NUMBER);
51 final RouteDistinguisher parsed = RouteDistinguisherUtil
52 .parseRouteDistinguisher(Unpooled.copiedBuffer(AS_2B_BYTES));
53 assertEquals(expected.getRdTwoOctetAs(), parsed.getRdTwoOctetAs());
54 final ByteBuf byteAggregator = Unpooled.buffer(AS_2B_BYTES.length);
55 RouteDistinguisherUtil.serializeRouteDistinquisher(expected, byteAggregator);
56 assertArrayEquals(AS_2B_BYTES, byteAggregator.array());
57 assertEquals("0" + SEPARATOR + ADMIN + SEPARATOR + ASSIGNED_NUMBER,
58 parsed.getRdTwoOctetAs().getValue());
62 public void testAs2BLongRouteDistinguisher() {
63 final RouteDistinguisher expected = createRouteDistinguisher(0, ADMIN, ASSIGNED_NUMBER_BIG);
64 final RouteDistinguisher parsed = RouteDistinguisherUtil
65 .parseRouteDistinguisher(Unpooled.copiedBuffer(AS_2B_BYTES_BIG));
66 assertEquals(expected.getRdTwoOctetAs(), parsed.getRdTwoOctetAs());
67 final ByteBuf byteAggregator = Unpooled.buffer(AS_2B_BYTES_BIG.length);
68 RouteDistinguisherUtil.serializeRouteDistinquisher(expected, byteAggregator);
69 assertArrayEquals(AS_2B_BYTES_BIG, byteAggregator.array());
70 assertEquals("0" + SEPARATOR + ADMIN + SEPARATOR + ASSIGNED_NUMBER_BIG,
71 parsed.getRdTwoOctetAs().getValue());
75 public void testIpv4RouteDistinguisher() {
76 final RouteDistinguisher expected = createRouteDistinguisher(1, IP_ADDRESS, IP_PORT);
77 final RouteDistinguisher parsed = RouteDistinguisherUtil
78 .parseRouteDistinguisher(Unpooled.copiedBuffer(IP_BYTES));
79 assertEquals(expected.getRdIpv4(), parsed.getRdIpv4());
80 final ByteBuf byteAggregator = Unpooled.buffer(IP_BYTES.length);
81 RouteDistinguisherUtil.serializeRouteDistinquisher(expected, byteAggregator);
82 assertArrayEquals(IP_BYTES, byteAggregator.array());
83 assertEquals(IP_ADDRESS + SEPARATOR + IP_PORT, parsed.getRdIpv4().getValue());
87 public void testAs4BRouteDistinguisher() {
88 final RouteDistinguisher expected = createRouteDistinguisher(2, ADMIN, ASSIGNED_NUMBER);
89 final RouteDistinguisher parsed = RouteDistinguisherUtil
90 .parseRouteDistinguisher(Unpooled.copiedBuffer(AS_4B_BYTES));
91 assertEquals(expected.getRdAs(), parsed.getRdAs());
92 final ByteBuf byteAggregator = Unpooled.buffer(AS_4B_BYTES.length);
93 RouteDistinguisherUtil.serializeRouteDistinquisher(expected, byteAggregator);
94 assertArrayEquals(AS_4B_BYTES, byteAggregator.array());
95 assertEquals(ADMIN + SEPARATOR + ASSIGNED_NUMBER, parsed.getRdAs().getValue());
99 public void testParseRouteDistinguisher() {
100 final RouteDistinguisher expected = RouteDistinguisherUtil
101 .parseRouteDistinguisher(ADMIN + SEPARATOR + ASSIGNED_NUMBER);
102 final RouteDistinguisher parsed = RouteDistinguisherUtil
103 .parseRouteDistinguisher(Unpooled.copiedBuffer(AS_4B_BYTES));
104 assertEquals(expected.getRdAs(), parsed.getRdAs());
106 final RouteDistinguisher expectedRD = RouteDistinguisherUtil.parseRouteDistinguisher(expected);
107 assertEquals(expectedRD.getRdAs(), parsed.getRdAs());
109 final RouteDistinguisher expectedObj = RouteDistinguisherUtil
110 .parseRouteDistinguisher((Object) (ADMIN + SEPARATOR + ASSIGNED_NUMBER));
111 assertEquals(expectedObj.getRdAs(), parsed.getRdAs());
114 @Test(expected = IllegalArgumentException.class)
115 public void testInvalidRDType() {
116 RouteDistinguisherUtil.parseRouteDistinguisher(Unpooled.copiedBuffer(INVALID_RD_TYPE_BYTES));