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 | |
| 18 | import com.google.common.collect.Lists; |
Thomas Vachuska | 8e038eb | 2016-02-23 23:28:23 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Maps; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 20 | import org.onosproject.net.ConnectPoint; |
Thomas Vachuska | 8e038eb | 2016-02-23 23:28:23 -0800 | [diff] [blame] | 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.Link; |
| 23 | import org.onosproject.net.device.DeviceProviderService; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 24 | import org.onosproject.net.device.DeviceService; |
| 25 | import org.onosproject.net.link.DefaultLinkDescription; |
| 26 | import org.onosproject.net.link.LinkDescription; |
| 27 | import org.onosproject.net.link.LinkProviderService; |
| 28 | import org.onosproject.net.link.LinkService; |
| 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
Ray Milkey | b68bbbc | 2017-12-18 10:05:49 -0800 | [diff] [blame] | 32 | import java.security.SecureRandom; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 33 | import java.util.List; |
Thomas Vachuska | 8e038eb | 2016-02-23 23:28:23 -0800 | [diff] [blame] | 34 | import java.util.Map; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 35 | import java.util.Random; |
Thomas Vachuska | 8e038eb | 2016-02-23 23:28:23 -0800 | [diff] [blame] | 36 | import java.util.Set; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 37 | import java.util.concurrent.ExecutorService; |
| 38 | import java.util.stream.Collectors; |
| 39 | |
| 40 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 41 | import static org.onlab.util.Tools.delay; |
| 42 | import static org.onlab.util.Tools.groupedThreads; |
| 43 | import static org.onosproject.net.Link.Type.DIRECT; |
| 44 | import static org.onosproject.net.MastershipRole.MASTER; |
| 45 | import static org.onosproject.provider.nil.TopologySimulator.description; |
| 46 | |
| 47 | /** |
| 48 | * Drives topology mutations at a specified rate of events per second. |
| 49 | */ |
| 50 | class TopologyMutationDriver implements Runnable { |
| 51 | |
| 52 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 53 | |
| 54 | private static final int WAIT_DELAY = 2_000; |
| 55 | private static final int MAX_DOWN_LINKS = 5; |
| 56 | |
Ray Milkey | b68bbbc | 2017-12-18 10:05:49 -0800 | [diff] [blame] | 57 | private final Random random = new SecureRandom(); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 58 | |
| 59 | private volatile boolean stopped = true; |
| 60 | |
| 61 | private double mutationRate; |
| 62 | private int millis, nanos; |
| 63 | |
| 64 | private LinkService linkService; |
| 65 | private DeviceService deviceService; |
| 66 | private LinkProviderService linkProviderService; |
Thomas Vachuska | 8e038eb | 2016-02-23 23:28:23 -0800 | [diff] [blame] | 67 | private DeviceProviderService deviceProviderService; |
| 68 | private TopologySimulator simulator; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 69 | |
| 70 | private List<LinkDescription> activeLinks; |
| 71 | private List<LinkDescription> inactiveLinks; |
| 72 | |
| 73 | private final ExecutorService executor = |
Andrea Campanella | 90f044f | 2016-03-02 09:14:57 -0800 | [diff] [blame] | 74 | newSingleThreadScheduledExecutor(groupedThreads("onos/null", "topo-mutator", log)); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 75 | |
Thomas Vachuska | 8e038eb | 2016-02-23 23:28:23 -0800 | [diff] [blame] | 76 | private Map<DeviceId, Set<Link>> savedLinks = Maps.newConcurrentMap(); |
| 77 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 78 | /** |
| 79 | * Starts the mutation process. |
| 80 | * |
Thomas Vachuska | 8e038eb | 2016-02-23 23:28:23 -0800 | [diff] [blame] | 81 | * @param mutationRate link events per second |
| 82 | * @param linkService link service |
| 83 | * @param deviceService device service |
| 84 | * @param linkProviderService link provider service |
| 85 | * @param deviceProviderService device provider service |
| 86 | * @param simulator topology simulator |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 87 | */ |
| 88 | void start(double mutationRate, |
| 89 | LinkService linkService, DeviceService deviceService, |
Thomas Vachuska | 8e038eb | 2016-02-23 23:28:23 -0800 | [diff] [blame] | 90 | LinkProviderService linkProviderService, |
| 91 | DeviceProviderService deviceProviderService, |
| 92 | TopologySimulator simulator) { |
| 93 | savedLinks.clear(); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 94 | stopped = false; |
| 95 | this.linkService = linkService; |
| 96 | this.deviceService = deviceService; |
| 97 | this.linkProviderService = linkProviderService; |
Thomas Vachuska | 8e038eb | 2016-02-23 23:28:23 -0800 | [diff] [blame] | 98 | this.deviceProviderService = deviceProviderService; |
| 99 | this.simulator = simulator; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 100 | activeLinks = reduceLinks(); |
| 101 | inactiveLinks = Lists.newArrayList(); |
| 102 | adjustRate(mutationRate); |
Andrea Campanella | 90f044f | 2016-03-02 09:14:57 -0800 | [diff] [blame] | 103 | executor.execute(this); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Adjusts the topology mutation rate. |
| 108 | * |
| 109 | * @param mutationRate new topology mutation rate |
| 110 | */ |
| 111 | void adjustRate(double mutationRate) { |
| 112 | this.mutationRate = mutationRate; |
| 113 | if (mutationRate > 0) { |
| 114 | this.millis = (int) (1_000 / mutationRate / 2); |
| 115 | this.nanos = (int) (1_000_000 / mutationRate / 2) % 1_000_000; |
| 116 | } else { |
| 117 | this.millis = 0; |
| 118 | this.nanos = 0; |
| 119 | } |
| 120 | log.info("Settings: millis={}, nanos={}", millis, nanos); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Stops the mutation process. |
| 125 | */ |
| 126 | void stop() { |
| 127 | stopped = true; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Severs the link between the specified end-points in both directions. |
| 132 | * |
| 133 | * @param one link endpoint |
| 134 | * @param two link endpoint |
| 135 | */ |
| 136 | void severLink(ConnectPoint one, ConnectPoint two) { |
| 137 | LinkDescription link = new DefaultLinkDescription(one, two, DIRECT); |
| 138 | linkProviderService.linkVanished(link); |
| 139 | linkProviderService.linkVanished(reverse(link)); |
| 140 | |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Repairs the link between the specified end-points in both directions. |
| 145 | * |
| 146 | * @param one link endpoint |
| 147 | * @param two link endpoint |
| 148 | */ |
| 149 | void repairLink(ConnectPoint one, ConnectPoint two) { |
| 150 | LinkDescription link = new DefaultLinkDescription(one, two, DIRECT); |
| 151 | linkProviderService.linkDetected(link); |
| 152 | linkProviderService.linkDetected(reverse(link)); |
| 153 | } |
| 154 | |
Thomas Vachuska | 8e038eb | 2016-02-23 23:28:23 -0800 | [diff] [blame] | 155 | /** |
| 156 | * Fails the specified device. |
| 157 | * |
| 158 | * @param deviceId device identifier |
| 159 | */ |
| 160 | void failDevice(DeviceId deviceId) { |
| 161 | savedLinks.put(deviceId, linkService.getDeviceLinks(deviceId)); |
| 162 | deviceProviderService.deviceDisconnected(deviceId); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Repairs the specified device. |
| 167 | * |
| 168 | * @param deviceId device identifier |
| 169 | */ |
| 170 | void repairDevice(DeviceId deviceId) { |
Simon Hunt | a77d3bf | 2017-01-30 15:27:36 -0800 | [diff] [blame] | 171 | // device IDs are expressed in hexadecimal... (use radix 16) |
| 172 | int chassisId = Integer.parseInt(deviceId.uri().getSchemeSpecificPart(), 16); |
Thomas Vachuska | 8e038eb | 2016-02-23 23:28:23 -0800 | [diff] [blame] | 173 | simulator.createDevice(deviceId, chassisId); |
| 174 | Set<Link> links = savedLinks.remove(deviceId); |
| 175 | if (links != null) { |
| 176 | links.forEach(l -> linkProviderService |
| 177 | .linkDetected(new DefaultLinkDescription(l.src(), l.dst(), DIRECT))); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Returns whether the given device is considered reachable or not. |
| 183 | * |
| 184 | * @param deviceId device identifier |
| 185 | * @return true if device is reachable |
| 186 | */ |
| 187 | boolean isReachable(DeviceId deviceId) { |
| 188 | return !savedLinks.containsKey(deviceId); |
| 189 | } |
| 190 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 191 | @Override |
| 192 | public void run() { |
| 193 | delay(WAIT_DELAY); |
| 194 | |
| 195 | while (!stopped) { |
| 196 | if (mutationRate > 0 && inactiveLinks.isEmpty()) { |
| 197 | primeInactiveLinks(); |
| 198 | } else if (mutationRate <= 0 && !inactiveLinks.isEmpty()) { |
| 199 | repairInactiveLinks(); |
| 200 | } else if (inactiveLinks.isEmpty()) { |
| 201 | delay(WAIT_DELAY); |
| 202 | |
| 203 | } else { |
| 204 | activeLinks.add(repairLink()); |
| 205 | pause(); |
| 206 | inactiveLinks.add(severLink()); |
| 207 | pause(); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // Primes the inactive links with a few random links. |
| 213 | private void primeInactiveLinks() { |
| 214 | for (int i = 0, n = Math.min(MAX_DOWN_LINKS, activeLinks.size()); i < n; i++) { |
| 215 | inactiveLinks.add(severLink()); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | // Repairs all inactive links. |
| 220 | private void repairInactiveLinks() { |
| 221 | while (!inactiveLinks.isEmpty()) { |
| 222 | repairLink(); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | // Picks a random active link and severs it. |
| 227 | private LinkDescription severLink() { |
| 228 | LinkDescription link = getRandomLink(activeLinks); |
| 229 | linkProviderService.linkVanished(link); |
| 230 | linkProviderService.linkVanished(reverse(link)); |
| 231 | return link; |
| 232 | } |
| 233 | |
| 234 | // Picks a random inactive link and repairs it. |
| 235 | private LinkDescription repairLink() { |
| 236 | LinkDescription link = getRandomLink(inactiveLinks); |
| 237 | linkProviderService.linkDetected(link); |
| 238 | linkProviderService.linkDetected(reverse(link)); |
| 239 | return link; |
| 240 | } |
| 241 | |
| 242 | // Produces a reverse of the specified link. |
| 243 | private LinkDescription reverse(LinkDescription link) { |
| 244 | return new DefaultLinkDescription(link.dst(), link.src(), link.type()); |
| 245 | } |
| 246 | |
| 247 | // Returns a random link from the specified list of links. |
| 248 | private LinkDescription getRandomLink(List<LinkDescription> links) { |
| 249 | return links.remove(random.nextInt(links.size())); |
| 250 | } |
| 251 | |
| 252 | // Reduces the given list of links to just a single link in each original pair. |
| 253 | private List<LinkDescription> reduceLinks() { |
| 254 | List<LinkDescription> links = Lists.newArrayList(); |
| 255 | linkService.getLinks().forEach(link -> links.add(description(link))); |
| 256 | return links.stream() |
| 257 | .filter(this::isOurLink) |
| 258 | .filter(this::isRightDirection) |
| 259 | .collect(Collectors.toList()); |
| 260 | } |
| 261 | |
| 262 | // Returns true if the specified link is ours. |
| 263 | private boolean isOurLink(LinkDescription linkDescription) { |
| 264 | return deviceService.getRole(linkDescription.src().deviceId()) == MASTER; |
| 265 | } |
| 266 | |
| 267 | // Returns true if the link source is greater than the link destination. |
| 268 | private boolean isRightDirection(LinkDescription link) { |
| 269 | return link.src().deviceId().toString().compareTo(link.dst().deviceId().toString()) > 0; |
| 270 | } |
| 271 | |
| 272 | // Pauses the current thread for the pre-computed time of millis & nanos. |
| 273 | private void pause() { |
| 274 | delay(millis, nanos); |
| 275 | } |
| 276 | |
| 277 | } |