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