Remove explicit default super-constructor calls
[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 java.io.BufferedReader;
19 import java.io.File;
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.opendaylight.netconf.test.tool.config.Configuration;
36 import org.opendaylight.netconf.test.tool.config.ConfigurationBuilder;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
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, TimeUnit.MINUTES);
68             final Configuration configuration = new ConfigurationBuilder().from(params).build();
69             final NetconfDeviceSimulator netconfDeviceSimulator = new NetconfDeviceSimulator(configuration);
70             try {
71                 final List<Integer> openDevices = netconfDeviceSimulator.start();
72                 if (openDevices.size() == 0) {
73                     root.error("Failed to start any simulated devices, exiting...");
74                     System.exit(1);
75                 }
76                 if (params.distroFolder != null) {
77                     final Main.ConfigGenerator configGenerator = new Main.ConfigGenerator(
78                         params.distroFolder, openDevices);
79                     final List<File> generated = configGenerator.generate(
80                             params.ssh, params.generateConfigBatchSize,
81                             params.generateConfigsTimeout, params.generateConfigsAddress,
82                             params.devicesPerPort);
83                     configGenerator.updateFeatureFile(generated);
84                     configGenerator.changeLoadOrder();
85                 }
86             } catch (final Exception e) {
87                 root.error("Unhandled exception", e);
88                 netconfDeviceSimulator.close();
89                 System.exit(1);
90             }
91
92             root.warn(params.distroFolder.getAbsolutePath());
93             try {
94                 runtime.exec(params.distroFolder.getAbsolutePath() + "/bin/start");
95                 String status;
96                 do {
97                     final Process exec = runtime.exec(params.distroFolder.getAbsolutePath() + "/bin/status");
98                     try {
99                         Thread.sleep(2000L);
100                     } catch (InterruptedException e) {
101                         root.warn("Failed to sleep", e);
102                     }
103                     status = CharStreams.toString(new BufferedReader(new InputStreamReader(exec.getInputStream())));
104                     root.warn("Current status: {}", status);
105                 } while (!status.startsWith("Running ..."));
106                 root.warn("Doing feature install {}", params.distroFolder.getAbsolutePath()
107                     + "/bin/client -u karaf feature:install odl-restconf-noauth odl-netconf-connector-all");
108                 final Process featureInstall = runtime.exec(params.distroFolder.getAbsolutePath()
109                     + "/bin/client -u karaf feature:install odl-restconf-noauth odl-netconf-connector-all");
110                 root.warn(
111                     CharStreams.toString(new BufferedReader(new InputStreamReader(featureInstall.getInputStream()))));
112                 root.warn(
113                     CharStreams.toString(new BufferedReader(new InputStreamReader(featureInstall.getErrorStream()))));
114
115             } catch (IOException e) {
116                 root.warn("Failed to start karaf", e);
117                 System.exit(1);
118             }
119
120             root.warn("Karaf started, starting stopwatch");
121             STOPWATCH.start();
122
123             try {
124                 EXECUTOR.schedule(
125                     new ScaleVerifyCallable(netconfDeviceSimulator, params.deviceCount), RETRY_DELAY, TimeUnit.SECONDS);
126                 root.warn("First callable scheduled");
127                 SEMAPHORE.acquire();
128                 root.warn("semaphore released");
129             } catch (InterruptedException e) {
130                 throw new RuntimeException(e);
131             }
132
133             timeoutGuardFuture.cancel(false);
134             params.deviceCount += DEVICE_STEP;
135             netconfDeviceSimulator.close();
136             STOPWATCH.reset();
137
138             cleanup(runtime, params);
139         }
140     }
141
142     private static void setUpLoggers(final TesttoolParameters params) {
143         System.setProperty("log_file_name", "scale-util.log");
144
145         root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
146         root.setLevel(params.debug ? Level.DEBUG : Level.INFO);
147         resultsLog = LoggerFactory.getLogger("results");
148     }
149
150     private static void cleanup(final Runtime runtime, final TesttoolParameters params) {
151         try {
152             stopKaraf(runtime, params);
153             deleteFolder(new File(params.distroFolder.getAbsoluteFile() + "/data"));
154
155         } catch (IOException | InterruptedException e) {
156             root.warn("Failed to stop karaf", e);
157             System.exit(1);
158         }
159     }
160
161     private static void stopKaraf(final Runtime runtime, final TesttoolParameters params)
162             throws IOException, InterruptedException {
163         root.info("Stopping karaf and sleeping for 10 sec..");
164         String controllerPid = "";
165         do {
166             final Process pgrep = runtime.exec("pgrep -f org.apache.karaf.main.Main");
167
168             controllerPid = CharStreams.toString(new BufferedReader(new InputStreamReader(pgrep.getInputStream())));
169             root.warn(controllerPid);
170             runtime.exec("kill -9 " + controllerPid);
171
172             Thread.sleep(10000L);
173         } while (!controllerPid.isEmpty());
174         deleteFolder(new File(params.distroFolder.getAbsoluteFile() + "/data"));
175     }
176
177     private static void deleteFolder(File folder) {
178         File[] files = folder.listFiles();
179         if (files != null) { //some JVMs return null for empty dirs
180             for (File f : files) {
181                 if (f.isDirectory()) {
182                     deleteFolder(f);
183                 } else {
184                     f.delete();
185                 }
186             }
187         }
188         folder.delete();
189     }
190
191     private static TesttoolParameters parseArgs(final String[] args, final ArgumentParser parser) {
192         final TesttoolParameters parameters = new TesttoolParameters();
193         try {
194             parser.parseArgs(args, parameters);
195             return parameters;
196         } catch (ArgumentParserException e) {
197             parser.handleError(e);
198         }
199
200         System.exit(1);
201         return null;
202     }
203
204     private static class ScaleVerifyCallable implements Callable {
205         private static final Logger LOG = LoggerFactory.getLogger(ScaleVerifyCallable.class);
206
207         private static final String RESTCONF_URL
208                 = "http://127.0.0.1:8181/restconf/operational/network-topology:network-topology/topology/topology-netconf/";
209         private static final Pattern PATTERN = Pattern.compile("connected");
210
211         private final AsyncHttpClient asyncHttpClient = new AsyncHttpClient(new Builder()
212                 .setConnectTimeout(Integer.MAX_VALUE)
213                 .setRequestTimeout(Integer.MAX_VALUE)
214                 .setAllowPoolingConnections(true)
215                 .build());
216         private final NetconfDeviceSimulator simulator;
217         private final int deviceCount;
218         private final Request request;
219
220         ScaleVerifyCallable(final NetconfDeviceSimulator simulator, final int deviceCount) {
221             LOG.info("New callable created");
222             this.simulator = simulator;
223             this.deviceCount = deviceCount;
224             AsyncHttpClient.BoundRequestBuilder requestBuilder = asyncHttpClient.prepareGet(RESTCONF_URL)
225                     .addHeader("content-type", "application/xml")
226                     .addHeader("Accept", "application/xml")
227                     .setRequestTimeout(Integer.MAX_VALUE);
228             request = requestBuilder.build();
229         }
230
231         @Override
232         public Object call() throws Exception {
233             try {
234                 final Response response = asyncHttpClient.executeRequest(request).get();
235
236                 if (response.getStatusCode() != 200 && response.getStatusCode() != 204) {
237                     LOG.warn("Request failed, status code: {}", response.getStatusCode() + response.getStatusText());
238                     EXECUTOR.schedule(new ScaleVerifyCallable(simulator, deviceCount), RETRY_DELAY, TimeUnit.SECONDS);
239                 } else {
240                     final String body = response.getResponseBody();
241                     final Matcher matcher = PATTERN.matcher(body);
242                     int count = 0;
243                     while (matcher.find()) {
244                         count++;
245                     }
246                     resultsLog.info("Currently connected devices : {} out of {}, time elapsed: {}",
247                         count, deviceCount + 1, STOPWATCH);
248                     if (count != deviceCount + 1) {
249                         EXECUTOR.schedule(
250                             new ScaleVerifyCallable(simulator, deviceCount), RETRY_DELAY, TimeUnit.SECONDS);
251                     } else {
252                         STOPWATCH.stop();
253                         resultsLog.info("All devices connected in {}", STOPWATCH);
254                         SEMAPHORE.release();
255                     }
256                 }
257             } catch (ConnectException | ExecutionException e) {
258                 LOG.warn("Failed to connect to Restconf, is the controller running?", e);
259                 EXECUTOR.schedule(new ScaleVerifyCallable(simulator, deviceCount), RETRY_DELAY, TimeUnit.SECONDS);
260             }
261             return null;
262         }
263     }
264
265     private static class TimeoutGuard implements Callable {
266         @Override
267         public Object call() throws Exception {
268             resultsLog.warn("Timeout for scale test reached after: {} ..aborting", STOPWATCH);
269             root.warn("Timeout for scale test reached after: {} ..aborting", STOPWATCH);
270             System.exit(0);
271             return null;
272         }
273     }
274
275     @SuppressWarnings("checkstyle:illegalCatch")
276     public static class LoggingWrapperExecutor extends ScheduledThreadPoolExecutor {
277         public LoggingWrapperExecutor(int corePoolSize) {
278             super(corePoolSize);
279         }
280
281         @Override
282         public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
283             return super.schedule(wrapCallable(callable), delay, unit);
284         }
285
286         private Callable wrapCallable(Callable callable) {
287             return new LogOnExceptionCallable(callable);
288         }
289
290         private class LogOnExceptionCallable implements Callable {
291             private Callable theCallable;
292
293             LogOnExceptionCallable(Callable theCallable) {
294                 this.theCallable = theCallable;
295             }
296
297             @Override
298             public Object call() throws Exception {
299                 try {
300                     theCallable.call();
301                     return null;
302                 } catch (Exception e) {
303                     // log
304                     root.warn("error in executing: " + theCallable + ". It will no longer be run!", e);
305
306                     // rethrow so that the executor can do it's thing
307                     throw new RuntimeException(e);
308                 }
309             }
310         }
311     }
312 }