blob: 0dc5dbec6afab8b1866029e45a0e3aeb8c6cd923 [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
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
25import org.onlab.packet.IpAddress;
Jonathan Hartf7021682017-03-22 18:17:21 -070026import org.onlab.packet.IpPrefix;
Jonathan Hartd4be52f2017-05-25 14:21:44 -070027import org.onosproject.cluster.ClusterService;
Ray Milkey69ec8712017-08-08 13:00:43 -070028import org.onosproject.routeservice.InternalRouteEvent;
29import org.onosproject.routeservice.ResolvedRoute;
30import org.onosproject.routeservice.Route;
31import org.onosproject.routeservice.RouteAdminService;
32import org.onosproject.routeservice.RouteEvent;
33import org.onosproject.routeservice.RouteInfo;
34import org.onosproject.routeservice.RouteListener;
35import org.onosproject.routeservice.RouteService;
36import org.onosproject.routeservice.RouteSet;
37import org.onosproject.routeservice.RouteStore;
38import org.onosproject.routeservice.RouteStoreDelegate;
39import org.onosproject.routeservice.RouteTableId;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070040import org.onosproject.net.Host;
41import org.onosproject.net.host.HostEvent;
42import org.onosproject.net.host.HostListener;
43import org.onosproject.net.host.HostService;
Jonathan Hartd4be52f2017-05-25 14:21:44 -070044import org.onosproject.store.service.StorageService;
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;
57import java.util.concurrent.ExecutorService;
58import java.util.concurrent.LinkedBlockingQueue;
59import java.util.concurrent.ThreadFactory;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070060import java.util.stream.Collectors;
61
62import static java.util.concurrent.Executors.newSingleThreadExecutor;
63import static org.onlab.util.Tools.groupedThreads;
64
65/**
66 * Implementation of the unicast route service.
67 */
68@Service
69@Component
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
77 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
78 protected RouteStore routeStore;
79
80 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
81 protected HostService hostService;
82
Jonathan Hartd4be52f2017-05-25 14:21:44 -070083 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
84 protected ClusterService clusterService;
85
86 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
87 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
98 @Activate
99 protected void activate() {
Jonathan Hartd4be52f2017-05-25 14:21:44 -0700100 routeMonitor = new RouteMonitor(this, clusterService, storageService);
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700101 threadFactory = groupedThreads("onos/route", "listener-%d", log);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700102
Jonathan Hart96c146b2017-02-24 16:32:00 -0800103 resolvedRouteStore = new DefaultResolvedRouteStore();
104
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700105 routeStore.setDelegate(delegate);
106 hostService.addListener(hostListener);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800107
108 routeStore.getRouteTables().stream()
109 .flatMap(id -> routeStore.getRoutes(id).stream())
110 .forEach(this::resolve);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700111 }
112
113 @Deactivate
114 protected void deactivate() {
Jonathan Hartd4be52f2017-05-25 14:21:44 -0700115 routeMonitor.shutdown();
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -0800116 synchronized (this) {
117 listeners.values().forEach(ListenerQueue::stop);
118 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700119
120 routeStore.unsetDelegate(delegate);
121 hostService.removeListener(hostListener);
122 }
123
124 /**
125 * {@inheritDoc}
126 *
127 * In a departure from other services in ONOS, calling addListener will
128 * cause all current routes to be pushed to the listener before any new
129 * events are sent. This allows a listener to easily get the exact set of
130 * routes without worrying about missing any.
131 *
132 * @param listener listener to be added
133 */
134 @Override
135 public void addListener(RouteListener listener) {
136 synchronized (this) {
137 log.debug("Synchronizing current routes to new listener");
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700138 ListenerQueue l = createListenerQueue(listener);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800139 resolvedRouteStore.getRouteTables().stream()
140 .map(resolvedRouteStore::getRoutes)
141 .flatMap(Collection::stream)
Jonathan Hartf7021682017-03-22 18:17:21 -0700142 .map(route -> new RouteEvent(RouteEvent.Type.ROUTE_ADDED, route,
Ray Milkey69ec8712017-08-08 13:00:43 -0700143 resolvedRouteStore.getAllRoutes(route.prefix())))
Jonathan Hart96c146b2017-02-24 16:32:00 -0800144 .forEach(l::post);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700145
146 listeners.put(listener, l);
147
148 l.start();
149 log.debug("Route synchronization complete");
150 }
151 }
152
153 @Override
154 public void removeListener(RouteListener listener) {
155 synchronized (this) {
156 ListenerQueue l = listeners.remove(listener);
157 if (l != null) {
158 l.stop();
159 }
160 }
161 }
162
163 /**
164 * Posts an event to all listeners.
165 *
166 * @param event event
167 */
168 private void post(RouteEvent event) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800169 if (event != null) {
170 log.debug("Sending event {}", event);
171 synchronized (this) {
172 listeners.values().forEach(l -> l.post(event));
173 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700174 }
175 }
176
Jonathan Hart96c146b2017-02-24 16:32:00 -0800177 private Collection<Route> reformatRoutes(Collection<RouteSet> routeSets) {
178 return routeSets.stream().flatMap(r -> r.routes().stream()).collect(Collectors.toList());
179 }
180
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -0800181 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800182 public Collection<RouteTableId> getRouteTables() {
183 return routeStore.getRouteTables();
184 }
185
186 @Override
187 public Collection<RouteInfo> getRoutes(RouteTableId id) {
188 return routeStore.getRoutes(id).stream()
189 .map(routeSet -> new RouteInfo(routeSet.prefix(),
190 resolvedRouteStore.getRoute(routeSet.prefix()).orElse(null), resolveRouteSet(routeSet)))
191 .collect(Collectors.toList());
192 }
193
194 private Set<ResolvedRoute> resolveRouteSet(RouteSet routeSet) {
195 return routeSet.routes().stream()
196 .map(this::tryResolve)
197 .collect(Collectors.toSet());
198 }
199
200 private ResolvedRoute tryResolve(Route route) {
201 ResolvedRoute resolvedRoute = resolve(route);
202 if (resolvedRoute == null) {
Charles Chan9640c812017-08-23 13:55:39 -0700203 resolvedRoute = new ResolvedRoute(route, null, null, null);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800204 }
205 return resolvedRoute;
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700206 }
207
208 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800209 public Optional<ResolvedRoute> longestPrefixLookup(IpAddress ip) {
210 return resolvedRouteStore.longestPrefixMatch(ip);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700211 }
212
213 @Override
214 public void update(Collection<Route> routes) {
215 synchronized (this) {
216 routes.forEach(route -> {
Jonathan Hartfd176612016-04-11 10:42:10 -0700217 log.debug("Received update {}", route);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700218 routeStore.updateRoute(route);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700219 });
220 }
221 }
222
223 @Override
224 public void withdraw(Collection<Route> routes) {
225 synchronized (this) {
Jonathan Hartfd176612016-04-11 10:42:10 -0700226 routes.forEach(route -> {
Charles Chanb21d69a2016-11-11 17:46:14 -0800227 log.debug("Received withdraw {}", route);
Jonathan Hartfd176612016-04-11 10:42:10 -0700228 routeStore.removeRoute(route);
229 });
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700230 }
231 }
232
Jonathan Hart89ef1582017-06-19 11:12:23 -0700233 @Override
234 public Route longestPrefixMatch(IpAddress ip) {
235 return longestPrefixLookup(ip)
236 .map(ResolvedRoute::route)
237 .orElse(null);
238 }
239
Jonathan Hart96c146b2017-02-24 16:32:00 -0800240 private ResolvedRoute resolve(Route route) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700241 hostService.startMonitoringIp(route.nextHop());
Jonathan Hart96c146b2017-02-24 16:32:00 -0800242 Set<Host> hosts = hostService.getHostsByIp(route.nextHop());
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700243
Charles Chan9640c812017-08-23 13:55:39 -0700244 return hosts.stream().findFirst()
245 .map(host -> new ResolvedRoute(route, host.mac(), host.vlan(), host.location()))
246 .orElse(null);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800247 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700248
Jonathan Hart96c146b2017-02-24 16:32:00 -0800249 private ResolvedRoute decide(ResolvedRoute route1, ResolvedRoute route2) {
Jonathan Hartf7021682017-03-22 18:17:21 -0700250 return Comparator.comparing(ResolvedRoute::nextHop)
Jonathan Hart96c146b2017-02-24 16:32:00 -0800251 .compare(route1, route2) <= 0 ? route1 : route2;
252 }
253
Jonathan Hartf7021682017-03-22 18:17:21 -0700254 private void store(ResolvedRoute route, Set<ResolvedRoute> alternatives) {
255 post(resolvedRouteStore.updateRoute(route, alternatives));
256 }
257
258 private void remove(IpPrefix prefix) {
259 post(resolvedRouteStore.removeRoute(prefix));
Jonathan Hart96c146b2017-02-24 16:32:00 -0800260 }
261
262 private void resolve(RouteSet routes) {
Jonathan Hartf7021682017-03-22 18:17:21 -0700263 Set<ResolvedRoute> resolvedRoutes = routes.routes().stream()
264 .map(this::resolve)
265 .filter(Objects::nonNull)
266 .collect(Collectors.toSet());
267
268 Optional<ResolvedRoute> bestRoute = resolvedRoutes.stream()
Jonathan Hart96c146b2017-02-24 16:32:00 -0800269 .reduce(this::decide);
270
Jonathan Hartf7021682017-03-22 18:17:21 -0700271 if (bestRoute.isPresent()) {
272 store(bestRoute.get(), resolvedRoutes);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800273 } else {
Jonathan Hartf7021682017-03-22 18:17:21 -0700274 remove(routes.prefix());
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700275 }
276 }
277
278 private void hostUpdated(Host host) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800279 hostChanged(host);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700280 }
281
282 private void hostRemoved(Host host) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800283 hostChanged(host);
284 }
285
286 private void hostChanged(Host host) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700287 synchronized (this) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800288 host.ipAddresses().stream()
289 .flatMap(ip -> routeStore.getRoutesForNextHop(ip).stream())
290 .map(route -> routeStore.getRoutes(route.prefix()))
291 .forEach(this::resolve);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700292 }
293 }
294
295 /**
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700296 * Creates a new listener queue.
297 *
298 * @param listener route listener
299 * @return listener queue
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700300 */
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700301 ListenerQueue createListenerQueue(RouteListener listener) {
302 return new DefaultListenerQueue(listener);
303 }
304
305 /**
306 * Default route listener queue.
307 */
308 private class DefaultListenerQueue implements ListenerQueue {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700309
310 private final ExecutorService executorService;
311 private final BlockingQueue<RouteEvent> queue;
312 private final RouteListener listener;
313
314 /**
315 * Creates a new listener queue.
316 *
317 * @param listener route listener to queue updates for
318 */
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700319 public DefaultListenerQueue(RouteListener listener) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700320 this.listener = listener;
321 queue = new LinkedBlockingQueue<>();
322 executorService = newSingleThreadExecutor(threadFactory);
323 }
324
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700325 @Override
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700326 public void post(RouteEvent event) {
327 queue.add(event);
328 }
329
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700330 @Override
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700331 public void start() {
332 executorService.execute(this::poll);
333 }
334
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700335 @Override
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700336 public void stop() {
337 executorService.shutdown();
338 }
339
340 private void poll() {
Jonathan Hartf79ab482016-08-19 14:20:50 -0700341 while (true) {
342 try {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700343 listener.event(queue.take());
Jonathan Hartf79ab482016-08-19 14:20:50 -0700344 } catch (InterruptedException e) {
345 log.info("Route listener event thread shutting down: {}", e.getMessage());
346 break;
347 } catch (Exception e) {
348 log.warn("Exception during route event handler", e);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700349 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700350 }
351 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700352 }
353
354 /**
355 * Delegate to receive events from the route store.
356 */
357 private class InternalRouteStoreDelegate implements RouteStoreDelegate {
358 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800359 public void notify(InternalRouteEvent event) {
360 switch (event.type()) {
361 case ROUTE_ADDED:
362 resolve(event.subject());
363 break;
364 case ROUTE_REMOVED:
365 resolve(event.subject());
366 break;
367 default:
368 break;
369 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700370 }
371 }
372
373 /**
374 * Internal listener for host events.
375 */
376 private class InternalHostListener implements HostListener {
377 @Override
378 public void event(HostEvent event) {
379 switch (event.type()) {
380 case HOST_ADDED:
381 case HOST_UPDATED:
382 hostUpdated(event.subject());
383 break;
384 case HOST_REMOVED:
385 hostRemoved(event.subject());
386 break;
387 case HOST_MOVED:
388 break;
389 default:
390 break;
391 }
392 }
393 }
394
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700395}