4e88e9160787f7f3ddbed2755be80c9ce9c2d640
[bgpcep.git] / bgp / inet / src / test / java / org / opendaylight / protocol / bgp / inet / codec / BGPParserTest.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.protocol.bgp.inet.codec;
10
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNull;
14
15 import com.google.common.collect.Lists;
16 import io.netty.buffer.ByteBuf;
17 import io.netty.buffer.Unpooled;
18 import java.io.ByteArrayOutputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
27 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
28 import org.opendaylight.protocol.util.ByteArray;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.prefixes.DestinationIpv6Builder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.prefixes.destination.ipv6.Ipv6Prefixes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.prefixes.destination.ipv6.Ipv6PrefixesBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv6CaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.UpdateBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.AsPathBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.ClusterIdBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.MultiExitDiscBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.OriginBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.OriginatorIdBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.as.path.Segments;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.as.path.SegmentsBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1Builder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlriBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpOrigin;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ClusterIdentifier;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv6NextHopCase;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv6NextHopCaseBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.ipv6.next.hop._case.Ipv6NextHopBuilder;
59
60 public class BGPParserTest {
61
62     private static int MAX_SIZE = 300;
63
64     private static MessageRegistry messageRegistry;
65
66     private static byte[] input;
67
68     @BeforeClass
69     public static void setUp() throws Exception {
70         messageRegistry = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry();
71
72         final String name = "/up2.bin";
73         try (final InputStream is = BGPParserTest.class.getResourceAsStream(name)) {
74             if (is == null) {
75                 throw new IOException("Failed to get resource " + name);
76             }
77             final ByteArrayOutputStream bis = new ByteArrayOutputStream();
78             final byte[] data = new byte[MAX_SIZE];
79             int nRead = 0;
80             while ((nRead = is.read(data, 0, data.length)) != -1) {
81                 bis.write(data, 0, nRead);
82             }
83             bis.flush();
84
85             input = bis.toByteArray();
86             is.close();
87         }
88     }
89
90     /*
91      * Tests IPv6 NEXT_HOP, NLRI, ORIGIN.IGP, MULTI_EXIT_DISC, ORIGINATOR-ID, CLUSTER_LIST.
92      *
93      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
94      * 00 80 <- length (128) - including header
95      * 02 <- message type
96      * 00 00 <- withdrawn routes length
97      * 00 69 <- total path attribute length (105)
98      * 40 <- attribute flags
99      * 01 <- attribute type code (origin)
100      * 01 <- attribute length
101      * 00 <- Origin value (IGP)
102      * 40 <- attribute flags
103      * 02 <- attribute type code (as path)
104      * 06 <- attribute length
105      * 02 <- AS_SEQUENCE
106      * 01 <- path segment count
107      * 00 00 fd e9 <- path segment value (65001)
108      * 40 <- attribute flags
109      * 03 <- attribute type code (next hop)
110      * 04 <- attribute length
111      * 0a 00 00 00 <- next hop value (10.0.0.0)
112      * 80 <- attribute flags
113      * 04 <- attribute type code (multi exit disc)
114      * 04 <- attribute length
115      * 00 00 00 00 <- value
116      * 80 <- attribute flags
117      * 09 <- attribute type code (originator id)
118      * 04 <- attribute length
119      * 7f 00 00 01 <- value (localhost ip)
120      * 80 <- attribute flags
121      * 0a <- attribute type code (cluster list)
122      * 08 <- attribute length
123      * 01 02 03 04 <- value
124      * 05 06 07 08 <- value
125      * 80 <- attribute flags
126      * 0e <- attribute type code (mp reach nlri)
127      * 40 <- attribute length
128      * 00 02 <- AFI (Ipv6)
129      * 01 <- SAFI (Unicast)
130      * 20 <- length of next hop
131      * 20 01 0d b8 00 00 00 00 00 00 00 00 00 00 00 01 <- global
132      * fe 80 00 00 00 00 00 00 c0 01 0b ff fe 7e 00 <- link local
133      * 00 <- reserved
134      *
135      * //NLRI
136      * 40 20 01 0d b8 00 01 00 02 <- IPv6 Prefix (2001:db8:1:2:: / 64)
137      * 40 20 01 0d b8 00 01 00 01 <- IPv6 Prefix (2001:db8:1:1:: / 64)
138      * 40 20 01 0d b8 00 01 00 00 <- IPv6 Prefix (2001:db8:1:: / 64)
139      *
140      */
141     @Test
142     public void testIPv6Nlri() throws Exception {
143         final Update message = (Update) messageRegistry.parseMessage(Unpooled.wrappedBuffer(input), null);
144
145         // check fields
146         assertNull(message.getWithdrawnRoutes());
147
148         final UpdateBuilder builder = new UpdateBuilder();
149
150         // check NLRI
151
152         final List<Ipv6Prefixes> prefs = new ArrayList<>();
153         prefs.add(new Ipv6PrefixesBuilder().setPrefix(new Ipv6Prefix("2001:db8:1:2::/64")).build());
154         prefs.add(new Ipv6PrefixesBuilder().setPrefix(new Ipv6Prefix("2001:db8:1:1::/64")).build());
155         prefs.add(new Ipv6PrefixesBuilder().setPrefix(new Ipv6Prefix("2001:db8:1::/64")).build());
156
157         assertNull(message.getNlri());
158
159         // attributes
160         final List<AsNumber> asNumbers = new ArrayList<AsNumber>();
161         asNumbers.add(new AsNumber(65001L));
162         final List<Segments> asPath = Lists.newArrayList();
163         asPath.add(new SegmentsBuilder().setAsSequence(asNumbers).build());
164
165         final Ipv6NextHopCase nextHop = new Ipv6NextHopCaseBuilder().setIpv6NextHop(
166                 new Ipv6NextHopBuilder().setGlobal(new Ipv6Address("2001:db8::1")).setLinkLocal(new Ipv6Address("fe80::c001:bff:fe7e:0")).build()).build();
167
168         final List<ClusterIdentifier> clusters = Lists.newArrayList(new ClusterIdentifier(new Ipv4Address("1.2.3.4")),
169                 new ClusterIdentifier(new Ipv4Address("5.6.7.8")));
170
171         // check path attributes
172
173         final Attributes attrs = message.getAttributes();
174
175         final AttributesBuilder paBuilder = new AttributesBuilder();
176
177         paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
178         assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
179
180         paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
181         assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
182
183         paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
184         assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
185
186         paBuilder.setOriginatorId(new OriginatorIdBuilder().setOriginator(new Ipv4Address("127.0.0.1")).build());
187         assertEquals(paBuilder.getOriginatorId(), attrs.getOriginatorId());
188
189         paBuilder.setClusterId(new ClusterIdBuilder().setCluster(clusters).build());
190         assertEquals(paBuilder.getClusterId(), attrs.getClusterId());
191
192         final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
193         mpBuilder.setAfi(Ipv6AddressFamily.class);
194         mpBuilder.setSafi(UnicastSubsequentAddressFamily.class);
195         mpBuilder.setCNextHop(nextHop);
196         mpBuilder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
197                 new DestinationIpv6CaseBuilder().setDestinationIpv6(new DestinationIpv6Builder().setIpv6Prefixes(prefs).build()).build()).build());
198
199         paBuilder.addAugmentation(Attributes1.class, new Attributes1Builder().setMpReachNlri(mpBuilder.build()).build());
200         assertEquals(paBuilder.getAugmentation(Attributes1.class).getMpReachNlri(),
201                 attrs.getAugmentation(Attributes1.class).getMpReachNlri());
202         paBuilder.setUnrecognizedAttributes(Collections.emptyList());
203         // check API message
204
205         builder.setAttributes(paBuilder.build());
206         assertEquals(builder.build(), message);
207
208         final ByteBuf buffer = Unpooled.buffer();
209         messageRegistry.serializeMessage(message, buffer);
210         assertArrayEquals(input, ByteArray.readAllBytes(buffer));
211     }
212
213 }