blob: e740f3fecd378589e8aa3e5f93756fea9db501cd [file] [log] [blame]
Jonathan Hart6c2e7962016-04-11 13:54:09 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
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;
30import org.onosproject.incubator.net.routing.ResolvedRoute;
31import org.onosproject.incubator.net.routing.Route;
32import org.onosproject.incubator.net.routing.RouteEvent;
33import org.onosproject.incubator.net.routing.RouteListener;
34import org.onosproject.incubator.store.routing.impl.LocalRouteStore;
35import org.onosproject.net.ConnectPoint;
36import org.onosproject.net.DefaultHost;
37import org.onosproject.net.DeviceId;
38import org.onosproject.net.Host;
39import org.onosproject.net.HostId;
40import org.onosproject.net.HostLocation;
41import org.onosproject.net.PortNumber;
Charles Chancab494d2016-11-11 16:31:49 -080042import org.onosproject.net.config.NetworkConfigRegistry;
Jonathan Hart6c2e7962016-04-11 13:54:09 -070043import 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;
48
49import java.util.Collections;
50
51import static org.easymock.EasyMock.anyObject;
52import static org.easymock.EasyMock.createMock;
53import static org.easymock.EasyMock.expect;
54import static org.easymock.EasyMock.expectLastCall;
55import static org.easymock.EasyMock.replay;
56import static org.easymock.EasyMock.reset;
57import static org.easymock.EasyMock.verify;
58
59/**
60 * Unit tests for the route manager.
61 */
62public class RouteManagerTest {
63
64 private static final ConnectPoint CP1 = new ConnectPoint(
65 DeviceId.deviceId("of:0000000000000001"),
66 PortNumber.portNumber(1));
67
68 private static final IpPrefix V4_PREFIX1 = Ip4Prefix.valueOf("1.1.1.0/24");
Jonathan Hart7f2d5742016-04-13 16:22:50 -070069 private static final IpPrefix V4_PREFIX2 = Ip4Prefix.valueOf("2.2.2.0/24");
Jonathan Hart6c2e7962016-04-11 13:54:09 -070070 private static final IpPrefix V6_PREFIX1 = Ip6Prefix.valueOf("4000::/64");
71
72 private static final IpAddress V4_NEXT_HOP1 = Ip4Address.valueOf("192.168.10.1");
73 private static final IpAddress V4_NEXT_HOP2 = Ip4Address.valueOf("192.168.20.1");
74 private static final IpAddress V6_NEXT_HOP1 = Ip6Address.valueOf("1000::1");
75 private static final IpAddress V6_NEXT_HOP2 = Ip6Address.valueOf("2000::1");
76
77 private static final MacAddress MAC1 = MacAddress.valueOf("00:00:00:00:00:01");
78 private static final MacAddress MAC2 = MacAddress.valueOf("00:00:00:00:00:02");
79 private static final MacAddress MAC3 = MacAddress.valueOf("00:00:00:00:00:03");
80 private static final MacAddress MAC4 = MacAddress.valueOf("00:00:00:00:00:04");
81
82 private HostService hostService;
83
84 private RouteListener routeListener;
85 private HostListener hostListener;
86
87 private RouteManager routeManager;
88
89 @Before
90 public void setUp() throws Exception {
91 setUpHostService();
92
93 routeListener = createMock(RouteListener.class);
94
95 routeManager = new TestRouteManager();
96 routeManager.hostService = hostService;
Charles Chancab494d2016-11-11 16:31:49 -080097 routeManager.netcfgRegistry = createMock(NetworkConfigRegistry.class);
Jonathan Hart6c2e7962016-04-11 13:54:09 -070098
99 LocalRouteStore routeStore = new LocalRouteStore();
100 routeStore.activate();
101 routeManager.routeStore = routeStore;
102 routeManager.activate();
103
104 routeManager.addListener(routeListener);
105 }
106
107 /**
108 * Sets up the host service with details of some hosts.
109 */
110 private void setUpHostService() {
111 hostService = createMock(HostService.class);
112
113 hostService.addListener(anyObject(HostListener.class));
114 expectLastCall().andDelegateTo(new TestHostService()).anyTimes();
115
116 Host host1 = createHost(MAC1, V4_NEXT_HOP1);
117 expectHost(host1);
118
119 Host host2 = createHost(MAC2, V4_NEXT_HOP2);
120 expectHost(host2);
121
122 Host host3 = createHost(MAC3, V6_NEXT_HOP1);
123 expectHost(host3);
124
125 Host host4 = createHost(MAC4, V6_NEXT_HOP2);
126 expectHost(host4);
127
128 replay(hostService);
129 }
130
131 /**
132 * Sets expectations on the host service for a given host.
133 *
134 * @param host host
135 */
136 private void expectHost(Host host) {
137 // Assume the host only has one IP address
138 IpAddress ip = host.ipAddresses().iterator().next();
139
140 expect(hostService.getHostsByIp(ip))
141 .andReturn(Sets.newHashSet(host)).anyTimes();
142 hostService.startMonitoringIp(ip);
143 expectLastCall().anyTimes();
144 }
145
146 /**
147 * Creates a host with the given parameters.
148 *
149 * @param macAddress MAC address
150 * @param ipAddress IP address
151 * @return new host
152 */
153 private Host createHost(MacAddress macAddress, IpAddress ipAddress) {
154 return new DefaultHost(ProviderId.NONE, HostId.NONE, macAddress,
155 VlanId.NONE, new HostLocation(CP1, 1),
156 Sets.newHashSet(ipAddress));
157 }
158
159 /**
160 * Adds a route to the route service without expecting any specific events
161 * to be sent to the route listeners.
162 *
163 * @param route route to add
164 */
165 private void addRoute(Route route) {
166 reset(routeListener);
167
168 routeListener.event(anyObject(RouteEvent.class));
169 expectLastCall().once();
170 replay(routeListener);
171
172 routeManager.update(Collections.singleton(route));
173
174 reset(routeListener);
175 }
176
177 /**
178 * Tests adding routes to the route manager.
179 */
180 @Test
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700181 public void testRouteAdd() {
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700182 Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700183 ResolvedRoute resolvedRoute = new ResolvedRoute(route, MAC1, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700184
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700185 verifyRouteAdd(route, resolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700186
187 route = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700188 resolvedRoute = new ResolvedRoute(route, MAC3, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700189
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700190 verifyRouteAdd(route, resolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700191 }
192
193 /**
194 * Tests adding a new route and verifies that the correct event was sent
195 * to the route listener.
196 *
197 * @param route route to add
198 * @param resolvedRoute resolved route that should be sent to the route
199 * listener
200 */
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700201 private void verifyRouteAdd(Route route, ResolvedRoute resolvedRoute) {
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700202 reset(routeListener);
203
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700204 routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_ADDED, resolvedRoute));
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700205
206 replay(routeListener);
207
208 routeManager.update(Collections.singleton(route));
209
210 verify(routeListener);
211 }
212
213 /**
214 * Tests updating routes in the route manager.
215 */
216 @Test
217 public void testRouteUpdate() {
218 Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1);
219 Route updatedRoute = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP2);
Charles Chane4d13102016-11-08 15:38:44 -0800220 ResolvedRoute resolvedRoute = new ResolvedRoute(updatedRoute, MAC1, CP1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700221 ResolvedRoute updatedResolvedRoute = new ResolvedRoute(updatedRoute, MAC2, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700222
Charles Chane4d13102016-11-08 15:38:44 -0800223 verifyRouteRemoveThenAdd(route, updatedRoute, resolvedRoute, updatedResolvedRoute);
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700224
225 // Different prefix pointing to the same next hop.
226 // In this case we expect to receive a ROUTE_UPDATED event.
227 route = new Route(Route.Source.STATIC, V4_PREFIX2, V4_NEXT_HOP1);
228 updatedRoute = new Route(Route.Source.STATIC, V4_PREFIX2, V4_NEXT_HOP2);
Charles Chane4d13102016-11-08 15:38:44 -0800229 resolvedRoute = new ResolvedRoute(route, MAC1, CP1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700230 updatedResolvedRoute = new ResolvedRoute(updatedRoute, MAC2, CP1);
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700231
Charles Chane4d13102016-11-08 15:38:44 -0800232 verifyRouteUpdated(route, updatedRoute, resolvedRoute, updatedResolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700233
234 route = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP1);
235 updatedRoute = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP2);
Charles Chane4d13102016-11-08 15:38:44 -0800236 resolvedRoute = new ResolvedRoute(route, MAC3, CP1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700237 updatedResolvedRoute = new ResolvedRoute(updatedRoute, MAC4, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700238
Charles Chane4d13102016-11-08 15:38:44 -0800239 verifyRouteRemoveThenAdd(route, updatedRoute, resolvedRoute, updatedResolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700240 }
241
242 /**
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700243 * Tests updating a route and verifies that the route listener receives a
244 * route remove event followed by a route add event.
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700245 *
246 * @param original original route
247 * @param updated updated route
Charles Chane4d13102016-11-08 15:38:44 -0800248 * @param resolvedRoute resolved route before update
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700249 * @param updatedResolvedRoute resolved route that is expected to be sent to
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700250 * the route listener
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700251 */
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700252 private void verifyRouteRemoveThenAdd(Route original, Route updated,
Charles Chane4d13102016-11-08 15:38:44 -0800253 ResolvedRoute resolvedRoute,
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700254 ResolvedRoute updatedResolvedRoute) {
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700255 // First add the original route
256 addRoute(original);
257
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700258 routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_REMOVED,
Charles Chane4d13102016-11-08 15:38:44 -0800259 new ResolvedRoute(original, resolvedRoute.nextHopMac(), resolvedRoute.location())));
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700260 expectLastCall().once();
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700261 routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_ADDED, updatedResolvedRoute));
262 expectLastCall().once();
263
264 replay(routeListener);
265
266 routeManager.update(Collections.singleton(updated));
267
268 verify(routeListener);
269 }
270
271 /**
272 * Tests updating a route and verifies that the route listener receives a
273 * route updated event.
274 *
275 * @param original original route
276 * @param updated updated route
Charles Chane4d13102016-11-08 15:38:44 -0800277 * @param resolvedRoute resolved route before update
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700278 * @param updatedResolvedRoute resolved route that is expected to be sent to
279 * the route listener
280 */
281 private void verifyRouteUpdated(Route original, Route updated,
Charles Chane4d13102016-11-08 15:38:44 -0800282 ResolvedRoute resolvedRoute,
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700283 ResolvedRoute updatedResolvedRoute) {
284 // First add the original route
285 addRoute(original);
286
Charles Chane4d13102016-11-08 15:38:44 -0800287 routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_UPDATED,
288 updatedResolvedRoute, 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 Chane4d13102016-11-08 15:38:44 -0800325 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);
350 replay(hostService);
351
352 // Initially when we add the route, no route event will be sent because
353 // the host is not known
354 replay(routeListener);
355
356 routeManager.update(Collections.singleton(route));
357
358 verify(routeListener);
359
360 // Now when we send the event, we expect the FIB update to be sent
361 reset(routeListener);
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700362 routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_ADDED,
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700363 new ResolvedRoute(route, MAC1, CP1)));
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700364 replay(routeListener);
365
366 // Send in the host event
367 Host host = createHost(MAC1, V4_NEXT_HOP1);
368 hostListener.event(new HostEvent(HostEvent.Type.HOST_ADDED, host));
369
370 verify(routeListener);
371 }
372
373 /**
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}