5a65a5f88741d50ae1a7dd9f4c917e49e1d6fe01
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / ScaleUtil.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netconf.test.tool;
10
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;
20 import java.io.File;
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;
38
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();
44
45     private static final long TIMEOUT = 20L;
46     private static final long RETRY_DELAY = 10L;
47     private static final int DEVICE_STEP = 1000;
48
49     private static ch.qos.logback.classic.Logger root;
50     private static Logger resultsLog;
51
52     private ScaleUtil() {
53     }
54
55     @SuppressWarnings("checkstyle:illegalCatch")
56     public static void main(final String[] args) {
57         final TesttoolParameters params = TesttoolParameters.parseArgs(args, TesttoolParameters.getParser());
58
59         setUpLoggers(params);
60
61         // cleanup at the start in case controller was already running
62         final Runtime runtime = Runtime.getRuntime();
63         cleanup(runtime, params);
64
65         while (true) {
66             root.warn("Starting scale test with {} devices", params.deviceCount);
67             final ScheduledFuture<?> timeoutGuardFuture = EXECUTOR.schedule(new TimeoutGuard(), TIMEOUT,
68                 TimeUnit.MINUTES);
69             final Configuration configuration = new ConfigurationBuilder().from(params).build();
70             final NetconfDeviceSimulator netconfDeviceSimulator = new NetconfDeviceSimulator(configuration);
71
72             final List<Integer> openDevices = netconfDeviceSimulator.start();
73             if (openDevices.size() == 0) {
74                 root.error("Failed to start any simulated devices, exiting...");
75                 System.exit(1);
76             }
77
78             if (params.distroFolder == null) {
79                 root.error("Distro folder is not set, exiting...");
80                 System.exit(1);
81             }
82
83             root.warn(params.distroFolder.getAbsolutePath());
84             try {
85                 runtime.exec(params.distroFolder.getAbsolutePath() + "/bin/start");
86                 String status;
87                 do {
88                     final Process exec = runtime.exec(params.distroFolder.getAbsolutePath() + "/bin/status");
89                     try {
90                         Thread.sleep(2000L);
91                     } catch (InterruptedException e) {
92                         root.warn("Failed to sleep", e);
93                     }
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");
101                 root.warn(
102                     CharStreams.toString(new BufferedReader(new InputStreamReader(featureInstall.getInputStream()))));
103                 root.warn(
104                     CharStreams.toString(new BufferedReader(new InputStreamReader(featureInstall.getErrorStream()))));
105
106             } catch (IOException e) {
107                 root.warn("Failed to start karaf", e);
108                 System.exit(1);
109             }
110
111             root.warn("Karaf started, starting stopwatch");
112             STOPWATCH.start();
113
114             try {
115                 EXECUTOR.schedule(
116                     new ScaleVerifyCallable(netconfDeviceSimulator, params.deviceCount), RETRY_DELAY, TimeUnit.SECONDS);
117                 root.warn("First callable scheduled");
118                 SEMAPHORE.acquire();
119                 root.warn("semaphore released");
120             } catch (InterruptedException e) {
121                 throw new RuntimeException(e);
122             }
123
124             timeoutGuardFuture.cancel(false);
125             params.deviceCount += DEVICE_STEP;
126             netconfDeviceSimulator.close();
127             STOPWATCH.reset();
128
129             cleanup(runtime, params);
130         }
131     }
132
133     private static void setUpLoggers(final TesttoolParameters params) {
134         System.setProperty("log_file_name", "scale-util.log");
135
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");
139     }
140
141     private static void cleanup(final Runtime runtime, final TesttoolParameters params) {
142         try {
143             stopKaraf(runtime, params);
144             deleteFolder(new File(params.distroFolder.getAbsoluteFile() + "/data"));
145
146         } catch (IOException | InterruptedException e) {
147             root.warn("Failed to stop karaf", e);
148             System.exit(1);
149         }
150     }
151
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 = "";
156         do {
157             final Process pgrep = runtime.exec("pgrep -f org.apache.karaf.main.Main");
158
159             controllerPid = CharStreams.toString(new BufferedReader(new InputStreamReader(pgrep.getInputStream())));
160             root.warn(controllerPid);
161             runtime.exec("kill -9 " + controllerPid);
162
163             Thread.sleep(10000L);
164         } while (!controllerPid.isEmpty());
165         deleteFolder(new File(params.distroFolder.getAbsoluteFile() + "/data"));
166     }
167
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()) {
173                     deleteFolder(f);
174                 } else {
175                     if (!f.delete()) {
176                         root.warn("Failed to delete {}", f);
177                     }
178                 }
179             }
180         }
181         if (!folder.delete()) {
182             root.warn("Failed to delete {}", folder);
183         }
184     }
185
186     private static class ScaleVerifyCallable implements Callable<Void> {
187         private static final Logger LOG = LoggerFactory.getLogger(ScaleVerifyCallable.class);
188
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");
192
193         private final AsyncHttpClient asyncHttpClient = new AsyncHttpClient(new Builder()
194                 .setConnectTimeout(Integer.MAX_VALUE)
195                 .setRequestTimeout(Integer.MAX_VALUE)
196                 .setAllowPoolingConnections(true)
197                 .build());
198         private final NetconfDeviceSimulator simulator;
199         private final int deviceCount;
200         private final Request request;
201
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();
211         }
212
213         @Override
214         public Void call() throws Exception {
215             try {
216                 final Response response = asyncHttpClient.executeRequest(request).get();
217
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);
221                 } else {
222                     final String body = response.getResponseBody();
223                     final Matcher matcher = PATTERN.matcher(body);
224                     int count = 0;
225                     while (matcher.find()) {
226                         count++;
227                     }
228                     resultsLog.info("Currently connected devices : {} out of {}, time elapsed: {}",
229                         count, deviceCount + 1, STOPWATCH);
230                     if (count != deviceCount + 1) {
231                         EXECUTOR.schedule(
232                             new ScaleVerifyCallable(simulator, deviceCount), RETRY_DELAY, TimeUnit.SECONDS);
233                     } else {
234                         STOPWATCH.stop();
235                         resultsLog.info("All devices connected in {}", STOPWATCH);
236                         SEMAPHORE.release();
237                     }
238                 }
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);
242             }
243             return null;
244         }
245     }
246
247     private static class TimeoutGuard implements Callable<Void> {
248         @Override
249         public Void call() {
250             resultsLog.warn("Timeout for scale test reached after: {} ..aborting", STOPWATCH);
251             root.warn("Timeout for scale test reached after: {} ..aborting", STOPWATCH);
252             System.exit(0);
253             return null;
254         }
255     }
256
257     @SuppressWarnings("checkstyle:illegalCatch")
258     public static class LoggingWrapperExecutor extends ScheduledThreadPoolExecutor {
259         public LoggingWrapperExecutor(final int corePoolSize) {
260             super(corePoolSize);
261         }
262
263         @Override
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);
266         }
267
268         private static class LogOnExceptionCallable<T> implements Callable<T> {
269             private final Callable<T> theCallable;
270
271             LogOnExceptionCallable(final Callable<T> theCallable) {
272                 this.theCallable = theCallable;
273             }
274
275             @Override
276             public T call() {
277                 try {
278                     return theCallable.call();
279                 } catch (Exception e) {
280                     // log
281                     root.warn("error in executing: " + theCallable + ". It will no longer be run!", e);
282
283                     // rethrow so that the executor can do it's thing
284                     throw new RuntimeException(e);
285                 }
286             }
287         }
288     }
289 }