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.assertNotNull;
13 import static org.junit.Assert.assertNull;
15 import com.google.common.collect.Lists;
16 import io.netty.buffer.ByteBuf;
17 import io.netty.buffer.Unpooled;
18 import java.io.ByteArrayOutputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26 import org.opendaylight.protocol.bgp.parser.impl.message.BGPUpdateMessageParser;
27 import org.opendaylight.protocol.bgp.parser.impl.message.update.CommunityUtil;
28 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
29 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
30 import org.opendaylight.protocol.util.ByteArray;
31 import org.opendaylight.protocol.util.NoopReferenceCache;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.prefixes.DestinationIpv6Builder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.prefixes.destination.ipv6.Ipv6Prefixes;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.prefixes.destination.ipv6.Ipv6PrefixesBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv6CaseBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.UpdateBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Aggregator;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AggregatorBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AsPathBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AtomicAggregateBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.ClusterIdBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Communities;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.ExtendedCommunities;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.ExtendedCommunitiesBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.LocalPrefBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.MultiExitDiscBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.OriginBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.OriginatorIdBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.UnrecognizedAttributes;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.as.path.Segments;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.as.path.SegmentsBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.Nlri;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.NlriBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.WithdrawnRoutesBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1Builder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlriBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpOrigin;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ClusterIdentifier;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.AListCaseBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.ASetCaseBuilder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.a.list._case.AListBuilder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.a.list._case.a.list.AsSequence;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.a.list._case.a.list.AsSequenceBuilder;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.a.set._case.ASetBuilder;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.Inet4SpecificExtendedCommunityCaseBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.inet4.specific.extended.community._case.Inet4SpecificExtendedCommunityBuilder;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCase;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv6NextHopCase;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv6NextHopCaseBuilder;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.ipv4.next.hop._case.Ipv4NextHopBuilder;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.ipv6.next.hop._case.Ipv6NextHopBuilder;
89 public class BGPParserTest {
92 * Used by other tests as well
94 static final List<byte[]> inputBytes = new ArrayList<byte[]>();
96 private static int COUNTER = 7;
98 private static int MAX_SIZE = 300;
100 private static BGPUpdateMessageParser updateParser;
102 private static final int LENGTH_FIELD_LENGTH = 2;
105 public static void setUp() throws Exception {
106 updateParser = new BGPUpdateMessageParser(ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry());
107 for (int i = 1; i <= COUNTER; i++) {
108 final String name = "/up" + i + ".bin";
109 try (final InputStream is = BGPParserTest.class.getResourceAsStream(name)){
111 throw new IOException("Failed to get resource " + name);
113 final ByteArrayOutputStream bis = new ByteArrayOutputStream();
114 final byte[] data = new byte[MAX_SIZE];
116 while ((nRead = is.read(data, 0, data.length)) != -1) {
117 bis.write(data, 0, nRead);
121 inputBytes.add(bis.toByteArray());
129 public void testResource() {
130 assertNotNull(inputBytes);
134 * Tests IPv4 NEXT_HOP, ATOMIC_AGGREGATE, COMMUNITY, NLRI
136 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
137 * 00 54 <- length (84) - including header
139 * 00 00 <- withdrawn routes length
140 * 00 31 <- total path attribute length (49)
141 * 40 <- attribute flags
142 * 01 <- attribute type code (origin)
143 * 01 <- attribute length
144 * 00 <- Origin value (IGP)
145 * 40 <- attribute flags
146 * 02 <- attribute type code (as path)
147 * 06 <- attribute length
149 * 01 <- path segment count
150 * 00 00 fd ea <- path segment value (65002)
151 * 40 <- attribute flags
152 * 03 <- attribute type code (Next Hop)
153 * 04 <- attribute length
154 * 10 00 00 02 <- value (10.0.0.2)
155 * 80 <- attribute flags
156 * 04 <- attribute type code (multi exit disc)
157 * 04 <- attribute length
158 * 00 00 00 00 <- value
159 * 60 <- attribute flags
160 * 06 <- attribute type code (atomic aggregate)
161 * 00 <- attribute length
162 * 40 <- attribute flags
163 * 08 <- attribute type code (community)
164 * 10 <- attribute length FF FF FF
165 * 01 <- value (NO_EXPORT)
166 * FF FF FF 02 <- value (NO_ADVERTISE)
167 * FF FF FF 03 <- value (NO_EXPORT_SUBCONFED)
168 * FF FF FF 10 <- unknown Community
171 * 18 ac 11 02 <- IPv4 Prefix (172.17.2.0 / 24)
172 * 18 ac 11 01 <- IPv4 Prefix (172.17.1.0 / 24)
173 * 18 ac 11 00 <- IPv4 Prefix (172.17.0.0 / 24)
176 public void testGetUpdateMessage1() throws Exception {
178 final byte[] body = ByteArray.cutBytes(inputBytes.get(0), MessageUtil.COMMON_HEADER_LENGTH);
179 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(0), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
180 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
184 assertNull(message.getWithdrawnRoutes());
188 final List<AsSequence> asnums = Lists.newArrayList(new AsSequenceBuilder().setAs(new AsNumber(65002L)).build());
189 final List<Segments> asPath = Lists.newArrayList();
190 asPath.add(new SegmentsBuilder().setCSegment(
191 new AListCaseBuilder().setAList(new AListBuilder().setAsSequence(asnums).build()).build()).build());
193 final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
194 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.2")).build()).build();
196 final List<Communities> comms = Lists.newArrayList();
197 comms.add((Communities) CommunityUtil.NO_EXPORT);
198 comms.add((Communities) CommunityUtil.NO_ADVERTISE);
199 comms.add((Communities) CommunityUtil.NO_EXPORT_SUBCONFED);
200 comms.add((Communities) CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF10));
202 final UpdateBuilder builder = new UpdateBuilder();
206 final List<Ipv4Prefix> prefs = Lists.newArrayList();
207 prefs.add(new Ipv4Prefix("172.17.2.0/24"));
208 prefs.add(new Ipv4Prefix("172.17.1.0/24"));
209 prefs.add(new Ipv4Prefix("172.17.0.0/24"));
211 final Nlri nlri = new NlriBuilder().setNlri(prefs).build();
213 assertEquals(nlri, message.getNlri());
215 builder.setNlri(nlri);
217 // check path attributes
219 final PathAttributes attrs = message.getPathAttributes();
221 final PathAttributesBuilder paBuilder = new PathAttributesBuilder();
223 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
224 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
226 paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
227 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
229 paBuilder.setCNextHop(nextHop);
230 assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
232 paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
233 assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
235 paBuilder.setAtomicAggregate(new AtomicAggregateBuilder().build());
236 assertEquals(paBuilder.getAtomicAggregate(), attrs.getAtomicAggregate());
238 paBuilder.setCommunities(comms);
239 assertEquals(paBuilder.getCommunities(), attrs.getCommunities());
241 paBuilder.setUnrecognizedAttributes(Collections.<UnrecognizedAttributes> emptyList());
243 builder.setPathAttributes(paBuilder.build());
245 assertEquals(builder.build(), message);
247 final ByteBuf buffer = Unpooled.buffer();
248 BGPParserTest.updateParser.serializeMessage(message, buffer);
249 assertArrayEquals(inputBytes.get(0), ByteArray.readAllBytes(buffer));
253 * Tests IPv6 NEXT_HOP, NLRI, ORIGIN.IGP, MULTI_EXIT_DISC, ORIGINATOR-ID, CLUSTER_LIST.
255 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
256 * 00 80 <- length (128) - including header
258 * 00 00 <- withdrawn routes length
259 * 00 69 <- total path attribute length (105)
260 * 40 <- attribute flags
261 * 01 <- attribute type code (origin)
262 * 01 <- attribute length
263 * 00 <- Origin value (IGP)
264 * 40 <- attribute flags
265 * 02 <- attribute type code (as path)
266 * 06 <- attribute length
268 * 01 <- path segment count
269 * 00 00 fd e9 <- path segment value (65001)
270 * 40 <- attribute flags
271 * 03 <- attribute type code (next hop)
272 * 04 <- attribute length
273 * 0a 00 00 00 <- next hop value (10.0.0.0)
274 * 80 <- attribute flags
275 * 04 <- attribute type code (multi exit disc)
276 * 04 <- attribute length
277 * 00 00 00 00 <- value
278 * 80 <- attribute flags
279 * 09 <- attribute type code (originator id)
280 * 04 <- attribute length
281 * 7f 00 00 01 <- value (localhost ip)
282 * 80 <- attribute flags
283 * 0a <- attribute type code (cluster list)
284 * 08 <- attribute length
285 * 01 02 03 04 <- value
286 * 05 06 07 08 <- value
287 * 80 <- attribute flags
288 * 0e <- attribute type code (mp reach nlri)
289 * 40 <- attribute length
290 * 00 02 <- AFI (Ipv6)
291 * 01 <- SAFI (Unicast)
292 * 20 <- length of next hop
293 * 20 01 0d b8 00 00 00 00 00 00 00 00 00 00 00 01 <- global
294 * fe 80 00 00 00 00 00 00 c0 01 0b ff fe 7e 00 <- link local
298 * 40 20 01 0d b8 00 01 00 02 <- IPv6 Prefix (2001:db8:1:2:: / 64)
299 * 40 20 01 0d b8 00 01 00 01 <- IPv6 Prefix (2001:db8:1:1:: / 64)
300 * 40 20 01 0d b8 00 01 00 00 <- IPv6 Prefix (2001:db8:1:: / 64)
304 public void testGetUpdateMessage2() throws Exception {
305 final byte[] body = ByteArray.cutBytes(inputBytes.get(1), MessageUtil.COMMON_HEADER_LENGTH);
306 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(1), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
307 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
310 assertNull(message.getWithdrawnRoutes());
312 final UpdateBuilder builder = new UpdateBuilder();
316 final List<Ipv6Prefixes> prefs = new ArrayList<>();
317 prefs.add(new Ipv6PrefixesBuilder().setPrefix(new Ipv6Prefix("2001:db8:1:2::/64")).build());
318 prefs.add(new Ipv6PrefixesBuilder().setPrefix(new Ipv6Prefix("2001:db8:1:1::/64")).build());
319 prefs.add(new Ipv6PrefixesBuilder().setPrefix(new Ipv6Prefix("2001:db8:1::/64")).build());
321 assertNull(message.getNlri());
325 final List<AsSequence> asnums = Lists.newArrayList(new AsSequenceBuilder().setAs(new AsNumber(65001L)).build());
326 final List<Segments> asPath = Lists.newArrayList();
327 asPath.add(new SegmentsBuilder().setCSegment(
328 new AListCaseBuilder().setAList(new AListBuilder().setAsSequence(asnums).build()).build()).build());
330 final Ipv6NextHopCase nextHop = new Ipv6NextHopCaseBuilder().setIpv6NextHop(
331 new Ipv6NextHopBuilder().setGlobal(new Ipv6Address("2001:db8::1")).setLinkLocal(new Ipv6Address("fe80::c001:bff:fe7e:0")).build()).build();
333 final List<ClusterIdentifier> clusters = Lists.newArrayList(new ClusterIdentifier(new Ipv4Address("1.2.3.4")),
334 new ClusterIdentifier(new Ipv4Address("5.6.7.8")));
336 // check path attributes
338 final PathAttributes attrs = message.getPathAttributes();
340 final PathAttributesBuilder paBuilder = new PathAttributesBuilder();
342 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
343 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
345 paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
346 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
348 paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
349 assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
351 paBuilder.setOriginatorId(new OriginatorIdBuilder().setOriginator(new Ipv4Address("127.0.0.1")).build());
352 assertEquals(paBuilder.getOriginatorId(), attrs.getOriginatorId());
354 paBuilder.setClusterId(new ClusterIdBuilder().setCluster(clusters).build());
355 assertEquals(paBuilder.getClusterId(), attrs.getClusterId());
357 final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
358 mpBuilder.setAfi(Ipv6AddressFamily.class);
359 mpBuilder.setSafi(UnicastSubsequentAddressFamily.class);
360 mpBuilder.setCNextHop(nextHop);
361 mpBuilder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
362 new DestinationIpv6CaseBuilder().setDestinationIpv6(new DestinationIpv6Builder().setIpv6Prefixes(prefs).build()).build()).build());
364 paBuilder.addAugmentation(PathAttributes1.class, new PathAttributes1Builder().setMpReachNlri(mpBuilder.build()).build());
365 assertEquals(paBuilder.getAugmentation(PathAttributes1.class).getMpReachNlri(),
366 attrs.getAugmentation(PathAttributes1.class).getMpReachNlri());
367 paBuilder.setUnrecognizedAttributes(Collections.<UnrecognizedAttributes> emptyList());
370 builder.setPathAttributes(paBuilder.build());
371 assertEquals(builder.build(), message);
373 final ByteBuf buffer = Unpooled.buffer();
374 BGPParserTest.updateParser.serializeMessage(message, buffer);
375 assertArrayEquals(inputBytes.get(1), ByteArray.readAllBytes(buffer));
379 * Tests more AS Numbers in AS_PATH, AGGREGATOR, ORIGIN.INCOMPLETE
381 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
382 * 00 4b <- length (75) - including header
384 * 00 00 <- withdrawn routes length
385 * 00 30 <- total path attribute length (48)
386 * 40 <- attribute flags
387 * 01 <- attribute type code (origin)
388 * 01 <- attribute length
389 * 02 <- Origin value (Incomplete)
390 * 40 <- attribute flags
391 * 02 <- attribute type code (as path)
392 * 10 <- attribute length
394 * 01 <- path segment count
395 * 00 00 00 1e <- path segment value (30)
397 * 02 <- path segment count
398 * 00 00 00 0a <- path segment value (10)
399 * 00 00 00 14 <- path segment value (20)
400 * 40 <- attribute flags
401 * 03 <- attribute type (Next hop)
402 * 04 <- attribute length
403 * 0a 00 00 09 <- value (10.0.0.9)
404 * 80 <- attribute flags
405 * 04 <- attribute type code (multi exit disc)
406 * 04 <- attribute length
407 * 00 00 00 00 <- value
408 * c0 <- attribute flags
409 * 07 <- attribute type (Aggregator)
410 * 08 <- attribute length
411 * 00 00 00 1e <- value (AS number = 30)
412 * 0a 00 00 09 <- value (IP address = 10.0.0.9)
415 * 15 ac 10 00 <- IPv4 Prefix (172.16.0.0 / 21)
418 public void testGetUpdateMessage3() throws Exception {
419 final byte[] body = ByteArray.cutBytes(inputBytes.get(2), MessageUtil.COMMON_HEADER_LENGTH);
420 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(2), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
421 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
423 final UpdateBuilder builder = new UpdateBuilder();
426 final Ipv4Prefix pref1 = new Ipv4Prefix("172.16.0.0/21");
428 final List<Ipv4Prefix> nlri = Lists.newArrayList(pref1);
429 builder.setNlri(new NlriBuilder().setNlri(nlri).build());
430 assertEquals(builder.getNlri(), message.getNlri());
433 assertNull(message.getWithdrawnRoutes());
436 final List<AsSequence> asnums = Lists.newArrayList(new AsSequenceBuilder().setAs(new AsNumber(30L)).build());
437 final List<Segments> asPath = Lists.newArrayList();
438 asPath.add(new SegmentsBuilder().setCSegment(
439 new AListCaseBuilder().setAList(new AListBuilder().setAsSequence(asnums).build()).build()).build());
440 asPath.add(new SegmentsBuilder().setCSegment(
441 new ASetCaseBuilder().setASet(new ASetBuilder().setAsSet(Lists.newArrayList(new AsNumber(10L), new AsNumber(20L))).build()).build()).build());
443 final Aggregator aggregator = new AggregatorBuilder().setAsNumber(new AsNumber((long) 30)).setNetworkAddress(
444 new Ipv4Address("10.0.0.9")).build();
445 final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
446 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.9")).build()).build();
448 // check path attributes
449 final PathAttributes attrs = message.getPathAttributes();
451 final PathAttributesBuilder paBuilder = new PathAttributesBuilder();
453 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Incomplete).build());
454 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
456 paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
457 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
459 paBuilder.setCNextHop(nextHop);
460 assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
462 paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
463 assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
465 paBuilder.setAggregator(aggregator);
466 assertEquals(paBuilder.getAggregator(), attrs.getAggregator());
467 paBuilder.setUnrecognizedAttributes(Collections.<UnrecognizedAttributes> emptyList());
468 builder.setPathAttributes(paBuilder.build());
470 assertEquals(builder.build(), message);
472 final ByteBuf buffer = Unpooled.buffer();
473 BGPParserTest.updateParser.serializeMessage(message, buffer);
474 assertArrayEquals(inputBytes.get(2), ByteArray.readAllBytes(buffer));
478 * Tests empty AS_PATH, ORIGIN.EGP, LOCAL_PREF, EXTENDED_COMMUNITIES (Ipv4 Addr specific)
480 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
481 * 00 4A <- length (73) - including header
483 * 00 00 <- withdrawn routes length
484 * 00 27 <- total path attribute length (39)
485 * 40 <- attribute flags
486 * 01 <- attribute type code (Origin)
487 * 01 <- attribute length
488 * 01 <- Origin value (EGP)
489 * 40 <- attribute flags
490 * 02 <- attribute type code (As path)
491 * 00 <- attribute length
492 * 40 <- attribute flags
493 * 03 <- attribute type (Next hop)
494 * 04 <- attribute length
495 * 03 03 03 03 <- value (3.3.3.3)
496 * 80 <- attribute flags
497 * 04 <- attribute type code (Multi exit disc)
498 * 04 <- attribute length
499 * 00 00 00 00 <- value
500 * 40 <- attribute flags
501 * 05 <- attribute type (Local Pref)
502 * 04 <- attribute length
503 * 00 00 00 64 <- value (100)
504 * c0 <- attribute flags
505 * 10 <- attribute type (extended community)
506 * 08 <- attribute length
507 * 01 04 <- value (type - Ipv4 Address Specific Extended Community)
508 * c0 a8 01 00 <- value (global adm. 198.162.1.0)
509 * 12 34 <- value (local adm. 4660)
512 * 18 0a 1e 03 <- IPv4 Prefix (10.30.3.0 / 24)
513 * 18 0a 1e 02 <- IPv4 Prefix (10.30.2.0 / 24)
514 * 18 0a 1e 01 <- IPv4 Prefix (10.30.1.0 / 24)
517 public void testGetUpdateMessage4() throws Exception {
518 final byte[] body = ByteArray.cutBytes(inputBytes.get(3), MessageUtil.COMMON_HEADER_LENGTH);
519 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(3), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
520 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
522 final UpdateBuilder builder = new UpdateBuilder();
525 assertNull(message.getWithdrawnRoutes());
528 final Ipv4Prefix pref1 = new Ipv4Prefix("10.30.3.0/24");
529 final Ipv4Prefix pref2 = new Ipv4Prefix("10.30.2.0/24");
530 final Ipv4Prefix pref3 = new Ipv4Prefix("10.30.1.0/24");
532 final List<Ipv4Prefix> nlri = Lists.newArrayList(pref1, pref2, pref3);
533 builder.setNlri(new NlriBuilder().setNlri(nlri).build());
534 assertEquals(builder.getNlri(), message.getNlri());
537 final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
538 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("3.3.3.3")).build()).build();
540 final List<ExtendedCommunities> comms = Lists.newArrayList();
541 comms.add(new ExtendedCommunitiesBuilder().setCommType((short) 1).setCommSubType((short) 4).setExtendedCommunity(
542 new Inet4SpecificExtendedCommunityCaseBuilder().setInet4SpecificExtendedCommunity(
543 new Inet4SpecificExtendedCommunityBuilder().setTransitive(false).setGlobalAdministrator(
544 new Ipv4Address("192.168.1.0")).setLocalAdministrator(new byte[] { 0x12, 0x34 }).build()).build()).build());
546 // check path attributes
547 final PathAttributes attrs = message.getPathAttributes();
549 final PathAttributesBuilder paBuilder = new PathAttributesBuilder();
551 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Egp).build());
552 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
554 paBuilder.setAsPath(new AsPathBuilder().setSegments(Collections.<Segments> emptyList()).build());
555 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
557 paBuilder.setCNextHop(nextHop);
558 assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
560 paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
561 assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
563 paBuilder.setLocalPref(new LocalPrefBuilder().setPref(100L).build());
564 assertEquals(paBuilder.getLocalPref(), attrs.getLocalPref());
566 paBuilder.setExtendedCommunities(comms);
567 assertEquals(paBuilder.getExtendedCommunities(), attrs.getExtendedCommunities());
569 paBuilder.setUnrecognizedAttributes(Collections.<UnrecognizedAttributes> emptyList());
571 builder.setPathAttributes(paBuilder.build());
572 assertEquals(builder.build(), message);
574 final ByteBuf buffer = Unpooled.buffer();
575 BGPParserTest.updateParser.serializeMessage(message, buffer);
576 assertArrayEquals(inputBytes.get(3), ByteArray.readAllBytes(buffer));
580 * Tests withdrawn routes.
582 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
583 * 00 1c <- length (28) - including header
585 * 00 05 <- withdrawn routes length (5)
586 * 1e ac 10 00 04 <- route (172.16.0.4)
587 * 00 00 <- total path attribute length
590 public void testGetUpdateMessage5() throws Exception {
591 final byte[] body = ByteArray.cutBytes(inputBytes.get(4), MessageUtil.COMMON_HEADER_LENGTH);
592 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(4), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
593 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
596 final List<Ipv4Prefix> prefs = Lists.newArrayList(new Ipv4Prefix("172.16.0.4/30"));
599 final Update expectedMessage = new UpdateBuilder().setWithdrawnRoutes(
600 new WithdrawnRoutesBuilder().setWithdrawnRoutes(prefs).build()).build();
602 assertEquals(expectedMessage.getWithdrawnRoutes(), message.getWithdrawnRoutes());
604 final ByteBuf buffer = Unpooled.buffer();
605 BGPParserTest.updateParser.serializeMessage(message, buffer);
606 assertArrayEquals(inputBytes.get(4), ByteArray.readAllBytes(buffer));
610 * End of Rib for Ipv4.
612 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
613 * 00 17 <- length (23) - including header
615 * 00 00 <- withdrawn routes length
616 * 00 00 <- total path attribute length
619 public void testEORIpv4() throws Exception {
620 final byte[] body = ByteArray.cutBytes(inputBytes.get(5), MessageUtil.COMMON_HEADER_LENGTH);
621 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(5), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
622 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
624 assertEquals(new UpdateBuilder().build(), message);
626 final ByteBuf buffer = Unpooled.buffer();
627 BGPParserTest.updateParser.serializeMessage(message, buffer);
628 assertArrayEquals(inputBytes.get(5), ByteArray.readAllBytes(buffer));
632 * End of Rib for Ipv6 consists of empty MP_UNREACH_NLRI, with AFI 2 and SAFI 1
634 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
635 * 00 1d <- length (29) - including header
637 * 00 00 <- withdrawn routes length
638 * 00 06 <- total path attribute length
639 * 80 <- attribute flags
640 * 0f <- attribute type (15 - MP_UNREACH_NLRI)
641 * 03 <- attribute length
642 * 00 02 <- value (AFI 2: IPv6)
643 * 01 <- value (SAFI 1)
646 public void testEORIpv6() throws Exception {
647 final byte[] body = ByteArray.cutBytes(inputBytes.get(6), MessageUtil.COMMON_HEADER_LENGTH);
648 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(6), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
649 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
651 final Class<? extends AddressFamily> afi = message.getPathAttributes().getAugmentation(PathAttributes2.class).getMpUnreachNlri().getAfi();
652 final Class<? extends SubsequentAddressFamily> safi = message.getPathAttributes().getAugmentation(PathAttributes2.class).getMpUnreachNlri().getSafi();
654 assertEquals(Ipv6AddressFamily.class, afi);
655 assertEquals(UnicastSubsequentAddressFamily.class, safi);
657 final ByteBuf buffer = Unpooled.buffer();
658 BGPParserTest.updateParser.serializeMessage(message, buffer);
659 assertArrayEquals(inputBytes.get(6), ByteArray.readAllBytes(buffer));
663 * End of Rib for Ipv6 consists of empty MP_UNREACH_NLRI, with AFI 2 and SAFI 1
665 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
666 * 00 1e <- length (29) - including header
668 * 00 00 <- withdrawn routes length
669 * 00 07 <- total path attribute length
670 * 90 <- attribute flags
671 * 0f <- attribute type (15 - MP_UNREACH_NLRI)
672 * 00 03 <- attribute length
673 * 00 02 <- value (AFI 2: IPv6)
674 * 01 <- value (SAFI 1)
677 public void testEORIpv6exLength() throws Exception {
678 final byte[] body = ByteArray.cutBytes(inputBytes.get(6), MessageUtil.COMMON_HEADER_LENGTH);
679 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(6), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
680 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
682 final Class<? extends AddressFamily> afi = message.getPathAttributes().getAugmentation(PathAttributes2.class).getMpUnreachNlri().getAfi();
683 final Class<? extends SubsequentAddressFamily> safi = message.getPathAttributes().getAugmentation(PathAttributes2.class).getMpUnreachNlri().getSafi();
685 assertEquals(Ipv6AddressFamily.class, afi);
686 assertEquals(UnicastSubsequentAddressFamily.class, safi);
688 final ByteBuf buffer = Unpooled.buffer();
689 BGPParserTest.updateParser.serializeMessage(message, buffer);
690 assertArrayEquals(inputBytes.get(6), ByteArray.readAllBytes(buffer));