67367b829cb23b9ec7f85a1bc2638b5dac88ec6e
[openflowjava.git] / simple-client / src / main / java / org / opendaylight / openflowjava / protocol / impl / clients / ControllerConnectionTestTool.java
1 /*
2  * Copyright (c) 2016 Pantheon Technologies s.r.o. 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.openflowjava.protocol.impl.clients;
10
11 import static com.google.common.base.Preconditions.checkArgument;
12
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;
22
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;
36
37 /**
38  * ControllerConnectionTestTool class, utilities for testing device's connect
39  * @author Jozef Bacigal
40  * Date: 4.3.2016.
41  */
42 public class ControllerConnectionTestTool {
43
44     private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(ControllerConnectionTestTool.class);
45
46     public static class Params {
47
48         @Arg(dest = "controller-ip")
49         public String controllerIP;
50
51         @Arg(dest = "devices-count")
52         public int deviceCount;
53
54         @Arg(dest = "ssl")
55         public boolean ssl;
56
57         @Arg(dest = "threads")
58         public int threads;
59
60         @Arg(dest = "port")
61         public int port;
62
63         @Arg(dest = "timeout")
64         public int timeout;
65
66         @Arg(dest = "freeze")
67         public int freeze;
68
69         @Arg(dest = "sleep")
70         public long sleep;
71
72         static ArgumentParser getParser() throws UnknownHostException {
73             final ArgumentParser parser = ArgumentParsers.newArgumentParser("openflowjava test-tool");
74
75             parser.description("Openflowjava switch -> controller connector simulator");
76
77             parser.addArgument("--device-count")
78                     .type(Integer.class)
79                     .setDefault(1)
80                     .help("Number of simulated switches. Has to be more than 0")
81                     .dest("devices-count");
82
83             parser.addArgument("--controller-ip")
84                     .type(String.class)
85                     .setDefault("127.0.0.1")
86                     .help("ODL controller ip address")
87                     .dest("controller-ip");
88
89             parser.addArgument("--ssl")
90                     .type(Boolean.class)
91                     .setDefault(false)
92                     .help("Use secured connection")
93                     .dest("ssl");
94
95             parser.addArgument("--threads")
96                     .type(Integer.class)
97                     .setDefault(1)
98                     .help("Number of threads: MAX 1024")
99                     .dest("threads");
100
101             parser.addArgument("--port")
102                     .type(Integer.class)
103                     .setDefault(6653)
104                     .help("Connection port")
105                     .dest("port");
106
107             parser.addArgument("--timeout")
108                     .type(Integer.class)
109                     .setDefault(60)
110                     .help("Timeout in seconds")
111                     .dest("timeout");
112
113             parser.addArgument("--scenarioTries")
114                     .type(Integer.class)
115                     .setDefault(3)
116                     .help("Number of tries in scenario, while waiting for response")
117                     .dest("freeze");
118
119             parser.addArgument("--timeBetweenScenario")
120                     .type(Long.class)
121                     .setDefault(100)
122                     .help("Waiting time in milliseconds between tries.")
123                     .dest("sleep");
124
125             return parser;
126         }
127
128         void validate() {
129             checkArgument(deviceCount > 0, "Switch count has to be > 0");
130             checkArgument(threads > 0 && threads < 1024, "Switch count has to be > 0 and < 1024");
131         }
132     }
133
134     public static void main(final String[] args) {
135
136         List<Callable<Boolean>> callableList = new ArrayList<>();
137         final EventLoopGroup workerGroup = new NioEventLoopGroup();
138
139         try {
140             final Params params = parseArgs(args, Params.getParser());
141             params.validate();
142
143             for(int loop=0;loop < params.deviceCount; loop++){
144
145                 CallableClient cc = new CallableClient(
146                         params.port,
147                         params.ssl,
148                         InetAddress.getByName(params.controllerIP),
149                         "Switch no." + String.valueOf(loop),
150                         new ScenarioHandler(ScenarioFactory.createHandshakeScenarioWithBarrier(), params.freeze, params.sleep),
151                         new Bootstrap(),
152                         workerGroup);
153
154                 callableList.add(cc);
155
156             }
157
158             ExecutorService executorService = Executors.newFixedThreadPool(params.threads);
159             final ListeningExecutorService listeningExecutorService = MoreExecutors.listeningDecorator(executorService);
160
161             final List<ListenableFuture<Boolean>> listenableFutures = new ArrayList<>();
162             for (Callable<Boolean> booleanCallable : callableList) {
163                listenableFutures.add(listeningExecutorService.submit(booleanCallable));
164             }
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>>() {
168                 @Override
169                 public void onSuccess(@Nullable final List<Boolean> booleanList) {
170                     LOG.info("Tests finished");
171                     workerGroup.shutdownGracefully();
172                     LOG.info("Summary:");
173                     int testsOK = 0;
174                     int testFailure = 0;
175                     for (Boolean aBoolean : booleanList) {
176                         if (aBoolean) {
177                             testsOK++;
178                         } else {
179                             testFailure++;
180                         }
181                     }
182                     LOG.info("Tests OK: {}", testsOK);
183                     LOG.info("Tests failure: {}", testFailure);
184                     System.exit(0);
185                 }
186
187                 @Override
188                 public void onFailure(final Throwable throwable) {
189                     LOG.warn("Tests call failure");
190                     workerGroup.shutdownGracefully();
191                     System.exit(1);
192                 }
193             });
194         } catch (Exception e) {
195             LOG.warn("Exception has been thrown: {}", e);
196             System.exit(1);
197         }
198     }
199
200     private static Params parseArgs(final String[] args, final ArgumentParser parser) throws ArgumentParserException {
201         final Params opt = new Params();
202         parser.parseArgs(args, opt);
203         return opt;
204     }
205
206
207 }