blob: 3ba34da2a90f1d034a333e521419e4d1f17bb86c [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;
Jonathan Hartd4be52f2017-05-25 14:21:44 -070050import org.onosproject.store.service.StorageService;
51import org.onosproject.store.service.WorkQueue;
Jonathan Hart6c2e7962016-04-11 13:54:09 -070052
Ray Milkey69ec8712017-08-08 13:00:43 -070053import com.google.common.collect.Sets;
Jonathan Hart6c2e7962016-04-11 13:54:09 -070054
55import static org.easymock.EasyMock.anyObject;
Jonathan Hartd4be52f2017-05-25 14:21:44 -070056import static org.easymock.EasyMock.anyString;
Jonathan Hart6c2e7962016-04-11 13:54:09 -070057import static org.easymock.EasyMock.createMock;
Jonathan Hartd4be52f2017-05-25 14:21:44 -070058import static org.easymock.EasyMock.createNiceMock;
Jonathan Hart6c2e7962016-04-11 13:54:09 -070059import static org.easymock.EasyMock.expect;
60import static org.easymock.EasyMock.expectLastCall;
61import static org.easymock.EasyMock.replay;
62import static org.easymock.EasyMock.reset;
63import static org.easymock.EasyMock.verify;
64
65/**
66 * Unit tests for the route manager.
67 */
68public class RouteManagerTest {
69
70 private static final ConnectPoint CP1 = new ConnectPoint(
71 DeviceId.deviceId("of:0000000000000001"),
72 PortNumber.portNumber(1));
73
74 private static final IpPrefix V4_PREFIX1 = Ip4Prefix.valueOf("1.1.1.0/24");
Jonathan Hart7f2d5742016-04-13 16:22:50 -070075 private static final IpPrefix V4_PREFIX2 = Ip4Prefix.valueOf("2.2.2.0/24");
Jonathan Hart6c2e7962016-04-11 13:54:09 -070076 private static final IpPrefix V6_PREFIX1 = Ip6Prefix.valueOf("4000::/64");
77
78 private static final IpAddress V4_NEXT_HOP1 = Ip4Address.valueOf("192.168.10.1");
79 private static final IpAddress V4_NEXT_HOP2 = Ip4Address.valueOf("192.168.20.1");
80 private static final IpAddress V6_NEXT_HOP1 = Ip6Address.valueOf("1000::1");
81 private static final IpAddress V6_NEXT_HOP2 = Ip6Address.valueOf("2000::1");
82
83 private static final MacAddress MAC1 = MacAddress.valueOf("00:00:00:00:00:01");
84 private static final MacAddress MAC2 = MacAddress.valueOf("00:00:00:00:00:02");
85 private static final MacAddress MAC3 = MacAddress.valueOf("00:00:00:00:00:03");
86 private static final MacAddress MAC4 = MacAddress.valueOf("00:00:00:00:00:04");
87
88 private HostService hostService;
89
90 private RouteListener routeListener;
91 private HostListener hostListener;
92
93 private RouteManager routeManager;
94
95 @Before
96 public void setUp() throws Exception {
97 setUpHostService();
98
99 routeListener = createMock(RouteListener.class);
100
101 routeManager = new TestRouteManager();
102 routeManager.hostService = hostService;
103
Jonathan Hartd4be52f2017-05-25 14:21:44 -0700104 routeManager.clusterService = createNiceMock(ClusterService.class);
105 replay(routeManager.clusterService);
106 routeManager.storageService = createNiceMock(StorageService.class);
107 expect(routeManager.storageService.getWorkQueue(anyString(), anyObject()))
108 .andReturn(createNiceMock(WorkQueue.class));
109 replay(routeManager.storageService);
110
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700111 LocalRouteStore routeStore = new LocalRouteStore();
112 routeStore.activate();
113 routeManager.routeStore = routeStore;
114 routeManager.activate();
115
116 routeManager.addListener(routeListener);
117 }
118
119 /**
120 * Sets up the host service with details of some hosts.
121 */
122 private void setUpHostService() {
123 hostService = createMock(HostService.class);
124
125 hostService.addListener(anyObject(HostListener.class));
126 expectLastCall().andDelegateTo(new TestHostService()).anyTimes();
127
128 Host host1 = createHost(MAC1, V4_NEXT_HOP1);
129 expectHost(host1);
130
131 Host host2 = createHost(MAC2, V4_NEXT_HOP2);
132 expectHost(host2);
133
134 Host host3 = createHost(MAC3, V6_NEXT_HOP1);
135 expectHost(host3);
136
137 Host host4 = createHost(MAC4, V6_NEXT_HOP2);
138 expectHost(host4);
139
140 replay(hostService);
141 }
142
143 /**
144 * Sets expectations on the host service for a given host.
145 *
146 * @param host host
147 */
148 private void expectHost(Host host) {
149 // Assume the host only has one IP address
150 IpAddress ip = host.ipAddresses().iterator().next();
151
152 expect(hostService.getHostsByIp(ip))
153 .andReturn(Sets.newHashSet(host)).anyTimes();
154 hostService.startMonitoringIp(ip);
155 expectLastCall().anyTimes();
156 }
157
158 /**
159 * Creates a host with the given parameters.
160 *
161 * @param macAddress MAC address
162 * @param ipAddress IP address
163 * @return new host
164 */
165 private Host createHost(MacAddress macAddress, IpAddress ipAddress) {
166 return new DefaultHost(ProviderId.NONE, HostId.NONE, macAddress,
167 VlanId.NONE, new HostLocation(CP1, 1),
168 Sets.newHashSet(ipAddress));
169 }
170
171 /**
172 * Adds a route to the route service without expecting any specific events
173 * to be sent to the route listeners.
174 *
175 * @param route route to add
176 */
177 private void addRoute(Route route) {
178 reset(routeListener);
179
180 routeListener.event(anyObject(RouteEvent.class));
181 expectLastCall().once();
182 replay(routeListener);
183
184 routeManager.update(Collections.singleton(route));
185
186 reset(routeListener);
187 }
188
189 /**
190 * Tests adding routes to the route manager.
191 */
192 @Test
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700193 public void testRouteAdd() {
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700194 Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700195 ResolvedRoute resolvedRoute = new ResolvedRoute(route, MAC1, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700196
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700197 verifyRouteAdd(route, resolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700198
199 route = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700200 resolvedRoute = new ResolvedRoute(route, MAC3, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700201
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700202 verifyRouteAdd(route, resolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700203 }
204
205 /**
206 * Tests adding a new route and verifies that the correct event was sent
207 * to the route listener.
208 *
209 * @param route route to add
210 * @param resolvedRoute resolved route that should be sent to the route
211 * listener
212 */
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700213 private void verifyRouteAdd(Route route, ResolvedRoute resolvedRoute) {
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700214 reset(routeListener);
215
Charles Chan23686832017-08-23 14:46:43 -0700216 routeListener.event(event(RouteEvent.Type.ROUTE_ADDED, resolvedRoute, null,
217 Sets.newHashSet(resolvedRoute), null));
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700218
219 replay(routeListener);
220
221 routeManager.update(Collections.singleton(route));
222
223 verify(routeListener);
224 }
225
226 /**
227 * Tests updating routes in the route manager.
228 */
229 @Test
230 public void testRouteUpdate() {
231 Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1);
232 Route updatedRoute = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP2);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800233 ResolvedRoute resolvedRoute = new ResolvedRoute(route, MAC1, CP1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700234 ResolvedRoute updatedResolvedRoute = new ResolvedRoute(updatedRoute, MAC2, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700235
Jonathan Hart96c146b2017-02-24 16:32:00 -0800236 verifyRouteUpdated(route, updatedRoute, resolvedRoute, updatedResolvedRoute);
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700237
238 // Different prefix pointing to the same next hop.
239 // In this case we expect to receive a ROUTE_UPDATED event.
240 route = new Route(Route.Source.STATIC, V4_PREFIX2, V4_NEXT_HOP1);
241 updatedRoute = new Route(Route.Source.STATIC, V4_PREFIX2, V4_NEXT_HOP2);
Charles Chane4d13102016-11-08 15:38:44 -0800242 resolvedRoute = new ResolvedRoute(route, MAC1, CP1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700243 updatedResolvedRoute = new ResolvedRoute(updatedRoute, MAC2, CP1);
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700244
Charles Chane4d13102016-11-08 15:38:44 -0800245 verifyRouteUpdated(route, updatedRoute, resolvedRoute, updatedResolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700246
247 route = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP1);
248 updatedRoute = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP2);
Charles Chane4d13102016-11-08 15:38:44 -0800249 resolvedRoute = new ResolvedRoute(route, MAC3, CP1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700250 updatedResolvedRoute = new ResolvedRoute(updatedRoute, MAC4, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700251
Jonathan Hart96c146b2017-02-24 16:32:00 -0800252 verifyRouteUpdated(route, updatedRoute, resolvedRoute, updatedResolvedRoute);
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700253 }
254
255 /**
256 * Tests updating a route and verifies that the route listener receives a
257 * route updated event.
258 *
259 * @param original original route
260 * @param updated updated route
Charles Chane4d13102016-11-08 15:38:44 -0800261 * @param resolvedRoute resolved route before update
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700262 * @param updatedResolvedRoute resolved route that is expected to be sent to
263 * the route listener
264 */
265 private void verifyRouteUpdated(Route original, Route updated,
Charles Chane4d13102016-11-08 15:38:44 -0800266 ResolvedRoute resolvedRoute,
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700267 ResolvedRoute updatedResolvedRoute) {
268 // First add the original route
269 addRoute(original);
270
Jonathan Hartf7021682017-03-22 18:17:21 -0700271 routeListener.event(event(RouteEvent.Type.ROUTE_UPDATED,
Charles Chan23686832017-08-23 14:46:43 -0700272 updatedResolvedRoute, resolvedRoute,
273 Sets.newHashSet(updatedResolvedRoute), Sets.newHashSet(resolvedRoute)));
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700274 expectLastCall().once();
275
276 replay(routeListener);
277
278 routeManager.update(Collections.singleton(updated));
279
280 verify(routeListener);
281 }
282
283 /**
284 * Tests deleting routes from the route manager.
285 */
286 @Test
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700287 public void testRouteDelete() {
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700288 Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1);
Charles Chane4d13102016-11-08 15:38:44 -0800289 ResolvedRoute removedResolvedRoute = new ResolvedRoute(route, MAC1, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700290
Charles Chane4d13102016-11-08 15:38:44 -0800291 verifyDelete(route, removedResolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700292
293 route = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP1);
Charles Chane4d13102016-11-08 15:38:44 -0800294 removedResolvedRoute = new ResolvedRoute(route, MAC3, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700295
Charles Chane4d13102016-11-08 15:38:44 -0800296 verifyDelete(route, removedResolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700297 }
298
299 /**
300 * Tests deleting a route and verifies that the correct event is sent to
301 * the route listener.
302 *
303 * @param route route to delete
Charles Chane4d13102016-11-08 15:38:44 -0800304 * @param removedResolvedRoute the resolved route being removed
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700305 */
Charles Chane4d13102016-11-08 15:38:44 -0800306 private void verifyDelete(Route route, ResolvedRoute removedResolvedRoute) {
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700307 addRoute(route);
308
309 RouteEvent withdrawRouteEvent = new RouteEvent(RouteEvent.Type.ROUTE_REMOVED,
Charles Chan23686832017-08-23 14:46:43 -0700310 removedResolvedRoute, Sets.newHashSet(removedResolvedRoute));
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700311
312 reset(routeListener);
313 routeListener.event(withdrawRouteEvent);
314
315 replay(routeListener);
316
317 routeManager.withdraw(Collections.singleton(route));
318
319 verify(routeListener);
320 }
321
322 /**
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700323 * Tests adding a route entry where the HostService does not immediately
324 * know the MAC address of the next hop, but this is learnt later.
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700325 */
326 @Test
327 public void testAsyncRouteAdd() {
328 Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1);
329
330 // Host service will reply with no hosts when asked
331 reset(hostService);
332 expect(hostService.getHostsByIp(anyObject(IpAddress.class))).andReturn(
333 Collections.emptySet()).anyTimes();
334 hostService.startMonitoringIp(V4_NEXT_HOP1);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800335 expectLastCall().anyTimes();
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700336 replay(hostService);
337
338 // Initially when we add the route, no route event will be sent because
339 // the host is not known
340 replay(routeListener);
341
342 routeManager.update(Collections.singleton(route));
343
344 verify(routeListener);
345
346 // Now when we send the event, we expect the FIB update to be sent
347 reset(routeListener);
Charles Chan23686832017-08-23 14:46:43 -0700348 ResolvedRoute resolvedRoute = new ResolvedRoute(route, MAC1, CP1);
349 routeListener.event(event(RouteEvent.Type.ROUTE_ADDED, resolvedRoute, null,
350 Sets.newHashSet(resolvedRoute), null));
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700351 replay(routeListener);
352
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700353 Host host = createHost(MAC1, V4_NEXT_HOP1);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800354
355 // Set up the host service with a host
356 reset(hostService);
357 expect(hostService.getHostsByIp(V4_NEXT_HOP1)).andReturn(
358 Collections.singleton(host)).anyTimes();
359 hostService.startMonitoringIp(V4_NEXT_HOP1);
360 expectLastCall().anyTimes();
361 replay(hostService);
362
363 // Send in the host event
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700364 hostListener.event(new HostEvent(HostEvent.Type.HOST_ADDED, host));
365
366 verify(routeListener);
367 }
368
Charles Chan23686832017-08-23 14:46:43 -0700369 private static RouteEvent event(RouteEvent.Type type, ResolvedRoute subject, ResolvedRoute prevSubject,
370 Collection<ResolvedRoute> alternatives,
371 Collection<ResolvedRoute> prevAlternatives) {
372 return new RouteEvent(type, subject, prevSubject, alternatives, prevAlternatives);
Jonathan Hartf7021682017-03-22 18:17:21 -0700373 }
374
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700375 /**
376 * Test host service that stores a reference to the host listener.
377 */
378 private class TestHostService extends HostServiceAdapter {
379 @Override
380 public void addListener(HostListener listener) {
381 hostListener = listener;
382 }
383 }
384
385 /**
386 * Test route manager that extends the real route manager and injects a test
387 * listener queue instead of the real listener queue.
388 */
389 private static class TestRouteManager extends RouteManager {
390 @Override
391 ListenerQueue createListenerQueue(RouteListener listener) {
392 return new TestListenerQueue(listener);
393 }
394 }
395
396 /**
397 * Test listener queue that simply passes route events straight through to
398 * the route listener on the caller's thread.
399 */
400 private static class TestListenerQueue implements ListenerQueue {
401
402 private final RouteListener listener;
403
404 public TestListenerQueue(RouteListener listener) {
405 this.listener = listener;
406 }
407
408 @Override
409 public void post(RouteEvent event) {
410 listener.event(event);
411 }
412
413 @Override
414 public void start() {
415 }
416
417 @Override
418 public void stop() {
419 }
420 }
421
422}