Bump upstreams to 2022.09
[bgpcep.git] / bgp / extensions / linkstate / src / test / java / org / opendaylight / protocol / bgp / linkstate / ParserTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.protocol.bgp.linkstate;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.mockito.Mockito.mock;
15
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.HashSet;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.ServiceLoader;
26 import java.util.Set;
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.BGPExtensionConsumerContext;
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.util.ByteArray;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.AreaIdentifier;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.DomainIdentifier;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.Identifier;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.Ipv4InterfaceIdentifier;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.LinkstateAddressFamily;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.LinkstateSubsequentAddressFamily;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.OspfInterfaceIdentifier;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.ProtocolId;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.destination.CLinkstateDestination;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.destination.CLinkstateDestinationBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.object.type.LinkCaseBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.object.type.NodeCaseBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.object.type.link._case.LinkDescriptorsBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.object.type.link._case.LocalNodeDescriptorsBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.object.type.link._case.RemoteNodeDescriptorsBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.object.type.node._case.NodeDescriptorsBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.path.attribute.link.state.attribute.LinkAttributesCaseBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.path.attribute.link.state.attribute.link.attributes._case.LinkAttributesBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.node.identifier.c.router.identifier.OspfNodeCaseBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.node.identifier.c.router.identifier.OspfPseudonodeCaseBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.node.identifier.c.router.identifier.ospf.node._case.OspfNodeBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.node.identifier.c.router.identifier.ospf.pseudonode._case.OspfPseudonodeBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCase;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCaseBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.linkstate._case.DestinationLinkstateBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Open;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Update;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.UpdateBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.open.message.BgpParameters;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.open.message.bgp.parameters.OptionalCapabilities;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.open.message.bgp.parameters.optional.capabilities.CParameters;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.AsPathBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.LocalPrefBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.OriginBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.AttributesReach;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.AttributesReachBuilder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.AttributesUnreach;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.BgpTableType;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.CParameters1;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.reach.MpReachNlri;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.reach.MpReachNlriBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.reach.mp.reach.nlri.AdvertizedRoutesBuilder;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.mp.capabilities.MultiprotocolCapability;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.AddressFamily;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.BgpOrigin;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv6AddressFamily;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.SubsequentAddressFamily;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.UnicastSubsequentAddressFamily;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.next.hop.c.next.hop.Ipv4NextHopCase;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.next.hop.c.next.hop.ipv4.next.hop._case.Ipv4NextHopBuilder;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Metric;
93 import org.opendaylight.yangtools.yang.binding.Notification;
94 import org.opendaylight.yangtools.yang.common.Uint32;
95 import org.opendaylight.yangtools.yang.common.Uint64;
96
97 public class ParserTest {
98
99     // Used by other tests as well
100     private static final List<byte[]> INPUT_BYTES = new ArrayList<>();
101
102     private static final int COUNTER = 4;
103
104     private static final int MAX_SIZE = 300;
105
106     private static BGPUpdateMessageParser updateParser;
107
108     private static final int LENGTH_FIELD_LENGTH = 2;
109
110     private MessageRegistry msgReg;
111
112     @Before
113     public void setUp() throws Exception {
114         final BGPExtensionConsumerContext context = ServiceLoader.load(BGPExtensionConsumerContext.class).findFirst()
115             .orElseThrow();
116
117         msgReg = context.getMessageRegistry();
118         updateParser = new BGPUpdateMessageParser(context.getAttributeRegistry(), mock(NlriRegistry.class));
119         for (int i = 1; i <= COUNTER; i++) {
120             final String name = "/up" + i + ".bin";
121             try (InputStream is = ParserTest.class.getResourceAsStream(name)) {
122                 if (is == null) {
123                     throw new IOException("Failed to get resource " + name);
124                 }
125                 final ByteArrayOutputStream bis = new ByteArrayOutputStream();
126                 final byte[] data = new byte[MAX_SIZE];
127                 int numRead;
128                 while ((numRead = is.read(data, 0, data.length)) != -1) {
129                     bis.write(data, 0, numRead);
130                 }
131                 bis.flush();
132
133                 INPUT_BYTES.add(bis.toByteArray());
134                 is.close();
135             }
136         }
137     }
138
139
140     @Test
141     public void testResource() {
142         assertNotNull(INPUT_BYTES);
143     }
144
145     /*
146      * End of Rib for LS consists of empty MP_UNREACH_NLRI, with AFI 16388 and SAFI 71
147      *
148      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
149      * 00 1d <- length (29) - including header
150      * 02 <- message type
151      * 00 00 <- withdrawn routes length
152      * 00 06 <- total path attribute length
153      * 80 <- attribute flags
154      * 0f <- attribute type (15 - MP_UNREACH_NLRI)
155      * 03 <- attribute length
156      * 40 04 <- value (AFI 16388: LS)
157      * 47 <- value (SAFI 71)
158      */
159     @Test
160     public void testEORLS() throws Exception {
161         final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(0), MessageUtil.COMMON_HEADER_LENGTH);
162         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(0),
163             MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
164         final Update message = ParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
165             null);
166
167         final AddressFamily afi = message.getAttributes()
168             .augmentation(AttributesUnreach.class).getMpUnreachNlri().getAfi();
169         final SubsequentAddressFamily safi = message.getAttributes()
170             .augmentation(AttributesUnreach.class).getMpUnreachNlri().getSafi();
171
172         assertEquals(LinkstateAddressFamily.VALUE, afi);
173         assertEquals(LinkstateSubsequentAddressFamily.VALUE, safi);
174
175         final ByteBuf buffer = Unpooled.buffer();
176         ParserTest.updateParser.serializeMessage(message, buffer);
177         assertArrayEquals(INPUT_BYTES.get(0), ByteArray.readAllBytes(buffer));
178     }
179
180     /*
181      * Tests BGP Link Ipv4
182      *
183      * 00 00 <- withdrawn routes length
184      * 01 48 <- total path attribute length (328)
185      * 90 <- attribute flags
186         0e <- attribute type code (MP reach)
187         01 2c <- attribute extended length (300)
188         40 04 <- AFI (16388 - Linkstate)
189         47 <- SAFI (71 - Linkstate)
190         04 <- next hop length
191         19 19 19 01 <- nexthop (25.25.25.1)
192         00 <- reserved
193
194         00 02 <- NLRI type (2 - linkNLRI)
195         00 5d <- NLRI length (93)
196         03 <- ProtocolID - OSPF
197         00 00 00 00 00 00 00 01 <- identifier
198
199         01 00 <- local node descriptor type (256)
200         00 24 <- length (36)
201         02 00 <- node descriptor type (member AS - 512)
202         00 04 <- length
203         00 00 00 64 <- value (100)
204         02 01 <- node descriptor type (bgpId - 513)
205         00 04 <- length
206         19 19 19 01 <- bgpId (25.25.25.1)
207         02 02 <- node descriptor type (areaId - 514)
208         00 04 <- length
209         00 00 00 00 <- value
210         02 03 <- node descriptor type (routeId - 515)
211         00 08 <- length
212         03 03 03 04 0b 0b 0b 03 <- OSPF Router Id
213
214         01 01 <- remote node descriptor type (257)
215         00 20 <- length (32)
216         02 00 <- node descriptor type (member AS - 512)
217         00 04 <- length
218         00 00 00 64 <- value (100)
219         02 01 <- node descriptor type (bgpId - 513)
220         00 04 <- length
221         19 19 19 01 <- bgpId (25.25.25.1)
222         02 02 <- node descriptor type (areaId - 514)
223         00 04 <- length
224         00 00 00 00 <- value
225         02 03 <- node descriptor type (routeId - 515)
226         00 04 <- length
227         03 03 03 04 <- OSPF Router Id
228
229         01 03 <- link descriptor type (IPv4 interface address - 259)
230         00 04 <- length (4)
231         0b 0b 0b 03 <- value (11.11.11.3)
232
233         00 02 <- NLRI type (2 - linkNLRI)
234         00 5d <- NLRI length (93)
235         03 <- ProtocolID - OSPF
236         00 00 00 00 00 00 00 01 <- identifier
237
238         01 00 <- local node descriptor type (256)
239         00 24 <- length (36)
240         02 00 <- node descriptor type (member AS - 512)
241         00 04 <- length
242         00 00 00 64 <- value (100)
243         02 01 <- node descriptor type (bgpId - 513)
244         00 04 <- length
245         19 19 19 01 <- bgpId (25.25.25.1)
246         02 02 <- node descriptor type (areaId - 514)
247         00 04 <- length
248         00 00 00 00 <- value
249         02 03 <- node descriptor type (routeId - 515)
250         00 08 <- length
251         03 03 03 04 0b 0b 0b 03 <- OSPF Router Id
252
253         01 01 <- remote node descriptor type (257)
254         00 20 <- length (32)
255         02 00 <- node descriptor type (member AS - 512)
256         00 04 <- length
257         00 00 00 64 <- value (100)
258         02 01 <- node descriptor type (bgpId - 513)
259         00 04 <- length
260         19 19 19 01 <- bgpId (25.25.25.1)
261         02 02 <- node descriptor type (areaId - 514)
262         00 04 <- length
263         00 00 00 00 <- value
264         02 03 <- node descriptor type (routeId - 515)
265         00 04 <- length
266         01 01 01 02 <- OSPF Router Id
267
268         01 03 <- link descriptor type (IPv4 interface address - 259)
269         00 04 <- length
270         0b 0b 0b 01 <- value (11.11.11.1)
271
272         00 02 <- NLRI type (2 - linkNLRI)
273         00 5d <- NLRI length (93)
274         03 <- ProtocolID - OSPF
275         00 00 00 00 00 00 00 01 <- identifier
276
277         01 00 <- local node descriptor type (256)
278         00 20 <- length (32)
279         02 00 <- node descriptor type (member AS - 512)
280         00 04 <- length
281         00 00 00 64 <- value (100)
282         02 01 <- node descriptor type (bgpId - 513)
283         00 04 <- length
284         19 19 19 01 <- bgpId (25.25.25.1)
285         02 02 <- node descriptor type (areaId - 514)
286         00 04 <- length
287         00 00 00 00 <- value
288         02 03 <- node descriptor type (routeId - 515)
289         00 04 <- length
290         01 01 01 02 <- OSPF Router Id
291
292         01 01 <- remote node descriptor type (257)
293         00 24 <- length (36)
294         02 00 <- node descriptor type (member AS - 512)
295         00 04 <- length
296         00 00 00 64 <- value (100)
297         02 01 <- node descriptor type (bgpId - 513)
298         00 04 <- length
299         19 19 19 01 <- bgpId (25.25.25.1)
300         02 02 <- node descriptor type (areaId - 514)
301         00 04 <- length
302         00 00 00 00 <- value
303         02 03 <- node descriptor type (routeId - 515)
304         00 08 <- length
305         03 03 03 04 0b 0b 0b 03 <- OSPF Router Id
306
307         01 03 <- link descriptor type (IPv4 interface address - 259)
308         00 04 <- length
309         0b 0b 0b 01 <- value (11.11.11.1)
310
311         40 <- attribute flags
312         01 <- attribute type (Origin)
313         01 <- attribute length
314         00 <- value (IGP)
315         40 <- attribute flags
316         02 <- attribute type (AS Path)
317         00 <- length
318         40 <- attribute flags
319         05 <- attribute type (local pref)
320         04 <- length
321         00 00 00 64 <- value
322         c0 <- attribute flags
323         1D <- attribute type (Link STATE - 29)
324         07 <- length
325         04 47 <- link attribute (1095 - Metric)
326         00 03 <- length
327         00 00 01 <- value
328      */
329     @Test
330     public void testBGPLink() throws Exception {
331         final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(1), MessageUtil.COMMON_HEADER_LENGTH);
332         final int messageLength = ByteArray.bytesToInt(ByteArray.subByte(INPUT_BYTES.get(1),
333             MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
334         final Update message = ParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
335             null);
336
337         final UpdateBuilder builder = new UpdateBuilder();
338
339         // check fields
340
341         assertNull(message.getWithdrawnRoutes());
342
343         final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
344             new Ipv4NextHopBuilder().setGlobal(new Ipv4AddressNoZone("25.25.25.1")).build()).build();
345
346         final LocalNodeDescriptorsBuilder ndBuilder = new LocalNodeDescriptorsBuilder()
347             .setAsNumber(new AsNumber(Uint32.valueOf(100))).setDomainId(
348                 new DomainIdentifier(Uint32.valueOf(0x19191901L))).setAreaId(new AreaIdentifier(Uint32.ZERO));
349
350         final RemoteNodeDescriptorsBuilder rdBuilder = new RemoteNodeDescriptorsBuilder()
351             .setAsNumber(new AsNumber(Uint32.valueOf(100))).setDomainId(
352                 new DomainIdentifier(Uint32.valueOf(0x19191901L))).setAreaId(new AreaIdentifier(Uint32.ZERO));
353
354         final CLinkstateDestinationBuilder clBuilder = new CLinkstateDestinationBuilder();
355         clBuilder.setIdentifier(new Identifier(Uint64.ONE));
356         clBuilder.setProtocolId(ProtocolId.Ospf);
357
358         final AttributesReachBuilder lsBuilder = new AttributesReachBuilder();
359         final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder()
360             .setAfi(LinkstateAddressFamily.VALUE)
361             .setSafi(LinkstateSubsequentAddressFamily.VALUE)
362             .setCNextHop(nextHop);
363
364         final List<CLinkstateDestination> linkstates = new ArrayList<>();
365         final LinkCaseBuilder lCase = new LinkCaseBuilder()
366                 .setLocalNodeDescriptors(ndBuilder.setCRouterIdentifier(new OspfPseudonodeCaseBuilder()
367                     .setOspfPseudonode(new OspfPseudonodeBuilder()
368                         .setOspfRouterId(Uint32.valueOf(0x03030304L))
369                         .setLanInterface(new OspfInterfaceIdentifier(Uint32.valueOf(0x0b0b0b03L)))
370                         .build()).build()).build());
371         lCase.setRemoteNodeDescriptors(rdBuilder.setCRouterIdentifier(
372             new OspfNodeCaseBuilder().setOspfNode(new OspfNodeBuilder()
373                 .setOspfRouterId(Uint32.valueOf(0x03030304L)).build()).build()).build());
374         lCase.setLinkDescriptors(new LinkDescriptorsBuilder().setIpv4InterfaceAddress(
375             new Ipv4InterfaceIdentifier(new Ipv4AddressNoZone("11.11.11.3"))).build());
376         linkstates.add(clBuilder.setObjectType(lCase.build()).build());
377
378         lCase.setRemoteNodeDescriptors(rdBuilder.setCRouterIdentifier(
379             new OspfNodeCaseBuilder().setOspfNode(new OspfNodeBuilder()
380                 .setOspfRouterId(Uint32.valueOf(0x01010102L)).build()).build()).build());
381         lCase.setLinkDescriptors(new LinkDescriptorsBuilder().setIpv4InterfaceAddress(
382             new Ipv4InterfaceIdentifier(new Ipv4AddressNoZone("11.11.11.1"))).build());
383         linkstates.add(clBuilder.setObjectType(lCase.build()).build());
384
385         lCase.setLocalNodeDescriptors(ndBuilder.setCRouterIdentifier(
386             new OspfNodeCaseBuilder().setOspfNode(new OspfNodeBuilder()
387                 .setOspfRouterId(Uint32.valueOf(0x01010102L)).build()).build()).build());
388         lCase.setRemoteNodeDescriptors(rdBuilder.setCRouterIdentifier(
389             new OspfPseudonodeCaseBuilder().setOspfPseudonode(new OspfPseudonodeBuilder()
390                 .setOspfRouterId(Uint32.valueOf(0x03030304L))
391                 .setLanInterface(new OspfInterfaceIdentifier(Uint32.valueOf(0x0b0b0b03L))).build()).build()).build());
392         linkstates.add(clBuilder.setObjectType(lCase.build()).build());
393
394         lsBuilder.setMpReachNlri(mpBuilder.build());
395
396         // check path attributes
397         final Attributes attrs = message.getAttributes();
398
399         final AttributesBuilder paBuilder = new AttributesBuilder();
400
401         paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
402         assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
403
404         paBuilder.setAsPath(new AsPathBuilder().setSegments(List.of()).build());
405         assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
406
407         paBuilder.setLocalPref(new LocalPrefBuilder().setPref(Uint32.valueOf(100)).build());
408         assertEquals(paBuilder.getLocalPref(), attrs.getLocalPref());
409
410         final MpReachNlri mp = attrs.augmentation(AttributesReach.class).getMpReachNlri();
411         assertEquals(mpBuilder.getAfi(), mp.getAfi());
412         assertEquals(mpBuilder.getSafi(), mp.getSafi());
413         assertEquals(mpBuilder.getCNextHop(), mp.getCNextHop());
414
415         final DestinationLinkstateBuilder dBuilder = new DestinationLinkstateBuilder();
416         dBuilder.setCLinkstateDestination(linkstates);
417
418         mpBuilder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
419             new DestinationLinkstateCaseBuilder().setDestinationLinkstate(dBuilder.build()).build()).build());
420         lsBuilder.setMpReachNlri(mpBuilder.build());
421
422         paBuilder.addAugmentation(lsBuilder.build());
423
424         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120
425             .Attributes1Builder lsAttrBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang
426                 .bgp.linkstate.rev200120.Attributes1Builder();
427
428         lsAttrBuilder.setLinkStateAttribute(
429             new LinkAttributesCaseBuilder().setLinkAttributes(new LinkAttributesBuilder()
430                 .setMetric(new Metric(Uint32.ONE)).build()).build());
431         paBuilder.addAugmentation(lsAttrBuilder.build());
432         paBuilder.setUnrecognizedAttributes(Map.of());
433
434         assertEquals(
435             lsAttrBuilder.build(),
436             attrs.augmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120
437                 .Attributes1.class));
438
439         final List<CLinkstateDestination> dests = ((DestinationLinkstateCase) mp.getAdvertizedRoutes()
440             .getDestinationType()).getDestinationLinkstate().getCLinkstateDestination();
441
442         assertEquals(linkstates.size(), dests.size());
443
444         assertEquals(linkstates, dests);
445         // check API message
446         builder.setAttributes(paBuilder.build());
447         assertEquals(builder.build(), message);
448
449         final ByteBuf buffer = Unpooled.buffer();
450         ParserTest.updateParser.serializeMessage(message, buffer);
451         assertArrayEquals(INPUT_BYTES.get(1), ByteArray.readAllBytes(buffer));
452     }
453
454     /*
455      * TEST BGP Node
456      *
457      *  00 00 <- withdrawn routes length
458         00 b1 <- total path attribute length (177)
459         80 <- attribute flags
460         0e <- attribute type code (MP reach)
461         a0 <- attribute length (160)
462         40 04 <- AFI (16388 - Linkstate)
463         47 <- SAFI (71 - Linkstate)
464         04 <- next hop length
465         19 19 19 01 - nexthop (25.25.25.1)
466         00 <- reserved
467
468         00 01 <- NLRI type (1 - nodeNLRI)
469         00 31 <- NLRI length (49)
470         03 <- ProtocolID - OSPF
471         00 00 00 00 00 00 00 01 <- identifier
472         01 00 <- local node descriptor type (256)
473         00 24 <- length (36)
474         02 00 <- node descriptor type (member AS - 512)
475         00 04 <- length
476         00 00 00 64 <- value (100)
477         02 01 <- node descriptor type (bgpId - 513)
478         00 04 <- length
479         19 19 19 01 <- bgpId (25.25.25.1)
480         02 02 <- node descriptor type (areaId - 514)
481         00 04 <- length
482         00 00 00 00 <- value
483         02 03 <- node descriptor type (routeId - 515)
484         00 08 <- length
485         03 03 03 04 0b 0b 0b 03 <- OSPF Router Id
486
487         00 01 <- NLRI type (1 - nodeNLRI)
488         00 2d <- NLRI length (45)
489         03 <- ProtocolID - OSPF
490         00 00 00 00 00 00 00 01 <- identifier
491         01 00 <- local node descriptor type (256)
492         00 20 <- length (32)
493         02 00 <- node descriptor type (member AS - 512)
494         00 04 <- length
495         00 00 00 64 <- value (100)
496         02 01 <- node descriptor type (bgpId - 513)
497         00 04 <- length
498         19 19 19 01 <- bgpId (25.25.25.1)
499         02 02 <- node descriptor type (areaId - 514)
500         00 04 <- length
501         00 00 00 00 <- value
502         02 03 <- node descriptor type (routeId - 515)
503         00 04 <- length
504         03 03 03 04 <- OSPF Router Id
505
506         00 01 <- NLRI type (1 - nodeNLRI)
507         00 2d <- NLRI length (45)
508         03 <- ProtocolID - OSPF
509         00 00 00 00 00 00 00 01 <- identifier
510         01 00 <- local node descriptor type (256)
511         00 20 <- length (32)
512         02 00 <- node descriptor type (member AS - 512)
513         00 04 <- length
514         00 00 00 64 <- value (100)
515         02 01 <- node descriptor type (bgpId - 513)
516         00 04 <- length
517         19 19 19 01 <- bgpId (25.25.25.1)
518         02 02 <- node descriptor type (areaId - 514)
519         00 04 <- length
520         00 00 00 00 <- value
521         02 03 <- node descriptor type (routeId - 515)
522         00 04 <- length
523         01 01 01 02  <- OSPF Router Id
524
525         40 <- attribute flags
526         01 <- attribute type (Origin)
527         01 <- attribute length
528         00 <- value (IGP)
529         40 <- attribute flags
530         02 <- attribute type (AS Path)
531         00 <- length
532         40 <- attribute flags
533         05 <- attribute type (local pref)
534         04 <- length
535         00 00 00 64 <- value
536      */
537     @Test
538     public void testBGPNode() throws Exception {
539         final byte[] body = ByteArray.cutBytes(INPUT_BYTES.get(2), MessageUtil.COMMON_HEADER_LENGTH);
540         final int messageLength = ByteArray
541             .bytesToInt(ByteArray.subByte(INPUT_BYTES.get(2), MessageUtil.MARKER_LENGTH, LENGTH_FIELD_LENGTH));
542         final Update message = ParserTest.updateParser.parseMessageBody(Unpooled.copiedBuffer(body), messageLength,
543             null);
544
545         final UpdateBuilder builder = new UpdateBuilder();
546
547         // check fields
548
549         assertNull(message.getWithdrawnRoutes());
550
551         // attributes
552
553         final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
554             new Ipv4NextHopBuilder().setGlobal(new Ipv4AddressNoZone("25.25.25.1")).build()).build();
555
556         final CLinkstateDestinationBuilder clBuilder = new CLinkstateDestinationBuilder();
557         clBuilder.setIdentifier(new Identifier(Uint64.ONE));
558         clBuilder.setProtocolId(ProtocolId.Ospf);
559
560         final NodeDescriptorsBuilder n = new NodeDescriptorsBuilder()
561                 .setAsNumber(new AsNumber(Uint32.valueOf(100)))
562                 .setDomainId(new DomainIdentifier(Uint32.valueOf(0x19191901L)))
563                 .setAreaId(new AreaIdentifier(Uint32.ZERO));
564
565         final List<CLinkstateDestination> linkstates = new ArrayList<>();
566         final NodeCaseBuilder nCase = new NodeCaseBuilder()
567                 .setNodeDescriptors(n.setCRouterIdentifier(new OspfPseudonodeCaseBuilder()
568                     .setOspfPseudonode(new OspfPseudonodeBuilder()
569                         .setOspfRouterId(Uint32.valueOf(0x03030304L))
570                         .setLanInterface(new OspfInterfaceIdentifier(Uint32.valueOf(0x0b0b0b03L)))
571                         .build())
572                     .build()).build());
573         linkstates.add(clBuilder.setObjectType(nCase.build()).build());
574
575         nCase.setNodeDescriptors(n.setCRouterIdentifier(
576             new OspfNodeCaseBuilder().setOspfNode(new OspfNodeBuilder()
577                 .setOspfRouterId(Uint32.valueOf(0x03030304L)).build()).build()).build());
578         linkstates.add(clBuilder.setObjectType(nCase.build()).build());
579
580         nCase.setNodeDescriptors(n.setCRouterIdentifier(
581             new OspfNodeCaseBuilder().setOspfNode(new OspfNodeBuilder()
582                 .setOspfRouterId(Uint32.valueOf(0x01010102L)).build()).build()).build());
583         linkstates.add(clBuilder.setObjectType(nCase.build()).build());
584
585         final AttributesReachBuilder lsBuilder = new AttributesReachBuilder();
586         final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder()
587             .setAfi(LinkstateAddressFamily.VALUE)
588             .setSafi(LinkstateSubsequentAddressFamily.VALUE)
589             .setCNextHop(nextHop);
590
591         final DestinationLinkstateBuilder dBuilder = new DestinationLinkstateBuilder();
592         dBuilder.setCLinkstateDestination(linkstates);
593
594         mpBuilder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
595             new DestinationLinkstateCaseBuilder().setDestinationLinkstate(dBuilder.build()).build()).build());
596         lsBuilder.setMpReachNlri(mpBuilder.build());
597
598         // check path attributes
599         final Attributes attrs = message.getAttributes();
600
601         final AttributesBuilder paBuilder = new AttributesBuilder();
602
603         paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
604         assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
605
606         paBuilder.setAsPath(new AsPathBuilder().setSegments(List.of()).build());
607         assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
608
609         paBuilder.setLocalPref(new LocalPrefBuilder().setPref(Uint32.valueOf(100)).build());
610         assertEquals(paBuilder.getLocalPref(), attrs.getLocalPref());
611
612         paBuilder.addAugmentation(lsBuilder.build());
613         paBuilder.setUnrecognizedAttributes(Map.of());
614
615         final MpReachNlri mp = attrs.augmentation(AttributesReach.class).getMpReachNlri();
616         assertEquals(mpBuilder.getAfi(), mp.getAfi());
617         assertEquals(mpBuilder.getSafi(), mp.getSafi());
618         assertEquals(mpBuilder.getCNextHop(), mp.getCNextHop());
619
620         final List<CLinkstateDestination> dests = ((DestinationLinkstateCase) mp.getAdvertizedRoutes()
621             .getDestinationType()).getDestinationLinkstate().getCLinkstateDestination();
622
623         assertEquals(linkstates.size(), dests.size());
624
625         assertEquals(linkstates, dests);
626
627         // check API message
628         builder.setAttributes(paBuilder.build());
629         assertEquals(builder.build(), message);
630
631         final ByteBuf buffer = Unpooled.buffer();
632         ParserTest.updateParser.serializeMessage(message, buffer);
633         assertArrayEquals(INPUT_BYTES.get(2), ByteArray.readAllBytes(buffer));
634     }
635
636     /*
637      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
638      * 00 3d <- length (61) - including header
639      * 01 <- message type
640      * 04 <- BGP version
641      * 00 64 <- My AS Number (AS TRANS in this case)
642      * 00 b4 <- Hold Time
643      * 00 00 00 00 <- BGP Identifier
644      * 20 <- Optional Parameters Length
645      * 02 <- opt. param. type (capabilities)
646      * 06 <- length
647      * 01 <- capability code (MP Extensions for BGP4)
648      * 04 <- length
649      * 00 01 00 01 <- AFI 1, SAFI 1
650      * 02 <- opt. param. type (capabilities)
651      * 06 <- length
652      * 01 <- capability code (MP Extensions for BGP4)
653      * 04 <- length
654      * 00 02 00 01 <- AFI 2, SAFI 1
655      * 02 <- opt. param. type (capabilities)
656      * 06 <- length
657      * 01 <- capability code (MP Extensions for BGP4)
658      * 04 <- length
659      * 40 04 00 47 <- AFI 16388, SAFI 71
660      * 02 <- opt. param. type (capabilities)
661      * 06 <- length
662      * 41 <- capability code (AS4 octet support)
663      * 04 <- length
664      * 00 00 00 64 <- AS number
665      */
666     @Test
667     public void testOpenMessage() throws Exception {
668         final Notification<?> o = msgReg.parseMessage(Unpooled.copiedBuffer(INPUT_BYTES.get(3)), null);
669         final Open open = (Open) o;
670         final Set<BgpTableType> types = new HashSet<>();
671         for (final BgpParameters param : open.getBgpParameters()) {
672             for (final OptionalCapabilities optCapa : param.getOptionalCapabilities()) {
673                 final CParameters cParam = optCapa.getCParameters();
674                 if (cParam != null && cParam.augmentation(CParameters1.class) != null
675                         && cParam.augmentation(CParameters1.class).getMultiprotocolCapability() != null) {
676                     final MultiprotocolCapability mp = cParam.augmentation(CParameters1.class)
677                             .getMultiprotocolCapability();
678                     final BgpTableType type = new BgpTableTypeImpl(mp.getAfi(), mp.getSafi());
679                     types.add(type);
680                 }
681             }
682         }
683         final Set<BgpTableType> expected = new HashSet<>();
684         expected.add(new BgpTableTypeImpl(Ipv4AddressFamily.VALUE, UnicastSubsequentAddressFamily.VALUE));
685         expected.add(new BgpTableTypeImpl(Ipv6AddressFamily.VALUE, UnicastSubsequentAddressFamily.VALUE));
686         expected.add(new BgpTableTypeImpl(LinkstateAddressFamily.VALUE, LinkstateSubsequentAddressFamily.VALUE));
687         assertEquals(expected, types);
688
689         final ByteBuf buffer = Unpooled.buffer();
690         msgReg.serializeMessage(o, buffer);
691         assertArrayEquals(INPUT_BYTES.get(3), ByteArray.readAllBytes(buffer));
692     }
693 }