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 ch.qos.logback.classic.LoggerContext;
13 import ch.qos.logback.classic.util.ContextInitializer;
14 import ch.qos.logback.core.joran.spi.JoranException;
15 import ch.qos.logback.core.util.StatusPrinter;
16 import com.google.common.base.Stopwatch;
17 import com.google.common.io.CharStreams;
18 import com.ning.http.client.AsyncHttpClient;
19 import com.ning.http.client.AsyncHttpClientConfig.Builder;
20 import com.ning.http.client.Request;
21 import com.ning.http.client.Response;
22 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStreamReader;
26 import java.net.ConnectException;
27 import java.util.List;
28 import java.util.concurrent.Callable;
29 import java.util.concurrent.ExecutionException;
30 import java.util.concurrent.ScheduledExecutorService;
31 import java.util.concurrent.ScheduledFuture;
32 import java.util.concurrent.ScheduledThreadPoolExecutor;
33 import java.util.concurrent.Semaphore;
34 import java.util.concurrent.TimeUnit;
35 import java.util.regex.Matcher;
36 import java.util.regex.Pattern;
37 import net.sourceforge.argparse4j.inf.ArgumentParser;
38 import net.sourceforge.argparse4j.inf.ArgumentParserException;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 public class ScaleUtil {
43 private static Logger RESULTS_LOG ;
44 private static final ScheduledExecutorService executor = new LoggingWrapperExecutor(4);
46 private static final int deviceStep = 1000;
47 private static final long retryDelay = 10l;
48 private static final long timeout = 20l;
50 private static final Stopwatch stopwatch = Stopwatch.createUnstarted();
52 private static ScheduledFuture timeoutGuardFuture;
53 private static ch.qos.logback.classic.Logger root;
54 private static final Semaphore semaphore = new Semaphore(0);
56 public static void main(final String[] args) {
57 final TesttoolParameters params = TesttoolParameters.parseArgs(args, TesttoolParameters.getParser());
61 // cleanup at the start in case controller was already running
62 final Runtime runtime = Runtime.getRuntime();
63 cleanup(runtime, params);
66 root.warn("Starting scale test with {} devices", params.deviceCount);
67 timeoutGuardFuture = executor.schedule(new TimeoutGuard(), timeout, TimeUnit.MINUTES);
68 final NetconfDeviceSimulator netconfDeviceSimulator = new NetconfDeviceSimulator(params.threadAmount);
70 final List<Integer> openDevices = netconfDeviceSimulator.start(params);
71 if (openDevices.size() == 0) {
72 root.error("Failed to start any simulated devices, exiting...");
75 if (params.distroFolder != null) {
76 final Main.ConfigGenerator configGenerator = new Main.ConfigGenerator(params.distroFolder, openDevices);
77 final List<File> generated = configGenerator.generate(
78 params.ssh, params.generateConfigBatchSize,
79 params.generateConfigsTimeout, params.generateConfigsAddress,
80 params.devicesPerPort);
81 configGenerator.updateFeatureFile(generated);
82 configGenerator.changeLoadOrder();
84 } catch (final Exception e) {
85 root.error("Unhandled exception", e);
86 netconfDeviceSimulator.close();
90 root.warn(params.distroFolder.getAbsolutePath());
92 runtime.exec(params.distroFolder.getAbsolutePath() + "/bin/start");
95 final Process exec = runtime.exec(params.distroFolder.getAbsolutePath() + "/bin/status");
98 } catch (InterruptedException e) {
99 root.warn("Failed to sleep", e);
101 status = CharStreams.toString(new BufferedReader(new InputStreamReader(exec.getInputStream())));
102 root.warn("Current status: {}", status);
103 } while (!status.startsWith("Running ..."));
104 root.warn("Doing feature install {}", params.distroFolder.getAbsolutePath() + "/bin/client -u karaf feature:install odl-restconf-noauth odl-netconf-connector-all");
105 final Process featureInstall = runtime.exec(params.distroFolder.getAbsolutePath() + "/bin/client -u karaf feature:install odl-restconf-noauth odl-netconf-connector-all");
106 root.warn(CharStreams.toString(new BufferedReader(new InputStreamReader(featureInstall.getInputStream()))));
107 root.warn(CharStreams.toString(new BufferedReader(new InputStreamReader(featureInstall.getErrorStream()))));
109 } catch (IOException e) {
110 root.warn("Failed to start karaf", e);
114 root.warn("Karaf started, starting stopwatch");
118 executor.schedule(new ScaleVerifyCallable(netconfDeviceSimulator, params.deviceCount), retryDelay, TimeUnit.SECONDS);
119 root.warn("First callable scheduled");
121 root.warn("semaphore released");
122 } catch (InterruptedException e) {
123 throw new RuntimeException(e);
126 timeoutGuardFuture.cancel(false);
127 params.deviceCount += deviceStep;
128 netconfDeviceSimulator.close();
131 cleanup(runtime, params);
135 private static void setUpLoggers(final TesttoolParameters params) {
136 System.setProperty("log_file_name", "scale-util.log");
138 root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
139 root.setLevel(params.debug ? Level.DEBUG : Level.INFO);
140 RESULTS_LOG = LoggerFactory.getLogger("results");
143 private static void cleanup(final Runtime runtime, final TesttoolParameters params) {
145 stopKaraf(runtime, params);
146 deleteFolder(new File(params.distroFolder.getAbsoluteFile() + "/data"));
148 } catch (IOException | InterruptedException e) {
149 root.warn("Failed to stop karaf", e);
154 private static void stopKaraf(final Runtime runtime, final TesttoolParameters params) throws IOException, InterruptedException {
155 root.info("Stopping karaf and sleeping for 10 sec..");
156 String controllerPid = "";
159 final Process pgrep = runtime.exec("pgrep -f org.apache.karaf.main.Main");
161 controllerPid = CharStreams.toString(new BufferedReader(new InputStreamReader(pgrep.getInputStream())));
162 root.warn(controllerPid);
163 runtime.exec("kill -9 " + controllerPid);
165 Thread.sleep(10000l);
166 } while (!controllerPid.isEmpty());
167 deleteFolder(new File(params.distroFolder.getAbsoluteFile() + "/data"));
170 private static void deleteFolder(File folder) {
171 File[] files = folder.listFiles();
172 if(files!=null) { //some JVMs return null for empty dirs
174 if(f.isDirectory()) {
184 private static TesttoolParameters parseArgs(final String[] args, final ArgumentParser parser) {
185 final TesttoolParameters parameters = new TesttoolParameters();
187 parser.parseArgs(args, parameters);
189 } catch (ArgumentParserException e) {
190 parser.handleError(e);
197 private static class ScaleVerifyCallable implements Callable {
199 private static final Logger LOG = LoggerFactory.getLogger(ScaleVerifyCallable.class);
201 private static final String RESTCONF_URL = "http://127.0.0.1:8181/restconf/operational/network-topology:network-topology/topology/topology-netconf/";
202 private static final Pattern PATTERN = Pattern.compile("connected");
204 private final AsyncHttpClient asyncHttpClient = new AsyncHttpClient(new Builder()
205 .setConnectTimeout(Integer.MAX_VALUE)
206 .setRequestTimeout(Integer.MAX_VALUE)
207 .setAllowPoolingConnections(true)
209 private final NetconfDeviceSimulator simulator;
210 private final int deviceCount;
211 private final Request request;
213 public ScaleVerifyCallable(final NetconfDeviceSimulator simulator, final int deviceCount) {
214 LOG.info("New callable created");
215 this.simulator = simulator;
216 this.deviceCount = deviceCount;
217 AsyncHttpClient.BoundRequestBuilder requestBuilder = asyncHttpClient.prepareGet(RESTCONF_URL)
218 .addHeader("content-type", "application/xml")
219 .addHeader("Accept", "application/xml")
220 .setRequestTimeout(Integer.MAX_VALUE);
221 request = requestBuilder.build();
225 public Object call() throws Exception {
227 final Response response = asyncHttpClient.executeRequest(request).get();
229 if (response.getStatusCode() != 200 && response.getStatusCode() != 204) {
230 LOG.warn("Request failed, status code: {}", response.getStatusCode() + response.getStatusText());
231 executor.schedule(new ScaleVerifyCallable(simulator, deviceCount), retryDelay, TimeUnit.SECONDS);
233 final String body = response.getResponseBody();
234 final Matcher matcher = PATTERN.matcher(body);
236 while (matcher.find()) {
239 RESULTS_LOG.info("Currently connected devices : {} out of {}, time elapsed: {}", count, deviceCount + 1, stopwatch);
240 if (count != deviceCount + 1) {
241 executor.schedule(new ScaleVerifyCallable(simulator, deviceCount), retryDelay, TimeUnit.SECONDS);
244 RESULTS_LOG.info("All devices connected in {}", stopwatch);
248 } catch (ConnectException | ExecutionException e) {
249 LOG.warn("Failed to connect to Restconf, is the controller running?", e);
250 executor.schedule(new ScaleVerifyCallable(simulator, deviceCount), retryDelay, TimeUnit.SECONDS);
256 private static class TimeoutGuard implements Callable {
259 public Object call() throws Exception {
260 RESULTS_LOG.warn("Timeout for scale test reached after: {} ..aborting", stopwatch);
261 root.warn("Timeout for scale test reached after: {} ..aborting", stopwatch);
267 public static class LoggingWrapperExecutor extends ScheduledThreadPoolExecutor {
269 public LoggingWrapperExecutor(int corePoolSize) {
274 public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
275 return super.schedule(wrapCallable(callable), delay, unit);
278 private Callable wrapCallable(Callable callable) {
279 return new LogOnExceptionCallable(callable);
282 private class LogOnExceptionCallable implements Callable {
283 private Callable theCallable;
285 public LogOnExceptionCallable(Callable theCallable) {
287 this.theCallable = theCallable;
291 public Object call() throws Exception {
295 } catch (Exception e) {
297 root.warn("error in executing: " + theCallable + ". It will no longer be run!", e);
299 // rethrow so that the executor can do it's thing
300 throw new RuntimeException(e);