blob: e5db364f33396486faeb52ff997a6c6f91a2f5e7 [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();
Jonathan Hart96c146b2017-02-24 16:32:00 -0800116 listeners.values().forEach(ListenerQueue::stop);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700117
118 routeStore.unsetDelegate(delegate);
119 hostService.removeListener(hostListener);
120 }
121
122 /**
123 * {@inheritDoc}
124 *
125 * In a departure from other services in ONOS, calling addListener will
126 * cause all current routes to be pushed to the listener before any new
127 * events are sent. This allows a listener to easily get the exact set of
128 * routes without worrying about missing any.
129 *
130 * @param listener listener to be added
131 */
132 @Override
133 public void addListener(RouteListener listener) {
134 synchronized (this) {
135 log.debug("Synchronizing current routes to new listener");
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700136 ListenerQueue l = createListenerQueue(listener);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800137 resolvedRouteStore.getRouteTables().stream()
138 .map(resolvedRouteStore::getRoutes)
139 .flatMap(Collection::stream)
Jonathan Hartf7021682017-03-22 18:17:21 -0700140 .map(route -> new RouteEvent(RouteEvent.Type.ROUTE_ADDED, route,
Ray Milkey69ec8712017-08-08 13:00:43 -0700141 resolvedRouteStore.getAllRoutes(route.prefix())))
Jonathan Hart96c146b2017-02-24 16:32:00 -0800142 .forEach(l::post);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700143
144 listeners.put(listener, l);
145
146 l.start();
147 log.debug("Route synchronization complete");
148 }
149 }
150
151 @Override
152 public void removeListener(RouteListener listener) {
153 synchronized (this) {
154 ListenerQueue l = listeners.remove(listener);
155 if (l != null) {
156 l.stop();
157 }
158 }
159 }
160
161 /**
162 * Posts an event to all listeners.
163 *
164 * @param event event
165 */
166 private void post(RouteEvent event) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800167 if (event != null) {
168 log.debug("Sending event {}", event);
169 synchronized (this) {
170 listeners.values().forEach(l -> l.post(event));
171 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700172 }
173 }
174
Jonathan Hart96c146b2017-02-24 16:32:00 -0800175 private Collection<Route> reformatRoutes(Collection<RouteSet> routeSets) {
176 return routeSets.stream().flatMap(r -> r.routes().stream()).collect(Collectors.toList());
177 }
178
179 public Collection<RouteTableId> getRouteTables() {
180 return routeStore.getRouteTables();
181 }
182
183 @Override
184 public Collection<RouteInfo> getRoutes(RouteTableId id) {
185 return routeStore.getRoutes(id).stream()
186 .map(routeSet -> new RouteInfo(routeSet.prefix(),
187 resolvedRouteStore.getRoute(routeSet.prefix()).orElse(null), resolveRouteSet(routeSet)))
188 .collect(Collectors.toList());
189 }
190
191 private Set<ResolvedRoute> resolveRouteSet(RouteSet routeSet) {
192 return routeSet.routes().stream()
193 .map(this::tryResolve)
194 .collect(Collectors.toSet());
195 }
196
197 private ResolvedRoute tryResolve(Route route) {
198 ResolvedRoute resolvedRoute = resolve(route);
199 if (resolvedRoute == null) {
200 resolvedRoute = new ResolvedRoute(route, null, null);
201 }
202 return resolvedRoute;
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700203 }
204
205 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800206 public Optional<ResolvedRoute> longestPrefixLookup(IpAddress ip) {
207 return resolvedRouteStore.longestPrefixMatch(ip);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700208 }
209
210 @Override
211 public void update(Collection<Route> routes) {
212 synchronized (this) {
213 routes.forEach(route -> {
Jonathan Hartfd176612016-04-11 10:42:10 -0700214 log.debug("Received update {}", route);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700215 routeStore.updateRoute(route);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700216 });
217 }
218 }
219
220 @Override
221 public void withdraw(Collection<Route> routes) {
222 synchronized (this) {
Jonathan Hartfd176612016-04-11 10:42:10 -0700223 routes.forEach(route -> {
Charles Chanb21d69a2016-11-11 17:46:14 -0800224 log.debug("Received withdraw {}", route);
Jonathan Hartfd176612016-04-11 10:42:10 -0700225 routeStore.removeRoute(route);
226 });
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700227 }
228 }
229
Jonathan Hart89ef1582017-06-19 11:12:23 -0700230 @Override
231 public Route longestPrefixMatch(IpAddress ip) {
232 return longestPrefixLookup(ip)
233 .map(ResolvedRoute::route)
234 .orElse(null);
235 }
236
Jonathan Hart96c146b2017-02-24 16:32:00 -0800237 private ResolvedRoute resolve(Route route) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700238 hostService.startMonitoringIp(route.nextHop());
Jonathan Hart96c146b2017-02-24 16:32:00 -0800239 Set<Host> hosts = hostService.getHostsByIp(route.nextHop());
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700240
Jonathan Hart96c146b2017-02-24 16:32:00 -0800241 Optional<Host> host = hosts.stream().findFirst();
242 if (host.isPresent()) {
Charles Chan92ca94d2017-03-17 18:05:22 -0700243 return new ResolvedRoute(route, host.get().mac(), host.get().vlan(),
244 host.get().location());
Jonathan Hart96c146b2017-02-24 16:32:00 -0800245 } else {
246 return null;
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700247 }
Jonathan Hart96c146b2017-02-24 16:32:00 -0800248 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700249
Jonathan Hart96c146b2017-02-24 16:32:00 -0800250 private ResolvedRoute decide(ResolvedRoute route1, ResolvedRoute route2) {
Jonathan Hartf7021682017-03-22 18:17:21 -0700251 return Comparator.comparing(ResolvedRoute::nextHop)
Jonathan Hart96c146b2017-02-24 16:32:00 -0800252 .compare(route1, route2) <= 0 ? route1 : route2;
253 }
254
Jonathan Hartf7021682017-03-22 18:17:21 -0700255 private void store(ResolvedRoute route, Set<ResolvedRoute> alternatives) {
256 post(resolvedRouteStore.updateRoute(route, alternatives));
257 }
258
259 private void remove(IpPrefix prefix) {
260 post(resolvedRouteStore.removeRoute(prefix));
Jonathan Hart96c146b2017-02-24 16:32:00 -0800261 }
262
263 private void resolve(RouteSet routes) {
Jonathan Hartf7021682017-03-22 18:17:21 -0700264 Set<ResolvedRoute> resolvedRoutes = routes.routes().stream()
265 .map(this::resolve)
266 .filter(Objects::nonNull)
267 .collect(Collectors.toSet());
268
269 Optional<ResolvedRoute> bestRoute = resolvedRoutes.stream()
Jonathan Hart96c146b2017-02-24 16:32:00 -0800270 .reduce(this::decide);
271
Jonathan Hartf7021682017-03-22 18:17:21 -0700272 if (bestRoute.isPresent()) {
273 store(bestRoute.get(), resolvedRoutes);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800274 } else {
Jonathan Hartf7021682017-03-22 18:17:21 -0700275 remove(routes.prefix());
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700276 }
277 }
278
279 private void hostUpdated(Host host) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800280 hostChanged(host);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700281 }
282
283 private void hostRemoved(Host host) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800284 hostChanged(host);
285 }
286
287 private void hostChanged(Host host) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700288 synchronized (this) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800289 host.ipAddresses().stream()
290 .flatMap(ip -> routeStore.getRoutesForNextHop(ip).stream())
291 .map(route -> routeStore.getRoutes(route.prefix()))
292 .forEach(this::resolve);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700293 }
294 }
295
296 /**
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700297 * Creates a new listener queue.
298 *
299 * @param listener route listener
300 * @return listener queue
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700301 */
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700302 ListenerQueue createListenerQueue(RouteListener listener) {
303 return new DefaultListenerQueue(listener);
304 }
305
306 /**
307 * Default route listener queue.
308 */
309 private class DefaultListenerQueue implements ListenerQueue {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700310
311 private final ExecutorService executorService;
312 private final BlockingQueue<RouteEvent> queue;
313 private final RouteListener listener;
314
315 /**
316 * Creates a new listener queue.
317 *
318 * @param listener route listener to queue updates for
319 */
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700320 public DefaultListenerQueue(RouteListener listener) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700321 this.listener = listener;
322 queue = new LinkedBlockingQueue<>();
323 executorService = newSingleThreadExecutor(threadFactory);
324 }
325
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700326 @Override
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700327 public void post(RouteEvent event) {
328 queue.add(event);
329 }
330
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700331 @Override
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700332 public void start() {
333 executorService.execute(this::poll);
334 }
335
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700336 @Override
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700337 public void stop() {
338 executorService.shutdown();
339 }
340
341 private void poll() {
Jonathan Hartf79ab482016-08-19 14:20:50 -0700342 while (true) {
343 try {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700344 listener.event(queue.take());
Jonathan Hartf79ab482016-08-19 14:20:50 -0700345 } catch (InterruptedException e) {
346 log.info("Route listener event thread shutting down: {}", e.getMessage());
347 break;
348 } catch (Exception e) {
349 log.warn("Exception during route event handler", e);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700350 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700351 }
352 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700353 }
354
355 /**
356 * Delegate to receive events from the route store.
357 */
358 private class InternalRouteStoreDelegate implements RouteStoreDelegate {
359 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800360 public void notify(InternalRouteEvent event) {
361 switch (event.type()) {
362 case ROUTE_ADDED:
363 resolve(event.subject());
364 break;
365 case ROUTE_REMOVED:
366 resolve(event.subject());
367 break;
368 default:
369 break;
370 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700371 }
372 }
373
374 /**
375 * Internal listener for host events.
376 */
377 private class InternalHostListener implements HostListener {
378 @Override
379 public void event(HostEvent event) {
380 switch (event.type()) {
381 case HOST_ADDED:
382 case HOST_UPDATED:
383 hostUpdated(event.subject());
384 break;
385 case HOST_REMOVED:
386 hostRemoved(event.subject());
387 break;
388 case HOST_MOVED:
389 break;
390 default:
391 break;
392 }
393 }
394 }
395
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700396}