blob: 267bb6976570d964384f6562065105b0993d7a5a [file] [log] [blame]
Jonathan Hartbfc5c482016-04-05 18:57:00 -07001/*
Jonathan Hart96c146b2017-02-24 16:32:00 -08002 * Copyright 2017-present Open Networking Laboratory
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
17package org.onosproject.incubator.net.routing.impl;
18
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;
Jonathan Hart96c146b2017-02-24 16:32:00 -080028import org.onosproject.incubator.net.routing.InternalRouteEvent;
Jonathan Hartfd176612016-04-11 10:42:10 -070029import org.onosproject.incubator.net.routing.NextHop;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070030import org.onosproject.incubator.net.routing.ResolvedRoute;
31import org.onosproject.incubator.net.routing.Route;
32import org.onosproject.incubator.net.routing.RouteAdminService;
33import org.onosproject.incubator.net.routing.RouteEvent;
Jonathan Hart96c146b2017-02-24 16:32:00 -080034import org.onosproject.incubator.net.routing.RouteInfo;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070035import org.onosproject.incubator.net.routing.RouteListener;
36import org.onosproject.incubator.net.routing.RouteService;
Jonathan Hart96c146b2017-02-24 16:32:00 -080037import org.onosproject.incubator.net.routing.RouteSet;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070038import org.onosproject.incubator.net.routing.RouteStore;
39import org.onosproject.incubator.net.routing.RouteStoreDelegate;
40import org.onosproject.incubator.net.routing.RouteTableId;
41import 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;
51import java.util.Collections;
Jonathan Hart96c146b2017-02-24 16:32:00 -080052import java.util.Comparator;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070053import java.util.HashMap;
54import java.util.Map;
Jonathan Hart96c146b2017-02-24 16:32:00 -080055import java.util.Objects;
Jonathan Hartbfc5c482016-04-05 18:57:00 -070056import java.util.Optional;
57import java.util.Set;
58import java.util.concurrent.BlockingQueue;
59import java.util.concurrent.ExecutorService;
60import java.util.concurrent.LinkedBlockingQueue;
61import java.util.concurrent.ThreadFactory;
62import java.util.function.Function;
63import java.util.stream.Collectors;
64
65import static java.util.concurrent.Executors.newSingleThreadExecutor;
66import static org.onlab.util.Tools.groupedThreads;
67
68/**
69 * Implementation of the unicast route service.
70 */
71@Service
72@Component
Jonathan Hartf7021682017-03-22 18:17:21 -070073public class RouteManager implements RouteService, RouteAdminService {
Jonathan Hartbfc5c482016-04-05 18:57:00 -070074
75 private final Logger log = LoggerFactory.getLogger(getClass());
76
77 private RouteStoreDelegate delegate = new InternalRouteStoreDelegate();
78 private InternalHostListener hostListener = new InternalHostListener();
79
80 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
81 protected RouteStore routeStore;
82
83 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
84 protected HostService hostService;
85
Jonathan Hartd4be52f2017-05-25 14:21:44 -070086 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
87 protected ClusterService clusterService;
88
89 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
90 protected StorageService storageService;
91
Jonathan Hart96c146b2017-02-24 16:32:00 -080092 private ResolvedRouteStore resolvedRouteStore;
93
Jonathan Hartd4be52f2017-05-25 14:21:44 -070094 private RouteMonitor routeMonitor;
95
Jonathan Hartbfc5c482016-04-05 18:57:00 -070096 @GuardedBy(value = "this")
97 private Map<RouteListener, ListenerQueue> listeners = new HashMap<>();
98
99 private ThreadFactory threadFactory;
100
101 @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();
Jonathan Hart96c146b2017-02-24 16:32:00 -0800119 listeners.values().forEach(ListenerQueue::stop);
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,
144 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
178 @Override
179 public Map<RouteTableId, Collection<Route>> getAllRoutes() {
180 return routeStore.getRouteTables().stream()
181 .collect(Collectors.toMap(Function.identity(),
182 table -> (table == null) ?
Jonathan Hart96c146b2017-02-24 16:32:00 -0800183 Collections.emptySet() : reformatRoutes(routeStore.getRoutes(table))));
184 }
185
186 private Collection<Route> reformatRoutes(Collection<RouteSet> routeSets) {
187 return routeSets.stream().flatMap(r -> r.routes().stream()).collect(Collectors.toList());
188 }
189
190 public Collection<RouteTableId> getRouteTables() {
191 return routeStore.getRouteTables();
192 }
193
194 @Override
195 public Collection<RouteInfo> getRoutes(RouteTableId id) {
196 return routeStore.getRoutes(id).stream()
197 .map(routeSet -> new RouteInfo(routeSet.prefix(),
198 resolvedRouteStore.getRoute(routeSet.prefix()).orElse(null), resolveRouteSet(routeSet)))
199 .collect(Collectors.toList());
200 }
201
202 private Set<ResolvedRoute> resolveRouteSet(RouteSet routeSet) {
203 return routeSet.routes().stream()
204 .map(this::tryResolve)
205 .collect(Collectors.toSet());
206 }
207
208 private ResolvedRoute tryResolve(Route route) {
209 ResolvedRoute resolvedRoute = resolve(route);
210 if (resolvedRoute == null) {
211 resolvedRoute = new ResolvedRoute(route, null, null);
212 }
213 return resolvedRoute;
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700214 }
215
216 @Override
217 public Route longestPrefixMatch(IpAddress ip) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800218 return longestPrefixLookup(ip)
Jonathan Hart10dbafd2017-05-18 15:53:03 -0700219 .map(ResolvedRoute::route)
Jonathan Hart96c146b2017-02-24 16:32:00 -0800220 .orElse(null);
221 }
222
223 @Override
224 public Optional<ResolvedRoute> longestPrefixLookup(IpAddress ip) {
225 return resolvedRouteStore.longestPrefixMatch(ip);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700226 }
227
228 @Override
Jonathan Hartfd176612016-04-11 10:42:10 -0700229 public Collection<Route> getRoutesForNextHop(IpAddress nextHop) {
230 return routeStore.getRoutesForNextHop(nextHop);
231 }
232
233 @Override
234 public Set<NextHop> getNextHops() {
235 return routeStore.getNextHops().entrySet().stream()
Charles Chanc78a0982016-11-09 16:52:11 -0800236 .map(entry -> new NextHop(entry.getKey(), entry.getValue()))
Jonathan Hartfd176612016-04-11 10:42:10 -0700237 .collect(Collectors.toSet());
238 }
239
240 @Override
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700241 public void update(Collection<Route> routes) {
242 synchronized (this) {
243 routes.forEach(route -> {
Jonathan Hartfd176612016-04-11 10:42:10 -0700244 log.debug("Received update {}", route);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700245 routeStore.updateRoute(route);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700246 });
247 }
248 }
249
250 @Override
251 public void withdraw(Collection<Route> routes) {
252 synchronized (this) {
Jonathan Hartfd176612016-04-11 10:42:10 -0700253 routes.forEach(route -> {
Charles Chanb21d69a2016-11-11 17:46:14 -0800254 log.debug("Received withdraw {}", route);
Jonathan Hartfd176612016-04-11 10:42:10 -0700255 routeStore.removeRoute(route);
256 });
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700257 }
258 }
259
Jonathan Hart96c146b2017-02-24 16:32:00 -0800260 private ResolvedRoute resolve(Route route) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700261 hostService.startMonitoringIp(route.nextHop());
Jonathan Hart96c146b2017-02-24 16:32:00 -0800262 Set<Host> hosts = hostService.getHostsByIp(route.nextHop());
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700263
Jonathan Hart96c146b2017-02-24 16:32:00 -0800264 Optional<Host> host = hosts.stream().findFirst();
265 if (host.isPresent()) {
Charles Chan92ca94d2017-03-17 18:05:22 -0700266 return new ResolvedRoute(route, host.get().mac(), host.get().vlan(),
267 host.get().location());
Jonathan Hart96c146b2017-02-24 16:32:00 -0800268 } else {
269 return null;
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700270 }
Jonathan Hart96c146b2017-02-24 16:32:00 -0800271 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700272
Jonathan Hart96c146b2017-02-24 16:32:00 -0800273 private ResolvedRoute decide(ResolvedRoute route1, ResolvedRoute route2) {
Jonathan Hartf7021682017-03-22 18:17:21 -0700274 return Comparator.comparing(ResolvedRoute::nextHop)
Jonathan Hart96c146b2017-02-24 16:32:00 -0800275 .compare(route1, route2) <= 0 ? route1 : route2;
276 }
277
Jonathan Hartf7021682017-03-22 18:17:21 -0700278 private void store(ResolvedRoute route, Set<ResolvedRoute> alternatives) {
279 post(resolvedRouteStore.updateRoute(route, alternatives));
280 }
281
282 private void remove(IpPrefix prefix) {
283 post(resolvedRouteStore.removeRoute(prefix));
Jonathan Hart96c146b2017-02-24 16:32:00 -0800284 }
285
286 private void resolve(RouteSet routes) {
Jonathan Hartf7021682017-03-22 18:17:21 -0700287 Set<ResolvedRoute> resolvedRoutes = routes.routes().stream()
288 .map(this::resolve)
289 .filter(Objects::nonNull)
290 .collect(Collectors.toSet());
291
292 Optional<ResolvedRoute> bestRoute = resolvedRoutes.stream()
Jonathan Hart96c146b2017-02-24 16:32:00 -0800293 .reduce(this::decide);
294
Jonathan Hartf7021682017-03-22 18:17:21 -0700295 if (bestRoute.isPresent()) {
296 store(bestRoute.get(), resolvedRoutes);
Jonathan Hart96c146b2017-02-24 16:32:00 -0800297 } else {
Jonathan Hartf7021682017-03-22 18:17:21 -0700298 remove(routes.prefix());
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700299 }
300 }
301
302 private void hostUpdated(Host host) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800303 hostChanged(host);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700304 }
305
306 private void hostRemoved(Host host) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800307 hostChanged(host);
308 }
309
310 private void hostChanged(Host host) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700311 synchronized (this) {
Jonathan Hart96c146b2017-02-24 16:32:00 -0800312 host.ipAddresses().stream()
313 .flatMap(ip -> routeStore.getRoutesForNextHop(ip).stream())
314 .map(route -> routeStore.getRoutes(route.prefix()))
315 .forEach(this::resolve);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700316 }
317 }
318
319 /**
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700320 * Creates a new listener queue.
321 *
322 * @param listener route listener
323 * @return listener queue
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700324 */
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700325 ListenerQueue createListenerQueue(RouteListener listener) {
326 return new DefaultListenerQueue(listener);
327 }
328
329 /**
330 * Default route listener queue.
331 */
332 private class DefaultListenerQueue implements ListenerQueue {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700333
334 private final ExecutorService executorService;
335 private final BlockingQueue<RouteEvent> queue;
336 private final RouteListener listener;
337
338 /**
339 * Creates a new listener queue.
340 *
341 * @param listener route listener to queue updates for
342 */
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700343 public DefaultListenerQueue(RouteListener listener) {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700344 this.listener = listener;
345 queue = new LinkedBlockingQueue<>();
346 executorService = newSingleThreadExecutor(threadFactory);
347 }
348
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700349 @Override
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700350 public void post(RouteEvent event) {
351 queue.add(event);
352 }
353
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700354 @Override
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700355 public void start() {
356 executorService.execute(this::poll);
357 }
358
Jonathan Hart6c2e7962016-04-11 13:54:09 -0700359 @Override
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700360 public void stop() {
361 executorService.shutdown();
362 }
363
364 private void poll() {
Jonathan Hartf79ab482016-08-19 14:20:50 -0700365 while (true) {
366 try {
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700367 listener.event(queue.take());
Jonathan Hartf79ab482016-08-19 14:20:50 -0700368 } catch (InterruptedException e) {
369 log.info("Route listener event thread shutting down: {}", e.getMessage());
370 break;
371 } catch (Exception e) {
372 log.warn("Exception during route event handler", e);
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700373 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700374 }
375 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700376 }
377
378 /**
379 * Delegate to receive events from the route store.
380 */
381 private class InternalRouteStoreDelegate implements RouteStoreDelegate {
382 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800383 public void notify(InternalRouteEvent event) {
384 switch (event.type()) {
385 case ROUTE_ADDED:
386 resolve(event.subject());
387 break;
388 case ROUTE_REMOVED:
389 resolve(event.subject());
390 break;
391 default:
392 break;
393 }
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700394 }
395 }
396
397 /**
398 * Internal listener for host events.
399 */
400 private class InternalHostListener implements HostListener {
401 @Override
402 public void event(HostEvent event) {
403 switch (event.type()) {
404 case HOST_ADDED:
405 case HOST_UPDATED:
406 hostUpdated(event.subject());
407 break;
408 case HOST_REMOVED:
409 hostRemoved(event.subject());
410 break;
411 case HOST_MOVED:
412 break;
413 default:
414 break;
415 }
416 }
417 }
418
Jonathan Hartbfc5c482016-04-05 18:57:00 -0700419}