5c5ee8b93e947d0ec03a1eaf1a63bcef15e4a227
[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
15 import com.google.common.collect.Lists;
16 import io.netty.buffer.ByteBuf;
17 import io.netty.buffer.Unpooled;
18 import java.io.ByteArrayOutputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26 import org.opendaylight.protocol.bgp.parser.impl.message.BGPUpdateMessageParser;
27 import org.opendaylight.protocol.bgp.parser.impl.message.update.CommunityUtil;
28 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
29 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
30 import org.opendaylight.protocol.util.ByteArray;
31 import org.opendaylight.protocol.util.NoopReferenceCache;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.UpdateBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.Aggregator;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.AggregatorBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.AsPathBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.AtomicAggregateBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.Communities;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.ExtendedCommunities;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.ExtendedCommunitiesBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.LocalPrefBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.MultiExitDiscBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.OriginBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.as.path.Segments;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.as.path.SegmentsBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.message.Nlri;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.message.NlriBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.message.WithdrawnRoutesBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpOrigin;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteTargetIpv4CaseBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.target.ipv4._case.RouteTargetIpv4Builder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCase;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.ipv4.next.hop._case.Ipv4NextHopBuilder;
65
66 public class BGPParserTest {
67
68     /**
69      * Used by other tests as well
70      */
71     static final List<byte[]> inputBytes = new ArrayList<byte[]>();
72
73     private static int COUNTER = 7;
74
75     private static int MAX_SIZE = 300;
76
77     private static BGPUpdateMessageParser updateParser;
78
79     private static final int LENGTH_FIELD_LENGTH = 2;
80
81     @BeforeClass
82     public static void setUp() throws Exception {
83         updateParser = new BGPUpdateMessageParser(ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry());
84         for (int i = 1; i <= COUNTER; i++) {
85             final String name = "/up" + i + ".bin";
86             try (final InputStream is = BGPParserTest.class.getResourceAsStream(name)){
87                 if (is == null) {
88                     throw new IOException("Failed to get resource " + name);
89                 }
90                 final ByteArrayOutputStream bis = new ByteArrayOutputStream();
91                 final byte[] data = new byte[MAX_SIZE];
92                 int nRead = 0;
93                 while ((nRead = is.read(data, 0, data.length)) != -1) {
94                     bis.write(data, 0, nRead);
95                 }
96                 bis.flush();
97
98                 inputBytes.add(bis.toByteArray());
99                 is.close();
100             }
101         }
102     }
103
104
105     @Test
106     public void testResource() {
107         assertNotNull(inputBytes);
108     }
109
110     /*
111      * Tests IPv4 NEXT_HOP, ATOMIC_AGGREGATE, COMMUNITY, NLRI
112      *
113      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
114      * 00 54 <- length (84) - including header
115      * 02 <- message type
116      * 00 00 <- withdrawn routes length
117      * 00 31 <- total path attribute length (49)
118      * 40 <- attribute flags
119      * 01 <- attribute type code (origin)
120      * 01 <- attribute length
121      * 00 <- Origin value (IGP)
122      * 40 <- attribute flags
123      * 02 <- attribute type code (as path)
124      * 06 <- attribute length
125      * 02 <- AS_SEQUENCE
126      * 01 <- path segment count
127      * 00 00 fd ea <- path segment value (65002)
128      * 40 <- attribute flags
129      * 03 <- attribute type code (Next Hop)
130      * 04 <- attribute length
131      * 10 00 00 02 <- value (10.0.0.2)
132      * 80 <- attribute flags
133      * 04 <- attribute type code (multi exit disc)
134      * 04 <- attribute length
135      * 00 00 00 00 <- value
136      * 60 <- attribute flags
137      * 06 <- attribute type code (atomic aggregate)
138      * 00 <- attribute length
139      * 40 <- attribute flags
140      * 08 <- attribute type code (community)
141      * 10 <- attribute length FF FF FF
142      * 01 <- value (NO_EXPORT)
143      * FF FF FF 02 <- value (NO_ADVERTISE)
144      * FF FF FF 03 <- value (NO_EXPORT_SUBCONFED)
145      * FF FF FF 10 <- unknown Community
146      *
147      * //NLRI
148      * 18 ac 11 02 <- IPv4 Prefix (172.17.2.0 / 24)
149      * 18 ac 11 01 <- IPv4 Prefix (172.17.1.0 / 24)
150      * 18 ac 11 00 <- IPv4 Prefix (172.17.0.0 / 24)
151      */
152     @Test
153     public void testGetUpdateMessage1() throws Exception {
154
155         final byte[] body = ByteArray.cutBytes(inputBytes.get(0), MessageUtil.COMMON_HEADER_LENGTH);
156         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(0), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
157         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
158
159         // check fields
160
161         assertNull(message.getWithdrawnRoutes());
162
163         // attributes
164         final List<AsNumber> asNumbers = new ArrayList<AsNumber>();
165         asNumbers.add(new AsNumber(65002L));
166         final List<Segments> asPath = Lists.newArrayList();
167         asPath.add(new SegmentsBuilder().setAsSequence(asNumbers).build());
168
169         final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
170                 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.2")).build()).build();
171
172         final List<Communities> comms = Lists.newArrayList();
173         comms.add((Communities) CommunityUtil.NO_EXPORT);
174         comms.add((Communities) CommunityUtil.NO_ADVERTISE);
175         comms.add((Communities) CommunityUtil.NO_EXPORT_SUBCONFED);
176         comms.add((Communities) CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF10));
177
178         final UpdateBuilder builder = new UpdateBuilder();
179
180         // check nlri
181
182         final List<Ipv4Prefix> prefs = Lists.newArrayList();
183         prefs.add(new Ipv4Prefix("172.17.2.0/24"));
184         prefs.add(new Ipv4Prefix("172.17.1.0/24"));
185         prefs.add(new Ipv4Prefix("172.17.0.0/24"));
186
187         final Nlri nlri = new NlriBuilder().setNlri(prefs).build();
188
189         assertEquals(nlri, message.getNlri());
190
191         builder.setNlri(nlri);
192
193         // check path attributes
194
195         final Attributes attrs = message.getAttributes();
196
197         final AttributesBuilder paBuilder = new AttributesBuilder();
198
199         paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
200         assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
201
202         paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
203         assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
204
205         paBuilder.setCNextHop(nextHop);
206         assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
207
208         paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
209         assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
210
211         paBuilder.setAtomicAggregate(new AtomicAggregateBuilder().build());
212         assertEquals(paBuilder.getAtomicAggregate(), attrs.getAtomicAggregate());
213
214         paBuilder.setCommunities(comms);
215         assertEquals(paBuilder.getCommunities(), attrs.getCommunities());
216
217         paBuilder.setUnrecognizedAttributes(Collections.emptyList());
218
219         builder.setAttributes(paBuilder.build());
220
221         assertEquals(builder.build(), message);
222
223         final ByteBuf buffer = Unpooled.buffer();
224         BGPParserTest.updateParser.serializeMessage(message, buffer);
225         assertArrayEquals(inputBytes.get(0), ByteArray.readAllBytes(buffer));
226     }
227
228     /*
229      * Tests more AS Numbers in AS_PATH, AGGREGATOR, ORIGIN.INCOMPLETE
230      *
231      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
232      * 00 4b <- length (75) - including header
233      * 02 <- message type
234      * 00 00 <- withdrawn routes length
235      * 00 30 <- total path attribute length (48)
236      * 40 <- attribute flags
237      * 01 <- attribute type code (origin)
238      * 01 <- attribute length
239      * 02 <- Origin value (Incomplete)
240      * 40 <- attribute flags
241      * 02 <- attribute type code (as path)
242      * 10 <- attribute length
243      * 02 <- AS_SEQUENCE
244      * 01 <- path segment count
245      * 00 00 00 1e <- path segment value (30)
246      * 01 <- AS_SET
247      * 02 <- path segment count
248      * 00 00 00 0a <- path segment value (10)
249      * 00 00 00 14 <- path segment value (20)
250      * 40 <- attribute flags
251      * 03 <- attribute type (Next hop)
252      * 04 <- attribute length
253      * 0a 00 00 09 <- value (10.0.0.9)
254      * 80 <- attribute flags
255      * 04 <- attribute type code (multi exit disc)
256      * 04 <- attribute length
257      * 00 00 00 00 <- value
258      * c0 <- attribute flags
259      * 07 <- attribute type (Aggregator)
260      * 08 <- attribute length
261      * 00 00 00 1e <- value (AS number = 30)
262      * 0a 00 00 09 <- value (IP address = 10.0.0.9)
263      *
264      * //NLRI
265      * 15 ac 10 00 <- IPv4 Prefix (172.16.0.0 / 21)
266      */
267     @Test
268     public void testGetUpdateMessage3() throws Exception {
269         final byte[] body = ByteArray.cutBytes(inputBytes.get(2), MessageUtil.COMMON_HEADER_LENGTH);
270         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(2), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
271         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
272
273         final UpdateBuilder builder = new UpdateBuilder();
274
275         // check nlri
276         final Ipv4Prefix pref1 = new Ipv4Prefix("172.16.0.0/21");
277
278         final List<Ipv4Prefix> nlri = Lists.newArrayList(pref1);
279         builder.setNlri(new NlriBuilder().setNlri(nlri).build());
280         assertEquals(builder.getNlri(), message.getNlri());
281
282         // check fields
283         assertNull(message.getWithdrawnRoutes());
284
285         // attributes
286         final List<AsNumber> asNumbers = new ArrayList<AsNumber>();
287         asNumbers.add(new AsNumber(30L));
288         final List<Segments> asPath = Lists.newArrayList();
289         asPath.add(new SegmentsBuilder().setAsSequence(asNumbers).build());
290         final List<AsNumber> asSet = Lists.newArrayList(new AsNumber(10L), new AsNumber(20L));
291         asPath.add(new SegmentsBuilder().setAsSet(asSet).build());
292
293         final Aggregator aggregator = new AggregatorBuilder().setAsNumber(new AsNumber((long) 30)).setNetworkAddress(
294                 new Ipv4Address("10.0.0.9")).build();
295         final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
296                 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.9")).build()).build();
297
298         // check path attributes
299         final Attributes attrs = message.getAttributes();
300
301         final AttributesBuilder paBuilder = new AttributesBuilder();
302
303         paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Incomplete).build());
304         assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
305
306         paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
307         assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
308
309         paBuilder.setCNextHop(nextHop);
310         assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
311
312         paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
313         assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
314
315         paBuilder.setAggregator(aggregator);
316         assertEquals(paBuilder.getAggregator(), attrs.getAggregator());
317         paBuilder.setUnrecognizedAttributes(Collections.emptyList());
318         builder.setAttributes(paBuilder.build());
319
320         assertEquals(builder.build(), message);
321
322         final ByteBuf buffer = Unpooled.buffer();
323         BGPParserTest.updateParser.serializeMessage(message, buffer);
324         assertArrayEquals(inputBytes.get(2), ByteArray.readAllBytes(buffer));
325     }
326
327     /*
328      * Tests empty AS_PATH, ORIGIN.EGP, LOCAL_PREF, EXTENDED_COMMUNITIES (Ipv4 Addr specific)
329      *
330      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
331      * 00 4A <- length (73) - including header
332      * 02 <- message type
333      * 00 00 <- withdrawn routes length
334      * 00 27 <- total path attribute length (39)
335      * 40 <- attribute flags
336      * 01 <- attribute type code (Origin)
337      * 01 <- attribute length
338      * 01 <- Origin value (EGP)
339      * 40 <- attribute flags
340      * 02 <- attribute type code (As path)
341      * 00 <- attribute length
342      * 40 <- attribute flags
343      * 03 <- attribute type (Next hop)
344      * 04 <- attribute length
345      * 03 03 03 03 <- value (3.3.3.3)
346      * 80 <- attribute flags
347      * 04 <- attribute type code (Multi exit disc)
348      * 04 <- attribute length
349      * 00 00 00 00 <- value
350      * 40 <- attribute flags
351      * 05 <- attribute type (Local Pref)
352      * 04 <- attribute length
353      * 00 00 00 64 <- value (100)
354      * c0 <- attribute flags
355      * 10 <- attribute type (extended community)
356      * 08 <- attribute length
357      * 01 04 <- value (type - Ipv4 Address Specific Extended Community)
358      * c0 a8 01 00 <- value (global adm. 198.162.1.0)
359      * 12 34 <- value (local adm. 4660)
360      *
361      * //NLRI
362      * 18 0a 1e 03 <- IPv4 Prefix (10.30.3.0 / 24)
363      * 18 0a 1e 02 <- IPv4 Prefix (10.30.2.0 / 24)
364      * 18 0a 1e 01 <- IPv4 Prefix (10.30.1.0 / 24)
365      */
366     @Test
367     public void testGetUpdateMessage4() throws Exception {
368         final byte[] body = ByteArray.cutBytes(inputBytes.get(3), MessageUtil.COMMON_HEADER_LENGTH);
369         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(3), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
370         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
371
372         final UpdateBuilder builder = new UpdateBuilder();
373
374         // check fields
375         assertNull(message.getWithdrawnRoutes());
376
377         // check nlri
378         final Ipv4Prefix pref1 = new Ipv4Prefix("10.30.3.0/24");
379         final Ipv4Prefix pref2 = new Ipv4Prefix("10.30.2.0/24");
380         final Ipv4Prefix pref3 = new Ipv4Prefix("10.30.1.0/24");
381
382         final List<Ipv4Prefix> nlri = Lists.newArrayList(pref1, pref2, pref3);
383         builder.setNlri(new NlriBuilder().setNlri(nlri).build());
384         assertEquals(builder.getNlri(), message.getNlri());
385
386         // attributes
387         final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
388                 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("3.3.3.3")).build()).build();
389
390         final List<ExtendedCommunities> comms = Lists.newArrayList();
391         comms.add(new ExtendedCommunitiesBuilder().setTransitive(true).setExtendedCommunity(
392                 new RouteTargetIpv4CaseBuilder().setRouteTargetIpv4(
393                         new RouteTargetIpv4Builder().setGlobalAdministrator(
394                                 new Ipv4Address("192.168.1.0")).setLocalAdministrator(4660).build()).build()).build());
395
396         // check path attributes
397         final Attributes attrs = message.getAttributes();
398
399         final AttributesBuilder paBuilder = new AttributesBuilder();
400
401         paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Egp).build());
402         assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
403
404         paBuilder.setAsPath(new AsPathBuilder().setSegments(Collections.emptyList()).build());
405         assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
406
407         paBuilder.setCNextHop(nextHop);
408         assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
409
410         paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
411         assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
412
413         paBuilder.setLocalPref(new LocalPrefBuilder().setPref(100L).build());
414         assertEquals(paBuilder.getLocalPref(), attrs.getLocalPref());
415
416         paBuilder.setExtendedCommunities(comms);
417         assertEquals(paBuilder.getExtendedCommunities(), attrs.getExtendedCommunities());
418
419         paBuilder.setUnrecognizedAttributes(Collections.emptyList());
420         // check API message
421         builder.setAttributes(paBuilder.build());
422         assertEquals(builder.build(), message);
423
424         final ByteBuf buffer = Unpooled.buffer();
425         BGPParserTest.updateParser.serializeMessage(message, buffer);
426         assertArrayEquals(inputBytes.get(3), ByteArray.readAllBytes(buffer));
427     }
428
429     /*
430      * Tests withdrawn routes.
431      *
432      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
433      * 00 1c <- length (28) - including header
434      * 02 <- message type
435      * 00 05 <- withdrawn routes length (5)
436      * 1e ac 10 00 04 <- route (172.16.0.4)
437      * 00 00 <- total path attribute length
438      */
439     @Test
440     public void testGetUpdateMessage5() throws Exception {
441         final byte[] body = ByteArray.cutBytes(inputBytes.get(4), MessageUtil.COMMON_HEADER_LENGTH);
442         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(4), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
443         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
444
445         // attributes
446         final List<Ipv4Prefix> prefs = Lists.newArrayList(new Ipv4Prefix("172.16.0.4/30"));
447
448         // check API message
449         final Update expectedMessage = new UpdateBuilder().setWithdrawnRoutes(
450                 new WithdrawnRoutesBuilder().setWithdrawnRoutes(prefs).build()).build();
451
452         assertEquals(expectedMessage.getWithdrawnRoutes(), message.getWithdrawnRoutes());
453
454         final ByteBuf buffer = Unpooled.buffer();
455         BGPParserTest.updateParser.serializeMessage(message, buffer);
456         assertArrayEquals(inputBytes.get(4), ByteArray.readAllBytes(buffer));
457     }
458
459     /*
460      * End of Rib for Ipv4.
461      *
462      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
463      * 00 17 <- length (23) - including header
464      * 02 <- message type
465      * 00 00 <- withdrawn routes length
466      * 00 00 <- total path attribute length
467      */
468     @Test
469     public void testEORIpv4() throws Exception {
470         final byte[] body = ByteArray.cutBytes(inputBytes.get(5), MessageUtil.COMMON_HEADER_LENGTH);
471         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(5), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
472         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
473
474         assertEquals(new UpdateBuilder().build(), message);
475
476         final ByteBuf buffer = Unpooled.buffer();
477         BGPParserTest.updateParser.serializeMessage(message, buffer);
478         assertArrayEquals(inputBytes.get(5), ByteArray.readAllBytes(buffer));
479     }
480
481     /*
482      * End of Rib for Ipv6 consists of empty MP_UNREACH_NLRI, with AFI 2 and SAFI 1
483      *
484      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
485      * 00 1d <- length (29) - including header
486      * 02 <- message type
487      * 00 00 <- withdrawn routes length
488      * 00 06 <- total path attribute length
489      * 80 <- attribute flags
490      * 0f <- attribute type (15 - MP_UNREACH_NLRI)
491      * 03 <- attribute length
492      * 00 02 <- value (AFI 2: IPv6)
493      * 01 <- value (SAFI 1)
494      */
495     @Test
496     public void testEORIpv6() throws Exception {
497         final byte[] body = ByteArray.cutBytes(inputBytes.get(6), MessageUtil.COMMON_HEADER_LENGTH);
498         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(6), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
499         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
500
501         final Class<? extends AddressFamily> afi = message.getAttributes().getAugmentation(Attributes2.class).getMpUnreachNlri().getAfi();
502         final Class<? extends SubsequentAddressFamily> safi = message.getAttributes().getAugmentation(Attributes2.class).getMpUnreachNlri().getSafi();
503
504         assertEquals(Ipv6AddressFamily.class, afi);
505         assertEquals(UnicastSubsequentAddressFamily.class, safi);
506
507         final ByteBuf buffer = Unpooled.buffer();
508         BGPParserTest.updateParser.serializeMessage(message, buffer);
509         assertArrayEquals(inputBytes.get(6), ByteArray.readAllBytes(buffer));
510     }
511
512     /*
513      * End of Rib for Ipv6 consists of empty MP_UNREACH_NLRI, with AFI 2 and SAFI 1
514      *
515      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
516      * 00 1e <- length (29) - including header
517      * 02 <- message type
518      * 00 00 <- withdrawn routes length
519      * 00 07 <- total path attribute length
520      * 90 <- attribute flags
521      * 0f <- attribute type (15 - MP_UNREACH_NLRI)
522      * 00 03 <- attribute length
523      * 00 02 <- value (AFI 2: IPv6)
524      * 01 <- value (SAFI 1)
525      */
526     @Test
527     public void testEORIpv6exLength() throws Exception {
528         final byte[] body = ByteArray.cutBytes(inputBytes.get(6), MessageUtil.COMMON_HEADER_LENGTH);
529         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(6), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
530         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
531
532         final Class<? extends AddressFamily> afi = message.getAttributes().getAugmentation(Attributes2.class).getMpUnreachNlri().getAfi();
533         final Class<? extends SubsequentAddressFamily> safi = message.getAttributes().getAugmentation(Attributes2.class).getMpUnreachNlri().getSafi();
534
535         assertEquals(Ipv6AddressFamily.class, afi);
536         assertEquals(UnicastSubsequentAddressFamily.class, safi);
537
538         final ByteBuf buffer = Unpooled.buffer();
539         BGPParserTest.updateParser.serializeMessage(message, buffer);
540         assertArrayEquals(inputBytes.get(6), ByteArray.readAllBytes(buffer));
541     }
542 }