blob: ba73cdefae8ec6e634ede0ae0f3d47ed254905a9 [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;
42import org.onosproject.net.host.HostEvent;
43import org.onosproject.net.host.HostListener;
44import org.onosproject.net.host.HostService;
45import org.onosproject.net.host.HostServiceAdapter;
46import org.onosproject.net.provider.ProviderId;
47
48import java.util.Collections;
49
50import static org.easymock.EasyMock.anyObject;
51import static org.easymock.EasyMock.createMock;
52import static org.easymock.EasyMock.expect;
53import static org.easymock.EasyMock.expectLastCall;
54import static org.easymock.EasyMock.replay;
55import static org.easymock.EasyMock.reset;
56import static org.easymock.EasyMock.verify;
57
58/**
59 * Unit tests for the route manager.
60 */
61public class RouteManagerTest {
62
63 private static final ConnectPoint CP1 = new ConnectPoint(
64 DeviceId.deviceId("of:0000000000000001"),
65 PortNumber.portNumber(1));
66
67 private static final IpPrefix V4_PREFIX1 = Ip4Prefix.valueOf("1.1.1.0/24");
Jonathan Hart7f2d5742016-04-13 16:22:50 -070068 private static final IpPrefix V4_PREFIX2 = Ip4Prefix.valueOf("2.2.2.0/24");
Jonathan Hart6c2e7962016-04-11 13:54:09 -070069 private static final IpPrefix V6_PREFIX1 = Ip6Prefix.valueOf("4000::/64");
70
71 private static final IpAddress V4_NEXT_HOP1 = Ip4Address.valueOf("192.168.10.1");
72 private static final IpAddress V4_NEXT_HOP2 = Ip4Address.valueOf("192.168.20.1");
73 private static final IpAddress V6_NEXT_HOP1 = Ip6Address.valueOf("1000::1");
74 private static final IpAddress V6_NEXT_HOP2 = Ip6Address.valueOf("2000::1");
75
76 private static final MacAddress MAC1 = MacAddress.valueOf("00:00:00:00:00:01");
77 private static final MacAddress MAC2 = MacAddress.valueOf("00:00:00:00:00:02");
78 private static final MacAddress MAC3 = MacAddress.valueOf("00:00:00:00:00:03");
79 private static final MacAddress MAC4 = MacAddress.valueOf("00:00:00:00:00:04");
80
81 private HostService hostService;
82
83 private RouteListener routeListener;
84 private HostListener hostListener;
85
86 private RouteManager routeManager;
87
88 @Before
89 public void setUp() throws Exception {
90 setUpHostService();
91
92 routeListener = createMock(RouteListener.class);
93
94 routeManager = new TestRouteManager();
95 routeManager.hostService = hostService;
96
97 LocalRouteStore routeStore = new LocalRouteStore();
98 routeStore.activate();
99 routeManager.routeStore = routeStore;
100 routeManager.activate();
101
102 routeManager.addListener(routeListener);
103 }
104
105 /**
106 * Sets up the host service with details of some hosts.
107 */
108 private void setUpHostService() {
109 hostService = createMock(HostService.class);
110
111 hostService.addListener(anyObject(HostListener.class));
112 expectLastCall().andDelegateTo(new TestHostService()).anyTimes();
113
114 Host host1 = createHost(MAC1, V4_NEXT_HOP1);
115 expectHost(host1);
116
117 Host host2 = createHost(MAC2, V4_NEXT_HOP2);
118 expectHost(host2);
119
120 Host host3 = createHost(MAC3, V6_NEXT_HOP1);
121 expectHost(host3);
122
123 Host host4 = createHost(MAC4, V6_NEXT_HOP2);
124 expectHost(host4);
125
126 replay(hostService);
127 }
128
129 /**
130 * Sets expectations on the host service for a given host.
131 *
132 * @param host host
133 */
134 private void expectHost(Host host) {
135 // Assume the host only has one IP address
136 IpAddress ip = host.ipAddresses().iterator().next();
137
138 expect(hostService.getHostsByIp(ip))
139 .andReturn(Sets.newHashSet(host)).anyTimes();
140 hostService.startMonitoringIp(ip);
141 expectLastCall().anyTimes();
142 }
143
144 /**
145 * Creates a host with the given parameters.
146 *
147 * @param macAddress MAC address
148 * @param ipAddress IP address
149 * @return new host
150 */
151 private Host createHost(MacAddress macAddress, IpAddress ipAddress) {
152 return new DefaultHost(ProviderId.NONE, HostId.NONE, macAddress,
153 VlanId.NONE, new HostLocation(CP1, 1),
154 Sets.newHashSet(ipAddress));
155 }
156
157 /**
158 * Adds a route to the route service without expecting any specific events
159 * to be sent to the route listeners.
160 *
161 * @param route route to add
162 */
163 private void addRoute(Route route) {
164 reset(routeListener);
165
166 routeListener.event(anyObject(RouteEvent.class));
167 expectLastCall().once();
168 replay(routeListener);
169
170 routeManager.update(Collections.singleton(route));
171
172 reset(routeListener);
173 }
174
175 /**
176 * Tests adding routes to the route manager.
177 */
178 @Test
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700179 public void testRouteAdd() {
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700180 Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700181 ResolvedRoute resolvedRoute = new ResolvedRoute(route, MAC1, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700182
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700183 verifyRouteAdd(route, resolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700184
185 route = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700186 resolvedRoute = new ResolvedRoute(route, MAC3, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700187
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700188 verifyRouteAdd(route, resolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700189 }
190
191 /**
192 * Tests adding a new route and verifies that the correct event was sent
193 * to the route listener.
194 *
195 * @param route route to add
196 * @param resolvedRoute resolved route that should be sent to the route
197 * listener
198 */
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700199 private void verifyRouteAdd(Route route, ResolvedRoute resolvedRoute) {
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700200 reset(routeListener);
201
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700202 routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_ADDED, resolvedRoute));
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700203
204 replay(routeListener);
205
206 routeManager.update(Collections.singleton(route));
207
208 verify(routeListener);
209 }
210
211 /**
212 * Tests updating routes in the route manager.
213 */
214 @Test
215 public void testRouteUpdate() {
216 Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1);
217 Route updatedRoute = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP2);
Charles Chane4d13102016-11-08 15:38:44 -0800218 ResolvedRoute resolvedRoute = new ResolvedRoute(updatedRoute, MAC1, CP1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700219 ResolvedRoute updatedResolvedRoute = new ResolvedRoute(updatedRoute, MAC2, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700220
Charles Chane4d13102016-11-08 15:38:44 -0800221 verifyRouteRemoveThenAdd(route, updatedRoute, resolvedRoute, updatedResolvedRoute);
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700222
223 // Different prefix pointing to the same next hop.
224 // In this case we expect to receive a ROUTE_UPDATED event.
225 route = new Route(Route.Source.STATIC, V4_PREFIX2, V4_NEXT_HOP1);
226 updatedRoute = new Route(Route.Source.STATIC, V4_PREFIX2, V4_NEXT_HOP2);
Charles Chane4d13102016-11-08 15:38:44 -0800227 resolvedRoute = new ResolvedRoute(route, MAC1, CP1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700228 updatedResolvedRoute = new ResolvedRoute(updatedRoute, MAC2, CP1);
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700229
Charles Chane4d13102016-11-08 15:38:44 -0800230 verifyRouteUpdated(route, updatedRoute, resolvedRoute, updatedResolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700231
232 route = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP1);
233 updatedRoute = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP2);
Charles Chane4d13102016-11-08 15:38:44 -0800234 resolvedRoute = new ResolvedRoute(route, MAC3, CP1);
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700235 updatedResolvedRoute = new ResolvedRoute(updatedRoute, MAC4, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700236
Charles Chane4d13102016-11-08 15:38:44 -0800237 verifyRouteRemoveThenAdd(route, updatedRoute, resolvedRoute, updatedResolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700238 }
239
240 /**
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700241 * Tests updating a route and verifies that the route listener receives a
242 * route remove event followed by a route add event.
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700243 *
244 * @param original original route
245 * @param updated updated route
Charles Chane4d13102016-11-08 15:38:44 -0800246 * @param resolvedRoute resolved route before update
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700247 * @param updatedResolvedRoute resolved route that is expected to be sent to
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700248 * the route listener
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700249 */
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700250 private void verifyRouteRemoveThenAdd(Route original, Route updated,
Charles Chane4d13102016-11-08 15:38:44 -0800251 ResolvedRoute resolvedRoute,
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700252 ResolvedRoute updatedResolvedRoute) {
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700253 // First add the original route
254 addRoute(original);
255
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700256 routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_REMOVED,
Charles Chane4d13102016-11-08 15:38:44 -0800257 new ResolvedRoute(original, resolvedRoute.nextHopMac(), resolvedRoute.location())));
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700258 expectLastCall().once();
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700259 routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_ADDED, updatedResolvedRoute));
260 expectLastCall().once();
261
262 replay(routeListener);
263
264 routeManager.update(Collections.singleton(updated));
265
266 verify(routeListener);
267 }
268
269 /**
270 * Tests updating a route and verifies that the route listener receives a
271 * route updated event.
272 *
273 * @param original original route
274 * @param updated updated route
Charles Chane4d13102016-11-08 15:38:44 -0800275 * @param resolvedRoute resolved route before update
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700276 * @param updatedResolvedRoute resolved route that is expected to be sent to
277 * the route listener
278 */
279 private void verifyRouteUpdated(Route original, Route updated,
Charles Chane4d13102016-11-08 15:38:44 -0800280 ResolvedRoute resolvedRoute,
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700281 ResolvedRoute updatedResolvedRoute) {
282 // First add the original route
283 addRoute(original);
284
Charles Chane4d13102016-11-08 15:38:44 -0800285 routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_UPDATED,
286 updatedResolvedRoute, resolvedRoute));
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700287 expectLastCall().once();
288
289 replay(routeListener);
290
291 routeManager.update(Collections.singleton(updated));
292
293 verify(routeListener);
294 }
295
296 /**
297 * Tests deleting routes from the route manager.
298 */
299 @Test
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700300 public void testRouteDelete() {
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700301 Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1);
Charles Chane4d13102016-11-08 15:38:44 -0800302 ResolvedRoute removedResolvedRoute = new ResolvedRoute(route, MAC1, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700303
Charles Chane4d13102016-11-08 15:38:44 -0800304 verifyDelete(route, removedResolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700305
306 route = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP1);
Charles Chane4d13102016-11-08 15:38:44 -0800307 removedResolvedRoute = new ResolvedRoute(route, MAC3, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700308
Charles Chane4d13102016-11-08 15:38:44 -0800309 verifyDelete(route, removedResolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700310 }
311
312 /**
313 * Tests deleting a route and verifies that the correct event is sent to
314 * the route listener.
315 *
316 * @param route route to delete
Charles Chane4d13102016-11-08 15:38:44 -0800317 * @param removedResolvedRoute the resolved route being removed
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700318 */
Charles Chane4d13102016-11-08 15:38:44 -0800319 private void verifyDelete(Route route, ResolvedRoute removedResolvedRoute) {
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700320 addRoute(route);
321
322 RouteEvent withdrawRouteEvent = new RouteEvent(RouteEvent.Type.ROUTE_REMOVED,
Charles Chane4d13102016-11-08 15:38:44 -0800323 removedResolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700324
325 reset(routeListener);
326 routeListener.event(withdrawRouteEvent);
327
328 replay(routeListener);
329
330 routeManager.withdraw(Collections.singleton(route));
331
332 verify(routeListener);
333 }
334
335 /**
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700336 * Tests adding a route entry where the HostService does not immediately
337 * know the MAC address of the next hop, but this is learnt later.
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700338 */
339 @Test
340 public void testAsyncRouteAdd() {
341 Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1);
342
343 // Host service will reply with no hosts when asked
344 reset(hostService);
345 expect(hostService.getHostsByIp(anyObject(IpAddress.class))).andReturn(
346 Collections.emptySet()).anyTimes();
347 hostService.startMonitoringIp(V4_NEXT_HOP1);
348 replay(hostService);
349
350 // Initially when we add the route, no route event will be sent because
351 // the host is not known
352 replay(routeListener);
353
354 routeManager.update(Collections.singleton(route));
355
356 verify(routeListener);
357
358 // Now when we send the event, we expect the FIB update to be sent
359 reset(routeListener);
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700360 routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_ADDED,
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700361 new ResolvedRoute(route, MAC1, CP1)));
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700362 replay(routeListener);
363
364 // Send in the host event
365 Host host = createHost(MAC1, V4_NEXT_HOP1);
366 hostListener.event(new HostEvent(HostEvent.Type.HOST_ADDED, host));
367
368 verify(routeListener);
369 }
370
371 /**
372 * Test host service that stores a reference to the host listener.
373 */
374 private class TestHostService extends HostServiceAdapter {
375 @Override
376 public void addListener(HostListener listener) {
377 hostListener = listener;
378 }
379 }
380
381 /**
382 * Test route manager that extends the real route manager and injects a test
383 * listener queue instead of the real listener queue.
384 */
385 private static class TestRouteManager extends RouteManager {
386 @Override
387 ListenerQueue createListenerQueue(RouteListener listener) {
388 return new TestListenerQueue(listener);
389 }
390 }
391
392 /**
393 * Test listener queue that simply passes route events straight through to
394 * the route listener on the caller's thread.
395 */
396 private static class TestListenerQueue implements ListenerQueue {
397
398 private final RouteListener listener;
399
400 public TestListenerQueue(RouteListener listener) {
401 this.listener = listener;
402 }
403
404 @Override
405 public void post(RouteEvent event) {
406 listener.event(event);
407 }
408
409 @Override
410 public void start() {
411 }
412
413 @Override
414 public void stop() {
415 }
416 }
417
418}