blob: e10e3dbdc837f6bf868ce985e449c19d98674de3 [file] [log] [blame]
Thomas Vachuskac40d4632015-04-09 16:55:03 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuskac40d4632015-04-09 16:55:03 -07003 *
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 */
16package org.onosproject.provider.nil;
17
18import com.google.common.collect.Lists;
Thomas Vachuska8e038eb2016-02-23 23:28:23 -080019import com.google.common.collect.Maps;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070020import org.onosproject.net.ConnectPoint;
Thomas Vachuska8e038eb2016-02-23 23:28:23 -080021import org.onosproject.net.DeviceId;
22import org.onosproject.net.Link;
23import org.onosproject.net.device.DeviceProviderService;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070024import org.onosproject.net.device.DeviceService;
25import org.onosproject.net.link.DefaultLinkDescription;
26import org.onosproject.net.link.LinkDescription;
27import org.onosproject.net.link.LinkProviderService;
28import org.onosproject.net.link.LinkService;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
Ray Milkeyb68bbbc2017-12-18 10:05:49 -080032import java.security.SecureRandom;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070033import java.util.List;
Thomas Vachuska8e038eb2016-02-23 23:28:23 -080034import java.util.Map;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070035import java.util.Random;
Thomas Vachuska8e038eb2016-02-23 23:28:23 -080036import java.util.Set;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070037import java.util.concurrent.ExecutorService;
38import java.util.stream.Collectors;
39
40import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
41import static org.onlab.util.Tools.delay;
42import static org.onlab.util.Tools.groupedThreads;
43import static org.onosproject.net.Link.Type.DIRECT;
44import static org.onosproject.net.MastershipRole.MASTER;
45import static org.onosproject.provider.nil.TopologySimulator.description;
46
47/**
48 * Drives topology mutations at a specified rate of events per second.
49 */
50class 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 Milkeyb68bbbc2017-12-18 10:05:49 -080057 private final Random random = new SecureRandom();
Thomas Vachuskac40d4632015-04-09 16:55:03 -070058
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 Vachuska8e038eb2016-02-23 23:28:23 -080067 private DeviceProviderService deviceProviderService;
68 private TopologySimulator simulator;
Thomas Vachuskac40d4632015-04-09 16:55:03 -070069
70 private List<LinkDescription> activeLinks;
71 private List<LinkDescription> inactiveLinks;
72
73 private final ExecutorService executor =
Andrea Campanella90f044f2016-03-02 09:14:57 -080074 newSingleThreadScheduledExecutor(groupedThreads("onos/null", "topo-mutator", log));
Thomas Vachuskac40d4632015-04-09 16:55:03 -070075
Thomas Vachuska8e038eb2016-02-23 23:28:23 -080076 private Map<DeviceId, Set<Link>> savedLinks = Maps.newConcurrentMap();
77
Thomas Vachuskac40d4632015-04-09 16:55:03 -070078 /**
79 * Starts the mutation process.
80 *
Thomas Vachuska8e038eb2016-02-23 23:28:23 -080081 * @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 Vachuskac40d4632015-04-09 16:55:03 -070087 */
88 void start(double mutationRate,
89 LinkService linkService, DeviceService deviceService,
Thomas Vachuska8e038eb2016-02-23 23:28:23 -080090 LinkProviderService linkProviderService,
91 DeviceProviderService deviceProviderService,
92 TopologySimulator simulator) {
93 savedLinks.clear();
Thomas Vachuskac40d4632015-04-09 16:55:03 -070094 stopped = false;
95 this.linkService = linkService;
96 this.deviceService = deviceService;
97 this.linkProviderService = linkProviderService;
Thomas Vachuska8e038eb2016-02-23 23:28:23 -080098 this.deviceProviderService = deviceProviderService;
99 this.simulator = simulator;
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700100 activeLinks = reduceLinks();
101 inactiveLinks = Lists.newArrayList();
102 adjustRate(mutationRate);
Andrea Campanella90f044f2016-03-02 09:14:57 -0800103 executor.execute(this);
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700104 }
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 Vachuska8e038eb2016-02-23 23:28:23 -0800155 /**
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 Hunta77d3bf2017-01-30 15:27:36 -0800171 // device IDs are expressed in hexadecimal... (use radix 16)
172 int chassisId = Integer.parseInt(deviceId.uri().getSchemeSpecificPart(), 16);
Thomas Vachuska8e038eb2016-02-23 23:28:23 -0800173 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 Vachuskac40d4632015-04-09 16:55:03 -0700191 @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}