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 io.netty.buffer.ByteBuf;
17 import io.netty.buffer.Unpooled;
18 import java.io.ByteArrayOutputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.HashSet;
24 import java.util.List;
26 import org.junit.Before;
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.spi.MessageRegistry;
31 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
32 import org.opendaylight.protocol.bgp.parser.spi.NlriRegistry;
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.rev180329.AreaIdentifier;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.DomainIdentifier;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.Identifier;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.Ipv4InterfaceIdentifier;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.LinkstateAddressFamily;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.LinkstateSubsequentAddressFamily;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.OspfInterfaceIdentifier;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.ProtocolId;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.destination.CLinkstateDestination;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.destination.CLinkstateDestinationBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.LinkCaseBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.NodeCaseBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.link._case.LinkDescriptorsBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.link._case.LocalNodeDescriptorsBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.link._case.RemoteNodeDescriptorsBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.node._case.NodeDescriptorsBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.LinkAttributesCaseBuilder;
54 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;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.node.identifier.c.router.identifier.OspfNodeCaseBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.node.identifier.c.router.identifier.OspfPseudonodeCaseBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.node.identifier.c.router.identifier.ospf.node._case.OspfNodeBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.node.identifier.c.router.identifier.ospf.pseudonode._case.OspfPseudonodeBuilder;
59 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;
60 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;
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.destination.linkstate._case.DestinationLinkstateBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Open;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.UpdateBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.BgpParameters;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.OptionalCapabilities;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.optional.capabilities.CParameters;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.AsPathBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.LocalPrefBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.OriginBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1Builder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.BgpTableType;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.CParameters1;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.mp.capabilities.MultiprotocolCapability;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlri;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlriBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.BgpOrigin;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv6AddressFamily;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.SubsequentAddressFamily;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv4NextHopCase;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
90 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;
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;
93 import org.opendaylight.yangtools.yang.common.Uint32;
94 import org.opendaylight.yangtools.yang.common.Uint64;
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(Uint32.valueOf(100))).setDomainId(
342 new DomainIdentifier(Uint32.valueOf(0x19191901L))).setAreaId(new AreaIdentifier(Uint32.ZERO));
344 final RemoteNodeDescriptorsBuilder rdBuilder = new RemoteNodeDescriptorsBuilder()
345 .setAsNumber(new AsNumber(Uint32.valueOf(100))).setDomainId(
346 new DomainIdentifier(Uint32.valueOf(0x19191901L))).setAreaId(new AreaIdentifier(Uint32.ZERO));
348 final CLinkstateDestinationBuilder clBuilder = new CLinkstateDestinationBuilder();
349 clBuilder.setIdentifier(new Identifier(Uint64.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 = new ArrayList<>();
359 final LinkCaseBuilder lCase = new LinkCaseBuilder()
360 .setLocalNodeDescriptors(ndBuilder.setCRouterIdentifier(new OspfPseudonodeCaseBuilder()
361 .setOspfPseudonode(new OspfPseudonodeBuilder()
362 .setOspfRouterId(Uint32.valueOf(0x03030304L))
363 .setLanInterface(new OspfInterfaceIdentifier(Uint32.valueOf(0x0b0b0b03L)))
364 .build()).build()).build());
365 lCase.setRemoteNodeDescriptors(rdBuilder.setCRouterIdentifier(
366 new OspfNodeCaseBuilder().setOspfNode(new OspfNodeBuilder()
367 .setOspfRouterId(Uint32.valueOf(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(Uint32.valueOf(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(Uint32.valueOf(0x01010102L)).build()).build()).build());
382 lCase.setRemoteNodeDescriptors(rdBuilder.setCRouterIdentifier(
383 new OspfPseudonodeCaseBuilder().setOspfPseudonode(new OspfPseudonodeBuilder()
384 .setOspfRouterId(Uint32.valueOf(0x03030304L))
385 .setLanInterface(new OspfInterfaceIdentifier(Uint32.valueOf(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(Uint32.valueOf(100)).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(Uint32.ONE)).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(INPUT_BYTES.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(INPUT_BYTES.get(2), MessageUtil.COMMON_HEADER_LENGTH);
536 final int messageLength = ByteArray
537 .bytesToInt(ByteArray.subByte(INPUT_BYTES.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(Uint64.ONE));
554 clBuilder.setProtocolId(ProtocolId.Ospf);
556 final NodeDescriptorsBuilder n = new NodeDescriptorsBuilder()
557 .setAsNumber(new AsNumber(Uint32.valueOf(100)))
558 .setDomainId(new DomainIdentifier(Uint32.valueOf(0x19191901L)))
559 .setAreaId(new AreaIdentifier(Uint32.ZERO));
561 final List<CLinkstateDestination> linkstates = new ArrayList<>();
562 final NodeCaseBuilder nCase = new NodeCaseBuilder()
563 .setNodeDescriptors(n.setCRouterIdentifier(new OspfPseudonodeCaseBuilder()
564 .setOspfPseudonode(new OspfPseudonodeBuilder()
565 .setOspfRouterId(Uint32.valueOf(0x03030304L))
566 .setLanInterface(new OspfInterfaceIdentifier(Uint32.valueOf(0x0b0b0b03L)))
569 linkstates.add(clBuilder.setObjectType(nCase.build()).build());
571 nCase.setNodeDescriptors(n.setCRouterIdentifier(
572 new OspfNodeCaseBuilder().setOspfNode(new OspfNodeBuilder()
573 .setOspfRouterId(Uint32.valueOf(0x03030304L)).build()).build()).build());
574 linkstates.add(clBuilder.setObjectType(nCase.build()).build());
576 nCase.setNodeDescriptors(n.setCRouterIdentifier(
577 new OspfNodeCaseBuilder().setOspfNode(new OspfNodeBuilder()
578 .setOspfRouterId(Uint32.valueOf(0x01010102L)).build()).build()).build());
579 linkstates.add(clBuilder.setObjectType(nCase.build()).build());
581 final Attributes1Builder lsBuilder = new Attributes1Builder();
582 final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
583 mpBuilder.setAfi(LinkstateAddressFamily.class);
584 mpBuilder.setSafi(LinkstateSubsequentAddressFamily.class);
585 mpBuilder.setCNextHop(nextHop);
587 final DestinationLinkstateBuilder dBuilder = new DestinationLinkstateBuilder();
588 dBuilder.setCLinkstateDestination(linkstates);
590 mpBuilder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
591 new DestinationLinkstateCaseBuilder().setDestinationLinkstate(dBuilder.build()).build()).build());
592 lsBuilder.setMpReachNlri(mpBuilder.build());
594 // check path attributes
595 final Attributes attrs = message.getAttributes();
597 final AttributesBuilder paBuilder = new AttributesBuilder();
599 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
600 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
602 paBuilder.setAsPath(new AsPathBuilder().setSegments(Collections.emptyList()).build());
603 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
605 paBuilder.setLocalPref(new LocalPrefBuilder().setPref(Uint32.valueOf(100)).build());
606 assertEquals(paBuilder.getLocalPref(), attrs.getLocalPref());
608 paBuilder.addAugmentation(Attributes1.class, lsBuilder.build());
609 paBuilder.setUnrecognizedAttributes(Collections.emptyList());
611 final MpReachNlri mp = attrs.augmentation(Attributes1.class).getMpReachNlri();
612 assertEquals(mpBuilder.getAfi(), mp.getAfi());
613 assertEquals(mpBuilder.getSafi(), mp.getSafi());
614 assertEquals(mpBuilder.getCNextHop(), mp.getCNextHop());
616 final List<CLinkstateDestination> dests = ((DestinationLinkstateCase) mp.getAdvertizedRoutes()
617 .getDestinationType()).getDestinationLinkstate().getCLinkstateDestination();
619 assertEquals(linkstates.size(), dests.size());
621 assertEquals(linkstates, dests);
624 builder.setAttributes(paBuilder.build());
625 assertEquals(builder.build(), message);
627 final ByteBuf buffer = Unpooled.buffer();
628 ParserTest.updateParser.serializeMessage(message, buffer);
629 assertArrayEquals(INPUT_BYTES.get(2), ByteArray.readAllBytes(buffer));
633 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
634 * 00 3d <- length (61) - including header
637 * 00 64 <- My AS Number (AS TRANS in this case)
639 * 00 00 00 00 <- BGP Identifier
640 * 20 <- Optional Parameters Length
641 * 02 <- opt. param. type (capabilities)
643 * 01 <- capability code (MP Extensions for BGP4)
645 * 00 01 00 01 <- AFI 1, SAFI 1
646 * 02 <- opt. param. type (capabilities)
648 * 01 <- capability code (MP Extensions for BGP4)
650 * 00 02 00 01 <- AFI 2, SAFI 1
651 * 02 <- opt. param. type (capabilities)
653 * 01 <- capability code (MP Extensions for BGP4)
655 * 40 04 00 47 <- AFI 16388, SAFI 71
656 * 02 <- opt. param. type (capabilities)
658 * 41 <- capability code (AS4 octet support)
660 * 00 00 00 64 <- AS number
663 public void testOpenMessage() throws Exception {
664 final MessageRegistry msgReg = ServiceLoaderBGPExtensionProviderContext
665 .getSingletonInstance().getMessageRegistry();
666 final Notification o = msgReg.parseMessage(Unpooled.copiedBuffer(INPUT_BYTES.get(3)), null);
667 final Open open = (Open) o;
668 final Set<BgpTableType> types = new HashSet<>();
669 for (final BgpParameters param : open.getBgpParameters()) {
670 for (final OptionalCapabilities optCapa : param.getOptionalCapabilities()) {
671 final CParameters cParam = optCapa.getCParameters();
672 if (cParam != null && cParam.augmentation(CParameters1.class) != null
673 && cParam.augmentation(CParameters1.class).getMultiprotocolCapability() != null) {
674 final MultiprotocolCapability mp = cParam.augmentation(CParameters1.class)
675 .getMultiprotocolCapability();
676 final BgpTableType type = new BgpTableTypeImpl(mp.getAfi(), mp.getSafi());
681 final Set<BgpTableType> expected = new HashSet<>();
682 expected.add(new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class));
683 expected.add(new BgpTableTypeImpl(Ipv6AddressFamily.class, UnicastSubsequentAddressFamily.class));
684 expected.add(new BgpTableTypeImpl(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class));
685 assertEquals(expected, types);
687 final ByteBuf buffer = Unpooled.buffer();
688 msgReg.serializeMessage(o, buffer);
689 assertArrayEquals(INPUT_BYTES.get(3), ByteArray.readAllBytes(buffer));