2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.protocol.bgp.parser.impl;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.mockito.Mockito.mock;
16 import com.google.common.collect.Lists;
17 import io.netty.buffer.ByteBuf;
18 import io.netty.buffer.Unpooled;
19 import java.io.ByteArrayOutputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.Optional;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.mockito.Mockito;
29 import org.opendaylight.protocol.bgp.parser.impl.message.BGPUpdateMessageParser;
30 import org.opendaylight.protocol.bgp.parser.impl.message.update.CommunityUtil;
31 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
32 import org.opendaylight.protocol.bgp.parser.spi.MultiPathSupport;
33 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
34 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
35 import org.opendaylight.protocol.bgp.util.HexDumpBGPFileParser;
36 import org.opendaylight.protocol.util.ByteArray;
37 import org.opendaylight.protocol.util.NoopReferenceCache;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.UpdateBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.Aggregator;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.AggregatorBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.AsPathBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.AtomicAggregateBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.Communities;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.ExtendedCommunities;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.ExtendedCommunitiesBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.LocalPrefBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.MultiExitDiscBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.OriginBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.as.path.Segments;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.as.path.SegmentsBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.update.message.Nlri;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.update.message.NlriBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.update.message.WithdrawnRoutes;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.update.message.WithdrawnRoutesBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.BgpOrigin;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv6AddressFamily;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.SubsequentAddressFamily;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.RouteTargetIpv4CaseBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.route.target.ipv4._case.RouteTargetIpv4Builder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv4NextHopCase;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
72 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;
74 public class BGPParserTest {
77 * Used by other tests as well
79 static final List<byte[]> inputBytes = new ArrayList<>();
81 private static int COUNTER = 7;
83 private static int MAX_SIZE = 300;
85 private static BGPUpdateMessageParser updateParser;
87 private static final int LENGTH_FIELD_LENGTH = 2;
89 private static final String multiPathHexFile = "/bgp-update-multipath.txt";
91 private static List<byte[]> updatesWithMultiplePath;
93 private static PeerSpecificParserConstraint constraint;
95 private static MultiPathSupport mpSupport;
98 public static void setUp() throws Exception {
99 updateParser = new BGPUpdateMessageParser(ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry());
100 for (int i = 1; i <= COUNTER; i++) {
101 final String name = "/up" + i + ".bin";
102 try (final InputStream is = BGPParserTest.class.getResourceAsStream(name)){
104 throw new IOException("Failed to get resource " + name);
106 final ByteArrayOutputStream bis = new ByteArrayOutputStream();
107 final byte[] data = new byte[MAX_SIZE];
109 while ((nRead = is.read(data, 0, data.length)) != -1) {
110 bis.write(data, 0, nRead);
114 inputBytes.add(bis.toByteArray());
118 updatesWithMultiplePath = HexDumpBGPFileParser.parseMessages(BGPParserTest.class.getResourceAsStream(multiPathHexFile));
119 constraint = mock(PeerSpecificParserConstraint.class);
120 mpSupport = mock(MultiPathSupport.class);
121 Mockito.doReturn(Optional.of(mpSupport)).when(constraint).getPeerConstraint(Mockito.any());
122 Mockito.doReturn(true).when(mpSupport).isTableTypeSupported(Mockito.any());
126 public void testResource() {
127 assertNotNull(inputBytes);
131 * Tests IPv4 NEXT_HOP, ATOMIC_AGGREGATE, COMMUNITY, NLRI
133 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
134 * 00 54 <- length (84) - including header
136 * 00 00 <- withdrawn routes length
137 * 00 31 <- total path attribute length (49)
138 * 40 <- attribute flags
139 * 01 <- attribute type code (origin)
140 * 01 <- attribute length
141 * 00 <- Origin value (IGP)
142 * 40 <- attribute flags
143 * 02 <- attribute type code (as path)
144 * 06 <- attribute length
146 * 01 <- path segment count
147 * 00 00 fd ea <- path segment value (65002)
148 * 40 <- attribute flags
149 * 03 <- attribute type code (Next Hop)
150 * 04 <- attribute length
151 * 10 00 00 02 <- value (10.0.0.2)
152 * 80 <- attribute flags
153 * 04 <- attribute type code (multi exit disc)
154 * 04 <- attribute length
155 * 00 00 00 00 <- value
156 * 60 <- attribute flags
157 * 06 <- attribute type code (atomic aggregate)
158 * 00 <- attribute length
159 * 40 <- attribute flags
160 * 08 <- attribute type code (community)
161 * 10 <- attribute length FF FF FF
162 * 01 <- value (NO_EXPORT)
163 * FF FF FF 02 <- value (NO_ADVERTISE)
164 * FF FF FF 03 <- value (NO_EXPORT_SUBCONFED)
165 * FF FF FF 10 <- unknown Community
168 * 18 ac 11 02 <- IPv4 Prefix (172.17.2.0 / 24)
169 * 18 ac 11 01 <- IPv4 Prefix (172.17.1.0 / 24)
170 * 18 ac 11 00 <- IPv4 Prefix (172.17.0.0 / 24)
173 public void testGetUpdateMessage1() throws Exception {
175 final byte[] body = ByteArray.cutBytes(inputBytes.get(0), MessageUtil.COMMON_HEADER_LENGTH);
176 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(0), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
177 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
181 assertNull(message.getWithdrawnRoutes());
184 final List<AsNumber> asNumbers = new ArrayList<>();
185 asNumbers.add(new AsNumber(65002L));
186 final List<Segments> asPath = Lists.newArrayList();
187 asPath.add(new SegmentsBuilder().setAsSequence(asNumbers).build());
189 final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
190 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.2")).build()).build();
192 final List<Communities> comms = Lists.newArrayList();
193 comms.add((Communities) CommunityUtil.NO_EXPORT);
194 comms.add((Communities) CommunityUtil.NO_ADVERTISE);
195 comms.add((Communities) CommunityUtil.NO_EXPORT_SUBCONFED);
196 comms.add((Communities) CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF10));
198 final UpdateBuilder builder = new UpdateBuilder();
202 final List<Nlri> nlris = Lists.newArrayList();
203 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.2.0/24")).build());
204 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.1.0/24")).build());
205 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.0.0/24")).build());
207 assertEquals(nlris, message.getNlri());
209 builder.setNlri(nlris);
211 // check path attributes
213 final Attributes attrs = message.getAttributes();
215 final AttributesBuilder paBuilder = new AttributesBuilder();
217 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
218 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
220 paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
221 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
223 paBuilder.setCNextHop(nextHop);
224 assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
226 paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
227 assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
229 paBuilder.setAtomicAggregate(new AtomicAggregateBuilder().build());
230 assertEquals(paBuilder.getAtomicAggregate(), attrs.getAtomicAggregate());
232 paBuilder.setCommunities(comms);
233 assertEquals(paBuilder.getCommunities(), attrs.getCommunities());
235 paBuilder.setUnrecognizedAttributes(Collections.emptyList());
237 builder.setAttributes(paBuilder.build());
239 assertEquals(builder.build(), message);
241 final ByteBuf buffer = Unpooled.buffer();
242 BGPParserTest.updateParser.serializeMessage(message, buffer);
243 assertArrayEquals(inputBytes.get(0), ByteArray.readAllBytes(buffer));
247 * Tests more AS Numbers in AS_PATH, AGGREGATOR, ORIGIN.INCOMPLETE
249 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
250 * 00 4b <- length (75) - including header
252 * 00 00 <- withdrawn routes length
253 * 00 30 <- total path attribute length (48)
254 * 40 <- attribute flags
255 * 01 <- attribute type code (origin)
256 * 01 <- attribute length
257 * 02 <- Origin value (Incomplete)
258 * 40 <- attribute flags
259 * 02 <- attribute type code (as path)
260 * 10 <- attribute length
262 * 01 <- path segment count
263 * 00 00 00 1e <- path segment value (30)
265 * 02 <- path segment count
266 * 00 00 00 0a <- path segment value (10)
267 * 00 00 00 14 <- path segment value (20)
268 * 40 <- attribute flags
269 * 03 <- attribute type (Next hop)
270 * 04 <- attribute length
271 * 0a 00 00 09 <- value (10.0.0.9)
272 * 80 <- attribute flags
273 * 04 <- attribute type code (multi exit disc)
274 * 04 <- attribute length
275 * 00 00 00 00 <- value
276 * c0 <- attribute flags
277 * 07 <- attribute type (Aggregator)
278 * 08 <- attribute length
279 * 00 00 00 1e <- value (AS number = 30)
280 * 0a 00 00 09 <- value (IP address = 10.0.0.9)
283 * 15 ac 10 00 <- IPv4 Prefix (172.16.0.0 / 21)
286 public void testGetUpdateMessage3() throws Exception {
287 final byte[] body = ByteArray.cutBytes(inputBytes.get(2), MessageUtil.COMMON_HEADER_LENGTH);
288 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(2), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
289 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
291 final UpdateBuilder builder = new UpdateBuilder();
294 final List<Nlri> nlris = Lists.newArrayList(new NlriBuilder().setPrefix(new Ipv4Prefix("172.16.0.0/21")).build());
295 builder.setNlri(nlris);
296 assertEquals(builder.getNlri(), message.getNlri());
299 assertNull(message.getWithdrawnRoutes());
302 final List<AsNumber> asNumbers = new ArrayList<>();
303 asNumbers.add(new AsNumber(30L));
304 final List<Segments> asPath = Lists.newArrayList();
305 asPath.add(new SegmentsBuilder().setAsSequence(asNumbers).build());
306 final List<AsNumber> asSet = Lists.newArrayList(new AsNumber(10L), new AsNumber(20L));
307 asPath.add(new SegmentsBuilder().setAsSet(asSet).build());
309 final Aggregator aggregator = new AggregatorBuilder().setAsNumber(new AsNumber((long) 30)).setNetworkAddress(
310 new Ipv4Address("10.0.0.9")).build();
311 final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
312 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.9")).build()).build();
314 // check path attributes
315 final Attributes attrs = message.getAttributes();
317 final AttributesBuilder paBuilder = new AttributesBuilder();
319 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Incomplete).build());
320 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
322 paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
323 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
325 paBuilder.setCNextHop(nextHop);
326 assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
328 paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
329 assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
331 paBuilder.setAggregator(aggregator);
332 assertEquals(paBuilder.getAggregator(), attrs.getAggregator());
333 paBuilder.setUnrecognizedAttributes(Collections.emptyList());
334 builder.setAttributes(paBuilder.build());
336 assertEquals(builder.build(), message);
338 final ByteBuf buffer = Unpooled.buffer();
339 BGPParserTest.updateParser.serializeMessage(message, buffer);
340 assertArrayEquals(inputBytes.get(2), ByteArray.readAllBytes(buffer));
344 * Tests empty AS_PATH, ORIGIN.EGP, LOCAL_PREF, EXTENDED_COMMUNITIES (Ipv4 Addr specific)
346 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
347 * 00 4A <- length (73) - including header
349 * 00 00 <- withdrawn routes length
350 * 00 27 <- total path attribute length (39)
351 * 40 <- attribute flags
352 * 01 <- attribute type code (Origin)
353 * 01 <- attribute length
354 * 01 <- Origin value (EGP)
355 * 40 <- attribute flags
356 * 02 <- attribute type code (As path)
357 * 00 <- attribute length
358 * 40 <- attribute flags
359 * 03 <- attribute type (Next hop)
360 * 04 <- attribute length
361 * 03 03 03 03 <- value (3.3.3.3)
362 * 80 <- attribute flags
363 * 04 <- attribute type code (Multi exit disc)
364 * 04 <- attribute length
365 * 00 00 00 00 <- value
366 * 40 <- attribute flags
367 * 05 <- attribute type (Local Pref)
368 * 04 <- attribute length
369 * 00 00 00 64 <- value (100)
370 * c0 <- attribute flags
371 * 10 <- attribute type (extended community)
372 * 08 <- attribute length
373 * 01 04 <- value (type - Ipv4 Address Specific Extended Community)
374 * c0 a8 01 00 <- value (global adm. 198.162.1.0)
375 * 12 34 <- value (local adm. 4660)
378 * 18 0a 1e 03 <- IPv4 Prefix (10.30.3.0 / 24)
379 * 18 0a 1e 02 <- IPv4 Prefix (10.30.2.0 / 24)
380 * 18 0a 1e 01 <- IPv4 Prefix (10.30.1.0 / 24)
383 public void testGetUpdateMessage4() throws Exception {
384 final byte[] body = ByteArray.cutBytes(inputBytes.get(3), MessageUtil.COMMON_HEADER_LENGTH);
385 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(3), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
386 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
388 final UpdateBuilder builder = new UpdateBuilder();
391 assertNull(message.getWithdrawnRoutes());
394 final List<Nlri> nlris = Lists.newArrayList();
395 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("10.30.3.0/24")).build());
396 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("10.30.2.0/24")).build());
397 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("10.30.1.0/24")).build());
399 assertEquals(nlris, message.getNlri());
401 builder.setNlri(nlris);
404 final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
405 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("3.3.3.3")).build()).build();
407 final List<ExtendedCommunities> comms = Lists.newArrayList();
408 comms.add(new ExtendedCommunitiesBuilder().setTransitive(true).setExtendedCommunity(
409 new RouteTargetIpv4CaseBuilder().setRouteTargetIpv4(
410 new RouteTargetIpv4Builder().setGlobalAdministrator(
411 new Ipv4Address("192.168.1.0")).setLocalAdministrator(4660).build()).build()).build());
413 // check path attributes
414 final Attributes attrs = message.getAttributes();
416 final AttributesBuilder paBuilder = new AttributesBuilder();
418 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Egp).build());
419 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
421 paBuilder.setAsPath(new AsPathBuilder().setSegments(Collections.emptyList()).build());
422 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
424 paBuilder.setCNextHop(nextHop);
425 assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
427 paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
428 assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
430 paBuilder.setLocalPref(new LocalPrefBuilder().setPref(100L).build());
431 assertEquals(paBuilder.getLocalPref(), attrs.getLocalPref());
433 paBuilder.setExtendedCommunities(comms);
434 assertEquals(paBuilder.getExtendedCommunities(), attrs.getExtendedCommunities());
436 paBuilder.setUnrecognizedAttributes(Collections.emptyList());
438 builder.setAttributes(paBuilder.build());
439 assertEquals(builder.build(), message);
441 final ByteBuf buffer = Unpooled.buffer();
442 BGPParserTest.updateParser.serializeMessage(message, buffer);
443 assertArrayEquals(inputBytes.get(3), ByteArray.readAllBytes(buffer));
447 * Tests withdrawn routes.
449 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
450 * 00 1c <- length (28) - including header
452 * 00 05 <- withdrawn routes length (5)
453 * 1e ac 10 00 04 <- route (172.16.0.4)
454 * 00 00 <- total path attribute length
457 public void testGetUpdateMessage5() throws Exception {
458 final byte[] body = ByteArray.cutBytes(inputBytes.get(4), MessageUtil.COMMON_HEADER_LENGTH);
459 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(4), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
460 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
463 final List<WithdrawnRoutes> withdrawnRoutes = Lists.newArrayList(new WithdrawnRoutesBuilder().setPrefix(new Ipv4Prefix("172.16.0.4/30")).build());
466 final Update expectedMessage = new UpdateBuilder().setWithdrawnRoutes(withdrawnRoutes).build();
468 assertEquals(expectedMessage.getWithdrawnRoutes(), message.getWithdrawnRoutes());
470 final ByteBuf buffer = Unpooled.buffer();
471 BGPParserTest.updateParser.serializeMessage(message, buffer);
472 assertArrayEquals(inputBytes.get(4), ByteArray.readAllBytes(buffer));
476 * End of Rib for Ipv4.
478 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
479 * 00 17 <- length (23) - including header
481 * 00 00 <- withdrawn routes length
482 * 00 00 <- total path attribute length
485 public void testEORIpv4() throws Exception {
486 final byte[] body = ByteArray.cutBytes(inputBytes.get(5), MessageUtil.COMMON_HEADER_LENGTH);
487 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(5), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
488 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
490 assertEquals(new UpdateBuilder().build(), message);
492 final ByteBuf buffer = Unpooled.buffer();
493 BGPParserTest.updateParser.serializeMessage(message, buffer);
494 assertArrayEquals(inputBytes.get(5), ByteArray.readAllBytes(buffer));
498 * End of Rib for Ipv6 consists of empty MP_UNREACH_NLRI, with AFI 2 and SAFI 1
500 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
501 * 00 1d <- length (29) - including header
503 * 00 00 <- withdrawn routes length
504 * 00 06 <- total path attribute length
505 * 80 <- attribute flags
506 * 0f <- attribute type (15 - MP_UNREACH_NLRI)
507 * 03 <- attribute length
508 * 00 02 <- value (AFI 2: IPv6)
509 * 01 <- value (SAFI 1)
512 public void testEORIpv6() throws Exception {
513 final byte[] body = ByteArray.cutBytes(inputBytes.get(6), MessageUtil.COMMON_HEADER_LENGTH);
514 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(6), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
515 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
517 final Class<? extends AddressFamily> afi = message.getAttributes().getAugmentation(Attributes2.class).getMpUnreachNlri().getAfi();
518 final Class<? extends SubsequentAddressFamily> safi = message.getAttributes().getAugmentation(Attributes2.class).getMpUnreachNlri().getSafi();
520 assertEquals(Ipv6AddressFamily.class, afi);
521 assertEquals(UnicastSubsequentAddressFamily.class, safi);
523 final ByteBuf buffer = Unpooled.buffer();
524 BGPParserTest.updateParser.serializeMessage(message, buffer);
525 assertArrayEquals(inputBytes.get(6), ByteArray.readAllBytes(buffer));
529 * End of Rib for Ipv6 consists of empty MP_UNREACH_NLRI, with AFI 2 and SAFI 1
531 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
532 * 00 1e <- length (29) - including header
534 * 00 00 <- withdrawn routes length
535 * 00 07 <- total path attribute length
536 * 90 <- attribute flags
537 * 0f <- attribute type (15 - MP_UNREACH_NLRI)
538 * 00 03 <- attribute length
539 * 00 02 <- value (AFI 2: IPv6)
540 * 01 <- value (SAFI 1)
543 public void testEORIpv6exLength() throws Exception {
544 final byte[] body = ByteArray.cutBytes(inputBytes.get(6), MessageUtil.COMMON_HEADER_LENGTH);
545 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(6), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
546 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
548 final Class<? extends AddressFamily> afi = message.getAttributes().getAugmentation(Attributes2.class).getMpUnreachNlri().getAfi();
549 final Class<? extends SubsequentAddressFamily> safi = message.getAttributes().getAugmentation(Attributes2.class).getMpUnreachNlri().getSafi();
551 assertEquals(Ipv6AddressFamily.class, afi);
552 assertEquals(UnicastSubsequentAddressFamily.class, safi);
554 final ByteBuf buffer = Unpooled.buffer();
555 BGPParserTest.updateParser.serializeMessage(message, buffer);
556 assertArrayEquals(inputBytes.get(6), ByteArray.readAllBytes(buffer));
560 * Tests IPv4 NEXT_HOP, ATOMIC_AGGREGATE, COMMUNITY, NLRI with multiple paths.
562 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
563 * 00 60 <- length (96) - including header
565 * 00 00 <- withdrawn routes length
566 * 00 31 <- total path attribute length (49)
567 * 40 <- attribute flags
568 * 01 <- attribute type code (origin)
569 * 01 <- attribute length
570 * 00 <- Origin value (IGP)
571 * 40 <- attribute flags
572 * 02 <- attribute type code (as path)
573 * 06 <- attribute length
575 * 01 <- path segment count
576 * 00 00 fd ea <- path segment value (65002)
577 * 40 <- attribute flags
578 * 03 <- attribute type code (Next Hop)
579 * 04 <- attribute length
580 * 0a 00 00 02 <- value (10.0.0.2)
581 * 80 <- attribute flags
582 * 04 <- attribute type code (multi exit disc)
583 * 04 <- attribute length
584 * 00 00 00 00 <- value
585 * 40 <- attribute flags
586 * 06 <- attribute type code (atomic aggregate)
587 * 00 <- attribute length
588 * C0 <- attribute flags
589 * 08 <- attribute type code (community)
590 * 10 <- attribute length
591 * FF FF FF 01 <- value (NO_EXPORT)
592 * FF FF FF 02 <- value (NO_ADVERTISE)
593 * FF FF FF 03 <- value (NO_EXPORT_SUBCONFED)
594 * FF FF FF 10 <- unknown Community
597 * 00 00 00 01 <- path-id (1)
598 * 18 ac 11 02 <- IPv4 Prefix (172.17.1.0 / 24)
599 * 00 00 00 01 <- path-id (2)
600 * 18 ac 11 01 <- IPv4 Prefix (172.17.1.0 / 24)
601 * 00 00 00 01 <- path-id (1)
602 * 18 ac 11 00 <- IPv4 Prefix (172.17.0.0 / 24)
605 public void testUpdateMessageNlriAddPath() throws Exception {
606 final byte[] body = ByteArray.cutBytes(updatesWithMultiplePath.get(0), MessageUtil.COMMON_HEADER_LENGTH);
607 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(updatesWithMultiplePath.get(0), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
608 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength, constraint);
612 assertNull(message.getWithdrawnRoutes());
615 final List<AsNumber> asNumbers = new ArrayList<>();
616 asNumbers.add(new AsNumber(65002L));
617 final List<Segments> asPath = Lists.newArrayList();
618 asPath.add(new SegmentsBuilder().setAsSequence(asNumbers).build());
620 final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
621 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.2")).build()).build();
623 final List<Communities> comms = Lists.newArrayList();
624 comms.add((Communities) CommunityUtil.NO_EXPORT);
625 comms.add((Communities) CommunityUtil.NO_ADVERTISE);
626 comms.add((Communities) CommunityUtil.NO_EXPORT_SUBCONFED);
627 comms.add((Communities) CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF10));
629 final UpdateBuilder builder = new UpdateBuilder();
633 final List<Nlri> nlris = Lists.newArrayList();
634 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.1.0/24")).setPathId(new PathId(1L)).build());
635 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.1.0/24")).setPathId(new PathId(2L)).build());
636 nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.0.0/24")).setPathId(new PathId(1L)).build());
638 assertEquals(nlris, message.getNlri());
640 builder.setNlri(nlris);
642 // check path attributes
644 final Attributes attrs = message.getAttributes();
646 final AttributesBuilder paBuilder = new AttributesBuilder();
648 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
649 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
651 paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
652 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
654 paBuilder.setCNextHop(nextHop);
655 assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
657 paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
658 assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
660 paBuilder.setAtomicAggregate(new AtomicAggregateBuilder().build());
661 assertEquals(paBuilder.getAtomicAggregate(), attrs.getAtomicAggregate());
663 paBuilder.setCommunities(comms);
664 assertEquals(paBuilder.getCommunities(), attrs.getCommunities());
666 paBuilder.setUnrecognizedAttributes(Collections.emptyList());
668 builder.setAttributes(paBuilder.build());
670 assertEquals(builder.build(), message);
672 final ByteBuf buffer = Unpooled.buffer();
673 BGPParserTest.updateParser.serializeMessage(message, buffer);
674 assertArrayEquals(updatesWithMultiplePath.get(0), ByteArray.readAllBytes(buffer));
678 * Tests withdrawn routes with multiple paths.
680 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
681 * 00 29 <- length (41) - including header
683 * 00 12 <- withdrawn routes length (18)
684 * 00 00 00 01 <- path-id (1)
685 * 1e ac 10 00 04 <- route (172.16.0.4)
686 * 00 00 00 02 <- path-id (2)
687 * 1e ac 10 00 04 <- route (172.16.0.4)
688 * 00 00 <- total path attribute length
691 public void testUpdateMessageWithdrawAddPath() throws Exception {
692 final byte[] body = ByteArray.cutBytes(updatesWithMultiplePath.get(1), MessageUtil.COMMON_HEADER_LENGTH);
693 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(updatesWithMultiplePath.get(1), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
694 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength, constraint);
697 final List<WithdrawnRoutes> withdrawnRoutes = Lists.newArrayList();
698 withdrawnRoutes.add(new WithdrawnRoutesBuilder().setPrefix(new Ipv4Prefix("172.16.0.4/30")).setPathId(new PathId(1L)).build());
699 withdrawnRoutes.add(new WithdrawnRoutesBuilder().setPrefix(new Ipv4Prefix("172.16.0.4/30")).setPathId(new PathId(2L)).build());
702 final Update expectedMessage = new UpdateBuilder().setWithdrawnRoutes(withdrawnRoutes).build();
704 assertEquals(expectedMessage.getWithdrawnRoutes(), message.getWithdrawnRoutes());
706 final ByteBuf buffer = Unpooled.buffer();
707 BGPParserTest.updateParser.serializeMessage(message, buffer);
708 assertArrayEquals(updatesWithMultiplePath.get(1), ByteArray.readAllBytes(buffer));