blob: 8febe682d1dd68442e091e72f7bf617d8028c029 [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
Ray Milkey69ec8712017-08-08 13:00:43 -070019import java.util.Collections;
20
Jonathan Hart6c2e7962016-04-11 13:54:09 -070021import org.junit.Before;
22import org.junit.Test;
23import org.onlab.packet.Ip4Address;
24import org.onlab.packet.Ip4Prefix;
25import org.onlab.packet.Ip6Address;
26import org.onlab.packet.Ip6Prefix;
27import org.onlab.packet.IpAddress;
28import org.onlab.packet.IpPrefix;
29import org.onlab.packet.MacAddress;
30import org.onlab.packet.VlanId;
Ray Milkey69ec8712017-08-08 13:00:43 -070031import org.onosproject.routeservice.ResolvedRoute;
32import org.onosproject.routeservice.Route;
33import org.onosproject.routeservice.RouteEvent;
34import org.onosproject.routeservice.RouteListener;
35import org.onosproject.routeservice.store.LocalRouteStore;
Jonathan Hartd4be52f2017-05-25 14:21:44 -070036import org.onosproject.cluster.ClusterService;
Jonathan Hart6c2e7962016-04-11 13:54:09 -070037import org.onosproject.net.ConnectPoint;
38import org.onosproject.net.DefaultHost;
39import org.onosproject.net.DeviceId;
40import org.onosproject.net.Host;
41import org.onosproject.net.HostId;
42import org.onosproject.net.HostLocation;
43import org.onosproject.net.PortNumber;
44import org.onosproject.net.host.HostEvent;
45import org.onosproject.net.host.HostListener;
46import org.onosproject.net.host.HostService;
47import org.onosproject.net.host.HostServiceAdapter;
48import org.onosproject.net.provider.ProviderId;
Jonathan Hartd4be52f2017-05-25 14:21:44 -070049import org.onosproject.store.service.StorageService;
50import org.onosproject.store.service.WorkQueue;
Jonathan Hart6c2e7962016-04-11 13:54:09 -070051
Ray Milkey69ec8712017-08-08 13:00:43 -070052import com.google.common.collect.Sets;
Jonathan Hart6c2e7962016-04-11 13:54:09 -070053
54import static org.easymock.EasyMock.anyObject;
Jonathan Hartd4be52f2017-05-25 14:21:44 -070055import static org.easymock.EasyMock.anyString;
Jonathan Hart6c2e7962016-04-11 13:54:09 -070056import static org.easymock.EasyMock.createMock;
Jonathan Hartd4be52f2017-05-25 14:21:44 -070057import static org.easymock.EasyMock.createNiceMock;
Jonathan Hart6c2e7962016-04-11 13:54:09 -070058import static org.easymock.EasyMock.expect;
59import static org.easymock.EasyMock.expectLastCall;
60import static org.easymock.EasyMock.replay;
61import static org.easymock.EasyMock.reset;
62import static org.easymock.EasyMock.verify;
63
64/**
65 * Unit tests for the route manager.
66 */
67public class RouteManagerTest {
68
69 private static final ConnectPoint CP1 = new ConnectPoint(
70 DeviceId.deviceId("of:0000000000000001"),
71 PortNumber.portNumber(1));
72
73 private static final IpPrefix V4_PREFIX1 = Ip4Prefix.valueOf("1.1.1.0/24");
Jonathan Hart7f2d5742016-04-13 16:22:50 -070074 private static final IpPrefix V4_PREFIX2 = Ip4Prefix.valueOf("2.2.2.0/24");
Jonathan Hart6c2e7962016-04-11 13:54:09 -070075 private static final IpPrefix V6_PREFIX1 = Ip6Prefix.valueOf("4000::/64");
76
77 private static final IpAddress V4_NEXT_HOP1 = Ip4Address.valueOf("192.168.10.1");
78 private static final IpAddress V4_NEXT_HOP2 = Ip4Address.valueOf("192.168.20.1");
79 private static final IpAddress V6_NEXT_HOP1 = Ip6Address.valueOf("1000::1");
80 private static final IpAddress V6_NEXT_HOP2 = Ip6Address.valueOf("2000::1");
81
82 private static final MacAddress MAC1 = MacAddress.valueOf("00:00:00:00:00:01");
83 private static final MacAddress MAC2 = MacAddress.valueOf("00:00:00:00:00:02");
84 private static final MacAddress MAC3 = MacAddress.valueOf("00:00:00:00:00:03");
85 private static final MacAddress MAC4 = MacAddress.valueOf("00:00:00:00:00:04");
86
87 private HostService hostService;
88
89 private RouteListener routeListener;
90 private HostListener hostListener;
91
92 private RouteManager routeManager;
93
94 @Before
95 public void setUp() throws Exception {
96 setUpHostService();
97
98 routeListener = createMock(RouteListener.class);
99
100 routeManager = new TestRouteManager();
101 routeManager.hostService = hostService;
102
Jonathan Hartd4be52f2017-05-25 14:21:44 -0700103 routeManager.clusterService = createNiceMock(ClusterService.class);
104 replay(routeManager.clusterService);
105 routeManager.storageService = createNiceMock(StorageService.class);
106 expect(routeManager.storageService.getWorkQueue(anyString(), anyObject()))
107 .andReturn(createNiceMock(WorkQueue.class));
108 replay(routeManager.storageService);
109
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700110 LocalRouteStore routeStore = new LocalRouteStore();
111 routeStore.activate();
112 routeManager.routeStore = routeStore;
113 routeManager.activate();
114
115 routeManager.addListener(routeListener);
116 }
117
118 /**
119 * Sets up the host service with details of some hosts.
120 */
121 private void setUpHostService() {
122 hostService = createMock(HostService.class);
123
124 hostService.addListener(anyObject(HostListener.class));
125 expectLastCall().andDelegateTo(new TestHostService()).anyTimes();
126
127 Host host1 = createHost(MAC1, V4_NEXT_HOP1);
128 expectHost(host1);
129
130 Host host2 = createHost(MAC2, V4_NEXT_HOP2);
131 expectHost(host2);
132
133 Host host3 = createHost(MAC3, V6_NEXT_HOP1);
134 expectHost(host3);
135
136 Host host4 = createHost(MAC4, V6_NEXT_HOP2);
137 expectHost(host4);
138
139 replay(hostService);
140 }
141
142 /**
143 * Sets expectations on the host service for a given host.
144 *
145 * @param host host
146 */
147 private void expectHost(Host host) {
148 // Assume the host only has one IP address
149 IpAddress ip = host.ipAddresses().iterator().next();
150
151 expect(hostService.getHostsByIp(ip))
152 .andReturn(Sets.newHashSet(host)).anyTimes();
153 hostService.startMonitoringIp(ip);
154 expectLastCall().anyTimes();
155 }
156
157 /**
158 * Creates a host with the given parameters.
159 *
160 * @param macAddress MAC address
161 * @param ipAddress IP address
162 * @return new host
163 */
164 private Host createHost(MacAddress macAddress, IpAddress ipAddress) {
165 return new DefaultHost(ProviderId.NONE, HostId.NONE, macAddress,
166 VlanId.NONE, new HostLocation(CP1, 1),
167 Sets.newHashSet(ipAddress));
168 }
169
170 /**
171 * Adds a route to the route service without expecting any specific events
172 * to be sent to the route listeners.
173 *
174 * @param route route to add
175 */
176 private void addRoute(Route route) {
177 reset(routeListener);
178
179 routeListener.event(anyObject(RouteEvent.class));
180 expectLastCall().once();
181 replay(routeListener);
182
183 routeManager.update(Collections.singleton(route));
184
185 reset(routeListener);
186 }
187
188 /**
189 * Tests adding routes to the route manager.
190 */
191 @Test
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700192 public void testRouteAdd() {
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700193 Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700194 ResolvedRoute resolvedRoute = new ResolvedRoute(route, MAC1, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700195
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700196 verifyRouteAdd(route, resolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700197
198 route = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700199 resolvedRoute = new ResolvedRoute(route, MAC3, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700200
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700201 verifyRouteAdd(route, resolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700202 }
203
204 /**
205 * Tests adding a new route and verifies that the correct event was sent
206 * to the route listener.
207 *
208 * @param route route to add
209 * @param resolvedRoute resolved route that should be sent to the route
210 * listener
211 */
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700212 private void verifyRouteAdd(Route route, ResolvedRoute resolvedRoute) {
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700213 reset(routeListener);
214
Jonathan Hartf7021682017-03-22 18:17:21 -0700215 routeListener.event(event(RouteEvent.Type.ROUTE_ADDED, resolvedRoute));
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700216
217 replay(routeListener);
218
219 routeManager.update(Collections.singleton(route));
220
221 verify(routeListener);
222 }
223
224 /**
225 * Tests updating routes in the route manager.
226 */
227 @Test
228 public void testRouteUpdate() {
229 Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1);
230 Route updatedRoute = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP2);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800231 ResolvedRoute resolvedRoute = new ResolvedRoute(route, MAC1, CP1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700232 ResolvedRoute updatedResolvedRoute = new ResolvedRoute(updatedRoute, MAC2, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700233
Jonathan Hart96c146b2017-02-24 16:32:00 -0800234 verifyRouteUpdated(route, updatedRoute, resolvedRoute, updatedResolvedRoute);
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700235
236 // Different prefix pointing to the same next hop.
237 // In this case we expect to receive a ROUTE_UPDATED event.
238 route = new Route(Route.Source.STATIC, V4_PREFIX2, V4_NEXT_HOP1);
239 updatedRoute = new Route(Route.Source.STATIC, V4_PREFIX2, V4_NEXT_HOP2);
Charles Chane4d13102016-11-08 15:38:44 -0800240 resolvedRoute = new ResolvedRoute(route, MAC1, CP1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700241 updatedResolvedRoute = new ResolvedRoute(updatedRoute, MAC2, CP1);
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700242
Charles Chane4d13102016-11-08 15:38:44 -0800243 verifyRouteUpdated(route, updatedRoute, resolvedRoute, updatedResolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700244
245 route = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP1);
246 updatedRoute = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP2);
Charles Chane4d13102016-11-08 15:38:44 -0800247 resolvedRoute = new ResolvedRoute(route, MAC3, CP1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700248 updatedResolvedRoute = new ResolvedRoute(updatedRoute, MAC4, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700249
Jonathan Hart96c146b2017-02-24 16:32:00 -0800250 verifyRouteUpdated(route, updatedRoute, resolvedRoute, updatedResolvedRoute);
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700251 }
252
253 /**
254 * Tests updating a route and verifies that the route listener receives a
255 * route updated event.
256 *
257 * @param original original route
258 * @param updated updated route
Charles Chane4d13102016-11-08 15:38:44 -0800259 * @param resolvedRoute resolved route before update
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700260 * @param updatedResolvedRoute resolved route that is expected to be sent to
261 * the route listener
262 */
263 private void verifyRouteUpdated(Route original, Route updated,
Charles Chane4d13102016-11-08 15:38:44 -0800264 ResolvedRoute resolvedRoute,
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700265 ResolvedRoute updatedResolvedRoute) {
266 // First add the original route
267 addRoute(original);
268
Jonathan Hartf7021682017-03-22 18:17:21 -0700269 routeListener.event(event(RouteEvent.Type.ROUTE_UPDATED,
Charles Chane4d13102016-11-08 15:38:44 -0800270 updatedResolvedRoute, resolvedRoute));
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700271 expectLastCall().once();
272
273 replay(routeListener);
274
275 routeManager.update(Collections.singleton(updated));
276
277 verify(routeListener);
278 }
279
280 /**
281 * Tests deleting routes from the route manager.
282 */
283 @Test
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700284 public void testRouteDelete() {
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700285 Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1);
Charles Chane4d13102016-11-08 15:38:44 -0800286 ResolvedRoute removedResolvedRoute = new ResolvedRoute(route, MAC1, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700287
Charles Chane4d13102016-11-08 15:38:44 -0800288 verifyDelete(route, removedResolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700289
290 route = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP1);
Charles Chane4d13102016-11-08 15:38:44 -0800291 removedResolvedRoute = new ResolvedRoute(route, MAC3, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700292
Charles Chane4d13102016-11-08 15:38:44 -0800293 verifyDelete(route, removedResolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700294 }
295
296 /**
297 * Tests deleting a route and verifies that the correct event is sent to
298 * the route listener.
299 *
300 * @param route route to delete
Charles Chane4d13102016-11-08 15:38:44 -0800301 * @param removedResolvedRoute the resolved route being removed
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700302 */
Charles Chane4d13102016-11-08 15:38:44 -0800303 private void verifyDelete(Route route, ResolvedRoute removedResolvedRoute) {
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700304 addRoute(route);
305
306 RouteEvent withdrawRouteEvent = new RouteEvent(RouteEvent.Type.ROUTE_REMOVED,
Charles Chane4d13102016-11-08 15:38:44 -0800307 removedResolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700308
309 reset(routeListener);
310 routeListener.event(withdrawRouteEvent);
311
312 replay(routeListener);
313
314 routeManager.withdraw(Collections.singleton(route));
315
316 verify(routeListener);
317 }
318
319 /**
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700320 * Tests adding a route entry where the HostService does not immediately
321 * know the MAC address of the next hop, but this is learnt later.
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700322 */
323 @Test
324 public void testAsyncRouteAdd() {
325 Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1);
326
327 // Host service will reply with no hosts when asked
328 reset(hostService);
329 expect(hostService.getHostsByIp(anyObject(IpAddress.class))).andReturn(
330 Collections.emptySet()).anyTimes();
331 hostService.startMonitoringIp(V4_NEXT_HOP1);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800332 expectLastCall().anyTimes();
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700333 replay(hostService);
334
335 // Initially when we add the route, no route event will be sent because
336 // the host is not known
337 replay(routeListener);
338
339 routeManager.update(Collections.singleton(route));
340
341 verify(routeListener);
342
343 // Now when we send the event, we expect the FIB update to be sent
344 reset(routeListener);
Jonathan Hartf7021682017-03-22 18:17:21 -0700345 routeListener.event(event(RouteEvent.Type.ROUTE_ADDED,
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700346 new ResolvedRoute(route, MAC1, CP1)));
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700347 replay(routeListener);
348
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700349 Host host = createHost(MAC1, V4_NEXT_HOP1);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800350
351 // Set up the host service with a host
352 reset(hostService);
353 expect(hostService.getHostsByIp(V4_NEXT_HOP1)).andReturn(
354 Collections.singleton(host)).anyTimes();
355 hostService.startMonitoringIp(V4_NEXT_HOP1);
356 expectLastCall().anyTimes();
357 replay(hostService);
358
359 // Send in the host event
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700360 hostListener.event(new HostEvent(HostEvent.Type.HOST_ADDED, host));
361
362 verify(routeListener);
363 }
364
Jonathan Hartf7021682017-03-22 18:17:21 -0700365 private static RouteEvent event(RouteEvent.Type type, ResolvedRoute subject) {
366 return event(type, subject, null);
367 }
368
369 private static RouteEvent event(RouteEvent.Type type, ResolvedRoute subject, ResolvedRoute prevSubject) {
370 return new RouteEvent(type, subject, prevSubject, Collections.singleton(subject));
371 }
372
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700373 /**
374 * Test host service that stores a reference to the host listener.
375 */
376 private class TestHostService extends HostServiceAdapter {
377 @Override
378 public void addListener(HostListener listener) {
379 hostListener = listener;
380 }
381 }
382
383 /**
384 * Test route manager that extends the real route manager and injects a test
385 * listener queue instead of the real listener queue.
386 */
387 private static class TestRouteManager extends RouteManager {
388 @Override
389 ListenerQueue createListenerQueue(RouteListener listener) {
390 return new TestListenerQueue(listener);
391 }
392 }
393
394 /**
395 * Test listener queue that simply passes route events straight through to
396 * the route listener on the caller's thread.
397 */
398 private static class TestListenerQueue implements ListenerQueue {
399
400 private final RouteListener listener;
401
402 public TestListenerQueue(RouteListener listener) {
403 this.listener = listener;
404 }
405
406 @Override
407 public void post(RouteEvent event) {
408 listener.event(event);
409 }
410
411 @Override
412 public void start() {
413 }
414
415 @Override
416 public void stop() {
417 }
418 }
419
420}