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