Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.topology.impl; |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 17 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 19 | import org.onlab.util.AbstractAccumulator; |
| 20 | import org.onlab.util.Accumulator; |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 21 | import org.onosproject.cfg.ComponentConfigService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 22 | import org.onosproject.event.Event; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 23 | import org.onosproject.net.device.DeviceEvent; |
| 24 | import org.onosproject.net.device.DeviceListener; |
| 25 | import org.onosproject.net.device.DeviceService; |
| 26 | import org.onosproject.net.link.LinkEvent; |
| 27 | import org.onosproject.net.link.LinkListener; |
| 28 | import org.onosproject.net.link.LinkService; |
| 29 | import org.onosproject.net.provider.AbstractProvider; |
| 30 | import org.onosproject.net.topology.DefaultGraphDescription; |
| 31 | import org.onosproject.net.topology.GraphDescription; |
| 32 | import org.onosproject.net.topology.TopologyProvider; |
| 33 | import org.onosproject.net.topology.TopologyProviderRegistry; |
| 34 | import org.onosproject.net.topology.TopologyProviderService; |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 35 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 36 | import org.osgi.service.component.annotations.Activate; |
| 37 | import org.osgi.service.component.annotations.Component; |
| 38 | import org.osgi.service.component.annotations.Deactivate; |
| 39 | import org.osgi.service.component.annotations.Modified; |
| 40 | import org.osgi.service.component.annotations.Reference; |
| 41 | import org.osgi.service.component.annotations.ReferenceCardinality; |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 42 | import org.slf4j.Logger; |
| 43 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 44 | import java.util.Collections; |
| 45 | import java.util.Dictionary; |
| 46 | import java.util.List; |
| 47 | import java.util.Timer; |
| 48 | import java.util.concurrent.ExecutorService; |
| 49 | |
| 50 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 51 | import static java.util.concurrent.Executors.newFixedThreadPool; |
| 52 | import static org.onlab.util.Tools.get; |
| 53 | import static org.onlab.util.Tools.groupedThreads; |
| 54 | import static org.onosproject.core.CoreService.CORE_PROVIDER_ID; |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 55 | import static org.onosproject.net.OsgiPropertyConstants.DTP_MAX_BATCH_MS; |
| 56 | import static org.onosproject.net.OsgiPropertyConstants.DTP_MAX_BATCH_MS_DEFAULT; |
| 57 | import static org.onosproject.net.OsgiPropertyConstants.DTP_MAX_EVENTS; |
| 58 | import static org.onosproject.net.OsgiPropertyConstants.DTP_MAX_EVENTS_DEFAULT; |
| 59 | import static org.onosproject.net.OsgiPropertyConstants.DTP_MAX_IDLE_MS; |
| 60 | import static org.onosproject.net.OsgiPropertyConstants.DTP_MAX_IDLE_MS_DEFAULT; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 61 | import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED; |
| 62 | import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED; |
| 63 | import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_REMOVED; |
| 64 | import static org.slf4j.LoggerFactory.getLogger; |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 65 | |
| 66 | /** |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 67 | * Default implementation of a network topology provider that feeds off |
| 68 | * device and link subsystem events to trigger assembly and computation of |
| 69 | * new topology snapshots. |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 70 | */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 71 | @Component( |
| 72 | immediate = true, |
| 73 | service = TopologyProvider.class, |
| 74 | property = { |
Ray Milkey | 2d7bca1 | 2018-10-17 14:51:52 -0700 | [diff] [blame] | 75 | DTP_MAX_EVENTS + ":Integer=" + DTP_MAX_EVENTS_DEFAULT, |
| 76 | DTP_MAX_IDLE_MS + ":Integer=" + DTP_MAX_IDLE_MS_DEFAULT, |
| 77 | DTP_MAX_BATCH_MS + ":Integer=" + DTP_MAX_BATCH_MS_DEFAULT |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 78 | } |
| 79 | ) |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 80 | public class DefaultTopologyProvider extends AbstractProvider |
| 81 | implements TopologyProvider { |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 82 | |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 83 | private static final int MAX_THREADS = 8; |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 84 | |
tom | 025e09f | 2014-09-15 15:29:24 -0700 | [diff] [blame] | 85 | // FIXME: Replace with a system-wide timer instance; |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 86 | // TODO: Convert to use HashedWheelTimer or produce a variant of that; then decide which we want to adopt |
Thomas Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame] | 87 | private static final Timer TIMER = new Timer("onos-topo-event-batching"); |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 88 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 89 | /** Maximum number of events to accumulate. */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 90 | private int maxEvents = DTP_MAX_EVENTS_DEFAULT; |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 91 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 92 | /** Maximum number of millis between events. */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 93 | private int maxIdleMs = DTP_MAX_IDLE_MS_DEFAULT; |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 94 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 95 | /** Maximum number of millis for whole batch. */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 96 | private int maxBatchMs = DTP_MAX_BATCH_MS_DEFAULT; |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 97 | |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 98 | private final Logger log = getLogger(getClass()); |
| 99 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 100 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 101 | protected TopologyProviderRegistry providerRegistry; |
| 102 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 103 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 104 | protected DeviceService deviceService; |
| 105 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 107 | protected LinkService linkService; |
| 108 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 109 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 110 | protected ComponentConfigService cfgService; |
| 111 | |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 112 | private volatile boolean isStarted = false; |
| 113 | |
| 114 | private TopologyProviderService providerService; |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 115 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
| 116 | private final LinkListener linkListener = new InternalLinkListener(); |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 117 | |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 118 | private Accumulator<Event> accumulator; |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 119 | private ExecutorService executor; |
| 120 | |
| 121 | /** |
| 122 | * Creates a provider with the supplier identifier. |
| 123 | */ |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 124 | public DefaultTopologyProvider() { |
Thomas Vachuska | 6acd3bb | 2014-11-09 23:44:22 -0800 | [diff] [blame] | 125 | super(CORE_PROVIDER_ID); |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Activate |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 129 | public synchronized void activate(ComponentContext context) { |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 130 | cfgService.registerProperties(DefaultTopologyProvider.class); |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 131 | executor = newFixedThreadPool(MAX_THREADS, groupedThreads("onos/topo", "build-%d", log)); |
Thomas Vachuska | 6b7920d | 2014-11-25 19:48:39 -0800 | [diff] [blame] | 132 | accumulator = new TopologyChangeAccumulator(); |
| 133 | logConfig("Configured"); |
| 134 | |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 135 | modified(context); |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 136 | |
| 137 | providerService = providerRegistry.register(this); |
| 138 | deviceService.addListener(deviceListener); |
| 139 | linkService.addListener(linkListener); |
| 140 | |
| 141 | isStarted = true; |
Thomas Vachuska | 0e752bd | 2014-10-22 22:33:41 -0700 | [diff] [blame] | 142 | triggerRecompute(); |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 143 | log.info("Started"); |
| 144 | } |
| 145 | |
| 146 | @Deactivate |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 147 | public synchronized void deactivate(ComponentContext context) { |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 148 | cfgService.unregisterProperties(DefaultTopologyProvider.class, false); |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 149 | isStarted = false; |
| 150 | |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 151 | deviceService.removeListener(deviceListener); |
| 152 | linkService.removeListener(linkListener); |
| 153 | providerRegistry.unregister(this); |
| 154 | providerService = null; |
| 155 | |
| 156 | executor.shutdownNow(); |
| 157 | executor = null; |
| 158 | |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 159 | log.info("Stopped"); |
| 160 | } |
| 161 | |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 162 | @Modified |
| 163 | public void modified(ComponentContext context) { |
| 164 | if (context == null) { |
| 165 | accumulator = new TopologyChangeAccumulator(); |
Thomas Vachuska | 6b7920d | 2014-11-25 19:48:39 -0800 | [diff] [blame] | 166 | logConfig("Reconfigured"); |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 167 | return; |
| 168 | } |
| 169 | |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 170 | Dictionary<?, ?> properties = context.getProperties(); |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 171 | int newMaxEvents, newMaxBatchMs, newMaxIdleMs; |
| 172 | try { |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 173 | String s = get(properties, DTP_MAX_EVENTS); |
Ayaka Koshibe | 8851ed9 | 2015-01-22 12:07:24 -0800 | [diff] [blame] | 174 | newMaxEvents = isNullOrEmpty(s) ? maxEvents : Integer.parseInt(s.trim()); |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 175 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 176 | s = get(properties, DTP_MAX_BATCH_MS); |
Ayaka Koshibe | 8851ed9 | 2015-01-22 12:07:24 -0800 | [diff] [blame] | 177 | newMaxBatchMs = isNullOrEmpty(s) ? maxBatchMs : Integer.parseInt(s.trim()); |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 178 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 179 | s = get(properties, DTP_MAX_IDLE_MS); |
Ayaka Koshibe | 8851ed9 | 2015-01-22 12:07:24 -0800 | [diff] [blame] | 180 | newMaxIdleMs = isNullOrEmpty(s) ? maxIdleMs : Integer.parseInt(s.trim()); |
Thomas Vachuska | 6b7920d | 2014-11-25 19:48:39 -0800 | [diff] [blame] | 181 | |
Pavlin Radoslavov | b9e50df | 2015-02-20 20:01:26 -0800 | [diff] [blame] | 182 | } catch (NumberFormatException | ClassCastException e) { |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 183 | newMaxEvents = DTP_MAX_EVENTS_DEFAULT; |
| 184 | newMaxBatchMs = DTP_MAX_BATCH_MS_DEFAULT; |
| 185 | newMaxIdleMs = DTP_MAX_IDLE_MS_DEFAULT; |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 188 | if (newMaxEvents != maxEvents || newMaxBatchMs != maxBatchMs || newMaxIdleMs != maxIdleMs) { |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 189 | maxEvents = newMaxEvents; |
| 190 | maxBatchMs = newMaxBatchMs; |
| 191 | maxIdleMs = newMaxIdleMs; |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 192 | accumulator = maxEvents > 1 ? new TopologyChangeAccumulator() : null; |
Thomas Vachuska | 6b7920d | 2014-11-25 19:48:39 -0800 | [diff] [blame] | 193 | logConfig("Reconfigured"); |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Thomas Vachuska | 6b7920d | 2014-11-25 19:48:39 -0800 | [diff] [blame] | 197 | private void logConfig(String prefix) { |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 198 | log.info("{} with maxEvents = {}; maxBatchMs = {}; maxIdleMs = {}; accumulator={}", |
| 199 | prefix, maxEvents, maxBatchMs, maxIdleMs, accumulator != null); |
Thomas Vachuska | 6b7920d | 2014-11-25 19:48:39 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 202 | |
Thomas Vachuska | 0e752bd | 2014-10-22 22:33:41 -0700 | [diff] [blame] | 203 | @Override |
| 204 | public void triggerRecompute() { |
Sho SHIMIZU | 21d0069 | 2016-08-15 11:15:28 -0700 | [diff] [blame] | 205 | triggerTopologyBuild(Collections.emptyList()); |
Thomas Vachuska | 0e752bd | 2014-10-22 22:33:41 -0700 | [diff] [blame] | 206 | } |
| 207 | |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 208 | /** |
| 209 | * Triggers assembly of topology data citing the specified events as the |
| 210 | * reason. |
| 211 | * |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 212 | * @param reasons events which triggered the topology change |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 213 | */ |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 214 | private synchronized void triggerTopologyBuild(List<Event> reasons) { |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 215 | if (executor != null) { |
| 216 | executor.execute(new TopologyBuilderTask(reasons)); |
| 217 | } |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | // Builds the topology using the latest device and link information |
| 221 | // and citing the specified events as reasons for the change. |
| 222 | private void buildTopology(List<Event> reasons) { |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 223 | if (isStarted) { |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 224 | GraphDescription desc = |
| 225 | new DefaultGraphDescription(System.nanoTime(), |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 226 | System.currentTimeMillis(), |
| 227 | deviceService.getAvailableDevices(), |
| 228 | linkService.getActiveLinks()); |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 229 | providerService.topologyChanged(desc, reasons); |
| 230 | } |
| 231 | } |
| 232 | |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 233 | private void processEvent(Event event) { |
| 234 | if (accumulator != null) { |
| 235 | accumulator.add(event); |
| 236 | } else { |
| 237 | triggerTopologyBuild(ImmutableList.of(event)); |
| 238 | } |
| 239 | } |
| 240 | |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 241 | // Callback for device events |
Ayaka Koshibe | 3de43ca | 2014-09-26 16:40:23 -0700 | [diff] [blame] | 242 | private class InternalDeviceListener implements DeviceListener { |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 243 | @Override |
| 244 | public void event(DeviceEvent event) { |
| 245 | DeviceEvent.Type type = event.type(); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 246 | if (type == DEVICE_ADDED || type == DEVICE_REMOVED || |
| 247 | type == DEVICE_AVAILABILITY_CHANGED) { |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 248 | processEvent(event); |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | // Callback for link events |
Ayaka Koshibe | 3de43ca | 2014-09-26 16:40:23 -0700 | [diff] [blame] | 254 | private class InternalLinkListener implements LinkListener { |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 255 | @Override |
| 256 | public void event(LinkEvent event) { |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 257 | processEvent(event); |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
| 261 | // Event accumulator for paced triggering of topology assembly. |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 262 | private class TopologyChangeAccumulator extends AbstractAccumulator<Event> { |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 263 | TopologyChangeAccumulator() { |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 264 | super(TIMER, maxEvents, maxBatchMs, maxIdleMs); |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | @Override |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 268 | public void processItems(List<Event> items) { |
| 269 | triggerTopologyBuild(items); |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 270 | } |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | // Task for building topology data in a separate thread. |
| 274 | private class TopologyBuilderTask implements Runnable { |
| 275 | private final List<Event> reasons; |
| 276 | |
| 277 | public TopologyBuilderTask(List<Event> reasons) { |
| 278 | this.reasons = reasons; |
| 279 | } |
| 280 | |
| 281 | @Override |
| 282 | public void run() { |
Thomas Vachuska | 0e752bd | 2014-10-22 22:33:41 -0700 | [diff] [blame] | 283 | try { |
| 284 | buildTopology(reasons); |
| 285 | } catch (Exception e) { |
Thomas Vachuska | 320c58f | 2015-08-05 10:42:32 -0700 | [diff] [blame] | 286 | log.warn("Unable to compute topology", e); |
Thomas Vachuska | 0e752bd | 2014-10-22 22:33:41 -0700 | [diff] [blame] | 287 | } |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
| 291 | } |