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