Fix most bgp-parser-impl checkstyle
[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 static org.mockito.Mockito.mock;
15
16 import com.google.common.collect.Lists;
17 import io.netty.buffer.ByteBuf;
18 import io.netty.buffer.Unpooled;
19 import java.io.ByteArrayOutputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.Optional;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.mockito.Mockito;
29 import org.opendaylight.protocol.bgp.parser.impl.message.BGPUpdateMessageParser;
30 import org.opendaylight.protocol.bgp.parser.impl.message.update.CommunityUtil;
31 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
32 import org.opendaylight.protocol.bgp.parser.spi.MultiPathSupport;
33 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
34 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
35 import org.opendaylight.protocol.bgp.util.HexDumpBGPFileParser;
36 import org.opendaylight.protocol.util.ByteArray;
37 import org.opendaylight.protocol.util.NoopReferenceCache;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.UpdateBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.Aggregator;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.AggregatorBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.AsPathBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.AtomicAggregateBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.Communities;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.ExtendedCommunities;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.ExtendedCommunitiesBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.LocalPrefBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.MultiExitDiscBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.OriginBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.as.path.Segments;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.as.path.SegmentsBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.update.message.Nlri;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.update.message.NlriBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.update.message.WithdrawnRoutes;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.update.message.WithdrawnRoutesBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.BgpOrigin;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv6AddressFamily;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.SubsequentAddressFamily;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.RouteTargetIpv4CaseBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv4NextHopCase;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.ipv4.next.hop._case.Ipv4NextHopBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.route.target.ipv4.grouping.RouteTargetIpv4Builder;
73
74 public class BGPParserTest {
75
76     /**
77      * Used by other tests as well.
78      */
79     static final List<byte[]> inputBytes = new ArrayList<>();
80
81     private static final int COUNTER = 7;
82
83     private static final int MAX_SIZE = 300;
84
85     private static BGPUpdateMessageParser updateParser;
86
87     private static final int LENGTH_FIELD_LENGTH = 2;
88
89     private static final String MULTIPATH_HEX_FILE = "/bgp-update-multipath.txt";
90
91     private static List<byte[]> updatesWithMultiplePath;
92
93     private static PeerSpecificParserConstraint constraint;
94
95     private static MultiPathSupport mpSupport;
96
97     @BeforeClass
98     public static void setUp() throws Exception {
99         updateParser = new BGPUpdateMessageParser(ServiceLoaderBGPExtensionProviderContext.getSingletonInstance()
100             .getAttributeRegistry());
101         for (int i = 1; i <= COUNTER; i++) {
102             final String name = "/up" + i + ".bin";
103             try (InputStream is = BGPParserTest.class.getResourceAsStream(name)) {
104                 if (is == null) {
105                     throw new IOException("Failed to get resource " + name);
106                 }
107                 final ByteArrayOutputStream bis = new ByteArrayOutputStream();
108                 final byte[] data = new byte[MAX_SIZE];
109                 int numRead = 0;
110                 while ((numRead = is.read(data, 0, data.length)) != -1) {
111                     bis.write(data, 0, numRead);
112                 }
113                 bis.flush();
114
115                 inputBytes.add(bis.toByteArray());
116                 is.close();
117             }
118         }
119         updatesWithMultiplePath = HexDumpBGPFileParser.parseMessages(BGPParserTest.class.getResourceAsStream(
120             MULTIPATH_HEX_FILE));
121         constraint = mock(PeerSpecificParserConstraint.class);
122         mpSupport = mock(MultiPathSupport.class);
123         Mockito.doReturn(Optional.empty()).when(constraint).getPeerConstraint(Mockito.any());
124         Mockito.doReturn(Optional.of(mpSupport)).when(constraint).getPeerConstraint(MultiPathSupport.class);
125         Mockito.doReturn(true).when(mpSupport).isTableTypeSupported(Mockito.any());
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,
180             LENGTH_FIELD_LENGTH));
181         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
182             null);
183
184         // check fields
185
186         assertNull(message.getWithdrawnRoutes());
187
188         // attributes
189         final List<AsNumber> asNumbers = new ArrayList<>();
190         asNumbers.add(new AsNumber(65002L));
191         final List<Segments> asPath = Lists.newArrayList();
192         asPath.add(new SegmentsBuilder().setAsSequence(asNumbers).build());
193
194         final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
195                 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.2")).build()).build();
196
197         final List<Communities> comms = Lists.newArrayList();
198         comms.add((Communities) CommunityUtil.NO_EXPORT);
199         comms.add((Communities) CommunityUtil.NO_ADVERTISE);
200         comms.add((Communities) CommunityUtil.NO_EXPORT_SUBCONFED);
201         comms.add((Communities) CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF10));
202
203         final UpdateBuilder builder = new UpdateBuilder();
204
205         // check nlri
206
207         final List<Nlri> nlris = Lists.newArrayList();
208         nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.2.0/24")).build());
209         nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.1.0/24")).build());
210         nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.0.0/24")).build());
211
212         assertEquals(nlris, message.getNlri());
213
214         builder.setNlri(nlris);
215
216         // check path attributes
217
218         final Attributes attrs = message.getAttributes();
219
220         final AttributesBuilder paBuilder = new AttributesBuilder();
221
222         paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
223         assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
224
225         paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
226         assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
227
228         paBuilder.setCNextHop(nextHop);
229         assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
230
231         paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
232         assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
233
234         paBuilder.setAtomicAggregate(new AtomicAggregateBuilder().build());
235         assertEquals(paBuilder.getAtomicAggregate(), attrs.getAtomicAggregate());
236
237         paBuilder.setCommunities(comms);
238         assertEquals(paBuilder.getCommunities(), attrs.getCommunities());
239
240         paBuilder.setUnrecognizedAttributes(Collections.emptyList());
241
242         builder.setAttributes(paBuilder.build());
243
244         assertEquals(builder.build(), message);
245
246         final ByteBuf buffer = Unpooled.buffer();
247         BGPParserTest.updateParser.serializeMessage(message, buffer);
248         assertArrayEquals(inputBytes.get(0), ByteArray.readAllBytes(buffer));
249     }
250
251     /*
252      * Tests more AS Numbers in AS_PATH, AGGREGATOR, ORIGIN.INCOMPLETE
253      *
254      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
255      * 00 4b <- length (75) - including header
256      * 02 <- message type
257      * 00 00 <- withdrawn routes length
258      * 00 30 <- total path attribute length (48)
259      * 40 <- attribute flags
260      * 01 <- attribute type code (origin)
261      * 01 <- attribute length
262      * 02 <- Origin value (Incomplete)
263      * 40 <- attribute flags
264      * 02 <- attribute type code (as path)
265      * 10 <- attribute length
266      * 02 <- AS_SEQUENCE
267      * 01 <- path segment count
268      * 00 00 00 1e <- path segment value (30)
269      * 01 <- AS_SET
270      * 02 <- path segment count
271      * 00 00 00 0a <- path segment value (10)
272      * 00 00 00 14 <- path segment value (20)
273      * 40 <- attribute flags
274      * 03 <- attribute type (Next hop)
275      * 04 <- attribute length
276      * 0a 00 00 09 <- value (10.0.0.9)
277      * 80 <- attribute flags
278      * 04 <- attribute type code (multi exit disc)
279      * 04 <- attribute length
280      * 00 00 00 00 <- value
281      * c0 <- attribute flags
282      * 07 <- attribute type (Aggregator)
283      * 08 <- attribute length
284      * 00 00 00 1e <- value (AS number = 30)
285      * 0a 00 00 09 <- value (IP address = 10.0.0.9)
286      *
287      * //NLRI
288      * 15 ac 10 00 <- IPv4 Prefix (172.16.0.0 / 21)
289      */
290     @Test
291     public void testGetUpdateMessage3() throws Exception {
292         final byte[] body = ByteArray.cutBytes(inputBytes.get(2), MessageUtil.COMMON_HEADER_LENGTH);
293         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(2), MessageUtil.MARKER_LENGTH,
294             LENGTH_FIELD_LENGTH));
295         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
296             null);
297
298         final UpdateBuilder builder = new UpdateBuilder();
299
300         // check nlri
301         final List<Nlri> nlris = Lists.newArrayList(new NlriBuilder().setPrefix(new Ipv4Prefix("172.16.0.0/21"))
302             .build());
303         builder.setNlri(nlris);
304         assertEquals(builder.getNlri(), message.getNlri());
305
306         // check fields
307         assertNull(message.getWithdrawnRoutes());
308
309         // attributes
310         final List<AsNumber> asNumbers = new ArrayList<>();
311         asNumbers.add(new AsNumber(30L));
312         final List<Segments> asPath = Lists.newArrayList();
313         asPath.add(new SegmentsBuilder().setAsSequence(asNumbers).build());
314         final List<AsNumber> asSet = Lists.newArrayList(new AsNumber(10L), new AsNumber(20L));
315         asPath.add(new SegmentsBuilder().setAsSet(asSet).build());
316
317         final Aggregator aggregator = new AggregatorBuilder().setAsNumber(new AsNumber((long) 30)).setNetworkAddress(
318                 new Ipv4Address("10.0.0.9")).build();
319         final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
320                 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.9")).build()).build();
321
322         // check path attributes
323         final Attributes attrs = message.getAttributes();
324
325         final AttributesBuilder paBuilder = new AttributesBuilder();
326
327         paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Incomplete).build());
328         assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
329
330         paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
331         assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
332
333         paBuilder.setCNextHop(nextHop);
334         assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
335
336         paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
337         assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
338
339         paBuilder.setAggregator(aggregator);
340         assertEquals(paBuilder.getAggregator(), attrs.getAggregator());
341         paBuilder.setUnrecognizedAttributes(Collections.emptyList());
342         builder.setAttributes(paBuilder.build());
343
344         assertEquals(builder.build(), message);
345
346         final ByteBuf buffer = Unpooled.buffer();
347         BGPParserTest.updateParser.serializeMessage(message, buffer);
348         assertArrayEquals(inputBytes.get(2), ByteArray.readAllBytes(buffer));
349     }
350
351     /*
352      * Tests empty AS_PATH, ORIGIN.EGP, LOCAL_PREF, EXTENDED_COMMUNITIES (Ipv4 Addr specific)
353      *
354      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
355      * 00 4A <- length (73) - including header
356      * 02 <- message type
357      * 00 00 <- withdrawn routes length
358      * 00 27 <- total path attribute length (39)
359      * 40 <- attribute flags
360      * 01 <- attribute type code (Origin)
361      * 01 <- attribute length
362      * 01 <- Origin value (EGP)
363      * 40 <- attribute flags
364      * 02 <- attribute type code (As path)
365      * 00 <- attribute length
366      * 40 <- attribute flags
367      * 03 <- attribute type (Next hop)
368      * 04 <- attribute length
369      * 03 03 03 03 <- value (3.3.3.3)
370      * 80 <- attribute flags
371      * 04 <- attribute type code (Multi exit disc)
372      * 04 <- attribute length
373      * 00 00 00 00 <- value
374      * 40 <- attribute flags
375      * 05 <- attribute type (Local Pref)
376      * 04 <- attribute length
377      * 00 00 00 64 <- value (100)
378      * c0 <- attribute flags
379      * 10 <- attribute type (extended community)
380      * 08 <- attribute length
381      * 01 04 <- value (type - Ipv4 Address Specific Extended Community)
382      * c0 a8 01 00 <- value (global adm. 198.162.1.0)
383      * 12 34 <- value (local adm. 4660)
384      *
385      * //NLRI
386      * 18 0a 1e 03 <- IPv4 Prefix (10.30.3.0 / 24)
387      * 18 0a 1e 02 <- IPv4 Prefix (10.30.2.0 / 24)
388      * 18 0a 1e 01 <- IPv4 Prefix (10.30.1.0 / 24)
389      */
390     @Test
391     public void testGetUpdateMessage4() throws Exception {
392         final byte[] body = ByteArray.cutBytes(inputBytes.get(3), MessageUtil.COMMON_HEADER_LENGTH);
393         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(3), MessageUtil.MARKER_LENGTH,
394             LENGTH_FIELD_LENGTH));
395         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
396             null);
397
398         final UpdateBuilder builder = new UpdateBuilder();
399
400         // check fields
401         assertNull(message.getWithdrawnRoutes());
402
403         // check nlri
404         final List<Nlri> nlris = Lists.newArrayList();
405         nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("10.30.3.0/24")).build());
406         nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("10.30.2.0/24")).build());
407         nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("10.30.1.0/24")).build());
408
409         assertEquals(nlris, message.getNlri());
410
411         builder.setNlri(nlris);
412
413         // attributes
414         final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
415                 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("3.3.3.3")).build()).build();
416
417         final List<ExtendedCommunities> comms = Lists.newArrayList();
418         comms.add(new ExtendedCommunitiesBuilder().setTransitive(true).setExtendedCommunity(
419                 new RouteTargetIpv4CaseBuilder().setRouteTargetIpv4(
420                         new RouteTargetIpv4Builder().setGlobalAdministrator(
421                                 new Ipv4Address("192.168.1.0")).setLocalAdministrator(4660)
422                                 .build()).build()).build());
423
424         // check path attributes
425         final Attributes attrs = message.getAttributes();
426
427         final AttributesBuilder paBuilder = new AttributesBuilder();
428
429         paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Egp).build());
430         assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
431
432         paBuilder.setAsPath(new AsPathBuilder().setSegments(Collections.emptyList()).build());
433         assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
434
435         paBuilder.setCNextHop(nextHop);
436         assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
437
438         paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
439         assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
440
441         paBuilder.setLocalPref(new LocalPrefBuilder().setPref(100L).build());
442         assertEquals(paBuilder.getLocalPref(), attrs.getLocalPref());
443
444         paBuilder.setExtendedCommunities(comms);
445         assertEquals(paBuilder.getExtendedCommunities(), attrs.getExtendedCommunities());
446
447         paBuilder.setUnrecognizedAttributes(Collections.emptyList());
448         // check API message
449         builder.setAttributes(paBuilder.build());
450         assertEquals(builder.build(), message);
451
452         final ByteBuf buffer = Unpooled.buffer();
453         BGPParserTest.updateParser.serializeMessage(message, buffer);
454         assertArrayEquals(inputBytes.get(3), ByteArray.readAllBytes(buffer));
455     }
456
457     /*
458      * Tests withdrawn routes.
459      *
460      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
461      * 00 1c <- length (28) - including header
462      * 02 <- message type
463      * 00 05 <- withdrawn routes length (5)
464      * 1e ac 10 00 04 <- route (172.16.0.4)
465      * 00 00 <- total path attribute length
466      */
467     @Test
468     public void testGetUpdateMessage5() throws Exception {
469         final byte[] body = ByteArray.cutBytes(inputBytes.get(4), MessageUtil.COMMON_HEADER_LENGTH);
470         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(4), MessageUtil.MARKER_LENGTH,
471             LENGTH_FIELD_LENGTH));
472         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
473             null);
474
475         // attributes
476         final List<WithdrawnRoutes> withdrawnRoutes = Lists.newArrayList(new WithdrawnRoutesBuilder()
477             .setPrefix(new Ipv4Prefix("172.16.0.4/30")).build());
478
479         // check API message
480         final Update expectedMessage = new UpdateBuilder().setWithdrawnRoutes(withdrawnRoutes).build();
481
482         assertEquals(expectedMessage.getWithdrawnRoutes(), message.getWithdrawnRoutes());
483
484         final ByteBuf buffer = Unpooled.buffer();
485         BGPParserTest.updateParser.serializeMessage(message, buffer);
486         assertArrayEquals(inputBytes.get(4), ByteArray.readAllBytes(buffer));
487     }
488
489     /*
490      * End of Rib for Ipv4.
491      *
492      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
493      * 00 17 <- length (23) - including header
494      * 02 <- message type
495      * 00 00 <- withdrawn routes length
496      * 00 00 <- total path attribute length
497      */
498     @Test
499     public void testEORIpv4() throws Exception {
500         final byte[] body = ByteArray.cutBytes(inputBytes.get(5), MessageUtil.COMMON_HEADER_LENGTH);
501         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(5), MessageUtil.MARKER_LENGTH,
502             LENGTH_FIELD_LENGTH));
503         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
504             null);
505
506         assertEquals(new UpdateBuilder().build(), message);
507
508         final ByteBuf buffer = Unpooled.buffer();
509         BGPParserTest.updateParser.serializeMessage(message, buffer);
510         assertArrayEquals(inputBytes.get(5), ByteArray.readAllBytes(buffer));
511     }
512
513     /*
514      * End of Rib for Ipv6 consists of empty MP_UNREACH_NLRI, with AFI 2 and SAFI 1
515      *
516      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
517      * 00 1d <- length (29) - including header
518      * 02 <- message type
519      * 00 00 <- withdrawn routes length
520      * 00 06 <- total path attribute length
521      * 80 <- attribute flags
522      * 0f <- attribute type (15 - MP_UNREACH_NLRI)
523      * 03 <- attribute length
524      * 00 02 <- value (AFI 2: IPv6)
525      * 01 <- value (SAFI 1)
526      */
527     @Test
528     public void testEORIpv6() throws Exception {
529         final byte[] body = ByteArray.cutBytes(inputBytes.get(6), MessageUtil.COMMON_HEADER_LENGTH);
530         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(6), MessageUtil.MARKER_LENGTH,
531             LENGTH_FIELD_LENGTH));
532         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
533             null);
534
535         final Class<? extends AddressFamily> afi = message.getAttributes().augmentation(Attributes2.class)
536                 .getMpUnreachNlri().getAfi();
537         final Class<? extends SubsequentAddressFamily> safi = message.getAttributes().augmentation(Attributes2.class)
538                 .getMpUnreachNlri().getSafi();
539
540         assertEquals(Ipv6AddressFamily.class, afi);
541         assertEquals(UnicastSubsequentAddressFamily.class, safi);
542
543         final ByteBuf buffer = Unpooled.buffer();
544         BGPParserTest.updateParser.serializeMessage(message, buffer);
545         assertArrayEquals(inputBytes.get(6), ByteArray.readAllBytes(buffer));
546     }
547
548     /*
549      * End of Rib for Ipv6 consists of empty MP_UNREACH_NLRI, with AFI 2 and SAFI 1
550      *
551      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
552      * 00 1e <- length (29) - including header
553      * 02 <- message type
554      * 00 00 <- withdrawn routes length
555      * 00 07 <- total path attribute length
556      * 90 <- attribute flags
557      * 0f <- attribute type (15 - MP_UNREACH_NLRI)
558      * 00 03 <- attribute length
559      * 00 02 <- value (AFI 2: IPv6)
560      * 01 <- value (SAFI 1)
561      */
562     @Test
563     public void testEORIpv6exLength() throws Exception {
564         final byte[] body = ByteArray.cutBytes(inputBytes.get(6), MessageUtil.COMMON_HEADER_LENGTH);
565         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(6), MessageUtil.MARKER_LENGTH,
566             LENGTH_FIELD_LENGTH));
567         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
568             null);
569
570         final Class<? extends AddressFamily> afi = message.getAttributes().augmentation(Attributes2.class)
571                 .getMpUnreachNlri().getAfi();
572         final Class<? extends SubsequentAddressFamily> safi = message.getAttributes().augmentation(Attributes2.class)
573                 .getMpUnreachNlri().getSafi();
574
575         assertEquals(Ipv6AddressFamily.class, afi);
576         assertEquals(UnicastSubsequentAddressFamily.class, safi);
577
578         final ByteBuf buffer = Unpooled.buffer();
579         BGPParserTest.updateParser.serializeMessage(message, buffer);
580         assertArrayEquals(inputBytes.get(6), ByteArray.readAllBytes(buffer));
581     }
582
583     /*
584      * Tests IPv4 NEXT_HOP, ATOMIC_AGGREGATE, COMMUNITY, NLRI with multiple paths.
585      *
586      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
587      * 00 60 <- length (96) - including header
588      * 02 <- message type
589      * 00 00 <- withdrawn routes length
590      * 00 31 <- total path attribute length (49)
591      * 40 <- attribute flags
592      * 01 <- attribute type code (origin)
593      * 01 <- attribute length
594      * 00 <- Origin value (IGP)
595      * 40 <- attribute flags
596      * 02 <- attribute type code (as path)
597      * 06 <- attribute length
598      * 02 <- AS_SEQUENCE
599      * 01 <- path segment count
600      * 00 00 fd ea <- path segment value (65002)
601      * 40 <- attribute flags
602      * 03 <- attribute type code (Next Hop)
603      * 04 <- attribute length
604      * 0a 00 00 02 <- value (10.0.0.2)
605      * 80 <- attribute flags
606      * 04 <- attribute type code (multi exit disc)
607      * 04 <- attribute length
608      * 00 00 00 00 <- value
609      * 40 <- attribute flags
610      * 06 <- attribute type code (atomic aggregate)
611      * 00 <- attribute length
612      * C0 <- attribute flags
613      * 08 <- attribute type code (community)
614      * 10 <- attribute length
615      * FF FF FF 01 <- value (NO_EXPORT)
616      * FF FF FF 02 <- value (NO_ADVERTISE)
617      * FF FF FF 03 <- value (NO_EXPORT_SUBCONFED)
618      * FF FF FF 10 <- unknown Community
619      *
620      * //NLRI
621      * 00 00 00 01 <- path-id (1)
622      * 18 ac 11 02 <- IPv4 Prefix (172.17.1.0 / 24)
623      * 00 00 00 01 <- path-id (2)
624      * 18 ac 11 01 <- IPv4 Prefix (172.17.1.0 / 24)
625      * 00 00 00 01 <- path-id (1)
626      * 18 ac 11 00 <- IPv4 Prefix (172.17.0.0 / 24)
627      */
628     @Test
629     public void testUpdateMessageNlriAddPath() throws Exception {
630         final byte[] body = ByteArray.cutBytes(updatesWithMultiplePath.get(0), MessageUtil.COMMON_HEADER_LENGTH);
631         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(updatesWithMultiplePath.get(0),
632             MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
633         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
634             constraint);
635
636         // check fields
637
638         assertNull(message.getWithdrawnRoutes());
639
640         // attributes
641         final List<AsNumber> asNumbers = new ArrayList<>();
642         asNumbers.add(new AsNumber(65002L));
643         final List<Segments> asPath = Lists.newArrayList();
644         asPath.add(new SegmentsBuilder().setAsSequence(asNumbers).build());
645
646         final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
647                 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.2")).build()).build();
648
649         final List<Communities> comms = Lists.newArrayList();
650         comms.add((Communities) CommunityUtil.NO_EXPORT);
651         comms.add((Communities) CommunityUtil.NO_ADVERTISE);
652         comms.add((Communities) CommunityUtil.NO_EXPORT_SUBCONFED);
653         comms.add((Communities) CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF10));
654
655         final UpdateBuilder builder = new UpdateBuilder();
656
657         // check nlri
658
659         final List<Nlri> nlris = Lists.newArrayList();
660         nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.1.0/24")).setPathId(new PathId(1L)).build());
661         nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.1.0/24")).setPathId(new PathId(2L)).build());
662         nlris.add(new NlriBuilder().setPrefix(new Ipv4Prefix("172.17.0.0/24")).setPathId(new PathId(1L)).build());
663
664         assertEquals(nlris, message.getNlri());
665
666         builder.setNlri(nlris);
667
668         // check path attributes
669
670         final Attributes attrs = message.getAttributes();
671
672         final AttributesBuilder paBuilder = new AttributesBuilder();
673
674         paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
675         assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
676
677         paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
678         assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
679
680         paBuilder.setCNextHop(nextHop);
681         assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
682
683         paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
684         assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
685
686         paBuilder.setAtomicAggregate(new AtomicAggregateBuilder().build());
687         assertEquals(paBuilder.getAtomicAggregate(), attrs.getAtomicAggregate());
688
689         paBuilder.setCommunities(comms);
690         assertEquals(paBuilder.getCommunities(), attrs.getCommunities());
691
692         paBuilder.setUnrecognizedAttributes(Collections.emptyList());
693
694         builder.setAttributes(paBuilder.build());
695
696         assertEquals(builder.build(), message);
697
698         final ByteBuf buffer = Unpooled.buffer();
699         BGPParserTest.updateParser.serializeMessage(message, buffer);
700         assertArrayEquals(updatesWithMultiplePath.get(0), ByteArray.readAllBytes(buffer));
701     }
702
703     /*
704      * Tests withdrawn routes with multiple paths.
705      *
706      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
707      * 00 29 <- length (41) - including header
708      * 02 <- message type
709      * 00 12 <- withdrawn routes length (18)
710      * 00 00 00 01 <- path-id (1)
711      * 1e ac 10 00 04 <- route (172.16.0.4)
712      * 00 00 00 02 <- path-id (2)
713      * 1e ac 10 00 04 <- route (172.16.0.4)
714      * 00 00 <- total path attribute length
715      */
716     @Test
717     public void testUpdateMessageWithdrawAddPath() throws Exception {
718         final byte[] body = ByteArray.cutBytes(updatesWithMultiplePath.get(1), MessageUtil.COMMON_HEADER_LENGTH);
719         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(updatesWithMultiplePath.get(1),
720             MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
721         final Update message = BGPParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
722             constraint);
723
724         // attributes
725         final List<WithdrawnRoutes> withdrawnRoutes = Lists.newArrayList();
726         withdrawnRoutes.add(new WithdrawnRoutesBuilder().setPrefix(new Ipv4Prefix("172.16.0.4/30"))
727             .setPathId(new PathId(1L)).build());
728         withdrawnRoutes.add(new WithdrawnRoutesBuilder().setPrefix(new Ipv4Prefix("172.16.0.4/30"))
729             .setPathId(new PathId(2L)).build());
730
731         // check API message
732         final Update expectedMessage = new UpdateBuilder().setWithdrawnRoutes(withdrawnRoutes).build();
733
734         assertEquals(expectedMessage.getWithdrawnRoutes(), message.getWithdrawnRoutes());
735
736         final ByteBuf buffer = Unpooled.buffer();
737         BGPParserTest.updateParser.serializeMessage(message, buffer);
738         assertArrayEquals(updatesWithMultiplePath.get(1), ByteArray.readAllBytes(buffer));
739     }
740 }