2 * Copyright (c) 2015 Cisco Systems, Inc. and others. 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;
12 import static org.junit.Assert.assertTrue;
14 import com.google.common.primitives.Bytes;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import java.util.ArrayList;
18 import java.util.List;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
22 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
23 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunityRegistry;
24 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
25 import org.opendaylight.protocol.util.ByteArray;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.ExtendedCommunities;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.ExtendedCommunitiesBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.ShortAsNumber;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.extended.community.extended.community.RouteOriginExtendedCommunityCase;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.extended.community.extended.community.RouteOriginExtendedCommunityCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.extended.community.extended.community.route.origin.extended.community._case.RouteOriginExtendedCommunityBuilder;
33 import org.opendaylight.yangtools.yang.common.Uint32;
35 public class ExtendedCommunitiesAttributeParserTest {
37 private static final byte[] INPUT = {
38 0x40, 0x03, 0x00, 54, 0, 0, 1, 76
41 private static final byte[] UNKOWN = {
42 0x40, 0x05, 0x00, 54, 0, 0, 1, 76
45 private ExtendedCommunitiesAttributeParser handler;
49 final ExtendedCommunityRegistry exReg = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance()
50 .getExtendedCommunityRegistry();
51 this.handler = new ExtendedCommunitiesAttributeParser(exReg);
55 public void testExtendedCommunityAttributeParser() throws BGPDocumentedException, BGPParsingException {
56 final RouteOriginExtendedCommunityCase routeOrigin = new RouteOriginExtendedCommunityCaseBuilder()
57 .setRouteOriginExtendedCommunity(new RouteOriginExtendedCommunityBuilder()
58 .setGlobalAdministrator(new ShortAsNumber(Uint32.valueOf(54)))
59 .setLocalAdministrator(new byte[] { 0, 0, 1, 76 }).build())
61 final ExtendedCommunities expected = new ExtendedCommunitiesBuilder().setTransitive(false)
62 .setExtendedCommunity(routeOrigin).build();
63 final AttributesBuilder attBuilder = new AttributesBuilder();
65 this.handler.parseAttribute(Unpooled.copiedBuffer(INPUT), attBuilder, null);
66 final ExtendedCommunities parsed = attBuilder.getExtendedCommunities().get(0);
67 assertEquals(expected, parsed);
69 final ByteBuf output = Unpooled.buffer(INPUT.length);
70 this.handler.serializeAttribute(attBuilder.build(), output);
71 assertArrayEquals(Bytes.concat(new byte[] {(byte)192, 16, 8}, INPUT), ByteArray.readAllBytes(output));
75 public void testEmptyListExtendedCommunityAttributeParser() throws BGPDocumentedException, BGPParsingException {
76 final List<ExtendedCommunities> extendedCommunitiesList = new ArrayList<>();
77 final AttributesBuilder attBuilder = new AttributesBuilder().setExtendedCommunities(extendedCommunitiesList);
78 final ByteBuf output = Unpooled.buffer();
80 this.handler.serializeAttribute(attBuilder.build(), output);
81 assertEquals(output, output);
85 public void testEmptyExtendedCommunityAttributeParser() throws BGPDocumentedException, BGPParsingException {
86 final ByteBuf output = Unpooled.buffer();
87 this.handler.serializeAttribute(new AttributesBuilder().build(), output);
88 assertEquals(Unpooled.buffer(), output);
92 public void testExtendedCommunityAttributeParserUnknown() throws BGPDocumentedException, BGPParsingException {
93 final AttributesBuilder attBuilder = new AttributesBuilder();
94 this.handler.parseAttribute(Unpooled.copiedBuffer(UNKOWN), attBuilder, null);
95 assertTrue(attBuilder.getExtendedCommunities().isEmpty());