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