blob: c6c0f82d087a78c2b2b91375886cdc24d8111036 [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 Chan23686832017-08-23 14:46:43 -070019import java.util.Collection;
Ray Milkey69ec8712017-08-08 13:00:43 -070020import java.util.Collections;
21
Jonathan Hart6c2e7962016-04-11 13:54:09 -070022import org.junit.Before;
23import org.junit.Test;
24import org.onlab.packet.Ip4Address;
25import org.onlab.packet.Ip4Prefix;
26import org.onlab.packet.Ip6Address;
27import org.onlab.packet.Ip6Prefix;
28import org.onlab.packet.IpAddress;
29import org.onlab.packet.IpPrefix;
30import org.onlab.packet.MacAddress;
31import org.onlab.packet.VlanId;
Ray Milkey69ec8712017-08-08 13:00:43 -070032import org.onosproject.routeservice.ResolvedRoute;
33import org.onosproject.routeservice.Route;
34import org.onosproject.routeservice.RouteEvent;
35import org.onosproject.routeservice.RouteListener;
36import org.onosproject.routeservice.store.LocalRouteStore;
Jonathan Hartd4be52f2017-05-25 14:21:44 -070037import org.onosproject.cluster.ClusterService;
Jonathan Hart6c2e7962016-04-11 13:54:09 -070038import org.onosproject.net.ConnectPoint;
39import org.onosproject.net.DefaultHost;
40import org.onosproject.net.DeviceId;
41import org.onosproject.net.Host;
42import org.onosproject.net.HostId;
43import org.onosproject.net.HostLocation;
44import org.onosproject.net.PortNumber;
45import org.onosproject.net.host.HostEvent;
46import org.onosproject.net.host.HostListener;
47import org.onosproject.net.host.HostService;
48import org.onosproject.net.host.HostServiceAdapter;
49import org.onosproject.net.provider.ProviderId;
Charles Chan3a35e142018-01-29 15:25:52 -080050import org.onosproject.store.service.AsyncDistributedLock;
51import org.onosproject.store.service.DistributedLock;
52import org.onosproject.store.service.DistributedLockBuilder;
Jonathan Hartd4be52f2017-05-25 14:21:44 -070053import org.onosproject.store.service.StorageService;
54import org.onosproject.store.service.WorkQueue;
Jonathan Hart6c2e7962016-04-11 13:54:09 -070055
Ray Milkey69ec8712017-08-08 13:00:43 -070056import com.google.common.collect.Sets;
Jonathan Hart6c2e7962016-04-11 13:54:09 -070057
58import static org.easymock.EasyMock.anyObject;
Jonathan Hartd4be52f2017-05-25 14:21:44 -070059import static org.easymock.EasyMock.anyString;
Jonathan Hart6c2e7962016-04-11 13:54:09 -070060import static org.easymock.EasyMock.createMock;
Jonathan Hartd4be52f2017-05-25 14:21:44 -070061import static org.easymock.EasyMock.createNiceMock;
Jonathan Hart6c2e7962016-04-11 13:54:09 -070062import static org.easymock.EasyMock.expect;
63import static org.easymock.EasyMock.expectLastCall;
64import static org.easymock.EasyMock.replay;
65import static org.easymock.EasyMock.reset;
66import static org.easymock.EasyMock.verify;
67
68/**
69 * Unit tests for the route manager.
70 */
71public 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 Hart7f2d5742016-04-13 16:22:50 -070078 private static final IpPrefix V4_PREFIX2 = Ip4Prefix.valueOf("2.2.2.0/24");
Jonathan Hart6c2e7962016-04-11 13:54:09 -070079 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 Hartd4be52f2017-05-25 14:21:44 -0700107 routeManager.clusterService = createNiceMock(ClusterService.class);
108 replay(routeManager.clusterService);
109 routeManager.storageService = createNiceMock(StorageService.class);
Charles Chan3a35e142018-01-29 15:25:52 -0800110
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 Hartd4be52f2017-05-25 14:21:44 -0700122 expect(routeManager.storageService.getWorkQueue(anyString(), anyObject()))
123 .andReturn(createNiceMock(WorkQueue.class));
124 replay(routeManager.storageService);
125
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700126 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 Hart7f2d5742016-04-13 16:22:50 -0700208 public void testRouteAdd() {
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700209 Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700210 ResolvedRoute resolvedRoute = new ResolvedRoute(route, MAC1, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700211
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700212 verifyRouteAdd(route, resolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700213
214 route = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700215 resolvedRoute = new ResolvedRoute(route, MAC3, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700216
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700217 verifyRouteAdd(route, resolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700218 }
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 Hart7f2d5742016-04-13 16:22:50 -0700228 private void verifyRouteAdd(Route route, ResolvedRoute resolvedRoute) {
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700229 reset(routeListener);
230
Charles Chan23686832017-08-23 14:46:43 -0700231 routeListener.event(event(RouteEvent.Type.ROUTE_ADDED, resolvedRoute, null,
232 Sets.newHashSet(resolvedRoute), null));
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700233
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 Hart96c146b2017-02-24 16:32:00 -0800248 ResolvedRoute resolvedRoute = new ResolvedRoute(route, MAC1, CP1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700249 ResolvedRoute updatedResolvedRoute = new ResolvedRoute(updatedRoute, MAC2, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700250
Jonathan Hart96c146b2017-02-24 16:32:00 -0800251 verifyRouteUpdated(route, updatedRoute, resolvedRoute, updatedResolvedRoute);
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700252
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 Chane4d13102016-11-08 15:38:44 -0800257 resolvedRoute = new ResolvedRoute(route, MAC1, CP1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700258 updatedResolvedRoute = new ResolvedRoute(updatedRoute, MAC2, CP1);
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700259
Charles Chane4d13102016-11-08 15:38:44 -0800260 verifyRouteUpdated(route, updatedRoute, resolvedRoute, updatedResolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700261
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 Chane4d13102016-11-08 15:38:44 -0800264 resolvedRoute = new ResolvedRoute(route, MAC3, CP1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700265 updatedResolvedRoute = new ResolvedRoute(updatedRoute, MAC4, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700266
Jonathan Hart96c146b2017-02-24 16:32:00 -0800267 verifyRouteUpdated(route, updatedRoute, resolvedRoute, updatedResolvedRoute);
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700268 }
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 Chane4d13102016-11-08 15:38:44 -0800276 * @param resolvedRoute resolved route before update
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700277 * @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 Chane4d13102016-11-08 15:38:44 -0800281 ResolvedRoute resolvedRoute,
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700282 ResolvedRoute updatedResolvedRoute) {
283 // First add the original route
284 addRoute(original);
285
Jonathan Hartf7021682017-03-22 18:17:21 -0700286 routeListener.event(event(RouteEvent.Type.ROUTE_UPDATED,
Charles Chan23686832017-08-23 14:46:43 -0700287 updatedResolvedRoute, resolvedRoute,
288 Sets.newHashSet(updatedResolvedRoute), Sets.newHashSet(resolvedRoute)));
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700289 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 Hart7f2d5742016-04-13 16:22:50 -0700302 public void testRouteDelete() {
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700303 Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1);
Charles Chane4d13102016-11-08 15:38:44 -0800304 ResolvedRoute removedResolvedRoute = new ResolvedRoute(route, MAC1, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700305
Charles Chane4d13102016-11-08 15:38:44 -0800306 verifyDelete(route, removedResolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700307
308 route = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP1);
Charles Chane4d13102016-11-08 15:38:44 -0800309 removedResolvedRoute = new ResolvedRoute(route, MAC3, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700310
Charles Chane4d13102016-11-08 15:38:44 -0800311 verifyDelete(route, removedResolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700312 }
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 Chane4d13102016-11-08 15:38:44 -0800319 * @param removedResolvedRoute the resolved route being removed
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700320 */
Charles Chane4d13102016-11-08 15:38:44 -0800321 private void verifyDelete(Route route, ResolvedRoute removedResolvedRoute) {
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700322 addRoute(route);
323
324 RouteEvent withdrawRouteEvent = new RouteEvent(RouteEvent.Type.ROUTE_REMOVED,
Charles Chan23686832017-08-23 14:46:43 -0700325 removedResolvedRoute, Sets.newHashSet(removedResolvedRoute));
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700326
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 Hart7f2d5742016-04-13 16:22:50 -0700338 * 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 Hart6c2e7962016-04-11 13:54:09 -0700340 */
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 Hart96c146b2017-02-24 16:32:00 -0800350 expectLastCall().anyTimes();
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700351 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 Chan23686832017-08-23 14:46:43 -0700363 ResolvedRoute resolvedRoute = new ResolvedRoute(route, MAC1, CP1);
364 routeListener.event(event(RouteEvent.Type.ROUTE_ADDED, resolvedRoute, null,
365 Sets.newHashSet(resolvedRoute), null));
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700366 replay(routeListener);
367
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700368 Host host = createHost(MAC1, V4_NEXT_HOP1);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800369
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 Hart6c2e7962016-04-11 13:54:09 -0700379 hostListener.event(new HostEvent(HostEvent.Type.HOST_ADDED, host));
380
381 verify(routeListener);
382 }
383
Charles Chan23686832017-08-23 14:46:43 -0700384 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 Hartf7021682017-03-22 18:17:21 -0700388 }
389
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700390 /**
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}