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 |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 212 | public Optional<ResolvedRoute> longestPrefixLookup(IpAddress ip) { |
| 213 | return resolvedRouteStore.longestPrefixMatch(ip); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | @Override |
Andrea Campanella | cc2424a | 2018-03-07 14:27:54 -0800 | [diff] [blame] | 217 | public Collection<ResolvedRoute> getAllResolvedRoutes(IpPrefix prefix) { |
| 218 | return ImmutableList.copyOf(resolvedRouteStore.getAllRoutes(prefix)); |
| 219 | } |
| 220 | |
| 221 | @Override |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 222 | public void update(Collection<Route> routes) { |
| 223 | synchronized (this) { |
| 224 | routes.forEach(route -> { |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 225 | log.debug("Received update {}", route); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 226 | routeStore.updateRoute(route); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 227 | }); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public void withdraw(Collection<Route> routes) { |
| 233 | synchronized (this) { |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 234 | routes.forEach(route -> { |
Charles Chan | b21d69a | 2016-11-11 17:46:14 -0800 | [diff] [blame] | 235 | log.debug("Received withdraw {}", route); |
Jonathan Hart | fd17661 | 2016-04-11 10:42:10 -0700 | [diff] [blame] | 236 | routeStore.removeRoute(route); |
| 237 | }); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
Jonathan Hart | 89ef158 | 2017-06-19 11:12:23 -0700 | [diff] [blame] | 241 | @Override |
| 242 | public Route longestPrefixMatch(IpAddress ip) { |
| 243 | return longestPrefixLookup(ip) |
| 244 | .map(ResolvedRoute::route) |
| 245 | .orElse(null); |
| 246 | } |
| 247 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 248 | private ResolvedRoute resolve(Route route) { |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 249 | hostService.startMonitoringIp(route.nextHop()); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 250 | Set<Host> hosts = hostService.getHostsByIp(route.nextHop()); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 251 | |
Charles Chan | 9640c81 | 2017-08-23 13:55:39 -0700 | [diff] [blame] | 252 | return hosts.stream().findFirst() |
Ray Milkey | 69aad44 | 2018-09-14 16:24:26 -0700 | [diff] [blame] | 253 | .map(host -> new ResolvedRoute(route, host.mac(), host.vlan())) |
Charles Chan | 9640c81 | 2017-08-23 13:55:39 -0700 | [diff] [blame] | 254 | .orElse(null); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 255 | } |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 256 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 257 | private ResolvedRoute decide(ResolvedRoute route1, ResolvedRoute route2) { |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 258 | return Comparator.comparing(ResolvedRoute::nextHop) |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 259 | .compare(route1, route2) <= 0 ? route1 : route2; |
| 260 | } |
| 261 | |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 262 | private void store(ResolvedRoute route, Set<ResolvedRoute> alternatives) { |
| 263 | post(resolvedRouteStore.updateRoute(route, alternatives)); |
| 264 | } |
| 265 | |
| 266 | private void remove(IpPrefix prefix) { |
| 267 | post(resolvedRouteStore.removeRoute(prefix)); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | private void resolve(RouteSet routes) { |
Ray Milkey | b213743 | 2018-05-08 16:23:06 -0700 | [diff] [blame] | 271 | if (routes.routes() == null) { |
| 272 | // The routes were removed before we got to them, nothing to do |
| 273 | return; |
| 274 | } |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 275 | Set<ResolvedRoute> resolvedRoutes = routes.routes().stream() |
| 276 | .map(this::resolve) |
| 277 | .filter(Objects::nonNull) |
| 278 | .collect(Collectors.toSet()); |
| 279 | |
| 280 | Optional<ResolvedRoute> bestRoute = resolvedRoutes.stream() |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 281 | .reduce(this::decide); |
| 282 | |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 283 | if (bestRoute.isPresent()) { |
| 284 | store(bestRoute.get(), resolvedRoutes); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 285 | } else { |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 286 | remove(routes.prefix()); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
| 290 | private void hostUpdated(Host host) { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 291 | hostChanged(host); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | private void hostRemoved(Host host) { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 295 | hostChanged(host); |
| 296 | } |
| 297 | |
| 298 | private void hostChanged(Host host) { |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 299 | synchronized (this) { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 300 | host.ipAddresses().stream() |
| 301 | .flatMap(ip -> routeStore.getRoutesForNextHop(ip).stream()) |
| 302 | .map(route -> routeStore.getRoutes(route.prefix())) |
| 303 | .forEach(this::resolve); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
| 307 | /** |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 308 | * Creates a new listener queue. |
| 309 | * |
| 310 | * @param listener route listener |
| 311 | * @return listener queue |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 312 | */ |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 313 | ListenerQueue createListenerQueue(RouteListener listener) { |
| 314 | return new DefaultListenerQueue(listener); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Default route listener queue. |
| 319 | */ |
| 320 | private class DefaultListenerQueue implements ListenerQueue { |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 321 | |
| 322 | private final ExecutorService executorService; |
| 323 | private final BlockingQueue<RouteEvent> queue; |
| 324 | private final RouteListener listener; |
| 325 | |
| 326 | /** |
| 327 | * Creates a new listener queue. |
| 328 | * |
| 329 | * @param listener route listener to queue updates for |
| 330 | */ |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 331 | public DefaultListenerQueue(RouteListener listener) { |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 332 | this.listener = listener; |
| 333 | queue = new LinkedBlockingQueue<>(); |
| 334 | executorService = newSingleThreadExecutor(threadFactory); |
| 335 | } |
| 336 | |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 337 | @Override |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 338 | public void post(RouteEvent event) { |
| 339 | queue.add(event); |
| 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 start() { |
| 344 | executorService.execute(this::poll); |
| 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 stop() { |
| 349 | executorService.shutdown(); |
| 350 | } |
| 351 | |
| 352 | private void poll() { |
Jonathan Hart | f79ab48 | 2016-08-19 14:20:50 -0700 | [diff] [blame] | 353 | while (true) { |
| 354 | try { |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 355 | listener.event(queue.take()); |
Jonathan Hart | f79ab48 | 2016-08-19 14:20:50 -0700 | [diff] [blame] | 356 | } catch (InterruptedException e) { |
| 357 | log.info("Route listener event thread shutting down: {}", e.getMessage()); |
Ray Milkey | 5c7d488 | 2018-02-05 14:50:39 -0800 | [diff] [blame] | 358 | Thread.currentThread().interrupt(); |
Jonathan Hart | f79ab48 | 2016-08-19 14:20:50 -0700 | [diff] [blame] | 359 | break; |
| 360 | } catch (Exception e) { |
| 361 | log.warn("Exception during route event handler", e); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 362 | } |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 363 | } |
| 364 | } |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Delegate to receive events from the route store. |
| 369 | */ |
| 370 | private class InternalRouteStoreDelegate implements RouteStoreDelegate { |
| 371 | @Override |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 372 | public void notify(InternalRouteEvent event) { |
| 373 | switch (event.type()) { |
| 374 | case ROUTE_ADDED: |
| 375 | resolve(event.subject()); |
| 376 | break; |
| 377 | case ROUTE_REMOVED: |
| 378 | resolve(event.subject()); |
| 379 | break; |
| 380 | default: |
| 381 | break; |
| 382 | } |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Internal listener for host events. |
| 388 | */ |
| 389 | private class InternalHostListener implements HostListener { |
| 390 | @Override |
| 391 | public void event(HostEvent event) { |
| 392 | switch (event.type()) { |
| 393 | case HOST_ADDED: |
| 394 | case HOST_UPDATED: |
Charles Chan | e440498 | 2018-07-12 12:53:33 -0700 | [diff] [blame] | 395 | case HOST_MOVED: |
Jordan Halterman | 6328db7 | 2018-04-10 13:34:50 -0400 | [diff] [blame] | 396 | log.trace("Scheduled host event {}", event); |
| 397 | hostEventExecutor.execute(() -> hostUpdated(event.subject())); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 398 | break; |
| 399 | case HOST_REMOVED: |
Jordan Halterman | 6328db7 | 2018-04-10 13:34:50 -0400 | [diff] [blame] | 400 | log.trace("Scheduled host event {}", event); |
| 401 | hostEventExecutor.execute(() -> hostRemoved(event.subject())); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 402 | break; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 403 | default: |
| 404 | break; |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 409 | } |