blob: ccf7e08acffd61176e537c976b0d4641651b0c67 [file] [log] [blame]
Thomas Vachuskac40d4632015-04-09 16:55:03 -07001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
16package org.onosproject.provider.nil;
17
18import com.google.common.collect.Lists;
19import org.onosproject.net.ConnectPoint;
20import org.onosproject.net.device.DeviceService;
21import org.onosproject.net.link.DefaultLinkDescription;
22import org.onosproject.net.link.LinkDescription;
23import org.onosproject.net.link.LinkProviderService;
24import org.onosproject.net.link.LinkService;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28import java.util.List;
29import java.util.Random;
30import java.util.concurrent.ExecutorService;
31import java.util.stream.Collectors;
32
33import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
34import static org.onlab.util.Tools.delay;
35import static org.onlab.util.Tools.groupedThreads;
36import static org.onosproject.net.Link.Type.DIRECT;
37import static org.onosproject.net.MastershipRole.MASTER;
38import static org.onosproject.provider.nil.TopologySimulator.description;
39
40/**
41 * Drives topology mutations at a specified rate of events per second.
42 */
43class TopologyMutationDriver implements Runnable {
44
45 private final Logger log = LoggerFactory.getLogger(getClass());
46
47 private static final int WAIT_DELAY = 2_000;
48 private static final int MAX_DOWN_LINKS = 5;
49
50 private final Random random = new Random();
51
52 private volatile boolean stopped = true;
53
54 private double mutationRate;
55 private int millis, nanos;
56
57 private LinkService linkService;
58 private DeviceService deviceService;
59 private LinkProviderService linkProviderService;
60
61 private List<LinkDescription> activeLinks;
62 private List<LinkDescription> inactiveLinks;
63
64 private final ExecutorService executor =
65 newSingleThreadScheduledExecutor(groupedThreads("onos/null", "topo-mutator"));
66
67 /**
68 * Starts the mutation process.
69 *
70 * @param mutationRate link events per second
71 * @param linkService link service
72 * @param deviceService device service
73 * @param linkProviderService link provider service
74 */
75 void start(double mutationRate,
76 LinkService linkService, DeviceService deviceService,
77 LinkProviderService linkProviderService) {
78 stopped = false;
79 this.linkService = linkService;
80 this.deviceService = deviceService;
81 this.linkProviderService = linkProviderService;
82 activeLinks = reduceLinks();
83 inactiveLinks = Lists.newArrayList();
84 adjustRate(mutationRate);
85 executor.submit(this);
86 }
87
88 /**
89 * Adjusts the topology mutation rate.
90 *
91 * @param mutationRate new topology mutation rate
92 */
93 void adjustRate(double mutationRate) {
94 this.mutationRate = mutationRate;
95 if (mutationRate > 0) {
96 this.millis = (int) (1_000 / mutationRate / 2);
97 this.nanos = (int) (1_000_000 / mutationRate / 2) % 1_000_000;
98 } else {
99 this.millis = 0;
100 this.nanos = 0;
101 }
102 log.info("Settings: millis={}, nanos={}", millis, nanos);
103 }
104
105 /**
106 * Stops the mutation process.
107 */
108 void stop() {
109 stopped = true;
110 }
111
112 /**
113 * Severs the link between the specified end-points in both directions.
114 *
115 * @param one link endpoint
116 * @param two link endpoint
117 */
118 void severLink(ConnectPoint one, ConnectPoint two) {
119 LinkDescription link = new DefaultLinkDescription(one, two, DIRECT);
120 linkProviderService.linkVanished(link);
121 linkProviderService.linkVanished(reverse(link));
122
123 }
124
125 /**
126 * Repairs the link between the specified end-points in both directions.
127 *
128 * @param one link endpoint
129 * @param two link endpoint
130 */
131 void repairLink(ConnectPoint one, ConnectPoint two) {
132 LinkDescription link = new DefaultLinkDescription(one, two, DIRECT);
133 linkProviderService.linkDetected(link);
134 linkProviderService.linkDetected(reverse(link));
135 }
136
137 @Override
138 public void run() {
139 delay(WAIT_DELAY);
140
141 while (!stopped) {
142 if (mutationRate > 0 && inactiveLinks.isEmpty()) {
143 primeInactiveLinks();
144 } else if (mutationRate <= 0 && !inactiveLinks.isEmpty()) {
145 repairInactiveLinks();
146 } else if (inactiveLinks.isEmpty()) {
147 delay(WAIT_DELAY);
148
149 } else {
150 activeLinks.add(repairLink());
151 pause();
152 inactiveLinks.add(severLink());
153 pause();
154 }
155 }
156 }
157
158 // Primes the inactive links with a few random links.
159 private void primeInactiveLinks() {
160 for (int i = 0, n = Math.min(MAX_DOWN_LINKS, activeLinks.size()); i < n; i++) {
161 inactiveLinks.add(severLink());
162 }
163 }
164
165 // Repairs all inactive links.
166 private void repairInactiveLinks() {
167 while (!inactiveLinks.isEmpty()) {
168 repairLink();
169 }
170 }
171
172 // Picks a random active link and severs it.
173 private LinkDescription severLink() {
174 LinkDescription link = getRandomLink(activeLinks);
175 linkProviderService.linkVanished(link);
176 linkProviderService.linkVanished(reverse(link));
177 return link;
178 }
179
180 // Picks a random inactive link and repairs it.
181 private LinkDescription repairLink() {
182 LinkDescription link = getRandomLink(inactiveLinks);
183 linkProviderService.linkDetected(link);
184 linkProviderService.linkDetected(reverse(link));
185 return link;
186 }
187
188 // Produces a reverse of the specified link.
189 private LinkDescription reverse(LinkDescription link) {
190 return new DefaultLinkDescription(link.dst(), link.src(), link.type());
191 }
192
193 // Returns a random link from the specified list of links.
194 private LinkDescription getRandomLink(List<LinkDescription> links) {
195 return links.remove(random.nextInt(links.size()));
196 }
197
198 // Reduces the given list of links to just a single link in each original pair.
199 private List<LinkDescription> reduceLinks() {
200 List<LinkDescription> links = Lists.newArrayList();
201 linkService.getLinks().forEach(link -> links.add(description(link)));
202 return links.stream()
203 .filter(this::isOurLink)
204 .filter(this::isRightDirection)
205 .collect(Collectors.toList());
206 }
207
208 // Returns true if the specified link is ours.
209 private boolean isOurLink(LinkDescription linkDescription) {
210 return deviceService.getRole(linkDescription.src().deviceId()) == MASTER;
211 }
212
213 // Returns true if the link source is greater than the link destination.
214 private boolean isRightDirection(LinkDescription link) {
215 return link.src().deviceId().toString().compareTo(link.dst().deviceId().toString()) > 0;
216 }
217
218 // Pauses the current thread for the pre-computed time of millis & nanos.
219 private void pause() {
220 delay(millis, nanos);
221 }
222
223}