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