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