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 | ef69be3 | 2015-03-15 22:29:19 -0700 | [diff] [blame] | 18 | import com.google.common.base.Charsets; |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 19 | import com.google.common.collect.HashMultimap; |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Lists; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 21 | import com.google.common.collect.Maps; |
| 22 | import com.google.common.collect.Sets; |
Ayaka Koshibe | ef69be3 | 2015-03-15 22:29:19 -0700 | [diff] [blame] | 23 | import com.google.common.io.Files; |
| 24 | |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Activate; |
| 26 | import org.apache.felix.scr.annotations.Component; |
| 27 | import org.apache.felix.scr.annotations.Deactivate; |
Ayaka Koshibe | 7dc1d04 | 2015-01-21 15:28:03 -0800 | [diff] [blame] | 28 | import org.apache.felix.scr.annotations.Modified; |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 29 | import org.apache.felix.scr.annotations.Property; |
| 30 | import org.apache.felix.scr.annotations.Reference; |
| 31 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 32 | import org.onosproject.cfg.ComponentConfigService; |
Ayaka Koshibe | 8eddc0d | 2015-02-02 18:07:29 -0800 | [diff] [blame] | 33 | import org.onosproject.cluster.ClusterService; |
| 34 | import org.onosproject.cluster.NodeId; |
Ayaka Koshibe | 0cd4282 | 2015-01-22 16:02:31 -0800 | [diff] [blame] | 35 | import org.onosproject.mastership.MastershipService; |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 36 | import org.onosproject.net.ConnectPoint; |
| 37 | import org.onosproject.net.Device; |
| 38 | import org.onosproject.net.DeviceId; |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 39 | import org.onosproject.net.PortNumber; |
| 40 | import org.onosproject.net.device.DeviceEvent; |
| 41 | import org.onosproject.net.device.DeviceListener; |
| 42 | import org.onosproject.net.device.DeviceService; |
| 43 | import org.onosproject.net.link.DefaultLinkDescription; |
| 44 | import org.onosproject.net.link.LinkDescription; |
| 45 | import org.onosproject.net.link.LinkProvider; |
| 46 | import org.onosproject.net.link.LinkProviderRegistry; |
| 47 | import org.onosproject.net.link.LinkProviderService; |
| 48 | import org.onosproject.net.provider.AbstractProvider; |
| 49 | import org.onosproject.net.provider.ProviderId; |
| 50 | import org.osgi.service.component.ComponentContext; |
| 51 | import org.slf4j.Logger; |
| 52 | |
Brian O'Connor | b34ae26 | 2015-03-13 18:59:34 -0700 | [diff] [blame] | 53 | import java.io.BufferedReader; |
Ayaka Koshibe | ef69be3 | 2015-03-15 22:29:19 -0700 | [diff] [blame] | 54 | import java.io.File; |
Brian O'Connor | b34ae26 | 2015-03-13 18:59:34 -0700 | [diff] [blame] | 55 | import java.io.IOException; |
| 56 | import java.net.URI; |
| 57 | import java.net.URISyntaxException; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 58 | import java.util.Dictionary; |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 59 | import java.util.List; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 60 | import java.util.Set; |
| 61 | import java.util.concurrent.ConcurrentMap; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 62 | import java.util.concurrent.Executors; |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 63 | import java.util.concurrent.ScheduledExecutorService; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 64 | import java.util.concurrent.TimeUnit; |
| 65 | |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 66 | import static org.onlab.util.Tools.groupedThreads; |
Ayaka Koshibe | ef69be3 | 2015-03-15 22:29:19 -0700 | [diff] [blame] | 67 | import static org.onlab.util.Tools.get; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 68 | import static org.onlab.util.Tools.toHex; |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 69 | import static org.onosproject.net.Link.Type.DIRECT; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 70 | import static org.slf4j.LoggerFactory.getLogger; |
Ayaka Koshibe | ef69be3 | 2015-03-15 22:29:19 -0700 | [diff] [blame] | 71 | import static com.google.common.base.Strings.isNullOrEmpty; |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * Provider which advertises fake/nonexistent links to the core. To be used for |
| 75 | * benchmarking only. |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 76 | * |
| 77 | * This provider takes a topology graph file with a DOT-like syntax. |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 78 | */ |
| 79 | @Component(immediate = true) |
| 80 | public class NullLinkProvider extends AbstractProvider implements LinkProvider { |
| 81 | |
| 82 | private final Logger log = getLogger(getClass()); |
| 83 | |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 84 | // default topology file location and name. |
Thomas Vachuska | 5630c61 | 2015-03-24 12:24:12 -0700 | [diff] [blame] | 85 | private static final String CFG_PATH = "etc/linkGraph.cfg"; |
| 86 | |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 87 | // default number of workers. Eventually make this tunable |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 88 | 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] | 89 | |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 90 | private static final int CHECK_DURATION = 10; // sec |
| 91 | private static final int DEFAULT_RATE = 0; // usec |
| 92 | private static final int REFRESH_RATE = 3; // sec |
| 93 | // Fake device used for non-flickering thread in deviceMap |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 94 | private static final DeviceId DEFAULT = DeviceId.deviceId("null:ffffffffffffffff"); |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 95 | |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 97 | protected DeviceService deviceService; |
| 98 | |
| 99 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | 0cd4282 | 2015-01-22 16:02:31 -0800 | [diff] [blame] | 100 | protected MastershipService roleService; |
| 101 | |
| 102 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | 8eddc0d | 2015-02-02 18:07:29 -0800 | [diff] [blame] | 103 | protected ClusterService nodeService; |
| 104 | |
| 105 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 106 | protected LinkProviderRegistry providerRegistry; |
| 107 | |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 108 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 109 | protected ComponentConfigService cfgService; |
| 110 | |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 111 | private LinkProviderService providerService; |
| 112 | |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 113 | private final InternalLinkProvider linkProvider = new InternalLinkProvider(); |
| 114 | |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 115 | // Mapping between device and drivers that advertise links from device |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 116 | private final ConcurrentMap<DeviceId, Set<LinkDriver>> driverMap = Maps |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 117 | .newConcurrentMap(); |
| 118 | |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 119 | // Link descriptions |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 120 | private final List<LinkDescription> linkDescrs = Lists.newArrayList(); |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 121 | |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 122 | // Thread to description map for dividing links amongst threads in flicker mode |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 123 | private final List<List<LinkDescription>> linkTasks = Lists.newArrayList(); |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 124 | |
| 125 | private ScheduledExecutorService linkDriver = |
| 126 | Executors.newScheduledThreadPool(THREADS, groupedThreads("onos/null", "link-driver-%d")); |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 127 | |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 128 | // For flicker = true, duration between events in msec. |
Thomas Vachuska | 3181de3 | 2015-03-11 10:07:05 -0700 | [diff] [blame] | 129 | @Property(name = "eventRate", intValue = DEFAULT_RATE, label = "Duration between Link Event") |
Ayaka Koshibe | 0cd4282 | 2015-01-22 16:02:31 -0800 | [diff] [blame] | 130 | private int eventRate = DEFAULT_RATE; |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 131 | |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 132 | // topology configuration file |
Thomas Vachuska | 3181de3 | 2015-03-11 10:07:05 -0700 | [diff] [blame] | 133 | @Property(name = "cfgFile", value = CFG_PATH, label = "Topology file location") |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 134 | private String cfgFile = CFG_PATH; |
Ayaka Koshibe | 8eddc0d | 2015-02-02 18:07:29 -0800 | [diff] [blame] | 135 | |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 136 | // flag checked to create a LinkDriver, if rate is non-zero. |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 137 | private volatile boolean flicker = false; |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 138 | |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 139 | public NullLinkProvider() { |
| 140 | super(new ProviderId("null", "org.onosproject.provider.nil")); |
| 141 | } |
| 142 | |
| 143 | @Activate |
| 144 | public void activate(ComponentContext context) { |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 145 | cfgService.registerProperties(getClass()); |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 146 | providerService = providerRegistry.register(this); |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 147 | modified(context); |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 148 | log.info("started"); |
| 149 | } |
| 150 | |
| 151 | @Deactivate |
| 152 | public void deactivate(ComponentContext context) { |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 153 | cfgService.unregisterProperties(getClass(), false); |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 154 | linkDriver.shutdown(); |
| 155 | try { |
| 156 | linkDriver.awaitTermination(1000, TimeUnit.MILLISECONDS); |
| 157 | } catch (InterruptedException e) { |
| 158 | log.error("LinkBuilder did not terminate"); |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 159 | linkDriver.shutdownNow(); |
| 160 | } |
| 161 | deviceService.removeListener(linkProvider); |
| 162 | providerRegistry.unregister(this); |
| 163 | deviceService = null; |
| 164 | |
| 165 | log.info("stopped"); |
| 166 | } |
| 167 | |
Ayaka Koshibe | 7dc1d04 | 2015-01-21 15:28:03 -0800 | [diff] [blame] | 168 | @Modified |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 169 | public void modified(ComponentContext context) { |
| 170 | if (context == null) { |
Ayaka Koshibe | 8eddc0d | 2015-02-02 18:07:29 -0800 | [diff] [blame] | 171 | log.info("No configs, using defaults: eventRate={}", DEFAULT_RATE); |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 172 | return; |
| 173 | } |
| 174 | Dictionary<?, ?> properties = context.getProperties(); |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 175 | int newRate; |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 176 | String newPath; |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 177 | try { |
Ayaka Koshibe | ef69be3 | 2015-03-15 22:29:19 -0700 | [diff] [blame] | 178 | String s = get(properties, "eventRate"); |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 179 | newRate = isNullOrEmpty(s) ? DEFAULT_RATE : Integer.parseInt(s.trim()); |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 180 | s = (String) properties.get("cfgFile"); |
Ayaka Koshibe | ef69be3 | 2015-03-15 22:29:19 -0700 | [diff] [blame] | 181 | newPath = isNullOrEmpty(s) ? CFG_PATH : s.trim(); |
| 182 | } catch (NumberFormatException e) { |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 183 | log.warn(e.getMessage()); |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 184 | newRate = eventRate; |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 185 | newPath = cfgFile; |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 186 | } |
Ayaka Koshibe | ef69be3 | 2015-03-15 22:29:19 -0700 | [diff] [blame] | 187 | // find/read topology file. |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 188 | if (!newPath.equals(cfgFile)) { |
| 189 | cfgFile = newPath; |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 190 | } |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 191 | readGraph(cfgFile, nodeService.getLocalNode().id()); |
Ayaka Koshibe | ef69be3 | 2015-03-15 22:29:19 -0700 | [diff] [blame] | 192 | // check for new eventRate settings. |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 193 | if (newRate != eventRate) { |
| 194 | if (eventRate < 0) { |
| 195 | log.warn("Invalid rate, ignoring and using default"); |
| 196 | eventRate = DEFAULT_RATE; |
| 197 | } else { |
| 198 | eventRate = newRate; |
| 199 | } |
| 200 | } |
Ayaka Koshibe | ef69be3 | 2015-03-15 22:29:19 -0700 | [diff] [blame] | 201 | configureWorkers(); |
| 202 | log.info("Using settings: eventRate={}, topofile={}", eventRate, cfgFile); |
| 203 | } |
| 204 | |
| 205 | // Configures and schedules worker threads based on settings. |
| 206 | private void configureWorkers() { |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 207 | if (eventRate > 0) { |
Ayaka Koshibe | ef69be3 | 2015-03-15 22:29:19 -0700 | [diff] [blame] | 208 | // now set to 'flicker', previously not flickering |
| 209 | if (!flicker) { |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 210 | flicker = true; |
| 211 | allocateLinks(); |
Ayaka Koshibe | ef69be3 | 2015-03-15 22:29:19 -0700 | [diff] [blame] | 212 | // kill off refresh worker for symmetry |
| 213 | if (driverMap.containsKey(DEFAULT)) { |
| 214 | driverMap.get(DEFAULT).forEach(d -> d.setTasks(Lists.newArrayList())); |
| 215 | driverMap.remove(DEFAULT); |
| 216 | } |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 217 | for (int i = 0; i < linkTasks.size(); i++) { |
| 218 | List<LinkDescription> links = linkTasks.get(i); |
| 219 | LinkDriver driver = new LinkDriver(links); |
| 220 | links.forEach(v -> { |
| 221 | DeviceId sd = v.src().deviceId(); |
| 222 | DeviceId dd = v.src().deviceId(); |
| 223 | driverMap.computeIfAbsent(sd, k -> Sets.newConcurrentHashSet()).add(driver); |
| 224 | driverMap.computeIfAbsent(dd, k -> Sets.newConcurrentHashSet()).add(driver); |
| 225 | }); |
Ayaka Koshibe | ef69be3 | 2015-03-15 22:29:19 -0700 | [diff] [blame] | 226 | linkDriver.schedule(driver, eventRate, TimeUnit.MICROSECONDS); |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
Ayaka Koshibe | ef69be3 | 2015-03-15 22:29:19 -0700 | [diff] [blame] | 229 | // no need for was flicker since eventRate will be read by workers |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 230 | } else { |
Ayaka Koshibe | ef69be3 | 2015-03-15 22:29:19 -0700 | [diff] [blame] | 231 | // now set to 'refresh' was 'flicker' before |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 232 | if (flicker) { |
| 233 | driverMap.forEach((dev, lds) -> lds.forEach(l -> l.deviceRemoved(dev))); |
| 234 | driverMap.clear(); |
| 235 | linkTasks.clear(); |
Ayaka Koshibe | ef69be3 | 2015-03-15 22:29:19 -0700 | [diff] [blame] | 236 | flicker = false; |
| 237 | LinkDriver driver = new LinkDriver(linkDescrs); |
| 238 | driverMap.computeIfAbsent(DEFAULT, k -> Sets.newConcurrentHashSet()).add(driver); |
| 239 | linkDriver.schedule(driver, DEFAULT_RATE, TimeUnit.SECONDS); |
| 240 | // was 'refresh' - something changed or we're just starting. |
| 241 | } else { |
| 242 | if (driverMap.containsKey(DEFAULT)) { |
| 243 | driverMap.forEach((dev, ld) -> ld.forEach(d -> d.setTasks(linkDescrs))); |
| 244 | return; |
| 245 | } |
| 246 | LinkDriver driver = new LinkDriver(linkDescrs); |
| 247 | driverMap.computeIfAbsent(DEFAULT, k -> Sets.newConcurrentHashSet()).add(driver); |
| 248 | linkDriver.schedule(driver, DEFAULT_RATE, TimeUnit.SECONDS); |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 249 | } |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 250 | } |
Ayaka Koshibe | 8eddc0d | 2015-02-02 18:07:29 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 253 | // parse simplified dot-like topology graph |
| 254 | private void readGraph(String path, NodeId me) { |
| 255 | log.info("path: {}, local: {}", path, me); |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 256 | Set<LinkDescription> read = Sets.newHashSet(); |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 257 | BufferedReader br = null; |
| 258 | try { |
Ayaka Koshibe | ef69be3 | 2015-03-15 22:29:19 -0700 | [diff] [blame] | 259 | br = Files.newReader(new File(path), Charsets.US_ASCII); |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 260 | String cur = br.readLine(); |
| 261 | while (cur != null) { |
| 262 | if (cur.startsWith("#")) { |
| 263 | cur = br.readLine(); |
| 264 | continue; |
| 265 | } |
| 266 | String[] parts = cur.trim().split(" "); |
| 267 | if (parts.length < 1) { |
| 268 | continue; |
| 269 | } |
| 270 | if (parts[0].equals("graph")) { |
| 271 | String node = parts[1].trim(); |
| 272 | if (node.equals(me.toString())) { |
| 273 | cur = br.readLine(); // move to next line, start of links list |
| 274 | while (cur != null) { |
| 275 | if (cur.trim().contains("}")) { |
| 276 | break; |
| 277 | } |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 278 | readLink(cur.trim().split(" "), me, read); |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 279 | cur = br.readLine(); |
| 280 | } |
| 281 | } else { |
| 282 | while (cur != null) { |
| 283 | if (cur.trim().equals("}")) { |
| 284 | break; |
| 285 | } |
| 286 | cur = br.readLine(); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | cur = br.readLine(); |
| 291 | } |
| 292 | } catch (IOException e) { |
| 293 | log.warn("Could not find topology file: {}", e); |
| 294 | } finally { |
| 295 | try { |
| 296 | if (br != null) { |
| 297 | br.close(); |
| 298 | } |
| 299 | } catch (IOException e) { |
| 300 | log.warn("Could not close topology file: {}", e); |
| 301 | } |
| 302 | } |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 303 | synchronized (linkDescrs) { |
| 304 | if (!read.isEmpty()) { |
| 305 | linkDescrs.clear(); |
| 306 | linkDescrs.addAll(read); |
| 307 | } |
| 308 | } |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | // parses a link descriptor to make a LinkDescription |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 312 | private void readLink(String[] linkArr, NodeId me, Set<LinkDescription> links) { |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 313 | if (linkArr[0].startsWith("#")) { |
| 314 | return; |
| 315 | } |
| 316 | if (linkArr.length != 3) { |
| 317 | log.warn("Malformed link descriptor:" |
| 318 | + " link should be of format src:port [--|->] dst:port," |
| 319 | + " skipping"); |
| 320 | return; |
| 321 | } |
| 322 | |
| 323 | String op = linkArr[1]; |
| 324 | String[] cp1 = linkArr[0].split(":"); |
| 325 | String[] cp2 = linkArr[2].split(":"); |
| 326 | |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 327 | if (cp1.length != 2 && (cp2.length != 2 || cp2.length != 3)) { |
| 328 | log.warn("Malformed endpoint descriptor(s):" |
| 329 | + "endpoint format should be DeviceId:port or DeviceId:port:NodeId," |
| 330 | + "skipping"); |
| 331 | return; |
| 332 | } |
| 333 | // read in hints about topology. |
| 334 | NodeId adj = null; |
| 335 | if (cp2.length == 3) { |
| 336 | adj = new NodeId(cp2[2]); |
| 337 | log.debug("found an island: {}", adj); |
| 338 | } |
| 339 | |
| 340 | // reconstruct deviceIDs. Convention is based on NullDeviceProvider. |
| 341 | DeviceId sdev = recover(cp1[0], me); |
| 342 | DeviceId ddev = (adj == null) ? recover(cp2[0], me) : recover(cp2[0], adj); |
| 343 | ConnectPoint src = new ConnectPoint(sdev, PortNumber.portNumber(cp1[1])); |
| 344 | ConnectPoint dst = new ConnectPoint(ddev, PortNumber.portNumber(cp2[1])); |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 345 | // both link types have incoming half-link |
| 346 | LinkDescription in = new DefaultLinkDescription(dst, src, DIRECT); |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 347 | links.add(in); |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 348 | if (op.equals("--")) { |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 349 | // bidirectional - within our node's island, make outbound link |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 350 | LinkDescription out = new DefaultLinkDescription(src, dst, DIRECT); |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 351 | links.add(out); |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 352 | log.info("Created bidirectional link: {}, {}", out, in); |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 353 | } else if (op.equals("->")) { |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 354 | log.info("Created unidirectional link: {}", in); |
| 355 | } else { |
| 356 | log.warn("Unknown link descriptor operand:" |
| 357 | + " operand must be '--' or '->', skipping"); |
| 358 | return; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | // recover DeviceId from configs and NodeID |
| 363 | private DeviceId recover(String base, NodeId node) { |
| 364 | long hash = node.hashCode() << 16; |
Ayaka Koshibe | ef69be3 | 2015-03-15 22:29:19 -0700 | [diff] [blame] | 365 | int dev = Integer.parseInt(base); |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 366 | try { |
| 367 | return DeviceId.deviceId(new URI("null", toHex(hash | dev), null)); |
| 368 | } catch (URISyntaxException e) { |
| 369 | log.warn("could not create a DeviceID for descriptor {}", dev); |
| 370 | return DeviceId.NONE; |
| 371 | } |
| 372 | } |
| 373 | |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 374 | // adds a LinkDescription to a worker's to-be queue, for flickering |
| 375 | private void allocateLinks() { |
| 376 | int index, lcount = 0; |
Ayaka Koshibe | ef69be3 | 2015-03-15 22:29:19 -0700 | [diff] [blame] | 377 | linkTasks.clear(); |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 378 | for (LinkDescription ld : linkDescrs) { |
| 379 | index = (lcount % THREADS); |
| 380 | log.info("allocation: total={}, index={}", linkDescrs.size(), lcount, index); |
| 381 | if (linkTasks.size() <= index) { |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 382 | linkTasks.add(index, Lists.newArrayList(ld)); |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 383 | } else { |
| 384 | linkTasks.get(index).add(ld); |
| 385 | } |
| 386 | lcount++; |
| 387 | } |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 388 | } |
| 389 | |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 390 | /** |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 391 | * Generate LinkEvents using configurations when devices are found. |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 392 | */ |
| 393 | private class InternalLinkProvider implements DeviceListener { |
| 394 | |
| 395 | @Override |
| 396 | public void event(DeviceEvent event) { |
Ayaka Koshibe | 0cd4282 | 2015-01-22 16:02:31 -0800 | [diff] [blame] | 397 | Device dev = event.subject(); |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 398 | switch (event.type()) { |
| 399 | case DEVICE_ADDED: |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 400 | // TODO: wait for all devices to stop core from balking |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 401 | break; |
| 402 | case DEVICE_REMOVED: |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 403 | for (LinkDriver d : driverMap.get(dev.id())) { |
| 404 | d.deviceRemoved(dev.id()); |
Ayaka Koshibe | 35c71e1 | 2015-01-27 17:10:04 -0800 | [diff] [blame] | 405 | } |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 406 | providerService.linksVanished(dev.id()); |
Ayaka Koshibe | 35c71e1 | 2015-01-27 17:10:04 -0800 | [diff] [blame] | 407 | break; |
| 408 | default: |
| 409 | break; |
| 410 | } |
| 411 | } |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Generates link events using fake links. |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 416 | * TODO: stats collection should be its own thing. |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 417 | */ |
| 418 | private class LinkDriver implements Runnable { |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 419 | // List to actually work off of |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 420 | List<LinkDescription> tasks = Lists.newCopyOnWriteArrayList(); |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 421 | float effLoad = 0; |
| 422 | Long counter = 0L; |
| 423 | int next = 0; |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 424 | boolean up = true; |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 425 | |
| 426 | long startTime; |
| 427 | |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 428 | LinkDriver(List<LinkDescription> links) { |
| 429 | setTasks(links); |
| 430 | startTime = System.currentTimeMillis(); // yes, this will start off inaccurate |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 431 | } |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 432 | |
| 433 | @Override |
| 434 | public void run() { |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 435 | if (flicker) { |
| 436 | flicker(); |
| 437 | } else { |
| 438 | refresh(); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | private void flicker() { |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 443 | if ((!linkDriver.isShutdown() || !tasks.isEmpty())) { |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 444 | log.trace("next: {}, count: {}", next, counter); |
| 445 | if (counter <= CHECK_DURATION * 1_000_000 / eventRate) { |
| 446 | if (up) { |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 447 | providerService.linkDetected(tasks.get(next++)); |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 448 | } else { |
| 449 | providerService.linkVanished(tasks.get(next++)); |
suibin | 1a7b7bd | 2015-02-12 09:41:47 -0800 | [diff] [blame] | 450 | } |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 451 | if (next >= tasks.size()) { |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 452 | next = 0; |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 453 | up = !up; |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 454 | } |
| 455 | counter++; |
suibin | 1a7b7bd | 2015-02-12 09:41:47 -0800 | [diff] [blame] | 456 | } else { |
| 457 | // 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] | 458 | effLoad = (float) (counter * 1000.0 / (System |
| 459 | .currentTimeMillis() - startTime)); |
Ayaka Koshibe | d2c7ad2 | 2015-02-13 16:44:07 -0800 | [diff] [blame] | 460 | log.warn("Effective Loading for thread is {} events/second", |
| 461 | String.valueOf(effLoad)); |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 462 | counter = 0L; |
suibin | 1a7b7bd | 2015-02-12 09:41:47 -0800 | [diff] [blame] | 463 | startTime = System.currentTimeMillis(); |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 464 | } |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 465 | linkDriver.schedule(this, eventRate, TimeUnit.MICROSECONDS); |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 466 | } |
| 467 | } |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 468 | |
| 469 | private void refresh() { |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 470 | if (!linkDriver.isShutdown() || !tasks.isEmpty()) { |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 471 | log.trace("iter {} refresh_links", counter); |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 472 | |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 473 | for (LinkDescription desc : tasks) { |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 474 | providerService.linkDetected(desc); |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 475 | log.info("iteration {}, {}", counter, desc); |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 476 | } |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 477 | counter++; |
| 478 | linkDriver.schedule(this, REFRESH_RATE, TimeUnit.SECONDS); |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 482 | public void deviceRemoved(DeviceId did) { |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 483 | List<LinkDescription> rm = Lists.newArrayList(); |
| 484 | for (LinkDescription ld : tasks) { |
| 485 | if (did.equals(ld.dst().deviceId()) |
| 486 | || (did.equals(ld.src().deviceId()))) { |
| 487 | rm.add(ld); |
Ayaka Koshibe | 9209ea2 | 2015-03-09 10:57:50 -0700 | [diff] [blame] | 488 | } |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 489 | } |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 490 | tasks.removeAll(rm); |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 491 | } |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 492 | |
| 493 | public void setTasks(List<LinkDescription> links) { |
| 494 | HashMultimap<ConnectPoint, ConnectPoint> nm = HashMultimap.create(); |
Ayaka Koshibe | ef69be3 | 2015-03-15 22:29:19 -0700 | [diff] [blame] | 495 | List<LinkDescription> rm = Lists.newArrayList(); |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 496 | links.forEach(v -> nm.put(v.src(), v.dst())); |
| 497 | // remove and send linkVanished for stale links. |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 498 | for (LinkDescription l : tasks) { |
| 499 | if (!nm.containsEntry(l.src(), l.dst())) { |
Ayaka Koshibe | ef69be3 | 2015-03-15 22:29:19 -0700 | [diff] [blame] | 500 | rm.add(l); |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 501 | } |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 502 | } |
Ayaka Koshibe | 9794056 | 2015-03-11 12:25:46 -0700 | [diff] [blame] | 503 | tasks.clear(); |
| 504 | tasks.addAll(links); |
Ayaka Koshibe | ef69be3 | 2015-03-15 22:29:19 -0700 | [diff] [blame] | 505 | rm.forEach(l -> providerService.linkVanished(l)); |
Ayaka Koshibe | 13f896f | 2015-03-10 15:01:44 -0700 | [diff] [blame] | 506 | } |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 507 | } |
Ayaka Koshibe | 839a8a9 | 2015-03-03 17:07:22 -0800 | [diff] [blame] | 508 | |
Ayaka Koshibe | 422916f | 2015-01-15 15:30:23 -0800 | [diff] [blame] | 509 | } |