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.rev180329.path.attributes.Attributes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.AsPathBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.as.path.Segments;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.as.path.SegmentsBuilder;
30 public class AsPathAttributeParserTest {
31 private static final byte[] ATTRIBUTE_BYTES = {
32 (byte) 0x40, (byte) 0x02, (byte) 0x14,
33 (byte) 0x01, (byte) 0x02, (byte) 0x00, (byte) 0x00,
34 (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00,
35 (byte) 0x00, (byte) 0x02, (byte) 0x02, (byte) 0x02,
36 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x03,
37 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x04
39 private static final byte[] EMPTY_ATTRIBUTE_BYTES = { (byte) 0x40, (byte) 0x02, (byte) 0x00 };
42 public void testAttributeParser() throws BGPParsingException, BGPDocumentedException {
43 final List<Segments> segments = new ArrayList<>();
44 segments.add(new SegmentsBuilder()
45 .setAsSet(Arrays.asList(new AsNumber(1L), new AsNumber(2L)))
47 segments.add(new SegmentsBuilder()
48 .setAsSequence(Arrays.asList(new AsNumber(3L), new AsNumber(4L)))
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);