Merge "NETCONF-557: Add support for URL capability"
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / ScaleUtil.java
index e28d936f6df34644ab5469c552e887314803a59a..7d4d7e4cf19e143b755cf8e5f06ab736ed4f5ed6 100644 (file)
@@ -15,6 +15,7 @@ import com.ning.http.client.AsyncHttpClient;
 import com.ning.http.client.AsyncHttpClientConfig.Builder;
 import com.ning.http.client.Request;
 import com.ning.http.client.Response;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.IOException;
@@ -30,12 +31,13 @@ import java.util.concurrent.Semaphore;
 import java.util.concurrent.TimeUnit;
 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 {
+@SuppressFBWarnings({"DM_EXIT", "DM_DEFAULT_ENCODING", "SLF4J_LOGGER_SHOULD_BE_FINAL"})
+public final class ScaleUtil {
     private static final ScheduledExecutorService EXECUTOR = new LoggingWrapperExecutor(4);
     private static final Semaphore SEMAPHORE = new Semaphore(0);
     private static final Stopwatch STOPWATCH = Stopwatch.createUnstarted();
@@ -47,6 +49,9 @@ public class ScaleUtil {
     private static ch.qos.logback.classic.Logger root;
     private static Logger resultsLog;
 
+    private ScaleUtil() {
+    }
+
     @SuppressWarnings("checkstyle:illegalCatch")
     public static void main(final String[] args) {
         final TesttoolParameters params = TesttoolParameters.parseArgs(args, TesttoolParameters.getParser());
@@ -60,23 +65,28 @@ public class ScaleUtil {
         while (true) {
             root.warn("Starting scale test with {} devices", params.deviceCount);
             final ScheduledFuture timeoutGuardFuture = EXECUTOR.schedule(new TimeoutGuard(), TIMEOUT, TimeUnit.MINUTES);
-            final NetconfDeviceSimulator netconfDeviceSimulator = new NetconfDeviceSimulator(params.threadAmount);
+            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 List<File> generated = configGenerator.generate(
-                            params.ssh, params.generateConfigBatchSize,
-                            params.generateConfigsTimeout, params.generateConfigsAddress,
-                            params.devicesPerPort);
-                    configGenerator.updateFeatureFile(generated);
-                    configGenerator.changeLoadOrder();
+
+                if (params.distroFolder == null) {
+                    root.error("Distro folder is not set, exiting...");
+                    System.exit(1);
                 }
+
+                final Main.ConfigGenerator configGenerator = new Main.ConfigGenerator(
+                        params.distroFolder, openDevices);
+                final List<File> generated = configGenerator.generate(
+                        params.ssh, params.generateConfigBatchSize,
+                        params.generateConfigsTimeout, params.generateConfigsAddress,
+                        params.devicesPerPort);
+                configGenerator.updateFeatureFile(generated);
+                configGenerator.changeLoadOrder();
             } catch (final Exception e) {
                 root.error("Unhandled exception", e);
                 netconfDeviceSimulator.close();
@@ -168,34 +178,25 @@ public class ScaleUtil {
         deleteFolder(new File(params.distroFolder.getAbsoluteFile() + "/data"));
     }
 
-    private static void deleteFolder(File folder) {
+    private static void deleteFolder(final File folder) {
         File[] files = folder.listFiles();
         if (files != null) { //some JVMs return null for empty dirs
             for (File f : files) {
                 if (f.isDirectory()) {
                     deleteFolder(f);
                 } else {
-                    f.delete();
+                    if (!f.delete()) {
+                        root.warn("Failed to delete {}", f);
+                    }
                 }
             }
         }
-        folder.delete();
-    }
-
-    private static TesttoolParameters parseArgs(final String[] args, final ArgumentParser parser) {
-        final TesttoolParameters parameters = new TesttoolParameters();
-        try {
-            parser.parseArgs(args, parameters);
-            return parameters;
-        } catch (ArgumentParserException e) {
-            parser.handleError(e);
+        if (!folder.delete()) {
+            root.warn("Failed to delete {}", folder);
         }
-
-        System.exit(1);
-        return null;
     }
 
-    private static class ScaleVerifyCallable implements Callable {
+    private static class ScaleVerifyCallable implements Callable<Void> {
         private static final Logger LOG = LoggerFactory.getLogger(ScaleVerifyCallable.class);
 
         private static final String RESTCONF_URL
@@ -223,7 +224,7 @@ public class ScaleUtil {
         }
 
         @Override
-        public Object call() throws Exception {
+        public Void call() throws Exception {
             try {
                 final Response response = asyncHttpClient.executeRequest(request).get();
 
@@ -256,9 +257,9 @@ public class ScaleUtil {
         }
     }
 
-    private static class TimeoutGuard implements Callable {
+    private static class TimeoutGuard implements Callable<Void> {
         @Override
-        public Object call() throws Exception {
+        public Void call() {
             resultsLog.warn("Timeout for scale test reached after: {} ..aborting", STOPWATCH);
             root.warn("Timeout for scale test reached after: {} ..aborting", STOPWATCH);
             System.exit(0);
@@ -268,32 +269,26 @@ public class ScaleUtil {
 
     @SuppressWarnings("checkstyle:illegalCatch")
     public static class LoggingWrapperExecutor extends ScheduledThreadPoolExecutor {
-        public LoggingWrapperExecutor(int corePoolSize) {
+        public LoggingWrapperExecutor(final int corePoolSize) {
             super(corePoolSize);
         }
 
         @Override
-        public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
-            return super.schedule(wrapCallable(callable), delay, unit);
-        }
-
-        private Callable wrapCallable(Callable callable) {
-            return new LogOnExceptionCallable(callable);
+        public <V> ScheduledFuture<V> schedule(final Callable<V> callable, final long delay, final TimeUnit unit) {
+            return super.schedule(new LogOnExceptionCallable<>(callable), delay, unit);
         }
 
-        private class LogOnExceptionCallable implements Callable {
-            private Callable theCallable;
+        private static class LogOnExceptionCallable<T> implements Callable<T> {
+            private final Callable<T> theCallable;
 
-            LogOnExceptionCallable(Callable theCallable) {
-                super();
+            LogOnExceptionCallable(final Callable<T> theCallable) {
                 this.theCallable = theCallable;
             }
 
             @Override
-            public Object call() throws Exception {
+            public T call() {
                 try {
-                    theCallable.call();
-                    return null;
+                    return theCallable.call();
                 } catch (Exception e) {
                     // log
                     root.warn("error in executing: " + theCallable + ". It will no longer be run!", e);