blob: 7ab3044a1061a0ca74dca62b81cc9628499c7224 [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
17package org.onosproject.incubator.net.routing.impl;
18
19import com.google.common.collect.Sets;
20import org.junit.Before;
21import org.junit.Test;
22import org.onlab.packet.Ip4Address;
23import org.onlab.packet.Ip4Prefix;
24import org.onlab.packet.Ip6Address;
25import org.onlab.packet.Ip6Prefix;
26import org.onlab.packet.IpAddress;
27import org.onlab.packet.IpPrefix;
28import org.onlab.packet.MacAddress;
29import org.onlab.packet.VlanId;
Jonathan Hartd4be52f2017-05-25 14:21:44 -070030import org.onosproject.cluster.ClusterService;
Jonathan Hart6c2e7962016-04-11 13:54:09 -070031import org.onosproject.incubator.net.routing.ResolvedRoute;
32import org.onosproject.incubator.net.routing.Route;
33import org.onosproject.incubator.net.routing.RouteEvent;
34import org.onosproject.incubator.net.routing.RouteListener;
35import org.onosproject.incubator.store.routing.impl.LocalRouteStore;
36import org.onosproject.net.ConnectPoint;
37import org.onosproject.net.DefaultHost;
38import org.onosproject.net.DeviceId;
39import org.onosproject.net.Host;
40import org.onosproject.net.HostId;
41import org.onosproject.net.HostLocation;
42import org.onosproject.net.PortNumber;
43import org.onosproject.net.host.HostEvent;
44import org.onosproject.net.host.HostListener;
45import org.onosproject.net.host.HostService;
46import org.onosproject.net.host.HostServiceAdapter;
47import org.onosproject.net.provider.ProviderId;
Jonathan Hartd4be52f2017-05-25 14:21:44 -070048import org.onosproject.store.service.StorageService;
49import org.onosproject.store.service.WorkQueue;
Jonathan Hart6c2e7962016-04-11 13:54:09 -070050
51import java.util.Collections;
52
53import static org.easymock.EasyMock.anyObject;
Jonathan Hartd4be52f2017-05-25 14:21:44 -070054import static org.easymock.EasyMock.anyString;
Jonathan Hart6c2e7962016-04-11 13:54:09 -070055import static org.easymock.EasyMock.createMock;
Jonathan Hartd4be52f2017-05-25 14:21:44 -070056import static org.easymock.EasyMock.createNiceMock;
Jonathan Hart6c2e7962016-04-11 13:54:09 -070057import static org.easymock.EasyMock.expect;
58import static org.easymock.EasyMock.expectLastCall;
59import static org.easymock.EasyMock.replay;
60import static org.easymock.EasyMock.reset;
61import static org.easymock.EasyMock.verify;
62
63/**
64 * Unit tests for the route manager.
65 */
66public class RouteManagerTest {
67
68 private static final ConnectPoint CP1 = new ConnectPoint(
69 DeviceId.deviceId("of:0000000000000001"),
70 PortNumber.portNumber(1));
71
72 private static final IpPrefix V4_PREFIX1 = Ip4Prefix.valueOf("1.1.1.0/24");
Jonathan Hart7f2d5742016-04-13 16:22:50 -070073 private static final IpPrefix V4_PREFIX2 = Ip4Prefix.valueOf("2.2.2.0/24");
Jonathan Hart6c2e7962016-04-11 13:54:09 -070074 private static final IpPrefix V6_PREFIX1 = Ip6Prefix.valueOf("4000::/64");
75
76 private static final IpAddress V4_NEXT_HOP1 = Ip4Address.valueOf("192.168.10.1");
77 private static final IpAddress V4_NEXT_HOP2 = Ip4Address.valueOf("192.168.20.1");
78 private static final IpAddress V6_NEXT_HOP1 = Ip6Address.valueOf("1000::1");
79 private static final IpAddress V6_NEXT_HOP2 = Ip6Address.valueOf("2000::1");
80
81 private static final MacAddress MAC1 = MacAddress.valueOf("00:00:00:00:00:01");
82 private static final MacAddress MAC2 = MacAddress.valueOf("00:00:00:00:00:02");
83 private static final MacAddress MAC3 = MacAddress.valueOf("00:00:00:00:00:03");
84 private static final MacAddress MAC4 = MacAddress.valueOf("00:00:00:00:00:04");
85
86 private HostService hostService;
87
88 private RouteListener routeListener;
89 private HostListener hostListener;
90
91 private RouteManager routeManager;
92
93 @Before
94 public void setUp() throws Exception {
95 setUpHostService();
96
97 routeListener = createMock(RouteListener.class);
98
99 routeManager = new TestRouteManager();
100 routeManager.hostService = hostService;
101
Jonathan Hartd4be52f2017-05-25 14:21:44 -0700102 routeManager.clusterService = createNiceMock(ClusterService.class);
103 replay(routeManager.clusterService);
104 routeManager.storageService = createNiceMock(StorageService.class);
105 expect(routeManager.storageService.getWorkQueue(anyString(), anyObject()))
106 .andReturn(createNiceMock(WorkQueue.class));
107 replay(routeManager.storageService);
108
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700109 LocalRouteStore routeStore = new LocalRouteStore();
110 routeStore.activate();
111 routeManager.routeStore = routeStore;
112 routeManager.activate();
113
114 routeManager.addListener(routeListener);
115 }
116
117 /**
118 * Sets up the host service with details of some hosts.
119 */
120 private void setUpHostService() {
121 hostService = createMock(HostService.class);
122
123 hostService.addListener(anyObject(HostListener.class));
124 expectLastCall().andDelegateTo(new TestHostService()).anyTimes();
125
126 Host host1 = createHost(MAC1, V4_NEXT_HOP1);
127 expectHost(host1);
128
129 Host host2 = createHost(MAC2, V4_NEXT_HOP2);
130 expectHost(host2);
131
132 Host host3 = createHost(MAC3, V6_NEXT_HOP1);
133 expectHost(host3);
134
135 Host host4 = createHost(MAC4, V6_NEXT_HOP2);
136 expectHost(host4);
137
138 replay(hostService);
139 }
140
141 /**
142 * Sets expectations on the host service for a given host.
143 *
144 * @param host host
145 */
146 private void expectHost(Host host) {
147 // Assume the host only has one IP address
148 IpAddress ip = host.ipAddresses().iterator().next();
149
150 expect(hostService.getHostsByIp(ip))
151 .andReturn(Sets.newHashSet(host)).anyTimes();
152 hostService.startMonitoringIp(ip);
153 expectLastCall().anyTimes();
154 }
155
156 /**
157 * Creates a host with the given parameters.
158 *
159 * @param macAddress MAC address
160 * @param ipAddress IP address
161 * @return new host
162 */
163 private Host createHost(MacAddress macAddress, IpAddress ipAddress) {
164 return new DefaultHost(ProviderId.NONE, HostId.NONE, macAddress,
165 VlanId.NONE, new HostLocation(CP1, 1),
166 Sets.newHashSet(ipAddress));
167 }
168
169 /**
170 * Adds a route to the route service without expecting any specific events
171 * to be sent to the route listeners.
172 *
173 * @param route route to add
174 */
175 private void addRoute(Route route) {
176 reset(routeListener);
177
178 routeListener.event(anyObject(RouteEvent.class));
179 expectLastCall().once();
180 replay(routeListener);
181
182 routeManager.update(Collections.singleton(route));
183
184 reset(routeListener);
185 }
186
187 /**
188 * Tests adding routes to the route manager.
189 */
190 @Test
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700191 public void testRouteAdd() {
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700192 Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700193 ResolvedRoute resolvedRoute = new ResolvedRoute(route, MAC1, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700194
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700195 verifyRouteAdd(route, resolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700196
197 route = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700198 resolvedRoute = new ResolvedRoute(route, MAC3, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700199
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700200 verifyRouteAdd(route, resolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700201 }
202
203 /**
204 * Tests adding a new route and verifies that the correct event was sent
205 * to the route listener.
206 *
207 * @param route route to add
208 * @param resolvedRoute resolved route that should be sent to the route
209 * listener
210 */
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700211 private void verifyRouteAdd(Route route, ResolvedRoute resolvedRoute) {
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700212 reset(routeListener);
213
Jonathan Hartf7021682017-03-22 18:17:21 -0700214 routeListener.event(event(RouteEvent.Type.ROUTE_ADDED, resolvedRoute));
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700215
216 replay(routeListener);
217
218 routeManager.update(Collections.singleton(route));
219
220 verify(routeListener);
221 }
222
223 /**
224 * Tests updating routes in the route manager.
225 */
226 @Test
227 public void testRouteUpdate() {
228 Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1);
229 Route updatedRoute = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP2);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800230 ResolvedRoute resolvedRoute = new ResolvedRoute(route, MAC1, CP1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700231 ResolvedRoute updatedResolvedRoute = new ResolvedRoute(updatedRoute, MAC2, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700232
Jonathan Hart96c146b2017-02-24 16:32:00 -0800233 verifyRouteUpdated(route, updatedRoute, resolvedRoute, updatedResolvedRoute);
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700234
235 // Different prefix pointing to the same next hop.
236 // In this case we expect to receive a ROUTE_UPDATED event.
237 route = new Route(Route.Source.STATIC, V4_PREFIX2, V4_NEXT_HOP1);
238 updatedRoute = new Route(Route.Source.STATIC, V4_PREFIX2, V4_NEXT_HOP2);
Charles Chane4d13102016-11-08 15:38:44 -0800239 resolvedRoute = new ResolvedRoute(route, MAC1, CP1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700240 updatedResolvedRoute = new ResolvedRoute(updatedRoute, MAC2, CP1);
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700241
Charles Chane4d13102016-11-08 15:38:44 -0800242 verifyRouteUpdated(route, updatedRoute, resolvedRoute, updatedResolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700243
244 route = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP1);
245 updatedRoute = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP2);
Charles Chane4d13102016-11-08 15:38:44 -0800246 resolvedRoute = new ResolvedRoute(route, MAC3, CP1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700247 updatedResolvedRoute = new ResolvedRoute(updatedRoute, MAC4, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700248
Jonathan Hart96c146b2017-02-24 16:32:00 -0800249 verifyRouteUpdated(route, updatedRoute, resolvedRoute, updatedResolvedRoute);
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700250 }
251
252 /**
253 * Tests updating a route and verifies that the route listener receives a
254 * route updated event.
255 *
256 * @param original original route
257 * @param updated updated route
Charles Chane4d13102016-11-08 15:38:44 -0800258 * @param resolvedRoute resolved route before update
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700259 * @param updatedResolvedRoute resolved route that is expected to be sent to
260 * the route listener
261 */
262 private void verifyRouteUpdated(Route original, Route updated,
Charles Chane4d13102016-11-08 15:38:44 -0800263 ResolvedRoute resolvedRoute,
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700264 ResolvedRoute updatedResolvedRoute) {
265 // First add the original route
266 addRoute(original);
267
Jonathan Hartf7021682017-03-22 18:17:21 -0700268 routeListener.event(event(RouteEvent.Type.ROUTE_UPDATED,
Charles Chane4d13102016-11-08 15:38:44 -0800269 updatedResolvedRoute, resolvedRoute));
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700270 expectLastCall().once();
271
272 replay(routeListener);
273
274 routeManager.update(Collections.singleton(updated));
275
276 verify(routeListener);
277 }
278
279 /**
280 * Tests deleting routes from the route manager.
281 */
282 @Test
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700283 public void testRouteDelete() {
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700284 Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1);
Charles Chane4d13102016-11-08 15:38:44 -0800285 ResolvedRoute removedResolvedRoute = new ResolvedRoute(route, MAC1, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700286
Charles Chane4d13102016-11-08 15:38:44 -0800287 verifyDelete(route, removedResolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700288
289 route = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP1);
Charles Chane4d13102016-11-08 15:38:44 -0800290 removedResolvedRoute = new ResolvedRoute(route, MAC3, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700291
Charles Chane4d13102016-11-08 15:38:44 -0800292 verifyDelete(route, removedResolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700293 }
294
295 /**
296 * Tests deleting a route and verifies that the correct event is sent to
297 * the route listener.
298 *
299 * @param route route to delete
Charles Chane4d13102016-11-08 15:38:44 -0800300 * @param removedResolvedRoute the resolved route being removed
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700301 */
Charles Chane4d13102016-11-08 15:38:44 -0800302 private void verifyDelete(Route route, ResolvedRoute removedResolvedRoute) {
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700303 addRoute(route);
304
305 RouteEvent withdrawRouteEvent = new RouteEvent(RouteEvent.Type.ROUTE_REMOVED,
Charles Chane4d13102016-11-08 15:38:44 -0800306 removedResolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700307
308 reset(routeListener);
309 routeListener.event(withdrawRouteEvent);
310
311 replay(routeListener);
312
313 routeManager.withdraw(Collections.singleton(route));
314
315 verify(routeListener);
316 }
317
318 /**
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700319 * Tests adding a route entry where the HostService does not immediately
320 * know the MAC address of the next hop, but this is learnt later.
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700321 */
322 @Test
323 public void testAsyncRouteAdd() {
324 Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1);
325
326 // Host service will reply with no hosts when asked
327 reset(hostService);
328 expect(hostService.getHostsByIp(anyObject(IpAddress.class))).andReturn(
329 Collections.emptySet()).anyTimes();
330 hostService.startMonitoringIp(V4_NEXT_HOP1);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800331 expectLastCall().anyTimes();
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700332 replay(hostService);
333
334 // Initially when we add the route, no route event will be sent because
335 // the host is not known
336 replay(routeListener);
337
338 routeManager.update(Collections.singleton(route));
339
340 verify(routeListener);
341
342 // Now when we send the event, we expect the FIB update to be sent
343 reset(routeListener);
Jonathan Hartf7021682017-03-22 18:17:21 -0700344 routeListener.event(event(RouteEvent.Type.ROUTE_ADDED,
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700345 new ResolvedRoute(route, MAC1, CP1)));
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700346 replay(routeListener);
347
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700348 Host host = createHost(MAC1, V4_NEXT_HOP1);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800349
350 // Set up the host service with a host
351 reset(hostService);
352 expect(hostService.getHostsByIp(V4_NEXT_HOP1)).andReturn(
353 Collections.singleton(host)).anyTimes();
354 hostService.startMonitoringIp(V4_NEXT_HOP1);
355 expectLastCall().anyTimes();
356 replay(hostService);
357
358 // Send in the host event
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700359 hostListener.event(new HostEvent(HostEvent.Type.HOST_ADDED, host));
360
361 verify(routeListener);
362 }
363
Jonathan Hartf7021682017-03-22 18:17:21 -0700364 private static RouteEvent event(RouteEvent.Type type, ResolvedRoute subject) {
365 return event(type, subject, null);
366 }
367
368 private static RouteEvent event(RouteEvent.Type type, ResolvedRoute subject, ResolvedRoute prevSubject) {
369 return new RouteEvent(type, subject, prevSubject, Collections.singleton(subject));
370 }
371
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700372 /**
373 * Test host service that stores a reference to the host listener.
374 */
375 private class TestHostService extends HostServiceAdapter {
376 @Override
377 public void addListener(HostListener listener) {
378 hostListener = listener;
379 }
380 }
381
382 /**
383 * Test route manager that extends the real route manager and injects a test
384 * listener queue instead of the real listener queue.
385 */
386 private static class TestRouteManager extends RouteManager {
387 @Override
388 ListenerQueue createListenerQueue(RouteListener listener) {
389 return new TestListenerQueue(listener);
390 }
391 }
392
393 /**
394 * Test listener queue that simply passes route events straight through to
395 * the route listener on the caller's thread.
396 */
397 private static class TestListenerQueue implements ListenerQueue {
398
399 private final RouteListener listener;
400
401 public TestListenerQueue(RouteListener listener) {
402 this.listener = listener;
403 }
404
405 @Override
406 public void post(RouteEvent event) {
407 listener.event(event);
408 }
409
410 @Override
411 public void start() {
412 }
413
414 @Override
415 public void stop() {
416 }
417 }
418
419}