0bcdbdd26244d0e23898f50fd8986bddb5f99a44
[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 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;
19
20 public class RouteDistinguisherUtilTest {
21
22     private static final String IP_ADDRESS = "1.2.3.4";
23     private static final String ADMIN = "55";
24     private static final byte[] IP_BYTES = { 0, 1, 1, 2, 3, 4, 0, 10 };
25     private static final byte[] AS_4B_BYTES = { 0, 2, 0,0, 0, 55, (byte)0xff, (byte)0xff};
26     private static final char SEPARATOR = ':';
27
28     @Test
29     public void testIpv4RouteDistinguisher() {
30         final RouteDistinguisher expected = createRouteDistinguisher(1, 10L, IP_ADDRESS);
31         final RouteDistinguisher parsed = RouteDistinguisherUtil.parseRouteDistinguisher(Unpooled.copiedBuffer(IP_BYTES));
32         assertEquals(expected.getRdIpv4(), parsed.getRdIpv4());
33         final ByteBuf byteAggregator = Unpooled.buffer(IP_BYTES.length);
34         RouteDistinguisherUtil.serializeRouteDistinquisher(expected, byteAggregator);
35         assertArrayEquals(IP_BYTES, byteAggregator.array());
36     }
37
38     @Test
39     public void testAs4BRouteDistinguisher() {
40         final RouteDistinguisher expected = createRouteDistinguisher(2, 65535L, ADMIN);
41         final RouteDistinguisher parsed = RouteDistinguisherUtil.parseRouteDistinguisher(Unpooled.copiedBuffer(AS_4B_BYTES));
42         assertEquals(expected.getRdAs(), parsed.getRdAs());
43         final ByteBuf byteAggregator = Unpooled.buffer(AS_4B_BYTES.length);
44         RouteDistinguisherUtil.serializeRouteDistinquisher(expected, byteAggregator);
45         assertArrayEquals(AS_4B_BYTES, byteAggregator.array());
46     }
47
48     @Test(expected=UnsupportedOperationException.class)
49     public void testPrivateConstructor() throws Throwable {
50         final Constructor<RouteDistinguisherUtil> c = RouteDistinguisherUtil.class.getDeclaredConstructor();
51         c.setAccessible(true);
52         try {
53             c.newInstance();
54         } catch (final InvocationTargetException e) {
55             throw e.getCause();
56         }
57     }
58
59     private RouteDistinguisher createRouteDistinguisher(final int type, final long assignedNumberSubfield, final String administratorSubfield) {
60         final StringBuffer routeDistiguisher = new StringBuffer();
61         routeDistiguisher.append(administratorSubfield);
62         routeDistiguisher.append(SEPARATOR);
63         routeDistiguisher.append(assignedNumberSubfield);
64         return RouteDistinguisherBuilder.getDefaultInstance(routeDistiguisher.toString());
65     }
66
67 }