2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.test.tool;
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;
28 @SuppressFBWarnings("DM_DEFAULT_ENCODING")
29 public final class Main {
30 private static final Logger LOG = LoggerFactory.getLogger(Main.class);
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());
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);
45 final Configuration configuration = new ConfigurationBuilder().from(params).build();
46 final NetconfDeviceSimulator netconfDeviceSimulator = new NetconfDeviceSimulator(configuration);
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...");
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);
62 for (final Future<Void> future : futures) {
64 if (future.isCancelled()) {
65 LOG.info("{}. thread timed out.", threadNum);
69 } catch (final ExecutionException | InterruptedException e) {
70 LOG.info("{}. thread failed.", threadNum, e);
75 LOG.info("Time spent with configuration of devices: {}.", time);
77 } catch (final RuntimeException | InterruptedException e) {
78 LOG.error("Unhandled exception", e);
79 netconfDeviceSimulator.close();
84 synchronized (netconfDeviceSimulator) {
86 netconfDeviceSimulator.wait();
87 } catch (final InterruptedException e) {
88 throw new IllegalStateException("Interrupted while waiting", e);
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());