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