2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.netconf.test.tool;
11 import ch.qos.logback.classic.Level;
12 import com.google.common.base.Stopwatch;
13 import com.google.common.io.CharStreams;
14 import com.ning.http.client.AsyncHttpClient;
15 import com.ning.http.client.AsyncHttpClientConfig.Builder;
16 import com.ning.http.client.Request;
17 import com.ning.http.client.Response;
18 import java.io.BufferedReader;
20 import java.io.IOException;
21 import java.io.InputStreamReader;
22 import java.net.ConnectException;
23 import java.util.List;
24 import java.util.concurrent.Callable;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.ScheduledExecutorService;
27 import java.util.concurrent.ScheduledFuture;
28 import java.util.concurrent.ScheduledThreadPoolExecutor;
29 import java.util.concurrent.Semaphore;
30 import java.util.concurrent.TimeUnit;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
33 import net.sourceforge.argparse4j.inf.ArgumentParser;
34 import net.sourceforge.argparse4j.inf.ArgumentParserException;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public class ScaleUtil {
39 private static Logger RESULTS_LOG ;
40 private static final ScheduledExecutorService executor = new LoggingWrapperExecutor(4);
42 private static final int deviceStep = 1000;
43 private static final long retryDelay = 10l;
44 private static final long timeout = 20l;
46 private static final Stopwatch stopwatch = Stopwatch.createUnstarted();
48 private static ScheduledFuture timeoutGuardFuture;
49 private static ch.qos.logback.classic.Logger root;
50 private static final Semaphore semaphore = new Semaphore(0);
52 public static void main(final String[] args) {
53 final TesttoolParameters params = TesttoolParameters.parseArgs(args, TesttoolParameters.getParser());
57 // cleanup at the start in case controller was already running
58 final Runtime runtime = Runtime.getRuntime();
59 cleanup(runtime, params);
62 root.warn("Starting scale test with {} devices", params.deviceCount);
63 timeoutGuardFuture = executor.schedule(new TimeoutGuard(), timeout, TimeUnit.MINUTES);
64 final NetconfDeviceSimulator netconfDeviceSimulator = new NetconfDeviceSimulator(params.threadAmount);
66 final List<Integer> openDevices = netconfDeviceSimulator.start(params);
67 if (openDevices.size() == 0) {
68 root.error("Failed to start any simulated devices, exiting...");
71 if (params.distroFolder != null) {
72 final Main.ConfigGenerator configGenerator = new Main.ConfigGenerator(params.distroFolder, openDevices);
73 final List<File> generated = configGenerator.generate(
74 params.ssh, params.generateConfigBatchSize,
75 params.generateConfigsTimeout, params.generateConfigsAddress,
76 params.devicesPerPort);
77 configGenerator.updateFeatureFile(generated);
78 configGenerator.changeLoadOrder();
80 } catch (final Exception e) {
81 root.error("Unhandled exception", e);
82 netconfDeviceSimulator.close();
86 root.warn(params.distroFolder.getAbsolutePath());
88 runtime.exec(params.distroFolder.getAbsolutePath() + "/bin/start");
91 final Process exec = runtime.exec(params.distroFolder.getAbsolutePath() + "/bin/status");
94 } catch (InterruptedException e) {
95 root.warn("Failed to sleep", e);
97 status = CharStreams.toString(new BufferedReader(new InputStreamReader(exec.getInputStream())));
98 root.warn("Current status: {}", status);
99 } while (!status.startsWith("Running ..."));
100 root.warn("Doing feature install {}", params.distroFolder.getAbsolutePath() + "/bin/client -u karaf feature:install odl-restconf-noauth odl-netconf-connector-all");
101 final Process featureInstall = runtime.exec(params.distroFolder.getAbsolutePath() + "/bin/client -u karaf feature:install odl-restconf-noauth odl-netconf-connector-all");
102 root.warn(CharStreams.toString(new BufferedReader(new InputStreamReader(featureInstall.getInputStream()))));
103 root.warn(CharStreams.toString(new BufferedReader(new InputStreamReader(featureInstall.getErrorStream()))));
105 } catch (IOException e) {
106 root.warn("Failed to start karaf", e);
110 root.warn("Karaf started, starting stopwatch");
114 executor.schedule(new ScaleVerifyCallable(netconfDeviceSimulator, params.deviceCount), retryDelay, TimeUnit.SECONDS);
115 root.warn("First callable scheduled");
117 root.warn("semaphore released");
118 } catch (InterruptedException e) {
119 throw new RuntimeException(e);
122 timeoutGuardFuture.cancel(false);
123 params.deviceCount += deviceStep;
124 netconfDeviceSimulator.close();
127 cleanup(runtime, params);
131 private static void setUpLoggers(final TesttoolParameters params) {
132 System.setProperty("log_file_name", "scale-util.log");
134 root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
135 root.setLevel(params.debug ? Level.DEBUG : Level.INFO);
136 RESULTS_LOG = LoggerFactory.getLogger("results");
139 private static void cleanup(final Runtime runtime, final TesttoolParameters params) {
141 stopKaraf(runtime, params);
142 deleteFolder(new File(params.distroFolder.getAbsoluteFile() + "/data"));
144 } catch (IOException | InterruptedException e) {
145 root.warn("Failed to stop karaf", e);
150 private static void stopKaraf(final Runtime runtime, final TesttoolParameters params) throws IOException, InterruptedException {
151 root.info("Stopping karaf and sleeping for 10 sec..");
152 String controllerPid = "";
155 final Process pgrep = runtime.exec("pgrep -f org.apache.karaf.main.Main");
157 controllerPid = CharStreams.toString(new BufferedReader(new InputStreamReader(pgrep.getInputStream())));
158 root.warn(controllerPid);
159 runtime.exec("kill -9 " + controllerPid);
161 Thread.sleep(10000l);
162 } while (!controllerPid.isEmpty());
163 deleteFolder(new File(params.distroFolder.getAbsoluteFile() + "/data"));
166 private static void deleteFolder(File folder) {
167 File[] files = folder.listFiles();
168 if(files!=null) { //some JVMs return null for empty dirs
170 if(f.isDirectory()) {
180 private static TesttoolParameters parseArgs(final String[] args, final ArgumentParser parser) {
181 final TesttoolParameters parameters = new TesttoolParameters();
183 parser.parseArgs(args, parameters);
185 } catch (ArgumentParserException e) {
186 parser.handleError(e);
193 private static class ScaleVerifyCallable implements Callable {
195 private static final Logger LOG = LoggerFactory.getLogger(ScaleVerifyCallable.class);
197 private static final String RESTCONF_URL = "http://127.0.0.1:8181/restconf/operational/network-topology:network-topology/topology/topology-netconf/";
198 private static final Pattern PATTERN = Pattern.compile("connected");
200 private final AsyncHttpClient asyncHttpClient = new AsyncHttpClient(new Builder()
201 .setConnectTimeout(Integer.MAX_VALUE)
202 .setRequestTimeout(Integer.MAX_VALUE)
203 .setAllowPoolingConnections(true)
205 private final NetconfDeviceSimulator simulator;
206 private final int deviceCount;
207 private final Request request;
209 public ScaleVerifyCallable(final NetconfDeviceSimulator simulator, final int deviceCount) {
210 LOG.info("New callable created");
211 this.simulator = simulator;
212 this.deviceCount = deviceCount;
213 AsyncHttpClient.BoundRequestBuilder requestBuilder = asyncHttpClient.prepareGet(RESTCONF_URL)
214 .addHeader("content-type", "application/xml")
215 .addHeader("Accept", "application/xml")
216 .setRequestTimeout(Integer.MAX_VALUE);
217 request = requestBuilder.build();
221 public Object call() throws Exception {
223 final Response response = asyncHttpClient.executeRequest(request).get();
225 if (response.getStatusCode() != 200 && response.getStatusCode() != 204) {
226 LOG.warn("Request failed, status code: {}", response.getStatusCode() + response.getStatusText());
227 executor.schedule(new ScaleVerifyCallable(simulator, deviceCount), retryDelay, TimeUnit.SECONDS);
229 final String body = response.getResponseBody();
230 final Matcher matcher = PATTERN.matcher(body);
232 while (matcher.find()) {
235 RESULTS_LOG.info("Currently connected devices : {} out of {}, time elapsed: {}", count, deviceCount + 1, stopwatch);
236 if (count != deviceCount + 1) {
237 executor.schedule(new ScaleVerifyCallable(simulator, deviceCount), retryDelay, TimeUnit.SECONDS);
240 RESULTS_LOG.info("All devices connected in {}", stopwatch);
244 } catch (ConnectException | ExecutionException e) {
245 LOG.warn("Failed to connect to Restconf, is the controller running?", e);
246 executor.schedule(new ScaleVerifyCallable(simulator, deviceCount), retryDelay, TimeUnit.SECONDS);
252 private static class TimeoutGuard implements Callable {
255 public Object call() throws Exception {
256 RESULTS_LOG.warn("Timeout for scale test reached after: {} ..aborting", stopwatch);
257 root.warn("Timeout for scale test reached after: {} ..aborting", stopwatch);
263 public static class LoggingWrapperExecutor extends ScheduledThreadPoolExecutor {
265 public LoggingWrapperExecutor(int corePoolSize) {
270 public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
271 return super.schedule(wrapCallable(callable), delay, unit);
274 private Callable wrapCallable(Callable callable) {
275 return new LogOnExceptionCallable(callable);
278 private class LogOnExceptionCallable implements Callable {
279 private Callable theCallable;
281 public LogOnExceptionCallable(Callable theCallable) {
283 this.theCallable = theCallable;
287 public Object call() throws Exception {
291 } catch (Exception e) {
293 root.warn("error in executing: " + theCallable + ". It will no longer be run!", e);
295 // rethrow so that the executor can do it's thing
296 throw new RuntimeException(e);