blob: 9e9f286c5406f46037e45767332cd44bc8862851 [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 Koshibe422916f2015-01-15 15:30:23 -080022import org.apache.felix.scr.annotations.Activate;
23import org.apache.felix.scr.annotations.Component;
24import org.apache.felix.scr.annotations.Deactivate;
Ayaka Koshibe7dc1d042015-01-21 15:28:03 -080025import org.apache.felix.scr.annotations.Modified;
Ayaka Koshibe422916f2015-01-15 15:30:23 -080026import org.apache.felix.scr.annotations.Property;
27import org.apache.felix.scr.annotations.Reference;
28import org.apache.felix.scr.annotations.ReferenceCardinality;
Brian O'Connorb34ae262015-03-13 18:59:34 -070029import org.onlab.util.Tools;
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
Brian O'Connorb34ae262015-03-13 18:59:34 -070051import java.io.BufferedReader;
52import java.io.FileReader;
53import java.io.IOException;
54import java.net.URI;
55import java.net.URISyntaxException;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080056import java.util.Dictionary;
Ayaka Koshibe9209ea22015-03-09 10:57:50 -070057import java.util.List;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080058import java.util.Set;
59import java.util.concurrent.ConcurrentMap;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080060import java.util.concurrent.Executors;
Ayaka Koshibe9209ea22015-03-09 10:57:50 -070061import java.util.concurrent.ScheduledExecutorService;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080062import java.util.concurrent.TimeUnit;
63
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 }
Brian O'Connorb34ae262015-03-13 18:59:34 -0700283 Set<LinkDescription> removedLinks = null;
Ayaka Koshibe13f896f2015-03-10 15:01:44 -0700284 synchronized (linkDescrs) {
285 if (!read.isEmpty()) {
Brian O'Connorb34ae262015-03-13 18:59:34 -0700286 removedLinks = Sets.difference(Sets.newHashSet(linkDescrs), read);
Ayaka Koshibe13f896f2015-03-10 15:01:44 -0700287 linkDescrs.clear();
288 linkDescrs.addAll(read);
289 }
290 }
Brian O'Connorb34ae262015-03-13 18:59:34 -0700291 if (!Tools.isNullOrEmpty(removedLinks)) {
292 log.info("Removing {} old link(s)", removedLinks.size());
293 removedLinks.forEach(providerService::linkVanished);
294 }
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800295 }
296
297 // parses a link descriptor to make a LinkDescription
Ayaka Koshibe13f896f2015-03-10 15:01:44 -0700298 private void readLink(String[] linkArr, NodeId me, Set<LinkDescription> links) {
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800299 if (linkArr[0].startsWith("#")) {
300 return;
301 }
302 if (linkArr.length != 3) {
303 log.warn("Malformed link descriptor:"
304 + " link should be of format src:port [--|->] dst:port,"
305 + " skipping");
306 return;
307 }
308
309 String op = linkArr[1];
310 String[] cp1 = linkArr[0].split(":");
311 String[] cp2 = linkArr[2].split(":");
312
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800313 if (cp1.length != 2 && (cp2.length != 2 || cp2.length != 3)) {
314 log.warn("Malformed endpoint descriptor(s):"
315 + "endpoint format should be DeviceId:port or DeviceId:port:NodeId,"
316 + "skipping");
317 return;
318 }
319 // read in hints about topology.
320 NodeId adj = null;
321 if (cp2.length == 3) {
322 adj = new NodeId(cp2[2]);
323 log.debug("found an island: {}", adj);
324 }
325
326 // reconstruct deviceIDs. Convention is based on NullDeviceProvider.
327 DeviceId sdev = recover(cp1[0], me);
328 DeviceId ddev = (adj == null) ? recover(cp2[0], me) : recover(cp2[0], adj);
329 ConnectPoint src = new ConnectPoint(sdev, PortNumber.portNumber(cp1[1]));
330 ConnectPoint dst = new ConnectPoint(ddev, PortNumber.portNumber(cp2[1]));
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700331 // both link types have incoming half-link
332 LinkDescription in = new DefaultLinkDescription(dst, src, DIRECT);
Ayaka Koshibe13f896f2015-03-10 15:01:44 -0700333 links.add(in);
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800334 if (op.equals("--")) {
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700335 // bidirectional - within our node's island, make outbound link
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800336 LinkDescription out = new DefaultLinkDescription(src, dst, DIRECT);
Ayaka Koshibe13f896f2015-03-10 15:01:44 -0700337 links.add(out);
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700338 log.info("Created bidirectional link: {}, {}", out, in);
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800339 } else if (op.equals("->")) {
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800340 log.info("Created unidirectional link: {}", in);
341 } else {
342 log.warn("Unknown link descriptor operand:"
343 + " operand must be '--' or '->', skipping");
344 return;
345 }
346 }
347
348 // recover DeviceId from configs and NodeID
349 private DeviceId recover(String base, NodeId node) {
350 long hash = node.hashCode() << 16;
351 int dev = Integer.valueOf(base);
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800352 try {
353 return DeviceId.deviceId(new URI("null", toHex(hash | dev), null));
354 } catch (URISyntaxException e) {
355 log.warn("could not create a DeviceID for descriptor {}", dev);
356 return DeviceId.NONE;
357 }
358 }
359
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700360 // adds a LinkDescription to a worker's to-be queue, for flickering
361 private void allocateLinks() {
362 int index, lcount = 0;
363 for (LinkDescription ld : linkDescrs) {
364 index = (lcount % THREADS);
365 log.info("allocation: total={}, index={}", linkDescrs.size(), lcount, index);
366 if (linkTasks.size() <= index) {
Ayaka Koshibe13f896f2015-03-10 15:01:44 -0700367 linkTasks.add(index, Lists.newArrayList(ld));
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700368 } else {
369 linkTasks.get(index).add(ld);
370 }
371 lcount++;
372 }
Ayaka Koshibed2c7ad22015-02-13 16:44:07 -0800373 }
374
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800375 /**
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800376 * Generate LinkEvents using configurations when devices are found.
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800377 */
378 private class InternalLinkProvider implements DeviceListener {
379
380 @Override
381 public void event(DeviceEvent event) {
Ayaka Koshibe0cd42822015-01-22 16:02:31 -0800382 Device dev = event.subject();
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800383 switch (event.type()) {
384 case DEVICE_ADDED:
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700385 // TODO: wait for all devices to stop core from balking
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800386 break;
387 case DEVICE_REMOVED:
Ayaka Koshibe97940562015-03-11 12:25:46 -0700388 for (LinkDriver d : driverMap.get(dev.id())) {
389 d.deviceRemoved(dev.id());
Ayaka Koshibe35c71e12015-01-27 17:10:04 -0800390 }
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800391 providerService.linksVanished(dev.id());
Ayaka Koshibe35c71e12015-01-27 17:10:04 -0800392 break;
393 default:
394 break;
395 }
396 }
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800397 }
398
399 /**
400 * Generates link events using fake links.
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700401 * TODO: stats collection should be its own thing.
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800402 */
403 private class LinkDriver implements Runnable {
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700404 // List to actually work off of
Ayaka Koshibe97940562015-03-11 12:25:46 -0700405 List<LinkDescription> tasks = Lists.newCopyOnWriteArrayList();
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700406 float effLoad = 0;
407 Long counter = 0L;
408 int next = 0;
Ayaka Koshibe97940562015-03-11 12:25:46 -0700409 boolean up = true;
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700410
411 long startTime;
412
Ayaka Koshibe13f896f2015-03-10 15:01:44 -0700413 LinkDriver(List<LinkDescription> links) {
414 setTasks(links);
415 startTime = System.currentTimeMillis(); // yes, this will start off inaccurate
Ayaka Koshibed2c7ad22015-02-13 16:44:07 -0800416 }
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800417
418 @Override
419 public void run() {
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800420 if (flicker) {
421 flicker();
422 } else {
423 refresh();
424 }
425 }
426
427 private void flicker() {
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700428 if ((!linkDriver.isShutdown() || !tasks.isEmpty())) {
Ayaka Koshibe97940562015-03-11 12:25:46 -0700429 log.trace("next: {}, count: {}", next, counter);
430 if (counter <= CHECK_DURATION * 1_000_000 / eventRate) {
431 if (up) {
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700432 providerService.linkDetected(tasks.get(next++));
Ayaka Koshibe97940562015-03-11 12:25:46 -0700433 } else {
434 providerService.linkVanished(tasks.get(next++));
suibin1a7b7bd2015-02-12 09:41:47 -0800435 }
Ayaka Koshibe97940562015-03-11 12:25:46 -0700436 if (next >= tasks.size()) {
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700437 next = 0;
Ayaka Koshibe97940562015-03-11 12:25:46 -0700438 up = !up;
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700439 }
440 counter++;
suibin1a7b7bd2015-02-12 09:41:47 -0800441 } else {
442 // log in WARN the effective load generation rate in events/sec, every 10 seconds
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700443 effLoad = (float) (counter * 1000.0 / (System
444 .currentTimeMillis() - startTime));
Ayaka Koshibed2c7ad22015-02-13 16:44:07 -0800445 log.warn("Effective Loading for thread is {} events/second",
446 String.valueOf(effLoad));
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700447 counter = 0L;
suibin1a7b7bd2015-02-12 09:41:47 -0800448 startTime = System.currentTimeMillis();
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800449 }
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700450 linkDriver.schedule(this, eventRate, TimeUnit.MICROSECONDS);
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800451 }
452 }
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800453
454 private void refresh() {
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700455 if (!linkDriver.isShutdown() || !tasks.isEmpty()) {
Ayaka Koshibe97940562015-03-11 12:25:46 -0700456 log.trace("iter {} refresh_links", counter);
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700457
Ayaka Koshibe13f896f2015-03-10 15:01:44 -0700458 for (LinkDescription desc : tasks) {
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800459 providerService.linkDetected(desc);
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700460 log.info("iteration {}, {}", counter, desc);
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800461 }
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700462 counter++;
463 linkDriver.schedule(this, REFRESH_RATE, TimeUnit.SECONDS);
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800464 }
465 }
466
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700467 public void deviceRemoved(DeviceId did) {
Ayaka Koshibe97940562015-03-11 12:25:46 -0700468 List<LinkDescription> rm = Lists.newArrayList();
469 for (LinkDescription ld : tasks) {
470 if (did.equals(ld.dst().deviceId())
471 || (did.equals(ld.src().deviceId()))) {
472 rm.add(ld);
Ayaka Koshibe9209ea22015-03-09 10:57:50 -0700473 }
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800474 }
Ayaka Koshibe97940562015-03-11 12:25:46 -0700475 tasks.removeAll(rm);
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800476 }
Ayaka Koshibe13f896f2015-03-10 15:01:44 -0700477
478 public void setTasks(List<LinkDescription> links) {
479 HashMultimap<ConnectPoint, ConnectPoint> nm = HashMultimap.create();
480 links.forEach(v -> nm.put(v.src(), v.dst()));
481 // remove and send linkVanished for stale links.
Ayaka Koshibe97940562015-03-11 12:25:46 -0700482 for (LinkDescription l : tasks) {
483 if (!nm.containsEntry(l.src(), l.dst())) {
484 providerService.linkVanished(l);
Ayaka Koshibe13f896f2015-03-10 15:01:44 -0700485 }
Ayaka Koshibe13f896f2015-03-10 15:01:44 -0700486 }
Ayaka Koshibe97940562015-03-11 12:25:46 -0700487 tasks.clear();
488 tasks.addAll(links);
Ayaka Koshibe13f896f2015-03-10 15:01:44 -0700489 }
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800490 }
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800491
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800492}