2 * Copyright (c) 2018 AT&T Intellectual Property. 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
8 package org.opendaylight.protocol.bgp.parser.impl.message.update;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.List;
18 import org.junit.Test;
19 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
20 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
21 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
22 import org.opendaylight.protocol.util.ByteArray;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.AsPathBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.as.path.Segments;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.as.path.SegmentsBuilder;
29 import org.opendaylight.yangtools.yang.common.Uint32;
31 public class AsPathAttributeParserTest {
32 private static final byte[] ATTRIBUTE_BYTES = {
33 (byte) 0x40, (byte) 0x02, (byte) 0x14,
34 (byte) 0x01, (byte) 0x02, (byte) 0x00, (byte) 0x00,
35 (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00,
36 (byte) 0x00, (byte) 0x02, (byte) 0x02, (byte) 0x02,
37 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x03,
38 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x04
40 private static final byte[] EMPTY_ATTRIBUTE_BYTES = { (byte) 0x40, (byte) 0x02, (byte) 0x00 };
43 public void testAttributeParser() throws BGPParsingException, BGPDocumentedException {
44 final List<Segments> segments = new ArrayList<>();
45 segments.add(new SegmentsBuilder()
46 .setAsSet(Arrays.asList(new AsNumber(Uint32.ONE), new AsNumber(Uint32.TWO))).build());
47 segments.add(new SegmentsBuilder()
48 .setAsSequence(Arrays.asList(new AsNumber(Uint32.valueOf(3)), new AsNumber(Uint32.valueOf(4))))
50 final Attributes attr = new AttributesBuilder().setAsPath(new AsPathBuilder()
51 .setSegments(segments)
55 final ByteBuf actual = Unpooled.buffer();
56 ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry()
57 .serializeAttribute(attr, actual);
58 assertArrayEquals(ATTRIBUTE_BYTES, ByteArray.getAllBytes(actual));
60 final Attributes attributeOut = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance()
61 .getAttributeRegistry().parseAttributes(actual, null).getAttributes();
62 assertEquals(attr.getAsPath(), attributeOut.getAsPath());
66 public void testParseEmptyAttribute() {
67 final ByteBuf actual = Unpooled.buffer();
68 ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry()
69 .serializeAttribute(new AttributesBuilder()
70 .setAsPath(new AsPathBuilder()
73 assertEquals(Unpooled.buffer().writeBytes(EMPTY_ATTRIBUTE_BYTES), actual);