NetConf test tool improved so now it can be
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / ScaleUtil.java
index 555f6833b2b38eb10a96c1d2bc7e6c7225fa5361..36e336ad69435fc3b05d283f0d2369578ed59eef 100644 (file)
@@ -9,10 +9,6 @@
 package org.opendaylight.netconf.test.tool;
 
 import ch.qos.logback.classic.Level;
-import ch.qos.logback.classic.LoggerContext;
-import ch.qos.logback.classic.util.ContextInitializer;
-import ch.qos.logback.core.joran.spi.JoranException;
-import ch.qos.logback.core.util.StatusPrinter;
 import com.google.common.base.Stopwatch;
 import com.google.common.io.CharStreams;
 import com.ning.http.client.AsyncHttpClient;
@@ -36,23 +32,24 @@ import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import net.sourceforge.argparse4j.inf.ArgumentParser;
 import net.sourceforge.argparse4j.inf.ArgumentParserException;
+import org.opendaylight.netconf.test.tool.config.Configuration;
+import org.opendaylight.netconf.test.tool.config.ConfigurationBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class ScaleUtil {
-    private static Logger RESULTS_LOG ;
-    private static final ScheduledExecutorService executor = new LoggingWrapperExecutor(4);
+    private static final ScheduledExecutorService EXECUTOR = new LoggingWrapperExecutor(4);
+    private static final Semaphore SEMAPHORE = new Semaphore(0);
+    private static final Stopwatch STOPWATCH = Stopwatch.createUnstarted();
 
-    private static final int deviceStep = 1000;
-    private static final long retryDelay = 10l;
-    private static final long timeout = 20l;
+    private static final long TIMEOUT = 20L;
+    private static final long RETRY_DELAY = 10L;
+    private static final int DEVICE_STEP = 1000;
 
-    private static final Stopwatch stopwatch = Stopwatch.createUnstarted();
-
-    private static ScheduledFuture timeoutGuardFuture;
     private static ch.qos.logback.classic.Logger root;
-    private static final Semaphore semaphore = new Semaphore(0);
+    private static Logger resultsLog;
 
+    @SuppressWarnings("checkstyle:illegalCatch")
     public static void main(final String[] args) {
         final TesttoolParameters params = TesttoolParameters.parseArgs(args, TesttoolParameters.getParser());
 
@@ -64,16 +61,18 @@ public class ScaleUtil {
 
         while (true) {
             root.warn("Starting scale test with {} devices", params.deviceCount);
-            timeoutGuardFuture = executor.schedule(new TimeoutGuard(), timeout, TimeUnit.MINUTES);
-            final NetconfDeviceSimulator netconfDeviceSimulator = new NetconfDeviceSimulator(params.threadAmount);
+            final ScheduledFuture timeoutGuardFuture = EXECUTOR.schedule(new TimeoutGuard(), TIMEOUT, TimeUnit.MINUTES);
+            final Configuration configuration = new ConfigurationBuilder().from(params).build();
+            final NetconfDeviceSimulator netconfDeviceSimulator = new NetconfDeviceSimulator(configuration);
             try {
-                final List<Integer> openDevices = netconfDeviceSimulator.start(params);
+                final List<Integer> openDevices = netconfDeviceSimulator.start();
                 if (openDevices.size() == 0) {
                     root.error("Failed to start any simulated devices, exiting...");
                     System.exit(1);
                 }
                 if (params.distroFolder != null) {
-                    final Main.ConfigGenerator configGenerator = new Main.ConfigGenerator(params.distroFolder, openDevices);
+                    final Main.ConfigGenerator configGenerator = new Main.ConfigGenerator(
+                        params.distroFolder, openDevices);
                     final List<File> generated = configGenerator.generate(
                             params.ssh, params.generateConfigBatchSize,
                             params.generateConfigsTimeout, params.generateConfigsAddress,
@@ -94,17 +93,21 @@ public class ScaleUtil {
                 do {
                     final Process exec = runtime.exec(params.distroFolder.getAbsolutePath() + "/bin/status");
                     try {
-                        Thread.sleep(2000l);
+                        Thread.sleep(2000L);
                     } catch (InterruptedException e) {
                         root.warn("Failed to sleep", e);
                     }
                     status = CharStreams.toString(new BufferedReader(new InputStreamReader(exec.getInputStream())));
                     root.warn("Current status: {}", status);
                 } while (!status.startsWith("Running ..."));
-                root.warn("Doing feature install {}", params.distroFolder.getAbsolutePath() + "/bin/client -u karaf feature:install odl-restconf-noauth odl-netconf-connector-all");
-                final Process featureInstall = runtime.exec(params.distroFolder.getAbsolutePath() + "/bin/client -u karaf feature:install odl-restconf-noauth odl-netconf-connector-all");
-                root.warn(CharStreams.toString(new BufferedReader(new InputStreamReader(featureInstall.getInputStream()))));
-                root.warn(CharStreams.toString(new BufferedReader(new InputStreamReader(featureInstall.getErrorStream()))));
+                root.warn("Doing feature install {}", params.distroFolder.getAbsolutePath()
+                    + "/bin/client -u karaf feature:install odl-restconf-noauth odl-netconf-connector-all");
+                final Process featureInstall = runtime.exec(params.distroFolder.getAbsolutePath()
+                    + "/bin/client -u karaf feature:install odl-restconf-noauth odl-netconf-connector-all");
+                root.warn(
+                    CharStreams.toString(new BufferedReader(new InputStreamReader(featureInstall.getInputStream()))));
+                root.warn(
+                    CharStreams.toString(new BufferedReader(new InputStreamReader(featureInstall.getErrorStream()))));
 
             } catch (IOException e) {
                 root.warn("Failed to start karaf", e);
@@ -112,21 +115,22 @@ public class ScaleUtil {
             }
 
             root.warn("Karaf started, starting stopwatch");
-            stopwatch.start();
+            STOPWATCH.start();
 
             try {
-                executor.schedule(new ScaleVerifyCallable(netconfDeviceSimulator, params.deviceCount), retryDelay, TimeUnit.SECONDS);
+                EXECUTOR.schedule(
+                    new ScaleVerifyCallable(netconfDeviceSimulator, params.deviceCount), RETRY_DELAY, TimeUnit.SECONDS);
                 root.warn("First callable scheduled");
-                semaphore.acquire();
+                SEMAPHORE.acquire();
                 root.warn("semaphore released");
             } catch (InterruptedException e) {
                 throw new RuntimeException(e);
             }
 
             timeoutGuardFuture.cancel(false);
-            params.deviceCount += deviceStep;
+            params.deviceCount += DEVICE_STEP;
             netconfDeviceSimulator.close();
-            stopwatch.reset();
+            STOPWATCH.reset();
 
             cleanup(runtime, params);
         }
@@ -137,7 +141,7 @@ public class ScaleUtil {
 
         root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
         root.setLevel(params.debug ? Level.DEBUG : Level.INFO);
-        RESULTS_LOG = LoggerFactory.getLogger("results");
+        resultsLog = LoggerFactory.getLogger("results");
     }
 
     private static void cleanup(final Runtime runtime, final TesttoolParameters params) {
@@ -151,27 +155,27 @@ public class ScaleUtil {
         }
     }
 
-    private static void stopKaraf(final Runtime runtime, final TesttoolParameters params) throws IOException, InterruptedException {
+    private static void stopKaraf(final Runtime runtime, final TesttoolParameters params)
+            throws IOException, InterruptedException {
         root.info("Stopping karaf and sleeping for 10 sec..");
         String controllerPid = "";
         do {
-
             final Process pgrep = runtime.exec("pgrep -f org.apache.karaf.main.Main");
 
             controllerPid = CharStreams.toString(new BufferedReader(new InputStreamReader(pgrep.getInputStream())));
             root.warn(controllerPid);
             runtime.exec("kill -9 " + controllerPid);
 
-            Thread.sleep(10000l);
+            Thread.sleep(10000L);
         } while (!controllerPid.isEmpty());
         deleteFolder(new File(params.distroFolder.getAbsoluteFile() + "/data"));
     }
 
     private static void deleteFolder(File folder) {
         File[] files = folder.listFiles();
-        if(files!=null) { //some JVMs return null for empty dirs
-            for(File f: files) {
-                if(f.isDirectory()) {
+        if (files != null) { //some JVMs return null for empty dirs
+            for (File f : files) {
+                if (f.isDirectory()) {
                     deleteFolder(f);
                 } else {
                     f.delete();
@@ -195,10 +199,10 @@ public class ScaleUtil {
     }
 
     private static class ScaleVerifyCallable implements Callable {
-
         private static final Logger LOG = LoggerFactory.getLogger(ScaleVerifyCallable.class);
 
-        private static final String RESTCONF_URL = "http://127.0.0.1:8181/restconf/operational/network-topology:network-topology/topology/topology-netconf/";
+        private static final String RESTCONF_URL
+                = "http://127.0.0.1:8181/restconf/operational/network-topology:network-topology/topology/topology-netconf/";
         private static final Pattern PATTERN = Pattern.compile("connected");
 
         private final AsyncHttpClient asyncHttpClient = new AsyncHttpClient(new Builder()
@@ -210,7 +214,7 @@ public class ScaleUtil {
         private final int deviceCount;
         private final Request request;
 
-        public ScaleVerifyCallable(final NetconfDeviceSimulator simulator, final int deviceCount) {
+        ScaleVerifyCallable(final NetconfDeviceSimulator simulator, final int deviceCount) {
             LOG.info("New callable created");
             this.simulator = simulator;
             this.deviceCount = deviceCount;
@@ -228,7 +232,7 @@ public class ScaleUtil {
 
                 if (response.getStatusCode() != 200 && response.getStatusCode() != 204) {
                     LOG.warn("Request failed, status code: {}", response.getStatusCode() + response.getStatusText());
-                    executor.schedule(new ScaleVerifyCallable(simulator, deviceCount), retryDelay, TimeUnit.SECONDS);
+                    EXECUTOR.schedule(new ScaleVerifyCallable(simulator, deviceCount), RETRY_DELAY, TimeUnit.SECONDS);
                 } else {
                     final String body = response.getResponseBody();
                     final Matcher matcher = PATTERN.matcher(body);
@@ -236,36 +240,37 @@ public class ScaleUtil {
                     while (matcher.find()) {
                         count++;
                     }
-                    RESULTS_LOG.info("Currently connected devices : {} out of {}, time elapsed: {}", count, deviceCount + 1, stopwatch);
+                    resultsLog.info("Currently connected devices : {} out of {}, time elapsed: {}",
+                        count, deviceCount + 1, STOPWATCH);
                     if (count != deviceCount + 1) {
-                        executor.schedule(new ScaleVerifyCallable(simulator, deviceCount), retryDelay, TimeUnit.SECONDS);
+                        EXECUTOR.schedule(
+                            new ScaleVerifyCallable(simulator, deviceCount), RETRY_DELAY, TimeUnit.SECONDS);
                     } else {
-                        stopwatch.stop();
-                        RESULTS_LOG.info("All devices connected in {}", stopwatch);
-                        semaphore.release();
+                        STOPWATCH.stop();
+                        resultsLog.info("All devices connected in {}", STOPWATCH);
+                        SEMAPHORE.release();
                     }
                 }
             } catch (ConnectException | ExecutionException e) {
                 LOG.warn("Failed to connect to Restconf, is the controller running?", e);
-                executor.schedule(new ScaleVerifyCallable(simulator, deviceCount), retryDelay, TimeUnit.SECONDS);
+                EXECUTOR.schedule(new ScaleVerifyCallable(simulator, deviceCount), RETRY_DELAY, TimeUnit.SECONDS);
             }
             return null;
         }
     }
 
     private static class TimeoutGuard implements Callable {
-
         @Override
         public Object call() throws Exception {
-            RESULTS_LOG.warn("Timeout for scale test reached after: {} ..aborting", stopwatch);
-            root.warn("Timeout for scale test reached after: {} ..aborting", stopwatch);
+            resultsLog.warn("Timeout for scale test reached after: {} ..aborting", STOPWATCH);
+            root.warn("Timeout for scale test reached after: {} ..aborting", STOPWATCH);
             System.exit(0);
             return null;
         }
     }
 
+    @SuppressWarnings("checkstyle:illegalCatch")
     public static class LoggingWrapperExecutor extends ScheduledThreadPoolExecutor {
-
         public LoggingWrapperExecutor(int corePoolSize) {
             super(corePoolSize);
         }
@@ -282,7 +287,7 @@ public class ScaleUtil {
         private class LogOnExceptionCallable implements Callable {
             private Callable theCallable;
 
-            public LogOnExceptionCallable(Callable theCallable) {
+            LogOnExceptionCallable(Callable theCallable) {
                 super();
                 this.theCallable = theCallable;
             }