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