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
9 package org.opendaylight.protocol.bgp.parser.impl.message.update;
11 import com.google.common.primitives.Bytes;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.util.ArrayList;
15 import java.util.List;
16 import org.junit.Assert;
17 import org.junit.Before;
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.extended.community.ExtendedCommunityRegistry;
22 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
23 import org.opendaylight.protocol.util.ByteArray;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.AttributesBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.ExtendedCommunities;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.ExtendedCommunitiesBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ShortAsNumber;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteOriginExtendedCommunityCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteOriginExtendedCommunityCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.origin.extended.community._case.RouteOriginExtendedCommunityBuilder;
32 public class ExtendedCommunitiesAttributeParserTest {
34 private static final byte[] INPUT = {
35 0x40, 0x03, 0x00, 54, 0, 0, 1, 76
38 private static final byte[] UNKOWN = {
39 0x40, 0x05, 0x00, 54, 0, 0, 1, 76
42 private ExtendedCommunitiesAttributeParser handler;
46 final ExtendedCommunityRegistry exReg = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getExtendedCommunityRegistry();
47 this.handler = new ExtendedCommunitiesAttributeParser(exReg);
51 public void testExtendedCommunityAttributeParser() throws BGPDocumentedException, BGPParsingException {
52 final RouteOriginExtendedCommunityCase routeOrigin = new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(
53 new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(new ShortAsNumber(54L)).setLocalAdministrator(
54 new byte[] { 0, 0, 1, 76 }).build()).build();
55 final ExtendedCommunities expected = new ExtendedCommunitiesBuilder().setTransitive(false).setExtendedCommunity(routeOrigin).build();
56 final AttributesBuilder attBuilder = new AttributesBuilder();
58 this.handler.parseAttribute(Unpooled.copiedBuffer(INPUT), attBuilder);
59 final ExtendedCommunities parsed = attBuilder.getExtendedCommunities().get(0);
60 Assert.assertEquals(expected, parsed);
62 final ByteBuf output = Unpooled.buffer(INPUT.length);
63 this.handler.serializeAttribute(attBuilder.build(), output);
64 Assert.assertArrayEquals(Bytes.concat(new byte[] {(byte)192, 16, 8}, INPUT), ByteArray.readAllBytes(output));
68 public void testEmptyListExtendedCommunityAttributeParser() throws BGPDocumentedException, BGPParsingException {
69 final List<ExtendedCommunities> extendedCommunitiesList = new ArrayList<>();
70 final AttributesBuilder attBuilder = new AttributesBuilder().setExtendedCommunities(extendedCommunitiesList);
71 final ByteBuf output = Unpooled.buffer();
73 this.handler.serializeAttribute(attBuilder.build(), output);
74 Assert.assertEquals(output, output);
78 public void testEmptyExtendedCommunityAttributeParser() throws BGPDocumentedException, BGPParsingException {
79 final ByteBuf output = Unpooled.buffer();
80 this.handler.serializeAttribute(new AttributesBuilder().build(), output);
81 Assert.assertEquals( Unpooled.buffer(), output);
85 public void testExtendedCommunityAttributeParserUnknown() throws BGPDocumentedException, BGPParsingException {
86 final AttributesBuilder attBuilder = new AttributesBuilder();
87 this.handler.parseAttribute(Unpooled.copiedBuffer(UNKOWN), attBuilder);
88 Assert.assertTrue(attBuilder.getExtendedCommunities().isEmpty());