blob: 38c5a57d91dcd0dea1cbe9404cdea07fd9996aa6 [file] [log] [blame]
Ayaka Koshibe422916f2015-01-15 15:30:23 -08001/*
2 * Copyright 2015 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 */
16package org.onosproject.provider.nil.link.impl;
17
Ayaka Koshibe13f896f2015-03-10 15:01:44 -070018import com.google.common.collect.HashMultimap;
Ayaka Koshibe9209ea22015-03-09 10:57:50 -070019import com.google.common.collect.Lists;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080020import com.google.common.collect.Maps;
21import com.google.common.collect.Sets;
Ayaka Koshibe839a8a92015-03-03 17:07:22 -080022
Ayaka Koshibe422916f2015-01-15 15:30:23 -080023import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
Ayaka Koshibe7dc1d042015-01-21 15:28:03 -080026import org.apache.felix.scr.annotations.Modified;
Ayaka Koshibe422916f2015-01-15 15:30:23 -080027import org.apache.felix.scr.annotations.Property;
28import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070030import org.onosproject.cfg.ComponentConfigService;
Ayaka Koshibe8eddc0d2015-02-02 18:07:29 -080031import org.onosproject.cluster.ClusterService;
32import org.onosproject.cluster.NodeId;
Ayaka Koshibe0cd42822015-01-22 16:02:31 -080033import org.onosproject.mastership.MastershipService;
Ayaka Koshibe422916f2015-01-15 15:30:23 -080034import org.onosproject.net.ConnectPoint;
35import org.onosproject.net.Device;
36import org.onosproject.net.DeviceId;
Ayaka Koshibe422916f2015-01-15 15:30:23 -080037import org.onosproject.net.PortNumber;
38import org.onosproject.net.device.DeviceEvent;
39import org.onosproject.net.device.DeviceListener;
40import org.onosproject.net.device.DeviceService;
41import org.onosproject.net.link.DefaultLinkDescription;
42import org.onosproject.net.link.LinkDescription;
43import org.onosproject.net.link.LinkProvider;
44import org.onosproject.net.link.LinkProviderRegistry;
45import org.onosproject.net.link.LinkProviderService;
46import org.onosproject.net.provider.AbstractProvider;
47import org.onosproject.net.provider.ProviderId;
48import org.osgi.service.component.ComponentContext;
49import org.slf4j.Logger;
50
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080051import java.util.Dictionary;
Ayaka Koshibe9209ea22015-03-09 10:57:50 -070052import java.util.List;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080053import java.util.Set;
54import java.util.concurrent.ConcurrentMap;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080055import java.util.concurrent.Executors;
Ayaka Koshibe9209ea22015-03-09 10:57:50 -070056import java.util.concurrent.ScheduledExecutorService;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080057import java.util.concurrent.TimeUnit;
Ayaka Koshibe839a8a92015-03-03 17:07:22 -080058import java.io.BufferedReader;
59import java.io.FileReader;
60import java.io.IOException;
61import java.net.URI;
62import java.net.URISyntaxException;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080063
64import static com.google.common.base.Strings.isNullOrEmpty;
65import static org.onlab.util.Tools.groupedThreads;
66import static org.onlab.util.Tools.toHex;
Ayaka Koshibe839a8a92015-03-03 17:07:22 -080067import static org.onosproject.net.Link.Type.DIRECT;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080068import static org.slf4j.LoggerFactory.getLogger;
Ayaka Koshibe422916f2015-01-15 15:30:23 -080069
70/**
71 * Provider which advertises fake/nonexistent links to the core. To be used for
72 * benchmarking only.
Ayaka Koshibe839a8a92015-03-03 17:07:22 -080073 *
74 * This provider takes a topology graph file with a DOT-like syntax.
Ayaka Koshibe422916f2015-01-15 15:30:23 -080075 */
76@Component(immediate = true)
77public class NullLinkProvider extends AbstractProvider implements LinkProvider {
78
79 private final Logger log = getLogger(getClass());
80
Ayaka Koshibe9209ea22015-03-09 10:57:50 -070081 // default topology file location and name.
Ayaka Koshibe839a8a92015-03-03 17:07:22 -080082 private static final String CFG_PATH = "/opt/onos/apache-karaf-3.0.2/etc/linkGraph.cfg";
Ayaka Koshibe9209ea22015-03-09 10:57:50 -070083 // default number of workers. Eventually make this tunable
Ayaka Koshibe97940562015-03-11 12:25:46 -070084 private static final int THREADS = (int) Math.max(1, Runtime.getRuntime().availableProcessors() * 0.8);
Ayaka Koshibe839a8a92015-03-03 17:07:22 -080085
Ayaka Koshibe97940562015-03-11 12:25:46 -070086 private static final int CHECK_DURATION = 10; // sec
87 private static final int DEFAULT_RATE = 0; // usec
88 private static final int REFRESH_RATE = 3; // sec
89 // Fake device used for non-flickering thread in deviceMap
Ayaka Koshibe13f896f2015-03-10 15:01:44 -070090 private static final DeviceId DEFAULT = DeviceId.deviceId("null:ffffffffffffffff");
Ayaka Koshibe839a8a92015-03-03 17:07:22 -080091
Ayaka Koshibe422916f2015-01-15 15:30:23 -080092 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
93 protected DeviceService deviceService;
94
95 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibe0cd42822015-01-22 16:02:31 -080096 protected MastershipService roleService;
97
98 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibe8eddc0d2015-02-02 18:07:29 -080099 protected ClusterService nodeService;
100
101 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800102 protected LinkProviderRegistry providerRegistry;
103
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700104 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
105 protected ComponentConfigService cfgService;
106
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800107 private LinkProviderService providerService;
108
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800109 private final InternalLinkProvider linkProvider = new InternalLinkProvider();
110
Ayaka Koshibe97940562015-03-11 12:25:46 -0700111 // Mapping between device and drivers that advertise links from device
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700112 private final ConcurrentMap<DeviceId, Set<LinkDriver>> driverMap = Maps
Ayaka Koshibed2c7ad22015-02-13 16:44:07 -0800113 .newConcurrentMap();
114
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800115 // Link descriptions
Ayaka Koshibe13f896f2015-03-10 15:01:44 -0700116 private final List<LinkDescription> linkDescrs = Lists.newArrayList();
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800117
Ayaka Koshibe97940562015-03-11 12:25:46 -0700118 // Thread to description map for dividing links amongst threads in flicker mode
Ayaka Koshibe13f896f2015-03-10 15:01:44 -0700119 private final List<List<LinkDescription>> linkTasks = Lists.newArrayList();
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700120
121 private ScheduledExecutorService linkDriver =
122 Executors.newScheduledThreadPool(THREADS, groupedThreads("onos/null", "link-driver-%d"));
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800123
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800124 // For flicker = true, duration between events in msec.
Thomas Vachuska3181de32015-03-11 10:07:05 -0700125 @Property(name = "eventRate", intValue = DEFAULT_RATE, label = "Duration between Link Event")
Ayaka Koshibe0cd42822015-01-22 16:02:31 -0800126 private int eventRate = DEFAULT_RATE;
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800127
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800128 // topology configuration file
Thomas Vachuska3181de32015-03-11 10:07:05 -0700129 @Property(name = "cfgFile", value = CFG_PATH, label = "Topology file location")
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800130 private String cfgFile = CFG_PATH;
Ayaka Koshibe8eddc0d2015-02-02 18:07:29 -0800131
Ayaka Koshibed2c7ad22015-02-13 16:44:07 -0800132 // flag checked to create a LinkDriver, if rate is non-zero.
Ayaka Koshibe97940562015-03-11 12:25:46 -0700133 private volatile boolean flicker = false;
Ayaka Koshibed2c7ad22015-02-13 16:44:07 -0800134
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800135 public NullLinkProvider() {
136 super(new ProviderId("null", "org.onosproject.provider.nil"));
137 }
138
139 @Activate
140 public void activate(ComponentContext context) {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700141 cfgService.registerProperties(getClass());
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800142 providerService = providerRegistry.register(this);
Ayaka Koshibed2c7ad22015-02-13 16:44:07 -0800143 modified(context);
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800144 log.info("started");
145 }
146
147 @Deactivate
148 public void deactivate(ComponentContext context) {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700149 cfgService.unregisterProperties(getClass(), false);
Ayaka Koshibed2c7ad22015-02-13 16:44:07 -0800150 linkDriver.shutdown();
151 try {
152 linkDriver.awaitTermination(1000, TimeUnit.MILLISECONDS);
153 } catch (InterruptedException e) {
154 log.error("LinkBuilder did not terminate");
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800155 linkDriver.shutdownNow();
156 }
157 deviceService.removeListener(linkProvider);
158 providerRegistry.unregister(this);
159 deviceService = null;
160
161 log.info("stopped");
162 }
163
Ayaka Koshibe7dc1d042015-01-21 15:28:03 -0800164 @Modified
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800165 public void modified(ComponentContext context) {
166 if (context == null) {
Ayaka Koshibe8eddc0d2015-02-02 18:07:29 -0800167 log.info("No configs, using defaults: eventRate={}", DEFAULT_RATE);
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800168 return;
169 }
170 Dictionary<?, ?> properties = context.getProperties();
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800171 int newRate;
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800172 String newPath;
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800173 try {
Ayaka Koshibe8eddc0d2015-02-02 18:07:29 -0800174 String s = (String) properties.get("eventRate");
Ayaka Koshibe97940562015-03-11 12:25:46 -0700175 newRate = isNullOrEmpty(s) ? DEFAULT_RATE : Integer.parseInt(s.trim());
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800176 s = (String) properties.get("cfgFile");
177 newPath = s.trim();
Pavlin Radoslavovb9e50df2015-02-20 20:01:26 -0800178 } catch (NumberFormatException | ClassCastException e) {
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800179 log.warn(e.getMessage());
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800180 newRate = eventRate;
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800181 newPath = cfgFile;
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800182 }
183
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800184 // topology file configuration
185 if (!newPath.equals(cfgFile)) {
186 cfgFile = newPath;
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800187 }
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800188 readGraph(cfgFile, nodeService.getLocalNode().id());
Ayaka Koshibe97940562015-03-11 12:25:46 -0700189 if (newRate != eventRate) {
190 if (eventRate < 0) {
191 log.warn("Invalid rate, ignoring and using default");
192 eventRate = DEFAULT_RATE;
193 } else {
194 eventRate = newRate;
195 }
196 }
197 if (eventRate > 0) {
198 if (!flicker) { // previously not flickering
199 flicker = true;
200 allocateLinks();
201 driverMap.remove(DEFAULT);
202 for (int i = 0; i < linkTasks.size(); i++) {
203 List<LinkDescription> links = linkTasks.get(i);
204 LinkDriver driver = new LinkDriver(links);
205 links.forEach(v -> {
206 DeviceId sd = v.src().deviceId();
207 DeviceId dd = v.src().deviceId();
208 driverMap.computeIfAbsent(sd, k -> Sets.newConcurrentHashSet()).add(driver);
209 driverMap.computeIfAbsent(dd, k -> Sets.newConcurrentHashSet()).add(driver);
210 });
211 try {
212 linkDriver.schedule(driver, eventRate, TimeUnit.MICROSECONDS);
213 } catch (Exception e) {
214 log.warn(e.getMessage());
215 }
216 }
217 }
218 } else {
219 if (flicker) {
220 driverMap.forEach((dev, lds) -> lds.forEach(l -> l.deviceRemoved(dev)));
221 driverMap.clear();
222 linkTasks.clear();
223 }
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800224 flicker = false;
Ayaka Koshibe97940562015-03-11 12:25:46 -0700225 LinkDriver driver = new LinkDriver(linkDescrs);
226 driverMap.put(DEFAULT, Sets.newHashSet(driver));
227 linkDriver.schedule(driver, REFRESH_RATE, TimeUnit.SECONDS);
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800228 }
Ayaka Koshibe8eddc0d2015-02-02 18:07:29 -0800229
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800230 log.info("Using settings: eventRate={}, topofile={}", eventRate, cfgFile);
Ayaka Koshibe8eddc0d2015-02-02 18:07:29 -0800231 }
232
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800233 // parse simplified dot-like topology graph
234 private void readGraph(String path, NodeId me) {
235 log.info("path: {}, local: {}", path, me);
Ayaka Koshibe13f896f2015-03-10 15:01:44 -0700236 Set<LinkDescription> read = Sets.newHashSet();
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800237 BufferedReader br = null;
238 try {
239 br = new BufferedReader(new FileReader(path));
240 String cur = br.readLine();
241 while (cur != null) {
242 if (cur.startsWith("#")) {
243 cur = br.readLine();
244 continue;
245 }
246 String[] parts = cur.trim().split(" ");
247 if (parts.length < 1) {
248 continue;
249 }
250 if (parts[0].equals("graph")) {
251 String node = parts[1].trim();
252 if (node.equals(me.toString())) {
253 cur = br.readLine(); // move to next line, start of links list
254 while (cur != null) {
255 if (cur.trim().contains("}")) {
256 break;
257 }
Ayaka Koshibe13f896f2015-03-10 15:01:44 -0700258 readLink(cur.trim().split(" "), me, read);
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800259 cur = br.readLine();
260 }
261 } else {
262 while (cur != null) {
263 if (cur.trim().equals("}")) {
264 break;
265 }
266 cur = br.readLine();
267 }
268 }
269 }
270 cur = br.readLine();
271 }
272 } catch (IOException e) {
273 log.warn("Could not find topology file: {}", e);
274 } finally {
275 try {
276 if (br != null) {
277 br.close();
278 }
279 } catch (IOException e) {
280 log.warn("Could not close topology file: {}", e);
281 }
282 }
Ayaka Koshibe13f896f2015-03-10 15:01:44 -0700283 synchronized (linkDescrs) {
284 if (!read.isEmpty()) {
285 linkDescrs.clear();
286 linkDescrs.addAll(read);
287 }
288 }
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800289 }
290
291 // parses a link descriptor to make a LinkDescription
Ayaka Koshibe13f896f2015-03-10 15:01:44 -0700292 private void readLink(String[] linkArr, NodeId me, Set<LinkDescription> links) {
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800293 if (linkArr[0].startsWith("#")) {
294 return;
295 }
296 if (linkArr.length != 3) {
297 log.warn("Malformed link descriptor:"
298 + " link should be of format src:port [--|->] dst:port,"
299 + " skipping");
300 return;
301 }
302
303 String op = linkArr[1];
304 String[] cp1 = linkArr[0].split(":");
305 String[] cp2 = linkArr[2].split(":");
306
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800307 if (cp1.length != 2 && (cp2.length != 2 || cp2.length != 3)) {
308 log.warn("Malformed endpoint descriptor(s):"
309 + "endpoint format should be DeviceId:port or DeviceId:port:NodeId,"
310 + "skipping");
311 return;
312 }
313 // read in hints about topology.
314 NodeId adj = null;
315 if (cp2.length == 3) {
316 adj = new NodeId(cp2[2]);
317 log.debug("found an island: {}", adj);
318 }
319
320 // reconstruct deviceIDs. Convention is based on NullDeviceProvider.
321 DeviceId sdev = recover(cp1[0], me);
322 DeviceId ddev = (adj == null) ? recover(cp2[0], me) : recover(cp2[0], adj);
323 ConnectPoint src = new ConnectPoint(sdev, PortNumber.portNumber(cp1[1]));
324 ConnectPoint dst = new ConnectPoint(ddev, PortNumber.portNumber(cp2[1]));
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700325 // both link types have incoming half-link
326 LinkDescription in = new DefaultLinkDescription(dst, src, DIRECT);
Ayaka Koshibe13f896f2015-03-10 15:01:44 -0700327 links.add(in);
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800328 if (op.equals("--")) {
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700329 // bidirectional - within our node's island, make outbound link
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800330 LinkDescription out = new DefaultLinkDescription(src, dst, DIRECT);
Ayaka Koshibe13f896f2015-03-10 15:01:44 -0700331 links.add(out);
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700332 log.info("Created bidirectional link: {}, {}", out, in);
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800333 } else if (op.equals("->")) {
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800334 log.info("Created unidirectional link: {}", in);
335 } else {
336 log.warn("Unknown link descriptor operand:"
337 + " operand must be '--' or '->', skipping");
338 return;
339 }
340 }
341
342 // recover DeviceId from configs and NodeID
343 private DeviceId recover(String base, NodeId node) {
344 long hash = node.hashCode() << 16;
345 int dev = Integer.valueOf(base);
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800346 try {
347 return DeviceId.deviceId(new URI("null", toHex(hash | dev), null));
348 } catch (URISyntaxException e) {
349 log.warn("could not create a DeviceID for descriptor {}", dev);
350 return DeviceId.NONE;
351 }
352 }
353
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700354 // adds a LinkDescription to a worker's to-be queue, for flickering
355 private void allocateLinks() {
356 int index, lcount = 0;
357 for (LinkDescription ld : linkDescrs) {
358 index = (lcount % THREADS);
359 log.info("allocation: total={}, index={}", linkDescrs.size(), lcount, index);
360 if (linkTasks.size() <= index) {
Ayaka Koshibe13f896f2015-03-10 15:01:44 -0700361 linkTasks.add(index, Lists.newArrayList(ld));
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700362 } else {
363 linkTasks.get(index).add(ld);
364 }
365 lcount++;
366 }
Ayaka Koshibed2c7ad22015-02-13 16:44:07 -0800367 }
368
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800369 /**
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800370 * Generate LinkEvents using configurations when devices are found.
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800371 */
372 private class InternalLinkProvider implements DeviceListener {
373
374 @Override
375 public void event(DeviceEvent event) {
Ayaka Koshibe0cd42822015-01-22 16:02:31 -0800376 Device dev = event.subject();
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800377 switch (event.type()) {
378 case DEVICE_ADDED:
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700379 // TODO: wait for all devices to stop core from balking
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800380 break;
381 case DEVICE_REMOVED:
Ayaka Koshibe97940562015-03-11 12:25:46 -0700382 for (LinkDriver d : driverMap.get(dev.id())) {
383 d.deviceRemoved(dev.id());
Ayaka Koshibe35c71e12015-01-27 17:10:04 -0800384 }
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800385 providerService.linksVanished(dev.id());
Ayaka Koshibe35c71e12015-01-27 17:10:04 -0800386 break;
387 default:
388 break;
389 }
390 }
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800391 }
392
393 /**
394 * Generates link events using fake links.
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700395 * TODO: stats collection should be its own thing.
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800396 */
397 private class LinkDriver implements Runnable {
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700398 // List to actually work off of
Ayaka Koshibe97940562015-03-11 12:25:46 -0700399 List<LinkDescription> tasks = Lists.newCopyOnWriteArrayList();
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700400 float effLoad = 0;
401 Long counter = 0L;
402 int next = 0;
Ayaka Koshibe97940562015-03-11 12:25:46 -0700403 boolean up = true;
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700404
405 long startTime;
406
Ayaka Koshibe13f896f2015-03-10 15:01:44 -0700407 LinkDriver(List<LinkDescription> links) {
408 setTasks(links);
409 startTime = System.currentTimeMillis(); // yes, this will start off inaccurate
Ayaka Koshibed2c7ad22015-02-13 16:44:07 -0800410 }
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800411
412 @Override
413 public void run() {
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800414 if (flicker) {
415 flicker();
416 } else {
417 refresh();
418 }
419 }
420
421 private void flicker() {
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700422 if ((!linkDriver.isShutdown() || !tasks.isEmpty())) {
Ayaka Koshibe97940562015-03-11 12:25:46 -0700423 log.trace("next: {}, count: {}", next, counter);
424 if (counter <= CHECK_DURATION * 1_000_000 / eventRate) {
425 if (up) {
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700426 providerService.linkDetected(tasks.get(next++));
Ayaka Koshibe97940562015-03-11 12:25:46 -0700427 } else {
428 providerService.linkVanished(tasks.get(next++));
suibin1a7b7bd2015-02-12 09:41:47 -0800429 }
Ayaka Koshibe97940562015-03-11 12:25:46 -0700430 if (next >= tasks.size()) {
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700431 next = 0;
Ayaka Koshibe97940562015-03-11 12:25:46 -0700432 up = !up;
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700433 }
434 counter++;
suibin1a7b7bd2015-02-12 09:41:47 -0800435 } else {
436 // log in WARN the effective load generation rate in events/sec, every 10 seconds
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700437 effLoad = (float) (counter * 1000.0 / (System
438 .currentTimeMillis() - startTime));
Ayaka Koshibed2c7ad22015-02-13 16:44:07 -0800439 log.warn("Effective Loading for thread is {} events/second",
440 String.valueOf(effLoad));
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700441 counter = 0L;
suibin1a7b7bd2015-02-12 09:41:47 -0800442 startTime = System.currentTimeMillis();
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800443 }
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700444 linkDriver.schedule(this, eventRate, TimeUnit.MICROSECONDS);
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800445 }
446 }
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800447
448 private void refresh() {
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700449 if (!linkDriver.isShutdown() || !tasks.isEmpty()) {
Ayaka Koshibe97940562015-03-11 12:25:46 -0700450 log.trace("iter {} refresh_links", counter);
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700451
Ayaka Koshibe13f896f2015-03-10 15:01:44 -0700452 for (LinkDescription desc : tasks) {
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800453 providerService.linkDetected(desc);
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700454 log.info("iteration {}, {}", counter, desc);
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800455 }
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700456 counter++;
457 linkDriver.schedule(this, REFRESH_RATE, TimeUnit.SECONDS);
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800458 }
459 }
460
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700461 public void deviceRemoved(DeviceId did) {
Ayaka Koshibe97940562015-03-11 12:25:46 -0700462 List<LinkDescription> rm = Lists.newArrayList();
463 for (LinkDescription ld : tasks) {
464 if (did.equals(ld.dst().deviceId())
465 || (did.equals(ld.src().deviceId()))) {
466 rm.add(ld);
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700467 }
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800468 }
Ayaka Koshibe97940562015-03-11 12:25:46 -0700469 tasks.removeAll(rm);
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800470 }
Ayaka Koshibe13f896f2015-03-10 15:01:44 -0700471
472 public void setTasks(List<LinkDescription> links) {
473 HashMultimap<ConnectPoint, ConnectPoint> nm = HashMultimap.create();
474 links.forEach(v -> nm.put(v.src(), v.dst()));
475 // remove and send linkVanished for stale links.
Ayaka Koshibe97940562015-03-11 12:25:46 -0700476 for (LinkDescription l : tasks) {
477 if (!nm.containsEntry(l.src(), l.dst())) {
478 providerService.linkVanished(l);
Ayaka Koshibe13f896f2015-03-10 15:01:44 -0700479 }
Ayaka Koshibe13f896f2015-03-10 15:01:44 -0700480 }
Ayaka Koshibe97940562015-03-11 12:25:46 -0700481 tasks.clear();
482 tasks.addAll(links);
Ayaka Koshibe13f896f2015-03-10 15:01:44 -0700483 }
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800484 }
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800485
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800486}