f84eba1741d6df712bffa1b6d0343e1b998e2cb4
[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.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13
14 import java.io.ByteArrayOutputStream;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.net.UnknownHostException;
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.Set;
22
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.opendaylight.protocol.bgp.concepts.ASPath;
26 import org.opendaylight.protocol.bgp.concepts.BGPAddressFamily;
27 import org.opendaylight.protocol.bgp.concepts.BGPAggregator;
28 import org.opendaylight.protocol.bgp.concepts.BGPObject;
29 import org.opendaylight.protocol.bgp.concepts.BGPOrigin;
30 import org.opendaylight.protocol.bgp.concepts.BGPSubsequentAddressFamily;
31 import org.opendaylight.protocol.bgp.concepts.BGPTableType;
32 import org.opendaylight.protocol.bgp.concepts.BaseBGPObjectState;
33 import org.opendaylight.protocol.bgp.concepts.Community;
34 import org.opendaylight.protocol.bgp.concepts.ExtendedCommunity;
35 import org.opendaylight.protocol.bgp.concepts.IPv4NextHop;
36 import org.opendaylight.protocol.bgp.concepts.IPv6NextHop;
37 import org.opendaylight.protocol.bgp.concepts.Inet4SpecificExtendedCommunity;
38 import org.opendaylight.protocol.bgp.linkstate.AreaIdentifier;
39 import org.opendaylight.protocol.bgp.linkstate.DomainIdentifier;
40 import org.opendaylight.protocol.bgp.linkstate.IPv4InterfaceIdentifier;
41 import org.opendaylight.protocol.bgp.linkstate.ISISAreaIdentifier;
42 import org.opendaylight.protocol.bgp.linkstate.LinkAnchor;
43 import org.opendaylight.protocol.bgp.linkstate.LinkIdentifier;
44 import org.opendaylight.protocol.bgp.linkstate.LinkProtectionType;
45 import org.opendaylight.protocol.bgp.linkstate.NetworkLinkState;
46 import org.opendaylight.protocol.bgp.linkstate.NetworkNodeState;
47 import org.opendaylight.protocol.bgp.linkstate.NetworkObjectState;
48 import org.opendaylight.protocol.bgp.linkstate.NetworkRouteState;
49 import org.opendaylight.protocol.bgp.linkstate.NodeIdentifier;
50 import org.opendaylight.protocol.bgp.linkstate.NodeIdentifierFactory;
51 import org.opendaylight.protocol.bgp.linkstate.OSPFInterfaceIdentifier;
52 import org.opendaylight.protocol.bgp.linkstate.OSPFRouterIdentifier;
53 import org.opendaylight.protocol.bgp.linkstate.OSPFv3LANIdentifier;
54 import org.opendaylight.protocol.bgp.linkstate.RouterIdentifier;
55 import org.opendaylight.protocol.bgp.linkstate.TopologyIdentifier;
56 import org.opendaylight.protocol.bgp.parser.BGPLink;
57 import org.opendaylight.protocol.bgp.parser.BGPNode;
58 import org.opendaylight.protocol.bgp.parser.BGPParameter;
59 import org.opendaylight.protocol.bgp.parser.BGPRoute;
60 import org.opendaylight.protocol.bgp.parser.BGPUpdateEvent;
61 import org.opendaylight.protocol.bgp.parser.BGPUpdateMessage;
62 import org.opendaylight.protocol.bgp.parser.BGPUpdateSynchronized;
63 import org.opendaylight.protocol.bgp.parser.impl.PathAttribute.TypeCode;
64 import org.opendaylight.protocol.bgp.parser.impl.message.BGPUpdateMessageParser;
65 import org.opendaylight.protocol.bgp.parser.message.BGPOpenMessage;
66 import org.opendaylight.protocol.bgp.parser.parameter.MultiprotocolCapability;
67 import org.opendaylight.protocol.bgp.util.BGPIPv4RouteImpl;
68 import org.opendaylight.protocol.bgp.util.BGPIPv6RouteImpl;
69 import org.opendaylight.protocol.bgp.util.BGPLinkImpl;
70 import org.opendaylight.protocol.bgp.util.BGPNodeImpl;
71 import org.opendaylight.protocol.concepts.ASNumber;
72 import org.opendaylight.protocol.concepts.IGPMetric;
73 import org.opendaylight.protocol.concepts.IPv4;
74 import org.opendaylight.protocol.concepts.IPv4Address;
75 import org.opendaylight.protocol.concepts.IPv4Prefix;
76 import org.opendaylight.protocol.concepts.IPv6;
77 import org.opendaylight.protocol.concepts.IPv6Address;
78 import org.opendaylight.protocol.concepts.Identifier;
79 import org.opendaylight.protocol.concepts.Metric;
80 import org.opendaylight.protocol.util.ByteArray;
81 import org.opendaylight.protocol.util.DefaultingTypesafeContainer;
82
83 import com.google.common.collect.Lists;
84 import com.google.common.collect.Sets;
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 = 17;
94
95         private static int MAX_SIZE = 300;
96
97         @BeforeClass
98         public static void setUp() throws Exception {
99
100                 for (int i = 1; i <= COUNTER; i++) {
101                         final String name = "/up" + i + ".bin";
102                         final InputStream is = BGPParserTest.class.getResourceAsStream(name);
103                         if (is == null)
104                                 throw new IOException("Failed to get resource " + name);
105
106                         final ByteArrayOutputStream bis = new ByteArrayOutputStream();
107                         final byte[] data = new byte[MAX_SIZE];
108                         int nRead = 0;
109                         while ((nRead = is.read(data, 0, data.length)) != -1) {
110                                 bis.write(data, 0, nRead);
111                         }
112                         bis.flush();
113
114                         inputBytes.add(bis.toByteArray());
115                 }
116         }
117
118         @Test
119         public void testResource() {
120                 assertNotNull(inputBytes);
121         }
122
123         /*
124          * Tests IPv4 NEXT_HOP, ATOMIC_AGGREGATE, COMMUNITY, NLRI
125          * 
126          * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
127          * 00 54 <- length (84) - including header
128          * 02 <- message type
129          * 00 00 <- withdrawn routes length
130          * 00 31 <- total path attribute length (49)
131          * 40 <- attribute flags
132          * 01 <- attribute type code (origin)
133          * 01 <- attribute length
134          * 00 <- Origin value (IGP)
135          * 40 <- attribute flags
136          * 02 <- attribute type code (as path)
137          * 06 <- attribute length
138          * 02 <- AS_SEQUENCE
139          * 01 <- path segment count
140          * 00 00 fd ea <- path segment value (65002)
141          * 40 <- attribute flags
142          * 03 <- attribute type code (Next Hop)
143          * 04 <- attribute length
144          * 10 00 00 02 <- value (10.0.0.2)
145          * 80 <- attribute flags
146          * 04 <- attribute type code (multi exit disc)
147          * 04 <- attribute length
148          * 00 00 00 00 <- value
149          * 64 <- attribute flags
150          * 06 <- attribute type code (atomic aggregate)
151          * 00 <- attribute length
152          * 64 <- attribute flags
153          * 08 <- attribute type code (community)
154          * 10 <- attribute length FF FF FF
155          * 01 <- value (NO_EXPORT)
156          * FF FF FF 02 <- value (NO_ADVERTISE)
157          * FF FF FF 03 <- value (NO_EXPORT_SUBCONFED)
158          * FF FF FF 10 <- unknown Community
159          * 
160          * //NLRI
161          * 18 ac 11 02 <- IPv4 Prefix (172.17.2.0 / 24)
162          * 18 ac 11 01 <- IPv4 Prefix (172.17.1.0 / 24)
163          * 18 ac 11 00 <- IPv4 Prefix (172.17.0.0 / 24)
164          */
165         @Test
166         public void testGetUpdateMessage1() throws Exception {
167
168                 final byte[] body = ByteArray.cutBytes(inputBytes.get(0), BGPMessageFactory.COMMON_HEADER_LENGTH);
169                 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(0), BGPMessageFactory.MARKER_LENGTH,
170                                 BGPMessageFactory.LENGTH_FIELD_LENGTH));
171                 final BGPUpdateEvent ret = BGPUpdateMessageParser.parse(body, messageLength);
172
173                 assertTrue(ret instanceof BGPUpdateMessage);
174                 final BGPUpdateMessage message = (BGPUpdateMessage) ret;
175
176                 // check fields
177
178                 assertEquals(Collections.EMPTY_SET, message.getRemovedObjects());
179
180                 // attributes
181
182                 final ASPath asPath = new ASPath(Lists.newArrayList(new ASNumber(65002)));
183
184                 final IPv4NextHop nextHop = IPv4NextHop.forString("10.0.0.2");
185
186                 final Set<Community> comms = Sets.newHashSet(Community.NO_EXPORT, Community.NO_ADVERTISE, Community.NO_EXPORT_SUBCONFED,
187                                 new Community(new ASNumber(0xFFFF), 0xFF10));
188
189                 // check path attributes
190
191                 // final PathAttribute originAttr = new PathAttribute(TypeCode.ORIGIN, false,
192                 // true, false, false, BGPOrigin.IGP);
193                 // assertEquals(originAttr, attrs.get(0));
194                 //
195                 // final PathAttribute asPathAttr = new PathAttribute(TypeCode.AS_PATH, false,
196                 // true, false, false, asPath);
197                 // assertEquals(asPathAttr, attrs.get(1));
198                 //
199                 // final PathAttribute nextHopAttr = new PathAttribute(TypeCode.NEXT_HOP, false,
200                 // true, false, false, nextHop);
201                 // assertEquals(nextHopAttr, attrs.get(2));
202                 //
203                 // final PathAttribute multiExitDisc = new PathAttribute(
204                 // TypeCode.MULTI_EXIT_DISC, true, false, false, false, 0);
205                 // assertEquals(multiExitDisc, attrs.get(3));
206                 //
207                 // final PathAttribute atomic = new PathAttribute(TypeCode.ATOMIC_AGGREGATE, false,
208                 // true, true, false, null);
209                 // assertEquals(atomic, attrs.get(4));
210                 //
211                 // final PathAttribute comm = new PathAttribute(TypeCode.COMMUNITIES, false,
212                 // true, true, false, comms);
213                 // assertEquals(comm, attrs.get(5));
214
215                 // check nlri
216
217                 // final Set<IPv4Prefix> nlri = Sets.newHashSet(pref1, pref2, pref3);
218                 // assertEquals(nlri, ret.getBgpUpdateMessageBuilder().getNlri());
219
220                 final BaseBGPObjectState state = new BaseBGPObjectState(BGPOrigin.IGP, null);
221                 final NetworkRouteState<IPv4Address> routeState = new NetworkRouteState<>(new NetworkObjectState(asPath, comms, Collections.<ExtendedCommunity> emptySet()), nextHop);
222
223                 // check API message
224
225                 final Set<BGPObject> addedObjects = Sets.newHashSet();
226
227                 final BGPRoute<IPv4Address> route1 = new BGPIPv4RouteImpl(IPv4.FAMILY.prefixForString("172.17.2.0/24"), state, routeState);
228
229                 addedObjects.add(route1);
230
231                 final BGPRoute<IPv4Address> route2 = new BGPIPv4RouteImpl(IPv4.FAMILY.prefixForString("172.17.1.0/24"), state, routeState);
232
233                 addedObjects.add(route2);
234
235                 final BGPRoute<IPv4Address> route3 = new BGPIPv4RouteImpl(IPv4.FAMILY.prefixForString("172.17.0.0/24"), state, routeState);
236
237                 addedObjects.add(route3);
238
239                 final BGPUpdateMessage expectedMessage = new BGPUpdateMessageImpl(addedObjects, Collections.<Identifier> emptySet());
240
241                 assertEquals(expectedMessage, message);
242
243         }
244
245         /*
246          * Tests IPv6 NEXT_HOP, NLRI, ORIGIN.IGP, MULTI_EXIT_DISC, ORIGINATOR-ID, CLUSTER_LIST.
247          * 
248          * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
249          * 00 80 <- length (128) - including header
250          * 02 <- message type
251          * 00 00 <- withdrawn routes length
252          * 00 69 <- total path attribute length (105)
253          * 40 <- attribute flags
254          * 01 <- attribute type code (origin)
255          * 01 <- attribute length
256          * 00 <- Origin value (IGP)
257          * 40 <- attribute flags
258          * 02 <- attribute type code (as path)
259          * 06 <- attribute length
260          * 02 <- AS_SEQUENCE
261          * 01 <- path segment count
262          * 00 00 fd e9 <- path segment value (65001)
263          * 80 <- attribute flags
264          * 04 <- attribute type code (multi exit disc)
265          * 04 <- attribute length
266          * 00 00 00 00 <- value
267          * 80 <- attribute flags
268          * 09 <- attribute type code (originator id)
269          * 04 <- attribute length
270          * 7f 00 00 01 <- value (localhost ip)
271          * 80 <- attribute flags
272          * 0a <- attribute type code (cluster list)
273          * 08 <- attribute length
274          * 01 02 03 04 <- value
275          * 05 06 07 08 <- value
276          * 80 <- attribute flags
277          * 0e <- attribute type code (mp reach nlri)
278          * 40 <- attribute length
279          * 00 02 <- AFI (Ipv6)
280          * 01 <- SAFI (Unicast)
281          * 20 <- length of next hop
282          * 20 01 0d b8 00 00 00 00 00 00 00 00 00 00 00 01 <- global
283          * fe 80 00 00 00 00 00 00 c0 01 0b ff fe 7e 00 <- link local
284          * 00 <- reserved
285          * 
286          * //NLRI
287          * 40 20 01 0d b8 00 01 00 02 <- IPv6 Prefix (2001:db8:1:2:: / 64)
288          * 40 20 01 0d b8 00 01 00 01 <- IPv6 Prefix (2001:db8:1:1:: / 64)
289          * 40 20 01 0d b8 00 01 00 00 <- IPv6 Prefix (2001:db8:1:: / 64)
290          * 
291          */
292         @Test
293         public void testGetUpdateMessage2() throws Exception {
294                 final byte[] body = ByteArray.cutBytes(inputBytes.get(1), BGPMessageFactory.COMMON_HEADER_LENGTH);
295                 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(1), BGPMessageFactory.MARKER_LENGTH,
296                                 BGPMessageFactory.LENGTH_FIELD_LENGTH));
297                 final BGPUpdateEvent ret = BGPUpdateMessageParser.parse(body, messageLength);
298
299                 assertTrue(ret instanceof BGPUpdateMessage);
300                 final BGPUpdateMessage message = (BGPUpdateMessage) ret;
301
302                 // check fields
303
304                 assertEquals(Collections.EMPTY_SET, message.getRemovedObjects());
305
306                 // attributes
307
308                 final ASPath asPath = new ASPath(Lists.newArrayList(new ASNumber(65001)));
309
310                 final IPv6NextHop nextHop = IPv6NextHop.forString("2001:db8::1", "fe80::c001:bff:fe7e:0");
311
312                 // final List<ClusterIdentifier> clusters = Lists.newArrayList(
313                 // new ClusterIdentifier(new byte[] { 1, 2, 3, 4}),
314                 // new ClusterIdentifier(new byte[] { 5, 6, 7, 8}));
315
316                 // check path attributes
317
318                 // final PathAttribute originAttr = new PathAttribute(TypeCode.ORIGIN, false,
319                 // true, false, false, BGPOrigin.IGP);
320                 // assertEquals(originAttr, attrs.get(0));
321                 //
322                 // final PathAttribute asPathAttr = new PathAttribute(TypeCode.AS_PATH, false,
323                 // true, false, false, asPath);
324                 // assertEquals(asPathAttr, attrs.get(1));
325                 //
326                 // final PathAttribute multiExitDisc = new PathAttribute(
327                 // TypeCode.MULTI_EXIT_DISC, true, false, false, false, 0);
328                 // assertEquals(multiExitDisc, attrs.get(2));
329                 //
330                 // final PathAttribute originatorAttr = new PathAttribute(
331                 // TypeCode.ORIGINATOR_ID, true, false, false, false, IPv4.FAMILY.addressForString("127.0.0.1"));
332                 // assertEquals(originatorAttr, attrs.get(3));
333                 //
334                 // final PathAttribute clusterAttr = new PathAttribute(
335                 // TypeCode.CLUSTER_LIST, true, false, false, false, clusters);
336                 // assertEquals(clusterAttr, attrs.get(4));
337
338                 final BaseBGPObjectState state = new BaseBGPObjectState(BGPOrigin.IGP, null);
339                 final NetworkRouteState<IPv6Address> routeState = new NetworkRouteState<>(new NetworkObjectState(asPath, Collections.<Community> emptySet(), Collections.<ExtendedCommunity> emptySet()), nextHop);
340
341                 // check API message
342
343                 final Set<BGPObject> addedObjects = Sets.newHashSet();
344
345                 final BGPRoute<IPv6Address> route1 = new BGPIPv6RouteImpl(IPv6.FAMILY.prefixForString("2001:db8:1:2::/64"), state, routeState);
346
347                 addedObjects.add(route1);
348
349                 final BGPRoute<IPv6Address> route2 = new BGPIPv6RouteImpl(IPv6.FAMILY.prefixForString("2001:db8:1:1::/64"), state, routeState);
350
351                 addedObjects.add(route2);
352
353                 final BGPRoute<IPv6Address> route3 = new BGPIPv6RouteImpl(IPv6.FAMILY.prefixForString("2001:db8:1::/64"), state, routeState);
354
355                 addedObjects.add(route3);
356
357                 final BGPUpdateMessage expectedMessage = new BGPUpdateMessageImpl(addedObjects, Collections.<Identifier> emptySet());
358
359                 assertEquals(expectedMessage, message);
360         }
361
362         /*
363          * Tests more AS Numbers in AS_PATH, AGGREGATOR, ORIGIN.INCOMPLETE
364          * 
365          * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
366          * 00 4b <- length (75) - including header
367          * 02 <- message type
368          * 00 00 <- withdrawn routes length
369          * 00 30 <- total path attribute length (48)
370          * 40 <- attribute flags
371          * 01 <- attribute type code (origin)
372          * 01 <- attribute length
373          * 02 <- Origin value (Incomplete)
374          * 40 <- attribute flags
375          * 02 <- attribute type code (as path)
376          * 10 <- attribute length
377          * 02 <- AS_SEQUENCE
378          * 01 <- path segment count
379          * 00 00 00 1e <- path segment value (30)
380          * 01 <- AS_SET
381          * 02 <- path segment count
382          * 00 00 00 0a <- path segment value (10)
383          * 00 00 00 14 <- path segment value (20)
384          * 40 <- attribute flags
385          * 03 <- attribute type (Next hop)
386          * 04 <- attribute length
387          * 0a 00 00 09 <- value (10.0.0.9)
388          * 80 <- attribute flags
389          * 04 <- attribute type code (multi exit disc)
390          * 04 <- attribute length
391          * 00 00 00 00 <- value
392          * c0 <- attribute flags
393          * 07 <- attribute type (Aggregator)
394          * 08 <- attribute length
395          * 00 00 00 1e <- value (AS number = 30)
396          * 0a 00 00 09 <- value (IP address = 10.0.0.9)
397          * 
398          * //NLRI
399          * 15 ac 10 00 <- IPv4 Prefix (172.16.0.0 / 21)
400          */
401         @Test
402         public void testGetUpdateMessage3() throws Exception {
403                 final byte[] body = ByteArray.cutBytes(inputBytes.get(2), BGPMessageFactory.COMMON_HEADER_LENGTH);
404                 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(2), BGPMessageFactory.MARKER_LENGTH,
405                                 BGPMessageFactory.LENGTH_FIELD_LENGTH));
406                 final BGPUpdateEvent ret = BGPUpdateMessageParser.parse(body, messageLength);
407                 assertTrue(ret instanceof BGPUpdateMessage);
408                 final BGPUpdateMessage message = (BGPUpdateMessage) ret;
409
410                 // check fields
411                 assertEquals(Collections.EMPTY_SET, message.getRemovedObjects());
412
413                 // attributes
414
415                 final ASPath asPath = new ASPath(Lists.newArrayList(new ASNumber(30)), Sets.newHashSet(new ASNumber(10), new ASNumber(20)));
416
417                 final BGPAggregator aggregator = new BGPAggregatorImpl<IPv4Address>(new ASNumber(30), IPv4.FAMILY.addressForString("10.0.0.9"));
418                 final IPv4NextHop nextHop = IPv4NextHop.forString("10.0.0.9");
419
420                 final IPv4Prefix pref1 = IPv4.FAMILY.prefixForString("172.16.0.0/21");
421
422                 // check path attributes
423
424                 // final PathAttribute originAttr = new PathAttribute(TypeCode.ORIGIN, false,
425                 // true, false, false, BGPOrigin.INCOMPLETE);
426                 // assertEquals(originAttr, attrs.get(0));
427                 //
428                 // final PathAttribute asPathAttr = new PathAttribute(TypeCode.AS_PATH, false,
429                 // true, false, false, asPath);
430                 // assertEquals(asPathAttr, attrs.get(1));
431                 //
432                 // final PathAttribute nextHopAttr = new PathAttribute(TypeCode.NEXT_HOP, false,
433                 // true, false, false, nextHop);
434                 // assertEquals(nextHopAttr, attrs.get(2));
435                 //
436                 // final PathAttribute multiExitDisc = new PathAttribute(
437                 // TypeCode.MULTI_EXIT_DISC, true, false, false, false, 0);
438                 // assertEquals(multiExitDisc, attrs.get(3));
439                 //
440                 // final PathAttribute agg = new PathAttribute(TypeCode.AGGREGATOR, true, true,
441                 // false, false, aggregator);
442                 // assertEquals(agg, attrs.get(4));
443                 //
444                 // // check nlri
445                 //
446                 // final Set<IPv4Prefix> nlri = Sets.newHashSet(pref1);
447                 // assertEquals(nlri, ret.getBgpUpdateMessageBuilder().getNlri());
448
449                 final BaseBGPObjectState state = new BaseBGPObjectState(BGPOrigin.INCOMPLETE, aggregator);
450                 final NetworkRouteState<IPv4Address> routeState = new NetworkRouteState<>(new NetworkObjectState(asPath, Collections.<Community> emptySet(), Collections.<ExtendedCommunity> emptySet()), nextHop);
451
452                 // check API message
453
454                 final Set<BGPObject> addedObjects = Sets.newHashSet();
455
456                 final BGPRoute<IPv4Address> route1 = new BGPIPv4RouteImpl(pref1, state, routeState);
457
458                 addedObjects.add(route1);
459
460                 final BGPUpdateMessage expectedMessage = new BGPUpdateMessageImpl(addedObjects, Collections.<Identifier> emptySet());
461
462                 assertEquals(expectedMessage, message);
463
464         }
465
466         /*
467          * Tests empty AS_PATH, ORIGIN.EGP, LOCAL_PREF, EXTENDED_COMMUNITIES (Ipv4 Addr specific)
468          * 
469          * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
470          * 00 4A <- length (73) - including header
471          * 02 <- message type
472          * 00 00 <- withdrawn routes length
473          * 00 27 <- total path attribute length (39)
474          * 40 <- attribute flags
475          * 01 <- attribute type code (Origin)
476          * 01 <- attribute length
477          * 01 <- Origin value (EGP)
478          * 40 <- attribute flags
479          * 02 <- attribute type code (As path)
480          * 00 <- attribute length
481          * 40 <- attribute flags
482          * 03 <- attribute type (Next hop)
483          * 04 <- attribute length
484          * 03 03 03 03 <- value (3.3.3.3)
485          * 80 <- attribute flags
486          * 04 <- attribute type code (Multi exit disc)
487          * 04 <- attribute length
488          * 00 00 00 00 <- value
489          * 40 <- attribute flags
490          * 05 <- attribute type (Local Pref)
491          * 04 <- attribute length
492          * 00 00 00 64 <- value (100)
493          * 80 <- attribute flags
494          * 10 <- attribute type (extended community)
495          * 08 <- attribute length
496          * 01 04 <- value (type - Ipv4 Address Specific Extended Community)
497          * c0 a8 01 00 <- value (global adm. 198.162.1.0)
498          * 12 34 <- value (local adm. 4660)
499          * 
500          * //NLRI
501          * 18 0a 1e 03 <- IPv4 Prefix (10.30.3.0 / 24)
502          * 18 0a 1e 02 <- IPv4 Prefix (10.30.2.0 / 24)
503          * 18 0a 1e 01 <- IPv4 Prefix (10.30.1.0 / 24)
504          */
505         @Test
506         public void testGetUpdateMessage4() throws Exception {
507                 final byte[] body = ByteArray.cutBytes(inputBytes.get(3), BGPMessageFactory.COMMON_HEADER_LENGTH);
508                 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(3), BGPMessageFactory.MARKER_LENGTH,
509                                 BGPMessageFactory.LENGTH_FIELD_LENGTH));
510                 final BGPUpdateEvent ret = BGPUpdateMessageParser.parse(body, messageLength);
511
512                 assertTrue(ret instanceof BGPUpdateMessage);
513                 final BGPUpdateMessage message = (BGPUpdateMessage) ret;
514
515                 // check fields
516
517                 assertEquals(Collections.EMPTY_SET, message.getRemovedObjects());
518
519                 // attributes
520
521                 final IPv4NextHop nextHop = IPv4NextHop.forString("3.3.3.3");
522
523                 final Set<ExtendedCommunity> comms = Sets.newHashSet((ExtendedCommunity) new Inet4SpecificExtendedCommunity(false, 4, IPv4.FAMILY.addressForString("192.168.1.0"), new byte[] {
524                                 0x12, 0x34 }));
525
526                 // check path attributes
527
528                 // final PathAttribute originAttr = new PathAttribute(TypeCode.ORIGIN, false,
529                 // true, false, false, BGPOrigin.EGP);
530                 // assertEquals(originAttr, attrs.get(0));
531                 //
532                 // final PathAttribute asPathAttr = new PathAttribute(TypeCode.AS_PATH, false,
533                 // true, false, false, asPath);
534                 // assertEquals(asPathAttr, attrs.get(1));
535                 //
536                 // final PathAttribute nextHopAttr = new PathAttribute(TypeCode.NEXT_HOP, false,
537                 // true, false, false, nextHop);
538                 // assertEquals(nextHopAttr, attrs.get(2));
539                 //
540                 // final PathAttribute multiExitDisc = new PathAttribute(
541                 // TypeCode.MULTI_EXIT_DISC, true, false, false, false, 0);
542                 // assertEquals(multiExitDisc, attrs.get(3));
543                 //
544                 // final PathAttribute localPref = new PathAttribute(TypeCode.LOCAL_PREF, false,
545                 // true, false, false, 100);
546                 // assertEquals(localPref, attrs.get(4));
547
548                 // check nlri
549                 //
550                 // final Set<IPv4Prefix> nlri = Sets.newHashSet(pref1, pref2, pref3);
551                 // assertEquals(nlri, ret.getBgpUpdateMessageBuilder().getNlri());
552
553                 final BaseBGPObjectState state = new BaseBGPObjectState(BGPOrigin.EGP, null);
554                 final NetworkRouteState<IPv4Address> routeState = new NetworkRouteState<>(new NetworkObjectState(ASPath.EMPTY, Collections.<Community> emptySet(), comms), nextHop);
555
556                 // check API message
557
558                 final Set<BGPObject> addedObjects = Sets.newHashSet();
559
560                 final BGPRoute<IPv4Address> route1 = new BGPIPv4RouteImpl(IPv4.FAMILY.prefixForString("10.30.3.0/24"), state, routeState);
561
562                 addedObjects.add(route1);
563
564                 final BGPRoute<IPv4Address> route2 = new BGPIPv4RouteImpl(IPv4.FAMILY.prefixForString("10.30.2.0/24"), state, routeState);
565
566                 addedObjects.add(route2);
567
568                 final BGPRoute<IPv4Address> route3 = new BGPIPv4RouteImpl(IPv4.FAMILY.prefixForString("10.30.1.0/24"), state, routeState);
569
570                 addedObjects.add(route3);
571
572                 final BGPUpdateMessage expectedMessage = new BGPUpdateMessageImpl(addedObjects, Collections.<Identifier> emptySet());
573
574                 assertEquals(expectedMessage, message);
575         }
576
577         /*
578          * Tests withdrawn routes.
579          * 
580          * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
581          * 00 1c <- length (28) - including header
582          * 02 <- message type
583          * 00 05 <- withdrawn routes length (5)
584          * 1e ac 10 00 04 <- route (172.16.0.4)
585          * 00 00 <- total path attribute length
586          */
587         @Test
588         public void testGetUpdateMessage5() throws Exception {
589                 final byte[] body = ByteArray.cutBytes(inputBytes.get(4), BGPMessageFactory.COMMON_HEADER_LENGTH);
590                 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(4), BGPMessageFactory.MARKER_LENGTH,
591                                 BGPMessageFactory.LENGTH_FIELD_LENGTH));
592                 final BGPUpdateEvent ret = BGPUpdateMessageParser.parse(body, messageLength);
593
594                 assertTrue(ret instanceof BGPUpdateMessage);
595                 final BGPUpdateMessage message = (BGPUpdateMessage) ret;
596
597                 // attributes
598
599                 final IPv4Prefix pref1 = IPv4.FAMILY.prefixForString("172.16.0.4/30");
600
601                 // check API message
602
603                 final BGPUpdateEvent expectedMessage = new BGPUpdateMessageImpl(Collections.<BGPObject> emptySet(), Sets.newHashSet((Identifier) pref1));
604
605                 assertEquals(expectedMessage, message);
606         }
607
608         /*
609          * Test EOR for IPv4.
610          * 
611          * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
612          * 00 17 <- length (23) - including header
613          * 02 <- message type
614          * 00 00 <- withdrawn routes length
615          * 00 00 <- total path attribute length
616          */
617         @Test
618         public void testEORIpv4() throws Exception {
619                 final byte[] body = ByteArray.cutBytes(inputBytes.get(5), BGPMessageFactory.COMMON_HEADER_LENGTH);
620                 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(5), BGPMessageFactory.MARKER_LENGTH,
621                                 BGPMessageFactory.LENGTH_FIELD_LENGTH));
622                 final BGPUpdateEvent ret = BGPUpdateMessageParser.parse(body, messageLength);
623
624                 assertTrue(ret instanceof BGPUpdateSynchronized);
625                 final BGPUpdateSynchronized message = (BGPUpdateSynchronized) ret;
626
627                 final BGPUpdateSynchronized expectedMessage = new BGPUpdateSynchronized() {
628
629                         private static final long serialVersionUID = -5128220996581568885L;
630
631                         @Override
632                         public BGPTableType getTableType() {
633                                 return new BGPTableType(BGPAddressFamily.IPv4, BGPSubsequentAddressFamily.Unicast);
634                         }
635                 };
636                 assertEquals(expectedMessage.getTableType(), message.getTableType());
637         }
638
639         /*
640          * End of Rib for Ipv6 consists of empty MP_UNREACH_NLRI, with AFI 2 and SAFI 1
641          * 
642          * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
643          * 00 1d <- length (29) - including header
644          * 02 <- message type
645          * 00 00 <- withdrawn routes length
646          * 00 06 <- total path attribute length
647          * 80 <- attribute flags
648          * 0f <- attribute type (15 - MP_UNREACH_NLRI)
649          * 03 <- attribute length
650          * 00 02 <- value (AFI 2: IPv6)
651          * 01 <- value (SAFI 1)
652          */
653         @Test
654         public void testEORIpv6() throws Exception {
655                 final byte[] body = ByteArray.cutBytes(inputBytes.get(6), BGPMessageFactory.COMMON_HEADER_LENGTH);
656                 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(6), BGPMessageFactory.MARKER_LENGTH,
657                                 BGPMessageFactory.LENGTH_FIELD_LENGTH));
658                 final BGPUpdateEvent ret = BGPUpdateMessageParser.parse(body, messageLength);
659
660                 assertTrue(ret instanceof BGPUpdateSynchronized);
661                 final BGPUpdateSynchronized message = (BGPUpdateSynchronized) ret;
662
663                 // check fields
664
665                 final BGPUpdateSynchronized expectedMessage = new BGPUpdateSynchronized() {
666
667                         private static final long serialVersionUID = -4811827044855997014L;
668
669                         @Override
670                         public BGPTableType getTableType() {
671                                 return new BGPTableType(BGPAddressFamily.IPv6, BGPSubsequentAddressFamily.Unicast);
672                         }
673                 };
674                 assertEquals(expectedMessage.getTableType(), message.getTableType());
675         }
676
677         /*
678          * End of Rib for LS consists of empty MP_UNREACH_NLRI, with AFI 16388 and SAFI 71
679          * 
680          * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
681          * 00 1d <- length (29) - including header
682          * 02 <- message type
683          * 00 00 <- withdrawn routes length
684          * 00 06 <- total path attribute length
685          * 80 <- attribute flags
686          * 0f <- attribute type (15 - MP_UNREACH_NLRI)
687          * 03 <- attribute length
688          * 40 04 <- value (AFI 16388: LS)
689          * 47 <- value (SAFI 71)
690          */
691         @Test
692         public void testEORLS() throws Exception {
693                 final byte[] body = ByteArray.cutBytes(inputBytes.get(7), BGPMessageFactory.COMMON_HEADER_LENGTH);
694                 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(7), BGPMessageFactory.MARKER_LENGTH,
695                                 BGPMessageFactory.LENGTH_FIELD_LENGTH));
696                 final BGPUpdateEvent ret = BGPUpdateMessageParser.parse(body, messageLength);
697
698                 assertTrue(ret instanceof BGPUpdateSynchronized);
699                 final BGPUpdateSynchronized message = (BGPUpdateSynchronized) ret;
700
701                 // check fields
702
703                 final BGPUpdateSynchronized expectedMessage = new BGPUpdateSynchronized() {
704
705                         private static final long serialVersionUID = 3677704232769769042L;
706
707                         @Override
708                         public BGPTableType getTableType() {
709                                 return new BGPTableType(BGPAddressFamily.LinkState, BGPSubsequentAddressFamily.Unicast);
710                         }
711                 };
712
713                 assertEquals(expectedMessage.getTableType(), message.getTableType());
714         }
715
716         /*
717          *  Tests BGP Link Ipv4
718          * 
719                 00 00 <- withdrawn routes length
720                 01 48 <- total path attribute length (328)
721                 90 <- attribute flags
722                 0e <- attribute type code (MP reach)
723                 01 2c <- attribute extended length (300)
724                 40 04 <- AFI (16388 - Linkstate)
725                 47 <- SAFI (71 - Linkstate)
726                 04 <- next hop length
727                 19 19 19 01 <- nexthop (25.25.25.1)
728                 00 <- reserved
729
730                 00 02 <- NLRI type (2 - linkNLRI)
731                 00 5d <- NLRI length (93)
732                 03 <- ProtocolID - OSPF
733                 00 00 00 00 00 00 00 01 <- identifier
734
735                 01 00 <- local node descriptor type (256)
736                 00 24 <- length (36)
737                 02 00 <- node descriptor type (member AS - 512)
738                 00 04 <- length
739                 00 00 00 64 <- value (100)
740                 02 01 <- node descriptor type (bgpId - 513)
741                 00 04 <- length
742                 19 19 19 01 <- bgpId (25.25.25.1)
743                 02 02 <- node descriptor type (areaId - 514)
744                 00 04 <- length
745                 00 00 00 00 <- value
746                 02 03 <- node descriptor type (routeId - 515)
747                 00 08 <- length
748                 03 03 03 04 0b 0b 0b 03 <- OSPF Router Id
749
750                 01 01 <- remote node descriptor type (257)
751                 00 20 <- length (32)
752                 02 00 <- node descriptor type (member AS - 512)
753                 00 04 <- length
754                 00 00 00 64 <- value (100)
755                 02 01 <- node descriptor type (bgpId - 513)
756                 00 04 <- length
757                 19 19 19 01 <- bgpId (25.25.25.1)
758                 02 02 <- node descriptor type (areaId - 514)
759                 00 04 <- length
760                 00 00 00 00 <- value
761                 02 03 <- node descriptor type (routeId - 515)
762                 00 04 <- length
763                 03 03 03 04 <- OSPF Router Id
764
765                 01 03 <- link descriptor type (IPv4 interface address - 259)
766                 00 04 <- length (4)
767                 0b 0b 0b 03 <- value (11.11.11.3)
768
769                 00 02 <- NLRI type (2 - linkNLRI)
770                 00 5d <- NLRI length (93)
771                 03 <- ProtocolID - OSPF
772                 00 00 00 00 00 00 00 01 <- identifier
773
774                 01 00 <- local node descriptor type (256)
775                 00 24 <- length (36)
776                 02 00 <- node descriptor type (member AS - 512)
777                 00 04 <- length
778                 00 00 00 64 <- value (100)
779                 02 01 <- node descriptor type (bgpId - 513)
780                 00 04 <- length
781                 19 19 19 01 <- bgpId (25.25.25.1)
782                 02 02 <- node descriptor type (areaId - 514)
783                 00 04 <- length
784                 00 00 00 00 <- value
785                 02 03 <- node descriptor type (routeId - 515)
786                 00 08 <- length
787                 03 03 03 04 0b 0b 0b 03 <- OSPF Router Id
788
789                 01 01 <- remote node descriptor type (257)
790                 00 20 <- length (32)
791                 02 00 <- node descriptor type (member AS - 512)
792                 00 04 <- length
793                 00 00 00 64 <- value (100)
794                 02 01 <- node descriptor type (bgpId - 513)
795                 00 04 <- length
796                 19 19 19 01 <- bgpId (25.25.25.1)
797                 02 02 <- node descriptor type (areaId - 514)
798                 00 04 <- length
799                 00 00 00 00 <- value
800                 02 03 <- node descriptor type (routeId - 515)
801                 00 04 <- length
802                 01 01 01 02 <- OSPF Router Id
803
804                 01 03 <- link descriptor type (IPv4 interface address - 259)
805                 00 04 <- length
806                 0b 0b 0b 01 <- value (11.11.11.1)
807
808                 00 02 <- NLRI type (2 - linkNLRI)
809                 00 5d <- NLRI length (93)
810                 03 <- ProtocolID - OSPF
811                 00 00 00 00 00 00 00 01 <- identifier
812
813                 01 00 <- local node descriptor type (256)
814                 00 20 <- length (32)
815                 02 00 <- node descriptor type (member AS - 512)
816                 00 04 <- length
817                 00 00 00 64 <- value (100)
818                 02 01 <- node descriptor type (bgpId - 513)
819                 00 04 <- length
820                 19 19 19 01 <- bgpId (25.25.25.1)
821                 02 02 <- node descriptor type (areaId - 514)
822                 00 04 <- length
823                 00 00 00 00 <- value
824                 02 03 <- node descriptor type (routeId - 515)
825                 00 04 <- length
826                 01 01 01 02 <- OSPF Router Id
827
828                 01 01 <- remote node descriptor type (257)
829                 00 24 <- length (36)
830                 02 00 <- node descriptor type (member AS - 512)
831                 00 04 <- length
832                 00 00 00 64 <- value (100)
833                 02 01 <- node descriptor type (bgpId - 513)
834                 00 04 <- length
835                 19 19 19 01 <- bgpId (25.25.25.1)
836                 02 02 <- node descriptor type (areaId - 514)
837                 00 04 <- length
838                 00 00 00 00 <- value
839                 02 03 <- node descriptor type (routeId - 515)
840                 00 08 <- length
841                 03 03 03 04 0b 0b 0b 03 <- OSPF Router Id
842
843                 01 03 <- link descriptor type (IPv4 interface address - 259)
844                 00 04 <- length
845                 0b 0b 0b 01 <- value (11.11.11.1)
846
847                 40 <- attribute flags
848                 01 <- attribute type (Origin)
849                 01 <- attribute length
850                 00 <- value (IGP)
851                 40 <- attribute flags
852                 02 <- attribute type (AS Path)
853                 00 <- length
854                 40 <- attribute flags
855                 05 <- attribute type (local pref)
856                 04 <- length
857                 00 00 00 64 <- value
858                 c0 <- attribute flags
859                 63 <- attribute type (Link STATE - 99)
860                 07 <- length
861                 04 47 <- link attribute (1095 - Metric)
862                 00 03 <- length
863                 00 00 01 <- value
864          */
865         @Test
866         public void testBGPLink() throws Exception {
867                 final byte[] body = ByteArray.cutBytes(inputBytes.get(8), BGPMessageFactory.COMMON_HEADER_LENGTH);
868                 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(8), BGPMessageFactory.MARKER_LENGTH,
869                                 BGPMessageFactory.LENGTH_FIELD_LENGTH));
870                 final BGPUpdateEvent ret = BGPUpdateMessageParser.parse(body, messageLength);
871
872                 assertTrue(ret instanceof BGPUpdateMessage);
873                 final BGPUpdateMessage message = (BGPUpdateMessage) ret;
874
875                 // check fields
876
877                 assertEquals(Collections.EMPTY_SET, message.getRemovedObjects());
878
879                 // network object state
880                 final NetworkObjectState objState = new NetworkObjectState(ASPath.EMPTY, Collections.<Community> emptySet(), Collections.<ExtendedCommunity> emptySet());
881                 final BaseBGPObjectState state = new BaseBGPObjectState(BGPOrigin.IGP, null);
882
883                 // network link state
884                 final DefaultingTypesafeContainer<Metric<?>> container = new DefaultingTypesafeContainer<Metric<?>>();
885                 container.setDefaultEntry(new IGPMetric(1));
886                 final NetworkLinkState linkState = new NetworkLinkState(objState, container, null, LinkProtectionType.UNPROTECTED, null, null, null);
887
888                 final NodeIdentifierFactory f100 = new NodeIdentifierFactory(new ASNumber(100), new DomainIdentifier(new byte[] { 25, 25, 25, 1 }), new AreaIdentifier(new byte[] {
889                                 0, 0, 0, 0 }));
890
891                 final NodeIdentifier nodeid1 = f100.identifierForRouter(new OSPFv3LANIdentifier(new OSPFRouterIdentifier(new byte[] { 3, 3, 3, 4 }), new OSPFInterfaceIdentifier(new byte[] {
892                                 0x0b, 0x0b, 0x0b, 0x03 })));
893                 final NodeIdentifier nodeid2 = f100.identifierForRouter(new OSPFRouterIdentifier(new byte[] { 3, 3, 3, 4 }));
894
895                 final NodeIdentifier nodeid3 = f100.identifierForRouter(new OSPFRouterIdentifier(new byte[] { 1, 1, 1, 2 }));
896
897                 // check API message
898
899                 final LinkIdentifier linkId1 = new LinkIdentifier(null, new LinkAnchor(nodeid1, new IPv4InterfaceIdentifier(IPv4.FAMILY.addressForString("11.11.11.3"))), new LinkAnchor(nodeid2, null));
900                 final LinkIdentifier linkId2 = new LinkIdentifier(null, new LinkAnchor(nodeid1, new IPv4InterfaceIdentifier(IPv4.FAMILY.addressForString("11.11.11.1"))), new LinkAnchor(nodeid3, null));
901                 final LinkIdentifier linkId3 = new LinkIdentifier(null, new LinkAnchor(nodeid3, new IPv4InterfaceIdentifier(IPv4.FAMILY.addressForString("11.11.11.1"))), new LinkAnchor(nodeid1, null));
902
903                 final BGPLink link1 = new BGPLinkImpl(state, linkId1, linkState);
904                 final BGPLink link2 = new BGPLinkImpl(state, linkId2, linkState);
905                 final BGPLink link3 = new BGPLinkImpl(state, linkId3, linkState);
906
907                 final BGPUpdateMessage expectedMessage = new BGPUpdateMessageImpl(Sets.newHashSet((BGPObject) link1, (BGPObject) link2,
908                                 (BGPObject) link3), Collections.<Identifier> emptySet());
909
910                 assertEquals(expectedMessage, message);
911         }
912
913         /*
914          * TEST BGP Node
915          * 
916          *  00 00 <- withdrawn routes length
917                 00 b2 <- total path attribute length (178)
918                 90 <- attribute flags
919                 0e <- attribute type code (MP reach)
920                 00 a0 <- attribute extended length (160)
921                 40 04 <- AFI (16388 - Linkstate)
922                 47 <- SAFI (71 - Linkstate)
923                 04 <- next hop length
924                 19 19 19 01 - nexthop (25.25.25.1)
925                 00 <- reserved
926                 00 01 <- NLRI type (1 - nodeNLRI)
927                 00 31 <- NLRI length (49)
928                 03 <- ProtocolID - OSPF
929                 00 00 00 00 00 00 00 01 <- identifier
930         
931                 01 00 <- local node descriptor type (256)
932                 00 24 <- length (36)
933                 02 00 <- node descriptor type (member AS - 512)
934                 00 04 <- length
935                 00 00 00 64 <- value (100)
936                 02 01 <- node descriptor type (bgpId - 513)
937                 00 04 <- length
938                 19 19 19 01 <- bgpId (25.25.25.1)
939                 02 02 <- node descriptor type (areaId - 514)
940                 00 04 <- length
941                 00 00 00 00 <- value
942                 02 03 <- node descriptor type (routeId - 515)
943                 00 08 <- length
944                 03 03 03 04 0b 0b 0b 03 <- OSPF Router Id
945         
946                 00 01 <- NLRI type (1 - nodeNLRI)
947                 00 2d <- NLRI length (45)
948                 03 <- ProtocolID - OSPF
949                 00 00 00 00 00 00 00 01 <- identifier
950         
951                 01 00 <- local node descriptor type (256)
952                 00 20 <- length (32)
953                 02 00 <- node descriptor type (member AS - 512)
954                 00 04 <- length
955                 00 00 00 64 <- value (100)
956                 02 01 <- node descriptor type (bgpId - 513)
957                 00 04 <- length
958                 19 19 19 01 <- bgpId (25.25.25.1)
959                 02 02 <- node descriptor type (areaId - 514)
960                 00 04 <- length
961                 00 00 00 00 <- value
962                 02 03 <- node descriptor type (routeId - 515)
963                 00 04 <- length
964                 03 03 03 04 <- OSPF Router Id
965         
966                 00 01 <- NLRI type (1 - nodeNLRI)
967                 00 2d <- NLRI length (45)
968                 03 <- ProtocolID - OSPF
969                 00 00 00 00 00 00 00 01 <- identifier
970                 01 00 <- local node descriptor type (256)
971                 00 20 <- length (32)
972                 02 00 <- node descriptor type (member AS - 512)
973                 00 04 <- length
974                 00 00 00 64 <- value (100)
975                 02 01 <- node descriptor type (bgpId - 513)
976                 00 04 <- length
977                 19 19 19 01 <- bgpId (25.25.25.1)
978                 02 02 <- node descriptor type (areaId - 514)
979                 00 04 <- length
980                 00 00 00 00 <- value
981                 02 03 <- node descriptor type (routeId - 515)
982                 00 04 <- length
983                 01 01 01 02  <- OSPF Router Id
984         
985                 40 <- attribute flags
986                 01 <- attribute type (Origin)
987                 01 <- attribute length
988                 00 <- value (IGP)
989                 40 <- attribute flags
990                 02 <- attribute type (AS Path)
991                 00 <- length
992                 40 <- attribute flags
993                 05 <- attribute type (local pref)
994                 04 <- length
995                 00 00 00 64 <- value
996          */
997         @Test
998         public void testBGPNode() throws Exception {
999                 final byte[] body = ByteArray.cutBytes(inputBytes.get(9), BGPMessageFactory.COMMON_HEADER_LENGTH);
1000                 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(9), BGPMessageFactory.MARKER_LENGTH,
1001                                 BGPMessageFactory.LENGTH_FIELD_LENGTH));
1002                 final BGPUpdateEvent ret = BGPUpdateMessageParser.parse(body, messageLength);
1003
1004                 assertTrue(ret instanceof BGPUpdateMessage);
1005                 final BGPUpdateMessage message = (BGPUpdateMessage) ret;
1006
1007                 // check fields
1008
1009                 assertEquals(Collections.EMPTY_SET, message.getRemovedObjects());
1010
1011                 // network object state
1012                 final NetworkObjectState objState = new NetworkObjectState(ASPath.EMPTY, Collections.<Community> emptySet(), Collections.<ExtendedCommunity> emptySet());
1013                 final BaseBGPObjectState state = new BaseBGPObjectState(BGPOrigin.IGP, null);
1014                 final NetworkNodeState nstate = new NetworkNodeState(objState, Collections.<TopologyIdentifier> emptySet(), Collections.<ISISAreaIdentifier> emptySet(), false, false, false, false, Collections.<RouterIdentifier> emptySet(), null);
1015
1016                 // network link state
1017
1018                 final NodeIdentifierFactory f100 = new NodeIdentifierFactory(new ASNumber(100), new DomainIdentifier(new byte[] { 25, 25, 25, 1 }), new AreaIdentifier(new byte[] {
1019                                 0, 0, 0, 0 }));
1020
1021                 final NodeIdentifier nodeid1 = f100.identifierForRouter(new OSPFv3LANIdentifier(new OSPFRouterIdentifier(new byte[] { 3, 3, 3, 4 }), new OSPFInterfaceIdentifier(new byte[] {
1022                                 0x0b, 0x0b, 0x0b, 0x03 })));
1023                 final NodeIdentifier nodeid2 = f100.identifierForRouter(new OSPFRouterIdentifier(new byte[] { 3, 3, 3, 4 }));
1024
1025                 final NodeIdentifier nodeid3 = f100.identifierForRouter(new OSPFRouterIdentifier(new byte[] { 1, 1, 1, 2 }));
1026
1027                 // check API message
1028
1029                 final BGPNode node1 = new BGPNodeImpl(state, nodeid1, nstate);
1030                 final BGPNode node2 = new BGPNodeImpl(state, nodeid2, nstate);
1031                 final BGPNode node3 = new BGPNodeImpl(state, nodeid3, nstate);
1032
1033                 final BGPUpdateMessage expectedMessage = new BGPUpdateMessageImpl(Sets.newHashSet((BGPObject) node1, (BGPObject) node2,
1034                                 (BGPObject) node3), Collections.<Identifier> emptySet());
1035
1036                 assertEquals(expectedMessage, message);
1037         }
1038
1039         /*
1040          * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
1041          * 00 98 <- length (69) - including header
1042          * 01 <- message type
1043          * 04 <- BGP version
1044          * 00 64 <- My AS Number (AS TRANS in this case)
1045          * 00 b4 <- Hold Time
1046          * 00 00 00 00 <- BGP Identifier
1047          * 28 <- Optional Parameters Length
1048          * 02 <- opt. param. type (capabilities)
1049          * 06 <- length
1050          * 01 <- capability code (MP Extensions for BGP4)
1051          * 04 <- length
1052          * 00 01 00 01 <- AFI 1, SAFI 1
1053          * 02 <- opt. param. type (capabilities)
1054          * 06 <- length
1055          * 01 <- capability code (MP Extensions for BGP4)
1056          * 04 <- length
1057          * 00 02 00 01 <- AFI 2, SAFI 1
1058          * 02 <- opt. param. type (capabilities)
1059          * 06 <- length
1060          * 01 <- capability code (MP Extensions for BGP4)
1061          * 04 <- length
1062          * 40 04 00 47 <- AFI 16388, SAFI 71
1063          * 02 <- opt. param. type (capabilities)
1064          * 02 <- length
1065          * 80 <- capability code (private)
1066          * 00 <- length
1067          * 02 <- opt. param. type (capabilities)
1068          * 02 <- length
1069          * 02 <- capability code (Route refresh)
1070          * 00 <- length
1071          * 02 <- opt. param. type (capabilities)
1072          * 06 <- length
1073          * 41 <- capability code (AS4 octet support)
1074          * 04 <- length
1075          * 00 00 00 64 <- AS number
1076          */
1077         @Test
1078         public void testOpenMessage() throws Exception {
1079                 final BGPMessageFactory msgFactory = new BGPMessageFactory();
1080                 final BGPOpenMessage open = (BGPOpenMessage) msgFactory.parse(inputBytes.get(13));
1081                 final Set<BGPTableType> types = Sets.newHashSet();
1082                 for (final BGPParameter param : open.getOptParams()) {
1083                         if (param instanceof MultiprotocolCapability) {
1084                                 types.add(((MultiprotocolCapability) param).getTableType());
1085                         }
1086                 }
1087                 final Set<BGPTableType> expected = Sets.newHashSet(new BGPTableType(BGPAddressFamily.IPv4, BGPSubsequentAddressFamily.Unicast),
1088                                 new BGPTableType(BGPAddressFamily.IPv6, BGPSubsequentAddressFamily.Unicast),
1089                                 new BGPTableType(BGPAddressFamily.LinkState, BGPSubsequentAddressFamily.Linkstate));
1090                 assertEquals(expected, types);
1091         }
1092
1093         @Test
1094         public void testHashCodeEquals() throws UnknownHostException {
1095                 final BGPAggregator agg1 = new BGPAggregatorImpl<IPv4Address>(new ASNumber(6), IPv4.FAMILY.addressForString("10.0.0.9"));
1096
1097                 final BGPAggregator agg2 = new BGPAggregatorImpl<IPv4Address>(new ASNumber(6), IPv4.FAMILY.addressForString("10.0.0.9"));
1098
1099                 assertEquals(agg1, agg2);
1100                 assertEquals("HashCodes should be equal", agg1.hashCode(), agg2.hashCode());
1101                 assertEquals("toString should be equal", agg1.toString(), agg2.toString());
1102
1103                 final IPv4MP mp41 = new IPv4MP(false, new IPv4NextHop(IPv4.FAMILY.addressForString("10.0.0.9")), null);
1104
1105                 final IPv4MP mp42 = new IPv4MP(false, new IPv4NextHop(IPv4.FAMILY.addressForString("10.0.0.9")), null);
1106
1107                 assertEquals(mp41, mp42);
1108                 assertEquals("HashCodes should be equal", mp41.hashCode(), mp42.hashCode());
1109                 assertEquals("toString should be equal", mp41.toString(), mp42.toString());
1110
1111                 final IPv6MP mp61 = new IPv6MP(false, new IPv6NextHop(IPv6.FAMILY.addressForString("fe80::c001:bff:fe7e:0")), null);
1112
1113                 final IPv6MP mp62 = new IPv6MP(false, new IPv6NextHop(IPv6.FAMILY.addressForString("fe80::c001:bff:fe7e:0")), null);
1114
1115                 assertEquals(mp61, mp62);
1116                 assertEquals("HashCodes should be equal", mp61.hashCode(), mp62.hashCode());
1117                 assertEquals("toString should be equal", mp61.toString(), mp62.toString());
1118
1119                 final PathAttribute localPref1 = new PathAttribute(TypeCode.LOCAL_PREF, false, true, false, false, 100);
1120
1121                 final PathAttribute localPref2 = new PathAttribute(TypeCode.LOCAL_PREF, false, true, false, false, 100);
1122
1123                 assertEquals(localPref1, localPref2);
1124                 assertEquals("HashCodes should be equal", localPref1.hashCode(), localPref2.hashCode());
1125                 assertEquals("toString should be equal", localPref1.toString(), localPref2.toString());
1126         }
1127 }