blob: ac9fd00e36b64de48286fe789165f9ea8bd822b5 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
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 */
tombe988312014-09-19 18:38:47 -070016package org.onlab.onos.net.topology.impl;
tomcbff9392014-09-10 00:45:23 -070017
Thomas Vachuska912bdd52014-11-17 11:39:01 -080018import com.google.common.collect.ImmutableList;
tomcbff9392014-09-10 00:45:23 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
Thomas Vachuska912bdd52014-11-17 11:39:01 -080022import org.apache.felix.scr.annotations.Modified;
23import org.apache.felix.scr.annotations.Property;
tomcbff9392014-09-10 00:45:23 -070024import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
Thomas Vachuska0e752bd2014-10-22 22:33:41 -070026import org.apache.felix.scr.annotations.Service;
tomcbff9392014-09-10 00:45:23 -070027import org.onlab.onos.event.AbstractEventAccumulator;
28import org.onlab.onos.event.Event;
29import org.onlab.onos.event.EventAccumulator;
30import org.onlab.onos.net.device.DeviceEvent;
31import org.onlab.onos.net.device.DeviceListener;
32import org.onlab.onos.net.device.DeviceService;
33import org.onlab.onos.net.link.LinkEvent;
34import org.onlab.onos.net.link.LinkListener;
35import org.onlab.onos.net.link.LinkService;
36import org.onlab.onos.net.provider.AbstractProvider;
tombe988312014-09-19 18:38:47 -070037import org.onlab.onos.net.topology.DefaultGraphDescription;
tom97937552014-09-11 10:48:42 -070038import org.onlab.onos.net.topology.GraphDescription;
tomcbff9392014-09-10 00:45:23 -070039import org.onlab.onos.net.topology.TopologyProvider;
40import org.onlab.onos.net.topology.TopologyProviderRegistry;
41import org.onlab.onos.net.topology.TopologyProviderService;
Thomas Vachuska912bdd52014-11-17 11:39:01 -080042import org.osgi.service.component.ComponentContext;
tomcbff9392014-09-10 00:45:23 -070043import org.slf4j.Logger;
44
Pavlin Radoslavova0e47542014-10-17 19:22:17 -070045import java.util.Collections;
Thomas Vachuska912bdd52014-11-17 11:39:01 -080046import java.util.Dictionary;
tomcbff9392014-09-10 00:45:23 -070047import java.util.List;
48import java.util.Timer;
49import java.util.concurrent.ExecutorService;
50
Thomas Vachuska912bdd52014-11-17 11:39:01 -080051import static com.google.common.base.Strings.isNullOrEmpty;
tomcbff9392014-09-10 00:45:23 -070052import static java.util.concurrent.Executors.newFixedThreadPool;
Thomas Vachuska6acd3bb2014-11-09 23:44:22 -080053import static org.onlab.onos.core.CoreService.CORE_PROVIDER_ID;
tomcbff9392014-09-10 00:45:23 -070054import static org.onlab.onos.net.device.DeviceEvent.Type.*;
55import static org.onlab.util.Tools.namedThreads;
56import static org.slf4j.LoggerFactory.getLogger;
57
58/**
tom578ebdc2014-09-11 11:12:51 -070059 * 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.
tomcbff9392014-09-10 00:45:23 -070062 */
63@Component(immediate = true)
Thomas Vachuska0e752bd2014-10-22 22:33:41 -070064@Service
tom97937552014-09-11 10:48:42 -070065public class DefaultTopologyProvider extends AbstractProvider
tomcbff9392014-09-10 00:45:23 -070066 implements TopologyProvider {
67
tomcbff9392014-09-10 00:45:23 -070068 private static final int MAX_THREADS = 8;
Thomas Vachuska912bdd52014-11-17 11:39:01 -080069 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;
tomcbff9392014-09-10 00:45:23 -070072
tom025e09f2014-09-15 15:29:24 -070073 // 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
tomcbff9392014-09-10 00:45:23 -070075 private static final Timer TIMER = new Timer();
76
Thomas Vachuska912bdd52014-11-17 11:39:01 -080077 @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
tomcbff9392014-09-10 00:45:23 -070089 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 Koshibe3de43ca2014-09-26 16:40:23 -0700103 private DeviceListener deviceListener = new InternalDeviceListener();
104 private LinkListener linkListener = new InternalLinkListener();
tomcbff9392014-09-10 00:45:23 -0700105
106 private EventAccumulator accumulator;
107 private ExecutorService executor;
108
109 /**
110 * Creates a provider with the supplier identifier.
111 */
tom97937552014-09-11 10:48:42 -0700112 public DefaultTopologyProvider() {
Thomas Vachuska6acd3bb2014-11-09 23:44:22 -0800113 super(CORE_PROVIDER_ID);
tomcbff9392014-09-10 00:45:23 -0700114 }
115
116 @Activate
Thomas Vachuska912bdd52014-11-17 11:39:01 -0800117 public synchronized void activate(ComponentContext context) {
tom578ebdc2014-09-11 11:12:51 -0700118 executor = newFixedThreadPool(MAX_THREADS, namedThreads("topo-build-%d"));
Thomas Vachuska912bdd52014-11-17 11:39:01 -0800119 modified(context);
tomcbff9392014-09-10 00:45:23 -0700120
121 providerService = providerRegistry.register(this);
122 deviceService.addListener(deviceListener);
123 linkService.addListener(linkListener);
124
125 isStarted = true;
Thomas Vachuska0e752bd2014-10-22 22:33:41 -0700126 triggerRecompute();
tomcbff9392014-09-10 00:45:23 -0700127 log.info("Started");
128 }
129
130 @Deactivate
Thomas Vachuska912bdd52014-11-17 11:39:01 -0800131 public synchronized void deactivate(ComponentContext context) {
tome52ce702014-09-11 00:12:54 -0700132 isStarted = false;
133
tomcbff9392014-09-10 00:45:23 -0700134 deviceService.removeListener(deviceListener);
135 linkService.removeListener(linkListener);
136 providerRegistry.unregister(this);
137 providerService = null;
138
139 executor.shutdownNow();
140 executor = null;
141
tomcbff9392014-09-10 00:45:23 -0700142 log.info("Stopped");
143 }
144
Thomas Vachuska912bdd52014-11-17 11:39:01 -0800145 @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 Vachuska0e752bd2014-10-22 22:33:41 -0700180 @Override
181 public void triggerRecompute() {
182 triggerTopologyBuild(Collections.<Event>emptyList());
183 }
184
tomcbff9392014-09-10 00:45:23 -0700185 /**
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 */
tome52ce702014-09-11 00:12:54 -0700191 private synchronized void triggerTopologyBuild(List<Event> reasons) {
tom97937552014-09-11 10:48:42 -0700192 if (executor != null) {
193 executor.execute(new TopologyBuilderTask(reasons));
194 }
tomcbff9392014-09-10 00:45:23 -0700195 }
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) {
tomcbff9392014-09-10 00:45:23 -0700200 if (isStarted) {
tom97937552014-09-11 10:48:42 -0700201 GraphDescription desc =
202 new DefaultGraphDescription(System.nanoTime(),
203 deviceService.getDevices(),
204 linkService.getLinks());
tomcbff9392014-09-10 00:45:23 -0700205 providerService.topologyChanged(desc, reasons);
206 }
207 }
208
Thomas Vachuska912bdd52014-11-17 11:39:01 -0800209 private void processEvent(Event event) {
210 if (accumulator != null) {
211 accumulator.add(event);
212 } else {
213 triggerTopologyBuild(ImmutableList.of(event));
214 }
215 }
216
tomcbff9392014-09-10 00:45:23 -0700217 // Callback for device events
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -0700218 private class InternalDeviceListener implements DeviceListener {
tomcbff9392014-09-10 00:45:23 -0700219 @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 Vachuska912bdd52014-11-17 11:39:01 -0800224 processEvent(event);
tomcbff9392014-09-10 00:45:23 -0700225 }
226 }
227 }
228
229 // Callback for link events
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -0700230 private class InternalLinkListener implements LinkListener {
tomcbff9392014-09-10 00:45:23 -0700231 @Override
232 public void event(LinkEvent event) {
Thomas Vachuska912bdd52014-11-17 11:39:01 -0800233 processEvent(event);
tomcbff9392014-09-10 00:45:23 -0700234 }
235 }
236
237 // Event accumulator for paced triggering of topology assembly.
238 private class TopologyChangeAccumulator
239 extends AbstractEventAccumulator implements EventAccumulator {
240
241 TopologyChangeAccumulator() {
Thomas Vachuska912bdd52014-11-17 11:39:01 -0800242 super(TIMER, maxEvents, maxBatchMs, maxIdleMs);
tomcbff9392014-09-10 00:45:23 -0700243 }
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 Vachuska0e752bd2014-10-22 22:33:41 -0700262 try {
263 buildTopology(reasons);
264 } catch (Exception e) {
265 log.warn("Unable to compute topology due to: {}", e.getMessage());
Yuta HIGUCHI22102822014-11-12 23:09:59 -0800266 log.debug("Unable to compute topology", e);
Thomas Vachuska0e752bd2014-10-22 22:33:41 -0700267 }
tomcbff9392014-09-10 00:45:23 -0700268 }
269 }
270
271}