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.spi.pojo;
11 import static org.junit.Assert.assertNull;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Matchers.anyBoolean;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.never;
18 import static org.mockito.Mockito.verify;
20 import io.netty.buffer.ByteBuf;
21 import io.netty.buffer.Unpooled;
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
26 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
27 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunityParser;
28 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunitySerializer;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.ExtendedCommunities;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.ExtendedCommunitiesBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.ExtendedCommunity;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.RouteOriginIpv4CaseBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.RouteTargetIpv4Case;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.RouteTargetIpv4CaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.route.target.ipv4.grouping.RouteTargetIpv4Builder;
37 public class SimpleExtendedCommunityRegistryTest {
39 private SimpleExtendedCommunityRegistry register;
41 private final ExtendedCommunityParser parser = mock(ExtendedCommunityParser.class);
42 private final ExtendedCommunitySerializer serializer = mock(ExtendedCommunitySerializer.class);
45 public void setup() throws BGPDocumentedException, BGPParsingException {
46 this.register = new SimpleExtendedCommunityRegistry();
47 this.register.registerExtendedCommunityParser(0, 0, this.parser);
48 this.register.registerExtendedCommunitySerializer(RouteTargetIpv4Case.class, this.serializer);
49 doReturn(0).when(this.serializer).getType(anyBoolean());
50 doReturn(0).when(this.serializer).getSubType();
51 doNothing().when(this.serializer).serializeExtendedCommunity(any(ExtendedCommunity.class), any(ByteBuf.class));
52 doReturn(null).when(this.parser).parseExtendedCommunity(any(ByteBuf.class));
57 public void testExtendedCommunityRegistry() throws BGPDocumentedException, BGPParsingException {
58 final ByteBuf output = Unpooled.buffer();
59 this.register.serializeExtendedCommunity(
60 new ExtendedCommunitiesBuilder().setTransitive(true)
61 .setExtendedCommunity(new RouteTargetIpv4CaseBuilder()
62 .setRouteTargetIpv4(new RouteTargetIpv4Builder().build()).build()).build(), output);
63 verify(this.serializer).serializeExtendedCommunity(any(ExtendedCommunity.class), any(ByteBuf.class));
64 //no value serialized, just header
65 Assert.assertEquals(2, output.readableBytes());
67 final ExtendedCommunities parsedExtendedCommunity =
68 this.register.parseExtendedCommunity(Unpooled.copiedBuffer(new byte[] {0, 0, 0, 0, 0, 0, 0, 0}));
69 verify(this.parser).parseExtendedCommunity(any(ByteBuf.class));
70 Assert.assertTrue(parsedExtendedCommunity.isTransitive());
72 assertNull(parsedExtendedCommunity.getExtendedCommunity());
76 public void testExtendedCommunityRegistryUnknown() throws BGPDocumentedException, BGPParsingException {
77 final ByteBuf output = Unpooled.buffer();
78 this.register.serializeExtendedCommunity(
79 new ExtendedCommunitiesBuilder().setTransitive(false)
80 .setExtendedCommunity(new RouteOriginIpv4CaseBuilder().build()).build(), output);
81 //no ex. community was serialized
82 Assert.assertEquals(0, output.readableBytes());
83 verify(this.serializer, never())
84 .serializeExtendedCommunity(any(ExtendedCommunity.class), any(ByteBuf.class));
86 final ExtendedCommunities noExtCommunity = this.register
87 .parseExtendedCommunity(Unpooled.copiedBuffer(new byte[] {0, 1, 0, 0, 0, 0, 0, 0}));
88 //no ext. community was parsed
89 assertNull(noExtCommunity);
90 verify(this.parser, never()).parseExtendedCommunity(any(ByteBuf.class));
93 @Test(expected=IllegalArgumentException.class)
94 public void testRegisterParserOutOfRangeType() {
95 this.register.registerExtendedCommunityParser(1234, 0, this.parser);
98 @Test(expected=IllegalArgumentException.class)
99 public void testRegisterParserOutOfRangeSubType() {
100 this.register.registerExtendedCommunityParser(0, 1234, this.parser);