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.TEN), new AsNumber(Uint32.valueOf(20)));
318 asPath.add(new SegmentsBuilder().setAsSet(asSet).build());
320 final Aggregator aggregator = new AggregatorBuilder().setAsNumber(new AsNumber(Uint32.valueOf(30)))
321 .setNetworkAddress(new Ipv4Address("10.0.0.9")).build();
322 final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
323 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.9")).build()).build();
325 // check path attributes
326 final Attributes attrs = message.getAttributes();
328 final AttributesBuilder paBuilder = new AttributesBuilder();
330 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Incomplete).build());
331 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
333 paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
334 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
336 paBuilder.setCNextHop(nextHop);
337 assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
339 paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed(Uint32.ZERO).build());
340 assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
342 paBuilder.setAggregator(aggregator);
343 assertEquals(paBuilder.getAggregator(), attrs.getAggregator());
344 paBuilder.setUnrecognizedAttributes(Collections.emptyList());
345 builder.setAttributes(paBuilder.build());
347 assertEquals(builder.build(), message);
349 final ByteBuf buffer = Unpooled.buffer();
350 BGPParserTest.updateParser.serializeMessage(message, buffer);
351 assertArrayEquals(INPUT_BYTES.get(2), ByteArray.readAllBytes(buffer));
355 * Tests empty AS_PATH, ORIGIN.EGP, LOCAL_PREF, EXTENDED_COMMUNITIES (Ipv4 Addr specific)
357 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
358 * 00 4A <- length (73) - including header
360 * 00 00 <- withdrawn routes length
361 * 00 27 <- total path attribute length (39)
362 * 40 <- attribute flags
363 * 01 <- attribute type code (Origin)
364 * 01 <- attribute length
365 * 01 <- Origin value (EGP)
366 * 40 <- attribute flags
367 * 02 <- attribute type code (As path)
368 * 00 <- attribute length
369 * 40 <- attribute flags
370 * 03 <- attribute type (Next hop)
371 * 04 <- attribute length
372 * 03 03 03 03 <- value (3.3.3.3)
373 * 80 <- attribute flags
374 * 04 <- attribute type code (Multi exit disc)
375 * 04 <- attribute length
376 * 00 00 00 00 <- value
377 * 40 <- attribute flags
378 * 05 <- attribute type (Local Pref)
379 * 04 <- attribute length
380 * 00 00 00 64 <- value (100)
381 * c0 <- attribute flags
382 * 10 <- attribute type (extended community)
383 * 08 <- attribute length
384 * 01 04 <- value (type - Ipv4 Address Specific Extended Community)
385 * c0 a8 01 00 <- value (global adm. 198.162.1.0)
386 * 12 34 <- value (local adm. 4660)
389 * 18 0a 1e 03 <- IPv4 Prefix (10.30.3.0 / 24)
390 * 18 0a 1e 02 <- IPv4 Prefix (10.30.2.0 / 24)
391 * 18 0a 1e 01 <- IPv4 Prefix (10.30.1.0 / 24)
394 public void testGetUpdateMessage4() throws Exception {
395 final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(3), MessageUtil.COMMON_HEADER_LENGTH);
396 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(3), MessageUtil.MARKER_LENGTH,
397 LENGTH_FIELD_LENGTH));
398 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
401 final UpdateBuilder builder = new UpdateBuilder();
404 assertNull(message.getWithdrawnRoutes());
407 final List<Nlri> nlris = new ArrayList<>();
408 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("10.30.3.0/24")).build());
409 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("10.30.2.0/24")).build());
410 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("10.30.1.0/24")).build());
412 assertEquals(nlris, message.getNlri());
414 builder.setNlri(nlris);
417 final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
418 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("3.3.3.3")).build()).build();
420 final List<ExtendedCommunities> comms = new ArrayList<>();
421 comms.add(new ExtendedCommunitiesBuilder()
423 .setExtendedCommunity(new RouteTargetIpv4CaseBuilder()
424 .setRouteTargetIpv4(new RouteTargetIpv4Builder()
425 .setGlobalAdministrator(new Ipv4Address("192.168.1.0"))
426 .setLocalAdministrator(Uint16.valueOf(4660))
431 // check path attributes
432 final Attributes attrs = message.getAttributes();
434 final AttributesBuilder paBuilder = new AttributesBuilder();
436 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Egp).build());
437 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
439 paBuilder.setAsPath(new AsPathBuilder().setSegments(Collections.emptyList()).build());
440 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
442 paBuilder.setCNextHop(nextHop);
443 assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
445 paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed(Uint32.ZERO).build());
446 assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
448 paBuilder.setLocalPref(new LocalPrefBuilder().setPref(Uint32.valueOf(100)).build());
449 assertEquals(paBuilder.getLocalPref(), attrs.getLocalPref());
451 paBuilder.setExtendedCommunities(comms);
452 assertEquals(paBuilder.getExtendedCommunities(), attrs.getExtendedCommunities());
454 paBuilder.setUnrecognizedAttributes(Collections.emptyList());
456 builder.setAttributes(paBuilder.build());
457 assertEquals(builder.build(), message);
459 final ByteBuf buffer = Unpooled.buffer();
460 BGPParserTest.updateParser.serializeMessage(message, buffer);
461 assertArrayEquals(INPUT_BYTES.get(3), ByteArray.readAllBytes(buffer));
465 * Tests withdrawn routes.
467 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
468 * 00 1c <- length (28) - including header
470 * 00 05 <- withdrawn routes length (5)
471 * 1e ac 10 00 04 <- route (172.16.0.4)
472 * 00 00 <- total path attribute length
475 public void testGetUpdateMessage5() throws Exception {
476 final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(4), MessageUtil.COMMON_HEADER_LENGTH);
477 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(4), MessageUtil.MARKER_LENGTH,
478 LENGTH_FIELD_LENGTH));
479 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
483 final List<WithdrawnRoutes> withdrawnRoutes = Lists.newArrayList(new WithdrawnRoutesBuilder()
484 .setPrefix(new Ipv4Prefix("172.16.0.4/30")).build());
487 final Update expectedMessage = new UpdateBuilder().setWithdrawnRoutes(withdrawnRoutes).build();
489 assertEquals(expectedMessage.getWithdrawnRoutes(), message.getWithdrawnRoutes());
491 final ByteBuf buffer = Unpooled.buffer();
492 BGPParserTest.updateParser.serializeMessage(message, buffer);
493 assertArrayEquals(INPUT_BYTES.get(4), ByteArray.readAllBytes(buffer));
497 * End of Rib for Ipv4.
499 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
500 * 00 17 <- length (23) - including header
502 * 00 00 <- withdrawn routes length
503 * 00 00 <- total path attribute length
506 public void testEORIpv4() throws Exception {
507 final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(5), MessageUtil.COMMON_HEADER_LENGTH);
508 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(5), MessageUtil.MARKER_LENGTH,
509 LENGTH_FIELD_LENGTH));
510 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
513 assertEquals(new UpdateBuilder().build(), message);
515 final ByteBuf buffer = Unpooled.buffer();
516 BGPParserTest.updateParser.serializeMessage(message, buffer);
517 assertArrayEquals(INPUT_BYTES.get(5), ByteArray.readAllBytes(buffer));
521 * End of Rib for Ipv6 consists of empty MP_UNREACH_NLRI, with AFI 2 and SAFI 1
523 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
524 * 00 1d <- length (29) - including header
526 * 00 00 <- withdrawn routes length
527 * 00 06 <- total path attribute length
528 * 80 <- attribute flags
529 * 0f <- attribute type (15 - MP_UNREACH_NLRI)
530 * 03 <- attribute length
531 * 00 02 <- value (AFI 2: IPv6)
532 * 01 <- value (SAFI 1)
535 public void testEORIpv6() throws Exception {
536 final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(6), MessageUtil.COMMON_HEADER_LENGTH);
537 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(6), MessageUtil.MARKER_LENGTH,
538 LENGTH_FIELD_LENGTH));
539 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
542 final Class<? extends AddressFamily> afi = message.getAttributes().augmentation(Attributes2.class)
543 .getMpUnreachNlri().getAfi();
544 final Class<? extends SubsequentAddressFamily> safi = message.getAttributes().augmentation(Attributes2.class)
545 .getMpUnreachNlri().getSafi();
547 assertEquals(Ipv6AddressFamily.class, afi);
548 assertEquals(UnicastSubsequentAddressFamily.class, safi);
550 final ByteBuf buffer = Unpooled.buffer();
551 BGPParserTest.updateParser.serializeMessage(message, buffer);
552 assertArrayEquals(INPUT_BYTES.get(6), ByteArray.readAllBytes(buffer));
556 * End of Rib for Ipv6 consists of empty MP_UNREACH_NLRI, with AFI 2 and SAFI 1
558 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
559 * 00 1e <- length (29) - including header
561 * 00 00 <- withdrawn routes length
562 * 00 07 <- total path attribute length
563 * 90 <- attribute flags
564 * 0f <- attribute type (15 - MP_UNREACH_NLRI)
565 * 00 03 <- attribute length
566 * 00 02 <- value (AFI 2: IPv6)
567 * 01 <- value (SAFI 1)
570 public void testEORIpv6exLength() throws Exception {
571 final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(6), MessageUtil.COMMON_HEADER_LENGTH);
572 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(6), MessageUtil.MARKER_LENGTH,
573 LENGTH_FIELD_LENGTH));
574 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
577 final Class<? extends AddressFamily> afi = message.getAttributes().augmentation(Attributes2.class)
578 .getMpUnreachNlri().getAfi();
579 final Class<? extends SubsequentAddressFamily> safi = message.getAttributes().augmentation(Attributes2.class)
580 .getMpUnreachNlri().getSafi();
582 assertEquals(Ipv6AddressFamily.class, afi);
583 assertEquals(UnicastSubsequentAddressFamily.class, safi);
585 final ByteBuf buffer = Unpooled.buffer();
586 BGPParserTest.updateParser.serializeMessage(message, buffer);
587 assertArrayEquals(INPUT_BYTES.get(6), ByteArray.readAllBytes(buffer));
591 * Tests IPv4 NEXT_HOP, ATOMIC_AGGREGATE, COMMUNITY, NLRI with multiple paths.
593 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
594 * 00 60 <- length (96) - including header
596 * 00 00 <- withdrawn routes length
597 * 00 31 <- total path attribute length (49)
598 * 40 <- attribute flags
599 * 01 <- attribute type code (origin)
600 * 01 <- attribute length
601 * 00 <- Origin value (IGP)
602 * 40 <- attribute flags
603 * 02 <- attribute type code (as path)
604 * 06 <- attribute length
606 * 01 <- path segment count
607 * 00 00 fd ea <- path segment value (65002)
608 * 40 <- attribute flags
609 * 03 <- attribute type code (Next Hop)
610 * 04 <- attribute length
611 * 0a 00 00 02 <- value (10.0.0.2)
612 * 80 <- attribute flags
613 * 04 <- attribute type code (multi exit disc)
614 * 04 <- attribute length
615 * 00 00 00 00 <- value
616 * 40 <- attribute flags
617 * 06 <- attribute type code (atomic aggregate)
618 * 00 <- attribute length
619 * C0 <- attribute flags
620 * 08 <- attribute type code (community)
621 * 10 <- attribute length
622 * FF FF FF 01 <- value (NO_EXPORT)
623 * FF FF FF 02 <- value (NO_ADVERTISE)
624 * FF FF FF 03 <- value (NO_EXPORT_SUBCONFED)
625 * FF FF FF 10 <- unknown Community
628 * 00 00 00 01 <- path-id (1)
629 * 18 ac 11 02 <- IPv4 Prefix (172.17.1.0 / 24)
630 * 00 00 00 01 <- path-id (2)
631 * 18 ac 11 01 <- IPv4 Prefix (172.17.1.0 / 24)
632 * 00 00 00 01 <- path-id (1)
633 * 18 ac 11 00 <- IPv4 Prefix (172.17.0.0 / 24)
636 public void testUpdateMessageNlriAddPath() throws Exception {
637 final byte[] body = ByteArray.cutBytes(updatesWithMultiplePath.get(0), MessageUtil.COMMON_HEADER_LENGTH);
638 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(updatesWithMultiplePath.get(0),
639 MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
640 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
645 assertNull(message.getWithdrawnRoutes());
648 final List<AsNumber> asNumbers = new ArrayList<>();
649 asNumbers.add(new AsNumber(Uint32.valueOf(65002)));
650 final List<Segments> asPath = new ArrayList<>();
651 asPath.add(new SegmentsBuilder().setAsSequence(asNumbers).build());
653 final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
654 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.2")).build()).build();
656 final List<Communities> comms = new ArrayList<>();
657 comms.add((Communities) CommunityUtil.NO_EXPORT);
658 comms.add((Communities) CommunityUtil.NO_ADVERTISE);
659 comms.add((Communities) CommunityUtil.NO_EXPORT_SUBCONFED);
660 comms.add((Communities) CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF10));
662 final UpdateBuilder builder = new UpdateBuilder();
666 final List<Nlri> nlris = new ArrayList<>();
667 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.1.0/24")).setPathId(new PathId(Uint32.ONE))
669 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.1.0/24")).setPathId(new PathId(Uint32.TWO))
671 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.0.0/24")).setPathId(new PathId(Uint32.ONE))
674 assertEquals(nlris, message.getNlri());
676 builder.setNlri(nlris);
678 // check path attributes
680 final Attributes attrs = message.getAttributes();
682 final AttributesBuilder paBuilder = new AttributesBuilder();
684 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
685 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
687 paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
688 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
690 paBuilder.setCNextHop(nextHop);
691 assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
693 paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed(Uint32.ZERO).build());
694 assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
696 paBuilder.setAtomicAggregate(new AtomicAggregateBuilder().build());
697 assertEquals(paBuilder.getAtomicAggregate(), attrs.getAtomicAggregate());
699 paBuilder.setCommunities(comms);
700 assertEquals(paBuilder.getCommunities(), attrs.getCommunities());
702 paBuilder.setUnrecognizedAttributes(Collections.emptyList());
704 builder.setAttributes(paBuilder.build());
706 assertEquals(builder.build(), message);
708 final ByteBuf buffer = Unpooled.buffer();
709 BGPParserTest.updateParser.serializeMessage(message, buffer);
710 assertArrayEquals(updatesWithMultiplePath.get(0), ByteArray.readAllBytes(buffer));
714 * Tests withdrawn routes with multiple paths.
716 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
717 * 00 29 <- length (41) - including header
719 * 00 12 <- withdrawn routes length (18)
720 * 00 00 00 01 <- path-id (1)
721 * 1e ac 10 00 04 <- route (172.16.0.4)
722 * 00 00 00 02 <- path-id (2)
723 * 1e ac 10 00 04 <- route (172.16.0.4)
724 * 00 00 <- total path attribute length
728 public void testUpdateMessageWithdrawAddPath() throws Exception {
729 final byte[] body = ByteArray.cutBytes(updatesWithMultiplePath.get(1), MessageUtil.COMMON_HEADER_LENGTH);
730 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(updatesWithMultiplePath.get(1),
731 MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
732 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
736 final List<WithdrawnRoutes> withdrawnRoutes = new ArrayList<>();
737 withdrawnRoutes.add(new WithdrawnRoutesBuilder().setPrefix(new Ipv4Prefix("172.16.0.4/30"))
738 .setPathId(new PathId(Uint32.ONE)).build());
739 withdrawnRoutes.add(new WithdrawnRoutesBuilder().setPrefix(new Ipv4Prefix("172.16.0.4/30"))
740 .setPathId(new PathId(Uint32.TWO)).build());
743 final Update expectedMessage = new UpdateBuilder().setWithdrawnRoutes(withdrawnRoutes).build();
745 assertEquals(expectedMessage.getWithdrawnRoutes(), message.getWithdrawnRoutes());
747 final ByteBuf buffer = Unpooled.buffer();
748 BGPParserTest.updateParser.serializeMessage(message, buffer);
749 assertArrayEquals(updatesWithMultiplePath.get(1), ByteArray.readAllBytes(buffer));
753 * Tests withdrawn routes with malformed attribute.
755 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
756 * 00 35 <- length (53) - including header
758 * 00 00 <- withdrawn routes length
759 * 00 1a <- total path attribute length (26)
760 * 40 <- attribute flags
761 * 01 <- attribute type code (origin)
762 * 02 <- WRONG attribute length
763 * 00 <- Origin value (IGP)
764 * 40 <- attribute flags
765 * 03 <- attribute type code (Next Hop)
766 * 04 <- attribute length
767 * 0a 00 00 02 <- value (10.0.0.2)
768 * 40 <- attribute flags
769 * 0e <- attribute type code (MP_REACH)
770 * 00 01 <- AFI (Ipv4)
771 * 01 <- SAFI (Unicast)
772 * 04 <- next hop length
773 * ff ff ff ff <- next hop
776 * 0a 00 01 <- prefix (10.0.1.0)
779 * 0a 00 02 <- prefix (10.0.2.0)
782 public void testUpdateMessageWithMalformedAttribute() throws BGPDocumentedException {
783 final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(7), MessageUtil.COMMON_HEADER_LENGTH);
784 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(6), MessageUtil.MARKER_LENGTH,
785 LENGTH_FIELD_LENGTH));
786 final PeerSpecificParserConstraintImpl constraint = new PeerSpecificParserConstraintImpl();
787 constraint.addPeerConstraint(RevisedErrorHandlingSupport.class,
788 RevisedErrorHandlingSupportImpl.forExternalPeer());
789 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
791 assertNotNull(message);
792 assertNull(message.getNlri());
793 final List<WithdrawnRoutes> withdrawnRoutes = message.getWithdrawnRoutes();
794 assertNotNull(withdrawnRoutes);
795 assertEquals(1, withdrawnRoutes.size());