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