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