Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Jonathan Hart | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame^] | 2 | * Copyright 2017-present Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Jonathan Hart | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame^] | 16 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 17 | package org.onosproject.routing.bgp; |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 18 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 19 | import com.google.common.net.InetAddresses; |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 20 | import org.hamcrest.Description; |
| 21 | import org.hamcrest.TypeSafeMatcher; |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 22 | import org.jboss.netty.bootstrap.ClientBootstrap; |
| 23 | import org.jboss.netty.buffer.ChannelBuffer; |
| 24 | import org.jboss.netty.channel.Channel; |
| 25 | import org.jboss.netty.channel.ChannelFactory; |
| 26 | import org.jboss.netty.channel.ChannelPipeline; |
| 27 | import org.jboss.netty.channel.ChannelPipelineFactory; |
| 28 | import org.jboss.netty.channel.Channels; |
| 29 | import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; |
| 30 | import org.junit.After; |
| 31 | import org.junit.Before; |
| 32 | import org.junit.Test; |
Pavlin Radoslavov | d26f57a | 2014-10-23 17:19:45 -0700 | [diff] [blame] | 33 | import org.onlab.junit.TestUtils; |
| 34 | import org.onlab.junit.TestUtils.TestUtilsException; |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 35 | import org.onlab.packet.Ip4Address; |
| 36 | import org.onlab.packet.Ip4Prefix; |
Jonathan Hart | 1ad75f2 | 2016-04-13 21:24:13 -0700 | [diff] [blame] | 37 | import org.onosproject.incubator.net.routing.RouteAdminService; |
Jonathan Hart | d24fafb | 2015-02-09 17:55:32 -0800 | [diff] [blame] | 38 | import org.osgi.service.component.ComponentContext; |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 39 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 40 | import java.net.InetAddress; |
| 41 | import java.net.InetSocketAddress; |
| 42 | import java.net.SocketAddress; |
| 43 | import java.util.ArrayList; |
| 44 | import java.util.Collection; |
Jonathan Hart | d24fafb | 2015-02-09 17:55:32 -0800 | [diff] [blame] | 45 | import java.util.Dictionary; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 46 | import java.util.LinkedList; |
| 47 | import java.util.concurrent.Executors; |
| 48 | import java.util.concurrent.TimeUnit; |
| 49 | |
Jonathan Hart | d24fafb | 2015-02-09 17:55:32 -0800 | [diff] [blame] | 50 | import static org.easymock.EasyMock.createMock; |
Jonathan Hart | 1ad75f2 | 2016-04-13 21:24:13 -0700 | [diff] [blame] | 51 | import static org.easymock.EasyMock.createNiceMock; |
Jonathan Hart | d24fafb | 2015-02-09 17:55:32 -0800 | [diff] [blame] | 52 | import static org.easymock.EasyMock.expect; |
| 53 | import static org.easymock.EasyMock.replay; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 54 | import static org.hamcrest.Matchers.hasSize; |
| 55 | import static org.hamcrest.Matchers.is; |
| 56 | import static org.hamcrest.Matchers.notNullValue; |
| 57 | import static org.junit.Assert.assertThat; |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * Unit tests for the BgpSessionManager class. |
| 61 | */ |
| 62 | public class BgpSessionManagerTest { |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 63 | private static final Ip4Address IP_LOOPBACK_ID = |
| 64 | Ip4Address.valueOf("127.0.0.1"); |
| 65 | private static final Ip4Address BGP_PEER1_ID = |
| 66 | Ip4Address.valueOf("10.0.0.1"); |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 67 | private static final Ip4Address BGP_PEER2_ID = |
| 68 | Ip4Address.valueOf("10.0.0.2"); |
| 69 | private static final Ip4Address BGP_PEER3_ID = |
| 70 | Ip4Address.valueOf("10.0.0.3"); |
| 71 | private static final Ip4Address NEXT_HOP1_ROUTER = |
| 72 | Ip4Address.valueOf("10.20.30.41"); |
| 73 | private static final Ip4Address NEXT_HOP2_ROUTER = |
| 74 | Ip4Address.valueOf("10.20.30.42"); |
| 75 | private static final Ip4Address NEXT_HOP3_ROUTER = |
| 76 | Ip4Address.valueOf("10.20.30.43"); |
| 77 | |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 78 | private static final long DEFAULT_LOCAL_PREF = 10; |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 79 | private static final long BETTER_LOCAL_PREF = 20; |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 80 | private static final long DEFAULT_MULTI_EXIT_DISC = 20; |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 81 | private static final long BETTER_MULTI_EXIT_DISC = 30; |
| 82 | |
| 83 | BgpRouteEntry.AsPath asPathShort; |
| 84 | BgpRouteEntry.AsPath asPathLong; |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 85 | |
Pavlin Radoslavov | 23c0569 | 2014-12-02 13:18:10 -0800 | [diff] [blame] | 86 | // Timeout waiting for a message to be received |
| 87 | private static final int MESSAGE_TIMEOUT_MS = 5000; // 5s |
| 88 | |
Jonathan Hart | 1ad75f2 | 2016-04-13 21:24:13 -0700 | [diff] [blame] | 89 | private RouteAdminService routeService; |
| 90 | |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 91 | // The BGP Session Manager to test |
| 92 | private BgpSessionManager bgpSessionManager; |
| 93 | |
| 94 | // Remote Peer state |
Pavlin Radoslavov | 4b5acae | 2015-01-28 17:09:45 -0800 | [diff] [blame] | 95 | private final Collection<TestBgpPeer> peers = new LinkedList<>(); |
| 96 | TestBgpPeer peer1; |
| 97 | TestBgpPeer peer2; |
| 98 | TestBgpPeer peer3; |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 99 | |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 100 | // Local BGP per-peer session state |
| 101 | BgpSession bgpSession1; |
| 102 | BgpSession bgpSession2; |
| 103 | BgpSession bgpSession3; |
| 104 | |
| 105 | // The socket that the remote peers should connect to |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 106 | private InetSocketAddress connectToSocket; |
| 107 | |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 108 | /** |
| 109 | * A class to capture the state for a BGP peer. |
| 110 | */ |
| 111 | private final class TestBgpPeer { |
| 112 | private final Ip4Address peerId; |
| 113 | private ClientBootstrap peerBootstrap; |
| 114 | private TestBgpPeerChannelHandler peerChannelHandler; |
| 115 | private TestBgpPeerFrameDecoder peerFrameDecoder = |
| 116 | new TestBgpPeerFrameDecoder(); |
| 117 | |
| 118 | /** |
| 119 | * Constructor. |
| 120 | * |
| 121 | * @param peerId the peer ID |
| 122 | */ |
| 123 | private TestBgpPeer(Ip4Address peerId) { |
| 124 | this.peerId = peerId; |
| 125 | peerChannelHandler = new TestBgpPeerChannelHandler(peerId); |
| 126 | } |
| 127 | |
| 128 | /** |
Jonathan Hart | de15e1c | 2016-02-03 10:51:50 -0800 | [diff] [blame] | 129 | * Starts up the BGP peer and connects it to the tested BgpSessionManager |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 130 | * instance. |
| 131 | * |
| 132 | * @param connectToSocket the socket to connect to |
| 133 | */ |
| 134 | private void connect(InetSocketAddress connectToSocket) |
| 135 | throws InterruptedException { |
| 136 | // |
| 137 | // Setup the BGP Peer, i.e., the "remote" BGP router that will |
| 138 | // initiate the BGP connection, send BGP UPDATE messages, etc. |
| 139 | // |
| 140 | ChannelFactory channelFactory = |
| 141 | new NioClientSocketChannelFactory( |
| 142 | Executors.newCachedThreadPool(), |
| 143 | Executors.newCachedThreadPool()); |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 144 | ChannelPipelineFactory pipelineFactory = () -> { |
| 145 | // Setup the transmitting pipeline |
| 146 | ChannelPipeline pipeline = Channels.pipeline(); |
| 147 | pipeline.addLast("TestBgpPeerFrameDecoder", |
| 148 | peerFrameDecoder); |
| 149 | pipeline.addLast("TestBgpPeerChannelHandler", |
| 150 | peerChannelHandler); |
| 151 | return pipeline; |
| 152 | }; |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 153 | |
| 154 | peerBootstrap = new ClientBootstrap(channelFactory); |
| 155 | peerBootstrap.setOption("child.keepAlive", true); |
| 156 | peerBootstrap.setOption("child.tcpNoDelay", true); |
| 157 | peerBootstrap.setPipelineFactory(pipelineFactory); |
| 158 | peerBootstrap.connect(connectToSocket); |
| 159 | |
| 160 | boolean result; |
| 161 | // Wait until the OPEN message is received |
| 162 | result = peerFrameDecoder.receivedOpenMessageLatch.await( |
| 163 | MESSAGE_TIMEOUT_MS, |
| 164 | TimeUnit.MILLISECONDS); |
| 165 | assertThat(result, is(true)); |
| 166 | // Wait until the KEEPALIVE message is received |
| 167 | result = peerFrameDecoder.receivedKeepaliveMessageLatch.await( |
| 168 | MESSAGE_TIMEOUT_MS, |
| 169 | TimeUnit.MILLISECONDS); |
| 170 | assertThat(result, is(true)); |
| 171 | |
| 172 | for (BgpSession bgpSession : bgpSessionManager.getBgpSessions()) { |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 173 | if (bgpSession.remoteInfo().bgpId().equals(BGP_PEER1_ID)) { |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 174 | bgpSession1 = bgpSession; |
| 175 | } |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 176 | if (bgpSession.remoteInfo().bgpId().equals(BGP_PEER2_ID)) { |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 177 | bgpSession2 = bgpSession; |
| 178 | } |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 179 | if (bgpSession.remoteInfo().bgpId().equals(BGP_PEER3_ID)) { |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 180 | bgpSession3 = bgpSession; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Class that implements a matcher for BgpRouteEntry by considering |
| 188 | * the BGP peer the entry was received from. |
| 189 | */ |
| 190 | private static final class BgpRouteEntryAndPeerMatcher |
| 191 | extends TypeSafeMatcher<Collection<BgpRouteEntry>> { |
| 192 | private final BgpRouteEntry bgpRouteEntry; |
| 193 | |
| 194 | private BgpRouteEntryAndPeerMatcher(BgpRouteEntry bgpRouteEntry) { |
| 195 | this.bgpRouteEntry = bgpRouteEntry; |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public boolean matchesSafely(Collection<BgpRouteEntry> entries) { |
| 200 | for (BgpRouteEntry entry : entries) { |
| 201 | if (bgpRouteEntry.equals(entry) && |
| 202 | bgpRouteEntry.getBgpSession() == entry.getBgpSession()) { |
| 203 | return true; |
| 204 | } |
| 205 | } |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public void describeTo(Description description) { |
| 211 | description.appendText("BGP route entry lookup for entry \""). |
| 212 | appendText(bgpRouteEntry.toString()). |
| 213 | appendText("\""); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * A helper method used for testing whether a collection of |
| 219 | * BGP route entries contains an entry from a specific BGP peer. |
| 220 | * |
| 221 | * @param bgpRouteEntry the BGP route entry to test |
| 222 | * @return an instance of BgpRouteEntryAndPeerMatcher that implements |
| 223 | * the matching logic |
| 224 | */ |
| 225 | private static BgpRouteEntryAndPeerMatcher hasBgpRouteEntry( |
| 226 | BgpRouteEntry bgpRouteEntry) { |
| 227 | return new BgpRouteEntryAndPeerMatcher(bgpRouteEntry); |
| 228 | } |
| 229 | |
Ray Milkey | 8dc8208 | 2015-02-20 16:22:38 -0800 | [diff] [blame] | 230 | @SuppressWarnings("unchecked") |
Jonathan Hart | 1ad75f2 | 2016-04-13 21:24:13 -0700 | [diff] [blame] | 231 | private void getDictionaryMock(ComponentContext componentContext) { |
Ray Milkey | 7c25182 | 2016-04-06 17:38:25 -0700 | [diff] [blame] | 232 | Dictionary dictionary = createMock(Dictionary.class); |
Ray Milkey | 8dc8208 | 2015-02-20 16:22:38 -0800 | [diff] [blame] | 233 | expect(dictionary.get("bgpPort")).andReturn("0"); |
| 234 | replay(dictionary); |
| 235 | expect(componentContext.getProperties()).andReturn(dictionary); |
Ray Milkey | 8dc8208 | 2015-02-20 16:22:38 -0800 | [diff] [blame] | 236 | } |
| 237 | |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 238 | @Before |
| 239 | public void setUp() throws Exception { |
Pavlin Radoslavov | 4b5acae | 2015-01-28 17:09:45 -0800 | [diff] [blame] | 240 | peer1 = new TestBgpPeer(BGP_PEER1_ID); |
| 241 | peer2 = new TestBgpPeer(BGP_PEER2_ID); |
| 242 | peer3 = new TestBgpPeer(BGP_PEER3_ID); |
| 243 | peers.clear(); |
| 244 | peers.add(peer1); |
| 245 | peers.add(peer2); |
| 246 | peers.add(peer3); |
| 247 | |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 248 | // |
| 249 | // Setup the BGP Session Manager to test, and start listening for BGP |
| 250 | // connections. |
| 251 | // |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 252 | bgpSessionManager = new BgpSessionManager(); |
Jonathan Hart | 1ad75f2 | 2016-04-13 21:24:13 -0700 | [diff] [blame] | 253 | |
| 254 | routeService = createNiceMock(RouteAdminService.class); |
| 255 | replay(routeService); |
| 256 | bgpSessionManager.routeService = routeService; |
| 257 | |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 258 | // NOTE: We use port 0 to bind on any available port |
Jonathan Hart | d24fafb | 2015-02-09 17:55:32 -0800 | [diff] [blame] | 259 | ComponentContext componentContext = createMock(ComponentContext.class); |
Jonathan Hart | 1ad75f2 | 2016-04-13 21:24:13 -0700 | [diff] [blame] | 260 | getDictionaryMock(componentContext); |
Jonathan Hart | d24fafb | 2015-02-09 17:55:32 -0800 | [diff] [blame] | 261 | replay(componentContext); |
| 262 | bgpSessionManager.activate(componentContext); |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 263 | |
| 264 | // Get the port number the BGP Session Manager is listening on |
| 265 | Channel serverChannel = TestUtils.getField(bgpSessionManager, |
| 266 | "serverChannel"); |
| 267 | SocketAddress socketAddress = serverChannel.getLocalAddress(); |
| 268 | InetSocketAddress inetSocketAddress = |
| 269 | (InetSocketAddress) socketAddress; |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 270 | InetAddress connectToAddress = InetAddresses.forString("127.0.0.1"); |
| 271 | connectToSocket = new InetSocketAddress(connectToAddress, |
| 272 | inetSocketAddress.getPort()); |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 273 | |
| 274 | // |
| 275 | // Setup the AS Paths |
| 276 | // |
| 277 | ArrayList<BgpRouteEntry.PathSegment> pathSegments = new ArrayList<>(); |
| 278 | byte pathSegmentType1 = (byte) BgpConstants.Update.AsPath.AS_SEQUENCE; |
| 279 | ArrayList<Long> segmentAsNumbers1 = new ArrayList<>(); |
Sho SHIMIZU | c14b2a2 | 2015-05-05 18:15:13 -0700 | [diff] [blame] | 280 | segmentAsNumbers1.add(65010L); |
| 281 | segmentAsNumbers1.add(65020L); |
| 282 | segmentAsNumbers1.add(65030L); |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 283 | BgpRouteEntry.PathSegment pathSegment1 = |
| 284 | new BgpRouteEntry.PathSegment(pathSegmentType1, segmentAsNumbers1); |
| 285 | pathSegments.add(pathSegment1); |
Ray Milkey | 8dc8208 | 2015-02-20 16:22:38 -0800 | [diff] [blame] | 286 | asPathShort = new BgpRouteEntry.AsPath(new ArrayList<>(pathSegments)); |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 287 | // |
| 288 | byte pathSegmentType2 = (byte) BgpConstants.Update.AsPath.AS_SET; |
| 289 | ArrayList<Long> segmentAsNumbers2 = new ArrayList<>(); |
Sho SHIMIZU | c14b2a2 | 2015-05-05 18:15:13 -0700 | [diff] [blame] | 290 | segmentAsNumbers2.add(65041L); |
| 291 | segmentAsNumbers2.add(65042L); |
| 292 | segmentAsNumbers2.add(65043L); |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 293 | BgpRouteEntry.PathSegment pathSegment2 = |
| 294 | new BgpRouteEntry.PathSegment(pathSegmentType2, segmentAsNumbers2); |
| 295 | pathSegments.add(pathSegment2); |
| 296 | // |
| 297 | asPathLong = new BgpRouteEntry.AsPath(pathSegments); |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | @After |
| 301 | public void tearDown() throws Exception { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 302 | bgpSessionManager.stop(); |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 303 | bgpSessionManager = null; |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Gets BGP RIB-IN routes by waiting until they are received. |
Pavlin Radoslavov | f8a0f6c | 2015-02-04 15:31:47 -0800 | [diff] [blame] | 308 | * <p> |
Pavlin Radoslavov | 55b5f51 | 2014-12-08 09:59:35 -0800 | [diff] [blame] | 309 | * NOTE: We keep checking once every 10ms the number of received routes, |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 310 | * up to 5 seconds. |
Pavlin Radoslavov | f8a0f6c | 2015-02-04 15:31:47 -0800 | [diff] [blame] | 311 | * </p> |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 312 | * |
| 313 | * @param bgpSession the BGP session that is expected to receive the |
| 314 | * routes |
| 315 | * @param expectedRoutes the expected number of routes |
| 316 | * @return the BGP RIB-IN routes as received within the expected |
| 317 | * time interval |
| 318 | */ |
| 319 | private Collection<BgpRouteEntry> waitForBgpRibIn(BgpSession bgpSession, |
| 320 | long expectedRoutes) |
| 321 | throws InterruptedException { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 322 | Collection<BgpRouteEntry> bgpRibIn = bgpSession.getBgpRibIn4(); |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 323 | |
Pavlin Radoslavov | 55b5f51 | 2014-12-08 09:59:35 -0800 | [diff] [blame] | 324 | final int maxChecks = 500; // Max wait of 5 seconds |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 325 | for (int i = 0; i < maxChecks; i++) { |
| 326 | if (bgpRibIn.size() == expectedRoutes) { |
| 327 | break; |
| 328 | } |
Pavlin Radoslavov | 55b5f51 | 2014-12-08 09:59:35 -0800 | [diff] [blame] | 329 | Thread.sleep(10); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 330 | bgpRibIn = bgpSession.getBgpRibIn4(); |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | return bgpRibIn; |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Gets BGP merged routes by waiting until they are received. |
Pavlin Radoslavov | f8a0f6c | 2015-02-04 15:31:47 -0800 | [diff] [blame] | 338 | * <p> |
Pavlin Radoslavov | 55b5f51 | 2014-12-08 09:59:35 -0800 | [diff] [blame] | 339 | * NOTE: We keep checking once every 10ms the number of received routes, |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 340 | * up to 5 seconds. |
Pavlin Radoslavov | f8a0f6c | 2015-02-04 15:31:47 -0800 | [diff] [blame] | 341 | * </p> |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 342 | * |
| 343 | * @param expectedRoutes the expected number of routes |
| 344 | * @return the BGP Session Manager routes as received within the expected |
| 345 | * time interval |
| 346 | */ |
| 347 | private Collection<BgpRouteEntry> waitForBgpRoutes(long expectedRoutes) |
| 348 | throws InterruptedException { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 349 | Collection<BgpRouteEntry> bgpRoutes = |
| 350 | bgpSessionManager.getBgpRoutes4(); |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 351 | |
Pavlin Radoslavov | 55b5f51 | 2014-12-08 09:59:35 -0800 | [diff] [blame] | 352 | final int maxChecks = 500; // Max wait of 5 seconds |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 353 | for (int i = 0; i < maxChecks; i++) { |
| 354 | if (bgpRoutes.size() == expectedRoutes) { |
| 355 | break; |
| 356 | } |
Pavlin Radoslavov | 55b5f51 | 2014-12-08 09:59:35 -0800 | [diff] [blame] | 357 | Thread.sleep(10); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 358 | bgpRoutes = bgpSessionManager.getBgpRoutes4(); |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | return bgpRoutes; |
| 362 | } |
| 363 | |
| 364 | /** |
Pavlin Radoslavov | f8a0f6c | 2015-02-04 15:31:47 -0800 | [diff] [blame] | 365 | * Gets a merged BGP route by waiting until it is received. |
| 366 | * <p> |
| 367 | * NOTE: We keep checking once every 10ms whether the route is received, |
| 368 | * up to 5 seconds. |
| 369 | * </p> |
| 370 | * |
| 371 | * @param expectedRoute the expected route |
| 372 | * @return the merged BGP route if received within the expected time |
| 373 | * interval, otherwise null |
| 374 | */ |
| 375 | private BgpRouteEntry waitForBgpRoute(BgpRouteEntry expectedRoute) |
| 376 | throws InterruptedException { |
| 377 | Collection<BgpRouteEntry> bgpRoutes = |
| 378 | bgpSessionManager.getBgpRoutes4(); |
| 379 | |
| 380 | final int maxChecks = 500; // Max wait of 5 seconds |
| 381 | for (int i = 0; i < maxChecks; i++) { |
| 382 | for (BgpRouteEntry bgpRouteEntry : bgpRoutes) { |
| 383 | if (bgpRouteEntry.equals(expectedRoute) && |
| 384 | bgpRouteEntry.getBgpSession() == |
| 385 | expectedRoute.getBgpSession()) { |
| 386 | return bgpRouteEntry; |
| 387 | } |
| 388 | } |
| 389 | Thread.sleep(10); |
| 390 | bgpRoutes = bgpSessionManager.getBgpRoutes4(); |
| 391 | } |
| 392 | |
| 393 | return null; |
| 394 | } |
| 395 | |
| 396 | /** |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 397 | * Tests that the BGP OPEN messages have been exchanged, followed by |
| 398 | * KEEPALIVE. |
| 399 | * <p> |
| 400 | * The BGP Peer opens the sessions and transmits OPEN Message, eventually |
| 401 | * followed by KEEPALIVE. The tested BGP listener should respond by |
| 402 | * OPEN Message, followed by KEEPALIVE. |
Pavlin Radoslavov | f8a0f6c | 2015-02-04 15:31:47 -0800 | [diff] [blame] | 403 | * </p> |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 404 | * |
| 405 | * @throws TestUtilsException TestUtils error |
| 406 | */ |
| 407 | @Test |
| 408 | public void testExchangedBgpOpenMessages() |
| 409 | throws InterruptedException, TestUtilsException { |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 410 | // Initiate the connections |
| 411 | peer1.connect(connectToSocket); |
| 412 | peer2.connect(connectToSocket); |
| 413 | peer3.connect(connectToSocket); |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 414 | |
| 415 | // |
| 416 | // Test the fields from the BGP OPEN message: |
| 417 | // BGP version, AS number, BGP ID |
| 418 | // |
Pavlin Radoslavov | 4b5acae | 2015-01-28 17:09:45 -0800 | [diff] [blame] | 419 | for (TestBgpPeer peer : peers) { |
| 420 | assertThat(peer.peerFrameDecoder.remoteInfo.bgpVersion(), |
| 421 | is(BgpConstants.BGP_VERSION)); |
| 422 | assertThat(peer.peerFrameDecoder.remoteInfo.bgpId(), |
| 423 | is(IP_LOOPBACK_ID)); |
| 424 | assertThat(peer.peerFrameDecoder.remoteInfo.asNumber(), |
| 425 | is(TestBgpPeerChannelHandler.PEER_AS)); |
| 426 | } |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 427 | |
| 428 | // |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 429 | // Test that the BgpSession instances have been created |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 430 | // |
| 431 | assertThat(bgpSessionManager.getMyBgpId(), is(IP_LOOPBACK_ID)); |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 432 | assertThat(bgpSessionManager.getBgpSessions(), hasSize(3)); |
| 433 | assertThat(bgpSession1, notNullValue()); |
| 434 | assertThat(bgpSession2, notNullValue()); |
| 435 | assertThat(bgpSession3, notNullValue()); |
| 436 | for (BgpSession bgpSession : bgpSessionManager.getBgpSessions()) { |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 437 | long sessionAs = bgpSession.localInfo().asNumber(); |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 438 | assertThat(sessionAs, is(TestBgpPeerChannelHandler.PEER_AS)); |
| 439 | } |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Pavlin Radoslavov | 4b5acae | 2015-01-28 17:09:45 -0800 | [diff] [blame] | 442 | |
| 443 | /** |
| 444 | * Tests that the BGP OPEN with Capability messages have been exchanged, |
| 445 | * followed by KEEPALIVE. |
| 446 | * <p> |
| 447 | * The BGP Peer opens the sessions and transmits OPEN Message, eventually |
| 448 | * followed by KEEPALIVE. The tested BGP listener should respond by |
| 449 | * OPEN Message, followed by KEEPALIVE. |
Pavlin Radoslavov | f8a0f6c | 2015-02-04 15:31:47 -0800 | [diff] [blame] | 450 | * </p> |
Pavlin Radoslavov | 4b5acae | 2015-01-28 17:09:45 -0800 | [diff] [blame] | 451 | * |
| 452 | * @throws TestUtilsException TestUtils error |
| 453 | */ |
| 454 | @Test |
| 455 | public void testExchangedBgpOpenCapabilityMessages() |
| 456 | throws InterruptedException, TestUtilsException { |
| 457 | // |
| 458 | // Setup the BGP Capabilities for all peers |
| 459 | // |
| 460 | for (TestBgpPeer peer : peers) { |
| 461 | peer.peerChannelHandler.localInfo.setIpv4Unicast(); |
| 462 | peer.peerChannelHandler.localInfo.setIpv4Multicast(); |
| 463 | peer.peerChannelHandler.localInfo.setIpv6Unicast(); |
| 464 | peer.peerChannelHandler.localInfo.setIpv6Multicast(); |
| 465 | peer.peerChannelHandler.localInfo.setAs4OctetCapability(); |
| 466 | peer.peerChannelHandler.localInfo.setAs4Number( |
| 467 | TestBgpPeerChannelHandler.PEER_AS4); |
| 468 | } |
| 469 | |
| 470 | // Initiate the connections |
| 471 | peer1.connect(connectToSocket); |
| 472 | peer2.connect(connectToSocket); |
| 473 | peer3.connect(connectToSocket); |
| 474 | |
| 475 | // |
| 476 | // Test the fields from the BGP OPEN message: |
| 477 | // BGP version, BGP ID |
| 478 | // |
| 479 | for (TestBgpPeer peer : peers) { |
| 480 | assertThat(peer.peerFrameDecoder.remoteInfo.bgpVersion(), |
| 481 | is(BgpConstants.BGP_VERSION)); |
| 482 | assertThat(peer.peerFrameDecoder.remoteInfo.bgpId(), |
| 483 | is(IP_LOOPBACK_ID)); |
| 484 | } |
| 485 | |
| 486 | // |
| 487 | // Test that the BgpSession instances have been created, |
| 488 | // and contain the appropriate BGP session information. |
| 489 | // |
| 490 | assertThat(bgpSessionManager.getMyBgpId(), is(IP_LOOPBACK_ID)); |
| 491 | assertThat(bgpSessionManager.getBgpSessions(), hasSize(3)); |
| 492 | assertThat(bgpSession1, notNullValue()); |
| 493 | assertThat(bgpSession2, notNullValue()); |
| 494 | assertThat(bgpSession3, notNullValue()); |
| 495 | for (BgpSession bgpSession : bgpSessionManager.getBgpSessions()) { |
| 496 | BgpSessionInfo localInfo = bgpSession.localInfo(); |
| 497 | assertThat(localInfo.ipv4Unicast(), is(true)); |
| 498 | assertThat(localInfo.ipv4Multicast(), is(true)); |
| 499 | assertThat(localInfo.ipv6Unicast(), is(true)); |
| 500 | assertThat(localInfo.ipv6Multicast(), is(true)); |
| 501 | assertThat(localInfo.as4OctetCapability(), is(true)); |
| 502 | assertThat(localInfo.asNumber(), |
| 503 | is(TestBgpPeerChannelHandler.PEER_AS4)); |
| 504 | assertThat(localInfo.as4Number(), |
| 505 | is(TestBgpPeerChannelHandler.PEER_AS4)); |
| 506 | } |
| 507 | } |
| 508 | |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 509 | /** |
| 510 | * Tests that the BGP UPDATE messages have been received and processed. |
| 511 | */ |
| 512 | @Test |
| 513 | public void testProcessedBgpUpdateMessages() throws InterruptedException { |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 514 | ChannelBuffer message; |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 515 | BgpRouteEntry bgpRouteEntry; |
| 516 | Collection<BgpRouteEntry> bgpRibIn1; |
| 517 | Collection<BgpRouteEntry> bgpRibIn2; |
| 518 | Collection<BgpRouteEntry> bgpRibIn3; |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 519 | Collection<BgpRouteEntry> bgpRoutes; |
| 520 | |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 521 | // Initiate the connections |
| 522 | peer1.connect(connectToSocket); |
| 523 | peer2.connect(connectToSocket); |
| 524 | peer3.connect(connectToSocket); |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 525 | |
| 526 | // Prepare routes to add/delete |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 527 | Collection<Ip4Prefix> addedRoutes = new LinkedList<>(); |
| 528 | Collection<Ip4Prefix> withdrawnRoutes = new LinkedList<>(); |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 529 | |
| 530 | // |
| 531 | // Add and delete some routes |
| 532 | // |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 533 | addedRoutes.add(Ip4Prefix.valueOf("0.0.0.0/0")); |
| 534 | addedRoutes.add(Ip4Prefix.valueOf("20.0.0.0/8")); |
| 535 | addedRoutes.add(Ip4Prefix.valueOf("30.0.0.0/16")); |
| 536 | addedRoutes.add(Ip4Prefix.valueOf("40.0.0.0/24")); |
| 537 | addedRoutes.add(Ip4Prefix.valueOf("50.0.0.0/32")); |
| 538 | withdrawnRoutes.add(Ip4Prefix.valueOf("60.0.0.0/8")); |
| 539 | withdrawnRoutes.add(Ip4Prefix.valueOf("70.0.0.0/16")); |
| 540 | withdrawnRoutes.add(Ip4Prefix.valueOf("80.0.0.0/24")); |
| 541 | withdrawnRoutes.add(Ip4Prefix.valueOf("90.0.0.0/32")); |
Jonathan Hart | 1ad75f2 | 2016-04-13 21:24:13 -0700 | [diff] [blame] | 542 | |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 543 | // Write the routes |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 544 | message = peer1.peerChannelHandler.prepareBgpUpdate( |
| 545 | NEXT_HOP1_ROUTER, |
| 546 | DEFAULT_LOCAL_PREF, |
| 547 | DEFAULT_MULTI_EXIT_DISC, |
| 548 | asPathLong, |
| 549 | addedRoutes, |
| 550 | withdrawnRoutes); |
| 551 | peer1.peerChannelHandler.savedCtx.getChannel().write(message); |
| 552 | // |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 553 | // Check that the routes have been received, processed and stored |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 554 | // |
| 555 | bgpRibIn1 = waitForBgpRibIn(bgpSession1, 5); |
| 556 | assertThat(bgpRibIn1, hasSize(5)); |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 557 | bgpRoutes = waitForBgpRoutes(5); |
| 558 | assertThat(bgpRoutes, hasSize(5)); |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 559 | // |
| 560 | bgpRouteEntry = |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 561 | new BgpRouteEntry(bgpSession1, |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 562 | Ip4Prefix.valueOf("0.0.0.0/0"), |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 563 | NEXT_HOP1_ROUTER, |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 564 | (byte) BgpConstants.Update.Origin.IGP, |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 565 | asPathLong, |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 566 | DEFAULT_LOCAL_PREF); |
| 567 | bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC); |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 568 | assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry)); |
Pavlin Radoslavov | f8a0f6c | 2015-02-04 15:31:47 -0800 | [diff] [blame] | 569 | assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue()); |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 570 | // |
| 571 | bgpRouteEntry = |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 572 | new BgpRouteEntry(bgpSession1, |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 573 | Ip4Prefix.valueOf("20.0.0.0/8"), |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 574 | NEXT_HOP1_ROUTER, |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 575 | (byte) BgpConstants.Update.Origin.IGP, |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 576 | asPathLong, |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 577 | DEFAULT_LOCAL_PREF); |
| 578 | bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC); |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 579 | assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry)); |
Pavlin Radoslavov | f8a0f6c | 2015-02-04 15:31:47 -0800 | [diff] [blame] | 580 | assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue()); |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 581 | // |
| 582 | bgpRouteEntry = |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 583 | new BgpRouteEntry(bgpSession1, |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 584 | Ip4Prefix.valueOf("30.0.0.0/16"), |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 585 | NEXT_HOP1_ROUTER, |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 586 | (byte) BgpConstants.Update.Origin.IGP, |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 587 | asPathLong, |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 588 | DEFAULT_LOCAL_PREF); |
| 589 | bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC); |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 590 | assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry)); |
Pavlin Radoslavov | f8a0f6c | 2015-02-04 15:31:47 -0800 | [diff] [blame] | 591 | assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue()); |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 592 | // |
| 593 | bgpRouteEntry = |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 594 | new BgpRouteEntry(bgpSession1, |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 595 | Ip4Prefix.valueOf("40.0.0.0/24"), |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 596 | NEXT_HOP1_ROUTER, |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 597 | (byte) BgpConstants.Update.Origin.IGP, |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 598 | asPathLong, |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 599 | DEFAULT_LOCAL_PREF); |
| 600 | bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC); |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 601 | assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry)); |
Pavlin Radoslavov | f8a0f6c | 2015-02-04 15:31:47 -0800 | [diff] [blame] | 602 | assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue()); |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 603 | // |
| 604 | bgpRouteEntry = |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 605 | new BgpRouteEntry(bgpSession1, |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 606 | Ip4Prefix.valueOf("50.0.0.0/32"), |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 607 | NEXT_HOP1_ROUTER, |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 608 | (byte) BgpConstants.Update.Origin.IGP, |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 609 | asPathLong, |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 610 | DEFAULT_LOCAL_PREF); |
| 611 | bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC); |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 612 | assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry)); |
Pavlin Radoslavov | f8a0f6c | 2015-02-04 15:31:47 -0800 | [diff] [blame] | 613 | assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue()); |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 614 | |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 615 | // |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 616 | // Delete some routes |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 617 | // |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 618 | addedRoutes = new LinkedList<>(); |
| 619 | withdrawnRoutes = new LinkedList<>(); |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 620 | withdrawnRoutes.add(Ip4Prefix.valueOf("0.0.0.0/0")); |
| 621 | withdrawnRoutes.add(Ip4Prefix.valueOf("50.0.0.0/32")); |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 622 | // Write the routes |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 623 | message = peer1.peerChannelHandler.prepareBgpUpdate( |
| 624 | NEXT_HOP1_ROUTER, |
| 625 | DEFAULT_LOCAL_PREF, |
| 626 | DEFAULT_MULTI_EXIT_DISC, |
| 627 | asPathLong, |
| 628 | addedRoutes, |
| 629 | withdrawnRoutes); |
| 630 | peer1.peerChannelHandler.savedCtx.getChannel().write(message); |
| 631 | // |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 632 | // Check that the routes have been received, processed and stored |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 633 | // |
| 634 | bgpRibIn1 = waitForBgpRibIn(bgpSession1, 3); |
| 635 | assertThat(bgpRibIn1, hasSize(3)); |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 636 | bgpRoutes = waitForBgpRoutes(3); |
| 637 | assertThat(bgpRoutes, hasSize(3)); |
| 638 | // |
| 639 | bgpRouteEntry = |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 640 | new BgpRouteEntry(bgpSession1, |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 641 | Ip4Prefix.valueOf("20.0.0.0/8"), |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 642 | NEXT_HOP1_ROUTER, |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 643 | (byte) BgpConstants.Update.Origin.IGP, |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 644 | asPathLong, |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 645 | DEFAULT_LOCAL_PREF); |
| 646 | bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC); |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 647 | assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry)); |
Pavlin Radoslavov | f8a0f6c | 2015-02-04 15:31:47 -0800 | [diff] [blame] | 648 | assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue()); |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 649 | // |
| 650 | bgpRouteEntry = |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 651 | new BgpRouteEntry(bgpSession1, |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 652 | Ip4Prefix.valueOf("30.0.0.0/16"), |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 653 | NEXT_HOP1_ROUTER, |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 654 | (byte) BgpConstants.Update.Origin.IGP, |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 655 | asPathLong, |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 656 | DEFAULT_LOCAL_PREF); |
| 657 | bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC); |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 658 | assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry)); |
Pavlin Radoslavov | f8a0f6c | 2015-02-04 15:31:47 -0800 | [diff] [blame] | 659 | assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue()); |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 660 | // |
| 661 | bgpRouteEntry = |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 662 | new BgpRouteEntry(bgpSession1, |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 663 | Ip4Prefix.valueOf("40.0.0.0/24"), |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 664 | NEXT_HOP1_ROUTER, |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 665 | (byte) BgpConstants.Update.Origin.IGP, |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 666 | asPathLong, |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 667 | DEFAULT_LOCAL_PREF); |
| 668 | bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC); |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 669 | assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry)); |
Pavlin Radoslavov | f8a0f6c | 2015-02-04 15:31:47 -0800 | [diff] [blame] | 670 | assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue()); |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 671 | |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 672 | |
| 673 | // Close the channels and test there are no routes |
| 674 | peer1.peerChannelHandler.closeChannel(); |
| 675 | peer2.peerChannelHandler.closeChannel(); |
| 676 | peer3.peerChannelHandler.closeChannel(); |
| 677 | bgpRoutes = waitForBgpRoutes(0); |
| 678 | assertThat(bgpRoutes, hasSize(0)); |
| 679 | } |
| 680 | |
| 681 | /** |
| 682 | * Tests the BGP route preference. |
| 683 | */ |
| 684 | @Test |
| 685 | public void testBgpRoutePreference() throws InterruptedException { |
| 686 | ChannelBuffer message; |
| 687 | BgpRouteEntry bgpRouteEntry; |
| 688 | Collection<BgpRouteEntry> bgpRibIn1; |
| 689 | Collection<BgpRouteEntry> bgpRibIn2; |
| 690 | Collection<BgpRouteEntry> bgpRibIn3; |
| 691 | Collection<BgpRouteEntry> bgpRoutes; |
| 692 | Collection<Ip4Prefix> addedRoutes = new LinkedList<>(); |
| 693 | Collection<Ip4Prefix> withdrawnRoutes = new LinkedList<>(); |
| 694 | |
| 695 | // Initiate the connections |
| 696 | peer1.connect(connectToSocket); |
| 697 | peer2.connect(connectToSocket); |
| 698 | peer3.connect(connectToSocket); |
| 699 | |
| 700 | // |
| 701 | // Setup the initial set of routes to Peer1 |
| 702 | // |
| 703 | addedRoutes.add(Ip4Prefix.valueOf("20.0.0.0/8")); |
| 704 | addedRoutes.add(Ip4Prefix.valueOf("30.0.0.0/16")); |
| 705 | // Write the routes |
| 706 | message = peer1.peerChannelHandler.prepareBgpUpdate( |
| 707 | NEXT_HOP1_ROUTER, |
| 708 | DEFAULT_LOCAL_PREF, |
| 709 | DEFAULT_MULTI_EXIT_DISC, |
| 710 | asPathLong, |
| 711 | addedRoutes, |
| 712 | withdrawnRoutes); |
| 713 | peer1.peerChannelHandler.savedCtx.getChannel().write(message); |
| 714 | bgpRoutes = waitForBgpRoutes(2); |
| 715 | assertThat(bgpRoutes, hasSize(2)); |
| 716 | |
| 717 | // |
| 718 | // Add a route entry to Peer2 with a better LOCAL_PREF |
| 719 | // |
| 720 | addedRoutes = new LinkedList<>(); |
| 721 | withdrawnRoutes = new LinkedList<>(); |
| 722 | addedRoutes.add(Ip4Prefix.valueOf("20.0.0.0/8")); |
| 723 | // Write the routes |
| 724 | message = peer2.peerChannelHandler.prepareBgpUpdate( |
| 725 | NEXT_HOP2_ROUTER, |
| 726 | BETTER_LOCAL_PREF, |
| 727 | DEFAULT_MULTI_EXIT_DISC, |
| 728 | asPathLong, |
| 729 | addedRoutes, |
| 730 | withdrawnRoutes); |
| 731 | peer2.peerChannelHandler.savedCtx.getChannel().write(message); |
| 732 | // |
| 733 | // Check that the routes have been received, processed and stored |
| 734 | // |
| 735 | bgpRibIn2 = waitForBgpRibIn(bgpSession2, 1); |
| 736 | assertThat(bgpRibIn2, hasSize(1)); |
| 737 | bgpRoutes = waitForBgpRoutes(2); |
| 738 | assertThat(bgpRoutes, hasSize(2)); |
| 739 | // |
| 740 | bgpRouteEntry = |
| 741 | new BgpRouteEntry(bgpSession2, |
| 742 | Ip4Prefix.valueOf("20.0.0.0/8"), |
| 743 | NEXT_HOP2_ROUTER, |
| 744 | (byte) BgpConstants.Update.Origin.IGP, |
| 745 | asPathLong, |
| 746 | BETTER_LOCAL_PREF); |
| 747 | bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC); |
| 748 | assertThat(bgpRibIn2, hasBgpRouteEntry(bgpRouteEntry)); |
Pavlin Radoslavov | f8a0f6c | 2015-02-04 15:31:47 -0800 | [diff] [blame] | 749 | assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue()); |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 750 | |
| 751 | // |
| 752 | // Add a route entry to Peer3 with a shorter AS path |
| 753 | // |
| 754 | addedRoutes = new LinkedList<>(); |
| 755 | withdrawnRoutes = new LinkedList<>(); |
| 756 | addedRoutes.add(Ip4Prefix.valueOf("20.0.0.0/8")); |
| 757 | // Write the routes |
| 758 | message = peer3.peerChannelHandler.prepareBgpUpdate( |
| 759 | NEXT_HOP3_ROUTER, |
| 760 | BETTER_LOCAL_PREF, |
| 761 | DEFAULT_MULTI_EXIT_DISC, |
| 762 | asPathShort, |
| 763 | addedRoutes, |
| 764 | withdrawnRoutes); |
| 765 | peer3.peerChannelHandler.savedCtx.getChannel().write(message); |
| 766 | // |
| 767 | // Check that the routes have been received, processed and stored |
| 768 | // |
| 769 | bgpRibIn3 = waitForBgpRibIn(bgpSession3, 1); |
| 770 | assertThat(bgpRibIn3, hasSize(1)); |
| 771 | bgpRoutes = waitForBgpRoutes(2); |
| 772 | assertThat(bgpRoutes, hasSize(2)); |
| 773 | // |
| 774 | bgpRouteEntry = |
| 775 | new BgpRouteEntry(bgpSession3, |
| 776 | Ip4Prefix.valueOf("20.0.0.0/8"), |
| 777 | NEXT_HOP3_ROUTER, |
| 778 | (byte) BgpConstants.Update.Origin.IGP, |
| 779 | asPathShort, |
| 780 | BETTER_LOCAL_PREF); |
| 781 | bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC); |
| 782 | assertThat(bgpRibIn3, hasBgpRouteEntry(bgpRouteEntry)); |
Pavlin Radoslavov | f8a0f6c | 2015-02-04 15:31:47 -0800 | [diff] [blame] | 783 | assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue()); |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 784 | |
| 785 | // |
| 786 | // Cleanup in preparation for next test: delete old route entry from |
| 787 | // Peer2 |
| 788 | // |
| 789 | addedRoutes = new LinkedList<>(); |
| 790 | withdrawnRoutes = new LinkedList<>(); |
| 791 | withdrawnRoutes.add(Ip4Prefix.valueOf("20.0.0.0/8")); |
| 792 | // Write the routes |
| 793 | message = peer2.peerChannelHandler.prepareBgpUpdate( |
| 794 | NEXT_HOP2_ROUTER, |
| 795 | BETTER_LOCAL_PREF, |
| 796 | BETTER_MULTI_EXIT_DISC, |
| 797 | asPathShort, |
| 798 | addedRoutes, |
| 799 | withdrawnRoutes); |
| 800 | peer2.peerChannelHandler.savedCtx.getChannel().write(message); |
| 801 | // |
| 802 | // Check that the routes have been received, processed and stored |
| 803 | // |
| 804 | bgpRibIn2 = waitForBgpRibIn(bgpSession2, 0); |
| 805 | assertThat(bgpRibIn2, hasSize(0)); |
| 806 | |
| 807 | // |
| 808 | // Add a route entry to Peer2 with a better MED |
| 809 | // |
| 810 | addedRoutes = new LinkedList<>(); |
| 811 | withdrawnRoutes = new LinkedList<>(); |
| 812 | addedRoutes.add(Ip4Prefix.valueOf("20.0.0.0/8")); |
| 813 | // Write the routes |
| 814 | message = peer2.peerChannelHandler.prepareBgpUpdate( |
| 815 | NEXT_HOP2_ROUTER, |
| 816 | BETTER_LOCAL_PREF, |
| 817 | BETTER_MULTI_EXIT_DISC, |
| 818 | asPathShort, |
| 819 | addedRoutes, |
| 820 | withdrawnRoutes); |
| 821 | peer2.peerChannelHandler.savedCtx.getChannel().write(message); |
| 822 | // |
| 823 | // Check that the routes have been received, processed and stored |
| 824 | // |
| 825 | bgpRibIn2 = waitForBgpRibIn(bgpSession2, 1); |
| 826 | assertThat(bgpRibIn2, hasSize(1)); |
| 827 | bgpRoutes = waitForBgpRoutes(2); |
| 828 | assertThat(bgpRoutes, hasSize(2)); |
| 829 | // |
| 830 | bgpRouteEntry = |
| 831 | new BgpRouteEntry(bgpSession2, |
| 832 | Ip4Prefix.valueOf("20.0.0.0/8"), |
| 833 | NEXT_HOP2_ROUTER, |
| 834 | (byte) BgpConstants.Update.Origin.IGP, |
| 835 | asPathShort, |
| 836 | BETTER_LOCAL_PREF); |
| 837 | bgpRouteEntry.setMultiExitDisc(BETTER_MULTI_EXIT_DISC); |
| 838 | assertThat(bgpRibIn2, hasBgpRouteEntry(bgpRouteEntry)); |
Pavlin Radoslavov | f8a0f6c | 2015-02-04 15:31:47 -0800 | [diff] [blame] | 839 | assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue()); |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 840 | |
| 841 | // |
| 842 | // Add a route entry to Peer1 with a better (lower) BGP ID |
| 843 | // |
| 844 | addedRoutes = new LinkedList<>(); |
| 845 | withdrawnRoutes = new LinkedList<>(); |
| 846 | addedRoutes.add(Ip4Prefix.valueOf("20.0.0.0/8")); |
| 847 | withdrawnRoutes.add(Ip4Prefix.valueOf("30.0.0.0/16")); |
| 848 | // Write the routes |
| 849 | message = peer1.peerChannelHandler.prepareBgpUpdate( |
| 850 | NEXT_HOP1_ROUTER, |
| 851 | BETTER_LOCAL_PREF, |
| 852 | BETTER_MULTI_EXIT_DISC, |
| 853 | asPathShort, |
| 854 | addedRoutes, |
| 855 | withdrawnRoutes); |
| 856 | peer1.peerChannelHandler.savedCtx.getChannel().write(message); |
| 857 | // |
| 858 | // Check that the routes have been received, processed and stored |
| 859 | // |
| 860 | bgpRibIn1 = waitForBgpRibIn(bgpSession1, 1); |
| 861 | assertThat(bgpRibIn1, hasSize(1)); |
| 862 | bgpRoutes = waitForBgpRoutes(1); |
| 863 | assertThat(bgpRoutes, hasSize(1)); |
| 864 | // |
| 865 | bgpRouteEntry = |
| 866 | new BgpRouteEntry(bgpSession1, |
| 867 | Ip4Prefix.valueOf("20.0.0.0/8"), |
| 868 | NEXT_HOP1_ROUTER, |
| 869 | (byte) BgpConstants.Update.Origin.IGP, |
| 870 | asPathShort, |
| 871 | BETTER_LOCAL_PREF); |
| 872 | bgpRouteEntry.setMultiExitDisc(BETTER_MULTI_EXIT_DISC); |
| 873 | assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry)); |
Pavlin Radoslavov | f8a0f6c | 2015-02-04 15:31:47 -0800 | [diff] [blame] | 874 | assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue()); |
Pavlin Radoslavov | 0af11c1 | 2014-12-10 18:16:25 -0800 | [diff] [blame] | 875 | |
| 876 | |
| 877 | // Close the channels and test there are no routes |
| 878 | peer1.peerChannelHandler.closeChannel(); |
| 879 | peer2.peerChannelHandler.closeChannel(); |
| 880 | peer3.peerChannelHandler.closeChannel(); |
Jonathan Hart | 20d8e51 | 2014-10-16 11:05:52 -0700 | [diff] [blame] | 881 | bgpRoutes = waitForBgpRoutes(0); |
| 882 | assertThat(bgpRoutes, hasSize(0)); |
| 883 | } |
| 884 | } |