Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 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; |
| 17 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 18 | import org.onlab.osgi.DefaultServiceDirectory; |
| 19 | import org.onosproject.cfg.ComponentConfigService; |
| 20 | import org.onosproject.cluster.ClusterService; |
| 21 | import org.onosproject.cluster.NodeId; |
| 22 | import org.onosproject.mastership.MastershipAdminService; |
| 23 | import org.onosproject.net.ConnectPoint; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.Host; |
| 26 | import org.onosproject.net.MastershipRole; |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 27 | import org.onosproject.net.PortNumber; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 28 | import org.onosproject.net.device.DeviceAdminService; |
| 29 | import org.onosproject.net.device.DeviceProvider; |
| 30 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 31 | import org.onosproject.net.device.DeviceProviderService; |
| 32 | import org.onosproject.net.flow.FlowRuleProviderRegistry; |
| 33 | import org.onosproject.net.flow.FlowRuleProviderService; |
Yi Tseng | 3dca0f8 | 2018-04-03 14:33:03 +0800 | [diff] [blame] | 34 | import org.onosproject.net.group.GroupProviderRegistry; |
| 35 | import org.onosproject.net.group.GroupProviderService; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 36 | import org.onosproject.net.host.HostProvider; |
| 37 | import org.onosproject.net.host.HostProviderRegistry; |
| 38 | import org.onosproject.net.host.HostProviderService; |
| 39 | import org.onosproject.net.host.HostService; |
| 40 | import org.onosproject.net.link.LinkProvider; |
| 41 | import org.onosproject.net.link.LinkProviderRegistry; |
| 42 | import org.onosproject.net.link.LinkProviderService; |
| 43 | import org.onosproject.net.link.LinkService; |
| 44 | import org.onosproject.net.packet.PacketProviderRegistry; |
| 45 | import org.onosproject.net.packet.PacketProviderService; |
| 46 | import org.onosproject.net.provider.AbstractProvider; |
| 47 | import org.onosproject.net.provider.ProviderId; |
| 48 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 49 | import org.osgi.service.component.annotations.Activate; |
| 50 | import org.osgi.service.component.annotations.Component; |
| 51 | import org.osgi.service.component.annotations.Deactivate; |
| 52 | import org.osgi.service.component.annotations.Modified; |
| 53 | import org.osgi.service.component.annotations.Reference; |
| 54 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 55 | import org.slf4j.Logger; |
| 56 | |
| 57 | import java.util.Dictionary; |
Thomas Vachuska | 8e038eb | 2016-02-23 23:28:23 -0800 | [diff] [blame] | 58 | import java.util.Objects; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 59 | import java.util.Properties; |
| 60 | |
| 61 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 62 | import static org.onlab.util.Tools.delay; |
| 63 | import static org.onlab.util.Tools.get; |
| 64 | import static org.onosproject.net.DeviceId.deviceId; |
| 65 | import static org.onosproject.net.MastershipRole.MASTER; |
| 66 | import static org.onosproject.net.MastershipRole.NONE; |
| 67 | import static org.slf4j.LoggerFactory.getLogger; |
| 68 | |
| 69 | /** |
| 70 | * Provider of a fake network environment, i.e. devices, links, hosts, etc. |
| 71 | * To be used for benchmarking only. |
| 72 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 73 | @Component(immediate = true, service = NullProviders.class) |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 74 | public class NullProviders { |
| 75 | |
| 76 | private static final Logger log = getLogger(NullProviders.class); |
| 77 | |
| 78 | static final String SCHEME = "null"; |
| 79 | static final String PROVIDER_ID = "org.onosproject.provider.nil"; |
| 80 | |
| 81 | private static final String FORMAT = |
| 82 | "Settings: enabled={}, topoShape={}, deviceCount={}, " + |
| 83 | "hostCount={}, packetRate={}, mutationRate={}"; |
| 84 | |
| 85 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 87 | protected ClusterService clusterService; |
| 88 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 90 | protected MastershipAdminService mastershipService; |
| 91 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 92 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 93 | protected ComponentConfigService cfgService; |
| 94 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 95 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 96 | protected DeviceAdminService deviceService; |
| 97 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 98 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 99 | protected HostService hostService; |
| 100 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 101 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 102 | protected LinkService linkService; |
| 103 | |
| 104 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 105 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 106 | protected DeviceProviderRegistry deviceProviderRegistry; |
| 107 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 108 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 109 | protected HostProviderRegistry hostProviderRegistry; |
| 110 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 111 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 112 | protected LinkProviderRegistry linkProviderRegistry; |
| 113 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 114 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 115 | protected FlowRuleProviderRegistry flowRuleProviderRegistry; |
| 116 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 117 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yi Tseng | 3dca0f8 | 2018-04-03 14:33:03 +0800 | [diff] [blame] | 118 | protected GroupProviderRegistry groupProviderRegistry; |
| 119 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 120 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 121 | protected PacketProviderRegistry packetProviderRegistry; |
| 122 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 123 | private final NullDeviceProvider deviceProvider = new NullDeviceProvider(); |
| 124 | private final NullLinkProvider linkProvider = new NullLinkProvider(); |
| 125 | private final NullHostProvider hostProvider = new NullHostProvider(); |
| 126 | private final NullFlowRuleProvider flowRuleProvider = new NullFlowRuleProvider(); |
Yi Tseng | 3dca0f8 | 2018-04-03 14:33:03 +0800 | [diff] [blame] | 127 | private final NullGroupProvider groupProvider = new NullGroupProvider(); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 128 | private final NullPacketProvider packetProvider = new NullPacketProvider(); |
| 129 | private final TopologyMutationDriver topologyMutationDriver = new TopologyMutationDriver(); |
| 130 | |
| 131 | private DeviceProviderService deviceProviderService; |
| 132 | private HostProviderService hostProviderService; |
| 133 | private LinkProviderService linkProviderService; |
| 134 | private FlowRuleProviderService flowRuleProviderService; |
Yi Tseng | 3dca0f8 | 2018-04-03 14:33:03 +0800 | [diff] [blame] | 135 | private GroupProviderService groupProviderService; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 136 | private PacketProviderService packetProviderService; |
| 137 | |
| 138 | private TopologySimulator simulator; |
| 139 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 140 | //@Property(name = "enabled", boolValue = false, |
| 141 | // label = "Enables or disables the provider") |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 142 | private boolean enabled = false; |
| 143 | |
| 144 | private static final String DEFAULT_TOPO_SHAPE = "configured"; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 145 | //@Property(name = "topoShape", value = DEFAULT_TOPO_SHAPE, |
| 146 | // label = "Topology shape: configured, linear, reroute, tree, spineleaf, mesh, grid") |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 147 | private String topoShape = DEFAULT_TOPO_SHAPE; |
| 148 | |
| 149 | private static final int DEFAULT_DEVICE_COUNT = 10; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 150 | //@Property(name = "deviceCount", intValue = DEFAULT_DEVICE_COUNT, |
| 151 | // label = "Number of devices to generate") |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 152 | private int deviceCount = DEFAULT_DEVICE_COUNT; |
| 153 | |
| 154 | private static final int DEFAULT_HOST_COUNT = 5; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 155 | //@Property(name = "hostCount", intValue = DEFAULT_HOST_COUNT, |
| 156 | // label = "Number of host to generate per device") |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 157 | private int hostCount = DEFAULT_HOST_COUNT; |
| 158 | |
Thomas Vachuska | c3cdf90 | 2016-04-13 13:44:09 -0700 | [diff] [blame] | 159 | private static final int DEFAULT_PACKET_RATE = 0; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 160 | //@Property(name = "packetRate", intValue = DEFAULT_PACKET_RATE, |
| 161 | // label = "Packet-in/s rate; 0 for no packets") |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 162 | private int packetRate = DEFAULT_PACKET_RATE; |
| 163 | |
| 164 | private static final double DEFAULT_MUTATION_RATE = 0; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 165 | //@Property(name = "mutationRate", doubleValue = DEFAULT_MUTATION_RATE, |
| 166 | // label = "Link event/s topology mutation rate; 0 for no mutations") |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 167 | private double mutationRate = DEFAULT_MUTATION_RATE; |
| 168 | |
| 169 | private static final String DEFAULT_MASTERSHIP = "random"; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 170 | //@Property(name = "mastership", value = DEFAULT_MASTERSHIP, |
| 171 | // label = "Mastership given as 'random' or 'node1=dpid,dpid/node2=dpid,...'") |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 172 | private String mastership = DEFAULT_MASTERSHIP; |
| 173 | |
| 174 | |
| 175 | @Activate |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 176 | public void activate() { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 177 | cfgService.registerProperties(getClass()); |
| 178 | |
| 179 | deviceProviderService = deviceProviderRegistry.register(deviceProvider); |
| 180 | hostProviderService = hostProviderRegistry.register(hostProvider); |
| 181 | linkProviderService = linkProviderRegistry.register(linkProvider); |
| 182 | flowRuleProviderService = flowRuleProviderRegistry.register(flowRuleProvider); |
Yi Tseng | 3dca0f8 | 2018-04-03 14:33:03 +0800 | [diff] [blame] | 183 | groupProviderService = groupProviderRegistry.register(groupProvider); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 184 | packetProviderService = packetProviderRegistry.register(packetProvider); |
| 185 | log.info("Started"); |
| 186 | } |
| 187 | |
| 188 | @Deactivate |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 189 | public void deactivate() { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 190 | cfgService.unregisterProperties(getClass(), false); |
| 191 | tearDown(); |
| 192 | |
| 193 | deviceProviderRegistry.unregister(deviceProvider); |
| 194 | hostProviderRegistry.unregister(hostProvider); |
| 195 | linkProviderRegistry.unregister(linkProvider); |
| 196 | flowRuleProviderRegistry.unregister(flowRuleProvider); |
Yi Tseng | 3dca0f8 | 2018-04-03 14:33:03 +0800 | [diff] [blame] | 197 | groupProviderRegistry.unregister(groupProvider); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 198 | packetProviderRegistry.unregister(packetProvider); |
| 199 | |
| 200 | deviceProviderService = null; |
| 201 | hostProviderService = null; |
| 202 | linkProviderService = null; |
| 203 | flowRuleProviderService = null; |
Yi Tseng | 3dca0f8 | 2018-04-03 14:33:03 +0800 | [diff] [blame] | 204 | groupProviderService = null; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 205 | packetProviderService = null; |
| 206 | |
| 207 | log.info("Stopped"); |
| 208 | } |
| 209 | |
| 210 | @Modified |
| 211 | public void modified(ComponentContext context) { |
| 212 | Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties(); |
| 213 | |
| 214 | boolean newEnabled; |
| 215 | int newDeviceCount, newHostCount, newPacketRate; |
| 216 | double newMutationRate; |
| 217 | String newTopoShape, newMastership; |
| 218 | try { |
| 219 | String s = get(properties, "enabled"); |
| 220 | newEnabled = isNullOrEmpty(s) ? enabled : Boolean.parseBoolean(s.trim()); |
| 221 | |
| 222 | newTopoShape = get(properties, "topoShape"); |
| 223 | newMastership = get(properties, "mastership"); |
| 224 | |
| 225 | s = get(properties, "deviceCount"); |
| 226 | newDeviceCount = isNullOrEmpty(s) ? deviceCount : Integer.parseInt(s.trim()); |
| 227 | |
| 228 | s = get(properties, "hostCount"); |
| 229 | newHostCount = isNullOrEmpty(s) ? hostCount : Integer.parseInt(s.trim()); |
| 230 | |
| 231 | s = get(properties, "packetRate"); |
| 232 | newPacketRate = isNullOrEmpty(s) ? packetRate : Integer.parseInt(s.trim()); |
| 233 | |
| 234 | s = get(properties, "mutationRate"); |
| 235 | newMutationRate = isNullOrEmpty(s) ? mutationRate : Double.parseDouble(s.trim()); |
| 236 | |
| 237 | } catch (NumberFormatException e) { |
| 238 | log.warn(e.getMessage()); |
| 239 | newEnabled = enabled; |
| 240 | newTopoShape = topoShape; |
| 241 | newDeviceCount = deviceCount; |
| 242 | newHostCount = hostCount; |
| 243 | newPacketRate = packetRate; |
| 244 | newMutationRate = mutationRate; |
| 245 | newMastership = mastership; |
| 246 | } |
| 247 | |
| 248 | // Any change in the following parameters implies hard restart |
Thomas Vachuska | 8e038eb | 2016-02-23 23:28:23 -0800 | [diff] [blame] | 249 | if (newEnabled != enabled || !Objects.equals(newTopoShape, topoShape) || |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 250 | newDeviceCount != deviceCount || newHostCount != hostCount) { |
| 251 | enabled = newEnabled; |
| 252 | topoShape = newTopoShape; |
| 253 | deviceCount = newDeviceCount; |
| 254 | hostCount = newHostCount; |
| 255 | packetRate = newPacketRate; |
| 256 | mutationRate = newMutationRate; |
| 257 | restartSimulation(); |
| 258 | } |
| 259 | |
| 260 | // Any change in the following parameters implies just a rate change |
| 261 | if (newPacketRate != packetRate || newMutationRate != mutationRate) { |
| 262 | packetRate = newPacketRate; |
| 263 | mutationRate = newMutationRate; |
| 264 | adjustRates(); |
| 265 | } |
| 266 | |
| 267 | // Any change in mastership implies just reassignments. |
Thomas Vachuska | 8e038eb | 2016-02-23 23:28:23 -0800 | [diff] [blame] | 268 | if (!Objects.equals(newMastership, mastership)) { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 269 | mastership = newMastership; |
| 270 | reassignMastership(); |
| 271 | } |
| 272 | |
| 273 | log.info(FORMAT, enabled, topoShape, deviceCount, hostCount, |
| 274 | packetRate, mutationRate); |
| 275 | } |
| 276 | |
| 277 | /** |
Thomas Vachuska | 5c6ed22 | 2016-06-29 11:02:22 +0200 | [diff] [blame] | 278 | * Returns the currently active topology simulator. |
| 279 | * |
| 280 | * @return current simulator; null if none is active |
| 281 | */ |
| 282 | public TopologySimulator currentSimulator() { |
| 283 | return simulator; |
| 284 | } |
| 285 | |
| 286 | /** |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 287 | * Severs the link between the specified end-points in both directions. |
| 288 | * |
| 289 | * @param one link endpoint |
| 290 | * @param two link endpoint |
| 291 | */ |
| 292 | public void severLink(ConnectPoint one, ConnectPoint two) { |
| 293 | if (enabled) { |
| 294 | topologyMutationDriver.severLink(one, two); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Severs the link between the specified end-points in both directions. |
| 300 | * |
| 301 | * @param one link endpoint |
| 302 | * @param two link endpoint |
| 303 | */ |
| 304 | public void repairLink(ConnectPoint one, ConnectPoint two) { |
| 305 | if (enabled) { |
| 306 | topologyMutationDriver.repairLink(one, two); |
| 307 | } |
| 308 | } |
| 309 | |
Thomas Vachuska | 8e038eb | 2016-02-23 23:28:23 -0800 | [diff] [blame] | 310 | /** |
| 311 | * Fails the specified device. |
| 312 | * |
| 313 | * @param deviceId device identifier |
| 314 | */ |
| 315 | public void failDevice(DeviceId deviceId) { |
| 316 | if (enabled) { |
| 317 | topologyMutationDriver.failDevice(deviceId); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Repairs the specified device. |
| 323 | * |
| 324 | * @param deviceId device identifier |
| 325 | */ |
| 326 | public void repairDevice(DeviceId deviceId) { |
| 327 | if (enabled) { |
| 328 | topologyMutationDriver.repairDevice(deviceId); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 333 | // Resets simulation based on the current configuration parameters. |
| 334 | private void restartSimulation() { |
| 335 | tearDown(); |
| 336 | if (enabled) { |
| 337 | setUp(); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | // Sets up the topology simulation and all providers. |
| 342 | private void setUp() { |
| 343 | simulator = selectSimulator(topoShape); |
| 344 | simulator.init(topoShape, deviceCount, hostCount, |
| 345 | new DefaultServiceDirectory(), |
| 346 | deviceProviderService, hostProviderService, |
| 347 | linkProviderService); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 348 | flowRuleProvider.start(flowRuleProviderService); |
Yi Tseng | 3dca0f8 | 2018-04-03 14:33:03 +0800 | [diff] [blame] | 349 | groupProvider.start(groupProviderService); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 350 | packetProvider.start(packetRate, hostService, deviceService, |
| 351 | packetProviderService); |
Thomas Vachuska | 1cbd65e | 2016-06-25 16:27:57 +0200 | [diff] [blame] | 352 | simulator.setUpTopology(); |
Yi Tseng | 3dca0f8 | 2018-04-03 14:33:03 +0800 | [diff] [blame] | 353 | groupProvider.initDevicesGroupTable(simulator.deviceIds); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 354 | topologyMutationDriver.start(mutationRate, linkService, deviceService, |
Thomas Vachuska | 8e038eb | 2016-02-23 23:28:23 -0800 | [diff] [blame] | 355 | linkProviderService, deviceProviderService, |
| 356 | simulator); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | // Selects the simulator based on the specified name. |
| 360 | private TopologySimulator selectSimulator(String topoShape) { |
| 361 | if (topoShape.matches("linear([,].*|$)")) { |
| 362 | return new LinearTopologySimulator(); |
| 363 | } else if (topoShape.matches("centipede([,].*|$)")) { |
| 364 | return new CentipedeTopologySimulator(); |
| 365 | } else if (topoShape.matches("reroute([,].*|$)")) { |
| 366 | return new RerouteTopologySimulator(); |
| 367 | } else if (topoShape.matches("tree([,].*|$)")) { |
| 368 | return new TreeTopologySimulator(); |
Thomas Vachuska | 59da18d | 2015-07-07 17:44:21 -0700 | [diff] [blame] | 369 | } else if (topoShape.matches("agglink([,].*|$)")) { |
| 370 | return new AggLinkTopologySimulator(); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 371 | } else if (topoShape.matches("spineleaf([,].*|$)")) { |
| 372 | return new SpineLeafTopologySimulator(); |
| 373 | } else if (topoShape.matches("mesh([,].*|$)")) { |
| 374 | return new MeshTopologySimulator(); |
Thomas Vachuska | 8e038eb | 2016-02-23 23:28:23 -0800 | [diff] [blame] | 375 | } else if (topoShape.matches("grid([,].*|$)")) { |
| 376 | return new GridTopologySimulator(); |
Andreas Pantelopoulos | 620d3d7 | 2016-11-08 22:18:59 +0100 | [diff] [blame] | 377 | } else if (topoShape.matches("fattree([,].*|$)")) { |
| 378 | return new FatTreeTopologySimulator(); |
hyunmin no | 9717418 | 2016-12-13 21:14:30 +0900 | [diff] [blame] | 379 | } else if (topoShape.matches("chordal([,].*|$)")) { |
| 380 | return new ChordalTopologySimulator(); |
Thomas Vachuska | 5c6ed22 | 2016-06-29 11:02:22 +0200 | [diff] [blame] | 381 | } else if (topoShape.matches("custom([,].*|$)")) { |
| 382 | return new CustomTopologySimulator(); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 383 | } else { |
| 384 | return new ConfiguredTopologySimulator(); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | // Shuts down the topology simulator and all providers. |
| 389 | private void tearDown() { |
| 390 | if (simulator != null) { |
| 391 | topologyMutationDriver.stop(); |
| 392 | packetProvider.stop(); |
| 393 | flowRuleProvider.stop(); |
Yi Tseng | 3dca0f8 | 2018-04-03 14:33:03 +0800 | [diff] [blame] | 394 | groupProvider.stop(); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 395 | delay(500); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 396 | simulator.tearDownTopology(); |
| 397 | simulator = null; |
Thomas Vachuska | cab29d2 | 2018-02-21 15:47:29 -0800 | [diff] [blame] | 398 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
| 402 | // Changes packet and mutation rates. |
| 403 | private void adjustRates() { |
| 404 | packetProvider.adjustRate(packetRate); |
| 405 | topologyMutationDriver.adjustRate(mutationRate); |
| 406 | } |
| 407 | |
| 408 | // Re-assigns mastership roles. |
| 409 | private void reassignMastership() { |
| 410 | if (mastership.equals(DEFAULT_MASTERSHIP)) { |
| 411 | mastershipService.balanceRoles(); |
| 412 | } else { |
| 413 | NodeId localNode = clusterService.getLocalNode().id(); |
| 414 | rejectMastership(); |
| 415 | String[] nodeSpecs = mastership.split("/"); |
| 416 | for (int i = 0; i < nodeSpecs.length; i++) { |
| 417 | String[] specs = nodeSpecs[i].split("="); |
| 418 | if (specs[0].equals(localNode.toString())) { |
| 419 | String[] ids = specs[1].split(","); |
| 420 | for (String id : ids) { |
| 421 | mastershipService.setRole(localNode, deviceId(id), MASTER); |
| 422 | } |
| 423 | break; |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | // Rejects mastership of all devices. |
| 430 | private void rejectMastership() { |
| 431 | NodeId localNode = clusterService.getLocalNode().id(); |
| 432 | deviceService.getDevices() |
| 433 | .forEach(device -> mastershipService.setRole(localNode, device.id(), |
| 434 | NONE)); |
| 435 | } |
| 436 | |
| 437 | // Null provider base class. |
| 438 | abstract static class AbstractNullProvider extends AbstractProvider { |
| 439 | protected AbstractNullProvider() { |
| 440 | super(new ProviderId(SCHEME, PROVIDER_ID)); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | // Device provider facade. |
| 445 | private class NullDeviceProvider extends AbstractNullProvider implements DeviceProvider { |
| 446 | @Override |
| 447 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { |
| 448 | deviceProviderService.receivedRoleReply(deviceId, newRole, newRole); |
| 449 | } |
| 450 | |
| 451 | @Override |
| 452 | public boolean isReachable(DeviceId deviceId) { |
Thomas Vachuska | b1906d2 | 2018-02-14 16:50:16 -0800 | [diff] [blame] | 453 | return simulator != null && |
| 454 | (simulator.contains(deviceId) || !deviceService.getPorts(deviceId).isEmpty()) && |
| 455 | (simulator instanceof CustomTopologySimulator || |
Thomas Vachuska | 8e038eb | 2016-02-23 23:28:23 -0800 | [diff] [blame] | 456 | topologyMutationDriver.isReachable(deviceId)); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | @Override |
| 460 | public void triggerProbe(DeviceId deviceId) { |
| 461 | } |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 462 | |
| 463 | @Override |
| 464 | public void changePortState(DeviceId deviceId, PortNumber portNumber, |
| 465 | boolean enable) { |
| 466 | // TODO maybe required |
| 467 | } |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | // Host provider facade. |
| 471 | private class NullHostProvider extends AbstractNullProvider implements HostProvider { |
| 472 | @Override |
| 473 | public void triggerProbe(Host host) { |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | // Host provider facade. |
| 478 | private class NullLinkProvider extends AbstractNullProvider implements LinkProvider { |
| 479 | } |
| 480 | |
| 481 | } |