Merge "Attach sources and javadoc only with -DrepoBuild"
[bgpcep.git] / bgp / parser-impl / src / test / java / org / opendaylight / protocol / bgp / parser / impl / BGPParserTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.protocol.bgp.parser.impl;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14
15 import java.io.ByteArrayOutputStream;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.math.BigInteger;
19 import java.util.ArrayList;
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.parser.BgpTableTypeImpl;
26 import org.opendaylight.protocol.bgp.parser.impl.message.BGPUpdateMessageParser;
27 import org.opendaylight.protocol.bgp.parser.impl.message.update.CommunityUtil;
28 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
29 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
30 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
31 import org.opendaylight.protocol.util.ByteArray;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.AreaIdentifier;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.DomainIdentifier;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.Identifier;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.Ipv4InterfaceIdentifier;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.LinkstateAddressFamily;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.LinkstateSubsequentAddressFamily;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.NlriType;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.OspfInterfaceIdentifier;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.ProtocolId;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.CLinkstateDestination;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.CLinkstateDestinationBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.c.linkstate.destination.LinkDescriptorsBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.c.linkstate.destination.LocalNodeDescriptorsBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.c.linkstate.destination.RemoteNodeDescriptorsBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.COspfNodeBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.COspfPseudonodeBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.c.ospf.node.OspfNodeBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.c.ospf.pseudonode.OspfPseudonodeBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.LinkstatePathAttributeBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.LinkAttributesBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstate;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.UpdateBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.BgpParameters;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.CParameters;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Aggregator;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AggregatorBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AsPathBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AtomicAggregateBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Communities;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.ExtendedCommunities;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.ExtendedCommunitiesBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.LocalPrefBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.MultiExitDiscBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.OriginBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.as.path.Segments;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.as.path.SegmentsBuilder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.Nlri;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.NlriBuilder;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.WithdrawnRoutesBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1Builder;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.destination.type.DestinationIpv6Builder;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.c.parameters.CMultiprotocol;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlri;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlriBuilder;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpOrigin;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ClusterIdentifier;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.CAListBuilder;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.CASetBuilder;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.c.a.list.AsSequence;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.c.a.list.AsSequenceBuilder;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.CInet4SpecificExtendedCommunityBuilder;
102 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;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.CIpv4NextHop;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.CIpv4NextHopBuilder;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.CIpv6NextHop;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.CIpv6NextHopBuilder;
107 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;
108 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;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Metric;
110
111 import com.google.common.collect.Lists;
112 import com.google.common.collect.Sets;
113
114 public class BGPParserTest {
115
116         /**
117          * Used by other tests as well
118          */
119         static final List<byte[]> inputBytes = new ArrayList<byte[]>();
120
121         private static int COUNTER = 17;
122
123         private static int MAX_SIZE = 300;
124
125         private static BGPUpdateMessageParser updateParser;
126
127         @BeforeClass
128         public static void setUp() throws Exception {
129                 updateParser = new BGPUpdateMessageParser(ServiceLoaderBGPExtensionProviderContext.createConsumerContext().getAttributeRegistry());
130
131                 for (int i = 1; i <= COUNTER; i++) {
132                         final String name = "/up" + i + ".bin";
133                         final InputStream is = BGPParserTest.class.getResourceAsStream(name);
134                         if (is == null) {
135                                 throw new IOException("Failed to get resource " + name);
136                         }
137
138                         final ByteArrayOutputStream bis = new ByteArrayOutputStream();
139                         final byte[] data = new byte[MAX_SIZE];
140                         int nRead = 0;
141                         while ((nRead = is.read(data, 0, data.length)) != -1) {
142                                 bis.write(data, 0, nRead);
143                         }
144                         bis.flush();
145
146                         inputBytes.add(bis.toByteArray());
147                 }
148         }
149
150         @Test
151         public void testResource() {
152                 assertNotNull(inputBytes);
153         }
154
155         /*
156          * Tests IPv4 NEXT_HOP, ATOMIC_AGGREGATE, COMMUNITY, NLRI
157          * 
158          * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
159          * 00 54 <- length (84) - including header
160          * 02 <- message type
161          * 00 00 <- withdrawn routes length
162          * 00 31 <- total path attribute length (49)
163          * 40 <- attribute flags
164          * 01 <- attribute type code (origin)
165          * 01 <- attribute length
166          * 00 <- Origin value (IGP)
167          * 40 <- attribute flags
168          * 02 <- attribute type code (as path)
169          * 06 <- attribute length
170          * 02 <- AS_SEQUENCE
171          * 01 <- path segment count
172          * 00 00 fd ea <- path segment value (65002)
173          * 40 <- attribute flags
174          * 03 <- attribute type code (Next Hop)
175          * 04 <- attribute length
176          * 10 00 00 02 <- value (10.0.0.2)
177          * 80 <- attribute flags
178          * 04 <- attribute type code (multi exit disc)
179          * 04 <- attribute length
180          * 00 00 00 00 <- value
181          * 64 <- attribute flags
182          * 06 <- attribute type code (atomic aggregate)
183          * 00 <- attribute length
184          * 64 <- attribute flags
185          * 08 <- attribute type code (community)
186          * 10 <- attribute length FF FF FF
187          * 01 <- value (NO_EXPORT)
188          * FF FF FF 02 <- value (NO_ADVERTISE)
189          * FF FF FF 03 <- value (NO_EXPORT_SUBCONFED)
190          * FF FF FF 10 <- unknown Community
191          * 
192          * //NLRI
193          * 18 ac 11 02 <- IPv4 Prefix (172.17.2.0 / 24)
194          * 18 ac 11 01 <- IPv4 Prefix (172.17.1.0 / 24)
195          * 18 ac 11 00 <- IPv4 Prefix (172.17.0.0 / 24)
196          */
197         @Test
198         public void testGetUpdateMessage1() throws Exception {
199
200                 final byte[] body = ByteArray.cutBytes(inputBytes.get(0), MessageUtil.COMMON_HEADER_LENGTH);
201                 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(0), MessageUtil.MARKER_LENGTH,
202                                 MessageUtil.LENGTH_FIELD_LENGTH));
203                 final Update message = BGPParserTest.updateParser.parseMessageBody(body, messageLength);
204
205                 // check fields
206
207                 assertNull(message.getWithdrawnRoutes());
208
209                 // attributes
210
211                 final List<AsSequence> asnums = Lists.newArrayList(new AsSequenceBuilder().setAs(new AsNumber(65002L)).build());
212                 final List<Segments> asPath = Lists.newArrayList();
213                 asPath.add(new SegmentsBuilder().setCSegment(new CAListBuilder().setAsSequence(asnums).build()).build());
214
215                 final CIpv4NextHop nextHop = new CIpv4NextHopBuilder().setIpv4NextHop(
216                                 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.2")).build()).build();
217
218                 final List<Communities> comms = Lists.newArrayList();
219                 comms.add((Communities) CommunityUtil.NO_EXPORT);
220                 comms.add((Communities) CommunityUtil.NO_ADVERTISE);
221                 comms.add((Communities) CommunityUtil.NO_EXPORT_SUBCONFED);
222                 comms.add((Communities) CommunityUtil.create(0xFFFF, 0xFF10));
223
224                 final UpdateBuilder builder = new UpdateBuilder();
225
226                 // check nlri
227
228                 final List<Ipv4Prefix> prefs = Lists.newArrayList();
229                 prefs.add(new Ipv4Prefix("172.17.2.0/24"));
230                 prefs.add(new Ipv4Prefix("172.17.1.0/24"));
231                 prefs.add(new Ipv4Prefix("172.17.0.0/24"));
232
233                 final Nlri nlri = new NlriBuilder().setNlri(prefs).build();
234
235                 assertEquals(nlri, message.getNlri());
236
237                 builder.setNlri(nlri);
238
239                 // check path attributes
240
241                 final PathAttributes attrs = message.getPathAttributes();
242
243                 final PathAttributesBuilder paBuilder = new PathAttributesBuilder();
244
245                 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
246                 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
247
248                 paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
249                 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
250
251                 paBuilder.setCNextHop(nextHop);
252                 assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
253
254                 paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
255                 assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
256
257                 paBuilder.setAtomicAggregate(new AtomicAggregateBuilder().build());
258                 assertEquals(paBuilder.getAtomicAggregate(), attrs.getAtomicAggregate());
259
260                 paBuilder.setCommunities(comms);
261                 assertEquals(paBuilder.getCommunities(), attrs.getCommunities());
262
263                 builder.setPathAttributes(paBuilder.build());
264
265                 assertEquals(builder.build(), message);
266         }
267
268         /*
269          * Tests IPv6 NEXT_HOP, NLRI, ORIGIN.IGP, MULTI_EXIT_DISC, ORIGINATOR-ID, CLUSTER_LIST.
270          * 
271          * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
272          * 00 80 <- length (128) - including header
273          * 02 <- message type
274          * 00 00 <- withdrawn routes length
275          * 00 69 <- total path attribute length (105)
276          * 40 <- attribute flags
277          * 01 <- attribute type code (origin)
278          * 01 <- attribute length
279          * 00 <- Origin value (IGP)
280          * 40 <- attribute flags
281          * 02 <- attribute type code (as path)
282          * 06 <- attribute length
283          * 02 <- AS_SEQUENCE
284          * 01 <- path segment count
285          * 00 00 fd e9 <- path segment value (65001)
286          * 80 <- attribute flags
287          * 04 <- attribute type code (multi exit disc)
288          * 04 <- attribute length
289          * 00 00 00 00 <- value
290          * 80 <- attribute flags
291          * 09 <- attribute type code (originator id)
292          * 04 <- attribute length
293          * 7f 00 00 01 <- value (localhost ip)
294          * 80 <- attribute flags
295          * 0a <- attribute type code (cluster list)
296          * 08 <- attribute length
297          * 01 02 03 04 <- value
298          * 05 06 07 08 <- value
299          * 80 <- attribute flags
300          * 0e <- attribute type code (mp reach nlri)
301          * 40 <- attribute length
302          * 00 02 <- AFI (Ipv6)
303          * 01 <- SAFI (Unicast)
304          * 20 <- length of next hop
305          * 20 01 0d b8 00 00 00 00 00 00 00 00 00 00 00 01 <- global
306          * fe 80 00 00 00 00 00 00 c0 01 0b ff fe 7e 00 <- link local
307          * 00 <- reserved
308          * 
309          * //NLRI
310          * 40 20 01 0d b8 00 01 00 02 <- IPv6 Prefix (2001:db8:1:2:: / 64)
311          * 40 20 01 0d b8 00 01 00 01 <- IPv6 Prefix (2001:db8:1:1:: / 64)
312          * 40 20 01 0d b8 00 01 00 00 <- IPv6 Prefix (2001:db8:1:: / 64)
313          * 
314          */
315         @Test
316         public void testGetUpdateMessage2() throws Exception {
317                 final byte[] body = ByteArray.cutBytes(inputBytes.get(1), MessageUtil.COMMON_HEADER_LENGTH);
318                 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(1), MessageUtil.MARKER_LENGTH,
319                                 MessageUtil.LENGTH_FIELD_LENGTH));
320                 final Update message = BGPParserTest.updateParser.parseMessageBody(body, messageLength);
321
322                 // check fields
323                 assertNull(message.getWithdrawnRoutes());
324
325                 final UpdateBuilder builder = new UpdateBuilder();
326
327                 // check NLRI
328
329                 final List<Ipv6Prefix> prefs = Lists.newArrayList();
330                 prefs.add(new Ipv6Prefix("2001:db8:1:2::/64"));
331                 prefs.add(new Ipv6Prefix("2001:db8:1:1::/64"));
332                 prefs.add(new Ipv6Prefix("2001:db8:1::/64"));
333
334                 assertNull(message.getNlri());
335
336                 // attributes
337
338                 final List<AsSequence> asnums = Lists.newArrayList(new AsSequenceBuilder().setAs(new AsNumber(65001L)).build());
339                 final List<Segments> asPath = Lists.newArrayList();
340                 asPath.add(new SegmentsBuilder().setCSegment(new CAListBuilder().setAsSequence(asnums).build()).build());
341
342                 final CIpv6NextHop nextHop = new CIpv6NextHopBuilder().setIpv6NextHop(
343                                 new Ipv6NextHopBuilder().setGlobal(new Ipv6Address("2001:db8::1")).setLinkLocal(new Ipv6Address("fe80::c001:bff:fe7e:0")).build()).build();
344
345                 final List<ClusterIdentifier> clusters = Lists.newArrayList(new ClusterIdentifier(new byte[] { 1, 2, 3, 4 }),
346                                 new ClusterIdentifier(new byte[] { 5, 6, 7, 8 }));
347
348                 // check path attributes
349
350                 final PathAttributes attrs = message.getPathAttributes();
351
352                 final PathAttributesBuilder paBuilder = new PathAttributesBuilder();
353
354                 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
355                 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
356
357                 paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
358                 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
359
360                 paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
361                 assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
362
363                 paBuilder.setOriginatorId(new byte[] { 127, 0, 0, 1 });
364                 assertArrayEquals(paBuilder.getOriginatorId(), attrs.getOriginatorId());
365
366                 paBuilder.setClusterId(clusters);
367                 assertEquals(paBuilder.getClusterId(), attrs.getClusterId());
368
369                 final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
370                 mpBuilder.setAfi(Ipv6AddressFamily.class);
371                 mpBuilder.setSafi(UnicastSubsequentAddressFamily.class);
372                 mpBuilder.setCNextHop(nextHop);
373                 mpBuilder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
374                                 new DestinationIpv6Builder().setIpv6Prefixes(prefs).build()).build());
375
376                 paBuilder.addAugmentation(PathAttributes1.class, new PathAttributes1Builder().setMpReachNlri(mpBuilder.build()).build());
377                 assertEquals(paBuilder.getAugmentation(PathAttributes1.class).getMpReachNlri(),
378                                 attrs.getAugmentation(PathAttributes1.class).getMpReachNlri());
379
380                 // check API message
381
382                 builder.setPathAttributes(paBuilder.build());
383                 assertEquals(builder.build(), message);
384         }
385
386         /*
387          * Tests more AS Numbers in AS_PATH, AGGREGATOR, ORIGIN.INCOMPLETE
388          * 
389          * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
390          * 00 4b <- length (75) - including header
391          * 02 <- message type
392          * 00 00 <- withdrawn routes length
393          * 00 30 <- total path attribute length (48)
394          * 40 <- attribute flags
395          * 01 <- attribute type code (origin)
396          * 01 <- attribute length
397          * 02 <- Origin value (Incomplete)
398          * 40 <- attribute flags
399          * 02 <- attribute type code (as path)
400          * 10 <- attribute length
401          * 02 <- AS_SEQUENCE
402          * 01 <- path segment count
403          * 00 00 00 1e <- path segment value (30)
404          * 01 <- AS_SET
405          * 02 <- path segment count
406          * 00 00 00 0a <- path segment value (10)
407          * 00 00 00 14 <- path segment value (20)
408          * 40 <- attribute flags
409          * 03 <- attribute type (Next hop)
410          * 04 <- attribute length
411          * 0a 00 00 09 <- value (10.0.0.9)
412          * 80 <- attribute flags
413          * 04 <- attribute type code (multi exit disc)
414          * 04 <- attribute length
415          * 00 00 00 00 <- value
416          * c0 <- attribute flags
417          * 07 <- attribute type (Aggregator)
418          * 08 <- attribute length
419          * 00 00 00 1e <- value (AS number = 30)
420          * 0a 00 00 09 <- value (IP address = 10.0.0.9)
421          * 
422          * //NLRI
423          * 15 ac 10 00 <- IPv4 Prefix (172.16.0.0 / 21)
424          */
425         @Test
426         public void testGetUpdateMessage3() throws Exception {
427                 final byte[] body = ByteArray.cutBytes(inputBytes.get(2), MessageUtil.COMMON_HEADER_LENGTH);
428                 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(2), MessageUtil.MARKER_LENGTH,
429                                 MessageUtil.LENGTH_FIELD_LENGTH));
430                 final Update message = BGPParserTest.updateParser.parseMessageBody(body, messageLength);
431
432                 final UpdateBuilder builder = new UpdateBuilder();
433
434                 // check nlri
435                 final Ipv4Prefix pref1 = new Ipv4Prefix("172.16.0.0/21");
436
437                 final List<Ipv4Prefix> nlri = Lists.newArrayList(pref1);
438                 builder.setNlri(new NlriBuilder().setNlri(nlri).build());
439                 assertEquals(builder.getNlri(), message.getNlri());
440
441                 // check fields
442                 assertNull(message.getWithdrawnRoutes());
443
444                 // attributes
445                 final List<AsSequence> asnums = Lists.newArrayList(new AsSequenceBuilder().setAs(new AsNumber(30L)).build());
446                 final List<Segments> asPath = Lists.newArrayList();
447                 asPath.add(new SegmentsBuilder().setCSegment(new CAListBuilder().setAsSequence(asnums).build()).build());
448                 asPath.add(new SegmentsBuilder().setCSegment(
449                                 new CASetBuilder().setAsSet(Lists.newArrayList(new AsNumber(10L), new AsNumber(20L))).build()).build());
450
451                 final Aggregator aggregator = new AggregatorBuilder().setAsNumber(new AsNumber((long) 30)).setNetworkAddress(
452                                 new Ipv4Address("10.0.0.9")).build();
453                 final CIpv4NextHop nextHop = new CIpv4NextHopBuilder().setIpv4NextHop(
454                                 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("10.0.0.9")).build()).build();
455
456                 // check path attributes
457                 final PathAttributes attrs = message.getPathAttributes();
458
459                 final PathAttributesBuilder paBuilder = new PathAttributesBuilder();
460
461                 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Incomplete).build());
462                 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
463
464                 paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
465                 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
466
467                 paBuilder.setCNextHop(nextHop);
468                 assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
469
470                 paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
471                 assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
472
473                 paBuilder.setAggregator(aggregator);
474                 assertEquals(paBuilder.getAggregator(), attrs.getAggregator());
475
476                 builder.setPathAttributes(paBuilder.build());
477
478                 assertEquals(builder.build(), message);
479         }
480
481         /*
482          * Tests empty AS_PATH, ORIGIN.EGP, LOCAL_PREF, EXTENDED_COMMUNITIES (Ipv4 Addr specific)
483          * 
484          * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
485          * 00 4A <- length (73) - including header
486          * 02 <- message type
487          * 00 00 <- withdrawn routes length
488          * 00 27 <- total path attribute length (39)
489          * 40 <- attribute flags
490          * 01 <- attribute type code (Origin)
491          * 01 <- attribute length
492          * 01 <- Origin value (EGP)
493          * 40 <- attribute flags
494          * 02 <- attribute type code (As path)
495          * 00 <- attribute length
496          * 40 <- attribute flags
497          * 03 <- attribute type (Next hop)
498          * 04 <- attribute length
499          * 03 03 03 03 <- value (3.3.3.3)
500          * 80 <- attribute flags
501          * 04 <- attribute type code (Multi exit disc)
502          * 04 <- attribute length
503          * 00 00 00 00 <- value
504          * 40 <- attribute flags
505          * 05 <- attribute type (Local Pref)
506          * 04 <- attribute length
507          * 00 00 00 64 <- value (100)
508          * 80 <- attribute flags
509          * 10 <- attribute type (extended community)
510          * 08 <- attribute length
511          * 01 04 <- value (type - Ipv4 Address Specific Extended Community)
512          * c0 a8 01 00 <- value (global adm. 198.162.1.0)
513          * 12 34 <- value (local adm. 4660)
514          * 
515          * //NLRI
516          * 18 0a 1e 03 <- IPv4 Prefix (10.30.3.0 / 24)
517          * 18 0a 1e 02 <- IPv4 Prefix (10.30.2.0 / 24)
518          * 18 0a 1e 01 <- IPv4 Prefix (10.30.1.0 / 24)
519          */
520         @Test
521         public void testGetUpdateMessage4() throws Exception {
522                 final byte[] body = ByteArray.cutBytes(inputBytes.get(3), MessageUtil.COMMON_HEADER_LENGTH);
523                 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(3), MessageUtil.MARKER_LENGTH,
524                                 MessageUtil.LENGTH_FIELD_LENGTH));
525                 final Update message = BGPParserTest.updateParser.parseMessageBody(body, messageLength);
526
527                 final UpdateBuilder builder = new UpdateBuilder();
528
529                 // check fields
530                 assertNull(message.getWithdrawnRoutes());
531
532                 // check nlri
533                 final Ipv4Prefix pref1 = new Ipv4Prefix("10.30.3.0/24");
534                 final Ipv4Prefix pref2 = new Ipv4Prefix("10.30.2.0/24");
535                 final Ipv4Prefix pref3 = new Ipv4Prefix("10.30.1.0/24");
536
537                 final List<Ipv4Prefix> nlri = Lists.newArrayList(pref1, pref2, pref3);
538                 builder.setNlri(new NlriBuilder().setNlri(nlri).build());
539                 assertEquals(builder.getNlri(), message.getNlri());
540
541                 // attributes
542                 final CIpv4NextHop nextHop = new CIpv4NextHopBuilder().setIpv4NextHop(
543                                 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("3.3.3.3")).build()).build();
544
545                 final List<ExtendedCommunities> comms = Lists.newArrayList();
546                 comms.add(new ExtendedCommunitiesBuilder().setCommType((short) 1).setExtendedCommunity(
547                                 new CInet4SpecificExtendedCommunityBuilder().setInet4SpecificExtendedCommunity(
548                                                 new Inet4SpecificExtendedCommunityBuilder().setTransitive(false).setGlobalAdministrator(
549                                                                 new Ipv4Address("192.168.1.0")).setLocalAdministrator(new byte[] { 0x12, 0x34 }).build()).build()).build());
550
551                 final List<Segments> asPath = Lists.newArrayList();
552
553                 // check path attributes
554                 final PathAttributes attrs = message.getPathAttributes();
555
556                 final PathAttributesBuilder paBuilder = new PathAttributesBuilder();
557
558                 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Egp).build());
559                 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
560
561                 paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
562                 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
563
564                 paBuilder.setCNextHop(nextHop);
565                 assertEquals(paBuilder.getCNextHop(), attrs.getCNextHop());
566
567                 paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
568                 assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
569
570                 paBuilder.setLocalPref(new LocalPrefBuilder().setPref(100L).build());
571                 assertEquals(paBuilder.getLocalPref(), attrs.getLocalPref());
572
573                 paBuilder.setExtendedCommunities(comms);
574                 assertEquals(paBuilder.getExtendedCommunities(), attrs.getExtendedCommunities());
575
576                 // check API message
577                 builder.setPathAttributes(paBuilder.build());
578                 assertEquals(builder.build(), message);
579         }
580
581         /*
582          * Tests withdrawn routes.
583          * 
584          * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
585          * 00 1c <- length (28) - including header
586          * 02 <- message type
587          * 00 05 <- withdrawn routes length (5)
588          * 1e ac 10 00 04 <- route (172.16.0.4)
589          * 00 00 <- total path attribute length
590          */
591         @Test
592         public void testGetUpdateMessage5() throws Exception {
593                 final byte[] body = ByteArray.cutBytes(inputBytes.get(4), MessageUtil.COMMON_HEADER_LENGTH);
594                 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(4), MessageUtil.MARKER_LENGTH,
595                                 MessageUtil.LENGTH_FIELD_LENGTH));
596                 final Update message = BGPParserTest.updateParser.parseMessageBody(body, messageLength);
597
598                 // attributes
599                 final List<Ipv4Prefix> prefs = Lists.newArrayList(new Ipv4Prefix("172.16.0.4/30"));
600
601                 // check API message
602                 final Update expectedMessage = new UpdateBuilder().setWithdrawnRoutes(
603                                 new WithdrawnRoutesBuilder().setWithdrawnRoutes(prefs).build()).build();
604
605                 assertEquals(expectedMessage.getWithdrawnRoutes(), message.getWithdrawnRoutes());
606         }
607
608         /*
609          * End of Rib for Ipv4.
610          * 
611          * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
612          * 00 17 <- length (23) - including header
613          * 02 <- message type
614          * 00 00 <- withdrawn routes length
615          * 00 00 <- total path attribute length
616          */
617         @Test
618         public void testEORIpv4() throws Exception {
619                 final byte[] body = ByteArray.cutBytes(inputBytes.get(5), MessageUtil.COMMON_HEADER_LENGTH);
620                 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(5), MessageUtil.MARKER_LENGTH,
621                                 MessageUtil.LENGTH_FIELD_LENGTH));
622                 final Update message = BGPParserTest.updateParser.parseMessageBody(body, messageLength);
623
624                 assertEquals(new UpdateBuilder().build(), message);
625         }
626
627         /*
628          * End of Rib for Ipv6 consists of empty MP_UNREACH_NLRI, with AFI 2 and SAFI 1
629          * 
630          * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
631          * 00 1d <- length (29) - including header
632          * 02 <- message type
633          * 00 00 <- withdrawn routes length
634          * 00 06 <- total path attribute length
635          * 80 <- attribute flags
636          * 0f <- attribute type (15 - MP_UNREACH_NLRI)
637          * 03 <- attribute length
638          * 00 02 <- value (AFI 2: IPv6)
639          * 01 <- value (SAFI 1)
640          */
641         @Test
642         public void testEORIpv6() throws Exception {
643                 final byte[] body = ByteArray.cutBytes(inputBytes.get(6), MessageUtil.COMMON_HEADER_LENGTH);
644                 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(6), MessageUtil.MARKER_LENGTH,
645                                 MessageUtil.LENGTH_FIELD_LENGTH));
646                 final Update message = BGPParserTest.updateParser.parseMessageBody(body, messageLength);
647
648                 final Class<? extends AddressFamily> afi = message.getPathAttributes().getAugmentation(PathAttributes2.class).getMpUnreachNlri().getAfi();
649                 final Class<? extends SubsequentAddressFamily> safi = message.getPathAttributes().getAugmentation(PathAttributes2.class).getMpUnreachNlri().getSafi();
650
651                 assertEquals(Ipv6AddressFamily.class, afi);
652                 assertEquals(UnicastSubsequentAddressFamily.class, safi);
653         }
654
655         /*
656          * End of Rib for LS consists of empty MP_UNREACH_NLRI, with AFI 16388 and SAFI 71
657          * 
658          * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
659          * 00 1d <- length (29) - including header
660          * 02 <- message type
661          * 00 00 <- withdrawn routes length
662          * 00 06 <- total path attribute length
663          * 80 <- attribute flags
664          * 0f <- attribute type (15 - MP_UNREACH_NLRI)
665          * 03 <- attribute length
666          * 40 04 <- value (AFI 16388: LS)
667          * 47 <- value (SAFI 71)
668          */
669         @Test
670         public void testEORLS() throws Exception {
671                 final byte[] body = ByteArray.cutBytes(inputBytes.get(7), MessageUtil.COMMON_HEADER_LENGTH);
672                 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(7), MessageUtil.MARKER_LENGTH,
673                                 MessageUtil.LENGTH_FIELD_LENGTH));
674                 final Update message = BGPParserTest.updateParser.parseMessageBody(body, messageLength);
675
676                 final Class<? extends AddressFamily> afi = message.getPathAttributes().getAugmentation(PathAttributes2.class).getMpUnreachNlri().getAfi();
677                 final Class<? extends SubsequentAddressFamily> safi = message.getPathAttributes().getAugmentation(PathAttributes2.class).getMpUnreachNlri().getSafi();
678
679                 assertEquals(LinkstateAddressFamily.class, afi);
680                 assertEquals(LinkstateSubsequentAddressFamily.class, safi);
681         }
682
683         /*
684          *  Tests BGP Link Ipv4
685          * 
686                 00 00 <- withdrawn routes length
687                 01 48 <- total path attribute length (328)
688                 90 <- attribute flags
689                 0e <- attribute type code (MP reach)
690                 01 2c <- attribute extended length (300)
691                 40 04 <- AFI (16388 - Linkstate)
692                 47 <- SAFI (71 - Linkstate)
693                 04 <- next hop length
694                 19 19 19 01 <- nexthop (25.25.25.1)
695                 00 <- reserved
696
697                 00 02 <- NLRI type (2 - linkNLRI)
698                 00 5d <- NLRI length (93)
699                 03 <- ProtocolID - OSPF
700                 00 00 00 00 00 00 00 01 <- identifier
701
702                 01 00 <- local node descriptor type (256)
703                 00 24 <- length (36)
704                 02 00 <- node descriptor type (member AS - 512)
705                 00 04 <- length
706                 00 00 00 64 <- value (100)
707                 02 01 <- node descriptor type (bgpId - 513)
708                 00 04 <- length
709                 19 19 19 01 <- bgpId (25.25.25.1)
710                 02 02 <- node descriptor type (areaId - 514)
711                 00 04 <- length
712                 00 00 00 00 <- value
713                 02 03 <- node descriptor type (routeId - 515)
714                 00 08 <- length
715                 03 03 03 04 0b 0b 0b 03 <- OSPF Router Id
716
717                 01 01 <- remote node descriptor type (257)
718                 00 20 <- length (32)
719                 02 00 <- node descriptor type (member AS - 512)
720                 00 04 <- length
721                 00 00 00 64 <- value (100)
722                 02 01 <- node descriptor type (bgpId - 513)
723                 00 04 <- length
724                 19 19 19 01 <- bgpId (25.25.25.1)
725                 02 02 <- node descriptor type (areaId - 514)
726                 00 04 <- length
727                 00 00 00 00 <- value
728                 02 03 <- node descriptor type (routeId - 515)
729                 00 04 <- length
730                 03 03 03 04 <- OSPF Router Id
731
732                 01 03 <- link descriptor type (IPv4 interface address - 259)
733                 00 04 <- length (4)
734                 0b 0b 0b 03 <- value (11.11.11.3)
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                 01 01 01 02 <- OSPF Router Id
770
771                 01 03 <- link descriptor type (IPv4 interface address - 259)
772                 00 04 <- length
773                 0b 0b 0b 01 <- value (11.11.11.1)
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 20 <- length (32)
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 04 <- length
793                 01 01 01 02 <- OSPF Router Id
794
795                 01 01 <- remote node descriptor type (257)
796                 00 24 <- length (36)
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 08 <- length
808                 03 03 03 04 0b 0b 0b 03 <- 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                 40 <- attribute flags
815                 01 <- attribute type (Origin)
816                 01 <- attribute length
817                 00 <- value (IGP)
818                 40 <- attribute flags
819                 02 <- attribute type (AS Path)
820                 00 <- length
821                 40 <- attribute flags
822                 05 <- attribute type (local pref)
823                 04 <- length
824                 00 00 00 64 <- value
825                 c0 <- attribute flags
826                 63 <- attribute type (Link STATE - 99)
827                 07 <- length
828                 04 47 <- link attribute (1095 - Metric)
829                 00 03 <- length
830                 00 00 01 <- value
831          */
832         @Test
833         public void testBGPLink() throws Exception {
834                 final byte[] body = ByteArray.cutBytes(inputBytes.get(8), MessageUtil.COMMON_HEADER_LENGTH);
835                 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(8), MessageUtil.MARKER_LENGTH,
836                                 MessageUtil.LENGTH_FIELD_LENGTH));
837                 final Update message = BGPParserTest.updateParser.parseMessageBody(body, messageLength);
838
839                 final UpdateBuilder builder = new UpdateBuilder();
840
841                 // check fields
842
843                 assertNull(message.getWithdrawnRoutes());
844
845                 final CIpv4NextHop nextHop = new CIpv4NextHopBuilder().setIpv4NextHop(
846                                 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("25.25.25.1")).build()).build();
847
848                 final List<Segments> asPath = Lists.newArrayList();
849
850                 final LocalNodeDescriptorsBuilder lndBuilder = new LocalNodeDescriptorsBuilder().setAsNumber(new AsNumber((long) 100)).setDomainId(
851                                 new DomainIdentifier(new byte[] { (byte) 0x19, (byte) 0x19, (byte) 0x19, (byte) 0x01 })).setAreaId(
852                                 new AreaIdentifier(new byte[] { 0, 0, 0, 0 }));
853
854                 final RemoteNodeDescriptorsBuilder rndBuilder = new RemoteNodeDescriptorsBuilder().setAsNumber(new AsNumber((long) 100)).setDomainId(
855                                 new DomainIdentifier(new byte[] { (byte) 0x19, (byte) 0x19, (byte) 0x19, (byte) 0x01 })).setAreaId(
856                                 new AreaIdentifier(new byte[] { 0, 0, 0, 0 }));
857
858                 final CLinkstateDestinationBuilder clBuilder = new CLinkstateDestinationBuilder();
859                 clBuilder.setIdentifier(new Identifier(BigInteger.ONE));
860                 clBuilder.setNlriType(NlriType.Link);
861                 clBuilder.setProtocolId(ProtocolId.Ospf);
862
863                 final PathAttributes1Builder lsBuilder = new PathAttributes1Builder();
864                 final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
865                 mpBuilder.setAfi(LinkstateAddressFamily.class);
866                 mpBuilder.setSafi(LinkstateSubsequentAddressFamily.class);
867                 mpBuilder.setCNextHop(nextHop);
868
869                 final List<CLinkstateDestination> linkstates = Lists.newArrayList();
870                 clBuilder.setLocalNodeDescriptors(lndBuilder.setCRouterIdentifier(
871                                 new COspfPseudonodeBuilder().setOspfPseudonode(
872                                                 new OspfPseudonodeBuilder().setOspfRouterId(new byte[] { 3, 3, 3, 4 }).setLanInterface(
873                                                                 new OspfInterfaceIdentifier(new byte[] { 0x0b, 0x0b, 0x0b, 0x03 })).build()).build()).build());
874                 clBuilder.setRemoteNodeDescriptors(rndBuilder.setCRouterIdentifier(
875                                 new COspfNodeBuilder().setOspfNode(new OspfNodeBuilder().setOspfRouterId(new byte[] { 3, 3, 3, 4 }).build()).build()).build());
876                 clBuilder.setLinkDescriptors(new LinkDescriptorsBuilder().setIpv4InterfaceAddress(
877                                 new Ipv4InterfaceIdentifier(new Ipv4Address("11.11.11.3"))).build());
878                 linkstates.add(clBuilder.build());
879
880                 clBuilder.setLocalNodeDescriptors(lndBuilder.setCRouterIdentifier(
881                                 new COspfPseudonodeBuilder().setOspfPseudonode(
882                                                 new OspfPseudonodeBuilder().setOspfRouterId(new byte[] { 3, 3, 3, 4 }).setLanInterface(
883                                                                 new OspfInterfaceIdentifier(new byte[] { 0x0b, 0x0b, 0x0b, 0x03 })).build()).build()).build());
884                 clBuilder.setRemoteNodeDescriptors(rndBuilder.setCRouterIdentifier(
885                                 new COspfNodeBuilder().setOspfNode(new OspfNodeBuilder().setOspfRouterId(new byte[] { 1, 1, 1, 2 }).build()).build()).build());
886                 clBuilder.setLinkDescriptors(new LinkDescriptorsBuilder().setIpv4InterfaceAddress(
887                                 new Ipv4InterfaceIdentifier(new Ipv4Address("11.11.11.1"))).build());
888                 linkstates.add(clBuilder.build());
889
890                 clBuilder.setLocalNodeDescriptors(lndBuilder.setCRouterIdentifier(
891                                 new COspfNodeBuilder().setOspfNode(new OspfNodeBuilder().setOspfRouterId(new byte[] { 1, 1, 1, 2 }).build()).build()).build());
892                 clBuilder.setRemoteNodeDescriptors(rndBuilder.setCRouterIdentifier(
893                                 new COspfPseudonodeBuilder().setOspfPseudonode(
894                                                 new OspfPseudonodeBuilder().setOspfRouterId(new byte[] { 3, 3, 3, 4 }).setLanInterface(
895                                                                 new OspfInterfaceIdentifier(new byte[] { 0x0b, 0x0b, 0x0b, 0x03 })).build()).build()).build());
896                 clBuilder.setLinkDescriptors(new LinkDescriptorsBuilder().setIpv4InterfaceAddress(
897                                 new Ipv4InterfaceIdentifier(new Ipv4Address("11.11.11.1"))).build());
898                 linkstates.add(clBuilder.build());
899
900                 lsBuilder.setMpReachNlri(mpBuilder.build());
901
902                 // check path attributes
903                 final PathAttributes attrs = message.getPathAttributes();
904
905                 final PathAttributesBuilder paBuilder = new PathAttributesBuilder();
906
907                 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
908                 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
909
910                 paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
911                 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
912
913                 paBuilder.setLocalPref(new LocalPrefBuilder().setPref(100L).build());
914                 assertEquals(paBuilder.getLocalPref(), attrs.getLocalPref());
915
916                 final MpReachNlri mp = attrs.getAugmentation(PathAttributes1.class).getMpReachNlri();
917                 assertEquals(mpBuilder.getAfi(), mp.getAfi());
918                 assertEquals(mpBuilder.getSafi(), mp.getSafi());
919                 assertEquals(mpBuilder.getCNextHop(), mp.getCNextHop());
920
921                 final DestinationLinkstateBuilder dBuilder = new DestinationLinkstateBuilder();
922                 dBuilder.setCLinkstateDestination(linkstates);
923
924                 mpBuilder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(dBuilder.build()).build());
925                 lsBuilder.setMpReachNlri(mpBuilder.build());
926
927                 paBuilder.addAugmentation(PathAttributes1.class, lsBuilder.build());
928
929                 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.PathAttributes1Builder lsAttrBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.PathAttributes1Builder();
930
931                 lsAttrBuilder.setLinkstatePathAttribute(new LinkstatePathAttributeBuilder().setLinkStateAttribute(
932                                 new LinkAttributesBuilder().setMetric(new Metric(1L)).build()).build());
933                 paBuilder.addAugmentation(
934                                 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.PathAttributes1.class,
935                                 lsAttrBuilder.build());
936
937                 assertEquals(
938                                 lsAttrBuilder.build().getLinkstatePathAttribute(),
939                                 attrs.getAugmentation(
940                                                 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.PathAttributes1.class).getLinkstatePathAttribute());
941
942                 final List<CLinkstateDestination> dests = ((DestinationLinkstate) mp.getAdvertizedRoutes().getDestinationType()).getCLinkstateDestination();
943
944                 assertEquals(linkstates.size(), dests.size());
945
946                 assertEquals(linkstates, dests);
947                 // check API message
948                 builder.setPathAttributes(paBuilder.build());
949                 assertEquals(builder.build(), message);
950         }
951
952         /*
953          * TEST BGP Node
954          * 
955          *  00 00 <- withdrawn routes length
956                 00 b2 <- total path attribute length (178)
957                 90 <- attribute flags
958                 0e <- attribute type code (MP reach)
959                 00 a0 <- attribute extended length (160)
960                 40 04 <- AFI (16388 - Linkstate)
961                 47 <- SAFI (71 - Linkstate)
962                 04 <- next hop length
963                 19 19 19 01 - nexthop (25.25.25.1)
964                 00 <- reserved
965                 
966                 00 01 <- NLRI type (1 - nodeNLRI)
967                 00 31 <- NLRI length (49)
968                 03 <- ProtocolID - OSPF
969                 00 00 00 00 00 00 00 01 <- identifier
970                 01 00 <- local node descriptor type (256)
971                 00 24 <- length (36)
972                 02 00 <- node descriptor type (member AS - 512)
973                 00 04 <- length
974                 00 00 00 64 <- value (100)
975                 02 01 <- node descriptor type (bgpId - 513)
976                 00 04 <- length
977                 19 19 19 01 <- bgpId (25.25.25.1)
978                 02 02 <- node descriptor type (areaId - 514)
979                 00 04 <- length
980                 00 00 00 00 <- value
981                 02 03 <- node descriptor type (routeId - 515)
982                 00 08 <- length
983                 03 03 03 04 0b 0b 0b 03 <- OSPF Router Id
984
985                 00 01 <- NLRI type (1 - nodeNLRI)
986                 00 2d <- NLRI length (45)
987                 03 <- ProtocolID - OSPF
988                 00 00 00 00 00 00 00 01 <- identifier
989                 01 00 <- local node descriptor type (256)
990                 00 20 <- length (32)
991                 02 00 <- node descriptor type (member AS - 512)
992                 00 04 <- length
993                 00 00 00 64 <- value (100)
994                 02 01 <- node descriptor type (bgpId - 513)
995                 00 04 <- length
996                 19 19 19 01 <- bgpId (25.25.25.1)
997                 02 02 <- node descriptor type (areaId - 514)
998                 00 04 <- length
999                 00 00 00 00 <- value
1000                 02 03 <- node descriptor type (routeId - 515)
1001                 00 04 <- length
1002                 03 03 03 04 <- OSPF Router Id
1003
1004                 00 01 <- NLRI type (1 - nodeNLRI)
1005                 00 2d <- NLRI length (45)
1006                 03 <- ProtocolID - OSPF
1007                 00 00 00 00 00 00 00 01 <- identifier
1008                 01 00 <- local node descriptor type (256)
1009                 00 20 <- length (32)
1010                 02 00 <- node descriptor type (member AS - 512)
1011                 00 04 <- length
1012                 00 00 00 64 <- value (100)
1013                 02 01 <- node descriptor type (bgpId - 513)
1014                 00 04 <- length
1015                 19 19 19 01 <- bgpId (25.25.25.1)
1016                 02 02 <- node descriptor type (areaId - 514)
1017                 00 04 <- length
1018                 00 00 00 00 <- value
1019                 02 03 <- node descriptor type (routeId - 515)
1020                 00 04 <- length
1021                 01 01 01 02  <- OSPF Router Id
1022
1023                 40 <- attribute flags
1024                 01 <- attribute type (Origin)
1025                 01 <- attribute length
1026                 00 <- value (IGP)
1027                 40 <- attribute flags
1028                 02 <- attribute type (AS Path)
1029                 00 <- length
1030                 40 <- attribute flags
1031                 05 <- attribute type (local pref)
1032                 04 <- length
1033                 00 00 00 64 <- value
1034          */
1035         @Test
1036         public void testBGPNode() throws Exception {
1037                 final byte[] body = ByteArray.cutBytes(inputBytes.get(9), MessageUtil.COMMON_HEADER_LENGTH);
1038                 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(9), MessageUtil.MARKER_LENGTH,
1039                                 MessageUtil.LENGTH_FIELD_LENGTH));
1040                 final Update message = BGPParserTest.updateParser.parseMessageBody(body, messageLength);
1041
1042                 final UpdateBuilder builder = new UpdateBuilder();
1043
1044                 // check fields
1045
1046                 assertNull(message.getWithdrawnRoutes());
1047
1048                 // attributes
1049
1050                 final CIpv4NextHop nextHop = new CIpv4NextHopBuilder().setIpv4NextHop(
1051                                 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("25.25.25.1")).build()).build();
1052
1053                 final LocalNodeDescriptorsBuilder lndBuilder = new LocalNodeDescriptorsBuilder().setAsNumber(new AsNumber((long) 100)).setDomainId(
1054                                 new DomainIdentifier(new byte[] { (byte) 0x19, (byte) 0x19, (byte) 0x19, (byte) 0x01 })).setAreaId(
1055                                 new AreaIdentifier(new byte[] { 0, 0, 0, 0 }));
1056
1057                 final CLinkstateDestinationBuilder clBuilder = new CLinkstateDestinationBuilder();
1058                 clBuilder.setIdentifier(new Identifier(BigInteger.ONE));
1059                 clBuilder.setNlriType(NlriType.Node);
1060                 clBuilder.setProtocolId(ProtocolId.Ospf);
1061
1062                 final List<CLinkstateDestination> linkstates = Lists.newArrayList();
1063                 clBuilder.setLocalNodeDescriptors(lndBuilder.setCRouterIdentifier(
1064                                 new COspfPseudonodeBuilder().setOspfPseudonode(
1065                                                 new OspfPseudonodeBuilder().setOspfRouterId(new byte[] { 3, 3, 3, 4 }).setLanInterface(
1066                                                                 new OspfInterfaceIdentifier(new byte[] { 0x0b, 0x0b, 0x0b, 0x03 })).build()).build()).build());
1067                 linkstates.add(clBuilder.build());
1068
1069                 clBuilder.setLocalNodeDescriptors(lndBuilder.setCRouterIdentifier(
1070                                 new COspfNodeBuilder().setOspfNode(new OspfNodeBuilder().setOspfRouterId(new byte[] { 3, 3, 3, 4 }).build()).build()).build());
1071                 linkstates.add(clBuilder.build());
1072
1073                 clBuilder.setLocalNodeDescriptors(lndBuilder.setCRouterIdentifier(
1074                                 new COspfNodeBuilder().setOspfNode(new OspfNodeBuilder().setOspfRouterId(new byte[] { 1, 1, 1, 2 }).build()).build()).build());
1075                 linkstates.add(clBuilder.build());
1076
1077                 final PathAttributes1Builder lsBuilder = new PathAttributes1Builder();
1078                 final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
1079                 mpBuilder.setAfi(LinkstateAddressFamily.class);
1080                 mpBuilder.setSafi(LinkstateSubsequentAddressFamily.class);
1081                 mpBuilder.setCNextHop(nextHop);
1082
1083                 final DestinationLinkstateBuilder dBuilder = new DestinationLinkstateBuilder();
1084                 dBuilder.setCLinkstateDestination(linkstates);
1085
1086                 mpBuilder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(dBuilder.build()).build());
1087                 lsBuilder.setMpReachNlri(mpBuilder.build());
1088
1089                 final List<Segments> asPath = Lists.newArrayList();
1090
1091                 // check path attributes
1092                 final PathAttributes attrs = message.getPathAttributes();
1093
1094                 final PathAttributesBuilder paBuilder = new PathAttributesBuilder();
1095
1096                 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
1097                 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
1098
1099                 paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
1100                 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
1101
1102                 paBuilder.setLocalPref(new LocalPrefBuilder().setPref(100L).build());
1103                 assertEquals(paBuilder.getLocalPref(), attrs.getLocalPref());
1104
1105                 paBuilder.addAugmentation(PathAttributes1.class, lsBuilder.build());
1106
1107                 final MpReachNlri mp = attrs.getAugmentation(PathAttributes1.class).getMpReachNlri();
1108                 assertEquals(mpBuilder.getAfi(), mp.getAfi());
1109                 assertEquals(mpBuilder.getSafi(), mp.getSafi());
1110                 assertEquals(mpBuilder.getCNextHop(), mp.getCNextHop());
1111
1112                 final List<CLinkstateDestination> dests = ((DestinationLinkstate) mp.getAdvertizedRoutes().getDestinationType()).getCLinkstateDestination();
1113
1114                 assertEquals(linkstates.size(), dests.size());
1115
1116                 assertEquals(linkstates, dests);
1117
1118                 // check API message
1119                 builder.setPathAttributes(paBuilder.build());
1120                 assertEquals(builder.build(), message);
1121         }
1122
1123         /*
1124          * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
1125          * 00 98 <- length (69) - including header
1126          * 01 <- message type
1127          * 04 <- BGP version
1128          * 00 64 <- My AS Number (AS TRANS in this case)
1129          * 00 b4 <- Hold Time
1130          * 00 00 00 00 <- BGP Identifier
1131          * 28 <- Optional Parameters Length
1132          * 02 <- opt. param. type (capabilities)
1133          * 06 <- length
1134          * 01 <- capability code (MP Extensions for BGP4)
1135          * 04 <- length
1136          * 00 01 00 01 <- AFI 1, SAFI 1
1137          * 02 <- opt. param. type (capabilities)
1138          * 06 <- length
1139          * 01 <- capability code (MP Extensions for BGP4)
1140          * 04 <- length
1141          * 00 02 00 01 <- AFI 2, SAFI 1
1142          * 02 <- opt. param. type (capabilities)
1143          * 06 <- length
1144          * 01 <- capability code (MP Extensions for BGP4)
1145          * 04 <- length
1146          * 40 04 00 47 <- AFI 16388, SAFI 71
1147          * 02 <- opt. param. type (capabilities)
1148          * 02 <- length
1149          * 80 <- capability code (private)
1150          * 00 <- length
1151          * 02 <- opt. param. type (capabilities)
1152          * 02 <- length
1153          * 02 <- capability code (Route refresh)
1154          * 00 <- length
1155          * 02 <- opt. param. type (capabilities)
1156          * 06 <- length
1157          * 41 <- capability code (AS4 octet support)
1158          * 04 <- length
1159          * 00 00 00 64 <- AS number
1160          */
1161         @Test
1162         public void testOpenMessage() throws Exception {
1163                 final MessageRegistry msgReg = ServiceLoaderBGPExtensionProviderContext.createConsumerContext().getMessageRegistry();
1164                 final Open open = (Open) msgReg.parseMessage(inputBytes.get(13));
1165                 final Set<BgpTableType> types = Sets.newHashSet();
1166                 for (final BgpParameters param : open.getBgpParameters()) {
1167                         final CParameters p = param.getCParameters();
1168                         if (p instanceof CMultiprotocol) {
1169                                 final BgpTableType type = new BgpTableTypeImpl(((CMultiprotocol) p).getMultiprotocolCapability().getAfi(), ((CMultiprotocol) p).getMultiprotocolCapability().getSafi());
1170                                 types.add(type);
1171                         }
1172                 }
1173                 final Set<BgpTableType> expected = Sets.newHashSet();
1174                 expected.add(new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class));
1175                 expected.add(new BgpTableTypeImpl(Ipv6AddressFamily.class, UnicastSubsequentAddressFamily.class));
1176                 expected.add(new BgpTableTypeImpl(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class));
1177                 assertEquals(expected, types);
1178         }
1179 }