Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 | */ |
| 16 | |
| 17 | package org.onosproject.incubator.net.routing.impl; |
| 18 | |
| 19 | import com.google.common.collect.Sets; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.packet.Ip4Address; |
| 23 | import org.onlab.packet.Ip4Prefix; |
| 24 | import org.onlab.packet.Ip6Address; |
| 25 | import org.onlab.packet.Ip6Prefix; |
| 26 | import org.onlab.packet.IpAddress; |
| 27 | import org.onlab.packet.IpPrefix; |
| 28 | import org.onlab.packet.MacAddress; |
| 29 | import org.onlab.packet.VlanId; |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame^] | 30 | import org.onosproject.cluster.ClusterService; |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 31 | import org.onosproject.incubator.net.routing.ResolvedRoute; |
| 32 | import org.onosproject.incubator.net.routing.Route; |
| 33 | import org.onosproject.incubator.net.routing.RouteEvent; |
| 34 | import org.onosproject.incubator.net.routing.RouteListener; |
| 35 | import org.onosproject.incubator.store.routing.impl.LocalRouteStore; |
| 36 | import org.onosproject.net.ConnectPoint; |
| 37 | import org.onosproject.net.DefaultHost; |
| 38 | import org.onosproject.net.DeviceId; |
| 39 | import org.onosproject.net.Host; |
| 40 | import org.onosproject.net.HostId; |
| 41 | import org.onosproject.net.HostLocation; |
| 42 | import org.onosproject.net.PortNumber; |
| 43 | import org.onosproject.net.host.HostEvent; |
| 44 | import org.onosproject.net.host.HostListener; |
| 45 | import org.onosproject.net.host.HostService; |
| 46 | import org.onosproject.net.host.HostServiceAdapter; |
| 47 | import org.onosproject.net.provider.ProviderId; |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame^] | 48 | import org.onosproject.store.service.StorageService; |
| 49 | import org.onosproject.store.service.WorkQueue; |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 50 | |
| 51 | import java.util.Collections; |
| 52 | |
| 53 | import static org.easymock.EasyMock.anyObject; |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame^] | 54 | import static org.easymock.EasyMock.anyString; |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 55 | import static org.easymock.EasyMock.createMock; |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame^] | 56 | import static org.easymock.EasyMock.createNiceMock; |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 57 | import static org.easymock.EasyMock.expect; |
| 58 | import static org.easymock.EasyMock.expectLastCall; |
| 59 | import static org.easymock.EasyMock.replay; |
| 60 | import static org.easymock.EasyMock.reset; |
| 61 | import static org.easymock.EasyMock.verify; |
| 62 | |
| 63 | /** |
| 64 | * Unit tests for the route manager. |
| 65 | */ |
| 66 | public class RouteManagerTest { |
| 67 | |
| 68 | private static final ConnectPoint CP1 = new ConnectPoint( |
| 69 | DeviceId.deviceId("of:0000000000000001"), |
| 70 | PortNumber.portNumber(1)); |
| 71 | |
| 72 | private static final IpPrefix V4_PREFIX1 = Ip4Prefix.valueOf("1.1.1.0/24"); |
Jonathan Hart | 7f2d574 | 2016-04-13 16:22:50 -0700 | [diff] [blame] | 73 | private static final IpPrefix V4_PREFIX2 = Ip4Prefix.valueOf("2.2.2.0/24"); |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 74 | private static final IpPrefix V6_PREFIX1 = Ip6Prefix.valueOf("4000::/64"); |
| 75 | |
| 76 | private static final IpAddress V4_NEXT_HOP1 = Ip4Address.valueOf("192.168.10.1"); |
| 77 | private static final IpAddress V4_NEXT_HOP2 = Ip4Address.valueOf("192.168.20.1"); |
| 78 | private static final IpAddress V6_NEXT_HOP1 = Ip6Address.valueOf("1000::1"); |
| 79 | private static final IpAddress V6_NEXT_HOP2 = Ip6Address.valueOf("2000::1"); |
| 80 | |
| 81 | private static final MacAddress MAC1 = MacAddress.valueOf("00:00:00:00:00:01"); |
| 82 | private static final MacAddress MAC2 = MacAddress.valueOf("00:00:00:00:00:02"); |
| 83 | private static final MacAddress MAC3 = MacAddress.valueOf("00:00:00:00:00:03"); |
| 84 | private static final MacAddress MAC4 = MacAddress.valueOf("00:00:00:00:00:04"); |
| 85 | |
| 86 | private HostService hostService; |
| 87 | |
| 88 | private RouteListener routeListener; |
| 89 | private HostListener hostListener; |
| 90 | |
| 91 | private RouteManager routeManager; |
| 92 | |
| 93 | @Before |
| 94 | public void setUp() throws Exception { |
| 95 | setUpHostService(); |
| 96 | |
| 97 | routeListener = createMock(RouteListener.class); |
| 98 | |
| 99 | routeManager = new TestRouteManager(); |
| 100 | routeManager.hostService = hostService; |
| 101 | |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame^] | 102 | routeManager.clusterService = createNiceMock(ClusterService.class); |
| 103 | replay(routeManager.clusterService); |
| 104 | routeManager.storageService = createNiceMock(StorageService.class); |
| 105 | expect(routeManager.storageService.getWorkQueue(anyString(), anyObject())) |
| 106 | .andReturn(createNiceMock(WorkQueue.class)); |
| 107 | replay(routeManager.storageService); |
| 108 | |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 109 | LocalRouteStore routeStore = new LocalRouteStore(); |
| 110 | routeStore.activate(); |
| 111 | routeManager.routeStore = routeStore; |
| 112 | routeManager.activate(); |
| 113 | |
| 114 | routeManager.addListener(routeListener); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Sets up the host service with details of some hosts. |
| 119 | */ |
| 120 | private void setUpHostService() { |
| 121 | hostService = createMock(HostService.class); |
| 122 | |
| 123 | hostService.addListener(anyObject(HostListener.class)); |
| 124 | expectLastCall().andDelegateTo(new TestHostService()).anyTimes(); |
| 125 | |
| 126 | Host host1 = createHost(MAC1, V4_NEXT_HOP1); |
| 127 | expectHost(host1); |
| 128 | |
| 129 | Host host2 = createHost(MAC2, V4_NEXT_HOP2); |
| 130 | expectHost(host2); |
| 131 | |
| 132 | Host host3 = createHost(MAC3, V6_NEXT_HOP1); |
| 133 | expectHost(host3); |
| 134 | |
| 135 | Host host4 = createHost(MAC4, V6_NEXT_HOP2); |
| 136 | expectHost(host4); |
| 137 | |
| 138 | replay(hostService); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Sets expectations on the host service for a given host. |
| 143 | * |
| 144 | * @param host host |
| 145 | */ |
| 146 | private void expectHost(Host host) { |
| 147 | // Assume the host only has one IP address |
| 148 | IpAddress ip = host.ipAddresses().iterator().next(); |
| 149 | |
| 150 | expect(hostService.getHostsByIp(ip)) |
| 151 | .andReturn(Sets.newHashSet(host)).anyTimes(); |
| 152 | hostService.startMonitoringIp(ip); |
| 153 | expectLastCall().anyTimes(); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Creates a host with the given parameters. |
| 158 | * |
| 159 | * @param macAddress MAC address |
| 160 | * @param ipAddress IP address |
| 161 | * @return new host |
| 162 | */ |
| 163 | private Host createHost(MacAddress macAddress, IpAddress ipAddress) { |
| 164 | return new DefaultHost(ProviderId.NONE, HostId.NONE, macAddress, |
| 165 | VlanId.NONE, new HostLocation(CP1, 1), |
| 166 | Sets.newHashSet(ipAddress)); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Adds a route to the route service without expecting any specific events |
| 171 | * to be sent to the route listeners. |
| 172 | * |
| 173 | * @param route route to add |
| 174 | */ |
| 175 | private void addRoute(Route route) { |
| 176 | reset(routeListener); |
| 177 | |
| 178 | routeListener.event(anyObject(RouteEvent.class)); |
| 179 | expectLastCall().once(); |
| 180 | replay(routeListener); |
| 181 | |
| 182 | routeManager.update(Collections.singleton(route)); |
| 183 | |
| 184 | reset(routeListener); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Tests adding routes to the route manager. |
| 189 | */ |
| 190 | @Test |
Jonathan Hart | 7f2d574 | 2016-04-13 16:22:50 -0700 | [diff] [blame] | 191 | public void testRouteAdd() { |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 192 | Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1); |
Charles Chan | 8fe9f4c | 2016-10-24 16:46:25 -0700 | [diff] [blame] | 193 | ResolvedRoute resolvedRoute = new ResolvedRoute(route, MAC1, CP1); |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 194 | |
Jonathan Hart | 7f2d574 | 2016-04-13 16:22:50 -0700 | [diff] [blame] | 195 | verifyRouteAdd(route, resolvedRoute); |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 196 | |
| 197 | route = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP1); |
Charles Chan | 8fe9f4c | 2016-10-24 16:46:25 -0700 | [diff] [blame] | 198 | resolvedRoute = new ResolvedRoute(route, MAC3, CP1); |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 199 | |
Jonathan Hart | 7f2d574 | 2016-04-13 16:22:50 -0700 | [diff] [blame] | 200 | verifyRouteAdd(route, resolvedRoute); |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Tests adding a new route and verifies that the correct event was sent |
| 205 | * to the route listener. |
| 206 | * |
| 207 | * @param route route to add |
| 208 | * @param resolvedRoute resolved route that should be sent to the route |
| 209 | * listener |
| 210 | */ |
Jonathan Hart | 7f2d574 | 2016-04-13 16:22:50 -0700 | [diff] [blame] | 211 | private void verifyRouteAdd(Route route, ResolvedRoute resolvedRoute) { |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 212 | reset(routeListener); |
| 213 | |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 214 | routeListener.event(event(RouteEvent.Type.ROUTE_ADDED, resolvedRoute)); |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 215 | |
| 216 | replay(routeListener); |
| 217 | |
| 218 | routeManager.update(Collections.singleton(route)); |
| 219 | |
| 220 | verify(routeListener); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Tests updating routes in the route manager. |
| 225 | */ |
| 226 | @Test |
| 227 | public void testRouteUpdate() { |
| 228 | Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1); |
| 229 | Route updatedRoute = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP2); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 230 | ResolvedRoute resolvedRoute = new ResolvedRoute(route, MAC1, CP1); |
Charles Chan | 8fe9f4c | 2016-10-24 16:46:25 -0700 | [diff] [blame] | 231 | ResolvedRoute updatedResolvedRoute = new ResolvedRoute(updatedRoute, MAC2, CP1); |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 232 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 233 | verifyRouteUpdated(route, updatedRoute, resolvedRoute, updatedResolvedRoute); |
Jonathan Hart | 7f2d574 | 2016-04-13 16:22:50 -0700 | [diff] [blame] | 234 | |
| 235 | // Different prefix pointing to the same next hop. |
| 236 | // In this case we expect to receive a ROUTE_UPDATED event. |
| 237 | route = new Route(Route.Source.STATIC, V4_PREFIX2, V4_NEXT_HOP1); |
| 238 | updatedRoute = new Route(Route.Source.STATIC, V4_PREFIX2, V4_NEXT_HOP2); |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 239 | resolvedRoute = new ResolvedRoute(route, MAC1, CP1); |
Charles Chan | 8fe9f4c | 2016-10-24 16:46:25 -0700 | [diff] [blame] | 240 | updatedResolvedRoute = new ResolvedRoute(updatedRoute, MAC2, CP1); |
Jonathan Hart | 7f2d574 | 2016-04-13 16:22:50 -0700 | [diff] [blame] | 241 | |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 242 | verifyRouteUpdated(route, updatedRoute, resolvedRoute, updatedResolvedRoute); |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 243 | |
| 244 | route = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP1); |
| 245 | updatedRoute = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP2); |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 246 | resolvedRoute = new ResolvedRoute(route, MAC3, CP1); |
Charles Chan | 8fe9f4c | 2016-10-24 16:46:25 -0700 | [diff] [blame] | 247 | updatedResolvedRoute = new ResolvedRoute(updatedRoute, MAC4, CP1); |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 248 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 249 | verifyRouteUpdated(route, updatedRoute, resolvedRoute, updatedResolvedRoute); |
Jonathan Hart | 7f2d574 | 2016-04-13 16:22:50 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Tests updating a route and verifies that the route listener receives a |
| 254 | * route updated event. |
| 255 | * |
| 256 | * @param original original route |
| 257 | * @param updated updated route |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 258 | * @param resolvedRoute resolved route before update |
Jonathan Hart | 7f2d574 | 2016-04-13 16:22:50 -0700 | [diff] [blame] | 259 | * @param updatedResolvedRoute resolved route that is expected to be sent to |
| 260 | * the route listener |
| 261 | */ |
| 262 | private void verifyRouteUpdated(Route original, Route updated, |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 263 | ResolvedRoute resolvedRoute, |
Jonathan Hart | 7f2d574 | 2016-04-13 16:22:50 -0700 | [diff] [blame] | 264 | ResolvedRoute updatedResolvedRoute) { |
| 265 | // First add the original route |
| 266 | addRoute(original); |
| 267 | |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 268 | routeListener.event(event(RouteEvent.Type.ROUTE_UPDATED, |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 269 | updatedResolvedRoute, resolvedRoute)); |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 270 | expectLastCall().once(); |
| 271 | |
| 272 | replay(routeListener); |
| 273 | |
| 274 | routeManager.update(Collections.singleton(updated)); |
| 275 | |
| 276 | verify(routeListener); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Tests deleting routes from the route manager. |
| 281 | */ |
| 282 | @Test |
Jonathan Hart | 7f2d574 | 2016-04-13 16:22:50 -0700 | [diff] [blame] | 283 | public void testRouteDelete() { |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 284 | Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1); |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 285 | ResolvedRoute removedResolvedRoute = new ResolvedRoute(route, MAC1, CP1); |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 286 | |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 287 | verifyDelete(route, removedResolvedRoute); |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 288 | |
| 289 | route = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP1); |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 290 | removedResolvedRoute = new ResolvedRoute(route, MAC3, CP1); |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 291 | |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 292 | verifyDelete(route, removedResolvedRoute); |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Tests deleting a route and verifies that the correct event is sent to |
| 297 | * the route listener. |
| 298 | * |
| 299 | * @param route route to delete |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 300 | * @param removedResolvedRoute the resolved route being removed |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 301 | */ |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 302 | private void verifyDelete(Route route, ResolvedRoute removedResolvedRoute) { |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 303 | addRoute(route); |
| 304 | |
| 305 | RouteEvent withdrawRouteEvent = new RouteEvent(RouteEvent.Type.ROUTE_REMOVED, |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 306 | removedResolvedRoute); |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 307 | |
| 308 | reset(routeListener); |
| 309 | routeListener.event(withdrawRouteEvent); |
| 310 | |
| 311 | replay(routeListener); |
| 312 | |
| 313 | routeManager.withdraw(Collections.singleton(route)); |
| 314 | |
| 315 | verify(routeListener); |
| 316 | } |
| 317 | |
| 318 | /** |
Jonathan Hart | 7f2d574 | 2016-04-13 16:22:50 -0700 | [diff] [blame] | 319 | * Tests adding a route entry where the HostService does not immediately |
| 320 | * know the MAC address of the next hop, but this is learnt later. |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 321 | */ |
| 322 | @Test |
| 323 | public void testAsyncRouteAdd() { |
| 324 | Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1); |
| 325 | |
| 326 | // Host service will reply with no hosts when asked |
| 327 | reset(hostService); |
| 328 | expect(hostService.getHostsByIp(anyObject(IpAddress.class))).andReturn( |
| 329 | Collections.emptySet()).anyTimes(); |
| 330 | hostService.startMonitoringIp(V4_NEXT_HOP1); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 331 | expectLastCall().anyTimes(); |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 332 | replay(hostService); |
| 333 | |
| 334 | // Initially when we add the route, no route event will be sent because |
| 335 | // the host is not known |
| 336 | replay(routeListener); |
| 337 | |
| 338 | routeManager.update(Collections.singleton(route)); |
| 339 | |
| 340 | verify(routeListener); |
| 341 | |
| 342 | // Now when we send the event, we expect the FIB update to be sent |
| 343 | reset(routeListener); |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 344 | routeListener.event(event(RouteEvent.Type.ROUTE_ADDED, |
Charles Chan | 8fe9f4c | 2016-10-24 16:46:25 -0700 | [diff] [blame] | 345 | new ResolvedRoute(route, MAC1, CP1))); |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 346 | replay(routeListener); |
| 347 | |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 348 | Host host = createHost(MAC1, V4_NEXT_HOP1); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 349 | |
| 350 | // Set up the host service with a host |
| 351 | reset(hostService); |
| 352 | expect(hostService.getHostsByIp(V4_NEXT_HOP1)).andReturn( |
| 353 | Collections.singleton(host)).anyTimes(); |
| 354 | hostService.startMonitoringIp(V4_NEXT_HOP1); |
| 355 | expectLastCall().anyTimes(); |
| 356 | replay(hostService); |
| 357 | |
| 358 | // Send in the host event |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 359 | hostListener.event(new HostEvent(HostEvent.Type.HOST_ADDED, host)); |
| 360 | |
| 361 | verify(routeListener); |
| 362 | } |
| 363 | |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 364 | private static RouteEvent event(RouteEvent.Type type, ResolvedRoute subject) { |
| 365 | return event(type, subject, null); |
| 366 | } |
| 367 | |
| 368 | private static RouteEvent event(RouteEvent.Type type, ResolvedRoute subject, ResolvedRoute prevSubject) { |
| 369 | return new RouteEvent(type, subject, prevSubject, Collections.singleton(subject)); |
| 370 | } |
| 371 | |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 372 | /** |
| 373 | * Test host service that stores a reference to the host listener. |
| 374 | */ |
| 375 | private class TestHostService extends HostServiceAdapter { |
| 376 | @Override |
| 377 | public void addListener(HostListener listener) { |
| 378 | hostListener = listener; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Test route manager that extends the real route manager and injects a test |
| 384 | * listener queue instead of the real listener queue. |
| 385 | */ |
| 386 | private static class TestRouteManager extends RouteManager { |
| 387 | @Override |
| 388 | ListenerQueue createListenerQueue(RouteListener listener) { |
| 389 | return new TestListenerQueue(listener); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Test listener queue that simply passes route events straight through to |
| 395 | * the route listener on the caller's thread. |
| 396 | */ |
| 397 | private static class TestListenerQueue implements ListenerQueue { |
| 398 | |
| 399 | private final RouteListener listener; |
| 400 | |
| 401 | public TestListenerQueue(RouteListener listener) { |
| 402 | this.listener = listener; |
| 403 | } |
| 404 | |
| 405 | @Override |
| 406 | public void post(RouteEvent event) { |
| 407 | listener.event(event); |
| 408 | } |
| 409 | |
| 410 | @Override |
| 411 | public void start() { |
| 412 | } |
| 413 | |
| 414 | @Override |
| 415 | public void stop() { |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | } |