81c5c9fc5987848cc12ef7a0e5ac1bb7bb3abbb6
[bgpcep.git] / bgp / extensions / 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 import org.opendaylight.yangtools.yang.common.Uint16;
27 import org.opendaylight.yangtools.yang.common.Uint8;
28
29 public final class OpaqueUtilTest {
30     private static final byte[] OPAQUE_WRONG = {
31         (byte) 0xfc, (byte) 0x00, (byte) 0x03, // Opaque Type - Length
32         (byte) 0xb5, (byte) 0xeb, (byte) 0x2d,  //Value
33     };
34
35     private static final byte[] OPAQUE_EXPECTED = {
36         (byte) 0x01, (byte) 0x00, (byte) 0x0e, // Opaque Type - Length
37         (byte) 0x07, (byte) 0x00, (byte) 0x0B, //Value
38         (byte) 0x00, (byte) 0x00, (byte) 0x01,
39         (byte) 0x00, (byte) 0x00, (byte) 0x00,
40         (byte) 0x01, (byte) 0x00, (byte) 0x00,
41         (byte) 0x00, (byte) 0x00
42     };
43     private static final byte[] OPAQUE_EXT_EXPECTED = {
44         (byte) 0xff, (byte) 0x00, (byte) 0x04, (byte) 0x00, (byte) 0x10, // Opaque Type -Ext Type - Length
45         (byte) 0x07, (byte) 0x00, (byte) 0x0B, //Value
46         (byte) 0x00, (byte) 0x00, (byte) 0x01,
47         (byte) 0x00, (byte) 0x00, (byte) 0x00,
48         (byte) 0x01, (byte) 0x00, (byte) 0x00,
49         (byte) 0x00, (byte) 0x00, (byte) 0x01,
50         (byte) 0x02
51     };
52     private static final byte[] OPAQUE_VALUES_EXPECTED = {
53         (byte) 0x01, (byte) 0x00, (byte) 0x0e, // Opaque Type - Length
54         (byte) 0x07, (byte) 0x00, (byte) 0x0B, //Value
55         (byte) 0x00, (byte) 0x00, (byte) 0x01,
56         (byte) 0x00, (byte) 0x00, (byte) 0x00,
57         (byte) 0x01, (byte) 0x00, (byte) 0x00,
58         (byte) 0x00, (byte) 0x00,
59         (byte) 0xff, (byte) 0x00, (byte) 0x04, (byte) 0x00, (byte) 0x10, // Opaque Type -Ext Type - Length
60         (byte) 0x07, (byte) 0x00, (byte) 0x0B, //Value
61         (byte) 0x00, (byte) 0x00, (byte) 0x01,
62         (byte) 0x00, (byte) 0x00, (byte) 0x00,
63         (byte) 0x01, (byte) 0x00, (byte) 0x00,
64         (byte) 0x00, (byte) 0x00, (byte) 0x01,
65         (byte) 0x02
66     };
67
68     private static final HexString OPAQUE_TEST = new HexString("07:00:0b:00:00:01:00:00:00:01:00:00:00:00");
69     private static final HexString OPAQUE_TEST2
70             = new HexString("07:00:0b:00:00:01:00:00:00:01:00:00:00:00:01:02");
71     private static final Opaque OPAQUE = new OpaqueValueBuilder().setOpaque(OPAQUE_TEST)
72             .setOpaqueType(Uint8.ONE).build();
73     private static final Opaque OPAQUE_EXTENDED = new OpaqueValueBuilder().setOpaque(OPAQUE_TEST2)
74             .setOpaqueType(Uint8.valueOf(2)).setOpaqueType(Uint8.MAX_VALUE).setOpaqueExtendedType(Uint16.valueOf(4))
75             .build();
76     private static final List<OpaqueValue> OPAQUE_VALUE_LIST = Arrays.asList((OpaqueValue) OPAQUE,
77             (OpaqueValue) OPAQUE_EXTENDED);
78
79     @Test
80     public void serializeOpaque() {
81         final ByteBuf actualOpaque = Unpooled.buffer();
82         OpaqueUtil.serializeOpaque(OPAQUE, actualOpaque);
83         assertArrayEquals(OPAQUE_EXPECTED, ByteArray.readAllBytes(actualOpaque));
84
85         final ByteBuf actualOpaqueExt = Unpooled.buffer();
86         OpaqueUtil.serializeOpaque(OPAQUE_EXTENDED, actualOpaqueExt);
87         assertArrayEquals(OPAQUE_EXT_EXPECTED, ByteArray.readAllBytes(actualOpaqueExt));
88
89         final ByteBuf empty = Unpooled.buffer();
90         OpaqueUtil.serializeOpaque(new OpaqueValueBuilder().setOpaqueType(Uint8.valueOf(5)).build(), actualOpaqueExt);
91         assertArrayEquals(new byte[0], ByteArray.readAllBytes(empty));
92
93         final Opaque opaque = OpaqueUtil.parseOpaque(Unpooled.wrappedBuffer(OPAQUE_EXPECTED));
94         assertEquals(OPAQUE, opaque);
95
96         final Opaque opaqueExt = OpaqueUtil.parseOpaque(Unpooled.wrappedBuffer(OPAQUE_EXT_EXPECTED));
97         assertEquals(OPAQUE_EXTENDED, opaqueExt);
98
99         assertNull(OpaqueUtil.parseOpaque(Unpooled.wrappedBuffer(OPAQUE_WRONG)));
100     }
101
102     @Test
103     public void parseOpaqueList() {
104         final ByteBuf opaqueValues = Unpooled.buffer();
105         OpaqueUtil.serializeOpaqueList(OPAQUE_VALUE_LIST, opaqueValues);
106         assertArrayEquals(OPAQUE_VALUES_EXPECTED, ByteArray.readAllBytes(opaqueValues));
107         assertEquals(OPAQUE_VALUE_LIST, OpaqueUtil.parseOpaqueList(Unpooled.wrappedBuffer(OPAQUE_VALUES_EXPECTED)));
108     }
109 }