be712ff80720302ac94314e6ee89f5229cf6b4ce
[bgpcep.git] / bgp / evpn / src / test / java / org / opendaylight / protocol / bgp / evpn / impl / attributes / tunnel / identifier / OpaqueUtilTest.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
9 package org.opendaylight.protocol.bgp.evpn.impl.attributes.tunnel.identifier;
10
11 import static junit.framework.TestCase.assertNull;
12 import static org.junit.Assert.assertArrayEquals;
13 import static org.junit.Assert.assertEquals;
14
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import java.lang.reflect.Constructor;
18 import java.lang.reflect.InvocationTargetException;
19 import java.util.Arrays;
20 import java.util.List;
21 import org.junit.Test;
22 import org.opendaylight.protocol.util.ByteArray;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.HexString;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev160812.Opaque;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev160812.pmsi.tunnel.pmsi.tunnel.tunnel.identifier.mldp.p2mp.lsp.mldp.p2mp.lsp.OpaqueValue;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev160812.pmsi.tunnel.pmsi.tunnel.tunnel.identifier.mldp.p2mp.lsp.mldp.p2mp.lsp.OpaqueValueBuilder;
27
28 public class OpaqueUtilTest {
29     private static final byte[] OPAQUE_WRONG= {
30         (byte) 0xfc, (byte) 0x00, (byte) 0x03, // Opaque Type - Length
31         (byte) 0xb5, (byte) 0xeb, (byte) 0x2d,  //Value
32     };
33
34     private static final byte[] OPAQUE_EXPECTED = {
35         (byte) 0x01, (byte) 0x00, (byte) 0x0e, // Opaque Type - Length
36         (byte) 0x07, (byte) 0x00, (byte) 0x0B, //Value
37         (byte) 0x00, (byte) 0x00, (byte) 0x01,
38         (byte) 0x00, (byte) 0x00, (byte) 0x00,
39         (byte) 0x01, (byte) 0x00, (byte) 0x00,
40         (byte) 0x00, (byte) 0x00
41     };
42     private static final byte[] OPAQUE_EXT_EXPECTED = {
43         (byte) 0xff, (byte) 0x00, (byte) 0x04, (byte) 0x00, (byte) 0x10, // Opaque Type -Ext Type - Length
44         (byte) 0x07, (byte) 0x00, (byte) 0x0B, //Value
45         (byte) 0x00, (byte) 0x00, (byte) 0x01,
46         (byte) 0x00, (byte) 0x00, (byte) 0x00,
47         (byte) 0x01, (byte) 0x00, (byte) 0x00,
48         (byte) 0x00, (byte) 0x00, (byte) 0x01,
49         (byte) 0x02
50     };
51     private static final byte[] OPAQUE_VALUES_EXPECTED = {
52         (byte) 0x01, (byte) 0x00, (byte) 0x0e, // Opaque Type - Length
53         (byte) 0x07, (byte) 0x00, (byte) 0x0B, //Value
54         (byte) 0x00, (byte) 0x00, (byte) 0x01,
55         (byte) 0x00, (byte) 0x00, (byte) 0x00,
56         (byte) 0x01, (byte) 0x00, (byte) 0x00,
57         (byte) 0x00, (byte) 0x00,
58         (byte) 0xff, (byte) 0x00, (byte) 0x04, (byte) 0x00, (byte) 0x10, // Opaque Type -Ext Type - Length
59         (byte) 0x07, (byte) 0x00, (byte) 0x0B, //Value
60         (byte) 0x00, (byte) 0x00, (byte) 0x01,
61         (byte) 0x00, (byte) 0x00, (byte) 0x00,
62         (byte) 0x01, (byte) 0x00, (byte) 0x00,
63         (byte) 0x00, (byte) 0x00, (byte) 0x01,
64         (byte) 0x02
65     };
66
67     static final HexString OPAQUE_TEST = new HexString("07:00:0b:00:00:01:00:00:00:01:00:00:00:00");
68     static final HexString OPAQUE_TEST2 = new HexString ("07:00:0b:00:00:01:00:00:00:01:00:00:00:00:01:02");
69     private static final Opaque OPAQUE = new OpaqueValueBuilder().setOpaque(OPAQUE_TEST).setOpaqueType(OpaqueUtil.GENERIC_LSP_IDENTIFIER).build();
70     private static final Opaque OPAQUE_EXTENDED = new OpaqueValueBuilder().setOpaque(OPAQUE_TEST2).setOpaqueType((short) 2)
71         .setOpaqueType(OpaqueUtil.EXTENDED_TYPE).setOpaqueExtendedType(4).build();
72     private static final List<OpaqueValue> OPAQUE_VALUE_LIST = Arrays.asList((OpaqueValue) OPAQUE, (OpaqueValue) OPAQUE_EXTENDED);
73
74     @Test
75     public void serializeOpaque() throws Exception {
76         final ByteBuf actualOpaque = Unpooled.buffer();
77         OpaqueUtil.serializeOpaque(OPAQUE, actualOpaque);
78         assertArrayEquals(OPAQUE_EXPECTED, ByteArray.readAllBytes(actualOpaque));
79
80         final ByteBuf actualOpaqueExt = Unpooled.buffer();
81         OpaqueUtil.serializeOpaque(OPAQUE_EXTENDED, actualOpaqueExt);
82         assertArrayEquals(OPAQUE_EXT_EXPECTED, ByteArray.readAllBytes(actualOpaqueExt));
83
84         final ByteBuf empty = Unpooled.buffer();
85         OpaqueUtil.serializeOpaque(new OpaqueValueBuilder().setOpaqueType((short) 5).build(), actualOpaqueExt);
86         assertArrayEquals(new byte[0], ByteArray.readAllBytes(empty));
87
88         final Opaque opaque = OpaqueUtil.parseOpaque(Unpooled.wrappedBuffer(OPAQUE_EXPECTED));
89         assertEquals(OPAQUE, opaque);
90
91         final Opaque opaqueExt = OpaqueUtil.parseOpaque(Unpooled.wrappedBuffer(OPAQUE_EXT_EXPECTED));
92         assertEquals(OPAQUE_EXTENDED, opaqueExt);
93
94         assertNull(OpaqueUtil.parseOpaque(Unpooled.wrappedBuffer(OPAQUE_WRONG)));
95     }
96
97     @Test
98     public void parseOpaqueList() throws Exception {
99         final ByteBuf opaqueValues = Unpooled.buffer();
100         OpaqueUtil.serializeOpaqueList(OPAQUE_VALUE_LIST, opaqueValues);
101         assertArrayEquals(OPAQUE_VALUES_EXPECTED, ByteArray.readAllBytes(opaqueValues));
102         assertEquals(OPAQUE_VALUE_LIST, OpaqueUtil.parseOpaqueList(Unpooled.wrappedBuffer(OPAQUE_VALUES_EXPECTED)));
103     }
104
105     @Test(expected = UnsupportedOperationException.class)
106     public void testPrivateConstructor() throws Throwable {
107         final Constructor<OpaqueUtil> c = OpaqueUtil.class.getDeclaredConstructor();
108         c.setAccessible(true);
109         try {
110             c.newInstance();
111         } catch (final InvocationTargetException e) {
112             throw e.getCause();
113         }
114     }
115 }