7c0744bbb441cb6ca343fa94c969f60149a6fbc8
[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
9 package org.opendaylight.netconf.test.tool;
10
11 import ch.qos.logback.classic.Level;
12 import com.google.common.base.Stopwatch;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.ExecutorService;
18 import java.util.concurrent.Executors;
19 import java.util.concurrent.Future;
20 import java.util.concurrent.TimeUnit;
21 import org.opendaylight.netconf.test.tool.config.Configuration;
22 import org.opendaylight.netconf.test.tool.config.ConfigurationBuilder;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 @SuppressFBWarnings("DM_DEFAULT_ENCODING")
27 public final class Main {
28     private static final Logger LOG = LoggerFactory.getLogger(Main.class);
29
30     private Main() {
31
32     }
33
34     @SuppressWarnings("checkstyle:IllegalCatch")
35     @SuppressFBWarnings({"UW_UNCOND_WAIT", "WA_NOT_IN_LOOP"})
36     public static void main(final String[] args) {
37         final TesttoolParameters params = TesttoolParameters.parseArgs(args, TesttoolParameters.getParser());
38         params.validate();
39         final ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory
40             .getLogger(Logger.ROOT_LOGGER_NAME);
41         root.setLevel(params.debug ? Level.DEBUG : Level.INFO);
42
43         final Configuration configuration = new ConfigurationBuilder().from(params).build();
44         final NetconfDeviceSimulator netconfDeviceSimulator = new NetconfDeviceSimulator(configuration);
45         try {
46             LOG.debug("Trying to start netconf test-tool with parameters {}", params);
47             final List<Integer> openDevices = netconfDeviceSimulator.start();
48             if (openDevices.size() == 0) {
49                 LOG.error("Failed to start any simulated devices, exiting...");
50                 System.exit(1);
51             }
52             if (params.controllerDestination != null) {
53                 final ArrayList<ArrayList<Execution.DestToPayload>> allThreadsPayloads = params
54                     .getThreadsPayloads(openDevices);
55                 final ArrayList<Execution> executions = new ArrayList<>();
56                 for (ArrayList<Execution.DestToPayload> payloads : allThreadsPayloads) {
57                     executions.add(new Execution(params, payloads));
58                 }
59                 final ExecutorService executorService = Executors.newFixedThreadPool(params.threadAmount);
60                 final Stopwatch time = Stopwatch.createStarted();
61                 List<Future<Void>> futures = executorService.invokeAll(executions, params.timeOut, TimeUnit.SECONDS);
62                 int threadNum = 0;
63                 for (Future<Void> future : futures) {
64                     threadNum++;
65                     if (future.isCancelled()) {
66                         LOG.info("{}. thread timed out.",threadNum);
67                     } else {
68                         try {
69                             future.get();
70                         } catch (final ExecutionException | InterruptedException e) {
71                             LOG.info("{}. thread failed.", threadNum, e);
72                         }
73                     }
74                 }
75                 time.stop();
76                 LOG.info("Time spent with configuration of devices: {}.",time);
77             }
78         } catch (RuntimeException | InterruptedException e) {
79             LOG.error("Unhandled exception", e);
80             netconfDeviceSimulator.close();
81             System.exit(1);
82         }
83
84         // Block main thread
85         synchronized (netconfDeviceSimulator) {
86             try {
87                 netconfDeviceSimulator.wait();
88             } catch (final InterruptedException e) {
89                 throw new RuntimeException(e);
90             }
91         }
92     }
93 }