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