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