b5984a65fa9709e1bb39e58bc2236631ca122e66
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / Main.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.netconf.test.tool;
9
10 import ch.qos.logback.classic.Level;
11 import com.google.common.base.Stopwatch;
12 import com.google.common.collect.Lists;
13 import com.google.common.math.IntMath;
14 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15 import java.math.RoundingMode;
16 import java.util.List;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.ExecutorService;
19 import java.util.concurrent.Executors;
20 import java.util.concurrent.Future;
21 import java.util.concurrent.TimeUnit;
22 import java.util.stream.Collectors;
23 import org.opendaylight.netconf.test.tool.config.Configuration;
24 import org.opendaylight.netconf.test.tool.config.ConfigurationBuilder;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 @SuppressFBWarnings("DM_DEFAULT_ENCODING")
29 public final class Main {
30     private static final Logger LOG = LoggerFactory.getLogger(Main.class);
31
32     private Main() {
33         // hidden on purpose
34     }
35
36     @SuppressWarnings("checkstyle:IllegalCatch")
37     @SuppressFBWarnings({"UW_UNCOND_WAIT", "WA_NOT_IN_LOOP"})
38     public static void main(final String[] args) {
39         final TesttoolParameters params = TesttoolParameters.parseArgs(args, TesttoolParameters.getParser());
40         params.validate();
41         final ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory
42             .getLogger(Logger.ROOT_LOGGER_NAME);
43         root.setLevel(params.debug ? Level.DEBUG : Level.INFO);
44
45         final Configuration configuration = new ConfigurationBuilder().from(params).build();
46         final NetconfDeviceSimulator netconfDeviceSimulator = new NetconfDeviceSimulator(configuration);
47         try {
48             LOG.debug("Trying to start netconf test-tool with parameters {}", params);
49             final List<Integer> openDevices = netconfDeviceSimulator.start();
50             if (openDevices.size() == 0) {
51                 LOG.error("Failed to start any simulated devices, exiting...");
52                 System.exit(1);
53             }
54             //if ODL controller ip is not set NETCONF devices will be started, but not registered at the controller
55             if (params.controllerIp != null) {
56                 final List<Execution> executionThreads = divideDevicesForThreads(openDevices, params);
57                 final ExecutorService executorService = Executors.newFixedThreadPool(params.threadAmount);
58                 final Stopwatch time = Stopwatch.createStarted();
59                 final List<Future<Void>> futures = executorService.invokeAll(executionThreads,
60                         params.timeOut, TimeUnit.SECONDS);
61                 int threadNum = 0;
62                 for (final Future<Void> future : futures) {
63                     threadNum++;
64                     if (future.isCancelled()) {
65                         LOG.info("{}. thread timed out.", threadNum);
66                     } else {
67                         try {
68                             future.get();
69                         } catch (final ExecutionException | InterruptedException e) {
70                             LOG.info("{}. thread failed.", threadNum, e);
71                         }
72                     }
73                 }
74                 time.stop();
75                 LOG.info("Time spent with configuration of devices: {}.", time);
76             }
77         } catch (final RuntimeException | InterruptedException e) {
78             LOG.error("Unhandled exception", e);
79             netconfDeviceSimulator.close();
80             System.exit(1);
81         }
82
83         // Block main thread
84         synchronized (netconfDeviceSimulator) {
85             try {
86                 netconfDeviceSimulator.wait();
87             } catch (final InterruptedException e) {
88                 throw new RuntimeException(e);
89             }
90         }
91     }
92
93     private static List<Execution> divideDevicesForThreads(final List<Integer> openDevices,
94             final TesttoolParameters params) {
95         final int devicesPerThread = IntMath.divide(openDevices.size(), params.threadAmount, RoundingMode.UP);
96         return Lists.partition(openDevices, devicesPerThread).stream()
97                 .map(t -> new Execution(t, params))
98                 .collect(Collectors.toList());
99     }
100 }