blob: afa9cd6eb9d1b39d56e564ec39f73bd76b1c450d [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
Jonathan Hart96c146b2017-02-24 16:32:00 -0800212 public Optional<ResolvedRoute> longestPrefixLookup(IpAddress ip) {
213 return resolvedRouteStore.longestPrefixMatch(ip);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700214 }
215
216 @Override
Andrea Campanellacc2424a2018-03-07 14:27:54 -0800217 public Collection<ResolvedRoute> getAllResolvedRoutes(IpPrefix prefix) {
218 return ImmutableList.copyOf(resolvedRouteStore.getAllRoutes(prefix));
219 }
220
221 @Override
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700222 public void update(Collection<Route> routes) {
223 synchronized (this) {
224 routes.forEach(route -> {
Jonathan Hartfd176612016-04-11 10:42:10 -0700225 log.debug("Received update {}", route);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700226 routeStore.updateRoute(route);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700227 });
228 }
229 }
230
231 @Override
232 public void withdraw(Collection<Route> routes) {
233 synchronized (this) {
Jonathan Hartfd176612016-04-11 10:42:10 -0700234 routes.forEach(route -> {
Charles Chanb21d69a2016-11-11 17:46:14 -0800235 log.debug("Received withdraw {}", route);
Jonathan Hartfd176612016-04-11 10:42:10 -0700236 routeStore.removeRoute(route);
237 });
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700238 }
239 }
240
Jonathan Hart89ef1582017-06-19 11:12:23 -0700241 @Override
242 public Route longestPrefixMatch(IpAddress ip) {
243 return longestPrefixLookup(ip)
244 .map(ResolvedRoute::route)
245 .orElse(null);
246 }
247
Jonathan Hart96c146b2017-02-24 16:32:00 -0800248 private ResolvedRoute resolve(Route route) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700249 hostService.startMonitoringIp(route.nextHop());
Jonathan Hart96c146b2017-02-24 16:32:00 -0800250 Set<Host> hosts = hostService.getHostsByIp(route.nextHop());
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700251
Charles Chan9640c812017-08-23 13:55:39 -0700252 return hosts.stream().findFirst()
Ray Milkey69aad442018-09-14 16:24:26 -0700253 .map(host -> new ResolvedRoute(route, host.mac(), host.vlan()))
Charles Chan9640c812017-08-23 13:55:39 -0700254 .orElse(null);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800255 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700256
Jonathan Hart96c146b2017-02-24 16:32:00 -0800257 private ResolvedRoute decide(ResolvedRoute route1, ResolvedRoute route2) {
Jonathan Hartf7021682017-03-22 18:17:21 -0700258 return Comparator.comparing(ResolvedRoute::nextHop)
Jonathan Hart96c146b2017-02-24 16:32:00 -0800259 .compare(route1, route2) <= 0 ? route1 : route2;
260 }
261
Jonathan Hartf7021682017-03-22 18:17:21 -0700262 private void store(ResolvedRoute route, Set<ResolvedRoute> alternatives) {
263 post(resolvedRouteStore.updateRoute(route, alternatives));
264 }
265
266 private void remove(IpPrefix prefix) {
267 post(resolvedRouteStore.removeRoute(prefix));
Jonathan Hart96c146b2017-02-24 16:32:00 -0800268 }
269
270 private void resolve(RouteSet routes) {
Ray Milkeyb2137432018-05-08 16:23:06 -0700271 if (routes.routes() == null) {
272 // The routes were removed before we got to them, nothing to do
273 return;
274 }
Jonathan Hartf7021682017-03-22 18:17:21 -0700275 Set<ResolvedRoute> resolvedRoutes = routes.routes().stream()
276 .map(this::resolve)
277 .filter(Objects::nonNull)
278 .collect(Collectors.toSet());
279
280 Optional<ResolvedRoute> bestRoute = resolvedRoutes.stream()
Jonathan Hart96c146b2017-02-24 16:32:00 -0800281 .reduce(this::decide);
282
Jonathan Hartf7021682017-03-22 18:17:21 -0700283 if (bestRoute.isPresent()) {
284 store(bestRoute.get(), resolvedRoutes);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800285 } else {
Jonathan Hartf7021682017-03-22 18:17:21 -0700286 remove(routes.prefix());
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700287 }
288 }
289
290 private void hostUpdated(Host host) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800291 hostChanged(host);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700292 }
293
294 private void hostRemoved(Host host) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800295 hostChanged(host);
296 }
297
298 private void hostChanged(Host host) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700299 synchronized (this) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800300 host.ipAddresses().stream()
301 .flatMap(ip -> routeStore.getRoutesForNextHop(ip).stream())
302 .map(route -> routeStore.getRoutes(route.prefix()))
303 .forEach(this::resolve);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700304 }
305 }
306
307 /**
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700308 * Creates a new listener queue.
309 *
310 * @param listener route listener
311 * @return listener queue
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700312 */
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700313 ListenerQueue createListenerQueue(RouteListener listener) {
314 return new DefaultListenerQueue(listener);
315 }
316
317 /**
318 * Default route listener queue.
319 */
320 private class DefaultListenerQueue implements ListenerQueue {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700321
322 private final ExecutorService executorService;
323 private final BlockingQueue<RouteEvent> queue;
324 private final RouteListener listener;
325
326 /**
327 * Creates a new listener queue.
328 *
329 * @param listener route listener to queue updates for
330 */
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700331 public DefaultListenerQueue(RouteListener listener) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700332 this.listener = listener;
333 queue = new LinkedBlockingQueue<>();
334 executorService = newSingleThreadExecutor(threadFactory);
335 }
336
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700337 @Override
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700338 public void post(RouteEvent event) {
339 queue.add(event);
340 }
341
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700342 @Override
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700343 public void start() {
344 executorService.execute(this::poll);
345 }
346
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700347 @Override
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700348 public void stop() {
349 executorService.shutdown();
350 }
351
352 private void poll() {
Jonathan Hartf79ab482016-08-19 14:20:50 -0700353 while (true) {
354 try {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700355 listener.event(queue.take());
Jonathan Hartf79ab482016-08-19 14:20:50 -0700356 } catch (InterruptedException e) {
357 log.info("Route listener event thread shutting down: {}", e.getMessage());
Ray Milkey5c7d4882018-02-05 14:50:39 -0800358 Thread.currentThread().interrupt();
Jonathan Hartf79ab482016-08-19 14:20:50 -0700359 break;
360 } catch (Exception e) {
361 log.warn("Exception during route event handler", e);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700362 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700363 }
364 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700365 }
366
367 /**
368 * Delegate to receive events from the route store.
369 */
370 private class InternalRouteStoreDelegate implements RouteStoreDelegate {
371 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800372 public void notify(InternalRouteEvent event) {
373 switch (event.type()) {
374 case ROUTE_ADDED:
375 resolve(event.subject());
376 break;
377 case ROUTE_REMOVED:
378 resolve(event.subject());
379 break;
380 default:
381 break;
382 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700383 }
384 }
385
386 /**
387 * Internal listener for host events.
388 */
389 private class InternalHostListener implements HostListener {
390 @Override
391 public void event(HostEvent event) {
392 switch (event.type()) {
393 case HOST_ADDED:
394 case HOST_UPDATED:
Charles Chane4404982018-07-12 12:53:33 -0700395 case HOST_MOVED:
Jordan Halterman6328db72018-04-10 13:34:50 -0400396 log.trace("Scheduled host event {}", event);
397 hostEventExecutor.execute(() -> hostUpdated(event.subject()));
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700398 break;
399 case HOST_REMOVED:
Jordan Halterman6328db72018-04-10 13:34:50 -0400400 log.trace("Scheduled host event {}", event);
401 hostEventExecutor.execute(() -> hostRemoved(event.subject()));
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700402 break;
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700403 default:
404 break;
405 }
406 }
407 }
408
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700409}