BUG-2823: Simplify AS_PATH encoding
[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 com.google.common.collect.Lists;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import java.io.ByteArrayOutputStream;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.List;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.opendaylight.protocol.bgp.parser.impl.message.BGPUpdateMessageParser;
26 import org.opendaylight.protocol.bgp.parser.impl.message.update.CommunityUtil;
27 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
28 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
29 import org.opendaylight.protocol.util.ByteArray;
30 import org.opendaylight.protocol.util.NoopReferenceCache;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.prefixes.DestinationIpv6Builder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.prefixes.destination.ipv6.Ipv6Prefixes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.prefixes.destination.ipv6.Ipv6PrefixesBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv6CaseBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.UpdateBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.Aggregator;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.AggregatorBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.AsPathBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.AtomicAggregateBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.ClusterIdBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.Communities;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.ExtendedCommunities;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.ExtendedCommunitiesBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.LocalPrefBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.MultiExitDiscBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.OriginBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.OriginatorIdBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.UnrecognizedAttributes;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.as.path.Segments;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.as.path.SegmentsBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.message.Nlri;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.message.NlriBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.message.WithdrawnRoutesBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1Builder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlriBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpOrigin;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ClusterIdentifier;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.Inet4SpecificExtendedCommunityCaseBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.inet4.specific.extended.community._case.Inet4SpecificExtendedCommunityBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCase;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv6NextHopCase;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv6NextHopCaseBuilder;
79 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;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.ipv6.next.hop._case.Ipv6NextHopBuilder;
81
82 public class BGPParserTest {
83
84     /**
85      * Used by other tests as well
86      */
87     static final List<byte[]> inputBytes = new ArrayList<byte[]>();
88
89     private static int COUNTER = 7;
90
91     private static int MAX_SIZE = 300;
92
93     private static BGPUpdateMessageParser updateParser;
94
95     private static final int LENGTH_FIELD_LENGTH = 2;
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     }
119
120
121     @Test
122     public void testResource() {
123         assertNotNull(inputBytes);
124     }
125
126     /*
127      * Tests IPv4 NEXT_HOP, ATOMIC_AGGREGATE, COMMUNITY, NLRI
128      *
129      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
130      * 00 54 <- length (84) - including header
131      * 02 <- message type
132      * 00 00 <- withdrawn routes length
133      * 00 31 <- total path attribute length (49)
134      * 40 <- attribute flags
135      * 01 <- attribute type code (origin)
136      * 01 <- attribute length
137      * 00 <- Origin value (IGP)
138      * 40 <- attribute flags
139      * 02 <- attribute type code (as path)
140      * 06 <- attribute length
141      * 02 <- AS_SEQUENCE
142      * 01 <- path segment count
143      * 00 00 fd ea <- path segment value (65002)
144      * 40 <- attribute flags
145      * 03 <- attribute type code (Next Hop)
146      * 04 <- attribute length
147      * 10 00 00 02 <- value (10.0.0.2)
148      * 80 <- attribute flags
149      * 04 <- attribute type code (multi exit disc)
150      * 04 <- attribute length
151      * 00 00 00 00 <- value
152      * 60 <- attribute flags
153      * 06 <- attribute type code (atomic aggregate)
154      * 00 <- attribute length
155      * 40 <- attribute flags
156      * 08 <- attribute type code (community)
157      * 10 <- attribute length FF FF FF
158      * 01 <- value (NO_EXPORT)
159      * FF FF FF 02 <- value (NO_ADVERTISE)
160      * FF FF FF 03 <- value (NO_EXPORT_SUBCONFED)
161      * FF FF FF 10 <- unknown Community
162      *
163      * //NLRI
164      * 18 ac 11 02 <- IPv4 Prefix (172.17.2.0 / 24)
165      * 18 ac 11 01 <- IPv4 Prefix (172.17.1.0 / 24)
166      * 18 ac 11 00 <- IPv4 Prefix (172.17.0.0 / 24)
167      */
168     @Test
169     public void testGetUpdateMessage1() throws Exception {
170
171         final byte[] body = ByteArray.cutBytes(inputBytes.get(0), MessageUtil.COMMON_HEADER_LENGTH);
172         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(0), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
173         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
174
175         // check fields
176
177         assertNull(message.getWithdrawnRoutes());
178
179         // attributes
180         final List<AsNumber> asNumbers = new ArrayList<AsNumber>();
181         asNumbers.add(new AsNumber(65002L));
182         final List<Segments> asPath = Lists.newArrayList();
183         asPath.add(new SegmentsBuilder().setAsSequence(asNumbers).build());
184
185         final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
186             new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.2")).build()).build();
187
188         final List<Communities> comms = Lists.newArrayList();
189         comms.add((Communities) CommunityUtil.NO_EXPORT);
190         comms.add((Communities) CommunityUtil.NO_ADVERTISE);
191         comms.add((Communities) CommunityUtil.NO_EXPORT_SUBCONFED);
192         comms.add((Communities) CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF10));
193
194         final UpdateBuilder builder = new UpdateBuilder();
195
196         // check nlri
197
198         final List<Ipv4Prefix> prefs = Lists.newArrayList();
199         prefs.add(new Ipv4Prefix("172.17.2.0/24"));
200         prefs.add(new Ipv4Prefix("172.17.1.0/24"));
201         prefs.add(new Ipv4Prefix("172.17.0.0/24"));
202
203         final Nlri nlri = new NlriBuilder().setNlri(prefs).build();
204
205         assertEquals(nlri, message.getNlri());
206
207         builder.setNlri(nlri);
208
209         // check path attributes
210
211         final Attributes attrs = message.getAttributes();
212
213         final AttributesBuilder paBuilder = new AttributesBuilder();
214
215         paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
216         assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
217
218         paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
219         assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
220
221         paBuilder.setCNextHop(nextHop);
222         assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
223
224         paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
225         assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
226
227         paBuilder.setAtomicAggregate(new AtomicAggregateBuilder().build());
228         assertEquals(paBuilder.getAtomicAggregate(), attrs.getAtomicAggregate());
229
230         paBuilder.setCommunities(comms);
231         assertEquals(paBuilder.getCommunities(), attrs.getCommunities());
232
233         paBuilder.setUnrecognizedAttributes(Collections.<UnrecognizedAttributes> emptyList());
234
235         builder.setAttributes(paBuilder.build());
236
237         assertEquals(builder.build(), message);
238
239         final ByteBuf buffer = Unpooled.buffer();
240         BGPParserTest.updateParser.serializeMessage(message, buffer);
241         assertArrayEquals(inputBytes.get(0), ByteArray.readAllBytes(buffer));
242     }
243
244     /*
245      * Tests IPv6 NEXT_HOP, NLRI, ORIGIN.IGP, MULTI_EXIT_DISC, ORIGINATOR-ID, CLUSTER_LIST.
246      *
247      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
248      * 00 80 <- length (128) - including header
249      * 02 <- message type
250      * 00 00 <- withdrawn routes length
251      * 00 69 <- total path attribute length (105)
252      * 40 <- attribute flags
253      * 01 <- attribute type code (origin)
254      * 01 <- attribute length
255      * 00 <- Origin value (IGP)
256      * 40 <- attribute flags
257      * 02 <- attribute type code (as path)
258      * 06 <- attribute length
259      * 02 <- AS_SEQUENCE
260      * 01 <- path segment count
261      * 00 00 fd e9 <- path segment value (65001)
262      * 40 <- attribute flags
263      * 03 <- attribute type code (next hop)
264      * 04 <- attribute length
265      * 0a 00 00 00 <- next hop value (10.0.0.0)
266      * 80 <- attribute flags
267      * 04 <- attribute type code (multi exit disc)
268      * 04 <- attribute length
269      * 00 00 00 00 <- value
270      * 80 <- attribute flags
271      * 09 <- attribute type code (originator id)
272      * 04 <- attribute length
273      * 7f 00 00 01 <- value (localhost ip)
274      * 80 <- attribute flags
275      * 0a <- attribute type code (cluster list)
276      * 08 <- attribute length
277      * 01 02 03 04 <- value
278      * 05 06 07 08 <- value
279      * 80 <- attribute flags
280      * 0e <- attribute type code (mp reach nlri)
281      * 40 <- attribute length
282      * 00 02 <- AFI (Ipv6)
283      * 01 <- SAFI (Unicast)
284      * 20 <- length of next hop
285      * 20 01 0d b8 00 00 00 00 00 00 00 00 00 00 00 01 <- global
286      * fe 80 00 00 00 00 00 00 c0 01 0b ff fe 7e 00 <- link local
287      * 00 <- reserved
288      *
289      * //NLRI
290      * 40 20 01 0d b8 00 01 00 02 <- IPv6 Prefix (2001:db8:1:2:: / 64)
291      * 40 20 01 0d b8 00 01 00 01 <- IPv6 Prefix (2001:db8:1:1:: / 64)
292      * 40 20 01 0d b8 00 01 00 00 <- IPv6 Prefix (2001:db8:1:: / 64)
293      *
294      */
295     @Test
296     public void testGetUpdateMessage2() throws Exception {
297         final byte[] body = ByteArray.cutBytes(inputBytes.get(1), MessageUtil.COMMON_HEADER_LENGTH);
298         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(1), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
299         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
300
301         // check fields
302         assertNull(message.getWithdrawnRoutes());
303
304         final UpdateBuilder builder = new UpdateBuilder();
305
306         // check NLRI
307
308         final List<Ipv6Prefixes> prefs = new ArrayList<>();
309         prefs.add(new Ipv6PrefixesBuilder().setPrefix(new Ipv6Prefix("2001:db8:1:2::/64")).build());
310         prefs.add(new Ipv6PrefixesBuilder().setPrefix(new Ipv6Prefix("2001:db8:1:1::/64")).build());
311         prefs.add(new Ipv6PrefixesBuilder().setPrefix(new Ipv6Prefix("2001:db8:1::/64")).build());
312
313         assertNull(message.getNlri());
314
315         // attributes
316         final List<AsNumber> asNumbers = new ArrayList<AsNumber>();
317         asNumbers.add(new AsNumber(65001L));
318         final List<Segments> asPath = Lists.newArrayList();
319         asPath.add(new SegmentsBuilder().setAsSequence(asNumbers).build());
320
321         final Ipv6NextHopCase nextHop = new Ipv6NextHopCaseBuilder().setIpv6NextHop(
322             new Ipv6NextHopBuilder().setGlobal(new Ipv6Address("2001:db8::1")).setLinkLocal(new Ipv6Address("fe80::c001:bff:fe7e:0")).build()).build();
323
324         final List<ClusterIdentifier> clusters = Lists.newArrayList(new ClusterIdentifier(new Ipv4Address("1.2.3.4")),
325             new ClusterIdentifier(new Ipv4Address("5.6.7.8")));
326
327         // check path attributes
328
329         final Attributes attrs = message.getAttributes();
330
331         final AttributesBuilder paBuilder = new AttributesBuilder();
332
333         paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
334         assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
335
336         paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
337         assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
338
339         paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
340         assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
341
342         paBuilder.setOriginatorId(new OriginatorIdBuilder().setOriginator(new Ipv4Address("127.0.0.1")).build());
343         assertEquals(paBuilder.getOriginatorId(), attrs.getOriginatorId());
344
345         paBuilder.setClusterId(new ClusterIdBuilder().setCluster(clusters).build());
346         assertEquals(paBuilder.getClusterId(), attrs.getClusterId());
347
348         final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
349         mpBuilder.setAfi(Ipv6AddressFamily.class);
350         mpBuilder.setSafi(UnicastSubsequentAddressFamily.class);
351         mpBuilder.setCNextHop(nextHop);
352         mpBuilder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
353             new DestinationIpv6CaseBuilder().setDestinationIpv6(new DestinationIpv6Builder().setIpv6Prefixes(prefs).build()).build()).build());
354
355         paBuilder.addAugmentation(Attributes1.class, new Attributes1Builder().setMpReachNlri(mpBuilder.build()).build());
356         assertEquals(paBuilder.getAugmentation(Attributes1.class).getMpReachNlri(),
357             attrs.getAugmentation(Attributes1.class).getMpReachNlri());
358         paBuilder.setUnrecognizedAttributes(Collections.<UnrecognizedAttributes> emptyList());
359         // check API message
360
361         builder.setAttributes(paBuilder.build());
362         assertEquals(builder.build(), message);
363
364         final ByteBuf buffer = Unpooled.buffer();
365         BGPParserTest.updateParser.serializeMessage(message, buffer);
366         assertArrayEquals(inputBytes.get(1), ByteArray.readAllBytes(buffer));
367     }
368
369     /*
370      * Tests more AS Numbers in AS_PATH, AGGREGATOR, ORIGIN.INCOMPLETE
371      *
372      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
373      * 00 4b <- length (75) - including header
374      * 02 <- message type
375      * 00 00 <- withdrawn routes length
376      * 00 30 <- total path attribute length (48)
377      * 40 <- attribute flags
378      * 01 <- attribute type code (origin)
379      * 01 <- attribute length
380      * 02 <- Origin value (Incomplete)
381      * 40 <- attribute flags
382      * 02 <- attribute type code (as path)
383      * 10 <- attribute length
384      * 02 <- AS_SEQUENCE
385      * 01 <- path segment count
386      * 00 00 00 1e <- path segment value (30)
387      * 01 <- AS_SET
388      * 02 <- path segment count
389      * 00 00 00 0a <- path segment value (10)
390      * 00 00 00 14 <- path segment value (20)
391      * 40 <- attribute flags
392      * 03 <- attribute type (Next hop)
393      * 04 <- attribute length
394      * 0a 00 00 09 <- value (10.0.0.9)
395      * 80 <- attribute flags
396      * 04 <- attribute type code (multi exit disc)
397      * 04 <- attribute length
398      * 00 00 00 00 <- value
399      * c0 <- attribute flags
400      * 07 <- attribute type (Aggregator)
401      * 08 <- attribute length
402      * 00 00 00 1e <- value (AS number = 30)
403      * 0a 00 00 09 <- value (IP address = 10.0.0.9)
404      *
405      * //NLRI
406      * 15 ac 10 00 <- IPv4 Prefix (172.16.0.0 / 21)
407      */
408     @Test
409     public void testGetUpdateMessage3() throws Exception {
410         final byte[] body = ByteArray.cutBytes(inputBytes.get(2), MessageUtil.COMMON_HEADER_LENGTH);
411         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(2), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
412         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
413
414         final UpdateBuilder builder = new UpdateBuilder();
415
416         // check nlri
417         final Ipv4Prefix pref1 = new Ipv4Prefix("172.16.0.0/21");
418
419         final List<Ipv4Prefix> nlri = Lists.newArrayList(pref1);
420         builder.setNlri(new NlriBuilder().setNlri(nlri).build());
421         assertEquals(builder.getNlri(), message.getNlri());
422
423         // check fields
424         assertNull(message.getWithdrawnRoutes());
425
426         // attributes
427         final List<AsNumber> asNumbers = new ArrayList<AsNumber>();
428         asNumbers.add(new AsNumber(30L));
429         final List<Segments> asPath = Lists.newArrayList();
430         asPath.add(new SegmentsBuilder().setAsSequence(asNumbers).build());
431         final List<AsNumber> asSet = Lists.newArrayList(new AsNumber(10L), new AsNumber(20L));
432         asPath.add(new SegmentsBuilder().setAsSet(asSet).build());
433
434         final Aggregator aggregator = new AggregatorBuilder().setAsNumber(new AsNumber((long) 30)).setNetworkAddress(
435             new Ipv4Address("10.0.0.9")).build();
436         final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
437             new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.9")).build()).build();
438
439         // check path attributes
440         final Attributes attrs = message.getAttributes();
441
442         final AttributesBuilder paBuilder = new AttributesBuilder();
443
444         paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Incomplete).build());
445         assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
446
447         paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
448         assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
449
450         paBuilder.setCNextHop(nextHop);
451         assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
452
453         paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
454         assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
455
456         paBuilder.setAggregator(aggregator);
457         assertEquals(paBuilder.getAggregator(), attrs.getAggregator());
458         paBuilder.setUnrecognizedAttributes(Collections.<UnrecognizedAttributes> emptyList());
459         builder.setAttributes(paBuilder.build());
460
461         assertEquals(builder.build(), message);
462
463         final ByteBuf buffer = Unpooled.buffer();
464         BGPParserTest.updateParser.serializeMessage(message, buffer);
465         assertArrayEquals(inputBytes.get(2), ByteArray.readAllBytes(buffer));
466     }
467
468     /*
469      * Tests empty AS_PATH, ORIGIN.EGP, LOCAL_PREF, EXTENDED_COMMUNITIES (Ipv4 Addr specific)
470      *
471      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
472      * 00 4A <- length (73) - including header
473      * 02 <- message type
474      * 00 00 <- withdrawn routes length
475      * 00 27 <- total path attribute length (39)
476      * 40 <- attribute flags
477      * 01 <- attribute type code (Origin)
478      * 01 <- attribute length
479      * 01 <- Origin value (EGP)
480      * 40 <- attribute flags
481      * 02 <- attribute type code (As path)
482      * 00 <- attribute length
483      * 40 <- attribute flags
484      * 03 <- attribute type (Next hop)
485      * 04 <- attribute length
486      * 03 03 03 03 <- value (3.3.3.3)
487      * 80 <- attribute flags
488      * 04 <- attribute type code (Multi exit disc)
489      * 04 <- attribute length
490      * 00 00 00 00 <- value
491      * 40 <- attribute flags
492      * 05 <- attribute type (Local Pref)
493      * 04 <- attribute length
494      * 00 00 00 64 <- value (100)
495      * c0 <- attribute flags
496      * 10 <- attribute type (extended community)
497      * 08 <- attribute length
498      * 01 04 <- value (type - Ipv4 Address Specific Extended Community)
499      * c0 a8 01 00 <- value (global adm. 198.162.1.0)
500      * 12 34 <- value (local adm. 4660)
501      *
502      * //NLRI
503      * 18 0a 1e 03 <- IPv4 Prefix (10.30.3.0 / 24)
504      * 18 0a 1e 02 <- IPv4 Prefix (10.30.2.0 / 24)
505      * 18 0a 1e 01 <- IPv4 Prefix (10.30.1.0 / 24)
506      */
507     @Test
508     public void testGetUpdateMessage4() throws Exception {
509         final byte[] body = ByteArray.cutBytes(inputBytes.get(3), MessageUtil.COMMON_HEADER_LENGTH);
510         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(3), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
511         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
512
513         final UpdateBuilder builder = new UpdateBuilder();
514
515         // check fields
516         assertNull(message.getWithdrawnRoutes());
517
518         // check nlri
519         final Ipv4Prefix pref1 = new Ipv4Prefix("10.30.3.0/24");
520         final Ipv4Prefix pref2 = new Ipv4Prefix("10.30.2.0/24");
521         final Ipv4Prefix pref3 = new Ipv4Prefix("10.30.1.0/24");
522
523         final List<Ipv4Prefix> nlri = Lists.newArrayList(pref1, pref2, pref3);
524         builder.setNlri(new NlriBuilder().setNlri(nlri).build());
525         assertEquals(builder.getNlri(), message.getNlri());
526
527         // attributes
528         final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
529             new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("3.3.3.3")).build()).build();
530
531         final List<ExtendedCommunities> comms = Lists.newArrayList();
532         comms.add(new ExtendedCommunitiesBuilder().setCommType((short) 1).setCommSubType((short) 4).setExtendedCommunity(
533             new Inet4SpecificExtendedCommunityCaseBuilder().setInet4SpecificExtendedCommunity(
534                 new Inet4SpecificExtendedCommunityBuilder().setTransitive(false).setGlobalAdministrator(
535                     new Ipv4Address("192.168.1.0")).setLocalAdministrator(new byte[] { 0x12, 0x34 }).build()).build()).build());
536
537         // check path attributes
538         final Attributes attrs = message.getAttributes();
539
540         final AttributesBuilder paBuilder = new AttributesBuilder();
541
542         paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Egp).build());
543         assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
544
545         paBuilder.setAsPath(new AsPathBuilder().setSegments(Collections.<Segments> emptyList()).build());
546         assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
547
548         paBuilder.setCNextHop(nextHop);
549         assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
550
551         paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
552         assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
553
554         paBuilder.setLocalPref(new LocalPrefBuilder().setPref(100L).build());
555         assertEquals(paBuilder.getLocalPref(), attrs.getLocalPref());
556
557         paBuilder.setExtendedCommunities(comms);
558         assertEquals(paBuilder.getExtendedCommunities(), attrs.getExtendedCommunities());
559
560         paBuilder.setUnrecognizedAttributes(Collections.<UnrecognizedAttributes> emptyList());
561         // check API message
562         builder.setAttributes(paBuilder.build());
563         assertEquals(builder.build(), message);
564
565         final ByteBuf buffer = Unpooled.buffer();
566         BGPParserTest.updateParser.serializeMessage(message, buffer);
567         assertArrayEquals(inputBytes.get(3), ByteArray.readAllBytes(buffer));
568     }
569
570     /*
571      * Tests withdrawn routes.
572      *
573      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
574      * 00 1c <- length (28) - including header
575      * 02 <- message type
576      * 00 05 <- withdrawn routes length (5)
577      * 1e ac 10 00 04 <- route (172.16.0.4)
578      * 00 00 <- total path attribute length
579      */
580     @Test
581     public void testGetUpdateMessage5() throws Exception {
582         final byte[] body = ByteArray.cutBytes(inputBytes.get(4), MessageUtil.COMMON_HEADER_LENGTH);
583         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(4), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
584         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
585
586         // attributes
587         final List<Ipv4Prefix> prefs = Lists.newArrayList(new Ipv4Prefix("172.16.0.4/30"));
588
589         // check API message
590         final Update expectedMessage = new UpdateBuilder().setWithdrawnRoutes(
591             new WithdrawnRoutesBuilder().setWithdrawnRoutes(prefs).build()).build();
592
593         assertEquals(expectedMessage.getWithdrawnRoutes(), message.getWithdrawnRoutes());
594
595         final ByteBuf buffer = Unpooled.buffer();
596         BGPParserTest.updateParser.serializeMessage(message, buffer);
597         assertArrayEquals(inputBytes.get(4), ByteArray.readAllBytes(buffer));
598     }
599
600     /*
601      * End of Rib for Ipv4.
602      *
603      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
604      * 00 17 <- length (23) - including header
605      * 02 <- message type
606      * 00 00 <- withdrawn routes length
607      * 00 00 <- total path attribute length
608      */
609     @Test
610     public void testEORIpv4() throws Exception {
611         final byte[] body = ByteArray.cutBytes(inputBytes.get(5), MessageUtil.COMMON_HEADER_LENGTH);
612         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(5), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
613         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
614
615         assertEquals(new UpdateBuilder().build(), message);
616
617         final ByteBuf buffer = Unpooled.buffer();
618         BGPParserTest.updateParser.serializeMessage(message, buffer);
619         assertArrayEquals(inputBytes.get(5), ByteArray.readAllBytes(buffer));
620     }
621
622     /*
623      * End of Rib for Ipv6 consists of empty MP_UNREACH_NLRI, with AFI 2 and SAFI 1
624      *
625      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
626      * 00 1d <- length (29) - including header
627      * 02 <- message type
628      * 00 00 <- withdrawn routes length
629      * 00 06 <- total path attribute length
630      * 80 <- attribute flags
631      * 0f <- attribute type (15 - MP_UNREACH_NLRI)
632      * 03 <- attribute length
633      * 00 02 <- value (AFI 2: IPv6)
634      * 01 <- value (SAFI 1)
635      */
636     @Test
637     public void testEORIpv6() throws Exception {
638         final byte[] body = ByteArray.cutBytes(inputBytes.get(6), MessageUtil.COMMON_HEADER_LENGTH);
639         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(6), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
640         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
641
642         final Class<? extends AddressFamily> afi = message.getAttributes().getAugmentation(Attributes2.class).getMpUnreachNlri().getAfi();
643         final Class<? extends SubsequentAddressFamily> safi = message.getAttributes().getAugmentation(Attributes2.class).getMpUnreachNlri().getSafi();
644
645         assertEquals(Ipv6AddressFamily.class, afi);
646         assertEquals(UnicastSubsequentAddressFamily.class, safi);
647
648         final ByteBuf buffer = Unpooled.buffer();
649         BGPParserTest.updateParser.serializeMessage(message, buffer);
650         assertArrayEquals(inputBytes.get(6), ByteArray.readAllBytes(buffer));
651     }
652
653     /*
654      * End of Rib for Ipv6 consists of empty MP_UNREACH_NLRI, with AFI 2 and SAFI 1
655      *
656      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
657      * 00 1e <- length (29) - including header
658      * 02 <- message type
659      * 00 00 <- withdrawn routes length
660      * 00 07 <- total path attribute length
661      * 90 <- attribute flags
662      * 0f <- attribute type (15 - MP_UNREACH_NLRI)
663      * 00 03 <- attribute length
664      * 00 02 <- value (AFI 2: IPv6)
665      * 01 <- value (SAFI 1)
666      */
667     @Test
668     public void testEORIpv6exLength() throws Exception {
669         final byte[] body = ByteArray.cutBytes(inputBytes.get(6), MessageUtil.COMMON_HEADER_LENGTH);
670         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(6), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
671         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
672
673         final Class<? extends AddressFamily> afi = message.getAttributes().getAugmentation(Attributes2.class).getMpUnreachNlri().getAfi();
674         final Class<? extends SubsequentAddressFamily> safi = message.getAttributes().getAugmentation(Attributes2.class).getMpUnreachNlri().getSafi();
675
676         assertEquals(Ipv6AddressFamily.class, afi);
677         assertEquals(UnicastSubsequentAddressFamily.class, safi);
678
679         final ByteBuf buffer = Unpooled.buffer();
680         BGPParserTest.updateParser.serializeMessage(message, buffer);
681         assertArrayEquals(inputBytes.get(6), ByteArray.readAllBytes(buffer));
682     }
683 }