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