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;
14 import static org.mockito.Mockito.mock;
16 import com.google.common.collect.Lists;
17 import io.netty.buffer.ByteBuf;
18 import io.netty.buffer.Unpooled;
19 import java.io.ByteArrayOutputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.Optional;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.mockito.Mockito;
29 import org.opendaylight.protocol.bgp.parser.impl.message.BGPUpdateMessageParser;
30 import org.opendaylight.protocol.bgp.parser.impl.message.update.CommunityUtil;
31 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
32 import org.opendaylight.protocol.bgp.parser.spi.MultiPathSupport;
33 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
34 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
35 import org.opendaylight.protocol.bgp.util.HexDumpBGPFileParser;
36 import org.opendaylight.protocol.util.ByteArray;
37 import org.opendaylight.protocol.util.NoopReferenceCache;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.UpdateBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.Aggregator;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.AggregatorBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.AsPathBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.AtomicAggregateBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.Communities;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.ExtendedCommunities;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.ExtendedCommunitiesBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.LocalPrefBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.MultiExitDiscBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.OriginBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.as.path.Segments;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.as.path.SegmentsBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.update.message.Nlri;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.update.message.NlriBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.update.message.WithdrawnRoutes;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.update.message.WithdrawnRoutesBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.BgpOrigin;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv6AddressFamily;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.SubsequentAddressFamily;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.RouteTargetIpv4CaseBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv4NextHopCase;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.ipv4.next.hop._case.Ipv4NextHopBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.route.target.ipv4.grouping.RouteTargetIpv4Builder;
74 public class BGPParserTest {
77 * Used by other tests as well.
79 static final List<byte[]> inputBytes = new ArrayList<>();
81 private static final int COUNTER = 7;
83 private static final int MAX_SIZE = 300;
85 private static BGPUpdateMessageParser updateParser;
87 private static final int LENGTH_FIELD_LENGTH = 2;
89 private static final String MULTIPATH_HEX_FILE = "/bgp-update-multipath.txt";
91 private static List<byte[]> updatesWithMultiplePath;
93 private static PeerSpecificParserConstraint constraint;
95 private static MultiPathSupport mpSupport;
98 public static void setUp() throws Exception {
99 updateParser = new BGPUpdateMessageParser(ServiceLoaderBGPExtensionProviderContext.getSingletonInstance()
100 .getAttributeRegistry());
101 for (int i = 1; i <= COUNTER; i++) {
102 final String name = "/up" + i + ".bin";
103 try (InputStream is = BGPParserTest.class.getResourceAsStream(name)) {
105 throw new IOException("Failed to get resource " + name);
107 final ByteArrayOutputStream bis = new ByteArrayOutputStream();
108 final byte[] data = new byte[MAX_SIZE];
110 while ((numRead = is.read(data, 0, data.length)) != -1) {
111 bis.write(data, 0, numRead);
115 inputBytes.add(bis.toByteArray());
119 updatesWithMultiplePath = HexDumpBGPFileParser.parseMessages(BGPParserTest.class.getResourceAsStream(
120 MULTIPATH_HEX_FILE));
121 constraint = mock(PeerSpecificParserConstraint.class);
122 mpSupport = mock(MultiPathSupport.class);
123 Mockito.doReturn(Optional.empty()).when(constraint).getPeerConstraint(Mockito.any());
124 Mockito.doReturn(Optional.of(mpSupport)).when(constraint).getPeerConstraint(MultiPathSupport.class);
125 Mockito.doReturn(true).when(mpSupport).isTableTypeSupported(Mockito.any());
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,
180 LENGTH_FIELD_LENGTH));
181 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
186 assertNull(message.getWithdrawnRoutes());
189 final List<AsNumber> asNumbers = new ArrayList<>();
190 asNumbers.add(new AsNumber(65002L));
191 final List<Segments> asPath = Lists.newArrayList();
192 asPath.add(new SegmentsBuilder().setAsSequence(asNumbers).build());
194 final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
195 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.2")).build()).build();
197 final List<Communities> comms = Lists.newArrayList();
198 comms.add((Communities) CommunityUtil.NO_EXPORT);
199 comms.add((Communities) CommunityUtil.NO_ADVERTISE);
200 comms.add((Communities) CommunityUtil.NO_EXPORT_SUBCONFED);
201 comms.add((Communities) CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF10));
203 final UpdateBuilder builder = new UpdateBuilder();
207 final List<Nlri> nlris = Lists.newArrayList();
208 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.2.0/24")).build());
209 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.1.0/24")).build());
210 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.0.0/24")).build());
212 assertEquals(nlris, message.getNlri());
214 builder.setNlri(nlris);
216 // check path attributes
218 final Attributes attrs = message.getAttributes();
220 final AttributesBuilder paBuilder = new AttributesBuilder();
222 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
223 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
225 paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
226 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
228 paBuilder.setCNextHop(nextHop);
229 assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
231 paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
232 assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
234 paBuilder.setAtomicAggregate(new AtomicAggregateBuilder().build());
235 assertEquals(paBuilder.getAtomicAggregate(), attrs.getAtomicAggregate());
237 paBuilder.setCommunities(comms);
238 assertEquals(paBuilder.getCommunities(), attrs.getCommunities());
240 paBuilder.setUnrecognizedAttributes(Collections.emptyList());
242 builder.setAttributes(paBuilder.build());
244 assertEquals(builder.build(), message);
246 final ByteBuf buffer = Unpooled.buffer();
247 BGPParserTest.updateParser.serializeMessage(message, buffer);
248 assertArrayEquals(inputBytes.get(0), ByteArray.readAllBytes(buffer));
252 * Tests more AS Numbers in AS_PATH, AGGREGATOR, ORIGIN.INCOMPLETE
254 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
255 * 00 4b <- length (75) - including header
257 * 00 00 <- withdrawn routes length
258 * 00 30 <- total path attribute length (48)
259 * 40 <- attribute flags
260 * 01 <- attribute type code (origin)
261 * 01 <- attribute length
262 * 02 <- Origin value (Incomplete)
263 * 40 <- attribute flags
264 * 02 <- attribute type code (as path)
265 * 10 <- attribute length
267 * 01 <- path segment count
268 * 00 00 00 1e <- path segment value (30)
270 * 02 <- path segment count
271 * 00 00 00 0a <- path segment value (10)
272 * 00 00 00 14 <- path segment value (20)
273 * 40 <- attribute flags
274 * 03 <- attribute type (Next hop)
275 * 04 <- attribute length
276 * 0a 00 00 09 <- value (10.0.0.9)
277 * 80 <- attribute flags
278 * 04 <- attribute type code (multi exit disc)
279 * 04 <- attribute length
280 * 00 00 00 00 <- value
281 * c0 <- attribute flags
282 * 07 <- attribute type (Aggregator)
283 * 08 <- attribute length
284 * 00 00 00 1e <- value (AS number = 30)
285 * 0a 00 00 09 <- value (IP address = 10.0.0.9)
288 * 15 ac 10 00 <- IPv4 Prefix (172.16.0.0 / 21)
291 public void testGetUpdateMessage3() throws Exception {
292 final byte[] body = ByteArray.cutBytes(inputBytes.get(2), MessageUtil.COMMON_HEADER_LENGTH);
293 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(2), MessageUtil.MARKER_LENGTH,
294 LENGTH_FIELD_LENGTH));
295 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
298 final UpdateBuilder builder = new UpdateBuilder();
301 final List<Nlri> nlris = Lists.newArrayList(new NlriBuilder().setPrefix(new Ipv4Prefix("172.16.0.0/21"))
303 builder.setNlri(nlris);
304 assertEquals(builder.getNlri(), message.getNlri());
307 assertNull(message.getWithdrawnRoutes());
310 final List<AsNumber> asNumbers = new ArrayList<>();
311 asNumbers.add(new AsNumber(30L));
312 final List<Segments> asPath = Lists.newArrayList();
313 asPath.add(new SegmentsBuilder().setAsSequence(asNumbers).build());
314 final List<AsNumber> asSet = Lists.newArrayList(new AsNumber(10L), new AsNumber(20L));
315 asPath.add(new SegmentsBuilder().setAsSet(asSet).build());
317 final Aggregator aggregator = new AggregatorBuilder().setAsNumber(new AsNumber((long) 30)).setNetworkAddress(
318 new Ipv4Address("10.0.0.9")).build();
319 final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
320 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.9")).build()).build();
322 // check path attributes
323 final Attributes attrs = message.getAttributes();
325 final AttributesBuilder paBuilder = new AttributesBuilder();
327 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Incomplete).build());
328 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
330 paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
331 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
333 paBuilder.setCNextHop(nextHop);
334 assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
336 paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
337 assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
339 paBuilder.setAggregator(aggregator);
340 assertEquals(paBuilder.getAggregator(), attrs.getAggregator());
341 paBuilder.setUnrecognizedAttributes(Collections.emptyList());
342 builder.setAttributes(paBuilder.build());
344 assertEquals(builder.build(), message);
346 final ByteBuf buffer = Unpooled.buffer();
347 BGPParserTest.updateParser.serializeMessage(message, buffer);
348 assertArrayEquals(inputBytes.get(2), ByteArray.readAllBytes(buffer));
352 * Tests empty AS_PATH, ORIGIN.EGP, LOCAL_PREF, EXTENDED_COMMUNITIES (Ipv4 Addr specific)
354 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
355 * 00 4A <- length (73) - including header
357 * 00 00 <- withdrawn routes length
358 * 00 27 <- total path attribute length (39)
359 * 40 <- attribute flags
360 * 01 <- attribute type code (Origin)
361 * 01 <- attribute length
362 * 01 <- Origin value (EGP)
363 * 40 <- attribute flags
364 * 02 <- attribute type code (As path)
365 * 00 <- attribute length
366 * 40 <- attribute flags
367 * 03 <- attribute type (Next hop)
368 * 04 <- attribute length
369 * 03 03 03 03 <- value (3.3.3.3)
370 * 80 <- attribute flags
371 * 04 <- attribute type code (Multi exit disc)
372 * 04 <- attribute length
373 * 00 00 00 00 <- value
374 * 40 <- attribute flags
375 * 05 <- attribute type (Local Pref)
376 * 04 <- attribute length
377 * 00 00 00 64 <- value (100)
378 * c0 <- attribute flags
379 * 10 <- attribute type (extended community)
380 * 08 <- attribute length
381 * 01 04 <- value (type - Ipv4 Address Specific Extended Community)
382 * c0 a8 01 00 <- value (global adm. 198.162.1.0)
383 * 12 34 <- value (local adm. 4660)
386 * 18 0a 1e 03 <- IPv4 Prefix (10.30.3.0 / 24)
387 * 18 0a 1e 02 <- IPv4 Prefix (10.30.2.0 / 24)
388 * 18 0a 1e 01 <- IPv4 Prefix (10.30.1.0 / 24)
391 public void testGetUpdateMessage4() throws Exception {
392 final byte[] body = ByteArray.cutBytes(inputBytes.get(3), MessageUtil.COMMON_HEADER_LENGTH);
393 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(3), MessageUtil.MARKER_LENGTH,
394 LENGTH_FIELD_LENGTH));
395 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
398 final UpdateBuilder builder = new UpdateBuilder();
401 assertNull(message.getWithdrawnRoutes());
404 final List<Nlri> nlris = Lists.newArrayList();
405 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("10.30.3.0/24")).build());
406 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("10.30.2.0/24")).build());
407 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("10.30.1.0/24")).build());
409 assertEquals(nlris, message.getNlri());
411 builder.setNlri(nlris);
414 final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
415 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("3.3.3.3")).build()).build();
417 final List<ExtendedCommunities> comms = Lists.newArrayList();
418 comms.add(new ExtendedCommunitiesBuilder().setTransitive(true).setExtendedCommunity(
419 new RouteTargetIpv4CaseBuilder().setRouteTargetIpv4(
420 new RouteTargetIpv4Builder().setGlobalAdministrator(
421 new Ipv4Address("192.168.1.0")).setLocalAdministrator(4660)
422 .build()).build()).build());
424 // check path attributes
425 final Attributes attrs = message.getAttributes();
427 final AttributesBuilder paBuilder = new AttributesBuilder();
429 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Egp).build());
430 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
432 paBuilder.setAsPath(new AsPathBuilder().setSegments(Collections.emptyList()).build());
433 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
435 paBuilder.setCNextHop(nextHop);
436 assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
438 paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
439 assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
441 paBuilder.setLocalPref(new LocalPrefBuilder().setPref(100L).build());
442 assertEquals(paBuilder.getLocalPref(), attrs.getLocalPref());
444 paBuilder.setExtendedCommunities(comms);
445 assertEquals(paBuilder.getExtendedCommunities(), attrs.getExtendedCommunities());
447 paBuilder.setUnrecognizedAttributes(Collections.emptyList());
449 builder.setAttributes(paBuilder.build());
450 assertEquals(builder.build(), message);
452 final ByteBuf buffer = Unpooled.buffer();
453 BGPParserTest.updateParser.serializeMessage(message, buffer);
454 assertArrayEquals(inputBytes.get(3), ByteArray.readAllBytes(buffer));
458 * Tests withdrawn routes.
460 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
461 * 00 1c <- length (28) - including header
463 * 00 05 <- withdrawn routes length (5)
464 * 1e ac 10 00 04 <- route (172.16.0.4)
465 * 00 00 <- total path attribute length
468 public void testGetUpdateMessage5() throws Exception {
469 final byte[] body = ByteArray.cutBytes(inputBytes.get(4), MessageUtil.COMMON_HEADER_LENGTH);
470 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(4), MessageUtil.MARKER_LENGTH,
471 LENGTH_FIELD_LENGTH));
472 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
476 final List<WithdrawnRoutes> withdrawnRoutes = Lists.newArrayList(new WithdrawnRoutesBuilder()
477 .setPrefix(new Ipv4Prefix("172.16.0.4/30")).build());
480 final Update expectedMessage = new UpdateBuilder().setWithdrawnRoutes(withdrawnRoutes).build();
482 assertEquals(expectedMessage.getWithdrawnRoutes(), message.getWithdrawnRoutes());
484 final ByteBuf buffer = Unpooled.buffer();
485 BGPParserTest.updateParser.serializeMessage(message, buffer);
486 assertArrayEquals(inputBytes.get(4), ByteArray.readAllBytes(buffer));
490 * End of Rib for Ipv4.
492 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
493 * 00 17 <- length (23) - including header
495 * 00 00 <- withdrawn routes length
496 * 00 00 <- total path attribute length
499 public void testEORIpv4() throws Exception {
500 final byte[] body = ByteArray.cutBytes(inputBytes.get(5), MessageUtil.COMMON_HEADER_LENGTH);
501 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(5), MessageUtil.MARKER_LENGTH,
502 LENGTH_FIELD_LENGTH));
503 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
506 assertEquals(new UpdateBuilder().build(), message);
508 final ByteBuf buffer = Unpooled.buffer();
509 BGPParserTest.updateParser.serializeMessage(message, buffer);
510 assertArrayEquals(inputBytes.get(5), ByteArray.readAllBytes(buffer));
514 * End of Rib for Ipv6 consists of empty MP_UNREACH_NLRI, with AFI 2 and SAFI 1
516 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
517 * 00 1d <- length (29) - including header
519 * 00 00 <- withdrawn routes length
520 * 00 06 <- total path attribute length
521 * 80 <- attribute flags
522 * 0f <- attribute type (15 - MP_UNREACH_NLRI)
523 * 03 <- attribute length
524 * 00 02 <- value (AFI 2: IPv6)
525 * 01 <- value (SAFI 1)
528 public void testEORIpv6() throws Exception {
529 final byte[] body = ByteArray.cutBytes(inputBytes.get(6), MessageUtil.COMMON_HEADER_LENGTH);
530 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(6), MessageUtil.MARKER_LENGTH,
531 LENGTH_FIELD_LENGTH));
532 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
535 final Class<? extends AddressFamily> afi = message.getAttributes().augmentation(Attributes2.class)
536 .getMpUnreachNlri().getAfi();
537 final Class<? extends SubsequentAddressFamily> safi = message.getAttributes().augmentation(Attributes2.class)
538 .getMpUnreachNlri().getSafi();
540 assertEquals(Ipv6AddressFamily.class, afi);
541 assertEquals(UnicastSubsequentAddressFamily.class, safi);
543 final ByteBuf buffer = Unpooled.buffer();
544 BGPParserTest.updateParser.serializeMessage(message, buffer);
545 assertArrayEquals(inputBytes.get(6), ByteArray.readAllBytes(buffer));
549 * End of Rib for Ipv6 consists of empty MP_UNREACH_NLRI, with AFI 2 and SAFI 1
551 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
552 * 00 1e <- length (29) - including header
554 * 00 00 <- withdrawn routes length
555 * 00 07 <- total path attribute length
556 * 90 <- attribute flags
557 * 0f <- attribute type (15 - MP_UNREACH_NLRI)
558 * 00 03 <- attribute length
559 * 00 02 <- value (AFI 2: IPv6)
560 * 01 <- value (SAFI 1)
563 public void testEORIpv6exLength() throws Exception {
564 final byte[] body = ByteArray.cutBytes(inputBytes.get(6), MessageUtil.COMMON_HEADER_LENGTH);
565 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(6), MessageUtil.MARKER_LENGTH,
566 LENGTH_FIELD_LENGTH));
567 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
570 final Class<? extends AddressFamily> afi = message.getAttributes().augmentation(Attributes2.class)
571 .getMpUnreachNlri().getAfi();
572 final Class<? extends SubsequentAddressFamily> safi = message.getAttributes().augmentation(Attributes2.class)
573 .getMpUnreachNlri().getSafi();
575 assertEquals(Ipv6AddressFamily.class, afi);
576 assertEquals(UnicastSubsequentAddressFamily.class, safi);
578 final ByteBuf buffer = Unpooled.buffer();
579 BGPParserTest.updateParser.serializeMessage(message, buffer);
580 assertArrayEquals(inputBytes.get(6), ByteArray.readAllBytes(buffer));
584 * Tests IPv4 NEXT_HOP, ATOMIC_AGGREGATE, COMMUNITY, NLRI with multiple paths.
586 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
587 * 00 60 <- length (96) - including header
589 * 00 00 <- withdrawn routes length
590 * 00 31 <- total path attribute length (49)
591 * 40 <- attribute flags
592 * 01 <- attribute type code (origin)
593 * 01 <- attribute length
594 * 00 <- Origin value (IGP)
595 * 40 <- attribute flags
596 * 02 <- attribute type code (as path)
597 * 06 <- attribute length
599 * 01 <- path segment count
600 * 00 00 fd ea <- path segment value (65002)
601 * 40 <- attribute flags
602 * 03 <- attribute type code (Next Hop)
603 * 04 <- attribute length
604 * 0a 00 00 02 <- value (10.0.0.2)
605 * 80 <- attribute flags
606 * 04 <- attribute type code (multi exit disc)
607 * 04 <- attribute length
608 * 00 00 00 00 <- value
609 * 40 <- attribute flags
610 * 06 <- attribute type code (atomic aggregate)
611 * 00 <- attribute length
612 * C0 <- attribute flags
613 * 08 <- attribute type code (community)
614 * 10 <- attribute length
615 * FF FF FF 01 <- value (NO_EXPORT)
616 * FF FF FF 02 <- value (NO_ADVERTISE)
617 * FF FF FF 03 <- value (NO_EXPORT_SUBCONFED)
618 * FF FF FF 10 <- unknown Community
621 * 00 00 00 01 <- path-id (1)
622 * 18 ac 11 02 <- IPv4 Prefix (172.17.1.0 / 24)
623 * 00 00 00 01 <- path-id (2)
624 * 18 ac 11 01 <- IPv4 Prefix (172.17.1.0 / 24)
625 * 00 00 00 01 <- path-id (1)
626 * 18 ac 11 00 <- IPv4 Prefix (172.17.0.0 / 24)
629 public void testUpdateMessageNlriAddPath() throws Exception {
630 final byte[] body = ByteArray.cutBytes(updatesWithMultiplePath.get(0), MessageUtil.COMMON_HEADER_LENGTH);
631 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(updatesWithMultiplePath.get(0),
632 MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
633 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
638 assertNull(message.getWithdrawnRoutes());
641 final List<AsNumber> asNumbers = new ArrayList<>();
642 asNumbers.add(new AsNumber(65002L));
643 final List<Segments> asPath = Lists.newArrayList();
644 asPath.add(new SegmentsBuilder().setAsSequence(asNumbers).build());
646 final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
647 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.2")).build()).build();
649 final List<Communities> comms = Lists.newArrayList();
650 comms.add((Communities) CommunityUtil.NO_EXPORT);
651 comms.add((Communities) CommunityUtil.NO_ADVERTISE);
652 comms.add((Communities) CommunityUtil.NO_EXPORT_SUBCONFED);
653 comms.add((Communities) CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF10));
655 final UpdateBuilder builder = new UpdateBuilder();
659 final List<Nlri> nlris = Lists.newArrayList();
660 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.1.0/24")).setPathId(new PathId(1L)).build());
661 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.1.0/24")).setPathId(new PathId(2L)).build());
662 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.0.0/24")).setPathId(new PathId(1L)).build());
664 assertEquals(nlris, message.getNlri());
666 builder.setNlri(nlris);
668 // check path attributes
670 final Attributes attrs = message.getAttributes();
672 final AttributesBuilder paBuilder = new AttributesBuilder();
674 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
675 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
677 paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
678 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
680 paBuilder.setCNextHop(nextHop);
681 assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
683 paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
684 assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
686 paBuilder.setAtomicAggregate(new AtomicAggregateBuilder().build());
687 assertEquals(paBuilder.getAtomicAggregate(), attrs.getAtomicAggregate());
689 paBuilder.setCommunities(comms);
690 assertEquals(paBuilder.getCommunities(), attrs.getCommunities());
692 paBuilder.setUnrecognizedAttributes(Collections.emptyList());
694 builder.setAttributes(paBuilder.build());
696 assertEquals(builder.build(), message);
698 final ByteBuf buffer = Unpooled.buffer();
699 BGPParserTest.updateParser.serializeMessage(message, buffer);
700 assertArrayEquals(updatesWithMultiplePath.get(0), ByteArray.readAllBytes(buffer));
704 * Tests withdrawn routes with multiple paths.
706 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
707 * 00 29 <- length (41) - including header
709 * 00 12 <- withdrawn routes length (18)
710 * 00 00 00 01 <- path-id (1)
711 * 1e ac 10 00 04 <- route (172.16.0.4)
712 * 00 00 00 02 <- path-id (2)
713 * 1e ac 10 00 04 <- route (172.16.0.4)
714 * 00 00 <- total path attribute length
717 public void testUpdateMessageWithdrawAddPath() throws Exception {
718 final byte[] body = ByteArray.cutBytes(updatesWithMultiplePath.get(1), MessageUtil.COMMON_HEADER_LENGTH);
719 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(updatesWithMultiplePath.get(1),
720 MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
721 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
725 final List<WithdrawnRoutes> withdrawnRoutes = Lists.newArrayList();
726 withdrawnRoutes.add(new WithdrawnRoutesBuilder().setPrefix(new Ipv4Prefix("172.16.0.4/30"))
727 .setPathId(new PathId(1L)).build());
728 withdrawnRoutes.add(new WithdrawnRoutesBuilder().setPrefix(new Ipv4Prefix("172.16.0.4/30"))
729 .setPathId(new PathId(2L)).build());
732 final Update expectedMessage = new UpdateBuilder().setWithdrawnRoutes(withdrawnRoutes).build();
734 assertEquals(expectedMessage.getWithdrawnRoutes(), message.getWithdrawnRoutes());
736 final ByteBuf buffer = Unpooled.buffer();
737 BGPParserTest.updateParser.serializeMessage(message, buffer);
738 assertArrayEquals(updatesWithMultiplePath.get(1), ByteArray.readAllBytes(buffer));