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