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;
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.Collections;
25 import java.util.List;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
30 import org.opendaylight.protocol.bgp.parser.impl.message.BGPUpdateMessageParser;
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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.AreaIdentifier;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.DomainIdentifier;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Identifier;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Ipv4InterfaceIdentifier;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateAddressFamily;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateSubsequentAddressFamily;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.OspfInterfaceIdentifier;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.ProtocolId;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.destination.CLinkstateDestination;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.destination.CLinkstateDestinationBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.LinkCaseBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.NodeCaseBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.link._case.LinkDescriptorsBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.link._case.LocalNodeDescriptorsBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.link._case.RemoteNodeDescriptorsBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.node._case.NodeDescriptorsBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.LinkAttributesCaseBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.link.attributes._case.LinkAttributesBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.OspfNodeCaseBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.OspfPseudonodeCaseBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.ospf.node._case.OspfNodeBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.ospf.pseudonode._case.OspfPseudonodeBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCase;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCaseBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.linkstate._case.DestinationLinkstateBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.UpdateBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilities;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.CParameters;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.AsPathBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.LocalPrefBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.OriginBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1Builder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.MultiprotocolCapability;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlri;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlriBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpOrigin;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCase;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
90 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;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Metric;
92 import org.opendaylight.yangtools.yang.binding.Notification;
94 public class ParserTest {
97 * Used by other tests as well
99 private static final List<byte[]> inputBytes = new ArrayList<>();
101 private static final int COUNTER = 4;
103 private static final int MAX_SIZE = 300;
105 private static BGPUpdateMessageParser updateParser;
107 private static final int LENGTH_FIELD_LENGTH = 2;
110 public void setUp() throws Exception {
111 updateParser = new BGPUpdateMessageParser(ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry());
112 for (int i = 1; i <= COUNTER; i++) {
113 final String name = "/up" + i + ".bin";
114 try (final InputStream is = ParserTest.class.getResourceAsStream(name)){
116 throw new IOException("Failed to get resource " + name);
118 final ByteArrayOutputStream bis = new ByteArrayOutputStream();
119 final byte[] data = new byte[MAX_SIZE];
121 while ((nRead = is.read(data, 0, data.length)) != -1) {
122 bis.write(data, 0, nRead);
126 inputBytes.add(bis.toByteArray());
134 public void testResource() {
135 assertNotNull(inputBytes);
139 * End of Rib for LS consists of empty MP_UNREACH_NLRI, with AFI 16388 and SAFI 71
141 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
142 * 00 1d <- length (29) - including header
144 * 00 00 <- withdrawn routes length
145 * 00 06 <- total path attribute length
146 * 80 <- attribute flags
147 * 0f <- attribute type (15 - MP_UNREACH_NLRI)
148 * 03 <- attribute length
149 * 40 04 <- value (AFI 16388: LS)
150 * 47 <- value (SAFI 71)
153 public void testEORLS() throws Exception {
154 final byte[] body = ByteArray.cutBytes(inputBytes.get(0), MessageUtil.COMMON_HEADER_LENGTH);
155 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(0), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
156 final Update message = ParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
158 final Class<? extends AddressFamily> afi = message.getAttributes().getAugmentation(Attributes2.class).getMpUnreachNlri().getAfi();
159 final Class<? extends SubsequentAddressFamily> safi = message.getAttributes().getAugmentation(Attributes2.class).getMpUnreachNlri().getSafi();
161 assertEquals(LinkstateAddressFamily.class, afi);
162 assertEquals(LinkstateSubsequentAddressFamily.class, safi);
164 final ByteBuf buffer = Unpooled.buffer();
165 ParserTest.updateParser.serializeMessage(message, buffer);
166 assertArrayEquals(inputBytes.get(0), ByteArray.readAllBytes(buffer));
170 * Tests BGP Link Ipv4
172 * 00 00 <- withdrawn routes length
173 * 01 48 <- total path attribute length (328)
174 * 90 <- attribute flags
175 0e <- attribute type code (MP reach)
176 01 2c <- attribute extended length (300)
177 40 04 <- AFI (16388 - Linkstate)
178 47 <- SAFI (71 - Linkstate)
179 04 <- next hop length
180 19 19 19 01 <- nexthop (25.25.25.1)
183 00 02 <- NLRI type (2 - linkNLRI)
184 00 5d <- NLRI length (93)
185 03 <- ProtocolID - OSPF
186 00 00 00 00 00 00 00 01 <- identifier
188 01 00 <- local node descriptor type (256)
190 02 00 <- node descriptor type (member AS - 512)
192 00 00 00 64 <- value (100)
193 02 01 <- node descriptor type (bgpId - 513)
195 19 19 19 01 <- bgpId (25.25.25.1)
196 02 02 <- node descriptor type (areaId - 514)
199 02 03 <- node descriptor type (routeId - 515)
201 03 03 03 04 0b 0b 0b 03 <- OSPF Router Id
203 01 01 <- remote node descriptor type (257)
205 02 00 <- node descriptor type (member AS - 512)
207 00 00 00 64 <- value (100)
208 02 01 <- node descriptor type (bgpId - 513)
210 19 19 19 01 <- bgpId (25.25.25.1)
211 02 02 <- node descriptor type (areaId - 514)
214 02 03 <- node descriptor type (routeId - 515)
216 03 03 03 04 <- OSPF Router Id
218 01 03 <- link descriptor type (IPv4 interface address - 259)
220 0b 0b 0b 03 <- value (11.11.11.3)
222 00 02 <- NLRI type (2 - linkNLRI)
223 00 5d <- NLRI length (93)
224 03 <- ProtocolID - OSPF
225 00 00 00 00 00 00 00 01 <- identifier
227 01 00 <- local node descriptor type (256)
229 02 00 <- node descriptor type (member AS - 512)
231 00 00 00 64 <- value (100)
232 02 01 <- node descriptor type (bgpId - 513)
234 19 19 19 01 <- bgpId (25.25.25.1)
235 02 02 <- node descriptor type (areaId - 514)
238 02 03 <- node descriptor type (routeId - 515)
240 03 03 03 04 0b 0b 0b 03 <- OSPF Router Id
242 01 01 <- remote node descriptor type (257)
244 02 00 <- node descriptor type (member AS - 512)
246 00 00 00 64 <- value (100)
247 02 01 <- node descriptor type (bgpId - 513)
249 19 19 19 01 <- bgpId (25.25.25.1)
250 02 02 <- node descriptor type (areaId - 514)
253 02 03 <- node descriptor type (routeId - 515)
255 01 01 01 02 <- OSPF Router Id
257 01 03 <- link descriptor type (IPv4 interface address - 259)
259 0b 0b 0b 01 <- value (11.11.11.1)
261 00 02 <- NLRI type (2 - linkNLRI)
262 00 5d <- NLRI length (93)
263 03 <- ProtocolID - OSPF
264 00 00 00 00 00 00 00 01 <- identifier
266 01 00 <- local node descriptor type (256)
268 02 00 <- node descriptor type (member AS - 512)
270 00 00 00 64 <- value (100)
271 02 01 <- node descriptor type (bgpId - 513)
273 19 19 19 01 <- bgpId (25.25.25.1)
274 02 02 <- node descriptor type (areaId - 514)
277 02 03 <- node descriptor type (routeId - 515)
279 01 01 01 02 <- OSPF Router Id
281 01 01 <- remote node descriptor type (257)
283 02 00 <- node descriptor type (member AS - 512)
285 00 00 00 64 <- value (100)
286 02 01 <- node descriptor type (bgpId - 513)
288 19 19 19 01 <- bgpId (25.25.25.1)
289 02 02 <- node descriptor type (areaId - 514)
292 02 03 <- node descriptor type (routeId - 515)
294 03 03 03 04 0b 0b 0b 03 <- OSPF Router Id
296 01 03 <- link descriptor type (IPv4 interface address - 259)
298 0b 0b 0b 01 <- value (11.11.11.1)
300 40 <- attribute flags
301 01 <- attribute type (Origin)
302 01 <- attribute length
304 40 <- attribute flags
305 02 <- attribute type (AS Path)
307 40 <- attribute flags
308 05 <- attribute type (local pref)
311 c0 <- attribute flags
312 1D <- attribute type (Link STATE - 29)
314 04 47 <- link attribute (1095 - Metric)
319 public void testBGPLink() throws Exception {
320 final byte[] body = ByteArray.cutBytes(inputBytes.get(1), MessageUtil.COMMON_HEADER_LENGTH);
321 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(1), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
322 final Update message = ParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
324 final UpdateBuilder builder = new UpdateBuilder();
328 assertNull(message.getWithdrawnRoutes());
330 final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
331 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("25.25.25.1")).build()).build();
333 final LocalNodeDescriptorsBuilder ndBuilder = new LocalNodeDescriptorsBuilder().setAsNumber(new AsNumber((long) 100)).setDomainId(
334 new DomainIdentifier(0x19191901L)).setAreaId(new AreaIdentifier(0L));
336 final RemoteNodeDescriptorsBuilder rdBuilder = new RemoteNodeDescriptorsBuilder().setAsNumber(new AsNumber((long) 100)).setDomainId(
337 new DomainIdentifier(0x19191901L)).setAreaId(new AreaIdentifier(0L));
339 final CLinkstateDestinationBuilder clBuilder = new CLinkstateDestinationBuilder();
340 clBuilder.setIdentifier(new Identifier(BigInteger.ONE));
341 clBuilder.setProtocolId(ProtocolId.Ospf);
343 final Attributes1Builder lsBuilder = new Attributes1Builder();
344 final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
345 mpBuilder.setAfi(LinkstateAddressFamily.class);
346 mpBuilder.setSafi(LinkstateSubsequentAddressFamily.class);
347 mpBuilder.setCNextHop(nextHop);
349 final List<CLinkstateDestination> linkstates = Lists.newArrayList();
350 final LinkCaseBuilder lCase = new LinkCaseBuilder().setLocalNodeDescriptors(ndBuilder.setCRouterIdentifier(
351 new OspfPseudonodeCaseBuilder().setOspfPseudonode(
352 new OspfPseudonodeBuilder().setOspfRouterId(0x03030304L).setLanInterface(new OspfInterfaceIdentifier(0x0b0b0b03L)).build()).build()).build());
353 lCase.setRemoteNodeDescriptors(rdBuilder.setCRouterIdentifier(
354 new OspfNodeCaseBuilder().setOspfNode(new OspfNodeBuilder().setOspfRouterId(0x03030304L).build()).build()).build());
355 lCase.setLinkDescriptors(new LinkDescriptorsBuilder().setIpv4InterfaceAddress(
356 new Ipv4InterfaceIdentifier(new Ipv4Address("11.11.11.3"))).build());
357 linkstates.add(clBuilder.setObjectType(lCase.build()).build());
359 lCase.setRemoteNodeDescriptors(rdBuilder.setCRouterIdentifier(
360 new OspfNodeCaseBuilder().setOspfNode(new OspfNodeBuilder().setOspfRouterId(0x01010102L).build()).build()).build());
361 lCase.setLinkDescriptors(new LinkDescriptorsBuilder().setIpv4InterfaceAddress(
362 new Ipv4InterfaceIdentifier(new Ipv4Address("11.11.11.1"))).build());
363 linkstates.add(clBuilder.setObjectType(lCase.build()).build());
365 lCase.setLocalNodeDescriptors(ndBuilder.setCRouterIdentifier(
366 new OspfNodeCaseBuilder().setOspfNode(new OspfNodeBuilder().setOspfRouterId(0x01010102L).build()).build()).build());
367 lCase.setRemoteNodeDescriptors(rdBuilder.setCRouterIdentifier(
368 new OspfPseudonodeCaseBuilder().setOspfPseudonode(new OspfPseudonodeBuilder().setOspfRouterId(0x03030304L).setLanInterface(new OspfInterfaceIdentifier(0x0b0b0b03L)).build()).build()).build());
369 linkstates.add(clBuilder.setObjectType(lCase.build()).build());
371 lsBuilder.setMpReachNlri(mpBuilder.build());
373 // check path attributes
374 final Attributes attrs = message.getAttributes();
376 final AttributesBuilder paBuilder = new AttributesBuilder();
378 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
379 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
381 paBuilder.setAsPath(new AsPathBuilder().setSegments(Collections.emptyList()).build());
382 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
384 paBuilder.setLocalPref(new LocalPrefBuilder().setPref(100L).build());
385 assertEquals(paBuilder.getLocalPref(), attrs.getLocalPref());
387 final MpReachNlri mp = attrs.getAugmentation(Attributes1.class).getMpReachNlri();
388 assertEquals(mpBuilder.getAfi(), mp.getAfi());
389 assertEquals(mpBuilder.getSafi(), mp.getSafi());
390 assertEquals(mpBuilder.getCNextHop(), mp.getCNextHop());
392 final DestinationLinkstateBuilder dBuilder = new DestinationLinkstateBuilder();
393 dBuilder.setCLinkstateDestination(linkstates);
395 mpBuilder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
396 new DestinationLinkstateCaseBuilder().setDestinationLinkstate(dBuilder.build()).build()).build());
397 lsBuilder.setMpReachNlri(mpBuilder.build());
399 paBuilder.addAugmentation(Attributes1.class, lsBuilder.build());
401 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Attributes1Builder lsAttrBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Attributes1Builder();
403 lsAttrBuilder.setLinkStateAttribute(
404 new LinkAttributesCaseBuilder().setLinkAttributes(new LinkAttributesBuilder().setMetric(new Metric(1L)).build()).build());
405 paBuilder.addAugmentation(
406 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Attributes1.class,
407 lsAttrBuilder.build());
408 paBuilder.setUnrecognizedAttributes(Collections.emptyList());
411 lsAttrBuilder.build(),
412 attrs.getAugmentation(
413 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Attributes1.class));
415 final List<CLinkstateDestination> dests = ((DestinationLinkstateCase) mp.getAdvertizedRoutes().getDestinationType()).getDestinationLinkstate().getCLinkstateDestination();
417 assertEquals(linkstates.size(), dests.size());
419 assertEquals(linkstates, dests);
421 builder.setAttributes(paBuilder.build());
422 assertEquals(builder.build(), message);
424 final ByteBuf buffer = Unpooled.buffer();
425 ParserTest.updateParser.serializeMessage(message, buffer);
426 assertArrayEquals(inputBytes.get(1), ByteArray.readAllBytes(buffer));
432 * 00 00 <- withdrawn routes length
433 00 b1 <- total path attribute length (177)
434 80 <- attribute flags
435 0e <- attribute type code (MP reach)
436 a0 <- attribute length (160)
437 40 04 <- AFI (16388 - Linkstate)
438 47 <- SAFI (71 - Linkstate)
439 04 <- next hop length
440 19 19 19 01 - nexthop (25.25.25.1)
443 00 01 <- NLRI type (1 - nodeNLRI)
444 00 31 <- NLRI length (49)
445 03 <- ProtocolID - OSPF
446 00 00 00 00 00 00 00 01 <- identifier
447 01 00 <- local node descriptor type (256)
449 02 00 <- node descriptor type (member AS - 512)
451 00 00 00 64 <- value (100)
452 02 01 <- node descriptor type (bgpId - 513)
454 19 19 19 01 <- bgpId (25.25.25.1)
455 02 02 <- node descriptor type (areaId - 514)
458 02 03 <- node descriptor type (routeId - 515)
460 03 03 03 04 0b 0b 0b 03 <- OSPF Router Id
462 00 01 <- NLRI type (1 - nodeNLRI)
463 00 2d <- NLRI length (45)
464 03 <- ProtocolID - OSPF
465 00 00 00 00 00 00 00 01 <- identifier
466 01 00 <- local node descriptor type (256)
468 02 00 <- node descriptor type (member AS - 512)
470 00 00 00 64 <- value (100)
471 02 01 <- node descriptor type (bgpId - 513)
473 19 19 19 01 <- bgpId (25.25.25.1)
474 02 02 <- node descriptor type (areaId - 514)
477 02 03 <- node descriptor type (routeId - 515)
479 03 03 03 04 <- OSPF Router Id
481 00 01 <- NLRI type (1 - nodeNLRI)
482 00 2d <- NLRI length (45)
483 03 <- ProtocolID - OSPF
484 00 00 00 00 00 00 00 01 <- identifier
485 01 00 <- local node descriptor type (256)
487 02 00 <- node descriptor type (member AS - 512)
489 00 00 00 64 <- value (100)
490 02 01 <- node descriptor type (bgpId - 513)
492 19 19 19 01 <- bgpId (25.25.25.1)
493 02 02 <- node descriptor type (areaId - 514)
496 02 03 <- node descriptor type (routeId - 515)
498 01 01 01 02 <- OSPF Router Id
500 40 <- attribute flags
501 01 <- attribute type (Origin)
502 01 <- attribute length
504 40 <- attribute flags
505 02 <- attribute type (AS Path)
507 40 <- attribute flags
508 05 <- attribute type (local pref)
513 public void testBGPNode() throws Exception {
514 final byte[] body = ByteArray.cutBytes(inputBytes.get(2), MessageUtil.COMMON_HEADER_LENGTH);
515 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(inputBytes.get(2), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
516 final Update message = ParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength);
518 final UpdateBuilder builder = new UpdateBuilder();
522 assertNull(message.getWithdrawnRoutes());
526 final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
527 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("25.25.25.1")).build()).build();
529 final CLinkstateDestinationBuilder clBuilder = new CLinkstateDestinationBuilder();
530 clBuilder.setIdentifier(new Identifier(BigInteger.ONE));
531 clBuilder.setProtocolId(ProtocolId.Ospf);
533 final NodeDescriptorsBuilder n = new NodeDescriptorsBuilder();
534 n.setAsNumber(new AsNumber((long) 100)).setDomainId(new DomainIdentifier(0x19191901L)).setAreaId(new AreaIdentifier(0L));
536 final List<CLinkstateDestination> linkstates = Lists.newArrayList();
537 final NodeCaseBuilder nCase = new NodeCaseBuilder();
538 nCase.setNodeDescriptors(n.setCRouterIdentifier(
539 new OspfPseudonodeCaseBuilder().setOspfPseudonode(
540 new OspfPseudonodeBuilder().setOspfRouterId(0x03030304L).setLanInterface(new OspfInterfaceIdentifier(0x0b0b0b03L)).build()).build()).build());
541 linkstates.add(clBuilder.setObjectType(nCase.build()).build());
543 nCase.setNodeDescriptors(n.setCRouterIdentifier(
544 new OspfNodeCaseBuilder().setOspfNode(new OspfNodeBuilder().setOspfRouterId(0x03030304L).build()).build()).build());
545 linkstates.add(clBuilder.setObjectType(nCase.build()).build());
547 nCase.setNodeDescriptors(n.setCRouterIdentifier(
548 new OspfNodeCaseBuilder().setOspfNode(new OspfNodeBuilder().setOspfRouterId(0x01010102L).build()).build()).build());
549 linkstates.add(clBuilder.setObjectType(nCase.build()).build());
551 final Attributes1Builder lsBuilder = new Attributes1Builder();
552 final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
553 mpBuilder.setAfi(LinkstateAddressFamily.class);
554 mpBuilder.setSafi(LinkstateSubsequentAddressFamily.class);
555 mpBuilder.setCNextHop(nextHop);
557 final DestinationLinkstateBuilder dBuilder = new DestinationLinkstateBuilder();
558 dBuilder.setCLinkstateDestination(linkstates);
560 mpBuilder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
561 new DestinationLinkstateCaseBuilder().setDestinationLinkstate(dBuilder.build()).build()).build());
562 lsBuilder.setMpReachNlri(mpBuilder.build());
564 // check path attributes
565 final Attributes attrs = message.getAttributes();
567 final AttributesBuilder paBuilder = new AttributesBuilder();
569 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
570 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
572 paBuilder.setAsPath(new AsPathBuilder().setSegments(Collections.emptyList()).build());
573 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
575 paBuilder.setLocalPref(new LocalPrefBuilder().setPref(100L).build());
576 assertEquals(paBuilder.getLocalPref(), attrs.getLocalPref());
578 paBuilder.addAugmentation(Attributes1.class, lsBuilder.build());
579 paBuilder.setUnrecognizedAttributes(Collections.emptyList());
581 final MpReachNlri mp = attrs.getAugmentation(Attributes1.class).getMpReachNlri();
582 assertEquals(mpBuilder.getAfi(), mp.getAfi());
583 assertEquals(mpBuilder.getSafi(), mp.getSafi());
584 assertEquals(mpBuilder.getCNextHop(), mp.getCNextHop());
586 final List<CLinkstateDestination> dests = ((DestinationLinkstateCase) mp.getAdvertizedRoutes().getDestinationType()).getDestinationLinkstate().getCLinkstateDestination();
588 assertEquals(linkstates.size(), dests.size());
590 assertEquals(linkstates, dests);
593 builder.setAttributes(paBuilder.build());
594 assertEquals(builder.build(), message);
596 final ByteBuf buffer = Unpooled.buffer();
597 ParserTest.updateParser.serializeMessage(message, buffer);
598 assertArrayEquals(inputBytes.get(2), ByteArray.readAllBytes(buffer));
602 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
603 * 00 3d <- length (61) - including header
606 * 00 64 <- My AS Number (AS TRANS in this case)
608 * 00 00 00 00 <- BGP Identifier
609 * 20 <- Optional Parameters Length
610 * 02 <- opt. param. type (capabilities)
612 * 01 <- capability code (MP Extensions for BGP4)
614 * 00 01 00 01 <- AFI 1, SAFI 1
615 * 02 <- opt. param. type (capabilities)
617 * 01 <- capability code (MP Extensions for BGP4)
619 * 00 02 00 01 <- AFI 2, SAFI 1
620 * 02 <- opt. param. type (capabilities)
622 * 01 <- capability code (MP Extensions for BGP4)
624 * 40 04 00 47 <- AFI 16388, SAFI 71
625 * 02 <- opt. param. type (capabilities)
627 * 41 <- capability code (AS4 octet support)
629 * 00 00 00 64 <- AS number
632 public void testOpenMessage() throws Exception {
633 final MessageRegistry msgReg = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry();
634 final Notification o = msgReg.parseMessage(Unpooled.copiedBuffer(inputBytes.get(3)));
635 final Open open = (Open) o;
636 final Set<BgpTableType> types = Sets.newHashSet();
637 for (final BgpParameters param : open.getBgpParameters()) {
638 for (final OptionalCapabilities optCapa : param.getOptionalCapabilities()) {
639 final CParameters cParam = optCapa.getCParameters();
640 if(cParam != null && cParam.getAugmentation(CParameters1.class) != null && cParam.getAugmentation
641 (CParameters1.class).getMultiprotocolCapability() != null) {
642 final MultiprotocolCapability mp = cParam.getAugmentation(CParameters1.class).getMultiprotocolCapability();
643 final BgpTableType type = new BgpTableTypeImpl(mp.getAfi(), mp.getSafi());
648 final Set<BgpTableType> expected = Sets.newHashSet();
649 expected.add(new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class));
650 expected.add(new BgpTableTypeImpl(Ipv6AddressFamily.class, UnicastSubsequentAddressFamily.class));
651 expected.add(new BgpTableTypeImpl(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class));
652 assertEquals(expected, types);
654 final ByteBuf buffer = Unpooled.buffer();
655 msgReg.serializeMessage(o, buffer);
656 assertArrayEquals(inputBytes.get(3), ByteArray.readAllBytes(buffer));