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