Add some validations and new arguments
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / TesttoolParameters.java
1 /*
2  * Copyright (c) 2015 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 static com.google.common.base.Preconditions.checkArgument;
12
13 import com.google.common.base.Charsets;
14 import com.google.common.base.Preconditions;
15 import com.google.common.io.CharStreams;
16 import com.google.common.io.Files;
17 import java.io.File;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.InputStreamReader;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.concurrent.TimeUnit;
24 import java.util.regex.Pattern;
25 import net.sourceforge.argparse4j.ArgumentParsers;
26 import net.sourceforge.argparse4j.annotation.Arg;
27 import net.sourceforge.argparse4j.inf.ArgumentParser;
28 import net.sourceforge.argparse4j.inf.ArgumentParserException;
29
30 public class TesttoolParameters {
31
32     private static final String HOST_KEY = "{HOST}";
33     private static final String PORT_KEY = "{PORT}";
34     private static final String SSH = "{SSH}";
35     private static final String ADDRESS_PORT = "{ADDRESS:PORT}";
36     private static final String dest = "http://{ADDRESS:PORT}/restconf/config/network-topology:network-topology/topology/topology-netconf/node/{PORT}-sim-device";
37
38     private static final String RESOURCE = "/config-template.xml";
39     private InputStream stream;
40
41     @Arg(dest = "edit-content")
42     public File editContent;
43
44     @Arg(dest = "async")
45     public boolean async;
46
47     @Arg(dest = "thread-amount")
48     public int threadAmount;
49
50     @Arg(dest = "throttle")
51     public int throttle;
52
53     @Arg(dest = "auth")
54     public ArrayList<String> auth;
55
56     @Arg(dest = "controller-destination")
57     public String controllerDestination;
58
59     @Arg(dest = "schemas-dir")
60     public File schemasDir;
61
62     @Arg(dest = "devices-count")
63     public int deviceCount;
64
65     @Arg(dest = "devices-per-port")
66     public int devicesPerPort;
67
68     @Arg(dest = "starting-port")
69     public int startingPort;
70
71     @Arg(dest = "generate-config-connection-timeout")
72     public int generateConfigsTimeout;
73
74     @Arg(dest = "generate-config-address")
75     public String generateConfigsAddress;
76
77     @Arg(dest = "distro-folder")
78     public File distroFolder;
79
80     @Arg(dest = "generate-configs-batch-size")
81     public int generateConfigBatchSize;
82
83     @Arg(dest = "ssh")
84     public boolean ssh;
85
86     @Arg(dest = "exi")
87     public boolean exi;
88
89     @Arg(dest = "debug")
90     public boolean debug;
91
92     @Arg(dest = "notification-file")
93     public File notificationFile;
94
95     @Arg(dest = "md-sal")
96     public boolean mdSal;
97
98     @Arg(dest = "initial-config-xml-file")
99     public File initialConfigXMLFile;
100
101     @Arg(dest = "time-out")
102     public long timeOut;
103
104     @Arg(dest = "ip")
105     public String ip;
106
107     @Arg(dest = "thread-pool-size")
108     public int threadPoolSize;
109
110     static ArgumentParser getParser() {
111         final ArgumentParser parser = ArgumentParsers.newArgumentParser("netconf testtool");
112
113         parser.description("netconf testtool");
114
115         parser.addArgument("--edit-content")
116                 .type(String.class)
117                 .dest("edit-content");
118
119         parser.addArgument("--async-requests")
120                 .type(Boolean.class)
121                 .setDefault(false)
122                 .dest("async");
123
124         parser.addArgument("--thread-amount")
125                 .type(Integer.class)
126                 .setDefault(1)
127                 .dest("thread-amount")
128                 .help("The number of threads to use for configuring devices.");
129
130         parser.addArgument("--throttle")
131                 .type(Integer.class)
132                 .setDefault(5000)
133                 .help("Maximum amount of async requests that can be open at a time, " +
134                         "with mutltiple threads this gets divided among all threads")
135                 .dest("throttle");
136
137         parser.addArgument("--auth")
138                 .nargs(2)
139                 .help("Username and password for HTTP basic authentication in order username password.")
140                 .dest("auth");
141
142         parser.addArgument("--controller-destination")
143                 .type(String.class)
144                 .help("Ip address and port of controller. Must be in following format <ip>:<port> "+
145                       "if available it will be used for spawning netconf connectors via topology configuration as "+
146                       "a part of URI. Example (http://<controller destination>/restconf/config/network-topology:network-topology/topology/topology-netconf/node/<node-id>)"+
147                       "otherwise it will just start simulated devices and skip the execution of PUT requests")
148                 .dest("controller-destination");
149
150         parser.addArgument("--device-count")
151                 .type(Integer.class)
152                 .setDefault(1)
153                 .help("Number of simulated netconf devices to spin. This is the number of actual ports open for the devices.")
154                 .dest("devices-count");
155
156         parser.addArgument("--devices-per-port")
157                 .type(Integer.class)
158                 .setDefault(1)
159                 .help("Amount of config files generated per port to spoof more devices then are actually running")
160                 .dest("devices-per-port");
161
162         parser.addArgument("--schemas-dir")
163                 .type(File.class)
164                 .help("Directory containing yang schemas to describe simulated devices. Some schemas e.g. netconf monitoring and inet types are included by default")
165                 .dest("schemas-dir");
166
167         parser.addArgument("--notification-file")
168                 .type(File.class)
169                 .help("Xml file containing notifications that should be sent to clients after create subscription is called")
170                 .dest("notification-file");
171
172         parser.addArgument("--initial-config-xml-file")
173                 .type(File.class)
174                 .help("Xml file containing initial simulatted configuration to be returned via get-config rpc")
175                 .dest("initial-config-xml-file");
176
177         parser.addArgument("--starting-port")
178                 .type(Integer.class)
179                 .setDefault(17830)
180                 .help("First port for simulated device. Each other device will have previous+1 port number")
181                 .dest("starting-port");
182
183         parser.addArgument("--generate-config-connection-timeout")
184                 .type(Integer.class)
185                 .setDefault((int) TimeUnit.MINUTES.toMillis(30))
186                 .help("Timeout to be generated in initial config files")
187                 .dest("generate-config-connection-timeout");
188
189         parser.addArgument("--generate-config-address")
190                 .type(String.class)
191                 .setDefault("127.0.0.1")
192                 .help("Address to be placed in generated configs")
193                 .dest("generate-config-address");
194
195         parser.addArgument("--generate-configs-batch-size")
196                 .type(Integer.class)
197                 .setDefault(4000)
198                 .help("Number of connector configs per generated file")
199                 .dest("generate-configs-batch-size");
200
201         parser.addArgument("--distribution-folder")
202                 .type(File.class)
203                 .help("Directory where the karaf distribution for controller is located")
204                 .dest("distro-folder");
205
206         parser.addArgument("--ssh")
207                 .type(Boolean.class)
208                 .setDefault(true)
209                 .help("Whether to use ssh for transport or just pure tcp")
210                 .dest("ssh");
211
212         parser.addArgument("--exi")
213                 .type(Boolean.class)
214                 .setDefault(true)
215                 .help("Whether to use exi to transport xml content")
216                 .dest("exi");
217
218         parser.addArgument("--debug")
219                 .type(Boolean.class)
220                 .setDefault(false)
221                 .help("Whether to use debug log level instead of INFO")
222                 .dest("debug");
223
224         parser.addArgument("--md-sal")
225                 .type(Boolean.class)
226                 .setDefault(false)
227                 .help("Whether to use md-sal datastore instead of default simulated datastore.")
228                 .dest("md-sal");
229
230         parser.addArgument("--time-out")
231                 .type(long.class)
232                 .setDefault(20)
233                 .help("the maximum time in seconds for executing each PUT request")
234                 .dest("time-out");
235
236         parser.addArgument("-ip")
237                 .type(String.class)
238                 .setDefault("0.0.0.0")
239                 .help("Ip address which will be used for creating a socket address." +
240                         "It can either be a machine name, such as " +
241                         "java.sun.com, or a textual representation of its IP address.")
242                 .dest("ip");
243
244         parser.addArgument("--thread-pool-size")
245                 .type(Integer.class)
246                 .setDefault(8)
247                 .help("The number of threads to keep in the pool, when creating a device simulator. Even if they are idle.")
248                 .dest("thread-pool-size");
249
250         return parser;
251     }
252
253     public static TesttoolParameters parseArgs(final String[] args, final ArgumentParser parser) {
254         final TesttoolParameters opt = new TesttoolParameters();
255         try {
256             parser.parseArgs(args, opt);
257             return opt;
258         } catch (final ArgumentParserException e) {
259             parser.handleError(e);
260         }
261
262         System.exit(1);
263         return null;
264     }
265
266     void validate() {
267         if (editContent == null) {
268             stream = TesttoolParameters.class.getResourceAsStream(RESOURCE);
269         } else {
270             Preconditions.checkArgument(!editContent.isDirectory(), "Edit content file is a dir");
271             Preconditions.checkArgument(editContent.canRead(), "Edit content file is unreadable");
272         }
273
274         if (controllerDestination != null) {
275             Preconditions.checkArgument(controllerDestination.contains(":"), "Controller Destination needs to be in a following format <ip>:<port>");
276             String[] parts = controllerDestination.split(Pattern.quote(":"));
277             Preconditions.checkArgument(Integer.parseInt(parts[1]) > 0, "Port =< 0");
278         }
279
280         checkArgument(deviceCount > 0, "Device count has to be > 0");
281         checkArgument(startingPort > 1023, "Starting port has to be > 1023");
282         checkArgument(devicesPerPort > 0, "Atleast one device per port needed");
283
284         if (schemasDir != null) {
285             checkArgument(schemasDir.exists(), "Schemas dir has to exist");
286             checkArgument(schemasDir.isDirectory(), "Schemas dir has to be a directory");
287             checkArgument(schemasDir.canRead(), "Schemas dir has to be readable");
288         }
289     }
290
291     public ArrayList<ArrayList<Execution.DestToPayload>> getThreadsPayloads(List<Integer> openDevices) {
292         final String editContentString;
293         try {
294             if(stream == null)
295             {
296                 editContentString = Files.toString(editContent, Charsets.UTF_8);
297             } else {
298                 editContentString = CharStreams.toString(new InputStreamReader(stream, Charsets.UTF_8));
299             }
300         } catch (final IOException e) {
301             throw new IllegalArgumentException("Cannot read content of " + editContent);
302         }
303
304         final ArrayList<ArrayList<Execution.DestToPayload>> allThreadsPayloads = new ArrayList<>();
305         for (int i = 0; i < threadAmount; i++) {
306             final ArrayList<Execution.DestToPayload> payloads = new ArrayList<>();
307             for (int j = 0; j < openDevices.size(); j++) {
308                 final StringBuilder destBuilder = new StringBuilder(dest);
309                 destBuilder.replace(destBuilder.indexOf(ADDRESS_PORT), destBuilder.indexOf(ADDRESS_PORT) + ADDRESS_PORT.length(), controllerDestination)
310                         .replace(destBuilder.indexOf(PORT_KEY), destBuilder.indexOf(PORT_KEY) + PORT_KEY.length(), Integer.toString(openDevices.get(j)));
311                 payloads.add(new Execution.DestToPayload(destBuilder.toString(), prepareMessage(openDevices.get(j), editContentString)));
312             }
313             allThreadsPayloads.add(payloads);
314         }
315
316         return allThreadsPayloads;
317     }
318
319     private String prepareMessage(final int openDevice, final String editContentString) {
320         StringBuilder messageBuilder = new StringBuilder(editContentString);
321
322         if (editContentString.contains(HOST_KEY)) {
323             messageBuilder.replace(messageBuilder.indexOf(HOST_KEY), messageBuilder.indexOf(HOST_KEY) + HOST_KEY.length(), generateConfigsAddress);
324         }
325         if (editContentString.contains(PORT_KEY)) {
326             while (messageBuilder.indexOf(PORT_KEY) != -1)
327                 messageBuilder.replace(messageBuilder.indexOf(PORT_KEY), messageBuilder.indexOf(PORT_KEY) + PORT_KEY.length(), Integer.toString(openDevice));
328         }
329         if (editContentString.contains(SSH)) {
330             messageBuilder.replace(messageBuilder.indexOf(SSH), messageBuilder.indexOf(SSH) + SSH.length(), Boolean.toString(ssh));
331         }
332         return messageBuilder.toString();
333     }
334 }