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; |
pier | e91c87f | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 22 | import org.onlab.util.PredictableExecutor; |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame] | 23 | import org.onosproject.cluster.ClusterService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 24 | import org.onosproject.net.Host; |
| 25 | import org.onosproject.net.host.HostEvent; |
| 26 | import org.onosproject.net.host.HostListener; |
| 27 | import org.onosproject.net.host.HostService; |
Ray Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 28 | import org.onosproject.routeservice.InternalRouteEvent; |
| 29 | import org.onosproject.routeservice.ResolvedRoute; |
| 30 | import org.onosproject.routeservice.Route; |
| 31 | import org.onosproject.routeservice.RouteAdminService; |
| 32 | import org.onosproject.routeservice.RouteEvent; |
| 33 | import org.onosproject.routeservice.RouteInfo; |
| 34 | import org.onosproject.routeservice.RouteListener; |
| 35 | import org.onosproject.routeservice.RouteService; |
Ray Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 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 | |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 48 | import java.util.Collection; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 49 | import java.util.Map; |
| 50 | import java.util.Optional; |
| 51 | import java.util.Set; |
| 52 | import java.util.concurrent.BlockingQueue; |
pier | e91c87f | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 53 | import java.util.concurrent.ConcurrentHashMap; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 54 | import java.util.concurrent.ExecutorService; |
| 55 | import java.util.concurrent.LinkedBlockingQueue; |
| 56 | import java.util.concurrent.ThreadFactory; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 57 | import java.util.stream.Collectors; |
| 58 | |
| 59 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 60 | import static org.onlab.util.Tools.groupedThreads; |
| 61 | |
| 62 | /** |
| 63 | * Implementation of the unicast route service. |
| 64 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 65 | @Component(service = { RouteService.class, RouteAdminService.class }) |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 66 | public class RouteManager implements RouteService, RouteAdminService { |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 67 | |
| 68 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 69 | |
pier | e91c87f | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 70 | private static final int DEFAULT_BUCKETS = 0; |
| 71 | |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 72 | private RouteStoreDelegate delegate = new InternalRouteStoreDelegate(); |
| 73 | private InternalHostListener hostListener = new InternalHostListener(); |
| 74 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 76 | protected RouteStore routeStore; |
| 77 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 79 | protected HostService hostService; |
| 80 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame] | 82 | protected ClusterService clusterService; |
| 83 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame] | 85 | protected StorageService storageService; |
| 86 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 87 | private ResolvedRouteStore resolvedRouteStore; |
| 88 | |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame] | 89 | private RouteMonitor routeMonitor; |
| 90 | |
pier | e91c87f | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 91 | protected RouteResolver routeResolver; |
| 92 | |
| 93 | private Map<RouteListener, ListenerQueue> listeners = new ConcurrentHashMap<>(); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 94 | |
| 95 | private ThreadFactory threadFactory; |
| 96 | |
pier | e91c87f | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 97 | protected PredictableExecutor hostEventExecutors; |
Jordan Halterman | 6328db7 | 2018-04-10 13:34:50 -0400 | [diff] [blame] | 98 | |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 99 | @Activate |
| 100 | protected void activate() { |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame] | 101 | routeMonitor = new RouteMonitor(this, clusterService, storageService); |
pier | e91c87f | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 102 | routeResolver = new RouteResolver(this, hostService); |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 103 | threadFactory = groupedThreads("onos/route", "listener-%d", log); |
pier | e91c87f | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 104 | hostEventExecutors = new PredictableExecutor(DEFAULT_BUCKETS, groupedThreads("onos/route-manager", |
| 105 | "event-host-%d", log)); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 106 | |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 107 | resolvedRouteStore = new DefaultResolvedRouteStore(); |
| 108 | |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 109 | routeStore.setDelegate(delegate); |
| 110 | hostService.addListener(hostListener); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 111 | |
| 112 | routeStore.getRouteTables().stream() |
| 113 | .flatMap(id -> routeStore.getRoutes(id).stream()) |
pier | e91c87f | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 114 | .forEach(routeSet -> routeResolver.resolve(routeSet)); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Deactivate |
| 118 | protected void deactivate() { |
Jonathan Hart | d4be52f | 2017-05-25 14:21:44 -0700 | [diff] [blame] | 119 | routeMonitor.shutdown(); |
pier | e91c87f | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 120 | routeResolver.shutdown(); |
| 121 | listeners.values().forEach(ListenerQueue::stop); |
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) { |
pier | e91c87f | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 139 | log.debug("Synchronizing current routes to new listener"); |
| 140 | ListenerQueue listenerQueue = listeners.compute(listener, (key, value) -> { |
| 141 | // Create listener regardless the existence of a previous value |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 142 | ListenerQueue l = createListenerQueue(listener); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 143 | resolvedRouteStore.getRouteTables().stream() |
| 144 | .map(resolvedRouteStore::getRoutes) |
| 145 | .flatMap(Collection::stream) |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 146 | .map(route -> new RouteEvent(RouteEvent.Type.ROUTE_ADDED, route, |
Ray Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 147 | resolvedRouteStore.getAllRoutes(route.prefix()))) |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 148 | .forEach(l::post); |
pier | e91c87f | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 149 | return l; |
| 150 | }); |
| 151 | // Start draining the events |
| 152 | listenerQueue.start(); |
| 153 | log.debug("Route synchronization complete"); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public void removeListener(RouteListener listener) { |
pier | e91c87f | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 158 | ListenerQueue l = listeners.remove(listener); |
| 159 | if (l != null) { |
| 160 | l.stop(); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Posts an event to all listeners. |
| 166 | * |
| 167 | * @param event event |
| 168 | */ |
| 169 | private void post(RouteEvent event) { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 170 | if (event != null) { |
| 171 | log.debug("Sending event {}", event); |
pier | e91c87f | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 172 | listeners.values().forEach(l -> l.post(event)); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
Yuta HIGUCHI | 488a94c | 2018-01-26 17:24:09 -0800 | [diff] [blame] | 176 | @Override |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 177 | public Collection<RouteTableId> getRouteTables() { |
| 178 | return routeStore.getRouteTables(); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public Collection<RouteInfo> getRoutes(RouteTableId id) { |
| 183 | return routeStore.getRoutes(id).stream() |
| 184 | .map(routeSet -> new RouteInfo(routeSet.prefix(), |
pier | e91c87f | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 185 | resolvedRouteStore.getRoute(routeSet.prefix()).orElse(null), |
| 186 | routeResolver.resolveRouteSet(routeSet))) |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 187 | .collect(Collectors.toList()); |
| 188 | } |
| 189 | |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 190 | @Override |
Charles Chan | 11d4dc5 | 2019-05-03 13:19:49 -0700 | [diff] [blame] | 191 | public Collection<ResolvedRoute> getResolvedRoutes(RouteTableId id) { |
| 192 | return resolvedRouteStore.getRoutes(id); |
| 193 | } |
| 194 | |
| 195 | @Override |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 196 | public Optional<ResolvedRoute> longestPrefixLookup(IpAddress ip) { |
| 197 | return resolvedRouteStore.longestPrefixMatch(ip); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | @Override |
Andrea Campanella | cc2424a | 2018-03-07 14:27:54 -0800 | [diff] [blame] | 201 | public Collection<ResolvedRoute> getAllResolvedRoutes(IpPrefix prefix) { |
| 202 | return ImmutableList.copyOf(resolvedRouteStore.getAllRoutes(prefix)); |
| 203 | } |
| 204 | |
| 205 | @Override |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 206 | public void update(Collection<Route> routes) { |
pier | e91c87f | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 207 | log.debug("Received update {}", routes); |
| 208 | routeStore.updateRoutes(routes); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public void withdraw(Collection<Route> routes) { |
pier | e91c87f | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 213 | log.debug("Received withdraw {}", routes); |
| 214 | routeStore.removeRoutes(routes); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Jonathan Hart | 89ef158 | 2017-06-19 11:12:23 -0700 | [diff] [blame] | 217 | @Override |
| 218 | public Route longestPrefixMatch(IpAddress ip) { |
| 219 | return longestPrefixLookup(ip) |
| 220 | .map(ResolvedRoute::route) |
| 221 | .orElse(null); |
| 222 | } |
| 223 | |
pier | e91c87f | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 224 | void store(ResolvedRoute route, Set<ResolvedRoute> alternatives) { |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 225 | post(resolvedRouteStore.updateRoute(route, alternatives)); |
| 226 | } |
| 227 | |
pier | e91c87f | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 228 | void remove(IpPrefix prefix) { |
Jonathan Hart | f702168 | 2017-03-22 18:17:21 -0700 | [diff] [blame] | 229 | post(resolvedRouteStore.removeRoute(prefix)); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 230 | } |
| 231 | |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 232 | private void hostUpdated(Host host) { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 233 | hostChanged(host); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | private void hostRemoved(Host host) { |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 237 | hostChanged(host); |
| 238 | } |
| 239 | |
| 240 | private void hostChanged(Host host) { |
pier | e91c87f | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 241 | routeStore.getRoutesForNextHops(host.ipAddresses()) |
| 242 | .forEach(routeSet -> routeResolver.resolve(routeSet)); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /** |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 246 | * Creates a new listener queue. |
| 247 | * |
| 248 | * @param listener route listener |
| 249 | * @return listener queue |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 250 | */ |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 251 | ListenerQueue createListenerQueue(RouteListener listener) { |
| 252 | return new DefaultListenerQueue(listener); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Default route listener queue. |
| 257 | */ |
| 258 | private class DefaultListenerQueue implements ListenerQueue { |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 259 | |
| 260 | private final ExecutorService executorService; |
| 261 | private final BlockingQueue<RouteEvent> queue; |
| 262 | private final RouteListener listener; |
| 263 | |
| 264 | /** |
| 265 | * Creates a new listener queue. |
| 266 | * |
| 267 | * @param listener route listener to queue updates for |
| 268 | */ |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 269 | public DefaultListenerQueue(RouteListener listener) { |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 270 | this.listener = listener; |
| 271 | queue = new LinkedBlockingQueue<>(); |
| 272 | executorService = newSingleThreadExecutor(threadFactory); |
| 273 | } |
| 274 | |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 275 | @Override |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 276 | public void post(RouteEvent event) { |
| 277 | queue.add(event); |
| 278 | } |
| 279 | |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 280 | @Override |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 281 | public void start() { |
| 282 | executorService.execute(this::poll); |
| 283 | } |
| 284 | |
Jonathan Hart | 6c2e796 | 2016-04-11 13:54:09 -0700 | [diff] [blame] | 285 | @Override |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 286 | public void stop() { |
| 287 | executorService.shutdown(); |
| 288 | } |
| 289 | |
| 290 | private void poll() { |
Jonathan Hart | f79ab48 | 2016-08-19 14:20:50 -0700 | [diff] [blame] | 291 | while (true) { |
| 292 | try { |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 293 | listener.event(queue.take()); |
Jonathan Hart | f79ab48 | 2016-08-19 14:20:50 -0700 | [diff] [blame] | 294 | } catch (InterruptedException e) { |
| 295 | log.info("Route listener event thread shutting down: {}", e.getMessage()); |
Ray Milkey | 5c7d488 | 2018-02-05 14:50:39 -0800 | [diff] [blame] | 296 | Thread.currentThread().interrupt(); |
Jonathan Hart | f79ab48 | 2016-08-19 14:20:50 -0700 | [diff] [blame] | 297 | break; |
| 298 | } catch (Exception e) { |
| 299 | log.warn("Exception during route event handler", e); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 300 | } |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 301 | } |
| 302 | } |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Delegate to receive events from the route store. |
| 307 | */ |
| 308 | private class InternalRouteStoreDelegate implements RouteStoreDelegate { |
| 309 | @Override |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 310 | public void notify(InternalRouteEvent event) { |
| 311 | switch (event.type()) { |
| 312 | case ROUTE_ADDED: |
pier | e91c87f | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 313 | routeResolver.resolve(event.subject()); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 314 | break; |
| 315 | case ROUTE_REMOVED: |
pier | e91c87f | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 316 | routeResolver.resolve(event.subject()); |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 317 | break; |
| 318 | default: |
| 319 | break; |
| 320 | } |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Internal listener for host events. |
| 326 | */ |
| 327 | private class InternalHostListener implements HostListener { |
| 328 | @Override |
| 329 | public void event(HostEvent event) { |
| 330 | switch (event.type()) { |
| 331 | case HOST_ADDED: |
| 332 | case HOST_UPDATED: |
Charles Chan | e440498 | 2018-07-12 12:53:33 -0700 | [diff] [blame] | 333 | case HOST_MOVED: |
Jordan Halterman | 6328db7 | 2018-04-10 13:34:50 -0400 | [diff] [blame] | 334 | log.trace("Scheduled host event {}", event); |
pier | e91c87f | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 335 | hostEventExecutors.execute(() -> hostUpdated(event.subject()), event.subject().id().hashCode()); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 336 | break; |
| 337 | case HOST_REMOVED: |
Jordan Halterman | 6328db7 | 2018-04-10 13:34:50 -0400 | [diff] [blame] | 338 | log.trace("Scheduled host event {}", event); |
pier | e91c87f | 2019-10-16 16:58:20 +0200 | [diff] [blame] | 339 | hostEventExecutors.execute(() -> hostRemoved(event.subject()), event.subject().id().hashCode()); |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 340 | break; |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 341 | default: |
| 342 | break; |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
Jonathan Hart | bfc5c48 | 2016-04-05 18:57:00 -0700 | [diff] [blame] | 347 | } |