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;
16 import java.io.IOException;
19 import org.junit.Test;
20 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
21 import org.opendaylight.protocol.bgp.parser.BGPMessageFactory;
22 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
23 import org.opendaylight.protocol.bgp.parser.impl.message.update.CommunitiesParser;
24 import org.opendaylight.protocol.framework.DeserializerException;
25 import org.opendaylight.protocol.framework.DocumentedException;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev130918.LinkstateAddressFamily;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.bgp.parameters.CParameters;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.bgp.parameters.c.parameters.CAs4Bytes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.bgp.parameters.c.parameters.CAs4BytesBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.bgp.parameters.c.parameters.c.as4.bytes.As4BytesCapabilityBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.path.attributes.AggregatorBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.BgpTableType;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.open.bgp.parameters.c.parameters.CMultiprotocolBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.open.bgp.parameters.c.parameters.c.multiprotocol.MultiprotocolCapability;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.open.bgp.parameters.c.parameters.c.multiprotocol.MultiprotocolCapabilityBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpAggregator;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CAsSpecificExtendedCommunity;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CAsSpecificExtendedCommunityBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CInet4SpecificExtendedCommunity;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CInet4SpecificExtendedCommunityBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.COpaqueExtendedCommunity;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.COpaqueExtendedCommunityBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CRouteOriginExtendedCommunity;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CRouteOriginExtendedCommunityBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CRouteTargetExtendedCommunity;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CRouteTargetExtendedCommunityBuilder;
51 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;
52 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;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.c.opaque.extended.community.OpaqueExtendedCommunityBuilder;
54 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;
55 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;
57 import com.google.common.collect.Maps;
59 public class ComplementaryTest {
62 public void testBGPParameter() {
64 final BgpTableType t = new BgpTableTypeImpl(LinkstateAddressFamily.class, UnicastSubsequentAddressFamily.class);
65 final BgpTableType t1 = new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
67 final MultiprotocolCapability cap = new MultiprotocolCapabilityBuilder().setAfi(LinkstateAddressFamily.class).setSafi(
68 UnicastSubsequentAddressFamily.class).build();
69 final CParameters tlv1 = new CMultiprotocolBuilder().setMultiprotocolCapability(cap).build();
71 final MultiprotocolCapability cap1 = new MultiprotocolCapabilityBuilder().setAfi(Ipv4AddressFamily.class).setSafi(
72 UnicastSubsequentAddressFamily.class).build();
73 final CParameters tlv2 = new CMultiprotocolBuilder().setMultiprotocolCapability(cap1).build();
75 final Map<BgpTableType, Boolean> tt = Maps.newHashMap();
79 // final BGPParameter tlv3 = new GracefulCapability(false, 0, tt);
81 final CParameters tlv4 = new CAs4BytesBuilder().setAs4BytesCapability(
82 new As4BytesCapabilityBuilder().setAsNumber(new AsNumber((long) 40)).build()).build();
84 // assertFalse(((GracefulCapability) tlv3).isRestartFlag());
86 // assertEquals(0, ((GracefulCapability) tlv3).getRestartTimerValue());
88 assertFalse(tlv1.equals(tlv2));
90 // assertNotSame(tlv1.hashCode(), tlv3.hashCode());
92 // assertNotSame(tlv2.toString(), tlv3.toString());
94 // assertEquals(((GracefulCapability) tlv3).getTableTypes(), tt);
96 assertEquals(cap.getSafi(), cap1.getSafi());
98 assertNotSame(cap.getAfi(), cap1.getAfi());
100 assertEquals(40, ((CAs4Bytes) tlv4).getAs4BytesCapability().getAsNumber().getValue().longValue());
102 // FIXME: no generated toString
103 // assertEquals(new As4BytesBuilder().setCAs4Bytes(new CAs4BytesBuilder().setAsNumber(new AsNumber((long)
104 // 40)).build()).build().toString(), tlv4.toString());
108 public void testBGPAggregatorImpl() {
109 final BgpAggregator ipv4 = new AggregatorBuilder().setAsNumber(new AsNumber((long) 5524)).setNetworkAddress(
110 new Ipv4Address("124.55.42.1")).build();
111 final BgpAggregator ipv4i = new AggregatorBuilder().setAsNumber(new AsNumber((long) 5525)).setNetworkAddress(
112 new Ipv4Address("124.55.42.1")).build();
114 assertNotSame(ipv4.hashCode(), ipv4i.hashCode());
116 assertNotSame(ipv4.getAsNumber(), ipv4i.getAsNumber());
118 assertEquals(ipv4.getNetworkAddress(), ipv4i.getNetworkAddress());
122 public void testCommunitiesParser() {
123 CAsSpecificExtendedCommunity as = null;
125 as = (CAsSpecificExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 0, 5, 0, 54, 0, 0, 1, 76 });
126 } catch (final BGPDocumentedException e1) {
127 fail("Not expected exception: " + e1);
129 CAsSpecificExtendedCommunity expected = new CAsSpecificExtendedCommunityBuilder().setAsSpecificExtendedCommunity(
130 new AsSpecificExtendedCommunityBuilder().setTransitive(false).setGlobalAdministrator(new AsNumber(54L)).setLocalAdministrator(
131 new byte[] { 0, 0, 1, 76 }).build()).build();
132 assertEquals(expected.getAsSpecificExtendedCommunity().isTransitive(), as.getAsSpecificExtendedCommunity().isTransitive());
133 assertEquals(expected.getAsSpecificExtendedCommunity().getGlobalAdministrator(),
134 as.getAsSpecificExtendedCommunity().getGlobalAdministrator());
135 assertArrayEquals(expected.getAsSpecificExtendedCommunity().getLocalAdministrator(),
136 as.getAsSpecificExtendedCommunity().getLocalAdministrator());
139 as = (CAsSpecificExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 40, 5, 0, 54, 0, 0, 1, 76 });
140 } catch (final BGPDocumentedException e1) {
141 fail("Not expected exception: " + e1);
143 expected = new CAsSpecificExtendedCommunityBuilder().setAsSpecificExtendedCommunity(
144 new AsSpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(new AsNumber(54L)).setLocalAdministrator(
145 new byte[] { 0, 0, 1, 76 }).build()).build();
146 assertEquals(expected.getAsSpecificExtendedCommunity().isTransitive(), as.getAsSpecificExtendedCommunity().isTransitive());
148 CRouteTargetExtendedCommunity rtc = null;
150 rtc = (CRouteTargetExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 1, 2, 0, 35, 4, 2, 8, 7 });
151 } catch (final BGPDocumentedException e1) {
152 fail("Not expected exception: " + e1);
154 final CRouteTargetExtendedCommunity rexpected = new CRouteTargetExtendedCommunityBuilder().setRouteTargetExtendedCommunity(
155 new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(new AsNumber(35L)).setLocalAdministrator(
156 new byte[] { 4, 2, 8, 7 }).build()).build();
157 assertEquals(rexpected.getRouteTargetExtendedCommunity().getGlobalAdministrator(),
158 rtc.getRouteTargetExtendedCommunity().getGlobalAdministrator());
159 assertArrayEquals(rexpected.getRouteTargetExtendedCommunity().getLocalAdministrator(),
160 rtc.getRouteTargetExtendedCommunity().getLocalAdministrator());
162 CRouteOriginExtendedCommunity roc = null;
164 roc = (CRouteOriginExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 0, 3, 0, 24, 4, 2, 8, 7 });
165 } catch (final BGPDocumentedException e1) {
166 fail("Not expected exception: " + e1);
168 final CRouteOriginExtendedCommunity oexpected = new CRouteOriginExtendedCommunityBuilder().setRouteOriginExtendedCommunity(
169 new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(new AsNumber(24L)).setLocalAdministrator(
170 new byte[] { 4, 2, 8, 7 }).build()).build();
171 assertEquals(oexpected.getRouteOriginExtendedCommunity().getGlobalAdministrator(),
172 roc.getRouteOriginExtendedCommunity().getGlobalAdministrator());
173 assertArrayEquals(oexpected.getRouteOriginExtendedCommunity().getLocalAdministrator(),
174 roc.getRouteOriginExtendedCommunity().getLocalAdministrator());
176 CInet4SpecificExtendedCommunity sec = null;
178 sec = (CInet4SpecificExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 41, 6, 12, 51, 2, 5, 21, 45 });
179 } catch (final BGPDocumentedException e1) {
180 fail("Not expected exception: " + e1);
182 final CInet4SpecificExtendedCommunity iexpected = new CInet4SpecificExtendedCommunityBuilder().setInet4SpecificExtendedCommunity(
183 new Inet4SpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(new Ipv4Address("12.51.2.5")).setLocalAdministrator(
184 new byte[] { 21, 45 }).build()).build();
185 assertEquals(iexpected.getInet4SpecificExtendedCommunity().isTransitive(), sec.getInet4SpecificExtendedCommunity().isTransitive());
186 assertEquals(iexpected.getInet4SpecificExtendedCommunity().getGlobalAdministrator(),
187 sec.getInet4SpecificExtendedCommunity().getGlobalAdministrator());
188 assertArrayEquals(iexpected.getInet4SpecificExtendedCommunity().getLocalAdministrator(),
189 sec.getInet4SpecificExtendedCommunity().getLocalAdministrator());
191 COpaqueExtendedCommunity oec = null;
193 oec = (COpaqueExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 3, 6, 21, 45, 5, 4, 3, 1 });
194 } catch (final BGPDocumentedException e1) {
195 fail("Not expected exception: " + e1);
197 final COpaqueExtendedCommunity oeexpected = new COpaqueExtendedCommunityBuilder().setOpaqueExtendedCommunity(
198 new OpaqueExtendedCommunityBuilder().setTransitive(false).setValue(new byte[] { 21, 45, 5, 4, 3, 1 }).build()).build();
199 assertEquals(oeexpected.getOpaqueExtendedCommunity().isTransitive(), oec.getOpaqueExtendedCommunity().isTransitive());
200 assertArrayEquals(oeexpected.getOpaqueExtendedCommunity().getValue(), oec.getOpaqueExtendedCommunity().getValue());
203 oec = (COpaqueExtendedCommunity) CommunitiesParser.parseExtendedCommunity(new byte[] { 43, 6, 21, 45, 5, 4, 3, 1 });
204 } catch (final BGPDocumentedException e1) {
205 fail("Not expected exception: " + e1);
207 final COpaqueExtendedCommunity oeexpected1 = new COpaqueExtendedCommunityBuilder().setOpaqueExtendedCommunity(
208 new OpaqueExtendedCommunityBuilder().setTransitive(true).setValue(new byte[] { 21, 45, 5, 4, 3, 1 }).build()).build();
209 assertEquals(oeexpected1.getOpaqueExtendedCommunity().isTransitive(), oec.getOpaqueExtendedCommunity().isTransitive());
210 assertArrayEquals(oeexpected1.getOpaqueExtendedCommunity().getValue(), oec.getOpaqueExtendedCommunity().getValue());
213 CommunitiesParser.parseExtendedCommunity(new byte[] { 11, 11, 21, 45, 5, 4, 3, 1 });
214 fail("Exception should have occured.");
215 } catch (final BGPDocumentedException e) {
216 assertEquals("Could not parse Extended Community type: 11", e.getMessage());
221 public void testBGPHeaderParser() throws IOException {
222 final BGPMessageFactory h = BGPMessageFactoryImpl.INSTANCE;
224 h.parse(new byte[] { (byte) 0, (byte) 0 });
225 fail("Exception should have occured.");
226 } catch (final IllegalArgumentException e) {
227 assertEquals("Too few bytes in passed array. Passed: 2. Expected: >= 19.", e.getMessage());
228 } catch (final DeserializerException e) {
229 fail("Not this exception should have occured:" + e);
230 } catch (final DocumentedException e) {
231 fail("Not this exception should have occured:" + e);
236 public void testByteList() {
237 final ByteList b1 = new ByteList();
238 b1.add(new byte[] { 3, 4, 8 });
239 b1.add(new byte[] { 3, 4, 9 });
241 final ByteList b2 = new ByteList();
242 b2.add(new byte[] { 3, 4, 8 });
243 b2.add(new byte[] { 3, 4, 9 });
245 assertEquals(b1, b2);
246 assertEquals(b1.toString(), b2.toString());
250 public void testMessageParser() throws IOException {
251 final BGPMessageFactory parser = BGPMessageFactoryImpl.INSTANCE;
255 } catch (final IllegalArgumentException e) {
258 assertEquals("BGPMessage is mandatory.", ex);