blob: e92cc0ed8afa9318686a6ef912a7ac9ebbc3d6ab [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;
58import 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 */
69@Service
70@Component
Jonathan Hartf7021682017-03-22 18:17:21 -070071public class RouteManager implements RouteService, RouteAdminService {
Jonathan Hartbfc5c482016-04-05 18:57:00 -070072
73 private final Logger log = LoggerFactory.getLogger(getClass());
74
75 private RouteStoreDelegate delegate = new InternalRouteStoreDelegate();
76 private InternalHostListener hostListener = new InternalHostListener();
77
78 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
79 protected RouteStore routeStore;
80
81 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
82 protected HostService hostService;
83
Jonathan Hartd4be52f2017-05-25 14:21:44 -070084 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
85 protected ClusterService clusterService;
86
87 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
88 protected StorageService storageService;
89
Jonathan Hart96c146b2017-02-24 16:32:00 -080090 private ResolvedRouteStore resolvedRouteStore;
91
Jonathan Hartd4be52f2017-05-25 14:21:44 -070092 private RouteMonitor routeMonitor;
93
Jonathan Hartbfc5c482016-04-05 18:57:00 -070094 @GuardedBy(value = "this")
95 private Map<RouteListener, ListenerQueue> listeners = new HashMap<>();
96
97 private ThreadFactory threadFactory;
98
99 @Activate
100 protected void activate() {
Jonathan Hartd4be52f2017-05-25 14:21:44 -0700101 routeMonitor = new RouteMonitor(this, clusterService, storageService);
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700102 threadFactory = groupedThreads("onos/route", "listener-%d", log);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700103
Jonathan Hart96c146b2017-02-24 16:32:00 -0800104 resolvedRouteStore = new DefaultResolvedRouteStore();
105
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700106 routeStore.setDelegate(delegate);
107 hostService.addListener(hostListener);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800108
109 routeStore.getRouteTables().stream()
110 .flatMap(id -> routeStore.getRoutes(id).stream())
111 .forEach(this::resolve);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700112 }
113
114 @Deactivate
115 protected void deactivate() {
Jonathan Hartd4be52f2017-05-25 14:21:44 -0700116 routeMonitor.shutdown();
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -0800117 synchronized (this) {
118 listeners.values().forEach(ListenerQueue::stop);
119 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700120
121 routeStore.unsetDelegate(delegate);
122 hostService.removeListener(hostListener);
123 }
124
125 /**
126 * {@inheritDoc}
127 *
128 * In a departure from other services in ONOS, calling addListener will
129 * cause all current routes to be pushed to the listener before any new
130 * events are sent. This allows a listener to easily get the exact set of
131 * routes without worrying about missing any.
132 *
133 * @param listener listener to be added
134 */
135 @Override
136 public void addListener(RouteListener listener) {
137 synchronized (this) {
138 log.debug("Synchronizing current routes to new listener");
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700139 ListenerQueue l = createListenerQueue(listener);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800140 resolvedRouteStore.getRouteTables().stream()
141 .map(resolvedRouteStore::getRoutes)
142 .flatMap(Collection::stream)
Jonathan Hartf7021682017-03-22 18:17:21 -0700143 .map(route -> new RouteEvent(RouteEvent.Type.ROUTE_ADDED, route,
Ray Milkey69ec8712017-08-08 13:00:43 -0700144 resolvedRouteStore.getAllRoutes(route.prefix())))
Jonathan Hart96c146b2017-02-24 16:32:00 -0800145 .forEach(l::post);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700146
147 listeners.put(listener, l);
148
149 l.start();
150 log.debug("Route synchronization complete");
151 }
152 }
153
154 @Override
155 public void removeListener(RouteListener listener) {
156 synchronized (this) {
157 ListenerQueue l = listeners.remove(listener);
158 if (l != null) {
159 l.stop();
160 }
161 }
162 }
163
164 /**
165 * Posts an event to all listeners.
166 *
167 * @param event event
168 */
169 private void post(RouteEvent event) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800170 if (event != null) {
171 log.debug("Sending event {}", event);
172 synchronized (this) {
173 listeners.values().forEach(l -> l.post(event));
174 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700175 }
176 }
177
Jonathan Hart96c146b2017-02-24 16:32:00 -0800178 private Collection<Route> reformatRoutes(Collection<RouteSet> routeSets) {
179 return routeSets.stream().flatMap(r -> r.routes().stream()).collect(Collectors.toList());
180 }
181
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -0800182 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800183 public Collection<RouteTableId> getRouteTables() {
184 return routeStore.getRouteTables();
185 }
186
187 @Override
188 public Collection<RouteInfo> getRoutes(RouteTableId id) {
189 return routeStore.getRoutes(id).stream()
190 .map(routeSet -> new RouteInfo(routeSet.prefix(),
191 resolvedRouteStore.getRoute(routeSet.prefix()).orElse(null), resolveRouteSet(routeSet)))
192 .collect(Collectors.toList());
193 }
194
195 private Set<ResolvedRoute> resolveRouteSet(RouteSet routeSet) {
196 return routeSet.routes().stream()
197 .map(this::tryResolve)
198 .collect(Collectors.toSet());
199 }
200
201 private ResolvedRoute tryResolve(Route route) {
202 ResolvedRoute resolvedRoute = resolve(route);
203 if (resolvedRoute == null) {
Charles Chan9640c812017-08-23 13:55:39 -0700204 resolvedRoute = new ResolvedRoute(route, null, null, null);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800205 }
206 return resolvedRoute;
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700207 }
208
209 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800210 public Optional<ResolvedRoute> longestPrefixLookup(IpAddress ip) {
211 return resolvedRouteStore.longestPrefixMatch(ip);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700212 }
213
214 @Override
Andrea Campanellacc2424a2018-03-07 14:27:54 -0800215 public Collection<ResolvedRoute> getAllResolvedRoutes(IpPrefix prefix) {
216 return ImmutableList.copyOf(resolvedRouteStore.getAllRoutes(prefix));
217 }
218
219 @Override
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700220 public void update(Collection<Route> routes) {
221 synchronized (this) {
222 routes.forEach(route -> {
Jonathan Hartfd176612016-04-11 10:42:10 -0700223 log.debug("Received update {}", route);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700224 routeStore.updateRoute(route);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700225 });
226 }
227 }
228
229 @Override
230 public void withdraw(Collection<Route> routes) {
231 synchronized (this) {
Jonathan Hartfd176612016-04-11 10:42:10 -0700232 routes.forEach(route -> {
Charles Chanb21d69a2016-11-11 17:46:14 -0800233 log.debug("Received withdraw {}", route);
Jonathan Hartfd176612016-04-11 10:42:10 -0700234 routeStore.removeRoute(route);
235 });
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700236 }
237 }
238
Jonathan Hart89ef1582017-06-19 11:12:23 -0700239 @Override
240 public Route longestPrefixMatch(IpAddress ip) {
241 return longestPrefixLookup(ip)
242 .map(ResolvedRoute::route)
243 .orElse(null);
244 }
245
Jonathan Hart96c146b2017-02-24 16:32:00 -0800246 private ResolvedRoute resolve(Route route) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700247 hostService.startMonitoringIp(route.nextHop());
Jonathan Hart96c146b2017-02-24 16:32:00 -0800248 Set<Host> hosts = hostService.getHostsByIp(route.nextHop());
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700249
Charles Chan9640c812017-08-23 13:55:39 -0700250 return hosts.stream().findFirst()
251 .map(host -> new ResolvedRoute(route, host.mac(), host.vlan(), host.location()))
252 .orElse(null);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800253 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700254
Jonathan Hart96c146b2017-02-24 16:32:00 -0800255 private ResolvedRoute decide(ResolvedRoute route1, ResolvedRoute route2) {
Jonathan Hartf7021682017-03-22 18:17:21 -0700256 return Comparator.comparing(ResolvedRoute::nextHop)
Jonathan Hart96c146b2017-02-24 16:32:00 -0800257 .compare(route1, route2) <= 0 ? route1 : route2;
258 }
259
Jonathan Hartf7021682017-03-22 18:17:21 -0700260 private void store(ResolvedRoute route, Set<ResolvedRoute> alternatives) {
261 post(resolvedRouteStore.updateRoute(route, alternatives));
262 }
263
264 private void remove(IpPrefix prefix) {
265 post(resolvedRouteStore.removeRoute(prefix));
Jonathan Hart96c146b2017-02-24 16:32:00 -0800266 }
267
268 private void resolve(RouteSet routes) {
Jonathan Hartf7021682017-03-22 18:17:21 -0700269 Set<ResolvedRoute> resolvedRoutes = routes.routes().stream()
270 .map(this::resolve)
271 .filter(Objects::nonNull)
272 .collect(Collectors.toSet());
273
274 Optional<ResolvedRoute> bestRoute = resolvedRoutes.stream()
Jonathan Hart96c146b2017-02-24 16:32:00 -0800275 .reduce(this::decide);
276
Jonathan Hartf7021682017-03-22 18:17:21 -0700277 if (bestRoute.isPresent()) {
278 store(bestRoute.get(), resolvedRoutes);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800279 } else {
Jonathan Hartf7021682017-03-22 18:17:21 -0700280 remove(routes.prefix());
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700281 }
282 }
283
284 private void hostUpdated(Host host) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800285 hostChanged(host);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700286 }
287
288 private void hostRemoved(Host host) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800289 hostChanged(host);
290 }
291
292 private void hostChanged(Host host) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700293 synchronized (this) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800294 host.ipAddresses().stream()
295 .flatMap(ip -> routeStore.getRoutesForNextHop(ip).stream())
296 .map(route -> routeStore.getRoutes(route.prefix()))
297 .forEach(this::resolve);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700298 }
299 }
300
301 /**
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700302 * Creates a new listener queue.
303 *
304 * @param listener route listener
305 * @return listener queue
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700306 */
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700307 ListenerQueue createListenerQueue(RouteListener listener) {
308 return new DefaultListenerQueue(listener);
309 }
310
311 /**
312 * Default route listener queue.
313 */
314 private class DefaultListenerQueue implements ListenerQueue {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700315
316 private final ExecutorService executorService;
317 private final BlockingQueue<RouteEvent> queue;
318 private final RouteListener listener;
319
320 /**
321 * Creates a new listener queue.
322 *
323 * @param listener route listener to queue updates for
324 */
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700325 public DefaultListenerQueue(RouteListener listener) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700326 this.listener = listener;
327 queue = new LinkedBlockingQueue<>();
328 executorService = newSingleThreadExecutor(threadFactory);
329 }
330
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700331 @Override
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700332 public void post(RouteEvent event) {
333 queue.add(event);
334 }
335
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700336 @Override
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700337 public void start() {
338 executorService.execute(this::poll);
339 }
340
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700341 @Override
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700342 public void stop() {
343 executorService.shutdown();
344 }
345
346 private void poll() {
Jonathan Hartf79ab482016-08-19 14:20:50 -0700347 while (true) {
348 try {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700349 listener.event(queue.take());
Jonathan Hartf79ab482016-08-19 14:20:50 -0700350 } catch (InterruptedException e) {
351 log.info("Route listener event thread shutting down: {}", e.getMessage());
Ray Milkey5c7d4882018-02-05 14:50:39 -0800352 Thread.currentThread().interrupt();
Jonathan Hartf79ab482016-08-19 14:20:50 -0700353 break;
354 } catch (Exception e) {
355 log.warn("Exception during route event handler", e);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700356 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700357 }
358 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700359 }
360
361 /**
362 * Delegate to receive events from the route store.
363 */
364 private class InternalRouteStoreDelegate implements RouteStoreDelegate {
365 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800366 public void notify(InternalRouteEvent event) {
367 switch (event.type()) {
368 case ROUTE_ADDED:
369 resolve(event.subject());
370 break;
371 case ROUTE_REMOVED:
372 resolve(event.subject());
373 break;
374 default:
375 break;
376 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700377 }
378 }
379
380 /**
381 * Internal listener for host events.
382 */
383 private class InternalHostListener implements HostListener {
384 @Override
385 public void event(HostEvent event) {
386 switch (event.type()) {
387 case HOST_ADDED:
388 case HOST_UPDATED:
389 hostUpdated(event.subject());
390 break;
391 case HOST_REMOVED:
392 hostRemoved(event.subject());
393 break;
394 case HOST_MOVED:
395 break;
396 default:
397 break;
398 }
399 }
400 }
401
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700402}