JUnit test for treat-as-withdraw procedures
[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.BGPDocumentedException;
30 import org.opendaylight.protocol.bgp.parser.impl.message.BGPUpdateMessageParser;
31 import org.opendaylight.protocol.bgp.parser.impl.message.update.CommunityUtil;
32 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
33 import org.opendaylight.protocol.bgp.parser.spi.MultiPathSupport;
34 import org.opendaylight.protocol.bgp.parser.spi.NlriRegistry;
35 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
36 import org.opendaylight.protocol.bgp.parser.spi.RevisedErrorHandlingSupport;
37 import org.opendaylight.protocol.bgp.parser.spi.pojo.PeerSpecificParserConstraintImpl;
38 import org.opendaylight.protocol.bgp.parser.spi.pojo.RevisedErrorHandlingSupportImpl;
39 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
40 import org.opendaylight.protocol.bgp.util.HexDumpBGPFileParser;
41 import org.opendaylight.protocol.util.ByteArray;
42 import org.opendaylight.protocol.util.NoopReferenceCache;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.UpdateBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.Aggregator;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.AggregatorBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.AsPathBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.AtomicAggregateBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.Communities;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.ExtendedCommunities;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.ExtendedCommunitiesBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.LocalPrefBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.MultiExitDiscBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.OriginBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.as.path.Segments;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.as.path.SegmentsBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.update.message.Nlri;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.update.message.NlriBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.update.message.WithdrawnRoutes;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.update.message.WithdrawnRoutesBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.BgpOrigin;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv6AddressFamily;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.SubsequentAddressFamily;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.RouteTargetIpv4CaseBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv4NextHopCase;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.ipv4.next.hop._case.Ipv4NextHopBuilder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.route.target.ipv4.grouping.RouteTargetIpv4Builder;
78
79 public class BGPParserTest {
80     private static final List<byte[]> INPUT_BYTES = new ArrayList<>();
81
82     private static final int COUNTER = 8;
83
84     private static final int MAX_SIZE = 300;
85
86     private static BGPUpdateMessageParser updateParser;
87
88     private static final int LENGTH_FIELD_LENGTH = 2;
89
90     private static final String MULTIPATH_HEX_FILE = "/bgp-update-multipath.txt";
91
92     private static List<byte[]> updatesWithMultiplePath;
93
94     private static PeerSpecificParserConstraint mpConstraint;
95
96     private static MultiPathSupport mpSupport;
97
98     @BeforeClass
99     public static void setUp() throws Exception {
100         updateParser = new BGPUpdateMessageParser(ServiceLoaderBGPExtensionProviderContext.getSingletonInstance()
101             .getAttributeRegistry(), mock(NlriRegistry.class));
102         for (int i = 1; i <= COUNTER; i++) {
103             final String name = "/up" + i + ".bin";
104             try (InputStream is = BGPParserTest.class.getResourceAsStream(name)) {
105                 if (is == null) {
106                     throw new IOException("Failed to get resource " + name);
107                 }
108                 final ByteArrayOutputStream bis = new ByteArrayOutputStream();
109                 final byte[] data = new byte[MAX_SIZE];
110                 int numRead = 0;
111                 while ((numRead = is.read(data, 0, data.length)) != -1) {
112                     bis.write(data, 0, numRead);
113                 }
114                 bis.flush();
115
116                 INPUT_BYTES.add(bis.toByteArray());
117                 is.close();
118             }
119         }
120         updatesWithMultiplePath = HexDumpBGPFileParser.parseMessages(BGPParserTest.class.getResourceAsStream(
121             MULTIPATH_HEX_FILE));
122         mpConstraint = mock(PeerSpecificParserConstraint.class);
123         mpSupport = mock(MultiPathSupport.class);
124         Mockito.doReturn(Optional.empty()).when(mpConstraint).getPeerConstraint(Mockito.any());
125         Mockito.doReturn(Optional.of(mpSupport)).when(mpConstraint).getPeerConstraint(MultiPathSupport.class);
126         Mockito.doReturn(true).when(mpSupport).isTableTypeSupported(Mockito.any());
127     }
128
129     @Test
130     public void testResource() {
131         assertNotNull(INPUT_BYTES);
132     }
133
134     /*
135      * Tests IPv4 NEXT_HOP, ATOMIC_AGGREGATE, COMMUNITY, NLRI
136      *
137      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
138      * 00 54 <- length (84) - including header
139      * 02 <- message type
140      * 00 00 <- withdrawn routes length
141      * 00 31 <- total path attribute length (49)
142      * 40 <- attribute flags
143      * 01 <- attribute type code (origin)
144      * 01 <- attribute length
145      * 00 <- Origin value (IGP)
146      * 40 <- attribute flags
147      * 02 <- attribute type code (as path)
148      * 06 <- attribute length
149      * 02 <- AS_SEQUENCE
150      * 01 <- path segment count
151      * 00 00 fd ea <- path segment value (65002)
152      * 40 <- attribute flags
153      * 03 <- attribute type code (Next Hop)
154      * 04 <- attribute length
155      * 10 00 00 02 <- value (10.0.0.2)
156      * 80 <- attribute flags
157      * 04 <- attribute type code (multi exit disc)
158      * 04 <- attribute length
159      * 00 00 00 00 <- value
160      * 60 <- attribute flags
161      * 06 <- attribute type code (atomic aggregate)
162      * 00 <- attribute length
163      * 40 <- attribute flags
164      * 08 <- attribute type code (community)
165      * 10 <- attribute length FF FF FF
166      * 01 <- value (NO_EXPORT)
167      * FF FF FF 02 <- value (NO_ADVERTISE)
168      * FF FF FF 03 <- value (NO_EXPORT_SUBCONFED)
169      * FF FF FF 10 <- unknown Community
170      *
171      * //NLRI
172      * 18 ac 11 02 <- IPv4 Prefix (172.17.2.0 / 24)
173      * 18 ac 11 01 <- IPv4 Prefix (172.17.1.0 / 24)
174      * 18 ac 11 00 <- IPv4 Prefix (172.17.0.0 / 24)
175      */
176     @Test
177     public void testGetUpdateMessage1() throws Exception {
178
179         final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(0), MessageUtil.COMMON_HEADER_LENGTH);
180         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(0), MessageUtil.MARKER_LENGTH,
181             LENGTH_FIELD_LENGTH));
182         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
183             null);
184
185         // check fields
186
187         assertNull(message.getWithdrawnRoutes());
188
189         // attributes
190         final List<AsNumber> asNumbers = new ArrayList<>();
191         asNumbers.add(new AsNumber(65002L));
192         final List<Segments> asPath = Lists.newArrayList();
193         asPath.add(new SegmentsBuilder().setAsSequence(asNumbers).build());
194
195         final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
196                 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.2")).build()).build();
197
198         final List<Communities> comms = Lists.newArrayList();
199         comms.add((Communities) CommunityUtil.NO_EXPORT);
200         comms.add((Communities) CommunityUtil.NO_ADVERTISE);
201         comms.add((Communities) CommunityUtil.NO_EXPORT_SUBCONFED);
202         comms.add((Communities) CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF10));
203
204         final UpdateBuilder builder = new UpdateBuilder();
205
206         // check nlri
207
208         final List<Nlri> nlris = Lists.newArrayList();
209         nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.2.0/24")).build());
210         nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.1.0/24")).build());
211         nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.0.0/24")).build());
212
213         assertEquals(nlris, message.getNlri());
214
215         builder.setNlri(nlris);
216
217         // check path attributes
218
219         final Attributes attrs = message.getAttributes();
220
221         final AttributesBuilder paBuilder = new AttributesBuilder();
222
223         paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
224         assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
225
226         paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
227         assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
228
229         paBuilder.setCNextHop(nextHop);
230         assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
231
232         paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
233         assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
234
235         paBuilder.setAtomicAggregate(new AtomicAggregateBuilder().build());
236         assertEquals(paBuilder.getAtomicAggregate(), attrs.getAtomicAggregate());
237
238         paBuilder.setCommunities(comms);
239         assertEquals(paBuilder.getCommunities(), attrs.getCommunities());
240
241         paBuilder.setUnrecognizedAttributes(Collections.emptyList());
242
243         builder.setAttributes(paBuilder.build());
244
245         assertEquals(builder.build(), message);
246
247         final ByteBuf buffer = Unpooled.buffer();
248         BGPParserTest.updateParser.serializeMessage(message, buffer);
249         assertArrayEquals(INPUT_BYTES.get(0), ByteArray.readAllBytes(buffer));
250     }
251
252     /*
253      * Tests more AS Numbers in AS_PATH, AGGREGATOR, ORIGIN.INCOMPLETE
254      *
255      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
256      * 00 4b <- length (75) - including header
257      * 02 <- message type
258      * 00 00 <- withdrawn routes length
259      * 00 30 <- total path attribute length (48)
260      * 40 <- attribute flags
261      * 01 <- attribute type code (origin)
262      * 01 <- attribute length
263      * 02 <- Origin value (Incomplete)
264      * 40 <- attribute flags
265      * 02 <- attribute type code (as path)
266      * 10 <- attribute length
267      * 02 <- AS_SEQUENCE
268      * 01 <- path segment count
269      * 00 00 00 1e <- path segment value (30)
270      * 01 <- AS_SET
271      * 02 <- path segment count
272      * 00 00 00 0a <- path segment value (10)
273      * 00 00 00 14 <- path segment value (20)
274      * 40 <- attribute flags
275      * 03 <- attribute type (Next hop)
276      * 04 <- attribute length
277      * 0a 00 00 09 <- value (10.0.0.9)
278      * 80 <- attribute flags
279      * 04 <- attribute type code (multi exit disc)
280      * 04 <- attribute length
281      * 00 00 00 00 <- value
282      * c0 <- attribute flags
283      * 07 <- attribute type (Aggregator)
284      * 08 <- attribute length
285      * 00 00 00 1e <- value (AS number = 30)
286      * 0a 00 00 09 <- value (IP address = 10.0.0.9)
287      *
288      * //NLRI
289      * 15 ac 10 00 <- IPv4 Prefix (172.16.0.0 / 21)
290      */
291     @Test
292     public void testGetUpdateMessage3() throws Exception {
293         final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(2), MessageUtil.COMMON_HEADER_LENGTH);
294         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(2), MessageUtil.MARKER_LENGTH,
295             LENGTH_FIELD_LENGTH));
296         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
297             null);
298
299         final UpdateBuilder builder = new UpdateBuilder();
300
301         // check nlri
302         final List<Nlri> nlris = Lists.newArrayList(new NlriBuilder().setPrefix(new Ipv4Prefix("172.16.0.0/21"))
303             .build());
304         builder.setNlri(nlris);
305         assertEquals(builder.getNlri(), message.getNlri());
306
307         // check fields
308         assertNull(message.getWithdrawnRoutes());
309
310         // attributes
311         final List<AsNumber> asNumbers = new ArrayList<>();
312         asNumbers.add(new AsNumber(30L));
313         final List<Segments> asPath = Lists.newArrayList();
314         asPath.add(new SegmentsBuilder().setAsSequence(asNumbers).build());
315         final List<AsNumber> asSet = Lists.newArrayList(new AsNumber(10L), new AsNumber(20L));
316         asPath.add(new SegmentsBuilder().setAsSet(asSet).build());
317
318         final Aggregator aggregator = new AggregatorBuilder().setAsNumber(new AsNumber((long) 30)).setNetworkAddress(
319                 new Ipv4Address("10.0.0.9")).build();
320         final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
321                 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.9")).build()).build();
322
323         // check path attributes
324         final Attributes attrs = message.getAttributes();
325
326         final AttributesBuilder paBuilder = new AttributesBuilder();
327
328         paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Incomplete).build());
329         assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
330
331         paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
332         assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
333
334         paBuilder.setCNextHop(nextHop);
335         assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
336
337         paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
338         assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
339
340         paBuilder.setAggregator(aggregator);
341         assertEquals(paBuilder.getAggregator(), attrs.getAggregator());
342         paBuilder.setUnrecognizedAttributes(Collections.emptyList());
343         builder.setAttributes(paBuilder.build());
344
345         assertEquals(builder.build(), message);
346
347         final ByteBuf buffer = Unpooled.buffer();
348         BGPParserTest.updateParser.serializeMessage(message, buffer);
349         assertArrayEquals(INPUT_BYTES.get(2), ByteArray.readAllBytes(buffer));
350     }
351
352     /*
353      * Tests empty AS_PATH, ORIGIN.EGP, LOCAL_PREF, EXTENDED_COMMUNITIES (Ipv4 Addr specific)
354      *
355      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
356      * 00 4A <- length (73) - including header
357      * 02 <- message type
358      * 00 00 <- withdrawn routes length
359      * 00 27 <- total path attribute length (39)
360      * 40 <- attribute flags
361      * 01 <- attribute type code (Origin)
362      * 01 <- attribute length
363      * 01 <- Origin value (EGP)
364      * 40 <- attribute flags
365      * 02 <- attribute type code (As path)
366      * 00 <- attribute length
367      * 40 <- attribute flags
368      * 03 <- attribute type (Next hop)
369      * 04 <- attribute length
370      * 03 03 03 03 <- value (3.3.3.3)
371      * 80 <- attribute flags
372      * 04 <- attribute type code (Multi exit disc)
373      * 04 <- attribute length
374      * 00 00 00 00 <- value
375      * 40 <- attribute flags
376      * 05 <- attribute type (Local Pref)
377      * 04 <- attribute length
378      * 00 00 00 64 <- value (100)
379      * c0 <- attribute flags
380      * 10 <- attribute type (extended community)
381      * 08 <- attribute length
382      * 01 04 <- value (type - Ipv4 Address Specific Extended Community)
383      * c0 a8 01 00 <- value (global adm. 198.162.1.0)
384      * 12 34 <- value (local adm. 4660)
385      *
386      * //NLRI
387      * 18 0a 1e 03 <- IPv4 Prefix (10.30.3.0 / 24)
388      * 18 0a 1e 02 <- IPv4 Prefix (10.30.2.0 / 24)
389      * 18 0a 1e 01 <- IPv4 Prefix (10.30.1.0 / 24)
390      */
391     @Test
392     public void testGetUpdateMessage4() throws Exception {
393         final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(3), MessageUtil.COMMON_HEADER_LENGTH);
394         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(3), MessageUtil.MARKER_LENGTH,
395             LENGTH_FIELD_LENGTH));
396         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
397             null);
398
399         final UpdateBuilder builder = new UpdateBuilder();
400
401         // check fields
402         assertNull(message.getWithdrawnRoutes());
403
404         // check nlri
405         final List<Nlri> nlris = Lists.newArrayList();
406         nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("10.30.3.0/24")).build());
407         nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("10.30.2.0/24")).build());
408         nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("10.30.1.0/24")).build());
409
410         assertEquals(nlris, message.getNlri());
411
412         builder.setNlri(nlris);
413
414         // attributes
415         final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
416                 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("3.3.3.3")).build()).build();
417
418         final List<ExtendedCommunities> comms = Lists.newArrayList();
419         comms.add(new ExtendedCommunitiesBuilder().setTransitive(true).setExtendedCommunity(
420                 new RouteTargetIpv4CaseBuilder().setRouteTargetIpv4(
421                         new RouteTargetIpv4Builder().setGlobalAdministrator(
422                                 new Ipv4Address("192.168.1.0")).setLocalAdministrator(4660)
423                                 .build()).build()).build());
424
425         // check path attributes
426         final Attributes attrs = message.getAttributes();
427
428         final AttributesBuilder paBuilder = new AttributesBuilder();
429
430         paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Egp).build());
431         assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
432
433         paBuilder.setAsPath(new AsPathBuilder().setSegments(Collections.emptyList()).build());
434         assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
435
436         paBuilder.setCNextHop(nextHop);
437         assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
438
439         paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
440         assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
441
442         paBuilder.setLocalPref(new LocalPrefBuilder().setPref(100L).build());
443         assertEquals(paBuilder.getLocalPref(), attrs.getLocalPref());
444
445         paBuilder.setExtendedCommunities(comms);
446         assertEquals(paBuilder.getExtendedCommunities(), attrs.getExtendedCommunities());
447
448         paBuilder.setUnrecognizedAttributes(Collections.emptyList());
449         // check API message
450         builder.setAttributes(paBuilder.build());
451         assertEquals(builder.build(), message);
452
453         final ByteBuf buffer = Unpooled.buffer();
454         BGPParserTest.updateParser.serializeMessage(message, buffer);
455         assertArrayEquals(INPUT_BYTES.get(3), ByteArray.readAllBytes(buffer));
456     }
457
458     /*
459      * Tests withdrawn routes.
460      *
461      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
462      * 00 1c <- length (28) - including header
463      * 02 <- message type
464      * 00 05 <- withdrawn routes length (5)
465      * 1e ac 10 00 04 <- route (172.16.0.4)
466      * 00 00 <- total path attribute length
467      */
468     @Test
469     public void testGetUpdateMessage5() throws Exception {
470         final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(4), MessageUtil.COMMON_HEADER_LENGTH);
471         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(4), MessageUtil.MARKER_LENGTH,
472             LENGTH_FIELD_LENGTH));
473         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
474             null);
475
476         // attributes
477         final List<WithdrawnRoutes> withdrawnRoutes = Lists.newArrayList(new WithdrawnRoutesBuilder()
478             .setPrefix(new Ipv4Prefix("172.16.0.4/30")).build());
479
480         // check API message
481         final Update expectedMessage = new UpdateBuilder().setWithdrawnRoutes(withdrawnRoutes).build();
482
483         assertEquals(expectedMessage.getWithdrawnRoutes(), message.getWithdrawnRoutes());
484
485         final ByteBuf buffer = Unpooled.buffer();
486         BGPParserTest.updateParser.serializeMessage(message, buffer);
487         assertArrayEquals(INPUT_BYTES.get(4), ByteArray.readAllBytes(buffer));
488     }
489
490     /*
491      * End of Rib for Ipv4.
492      *
493      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
494      * 00 17 <- length (23) - including header
495      * 02 <- message type
496      * 00 00 <- withdrawn routes length
497      * 00 00 <- total path attribute length
498      */
499     @Test
500     public void testEORIpv4() throws Exception {
501         final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(5), MessageUtil.COMMON_HEADER_LENGTH);
502         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(5), MessageUtil.MARKER_LENGTH,
503             LENGTH_FIELD_LENGTH));
504         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
505             null);
506
507         assertEquals(new UpdateBuilder().build(), message);
508
509         final ByteBuf buffer = Unpooled.buffer();
510         BGPParserTest.updateParser.serializeMessage(message, buffer);
511         assertArrayEquals(INPUT_BYTES.get(5), ByteArray.readAllBytes(buffer));
512     }
513
514     /*
515      * End of Rib for Ipv6 consists of empty MP_UNREACH_NLRI, with AFI 2 and SAFI 1
516      *
517      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
518      * 00 1d <- length (29) - including header
519      * 02 <- message type
520      * 00 00 <- withdrawn routes length
521      * 00 06 <- total path attribute length
522      * 80 <- attribute flags
523      * 0f <- attribute type (15 - MP_UNREACH_NLRI)
524      * 03 <- attribute length
525      * 00 02 <- value (AFI 2: IPv6)
526      * 01 <- value (SAFI 1)
527      */
528     @Test
529     public void testEORIpv6() throws Exception {
530         final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(6), MessageUtil.COMMON_HEADER_LENGTH);
531         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(6), MessageUtil.MARKER_LENGTH,
532             LENGTH_FIELD_LENGTH));
533         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
534             null);
535
536         final Class<? extends AddressFamily> afi = message.getAttributes().augmentation(Attributes2.class)
537                 .getMpUnreachNlri().getAfi();
538         final Class<? extends SubsequentAddressFamily> safi = message.getAttributes().augmentation(Attributes2.class)
539                 .getMpUnreachNlri().getSafi();
540
541         assertEquals(Ipv6AddressFamily.class, afi);
542         assertEquals(UnicastSubsequentAddressFamily.class, safi);
543
544         final ByteBuf buffer = Unpooled.buffer();
545         BGPParserTest.updateParser.serializeMessage(message, buffer);
546         assertArrayEquals(INPUT_BYTES.get(6), ByteArray.readAllBytes(buffer));
547     }
548
549     /*
550      * End of Rib for Ipv6 consists of empty MP_UNREACH_NLRI, with AFI 2 and SAFI 1
551      *
552      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
553      * 00 1e <- length (29) - including header
554      * 02 <- message type
555      * 00 00 <- withdrawn routes length
556      * 00 07 <- total path attribute length
557      * 90 <- attribute flags
558      * 0f <- attribute type (15 - MP_UNREACH_NLRI)
559      * 00 03 <- attribute length
560      * 00 02 <- value (AFI 2: IPv6)
561      * 01 <- value (SAFI 1)
562      */
563     @Test
564     public void testEORIpv6exLength() throws Exception {
565         final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(6), MessageUtil.COMMON_HEADER_LENGTH);
566         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(6), MessageUtil.MARKER_LENGTH,
567             LENGTH_FIELD_LENGTH));
568         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
569             null);
570
571         final Class<? extends AddressFamily> afi = message.getAttributes().augmentation(Attributes2.class)
572                 .getMpUnreachNlri().getAfi();
573         final Class<? extends SubsequentAddressFamily> safi = message.getAttributes().augmentation(Attributes2.class)
574                 .getMpUnreachNlri().getSafi();
575
576         assertEquals(Ipv6AddressFamily.class, afi);
577         assertEquals(UnicastSubsequentAddressFamily.class, safi);
578
579         final ByteBuf buffer = Unpooled.buffer();
580         BGPParserTest.updateParser.serializeMessage(message, buffer);
581         assertArrayEquals(INPUT_BYTES.get(6), ByteArray.readAllBytes(buffer));
582     }
583
584     /*
585      * Tests IPv4 NEXT_HOP, ATOMIC_AGGREGATE, COMMUNITY, NLRI with multiple paths.
586      *
587      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
588      * 00 60 <- length (96) - including header
589      * 02 <- message type
590      * 00 00 <- withdrawn routes length
591      * 00 31 <- total path attribute length (49)
592      * 40 <- attribute flags
593      * 01 <- attribute type code (origin)
594      * 01 <- attribute length
595      * 00 <- Origin value (IGP)
596      * 40 <- attribute flags
597      * 02 <- attribute type code (as path)
598      * 06 <- attribute length
599      * 02 <- AS_SEQUENCE
600      * 01 <- path segment count
601      * 00 00 fd ea <- path segment value (65002)
602      * 40 <- attribute flags
603      * 03 <- attribute type code (Next Hop)
604      * 04 <- attribute length
605      * 0a 00 00 02 <- value (10.0.0.2)
606      * 80 <- attribute flags
607      * 04 <- attribute type code (multi exit disc)
608      * 04 <- attribute length
609      * 00 00 00 00 <- value
610      * 40 <- attribute flags
611      * 06 <- attribute type code (atomic aggregate)
612      * 00 <- attribute length
613      * C0 <- attribute flags
614      * 08 <- attribute type code (community)
615      * 10 <- attribute length
616      * FF FF FF 01 <- value (NO_EXPORT)
617      * FF FF FF 02 <- value (NO_ADVERTISE)
618      * FF FF FF 03 <- value (NO_EXPORT_SUBCONFED)
619      * FF FF FF 10 <- unknown Community
620      *
621      * //NLRI
622      * 00 00 00 01 <- path-id (1)
623      * 18 ac 11 02 <- IPv4 Prefix (172.17.1.0 / 24)
624      * 00 00 00 01 <- path-id (2)
625      * 18 ac 11 01 <- IPv4 Prefix (172.17.1.0 / 24)
626      * 00 00 00 01 <- path-id (1)
627      * 18 ac 11 00 <- IPv4 Prefix (172.17.0.0 / 24)
628      */
629     @Test
630     public void testUpdateMessageNlriAddPath() throws Exception {
631         final byte[] body = ByteArray.cutBytes(updatesWithMultiplePath.get(0), MessageUtil.COMMON_HEADER_LENGTH);
632         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(updatesWithMultiplePath.get(0),
633             MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
634         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
635             mpConstraint);
636
637         // check fields
638
639         assertNull(message.getWithdrawnRoutes());
640
641         // attributes
642         final List<AsNumber> asNumbers = new ArrayList<>();
643         asNumbers.add(new AsNumber(65002L));
644         final List<Segments> asPath = Lists.newArrayList();
645         asPath.add(new SegmentsBuilder().setAsSequence(asNumbers).build());
646
647         final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
648                 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.2")).build()).build();
649
650         final List<Communities> comms = Lists.newArrayList();
651         comms.add((Communities) CommunityUtil.NO_EXPORT);
652         comms.add((Communities) CommunityUtil.NO_ADVERTISE);
653         comms.add((Communities) CommunityUtil.NO_EXPORT_SUBCONFED);
654         comms.add((Communities) CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF10));
655
656         final UpdateBuilder builder = new UpdateBuilder();
657
658         // check nlri
659
660         final List<Nlri> nlris = Lists.newArrayList();
661         nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.1.0/24")).setPathId(new PathId(1L)).build());
662         nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.1.0/24")).setPathId(new PathId(2L)).build());
663         nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.0.0/24")).setPathId(new PathId(1L)).build());
664
665         assertEquals(nlris, message.getNlri());
666
667         builder.setNlri(nlris);
668
669         // check path attributes
670
671         final Attributes attrs = message.getAttributes();
672
673         final AttributesBuilder paBuilder = new AttributesBuilder();
674
675         paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
676         assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
677
678         paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
679         assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
680
681         paBuilder.setCNextHop(nextHop);
682         assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
683
684         paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
685         assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
686
687         paBuilder.setAtomicAggregate(new AtomicAggregateBuilder().build());
688         assertEquals(paBuilder.getAtomicAggregate(), attrs.getAtomicAggregate());
689
690         paBuilder.setCommunities(comms);
691         assertEquals(paBuilder.getCommunities(), attrs.getCommunities());
692
693         paBuilder.setUnrecognizedAttributes(Collections.emptyList());
694
695         builder.setAttributes(paBuilder.build());
696
697         assertEquals(builder.build(), message);
698
699         final ByteBuf buffer = Unpooled.buffer();
700         BGPParserTest.updateParser.serializeMessage(message, buffer);
701         assertArrayEquals(updatesWithMultiplePath.get(0), ByteArray.readAllBytes(buffer));
702     }
703
704     /*
705      * Tests withdrawn routes with multiple paths.
706      *
707      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
708      * 00 29 <- length (41) - including header
709      * 02 <- message type
710      * 00 12 <- withdrawn routes length (18)
711      * 00 00 00 01 <- path-id (1)
712      * 1e ac 10 00 04 <- route (172.16.0.4)
713      * 00 00 00 02 <- path-id (2)
714      * 1e ac 10 00 04 <- route (172.16.0.4)
715      * 00 00 <- total path attribute length
716      *
717      */
718     @Test
719     public void testUpdateMessageWithdrawAddPath() throws Exception {
720         final byte[] body = ByteArray.cutBytes(updatesWithMultiplePath.get(1), MessageUtil.COMMON_HEADER_LENGTH);
721         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(updatesWithMultiplePath.get(1),
722             MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
723         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
724             mpConstraint);
725
726         // attributes
727         final List<WithdrawnRoutes> withdrawnRoutes = Lists.newArrayList();
728         withdrawnRoutes.add(new WithdrawnRoutesBuilder().setPrefix(new Ipv4Prefix("172.16.0.4/30"))
729             .setPathId(new PathId(1L)).build());
730         withdrawnRoutes.add(new WithdrawnRoutesBuilder().setPrefix(new Ipv4Prefix("172.16.0.4/30"))
731             .setPathId(new PathId(2L)).build());
732
733         // check API message
734         final Update expectedMessage = new UpdateBuilder().setWithdrawnRoutes(withdrawnRoutes).build();
735
736         assertEquals(expectedMessage.getWithdrawnRoutes(), message.getWithdrawnRoutes());
737
738         final ByteBuf buffer = Unpooled.buffer();
739         BGPParserTest.updateParser.serializeMessage(message, buffer);
740         assertArrayEquals(updatesWithMultiplePath.get(1), ByteArray.readAllBytes(buffer));
741     }
742
743     /*
744      * Tests withdrawn routes with malformed attribute.
745      *
746      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
747      * 00 35 <- length (53) - including header
748      * 02 <- message type
749      * 00 00 <- withdrawn routes length
750      * 00 1a <- total path attribute length (26)
751      * 40 <- attribute flags
752      * 01 <- attribute type code (origin)
753      * 02 <- WRONG attribute length
754      * 00 <- Origin value (IGP)
755      * 40 <- attribute flags
756      * 03 <- attribute type code (Next Hop)
757      * 04 <- attribute length
758      * 0a 00 00 02 <- value (10.0.0.2)
759      * 40 <- attribute flags
760      * 0e <- attribute type code (MP_REACH)
761      * 00 01 <- AFI (Ipv4)
762      * 01 <- SAFI (Unicast)
763      * 04 <- next hop length
764      * ff ff ff ff <- next hop
765      * 00 <- reserved
766      * 18 <- length
767      * 0a 00 01 <- prefix (10.0.1.0)
768      * //NLRI
769      * 18 <- length
770      * 0a 00 02 <- prefix (10.0.2.0)
771      */
772     @Test
773     public void testUpdateMessageWithMalformedAttribute() throws BGPDocumentedException {
774         final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(7), MessageUtil.COMMON_HEADER_LENGTH);
775         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(6), MessageUtil.MARKER_LENGTH,
776                 LENGTH_FIELD_LENGTH));
777         final PeerSpecificParserConstraintImpl constraint = new PeerSpecificParserConstraintImpl();
778         constraint.addPeerConstraint(RevisedErrorHandlingSupport.class,
779                 RevisedErrorHandlingSupportImpl.forExternalPeer());
780         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
781                 constraint);
782         assertNotNull(message);
783         assertNull(message.getNlri());
784         final List<WithdrawnRoutes> withdrawnRoutes = message.getWithdrawnRoutes();
785         assertNotNull(withdrawnRoutes);
786         assertEquals(1, withdrawnRoutes.size());
787     }
788 }