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
9 package org.opendaylight.protocol.bgp.evpn.impl.attributes.tunnel.identifier;
11 import static junit.framework.TestCase.assertNull;
12 import static org.junit.Assert.assertArrayEquals;
13 import static org.junit.Assert.assertEquals;
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;
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
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
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,
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,
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);
75 public void serializeOpaque() throws Exception {
76 final ByteBuf actualOpaque = Unpooled.buffer();
77 OpaqueUtil.serializeOpaque(OPAQUE, actualOpaque);
78 assertArrayEquals(OPAQUE_EXPECTED, ByteArray.readAllBytes(actualOpaque));
80 final ByteBuf actualOpaqueExt = Unpooled.buffer();
81 OpaqueUtil.serializeOpaque(OPAQUE_EXTENDED, actualOpaqueExt);
82 assertArrayEquals(OPAQUE_EXT_EXPECTED, ByteArray.readAllBytes(actualOpaqueExt));
84 final ByteBuf empty = Unpooled.buffer();
85 OpaqueUtil.serializeOpaque(new OpaqueValueBuilder().setOpaqueType((short) 5).build(), actualOpaqueExt);
86 assertArrayEquals(new byte[0], ByteArray.readAllBytes(empty));
88 final Opaque opaque = OpaqueUtil.parseOpaque(Unpooled.wrappedBuffer(OPAQUE_EXPECTED));
89 assertEquals(OPAQUE, opaque);
91 final Opaque opaqueExt = OpaqueUtil.parseOpaque(Unpooled.wrappedBuffer(OPAQUE_EXT_EXPECTED));
92 assertEquals(OPAQUE_EXTENDED, opaqueExt);
94 assertNull(OpaqueUtil.parseOpaque(Unpooled.wrappedBuffer(OPAQUE_WRONG)));
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)));
105 @Test(expected = UnsupportedOperationException.class)
106 public void testPrivateConstructor() throws Throwable {
107 final Constructor<OpaqueUtil> c = OpaqueUtil.class.getDeclaredConstructor();
108 c.setAccessible(true);
111 } catch (final InvocationTargetException e) {