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