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