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