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