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 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 89 | //@Property(name = "maxEvents", intValue = DEFAULT_MAX_EVENTS, |
| 90 | // label = "Maximum number of events to accumulate") |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 91 | private int maxEvents = DTP_MAX_EVENTS_DEFAULT; |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 92 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 93 | //@Property(name = "maxIdleMs", intValue = DEFAULT_MAX_IDLE_MS, |
| 94 | // label = "Maximum number of millis between events") |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 95 | private int maxIdleMs = DTP_MAX_IDLE_MS_DEFAULT; |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 96 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 97 | //@Property(name = "maxBatchMs", intValue = DEFAULT_MAX_BATCH_MS, |
| 98 | // label = "Maximum number of millis for whole batch") |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 99 | private int maxBatchMs = DTP_MAX_BATCH_MS_DEFAULT; |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 100 | |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 101 | private final Logger log = getLogger(getClass()); |
| 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 TopologyProviderRegistry providerRegistry; |
| 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 DeviceService deviceService; |
| 108 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 109 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 110 | protected LinkService linkService; |
| 111 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 112 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 113 | protected ComponentConfigService cfgService; |
| 114 | |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 115 | private volatile boolean isStarted = false; |
| 116 | |
| 117 | private TopologyProviderService providerService; |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 118 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
| 119 | private final LinkListener linkListener = new InternalLinkListener(); |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 120 | |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 121 | private Accumulator<Event> accumulator; |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 122 | private ExecutorService executor; |
| 123 | |
| 124 | /** |
| 125 | * Creates a provider with the supplier identifier. |
| 126 | */ |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 127 | public DefaultTopologyProvider() { |
Thomas Vachuska | 6acd3bb | 2014-11-09 23:44:22 -0800 | [diff] [blame] | 128 | super(CORE_PROVIDER_ID); |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Activate |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 132 | public synchronized void activate(ComponentContext context) { |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 133 | cfgService.registerProperties(DefaultTopologyProvider.class); |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 134 | executor = newFixedThreadPool(MAX_THREADS, groupedThreads("onos/topo", "build-%d", log)); |
Thomas Vachuska | 6b7920d | 2014-11-25 19:48:39 -0800 | [diff] [blame] | 135 | accumulator = new TopologyChangeAccumulator(); |
| 136 | logConfig("Configured"); |
| 137 | |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 138 | modified(context); |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 139 | |
| 140 | providerService = providerRegistry.register(this); |
| 141 | deviceService.addListener(deviceListener); |
| 142 | linkService.addListener(linkListener); |
| 143 | |
| 144 | isStarted = true; |
Thomas Vachuska | 0e752bd | 2014-10-22 22:33:41 -0700 | [diff] [blame] | 145 | triggerRecompute(); |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 146 | log.info("Started"); |
| 147 | } |
| 148 | |
| 149 | @Deactivate |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 150 | public synchronized void deactivate(ComponentContext context) { |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 151 | cfgService.unregisterProperties(DefaultTopologyProvider.class, false); |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 152 | isStarted = false; |
| 153 | |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 154 | deviceService.removeListener(deviceListener); |
| 155 | linkService.removeListener(linkListener); |
| 156 | providerRegistry.unregister(this); |
| 157 | providerService = null; |
| 158 | |
| 159 | executor.shutdownNow(); |
| 160 | executor = null; |
| 161 | |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 162 | log.info("Stopped"); |
| 163 | } |
| 164 | |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 165 | @Modified |
| 166 | public void modified(ComponentContext context) { |
| 167 | if (context == null) { |
| 168 | accumulator = new TopologyChangeAccumulator(); |
Thomas Vachuska | 6b7920d | 2014-11-25 19:48:39 -0800 | [diff] [blame] | 169 | logConfig("Reconfigured"); |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 170 | return; |
| 171 | } |
| 172 | |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 173 | Dictionary<?, ?> properties = context.getProperties(); |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 174 | int newMaxEvents, newMaxBatchMs, newMaxIdleMs; |
| 175 | try { |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 176 | String s = get(properties, "maxEvents"); |
Ayaka Koshibe | 8851ed9 | 2015-01-22 12:07:24 -0800 | [diff] [blame] | 177 | newMaxEvents = isNullOrEmpty(s) ? maxEvents : Integer.parseInt(s.trim()); |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 178 | |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 179 | s = get(properties, "maxBatchMs"); |
Ayaka Koshibe | 8851ed9 | 2015-01-22 12:07:24 -0800 | [diff] [blame] | 180 | newMaxBatchMs = isNullOrEmpty(s) ? maxBatchMs : Integer.parseInt(s.trim()); |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 181 | |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 182 | s = get(properties, "maxIdleMs"); |
Ayaka Koshibe | 8851ed9 | 2015-01-22 12:07:24 -0800 | [diff] [blame] | 183 | newMaxIdleMs = isNullOrEmpty(s) ? maxIdleMs : Integer.parseInt(s.trim()); |
Thomas Vachuska | 6b7920d | 2014-11-25 19:48:39 -0800 | [diff] [blame] | 184 | |
Pavlin Radoslavov | b9e50df | 2015-02-20 20:01:26 -0800 | [diff] [blame] | 185 | } catch (NumberFormatException | ClassCastException e) { |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 186 | newMaxEvents = DTP_MAX_EVENTS_DEFAULT; |
| 187 | newMaxBatchMs = DTP_MAX_BATCH_MS_DEFAULT; |
| 188 | newMaxIdleMs = DTP_MAX_IDLE_MS_DEFAULT; |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 191 | if (newMaxEvents != maxEvents || newMaxBatchMs != maxBatchMs || newMaxIdleMs != maxIdleMs) { |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 192 | maxEvents = newMaxEvents; |
| 193 | maxBatchMs = newMaxBatchMs; |
| 194 | maxIdleMs = newMaxIdleMs; |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 195 | accumulator = maxEvents > 1 ? new TopologyChangeAccumulator() : null; |
Thomas Vachuska | 6b7920d | 2014-11-25 19:48:39 -0800 | [diff] [blame] | 196 | logConfig("Reconfigured"); |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
Thomas Vachuska | 6b7920d | 2014-11-25 19:48:39 -0800 | [diff] [blame] | 200 | private void logConfig(String prefix) { |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 201 | log.info("{} with maxEvents = {}; maxBatchMs = {}; maxIdleMs = {}; accumulator={}", |
| 202 | prefix, maxEvents, maxBatchMs, maxIdleMs, accumulator != null); |
Thomas Vachuska | 6b7920d | 2014-11-25 19:48:39 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 205 | |
Thomas Vachuska | 0e752bd | 2014-10-22 22:33:41 -0700 | [diff] [blame] | 206 | @Override |
| 207 | public void triggerRecompute() { |
Sho SHIMIZU | 21d0069 | 2016-08-15 11:15:28 -0700 | [diff] [blame] | 208 | triggerTopologyBuild(Collections.emptyList()); |
Thomas Vachuska | 0e752bd | 2014-10-22 22:33:41 -0700 | [diff] [blame] | 209 | } |
| 210 | |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 211 | /** |
| 212 | * Triggers assembly of topology data citing the specified events as the |
| 213 | * reason. |
| 214 | * |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 215 | * @param reasons events which triggered the topology change |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 216 | */ |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 217 | private synchronized void triggerTopologyBuild(List<Event> reasons) { |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 218 | if (executor != null) { |
| 219 | executor.execute(new TopologyBuilderTask(reasons)); |
| 220 | } |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | // Builds the topology using the latest device and link information |
| 224 | // and citing the specified events as reasons for the change. |
| 225 | private void buildTopology(List<Event> reasons) { |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 226 | if (isStarted) { |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 227 | GraphDescription desc = |
| 228 | new DefaultGraphDescription(System.nanoTime(), |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 229 | System.currentTimeMillis(), |
| 230 | deviceService.getAvailableDevices(), |
| 231 | linkService.getActiveLinks()); |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 232 | providerService.topologyChanged(desc, reasons); |
| 233 | } |
| 234 | } |
| 235 | |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 236 | private void processEvent(Event event) { |
| 237 | if (accumulator != null) { |
| 238 | accumulator.add(event); |
| 239 | } else { |
| 240 | triggerTopologyBuild(ImmutableList.of(event)); |
| 241 | } |
| 242 | } |
| 243 | |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 244 | // Callback for device events |
Ayaka Koshibe | 3de43ca | 2014-09-26 16:40:23 -0700 | [diff] [blame] | 245 | private class InternalDeviceListener implements DeviceListener { |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 246 | @Override |
| 247 | public void event(DeviceEvent event) { |
| 248 | DeviceEvent.Type type = event.type(); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 249 | if (type == DEVICE_ADDED || type == DEVICE_REMOVED || |
| 250 | type == DEVICE_AVAILABILITY_CHANGED) { |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 251 | processEvent(event); |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | // Callback for link events |
Ayaka Koshibe | 3de43ca | 2014-09-26 16:40:23 -0700 | [diff] [blame] | 257 | private class InternalLinkListener implements LinkListener { |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 258 | @Override |
| 259 | public void event(LinkEvent event) { |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 260 | processEvent(event); |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
| 264 | // Event accumulator for paced triggering of topology assembly. |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 265 | private class TopologyChangeAccumulator extends AbstractAccumulator<Event> { |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 266 | TopologyChangeAccumulator() { |
Thomas Vachuska | 912bdd5 | 2014-11-17 11:39:01 -0800 | [diff] [blame] | 267 | super(TIMER, maxEvents, maxBatchMs, maxIdleMs); |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | @Override |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 271 | public void processItems(List<Event> items) { |
| 272 | triggerTopologyBuild(items); |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 273 | } |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | // Task for building topology data in a separate thread. |
| 277 | private class TopologyBuilderTask implements Runnable { |
| 278 | private final List<Event> reasons; |
| 279 | |
| 280 | public TopologyBuilderTask(List<Event> reasons) { |
| 281 | this.reasons = reasons; |
| 282 | } |
| 283 | |
| 284 | @Override |
| 285 | public void run() { |
Thomas Vachuska | 0e752bd | 2014-10-22 22:33:41 -0700 | [diff] [blame] | 286 | try { |
| 287 | buildTopology(reasons); |
| 288 | } catch (Exception e) { |
Thomas Vachuska | 320c58f | 2015-08-05 10:42:32 -0700 | [diff] [blame] | 289 | log.warn("Unable to compute topology", e); |
Thomas Vachuska | 0e752bd | 2014-10-22 22:33:41 -0700 | [diff] [blame] | 290 | } |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | } |