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