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