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