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;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.lang.reflect.Constructor;
15 import java.lang.reflect.InvocationTargetException;
16 import org.junit.Test;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.RouteDistinguisher;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.RouteDistinguisherBuilder;
20 public class RouteDistinguisherUtilTest {
22 private static final String IP_ADDRESS = "1.2.3.4";
23 private static final String IP_PORT = "10";
24 private static final String ADMIN = "55";
25 private static final String ASSIGNED_NUMBER = "65535";
26 private static final byte[] AS_2B_BYTES = { 0, 0, 0, 55, 0, 0, (byte)0xff, (byte)0xff};
27 private static final byte[] IP_BYTES = { 0, 1, 1, 2, 3, 4, 0, 10 };
28 private static final byte[] AS_4B_BYTES = { 0, 2, 0, 0, 0, 55, (byte)0xff, (byte)0xff};
29 private static final byte[] INVALID_RD_TYPE_BYTES = { 0, 3, 0, 0, 0, 55, (byte)0xff, (byte)0xff};
30 private static final char SEPARATOR = ':';
33 public void testAs2BRouteDistinguisher() {
34 final RouteDistinguisher expected = createRouteDistinguisher(0, ADMIN, ASSIGNED_NUMBER);
35 final RouteDistinguisher parsed = RouteDistinguisherUtil.parseRouteDistinguisher(Unpooled.copiedBuffer(AS_2B_BYTES));
36 assertEquals(expected.getRdTwoOctetAs(), parsed.getRdTwoOctetAs());
37 final ByteBuf byteAggregator = Unpooled.buffer(AS_2B_BYTES.length);
38 RouteDistinguisherUtil.serializeRouteDistinquisher(expected, byteAggregator);
39 assertArrayEquals(AS_2B_BYTES, byteAggregator.array());
40 assertEquals("0" + SEPARATOR + ADMIN + SEPARATOR + ASSIGNED_NUMBER, parsed.getRdTwoOctetAs().getValue());
44 public void testIpv4RouteDistinguisher() {
45 final RouteDistinguisher expected = createRouteDistinguisher(1, IP_ADDRESS, IP_PORT);
46 final RouteDistinguisher parsed = RouteDistinguisherUtil.parseRouteDistinguisher(Unpooled.copiedBuffer(IP_BYTES));
47 assertEquals(expected.getRdIpv4(), parsed.getRdIpv4());
48 final ByteBuf byteAggregator = Unpooled.buffer(IP_BYTES.length);
49 RouteDistinguisherUtil.serializeRouteDistinquisher(expected, byteAggregator);
50 assertArrayEquals(IP_BYTES, byteAggregator.array());
51 assertEquals(IP_ADDRESS + SEPARATOR + IP_PORT, parsed.getRdIpv4().getValue());
55 public void testAs4BRouteDistinguisher() {
56 final RouteDistinguisher expected = createRouteDistinguisher(2, ADMIN, ASSIGNED_NUMBER);
57 final RouteDistinguisher parsed = RouteDistinguisherUtil.parseRouteDistinguisher(Unpooled.copiedBuffer(AS_4B_BYTES));
58 assertEquals(expected.getRdAs(), parsed.getRdAs());
59 final ByteBuf byteAggregator = Unpooled.buffer(AS_4B_BYTES.length);
60 RouteDistinguisherUtil.serializeRouteDistinquisher(expected, byteAggregator);
61 assertArrayEquals(AS_4B_BYTES, byteAggregator.array());
62 assertEquals(ADMIN + SEPARATOR + ASSIGNED_NUMBER, parsed.getRdAs().getValue());
66 public void testParseRouteDistinguisher() {
67 final RouteDistinguisher expected = RouteDistinguisherUtil.parseRouteDistinguisher(ADMIN + SEPARATOR + ASSIGNED_NUMBER);
68 final RouteDistinguisher parsed = RouteDistinguisherUtil.parseRouteDistinguisher(Unpooled.copiedBuffer(AS_4B_BYTES));
69 assertEquals(expected.getRdAs(), parsed.getRdAs());
71 final RouteDistinguisher expectedRD = RouteDistinguisherUtil.parseRouteDistinguisher(expected);
72 assertEquals(expectedRD.getRdAs(), parsed.getRdAs());
74 final RouteDistinguisher expectedObj = RouteDistinguisherUtil.parseRouteDistinguisher((Object) (ADMIN + SEPARATOR + ASSIGNED_NUMBER));
75 assertEquals(expectedObj.getRdAs(), parsed.getRdAs());
78 @Test(expected=UnsupportedOperationException.class)
79 public void testPrivateConstructor() throws Throwable {
80 final Constructor<RouteDistinguisherUtil> c = RouteDistinguisherUtil.class.getDeclaredConstructor();
81 c.setAccessible(true);
84 } catch (final InvocationTargetException e) {
89 @Test(expected=IllegalArgumentException.class)
90 public void testInvalidRDType() throws Throwable {
91 RouteDistinguisherUtil.parseRouteDistinguisher(Unpooled.copiedBuffer(INVALID_RD_TYPE_BYTES));
95 * Create 4-octet AS RD or IPv4 RD, 2-octet AS RD cannot be created with this function
96 * @param administratorSubfield
97 * @param assignedNumberSubfield
100 private RouteDistinguisher createRouteDistinguisher(final int type, final String administratorSubfield, final String assignedNumberSubfield) {
101 final StringBuffer routeDistiguisher = new StringBuffer();
103 routeDistiguisher.append(type).append(SEPARATOR);
105 routeDistiguisher.append(administratorSubfield);
106 routeDistiguisher.append(SEPARATOR);
107 routeDistiguisher.append(assignedNumberSubfield);
108 return RouteDistinguisherBuilder.getDefaultInstance(routeDistiguisher.toString());