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