blob: 5321a3e7c831b804c96d281e959d0ece8498eb2b [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.topology.impl;
tomcbff9392014-09-10 00:45:23 -070017
Ray Milkeyd84f89b2018-08-17 14:54:17 -070018import com.google.common.collect.ImmutableList;
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080019import org.onlab.util.AbstractAccumulator;
20import org.onlab.util.Accumulator;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070021import org.onosproject.cfg.ComponentConfigService;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.event.Event;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.net.device.DeviceEvent;
24import org.onosproject.net.device.DeviceListener;
25import org.onosproject.net.device.DeviceService;
26import org.onosproject.net.link.LinkEvent;
27import org.onosproject.net.link.LinkListener;
28import org.onosproject.net.link.LinkService;
29import org.onosproject.net.provider.AbstractProvider;
30import org.onosproject.net.topology.DefaultGraphDescription;
31import org.onosproject.net.topology.GraphDescription;
32import org.onosproject.net.topology.TopologyProvider;
33import org.onosproject.net.topology.TopologyProviderRegistry;
34import org.onosproject.net.topology.TopologyProviderService;
Thomas Vachuska912bdd52014-11-17 11:39:01 -080035import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070036import org.osgi.service.component.annotations.Activate;
37import org.osgi.service.component.annotations.Component;
38import org.osgi.service.component.annotations.Deactivate;
39import org.osgi.service.component.annotations.Modified;
40import org.osgi.service.component.annotations.Reference;
41import org.osgi.service.component.annotations.ReferenceCardinality;
tomcbff9392014-09-10 00:45:23 -070042import org.slf4j.Logger;
43
Ray Milkeyd84f89b2018-08-17 14:54:17 -070044import java.util.Collections;
45import java.util.Dictionary;
46import java.util.List;
47import java.util.Timer;
48import java.util.concurrent.ExecutorService;
49
50import static com.google.common.base.Strings.isNullOrEmpty;
51import static java.util.concurrent.Executors.newFixedThreadPool;
52import static org.onlab.util.Tools.get;
53import static org.onlab.util.Tools.groupedThreads;
54import static org.onosproject.core.CoreService.CORE_PROVIDER_ID;
Ray Milkeyd04e2272018-10-16 18:20:18 -070055import static org.onosproject.net.OsgiPropertyConstants.DTP_MAX_BATCH_MS;
56import static org.onosproject.net.OsgiPropertyConstants.DTP_MAX_BATCH_MS_DEFAULT;
57import static org.onosproject.net.OsgiPropertyConstants.DTP_MAX_EVENTS;
58import static org.onosproject.net.OsgiPropertyConstants.DTP_MAX_EVENTS_DEFAULT;
59import static org.onosproject.net.OsgiPropertyConstants.DTP_MAX_IDLE_MS;
60import static org.onosproject.net.OsgiPropertyConstants.DTP_MAX_IDLE_MS_DEFAULT;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070061import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED;
62import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED;
63import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_REMOVED;
64import static org.slf4j.LoggerFactory.getLogger;
tomcbff9392014-09-10 00:45:23 -070065
66/**
Thomas Vachuska6d697f12015-03-08 20:59:50 -070067 * Default implementation of a network topology provider that feeds off
68 * device and link subsystem events to trigger assembly and computation of
69 * new topology snapshots.
tomcbff9392014-09-10 00:45:23 -070070 */
Ray Milkeyd04e2272018-10-16 18:20:18 -070071@Component(
72 immediate = true,
73 service = TopologyProvider.class,
74 property = {
Ray Milkey2d7bca12018-10-17 14:51:52 -070075 DTP_MAX_EVENTS + ":Integer=" + DTP_MAX_EVENTS_DEFAULT,
76 DTP_MAX_IDLE_MS + ":Integer=" + DTP_MAX_IDLE_MS_DEFAULT,
77 DTP_MAX_BATCH_MS + ":Integer=" + DTP_MAX_BATCH_MS_DEFAULT
Ray Milkeyd04e2272018-10-16 18:20:18 -070078 }
79)
Thomas Vachuska6d697f12015-03-08 20:59:50 -070080public class DefaultTopologyProvider extends AbstractProvider
81 implements TopologyProvider {
tomcbff9392014-09-10 00:45:23 -070082
Thomas Vachuskaa132e3a2015-02-21 01:53:14 -080083 private static final int MAX_THREADS = 8;
tomcbff9392014-09-10 00:45:23 -070084
tom025e09f2014-09-15 15:29:24 -070085 // FIXME: Replace with a system-wide timer instance;
Thomas Vachuska6d697f12015-03-08 20:59:50 -070086 // TODO: Convert to use HashedWheelTimer or produce a variant of that; then decide which we want to adopt
Thomas Vachuskaa132e3a2015-02-21 01:53:14 -080087 private static final Timer TIMER = new Timer("onos-topo-event-batching");
tomcbff9392014-09-10 00:45:23 -070088
Thomas Vachuskaf566fa22018-10-30 14:03:36 -070089 /** Maximum number of events to accumulate. */
Ray Milkeyd04e2272018-10-16 18:20:18 -070090 private int maxEvents = DTP_MAX_EVENTS_DEFAULT;
Thomas Vachuska912bdd52014-11-17 11:39:01 -080091
Thomas Vachuskaf566fa22018-10-30 14:03:36 -070092 /** Maximum number of millis between events. */
Ray Milkeyd04e2272018-10-16 18:20:18 -070093 private int maxIdleMs = DTP_MAX_IDLE_MS_DEFAULT;
Thomas Vachuska912bdd52014-11-17 11:39:01 -080094
Thomas Vachuskaf566fa22018-10-30 14:03:36 -070095 /** Maximum number of millis for whole batch. */
Ray Milkeyd04e2272018-10-16 18:20:18 -070096 private int maxBatchMs = DTP_MAX_BATCH_MS_DEFAULT;
Thomas Vachuska912bdd52014-11-17 11:39:01 -080097
tomcbff9392014-09-10 00:45:23 -070098 private final Logger log = getLogger(getClass());
99
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700100 @Reference(cardinality = ReferenceCardinality.MANDATORY)
tomcbff9392014-09-10 00:45:23 -0700101 protected TopologyProviderRegistry providerRegistry;
102
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700103 @Reference(cardinality = ReferenceCardinality.MANDATORY)
tomcbff9392014-09-10 00:45:23 -0700104 protected DeviceService deviceService;
105
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700106 @Reference(cardinality = ReferenceCardinality.MANDATORY)
tomcbff9392014-09-10 00:45:23 -0700107 protected LinkService linkService;
108
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700109 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700110 protected ComponentConfigService cfgService;
111
tomcbff9392014-09-10 00:45:23 -0700112 private volatile boolean isStarted = false;
113
114 private TopologyProviderService providerService;
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -0500115 private final DeviceListener deviceListener = new InternalDeviceListener();
116 private final LinkListener linkListener = new InternalLinkListener();
tomcbff9392014-09-10 00:45:23 -0700117
Thomas Vachuskaecb63c52015-02-19 10:03:48 -0800118 private Accumulator<Event> accumulator;
tomcbff9392014-09-10 00:45:23 -0700119 private ExecutorService executor;
120
121 /**
122 * Creates a provider with the supplier identifier.
123 */
tom97937552014-09-11 10:48:42 -0700124 public DefaultTopologyProvider() {
Thomas Vachuska6acd3bb2014-11-09 23:44:22 -0800125 super(CORE_PROVIDER_ID);
tomcbff9392014-09-10 00:45:23 -0700126 }
127
128 @Activate
Thomas Vachuska912bdd52014-11-17 11:39:01 -0800129 public synchronized void activate(ComponentContext context) {
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700130 cfgService.registerProperties(DefaultTopologyProvider.class);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700131 executor = newFixedThreadPool(MAX_THREADS, groupedThreads("onos/topo", "build-%d", log));
Thomas Vachuska6b7920d2014-11-25 19:48:39 -0800132 accumulator = new TopologyChangeAccumulator();
133 logConfig("Configured");
134
Thomas Vachuska912bdd52014-11-17 11:39:01 -0800135 modified(context);
tomcbff9392014-09-10 00:45:23 -0700136
137 providerService = providerRegistry.register(this);
138 deviceService.addListener(deviceListener);
139 linkService.addListener(linkListener);
140
141 isStarted = true;
Thomas Vachuska0e752bd2014-10-22 22:33:41 -0700142 triggerRecompute();
tomcbff9392014-09-10 00:45:23 -0700143 log.info("Started");
144 }
145
146 @Deactivate
Thomas Vachuska912bdd52014-11-17 11:39:01 -0800147 public synchronized void deactivate(ComponentContext context) {
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700148 cfgService.unregisterProperties(DefaultTopologyProvider.class, false);
tome52ce702014-09-11 00:12:54 -0700149 isStarted = false;
150
tomcbff9392014-09-10 00:45:23 -0700151 deviceService.removeListener(deviceListener);
152 linkService.removeListener(linkListener);
153 providerRegistry.unregister(this);
154 providerService = null;
155
156 executor.shutdownNow();
157 executor = null;
158
tomcbff9392014-09-10 00:45:23 -0700159 log.info("Stopped");
160 }
161
Thomas Vachuska912bdd52014-11-17 11:39:01 -0800162 @Modified
163 public void modified(ComponentContext context) {
164 if (context == null) {
165 accumulator = new TopologyChangeAccumulator();
Thomas Vachuska6b7920d2014-11-25 19:48:39 -0800166 logConfig("Reconfigured");
Thomas Vachuska912bdd52014-11-17 11:39:01 -0800167 return;
168 }
169
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700170 Dictionary<?, ?> properties = context.getProperties();
Thomas Vachuska912bdd52014-11-17 11:39:01 -0800171 int newMaxEvents, newMaxBatchMs, newMaxIdleMs;
172 try {
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700173 String s = get(properties, DTP_MAX_EVENTS);
Ayaka Koshibe8851ed92015-01-22 12:07:24 -0800174 newMaxEvents = isNullOrEmpty(s) ? maxEvents : Integer.parseInt(s.trim());
Thomas Vachuska912bdd52014-11-17 11:39:01 -0800175
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700176 s = get(properties, DTP_MAX_BATCH_MS);
Ayaka Koshibe8851ed92015-01-22 12:07:24 -0800177 newMaxBatchMs = isNullOrEmpty(s) ? maxBatchMs : Integer.parseInt(s.trim());
Thomas Vachuska912bdd52014-11-17 11:39:01 -0800178
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700179 s = get(properties, DTP_MAX_IDLE_MS);
Ayaka Koshibe8851ed92015-01-22 12:07:24 -0800180 newMaxIdleMs = isNullOrEmpty(s) ? maxIdleMs : Integer.parseInt(s.trim());
Thomas Vachuska6b7920d2014-11-25 19:48:39 -0800181
Pavlin Radoslavovb9e50df2015-02-20 20:01:26 -0800182 } catch (NumberFormatException | ClassCastException e) {
Ray Milkeyd04e2272018-10-16 18:20:18 -0700183 newMaxEvents = DTP_MAX_EVENTS_DEFAULT;
184 newMaxBatchMs = DTP_MAX_BATCH_MS_DEFAULT;
185 newMaxIdleMs = DTP_MAX_IDLE_MS_DEFAULT;
Thomas Vachuska912bdd52014-11-17 11:39:01 -0800186 }
187
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700188 if (newMaxEvents != maxEvents || newMaxBatchMs != maxBatchMs || newMaxIdleMs != maxIdleMs) {
Thomas Vachuska912bdd52014-11-17 11:39:01 -0800189 maxEvents = newMaxEvents;
190 maxBatchMs = newMaxBatchMs;
191 maxIdleMs = newMaxIdleMs;
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700192 accumulator = maxEvents > 1 ? new TopologyChangeAccumulator() : null;
Thomas Vachuska6b7920d2014-11-25 19:48:39 -0800193 logConfig("Reconfigured");
Thomas Vachuska912bdd52014-11-17 11:39:01 -0800194 }
195 }
196
Thomas Vachuska6b7920d2014-11-25 19:48:39 -0800197 private void logConfig(String prefix) {
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700198 log.info("{} with maxEvents = {}; maxBatchMs = {}; maxIdleMs = {}; accumulator={}",
199 prefix, maxEvents, maxBatchMs, maxIdleMs, accumulator != null);
Thomas Vachuska6b7920d2014-11-25 19:48:39 -0800200 }
201
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700202
Thomas Vachuska0e752bd2014-10-22 22:33:41 -0700203 @Override
204 public void triggerRecompute() {
Sho SHIMIZU21d00692016-08-15 11:15:28 -0700205 triggerTopologyBuild(Collections.emptyList());
Thomas Vachuska0e752bd2014-10-22 22:33:41 -0700206 }
207
tomcbff9392014-09-10 00:45:23 -0700208 /**
209 * Triggers assembly of topology data citing the specified events as the
210 * reason.
211 *
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700212 * @param reasons events which triggered the topology change
tomcbff9392014-09-10 00:45:23 -0700213 */
tome52ce702014-09-11 00:12:54 -0700214 private synchronized void triggerTopologyBuild(List<Event> reasons) {
tom97937552014-09-11 10:48:42 -0700215 if (executor != null) {
216 executor.execute(new TopologyBuilderTask(reasons));
217 }
tomcbff9392014-09-10 00:45:23 -0700218 }
219
220 // Builds the topology using the latest device and link information
221 // and citing the specified events as reasons for the change.
222 private void buildTopology(List<Event> reasons) {
tomcbff9392014-09-10 00:45:23 -0700223 if (isStarted) {
tom97937552014-09-11 10:48:42 -0700224 GraphDescription desc =
225 new DefaultGraphDescription(System.nanoTime(),
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700226 System.currentTimeMillis(),
227 deviceService.getAvailableDevices(),
228 linkService.getActiveLinks());
tomcbff9392014-09-10 00:45:23 -0700229 providerService.topologyChanged(desc, reasons);
230 }
231 }
232
Thomas Vachuska912bdd52014-11-17 11:39:01 -0800233 private void processEvent(Event event) {
234 if (accumulator != null) {
235 accumulator.add(event);
236 } else {
237 triggerTopologyBuild(ImmutableList.of(event));
238 }
239 }
240
tomcbff9392014-09-10 00:45:23 -0700241 // Callback for device events
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -0700242 private class InternalDeviceListener implements DeviceListener {
tomcbff9392014-09-10 00:45:23 -0700243 @Override
244 public void event(DeviceEvent event) {
245 DeviceEvent.Type type = event.type();
Thomas Vachuska6d697f12015-03-08 20:59:50 -0700246 if (type == DEVICE_ADDED || type == DEVICE_REMOVED ||
247 type == DEVICE_AVAILABILITY_CHANGED) {
Thomas Vachuska912bdd52014-11-17 11:39:01 -0800248 processEvent(event);
tomcbff9392014-09-10 00:45:23 -0700249 }
250 }
251 }
252
253 // Callback for link events
Ayaka Koshibe3de43ca2014-09-26 16:40:23 -0700254 private class InternalLinkListener implements LinkListener {
tomcbff9392014-09-10 00:45:23 -0700255 @Override
256 public void event(LinkEvent event) {
Thomas Vachuska912bdd52014-11-17 11:39:01 -0800257 processEvent(event);
tomcbff9392014-09-10 00:45:23 -0700258 }
259 }
260
261 // Event accumulator for paced triggering of topology assembly.
Thomas Vachuskaecb63c52015-02-19 10:03:48 -0800262 private class TopologyChangeAccumulator extends AbstractAccumulator<Event> {
tomcbff9392014-09-10 00:45:23 -0700263 TopologyChangeAccumulator() {
Thomas Vachuska912bdd52014-11-17 11:39:01 -0800264 super(TIMER, maxEvents, maxBatchMs, maxIdleMs);
tomcbff9392014-09-10 00:45:23 -0700265 }
266
267 @Override
Thomas Vachuskaecb63c52015-02-19 10:03:48 -0800268 public void processItems(List<Event> items) {
269 triggerTopologyBuild(items);
tomcbff9392014-09-10 00:45:23 -0700270 }
tomcbff9392014-09-10 00:45:23 -0700271 }
272
273 // Task for building topology data in a separate thread.
274 private class TopologyBuilderTask implements Runnable {
275 private final List<Event> reasons;
276
277 public TopologyBuilderTask(List<Event> reasons) {
278 this.reasons = reasons;
279 }
280
281 @Override
282 public void run() {
Thomas Vachuska0e752bd2014-10-22 22:33:41 -0700283 try {
284 buildTopology(reasons);
285 } catch (Exception e) {
Thomas Vachuska320c58f2015-08-05 10:42:32 -0700286 log.warn("Unable to compute topology", e);
Thomas Vachuska0e752bd2014-10-22 22:33:41 -0700287 }
tomcbff9392014-09-10 00:45:23 -0700288 }
289 }
290
291}