Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.provider.nil.link.impl; |
| 17 | |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 18 | import com.google.common.collect.Maps; |
| 19 | import com.google.common.collect.Sets; |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame^] | 20 | |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 21 | import org.apache.commons.lang3.concurrent.ConcurrentUtils; |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
Ayaka Koshibe | 7dc1d04 | 2015-01-21 15:28:03 -0800 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Modified; |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Property; |
| 27 | import org.apache.felix.scr.annotations.Reference; |
| 28 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Ayaka Koshibe | 8eddc0d | 2015-02-02 18:07:29 -0800 | [diff] [blame] | 29 | import org.onosproject.cluster.ClusterService; |
| 30 | import org.onosproject.cluster.NodeId; |
Ayaka Koshibe | 0cd4282 | 2015-01-22 16:02:31 -0800 | [diff] [blame] | 31 | import org.onosproject.mastership.MastershipService; |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 32 | import org.onosproject.net.ConnectPoint; |
| 33 | import org.onosproject.net.Device; |
| 34 | import org.onosproject.net.DeviceId; |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 35 | import org.onosproject.net.PortNumber; |
| 36 | import org.onosproject.net.device.DeviceEvent; |
| 37 | import org.onosproject.net.device.DeviceListener; |
| 38 | import org.onosproject.net.device.DeviceService; |
| 39 | import org.onosproject.net.link.DefaultLinkDescription; |
| 40 | import org.onosproject.net.link.LinkDescription; |
| 41 | import org.onosproject.net.link.LinkProvider; |
| 42 | import org.onosproject.net.link.LinkProviderRegistry; |
| 43 | import org.onosproject.net.link.LinkProviderService; |
| 44 | import org.onosproject.net.provider.AbstractProvider; |
| 45 | import org.onosproject.net.provider.ProviderId; |
| 46 | import org.osgi.service.component.ComponentContext; |
| 47 | import org.slf4j.Logger; |
| 48 | |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 49 | import java.util.Dictionary; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 50 | import java.util.Set; |
| 51 | import java.util.concurrent.ConcurrentMap; |
| 52 | import java.util.concurrent.ExecutorService; |
| 53 | import java.util.concurrent.Executors; |
| 54 | import java.util.concurrent.TimeUnit; |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame^] | 55 | import java.io.BufferedReader; |
| 56 | import java.io.FileReader; |
| 57 | import java.io.IOException; |
| 58 | import java.net.URI; |
| 59 | import java.net.URISyntaxException; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 60 | |
| 61 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 62 | import static org.onlab.util.Tools.groupedThreads; |
| 63 | import static org.onlab.util.Tools.toHex; |
| 64 | import static org.onosproject.net.MastershipRole.MASTER; |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame^] | 65 | import static org.onosproject.net.Link.Type.DIRECT; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 66 | import static org.slf4j.LoggerFactory.getLogger; |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Provider which advertises fake/nonexistent links to the core. To be used for |
| 70 | * benchmarking only. |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame^] | 71 | * |
| 72 | * This provider takes a topology graph file with a DOT-like syntax. |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 73 | */ |
| 74 | @Component(immediate = true) |
| 75 | public class NullLinkProvider extends AbstractProvider implements LinkProvider { |
| 76 | |
| 77 | private final Logger log = getLogger(getClass()); |
| 78 | |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame^] | 79 | 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 Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 86 | protected DeviceService deviceService; |
| 87 | |
| 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | 0cd4282 | 2015-01-22 16:02:31 -0800 | [diff] [blame] | 89 | protected MastershipService roleService; |
| 90 | |
| 91 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | 8eddc0d | 2015-02-02 18:07:29 -0800 | [diff] [blame] | 92 | protected ClusterService nodeService; |
| 93 | |
| 94 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 95 | protected LinkProviderRegistry providerRegistry; |
| 96 | |
| 97 | private LinkProviderService providerService; |
| 98 | |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 99 | private final InternalLinkProvider linkProvider = new InternalLinkProvider(); |
| 100 | |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 101 | // True for device with Driver, false otherwise. |
| 102 | private final ConcurrentMap<DeviceId, Boolean> driverMap = Maps |
| 103 | .newConcurrentMap(); |
| 104 | |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 105 | // Link descriptions |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 106 | private final ConcurrentMap<DeviceId, Set<LinkDescription>> linkDescrs = Maps |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 107 | .newConcurrentMap(); |
| 108 | |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 109 | private ExecutorService linkDriver = |
| 110 | Executors.newCachedThreadPool(groupedThreads("onos/null", "link-driver-%d")); |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 111 | |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 112 | // For flicker = true, duration between events in msec. |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame^] | 113 | @Property(name = "eventRate", value = "0", label = "Duration between Link Event") |
Ayaka Koshibe | 0cd4282 | 2015-01-22 16:02:31 -0800 | [diff] [blame] | 114 | private int eventRate = DEFAULT_RATE; |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 115 | |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame^] | 116 | // 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 Koshibe | 8eddc0d | 2015-02-02 18:07:29 -0800 | [diff] [blame] | 121 | |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 122 | // flag checked to create a LinkDriver, if rate is non-zero. |
| 123 | private boolean flicker = false; |
| 124 | |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 125 | 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 Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 132 | modified(context); |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 133 | deviceService.addListener(linkProvider); |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 134 | |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 135 | log.info("started"); |
| 136 | } |
| 137 | |
| 138 | @Deactivate |
| 139 | public void deactivate(ComponentContext context) { |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 140 | linkDriver.shutdown(); |
| 141 | try { |
| 142 | linkDriver.awaitTermination(1000, TimeUnit.MILLISECONDS); |
| 143 | } catch (InterruptedException e) { |
| 144 | log.error("LinkBuilder did not terminate"); |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 145 | linkDriver.shutdownNow(); |
| 146 | } |
| 147 | deviceService.removeListener(linkProvider); |
| 148 | providerRegistry.unregister(this); |
| 149 | deviceService = null; |
| 150 | |
| 151 | log.info("stopped"); |
| 152 | } |
| 153 | |
Ayaka Koshibe | 7dc1d04 | 2015-01-21 15:28:03 -0800 | [diff] [blame] | 154 | @Modified |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 155 | public void modified(ComponentContext context) { |
| 156 | if (context == null) { |
Ayaka Koshibe | 8eddc0d | 2015-02-02 18:07:29 -0800 | [diff] [blame] | 157 | log.info("No configs, using defaults: eventRate={}", DEFAULT_RATE); |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 158 | return; |
| 159 | } |
| 160 | Dictionary<?, ?> properties = context.getProperties(); |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 161 | int newRate; |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame^] | 162 | String newPath; |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 163 | try { |
Ayaka Koshibe | 8eddc0d | 2015-02-02 18:07:29 -0800 | [diff] [blame] | 164 | String s = (String) properties.get("eventRate"); |
Yuta HIGUCHI | dd9228d | 2015-02-10 22:26:59 -0800 | [diff] [blame] | 165 | newRate = isNullOrEmpty(s) ? eventRate : Integer.parseInt(s.trim()); |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame^] | 166 | s = (String) properties.get("cfgFile"); |
| 167 | newPath = s.trim(); |
Pavlin Radoslavov | b9e50df | 2015-02-20 20:01:26 -0800 | [diff] [blame] | 168 | } catch (NumberFormatException | ClassCastException e) { |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 169 | log.warn(e.getMessage()); |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 170 | newRate = eventRate; |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame^] | 171 | newPath = cfgFile; |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame^] | 174 | // topology file configuration |
| 175 | if (!newPath.equals(cfgFile)) { |
| 176 | cfgFile = newPath; |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 177 | } |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame^] | 178 | readGraph(cfgFile, nodeService.getLocalNode().id()); |
| 179 | |
| 180 | // test mode configuration |
| 181 | if (eventRate != newRate && newRate > 0) { |
| 182 | driverMap.replaceAll((k, v) -> false); |
Ayaka Koshibe | 8eddc0d | 2015-02-02 18:07:29 -0800 | [diff] [blame] | 183 | eventRate = newRate; |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 184 | flicker = true; |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 185 | } else if (newRate == 0) { |
| 186 | driverMap.replaceAll((k, v) -> false); |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame^] | 187 | flicker = false; |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 188 | } |
Ayaka Koshibe | 8eddc0d | 2015-02-02 18:07:29 -0800 | [diff] [blame] | 189 | |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame^] | 190 | 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 Koshibe | 8eddc0d | 2015-02-02 18:07:29 -0800 | [diff] [blame] | 198 | } |
| 199 | } |
Ayaka Koshibe | 8eddc0d | 2015-02-02 18:07:29 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame^] | 202 | // 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 Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 322 | 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 Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 328 | /** |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame^] | 329 | * Generate LinkEvents using configurations when devices are found. |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 330 | */ |
| 331 | private class InternalLinkProvider implements DeviceListener { |
| 332 | |
| 333 | @Override |
| 334 | public void event(DeviceEvent event) { |
Ayaka Koshibe | 0cd4282 | 2015-01-22 16:02:31 -0800 | [diff] [blame] | 335 | Device dev = event.subject(); |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 336 | switch (event.type()) { |
| 337 | case DEVICE_ADDED: |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 338 | synchronized (this) { |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame^] | 339 | if (!driverMap.getOrDefault(dev.id(), false)) { |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 340 | driverMap.put(dev.id(), true); |
| 341 | linkDriver.submit(new LinkDriver(dev)); |
| 342 | } |
| 343 | } |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 344 | break; |
| 345 | case DEVICE_REMOVED: |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 346 | driverMap.put(dev.id(), false); |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame^] | 347 | if (!MASTER.equals(roleService.getLocalRole(dev.id()))) { |
Ayaka Koshibe | 35c71e1 | 2015-01-27 17:10:04 -0800 | [diff] [blame] | 348 | return; |
| 349 | } |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame^] | 350 | // no need to remove static links, just stop advertising them |
| 351 | providerService.linksVanished(dev.id()); |
Ayaka Koshibe | 35c71e1 | 2015-01-27 17:10:04 -0800 | [diff] [blame] | 352 | break; |
| 353 | default: |
| 354 | break; |
| 355 | } |
| 356 | } |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Generates link events using fake links. |
| 361 | */ |
| 362 | private class LinkDriver implements Runnable { |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 363 | Device myDev; |
| 364 | LinkDriver(Device dev) { |
| 365 | myDev = dev; |
| 366 | } |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 367 | |
| 368 | @Override |
| 369 | public void run() { |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 370 | log.info("Thread started for dev {}", myDev.id()); |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame^] | 371 | if (flicker) { |
| 372 | flicker(); |
| 373 | } else { |
| 374 | refresh(); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | private void flicker() { |
suibin | 1a7b7bd | 2015-02-12 09:41:47 -0800 | [diff] [blame] | 379 | long startTime = System.currentTimeMillis(); |
| 380 | long countEvent = 0; |
| 381 | float effLoad = 0; |
| 382 | |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 383 | while (!linkDriver.isShutdown() && driverMap.get(myDev.id())) { |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame^] | 384 | if (!flicker) { |
| 385 | break; |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 386 | } |
suibin | 1a7b7bd | 2015-02-12 09:41:47 -0800 | [diff] [blame] | 387 | //Assuming eventRate is in microsecond unit |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame^] | 388 | if (countEvent <= CHECK_DURATION * 1000000 / eventRate) { |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 389 | for (LinkDescription desc : linkDescrs.get(myDev.id())) { |
suibin | 1a7b7bd | 2015-02-12 09:41:47 -0800 | [diff] [blame] | 390 | providerService.linkVanished(desc); |
| 391 | countEvent++; |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame^] | 392 | sleepFor(eventRate); |
suibin | 1a7b7bd | 2015-02-12 09:41:47 -0800 | [diff] [blame] | 393 | providerService.linkDetected(desc); |
| 394 | countEvent++; |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame^] | 395 | sleepFor(eventRate); |
suibin | 1a7b7bd | 2015-02-12 09:41:47 -0800 | [diff] [blame] | 396 | } |
| 397 | } else { |
| 398 | // log in WARN the effective load generation rate in events/sec, every 10 seconds |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 399 | effLoad = (float) (countEvent * 1000.0 / |
| 400 | (System.currentTimeMillis() - startTime)); |
| 401 | log.warn("Effective Loading for thread is {} events/second", |
| 402 | String.valueOf(effLoad)); |
suibin | 1a7b7bd | 2015-02-12 09:41:47 -0800 | [diff] [blame] | 403 | countEvent = 0; |
| 404 | startTime = System.currentTimeMillis(); |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | } |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame^] | 408 | |
| 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 Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 428 | } |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame^] | 429 | |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 430 | } |