blob: d45415b4b66e7a6b03b0565b98da05a113242d79 [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.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.apache.felix.scr.annotations.Service;
26import org.onlab.packet.IpAddress;
Jonathan Hartf7021682017-03-22 18:17:21 -070027import org.onlab.packet.IpPrefix;
Jonathan Hartd4be52f2017-05-25 14:21:44 -070028import org.onosproject.cluster.ClusterService;
Ray Milkey69ec8712017-08-08 13:00:43 -070029import org.onosproject.routeservice.InternalRouteEvent;
30import org.onosproject.routeservice.ResolvedRoute;
31import org.onosproject.routeservice.Route;
32import org.onosproject.routeservice.RouteAdminService;
33import org.onosproject.routeservice.RouteEvent;
34import org.onosproject.routeservice.RouteInfo;
35import org.onosproject.routeservice.RouteListener;
36import org.onosproject.routeservice.RouteService;
37import org.onosproject.routeservice.RouteSet;
38import org.onosproject.routeservice.RouteStore;
39import org.onosproject.routeservice.RouteStoreDelegate;
40import org.onosproject.routeservice.RouteTableId;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070041import org.onosproject.net.Host;
42import org.onosproject.net.host.HostEvent;
43import org.onosproject.net.host.HostListener;
44import org.onosproject.net.host.HostService;
Jonathan Hartd4be52f2017-05-25 14:21:44 -070045import org.onosproject.store.service.StorageService;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070046import org.slf4j.Logger;
47import org.slf4j.LoggerFactory;
48
49import javax.annotation.concurrent.GuardedBy;
50import java.util.Collection;
Jonathan Hart96c146b2017-02-24 16:32:00 -080051import java.util.Comparator;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070052import java.util.HashMap;
53import java.util.Map;
Jonathan Hart96c146b2017-02-24 16:32:00 -080054import java.util.Objects;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070055import java.util.Optional;
56import java.util.Set;
57import java.util.concurrent.BlockingQueue;
Jordan Halterman6328db72018-04-10 13:34:50 -040058import java.util.concurrent.Executor;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070059import java.util.concurrent.ExecutorService;
60import java.util.concurrent.LinkedBlockingQueue;
61import java.util.concurrent.ThreadFactory;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070062import java.util.stream.Collectors;
63
64import static java.util.concurrent.Executors.newSingleThreadExecutor;
65import static org.onlab.util.Tools.groupedThreads;
66
67/**
68 * Implementation of the unicast route service.
69 */
70@Service
71@Component
Jonathan Hartf7021682017-03-22 18:17:21 -070072public class RouteManager implements RouteService, RouteAdminService {
Jonathan Hartbfc5c482016-04-05 18:57:00 -070073
74 private final Logger log = LoggerFactory.getLogger(getClass());
75
76 private RouteStoreDelegate delegate = new InternalRouteStoreDelegate();
77 private InternalHostListener hostListener = new InternalHostListener();
78
79 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
80 protected RouteStore routeStore;
81
82 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
83 protected HostService hostService;
84
Jonathan Hartd4be52f2017-05-25 14:21:44 -070085 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
86 protected ClusterService clusterService;
87
88 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
89 protected StorageService storageService;
90
Jonathan Hart96c146b2017-02-24 16:32:00 -080091 private ResolvedRouteStore resolvedRouteStore;
92
Jonathan Hartd4be52f2017-05-25 14:21:44 -070093 private RouteMonitor routeMonitor;
94
Jonathan Hartbfc5c482016-04-05 18:57:00 -070095 @GuardedBy(value = "this")
96 private Map<RouteListener, ListenerQueue> listeners = new HashMap<>();
97
98 private ThreadFactory threadFactory;
99
Jordan Halterman6328db72018-04-10 13:34:50 -0400100 protected Executor hostEventExecutor = newSingleThreadExecutor(
101 groupedThreads("rm-event-host", "%d", log));
102
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700103 @Activate
104 protected void activate() {
Jonathan Hartd4be52f2017-05-25 14:21:44 -0700105 routeMonitor = new RouteMonitor(this, clusterService, storageService);
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700106 threadFactory = groupedThreads("onos/route", "listener-%d", log);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700107
Jonathan Hart96c146b2017-02-24 16:32:00 -0800108 resolvedRouteStore = new DefaultResolvedRouteStore();
109
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700110 routeStore.setDelegate(delegate);
111 hostService.addListener(hostListener);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800112
113 routeStore.getRouteTables().stream()
114 .flatMap(id -> routeStore.getRoutes(id).stream())
115 .forEach(this::resolve);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700116 }
117
118 @Deactivate
119 protected void deactivate() {
Jonathan Hartd4be52f2017-05-25 14:21:44 -0700120 routeMonitor.shutdown();
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -0800121 synchronized (this) {
122 listeners.values().forEach(ListenerQueue::stop);
123 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700124
125 routeStore.unsetDelegate(delegate);
126 hostService.removeListener(hostListener);
127 }
128
129 /**
130 * {@inheritDoc}
131 *
132 * In a departure from other services in ONOS, calling addListener will
133 * cause all current routes to be pushed to the listener before any new
134 * events are sent. This allows a listener to easily get the exact set of
135 * routes without worrying about missing any.
136 *
137 * @param listener listener to be added
138 */
139 @Override
140 public void addListener(RouteListener listener) {
141 synchronized (this) {
142 log.debug("Synchronizing current routes to new listener");
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700143 ListenerQueue l = createListenerQueue(listener);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800144 resolvedRouteStore.getRouteTables().stream()
145 .map(resolvedRouteStore::getRoutes)
146 .flatMap(Collection::stream)
Jonathan Hartf7021682017-03-22 18:17:21 -0700147 .map(route -> new RouteEvent(RouteEvent.Type.ROUTE_ADDED, route,
Ray Milkey69ec8712017-08-08 13:00:43 -0700148 resolvedRouteStore.getAllRoutes(route.prefix())))
Jonathan Hart96c146b2017-02-24 16:32:00 -0800149 .forEach(l::post);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700150
151 listeners.put(listener, l);
152
153 l.start();
154 log.debug("Route synchronization complete");
155 }
156 }
157
158 @Override
159 public void removeListener(RouteListener listener) {
160 synchronized (this) {
161 ListenerQueue l = listeners.remove(listener);
162 if (l != null) {
163 l.stop();
164 }
165 }
166 }
167
168 /**
169 * Posts an event to all listeners.
170 *
171 * @param event event
172 */
173 private void post(RouteEvent event) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800174 if (event != null) {
175 log.debug("Sending event {}", event);
176 synchronized (this) {
177 listeners.values().forEach(l -> l.post(event));
178 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700179 }
180 }
181
Jonathan Hart96c146b2017-02-24 16:32:00 -0800182 private Collection<Route> reformatRoutes(Collection<RouteSet> routeSets) {
183 return routeSets.stream().flatMap(r -> r.routes().stream()).collect(Collectors.toList());
184 }
185
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -0800186 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800187 public Collection<RouteTableId> getRouteTables() {
188 return routeStore.getRouteTables();
189 }
190
191 @Override
192 public Collection<RouteInfo> getRoutes(RouteTableId id) {
193 return routeStore.getRoutes(id).stream()
194 .map(routeSet -> new RouteInfo(routeSet.prefix(),
195 resolvedRouteStore.getRoute(routeSet.prefix()).orElse(null), resolveRouteSet(routeSet)))
196 .collect(Collectors.toList());
197 }
198
199 private Set<ResolvedRoute> resolveRouteSet(RouteSet routeSet) {
200 return routeSet.routes().stream()
201 .map(this::tryResolve)
202 .collect(Collectors.toSet());
203 }
204
205 private ResolvedRoute tryResolve(Route route) {
206 ResolvedRoute resolvedRoute = resolve(route);
207 if (resolvedRoute == null) {
Ray Milkey69aad442018-09-14 16:24:26 -0700208 resolvedRoute = new ResolvedRoute(route, null, null);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800209 }
210 return resolvedRoute;
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700211 }
212
213 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800214 public Optional<ResolvedRoute> longestPrefixLookup(IpAddress ip) {
215 return resolvedRouteStore.longestPrefixMatch(ip);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700216 }
217
218 @Override
Andrea Campanellacc2424a2018-03-07 14:27:54 -0800219 public Collection<ResolvedRoute> getAllResolvedRoutes(IpPrefix prefix) {
220 return ImmutableList.copyOf(resolvedRouteStore.getAllRoutes(prefix));
221 }
222
223 @Override
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700224 public void update(Collection<Route> routes) {
225 synchronized (this) {
226 routes.forEach(route -> {
Jonathan Hartfd176612016-04-11 10:42:10 -0700227 log.debug("Received update {}", route);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700228 routeStore.updateRoute(route);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700229 });
230 }
231 }
232
233 @Override
234 public void withdraw(Collection<Route> routes) {
235 synchronized (this) {
Jonathan Hartfd176612016-04-11 10:42:10 -0700236 routes.forEach(route -> {
Charles Chanb21d69a2016-11-11 17:46:14 -0800237 log.debug("Received withdraw {}", route);
Jonathan Hartfd176612016-04-11 10:42:10 -0700238 routeStore.removeRoute(route);
239 });
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700240 }
241 }
242
Jonathan Hart89ef1582017-06-19 11:12:23 -0700243 @Override
244 public Route longestPrefixMatch(IpAddress ip) {
245 return longestPrefixLookup(ip)
246 .map(ResolvedRoute::route)
247 .orElse(null);
248 }
249
Jonathan Hart96c146b2017-02-24 16:32:00 -0800250 private ResolvedRoute resolve(Route route) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700251 hostService.startMonitoringIp(route.nextHop());
Jonathan Hart96c146b2017-02-24 16:32:00 -0800252 Set<Host> hosts = hostService.getHostsByIp(route.nextHop());
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700253
Charles Chan9640c812017-08-23 13:55:39 -0700254 return hosts.stream().findFirst()
Ray Milkey69aad442018-09-14 16:24:26 -0700255 .map(host -> new ResolvedRoute(route, host.mac(), host.vlan()))
Charles Chan9640c812017-08-23 13:55:39 -0700256 .orElse(null);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800257 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700258
Jonathan Hart96c146b2017-02-24 16:32:00 -0800259 private ResolvedRoute decide(ResolvedRoute route1, ResolvedRoute route2) {
Jonathan Hartf7021682017-03-22 18:17:21 -0700260 return Comparator.comparing(ResolvedRoute::nextHop)
Jonathan Hart96c146b2017-02-24 16:32:00 -0800261 .compare(route1, route2) <= 0 ? route1 : route2;
262 }
263
Jonathan Hartf7021682017-03-22 18:17:21 -0700264 private void store(ResolvedRoute route, Set<ResolvedRoute> alternatives) {
265 post(resolvedRouteStore.updateRoute(route, alternatives));
266 }
267
268 private void remove(IpPrefix prefix) {
269 post(resolvedRouteStore.removeRoute(prefix));
Jonathan Hart96c146b2017-02-24 16:32:00 -0800270 }
271
272 private void resolve(RouteSet routes) {
Ray Milkeyb2137432018-05-08 16:23:06 -0700273 if (routes.routes() == null) {
274 // The routes were removed before we got to them, nothing to do
275 return;
276 }
Jonathan Hartf7021682017-03-22 18:17:21 -0700277 Set<ResolvedRoute> resolvedRoutes = routes.routes().stream()
278 .map(this::resolve)
279 .filter(Objects::nonNull)
280 .collect(Collectors.toSet());
281
282 Optional<ResolvedRoute> bestRoute = resolvedRoutes.stream()
Jonathan Hart96c146b2017-02-24 16:32:00 -0800283 .reduce(this::decide);
284
Jonathan Hartf7021682017-03-22 18:17:21 -0700285 if (bestRoute.isPresent()) {
286 store(bestRoute.get(), resolvedRoutes);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800287 } else {
Jonathan Hartf7021682017-03-22 18:17:21 -0700288 remove(routes.prefix());
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700289 }
290 }
291
292 private void hostUpdated(Host host) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800293 hostChanged(host);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700294 }
295
296 private void hostRemoved(Host host) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800297 hostChanged(host);
298 }
299
300 private void hostChanged(Host host) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700301 synchronized (this) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800302 host.ipAddresses().stream()
303 .flatMap(ip -> routeStore.getRoutesForNextHop(ip).stream())
304 .map(route -> routeStore.getRoutes(route.prefix()))
305 .forEach(this::resolve);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700306 }
307 }
308
309 /**
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700310 * Creates a new listener queue.
311 *
312 * @param listener route listener
313 * @return listener queue
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700314 */
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700315 ListenerQueue createListenerQueue(RouteListener listener) {
316 return new DefaultListenerQueue(listener);
317 }
318
319 /**
320 * Default route listener queue.
321 */
322 private class DefaultListenerQueue implements ListenerQueue {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700323
324 private final ExecutorService executorService;
325 private final BlockingQueue<RouteEvent> queue;
326 private final RouteListener listener;
327
328 /**
329 * Creates a new listener queue.
330 *
331 * @param listener route listener to queue updates for
332 */
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700333 public DefaultListenerQueue(RouteListener listener) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700334 this.listener = listener;
335 queue = new LinkedBlockingQueue<>();
336 executorService = newSingleThreadExecutor(threadFactory);
337 }
338
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700339 @Override
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700340 public void post(RouteEvent event) {
341 queue.add(event);
342 }
343
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700344 @Override
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700345 public void start() {
346 executorService.execute(this::poll);
347 }
348
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700349 @Override
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700350 public void stop() {
351 executorService.shutdown();
352 }
353
354 private void poll() {
Jonathan Hartf79ab482016-08-19 14:20:50 -0700355 while (true) {
356 try {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700357 listener.event(queue.take());
Jonathan Hartf79ab482016-08-19 14:20:50 -0700358 } catch (InterruptedException e) {
359 log.info("Route listener event thread shutting down: {}", e.getMessage());
Ray Milkey5c7d4882018-02-05 14:50:39 -0800360 Thread.currentThread().interrupt();
Jonathan Hartf79ab482016-08-19 14:20:50 -0700361 break;
362 } catch (Exception e) {
363 log.warn("Exception during route event handler", e);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700364 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700365 }
366 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700367 }
368
369 /**
370 * Delegate to receive events from the route store.
371 */
372 private class InternalRouteStoreDelegate implements RouteStoreDelegate {
373 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800374 public void notify(InternalRouteEvent event) {
375 switch (event.type()) {
376 case ROUTE_ADDED:
377 resolve(event.subject());
378 break;
379 case ROUTE_REMOVED:
380 resolve(event.subject());
381 break;
382 default:
383 break;
384 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700385 }
386 }
387
388 /**
389 * Internal listener for host events.
390 */
391 private class InternalHostListener implements HostListener {
392 @Override
393 public void event(HostEvent event) {
394 switch (event.type()) {
395 case HOST_ADDED:
396 case HOST_UPDATED:
Charles Chane4404982018-07-12 12:53:33 -0700397 case HOST_MOVED:
Jordan Halterman6328db72018-04-10 13:34:50 -0400398 log.trace("Scheduled host event {}", event);
399 hostEventExecutor.execute(() -> hostUpdated(event.subject()));
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700400 break;
401 case HOST_REMOVED:
Jordan Halterman6328db72018-04-10 13:34:50 -0400402 log.trace("Scheduled host event {}", event);
403 hostEventExecutor.execute(() -> hostRemoved(event.subject()));
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700404 break;
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700405 default:
406 break;
407 }
408 }
409 }
410
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700411}