fb7eed3800056ee61abe6e5f9dcaa87680ddde8c
[bgpcep.git] / bgp / extensions / 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.assertNotNull;
14 import static org.junit.Assert.assertNull;
15
16 import com.google.common.collect.Lists;
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.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
28 import org.opendaylight.protocol.bgp.parser.spi.RevisedErrorHandlingSupport;
29 import org.opendaylight.protocol.bgp.parser.spi.pojo.PeerSpecificParserConstraintImpl;
30 import org.opendaylight.protocol.bgp.parser.spi.pojo.RevisedErrorHandlingSupportImpl;
31 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
32 import org.opendaylight.protocol.util.ByteArray;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.prefixes.DestinationIpv6Builder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.prefixes.destination.ipv6.Ipv6Prefixes;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.prefixes.destination.ipv6.Ipv6PrefixesBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv6CaseBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Update;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.UpdateBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.AsPathBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.ClusterIdBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.MultiExitDiscBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.OriginBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.OriginatorIdBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.as.path.Segments;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.as.path.SegmentsBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.update.message.WithdrawnRoutes;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1Builder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlriBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlri;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.BgpOrigin;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.ClusterIdentifier;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv6AddressFamily;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.UnicastSubsequentAddressFamily;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.next.hop.c.next.hop.Ipv6NextHopCase;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.next.hop.c.next.hop.Ipv6NextHopCaseBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.next.hop.c.next.hop.ipv6.next.hop._case.Ipv6NextHopBuilder;
66 import org.opendaylight.yangtools.yang.common.Uint32;
67
68 public class BGPParserTest {
69
70     private static final int MAX_SIZE = 300;
71
72     private static MessageRegistry messageRegistry;
73
74     private static List<byte[]> input;
75
76     private static int MESSAGE_COUNT = 2;
77
78     @BeforeClass
79     public static void setUp() throws Exception {
80         messageRegistry = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry();
81         input = new ArrayList<>(MESSAGE_COUNT);
82         for (int i = 1; i <= MESSAGE_COUNT; i++) {
83
84             final String name = "/up" + i + ".bin";
85             try (InputStream is = BGPParserTest.class.getResourceAsStream(name)) {
86                 if (is == null) {
87                     throw new IOException("Failed to get resource " + name);
88                 }
89                 final ByteArrayOutputStream bis = new ByteArrayOutputStream();
90                 final byte[] data = new byte[MAX_SIZE];
91                 int position;
92                 while ((position = is.read(data, 0, data.length)) != -1) {
93                     bis.write(data, 0, position);
94                 }
95                 bis.flush();
96
97                 input.add(bis.toByteArray());
98                 is.close();
99             }
100         }
101     }
102
103     /*
104      * Tests IPv6 NEXT_HOP, NLRI, ORIGIN.IGP, MULTI_EXIT_DISC, ORIGINATOR-ID, CLUSTER_LIST.
105      *
106      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
107      * 00 80 <- length (128) - including header
108      * 02 <- message type
109      * 00 00 <- withdrawn routes length
110      * 00 69 <- total path attribute length (105)
111      * 40 <- attribute flags
112      * 01 <- attribute type code (origin)
113      * 01 <- attribute length
114      * 00 <- Origin value (IGP)
115      * 40 <- attribute flags
116      * 02 <- attribute type code (as path)
117      * 06 <- attribute length
118      * 02 <- AS_SEQUENCE
119      * 01 <- path segment count
120      * 00 00 fd e9 <- path segment value (65001)
121      * 40 <- attribute flags
122      * 03 <- attribute type code (next hop)
123      * 04 <- attribute length
124      * 0a 00 00 00 <- next hop value (10.0.0.0)
125      * 80 <- attribute flags
126      * 04 <- attribute type code (multi exit disc)
127      * 04 <- attribute length
128      * 00 00 00 00 <- value
129      * 80 <- attribute flags
130      * 09 <- attribute type code (originator id)
131      * 04 <- attribute length
132      * 7f 00 00 01 <- value (localhost ip)
133      * 80 <- attribute flags
134      * 0a <- attribute type code (cluster list)
135      * 08 <- attribute length
136      * 01 02 03 04 <- value
137      * 05 06 07 08 <- value
138      * 80 <- attribute flags
139      * 0e <- attribute type code (mp reach nlri)
140      * 40 <- attribute length
141      * 00 02 <- AFI (Ipv6)
142      * 01 <- SAFI (Unicast)
143      * 20 <- length of next hop
144      * 20 01 0d b8 00 00 00 00 00 00 00 00 00 00 00 01 <- global
145      * fe 80 00 00 00 00 00 00 c0 01 0b ff fe 7e 00 <- link local
146      * 00 <- reserved
147      *
148      * //NLRI
149      * 40 20 01 0d b8 00 01 00 02 <- IPv6 Prefix (2001:db8:1:2:: / 64)
150      * 40 20 01 0d b8 00 01 00 01 <- IPv6 Prefix (2001:db8:1:1:: / 64)
151      * 40 20 01 0d b8 00 01 00 00 <- IPv6 Prefix (2001:db8:1:: / 64)
152      *
153      */
154     @Test
155     public void testIPv6Nlri() throws Exception {
156         final Update message = (Update) messageRegistry.parseMessage(Unpooled.wrappedBuffer(input.get(1)), null);
157
158         // check fields
159         assertNull(message.getWithdrawnRoutes());
160
161         final UpdateBuilder builder = new UpdateBuilder();
162
163         // check NLRI
164
165         final List<Ipv6Prefixes> prefs = new ArrayList<>();
166         prefs.add(new Ipv6PrefixesBuilder().setPrefix(new Ipv6Prefix("2001:db8:1:2::/64")).build());
167         prefs.add(new Ipv6PrefixesBuilder().setPrefix(new Ipv6Prefix("2001:db8:1:1::/64")).build());
168         prefs.add(new Ipv6PrefixesBuilder().setPrefix(new Ipv6Prefix("2001:db8:1::/64")).build());
169
170         assertNull(message.getNlri());
171
172         // attributes
173         final List<AsNumber> asNumbers = new ArrayList<>();
174         asNumbers.add(new AsNumber(Uint32.valueOf(65001)));
175         final List<Segments> asPath = new ArrayList<>();
176         asPath.add(new SegmentsBuilder().setAsSequence(asNumbers).build());
177
178         final Ipv6NextHopCase nextHop = new Ipv6NextHopCaseBuilder().setIpv6NextHop(
179                 new Ipv6NextHopBuilder().setGlobal(new Ipv6AddressNoZone("2001:db8::1"))
180                         .setLinkLocal(new Ipv6AddressNoZone("fe80::c001:bff:fe7e:0")).build()).build();
181
182         final List<ClusterIdentifier> clusters = Lists.newArrayList(
183             new ClusterIdentifier(new Ipv4AddressNoZone("1.2.3.4")),
184                 new ClusterIdentifier(new Ipv4AddressNoZone("5.6.7.8")));
185
186         // check path attributes
187
188         final Attributes attrs = message.getAttributes();
189
190         final AttributesBuilder paBuilder = new AttributesBuilder();
191
192         paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
193         assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
194
195         paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
196         assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
197
198         paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed(Uint32.ZERO).build());
199         assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
200
201         paBuilder.setOriginatorId(new OriginatorIdBuilder().setOriginator(new Ipv4AddressNoZone("127.0.0.1")).build());
202         assertEquals(paBuilder.getOriginatorId(), attrs.getOriginatorId());
203
204         paBuilder.setClusterId(new ClusterIdBuilder().setCluster(clusters).build());
205         assertEquals(paBuilder.getClusterId(), attrs.getClusterId());
206
207         final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
208         mpBuilder.setAfi(Ipv6AddressFamily.class);
209         mpBuilder.setSafi(UnicastSubsequentAddressFamily.class);
210         mpBuilder.setCNextHop(nextHop);
211         mpBuilder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
212                 new DestinationIpv6CaseBuilder().setDestinationIpv6(new DestinationIpv6Builder()
213                         .setIpv6Prefixes(prefs).build()).build()).build());
214
215         paBuilder.addAugmentation(Attributes1.class, new Attributes1Builder()
216                 .setMpReachNlri(mpBuilder.build()).build());
217         assertEquals(paBuilder.augmentation(Attributes1.class).getMpReachNlri(),
218                 attrs.augmentation(Attributes1.class).getMpReachNlri());
219         paBuilder.setUnrecognizedAttributes(Collections.emptyList());
220         // check API message
221
222         builder.setAttributes(paBuilder.build());
223         assertEquals(builder.build(), message);
224
225         final ByteBuf buffer = Unpooled.buffer();
226         messageRegistry.serializeMessage(message, buffer);
227         assertArrayEquals(input.get(1), ByteArray.readAllBytes(buffer));
228     }
229
230     /*
231      * Tests withdrawn routes with malformed attribute.
232      *
233      * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
234      * 00 36 <- length (54) - including header
235      * 02 <- message type
236      * 00 00 <- withdrawn routes length
237      * 00 1b <- total path attribute length (27)
238      * 40 <- attribute flags
239      * 01 <- attribute type code (origin)
240      * 01 <- WRONG attribute length
241      * 00 <- Origin value (IGP)
242      * 40 <- attribute flags
243      * 03 <- attribute type code (Next Hop)
244      * 04 <- attribute length
245      * 0a 00 00 02 <- value (10.0.0.2)
246      * 40 <- attribute flags
247      * 0e <- attribute type code (MP_REACH)
248      * 0d <- attribute length
249      * 00 01 <- AFI (Ipv4)
250      * 01 <- SAFI (Unicast)
251      * 04 <- next hop length
252      * ff ff ff ff <- next hop
253      * 00 <- reserved
254      * 18 <- length
255      * 0a 00 01 <- prefix (10.0.1.0)
256      * //NLRI
257      * 18 <- length
258      * 0a 00 02 <- prefix (10.0.2.0)
259      */
260     @Test
261     public void testParseUpdateMessageWithMalformedAttributes() throws Exception {
262         final PeerSpecificParserConstraintImpl constraint = new PeerSpecificParserConstraintImpl();
263         constraint.addPeerConstraint(RevisedErrorHandlingSupport.class,
264                 RevisedErrorHandlingSupportImpl.forExternalPeer());
265         final Update message = (Update) messageRegistry.parseMessage(Unpooled.wrappedBuffer(input.get(0)), constraint);
266         assertNotNull(message);
267         assertNull(message.getNlri());
268         final List<WithdrawnRoutes> withdrawnRoutes = message.getWithdrawnRoutes();
269         assertNotNull(withdrawnRoutes);
270         assertEquals(1, withdrawnRoutes.size());
271         final Attributes attributes = message.getAttributes();
272         assertNotNull(attributes);
273         assertNull(attributes.augmentation(Attributes1.class));
274         final Attributes2 attributes2 = attributes.augmentation(Attributes2.class);
275         assertNotNull(attributes2);
276         final MpUnreachNlri mpUnreachNlri = attributes2.getMpUnreachNlri();
277         assertNotNull(mpUnreachNlri);
278     }
279 }