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