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