2 * Copyright (c) 2013 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;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotSame;
14 import static org.junit.Assert.fail;
18 import org.junit.Test;
19 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
20 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
21 import org.opendaylight.protocol.bgp.parser.impl.message.update.CommunitiesParser;
22 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
23 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
24 import org.opendaylight.protocol.util.ByteList;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev130918.LinkstateAddressFamily;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.bgp.parameters.CParameters;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.bgp.parameters.c.parameters.CAs4Bytes;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.bgp.parameters.c.parameters.CAs4BytesBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.bgp.parameters.c.parameters.c.as4.bytes.As4BytesCapabilityBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.path.attributes.AggregatorBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.BgpTableType;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.open.bgp.parameters.c.parameters.CMultiprotocolBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.open.bgp.parameters.c.parameters.c.multiprotocol.MultiprotocolCapability;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.open.bgp.parameters.c.parameters.c.multiprotocol.MultiprotocolCapabilityBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpAggregator;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CAsSpecificExtendedCommunity;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CAsSpecificExtendedCommunityBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CInet4SpecificExtendedCommunity;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CInet4SpecificExtendedCommunityBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.COpaqueExtendedCommunity;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.COpaqueExtendedCommunityBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CRouteOriginExtendedCommunity;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CRouteOriginExtendedCommunityBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CRouteTargetExtendedCommunity;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CRouteTargetExtendedCommunityBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.c.as.specific.extended.community.AsSpecificExtendedCommunityBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.c.inet4.specific.extended.community.Inet4SpecificExtendedCommunityBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.c.opaque.extended.community.OpaqueExtendedCommunityBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.c.route.origin.extended.community.RouteOriginExtendedCommunityBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.c.route.target.extended.community.RouteTargetExtendedCommunityBuilder;
56 import com.google.common.collect.Maps;
58 public class ComplementaryTest {
61 public void testBGPParameter() {
63 final BgpTableType t = new BgpTableTypeImpl(LinkstateAddressFamily.class, UnicastSubsequentAddressFamily.class);
64 final BgpTableType t1 = new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
66 final MultiprotocolCapability cap = new MultiprotocolCapabilityBuilder().setAfi(LinkstateAddressFamily.class).setSafi(
67 UnicastSubsequentAddressFamily.class).build();
68 final CParameters tlv1 = new CMultiprotocolBuilder().setMultiprotocolCapability(cap).build();
70 final MultiprotocolCapability cap1 = new MultiprotocolCapabilityBuilder().setAfi(Ipv4AddressFamily.class).setSafi(
71 UnicastSubsequentAddressFamily.class).build();
72 final CParameters tlv2 = new CMultiprotocolBuilder().setMultiprotocolCapability(cap1).build();
74 final Map<BgpTableType, Boolean> tt = Maps.newHashMap();
78 // final BGPParameter tlv3 = new GracefulCapability(false, 0, tt);
80 final CParameters tlv4 = new CAs4BytesBuilder().setAs4BytesCapability(
81 new As4BytesCapabilityBuilder().setAsNumber(new AsNumber((long) 40)).build()).build();
83 // assertFalse(((GracefulCapability) tlv3).isRestartFlag());
85 // assertEquals(0, ((GracefulCapability) tlv3).getRestartTimerValue());
87 assertFalse(tlv1.equals(tlv2));
89 // assertNotSame(tlv1.hashCode(), tlv3.hashCode());
91 // assertNotSame(tlv2.toString(), tlv3.toString());
93 // assertEquals(((GracefulCapability) tlv3).getTableTypes(), tt);
95 assertEquals(cap.getSafi(), cap1.getSafi());
97 assertNotSame(cap.getAfi(), cap1.getAfi());
99 assertEquals(40, ((CAs4Bytes) tlv4).getAs4BytesCapability().getAsNumber().getValue().longValue());
102 new CAs4BytesBuilder().setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(new AsNumber((long) 40)).build()).build(),
107 public void testBGPAggregatorImpl() {
108 final BgpAggregator ipv4 = new AggregatorBuilder().setAsNumber(new AsNumber((long) 5524)).setNetworkAddress(
109 new Ipv4Address("124.55.42.1")).build();
110 final BgpAggregator ipv4i = new AggregatorBuilder().setAsNumber(new AsNumber((long) 5525)).setNetworkAddress(
111 new Ipv4Address("124.55.42.1")).build();
113 assertNotSame(ipv4.hashCode(), ipv4i.hashCode());
115 assertNotSame(ipv4.getAsNumber(), ipv4i.getAsNumber());
117 assertEquals(ipv4.getNetworkAddress(), ipv4i.getNetworkAddress());
121 public void testCommunitiesParser() {
122 CAsSpecificExtendedCommunity as = null;
124 as = (CAsSpecificExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 0, 5, 0, 54, 0, 0, 1, 76 });
125 } catch (final BGPDocumentedException e1) {
126 fail("Not expected exception: " + e1);
128 CAsSpecificExtendedCommunity expected = new CAsSpecificExtendedCommunityBuilder().setAsSpecificExtendedCommunity(
129 new AsSpecificExtendedCommunityBuilder().setTransitive(false).setGlobalAdministrator(new AsNumber(54L)).setLocalAdministrator(
130 new byte[] { 0, 0, 1, 76 }).build()).build();
131 assertEquals(expected.getAsSpecificExtendedCommunity().isTransitive(), as.getAsSpecificExtendedCommunity().isTransitive());
132 assertEquals(expected.getAsSpecificExtendedCommunity().getGlobalAdministrator(),
133 as.getAsSpecificExtendedCommunity().getGlobalAdministrator());
134 assertArrayEquals(expected.getAsSpecificExtendedCommunity().getLocalAdministrator(),
135 as.getAsSpecificExtendedCommunity().getLocalAdministrator());
138 as = (CAsSpecificExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 40, 5, 0, 54, 0, 0, 1, 76 });
139 } catch (final BGPDocumentedException e1) {
140 fail("Not expected exception: " + e1);
142 expected = new CAsSpecificExtendedCommunityBuilder().setAsSpecificExtendedCommunity(
143 new AsSpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(new AsNumber(54L)).setLocalAdministrator(
144 new byte[] { 0, 0, 1, 76 }).build()).build();
145 assertEquals(expected.getAsSpecificExtendedCommunity().isTransitive(), as.getAsSpecificExtendedCommunity().isTransitive());
147 CRouteTargetExtendedCommunity rtc = null;
149 rtc = (CRouteTargetExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 1, 2, 0, 35, 4, 2, 8, 7 });
150 } catch (final BGPDocumentedException e1) {
151 fail("Not expected exception: " + e1);
153 final CRouteTargetExtendedCommunity rexpected = new CRouteTargetExtendedCommunityBuilder().setRouteTargetExtendedCommunity(
154 new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(new AsNumber(35L)).setLocalAdministrator(
155 new byte[] { 4, 2, 8, 7 }).build()).build();
156 assertEquals(rexpected.getRouteTargetExtendedCommunity().getGlobalAdministrator(),
157 rtc.getRouteTargetExtendedCommunity().getGlobalAdministrator());
158 assertArrayEquals(rexpected.getRouteTargetExtendedCommunity().getLocalAdministrator(),
159 rtc.getRouteTargetExtendedCommunity().getLocalAdministrator());
161 CRouteOriginExtendedCommunity roc = null;
163 roc = (CRouteOriginExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 0, 3, 0, 24, 4, 2, 8, 7 });
164 } catch (final BGPDocumentedException e1) {
165 fail("Not expected exception: " + e1);
167 final CRouteOriginExtendedCommunity oexpected = new CRouteOriginExtendedCommunityBuilder().setRouteOriginExtendedCommunity(
168 new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(new AsNumber(24L)).setLocalAdministrator(
169 new byte[] { 4, 2, 8, 7 }).build()).build();
170 assertEquals(oexpected.getRouteOriginExtendedCommunity().getGlobalAdministrator(),
171 roc.getRouteOriginExtendedCommunity().getGlobalAdministrator());
172 assertArrayEquals(oexpected.getRouteOriginExtendedCommunity().getLocalAdministrator(),
173 roc.getRouteOriginExtendedCommunity().getLocalAdministrator());
175 CInet4SpecificExtendedCommunity sec = null;
177 sec = (CInet4SpecificExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 41, 6, 12, 51, 2, 5, 21, 45 });
178 } catch (final BGPDocumentedException e1) {
179 fail("Not expected exception: " + e1);
181 final CInet4SpecificExtendedCommunity iexpected = new CInet4SpecificExtendedCommunityBuilder().setInet4SpecificExtendedCommunity(
182 new Inet4SpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(new Ipv4Address("12.51.2.5")).setLocalAdministrator(
183 new byte[] { 21, 45 }).build()).build();
184 assertEquals(iexpected.getInet4SpecificExtendedCommunity().isTransitive(), sec.getInet4SpecificExtendedCommunity().isTransitive());
185 assertEquals(iexpected.getInet4SpecificExtendedCommunity().getGlobalAdministrator(),
186 sec.getInet4SpecificExtendedCommunity().getGlobalAdministrator());
187 assertArrayEquals(iexpected.getInet4SpecificExtendedCommunity().getLocalAdministrator(),
188 sec.getInet4SpecificExtendedCommunity().getLocalAdministrator());
190 COpaqueExtendedCommunity oec = null;
192 oec = (COpaqueExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 3, 6, 21, 45, 5, 4, 3, 1 });
193 } catch (final BGPDocumentedException e1) {
194 fail("Not expected exception: " + e1);
196 final COpaqueExtendedCommunity oeexpected = new COpaqueExtendedCommunityBuilder().setOpaqueExtendedCommunity(
197 new OpaqueExtendedCommunityBuilder().setTransitive(false).setValue(new byte[] { 21, 45, 5, 4, 3, 1 }).build()).build();
198 assertEquals(oeexpected.getOpaqueExtendedCommunity().isTransitive(), oec.getOpaqueExtendedCommunity().isTransitive());
199 assertArrayEquals(oeexpected.getOpaqueExtendedCommunity().getValue(), oec.getOpaqueExtendedCommunity().getValue());
202 oec = (COpaqueExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 43, 6, 21, 45, 5, 4, 3, 1 });
203 } catch (final BGPDocumentedException e1) {
204 fail("Not expected exception: " + e1);
206 final COpaqueExtendedCommunity oeexpected1 = new COpaqueExtendedCommunityBuilder().setOpaqueExtendedCommunity(
207 new OpaqueExtendedCommunityBuilder().setTransitive(true).setValue(new byte[] { 21, 45, 5, 4, 3, 1 }).build()).build();
208 assertEquals(oeexpected1.getOpaqueExtendedCommunity().isTransitive(), oec.getOpaqueExtendedCommunity().isTransitive());
209 assertArrayEquals(oeexpected1.getOpaqueExtendedCommunity().getValue(), oec.getOpaqueExtendedCommunity().getValue());
212 CommunitiesParser.parseExtendedCommunity(new byte[] { 11, 11, 21, 45, 5, 4, 3, 1 });
213 fail("Exception should have occured.");
214 } catch (final BGPDocumentedException e) {
215 assertEquals("Could not parse Extended Community type: 11", e.getMessage());
220 public void testBGPHeaderParser() throws Exception {
221 final MessageRegistry msgReg = ServiceLoaderBGPExtensionProviderContext.createConsumerContext().getMessageRegistry();
223 msgReg.parseMessage(new byte[] { (byte) 0, (byte) 0 });
224 fail("Exception should have occured.");
225 } catch (final IllegalArgumentException e) {
226 assertEquals("Too few bytes in passed array. Passed: 2. Expected: >= 19.", e.getMessage());
231 public void testByteList() {
232 final ByteList b1 = new ByteList();
233 b1.add(new byte[] { 3, 4, 8 });
234 b1.add(new byte[] { 3, 4, 9 });
236 final ByteList b2 = new ByteList();
237 b2.add(new byte[] { 3, 4, 8 });
238 b2.add(new byte[] { 3, 4, 9 });
240 assertEquals(b1, b2);
241 assertEquals(b1.toString(), b2.toString());
245 public void testMessageParser() throws Exception {
246 final MessageRegistry msgReg = ServiceLoaderBGPExtensionProviderContext.createConsumerContext().getMessageRegistry();
249 msgReg.serializeMessage(null);
250 } catch (final IllegalArgumentException e) {
253 assertEquals("BGPMessage is mandatory.", ex);