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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
19 import java.io.BufferedReader;
21 import java.io.IOException;
22 import java.io.InputStreamReader;
23 import java.net.ConnectException;
24 import java.util.List;
25 import java.util.concurrent.Callable;
26 import java.util.concurrent.ExecutionException;
27 import java.util.concurrent.ScheduledExecutorService;
28 import java.util.concurrent.ScheduledFuture;
29 import java.util.concurrent.ScheduledThreadPoolExecutor;
30 import java.util.concurrent.Semaphore;
31 import java.util.concurrent.TimeUnit;
32 import java.util.regex.Matcher;
33 import java.util.regex.Pattern;
34 import org.opendaylight.netconf.test.tool.config.Configuration;
35 import org.opendaylight.netconf.test.tool.config.ConfigurationBuilder;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 @SuppressFBWarnings({"DM_EXIT", "DM_DEFAULT_ENCODING", "SLF4J_LOGGER_SHOULD_BE_FINAL"})
40 public final class ScaleUtil {
41 private static final ScheduledExecutorService EXECUTOR = new LoggingWrapperExecutor(4);
42 private static final Semaphore SEMAPHORE = new Semaphore(0);
43 private static final Stopwatch STOPWATCH = Stopwatch.createUnstarted();
45 private static final long TIMEOUT = 20L;
46 private static final long RETRY_DELAY = 10L;
47 private static final int DEVICE_STEP = 1000;
49 private static ch.qos.logback.classic.Logger root;
50 private static Logger resultsLog;
55 @SuppressWarnings("checkstyle:illegalCatch")
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 final ScheduledFuture<?> timeoutGuardFuture = EXECUTOR.schedule(new TimeoutGuard(), TIMEOUT,
69 final Configuration configuration = new ConfigurationBuilder().from(params).build();
70 final NetconfDeviceSimulator netconfDeviceSimulator = new NetconfDeviceSimulator(configuration);
72 final List<Integer> openDevices = netconfDeviceSimulator.start();
73 if (openDevices.size() == 0) {
74 root.error("Failed to start any simulated devices, exiting...");
78 if (params.distroFolder == null) {
79 root.error("Distro folder is not set, exiting...");
83 root.warn(params.distroFolder.getAbsolutePath());
85 runtime.exec(params.distroFolder.getAbsolutePath() + "/bin/start");
88 final Process exec = runtime.exec(params.distroFolder.getAbsolutePath() + "/bin/status");
91 } catch (InterruptedException e) {
92 root.warn("Failed to sleep", e);
94 status = CharStreams.toString(new BufferedReader(new InputStreamReader(exec.getInputStream())));
95 root.warn("Current status: {}", status);
96 } while (!status.startsWith("Running ..."));
97 root.warn("Doing feature install {}", params.distroFolder.getAbsolutePath()
98 + "/bin/client -u karaf feature:install odl-restconf-noauth odl-netconf-connector-all");
99 final Process featureInstall = runtime.exec(params.distroFolder.getAbsolutePath()
100 + "/bin/client -u karaf feature:install odl-restconf-noauth odl-netconf-connector-all");
102 CharStreams.toString(new BufferedReader(new InputStreamReader(featureInstall.getInputStream()))));
104 CharStreams.toString(new BufferedReader(new InputStreamReader(featureInstall.getErrorStream()))));
106 } catch (IOException e) {
107 root.warn("Failed to start karaf", e);
111 root.warn("Karaf started, starting stopwatch");
116 new ScaleVerifyCallable(netconfDeviceSimulator, params.deviceCount), RETRY_DELAY, TimeUnit.SECONDS);
117 root.warn("First callable scheduled");
119 root.warn("semaphore released");
120 } catch (InterruptedException e) {
121 throw new RuntimeException(e);
124 timeoutGuardFuture.cancel(false);
125 params.deviceCount += DEVICE_STEP;
126 netconfDeviceSimulator.close();
129 cleanup(runtime, params);
133 private static void setUpLoggers(final TesttoolParameters params) {
134 System.setProperty("log_file_name", "scale-util.log");
136 root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
137 root.setLevel(params.debug ? Level.DEBUG : Level.INFO);
138 resultsLog = LoggerFactory.getLogger("results");
141 private static void cleanup(final Runtime runtime, final TesttoolParameters params) {
143 stopKaraf(runtime, params);
144 deleteFolder(new File(params.distroFolder.getAbsoluteFile() + "/data"));
146 } catch (IOException | InterruptedException e) {
147 root.warn("Failed to stop karaf", e);
152 private static void stopKaraf(final Runtime runtime, final TesttoolParameters params)
153 throws IOException, InterruptedException {
154 root.info("Stopping karaf and sleeping for 10 sec..");
155 String controllerPid = "";
157 final Process pgrep = runtime.exec("pgrep -f org.apache.karaf.main.Main");
159 controllerPid = CharStreams.toString(new BufferedReader(new InputStreamReader(pgrep.getInputStream())));
160 root.warn(controllerPid);
161 runtime.exec("kill -9 " + controllerPid);
163 Thread.sleep(10000L);
164 } while (!controllerPid.isEmpty());
165 deleteFolder(new File(params.distroFolder.getAbsoluteFile() + "/data"));
168 private static void deleteFolder(final File folder) {
169 File[] files = folder.listFiles();
170 if (files != null) { //some JVMs return null for empty dirs
171 for (File f : files) {
172 if (f.isDirectory()) {
176 root.warn("Failed to delete {}", f);
181 if (!folder.delete()) {
182 root.warn("Failed to delete {}", folder);
186 private static class ScaleVerifyCallable implements Callable<Void> {
187 private static final Logger LOG = LoggerFactory.getLogger(ScaleVerifyCallable.class);
189 private static final String RESTCONF_URL = "http://127.0.0.1:8181/restconf/operational/"
190 + "network-topology:network-topology/topology/topology-netconf/";
191 private static final Pattern PATTERN = Pattern.compile("connected");
193 private final AsyncHttpClient asyncHttpClient = new AsyncHttpClient(new Builder()
194 .setConnectTimeout(Integer.MAX_VALUE)
195 .setRequestTimeout(Integer.MAX_VALUE)
196 .setAllowPoolingConnections(true)
198 private final NetconfDeviceSimulator simulator;
199 private final int deviceCount;
200 private final Request request;
202 ScaleVerifyCallable(final NetconfDeviceSimulator simulator, final int deviceCount) {
203 LOG.info("New callable created");
204 this.simulator = simulator;
205 this.deviceCount = deviceCount;
206 AsyncHttpClient.BoundRequestBuilder requestBuilder = asyncHttpClient.prepareGet(RESTCONF_URL)
207 .addHeader("content-type", "application/xml")
208 .addHeader("Accept", "application/xml")
209 .setRequestTimeout(Integer.MAX_VALUE);
210 request = requestBuilder.build();
214 public Void call() throws Exception {
216 final Response response = asyncHttpClient.executeRequest(request).get();
218 if (response.getStatusCode() != 200 && response.getStatusCode() != 204) {
219 LOG.warn("Request failed, status code: {}", response.getStatusCode() + response.getStatusText());
220 EXECUTOR.schedule(new ScaleVerifyCallable(simulator, deviceCount), RETRY_DELAY, TimeUnit.SECONDS);
222 final String body = response.getResponseBody();
223 final Matcher matcher = PATTERN.matcher(body);
225 while (matcher.find()) {
228 resultsLog.info("Currently connected devices : {} out of {}, time elapsed: {}",
229 count, deviceCount + 1, STOPWATCH);
230 if (count != deviceCount + 1) {
232 new ScaleVerifyCallable(simulator, deviceCount), RETRY_DELAY, TimeUnit.SECONDS);
235 resultsLog.info("All devices connected in {}", STOPWATCH);
239 } catch (ConnectException | ExecutionException e) {
240 LOG.warn("Failed to connect to Restconf, is the controller running?", e);
241 EXECUTOR.schedule(new ScaleVerifyCallable(simulator, deviceCount), RETRY_DELAY, TimeUnit.SECONDS);
247 private static class TimeoutGuard implements Callable<Void> {
250 resultsLog.warn("Timeout for scale test reached after: {} ..aborting", STOPWATCH);
251 root.warn("Timeout for scale test reached after: {} ..aborting", STOPWATCH);
257 @SuppressWarnings("checkstyle:illegalCatch")
258 public static class LoggingWrapperExecutor extends ScheduledThreadPoolExecutor {
259 public LoggingWrapperExecutor(final int corePoolSize) {
264 public <V> ScheduledFuture<V> schedule(final Callable<V> callable, final long delay, final TimeUnit unit) {
265 return super.schedule(new LogOnExceptionCallable<>(callable), delay, unit);
268 private static class LogOnExceptionCallable<T> implements Callable<T> {
269 private final Callable<T> theCallable;
271 LogOnExceptionCallable(final Callable<T> theCallable) {
272 this.theCallable = theCallable;
278 return theCallable.call();
279 } catch (Exception e) {
281 root.warn("error in executing: " + theCallable + ". It will no longer be run!", e);
283 // rethrow so that the executor can do it's thing
284 throw new RuntimeException(e);