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.BGPDocumentedException;
30 import org.opendaylight.protocol.bgp.parser.impl.message.BGPUpdateMessageParser;
31 import org.opendaylight.protocol.bgp.parser.impl.message.update.CommunityUtil;
32 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
33 import org.opendaylight.protocol.bgp.parser.spi.MultiPathSupport;
34 import org.opendaylight.protocol.bgp.parser.spi.NlriRegistry;
35 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
36 import org.opendaylight.protocol.bgp.parser.spi.RevisedErrorHandlingSupport;
37 import org.opendaylight.protocol.bgp.parser.spi.pojo.PeerSpecificParserConstraintImpl;
38 import org.opendaylight.protocol.bgp.parser.spi.pojo.RevisedErrorHandlingSupportImpl;
39 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
40 import org.opendaylight.protocol.bgp.util.HexDumpBGPFileParser;
41 import org.opendaylight.protocol.util.ByteArray;
42 import org.opendaylight.protocol.util.NoopReferenceCache;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.UpdateBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.Aggregator;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.AggregatorBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.AsPathBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.AtomicAggregateBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.Communities;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.ExtendedCommunities;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.ExtendedCommunitiesBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.LocalPrefBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.MultiExitDiscBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.OriginBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.as.path.Segments;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.as.path.SegmentsBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.update.message.Nlri;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.update.message.NlriBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.update.message.WithdrawnRoutes;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.update.message.WithdrawnRoutesBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.BgpOrigin;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv6AddressFamily;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.SubsequentAddressFamily;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.RouteTargetIpv4CaseBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv4NextHopCase;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
76 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;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.route.target.ipv4.grouping.RouteTargetIpv4Builder;
79 public class BGPParserTest {
80 private static final List<byte[]> INPUT_BYTES = new ArrayList<>();
82 private static final int COUNTER = 8;
84 private static final int MAX_SIZE = 300;
86 private static BGPUpdateMessageParser updateParser;
88 private static final int LENGTH_FIELD_LENGTH = 2;
90 private static final String MULTIPATH_HEX_FILE = "/bgp-update-multipath.txt";
92 private static List<byte[]> updatesWithMultiplePath;
94 private static PeerSpecificParserConstraint mpConstraint;
96 private static MultiPathSupport mpSupport;
99 public static void setUp() throws Exception {
100 updateParser = new BGPUpdateMessageParser(ServiceLoaderBGPExtensionProviderContext.getSingletonInstance()
101 .getAttributeRegistry(), mock(NlriRegistry.class));
102 for (int i = 1; i <= COUNTER; i++) {
103 final String name = "/up" + i + ".bin";
104 try (InputStream is = BGPParserTest.class.getResourceAsStream(name)) {
106 throw new IOException("Failed to get resource " + name);
108 final ByteArrayOutputStream bis = new ByteArrayOutputStream();
109 final byte[] data = new byte[MAX_SIZE];
111 while ((numRead = is.read(data, 0, data.length)) != -1) {
112 bis.write(data, 0, numRead);
116 INPUT_BYTES.add(bis.toByteArray());
120 updatesWithMultiplePath = HexDumpBGPFileParser.parseMessages(BGPParserTest.class.getResourceAsStream(
121 MULTIPATH_HEX_FILE));
122 mpConstraint = mock(PeerSpecificParserConstraint.class);
123 mpSupport = mock(MultiPathSupport.class);
124 Mockito.doReturn(Optional.empty()).when(mpConstraint).getPeerConstraint(Mockito.any());
125 Mockito.doReturn(Optional.of(mpSupport)).when(mpConstraint).getPeerConstraint(MultiPathSupport.class);
126 Mockito.doReturn(true).when(mpSupport).isTableTypeSupported(Mockito.any());
130 public void testResource() {
131 assertNotNull(INPUT_BYTES);
135 * Tests IPv4 NEXT_HOP, ATOMIC_AGGREGATE, COMMUNITY, NLRI
137 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
138 * 00 54 <- length (84) - including header
140 * 00 00 <- withdrawn routes length
141 * 00 31 <- total path attribute length (49)
142 * 40 <- attribute flags
143 * 01 <- attribute type code (origin)
144 * 01 <- attribute length
145 * 00 <- Origin value (IGP)
146 * 40 <- attribute flags
147 * 02 <- attribute type code (as path)
148 * 06 <- attribute length
150 * 01 <- path segment count
151 * 00 00 fd ea <- path segment value (65002)
152 * 40 <- attribute flags
153 * 03 <- attribute type code (Next Hop)
154 * 04 <- attribute length
155 * 10 00 00 02 <- value (10.0.0.2)
156 * 80 <- attribute flags
157 * 04 <- attribute type code (multi exit disc)
158 * 04 <- attribute length
159 * 00 00 00 00 <- value
160 * 60 <- attribute flags
161 * 06 <- attribute type code (atomic aggregate)
162 * 00 <- attribute length
163 * 40 <- attribute flags
164 * 08 <- attribute type code (community)
165 * 10 <- attribute length FF FF FF
166 * 01 <- value (NO_EXPORT)
167 * FF FF FF 02 <- value (NO_ADVERTISE)
168 * FF FF FF 03 <- value (NO_EXPORT_SUBCONFED)
169 * FF FF FF 10 <- unknown Community
172 * 18 ac 11 02 <- IPv4 Prefix (172.17.2.0 / 24)
173 * 18 ac 11 01 <- IPv4 Prefix (172.17.1.0 / 24)
174 * 18 ac 11 00 <- IPv4 Prefix (172.17.0.0 / 24)
177 public void testGetUpdateMessage1() throws Exception {
179 final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(0), MessageUtil.COMMON_HEADER_LENGTH);
180 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(0), MessageUtil.MARKER_LENGTH,
181 LENGTH_FIELD_LENGTH));
182 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
187 assertNull(message.getWithdrawnRoutes());
190 final List<AsNumber> asNumbers = new ArrayList<>();
191 asNumbers.add(new AsNumber(65002L));
192 final List<Segments> asPath = Lists.newArrayList();
193 asPath.add(new SegmentsBuilder().setAsSequence(asNumbers).build());
195 final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
196 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.2")).build()).build();
198 final List<Communities> comms = Lists.newArrayList();
199 comms.add((Communities) CommunityUtil.NO_EXPORT);
200 comms.add((Communities) CommunityUtil.NO_ADVERTISE);
201 comms.add((Communities) CommunityUtil.NO_EXPORT_SUBCONFED);
202 comms.add((Communities) CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF10));
204 final UpdateBuilder builder = new UpdateBuilder();
208 final List<Nlri> nlris = Lists.newArrayList();
209 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.2.0/24")).build());
210 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.1.0/24")).build());
211 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.0.0/24")).build());
213 assertEquals(nlris, message.getNlri());
215 builder.setNlri(nlris);
217 // check path attributes
219 final Attributes attrs = message.getAttributes();
221 final AttributesBuilder paBuilder = new AttributesBuilder();
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.emptyList());
243 builder.setAttributes(paBuilder.build());
245 assertEquals(builder.build(), message);
247 final ByteBuf buffer = Unpooled.buffer();
248 BGPParserTest.updateParser.serializeMessage(message, buffer);
249 assertArrayEquals(INPUT_BYTES.get(0), ByteArray.readAllBytes(buffer));
253 * Tests more AS Numbers in AS_PATH, AGGREGATOR, ORIGIN.INCOMPLETE
255 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
256 * 00 4b <- length (75) - including header
258 * 00 00 <- withdrawn routes length
259 * 00 30 <- total path attribute length (48)
260 * 40 <- attribute flags
261 * 01 <- attribute type code (origin)
262 * 01 <- attribute length
263 * 02 <- Origin value (Incomplete)
264 * 40 <- attribute flags
265 * 02 <- attribute type code (as path)
266 * 10 <- attribute length
268 * 01 <- path segment count
269 * 00 00 00 1e <- path segment value (30)
271 * 02 <- path segment count
272 * 00 00 00 0a <- path segment value (10)
273 * 00 00 00 14 <- path segment value (20)
274 * 40 <- attribute flags
275 * 03 <- attribute type (Next hop)
276 * 04 <- attribute length
277 * 0a 00 00 09 <- value (10.0.0.9)
278 * 80 <- attribute flags
279 * 04 <- attribute type code (multi exit disc)
280 * 04 <- attribute length
281 * 00 00 00 00 <- value
282 * c0 <- attribute flags
283 * 07 <- attribute type (Aggregator)
284 * 08 <- attribute length
285 * 00 00 00 1e <- value (AS number = 30)
286 * 0a 00 00 09 <- value (IP address = 10.0.0.9)
289 * 15 ac 10 00 <- IPv4 Prefix (172.16.0.0 / 21)
292 public void testGetUpdateMessage3() throws Exception {
293 final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(2), MessageUtil.COMMON_HEADER_LENGTH);
294 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(2), MessageUtil.MARKER_LENGTH,
295 LENGTH_FIELD_LENGTH));
296 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
299 final UpdateBuilder builder = new UpdateBuilder();
302 final List<Nlri> nlris = Lists.newArrayList(new NlriBuilder().setPrefix(new Ipv4Prefix("172.16.0.0/21"))
304 builder.setNlri(nlris);
305 assertEquals(builder.getNlri(), message.getNlri());
308 assertNull(message.getWithdrawnRoutes());
311 final List<AsNumber> asNumbers = new ArrayList<>();
312 asNumbers.add(new AsNumber(30L));
313 final List<Segments> asPath = Lists.newArrayList();
314 asPath.add(new SegmentsBuilder().setAsSequence(asNumbers).build());
315 final List<AsNumber> asSet = Lists.newArrayList(new AsNumber(10L), new AsNumber(20L));
316 asPath.add(new SegmentsBuilder().setAsSet(asSet).build());
318 final Aggregator aggregator = new AggregatorBuilder().setAsNumber(new AsNumber((long) 30)).setNetworkAddress(
319 new Ipv4Address("10.0.0.9")).build();
320 final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
321 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.9")).build()).build();
323 // check path attributes
324 final Attributes attrs = message.getAttributes();
326 final AttributesBuilder paBuilder = new AttributesBuilder();
328 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Incomplete).build());
329 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
331 paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
332 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
334 paBuilder.setCNextHop(nextHop);
335 assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
337 paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
338 assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
340 paBuilder.setAggregator(aggregator);
341 assertEquals(paBuilder.getAggregator(), attrs.getAggregator());
342 paBuilder.setUnrecognizedAttributes(Collections.emptyList());
343 builder.setAttributes(paBuilder.build());
345 assertEquals(builder.build(), message);
347 final ByteBuf buffer = Unpooled.buffer();
348 BGPParserTest.updateParser.serializeMessage(message, buffer);
349 assertArrayEquals(INPUT_BYTES.get(2), ByteArray.readAllBytes(buffer));
353 * Tests empty AS_PATH, ORIGIN.EGP, LOCAL_PREF, EXTENDED_COMMUNITIES (Ipv4 Addr specific)
355 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
356 * 00 4A <- length (73) - including header
358 * 00 00 <- withdrawn routes length
359 * 00 27 <- total path attribute length (39)
360 * 40 <- attribute flags
361 * 01 <- attribute type code (Origin)
362 * 01 <- attribute length
363 * 01 <- Origin value (EGP)
364 * 40 <- attribute flags
365 * 02 <- attribute type code (As path)
366 * 00 <- attribute length
367 * 40 <- attribute flags
368 * 03 <- attribute type (Next hop)
369 * 04 <- attribute length
370 * 03 03 03 03 <- value (3.3.3.3)
371 * 80 <- attribute flags
372 * 04 <- attribute type code (Multi exit disc)
373 * 04 <- attribute length
374 * 00 00 00 00 <- value
375 * 40 <- attribute flags
376 * 05 <- attribute type (Local Pref)
377 * 04 <- attribute length
378 * 00 00 00 64 <- value (100)
379 * c0 <- attribute flags
380 * 10 <- attribute type (extended community)
381 * 08 <- attribute length
382 * 01 04 <- value (type - Ipv4 Address Specific Extended Community)
383 * c0 a8 01 00 <- value (global adm. 198.162.1.0)
384 * 12 34 <- value (local adm. 4660)
387 * 18 0a 1e 03 <- IPv4 Prefix (10.30.3.0 / 24)
388 * 18 0a 1e 02 <- IPv4 Prefix (10.30.2.0 / 24)
389 * 18 0a 1e 01 <- IPv4 Prefix (10.30.1.0 / 24)
392 public void testGetUpdateMessage4() throws Exception {
393 final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(3), MessageUtil.COMMON_HEADER_LENGTH);
394 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(3), MessageUtil.MARKER_LENGTH,
395 LENGTH_FIELD_LENGTH));
396 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
399 final UpdateBuilder builder = new UpdateBuilder();
402 assertNull(message.getWithdrawnRoutes());
405 final List<Nlri> nlris = Lists.newArrayList();
406 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("10.30.3.0/24")).build());
407 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("10.30.2.0/24")).build());
408 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("10.30.1.0/24")).build());
410 assertEquals(nlris, message.getNlri());
412 builder.setNlri(nlris);
415 final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
416 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("3.3.3.3")).build()).build();
418 final List<ExtendedCommunities> comms = Lists.newArrayList();
419 comms.add(new ExtendedCommunitiesBuilder().setTransitive(true).setExtendedCommunity(
420 new RouteTargetIpv4CaseBuilder().setRouteTargetIpv4(
421 new RouteTargetIpv4Builder().setGlobalAdministrator(
422 new Ipv4Address("192.168.1.0")).setLocalAdministrator(4660)
423 .build()).build()).build());
425 // check path attributes
426 final Attributes attrs = message.getAttributes();
428 final AttributesBuilder paBuilder = new AttributesBuilder();
430 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Egp).build());
431 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
433 paBuilder.setAsPath(new AsPathBuilder().setSegments(Collections.emptyList()).build());
434 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
436 paBuilder.setCNextHop(nextHop);
437 assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
439 paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
440 assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
442 paBuilder.setLocalPref(new LocalPrefBuilder().setPref(100L).build());
443 assertEquals(paBuilder.getLocalPref(), attrs.getLocalPref());
445 paBuilder.setExtendedCommunities(comms);
446 assertEquals(paBuilder.getExtendedCommunities(), attrs.getExtendedCommunities());
448 paBuilder.setUnrecognizedAttributes(Collections.emptyList());
450 builder.setAttributes(paBuilder.build());
451 assertEquals(builder.build(), message);
453 final ByteBuf buffer = Unpooled.buffer();
454 BGPParserTest.updateParser.serializeMessage(message, buffer);
455 assertArrayEquals(INPUT_BYTES.get(3), ByteArray.readAllBytes(buffer));
459 * Tests withdrawn routes.
461 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
462 * 00 1c <- length (28) - including header
464 * 00 05 <- withdrawn routes length (5)
465 * 1e ac 10 00 04 <- route (172.16.0.4)
466 * 00 00 <- total path attribute length
469 public void testGetUpdateMessage5() throws Exception {
470 final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(4), MessageUtil.COMMON_HEADER_LENGTH);
471 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(4), MessageUtil.MARKER_LENGTH,
472 LENGTH_FIELD_LENGTH));
473 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
477 final List<WithdrawnRoutes> withdrawnRoutes = Lists.newArrayList(new WithdrawnRoutesBuilder()
478 .setPrefix(new Ipv4Prefix("172.16.0.4/30")).build());
481 final Update expectedMessage = new UpdateBuilder().setWithdrawnRoutes(withdrawnRoutes).build();
483 assertEquals(expectedMessage.getWithdrawnRoutes(), message.getWithdrawnRoutes());
485 final ByteBuf buffer = Unpooled.buffer();
486 BGPParserTest.updateParser.serializeMessage(message, buffer);
487 assertArrayEquals(INPUT_BYTES.get(4), ByteArray.readAllBytes(buffer));
491 * End of Rib for Ipv4.
493 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
494 * 00 17 <- length (23) - including header
496 * 00 00 <- withdrawn routes length
497 * 00 00 <- total path attribute length
500 public void testEORIpv4() throws Exception {
501 final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(5), MessageUtil.COMMON_HEADER_LENGTH);
502 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(5), MessageUtil.MARKER_LENGTH,
503 LENGTH_FIELD_LENGTH));
504 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
507 assertEquals(new UpdateBuilder().build(), message);
509 final ByteBuf buffer = Unpooled.buffer();
510 BGPParserTest.updateParser.serializeMessage(message, buffer);
511 assertArrayEquals(INPUT_BYTES.get(5), ByteArray.readAllBytes(buffer));
515 * End of Rib for Ipv6 consists of empty MP_UNREACH_NLRI, with AFI 2 and SAFI 1
517 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
518 * 00 1d <- length (29) - including header
520 * 00 00 <- withdrawn routes length
521 * 00 06 <- total path attribute length
522 * 80 <- attribute flags
523 * 0f <- attribute type (15 - MP_UNREACH_NLRI)
524 * 03 <- attribute length
525 * 00 02 <- value (AFI 2: IPv6)
526 * 01 <- value (SAFI 1)
529 public void testEORIpv6() throws Exception {
530 final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(6), MessageUtil.COMMON_HEADER_LENGTH);
531 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(6), MessageUtil.MARKER_LENGTH,
532 LENGTH_FIELD_LENGTH));
533 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
536 final Class<? extends AddressFamily> afi = message.getAttributes().augmentation(Attributes2.class)
537 .getMpUnreachNlri().getAfi();
538 final Class<? extends SubsequentAddressFamily> safi = message.getAttributes().augmentation(Attributes2.class)
539 .getMpUnreachNlri().getSafi();
541 assertEquals(Ipv6AddressFamily.class, afi);
542 assertEquals(UnicastSubsequentAddressFamily.class, safi);
544 final ByteBuf buffer = Unpooled.buffer();
545 BGPParserTest.updateParser.serializeMessage(message, buffer);
546 assertArrayEquals(INPUT_BYTES.get(6), ByteArray.readAllBytes(buffer));
550 * End of Rib for Ipv6 consists of empty MP_UNREACH_NLRI, with AFI 2 and SAFI 1
552 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
553 * 00 1e <- length (29) - including header
555 * 00 00 <- withdrawn routes length
556 * 00 07 <- total path attribute length
557 * 90 <- attribute flags
558 * 0f <- attribute type (15 - MP_UNREACH_NLRI)
559 * 00 03 <- attribute length
560 * 00 02 <- value (AFI 2: IPv6)
561 * 01 <- value (SAFI 1)
564 public void testEORIpv6exLength() throws Exception {
565 final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(6), MessageUtil.COMMON_HEADER_LENGTH);
566 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(6), MessageUtil.MARKER_LENGTH,
567 LENGTH_FIELD_LENGTH));
568 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
571 final Class<? extends AddressFamily> afi = message.getAttributes().augmentation(Attributes2.class)
572 .getMpUnreachNlri().getAfi();
573 final Class<? extends SubsequentAddressFamily> safi = message.getAttributes().augmentation(Attributes2.class)
574 .getMpUnreachNlri().getSafi();
576 assertEquals(Ipv6AddressFamily.class, afi);
577 assertEquals(UnicastSubsequentAddressFamily.class, safi);
579 final ByteBuf buffer = Unpooled.buffer();
580 BGPParserTest.updateParser.serializeMessage(message, buffer);
581 assertArrayEquals(INPUT_BYTES.get(6), ByteArray.readAllBytes(buffer));
585 * Tests IPv4 NEXT_HOP, ATOMIC_AGGREGATE, COMMUNITY, NLRI with multiple paths.
587 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
588 * 00 60 <- length (96) - including header
590 * 00 00 <- withdrawn routes length
591 * 00 31 <- total path attribute length (49)
592 * 40 <- attribute flags
593 * 01 <- attribute type code (origin)
594 * 01 <- attribute length
595 * 00 <- Origin value (IGP)
596 * 40 <- attribute flags
597 * 02 <- attribute type code (as path)
598 * 06 <- attribute length
600 * 01 <- path segment count
601 * 00 00 fd ea <- path segment value (65002)
602 * 40 <- attribute flags
603 * 03 <- attribute type code (Next Hop)
604 * 04 <- attribute length
605 * 0a 00 00 02 <- value (10.0.0.2)
606 * 80 <- attribute flags
607 * 04 <- attribute type code (multi exit disc)
608 * 04 <- attribute length
609 * 00 00 00 00 <- value
610 * 40 <- attribute flags
611 * 06 <- attribute type code (atomic aggregate)
612 * 00 <- attribute length
613 * C0 <- attribute flags
614 * 08 <- attribute type code (community)
615 * 10 <- attribute length
616 * FF FF FF 01 <- value (NO_EXPORT)
617 * FF FF FF 02 <- value (NO_ADVERTISE)
618 * FF FF FF 03 <- value (NO_EXPORT_SUBCONFED)
619 * FF FF FF 10 <- unknown Community
622 * 00 00 00 01 <- path-id (1)
623 * 18 ac 11 02 <- IPv4 Prefix (172.17.1.0 / 24)
624 * 00 00 00 01 <- path-id (2)
625 * 18 ac 11 01 <- IPv4 Prefix (172.17.1.0 / 24)
626 * 00 00 00 01 <- path-id (1)
627 * 18 ac 11 00 <- IPv4 Prefix (172.17.0.0 / 24)
630 public void testUpdateMessageNlriAddPath() throws Exception {
631 final byte[] body = ByteArray.cutBytes(updatesWithMultiplePath.get(0), MessageUtil.COMMON_HEADER_LENGTH);
632 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(updatesWithMultiplePath.get(0),
633 MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
634 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
639 assertNull(message.getWithdrawnRoutes());
642 final List<AsNumber> asNumbers = new ArrayList<>();
643 asNumbers.add(new AsNumber(65002L));
644 final List<Segments> asPath = Lists.newArrayList();
645 asPath.add(new SegmentsBuilder().setAsSequence(asNumbers).build());
647 final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
648 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.2")).build()).build();
650 final List<Communities> comms = Lists.newArrayList();
651 comms.add((Communities) CommunityUtil.NO_EXPORT);
652 comms.add((Communities) CommunityUtil.NO_ADVERTISE);
653 comms.add((Communities) CommunityUtil.NO_EXPORT_SUBCONFED);
654 comms.add((Communities) CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF10));
656 final UpdateBuilder builder = new UpdateBuilder();
660 final List<Nlri> nlris = Lists.newArrayList();
661 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.1.0/24")).setPathId(new PathId(1L)).build());
662 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.1.0/24")).setPathId(new PathId(2L)).build());
663 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.0.0/24")).setPathId(new PathId(1L)).build());
665 assertEquals(nlris, message.getNlri());
667 builder.setNlri(nlris);
669 // check path attributes
671 final Attributes attrs = message.getAttributes();
673 final AttributesBuilder paBuilder = new AttributesBuilder();
675 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
676 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
678 paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
679 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
681 paBuilder.setCNextHop(nextHop);
682 assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
684 paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
685 assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
687 paBuilder.setAtomicAggregate(new AtomicAggregateBuilder().build());
688 assertEquals(paBuilder.getAtomicAggregate(), attrs.getAtomicAggregate());
690 paBuilder.setCommunities(comms);
691 assertEquals(paBuilder.getCommunities(), attrs.getCommunities());
693 paBuilder.setUnrecognizedAttributes(Collections.emptyList());
695 builder.setAttributes(paBuilder.build());
697 assertEquals(builder.build(), message);
699 final ByteBuf buffer = Unpooled.buffer();
700 BGPParserTest.updateParser.serializeMessage(message, buffer);
701 assertArrayEquals(updatesWithMultiplePath.get(0), ByteArray.readAllBytes(buffer));
705 * Tests withdrawn routes with multiple paths.
707 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
708 * 00 29 <- length (41) - including header
710 * 00 12 <- withdrawn routes length (18)
711 * 00 00 00 01 <- path-id (1)
712 * 1e ac 10 00 04 <- route (172.16.0.4)
713 * 00 00 00 02 <- path-id (2)
714 * 1e ac 10 00 04 <- route (172.16.0.4)
715 * 00 00 <- total path attribute length
719 public void testUpdateMessageWithdrawAddPath() throws Exception {
720 final byte[] body = ByteArray.cutBytes(updatesWithMultiplePath.get(1), MessageUtil.COMMON_HEADER_LENGTH);
721 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(updatesWithMultiplePath.get(1),
722 MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
723 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
727 final List<WithdrawnRoutes> withdrawnRoutes = Lists.newArrayList();
728 withdrawnRoutes.add(new WithdrawnRoutesBuilder().setPrefix(new Ipv4Prefix("172.16.0.4/30"))
729 .setPathId(new PathId(1L)).build());
730 withdrawnRoutes.add(new WithdrawnRoutesBuilder().setPrefix(new Ipv4Prefix("172.16.0.4/30"))
731 .setPathId(new PathId(2L)).build());
734 final Update expectedMessage = new UpdateBuilder().setWithdrawnRoutes(withdrawnRoutes).build();
736 assertEquals(expectedMessage.getWithdrawnRoutes(), message.getWithdrawnRoutes());
738 final ByteBuf buffer = Unpooled.buffer();
739 BGPParserTest.updateParser.serializeMessage(message, buffer);
740 assertArrayEquals(updatesWithMultiplePath.get(1), ByteArray.readAllBytes(buffer));
744 * Tests withdrawn routes with malformed attribute.
746 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
747 * 00 35 <- length (53) - including header
749 * 00 00 <- withdrawn routes length
750 * 00 1a <- total path attribute length (26)
751 * 40 <- attribute flags
752 * 01 <- attribute type code (origin)
753 * 02 <- WRONG attribute length
754 * 00 <- Origin value (IGP)
755 * 40 <- attribute flags
756 * 03 <- attribute type code (Next Hop)
757 * 04 <- attribute length
758 * 0a 00 00 02 <- value (10.0.0.2)
759 * 40 <- attribute flags
760 * 0e <- attribute type code (MP_REACH)
761 * 00 01 <- AFI (Ipv4)
762 * 01 <- SAFI (Unicast)
763 * 04 <- next hop length
764 * ff ff ff ff <- next hop
767 * 0a 00 01 <- prefix (10.0.1.0)
770 * 0a 00 02 <- prefix (10.0.2.0)
773 public void testUpdateMessageWithMalformedAttribute() throws BGPDocumentedException {
774 final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(7), MessageUtil.COMMON_HEADER_LENGTH);
775 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(6), MessageUtil.MARKER_LENGTH,
776 LENGTH_FIELD_LENGTH));
777 final PeerSpecificParserConstraintImpl constraint = new PeerSpecificParserConstraintImpl();
778 constraint.addPeerConstraint(RevisedErrorHandlingSupport.class,
779 RevisedErrorHandlingSupportImpl.forExternalPeer());
780 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
782 assertNotNull(message);
783 assertNull(message.getNlri());
784 final List<WithdrawnRoutes> withdrawnRoutes = message.getWithdrawnRoutes();
785 assertNotNull(withdrawnRoutes);
786 assertEquals(1, withdrawnRoutes.size());