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 {
98 // Used by other tests as well
99 private static final List<byte[]> INPUT_BYTES = 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
112 .getSingletonInstance().getAttributeRegistry(), mock(NlriRegistry.class));
113 for (int i = 1; i <= COUNTER; i++) {
114 final String name = "/up" + i + ".bin";
115 try (InputStream is = ParserTest.class.getResourceAsStream(name)) {
117 throw new IOException("Failed to get resource " + name);
119 final ByteArrayOutputStream bis = new ByteArrayOutputStream();
120 final byte[] data = new byte[MAX_SIZE];
122 while ((numRead = is.read(data, 0, data.length)) != -1) {
123 bis.write(data, 0, numRead);
127 INPUT_BYTES.add(bis.toByteArray());
135 public void testResource() {
136 assertNotNull(INPUT_BYTES);
140 * End of Rib for LS consists of empty MP_UNREACH_NLRI, with AFI 16388 and SAFI 71
142 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
143 * 00 1d <- length (29) - including header
145 * 00 00 <- withdrawn routes length
146 * 00 06 <- total path attribute length
147 * 80 <- attribute flags
148 * 0f <- attribute type (15 - MP_UNREACH_NLRI)
149 * 03 <- attribute length
150 * 40 04 <- value (AFI 16388: LS)
151 * 47 <- value (SAFI 71)
154 public void testEORLS() throws Exception {
155 final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(0), MessageUtil.COMMON_HEADER_LENGTH);
156 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(0),
157 MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
158 final Update message = ParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
161 final Class<? extends AddressFamily> afi = message.getAttributes()
162 .augmentation(Attributes2.class).getMpUnreachNlri().getAfi();
163 final Class<? extends SubsequentAddressFamily> safi = message.getAttributes()
164 .augmentation(Attributes2.class).getMpUnreachNlri().getSafi();
166 assertEquals(LinkstateAddressFamily.class, afi);
167 assertEquals(LinkstateSubsequentAddressFamily.class, safi);
169 final ByteBuf buffer = Unpooled.buffer();
170 ParserTest.updateParser.serializeMessage(message, buffer);
171 assertArrayEquals(INPUT_BYTES.get(0), ByteArray.readAllBytes(buffer));
175 * Tests BGP Link Ipv4
177 * 00 00 <- withdrawn routes length
178 * 01 48 <- total path attribute length (328)
179 * 90 <- attribute flags
180 0e <- attribute type code (MP reach)
181 01 2c <- attribute extended length (300)
182 40 04 <- AFI (16388 - Linkstate)
183 47 <- SAFI (71 - Linkstate)
184 04 <- next hop length
185 19 19 19 01 <- nexthop (25.25.25.1)
188 00 02 <- NLRI type (2 - linkNLRI)
189 00 5d <- NLRI length (93)
190 03 <- ProtocolID - OSPF
191 00 00 00 00 00 00 00 01 <- identifier
193 01 00 <- local node descriptor type (256)
195 02 00 <- node descriptor type (member AS - 512)
197 00 00 00 64 <- value (100)
198 02 01 <- node descriptor type (bgpId - 513)
200 19 19 19 01 <- bgpId (25.25.25.1)
201 02 02 <- node descriptor type (areaId - 514)
204 02 03 <- node descriptor type (routeId - 515)
206 03 03 03 04 0b 0b 0b 03 <- OSPF Router Id
208 01 01 <- remote node descriptor type (257)
210 02 00 <- node descriptor type (member AS - 512)
212 00 00 00 64 <- value (100)
213 02 01 <- node descriptor type (bgpId - 513)
215 19 19 19 01 <- bgpId (25.25.25.1)
216 02 02 <- node descriptor type (areaId - 514)
219 02 03 <- node descriptor type (routeId - 515)
221 03 03 03 04 <- OSPF Router Id
223 01 03 <- link descriptor type (IPv4 interface address - 259)
225 0b 0b 0b 03 <- value (11.11.11.3)
227 00 02 <- NLRI type (2 - linkNLRI)
228 00 5d <- NLRI length (93)
229 03 <- ProtocolID - OSPF
230 00 00 00 00 00 00 00 01 <- identifier
232 01 00 <- local node descriptor type (256)
234 02 00 <- node descriptor type (member AS - 512)
236 00 00 00 64 <- value (100)
237 02 01 <- node descriptor type (bgpId - 513)
239 19 19 19 01 <- bgpId (25.25.25.1)
240 02 02 <- node descriptor type (areaId - 514)
243 02 03 <- node descriptor type (routeId - 515)
245 03 03 03 04 0b 0b 0b 03 <- OSPF Router Id
247 01 01 <- remote node descriptor type (257)
249 02 00 <- node descriptor type (member AS - 512)
251 00 00 00 64 <- value (100)
252 02 01 <- node descriptor type (bgpId - 513)
254 19 19 19 01 <- bgpId (25.25.25.1)
255 02 02 <- node descriptor type (areaId - 514)
258 02 03 <- node descriptor type (routeId - 515)
260 01 01 01 02 <- OSPF Router Id
262 01 03 <- link descriptor type (IPv4 interface address - 259)
264 0b 0b 0b 01 <- value (11.11.11.1)
266 00 02 <- NLRI type (2 - linkNLRI)
267 00 5d <- NLRI length (93)
268 03 <- ProtocolID - OSPF
269 00 00 00 00 00 00 00 01 <- identifier
271 01 00 <- local node descriptor type (256)
273 02 00 <- node descriptor type (member AS - 512)
275 00 00 00 64 <- value (100)
276 02 01 <- node descriptor type (bgpId - 513)
278 19 19 19 01 <- bgpId (25.25.25.1)
279 02 02 <- node descriptor type (areaId - 514)
282 02 03 <- node descriptor type (routeId - 515)
284 01 01 01 02 <- OSPF Router Id
286 01 01 <- remote node descriptor type (257)
288 02 00 <- node descriptor type (member AS - 512)
290 00 00 00 64 <- value (100)
291 02 01 <- node descriptor type (bgpId - 513)
293 19 19 19 01 <- bgpId (25.25.25.1)
294 02 02 <- node descriptor type (areaId - 514)
297 02 03 <- node descriptor type (routeId - 515)
299 03 03 03 04 0b 0b 0b 03 <- OSPF Router Id
301 01 03 <- link descriptor type (IPv4 interface address - 259)
303 0b 0b 0b 01 <- value (11.11.11.1)
305 40 <- attribute flags
306 01 <- attribute type (Origin)
307 01 <- attribute length
309 40 <- attribute flags
310 02 <- attribute type (AS Path)
312 40 <- attribute flags
313 05 <- attribute type (local pref)
316 c0 <- attribute flags
317 1D <- attribute type (Link STATE - 29)
319 04 47 <- link attribute (1095 - Metric)
324 public void testBGPLink() throws Exception {
325 final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(1), MessageUtil.COMMON_HEADER_LENGTH);
326 final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(1),
327 MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
328 final Update message = ParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
331 final UpdateBuilder builder = new UpdateBuilder();
335 assertNull(message.getWithdrawnRoutes());
337 final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
338 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("25.25.25.1")).build()).build();
340 final LocalNodeDescriptorsBuilder ndBuilder = new LocalNodeDescriptorsBuilder()
341 .setAsNumber(new AsNumber((long) 100)).setDomainId(
342 new DomainIdentifier(0x19191901L)).setAreaId(new AreaIdentifier(0L));
344 final RemoteNodeDescriptorsBuilder rdBuilder = new RemoteNodeDescriptorsBuilder()
345 .setAsNumber(new AsNumber((long) 100)).setDomainId(
346 new DomainIdentifier(0x19191901L)).setAreaId(new AreaIdentifier(0L));
348 final CLinkstateDestinationBuilder clBuilder = new CLinkstateDestinationBuilder();
349 clBuilder.setIdentifier(new Identifier(BigInteger.ONE));
350 clBuilder.setProtocolId(ProtocolId.Ospf);
352 final Attributes1Builder lsBuilder = new Attributes1Builder();
353 final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
354 mpBuilder.setAfi(LinkstateAddressFamily.class);
355 mpBuilder.setSafi(LinkstateSubsequentAddressFamily.class);
356 mpBuilder.setCNextHop(nextHop);
358 final List<CLinkstateDestination> linkstates = Lists.newArrayList();
359 final LinkCaseBuilder lCase = new LinkCaseBuilder().setLocalNodeDescriptors(ndBuilder.setCRouterIdentifier(
360 new OspfPseudonodeCaseBuilder().setOspfPseudonode(
361 new OspfPseudonodeBuilder().setOspfRouterId(0x03030304L)
362 .setLanInterface(new OspfInterfaceIdentifier(0x0b0b0b03L)).build()).build()).build());
363 lCase.setRemoteNodeDescriptors(rdBuilder.setCRouterIdentifier(
364 new OspfNodeCaseBuilder().setOspfNode(new OspfNodeBuilder()
365 .setOspfRouterId(0x03030304L).build()).build()).build());
366 lCase.setLinkDescriptors(new LinkDescriptorsBuilder().setIpv4InterfaceAddress(
367 new Ipv4InterfaceIdentifier(new Ipv4Address("11.11.11.3"))).build());
368 linkstates.add(clBuilder.setObjectType(lCase.build()).build());
370 lCase.setRemoteNodeDescriptors(rdBuilder.setCRouterIdentifier(
371 new OspfNodeCaseBuilder().setOspfNode(new OspfNodeBuilder()
372 .setOspfRouterId(0x01010102L).build()).build()).build());
373 lCase.setLinkDescriptors(new LinkDescriptorsBuilder().setIpv4InterfaceAddress(
374 new Ipv4InterfaceIdentifier(new Ipv4Address("11.11.11.1"))).build());
375 linkstates.add(clBuilder.setObjectType(lCase.build()).build());
377 lCase.setLocalNodeDescriptors(ndBuilder.setCRouterIdentifier(
378 new OspfNodeCaseBuilder().setOspfNode(new OspfNodeBuilder()
379 .setOspfRouterId(0x01010102L).build()).build()).build());
380 lCase.setRemoteNodeDescriptors(rdBuilder.setCRouterIdentifier(
381 new OspfPseudonodeCaseBuilder().setOspfPseudonode(new OspfPseudonodeBuilder()
382 .setOspfRouterId(0x03030304L)
383 .setLanInterface(new OspfInterfaceIdentifier(0x0b0b0b03L)).build()).build()).build());
384 linkstates.add(clBuilder.setObjectType(lCase.build()).build());
386 lsBuilder.setMpReachNlri(mpBuilder.build());
388 // check path attributes
389 final Attributes attrs = message.getAttributes();
391 final AttributesBuilder paBuilder = new AttributesBuilder();
393 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
394 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
396 paBuilder.setAsPath(new AsPathBuilder().setSegments(Collections.emptyList()).build());
397 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
399 paBuilder.setLocalPref(new LocalPrefBuilder().setPref(100L).build());
400 assertEquals(paBuilder.getLocalPref(), attrs.getLocalPref());
402 final MpReachNlri mp = attrs.augmentation(Attributes1.class).getMpReachNlri();
403 assertEquals(mpBuilder.getAfi(), mp.getAfi());
404 assertEquals(mpBuilder.getSafi(), mp.getSafi());
405 assertEquals(mpBuilder.getCNextHop(), mp.getCNextHop());
407 final DestinationLinkstateBuilder dBuilder = new DestinationLinkstateBuilder();
408 dBuilder.setCLinkstateDestination(linkstates);
410 mpBuilder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
411 new DestinationLinkstateCaseBuilder().setDestinationLinkstate(dBuilder.build()).build()).build());
412 lsBuilder.setMpReachNlri(mpBuilder.build());
414 paBuilder.addAugmentation(Attributes1.class, lsBuilder.build());
416 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329
417 .Attributes1Builder lsAttrBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang
418 .bgp.linkstate.rev180329.Attributes1Builder();
420 lsAttrBuilder.setLinkStateAttribute(
421 new LinkAttributesCaseBuilder().setLinkAttributes(new LinkAttributesBuilder()
422 .setMetric(new Metric(1L)).build()).build());
423 paBuilder.addAugmentation(
424 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.Attributes1.class,
425 lsAttrBuilder.build());
426 paBuilder.setUnrecognizedAttributes(Collections.emptyList());
429 lsAttrBuilder.build(),
430 attrs.augmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp
431 .linkstate.rev180329.Attributes1.class));
433 final List<CLinkstateDestination> dests = ((DestinationLinkstateCase) mp.getAdvertizedRoutes()
434 .getDestinationType()).getDestinationLinkstate().getCLinkstateDestination();
436 assertEquals(linkstates.size(), dests.size());
438 assertEquals(linkstates, dests);
440 builder.setAttributes(paBuilder.build());
441 assertEquals(builder.build(), message);
443 final ByteBuf buffer = Unpooled.buffer();
444 ParserTest.updateParser.serializeMessage(message, buffer);
445 assertArrayEquals(INPUT_BYTES.get(1), ByteArray.readAllBytes(buffer));
451 * 00 00 <- withdrawn routes length
452 00 b1 <- total path attribute length (177)
453 80 <- attribute flags
454 0e <- attribute type code (MP reach)
455 a0 <- attribute length (160)
456 40 04 <- AFI (16388 - Linkstate)
457 47 <- SAFI (71 - Linkstate)
458 04 <- next hop length
459 19 19 19 01 - nexthop (25.25.25.1)
462 00 01 <- NLRI type (1 - nodeNLRI)
463 00 31 <- NLRI length (49)
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 0b 0b 0b 03 <- 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 03 03 03 04 <- OSPF Router Id
500 00 01 <- NLRI type (1 - nodeNLRI)
501 00 2d <- NLRI length (45)
502 03 <- ProtocolID - OSPF
503 00 00 00 00 00 00 00 01 <- identifier
504 01 00 <- local node descriptor type (256)
506 02 00 <- node descriptor type (member AS - 512)
508 00 00 00 64 <- value (100)
509 02 01 <- node descriptor type (bgpId - 513)
511 19 19 19 01 <- bgpId (25.25.25.1)
512 02 02 <- node descriptor type (areaId - 514)
515 02 03 <- node descriptor type (routeId - 515)
517 01 01 01 02 <- OSPF Router Id
519 40 <- attribute flags
520 01 <- attribute type (Origin)
521 01 <- attribute length
523 40 <- attribute flags
524 02 <- attribute type (AS Path)
526 40 <- attribute flags
527 05 <- attribute type (local pref)
532 public void testBGPNode() throws Exception {
533 final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(2), MessageUtil.COMMON_HEADER_LENGTH);
534 final int messageLength = ByteArray
535 .bytesToInt(ByteArray.subByte(INPUT_BYTES.get(2), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
536 final Update message = ParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
539 final UpdateBuilder builder = new UpdateBuilder();
543 assertNull(message.getWithdrawnRoutes());
547 final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
548 new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("25.25.25.1")).build()).build();
550 final CLinkstateDestinationBuilder clBuilder = new CLinkstateDestinationBuilder();
551 clBuilder.setIdentifier(new Identifier(BigInteger.ONE));
552 clBuilder.setProtocolId(ProtocolId.Ospf);
554 final NodeDescriptorsBuilder n = new NodeDescriptorsBuilder();
555 n.setAsNumber(new AsNumber((long) 100)).setDomainId(new DomainIdentifier(0x19191901L))
556 .setAreaId(new AreaIdentifier(0L));
558 final List<CLinkstateDestination> linkstates = Lists.newArrayList();
559 final NodeCaseBuilder nCase = new NodeCaseBuilder();
560 nCase.setNodeDescriptors(n.setCRouterIdentifier(
561 new OspfPseudonodeCaseBuilder().setOspfPseudonode(
562 new OspfPseudonodeBuilder().setOspfRouterId(0x03030304L)
563 .setLanInterface(new OspfInterfaceIdentifier(0x0b0b0b03L)).build()).build()).build());
564 linkstates.add(clBuilder.setObjectType(nCase.build()).build());
566 nCase.setNodeDescriptors(n.setCRouterIdentifier(
567 new OspfNodeCaseBuilder().setOspfNode(new OspfNodeBuilder()
568 .setOspfRouterId(0x03030304L).build()).build()).build());
569 linkstates.add(clBuilder.setObjectType(nCase.build()).build());
571 nCase.setNodeDescriptors(n.setCRouterIdentifier(
572 new OspfNodeCaseBuilder().setOspfNode(new OspfNodeBuilder()
573 .setOspfRouterId(0x01010102L).build()).build()).build());
574 linkstates.add(clBuilder.setObjectType(nCase.build()).build());
576 final Attributes1Builder lsBuilder = new Attributes1Builder();
577 final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
578 mpBuilder.setAfi(LinkstateAddressFamily.class);
579 mpBuilder.setSafi(LinkstateSubsequentAddressFamily.class);
580 mpBuilder.setCNextHop(nextHop);
582 final DestinationLinkstateBuilder dBuilder = new DestinationLinkstateBuilder();
583 dBuilder.setCLinkstateDestination(linkstates);
585 mpBuilder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
586 new DestinationLinkstateCaseBuilder().setDestinationLinkstate(dBuilder.build()).build()).build());
587 lsBuilder.setMpReachNlri(mpBuilder.build());
589 // check path attributes
590 final Attributes attrs = message.getAttributes();
592 final AttributesBuilder paBuilder = new AttributesBuilder();
594 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
595 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
597 paBuilder.setAsPath(new AsPathBuilder().setSegments(Collections.emptyList()).build());
598 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
600 paBuilder.setLocalPref(new LocalPrefBuilder().setPref(100L).build());
601 assertEquals(paBuilder.getLocalPref(), attrs.getLocalPref());
603 paBuilder.addAugmentation(Attributes1.class, lsBuilder.build());
604 paBuilder.setUnrecognizedAttributes(Collections.emptyList());
606 final MpReachNlri mp = attrs.augmentation(Attributes1.class).getMpReachNlri();
607 assertEquals(mpBuilder.getAfi(), mp.getAfi());
608 assertEquals(mpBuilder.getSafi(), mp.getSafi());
609 assertEquals(mpBuilder.getCNextHop(), mp.getCNextHop());
611 final List<CLinkstateDestination> dests = ((DestinationLinkstateCase) mp.getAdvertizedRoutes()
612 .getDestinationType()).getDestinationLinkstate().getCLinkstateDestination();
614 assertEquals(linkstates.size(), dests.size());
616 assertEquals(linkstates, dests);
619 builder.setAttributes(paBuilder.build());
620 assertEquals(builder.build(), message);
622 final ByteBuf buffer = Unpooled.buffer();
623 ParserTest.updateParser.serializeMessage(message, buffer);
624 assertArrayEquals(INPUT_BYTES.get(2), ByteArray.readAllBytes(buffer));
628 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
629 * 00 3d <- length (61) - including header
632 * 00 64 <- My AS Number (AS TRANS in this case)
634 * 00 00 00 00 <- BGP Identifier
635 * 20 <- Optional Parameters Length
636 * 02 <- opt. param. type (capabilities)
638 * 01 <- capability code (MP Extensions for BGP4)
640 * 00 01 00 01 <- AFI 1, SAFI 1
641 * 02 <- opt. param. type (capabilities)
643 * 01 <- capability code (MP Extensions for BGP4)
645 * 00 02 00 01 <- AFI 2, SAFI 1
646 * 02 <- opt. param. type (capabilities)
648 * 01 <- capability code (MP Extensions for BGP4)
650 * 40 04 00 47 <- AFI 16388, SAFI 71
651 * 02 <- opt. param. type (capabilities)
653 * 41 <- capability code (AS4 octet support)
655 * 00 00 00 64 <- AS number
658 public void testOpenMessage() throws Exception {
659 final MessageRegistry msgReg = ServiceLoaderBGPExtensionProviderContext
660 .getSingletonInstance().getMessageRegistry();
661 final Notification o = msgReg.parseMessage(Unpooled.copiedBuffer(INPUT_BYTES.get(3)), null);
662 final Open open = (Open) o;
663 final Set<BgpTableType> types = Sets.newHashSet();
664 for (final BgpParameters param : open.getBgpParameters()) {
665 for (final OptionalCapabilities optCapa : param.getOptionalCapabilities()) {
666 final CParameters cParam = optCapa.getCParameters();
667 if (cParam != null && cParam.augmentation(CParameters1.class) != null
668 && cParam.augmentation(CParameters1.class).getMultiprotocolCapability() != null) {
669 final MultiprotocolCapability mp = cParam.augmentation(CParameters1.class)
670 .getMultiprotocolCapability();
671 final BgpTableType type = new BgpTableTypeImpl(mp.getAfi(), mp.getSafi());
676 final Set<BgpTableType> expected = Sets.newHashSet();
677 expected.add(new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class));
678 expected.add(new BgpTableTypeImpl(Ipv6AddressFamily.class, UnicastSubsequentAddressFamily.class));
679 expected.add(new BgpTableTypeImpl(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class));
680 assertEquals(expected, types);
682 final ByteBuf buffer = Unpooled.buffer();
683 msgReg.serializeMessage(o, buffer);
684 assertArrayEquals(INPUT_BYTES.get(3), ByteArray.readAllBytes(buffer));