Migrate DTO builders
[bgpcep.git] / bgp / parser-impl / src / test / java / org / opendaylight / protocol / bgp / parser / impl / message / update / AsPathAttributeParserTest.java
1 /*
2  * Copyright (c) 2018 AT&T Intellectual Property. 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 package org.opendaylight.protocol.bgp.parser.impl.message.update;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12
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;
29 import org.opendaylight.yangtools.yang.common.Uint32;
30
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
39     };
40     private static final byte[] EMPTY_ATTRIBUTE_BYTES = { (byte) 0x40, (byte) 0x02, (byte) 0x00 };
41
42     @Test
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.valueOf(2))))
47                 .build());
48         segments.add(new SegmentsBuilder()
49                 .setAsSequence(Arrays.asList(new AsNumber(Uint32.valueOf(3)), new AsNumber(Uint32.valueOf(4))))
50                 .build());
51         final Attributes attr = new AttributesBuilder().setAsPath(new AsPathBuilder()
52                     .setSegments(segments)
53                     .build())
54                 .build();
55
56         final ByteBuf actual = Unpooled.buffer();
57         ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry()
58                 .serializeAttribute(attr, actual);
59         assertArrayEquals(ATTRIBUTE_BYTES, ByteArray.getAllBytes(actual));
60
61         final Attributes attributeOut = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance()
62                 .getAttributeRegistry().parseAttributes(actual, null).getAttributes();
63         assertEquals(attr.getAsPath(), attributeOut.getAsPath());
64     }
65
66     @Test
67     public void testParseEmptyAttribute() {
68         final ByteBuf actual = Unpooled.buffer();
69         ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry()
70                 .serializeAttribute(new AttributesBuilder()
71                         .setAsPath(new AsPathBuilder()
72                                 .build())
73                         .build(), actual);
74         assertEquals(Unpooled.buffer().writeBytes(EMPTY_ATTRIBUTE_BYTES), actual);
75     }
76 }