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