82ff8903c6b0c594c2488675c10d78560206a32b
[bgpcep.git] / bgp / concepts / src / test / java / org / opendaylight / bgp / concepts / RouteDistinguisherUtilTest.java
1 /*
2  * Copyright (c) 2016 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.bgp.concepts;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.lang.reflect.Constructor;
16 import java.lang.reflect.InvocationTargetException;
17 import org.junit.Test;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.RouteDistinguisher;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.RouteDistinguisherBuilder;
20
21 public class RouteDistinguisherUtilTest {
22
23     private static final String IP_ADDRESS = "1.2.3.4";
24     private static final String IP_PORT = "10";
25     private static final String ADMIN = "55";
26     private static final String ASSIGNED_NUMBER = "65535";
27     private static final String ASSIGNED_NUMBER_BIG = "4294967295";
28     private static final byte[] AS_2B_BYTES = { 0, 0, 0, 55, 0, 0, (byte)0xff, (byte)0xff};
29     private static final byte[] AS_2B_BYTES_BIG = { 0, 0, 0, 55, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff};
30     private static final byte[] IP_BYTES = { 0, 1, 1, 2, 3, 4, 0, 10 };
31     private static final byte[] AS_4B_BYTES = { 0, 2, 0, 0, 0, 55, (byte)0xff, (byte)0xff};
32     private static final byte[] INVALID_RD_TYPE_BYTES = { 0, 3, 0, 0, 0, 55, (byte)0xff, (byte)0xff};
33     private static final char SEPARATOR = ':';
34
35     @Test
36     public void testAs2BRouteDistinguisher() {
37         final RouteDistinguisher expected = createRouteDistinguisher(0, ADMIN, ASSIGNED_NUMBER);
38         final RouteDistinguisher parsed = RouteDistinguisherUtil.parseRouteDistinguisher(Unpooled.copiedBuffer(AS_2B_BYTES));
39         assertEquals(expected.getRdTwoOctetAs(), parsed.getRdTwoOctetAs());
40         final ByteBuf byteAggregator = Unpooled.buffer(AS_2B_BYTES.length);
41         RouteDistinguisherUtil.serializeRouteDistinquisher(expected, byteAggregator);
42         assertArrayEquals(AS_2B_BYTES, byteAggregator.array());
43         assertEquals("0" + SEPARATOR + ADMIN + SEPARATOR + ASSIGNED_NUMBER, parsed.getRdTwoOctetAs().getValue());
44     }
45
46     @Test
47     public void testAs2BLongRouteDistinguisher() {
48         final RouteDistinguisher expected = createRouteDistinguisher(0, ADMIN, ASSIGNED_NUMBER_BIG);
49         final RouteDistinguisher parsed = RouteDistinguisherUtil.parseRouteDistinguisher(Unpooled.copiedBuffer(AS_2B_BYTES_BIG));
50         assertEquals(expected.getRdTwoOctetAs(), parsed.getRdTwoOctetAs());
51         final ByteBuf byteAggregator = Unpooled.buffer(AS_2B_BYTES_BIG.length);
52         RouteDistinguisherUtil.serializeRouteDistinquisher(expected, byteAggregator);
53         assertArrayEquals(AS_2B_BYTES_BIG, byteAggregator.array());
54         assertEquals("0" + SEPARATOR + ADMIN + SEPARATOR + ASSIGNED_NUMBER_BIG, parsed.getRdTwoOctetAs().getValue());
55     }
56
57     @Test
58     public void testIpv4RouteDistinguisher() {
59         final RouteDistinguisher expected = createRouteDistinguisher(1, IP_ADDRESS, IP_PORT);
60         final RouteDistinguisher parsed = RouteDistinguisherUtil.parseRouteDistinguisher(Unpooled.copiedBuffer(IP_BYTES));
61         assertEquals(expected.getRdIpv4(), parsed.getRdIpv4());
62         final ByteBuf byteAggregator = Unpooled.buffer(IP_BYTES.length);
63         RouteDistinguisherUtil.serializeRouteDistinquisher(expected, byteAggregator);
64         assertArrayEquals(IP_BYTES, byteAggregator.array());
65         assertEquals(IP_ADDRESS + SEPARATOR + IP_PORT, parsed.getRdIpv4().getValue());
66     }
67
68     @Test
69     public void testAs4BRouteDistinguisher() {
70         final RouteDistinguisher expected = createRouteDistinguisher(2, ADMIN, ASSIGNED_NUMBER);
71         final RouteDistinguisher parsed = RouteDistinguisherUtil.parseRouteDistinguisher(Unpooled.copiedBuffer(AS_4B_BYTES));
72         assertEquals(expected.getRdAs(), parsed.getRdAs());
73         final ByteBuf byteAggregator = Unpooled.buffer(AS_4B_BYTES.length);
74         RouteDistinguisherUtil.serializeRouteDistinquisher(expected, byteAggregator);
75         assertArrayEquals(AS_4B_BYTES, byteAggregator.array());
76         assertEquals(ADMIN + SEPARATOR + ASSIGNED_NUMBER, parsed.getRdAs().getValue());
77     }
78
79     @Test
80     public void testParseRouteDistinguisher() {
81         final RouteDistinguisher expected = RouteDistinguisherUtil.parseRouteDistinguisher(ADMIN + SEPARATOR + ASSIGNED_NUMBER);
82         final RouteDistinguisher parsed = RouteDistinguisherUtil.parseRouteDistinguisher(Unpooled.copiedBuffer(AS_4B_BYTES));
83         assertEquals(expected.getRdAs(), parsed.getRdAs());
84
85         final RouteDistinguisher expectedRD = RouteDistinguisherUtil.parseRouteDistinguisher(expected);
86         assertEquals(expectedRD.getRdAs(), parsed.getRdAs());
87
88         final RouteDistinguisher expectedObj = RouteDistinguisherUtil.parseRouteDistinguisher((Object) (ADMIN + SEPARATOR + ASSIGNED_NUMBER));
89         assertEquals(expectedObj.getRdAs(), parsed.getRdAs());
90     }
91
92     @Test(expected=UnsupportedOperationException.class)
93     public void testPrivateConstructor() throws Throwable {
94         final Constructor<RouteDistinguisherUtil> c = RouteDistinguisherUtil.class.getDeclaredConstructor();
95         c.setAccessible(true);
96         try {
97             c.newInstance();
98         } catch (final InvocationTargetException e) {
99             throw e.getCause();
100         }
101     }
102
103     @Test(expected=IllegalArgumentException.class)
104     public void testInvalidRDType() throws Throwable {
105         RouteDistinguisherUtil.parseRouteDistinguisher(Unpooled.copiedBuffer(INVALID_RD_TYPE_BYTES));
106     }
107
108     /**
109      * Create 4-octet AS RD or IPv4 RD, 2-octet AS RD cannot be created with this function
110      * @param administratorSubfield
111      * @param assignedNumberSubfield
112      * @return
113      */
114     private static RouteDistinguisher createRouteDistinguisher(final int type, final String administratorSubfield,
115             final String assignedNumberSubfield) {
116         final StringBuilder routeDistiguisher = new StringBuilder();
117         if (type == 0) {
118             routeDistiguisher.append(type).append(SEPARATOR);
119         }
120         routeDistiguisher.append(administratorSubfield);
121         routeDistiguisher.append(SEPARATOR);
122         routeDistiguisher.append(assignedNumberSubfield);
123         return RouteDistinguisherBuilder.getDefaultInstance(routeDistiguisher.toString());
124     }
125 }