Move route target ext comm container
[bgpcep.git] / bgp / parser-spi / src / test / java / org / opendaylight / protocol / bgp / parser / spi / pojo / SimpleExtendedCommunityRegistryTest.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.spi.pojo;
10
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;
19
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;
36
37 public class SimpleExtendedCommunityRegistryTest {
38
39     private SimpleExtendedCommunityRegistry register;
40
41     private final ExtendedCommunityParser parser = mock(ExtendedCommunityParser.class);
42     private final ExtendedCommunitySerializer serializer = mock(ExtendedCommunitySerializer.class);
43
44     @Before
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));
53
54     }
55
56     @Test
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());
66
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());
71         //no value parser
72         assertNull(parsedExtendedCommunity.getExtendedCommunity());
73     }
74
75     @Test
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));
85
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));
91     }
92
93     @Test(expected=IllegalArgumentException.class)
94     public void testRegisterParserOutOfRangeType() {
95         this.register.registerExtendedCommunityParser(1234, 0, this.parser);
96     }
97
98     @Test(expected=IllegalArgumentException.class)
99     public void testRegisterParserOutOfRangeSubType() {
100         this.register.registerExtendedCommunityParser(0, 1234, this.parser);
101     }
102
103 }