247cc5dcdbaa63ed701c6024ac2ab87ffbcaf417
[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.util.Arrays;
18 import java.util.List;
19 import org.junit.Test;
20 import org.opendaylight.protocol.util.ByteArray;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.HexString;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev160812.Opaque;
23 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;
24 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;
25
26 public class OpaqueUtilTest {
27     private static final byte[] OPAQUE_WRONG = {
28         (byte) 0xfc, (byte) 0x00, (byte) 0x03, // Opaque Type - Length
29         (byte) 0xb5, (byte) 0xeb, (byte) 0x2d,  //Value
30     };
31
32     private static final byte[] OPAQUE_EXPECTED = {
33         (byte) 0x01, (byte) 0x00, (byte) 0x0e, // Opaque Type - Length
34         (byte) 0x07, (byte) 0x00, (byte) 0x0B, //Value
35         (byte) 0x00, (byte) 0x00, (byte) 0x01,
36         (byte) 0x00, (byte) 0x00, (byte) 0x00,
37         (byte) 0x01, (byte) 0x00, (byte) 0x00,
38         (byte) 0x00, (byte) 0x00
39     };
40     private static final byte[] OPAQUE_EXT_EXPECTED = {
41         (byte) 0xff, (byte) 0x00, (byte) 0x04, (byte) 0x00, (byte) 0x10, // Opaque Type -Ext Type - Length
42         (byte) 0x07, (byte) 0x00, (byte) 0x0B, //Value
43         (byte) 0x00, (byte) 0x00, (byte) 0x01,
44         (byte) 0x00, (byte) 0x00, (byte) 0x00,
45         (byte) 0x01, (byte) 0x00, (byte) 0x00,
46         (byte) 0x00, (byte) 0x00, (byte) 0x01,
47         (byte) 0x02
48     };
49     private static final byte[] OPAQUE_VALUES_EXPECTED = {
50         (byte) 0x01, (byte) 0x00, (byte) 0x0e, // Opaque Type - Length
51         (byte) 0x07, (byte) 0x00, (byte) 0x0B, //Value
52         (byte) 0x00, (byte) 0x00, (byte) 0x01,
53         (byte) 0x00, (byte) 0x00, (byte) 0x00,
54         (byte) 0x01, (byte) 0x00, (byte) 0x00,
55         (byte) 0x00, (byte) 0x00,
56         (byte) 0xff, (byte) 0x00, (byte) 0x04, (byte) 0x00, (byte) 0x10, // Opaque Type -Ext Type - Length
57         (byte) 0x07, (byte) 0x00, (byte) 0x0B, //Value
58         (byte) 0x00, (byte) 0x00, (byte) 0x01,
59         (byte) 0x00, (byte) 0x00, (byte) 0x00,
60         (byte) 0x01, (byte) 0x00, (byte) 0x00,
61         (byte) 0x00, (byte) 0x00, (byte) 0x01,
62         (byte) 0x02
63     };
64
65     static final HexString OPAQUE_TEST = new HexString("07:00:0b:00:00:01:00:00:00:01:00:00:00:00");
66     static final HexString OPAQUE_TEST2 = new HexString("07:00:0b:00:00:01:00:00:00:01:00:00:00:00:01:02");
67     private static final Opaque OPAQUE = new OpaqueValueBuilder().setOpaque(OPAQUE_TEST)
68             .setOpaqueType(OpaqueUtil.GENERIC_LSP_IDENTIFIER).build();
69     private static final Opaque OPAQUE_EXTENDED = new OpaqueValueBuilder().setOpaque(OPAQUE_TEST2)
70             .setOpaqueType((short) 2).setOpaqueType(OpaqueUtil.EXTENDED_TYPE).setOpaqueExtendedType(4).build();
71     private static final List<OpaqueValue> OPAQUE_VALUE_LIST = Arrays.asList((OpaqueValue) OPAQUE,
72             (OpaqueValue) OPAQUE_EXTENDED);
73
74     @Test
75     public void serializeOpaque() {
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() {
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 }