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