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;
15 import com.google.common.collect.Lists;
16 import io.netty.buffer.ByteBuf;
17 import io.netty.buffer.Unpooled;
18 import java.io.ByteArrayOutputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26 import org.opendaylight.protocol.bgp.parser.impl.message.BGPUpdateMessageParser;
27 import org.opendaylight.protocol.bgp.parser.impl.message.update.CommunityUtil;
28 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
29 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
30 import org.opendaylight.protocol.util.ByteArray;
31 import org.opendaylight.protocol.util.NoopReferenceCache;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.prefixes.DestinationIpv6Builder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.prefixes.destination.ipv6.Ipv6Prefixes;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.prefixes.destination.ipv6.Ipv6PrefixesBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv6CaseBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.UpdateBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.Aggregator;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.AggregatorBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.AsPathBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.AtomicAggregateBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.ClusterIdBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.Communities;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.ExtendedCommunities;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.ExtendedCommunitiesBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.LocalPrefBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.MultiExitDiscBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.OriginBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.OriginatorIdBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.UnrecognizedAttributes;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.as.path.Segments;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.as.path.SegmentsBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.message.Nlri;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.message.NlriBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.message.WithdrawnRoutesBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1Builder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlriBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpOrigin;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ClusterIdentifier;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteTargetIpv4CaseBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.target.ipv4._case.RouteTargetIpv4Builder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCase;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv6NextHopCase;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv6NextHopCaseBuilder;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.ipv4.next.hop._case.Ipv4NextHopBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.ipv6.next.hop._case.Ipv6NextHopBuilder;
83 public class BGPParserTest {
86 * Used by other tests as well
88 static final List<byte[]> inputBytes = new ArrayList<byte[]>();
90 private static int COUNTER = 7;
92 private static int MAX_SIZE = 300;
94 private static BGPUpdateMessageParser updateParser;
96 private static final int LENGTH_FIELD_LENGTH = 2;
99 public static void setUp() throws Exception {
100 updateParser = new BGPUpdateMessageParser(ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry());
101 for (int i = 1; i <= COUNTER; i++) {
102 final String name = "/up" + i + ".bin";
103 try (final InputStream is = BGPParserTest.class.getResourceAsStream(name)){
105 throw new IOException("Failed to get resource " + name);
107 final ByteArrayOutputStream bis = new ByteArrayOutputStream();
108 final byte[] data = new byte[MAX_SIZE];
110 while ((nRead = is.read(data, 0, data.length)) != -1) {
111 bis.write(data, 0, nRead);
115 inputBytes.add(bis.toByteArray());
123 public void testResource() {
124 assertNotNull(inputBytes);
128 * Tests IPv4 NEXT_HOP, ATOMIC_AGGREGATE, COMMUNITY, NLRI
130 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
131 * 00 54 <- length (84) - including header
133 * 00 00 <- withdrawn routes length
134 * 00 31 <- total path attribute length (49)
135 * 40 <- attribute flags
136 * 01 <- attribute type code (origin)
137 * 01 <- attribute length
138 * 00 <- Origin value (IGP)
139 * 40 <- attribute flags
140 * 02 <- attribute type code (as path)
141 * 06 <- attribute length
143 * 01 <- path segment count
144 * 00 00 fd ea <- path segment value (65002)
145 * 40 <- attribute flags
146 * 03 <- attribute type code (Next Hop)
147 * 04 <- attribute length
148 * 10 00 00 02 <- value (10.0.0.2)
149 * 80 <- attribute flags
150 * 04 <- attribute type code (multi exit disc)
151 * 04 <- attribute length
152 * 00 00 00 00 <- value
153 * 60 <- attribute flags
154 * 06 <- attribute type code (atomic aggregate)
155 * 00 <- attribute length
156 * 40 <- attribute flags
157 * 08 <- attribute type code (community)
158 * 10 <- attribute length FF FF FF
159 * 01 <- value (NO_EXPORT)
160 * FF FF FF 02 <- value (NO_ADVERTISE)
161 * FF FF FF 03 <- value (NO_EXPORT_SUBCONFED)
162 * FF FF FF 10 <- unknown Community
165 * 18 ac 11 02 <- IPv4 Prefix (172.17.2.0 / 24)
166 * 18 ac 11 01 <- IPv4 Prefix (172.17.1.0 / 24)
167 * 18 ac 11 00 <- IPv4 Prefix (172.17.0.0 / 24)
170 public void testGetUpdateMessage1() throws Exception {
172 final byte[] body = ByteArray.cutBytes(inputBytes.get(0), MessageUtil.COMMON_HEADER_LENGTH);
173 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(0), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
174 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
178 assertNull(message.getWithdrawnRoutes());
181 final List<AsNumber> asNumbers = new ArrayList<AsNumber>();
182 asNumbers.add(new AsNumber(65002L));
183 final List<Segments> asPath = Lists.newArrayList();
184 asPath.add(new SegmentsBuilder().setAsSequence(asNumbers).build());
186 final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
187 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.2")).build()).build();
189 final List<Communities> comms = Lists.newArrayList();
190 comms.add((Communities) CommunityUtil.NO_EXPORT);
191 comms.add((Communities) CommunityUtil.NO_ADVERTISE);
192 comms.add((Communities) CommunityUtil.NO_EXPORT_SUBCONFED);
193 comms.add((Communities) CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF10));
195 final UpdateBuilder builder = new UpdateBuilder();
199 final List<Ipv4Prefix> prefs = Lists.newArrayList();
200 prefs.add(new Ipv4Prefix("172.17.2.0/24"));
201 prefs.add(new Ipv4Prefix("172.17.1.0/24"));
202 prefs.add(new Ipv4Prefix("172.17.0.0/24"));
204 final Nlri nlri = new NlriBuilder().setNlri(prefs).build();
206 assertEquals(nlri, message.getNlri());
208 builder.setNlri(nlri);
210 // check path attributes
212 final Attributes attrs = message.getAttributes();
214 final AttributesBuilder paBuilder = new AttributesBuilder();
216 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
217 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
219 paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
220 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
222 paBuilder.setCNextHop(nextHop);
223 assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
225 paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
226 assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
228 paBuilder.setAtomicAggregate(new AtomicAggregateBuilder().build());
229 assertEquals(paBuilder.getAtomicAggregate(), attrs.getAtomicAggregate());
231 paBuilder.setCommunities(comms);
232 assertEquals(paBuilder.getCommunities(), attrs.getCommunities());
234 paBuilder.setUnrecognizedAttributes(Collections.<UnrecognizedAttributes> emptyList());
236 builder.setAttributes(paBuilder.build());
238 assertEquals(builder.build(), message);
240 final ByteBuf buffer = Unpooled.buffer();
241 BGPParserTest.updateParser.serializeMessage(message, buffer);
242 assertArrayEquals(inputBytes.get(0), ByteArray.readAllBytes(buffer));
246 * Tests IPv6 NEXT_HOP, NLRI, ORIGIN.IGP, MULTI_EXIT_DISC, ORIGINATOR-ID, CLUSTER_LIST.
248 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
249 * 00 80 <- length (128) - including header
251 * 00 00 <- withdrawn routes length
252 * 00 69 <- total path attribute length (105)
253 * 40 <- attribute flags
254 * 01 <- attribute type code (origin)
255 * 01 <- attribute length
256 * 00 <- Origin value (IGP)
257 * 40 <- attribute flags
258 * 02 <- attribute type code (as path)
259 * 06 <- attribute length
261 * 01 <- path segment count
262 * 00 00 fd e9 <- path segment value (65001)
263 * 40 <- attribute flags
264 * 03 <- attribute type code (next hop)
265 * 04 <- attribute length
266 * 0a 00 00 00 <- next hop value (10.0.0.0)
267 * 80 <- attribute flags
268 * 04 <- attribute type code (multi exit disc)
269 * 04 <- attribute length
270 * 00 00 00 00 <- value
271 * 80 <- attribute flags
272 * 09 <- attribute type code (originator id)
273 * 04 <- attribute length
274 * 7f 00 00 01 <- value (localhost ip)
275 * 80 <- attribute flags
276 * 0a <- attribute type code (cluster list)
277 * 08 <- attribute length
278 * 01 02 03 04 <- value
279 * 05 06 07 08 <- value
280 * 80 <- attribute flags
281 * 0e <- attribute type code (mp reach nlri)
282 * 40 <- attribute length
283 * 00 02 <- AFI (Ipv6)
284 * 01 <- SAFI (Unicast)
285 * 20 <- length of next hop
286 * 20 01 0d b8 00 00 00 00 00 00 00 00 00 00 00 01 <- global
287 * fe 80 00 00 00 00 00 00 c0 01 0b ff fe 7e 00 <- link local
291 * 40 20 01 0d b8 00 01 00 02 <- IPv6 Prefix (2001:db8:1:2:: / 64)
292 * 40 20 01 0d b8 00 01 00 01 <- IPv6 Prefix (2001:db8:1:1:: / 64)
293 * 40 20 01 0d b8 00 01 00 00 <- IPv6 Prefix (2001:db8:1:: / 64)
297 public void testGetUpdateMessage2() throws Exception {
298 final byte[] body = ByteArray.cutBytes(inputBytes.get(1), MessageUtil.COMMON_HEADER_LENGTH);
299 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(1), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
300 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
303 assertNull(message.getWithdrawnRoutes());
305 final UpdateBuilder builder = new UpdateBuilder();
309 final List<Ipv6Prefixes> prefs = new ArrayList<>();
310 prefs.add(new Ipv6PrefixesBuilder().setPrefix(new Ipv6Prefix("2001:db8:1:2::/64")).build());
311 prefs.add(new Ipv6PrefixesBuilder().setPrefix(new Ipv6Prefix("2001:db8:1:1::/64")).build());
312 prefs.add(new Ipv6PrefixesBuilder().setPrefix(new Ipv6Prefix("2001:db8:1::/64")).build());
314 assertNull(message.getNlri());
317 final List<AsNumber> asNumbers = new ArrayList<AsNumber>();
318 asNumbers.add(new AsNumber(65001L));
319 final List<Segments> asPath = Lists.newArrayList();
320 asPath.add(new SegmentsBuilder().setAsSequence(asNumbers).build());
322 final Ipv6NextHopCase nextHop = new Ipv6NextHopCaseBuilder().setIpv6NextHop(
323 new Ipv6NextHopBuilder().setGlobal(new Ipv6Address("2001:db8::1")).setLinkLocal(new Ipv6Address("fe80::c001:bff:fe7e:0")).build()).build();
325 final List<ClusterIdentifier> clusters = Lists.newArrayList(new ClusterIdentifier(new Ipv4Address("1.2.3.4")),
326 new ClusterIdentifier(new Ipv4Address("5.6.7.8")));
328 // check path attributes
330 final Attributes attrs = message.getAttributes();
332 final AttributesBuilder paBuilder = new AttributesBuilder();
334 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
335 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
337 paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
338 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
340 paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
341 assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
343 paBuilder.setOriginatorId(new OriginatorIdBuilder().setOriginator(new Ipv4Address("127.0.0.1")).build());
344 assertEquals(paBuilder.getOriginatorId(), attrs.getOriginatorId());
346 paBuilder.setClusterId(new ClusterIdBuilder().setCluster(clusters).build());
347 assertEquals(paBuilder.getClusterId(), attrs.getClusterId());
349 final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
350 mpBuilder.setAfi(Ipv6AddressFamily.class);
351 mpBuilder.setSafi(UnicastSubsequentAddressFamily.class);
352 mpBuilder.setCNextHop(nextHop);
353 mpBuilder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
354 new DestinationIpv6CaseBuilder().setDestinationIpv6(new DestinationIpv6Builder().setIpv6Prefixes(prefs).build()).build()).build());
356 paBuilder.addAugmentation(Attributes1.class, new Attributes1Builder().setMpReachNlri(mpBuilder.build()).build());
357 assertEquals(paBuilder.getAugmentation(Attributes1.class).getMpReachNlri(),
358 attrs.getAugmentation(Attributes1.class).getMpReachNlri());
359 paBuilder.setUnrecognizedAttributes(Collections.<UnrecognizedAttributes> emptyList());
362 builder.setAttributes(paBuilder.build());
363 assertEquals(builder.build(), message);
365 final ByteBuf buffer = Unpooled.buffer();
366 BGPParserTest.updateParser.serializeMessage(message, buffer);
367 assertArrayEquals(inputBytes.get(1), ByteArray.readAllBytes(buffer));
371 * Tests more AS Numbers in AS_PATH, AGGREGATOR, ORIGIN.INCOMPLETE
373 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
374 * 00 4b <- length (75) - including header
376 * 00 00 <- withdrawn routes length
377 * 00 30 <- total path attribute length (48)
378 * 40 <- attribute flags
379 * 01 <- attribute type code (origin)
380 * 01 <- attribute length
381 * 02 <- Origin value (Incomplete)
382 * 40 <- attribute flags
383 * 02 <- attribute type code (as path)
384 * 10 <- attribute length
386 * 01 <- path segment count
387 * 00 00 00 1e <- path segment value (30)
389 * 02 <- path segment count
390 * 00 00 00 0a <- path segment value (10)
391 * 00 00 00 14 <- path segment value (20)
392 * 40 <- attribute flags
393 * 03 <- attribute type (Next hop)
394 * 04 <- attribute length
395 * 0a 00 00 09 <- value (10.0.0.9)
396 * 80 <- attribute flags
397 * 04 <- attribute type code (multi exit disc)
398 * 04 <- attribute length
399 * 00 00 00 00 <- value
400 * c0 <- attribute flags
401 * 07 <- attribute type (Aggregator)
402 * 08 <- attribute length
403 * 00 00 00 1e <- value (AS number = 30)
404 * 0a 00 00 09 <- value (IP address = 10.0.0.9)
407 * 15 ac 10 00 <- IPv4 Prefix (172.16.0.0 / 21)
410 public void testGetUpdateMessage3() throws Exception {
411 final byte[] body = ByteArray.cutBytes(inputBytes.get(2), MessageUtil.COMMON_HEADER_LENGTH);
412 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(2), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
413 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
415 final UpdateBuilder builder = new UpdateBuilder();
418 final Ipv4Prefix pref1 = new Ipv4Prefix("172.16.0.0/21");
420 final List<Ipv4Prefix> nlri = Lists.newArrayList(pref1);
421 builder.setNlri(new NlriBuilder().setNlri(nlri).build());
422 assertEquals(builder.getNlri(), message.getNlri());
425 assertNull(message.getWithdrawnRoutes());
428 final List<AsNumber> asNumbers = new ArrayList<AsNumber>();
429 asNumbers.add(new AsNumber(30L));
430 final List<Segments> asPath = Lists.newArrayList();
431 asPath.add(new SegmentsBuilder().setAsSequence(asNumbers).build());
432 final List<AsNumber> asSet = Lists.newArrayList(new AsNumber(10L), new AsNumber(20L));
433 asPath.add(new SegmentsBuilder().setAsSet(asSet).build());
435 final Aggregator aggregator = new AggregatorBuilder().setAsNumber(new AsNumber((long) 30)).setNetworkAddress(
436 new Ipv4Address("10.0.0.9")).build();
437 final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
438 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.9")).build()).build();
440 // check path attributes
441 final Attributes attrs = message.getAttributes();
443 final AttributesBuilder paBuilder = new AttributesBuilder();
445 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Incomplete).build());
446 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
448 paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
449 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
451 paBuilder.setCNextHop(nextHop);
452 assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
454 paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
455 assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
457 paBuilder.setAggregator(aggregator);
458 assertEquals(paBuilder.getAggregator(), attrs.getAggregator());
459 paBuilder.setUnrecognizedAttributes(Collections.<UnrecognizedAttributes> emptyList());
460 builder.setAttributes(paBuilder.build());
462 assertEquals(builder.build(), message);
464 final ByteBuf buffer = Unpooled.buffer();
465 BGPParserTest.updateParser.serializeMessage(message, buffer);
466 assertArrayEquals(inputBytes.get(2), ByteArray.readAllBytes(buffer));
470 * Tests empty AS_PATH, ORIGIN.EGP, LOCAL_PREF, EXTENDED_COMMUNITIES (Ipv4 Addr specific)
472 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
473 * 00 4A <- length (73) - including header
475 * 00 00 <- withdrawn routes length
476 * 00 27 <- total path attribute length (39)
477 * 40 <- attribute flags
478 * 01 <- attribute type code (Origin)
479 * 01 <- attribute length
480 * 01 <- Origin value (EGP)
481 * 40 <- attribute flags
482 * 02 <- attribute type code (As path)
483 * 00 <- attribute length
484 * 40 <- attribute flags
485 * 03 <- attribute type (Next hop)
486 * 04 <- attribute length
487 * 03 03 03 03 <- value (3.3.3.3)
488 * 80 <- attribute flags
489 * 04 <- attribute type code (Multi exit disc)
490 * 04 <- attribute length
491 * 00 00 00 00 <- value
492 * 40 <- attribute flags
493 * 05 <- attribute type (Local Pref)
494 * 04 <- attribute length
495 * 00 00 00 64 <- value (100)
496 * c0 <- attribute flags
497 * 10 <- attribute type (extended community)
498 * 08 <- attribute length
499 * 01 04 <- value (type - Ipv4 Address Specific Extended Community)
500 * c0 a8 01 00 <- value (global adm. 198.162.1.0)
501 * 12 34 <- value (local adm. 4660)
504 * 18 0a 1e 03 <- IPv4 Prefix (10.30.3.0 / 24)
505 * 18 0a 1e 02 <- IPv4 Prefix (10.30.2.0 / 24)
506 * 18 0a 1e 01 <- IPv4 Prefix (10.30.1.0 / 24)
509 public void testGetUpdateMessage4() throws Exception {
510 final byte[] body = ByteArray.cutBytes(inputBytes.get(3), MessageUtil.COMMON_HEADER_LENGTH);
511 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(3), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
512 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
514 final UpdateBuilder builder = new UpdateBuilder();
517 assertNull(message.getWithdrawnRoutes());
520 final Ipv4Prefix pref1 = new Ipv4Prefix("10.30.3.0/24");
521 final Ipv4Prefix pref2 = new Ipv4Prefix("10.30.2.0/24");
522 final Ipv4Prefix pref3 = new Ipv4Prefix("10.30.1.0/24");
524 final List<Ipv4Prefix> nlri = Lists.newArrayList(pref1, pref2, pref3);
525 builder.setNlri(new NlriBuilder().setNlri(nlri).build());
526 assertEquals(builder.getNlri(), message.getNlri());
529 final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
530 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("3.3.3.3")).build()).build();
532 final List<ExtendedCommunities> comms = Lists.newArrayList();
533 comms.add(new ExtendedCommunitiesBuilder().setTransitive(true).setExtendedCommunity(
534 new RouteTargetIpv4CaseBuilder().setRouteTargetIpv4(
535 new RouteTargetIpv4Builder().setGlobalAdministrator(
536 new Ipv4Address("192.168.1.0")).setLocalAdministrator(4660).build()).build()).build());
538 // check path attributes
539 final Attributes attrs = message.getAttributes();
541 final AttributesBuilder paBuilder = new AttributesBuilder();
543 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Egp).build());
544 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
546 paBuilder.setAsPath(new AsPathBuilder().setSegments(Collections.<Segments> emptyList()).build());
547 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
549 paBuilder.setCNextHop(nextHop);
550 assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
552 paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
553 assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
555 paBuilder.setLocalPref(new LocalPrefBuilder().setPref(100L).build());
556 assertEquals(paBuilder.getLocalPref(), attrs.getLocalPref());
558 paBuilder.setExtendedCommunities(comms);
559 assertEquals(paBuilder.getExtendedCommunities(), attrs.getExtendedCommunities());
561 paBuilder.setUnrecognizedAttributes(Collections.<UnrecognizedAttributes> emptyList());
563 builder.setAttributes(paBuilder.build());
564 assertEquals(builder.build(), message);
566 final ByteBuf buffer = Unpooled.buffer();
567 BGPParserTest.updateParser.serializeMessage(message, buffer);
568 assertArrayEquals(inputBytes.get(3), ByteArray.readAllBytes(buffer));
572 * Tests withdrawn routes.
574 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
575 * 00 1c <- length (28) - including header
577 * 00 05 <- withdrawn routes length (5)
578 * 1e ac 10 00 04 <- route (172.16.0.4)
579 * 00 00 <- total path attribute length
582 public void testGetUpdateMessage5() throws Exception {
583 final byte[] body = ByteArray.cutBytes(inputBytes.get(4), MessageUtil.COMMON_HEADER_LENGTH);
584 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(4), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
585 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
588 final List<Ipv4Prefix> prefs = Lists.newArrayList(new Ipv4Prefix("172.16.0.4/30"));
591 final Update expectedMessage = new UpdateBuilder().setWithdrawnRoutes(
592 new WithdrawnRoutesBuilder().setWithdrawnRoutes(prefs).build()).build();
594 assertEquals(expectedMessage.getWithdrawnRoutes(), message.getWithdrawnRoutes());
596 final ByteBuf buffer = Unpooled.buffer();
597 BGPParserTest.updateParser.serializeMessage(message, buffer);
598 assertArrayEquals(inputBytes.get(4), ByteArray.readAllBytes(buffer));
602 * End of Rib for Ipv4.
604 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
605 * 00 17 <- length (23) - including header
607 * 00 00 <- withdrawn routes length
608 * 00 00 <- total path attribute length
611 public void testEORIpv4() throws Exception {
612 final byte[] body = ByteArray.cutBytes(inputBytes.get(5), MessageUtil.COMMON_HEADER_LENGTH);
613 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(5), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
614 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
616 assertEquals(new UpdateBuilder().build(), message);
618 final ByteBuf buffer = Unpooled.buffer();
619 BGPParserTest.updateParser.serializeMessage(message, buffer);
620 assertArrayEquals(inputBytes.get(5), ByteArray.readAllBytes(buffer));
624 * End of Rib for Ipv6 consists of empty MP_UNREACH_NLRI, with AFI 2 and SAFI 1
626 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
627 * 00 1d <- length (29) - including header
629 * 00 00 <- withdrawn routes length
630 * 00 06 <- total path attribute length
631 * 80 <- attribute flags
632 * 0f <- attribute type (15 - MP_UNREACH_NLRI)
633 * 03 <- attribute length
634 * 00 02 <- value (AFI 2: IPv6)
635 * 01 <- value (SAFI 1)
638 public void testEORIpv6() throws Exception {
639 final byte[] body = ByteArray.cutBytes(inputBytes.get(6), MessageUtil.COMMON_HEADER_LENGTH);
640 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(6), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
641 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
643 final Class<? extends AddressFamily> afi = message.getAttributes().getAugmentation(Attributes2.class).getMpUnreachNlri().getAfi();
644 final Class<? extends SubsequentAddressFamily> safi = message.getAttributes().getAugmentation(Attributes2.class).getMpUnreachNlri().getSafi();
646 assertEquals(Ipv6AddressFamily.class, afi);
647 assertEquals(UnicastSubsequentAddressFamily.class, safi);
649 final ByteBuf buffer = Unpooled.buffer();
650 BGPParserTest.updateParser.serializeMessage(message, buffer);
651 assertArrayEquals(inputBytes.get(6), ByteArray.readAllBytes(buffer));
655 * End of Rib for Ipv6 consists of empty MP_UNREACH_NLRI, with AFI 2 and SAFI 1
657 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
658 * 00 1e <- length (29) - including header
660 * 00 00 <- withdrawn routes length
661 * 00 07 <- total path attribute length
662 * 90 <- attribute flags
663 * 0f <- attribute type (15 - MP_UNREACH_NLRI)
664 * 00 03 <- attribute length
665 * 00 02 <- value (AFI 2: IPv6)
666 * 01 <- value (SAFI 1)
669 public void testEORIpv6exLength() throws Exception {
670 final byte[] body = ByteArray.cutBytes(inputBytes.get(6), MessageUtil.COMMON_HEADER_LENGTH);
671 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(6), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
672 final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
674 final Class<? extends AddressFamily> afi = message.getAttributes().getAugmentation(Attributes2.class).getMpUnreachNlri().getAfi();
675 final Class<? extends SubsequentAddressFamily> safi = message.getAttributes().getAugmentation(Attributes2.class).getMpUnreachNlri().getSafi();
677 assertEquals(Ipv6AddressFamily.class, afi);
678 assertEquals(UnicastSubsequentAddressFamily.class, safi);
680 final ByteBuf buffer = Unpooled.buffer();
681 BGPParserTest.updateParser.serializeMessage(message, buffer);
682 assertArrayEquals(inputBytes.get(6), ByteArray.readAllBytes(buffer));