blob: 3c025724af2a964b003fa43252777f496b466456 [file] [log] [blame]
Jonathan Hart6c2e7962016-04-11 13:54:09 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jonathan Hart6c2e7962016-04-11 13:54:09 -07003 *
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 Milkey69ec8712017-08-08 13:00:43 -070017package org.onosproject.routeservice.impl;
Jonathan Hart6c2e7962016-04-11 13:54:09 -070018
Charles Chan2fde6d42017-08-23 14:46:43 -070019import java.util.Collection;
Ray Milkey69ec8712017-08-08 13:00:43 -070020import java.util.Collections;
21
Jordan Halterman6328db72018-04-10 13:34:50 -040022import com.google.common.util.concurrent.MoreExecutors;
Jonathan Hart6c2e7962016-04-11 13:54:09 -070023import org.junit.Before;
24import org.junit.Test;
25import org.onlab.packet.Ip4Address;
26import org.onlab.packet.Ip4Prefix;
27import org.onlab.packet.Ip6Address;
28import org.onlab.packet.Ip6Prefix;
29import org.onlab.packet.IpAddress;
30import org.onlab.packet.IpPrefix;
31import org.onlab.packet.MacAddress;
32import org.onlab.packet.VlanId;
Ray Milkey69ec8712017-08-08 13:00:43 -070033import org.onosproject.routeservice.ResolvedRoute;
34import org.onosproject.routeservice.Route;
35import org.onosproject.routeservice.RouteEvent;
36import org.onosproject.routeservice.RouteListener;
37import org.onosproject.routeservice.store.LocalRouteStore;
Jonathan Hartd4be52f2017-05-25 14:21:44 -070038import org.onosproject.cluster.ClusterService;
Jonathan Hart6c2e7962016-04-11 13:54:09 -070039import org.onosproject.net.ConnectPoint;
40import org.onosproject.net.DefaultHost;
41import org.onosproject.net.DeviceId;
42import org.onosproject.net.Host;
43import org.onosproject.net.HostId;
44import org.onosproject.net.HostLocation;
45import org.onosproject.net.PortNumber;
46import org.onosproject.net.host.HostEvent;
47import org.onosproject.net.host.HostListener;
48import org.onosproject.net.host.HostService;
49import org.onosproject.net.host.HostServiceAdapter;
50import org.onosproject.net.provider.ProviderId;
Charles Chan0cc44502018-01-29 15:25:52 -080051import org.onosproject.store.service.AsyncDistributedLock;
52import org.onosproject.store.service.DistributedLock;
53import org.onosproject.store.service.DistributedLockBuilder;
Jonathan Hartd4be52f2017-05-25 14:21:44 -070054import org.onosproject.store.service.StorageService;
55import org.onosproject.store.service.WorkQueue;
Jonathan Hart6c2e7962016-04-11 13:54:09 -070056
Ray Milkey69ec8712017-08-08 13:00:43 -070057import com.google.common.collect.Sets;
Jonathan Hart6c2e7962016-04-11 13:54:09 -070058
59import static org.easymock.EasyMock.anyObject;
Jonathan Hartd4be52f2017-05-25 14:21:44 -070060import static org.easymock.EasyMock.anyString;
Jonathan Hart6c2e7962016-04-11 13:54:09 -070061import static org.easymock.EasyMock.createMock;
Jonathan Hartd4be52f2017-05-25 14:21:44 -070062import static org.easymock.EasyMock.createNiceMock;
Jonathan Hart6c2e7962016-04-11 13:54:09 -070063import static org.easymock.EasyMock.expect;
64import static org.easymock.EasyMock.expectLastCall;
65import static org.easymock.EasyMock.replay;
66import static org.easymock.EasyMock.reset;
67import static org.easymock.EasyMock.verify;
68
69/**
70 * Unit tests for the route manager.
71 */
72public 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 Hart7f2d5742016-04-13 16:22:50 -070079 private static final IpPrefix V4_PREFIX2 = Ip4Prefix.valueOf("2.2.2.0/24");
Jonathan Hart6c2e7962016-04-11 13:54:09 -070080 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 Halterman6328db72018-04-10 13:34:50 -0400107 routeManager.hostEventExecutor = MoreExecutors.directExecutor();
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700108
Jonathan Hartd4be52f2017-05-25 14:21:44 -0700109 routeManager.clusterService = createNiceMock(ClusterService.class);
110 replay(routeManager.clusterService);
111 routeManager.storageService = createNiceMock(StorageService.class);
Charles Chan0cc44502018-01-29 15:25:52 -0800112
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 Hartd4be52f2017-05-25 14:21:44 -0700124 expect(routeManager.storageService.getWorkQueue(anyString(), anyObject()))
125 .andReturn(createNiceMock(WorkQueue.class));
126 replay(routeManager.storageService);
127
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700128 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 Hart7f2d5742016-04-13 16:22:50 -0700210 public void testRouteAdd() {
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700211 Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1);
Ray Milkey69aad442018-09-14 16:24:26 -0700212 ResolvedRoute resolvedRoute = new ResolvedRoute(route, MAC1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700213
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700214 verifyRouteAdd(route, resolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700215
216 route = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP1);
Ray Milkey69aad442018-09-14 16:24:26 -0700217 resolvedRoute = new ResolvedRoute(route, MAC3);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700218
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700219 verifyRouteAdd(route, resolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700220 }
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 Hart7f2d5742016-04-13 16:22:50 -0700230 private void verifyRouteAdd(Route route, ResolvedRoute resolvedRoute) {
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700231 reset(routeListener);
232
Charles Chan2fde6d42017-08-23 14:46:43 -0700233 routeListener.event(event(RouteEvent.Type.ROUTE_ADDED, resolvedRoute, null,
234 Sets.newHashSet(resolvedRoute), null));
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700235
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);
Ray Milkey69aad442018-09-14 16:24:26 -0700250 ResolvedRoute resolvedRoute = new ResolvedRoute(route, MAC1);
251 ResolvedRoute updatedResolvedRoute = new ResolvedRoute(updatedRoute, MAC2);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700252
Jonathan Hart96c146b2017-02-24 16:32:00 -0800253 verifyRouteUpdated(route, updatedRoute, resolvedRoute, updatedResolvedRoute);
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700254
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);
Ray Milkey69aad442018-09-14 16:24:26 -0700259 resolvedRoute = new ResolvedRoute(route, MAC1);
260 updatedResolvedRoute = new ResolvedRoute(updatedRoute, MAC2);
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700261
Charles Chane4d13102016-11-08 15:38:44 -0800262 verifyRouteUpdated(route, updatedRoute, resolvedRoute, updatedResolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700263
264 route = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP1);
265 updatedRoute = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP2);
Ray Milkey69aad442018-09-14 16:24:26 -0700266 resolvedRoute = new ResolvedRoute(route, MAC3);
267 updatedResolvedRoute = new ResolvedRoute(updatedRoute, MAC4);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700268
Jonathan Hart96c146b2017-02-24 16:32:00 -0800269 verifyRouteUpdated(route, updatedRoute, resolvedRoute, updatedResolvedRoute);
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700270 }
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 Chane4d13102016-11-08 15:38:44 -0800278 * @param resolvedRoute resolved route before update
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700279 * @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 Chane4d13102016-11-08 15:38:44 -0800283 ResolvedRoute resolvedRoute,
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700284 ResolvedRoute updatedResolvedRoute) {
285 // First add the original route
286 addRoute(original);
287
Jonathan Hartf7021682017-03-22 18:17:21 -0700288 routeListener.event(event(RouteEvent.Type.ROUTE_UPDATED,
Charles Chan2fde6d42017-08-23 14:46:43 -0700289 updatedResolvedRoute, resolvedRoute,
290 Sets.newHashSet(updatedResolvedRoute), Sets.newHashSet(resolvedRoute)));
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700291 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 Hart7f2d5742016-04-13 16:22:50 -0700304 public void testRouteDelete() {
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700305 Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1);
Ray Milkey69aad442018-09-14 16:24:26 -0700306 ResolvedRoute removedResolvedRoute = new ResolvedRoute(route, MAC1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700307
Charles Chane4d13102016-11-08 15:38:44 -0800308 verifyDelete(route, removedResolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700309
310 route = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP1);
Ray Milkey69aad442018-09-14 16:24:26 -0700311 removedResolvedRoute = new ResolvedRoute(route, MAC3);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700312
Charles Chane4d13102016-11-08 15:38:44 -0800313 verifyDelete(route, removedResolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700314 }
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 Chane4d13102016-11-08 15:38:44 -0800321 * @param removedResolvedRoute the resolved route being removed
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700322 */
Charles Chane4d13102016-11-08 15:38:44 -0800323 private void verifyDelete(Route route, ResolvedRoute removedResolvedRoute) {
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700324 addRoute(route);
325
326 RouteEvent withdrawRouteEvent = new RouteEvent(RouteEvent.Type.ROUTE_REMOVED,
Charles Chan2fde6d42017-08-23 14:46:43 -0700327 removedResolvedRoute, Sets.newHashSet(removedResolvedRoute));
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700328
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 Hart7f2d5742016-04-13 16:22:50 -0700340 * 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 Hart6c2e7962016-04-11 13:54:09 -0700342 */
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 Hart96c146b2017-02-24 16:32:00 -0800352 expectLastCall().anyTimes();
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700353 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);
Ray Milkey69aad442018-09-14 16:24:26 -0700365 ResolvedRoute resolvedRoute = new ResolvedRoute(route, MAC1);
Charles Chan2fde6d42017-08-23 14:46:43 -0700366 routeListener.event(event(RouteEvent.Type.ROUTE_ADDED, resolvedRoute, null,
367 Sets.newHashSet(resolvedRoute), null));
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700368 replay(routeListener);
369
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700370 Host host = createHost(MAC1, V4_NEXT_HOP1);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800371
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 Hart6c2e7962016-04-11 13:54:09 -0700381 hostListener.event(new HostEvent(HostEvent.Type.HOST_ADDED, host));
382
383 verify(routeListener);
384 }
385
Charles Chan2fde6d42017-08-23 14:46:43 -0700386 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 Hartf7021682017-03-22 18:17:21 -0700390 }
391
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700392 /**
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}