Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 3 | * |
| 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 Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 17 | package org.onosproject.routeservice.impl; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 18 | |
Andrea Campanella | cc2424a | 2018-03-07 14:27:54 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 20 | import org.onlab.packet.IpAddress; |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 21 | import org.onlab.packet.IpPrefix; |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame] | 22 | import org.onosproject.cluster.ClusterService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 23 | import org.onosproject.net.Host; |
| 24 | import org.onosproject.net.host.HostEvent; |
| 25 | import org.onosproject.net.host.HostListener; |
| 26 | import org.onosproject.net.host.HostService; |
Ray Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 27 | import org.onosproject.routeservice.InternalRouteEvent; |
| 28 | import org.onosproject.routeservice.ResolvedRoute; |
| 29 | import org.onosproject.routeservice.Route; |
| 30 | import org.onosproject.routeservice.RouteAdminService; |
| 31 | import org.onosproject.routeservice.RouteEvent; |
| 32 | import org.onosproject.routeservice.RouteInfo; |
| 33 | import org.onosproject.routeservice.RouteListener; |
| 34 | import org.onosproject.routeservice.RouteService; |
| 35 | import org.onosproject.routeservice.RouteSet; |
| 36 | import org.onosproject.routeservice.RouteStore; |
| 37 | import org.onosproject.routeservice.RouteStoreDelegate; |
| 38 | import org.onosproject.routeservice.RouteTableId; |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame] | 39 | import org.onosproject.store.service.StorageService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 40 | import org.osgi.service.component.annotations.Activate; |
| 41 | import org.osgi.service.component.annotations.Component; |
| 42 | import org.osgi.service.component.annotations.Deactivate; |
| 43 | import org.osgi.service.component.annotations.Reference; |
| 44 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 45 | import org.slf4j.Logger; |
| 46 | import org.slf4j.LoggerFactory; |
| 47 | |
| 48 | import javax.annotation.concurrent.GuardedBy; |
| 49 | import java.util.Collection; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 50 | import java.util.Comparator; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 51 | import java.util.HashMap; |
| 52 | import java.util.Map; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 53 | import java.util.Objects; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 54 | import java.util.Optional; |
| 55 | import java.util.Set; |
| 56 | import java.util.concurrent.BlockingQueue; |
Jordan Halterman | 6328db7 | 2018-04-10 13:34:50 -0400 | [diff] [blame] | 57 | import java.util.concurrent.Executor; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 58 | import java.util.concurrent.ExecutorService; |
| 59 | import java.util.concurrent.LinkedBlockingQueue; |
| 60 | import java.util.concurrent.ThreadFactory; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 61 | import java.util.stream.Collectors; |
| 62 | |
| 63 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 64 | import static org.onlab.util.Tools.groupedThreads; |
| 65 | |
| 66 | /** |
| 67 | * Implementation of the unicast route service. |
| 68 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 69 | @Component(service = { RouteService.class, RouteAdminService.class }) |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 70 | public class RouteManager implements RouteService, RouteAdminService { |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 71 | |
| 72 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 73 | |
| 74 | private RouteStoreDelegate delegate = new InternalRouteStoreDelegate(); |
| 75 | private InternalHostListener hostListener = new InternalHostListener(); |
| 76 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 78 | protected RouteStore routeStore; |
| 79 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 81 | protected HostService hostService; |
| 82 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame] | 84 | protected ClusterService clusterService; |
| 85 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame] | 87 | protected StorageService storageService; |
| 88 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 89 | private ResolvedRouteStore resolvedRouteStore; |
| 90 | |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame] | 91 | private RouteMonitor routeMonitor; |
| 92 | |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 93 | @GuardedBy(value = "this") |
| 94 | private Map<RouteListener, ListenerQueue> listeners = new HashMap<>(); |
| 95 | |
| 96 | private ThreadFactory threadFactory; |
| 97 | |
Jordan Halterman | 6328db7 | 2018-04-10 13:34:50 -0400 | [diff] [blame] | 98 | protected Executor hostEventExecutor = newSingleThreadExecutor( |
| 99 | groupedThreads("rm-event-host", "%d", log)); |
| 100 | |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 101 | @Activate |
| 102 | protected void activate() { |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame] | 103 | routeMonitor = new RouteMonitor(this, clusterService, storageService); |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 104 | threadFactory = groupedThreads("onos/route", "listener-%d", log); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 105 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 106 | resolvedRouteStore = new DefaultResolvedRouteStore(); |
| 107 | |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 108 | routeStore.setDelegate(delegate); |
| 109 | hostService.addListener(hostListener); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 110 | |
| 111 | routeStore.getRouteTables().stream() |
| 112 | .flatMap(id -> routeStore.getRoutes(id).stream()) |
| 113 | .forEach(this::resolve); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Deactivate |
| 117 | protected void deactivate() { |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame] | 118 | routeMonitor.shutdown(); |
Yuta HIGUCHI | 488a94c | 2018-01-26 17:24:09 -0800 | [diff] [blame] | 119 | synchronized (this) { |
| 120 | listeners.values().forEach(ListenerQueue::stop); |
| 121 | } |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 122 | |
| 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 Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 141 | ListenerQueue l = createListenerQueue(listener); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 142 | resolvedRouteStore.getRouteTables().stream() |
| 143 | .map(resolvedRouteStore::getRoutes) |
| 144 | .flatMap(Collection::stream) |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 145 | .map(route -> new RouteEvent(RouteEvent.Type.ROUTE_ADDED, route, |
Ray Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 146 | resolvedRouteStore.getAllRoutes(route.prefix()))) |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 147 | .forEach(l::post); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 148 | |
| 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 Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 172 | if (event != null) { |
| 173 | log.debug("Sending event {}", event); |
| 174 | synchronized (this) { |
| 175 | listeners.values().forEach(l -> l.post(event)); |
| 176 | } |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 180 | private Collection<Route> reformatRoutes(Collection<RouteSet> routeSets) { |
| 181 | return routeSets.stream().flatMap(r -> r.routes().stream()).collect(Collectors.toList()); |
| 182 | } |
| 183 | |
Yuta HIGUCHI | 488a94c | 2018-01-26 17:24:09 -0800 | [diff] [blame] | 184 | @Override |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 185 | 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 Milkey | 69aad44 | 2018-09-14 16:24:26 -0700 | [diff] [blame] | 206 | resolvedRoute = new ResolvedRoute(route, null, null); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 207 | } |
| 208 | return resolvedRoute; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | @Override |
Charles Chan | 11d4dc5 | 2019-05-03 13:19:49 -0700 | [diff] [blame] | 212 | public Collection<ResolvedRoute> getResolvedRoutes(RouteTableId id) { |
| 213 | return resolvedRouteStore.getRoutes(id); |
| 214 | } |
| 215 | |
| 216 | @Override |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 217 | public Optional<ResolvedRoute> longestPrefixLookup(IpAddress ip) { |
| 218 | return resolvedRouteStore.longestPrefixMatch(ip); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | @Override |
Andrea Campanella | cc2424a | 2018-03-07 14:27:54 -0800 | [diff] [blame] | 222 | public Collection<ResolvedRoute> getAllResolvedRoutes(IpPrefix prefix) { |
| 223 | return ImmutableList.copyOf(resolvedRouteStore.getAllRoutes(prefix)); |
| 224 | } |
| 225 | |
| 226 | @Override |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 227 | public void update(Collection<Route> routes) { |
| 228 | synchronized (this) { |
| 229 | routes.forEach(route -> { |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 230 | log.debug("Received update {}", route); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 231 | routeStore.updateRoute(route); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 232 | }); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public void withdraw(Collection<Route> routes) { |
| 238 | synchronized (this) { |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 239 | routes.forEach(route -> { |
Charles Chan | b21d69a | 2016-11-11 17:46:14 -0800 | [diff] [blame] | 240 | log.debug("Received withdraw {}", route); |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 241 | routeStore.removeRoute(route); |
| 242 | }); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
Jonathan Hart | 89ef158 | 2017-06-19 11:12:23 -0700 | [diff] [blame] | 246 | @Override |
| 247 | public Route longestPrefixMatch(IpAddress ip) { |
| 248 | return longestPrefixLookup(ip) |
| 249 | .map(ResolvedRoute::route) |
| 250 | .orElse(null); |
| 251 | } |
| 252 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 253 | private ResolvedRoute resolve(Route route) { |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 254 | hostService.startMonitoringIp(route.nextHop()); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 255 | Set<Host> hosts = hostService.getHostsByIp(route.nextHop()); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 256 | |
Charles Chan | 9640c81 | 2017-08-23 13:55:39 -0700 | [diff] [blame] | 257 | return hosts.stream().findFirst() |
Ray Milkey | 69aad44 | 2018-09-14 16:24:26 -0700 | [diff] [blame] | 258 | .map(host -> new ResolvedRoute(route, host.mac(), host.vlan())) |
Charles Chan | 9640c81 | 2017-08-23 13:55:39 -0700 | [diff] [blame] | 259 | .orElse(null); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 260 | } |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 261 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 262 | private ResolvedRoute decide(ResolvedRoute route1, ResolvedRoute route2) { |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 263 | return Comparator.comparing(ResolvedRoute::nextHop) |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 264 | .compare(route1, route2) <= 0 ? route1 : route2; |
| 265 | } |
| 266 | |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 267 | private void store(ResolvedRoute route, Set<ResolvedRoute> alternatives) { |
| 268 | post(resolvedRouteStore.updateRoute(route, alternatives)); |
| 269 | } |
| 270 | |
| 271 | private void remove(IpPrefix prefix) { |
| 272 | post(resolvedRouteStore.removeRoute(prefix)); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | private void resolve(RouteSet routes) { |
Ray Milkey | b213743 | 2018-05-08 16:23:06 -0700 | [diff] [blame] | 276 | if (routes.routes() == null) { |
| 277 | // The routes were removed before we got to them, nothing to do |
| 278 | return; |
| 279 | } |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 280 | Set<ResolvedRoute> resolvedRoutes = routes.routes().stream() |
| 281 | .map(this::resolve) |
| 282 | .filter(Objects::nonNull) |
| 283 | .collect(Collectors.toSet()); |
| 284 | |
| 285 | Optional<ResolvedRoute> bestRoute = resolvedRoutes.stream() |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 286 | .reduce(this::decide); |
| 287 | |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 288 | if (bestRoute.isPresent()) { |
| 289 | store(bestRoute.get(), resolvedRoutes); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 290 | } else { |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 291 | remove(routes.prefix()); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
| 295 | private void hostUpdated(Host host) { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 296 | hostChanged(host); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | private void hostRemoved(Host host) { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 300 | hostChanged(host); |
| 301 | } |
| 302 | |
| 303 | private void hostChanged(Host host) { |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 304 | synchronized (this) { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 305 | host.ipAddresses().stream() |
| 306 | .flatMap(ip -> routeStore.getRoutesForNextHop(ip).stream()) |
| 307 | .map(route -> routeStore.getRoutes(route.prefix())) |
| 308 | .forEach(this::resolve); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
| 312 | /** |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 313 | * Creates a new listener queue. |
| 314 | * |
| 315 | * @param listener route listener |
| 316 | * @return listener queue |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 317 | */ |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 318 | ListenerQueue createListenerQueue(RouteListener listener) { |
| 319 | return new DefaultListenerQueue(listener); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Default route listener queue. |
| 324 | */ |
| 325 | private class DefaultListenerQueue implements ListenerQueue { |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 326 | |
| 327 | private final ExecutorService executorService; |
| 328 | private final BlockingQueue<RouteEvent> queue; |
| 329 | private final RouteListener listener; |
| 330 | |
| 331 | /** |
| 332 | * Creates a new listener queue. |
| 333 | * |
| 334 | * @param listener route listener to queue updates for |
| 335 | */ |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 336 | public DefaultListenerQueue(RouteListener listener) { |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 337 | this.listener = listener; |
| 338 | queue = new LinkedBlockingQueue<>(); |
| 339 | executorService = newSingleThreadExecutor(threadFactory); |
| 340 | } |
| 341 | |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 342 | @Override |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 343 | public void post(RouteEvent event) { |
| 344 | queue.add(event); |
| 345 | } |
| 346 | |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 347 | @Override |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 348 | public void start() { |
| 349 | executorService.execute(this::poll); |
| 350 | } |
| 351 | |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 352 | @Override |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 353 | public void stop() { |
| 354 | executorService.shutdown(); |
| 355 | } |
| 356 | |
| 357 | private void poll() { |
Jonathan Hart | f79ab48 | 2016-08-19 14:20:50 -0700 | [diff] [blame] | 358 | while (true) { |
| 359 | try { |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 360 | listener.event(queue.take()); |
Jonathan Hart | f79ab48 | 2016-08-19 14:20:50 -0700 | [diff] [blame] | 361 | } catch (InterruptedException e) { |
| 362 | log.info("Route listener event thread shutting down: {}", e.getMessage()); |
Ray Milkey | 5c7d488 | 2018-02-05 14:50:39 -0800 | [diff] [blame] | 363 | Thread.currentThread().interrupt(); |
Jonathan Hart | f79ab48 | 2016-08-19 14:20:50 -0700 | [diff] [blame] | 364 | break; |
| 365 | } catch (Exception e) { |
| 366 | log.warn("Exception during route event handler", e); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 367 | } |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 368 | } |
| 369 | } |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Delegate to receive events from the route store. |
| 374 | */ |
| 375 | private class InternalRouteStoreDelegate implements RouteStoreDelegate { |
| 376 | @Override |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 377 | public void notify(InternalRouteEvent event) { |
| 378 | switch (event.type()) { |
| 379 | case ROUTE_ADDED: |
| 380 | resolve(event.subject()); |
| 381 | break; |
| 382 | case ROUTE_REMOVED: |
| 383 | resolve(event.subject()); |
| 384 | break; |
| 385 | default: |
| 386 | break; |
| 387 | } |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * Internal listener for host events. |
| 393 | */ |
| 394 | private class InternalHostListener implements HostListener { |
| 395 | @Override |
| 396 | public void event(HostEvent event) { |
| 397 | switch (event.type()) { |
| 398 | case HOST_ADDED: |
| 399 | case HOST_UPDATED: |
Charles Chan | e440498 | 2018-07-12 12:53:33 -0700 | [diff] [blame] | 400 | case HOST_MOVED: |
Jordan Halterman | 6328db7 | 2018-04-10 13:34:50 -0400 | [diff] [blame] | 401 | log.trace("Scheduled host event {}", event); |
| 402 | hostEventExecutor.execute(() -> hostUpdated(event.subject())); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 403 | break; |
| 404 | case HOST_REMOVED: |
Jordan Halterman | 6328db7 | 2018-04-10 13:34:50 -0400 | [diff] [blame] | 405 | log.trace("Scheduled host event {}", event); |
| 406 | hostEventExecutor.execute(() -> hostRemoved(event.subject())); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 407 | break; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 408 | default: |
| 409 | break; |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 414 | } |