2 * Copyright (c) 2015 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
9 package org.opendaylight.netconf.test.tool;
11 import static com.google.common.base.Preconditions.checkArgument;
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;
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;
30 public class TesttoolParameters {
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";
38 private static final String RESOURCE = "/config-template.xml";
39 private InputStream stream;
41 @Arg(dest = "edit-content")
42 public File editContent;
47 @Arg(dest = "thread-amount")
48 public int threadAmount;
50 @Arg(dest = "throttle")
54 public ArrayList<String> auth;
56 @Arg(dest = "controller-destination")
57 public String controllerDestination;
59 @Arg(dest = "schemas-dir")
60 public File schemasDir;
62 @Arg(dest = "devices-count")
63 public int deviceCount;
65 @Arg(dest = "devices-per-port")
66 public int devicesPerPort;
68 @Arg(dest = "starting-port")
69 public int startingPort;
71 @Arg(dest = "generate-config-connection-timeout")
72 public int generateConfigsTimeout;
74 @Arg(dest = "generate-config-address")
75 public String generateConfigsAddress;
77 @Arg(dest = "distro-folder")
78 public File distroFolder;
80 @Arg(dest = "generate-configs-batch-size")
81 public int generateConfigBatchSize;
92 @Arg(dest = "notification-file")
93 public File notificationFile;
98 @Arg(dest = "initial-config-xml-file")
99 public File initialConfigXMLFile;
101 @Arg(dest = "time-out")
107 @Arg(dest = "thread-pool-size")
108 public int threadPoolSize;
110 static ArgumentParser getParser() {
111 final ArgumentParser parser = ArgumentParsers.newArgumentParser("netconf testtool");
113 parser.description("netconf testtool");
115 parser.addArgument("--edit-content")
117 .dest("edit-content");
119 parser.addArgument("--async-requests")
124 parser.addArgument("--thread-amount")
127 .dest("thread-amount")
128 .help("The number of threads to use for configuring devices.");
130 parser.addArgument("--throttle")
133 .help("Maximum amount of async requests that can be open at a time, " +
134 "with mutltiple threads this gets divided among all threads")
137 parser.addArgument("--auth")
139 .help("Username and password for HTTP basic authentication in order username password.")
142 parser.addArgument("--controller-destination")
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");
150 parser.addArgument("--device-count")
153 .help("Number of simulated netconf devices to spin. This is the number of actual ports open for the devices.")
154 .dest("devices-count");
156 parser.addArgument("--devices-per-port")
159 .help("Amount of config files generated per port to spoof more devices then are actually running")
160 .dest("devices-per-port");
162 parser.addArgument("--schemas-dir")
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");
167 parser.addArgument("--notification-file")
169 .help("Xml file containing notifications that should be sent to clients after create subscription is called")
170 .dest("notification-file");
172 parser.addArgument("--initial-config-xml-file")
174 .help("Xml file containing initial simulatted configuration to be returned via get-config rpc")
175 .dest("initial-config-xml-file");
177 parser.addArgument("--starting-port")
180 .help("First port for simulated device. Each other device will have previous+1 port number")
181 .dest("starting-port");
183 parser.addArgument("--generate-config-connection-timeout")
185 .setDefault((int) TimeUnit.MINUTES.toMillis(30))
186 .help("Timeout to be generated in initial config files")
187 .dest("generate-config-connection-timeout");
189 parser.addArgument("--generate-config-address")
191 .setDefault("127.0.0.1")
192 .help("Address to be placed in generated configs")
193 .dest("generate-config-address");
195 parser.addArgument("--generate-configs-batch-size")
198 .help("Number of connector configs per generated file")
199 .dest("generate-configs-batch-size");
201 parser.addArgument("--distribution-folder")
203 .help("Directory where the karaf distribution for controller is located")
204 .dest("distro-folder");
206 parser.addArgument("--ssh")
209 .help("Whether to use ssh for transport or just pure tcp")
212 parser.addArgument("--exi")
215 .help("Whether to use exi to transport xml content")
218 parser.addArgument("--debug")
221 .help("Whether to use debug log level instead of INFO")
224 parser.addArgument("--md-sal")
227 .help("Whether to use md-sal datastore instead of default simulated datastore.")
230 parser.addArgument("--time-out")
233 .help("the maximum time in seconds for executing each PUT request")
236 parser.addArgument("-ip")
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.")
244 parser.addArgument("--thread-pool-size")
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");
253 public static TesttoolParameters parseArgs(final String[] args, final ArgumentParser parser) {
254 final TesttoolParameters opt = new TesttoolParameters();
256 parser.parseArgs(args, opt);
258 } catch (final ArgumentParserException e) {
259 parser.handleError(e);
267 if (editContent == null) {
268 stream = TesttoolParameters.class.getResourceAsStream(RESOURCE);
270 Preconditions.checkArgument(!editContent.isDirectory(), "Edit content file is a dir");
271 Preconditions.checkArgument(editContent.canRead(), "Edit content file is unreadable");
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");
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");
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");
291 public ArrayList<ArrayList<Execution.DestToPayload>> getThreadsPayloads(List<Integer> openDevices) {
292 final String editContentString;
296 editContentString = Files.toString(editContent, Charsets.UTF_8);
298 editContentString = CharStreams.toString(new InputStreamReader(stream, Charsets.UTF_8));
300 } catch (final IOException e) {
301 throw new IllegalArgumentException("Cannot read content of " + editContent);
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)));
313 allThreadsPayloads.add(payloads);
316 return allThreadsPayloads;
319 private String prepareMessage(final int openDevice, final String editContentString) {
320 StringBuilder messageBuilder = new StringBuilder(editContentString);
322 if (editContentString.contains(HOST_KEY)) {
323 messageBuilder.replace(messageBuilder.indexOf(HOST_KEY), messageBuilder.indexOf(HOST_KEY) + HOST_KEY.length(), generateConfigsAddress);
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));
329 if (editContentString.contains(SSH)) {
330 messageBuilder.replace(messageBuilder.indexOf(SSH), messageBuilder.indexOf(SSH) + SSH.length(), Boolean.toString(ssh));
332 return messageBuilder.toString();