2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.linkstate;
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 import static org.mockito.Mockito.mock;
16 import com.google.common.collect.Lists;
17 import com.google.common.collect.Sets;
18 import io.netty.buffer.ByteBuf;
19 import io.netty.buffer.Unpooled;
20 import java.io.ByteArrayOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.math.BigInteger;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
31 import org.opendaylight.protocol.bgp.parser.impl.message.BGPUpdateMessageParser;
32 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
33 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
34 import org.opendaylight.protocol.bgp.parser.spi.NlriRegistry;
35 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
36 import org.opendaylight.protocol.util.ByteArray;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.AreaIdentifier;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.DomainIdentifier;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.Identifier;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.Ipv4InterfaceIdentifier;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.LinkstateAddressFamily;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.LinkstateSubsequentAddressFamily;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.OspfInterfaceIdentifier;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.ProtocolId;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.destination.CLinkstateDestination;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.destination.CLinkstateDestinationBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.LinkCaseBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.NodeCaseBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.link._case.LinkDescriptorsBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.link._case.LocalNodeDescriptorsBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.link._case.RemoteNodeDescriptorsBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.node._case.NodeDescriptorsBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.LinkAttributesCaseBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.link.attributes._case.LinkAttributesBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.node.identifier.c.router.identifier.OspfNodeCaseBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.node.identifier.c.router.identifier.OspfPseudonodeCaseBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.node.identifier.c.router.identifier.ospf.node._case.OspfNodeBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.node.identifier.c.router.identifier.ospf.pseudonode._case.OspfPseudonodeBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCase;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCaseBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.linkstate._case.DestinationLinkstateBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Open;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.UpdateBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.BgpParameters;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.OptionalCapabilities;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.optional.capabilities.CParameters;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.AsPathBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.LocalPrefBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.OriginBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1Builder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.BgpTableType;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.CParameters1;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.mp.capabilities.MultiprotocolCapability;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlri;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlriBuilder;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.BgpOrigin;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv6AddressFamily;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.SubsequentAddressFamily;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv4NextHopCase;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.ipv4.next.hop._case.Ipv4NextHopBuilder;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Metric;
94 import org.opendaylight.yangtools.yang.binding.Notification;
96 public class ParserTest {
99 * Used by other tests as well
101 private static final List<byte[]> inputBytes = new ArrayList<>();
103 private static final int COUNTER = 4;
105 private static final int MAX_SIZE = 300;
107 private static BGPUpdateMessageParser updateParser;
109 private static final int LENGTH_FIELD_LENGTH = 2;
112 public void setUp() throws Exception {
113 updateParser = new BGPUpdateMessageParser(ServiceLoaderBGPExtensionProviderContext
114 .getSingletonInstance().getAttributeRegistry(), mock(NlriRegistry.class));
115 for (int i = 1; i <= COUNTER; i++) {
116 final String name = "/up" + i + ".bin";
117 try (final InputStream is = ParserTest.class.getResourceAsStream(name)){
119 throw new IOException("Failed to get resource " + name);
121 final ByteArrayOutputStream bis = new ByteArrayOutputStream();
122 final byte[] data = new byte[MAX_SIZE];
124 while ((nRead = is.read(data, 0, data.length)) != -1) {
125 bis.write(data, 0, nRead);
129 inputBytes.add(bis.toByteArray());
137 public void testResource() {
138 assertNotNull(inputBytes);
142 * End of Rib for LS consists of empty MP_UNREACH_NLRI, with AFI 16388 and SAFI 71
144 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
145 * 00 1d <- length (29) - including header
147 * 00 00 <- withdrawn routes length
148 * 00 06 <- total path attribute length
149 * 80 <- attribute flags
150 * 0f <- attribute type (15 - MP_UNREACH_NLRI)
151 * 03 <- attribute length
152 * 40 04 <- value (AFI 16388: LS)
153 * 47 <- value (SAFI 71)
156 public void testEORLS() throws Exception {
157 final byte[] body = ByteArray.cutBytes(inputBytes.get(0), MessageUtil.COMMON_HEADER_LENGTH);
158 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(0),
159 MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
160 final Update message = ParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
163 final Class<? extends AddressFamily> afi = message.getAttributes()
164 .augmentation(Attributes2.class).getMpUnreachNlri().getAfi();
165 final Class<? extends SubsequentAddressFamily> safi = message.getAttributes()
166 .augmentation(Attributes2.class).getMpUnreachNlri().getSafi();
168 assertEquals(LinkstateAddressFamily.class, afi);
169 assertEquals(LinkstateSubsequentAddressFamily.class, safi);
171 final ByteBuf buffer = Unpooled.buffer();
172 ParserTest.updateParser.serializeMessage(message, buffer);
173 assertArrayEquals(inputBytes.get(0), ByteArray.readAllBytes(buffer));
177 * Tests BGP Link Ipv4
179 * 00 00 <- withdrawn routes length
180 * 01 48 <- total path attribute length (328)
181 * 90 <- attribute flags
182 0e <- attribute type code (MP reach)
183 01 2c <- attribute extended length (300)
184 40 04 <- AFI (16388 - Linkstate)
185 47 <- SAFI (71 - Linkstate)
186 04 <- next hop length
187 19 19 19 01 <- nexthop (25.25.25.1)
190 00 02 <- NLRI type (2 - linkNLRI)
191 00 5d <- NLRI length (93)
192 03 <- ProtocolID - OSPF
193 00 00 00 00 00 00 00 01 <- identifier
195 01 00 <- local node descriptor type (256)
197 02 00 <- node descriptor type (member AS - 512)
199 00 00 00 64 <- value (100)
200 02 01 <- node descriptor type (bgpId - 513)
202 19 19 19 01 <- bgpId (25.25.25.1)
203 02 02 <- node descriptor type (areaId - 514)
206 02 03 <- node descriptor type (routeId - 515)
208 03 03 03 04 0b 0b 0b 03 <- OSPF Router Id
210 01 01 <- remote node descriptor type (257)
212 02 00 <- node descriptor type (member AS - 512)
214 00 00 00 64 <- value (100)
215 02 01 <- node descriptor type (bgpId - 513)
217 19 19 19 01 <- bgpId (25.25.25.1)
218 02 02 <- node descriptor type (areaId - 514)
221 02 03 <- node descriptor type (routeId - 515)
223 03 03 03 04 <- OSPF Router Id
225 01 03 <- link descriptor type (IPv4 interface address - 259)
227 0b 0b 0b 03 <- value (11.11.11.3)
229 00 02 <- NLRI type (2 - linkNLRI)
230 00 5d <- NLRI length (93)
231 03 <- ProtocolID - OSPF
232 00 00 00 00 00 00 00 01 <- identifier
234 01 00 <- local node descriptor type (256)
236 02 00 <- node descriptor type (member AS - 512)
238 00 00 00 64 <- value (100)
239 02 01 <- node descriptor type (bgpId - 513)
241 19 19 19 01 <- bgpId (25.25.25.1)
242 02 02 <- node descriptor type (areaId - 514)
245 02 03 <- node descriptor type (routeId - 515)
247 03 03 03 04 0b 0b 0b 03 <- OSPF Router Id
249 01 01 <- remote node descriptor type (257)
251 02 00 <- node descriptor type (member AS - 512)
253 00 00 00 64 <- value (100)
254 02 01 <- node descriptor type (bgpId - 513)
256 19 19 19 01 <- bgpId (25.25.25.1)
257 02 02 <- node descriptor type (areaId - 514)
260 02 03 <- node descriptor type (routeId - 515)
262 01 01 01 02 <- OSPF Router Id
264 01 03 <- link descriptor type (IPv4 interface address - 259)
266 0b 0b 0b 01 <- value (11.11.11.1)
268 00 02 <- NLRI type (2 - linkNLRI)
269 00 5d <- NLRI length (93)
270 03 <- ProtocolID - OSPF
271 00 00 00 00 00 00 00 01 <- identifier
273 01 00 <- local node descriptor type (256)
275 02 00 <- node descriptor type (member AS - 512)
277 00 00 00 64 <- value (100)
278 02 01 <- node descriptor type (bgpId - 513)
280 19 19 19 01 <- bgpId (25.25.25.1)
281 02 02 <- node descriptor type (areaId - 514)
284 02 03 <- node descriptor type (routeId - 515)
286 01 01 01 02 <- OSPF Router Id
288 01 01 <- remote node descriptor type (257)
290 02 00 <- node descriptor type (member AS - 512)
292 00 00 00 64 <- value (100)
293 02 01 <- node descriptor type (bgpId - 513)
295 19 19 19 01 <- bgpId (25.25.25.1)
296 02 02 <- node descriptor type (areaId - 514)
299 02 03 <- node descriptor type (routeId - 515)
301 03 03 03 04 0b 0b 0b 03 <- OSPF Router Id
303 01 03 <- link descriptor type (IPv4 interface address - 259)
305 0b 0b 0b 01 <- value (11.11.11.1)
307 40 <- attribute flags
308 01 <- attribute type (Origin)
309 01 <- attribute length
311 40 <- attribute flags
312 02 <- attribute type (AS Path)
314 40 <- attribute flags
315 05 <- attribute type (local pref)
318 c0 <- attribute flags
319 1D <- attribute type (Link STATE - 29)
321 04 47 <- link attribute (1095 - Metric)
326 public void testBGPLink() throws Exception {
327 final byte[] body = ByteArray.cutBytes(inputBytes.get(1), MessageUtil.COMMON_HEADER_LENGTH);
328 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(1),
329 MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
330 final Update message = ParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
333 final UpdateBuilder builder = new UpdateBuilder();
337 assertNull(message.getWithdrawnRoutes());
339 final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
340 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("25.25.25.1")).build()).build();
342 final LocalNodeDescriptorsBuilder ndBuilder = new LocalNodeDescriptorsBuilder()
343 .setAsNumber(new AsNumber((long) 100)).setDomainId(
344 new DomainIdentifier(0x19191901L)).setAreaId(new AreaIdentifier(0L));
346 final RemoteNodeDescriptorsBuilder rdBuilder = new RemoteNodeDescriptorsBuilder()
347 .setAsNumber(new AsNumber((long) 100)).setDomainId(
348 new DomainIdentifier(0x19191901L)).setAreaId(new AreaIdentifier(0L));
350 final CLinkstateDestinationBuilder clBuilder = new CLinkstateDestinationBuilder();
351 clBuilder.setIdentifier(new Identifier(BigInteger.ONE));
352 clBuilder.setProtocolId(ProtocolId.Ospf);
354 final Attributes1Builder lsBuilder = new Attributes1Builder();
355 final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
356 mpBuilder.setAfi(LinkstateAddressFamily.class);
357 mpBuilder.setSafi(LinkstateSubsequentAddressFamily.class);
358 mpBuilder.setCNextHop(nextHop);
360 final List<CLinkstateDestination> linkstates = Lists.newArrayList();
361 final LinkCaseBuilder lCase = new LinkCaseBuilder().setLocalNodeDescriptors(ndBuilder.setCRouterIdentifier(
362 new OspfPseudonodeCaseBuilder().setOspfPseudonode(
363 new OspfPseudonodeBuilder().setOspfRouterId(0x03030304L)
364 .setLanInterface(new OspfInterfaceIdentifier(0x0b0b0b03L)).build()).build()).build());
365 lCase.setRemoteNodeDescriptors(rdBuilder.setCRouterIdentifier(
366 new OspfNodeCaseBuilder().setOspfNode(new OspfNodeBuilder()
367 .setOspfRouterId(0x03030304L).build()).build()).build());
368 lCase.setLinkDescriptors(new LinkDescriptorsBuilder().setIpv4InterfaceAddress(
369 new Ipv4InterfaceIdentifier(new Ipv4Address("11.11.11.3"))).build());
370 linkstates.add(clBuilder.setObjectType(lCase.build()).build());
372 lCase.setRemoteNodeDescriptors(rdBuilder.setCRouterIdentifier(
373 new OspfNodeCaseBuilder().setOspfNode(new OspfNodeBuilder()
374 .setOspfRouterId(0x01010102L).build()).build()).build());
375 lCase.setLinkDescriptors(new LinkDescriptorsBuilder().setIpv4InterfaceAddress(
376 new Ipv4InterfaceIdentifier(new Ipv4Address("11.11.11.1"))).build());
377 linkstates.add(clBuilder.setObjectType(lCase.build()).build());
379 lCase.setLocalNodeDescriptors(ndBuilder.setCRouterIdentifier(
380 new OspfNodeCaseBuilder().setOspfNode(new OspfNodeBuilder()
381 .setOspfRouterId(0x01010102L).build()).build()).build());
382 lCase.setRemoteNodeDescriptors(rdBuilder.setCRouterIdentifier(
383 new OspfPseudonodeCaseBuilder().setOspfPseudonode(new OspfPseudonodeBuilder()
384 .setOspfRouterId(0x03030304L)
385 .setLanInterface(new OspfInterfaceIdentifier(0x0b0b0b03L)).build()).build()).build());
386 linkstates.add(clBuilder.setObjectType(lCase.build()).build());
388 lsBuilder.setMpReachNlri(mpBuilder.build());
390 // check path attributes
391 final Attributes attrs = message.getAttributes();
393 final AttributesBuilder paBuilder = new AttributesBuilder();
395 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
396 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
398 paBuilder.setAsPath(new AsPathBuilder().setSegments(Collections.emptyList()).build());
399 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
401 paBuilder.setLocalPref(new LocalPrefBuilder().setPref(100L).build());
402 assertEquals(paBuilder.getLocalPref(), attrs.getLocalPref());
404 final MpReachNlri mp = attrs.augmentation(Attributes1.class).getMpReachNlri();
405 assertEquals(mpBuilder.getAfi(), mp.getAfi());
406 assertEquals(mpBuilder.getSafi(), mp.getSafi());
407 assertEquals(mpBuilder.getCNextHop(), mp.getCNextHop());
409 final DestinationLinkstateBuilder dBuilder = new DestinationLinkstateBuilder();
410 dBuilder.setCLinkstateDestination(linkstates);
412 mpBuilder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
413 new DestinationLinkstateCaseBuilder().setDestinationLinkstate(dBuilder.build()).build()).build());
414 lsBuilder.setMpReachNlri(mpBuilder.build());
416 paBuilder.addAugmentation(Attributes1.class, lsBuilder.build());
418 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.
419 Attributes1Builder lsAttrBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.
420 bgp.linkstate.rev180329.Attributes1Builder();
422 lsAttrBuilder.setLinkStateAttribute(
423 new LinkAttributesCaseBuilder().setLinkAttributes(new LinkAttributesBuilder()
424 .setMetric(new Metric(1L)).build()).build());
425 paBuilder.addAugmentation(
426 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.Attributes1.class,
427 lsAttrBuilder.build());
428 paBuilder.setUnrecognizedAttributes(Collections.emptyList());
431 lsAttrBuilder.build(),
432 attrs.augmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp
433 .linkstate.rev180329.Attributes1.class));
435 final List<CLinkstateDestination> dests = ((DestinationLinkstateCase) mp.getAdvertizedRoutes()
436 .getDestinationType()).getDestinationLinkstate().getCLinkstateDestination();
438 assertEquals(linkstates.size(), dests.size());
440 assertEquals(linkstates, dests);
442 builder.setAttributes(paBuilder.build());
443 assertEquals(builder.build(), message);
445 final ByteBuf buffer = Unpooled.buffer();
446 ParserTest.updateParser.serializeMessage(message, buffer);
447 assertArrayEquals(inputBytes.get(1), ByteArray.readAllBytes(buffer));
453 * 00 00 <- withdrawn routes length
454 00 b1 <- total path attribute length (177)
455 80 <- attribute flags
456 0e <- attribute type code (MP reach)
457 a0 <- attribute length (160)
458 40 04 <- AFI (16388 - Linkstate)
459 47 <- SAFI (71 - Linkstate)
460 04 <- next hop length
461 19 19 19 01 - nexthop (25.25.25.1)
464 00 01 <- NLRI type (1 - nodeNLRI)
465 00 31 <- NLRI length (49)
466 03 <- ProtocolID - OSPF
467 00 00 00 00 00 00 00 01 <- identifier
468 01 00 <- local node descriptor type (256)
470 02 00 <- node descriptor type (member AS - 512)
472 00 00 00 64 <- value (100)
473 02 01 <- node descriptor type (bgpId - 513)
475 19 19 19 01 <- bgpId (25.25.25.1)
476 02 02 <- node descriptor type (areaId - 514)
479 02 03 <- node descriptor type (routeId - 515)
481 03 03 03 04 0b 0b 0b 03 <- OSPF Router Id
483 00 01 <- NLRI type (1 - nodeNLRI)
484 00 2d <- NLRI length (45)
485 03 <- ProtocolID - OSPF
486 00 00 00 00 00 00 00 01 <- identifier
487 01 00 <- local node descriptor type (256)
489 02 00 <- node descriptor type (member AS - 512)
491 00 00 00 64 <- value (100)
492 02 01 <- node descriptor type (bgpId - 513)
494 19 19 19 01 <- bgpId (25.25.25.1)
495 02 02 <- node descriptor type (areaId - 514)
498 02 03 <- node descriptor type (routeId - 515)
500 03 03 03 04 <- OSPF Router Id
502 00 01 <- NLRI type (1 - nodeNLRI)
503 00 2d <- NLRI length (45)
504 03 <- ProtocolID - OSPF
505 00 00 00 00 00 00 00 01 <- identifier
506 01 00 <- local node descriptor type (256)
508 02 00 <- node descriptor type (member AS - 512)
510 00 00 00 64 <- value (100)
511 02 01 <- node descriptor type (bgpId - 513)
513 19 19 19 01 <- bgpId (25.25.25.1)
514 02 02 <- node descriptor type (areaId - 514)
517 02 03 <- node descriptor type (routeId - 515)
519 01 01 01 02 <- OSPF Router Id
521 40 <- attribute flags
522 01 <- attribute type (Origin)
523 01 <- attribute length
525 40 <- attribute flags
526 02 <- attribute type (AS Path)
528 40 <- attribute flags
529 05 <- attribute type (local pref)
534 public void testBGPNode() throws Exception {
535 final byte[] body = ByteArray.cutBytes(inputBytes.get(2), MessageUtil.COMMON_HEADER_LENGTH);
536 final int messageLength = ByteArray
537 .bytesToInt(ByteArray.subByte(inputBytes.get(2), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
538 final Update message = ParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
541 final UpdateBuilder builder = new UpdateBuilder();
545 assertNull(message.getWithdrawnRoutes());
549 final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
550 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("25.25.25.1")).build()).build();
552 final CLinkstateDestinationBuilder clBuilder = new CLinkstateDestinationBuilder();
553 clBuilder.setIdentifier(new Identifier(BigInteger.ONE));
554 clBuilder.setProtocolId(ProtocolId.Ospf);
556 final NodeDescriptorsBuilder n = new NodeDescriptorsBuilder();
557 n.setAsNumber(new AsNumber((long) 100)).setDomainId(new DomainIdentifier(0x19191901L))
558 .setAreaId(new AreaIdentifier(0L));
560 final List<CLinkstateDestination> linkstates = Lists.newArrayList();
561 final NodeCaseBuilder nCase = new NodeCaseBuilder();
562 nCase.setNodeDescriptors(n.setCRouterIdentifier(
563 new OspfPseudonodeCaseBuilder().setOspfPseudonode(
564 new OspfPseudonodeBuilder().setOspfRouterId(0x03030304L)
565 .setLanInterface(new OspfInterfaceIdentifier(0x0b0b0b03L)).build()).build()).build());
566 linkstates.add(clBuilder.setObjectType(nCase.build()).build());
568 nCase.setNodeDescriptors(n.setCRouterIdentifier(
569 new OspfNodeCaseBuilder().setOspfNode(new OspfNodeBuilder()
570 .setOspfRouterId(0x03030304L).build()).build()).build());
571 linkstates.add(clBuilder.setObjectType(nCase.build()).build());
573 nCase.setNodeDescriptors(n.setCRouterIdentifier(
574 new OspfNodeCaseBuilder().setOspfNode(new OspfNodeBuilder()
575 .setOspfRouterId(0x01010102L).build()).build()).build());
576 linkstates.add(clBuilder.setObjectType(nCase.build()).build());
578 final Attributes1Builder lsBuilder = new Attributes1Builder();
579 final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
580 mpBuilder.setAfi(LinkstateAddressFamily.class);
581 mpBuilder.setSafi(LinkstateSubsequentAddressFamily.class);
582 mpBuilder.setCNextHop(nextHop);
584 final DestinationLinkstateBuilder dBuilder = new DestinationLinkstateBuilder();
585 dBuilder.setCLinkstateDestination(linkstates);
587 mpBuilder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
588 new DestinationLinkstateCaseBuilder().setDestinationLinkstate(dBuilder.build()).build()).build());
589 lsBuilder.setMpReachNlri(mpBuilder.build());
591 // check path attributes
592 final Attributes attrs = message.getAttributes();
594 final AttributesBuilder paBuilder = new AttributesBuilder();
596 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
597 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
599 paBuilder.setAsPath(new AsPathBuilder().setSegments(Collections.emptyList()).build());
600 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
602 paBuilder.setLocalPref(new LocalPrefBuilder().setPref(100L).build());
603 assertEquals(paBuilder.getLocalPref(), attrs.getLocalPref());
605 paBuilder.addAugmentation(Attributes1.class, lsBuilder.build());
606 paBuilder.setUnrecognizedAttributes(Collections.emptyList());
608 final MpReachNlri mp = attrs.augmentation(Attributes1.class).getMpReachNlri();
609 assertEquals(mpBuilder.getAfi(), mp.getAfi());
610 assertEquals(mpBuilder.getSafi(), mp.getSafi());
611 assertEquals(mpBuilder.getCNextHop(), mp.getCNextHop());
613 final List<CLinkstateDestination> dests = ((DestinationLinkstateCase) mp.getAdvertizedRoutes()
614 .getDestinationType()).getDestinationLinkstate().getCLinkstateDestination();
616 assertEquals(linkstates.size(), dests.size());
618 assertEquals(linkstates, dests);
621 builder.setAttributes(paBuilder.build());
622 assertEquals(builder.build(), message);
624 final ByteBuf buffer = Unpooled.buffer();
625 ParserTest.updateParser.serializeMessage(message, buffer);
626 assertArrayEquals(inputBytes.get(2), ByteArray.readAllBytes(buffer));
630 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
631 * 00 3d <- length (61) - including header
634 * 00 64 <- My AS Number (AS TRANS in this case)
636 * 00 00 00 00 <- BGP Identifier
637 * 20 <- Optional Parameters Length
638 * 02 <- opt. param. type (capabilities)
640 * 01 <- capability code (MP Extensions for BGP4)
642 * 00 01 00 01 <- AFI 1, SAFI 1
643 * 02 <- opt. param. type (capabilities)
645 * 01 <- capability code (MP Extensions for BGP4)
647 * 00 02 00 01 <- AFI 2, SAFI 1
648 * 02 <- opt. param. type (capabilities)
650 * 01 <- capability code (MP Extensions for BGP4)
652 * 40 04 00 47 <- AFI 16388, SAFI 71
653 * 02 <- opt. param. type (capabilities)
655 * 41 <- capability code (AS4 octet support)
657 * 00 00 00 64 <- AS number
660 public void testOpenMessage() throws Exception {
661 final MessageRegistry msgReg = ServiceLoaderBGPExtensionProviderContext
662 .getSingletonInstance().getMessageRegistry();
663 final Notification o = msgReg.parseMessage(Unpooled.copiedBuffer(inputBytes.get(3)), null);
664 final Open open = (Open) o;
665 final Set<BgpTableType> types = Sets.newHashSet();
666 for (final BgpParameters param : open.getBgpParameters()) {
667 for (final OptionalCapabilities optCapa : param.getOptionalCapabilities()) {
668 final CParameters cParam = optCapa.getCParameters();
669 if(cParam != null && cParam.augmentation(CParameters1.class) != null
670 && cParam.augmentation(CParameters1.class).getMultiprotocolCapability() != null) {
671 final MultiprotocolCapability mp = cParam.augmentation(CParameters1.class)
672 .getMultiprotocolCapability();
673 final BgpTableType type = new BgpTableTypeImpl(mp.getAfi(), mp.getSafi());
678 final Set<BgpTableType> expected = Sets.newHashSet();
679 expected.add(new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class));
680 expected.add(new BgpTableTypeImpl(Ipv6AddressFamily.class, UnicastSubsequentAddressFamily.class));
681 expected.add(new BgpTableTypeImpl(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class));
682 assertEquals(expected, types);
684 final ByteBuf buffer = Unpooled.buffer();
685 msgReg.serializeMessage(o, buffer);
686 assertArrayEquals(inputBytes.get(3), ByteArray.readAllBytes(buffer));