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