MVPN RFC6514 Extendend communities
[bgpcep.git] / bgp / parser-impl / src / test / java / org / opendaylight / protocol / bgp / parser / impl / BGPParserTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.parser.impl;
9
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;
15
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;
73
74 public class BGPParserTest {
75
76     /**
77      * Used by other tests as well
78      */
79     static final List<byte[]> inputBytes = new ArrayList<>();
80
81     private static int COUNTER = 7;
82
83     private static int MAX_SIZE = 300;
84
85     private static BGPUpdateMessageParser updateParser;
86
87     private static final int LENGTH_FIELD_LENGTH = 2;
88
89     private static final String multiPathHexFile = "/bgp-update-multipath.txt";
90
91     private static List<byte[]> updatesWithMultiplePath;
92
93     private static PeerSpecificParserConstraint constraint;
94
95     private static MultiPathSupport mpSupport;
96
97     @BeforeClass
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)){
103                 if (is == null) {
104                     throw new IOException("Failed to get resource " + name);
105                 }
106                 final ByteArrayOutputStream bis = new ByteArrayOutputStream();
107                 final byte[] data = new byte[MAX_SIZE];
108                 int nRead = 0;
109                 while ((nRead = is.read(data, 0, data.length)) != -1) {
110                     bis.write(data, 0, nRead);
111                 }
112                 bis.flush();
113
114                 inputBytes.add(bis.toByteArray());
115                 is.close();
116             }
117         }
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());
123     }
124
125     @Test
126     public void testResource() {
127         assertNotNull(inputBytes);
128     }
129
130     /*
131      * Tests IPv4 NEXT_HOP, ATOMIC_AGGREGATE, COMMUNITY, NLRI
132      *
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
135      * 02 <- message type
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
145      * 02 <- AS_SEQUENCE
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
166      *
167      * //NLRI
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)
171      */
172     @Test
173     public void testGetUpdateMessage1() throws Exception {
174
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);
178
179         // check fields
180
181         assertNull(message.getWithdrawnRoutes());
182
183         // attributes
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());
188
189         final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
190                 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.2")).build()).build();
191
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));
197
198         final UpdateBuilder builder = new UpdateBuilder();
199
200         // check nlri
201
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());
206
207         assertEquals(nlris, message.getNlri());
208
209         builder.setNlri(nlris);
210
211         // check path attributes
212
213         final Attributes attrs = message.getAttributes();
214
215         final AttributesBuilder paBuilder = new AttributesBuilder();
216
217         paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
218         assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
219
220         paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
221         assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
222
223         paBuilder.setCNextHop(nextHop);
224         assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
225
226         paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
227         assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
228
229         paBuilder.setAtomicAggregate(new AtomicAggregateBuilder().build());
230         assertEquals(paBuilder.getAtomicAggregate(), attrs.getAtomicAggregate());
231
232         paBuilder.setCommunities(comms);
233         assertEquals(paBuilder.getCommunities(), attrs.getCommunities());
234
235         paBuilder.setUnrecognizedAttributes(Collections.emptyList());
236
237         builder.setAttributes(paBuilder.build());
238
239         assertEquals(builder.build(), message);
240
241         final ByteBuf buffer = Unpooled.buffer();
242         BGPParserTest.updateParser.serializeMessage(message, buffer);
243         assertArrayEquals(inputBytes.get(0), ByteArray.readAllBytes(buffer));
244     }
245
246     /*
247      * Tests more AS Numbers in AS_PATH, AGGREGATOR, ORIGIN.INCOMPLETE
248      *
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
251      * 02 <- message type
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
261      * 02 <- AS_SEQUENCE
262      * 01 <- path segment count
263      * 00 00 00 1e <- path segment value (30)
264      * 01 <- AS_SET
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)
281      *
282      * //NLRI
283      * 15 ac 10 00 <- IPv4 Prefix (172.16.0.0 / 21)
284      */
285     @Test
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);
290
291         final UpdateBuilder builder = new UpdateBuilder();
292
293         // check nlri
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());
297
298         // check fields
299         assertNull(message.getWithdrawnRoutes());
300
301         // attributes
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());
308
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();
313
314         // check path attributes
315         final Attributes attrs = message.getAttributes();
316
317         final AttributesBuilder paBuilder = new AttributesBuilder();
318
319         paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Incomplete).build());
320         assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
321
322         paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
323         assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
324
325         paBuilder.setCNextHop(nextHop);
326         assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
327
328         paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
329         assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
330
331         paBuilder.setAggregator(aggregator);
332         assertEquals(paBuilder.getAggregator(), attrs.getAggregator());
333         paBuilder.setUnrecognizedAttributes(Collections.emptyList());
334         builder.setAttributes(paBuilder.build());
335
336         assertEquals(builder.build(), message);
337
338         final ByteBuf buffer = Unpooled.buffer();
339         BGPParserTest.updateParser.serializeMessage(message, buffer);
340         assertArrayEquals(inputBytes.get(2), ByteArray.readAllBytes(buffer));
341     }
342
343     /*
344      * Tests empty AS_PATH, ORIGIN.EGP, LOCAL_PREF, EXTENDED_COMMUNITIES (Ipv4 Addr specific)
345      *
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
348      * 02 <- message type
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)
376      *
377      * //NLRI
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)
381      */
382     @Test
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);
387
388         final UpdateBuilder builder = new UpdateBuilder();
389
390         // check fields
391         assertNull(message.getWithdrawnRoutes());
392
393         // check nlri
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());
398
399         assertEquals(nlris, message.getNlri());
400
401         builder.setNlri(nlris);
402
403         // attributes
404         final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
405                 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("3.3.3.3")).build()).build();
406
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());
412
413         // check path attributes
414         final Attributes attrs = message.getAttributes();
415
416         final AttributesBuilder paBuilder = new AttributesBuilder();
417
418         paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Egp).build());
419         assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
420
421         paBuilder.setAsPath(new AsPathBuilder().setSegments(Collections.emptyList()).build());
422         assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
423
424         paBuilder.setCNextHop(nextHop);
425         assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
426
427         paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
428         assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
429
430         paBuilder.setLocalPref(new LocalPrefBuilder().setPref(100L).build());
431         assertEquals(paBuilder.getLocalPref(), attrs.getLocalPref());
432
433         paBuilder.setExtendedCommunities(comms);
434         assertEquals(paBuilder.getExtendedCommunities(), attrs.getExtendedCommunities());
435
436         paBuilder.setUnrecognizedAttributes(Collections.emptyList());
437         // check API message
438         builder.setAttributes(paBuilder.build());
439         assertEquals(builder.build(), message);
440
441         final ByteBuf buffer = Unpooled.buffer();
442         BGPParserTest.updateParser.serializeMessage(message, buffer);
443         assertArrayEquals(inputBytes.get(3), ByteArray.readAllBytes(buffer));
444     }
445
446     /*
447      * Tests withdrawn routes.
448      *
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
451      * 02 <- message type
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
455      */
456     @Test
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);
461
462         // attributes
463         final List<WithdrawnRoutes> withdrawnRoutes = Lists.newArrayList(new WithdrawnRoutesBuilder().setPrefix(new Ipv4Prefix("172.16.0.4/30")).build());
464
465         // check API message
466         final Update expectedMessage = new UpdateBuilder().setWithdrawnRoutes(withdrawnRoutes).build();
467
468         assertEquals(expectedMessage.getWithdrawnRoutes(), message.getWithdrawnRoutes());
469
470         final ByteBuf buffer = Unpooled.buffer();
471         BGPParserTest.updateParser.serializeMessage(message, buffer);
472         assertArrayEquals(inputBytes.get(4), ByteArray.readAllBytes(buffer));
473     }
474
475     /*
476      * End of Rib for Ipv4.
477      *
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
480      * 02 <- message type
481      * 00 00 <- withdrawn routes length
482      * 00 00 <- total path attribute length
483      */
484     @Test
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);
489
490         assertEquals(new UpdateBuilder().build(), message);
491
492         final ByteBuf buffer = Unpooled.buffer();
493         BGPParserTest.updateParser.serializeMessage(message, buffer);
494         assertArrayEquals(inputBytes.get(5), ByteArray.readAllBytes(buffer));
495     }
496
497     /*
498      * End of Rib for Ipv6 consists of empty MP_UNREACH_NLRI, with AFI 2 and SAFI 1
499      *
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
502      * 02 <- message type
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)
510      */
511     @Test
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);
516
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();
519
520         assertEquals(Ipv6AddressFamily.class, afi);
521         assertEquals(UnicastSubsequentAddressFamily.class, safi);
522
523         final ByteBuf buffer = Unpooled.buffer();
524         BGPParserTest.updateParser.serializeMessage(message, buffer);
525         assertArrayEquals(inputBytes.get(6), ByteArray.readAllBytes(buffer));
526     }
527
528     /*
529      * End of Rib for Ipv6 consists of empty MP_UNREACH_NLRI, with AFI 2 and SAFI 1
530      *
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
533      * 02 <- message type
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)
541      */
542     @Test
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);
547
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();
550
551         assertEquals(Ipv6AddressFamily.class, afi);
552         assertEquals(UnicastSubsequentAddressFamily.class, safi);
553
554         final ByteBuf buffer = Unpooled.buffer();
555         BGPParserTest.updateParser.serializeMessage(message, buffer);
556         assertArrayEquals(inputBytes.get(6), ByteArray.readAllBytes(buffer));
557     }
558
559     /*
560      * Tests IPv4 NEXT_HOP, ATOMIC_AGGREGATE, COMMUNITY, NLRI with multiple paths.
561      *
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
564      * 02 <- message type
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
574      * 02 <- AS_SEQUENCE
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
595      *
596      * //NLRI
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)
603      */
604     @Test
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);
609
610         // check fields
611
612         assertNull(message.getWithdrawnRoutes());
613
614         // attributes
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());
619
620         final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
621                 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.2")).build()).build();
622
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));
628
629         final UpdateBuilder builder = new UpdateBuilder();
630
631         // check nlri
632
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());
637
638         assertEquals(nlris, message.getNlri());
639
640         builder.setNlri(nlris);
641
642         // check path attributes
643
644         final Attributes attrs = message.getAttributes();
645
646         final AttributesBuilder paBuilder = new AttributesBuilder();
647
648         paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
649         assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
650
651         paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
652         assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
653
654         paBuilder.setCNextHop(nextHop);
655         assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
656
657         paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
658         assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
659
660         paBuilder.setAtomicAggregate(new AtomicAggregateBuilder().build());
661         assertEquals(paBuilder.getAtomicAggregate(), attrs.getAtomicAggregate());
662
663         paBuilder.setCommunities(comms);
664         assertEquals(paBuilder.getCommunities(), attrs.getCommunities());
665
666         paBuilder.setUnrecognizedAttributes(Collections.emptyList());
667
668         builder.setAttributes(paBuilder.build());
669
670         assertEquals(builder.build(), message);
671
672         final ByteBuf buffer = Unpooled.buffer();
673         BGPParserTest.updateParser.serializeMessage(message, buffer);
674         assertArrayEquals(updatesWithMultiplePath.get(0), ByteArray.readAllBytes(buffer));
675     }
676
677     /*
678      * Tests withdrawn routes with multiple paths.
679      *
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
682      * 02 <- message type
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
689      */
690     @Test
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);
695
696         // attributes
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());
700
701         // check API message
702         final Update expectedMessage = new UpdateBuilder().setWithdrawnRoutes(withdrawnRoutes).build();
703
704         assertEquals(expectedMessage.getWithdrawnRoutes(), message.getWithdrawnRoutes());
705
706         final ByteBuf buffer = Unpooled.buffer();
707         BGPParserTest.updateParser.serializeMessage(message, buffer);
708         assertArrayEquals(updatesWithMultiplePath.get(1), ByteArray.readAllBytes(buffer));
709     }
710 }