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;
78 import org.opendaylight.yangtools.yang.common.Uint16;
79 import org.opendaylight.yangtools.yang.common.Uint32;
81 public class BGPParserTest {
82 private static final List<byte[]> INPUT_BYTES = new ArrayList<>();
84 private static final int COUNTER = 8;
86 private static final int MAX_SIZE = 300;
88 private static BGPUpdateMessageParser updateParser;
90 private static final int LENGTH_FIELD_LENGTH = 2;
92 private static final String MULTIPATH_HEX_FILE = "/bgp-update-multipath.txt";
94 private static List<byte[]> updatesWithMultiplePath;
96 private static PeerSpecificParserConstraint mpConstraint;
98 private static MultiPathSupport mpSupport;
101 public static void setUp() throws Exception {
102 updateParser = new BGPUpdateMessageParser(ServiceLoaderBGPExtensionProviderContext.getSingletonInstance()
103 .getAttributeRegistry(), mock(NlriRegistry.class));
104 for (int i = 1; i <= COUNTER; i++) {
105 final String name = "/up" + i + ".bin";
106 try (InputStream is = BGPParserTest.class.getResourceAsStream(name)) {
108 throw new IOException("Failed to get resource " + name);
110 final ByteArrayOutputStream bis = new ByteArrayOutputStream();
111 final byte[] data = new byte[MAX_SIZE];
113 while ((numRead = is.read(data, 0, data.length)) != -1) {
114 bis.write(data, 0, numRead);
118 INPUT_BYTES.add(bis.toByteArray());
122 updatesWithMultiplePath = HexDumpBGPFileParser.parseMessages(BGPParserTest.class.getResourceAsStream(
123 MULTIPATH_HEX_FILE));
124 mpConstraint = mock(PeerSpecificParserConstraint.class);
125 mpSupport = mock(MultiPathSupport.class);
126 Mockito.doReturn(Optional.empty()).when(mpConstraint).getPeerConstraint(Mockito.any());
127 Mockito.doReturn(Optional.of(mpSupport)).when(mpConstraint).getPeerConstraint(MultiPathSupport.class);
128 Mockito.doReturn(true).when(mpSupport).isTableTypeSupported(Mockito.any());
132 public void testResource() {
133 assertNotNull(INPUT_BYTES);
137 * Tests IPv4 NEXT_HOP, ATOMIC_AGGREGATE, COMMUNITY, NLRI
139 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
140 * 00 54 <- length (84) - including header
142 * 00 00 <- withdrawn routes length
143 * 00 31 <- total path attribute length (49)
144 * 40 <- attribute flags
145 * 01 <- attribute type code (origin)
146 * 01 <- attribute length
147 * 00 <- Origin value (IGP)
148 * 40 <- attribute flags
149 * 02 <- attribute type code (as path)
150 * 06 <- attribute length
152 * 01 <- path segment count
153 * 00 00 fd ea <- path segment value (65002)
154 * 40 <- attribute flags
155 * 03 <- attribute type code (Next Hop)
156 * 04 <- attribute length
157 * 10 00 00 02 <- value (10.0.0.2)
158 * 80 <- attribute flags
159 * 04 <- attribute type code (multi exit disc)
160 * 04 <- attribute length
161 * 00 00 00 00 <- value
162 * 60 <- attribute flags
163 * 06 <- attribute type code (atomic aggregate)
164 * 00 <- attribute length
165 * 40 <- attribute flags
166 * 08 <- attribute type code (community)
167 * 10 <- attribute length FF FF FF
168 * 01 <- value (NO_EXPORT)
169 * FF FF FF 02 <- value (NO_ADVERTISE)
170 * FF FF FF 03 <- value (NO_EXPORT_SUBCONFED)
171 * FF FF FF 10 <- unknown Community
174 * 18 ac 11 02 <- IPv4 Prefix (172.17.2.0 / 24)
175 * 18 ac 11 01 <- IPv4 Prefix (172.17.1.0 / 24)
176 * 18 ac 11 00 <- IPv4 Prefix (172.17.0.0 / 24)
179 public void testGetUpdateMessage1() throws Exception {
181 final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(0), MessageUtil.COMMON_HEADER_LENGTH);
182 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(0), MessageUtil.MARKER_LENGTH,
183 LENGTH_FIELD_LENGTH));
184 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
189 assertNull(message.getWithdrawnRoutes());
192 final List<AsNumber> asNumbers = new ArrayList<>();
193 asNumbers.add(new AsNumber(Uint32.valueOf(65002)));
194 final List<Segments> asPath = new ArrayList<>();
195 asPath.add(new SegmentsBuilder().setAsSequence(asNumbers).build());
197 final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
198 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.2")).build()).build();
200 final List<Communities> comms = new ArrayList<>();
201 comms.add((Communities) CommunityUtil.NO_EXPORT);
202 comms.add((Communities) CommunityUtil.NO_ADVERTISE);
203 comms.add((Communities) CommunityUtil.NO_EXPORT_SUBCONFED);
204 comms.add((Communities) CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF10));
206 final UpdateBuilder builder = new UpdateBuilder();
210 final List<Nlri> nlris = new ArrayList<>();
211 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.2.0/24")).build());
212 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.1.0/24")).build());
213 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.0.0/24")).build());
215 assertEquals(nlris, message.getNlri());
217 builder.setNlri(nlris);
219 // check path attributes
221 final Attributes attrs = message.getAttributes();
223 final AttributesBuilder paBuilder = new AttributesBuilder();
225 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
226 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
228 paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
229 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
231 paBuilder.setCNextHop(nextHop);
232 assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
234 paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed(Uint32.ZERO).build());
235 assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
237 paBuilder.setAtomicAggregate(new AtomicAggregateBuilder().build());
238 assertEquals(paBuilder.getAtomicAggregate(), attrs.getAtomicAggregate());
240 paBuilder.setCommunities(comms);
241 assertEquals(paBuilder.getCommunities(), attrs.getCommunities());
243 paBuilder.setUnrecognizedAttributes(Collections.emptyList());
245 builder.setAttributes(paBuilder.build());
247 assertEquals(builder.build(), message);
249 final ByteBuf buffer = Unpooled.buffer();
250 BGPParserTest.updateParser.serializeMessage(message, buffer);
251 assertArrayEquals(INPUT_BYTES.get(0), ByteArray.readAllBytes(buffer));
255 * Tests more AS Numbers in AS_PATH, AGGREGATOR, ORIGIN.INCOMPLETE
257 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
258 * 00 4b <- length (75) - including header
260 * 00 00 <- withdrawn routes length
261 * 00 30 <- total path attribute length (48)
262 * 40 <- attribute flags
263 * 01 <- attribute type code (origin)
264 * 01 <- attribute length
265 * 02 <- Origin value (Incomplete)
266 * 40 <- attribute flags
267 * 02 <- attribute type code (as path)
268 * 10 <- attribute length
270 * 01 <- path segment count
271 * 00 00 00 1e <- path segment value (30)
273 * 02 <- path segment count
274 * 00 00 00 0a <- path segment value (10)
275 * 00 00 00 14 <- path segment value (20)
276 * 40 <- attribute flags
277 * 03 <- attribute type (Next hop)
278 * 04 <- attribute length
279 * 0a 00 00 09 <- value (10.0.0.9)
280 * 80 <- attribute flags
281 * 04 <- attribute type code (multi exit disc)
282 * 04 <- attribute length
283 * 00 00 00 00 <- value
284 * c0 <- attribute flags
285 * 07 <- attribute type (Aggregator)
286 * 08 <- attribute length
287 * 00 00 00 1e <- value (AS number = 30)
288 * 0a 00 00 09 <- value (IP address = 10.0.0.9)
291 * 15 ac 10 00 <- IPv4 Prefix (172.16.0.0 / 21)
294 public void testGetUpdateMessage3() throws Exception {
295 final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(2), MessageUtil.COMMON_HEADER_LENGTH);
296 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(2), MessageUtil.MARKER_LENGTH,
297 LENGTH_FIELD_LENGTH));
298 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
301 final UpdateBuilder builder = new UpdateBuilder();
304 final List<Nlri> nlris = Lists.newArrayList(new NlriBuilder().setPrefix(new Ipv4Prefix("172.16.0.0/21"))
306 builder.setNlri(nlris);
307 assertEquals(builder.getNlri(), message.getNlri());
310 assertNull(message.getWithdrawnRoutes());
313 final List<AsNumber> asNumbers = new ArrayList<>();
314 asNumbers.add(new AsNumber(Uint32.valueOf(30)));
315 final List<Segments> asPath = new ArrayList<>();
316 asPath.add(new SegmentsBuilder().setAsSequence(asNumbers).build());
317 final List<AsNumber> asSet = Lists.newArrayList(new AsNumber(Uint32.valueOf(10)),
318 new AsNumber(Uint32.valueOf(20)));
319 asPath.add(new SegmentsBuilder().setAsSet(asSet).build());
321 final Aggregator aggregator = new AggregatorBuilder().setAsNumber(new AsNumber(Uint32.valueOf(30)))
322 .setNetworkAddress(new Ipv4Address("10.0.0.9")).build();
323 final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
324 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.9")).build()).build();
326 // check path attributes
327 final Attributes attrs = message.getAttributes();
329 final AttributesBuilder paBuilder = new AttributesBuilder();
331 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Incomplete).build());
332 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
334 paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
335 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
337 paBuilder.setCNextHop(nextHop);
338 assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
340 paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed(Uint32.ZERO).build());
341 assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
343 paBuilder.setAggregator(aggregator);
344 assertEquals(paBuilder.getAggregator(), attrs.getAggregator());
345 paBuilder.setUnrecognizedAttributes(Collections.emptyList());
346 builder.setAttributes(paBuilder.build());
348 assertEquals(builder.build(), message);
350 final ByteBuf buffer = Unpooled.buffer();
351 BGPParserTest.updateParser.serializeMessage(message, buffer);
352 assertArrayEquals(INPUT_BYTES.get(2), ByteArray.readAllBytes(buffer));
356 * Tests empty AS_PATH, ORIGIN.EGP, LOCAL_PREF, EXTENDED_COMMUNITIES (Ipv4 Addr specific)
358 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
359 * 00 4A <- length (73) - including header
361 * 00 00 <- withdrawn routes length
362 * 00 27 <- total path attribute length (39)
363 * 40 <- attribute flags
364 * 01 <- attribute type code (Origin)
365 * 01 <- attribute length
366 * 01 <- Origin value (EGP)
367 * 40 <- attribute flags
368 * 02 <- attribute type code (As path)
369 * 00 <- attribute length
370 * 40 <- attribute flags
371 * 03 <- attribute type (Next hop)
372 * 04 <- attribute length
373 * 03 03 03 03 <- value (3.3.3.3)
374 * 80 <- attribute flags
375 * 04 <- attribute type code (Multi exit disc)
376 * 04 <- attribute length
377 * 00 00 00 00 <- value
378 * 40 <- attribute flags
379 * 05 <- attribute type (Local Pref)
380 * 04 <- attribute length
381 * 00 00 00 64 <- value (100)
382 * c0 <- attribute flags
383 * 10 <- attribute type (extended community)
384 * 08 <- attribute length
385 * 01 04 <- value (type - Ipv4 Address Specific Extended Community)
386 * c0 a8 01 00 <- value (global adm. 198.162.1.0)
387 * 12 34 <- value (local adm. 4660)
390 * 18 0a 1e 03 <- IPv4 Prefix (10.30.3.0 / 24)
391 * 18 0a 1e 02 <- IPv4 Prefix (10.30.2.0 / 24)
392 * 18 0a 1e 01 <- IPv4 Prefix (10.30.1.0 / 24)
395 public void testGetUpdateMessage4() throws Exception {
396 final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(3), MessageUtil.COMMON_HEADER_LENGTH);
397 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(3), MessageUtil.MARKER_LENGTH,
398 LENGTH_FIELD_LENGTH));
399 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
402 final UpdateBuilder builder = new UpdateBuilder();
405 assertNull(message.getWithdrawnRoutes());
408 final List<Nlri> nlris = new ArrayList<>();
409 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("10.30.3.0/24")).build());
410 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("10.30.2.0/24")).build());
411 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("10.30.1.0/24")).build());
413 assertEquals(nlris, message.getNlri());
415 builder.setNlri(nlris);
418 final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
419 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("3.3.3.3")).build()).build();
421 final List<ExtendedCommunities> comms = new ArrayList<>();
422 comms.add(new ExtendedCommunitiesBuilder()
424 .setExtendedCommunity(new RouteTargetIpv4CaseBuilder()
425 .setRouteTargetIpv4(new RouteTargetIpv4Builder()
426 .setGlobalAdministrator(new Ipv4Address("192.168.1.0"))
427 .setLocalAdministrator(Uint16.valueOf(4660))
432 // check path attributes
433 final Attributes attrs = message.getAttributes();
435 final AttributesBuilder paBuilder = new AttributesBuilder();
437 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Egp).build());
438 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
440 paBuilder.setAsPath(new AsPathBuilder().setSegments(Collections.emptyList()).build());
441 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
443 paBuilder.setCNextHop(nextHop);
444 assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
446 paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed(Uint32.ZERO).build());
447 assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
449 paBuilder.setLocalPref(new LocalPrefBuilder().setPref(Uint32.valueOf(100)).build());
450 assertEquals(paBuilder.getLocalPref(), attrs.getLocalPref());
452 paBuilder.setExtendedCommunities(comms);
453 assertEquals(paBuilder.getExtendedCommunities(), attrs.getExtendedCommunities());
455 paBuilder.setUnrecognizedAttributes(Collections.emptyList());
457 builder.setAttributes(paBuilder.build());
458 assertEquals(builder.build(), message);
460 final ByteBuf buffer = Unpooled.buffer();
461 BGPParserTest.updateParser.serializeMessage(message, buffer);
462 assertArrayEquals(INPUT_BYTES.get(3), ByteArray.readAllBytes(buffer));
466 * Tests withdrawn routes.
468 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
469 * 00 1c <- length (28) - including header
471 * 00 05 <- withdrawn routes length (5)
472 * 1e ac 10 00 04 <- route (172.16.0.4)
473 * 00 00 <- total path attribute length
476 public void testGetUpdateMessage5() throws Exception {
477 final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(4), MessageUtil.COMMON_HEADER_LENGTH);
478 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(4), MessageUtil.MARKER_LENGTH,
479 LENGTH_FIELD_LENGTH));
480 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
484 final List<WithdrawnRoutes> withdrawnRoutes = Lists.newArrayList(new WithdrawnRoutesBuilder()
485 .setPrefix(new Ipv4Prefix("172.16.0.4/30")).build());
488 final Update expectedMessage = new UpdateBuilder().setWithdrawnRoutes(withdrawnRoutes).build();
490 assertEquals(expectedMessage.getWithdrawnRoutes(), message.getWithdrawnRoutes());
492 final ByteBuf buffer = Unpooled.buffer();
493 BGPParserTest.updateParser.serializeMessage(message, buffer);
494 assertArrayEquals(INPUT_BYTES.get(4), ByteArray.readAllBytes(buffer));
498 * End of Rib for Ipv4.
500 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
501 * 00 17 <- length (23) - including header
503 * 00 00 <- withdrawn routes length
504 * 00 00 <- total path attribute length
507 public void testEORIpv4() throws Exception {
508 final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(5), MessageUtil.COMMON_HEADER_LENGTH);
509 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(5), MessageUtil.MARKER_LENGTH,
510 LENGTH_FIELD_LENGTH));
511 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
514 assertEquals(new UpdateBuilder().build(), message);
516 final ByteBuf buffer = Unpooled.buffer();
517 BGPParserTest.updateParser.serializeMessage(message, buffer);
518 assertArrayEquals(INPUT_BYTES.get(5), ByteArray.readAllBytes(buffer));
522 * End of Rib for Ipv6 consists of empty MP_UNREACH_NLRI, with AFI 2 and SAFI 1
524 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
525 * 00 1d <- length (29) - including header
527 * 00 00 <- withdrawn routes length
528 * 00 06 <- total path attribute length
529 * 80 <- attribute flags
530 * 0f <- attribute type (15 - MP_UNREACH_NLRI)
531 * 03 <- attribute length
532 * 00 02 <- value (AFI 2: IPv6)
533 * 01 <- value (SAFI 1)
536 public void testEORIpv6() throws Exception {
537 final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(6), MessageUtil.COMMON_HEADER_LENGTH);
538 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(6), MessageUtil.MARKER_LENGTH,
539 LENGTH_FIELD_LENGTH));
540 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
543 final Class<? extends AddressFamily> afi = message.getAttributes().augmentation(Attributes2.class)
544 .getMpUnreachNlri().getAfi();
545 final Class<? extends SubsequentAddressFamily> safi = message.getAttributes().augmentation(Attributes2.class)
546 .getMpUnreachNlri().getSafi();
548 assertEquals(Ipv6AddressFamily.class, afi);
549 assertEquals(UnicastSubsequentAddressFamily.class, safi);
551 final ByteBuf buffer = Unpooled.buffer();
552 BGPParserTest.updateParser.serializeMessage(message, buffer);
553 assertArrayEquals(INPUT_BYTES.get(6), ByteArray.readAllBytes(buffer));
557 * End of Rib for Ipv6 consists of empty MP_UNREACH_NLRI, with AFI 2 and SAFI 1
559 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
560 * 00 1e <- length (29) - including header
562 * 00 00 <- withdrawn routes length
563 * 00 07 <- total path attribute length
564 * 90 <- attribute flags
565 * 0f <- attribute type (15 - MP_UNREACH_NLRI)
566 * 00 03 <- attribute length
567 * 00 02 <- value (AFI 2: IPv6)
568 * 01 <- value (SAFI 1)
571 public void testEORIpv6exLength() throws Exception {
572 final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(6), MessageUtil.COMMON_HEADER_LENGTH);
573 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(6), MessageUtil.MARKER_LENGTH,
574 LENGTH_FIELD_LENGTH));
575 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
578 final Class<? extends AddressFamily> afi = message.getAttributes().augmentation(Attributes2.class)
579 .getMpUnreachNlri().getAfi();
580 final Class<? extends SubsequentAddressFamily> safi = message.getAttributes().augmentation(Attributes2.class)
581 .getMpUnreachNlri().getSafi();
583 assertEquals(Ipv6AddressFamily.class, afi);
584 assertEquals(UnicastSubsequentAddressFamily.class, safi);
586 final ByteBuf buffer = Unpooled.buffer();
587 BGPParserTest.updateParser.serializeMessage(message, buffer);
588 assertArrayEquals(INPUT_BYTES.get(6), ByteArray.readAllBytes(buffer));
592 * Tests IPv4 NEXT_HOP, ATOMIC_AGGREGATE, COMMUNITY, NLRI with multiple paths.
594 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
595 * 00 60 <- length (96) - including header
597 * 00 00 <- withdrawn routes length
598 * 00 31 <- total path attribute length (49)
599 * 40 <- attribute flags
600 * 01 <- attribute type code (origin)
601 * 01 <- attribute length
602 * 00 <- Origin value (IGP)
603 * 40 <- attribute flags
604 * 02 <- attribute type code (as path)
605 * 06 <- attribute length
607 * 01 <- path segment count
608 * 00 00 fd ea <- path segment value (65002)
609 * 40 <- attribute flags
610 * 03 <- attribute type code (Next Hop)
611 * 04 <- attribute length
612 * 0a 00 00 02 <- value (10.0.0.2)
613 * 80 <- attribute flags
614 * 04 <- attribute type code (multi exit disc)
615 * 04 <- attribute length
616 * 00 00 00 00 <- value
617 * 40 <- attribute flags
618 * 06 <- attribute type code (atomic aggregate)
619 * 00 <- attribute length
620 * C0 <- attribute flags
621 * 08 <- attribute type code (community)
622 * 10 <- attribute length
623 * FF FF FF 01 <- value (NO_EXPORT)
624 * FF FF FF 02 <- value (NO_ADVERTISE)
625 * FF FF FF 03 <- value (NO_EXPORT_SUBCONFED)
626 * FF FF FF 10 <- unknown Community
629 * 00 00 00 01 <- path-id (1)
630 * 18 ac 11 02 <- IPv4 Prefix (172.17.1.0 / 24)
631 * 00 00 00 01 <- path-id (2)
632 * 18 ac 11 01 <- IPv4 Prefix (172.17.1.0 / 24)
633 * 00 00 00 01 <- path-id (1)
634 * 18 ac 11 00 <- IPv4 Prefix (172.17.0.0 / 24)
637 public void testUpdateMessageNlriAddPath() throws Exception {
638 final byte[] body = ByteArray.cutBytes(updatesWithMultiplePath.get(0), MessageUtil.COMMON_HEADER_LENGTH);
639 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(updatesWithMultiplePath.get(0),
640 MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
641 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
646 assertNull(message.getWithdrawnRoutes());
649 final List<AsNumber> asNumbers = new ArrayList<>();
650 asNumbers.add(new AsNumber(Uint32.valueOf(65002)));
651 final List<Segments> asPath = new ArrayList<>();
652 asPath.add(new SegmentsBuilder().setAsSequence(asNumbers).build());
654 final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
655 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.2")).build()).build();
657 final List<Communities> comms = new ArrayList<>();
658 comms.add((Communities) CommunityUtil.NO_EXPORT);
659 comms.add((Communities) CommunityUtil.NO_ADVERTISE);
660 comms.add((Communities) CommunityUtil.NO_EXPORT_SUBCONFED);
661 comms.add((Communities) CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF10));
663 final UpdateBuilder builder = new UpdateBuilder();
667 final List<Nlri> nlris = new ArrayList<>();
668 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.1.0/24")).setPathId(new PathId(Uint32.ONE))
670 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.1.0/24")).setPathId(new PathId(Uint32.valueOf(2)))
672 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.0.0/24")).setPathId(new PathId(Uint32.ONE))
675 assertEquals(nlris, message.getNlri());
677 builder.setNlri(nlris);
679 // check path attributes
681 final Attributes attrs = message.getAttributes();
683 final AttributesBuilder paBuilder = new AttributesBuilder();
685 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
686 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
688 paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
689 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
691 paBuilder.setCNextHop(nextHop);
692 assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
694 paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed(Uint32.ZERO).build());
695 assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
697 paBuilder.setAtomicAggregate(new AtomicAggregateBuilder().build());
698 assertEquals(paBuilder.getAtomicAggregate(), attrs.getAtomicAggregate());
700 paBuilder.setCommunities(comms);
701 assertEquals(paBuilder.getCommunities(), attrs.getCommunities());
703 paBuilder.setUnrecognizedAttributes(Collections.emptyList());
705 builder.setAttributes(paBuilder.build());
707 assertEquals(builder.build(), message);
709 final ByteBuf buffer = Unpooled.buffer();
710 BGPParserTest.updateParser.serializeMessage(message, buffer);
711 assertArrayEquals(updatesWithMultiplePath.get(0), ByteArray.readAllBytes(buffer));
715 * Tests withdrawn routes with multiple paths.
717 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
718 * 00 29 <- length (41) - including header
720 * 00 12 <- withdrawn routes length (18)
721 * 00 00 00 01 <- path-id (1)
722 * 1e ac 10 00 04 <- route (172.16.0.4)
723 * 00 00 00 02 <- path-id (2)
724 * 1e ac 10 00 04 <- route (172.16.0.4)
725 * 00 00 <- total path attribute length
729 public void testUpdateMessageWithdrawAddPath() throws Exception {
730 final byte[] body = ByteArray.cutBytes(updatesWithMultiplePath.get(1), MessageUtil.COMMON_HEADER_LENGTH);
731 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(updatesWithMultiplePath.get(1),
732 MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
733 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
737 final List<WithdrawnRoutes> withdrawnRoutes = new ArrayList<>();
738 withdrawnRoutes.add(new WithdrawnRoutesBuilder().setPrefix(new Ipv4Prefix("172.16.0.4/30"))
739 .setPathId(new PathId(Uint32.ONE)).build());
740 withdrawnRoutes.add(new WithdrawnRoutesBuilder().setPrefix(new Ipv4Prefix("172.16.0.4/30"))
741 .setPathId(new PathId(Uint32.valueOf(2))).build());
744 final Update expectedMessage = new UpdateBuilder().setWithdrawnRoutes(withdrawnRoutes).build();
746 assertEquals(expectedMessage.getWithdrawnRoutes(), message.getWithdrawnRoutes());
748 final ByteBuf buffer = Unpooled.buffer();
749 BGPParserTest.updateParser.serializeMessage(message, buffer);
750 assertArrayEquals(updatesWithMultiplePath.get(1), ByteArray.readAllBytes(buffer));
754 * Tests withdrawn routes with malformed attribute.
756 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
757 * 00 35 <- length (53) - including header
759 * 00 00 <- withdrawn routes length
760 * 00 1a <- total path attribute length (26)
761 * 40 <- attribute flags
762 * 01 <- attribute type code (origin)
763 * 02 <- WRONG attribute length
764 * 00 <- Origin value (IGP)
765 * 40 <- attribute flags
766 * 03 <- attribute type code (Next Hop)
767 * 04 <- attribute length
768 * 0a 00 00 02 <- value (10.0.0.2)
769 * 40 <- attribute flags
770 * 0e <- attribute type code (MP_REACH)
771 * 00 01 <- AFI (Ipv4)
772 * 01 <- SAFI (Unicast)
773 * 04 <- next hop length
774 * ff ff ff ff <- next hop
777 * 0a 00 01 <- prefix (10.0.1.0)
780 * 0a 00 02 <- prefix (10.0.2.0)
783 public void testUpdateMessageWithMalformedAttribute() throws BGPDocumentedException {
784 final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(7), MessageUtil.COMMON_HEADER_LENGTH);
785 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(6), MessageUtil.MARKER_LENGTH,
786 LENGTH_FIELD_LENGTH));
787 final PeerSpecificParserConstraintImpl constraint = new PeerSpecificParserConstraintImpl();
788 constraint.addPeerConstraint(RevisedErrorHandlingSupport.class,
789 RevisedErrorHandlingSupportImpl.forExternalPeer());
790 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
792 assertNotNull(message);
793 assertNull(message.getNlri());
794 final List<WithdrawnRoutes> withdrawnRoutes = message.getWithdrawnRoutes();
795 assertNotNull(withdrawnRoutes);
796 assertEquals(1, withdrawnRoutes.size());