blob: aeb2ff1a22e190222eddd015809bf0e3e4ba53db [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);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800218 ResolvedRoute resolvedRoute = new ResolvedRoute(route, 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
Jonathan Hart96c146b2017-02-24 16:32:00 -0800221 verifyRouteUpdated(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
Jonathan Hart96c146b2017-02-24 16:32:00 -0800237 verifyRouteUpdated(route, updatedRoute, resolvedRoute, updatedResolvedRoute);
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700238 }
239
240 /**
241 * Tests updating a route and verifies that the route listener receives a
242 * route updated event.
243 *
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 Hart7f2d5742016-04-13 16:22:50 -0700247 * @param updatedResolvedRoute resolved route that is expected to be sent to
248 * the route listener
249 */
250 private void verifyRouteUpdated(Route original, Route updated,
Charles Chane4d13102016-11-08 15:38:44 -0800251 ResolvedRoute resolvedRoute,
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700252 ResolvedRoute updatedResolvedRoute) {
253 // First add the original route
254 addRoute(original);
255
Charles Chane4d13102016-11-08 15:38:44 -0800256 routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_UPDATED,
257 updatedResolvedRoute, resolvedRoute));
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700258 expectLastCall().once();
259
260 replay(routeListener);
261
262 routeManager.update(Collections.singleton(updated));
263
264 verify(routeListener);
265 }
266
267 /**
268 * Tests deleting routes from the route manager.
269 */
270 @Test
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700271 public void testRouteDelete() {
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700272 Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1);
Charles Chane4d13102016-11-08 15:38:44 -0800273 ResolvedRoute removedResolvedRoute = new ResolvedRoute(route, MAC1, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700274
Charles Chane4d13102016-11-08 15:38:44 -0800275 verifyDelete(route, removedResolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700276
277 route = new Route(Route.Source.STATIC, V6_PREFIX1, V6_NEXT_HOP1);
Charles Chane4d13102016-11-08 15:38:44 -0800278 removedResolvedRoute = new ResolvedRoute(route, MAC3, CP1);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700279
Charles Chane4d13102016-11-08 15:38:44 -0800280 verifyDelete(route, removedResolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700281 }
282
283 /**
284 * Tests deleting a route and verifies that the correct event is sent to
285 * the route listener.
286 *
287 * @param route route to delete
Charles Chane4d13102016-11-08 15:38:44 -0800288 * @param removedResolvedRoute the resolved route being removed
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700289 */
Charles Chane4d13102016-11-08 15:38:44 -0800290 private void verifyDelete(Route route, ResolvedRoute removedResolvedRoute) {
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700291 addRoute(route);
292
293 RouteEvent withdrawRouteEvent = new RouteEvent(RouteEvent.Type.ROUTE_REMOVED,
Charles Chane4d13102016-11-08 15:38:44 -0800294 removedResolvedRoute);
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700295
296 reset(routeListener);
297 routeListener.event(withdrawRouteEvent);
298
299 replay(routeListener);
300
301 routeManager.withdraw(Collections.singleton(route));
302
303 verify(routeListener);
304 }
305
306 /**
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700307 * Tests adding a route entry where the HostService does not immediately
308 * know the MAC address of the next hop, but this is learnt later.
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700309 */
310 @Test
311 public void testAsyncRouteAdd() {
312 Route route = new Route(Route.Source.STATIC, V4_PREFIX1, V4_NEXT_HOP1);
313
314 // Host service will reply with no hosts when asked
315 reset(hostService);
316 expect(hostService.getHostsByIp(anyObject(IpAddress.class))).andReturn(
317 Collections.emptySet()).anyTimes();
318 hostService.startMonitoringIp(V4_NEXT_HOP1);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800319 expectLastCall().anyTimes();
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700320 replay(hostService);
321
322 // Initially when we add the route, no route event will be sent because
323 // the host is not known
324 replay(routeListener);
325
326 routeManager.update(Collections.singleton(route));
327
328 verify(routeListener);
329
330 // Now when we send the event, we expect the FIB update to be sent
331 reset(routeListener);
Jonathan Hart7f2d5742016-04-13 16:22:50 -0700332 routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_ADDED,
Charles Chan8fe9f4c2016-10-24 16:46:25 -0700333 new ResolvedRoute(route, MAC1, CP1)));
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700334 replay(routeListener);
335
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700336 Host host = createHost(MAC1, V4_NEXT_HOP1);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800337
338 // Set up the host service with a host
339 reset(hostService);
340 expect(hostService.getHostsByIp(V4_NEXT_HOP1)).andReturn(
341 Collections.singleton(host)).anyTimes();
342 hostService.startMonitoringIp(V4_NEXT_HOP1);
343 expectLastCall().anyTimes();
344 replay(hostService);
345
346 // Send in the host event
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700347 hostListener.event(new HostEvent(HostEvent.Type.HOST_ADDED, host));
348
349 verify(routeListener);
350 }
351
352 /**
353 * Test host service that stores a reference to the host listener.
354 */
355 private class TestHostService extends HostServiceAdapter {
356 @Override
357 public void addListener(HostListener listener) {
358 hostListener = listener;
359 }
360 }
361
362 /**
363 * Test route manager that extends the real route manager and injects a test
364 * listener queue instead of the real listener queue.
365 */
366 private static class TestRouteManager extends RouteManager {
367 @Override
368 ListenerQueue createListenerQueue(RouteListener listener) {
369 return new TestListenerQueue(listener);
370 }
371 }
372
373 /**
374 * Test listener queue that simply passes route events straight through to
375 * the route listener on the caller's thread.
376 */
377 private static class TestListenerQueue implements ListenerQueue {
378
379 private final RouteListener listener;
380
381 public TestListenerQueue(RouteListener listener) {
382 this.listener = listener;
383 }
384
385 @Override
386 public void post(RouteEvent event) {
387 listener.event(event);
388 }
389
390 @Override
391 public void start() {
392 }
393
394 @Override
395 public void stop() {
396 }
397 }
398
399}