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