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 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Thomas Vachuska | 0e752bd | 2014-10-22 22:33:41 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Service; |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 24 | import org.onlab.onos.event.AbstractEventAccumulator; |
| 25 | import org.onlab.onos.event.Event; |
| 26 | import org.onlab.onos.event.EventAccumulator; |
| 27 | import org.onlab.onos.net.device.DeviceEvent; |
| 28 | import org.onlab.onos.net.device.DeviceListener; |
| 29 | import org.onlab.onos.net.device.DeviceService; |
| 30 | import org.onlab.onos.net.link.LinkEvent; |
| 31 | import org.onlab.onos.net.link.LinkListener; |
| 32 | import org.onlab.onos.net.link.LinkService; |
| 33 | import org.onlab.onos.net.provider.AbstractProvider; |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 34 | import org.onlab.onos.net.topology.DefaultGraphDescription; |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 35 | import org.onlab.onos.net.topology.GraphDescription; |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 36 | import org.onlab.onos.net.topology.TopologyProvider; |
| 37 | import org.onlab.onos.net.topology.TopologyProviderRegistry; |
| 38 | import org.onlab.onos.net.topology.TopologyProviderService; |
| 39 | import org.slf4j.Logger; |
| 40 | |
Pavlin Radoslavov | a0e4754 | 2014-10-17 19:22:17 -0700 | [diff] [blame] | 41 | import java.util.Collections; |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 42 | import java.util.List; |
| 43 | import java.util.Timer; |
| 44 | import java.util.concurrent.ExecutorService; |
| 45 | |
| 46 | import static java.util.concurrent.Executors.newFixedThreadPool; |
Thomas Vachuska | 6acd3bb | 2014-11-09 23:44:22 -0800 | [diff] [blame] | 47 | import static org.onlab.onos.core.CoreService.CORE_PROVIDER_ID; |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 48 | import static org.onlab.onos.net.device.DeviceEvent.Type.*; |
| 49 | import static org.onlab.util.Tools.namedThreads; |
| 50 | import static org.slf4j.LoggerFactory.getLogger; |
| 51 | |
| 52 | /** |
tom | 578ebdc | 2014-09-11 11:12:51 -0700 | [diff] [blame] | 53 | * Default implementation of a network topology provider that feeds off |
| 54 | * device and link subsystem events to trigger assembly and computation of |
| 55 | * new topology snapshots. |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 56 | */ |
| 57 | @Component(immediate = true) |
Thomas Vachuska | 0e752bd | 2014-10-22 22:33:41 -0700 | [diff] [blame] | 58 | @Service |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 59 | public class DefaultTopologyProvider extends AbstractProvider |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 60 | implements TopologyProvider { |
| 61 | |
| 62 | // TODO: make these configurable |
| 63 | private static final int MAX_EVENTS = 100; |
Thomas Vachuska | 10ac63b | 2014-11-07 15:33:22 -0800 | [diff] [blame] | 64 | private static final int MAX_IDLE_MS = 5; |
| 65 | private static final int MAX_BATCH_MS = 50; |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 66 | private static final int MAX_THREADS = 8; |
| 67 | |
tom | 025e09f | 2014-09-15 15:29:24 -0700 | [diff] [blame] | 68 | // FIXME: Replace with a system-wide timer instance; |
| 69 | // 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] | 70 | private static final Timer TIMER = new Timer(); |
| 71 | |
| 72 | private final Logger log = getLogger(getClass()); |
| 73 | |
| 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 75 | protected TopologyProviderRegistry providerRegistry; |
| 76 | |
| 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 78 | protected DeviceService deviceService; |
| 79 | |
| 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 81 | protected LinkService linkService; |
| 82 | |
| 83 | private volatile boolean isStarted = false; |
| 84 | |
| 85 | private TopologyProviderService providerService; |
Ayaka Koshibe | 3de43ca | 2014-09-26 16:40:23 -0700 | [diff] [blame] | 86 | private DeviceListener deviceListener = new InternalDeviceListener(); |
| 87 | private LinkListener linkListener = new InternalLinkListener(); |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 88 | |
| 89 | private EventAccumulator accumulator; |
| 90 | private ExecutorService executor; |
| 91 | |
| 92 | /** |
| 93 | * Creates a provider with the supplier identifier. |
| 94 | */ |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 95 | public DefaultTopologyProvider() { |
Thomas Vachuska | 6acd3bb | 2014-11-09 23:44:22 -0800 | [diff] [blame] | 96 | super(CORE_PROVIDER_ID); |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Activate |
| 100 | public synchronized void activate() { |
tom | 578ebdc | 2014-09-11 11:12:51 -0700 | [diff] [blame] | 101 | executor = newFixedThreadPool(MAX_THREADS, namedThreads("topo-build-%d")); |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 102 | accumulator = new TopologyChangeAccumulator(); |
| 103 | |
| 104 | providerService = providerRegistry.register(this); |
| 105 | deviceService.addListener(deviceListener); |
| 106 | linkService.addListener(linkListener); |
| 107 | |
| 108 | isStarted = true; |
Thomas Vachuska | 0e752bd | 2014-10-22 22:33:41 -0700 | [diff] [blame] | 109 | triggerRecompute(); |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 110 | log.info("Started"); |
| 111 | } |
| 112 | |
| 113 | @Deactivate |
| 114 | public synchronized void deactivate() { |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 115 | isStarted = false; |
| 116 | |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 117 | deviceService.removeListener(deviceListener); |
| 118 | linkService.removeListener(linkListener); |
| 119 | providerRegistry.unregister(this); |
| 120 | providerService = null; |
| 121 | |
| 122 | executor.shutdownNow(); |
| 123 | executor = null; |
| 124 | |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 125 | log.info("Stopped"); |
| 126 | } |
| 127 | |
Thomas Vachuska | 0e752bd | 2014-10-22 22:33:41 -0700 | [diff] [blame] | 128 | @Override |
| 129 | public void triggerRecompute() { |
| 130 | triggerTopologyBuild(Collections.<Event>emptyList()); |
| 131 | } |
| 132 | |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 133 | /** |
| 134 | * Triggers assembly of topology data citing the specified events as the |
| 135 | * reason. |
| 136 | * |
| 137 | * @param reasons events which triggered the topology change |
| 138 | */ |
tom | e52ce70 | 2014-09-11 00:12:54 -0700 | [diff] [blame] | 139 | private synchronized void triggerTopologyBuild(List<Event> reasons) { |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 140 | if (executor != null) { |
| 141 | executor.execute(new TopologyBuilderTask(reasons)); |
| 142 | } |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | // Builds the topology using the latest device and link information |
| 146 | // and citing the specified events as reasons for the change. |
| 147 | private void buildTopology(List<Event> reasons) { |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 148 | if (isStarted) { |
tom | 9793755 | 2014-09-11 10:48:42 -0700 | [diff] [blame] | 149 | GraphDescription desc = |
| 150 | new DefaultGraphDescription(System.nanoTime(), |
| 151 | deviceService.getDevices(), |
| 152 | linkService.getLinks()); |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 153 | providerService.topologyChanged(desc, reasons); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // Callback for device events |
Ayaka Koshibe | 3de43ca | 2014-09-26 16:40:23 -0700 | [diff] [blame] | 158 | private class InternalDeviceListener implements DeviceListener { |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 159 | @Override |
| 160 | public void event(DeviceEvent event) { |
| 161 | DeviceEvent.Type type = event.type(); |
| 162 | if (type == DEVICE_ADDED || type == DEVICE_REMOVED || |
| 163 | type == DEVICE_AVAILABILITY_CHANGED) { |
| 164 | accumulator.add(event); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | // Callback for link events |
Ayaka Koshibe | 3de43ca | 2014-09-26 16:40:23 -0700 | [diff] [blame] | 170 | private class InternalLinkListener implements LinkListener { |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 171 | @Override |
| 172 | public void event(LinkEvent event) { |
| 173 | accumulator.add(event); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // Event accumulator for paced triggering of topology assembly. |
| 178 | private class TopologyChangeAccumulator |
| 179 | extends AbstractEventAccumulator implements EventAccumulator { |
| 180 | |
| 181 | TopologyChangeAccumulator() { |
| 182 | super(TIMER, MAX_EVENTS, MAX_BATCH_MS, MAX_IDLE_MS); |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public void processEvents(List<Event> events) { |
| 187 | triggerTopologyBuild(events); |
| 188 | } |
| 189 | |
| 190 | } |
| 191 | |
| 192 | // Task for building topology data in a separate thread. |
| 193 | private class TopologyBuilderTask implements Runnable { |
| 194 | private final List<Event> reasons; |
| 195 | |
| 196 | public TopologyBuilderTask(List<Event> reasons) { |
| 197 | this.reasons = reasons; |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public void run() { |
Thomas Vachuska | 0e752bd | 2014-10-22 22:33:41 -0700 | [diff] [blame] | 202 | try { |
| 203 | buildTopology(reasons); |
| 204 | } catch (Exception e) { |
| 205 | log.warn("Unable to compute topology due to: {}", e.getMessage()); |
Yuta HIGUCHI | 2210282 | 2014-11-12 23:09:59 -0800 | [diff] [blame] | 206 | log.debug("Unable to compute topology", e); |
Thomas Vachuska | 0e752bd | 2014-10-22 22:33:41 -0700 | [diff] [blame] | 207 | } |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
| 211 | } |