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 | |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 18 | import com.google.common.collect.HashMultimap; |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 20 | import com.google.common.collect.Maps; |
| 21 | import com.google.common.collect.Sets; |
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; |
Brian O'Connor | b34ae26 | 2015-03-13 18:59:34 -0700 | [diff] [blame^] | 29 | import org.onlab.util.Tools; |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 30 | import org.onosproject.cfg.ComponentConfigService; |
Ayaka Koshibe | 8eddc0d | 2015-02-02 18:07:29 -0800 | [diff] [blame] | 31 | import org.onosproject.cluster.ClusterService; |
| 32 | import org.onosproject.cluster.NodeId; |
Ayaka Koshibe | 0cd4282 | 2015-01-22 16:02:31 -0800 | [diff] [blame] | 33 | import org.onosproject.mastership.MastershipService; |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 34 | import org.onosproject.net.ConnectPoint; |
| 35 | import org.onosproject.net.Device; |
| 36 | import org.onosproject.net.DeviceId; |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 37 | import org.onosproject.net.PortNumber; |
| 38 | import org.onosproject.net.device.DeviceEvent; |
| 39 | import org.onosproject.net.device.DeviceListener; |
| 40 | import org.onosproject.net.device.DeviceService; |
| 41 | import org.onosproject.net.link.DefaultLinkDescription; |
| 42 | import org.onosproject.net.link.LinkDescription; |
| 43 | import org.onosproject.net.link.LinkProvider; |
| 44 | import org.onosproject.net.link.LinkProviderRegistry; |
| 45 | import org.onosproject.net.link.LinkProviderService; |
| 46 | import org.onosproject.net.provider.AbstractProvider; |
| 47 | import org.onosproject.net.provider.ProviderId; |
| 48 | import org.osgi.service.component.ComponentContext; |
| 49 | import org.slf4j.Logger; |
| 50 | |
Brian O'Connor | b34ae26 | 2015-03-13 18:59:34 -0700 | [diff] [blame^] | 51 | import java.io.BufferedReader; |
| 52 | import java.io.FileReader; |
| 53 | import java.io.IOException; |
| 54 | import java.net.URI; |
| 55 | import java.net.URISyntaxException; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 56 | import java.util.Dictionary; |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 57 | import java.util.List; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 58 | import java.util.Set; |
| 59 | import java.util.concurrent.ConcurrentMap; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 60 | import java.util.concurrent.Executors; |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 61 | import java.util.concurrent.ScheduledExecutorService; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 62 | import java.util.concurrent.TimeUnit; |
| 63 | |
| 64 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 65 | import static org.onlab.util.Tools.groupedThreads; |
| 66 | import static org.onlab.util.Tools.toHex; |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 67 | import static org.onosproject.net.Link.Type.DIRECT; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 68 | import static org.slf4j.LoggerFactory.getLogger; |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * Provider which advertises fake/nonexistent links to the core. To be used for |
| 72 | * benchmarking only. |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 73 | * |
| 74 | * This provider takes a topology graph file with a DOT-like syntax. |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 75 | */ |
| 76 | @Component(immediate = true) |
| 77 | public class NullLinkProvider extends AbstractProvider implements LinkProvider { |
| 78 | |
| 79 | private final Logger log = getLogger(getClass()); |
| 80 | |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 81 | // default topology file location and name. |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 82 | private static final String CFG_PATH = "/opt/onos/apache-karaf-3.0.2/etc/linkGraph.cfg"; |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 83 | // default number of workers. Eventually make this tunable |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 84 | private static final int THREADS = (int) Math.max(1, Runtime.getRuntime().availableProcessors() * 0.8); |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 85 | |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 86 | 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 Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 90 | private static final DeviceId DEFAULT = DeviceId.deviceId("null:ffffffffffffffff"); |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 91 | |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 92 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 93 | protected DeviceService deviceService; |
| 94 | |
| 95 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | 0cd4282 | 2015-01-22 16:02:31 -0800 | [diff] [blame] | 96 | protected MastershipService roleService; |
| 97 | |
| 98 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | 8eddc0d | 2015-02-02 18:07:29 -0800 | [diff] [blame] | 99 | protected ClusterService nodeService; |
| 100 | |
| 101 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 102 | protected LinkProviderRegistry providerRegistry; |
| 103 | |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 104 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 105 | protected ComponentConfigService cfgService; |
| 106 | |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 107 | private LinkProviderService providerService; |
| 108 | |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 109 | private final InternalLinkProvider linkProvider = new InternalLinkProvider(); |
| 110 | |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 111 | // Mapping between device and drivers that advertise links from device |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 112 | private final ConcurrentMap<DeviceId, Set<LinkDriver>> driverMap = Maps |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 113 | .newConcurrentMap(); |
| 114 | |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 115 | // Link descriptions |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 116 | private final List<LinkDescription> linkDescrs = Lists.newArrayList(); |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 117 | |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 118 | // Thread to description map for dividing links amongst threads in flicker mode |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 119 | private final List<List<LinkDescription>> linkTasks = Lists.newArrayList(); |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 120 | |
| 121 | private ScheduledExecutorService linkDriver = |
| 122 | Executors.newScheduledThreadPool(THREADS, groupedThreads("onos/null", "link-driver-%d")); |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 123 | |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 124 | // For flicker = true, duration between events in msec. |
Thomas Vachuska | 3181de3 | 2015-03-11 10:07:05 -0700 | [diff] [blame] | 125 | @Property(name = "eventRate", intValue = DEFAULT_RATE, label = "Duration between Link Event") |
Ayaka Koshibe | 0cd4282 | 2015-01-22 16:02:31 -0800 | [diff] [blame] | 126 | private int eventRate = DEFAULT_RATE; |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 127 | |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 128 | // topology configuration file |
Thomas Vachuska | 3181de3 | 2015-03-11 10:07:05 -0700 | [diff] [blame] | 129 | @Property(name = "cfgFile", value = CFG_PATH, label = "Topology file location") |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 130 | private String cfgFile = CFG_PATH; |
Ayaka Koshibe | 8eddc0d | 2015-02-02 18:07:29 -0800 | [diff] [blame] | 131 | |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 132 | // flag checked to create a LinkDriver, if rate is non-zero. |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 133 | private volatile boolean flicker = false; |
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 | public NullLinkProvider() { |
| 136 | super(new ProviderId("null", "org.onosproject.provider.nil")); |
| 137 | } |
| 138 | |
| 139 | @Activate |
| 140 | public void activate(ComponentContext context) { |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 141 | cfgService.registerProperties(getClass()); |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 142 | providerService = providerRegistry.register(this); |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 143 | modified(context); |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 144 | log.info("started"); |
| 145 | } |
| 146 | |
| 147 | @Deactivate |
| 148 | public void deactivate(ComponentContext context) { |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 149 | cfgService.unregisterProperties(getClass(), false); |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 150 | linkDriver.shutdown(); |
| 151 | try { |
| 152 | linkDriver.awaitTermination(1000, TimeUnit.MILLISECONDS); |
| 153 | } catch (InterruptedException e) { |
| 154 | log.error("LinkBuilder did not terminate"); |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 155 | linkDriver.shutdownNow(); |
| 156 | } |
| 157 | deviceService.removeListener(linkProvider); |
| 158 | providerRegistry.unregister(this); |
| 159 | deviceService = null; |
| 160 | |
| 161 | log.info("stopped"); |
| 162 | } |
| 163 | |
Ayaka Koshibe | 7dc1d04 | 2015-01-21 15:28:03 -0800 | [diff] [blame] | 164 | @Modified |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 165 | public void modified(ComponentContext context) { |
| 166 | if (context == null) { |
Ayaka Koshibe | 8eddc0d | 2015-02-02 18:07:29 -0800 | [diff] [blame] | 167 | log.info("No configs, using defaults: eventRate={}", DEFAULT_RATE); |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 168 | return; |
| 169 | } |
| 170 | Dictionary<?, ?> properties = context.getProperties(); |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 171 | int newRate; |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 172 | String newPath; |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 173 | try { |
Ayaka Koshibe | 8eddc0d | 2015-02-02 18:07:29 -0800 | [diff] [blame] | 174 | String s = (String) properties.get("eventRate"); |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 175 | newRate = isNullOrEmpty(s) ? DEFAULT_RATE : Integer.parseInt(s.trim()); |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 176 | s = (String) properties.get("cfgFile"); |
| 177 | newPath = s.trim(); |
Pavlin Radoslavov | b9e50df | 2015-02-20 20:01:26 -0800 | [diff] [blame] | 178 | } catch (NumberFormatException | ClassCastException e) { |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 179 | log.warn(e.getMessage()); |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 180 | newRate = eventRate; |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 181 | newPath = cfgFile; |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 184 | // topology file configuration |
| 185 | if (!newPath.equals(cfgFile)) { |
| 186 | cfgFile = newPath; |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 187 | } |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 188 | readGraph(cfgFile, nodeService.getLocalNode().id()); |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 189 | 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 Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 224 | flicker = false; |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 225 | LinkDriver driver = new LinkDriver(linkDescrs); |
| 226 | driverMap.put(DEFAULT, Sets.newHashSet(driver)); |
| 227 | linkDriver.schedule(driver, REFRESH_RATE, TimeUnit.SECONDS); |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 228 | } |
Ayaka Koshibe | 8eddc0d | 2015-02-02 18:07:29 -0800 | [diff] [blame] | 229 | |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 230 | log.info("Using settings: eventRate={}, topofile={}", eventRate, cfgFile); |
Ayaka Koshibe | 8eddc0d | 2015-02-02 18:07:29 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 233 | // parse simplified dot-like topology graph |
| 234 | private void readGraph(String path, NodeId me) { |
| 235 | log.info("path: {}, local: {}", path, me); |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 236 | Set<LinkDescription> read = Sets.newHashSet(); |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 237 | 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 Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 258 | readLink(cur.trim().split(" "), me, read); |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 259 | 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'Connor | b34ae26 | 2015-03-13 18:59:34 -0700 | [diff] [blame^] | 283 | Set<LinkDescription> removedLinks = null; |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 284 | synchronized (linkDescrs) { |
| 285 | if (!read.isEmpty()) { |
Brian O'Connor | b34ae26 | 2015-03-13 18:59:34 -0700 | [diff] [blame^] | 286 | removedLinks = Sets.difference(Sets.newHashSet(linkDescrs), read); |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 287 | linkDescrs.clear(); |
| 288 | linkDescrs.addAll(read); |
| 289 | } |
| 290 | } |
Brian O'Connor | b34ae26 | 2015-03-13 18:59:34 -0700 | [diff] [blame^] | 291 | if (!Tools.isNullOrEmpty(removedLinks)) { |
| 292 | log.info("Removing {} old link(s)", removedLinks.size()); |
| 293 | removedLinks.forEach(providerService::linkVanished); |
| 294 | } |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | // parses a link descriptor to make a LinkDescription |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 298 | private void readLink(String[] linkArr, NodeId me, Set<LinkDescription> links) { |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 299 | 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 Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 313 | 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 Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 331 | // both link types have incoming half-link |
| 332 | LinkDescription in = new DefaultLinkDescription(dst, src, DIRECT); |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 333 | links.add(in); |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 334 | if (op.equals("--")) { |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 335 | // bidirectional - within our node's island, make outbound link |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 336 | LinkDescription out = new DefaultLinkDescription(src, dst, DIRECT); |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 337 | links.add(out); |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 338 | log.info("Created bidirectional link: {}, {}", out, in); |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 339 | } else if (op.equals("->")) { |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 340 | 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 Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 352 | 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 Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 360 | // 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 Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 367 | linkTasks.add(index, Lists.newArrayList(ld)); |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 368 | } else { |
| 369 | linkTasks.get(index).add(ld); |
| 370 | } |
| 371 | lcount++; |
| 372 | } |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 373 | } |
| 374 | |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 375 | /** |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 376 | * Generate LinkEvents using configurations when devices are found. |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 377 | */ |
| 378 | private class InternalLinkProvider implements DeviceListener { |
| 379 | |
| 380 | @Override |
| 381 | public void event(DeviceEvent event) { |
Ayaka Koshibe | 0cd4282 | 2015-01-22 16:02:31 -0800 | [diff] [blame] | 382 | Device dev = event.subject(); |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 383 | switch (event.type()) { |
| 384 | case DEVICE_ADDED: |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 385 | // TODO: wait for all devices to stop core from balking |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 386 | break; |
| 387 | case DEVICE_REMOVED: |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 388 | for (LinkDriver d : driverMap.get(dev.id())) { |
| 389 | d.deviceRemoved(dev.id()); |
Ayaka Koshibe | 35c71e1 | 2015-01-27 17:10:04 -0800 | [diff] [blame] | 390 | } |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 391 | providerService.linksVanished(dev.id()); |
Ayaka Koshibe | 35c71e1 | 2015-01-27 17:10:04 -0800 | [diff] [blame] | 392 | break; |
| 393 | default: |
| 394 | break; |
| 395 | } |
| 396 | } |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Generates link events using fake links. |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 401 | * TODO: stats collection should be its own thing. |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 402 | */ |
| 403 | private class LinkDriver implements Runnable { |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 404 | // List to actually work off of |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 405 | List<LinkDescription> tasks = Lists.newCopyOnWriteArrayList(); |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 406 | float effLoad = 0; |
| 407 | Long counter = 0L; |
| 408 | int next = 0; |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 409 | boolean up = true; |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 410 | |
| 411 | long startTime; |
| 412 | |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 413 | LinkDriver(List<LinkDescription> links) { |
| 414 | setTasks(links); |
| 415 | startTime = System.currentTimeMillis(); // yes, this will start off inaccurate |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 416 | } |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 417 | |
| 418 | @Override |
| 419 | public void run() { |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 420 | if (flicker) { |
| 421 | flicker(); |
| 422 | } else { |
| 423 | refresh(); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | private void flicker() { |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 428 | if ((!linkDriver.isShutdown() || !tasks.isEmpty())) { |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 429 | log.trace("next: {}, count: {}", next, counter); |
| 430 | if (counter <= CHECK_DURATION * 1_000_000 / eventRate) { |
| 431 | if (up) { |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 432 | providerService.linkDetected(tasks.get(next++)); |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 433 | } else { |
| 434 | providerService.linkVanished(tasks.get(next++)); |
suibin | 1a7b7bd | 2015-02-12 09:41:47 -0800 | [diff] [blame] | 435 | } |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 436 | if (next >= tasks.size()) { |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 437 | next = 0; |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 438 | up = !up; |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 439 | } |
| 440 | counter++; |
suibin | 1a7b7bd | 2015-02-12 09:41:47 -0800 | [diff] [blame] | 441 | } else { |
| 442 | // log in WARN the effective load generation rate in events/sec, every 10 seconds |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 443 | effLoad = (float) (counter * 1000.0 / (System |
| 444 | .currentTimeMillis() - startTime)); |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 445 | log.warn("Effective Loading for thread is {} events/second", |
| 446 | String.valueOf(effLoad)); |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 447 | counter = 0L; |
suibin | 1a7b7bd | 2015-02-12 09:41:47 -0800 | [diff] [blame] | 448 | startTime = System.currentTimeMillis(); |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 449 | } |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 450 | linkDriver.schedule(this, eventRate, TimeUnit.MICROSECONDS); |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 451 | } |
| 452 | } |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 453 | |
| 454 | private void refresh() { |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 455 | if (!linkDriver.isShutdown() || !tasks.isEmpty()) { |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 456 | log.trace("iter {} refresh_links", counter); |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 457 | |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 458 | for (LinkDescription desc : tasks) { |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 459 | providerService.linkDetected(desc); |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 460 | log.info("iteration {}, {}", counter, desc); |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 461 | } |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 462 | counter++; |
| 463 | linkDriver.schedule(this, REFRESH_RATE, TimeUnit.SECONDS); |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 467 | public void deviceRemoved(DeviceId did) { |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 468 | 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 Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 473 | } |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 474 | } |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 475 | tasks.removeAll(rm); |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 476 | } |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 477 | |
| 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 Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 482 | for (LinkDescription l : tasks) { |
| 483 | if (!nm.containsEntry(l.src(), l.dst())) { |
| 484 | providerService.linkVanished(l); |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 485 | } |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 486 | } |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 487 | tasks.clear(); |
| 488 | tasks.addAll(links); |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 489 | } |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 490 | } |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 491 | |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 492 | } |