e2d5a41bec040bfaa5f511abf9b44432b6d95c5e
[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 ODL controller ip is not set NETCONF devices will be started, but not registered at the controller
53             if (params.controllerIp != null) {
54                 final ArrayList<ArrayList<Execution.DestToPayload>> allThreadsPayloads = params
55                     .getThreadsPayloads(openDevices);
56                 final ArrayList<Execution> executions = new ArrayList<>();
57                 for (ArrayList<Execution.DestToPayload> payloads : allThreadsPayloads) {
58                     executions.add(new Execution(params, payloads));
59                 }
60                 final ExecutorService executorService = Executors.newFixedThreadPool(params.threadAmount);
61                 final Stopwatch time = Stopwatch.createStarted();
62                 List<Future<Void>> futures = executorService.invokeAll(executions, params.timeOut, TimeUnit.SECONDS);
63                 int threadNum = 0;
64                 for (Future<Void> future : futures) {
65                     threadNum++;
66                     if (future.isCancelled()) {
67                         LOG.info("{}. thread timed out.",threadNum);
68                     } else {
69                         try {
70                             future.get();
71                         } catch (final ExecutionException | InterruptedException e) {
72                             LOG.info("{}. thread failed.", threadNum, e);
73                         }
74                     }
75                 }
76                 time.stop();
77                 LOG.info("Time spent with configuration of devices: {}.",time);
78             }
79         } catch (RuntimeException | InterruptedException e) {
80             LOG.error("Unhandled exception", e);
81             netconfDeviceSimulator.close();
82             System.exit(1);
83         }
84
85         // Block main thread
86         synchronized (netconfDeviceSimulator) {
87             try {
88                 netconfDeviceSimulator.wait();
89             } catch (final InterruptedException e) {
90                 throw new RuntimeException(e);
91             }
92         }
93     }
94 }