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