MVPN RFC6514 Extendend communities
[bgpcep.git] / bgp / parser-impl / src / test / java / org / opendaylight / protocol / bgp / parser / impl / message / update / ExtendedCommunitiesAttributeParserTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  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
9 package org.opendaylight.protocol.bgp.parser.impl.message.update;
10
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.rev180329.path.attributes.AttributesBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.ExtendedCommunities;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.ExtendedCommunitiesBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.ShortAsNumber;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.RouteOriginExtendedCommunityCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.RouteOriginExtendedCommunityCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.route.origin.extended.community._case.RouteOriginExtendedCommunityBuilder;
31
32 public class ExtendedCommunitiesAttributeParserTest {
33
34     private static final byte[] INPUT = {
35         0x40, 0x03, 0x00, 54, 0, 0, 1, 76
36     };
37
38     private static final byte[] UNKOWN = {
39         0x40, 0x05, 0x00, 54, 0, 0, 1, 76
40     };
41
42     private ExtendedCommunitiesAttributeParser handler;
43
44     @Before
45     public void setUp() {
46         final ExtendedCommunityRegistry exReg = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getExtendedCommunityRegistry();
47         this.handler = new ExtendedCommunitiesAttributeParser(exReg);
48     }
49
50     @Test
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();
57
58         this.handler.parseAttribute(Unpooled.copiedBuffer(INPUT), attBuilder);
59         final ExtendedCommunities parsed = attBuilder.getExtendedCommunities().get(0);
60         Assert.assertEquals(expected, parsed);
61
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));
65     }
66
67     @Test
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();
72
73         this.handler.serializeAttribute(attBuilder.build(), output);
74         Assert.assertEquals(output, output);
75     }
76
77     @Test
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);
82     }
83
84     @Test
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());
89     }
90
91 }