Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
Jonathan Hart | 2da1e60 | 2015-02-18 19:09:24 -0800 | [diff] [blame] | 16 | package org.onosproject.routing.impl; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 17 | |
| 18 | import com.google.common.collect.Sets; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 19 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.Ip4Address; |
| 24 | import org.onlab.packet.Ip4Prefix; |
| 25 | import org.onlab.packet.IpAddress; |
| 26 | import org.onlab.packet.IpPrefix; |
| 27 | import org.onlab.packet.MacAddress; |
| 28 | import org.onlab.packet.VlanId; |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 29 | import org.onosproject.core.CoreService; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 30 | import org.onosproject.net.ConnectPoint; |
| 31 | import org.onosproject.net.DefaultHost; |
| 32 | import org.onosproject.net.DeviceId; |
| 33 | import org.onosproject.net.Host; |
| 34 | import org.onosproject.net.HostId; |
| 35 | import org.onosproject.net.HostLocation; |
| 36 | import org.onosproject.net.PortNumber; |
| 37 | import org.onosproject.net.host.HostListener; |
| 38 | import org.onosproject.net.host.HostService; |
| 39 | import org.onosproject.net.provider.ProviderId; |
Jonathan Hart | 2da1e60 | 2015-02-18 19:09:24 -0800 | [diff] [blame] | 40 | import org.onosproject.routing.BgpService; |
| 41 | import org.onosproject.routing.FibEntry; |
| 42 | import org.onosproject.routing.FibListener; |
| 43 | import org.onosproject.routing.FibUpdate; |
| 44 | import org.onosproject.routing.RouteEntry; |
| 45 | import org.onosproject.routing.RouteListener; |
| 46 | import org.onosproject.routing.RouteUpdate; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 47 | import org.onosproject.routing.config.RoutingConfigurationService; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 48 | |
| 49 | import java.util.Collections; |
| 50 | |
| 51 | import static org.easymock.EasyMock.*; |
| 52 | import static org.junit.Assert.assertEquals; |
| 53 | import static org.junit.Assert.assertTrue; |
| 54 | |
| 55 | /** |
| 56 | * This class tests adding a route, updating a route, deleting a route, |
| 57 | * and adding a route whose next hop is the local BGP speaker. |
| 58 | * <p/> |
| 59 | * The HostService answers requests synchronously. |
| 60 | */ |
| 61 | public class RouterTest { |
| 62 | |
| 63 | private HostService hostService; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 64 | private RoutingConfigurationService routingConfigurationService; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 65 | |
| 66 | private FibListener fibListener; |
| 67 | |
| 68 | private static final ConnectPoint SW1_ETH1 = new ConnectPoint( |
| 69 | DeviceId.deviceId("of:0000000000000001"), |
| 70 | PortNumber.portNumber(1)); |
| 71 | |
| 72 | private static final ConnectPoint SW2_ETH1 = new ConnectPoint( |
| 73 | DeviceId.deviceId("of:0000000000000002"), |
| 74 | PortNumber.portNumber(1)); |
| 75 | |
| 76 | private static final ConnectPoint SW3_ETH1 = new ConnectPoint( |
| 77 | DeviceId.deviceId("of:0000000000000003"), |
| 78 | PortNumber.portNumber(1)); |
| 79 | |
| 80 | private static final ConnectPoint SW4_ETH1 = new ConnectPoint( |
| 81 | DeviceId.deviceId("of:0000000000000004"), |
| 82 | PortNumber.portNumber(1)); |
| 83 | |
| 84 | private Router router; |
| 85 | |
| 86 | @Before |
| 87 | public void setUp() throws Exception { |
| 88 | setUpHostService(); |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 89 | routingConfigurationService = |
| 90 | createMock(RoutingConfigurationService.class); |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 91 | |
| 92 | BgpService bgpService = createMock(BgpService.class); |
Jonathan Hart | d24fafb | 2015-02-09 17:55:32 -0800 | [diff] [blame] | 93 | bgpService.start(anyObject(RouteListener.class)); |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 94 | bgpService.stop(); |
| 95 | replay(bgpService); |
| 96 | |
| 97 | fibListener = createMock(FibListener.class); |
| 98 | |
| 99 | router = new Router(); |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 100 | router.coreService = createNiceMock(CoreService.class); |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 101 | router.hostService = hostService; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 102 | router.routingConfigurationService = routingConfigurationService; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 103 | router.bgpService = bgpService; |
| 104 | router.activate(); |
| 105 | |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 106 | router.addFibListener(fibListener); |
| 107 | router.start(); |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @After |
| 111 | public void tearDown() { |
| 112 | router.stop(); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Sets up the host service with details of some hosts. |
| 117 | */ |
| 118 | private void setUpHostService() { |
| 119 | hostService = createMock(HostService.class); |
| 120 | |
| 121 | hostService.addListener(anyObject(HostListener.class)); |
| 122 | expectLastCall().anyTimes(); |
| 123 | |
| 124 | IpAddress host1Address = IpAddress.valueOf("192.168.10.1"); |
| 125 | Host host1 = new DefaultHost(ProviderId.NONE, HostId.NONE, |
| 126 | MacAddress.valueOf("00:00:00:00:00:01"), VlanId.NONE, |
| 127 | new HostLocation(SW1_ETH1, 1), |
| 128 | Sets.newHashSet(host1Address)); |
| 129 | |
| 130 | expect(hostService.getHostsByIp(host1Address)) |
| 131 | .andReturn(Sets.newHashSet(host1)).anyTimes(); |
| 132 | hostService.startMonitoringIp(host1Address); |
| 133 | expectLastCall().anyTimes(); |
| 134 | |
| 135 | |
| 136 | IpAddress host2Address = IpAddress.valueOf("192.168.20.1"); |
| 137 | Host host2 = new DefaultHost(ProviderId.NONE, HostId.NONE, |
| 138 | MacAddress.valueOf("00:00:00:00:00:02"), VlanId.NONE, |
| 139 | new HostLocation(SW2_ETH1, 1), |
| 140 | Sets.newHashSet(host2Address)); |
| 141 | |
| 142 | expect(hostService.getHostsByIp(host2Address)) |
| 143 | .andReturn(Sets.newHashSet(host2)).anyTimes(); |
| 144 | hostService.startMonitoringIp(host2Address); |
| 145 | expectLastCall().anyTimes(); |
| 146 | |
| 147 | // Next hop on a VLAN |
| 148 | IpAddress host3Address = IpAddress.valueOf("192.168.40.1"); |
| 149 | Host host3 = new DefaultHost(ProviderId.NONE, HostId.NONE, |
| 150 | MacAddress.valueOf("00:00:00:00:00:03"), VlanId.vlanId((short) 1), |
| 151 | new HostLocation(SW4_ETH1, 1), |
| 152 | Sets.newHashSet(host3Address)); |
| 153 | |
| 154 | expect(hostService.getHostsByIp(host3Address)) |
| 155 | .andReturn(Sets.newHashSet(host3)).anyTimes(); |
| 156 | hostService.startMonitoringIp(host3Address); |
| 157 | expectLastCall().anyTimes(); |
| 158 | |
| 159 | // Called during shutdown |
| 160 | hostService.removeListener(anyObject(HostListener.class)); |
| 161 | |
| 162 | replay(hostService); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Tests adding a route entry. |
| 167 | */ |
| 168 | @Test |
| 169 | public void testRouteAdd() { |
| 170 | // Construct a route entry |
| 171 | IpPrefix prefix = Ip4Prefix.valueOf("1.1.1.0/24"); |
| 172 | IpAddress nextHopIp = Ip4Address.valueOf("192.168.10.1"); |
| 173 | |
| 174 | RouteEntry routeEntry = new RouteEntry(prefix, nextHopIp); |
| 175 | |
| 176 | // Expected FIB entry |
| 177 | FibEntry fibEntry = new FibEntry(prefix, nextHopIp, |
| 178 | MacAddress.valueOf("00:00:00:00:00:01")); |
| 179 | |
| 180 | fibListener.update(Collections.singletonList(new FibUpdate( |
| 181 | FibUpdate.Type.UPDATE, fibEntry)), Collections.emptyList()); |
| 182 | |
| 183 | replay(fibListener); |
| 184 | |
| 185 | router.processRouteUpdates(Collections.singletonList( |
| 186 | new RouteUpdate(RouteUpdate.Type.UPDATE, routeEntry))); |
| 187 | |
| 188 | verify(fibListener); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Tests updating a route entry. |
| 193 | */ |
| 194 | @Test |
| 195 | public void testRouteUpdate() { |
| 196 | // Firstly add a route |
| 197 | testRouteAdd(); |
| 198 | |
| 199 | // Route entry with updated next hop for the original prefix |
| 200 | RouteEntry routeEntryUpdate = new RouteEntry( |
| 201 | Ip4Prefix.valueOf("1.1.1.0/24"), |
| 202 | Ip4Address.valueOf("192.168.20.1")); |
| 203 | |
| 204 | // The old FIB entry will be withdrawn |
| 205 | FibEntry withdrawFibEntry = new FibEntry( |
| 206 | Ip4Prefix.valueOf("1.1.1.0/24"), null, null); |
| 207 | |
| 208 | // A new FIB entry will be added |
| 209 | FibEntry updateFibEntry = new FibEntry( |
| 210 | Ip4Prefix.valueOf("1.1.1.0/24"), |
| 211 | Ip4Address.valueOf("192.168.20.1"), |
| 212 | MacAddress.valueOf("00:00:00:00:00:02")); |
| 213 | |
| 214 | reset(fibListener); |
| 215 | fibListener.update(Collections.singletonList(new FibUpdate( |
| 216 | FibUpdate.Type.UPDATE, updateFibEntry)), |
| 217 | Collections.singletonList(new FibUpdate( |
| 218 | FibUpdate.Type.DELETE, withdrawFibEntry))); |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 219 | replay(fibListener); |
| 220 | |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 221 | reset(routingConfigurationService); |
| 222 | expect(routingConfigurationService.isIpPrefixLocal( |
| 223 | anyObject(IpPrefix.class))).andReturn(false); |
| 224 | replay(routingConfigurationService); |
| 225 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 226 | router.processRouteUpdates(Collections.singletonList(new RouteUpdate( |
| 227 | RouteUpdate.Type.UPDATE, routeEntryUpdate))); |
| 228 | |
| 229 | verify(fibListener); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Tests deleting a route entry. |
| 234 | */ |
| 235 | @Test |
| 236 | public void testRouteDelete() { |
| 237 | // Firstly add a route |
| 238 | testRouteAdd(); |
| 239 | |
| 240 | RouteEntry deleteRouteEntry = new RouteEntry( |
| 241 | Ip4Prefix.valueOf("1.1.1.0/24"), |
| 242 | Ip4Address.valueOf("192.168.10.1")); |
| 243 | |
| 244 | FibEntry deleteFibEntry = new FibEntry( |
| 245 | Ip4Prefix.valueOf("1.1.1.0/24"), null, null); |
| 246 | |
| 247 | reset(fibListener); |
| 248 | fibListener.update(Collections.emptyList(), Collections.singletonList( |
| 249 | new FibUpdate(FibUpdate.Type.DELETE, deleteFibEntry))); |
| 250 | |
| 251 | replay(fibListener); |
| 252 | |
| 253 | router.processRouteUpdates(Collections.singletonList( |
| 254 | new RouteUpdate(RouteUpdate.Type.DELETE, deleteRouteEntry))); |
| 255 | |
| 256 | verify(fibListener); |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Tests adding a route whose next hop is the local BGP speaker. |
| 261 | */ |
| 262 | @Test |
| 263 | public void testLocalRouteAdd() { |
| 264 | // Construct a route entry, the next hop is the local BGP speaker |
| 265 | RouteEntry routeEntry = new RouteEntry( |
| 266 | Ip4Prefix.valueOf("1.1.1.0/24"), |
| 267 | Ip4Address.valueOf("0.0.0.0")); |
| 268 | |
| 269 | // No methods on the FIB listener should be called |
| 270 | replay(fibListener); |
| 271 | |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 272 | reset(routingConfigurationService); |
| 273 | expect(routingConfigurationService.isIpPrefixLocal( |
| 274 | anyObject(IpPrefix.class))).andReturn(true); |
| 275 | replay(routingConfigurationService); |
| 276 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 277 | // Call the processRouteUpdates() method in Router class |
| 278 | RouteUpdate routeUpdate = new RouteUpdate(RouteUpdate.Type.UPDATE, |
| 279 | routeEntry); |
| 280 | router.processRouteUpdates(Collections.singletonList(routeUpdate)); |
| 281 | |
| 282 | // Verify |
| 283 | assertEquals(1, router.getRoutes4().size()); |
| 284 | assertTrue(router.getRoutes4().contains(routeEntry)); |
| 285 | verify(fibListener); |
| 286 | } |
| 287 | } |