Fix testtool device registration
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / Main.java
index e2d5a41bec040bfaa5f511abf9b44432b6d95c5e..b5984a65fa9709e1bb39e58bc2236631ca122e66 100644 (file)
@@ -5,19 +5,21 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.netconf.test.tool;
 
 import ch.qos.logback.classic.Level;
 import com.google.common.base.Stopwatch;
+import com.google.common.collect.Lists;
+import com.google.common.math.IntMath;
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
-import java.util.ArrayList;
+import java.math.RoundingMode;
 import java.util.List;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
 import org.opendaylight.netconf.test.tool.config.Configuration;
 import org.opendaylight.netconf.test.tool.config.ConfigurationBuilder;
 import org.slf4j.Logger;
@@ -28,7 +30,7 @@ public final class Main {
     private static final Logger LOG = LoggerFactory.getLogger(Main.class);
 
     private Main() {
-
+        // hidden on purpose
     }
 
     @SuppressWarnings("checkstyle:IllegalCatch")
@@ -51,20 +53,16 @@ public final class Main {
             }
             //if ODL controller ip is not set NETCONF devices will be started, but not registered at the controller
             if (params.controllerIp != null) {
-                final ArrayList<ArrayList<Execution.DestToPayload>> allThreadsPayloads = params
-                    .getThreadsPayloads(openDevices);
-                final ArrayList<Execution> executions = new ArrayList<>();
-                for (ArrayList<Execution.DestToPayload> payloads : allThreadsPayloads) {
-                    executions.add(new Execution(params, payloads));
-                }
+                final List<Execution> executionThreads = divideDevicesForThreads(openDevices, params);
                 final ExecutorService executorService = Executors.newFixedThreadPool(params.threadAmount);
                 final Stopwatch time = Stopwatch.createStarted();
-                List<Future<Void>> futures = executorService.invokeAll(executions, params.timeOut, TimeUnit.SECONDS);
+                final List<Future<Void>> futures = executorService.invokeAll(executionThreads,
+                        params.timeOut, TimeUnit.SECONDS);
                 int threadNum = 0;
-                for (Future<Void> future : futures) {
+                for (final Future<Void> future : futures) {
                     threadNum++;
                     if (future.isCancelled()) {
-                        LOG.info("{}. thread timed out.",threadNum);
+                        LOG.info("{}. thread timed out.", threadNum);
                     } else {
                         try {
                             future.get();
@@ -74,9 +72,9 @@ public final class Main {
                     }
                 }
                 time.stop();
-                LOG.info("Time spent with configuration of devices: {}.",time);
+                LOG.info("Time spent with configuration of devices: {}.", time);
             }
-        } catch (RuntimeException | InterruptedException e) {
+        } catch (final RuntimeException | InterruptedException e) {
             LOG.error("Unhandled exception", e);
             netconfDeviceSimulator.close();
             System.exit(1);
@@ -91,4 +89,12 @@ public final class Main {
             }
         }
     }
+
+    private static List<Execution> divideDevicesForThreads(final List<Integer> openDevices,
+            final TesttoolParameters params) {
+        final int devicesPerThread = IntMath.divide(openDevices.size(), params.threadAmount, RoundingMode.UP);
+        return Lists.partition(openDevices, devicesPerThread).stream()
+                .map(t -> new Execution(t, params))
+                .collect(Collectors.toList());
+    }
 }