blob: 3e85b4e9f638ca17c522efcfa5c15842411aa351 [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
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080018import com.google.common.collect.Maps;
19import com.google.common.collect.Sets;
Ayaka Koshibe839a8a92015-03-03 17:07:22 -080020
Ayaka Koshibed2c7ad22015-02-13 16:44:07 -080021import org.apache.commons.lang3.concurrent.ConcurrentUtils;
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;
Ayaka Koshibe8eddc0d2015-02-02 18:07:29 -080029import org.onosproject.cluster.ClusterService;
30import org.onosproject.cluster.NodeId;
Ayaka Koshibe0cd42822015-01-22 16:02:31 -080031import org.onosproject.mastership.MastershipService;
Ayaka Koshibe422916f2015-01-15 15:30:23 -080032import org.onosproject.net.ConnectPoint;
33import org.onosproject.net.Device;
34import org.onosproject.net.DeviceId;
Ayaka Koshibe422916f2015-01-15 15:30:23 -080035import org.onosproject.net.PortNumber;
36import org.onosproject.net.device.DeviceEvent;
37import org.onosproject.net.device.DeviceListener;
38import org.onosproject.net.device.DeviceService;
39import org.onosproject.net.link.DefaultLinkDescription;
40import org.onosproject.net.link.LinkDescription;
41import org.onosproject.net.link.LinkProvider;
42import org.onosproject.net.link.LinkProviderRegistry;
43import org.onosproject.net.link.LinkProviderService;
44import org.onosproject.net.provider.AbstractProvider;
45import org.onosproject.net.provider.ProviderId;
46import org.osgi.service.component.ComponentContext;
47import org.slf4j.Logger;
48
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080049import java.util.Dictionary;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080050import java.util.Set;
51import java.util.concurrent.ConcurrentMap;
52import java.util.concurrent.ExecutorService;
53import java.util.concurrent.Executors;
54import java.util.concurrent.TimeUnit;
Ayaka Koshibe839a8a92015-03-03 17:07:22 -080055import java.io.BufferedReader;
56import java.io.FileReader;
57import java.io.IOException;
58import java.net.URI;
59import java.net.URISyntaxException;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080060
61import static com.google.common.base.Strings.isNullOrEmpty;
62import static org.onlab.util.Tools.groupedThreads;
63import static org.onlab.util.Tools.toHex;
64import static org.onosproject.net.MastershipRole.MASTER;
Ayaka Koshibe839a8a92015-03-03 17:07:22 -080065import static org.onosproject.net.Link.Type.DIRECT;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080066import static org.slf4j.LoggerFactory.getLogger;
Ayaka Koshibe422916f2015-01-15 15:30:23 -080067
68/**
69 * Provider which advertises fake/nonexistent links to the core. To be used for
70 * benchmarking only.
Ayaka Koshibe839a8a92015-03-03 17:07:22 -080071 *
72 * This provider takes a topology graph file with a DOT-like syntax.
Ayaka Koshibe422916f2015-01-15 15:30:23 -080073 */
74@Component(immediate = true)
75public class NullLinkProvider extends AbstractProvider implements LinkProvider {
76
77 private final Logger log = getLogger(getClass());
78
Ayaka Koshibe839a8a92015-03-03 17:07:22 -080079 private static final String CFG_PATH = "/opt/onos/apache-karaf-3.0.2/etc/linkGraph.cfg";
80
81 private static final int CHECK_DURATION = 10;
82 private static final int DEFAULT_RATE = 0;
83 private static final int REFRESH_RATE = 3000000; // in us
84
Ayaka Koshibe422916f2015-01-15 15:30:23 -080085 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
86 protected DeviceService deviceService;
87
88 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibe0cd42822015-01-22 16:02:31 -080089 protected MastershipService roleService;
90
91 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibe8eddc0d2015-02-02 18:07:29 -080092 protected ClusterService nodeService;
93
94 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibe422916f2015-01-15 15:30:23 -080095 protected LinkProviderRegistry providerRegistry;
96
97 private LinkProviderService providerService;
98
Ayaka Koshibe422916f2015-01-15 15:30:23 -080099 private final InternalLinkProvider linkProvider = new InternalLinkProvider();
100
Ayaka Koshibed2c7ad22015-02-13 16:44:07 -0800101 // True for device with Driver, false otherwise.
102 private final ConcurrentMap<DeviceId, Boolean> driverMap = Maps
103 .newConcurrentMap();
104
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800105 // Link descriptions
Ayaka Koshibed2c7ad22015-02-13 16:44:07 -0800106 private final ConcurrentMap<DeviceId, Set<LinkDescription>> linkDescrs = Maps
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800107 .newConcurrentMap();
108
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800109 private ExecutorService linkDriver =
110 Executors.newCachedThreadPool(groupedThreads("onos/null", "link-driver-%d"));
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800111
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800112 // For flicker = true, duration between events in msec.
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800113 @Property(name = "eventRate", value = "0", label = "Duration between Link Event")
Ayaka Koshibe0cd42822015-01-22 16:02:31 -0800114 private int eventRate = DEFAULT_RATE;
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800115
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800116 // topology configuration file
117 @Property(name = "cfgFile",
118 value = "/opt/onos/apache-karaf-3.0.2/etc/linkGraph.cfg",
119 label = "Topology file location")
120 private String cfgFile = CFG_PATH;
Ayaka Koshibe8eddc0d2015-02-02 18:07:29 -0800121
Ayaka Koshibed2c7ad22015-02-13 16:44:07 -0800122 // flag checked to create a LinkDriver, if rate is non-zero.
123 private boolean flicker = false;
124
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800125 public NullLinkProvider() {
126 super(new ProviderId("null", "org.onosproject.provider.nil"));
127 }
128
129 @Activate
130 public void activate(ComponentContext context) {
131 providerService = providerRegistry.register(this);
Ayaka Koshibed2c7ad22015-02-13 16:44:07 -0800132 modified(context);
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800133 deviceService.addListener(linkProvider);
Ayaka Koshibed2c7ad22015-02-13 16:44:07 -0800134
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800135 log.info("started");
136 }
137
138 @Deactivate
139 public void deactivate(ComponentContext context) {
Ayaka Koshibed2c7ad22015-02-13 16:44:07 -0800140 linkDriver.shutdown();
141 try {
142 linkDriver.awaitTermination(1000, TimeUnit.MILLISECONDS);
143 } catch (InterruptedException e) {
144 log.error("LinkBuilder did not terminate");
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800145 linkDriver.shutdownNow();
146 }
147 deviceService.removeListener(linkProvider);
148 providerRegistry.unregister(this);
149 deviceService = null;
150
151 log.info("stopped");
152 }
153
Ayaka Koshibe7dc1d042015-01-21 15:28:03 -0800154 @Modified
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800155 public void modified(ComponentContext context) {
156 if (context == null) {
Ayaka Koshibe8eddc0d2015-02-02 18:07:29 -0800157 log.info("No configs, using defaults: eventRate={}", DEFAULT_RATE);
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800158 return;
159 }
160 Dictionary<?, ?> properties = context.getProperties();
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800161 int newRate;
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800162 String newPath;
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800163 try {
Ayaka Koshibe8eddc0d2015-02-02 18:07:29 -0800164 String s = (String) properties.get("eventRate");
Yuta HIGUCHIdd9228d2015-02-10 22:26:59 -0800165 newRate = isNullOrEmpty(s) ? eventRate : Integer.parseInt(s.trim());
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800166 s = (String) properties.get("cfgFile");
167 newPath = s.trim();
Pavlin Radoslavovb9e50df2015-02-20 20:01:26 -0800168 } catch (NumberFormatException | ClassCastException e) {
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800169 log.warn(e.getMessage());
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800170 newRate = eventRate;
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800171 newPath = cfgFile;
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800172 }
173
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800174 // topology file configuration
175 if (!newPath.equals(cfgFile)) {
176 cfgFile = newPath;
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800177 }
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800178 readGraph(cfgFile, nodeService.getLocalNode().id());
179
180 // test mode configuration
181 if (eventRate != newRate && newRate > 0) {
182 driverMap.replaceAll((k, v) -> false);
Ayaka Koshibe8eddc0d2015-02-02 18:07:29 -0800183 eventRate = newRate;
Ayaka Koshibed2c7ad22015-02-13 16:44:07 -0800184 flicker = true;
Ayaka Koshibed2c7ad22015-02-13 16:44:07 -0800185 } else if (newRate == 0) {
186 driverMap.replaceAll((k, v) -> false);
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800187 flicker = false;
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800188 }
Ayaka Koshibe8eddc0d2015-02-02 18:07:29 -0800189
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800190 log.info("Using settings: eventRate={}, topofile={}", eventRate, cfgFile);
191 for (Device dev : deviceService.getDevices()) {
192 DeviceId did = dev.id();
193 synchronized (this) {
194 if (driverMap.get(did) == null || !driverMap.get(did)) {
195 driverMap.put(dev.id(), true);
196 linkDriver.submit(new LinkDriver(dev));
197 }
Ayaka Koshibe8eddc0d2015-02-02 18:07:29 -0800198 }
199 }
Ayaka Koshibe8eddc0d2015-02-02 18:07:29 -0800200 }
201
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800202 // parse simplified dot-like topology graph
203 private void readGraph(String path, NodeId me) {
204 log.info("path: {}, local: {}", path, me);
205 BufferedReader br = null;
206 try {
207 br = new BufferedReader(new FileReader(path));
208 String cur = br.readLine();
209 while (cur != null) {
210 if (cur.startsWith("#")) {
211 cur = br.readLine();
212 continue;
213 }
214 String[] parts = cur.trim().split(" ");
215 if (parts.length < 1) {
216 continue;
217 }
218 if (parts[0].equals("graph")) {
219 String node = parts[1].trim();
220 if (node.equals(me.toString())) {
221 cur = br.readLine(); // move to next line, start of links list
222 while (cur != null) {
223 if (cur.trim().contains("}")) {
224 break;
225 }
226 readLink(cur.trim().split(" "), me);
227 cur = br.readLine();
228 }
229 } else {
230 while (cur != null) {
231 if (cur.trim().equals("}")) {
232 break;
233 }
234 cur = br.readLine();
235 }
236 }
237 }
238 cur = br.readLine();
239 }
240 } catch (IOException e) {
241 log.warn("Could not find topology file: {}", e);
242 } finally {
243 try {
244 if (br != null) {
245 br.close();
246 }
247 } catch (IOException e) {
248 log.warn("Could not close topology file: {}", e);
249 }
250 }
251 }
252
253 // parses a link descriptor to make a LinkDescription
254 private void readLink(String[] linkArr, NodeId me) {
255 if (linkArr[0].startsWith("#")) {
256 return;
257 }
258 if (linkArr.length != 3) {
259 log.warn("Malformed link descriptor:"
260 + " link should be of format src:port [--|->] dst:port,"
261 + " skipping");
262 return;
263 }
264
265 String op = linkArr[1];
266 String[] cp1 = linkArr[0].split(":");
267 String[] cp2 = linkArr[2].split(":");
268
269 log.debug("cp1:{} cp2:{}", cp1, cp2);
270 if (cp1.length != 2 && (cp2.length != 2 || cp2.length != 3)) {
271 log.warn("Malformed endpoint descriptor(s):"
272 + "endpoint format should be DeviceId:port or DeviceId:port:NodeId,"
273 + "skipping");
274 return;
275 }
276 // read in hints about topology.
277 NodeId adj = null;
278 if (cp2.length == 3) {
279 adj = new NodeId(cp2[2]);
280 log.debug("found an island: {}", adj);
281 }
282
283 // reconstruct deviceIDs. Convention is based on NullDeviceProvider.
284 DeviceId sdev = recover(cp1[0], me);
285 DeviceId ddev = (adj == null) ? recover(cp2[0], me) : recover(cp2[0], adj);
286 ConnectPoint src = new ConnectPoint(sdev, PortNumber.portNumber(cp1[1]));
287 ConnectPoint dst = new ConnectPoint(ddev, PortNumber.portNumber(cp2[1]));
288
289 if (op.equals("--")) {
290 // bidirectional - within our node's island
291 LinkDescription out = new DefaultLinkDescription(src, dst, DIRECT);
292 LinkDescription in = new DefaultLinkDescription(dst, src, DIRECT);
293 addLdesc(sdev, out);
294 addLdesc(ddev, in);
295 log.info("Created bidirectional link: {}", out);
296 } else if (op.equals("->")) {
297 // unidirectional - likely from another island
298 LinkDescription in = new DefaultLinkDescription(dst, src, DIRECT);
299 addLdesc(ddev, in);
300 log.info("Created unidirectional link: {}", in);
301 } else {
302 log.warn("Unknown link descriptor operand:"
303 + " operand must be '--' or '->', skipping");
304 return;
305 }
306 }
307
308 // recover DeviceId from configs and NodeID
309 private DeviceId recover(String base, NodeId node) {
310 long hash = node.hashCode() << 16;
311 int dev = Integer.valueOf(base);
312 log.debug("hash: {}, dev: {}, {}", hash, dev, toHex(hash | dev));
313 try {
314 return DeviceId.deviceId(new URI("null", toHex(hash | dev), null));
315 } catch (URISyntaxException e) {
316 log.warn("could not create a DeviceID for descriptor {}", dev);
317 return DeviceId.NONE;
318 }
319 }
320
321 // add LinkDescriptions to map
Ayaka Koshibed2c7ad22015-02-13 16:44:07 -0800322 private boolean addLdesc(DeviceId did, LinkDescription ldesc) {
323 Set<LinkDescription> ldescs = ConcurrentUtils.putIfAbsent(
324 linkDescrs, did, Sets.newConcurrentHashSet());
325 return ldescs.add(ldesc);
326 }
327
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800328 /**
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800329 * Generate LinkEvents using configurations when devices are found.
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800330 */
331 private class InternalLinkProvider implements DeviceListener {
332
333 @Override
334 public void event(DeviceEvent event) {
Ayaka Koshibe0cd42822015-01-22 16:02:31 -0800335 Device dev = event.subject();
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800336 switch (event.type()) {
337 case DEVICE_ADDED:
Ayaka Koshibed2c7ad22015-02-13 16:44:07 -0800338 synchronized (this) {
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800339 if (!driverMap.getOrDefault(dev.id(), false)) {
Ayaka Koshibed2c7ad22015-02-13 16:44:07 -0800340 driverMap.put(dev.id(), true);
341 linkDriver.submit(new LinkDriver(dev));
342 }
343 }
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800344 break;
345 case DEVICE_REMOVED:
Ayaka Koshibed2c7ad22015-02-13 16:44:07 -0800346 driverMap.put(dev.id(), false);
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800347 if (!MASTER.equals(roleService.getLocalRole(dev.id()))) {
Ayaka Koshibe35c71e12015-01-27 17:10:04 -0800348 return;
349 }
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800350 // no need to remove static links, just stop advertising them
351 providerService.linksVanished(dev.id());
Ayaka Koshibe35c71e12015-01-27 17:10:04 -0800352 break;
353 default:
354 break;
355 }
356 }
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800357 }
358
359 /**
360 * Generates link events using fake links.
361 */
362 private class LinkDriver implements Runnable {
Ayaka Koshibed2c7ad22015-02-13 16:44:07 -0800363 Device myDev;
364 LinkDriver(Device dev) {
365 myDev = dev;
366 }
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800367
368 @Override
369 public void run() {
Ayaka Koshibed2c7ad22015-02-13 16:44:07 -0800370 log.info("Thread started for dev {}", myDev.id());
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800371 if (flicker) {
372 flicker();
373 } else {
374 refresh();
375 }
376 }
377
378 private void flicker() {
suibin1a7b7bd2015-02-12 09:41:47 -0800379 long startTime = System.currentTimeMillis();
380 long countEvent = 0;
381 float effLoad = 0;
382
Ayaka Koshibed2c7ad22015-02-13 16:44:07 -0800383 while (!linkDriver.isShutdown() && driverMap.get(myDev.id())) {
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800384 if (!flicker) {
385 break;
Ayaka Koshibed2c7ad22015-02-13 16:44:07 -0800386 }
suibin1a7b7bd2015-02-12 09:41:47 -0800387 //Assuming eventRate is in microsecond unit
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800388 if (countEvent <= CHECK_DURATION * 1000000 / eventRate) {
Ayaka Koshibed2c7ad22015-02-13 16:44:07 -0800389 for (LinkDescription desc : linkDescrs.get(myDev.id())) {
suibin1a7b7bd2015-02-12 09:41:47 -0800390 providerService.linkVanished(desc);
391 countEvent++;
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800392 sleepFor(eventRate);
suibin1a7b7bd2015-02-12 09:41:47 -0800393 providerService.linkDetected(desc);
394 countEvent++;
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800395 sleepFor(eventRate);
suibin1a7b7bd2015-02-12 09:41:47 -0800396 }
397 } else {
398 // log in WARN the effective load generation rate in events/sec, every 10 seconds
Ayaka Koshibed2c7ad22015-02-13 16:44:07 -0800399 effLoad = (float) (countEvent * 1000.0 /
400 (System.currentTimeMillis() - startTime));
401 log.warn("Effective Loading for thread is {} events/second",
402 String.valueOf(effLoad));
suibin1a7b7bd2015-02-12 09:41:47 -0800403 countEvent = 0;
404 startTime = System.currentTimeMillis();
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800405 }
406 }
407 }
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800408
409 private void refresh() {
410 while (!linkDriver.isShutdown() && driverMap.get(myDev.id())) {
411 if (flicker) {
412 break;
413 }
414 for (LinkDescription desc : linkDescrs.get(myDev.id())) {
415 providerService.linkDetected(desc);
416 sleepFor(REFRESH_RATE);
417 }
418 }
419 }
420
421 private void sleepFor(int time) {
422 try {
423 TimeUnit.MICROSECONDS.sleep(time);
424 } catch (InterruptedException e) {
425 log.warn(String.valueOf(e));
426 }
427 }
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800428 }
Ayaka Koshibe839a8a92015-03-03 17:07:22 -0800429
Ayaka Koshibe422916f2015-01-15 15:30:23 -0800430}