2 * Copyright (c) 2016 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.openflowjava.protocol.impl.clients;
11 import static com.google.common.base.Preconditions.checkArgument;
13 import javax.annotation.Nullable;
14 import java.net.InetAddress;
15 import java.net.UnknownHostException;
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.concurrent.Callable;
19 import java.util.concurrent.ExecutorService;
20 import java.util.concurrent.Executors;
21 import java.util.concurrent.TimeUnit;
23 import com.google.common.util.concurrent.FutureCallback;
24 import com.google.common.util.concurrent.Futures;
25 import com.google.common.util.concurrent.ListenableFuture;
26 import com.google.common.util.concurrent.ListeningExecutorService;
27 import com.google.common.util.concurrent.MoreExecutors;
28 import io.netty.bootstrap.Bootstrap;
29 import io.netty.channel.EventLoopGroup;
30 import io.netty.channel.nio.NioEventLoopGroup;
31 import net.sourceforge.argparse4j.ArgumentParsers;
32 import net.sourceforge.argparse4j.annotation.Arg;
33 import net.sourceforge.argparse4j.inf.ArgumentParser;
34 import net.sourceforge.argparse4j.inf.ArgumentParserException;
35 import org.slf4j.LoggerFactory;
38 * ControllerConnectionTestTool class, utilities for testing device's connect
39 * @author Jozef Bacigal
42 public class ControllerConnectionTestTool {
44 private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(ControllerConnectionTestTool.class);
46 public static class Params {
48 @Arg(dest = "controller-ip")
49 public String controllerIP;
51 @Arg(dest = "devices-count")
52 public int deviceCount;
57 @Arg(dest = "threads")
63 @Arg(dest = "timeout")
72 static ArgumentParser getParser() throws UnknownHostException {
73 final ArgumentParser parser = ArgumentParsers.newArgumentParser("openflowjava test-tool");
75 parser.description("Openflowjava switch -> controller connector simulator");
77 parser.addArgument("--device-count")
80 .help("Number of simulated switches. Has to be more than 0")
81 .dest("devices-count");
83 parser.addArgument("--controller-ip")
85 .setDefault("127.0.0.1")
86 .help("ODL controller ip address")
87 .dest("controller-ip");
89 parser.addArgument("--ssl")
92 .help("Use secured connection")
95 parser.addArgument("--threads")
98 .help("Number of threads: MAX 1024")
101 parser.addArgument("--port")
104 .help("Connection port")
107 parser.addArgument("--timeout")
110 .help("Timeout in seconds")
113 parser.addArgument("--scenarioTries")
116 .help("Number of tries in scenario, while waiting for response")
119 parser.addArgument("--timeBetweenScenario")
122 .help("Waiting time in milliseconds between tries.")
129 checkArgument(deviceCount > 0, "Switch count has to be > 0");
130 checkArgument(threads > 0 && threads < 1024, "Switch count has to be > 0 and < 1024");
134 public static void main(final String[] args) {
136 List<Callable<Boolean>> callableList = new ArrayList<>();
137 final EventLoopGroup workerGroup = new NioEventLoopGroup();
140 final Params params = parseArgs(args, Params.getParser());
143 for(int loop=0;loop < params.deviceCount; loop++){
145 CallableClient cc = new CallableClient(
148 InetAddress.getByName(params.controllerIP),
149 "Switch no." + String.valueOf(loop),
150 new ScenarioHandler(ScenarioFactory.createHandshakeScenarioWithBarrier(), params.freeze, params.sleep),
154 callableList.add(cc);
158 ExecutorService executorService = Executors.newFixedThreadPool(params.threads);
159 final ListeningExecutorService listeningExecutorService = MoreExecutors.listeningDecorator(executorService);
161 final List<ListenableFuture<Boolean>> listenableFutures = new ArrayList<>();
162 for (Callable<Boolean> booleanCallable : callableList) {
163 listenableFutures.add(listeningExecutorService.submit(booleanCallable));
165 final ListenableFuture<List<Boolean>> summaryFuture = Futures.successfulAsList(listenableFutures);
166 List<Boolean> booleanList = summaryFuture.get(params.timeout, TimeUnit.SECONDS);
167 Futures.addCallback(summaryFuture, new FutureCallback<List<Boolean>>() {
169 public void onSuccess(@Nullable final List<Boolean> booleanList) {
170 LOG.info("Tests finished");
171 workerGroup.shutdownGracefully();
172 LOG.info("Summary:");
175 for (Boolean aBoolean : booleanList) {
182 LOG.info("Tests OK: {}", testsOK);
183 LOG.info("Tests failure: {}", testFailure);
188 public void onFailure(final Throwable throwable) {
189 LOG.warn("Tests call failure");
190 workerGroup.shutdownGracefully();
194 } catch (Exception e) {
195 LOG.warn("Exception has been thrown: {}", e);
200 private static Params parseArgs(final String[] args, final ArgumentParser parser) throws ArgumentParserException {
201 final Params opt = new Params();
202 parser.parseArgs(args, opt);