2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.protocol.bgp.inet.codec;
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNull;
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.UnrecognizedAttributes;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.as.path.Segments;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.as.path.SegmentsBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1Builder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlriBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpOrigin;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ClusterIdentifier;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv6NextHopCase;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv6NextHopCaseBuilder;
59 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;
61 public class BGPParserTest {
63 private static int MAX_SIZE = 300;
65 private static MessageRegistry messageRegistry;
67 private static byte[] input;
70 public static void setUp() throws Exception {
71 messageRegistry = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry();
73 final String name = "/up2.bin";
74 try (final InputStream is = BGPParserTest.class.getResourceAsStream(name)) {
76 throw new IOException("Failed to get resource " + name);
78 final ByteArrayOutputStream bis = new ByteArrayOutputStream();
79 final byte[] data = new byte[MAX_SIZE];
81 while ((nRead = is.read(data, 0, data.length)) != -1) {
82 bis.write(data, 0, nRead);
86 input = bis.toByteArray();
92 * Tests IPv6 NEXT_HOP, NLRI, ORIGIN.IGP, MULTI_EXIT_DISC, ORIGINATOR-ID, CLUSTER_LIST.
94 * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
95 * 00 80 <- length (128) - including header
97 * 00 00 <- withdrawn routes length
98 * 00 69 <- total path attribute length (105)
99 * 40 <- attribute flags
100 * 01 <- attribute type code (origin)
101 * 01 <- attribute length
102 * 00 <- Origin value (IGP)
103 * 40 <- attribute flags
104 * 02 <- attribute type code (as path)
105 * 06 <- attribute length
107 * 01 <- path segment count
108 * 00 00 fd e9 <- path segment value (65001)
109 * 40 <- attribute flags
110 * 03 <- attribute type code (next hop)
111 * 04 <- attribute length
112 * 0a 00 00 00 <- next hop value (10.0.0.0)
113 * 80 <- attribute flags
114 * 04 <- attribute type code (multi exit disc)
115 * 04 <- attribute length
116 * 00 00 00 00 <- value
117 * 80 <- attribute flags
118 * 09 <- attribute type code (originator id)
119 * 04 <- attribute length
120 * 7f 00 00 01 <- value (localhost ip)
121 * 80 <- attribute flags
122 * 0a <- attribute type code (cluster list)
123 * 08 <- attribute length
124 * 01 02 03 04 <- value
125 * 05 06 07 08 <- value
126 * 80 <- attribute flags
127 * 0e <- attribute type code (mp reach nlri)
128 * 40 <- attribute length
129 * 00 02 <- AFI (Ipv6)
130 * 01 <- SAFI (Unicast)
131 * 20 <- length of next hop
132 * 20 01 0d b8 00 00 00 00 00 00 00 00 00 00 00 01 <- global
133 * fe 80 00 00 00 00 00 00 c0 01 0b ff fe 7e 00 <- link local
137 * 40 20 01 0d b8 00 01 00 02 <- IPv6 Prefix (2001:db8:1:2:: / 64)
138 * 40 20 01 0d b8 00 01 00 01 <- IPv6 Prefix (2001:db8:1:1:: / 64)
139 * 40 20 01 0d b8 00 01 00 00 <- IPv6 Prefix (2001:db8:1:: / 64)
143 public void testIPv6Nlri() throws Exception {
144 final Update message = (Update) messageRegistry.parseMessage(Unpooled.wrappedBuffer(input), null);
147 assertNull(message.getWithdrawnRoutes());
149 final UpdateBuilder builder = new UpdateBuilder();
153 final List<Ipv6Prefixes> prefs = new ArrayList<>();
154 prefs.add(new Ipv6PrefixesBuilder().setPrefix(new Ipv6Prefix("2001:db8:1:2::/64")).build());
155 prefs.add(new Ipv6PrefixesBuilder().setPrefix(new Ipv6Prefix("2001:db8:1:1::/64")).build());
156 prefs.add(new Ipv6PrefixesBuilder().setPrefix(new Ipv6Prefix("2001:db8:1::/64")).build());
158 assertNull(message.getNlri());
161 final List<AsNumber> asNumbers = new ArrayList<AsNumber>();
162 asNumbers.add(new AsNumber(65001L));
163 final List<Segments> asPath = Lists.newArrayList();
164 asPath.add(new SegmentsBuilder().setAsSequence(asNumbers).build());
166 final Ipv6NextHopCase nextHop = new Ipv6NextHopCaseBuilder().setIpv6NextHop(
167 new Ipv6NextHopBuilder().setGlobal(new Ipv6Address("2001:db8::1")).setLinkLocal(new Ipv6Address("fe80::c001:bff:fe7e:0")).build()).build();
169 final List<ClusterIdentifier> clusters = Lists.newArrayList(new ClusterIdentifier(new Ipv4Address("1.2.3.4")),
170 new ClusterIdentifier(new Ipv4Address("5.6.7.8")));
172 // check path attributes
174 final Attributes attrs = message.getAttributes();
176 final AttributesBuilder paBuilder = new AttributesBuilder();
178 paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
179 assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
181 paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
182 assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
184 paBuilder.setMultiExitDisc(new MultiExitDiscBuilder().setMed((long) 0).build());
185 assertEquals(paBuilder.getMultiExitDisc(), attrs.getMultiExitDisc());
187 paBuilder.setOriginatorId(new OriginatorIdBuilder().setOriginator(new Ipv4Address("127.0.0.1")).build());
188 assertEquals(paBuilder.getOriginatorId(), attrs.getOriginatorId());
190 paBuilder.setClusterId(new ClusterIdBuilder().setCluster(clusters).build());
191 assertEquals(paBuilder.getClusterId(), attrs.getClusterId());
193 final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
194 mpBuilder.setAfi(Ipv6AddressFamily.class);
195 mpBuilder.setSafi(UnicastSubsequentAddressFamily.class);
196 mpBuilder.setCNextHop(nextHop);
197 mpBuilder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
198 new DestinationIpv6CaseBuilder().setDestinationIpv6(new DestinationIpv6Builder().setIpv6Prefixes(prefs).build()).build()).build());
200 paBuilder.addAugmentation(Attributes1.class, new Attributes1Builder().setMpReachNlri(mpBuilder.build()).build());
201 assertEquals(paBuilder.getAugmentation(Attributes1.class).getMpReachNlri(),
202 attrs.getAugmentation(Attributes1.class).getMpReachNlri());
203 paBuilder.setUnrecognizedAttributes(Collections.<UnrecognizedAttributes> emptyList());
206 builder.setAttributes(paBuilder.build());
207 assertEquals(builder.build(), message);
209 final ByteBuf buffer = Unpooled.buffer();
210 messageRegistry.serializeMessage(message, buffer);
211 assertArrayEquals(input, ByteArray.readAllBytes(buffer));