Use new Uint* constants
[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.TEN), new AsNumber(Uint32.valueOf(20)));
318         asPath.add(new SegmentsBuilder().setAsSet(asSet).build());
319
320         final Aggregator aggregator = new AggregatorBuilder().setAsNumber(new AsNumber(Uint32.valueOf(30)))
321                 .setNetworkAddress(new Ipv4Address("10.0.0.9")).build();
322         final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
323                 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.9")).build()).build();
324
325         // check path attributes
326         final Attributes attrs = message.getAttributes();
327
328         final AttributesBuilder paBuilder = new AttributesBuilder();
329
330         paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Incomplete).build());
331         assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
332
333         paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
334         assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
335
336         paBuilder.setCNextHop(nextHop);
337         assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
338
339         paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed(Uint32.ZERO).build());
340         assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
341
342         paBuilder.setAggregator(aggregator);
343         assertEquals(paBuilder.getAggregator(), attrs.getAggregator());
344         paBuilder.setUnrecognizedAttributes(Collections.emptyList());
345         builder.setAttributes(paBuilder.build());
346
347         assertEquals(builder.build(), message);
348
349         final ByteBuf buffer = Unpooled.buffer();
350         BGPParserTest.updateParser.serializeMessage(message, buffer);
351         assertArrayEquals(INPUT_BYTES.get(2), ByteArray.readAllBytes(buffer));
352     }
353
354     /*
355      * Tests empty AS_PATH, ORIGIN.EGP, LOCAL_PREF, EXTENDED_COMMUNITIES (Ipv4 Addr specific)
356      *
357      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
358      * 00 4A <- length (73) - including header
359      * 02 <- message type
360      * 00 00 <- withdrawn routes length
361      * 00 27 <- total path attribute length (39)
362      * 40 <- attribute flags
363      * 01 <- attribute type code (Origin)
364      * 01 <- attribute length
365      * 01 <- Origin value (EGP)
366      * 40 <- attribute flags
367      * 02 <- attribute type code (As path)
368      * 00 <- attribute length
369      * 40 <- attribute flags
370      * 03 <- attribute type (Next hop)
371      * 04 <- attribute length
372      * 03 03 03 03 <- value (3.3.3.3)
373      * 80 <- attribute flags
374      * 04 <- attribute type code (Multi exit disc)
375      * 04 <- attribute length
376      * 00 00 00 00 <- value
377      * 40 <- attribute flags
378      * 05 <- attribute type (Local Pref)
379      * 04 <- attribute length
380      * 00 00 00 64 <- value (100)
381      * c0 <- attribute flags
382      * 10 <- attribute type (extended community)
383      * 08 <- attribute length
384      * 01 04 <- value (type - Ipv4 Address Specific Extended Community)
385      * c0 a8 01 00 <- value (global adm. 198.162.1.0)
386      * 12 34 <- value (local adm. 4660)
387      *
388      * //NLRI
389      * 18 0a 1e 03 <- IPv4 Prefix (10.30.3.0 / 24)
390      * 18 0a 1e 02 <- IPv4 Prefix (10.30.2.0 / 24)
391      * 18 0a 1e 01 <- IPv4 Prefix (10.30.1.0 / 24)
392      */
393     @Test
394     public void testGetUpdateMessage4() throws Exception {
395         final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(3), MessageUtil.COMMON_HEADER_LENGTH);
396         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(3), MessageUtil.MARKER_LENGTH,
397             LENGTH_FIELD_LENGTH));
398         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
399             null);
400
401         final UpdateBuilder builder = new UpdateBuilder();
402
403         // check fields
404         assertNull(message.getWithdrawnRoutes());
405
406         // check nlri
407         final List<Nlri> nlris = new ArrayList<>();
408         nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("10.30.3.0/24")).build());
409         nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("10.30.2.0/24")).build());
410         nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("10.30.1.0/24")).build());
411
412         assertEquals(nlris, message.getNlri());
413
414         builder.setNlri(nlris);
415
416         // attributes
417         final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
418                 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("3.3.3.3")).build()).build();
419
420         final List<ExtendedCommunities> comms = new ArrayList<>();
421         comms.add(new ExtendedCommunitiesBuilder()
422             .setTransitive(true)
423             .setExtendedCommunity(new RouteTargetIpv4CaseBuilder()
424                 .setRouteTargetIpv4(new RouteTargetIpv4Builder()
425                     .setGlobalAdministrator(new Ipv4Address("192.168.1.0"))
426                     .setLocalAdministrator(Uint16.valueOf(4660))
427                     .build())
428                 .build())
429             .build());
430
431         // check path attributes
432         final Attributes attrs = message.getAttributes();
433
434         final AttributesBuilder paBuilder = new AttributesBuilder();
435
436         paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Egp).build());
437         assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
438
439         paBuilder.setAsPath(new AsPathBuilder().setSegments(Collections.emptyList()).build());
440         assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
441
442         paBuilder.setCNextHop(nextHop);
443         assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
444
445         paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed(Uint32.ZERO).build());
446         assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
447
448         paBuilder.setLocalPref(new LocalPrefBuilder().setPref(Uint32.valueOf(100)).build());
449         assertEquals(paBuilder.getLocalPref(), attrs.getLocalPref());
450
451         paBuilder.setExtendedCommunities(comms);
452         assertEquals(paBuilder.getExtendedCommunities(), attrs.getExtendedCommunities());
453
454         paBuilder.setUnrecognizedAttributes(Collections.emptyList());
455         // check API message
456         builder.setAttributes(paBuilder.build());
457         assertEquals(builder.build(), message);
458
459         final ByteBuf buffer = Unpooled.buffer();
460         BGPParserTest.updateParser.serializeMessage(message, buffer);
461         assertArrayEquals(INPUT_BYTES.get(3), ByteArray.readAllBytes(buffer));
462     }
463
464     /*
465      * Tests withdrawn routes.
466      *
467      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
468      * 00 1c <- length (28) - including header
469      * 02 <- message type
470      * 00 05 <- withdrawn routes length (5)
471      * 1e ac 10 00 04 <- route (172.16.0.4)
472      * 00 00 <- total path attribute length
473      */
474     @Test
475     public void testGetUpdateMessage5() throws Exception {
476         final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(4), MessageUtil.COMMON_HEADER_LENGTH);
477         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(4), MessageUtil.MARKER_LENGTH,
478             LENGTH_FIELD_LENGTH));
479         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
480             null);
481
482         // attributes
483         final List<WithdrawnRoutes> withdrawnRoutes = Lists.newArrayList(new WithdrawnRoutesBuilder()
484             .setPrefix(new Ipv4Prefix("172.16.0.4/30")).build());
485
486         // check API message
487         final Update expectedMessage = new UpdateBuilder().setWithdrawnRoutes(withdrawnRoutes).build();
488
489         assertEquals(expectedMessage.getWithdrawnRoutes(), message.getWithdrawnRoutes());
490
491         final ByteBuf buffer = Unpooled.buffer();
492         BGPParserTest.updateParser.serializeMessage(message, buffer);
493         assertArrayEquals(INPUT_BYTES.get(4), ByteArray.readAllBytes(buffer));
494     }
495
496     /*
497      * End of Rib for Ipv4.
498      *
499      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
500      * 00 17 <- length (23) - including header
501      * 02 <- message type
502      * 00 00 <- withdrawn routes length
503      * 00 00 <- total path attribute length
504      */
505     @Test
506     public void testEORIpv4() throws Exception {
507         final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(5), MessageUtil.COMMON_HEADER_LENGTH);
508         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(5), MessageUtil.MARKER_LENGTH,
509             LENGTH_FIELD_LENGTH));
510         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
511             null);
512
513         assertEquals(new UpdateBuilder().build(), message);
514
515         final ByteBuf buffer = Unpooled.buffer();
516         BGPParserTest.updateParser.serializeMessage(message, buffer);
517         assertArrayEquals(INPUT_BYTES.get(5), ByteArray.readAllBytes(buffer));
518     }
519
520     /*
521      * End of Rib for Ipv6 consists of empty MP_UNREACH_NLRI, with AFI 2 and SAFI 1
522      *
523      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
524      * 00 1d <- length (29) - including header
525      * 02 <- message type
526      * 00 00 <- withdrawn routes length
527      * 00 06 <- total path attribute length
528      * 80 <- attribute flags
529      * 0f <- attribute type (15 - MP_UNREACH_NLRI)
530      * 03 <- attribute length
531      * 00 02 <- value (AFI 2: IPv6)
532      * 01 <- value (SAFI 1)
533      */
534     @Test
535     public void testEORIpv6() throws Exception {
536         final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(6), MessageUtil.COMMON_HEADER_LENGTH);
537         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(6), MessageUtil.MARKER_LENGTH,
538             LENGTH_FIELD_LENGTH));
539         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
540             null);
541
542         final Class<? extends AddressFamily> afi = message.getAttributes().augmentation(Attributes2.class)
543                 .getMpUnreachNlri().getAfi();
544         final Class<? extends SubsequentAddressFamily> safi = message.getAttributes().augmentation(Attributes2.class)
545                 .getMpUnreachNlri().getSafi();
546
547         assertEquals(Ipv6AddressFamily.class, afi);
548         assertEquals(UnicastSubsequentAddressFamily.class, safi);
549
550         final ByteBuf buffer = Unpooled.buffer();
551         BGPParserTest.updateParser.serializeMessage(message, buffer);
552         assertArrayEquals(INPUT_BYTES.get(6), ByteArray.readAllBytes(buffer));
553     }
554
555     /*
556      * End of Rib for Ipv6 consists of empty MP_UNREACH_NLRI, with AFI 2 and SAFI 1
557      *
558      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
559      * 00 1e <- length (29) - including header
560      * 02 <- message type
561      * 00 00 <- withdrawn routes length
562      * 00 07 <- total path attribute length
563      * 90 <- attribute flags
564      * 0f <- attribute type (15 - MP_UNREACH_NLRI)
565      * 00 03 <- attribute length
566      * 00 02 <- value (AFI 2: IPv6)
567      * 01 <- value (SAFI 1)
568      */
569     @Test
570     public void testEORIpv6exLength() throws Exception {
571         final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(6), MessageUtil.COMMON_HEADER_LENGTH);
572         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(6), MessageUtil.MARKER_LENGTH,
573             LENGTH_FIELD_LENGTH));
574         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
575             null);
576
577         final Class<? extends AddressFamily> afi = message.getAttributes().augmentation(Attributes2.class)
578                 .getMpUnreachNlri().getAfi();
579         final Class<? extends SubsequentAddressFamily> safi = message.getAttributes().augmentation(Attributes2.class)
580                 .getMpUnreachNlri().getSafi();
581
582         assertEquals(Ipv6AddressFamily.class, afi);
583         assertEquals(UnicastSubsequentAddressFamily.class, safi);
584
585         final ByteBuf buffer = Unpooled.buffer();
586         BGPParserTest.updateParser.serializeMessage(message, buffer);
587         assertArrayEquals(INPUT_BYTES.get(6), ByteArray.readAllBytes(buffer));
588     }
589
590     /*
591      * Tests IPv4 NEXT_HOP, ATOMIC_AGGREGATE, COMMUNITY, NLRI with multiple paths.
592      *
593      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
594      * 00 60 <- length (96) - including header
595      * 02 <- message type
596      * 00 00 <- withdrawn routes length
597      * 00 31 <- total path attribute length (49)
598      * 40 <- attribute flags
599      * 01 <- attribute type code (origin)
600      * 01 <- attribute length
601      * 00 <- Origin value (IGP)
602      * 40 <- attribute flags
603      * 02 <- attribute type code (as path)
604      * 06 <- attribute length
605      * 02 <- AS_SEQUENCE
606      * 01 <- path segment count
607      * 00 00 fd ea <- path segment value (65002)
608      * 40 <- attribute flags
609      * 03 <- attribute type code (Next Hop)
610      * 04 <- attribute length
611      * 0a 00 00 02 <- value (10.0.0.2)
612      * 80 <- attribute flags
613      * 04 <- attribute type code (multi exit disc)
614      * 04 <- attribute length
615      * 00 00 00 00 <- value
616      * 40 <- attribute flags
617      * 06 <- attribute type code (atomic aggregate)
618      * 00 <- attribute length
619      * C0 <- attribute flags
620      * 08 <- attribute type code (community)
621      * 10 <- attribute length
622      * FF FF FF 01 <- value (NO_EXPORT)
623      * FF FF FF 02 <- value (NO_ADVERTISE)
624      * FF FF FF 03 <- value (NO_EXPORT_SUBCONFED)
625      * FF FF FF 10 <- unknown Community
626      *
627      * //NLRI
628      * 00 00 00 01 <- path-id (1)
629      * 18 ac 11 02 <- IPv4 Prefix (172.17.1.0 / 24)
630      * 00 00 00 01 <- path-id (2)
631      * 18 ac 11 01 <- IPv4 Prefix (172.17.1.0 / 24)
632      * 00 00 00 01 <- path-id (1)
633      * 18 ac 11 00 <- IPv4 Prefix (172.17.0.0 / 24)
634      */
635     @Test
636     public void testUpdateMessageNlriAddPath() throws Exception {
637         final byte[] body = ByteArray.cutBytes(updatesWithMultiplePath.get(0), MessageUtil.COMMON_HEADER_LENGTH);
638         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(updatesWithMultiplePath.get(0),
639             MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
640         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
641             mpConstraint);
642
643         // check fields
644
645         assertNull(message.getWithdrawnRoutes());
646
647         // attributes
648         final List<AsNumber> asNumbers = new ArrayList<>();
649         asNumbers.add(new AsNumber(Uint32.valueOf(65002)));
650         final List<Segments> asPath = new ArrayList<>();
651         asPath.add(new SegmentsBuilder().setAsSequence(asNumbers).build());
652
653         final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
654                 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.2")).build()).build();
655
656         final List<Communities> comms = new ArrayList<>();
657         comms.add((Communities) CommunityUtil.NO_EXPORT);
658         comms.add((Communities) CommunityUtil.NO_ADVERTISE);
659         comms.add((Communities) CommunityUtil.NO_EXPORT_SUBCONFED);
660         comms.add((Communities) CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF10));
661
662         final UpdateBuilder builder = new UpdateBuilder();
663
664         // check nlri
665
666         final List<Nlri> nlris = new ArrayList<>();
667         nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.1.0/24")).setPathId(new PathId(Uint32.ONE))
668             .build());
669         nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.1.0/24")).setPathId(new PathId(Uint32.TWO))
670             .build());
671         nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.0.0/24")).setPathId(new PathId(Uint32.ONE))
672             .build());
673
674         assertEquals(nlris, message.getNlri());
675
676         builder.setNlri(nlris);
677
678         // check path attributes
679
680         final Attributes attrs = message.getAttributes();
681
682         final AttributesBuilder paBuilder = new AttributesBuilder();
683
684         paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
685         assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
686
687         paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
688         assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
689
690         paBuilder.setCNextHop(nextHop);
691         assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
692
693         paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed(Uint32.ZERO).build());
694         assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
695
696         paBuilder.setAtomicAggregate(new AtomicAggregateBuilder().build());
697         assertEquals(paBuilder.getAtomicAggregate(), attrs.getAtomicAggregate());
698
699         paBuilder.setCommunities(comms);
700         assertEquals(paBuilder.getCommunities(), attrs.getCommunities());
701
702         paBuilder.setUnrecognizedAttributes(Collections.emptyList());
703
704         builder.setAttributes(paBuilder.build());
705
706         assertEquals(builder.build(), message);
707
708         final ByteBuf buffer = Unpooled.buffer();
709         BGPParserTest.updateParser.serializeMessage(message, buffer);
710         assertArrayEquals(updatesWithMultiplePath.get(0), ByteArray.readAllBytes(buffer));
711     }
712
713     /*
714      * Tests withdrawn routes with multiple paths.
715      *
716      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
717      * 00 29 <- length (41) - including header
718      * 02 <- message type
719      * 00 12 <- withdrawn routes length (18)
720      * 00 00 00 01 <- path-id (1)
721      * 1e ac 10 00 04 <- route (172.16.0.4)
722      * 00 00 00 02 <- path-id (2)
723      * 1e ac 10 00 04 <- route (172.16.0.4)
724      * 00 00 <- total path attribute length
725      *
726      */
727     @Test
728     public void testUpdateMessageWithdrawAddPath() throws Exception {
729         final byte[] body = ByteArray.cutBytes(updatesWithMultiplePath.get(1), MessageUtil.COMMON_HEADER_LENGTH);
730         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(updatesWithMultiplePath.get(1),
731             MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
732         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
733             mpConstraint);
734
735         // attributes
736         final List<WithdrawnRoutes> withdrawnRoutes = new ArrayList<>();
737         withdrawnRoutes.add(new WithdrawnRoutesBuilder().setPrefix(new Ipv4Prefix("172.16.0.4/30"))
738             .setPathId(new PathId(Uint32.ONE)).build());
739         withdrawnRoutes.add(new WithdrawnRoutesBuilder().setPrefix(new Ipv4Prefix("172.16.0.4/30"))
740             .setPathId(new PathId(Uint32.TWO)).build());
741
742         // check API message
743         final Update expectedMessage = new UpdateBuilder().setWithdrawnRoutes(withdrawnRoutes).build();
744
745         assertEquals(expectedMessage.getWithdrawnRoutes(), message.getWithdrawnRoutes());
746
747         final ByteBuf buffer = Unpooled.buffer();
748         BGPParserTest.updateParser.serializeMessage(message, buffer);
749         assertArrayEquals(updatesWithMultiplePath.get(1), ByteArray.readAllBytes(buffer));
750     }
751
752     /*
753      * Tests withdrawn routes with malformed attribute.
754      *
755      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
756      * 00 35 <- length (53) - including header
757      * 02 <- message type
758      * 00 00 <- withdrawn routes length
759      * 00 1a <- total path attribute length (26)
760      * 40 <- attribute flags
761      * 01 <- attribute type code (origin)
762      * 02 <- WRONG attribute length
763      * 00 <- Origin value (IGP)
764      * 40 <- attribute flags
765      * 03 <- attribute type code (Next Hop)
766      * 04 <- attribute length
767      * 0a 00 00 02 <- value (10.0.0.2)
768      * 40 <- attribute flags
769      * 0e <- attribute type code (MP_REACH)
770      * 00 01 <- AFI (Ipv4)
771      * 01 <- SAFI (Unicast)
772      * 04 <- next hop length
773      * ff ff ff ff <- next hop
774      * 00 <- reserved
775      * 18 <- length
776      * 0a 00 01 <- prefix (10.0.1.0)
777      * //NLRI
778      * 18 <- length
779      * 0a 00 02 <- prefix (10.0.2.0)
780      */
781     @Test
782     public void testUpdateMessageWithMalformedAttribute() throws BGPDocumentedException {
783         final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(7), MessageUtil.COMMON_HEADER_LENGTH);
784         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(6), MessageUtil.MARKER_LENGTH,
785                 LENGTH_FIELD_LENGTH));
786         final PeerSpecificParserConstraintImpl constraint = new PeerSpecificParserConstraintImpl();
787         constraint.addPeerConstraint(RevisedErrorHandlingSupport.class,
788                 RevisedErrorHandlingSupportImpl.forExternalPeer());
789         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
790                 constraint);
791         assertNotNull(message);
792         assertNull(message.getNlri());
793         final List<WithdrawnRoutes> withdrawnRoutes = message.getWithdrawnRoutes();
794         assertNotNull(withdrawnRoutes);
795         assertEquals(1, withdrawnRoutes.size());
796     }
797 }