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")
104 static ArgumentParser getParser() {
105 final ArgumentParser parser = ArgumentParsers.newArgumentParser("netconf testtool");
107 parser.description("netconf testtool");
109 parser.addArgument("--edit-content")
111 .dest("edit-content");
113 parser.addArgument("--async-requests")
118 parser.addArgument("--thread-amount")
121 .dest("thread-amount");
123 parser.addArgument("--throttle")
126 .help("Maximum amount of async requests that can be open at a time, " +
127 "with mutltiple threads this gets divided among all threads")
130 parser.addArgument("--auth")
132 .help("Username and password for HTTP basic authentication in order username password.")
135 parser.addArgument("--controller-destination")
137 .help("Ip address and port of controller. Must be in following format <ip>:<port> "+
138 "if available it will be used for spawning netconf connectors via topology configuration as "+
139 "a part of URI. Example (http://<controller destination>/restconf/config/network-topology:network-topology/topology/topology-netconf/node/<node-id>)"+
140 "otherwise it will just start simulated devices and skip the execution of PUT requests")
141 .dest("controller-destination");
143 parser.addArgument("--device-count")
146 .help("Number of simulated netconf devices to spin. This is the number of actual ports open for the devices.")
147 .dest("devices-count");
149 parser.addArgument("--devices-per-port")
152 .help("Amount of config files generated per port to spoof more devices then are actually running")
153 .dest("devices-per-port");
155 parser.addArgument("--schemas-dir")
157 .help("Directory containing yang schemas to describe simulated devices. Some schemas e.g. netconf monitoring and inet types are included by default")
158 .dest("schemas-dir");
160 parser.addArgument("--notification-file")
162 .help("Xml file containing notifications that should be sent to clients after create subscription is called")
163 .dest("notification-file");
165 parser.addArgument("--initial-config-xml-file")
167 .help("Xml file containing initial simulatted configuration to be returned via get-config rpc")
168 .dest("initial-config-xml-file");
170 parser.addArgument("--starting-port")
173 .help("First port for simulated device. Each other device will have previous+1 port number")
174 .dest("starting-port");
176 parser.addArgument("--generate-config-connection-timeout")
178 .setDefault((int) TimeUnit.MINUTES.toMillis(30))
179 .help("Timeout to be generated in initial config files")
180 .dest("generate-config-connection-timeout");
182 parser.addArgument("--generate-config-address")
184 .setDefault("127.0.0.1")
185 .help("Address to be placed in generated configs")
186 .dest("generate-config-address");
188 parser.addArgument("--generate-configs-batch-size")
191 .help("Number of connector configs per generated file")
192 .dest("generate-configs-batch-size");
194 parser.addArgument("--distribution-folder")
196 .help("Directory where the karaf distribution for controller is located")
197 .dest("distro-folder");
199 parser.addArgument("--ssh")
202 .help("Whether to use ssh for transport or just pure tcp")
205 parser.addArgument("--exi")
208 .help("Whether to use exi to transport xml content")
211 parser.addArgument("--debug")
214 .help("Whether to use debug log level instead of INFO")
217 parser.addArgument("--md-sal")
220 .help("Whether to use md-sal datastore instead of default simulated datastore.")
223 parser.addArgument("--time-out")
226 .help("the maximum time in seconds for executing each PUT request")
232 public static TesttoolParameters parseArgs(final String[] args, final ArgumentParser parser) {
233 final TesttoolParameters opt = new TesttoolParameters();
235 parser.parseArgs(args, opt);
237 } catch (final ArgumentParserException e) {
238 parser.handleError(e);
246 if (editContent == null) {
247 stream = TesttoolParameters.class.getResourceAsStream(RESOURCE);
249 Preconditions.checkArgument(!editContent.isDirectory(), "Edit content file is a dir");
250 Preconditions.checkArgument(editContent.canRead(), "Edit content file is unreadable");
253 if (controllerDestination != null) {
254 Preconditions.checkArgument(controllerDestination.contains(":"), "Controller Destination needs to be in a following format <ip>:<port>");
255 String[] parts = controllerDestination.split(Pattern.quote(":"));
256 Preconditions.checkArgument(Integer.parseInt(parts[1]) > 0, "Port =< 0");
259 checkArgument(deviceCount > 0, "Device count has to be > 0");
260 checkArgument(startingPort > 1023, "Starting port has to be > 1023");
261 checkArgument(devicesPerPort > 0, "Atleast one device per port needed");
263 if (schemasDir != null) {
264 checkArgument(schemasDir.exists(), "Schemas dir has to exist");
265 checkArgument(schemasDir.isDirectory(), "Schemas dir has to be a directory");
266 checkArgument(schemasDir.canRead(), "Schemas dir has to be readable");
270 public ArrayList<ArrayList<Execution.DestToPayload>> getThreadsPayloads(List<Integer> openDevices) {
271 final String editContentString;
275 editContentString = Files.toString(editContent, Charsets.UTF_8);
277 editContentString = CharStreams.toString(new InputStreamReader(stream, Charsets.UTF_8));
279 } catch (final IOException e) {
280 throw new IllegalArgumentException("Cannot read content of " + editContent);
283 final ArrayList<ArrayList<Execution.DestToPayload>> allThreadsPayloads = new ArrayList<>();
284 for (int i = 0; i < threadAmount; i++) {
285 final ArrayList<Execution.DestToPayload> payloads = new ArrayList<>();
286 for (int j = 0; j < openDevices.size(); j++) {
287 final StringBuilder destBuilder = new StringBuilder(dest);
288 destBuilder.replace(destBuilder.indexOf(ADDRESS_PORT), destBuilder.indexOf(ADDRESS_PORT) + ADDRESS_PORT.length(), controllerDestination)
289 .replace(destBuilder.indexOf(PORT_KEY), destBuilder.indexOf(PORT_KEY) + PORT_KEY.length(), Integer.toString(openDevices.get(j)));
290 payloads.add(new Execution.DestToPayload(destBuilder.toString(), prepareMessage(openDevices.get(j), editContentString)));
292 allThreadsPayloads.add(payloads);
295 return allThreadsPayloads;
298 private String prepareMessage(final int openDevice, final String editContentString) {
299 StringBuilder messageBuilder = new StringBuilder(editContentString);
301 if (editContentString.contains(HOST_KEY)) {
302 messageBuilder.replace(messageBuilder.indexOf(HOST_KEY), messageBuilder.indexOf(HOST_KEY) + HOST_KEY.length(), generateConfigsAddress);
304 if (editContentString.contains(PORT_KEY)) {
305 while (messageBuilder.indexOf(PORT_KEY) != -1)
306 messageBuilder.replace(messageBuilder.indexOf(PORT_KEY), messageBuilder.indexOf(PORT_KEY) + PORT_KEY.length(), Integer.toString(openDevice));
308 if (editContentString.contains(SSH)) {
309 messageBuilder.replace(messageBuilder.indexOf(SSH), messageBuilder.indexOf(SSH) + SSH.length(), Boolean.toString(ssh));
311 return messageBuilder.toString();