blob: 6448d352c681bec1bfb34f4d4f4c87227cbf41ca [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) {
Charles Chan9640c812017-08-23 13:55:39 -0700208 resolvedRoute = new ResolvedRoute(route, null, null, null);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800209 }
210 return resolvedRoute;
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700211 }
212
213 @Override
Charles Chane8c959c2019-05-03 13:19:49 -0700214 public Collection<ResolvedRoute> getResolvedRoutes(RouteTableId id) {
215 return resolvedRouteStore.getRoutes(id);
216 }
217
218 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800219 public Optional<ResolvedRoute> longestPrefixLookup(IpAddress ip) {
220 return resolvedRouteStore.longestPrefixMatch(ip);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700221 }
222
223 @Override
Andrea Campanellacc2424a2018-03-07 14:27:54 -0800224 public Collection<ResolvedRoute> getAllResolvedRoutes(IpPrefix prefix) {
225 return ImmutableList.copyOf(resolvedRouteStore.getAllRoutes(prefix));
226 }
227
228 @Override
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700229 public void update(Collection<Route> routes) {
230 synchronized (this) {
231 routes.forEach(route -> {
Jonathan Hartfd176612016-04-11 10:42:10 -0700232 log.debug("Received update {}", route);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700233 routeStore.updateRoute(route);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700234 });
235 }
236 }
237
238 @Override
239 public void withdraw(Collection<Route> routes) {
240 synchronized (this) {
Jonathan Hartfd176612016-04-11 10:42:10 -0700241 routes.forEach(route -> {
Charles Chanb21d69a2016-11-11 17:46:14 -0800242 log.debug("Received withdraw {}", route);
Jonathan Hartfd176612016-04-11 10:42:10 -0700243 routeStore.removeRoute(route);
244 });
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700245 }
246 }
247
Jonathan Hart89ef1582017-06-19 11:12:23 -0700248 @Override
249 public Route longestPrefixMatch(IpAddress ip) {
250 return longestPrefixLookup(ip)
251 .map(ResolvedRoute::route)
252 .orElse(null);
253 }
254
Jonathan Hart96c146b2017-02-24 16:32:00 -0800255 private ResolvedRoute resolve(Route route) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700256 hostService.startMonitoringIp(route.nextHop());
Jonathan Hart96c146b2017-02-24 16:32:00 -0800257 Set<Host> hosts = hostService.getHostsByIp(route.nextHop());
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700258
Charles Chan9640c812017-08-23 13:55:39 -0700259 return hosts.stream().findFirst()
260 .map(host -> new ResolvedRoute(route, host.mac(), host.vlan(), host.location()))
261 .orElse(null);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800262 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700263
Jonathan Hart96c146b2017-02-24 16:32:00 -0800264 private ResolvedRoute decide(ResolvedRoute route1, ResolvedRoute route2) {
Jonathan Hartf7021682017-03-22 18:17:21 -0700265 return Comparator.comparing(ResolvedRoute::nextHop)
Jonathan Hart96c146b2017-02-24 16:32:00 -0800266 .compare(route1, route2) <= 0 ? route1 : route2;
267 }
268
Jonathan Hartf7021682017-03-22 18:17:21 -0700269 private void store(ResolvedRoute route, Set<ResolvedRoute> alternatives) {
270 post(resolvedRouteStore.updateRoute(route, alternatives));
271 }
272
273 private void remove(IpPrefix prefix) {
274 post(resolvedRouteStore.removeRoute(prefix));
Jonathan Hart96c146b2017-02-24 16:32:00 -0800275 }
276
277 private void resolve(RouteSet routes) {
Ray Milkey0ae030c2018-05-08 16:23:06 -0700278 if (routes.routes() == null) {
279 // The routes were removed before we got to them, nothing to do
280 return;
281 }
Jonathan Hartf7021682017-03-22 18:17:21 -0700282 Set<ResolvedRoute> resolvedRoutes = routes.routes().stream()
283 .map(this::resolve)
284 .filter(Objects::nonNull)
285 .collect(Collectors.toSet());
286
287 Optional<ResolvedRoute> bestRoute = resolvedRoutes.stream()
Jonathan Hart96c146b2017-02-24 16:32:00 -0800288 .reduce(this::decide);
289
Jonathan Hartf7021682017-03-22 18:17:21 -0700290 if (bestRoute.isPresent()) {
291 store(bestRoute.get(), resolvedRoutes);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800292 } else {
Jonathan Hartf7021682017-03-22 18:17:21 -0700293 remove(routes.prefix());
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700294 }
295 }
296
297 private void hostUpdated(Host host) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800298 hostChanged(host);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700299 }
300
301 private void hostRemoved(Host host) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800302 hostChanged(host);
303 }
304
305 private void hostChanged(Host host) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700306 synchronized (this) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800307 host.ipAddresses().stream()
308 .flatMap(ip -> routeStore.getRoutesForNextHop(ip).stream())
309 .map(route -> routeStore.getRoutes(route.prefix()))
310 .forEach(this::resolve);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700311 }
312 }
313
314 /**
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700315 * Creates a new listener queue.
316 *
317 * @param listener route listener
318 * @return listener queue
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700319 */
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700320 ListenerQueue createListenerQueue(RouteListener listener) {
321 return new DefaultListenerQueue(listener);
322 }
323
324 /**
325 * Default route listener queue.
326 */
327 private class DefaultListenerQueue implements ListenerQueue {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700328
329 private final ExecutorService executorService;
330 private final BlockingQueue<RouteEvent> queue;
331 private final RouteListener listener;
332
333 /**
334 * Creates a new listener queue.
335 *
336 * @param listener route listener to queue updates for
337 */
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700338 public DefaultListenerQueue(RouteListener listener) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700339 this.listener = listener;
340 queue = new LinkedBlockingQueue<>();
341 executorService = newSingleThreadExecutor(threadFactory);
342 }
343
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700344 @Override
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700345 public void post(RouteEvent event) {
346 queue.add(event);
347 }
348
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700349 @Override
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700350 public void start() {
351 executorService.execute(this::poll);
352 }
353
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700354 @Override
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700355 public void stop() {
356 executorService.shutdown();
357 }
358
359 private void poll() {
Jonathan Hartf79ab482016-08-19 14:20:50 -0700360 while (true) {
361 try {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700362 listener.event(queue.take());
Jonathan Hartf79ab482016-08-19 14:20:50 -0700363 } catch (InterruptedException e) {
364 log.info("Route listener event thread shutting down: {}", e.getMessage());
Ray Milkey5c7d4882018-02-05 14:50:39 -0800365 Thread.currentThread().interrupt();
Jonathan Hartf79ab482016-08-19 14:20:50 -0700366 break;
367 } catch (Exception e) {
368 log.warn("Exception during route event handler", e);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700369 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700370 }
371 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700372 }
373
374 /**
375 * Delegate to receive events from the route store.
376 */
377 private class InternalRouteStoreDelegate implements RouteStoreDelegate {
378 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800379 public void notify(InternalRouteEvent event) {
380 switch (event.type()) {
381 case ROUTE_ADDED:
382 resolve(event.subject());
383 break;
384 case ROUTE_REMOVED:
385 resolve(event.subject());
386 break;
387 default:
388 break;
389 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700390 }
391 }
392
393 /**
394 * Internal listener for host events.
395 */
396 private class InternalHostListener implements HostListener {
397 @Override
398 public void event(HostEvent event) {
399 switch (event.type()) {
400 case HOST_ADDED:
401 case HOST_UPDATED:
Charles Chane2dcadc2018-07-12 12:53:33 -0700402 case HOST_MOVED:
Jordan Halterman6328db72018-04-10 13:34:50 -0400403 log.trace("Scheduled host event {}", event);
404 hostEventExecutor.execute(() -> hostUpdated(event.subject()));
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700405 break;
406 case HOST_REMOVED:
Jordan Halterman6328db72018-04-10 13:34:50 -0400407 log.trace("Scheduled host event {}", event);
408 hostEventExecutor.execute(() -> hostRemoved(event.subject()));
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700409 break;
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700410 default:
411 break;
412 }
413 }
414 }
415
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700416}