blob: 72d5453fe6fcc92b1153429046446428ce707d61 [file] [log] [blame]
Jonathan Hartbfc5c482016-04-05 18:57:00 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jonathan Hartbfc5c482016-04-05 18:57:00 -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 Hartbfc5c482016-04-05 18:57:00 -070018
Andrea Campanellacc2424a2018-03-07 14:27:54 -080019import com.google.common.collect.ImmutableList;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070020import org.onlab.packet.IpAddress;
Jonathan Hartf7021682017-03-22 18:17:21 -070021import org.onlab.packet.IpPrefix;
Jonathan Hartd4be52f2017-05-25 14:21:44 -070022import org.onosproject.cluster.ClusterService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070023import org.onosproject.net.Host;
24import org.onosproject.net.host.HostEvent;
25import org.onosproject.net.host.HostListener;
26import org.onosproject.net.host.HostService;
Ray Milkey69ec8712017-08-08 13:00:43 -070027import org.onosproject.routeservice.InternalRouteEvent;
28import org.onosproject.routeservice.ResolvedRoute;
29import org.onosproject.routeservice.Route;
30import org.onosproject.routeservice.RouteAdminService;
31import org.onosproject.routeservice.RouteEvent;
32import org.onosproject.routeservice.RouteInfo;
33import org.onosproject.routeservice.RouteListener;
34import org.onosproject.routeservice.RouteService;
35import org.onosproject.routeservice.RouteSet;
36import org.onosproject.routeservice.RouteStore;
37import org.onosproject.routeservice.RouteStoreDelegate;
38import org.onosproject.routeservice.RouteTableId;
Jonathan Hartd4be52f2017-05-25 14:21:44 -070039import org.onosproject.store.service.StorageService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070040import org.osgi.service.component.annotations.Activate;
41import org.osgi.service.component.annotations.Component;
42import org.osgi.service.component.annotations.Deactivate;
43import org.osgi.service.component.annotations.Reference;
44import org.osgi.service.component.annotations.ReferenceCardinality;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070045import org.slf4j.Logger;
46import org.slf4j.LoggerFactory;
47
48import javax.annotation.concurrent.GuardedBy;
49import java.util.Collection;
Jonathan Hart96c146b2017-02-24 16:32:00 -080050import java.util.Comparator;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070051import java.util.HashMap;
52import java.util.Map;
Jonathan Hart96c146b2017-02-24 16:32:00 -080053import java.util.Objects;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070054import java.util.Optional;
55import java.util.Set;
56import java.util.concurrent.BlockingQueue;
Jordan Halterman6328db72018-04-10 13:34:50 -040057import java.util.concurrent.Executor;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070058import java.util.concurrent.ExecutorService;
59import java.util.concurrent.LinkedBlockingQueue;
60import java.util.concurrent.ThreadFactory;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070061import java.util.stream.Collectors;
62
63import static java.util.concurrent.Executors.newSingleThreadExecutor;
64import static org.onlab.util.Tools.groupedThreads;
65
66/**
67 * Implementation of the unicast route service.
68 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070069@Component(service = { RouteService.class, RouteAdminService.class })
Jonathan Hartf7021682017-03-22 18:17:21 -070070public class RouteManager implements RouteService, RouteAdminService {
Jonathan Hartbfc5c482016-04-05 18:57:00 -070071
72 private final Logger log = LoggerFactory.getLogger(getClass());
73
74 private RouteStoreDelegate delegate = new InternalRouteStoreDelegate();
75 private InternalHostListener hostListener = new InternalHostListener();
76
Ray Milkeyd84f89b2018-08-17 14:54:17 -070077 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonathan Hartbfc5c482016-04-05 18:57:00 -070078 protected RouteStore routeStore;
79
Ray Milkeyd84f89b2018-08-17 14:54:17 -070080 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonathan Hartbfc5c482016-04-05 18:57:00 -070081 protected HostService hostService;
82
Ray Milkeyd84f89b2018-08-17 14:54:17 -070083 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonathan Hartd4be52f2017-05-25 14:21:44 -070084 protected ClusterService clusterService;
85
Ray Milkeyd84f89b2018-08-17 14:54:17 -070086 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonathan Hartd4be52f2017-05-25 14:21:44 -070087 protected StorageService storageService;
88
Jonathan Hart96c146b2017-02-24 16:32:00 -080089 private ResolvedRouteStore resolvedRouteStore;
90
Jonathan Hartd4be52f2017-05-25 14:21:44 -070091 private RouteMonitor routeMonitor;
92
Jonathan Hartbfc5c482016-04-05 18:57:00 -070093 @GuardedBy(value = "this")
94 private Map<RouteListener, ListenerQueue> listeners = new HashMap<>();
95
96 private ThreadFactory threadFactory;
97
Jordan Halterman6328db72018-04-10 13:34:50 -040098 protected Executor hostEventExecutor = newSingleThreadExecutor(
99 groupedThreads("rm-event-host", "%d", log));
100
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700101 @Activate
102 protected void activate() {
Jonathan Hartd4be52f2017-05-25 14:21:44 -0700103 routeMonitor = new RouteMonitor(this, clusterService, storageService);
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700104 threadFactory = groupedThreads("onos/route", "listener-%d", log);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700105
Jonathan Hart96c146b2017-02-24 16:32:00 -0800106 resolvedRouteStore = new DefaultResolvedRouteStore();
107
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700108 routeStore.setDelegate(delegate);
109 hostService.addListener(hostListener);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800110
111 routeStore.getRouteTables().stream()
112 .flatMap(id -> routeStore.getRoutes(id).stream())
113 .forEach(this::resolve);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700114 }
115
116 @Deactivate
117 protected void deactivate() {
Jonathan Hartd4be52f2017-05-25 14:21:44 -0700118 routeMonitor.shutdown();
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -0800119 synchronized (this) {
120 listeners.values().forEach(ListenerQueue::stop);
121 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700122
123 routeStore.unsetDelegate(delegate);
124 hostService.removeListener(hostListener);
125 }
126
127 /**
128 * {@inheritDoc}
129 *
130 * In a departure from other services in ONOS, calling addListener will
131 * cause all current routes to be pushed to the listener before any new
132 * events are sent. This allows a listener to easily get the exact set of
133 * routes without worrying about missing any.
134 *
135 * @param listener listener to be added
136 */
137 @Override
138 public void addListener(RouteListener listener) {
139 synchronized (this) {
140 log.debug("Synchronizing current routes to new listener");
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700141 ListenerQueue l = createListenerQueue(listener);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800142 resolvedRouteStore.getRouteTables().stream()
143 .map(resolvedRouteStore::getRoutes)
144 .flatMap(Collection::stream)
Jonathan Hartf7021682017-03-22 18:17:21 -0700145 .map(route -> new RouteEvent(RouteEvent.Type.ROUTE_ADDED, route,
Ray Milkey69ec8712017-08-08 13:00:43 -0700146 resolvedRouteStore.getAllRoutes(route.prefix())))
Jonathan Hart96c146b2017-02-24 16:32:00 -0800147 .forEach(l::post);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700148
149 listeners.put(listener, l);
150
151 l.start();
152 log.debug("Route synchronization complete");
153 }
154 }
155
156 @Override
157 public void removeListener(RouteListener listener) {
158 synchronized (this) {
159 ListenerQueue l = listeners.remove(listener);
160 if (l != null) {
161 l.stop();
162 }
163 }
164 }
165
166 /**
167 * Posts an event to all listeners.
168 *
169 * @param event event
170 */
171 private void post(RouteEvent event) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800172 if (event != null) {
173 log.debug("Sending event {}", event);
174 synchronized (this) {
175 listeners.values().forEach(l -> l.post(event));
176 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700177 }
178 }
179
Jonathan Hart96c146b2017-02-24 16:32:00 -0800180 private Collection<Route> reformatRoutes(Collection<RouteSet> routeSets) {
181 return routeSets.stream().flatMap(r -> r.routes().stream()).collect(Collectors.toList());
182 }
183
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -0800184 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800185 public Collection<RouteTableId> getRouteTables() {
186 return routeStore.getRouteTables();
187 }
188
189 @Override
190 public Collection<RouteInfo> getRoutes(RouteTableId id) {
191 return routeStore.getRoutes(id).stream()
192 .map(routeSet -> new RouteInfo(routeSet.prefix(),
193 resolvedRouteStore.getRoute(routeSet.prefix()).orElse(null), resolveRouteSet(routeSet)))
194 .collect(Collectors.toList());
195 }
196
197 private Set<ResolvedRoute> resolveRouteSet(RouteSet routeSet) {
198 return routeSet.routes().stream()
199 .map(this::tryResolve)
200 .collect(Collectors.toSet());
201 }
202
203 private ResolvedRoute tryResolve(Route route) {
204 ResolvedRoute resolvedRoute = resolve(route);
205 if (resolvedRoute == null) {
Ray Milkey69aad442018-09-14 16:24:26 -0700206 resolvedRoute = new ResolvedRoute(route, null, null);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800207 }
208 return resolvedRoute;
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700209 }
210
211 @Override
Charles Chan11d4dc52019-05-03 13:19:49 -0700212 public Collection<ResolvedRoute> getResolvedRoutes(RouteTableId id) {
213 return resolvedRouteStore.getRoutes(id);
214 }
215
216 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800217 public Optional<ResolvedRoute> longestPrefixLookup(IpAddress ip) {
218 return resolvedRouteStore.longestPrefixMatch(ip);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700219 }
220
221 @Override
Andrea Campanellacc2424a2018-03-07 14:27:54 -0800222 public Collection<ResolvedRoute> getAllResolvedRoutes(IpPrefix prefix) {
223 return ImmutableList.copyOf(resolvedRouteStore.getAllRoutes(prefix));
224 }
225
226 @Override
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700227 public void update(Collection<Route> routes) {
228 synchronized (this) {
229 routes.forEach(route -> {
Jonathan Hartfd176612016-04-11 10:42:10 -0700230 log.debug("Received update {}", route);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700231 routeStore.updateRoute(route);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700232 });
233 }
234 }
235
236 @Override
237 public void withdraw(Collection<Route> routes) {
238 synchronized (this) {
Jonathan Hartfd176612016-04-11 10:42:10 -0700239 routes.forEach(route -> {
Charles Chanb21d69a2016-11-11 17:46:14 -0800240 log.debug("Received withdraw {}", route);
Jonathan Hartfd176612016-04-11 10:42:10 -0700241 routeStore.removeRoute(route);
242 });
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700243 }
244 }
245
Jonathan Hart89ef1582017-06-19 11:12:23 -0700246 @Override
247 public Route longestPrefixMatch(IpAddress ip) {
248 return longestPrefixLookup(ip)
249 .map(ResolvedRoute::route)
250 .orElse(null);
251 }
252
Jonathan Hart96c146b2017-02-24 16:32:00 -0800253 private ResolvedRoute resolve(Route route) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700254 hostService.startMonitoringIp(route.nextHop());
Jonathan Hart96c146b2017-02-24 16:32:00 -0800255 Set<Host> hosts = hostService.getHostsByIp(route.nextHop());
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700256
Charles Chan9640c812017-08-23 13:55:39 -0700257 return hosts.stream().findFirst()
Ray Milkey69aad442018-09-14 16:24:26 -0700258 .map(host -> new ResolvedRoute(route, host.mac(), host.vlan()))
Charles Chan9640c812017-08-23 13:55:39 -0700259 .orElse(null);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800260 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700261
Jonathan Hart96c146b2017-02-24 16:32:00 -0800262 private ResolvedRoute decide(ResolvedRoute route1, ResolvedRoute route2) {
Jonathan Hartf7021682017-03-22 18:17:21 -0700263 return Comparator.comparing(ResolvedRoute::nextHop)
Jonathan Hart96c146b2017-02-24 16:32:00 -0800264 .compare(route1, route2) <= 0 ? route1 : route2;
265 }
266
Jonathan Hartf7021682017-03-22 18:17:21 -0700267 private void store(ResolvedRoute route, Set<ResolvedRoute> alternatives) {
268 post(resolvedRouteStore.updateRoute(route, alternatives));
269 }
270
271 private void remove(IpPrefix prefix) {
272 post(resolvedRouteStore.removeRoute(prefix));
Jonathan Hart96c146b2017-02-24 16:32:00 -0800273 }
274
275 private void resolve(RouteSet routes) {
Ray Milkeyb2137432018-05-08 16:23:06 -0700276 if (routes.routes() == null) {
277 // The routes were removed before we got to them, nothing to do
278 return;
279 }
Jonathan Hartf7021682017-03-22 18:17:21 -0700280 Set<ResolvedRoute> resolvedRoutes = routes.routes().stream()
281 .map(this::resolve)
282 .filter(Objects::nonNull)
283 .collect(Collectors.toSet());
284
285 Optional<ResolvedRoute> bestRoute = resolvedRoutes.stream()
Jonathan Hart96c146b2017-02-24 16:32:00 -0800286 .reduce(this::decide);
287
Jonathan Hartf7021682017-03-22 18:17:21 -0700288 if (bestRoute.isPresent()) {
289 store(bestRoute.get(), resolvedRoutes);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800290 } else {
Jonathan Hartf7021682017-03-22 18:17:21 -0700291 remove(routes.prefix());
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700292 }
293 }
294
295 private void hostUpdated(Host host) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800296 hostChanged(host);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700297 }
298
299 private void hostRemoved(Host host) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800300 hostChanged(host);
301 }
302
303 private void hostChanged(Host host) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700304 synchronized (this) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800305 host.ipAddresses().stream()
306 .flatMap(ip -> routeStore.getRoutesForNextHop(ip).stream())
307 .map(route -> routeStore.getRoutes(route.prefix()))
308 .forEach(this::resolve);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700309 }
310 }
311
312 /**
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700313 * Creates a new listener queue.
314 *
315 * @param listener route listener
316 * @return listener queue
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700317 */
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700318 ListenerQueue createListenerQueue(RouteListener listener) {
319 return new DefaultListenerQueue(listener);
320 }
321
322 /**
323 * Default route listener queue.
324 */
325 private class DefaultListenerQueue implements ListenerQueue {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700326
327 private final ExecutorService executorService;
328 private final BlockingQueue<RouteEvent> queue;
329 private final RouteListener listener;
330
331 /**
332 * Creates a new listener queue.
333 *
334 * @param listener route listener to queue updates for
335 */
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700336 public DefaultListenerQueue(RouteListener listener) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700337 this.listener = listener;
338 queue = new LinkedBlockingQueue<>();
339 executorService = newSingleThreadExecutor(threadFactory);
340 }
341
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700342 @Override
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700343 public void post(RouteEvent event) {
344 queue.add(event);
345 }
346
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700347 @Override
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700348 public void start() {
349 executorService.execute(this::poll);
350 }
351
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700352 @Override
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700353 public void stop() {
354 executorService.shutdown();
355 }
356
357 private void poll() {
Jonathan Hartf79ab482016-08-19 14:20:50 -0700358 while (true) {
359 try {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700360 listener.event(queue.take());
Jonathan Hartf79ab482016-08-19 14:20:50 -0700361 } catch (InterruptedException e) {
362 log.info("Route listener event thread shutting down: {}", e.getMessage());
Ray Milkey5c7d4882018-02-05 14:50:39 -0800363 Thread.currentThread().interrupt();
Jonathan Hartf79ab482016-08-19 14:20:50 -0700364 break;
365 } catch (Exception e) {
366 log.warn("Exception during route event handler", e);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700367 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700368 }
369 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700370 }
371
372 /**
373 * Delegate to receive events from the route store.
374 */
375 private class InternalRouteStoreDelegate implements RouteStoreDelegate {
376 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800377 public void notify(InternalRouteEvent event) {
378 switch (event.type()) {
379 case ROUTE_ADDED:
380 resolve(event.subject());
381 break;
382 case ROUTE_REMOVED:
383 resolve(event.subject());
384 break;
385 default:
386 break;
387 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700388 }
389 }
390
391 /**
392 * Internal listener for host events.
393 */
394 private class InternalHostListener implements HostListener {
395 @Override
396 public void event(HostEvent event) {
397 switch (event.type()) {
398 case HOST_ADDED:
399 case HOST_UPDATED:
Charles Chane4404982018-07-12 12:53:33 -0700400 case HOST_MOVED:
Jordan Halterman6328db72018-04-10 13:34:50 -0400401 log.trace("Scheduled host event {}", event);
402 hostEventExecutor.execute(() -> hostUpdated(event.subject()));
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700403 break;
404 case HOST_REMOVED:
Jordan Halterman6328db72018-04-10 13:34:50 -0400405 log.trace("Scheduled host event {}", event);
406 hostEventExecutor.execute(() -> hostRemoved(event.subject()));
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700407 break;
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700408 default:
409 break;
410 }
411 }
412 }
413
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700414}