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