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