2 * Copyright (c) 2016 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.Preconditions;
14 import com.google.common.io.CharStreams;
15 import com.google.common.io.Files;
16 import java.io.BufferedReader;
18 import java.io.FileReader;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
22 import java.lang.reflect.Field;
23 import java.nio.charset.StandardCharsets;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.StringJoiner;
29 import java.util.concurrent.TimeUnit;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
32 import net.sourceforge.argparse4j.ArgumentParsers;
33 import net.sourceforge.argparse4j.annotation.Arg;
34 import net.sourceforge.argparse4j.inf.ArgumentParser;
35 import net.sourceforge.argparse4j.inf.ArgumentParserException;
37 public class TesttoolParameters {
39 private static final String HOST_KEY = "{HOST}";
40 private static final String PORT_KEY = "{PORT}";
41 private static final String TCP_ONLY = "{TCP_ONLY}";
42 private static final String ADDRESS_PORT = "{ADDRESS:PORT}";
43 private static final String DEST =
44 "http://{ADDRESS:PORT}/restconf/config/network-topology:network-topology/topology/topology-netconf/";
45 private static final Pattern YANG_FILENAME_PATTERN = Pattern
46 .compile("(?<name>.*)@(?<revision>\\d{4}-\\d{2}-\\d{2})\\.yang");
47 private static final Pattern DATE_PATTERN = Pattern.compile("(\\d{4}-\\d{2}-\\d{2})");
49 private static final String RESOURCE = "/config-template.json";
50 @Arg(dest = "edit-content")
51 public File editContent;
54 @Arg(dest = "thread-amount")
55 public int threadAmount;
56 @Arg(dest = "throttle")
59 public ArrayList<String> auth;
60 @Arg(dest = "controller-destination")
61 public String controllerDestination;
62 @Arg(dest = "schemas-dir")
63 public File schemasDir;
64 @Arg(dest = "devices-count")
65 public int deviceCount;
66 @Arg(dest = "devices-per-port")
67 public int devicesPerPort;
68 @Arg(dest = "starting-port")
69 public int startingPort;
70 @Arg(dest = "generate-config-connection-timeout")
71 public int generateConfigsTimeout;
72 @Arg(dest = "generate-config-address")
73 public String generateConfigsAddress;
74 @Arg(dest = "distro-folder")
75 public File distroFolder;
76 @Arg(dest = "generate-configs-batch-size")
77 public int generateConfigBatchSize;
84 @Arg(dest = "notification-file")
85 public File notificationFile;
88 @Arg(dest = "initial-config-xml-file")
89 public File initialConfigXMLFile;
90 @Arg(dest = "time-out")
92 private InputStream stream;
97 @Arg(dest = "thread-pool-size")
98 public int threadPoolSize;
99 @Arg(dest = "rpc-config")
100 public File rpcConfig;
102 @SuppressWarnings("checkstyle:lineLength")
103 static ArgumentParser getParser() {
104 final ArgumentParser parser = ArgumentParsers.newArgumentParser("netconf testtool");
106 parser.description("netconf testtool");
108 parser.addArgument("--edit-content")
110 .dest("edit-content");
112 parser.addArgument("--async-requests")
117 parser.addArgument("--thread-amount")
120 .dest("thread-amount")
121 .help("The number of threads to use for configuring devices.");
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")
229 parser.addArgument("-ip")
231 .setDefault("0.0.0.0")
232 .help("Ip address which will be used for creating a socket address."
233 + "It can either be a machine name, such as "
234 + "java.sun.com, or a textual representation of its IP address.")
237 parser.addArgument("--thread-pool-size")
240 .help("The number of threads to keep in the pool, when creating a device simulator. Even if they are idle.")
241 .dest("thread-pool-size");
242 parser.addArgument("--rpc-config")
244 .help("Rpc config file. It can be used to define custom rpc behavior, or override the default one."
245 + "Usable for testing buggy device behavior.")
251 public static TesttoolParameters parseArgs(final String[] args, final ArgumentParser parser) {
252 final TesttoolParameters opt = new TesttoolParameters();
254 parser.parseArgs(args, opt);
256 } catch (final ArgumentParserException e) {
257 parser.handleError(e);
264 private static String modifyMessage(final StringBuilder payloadBuilder, final int payloadPosition, final int size) {
266 return payloadBuilder.toString();
269 if (payloadPosition == 0) {
270 payloadBuilder.insert(payloadBuilder.toString().indexOf('{', 2), "[");
271 payloadBuilder.replace(payloadBuilder.length() - 1, payloadBuilder.length(), ",");
272 } else if (payloadPosition + 1 == size) {
273 payloadBuilder.delete(0, payloadBuilder.toString().indexOf(':') + 1);
274 payloadBuilder.insert(payloadBuilder.toString().indexOf('}', 2) + 1, "]");
276 payloadBuilder.delete(0, payloadBuilder.toString().indexOf(':') + 1);
277 payloadBuilder.replace(payloadBuilder.length() - 2, payloadBuilder.length() - 1, ",");
278 payloadBuilder.deleteCharAt(payloadBuilder.toString().lastIndexOf('}'));
280 return payloadBuilder.toString();
283 @SuppressWarnings("checkstyle:regexpSinglelineJava")
285 if (editContent == null) {
286 stream = TesttoolParameters.class.getResourceAsStream(RESOURCE);
288 Preconditions.checkArgument(!editContent.isDirectory(), "Edit content file is a dir");
289 Preconditions.checkArgument(editContent.canRead(), "Edit content file is unreadable");
292 if (controllerDestination != null) {
293 Preconditions.checkArgument(controllerDestination.contains(":"),
294 "Controller Destination needs to be in a following format <ip>:<port>");
295 final String[] parts = controllerDestination.split(Pattern.quote(":"));
296 Preconditions.checkArgument(Integer.parseInt(parts[1]) > 0, "Port =< 0");
299 checkArgument(deviceCount > 0, "Device count has to be > 0");
300 checkArgument(startingPort > 1023, "Starting port has to be > 1023");
301 checkArgument(devicesPerPort > 0, "Atleast one device per port needed");
303 if (schemasDir != null) {
304 checkArgument(schemasDir.exists(), "Schemas dir has to exist");
305 checkArgument(schemasDir.isDirectory(), "Schemas dir has to be a directory");
306 checkArgument(schemasDir.canRead(), "Schemas dir has to be readable");
308 final List<File> files = Arrays.asList(schemasDir.listFiles());
309 for (final File file : files) {
310 final Matcher matcher = YANG_FILENAME_PATTERN.matcher(file.getName());
311 if (!matcher.matches()) {
312 final BufferedReader reader;
314 reader = new BufferedReader(new FileReader(file));
315 String line = reader.readLine();
316 while (!DATE_PATTERN.matcher(line).find()) {
317 line = reader.readLine();
319 final Matcher m = DATE_PATTERN.matcher(line);
322 String moduleName = file.getAbsolutePath();
323 if (file.getName().endsWith(".yang")) {
324 moduleName = moduleName.substring(0, moduleName.length() - 5);
326 final String revision = m.group(1);
327 final String correctName = moduleName + "@" + revision + ".yang";
328 final File correctNameFile = new File(correctName);
329 file.renameTo(correctNameFile);
331 } catch (final IOException e) {
332 // print error to console (test tool is running from console)
338 if (rpcConfig != null) {
339 checkArgument(rpcConfig.exists(), "Rpc config file has to exist");
340 checkArgument(!rpcConfig.isDirectory(), "Rpc config file can't be a directory");
341 checkArgument(rpcConfig.canRead(), "Rpc config file to be readable");
345 public ArrayList<ArrayList<Execution.DestToPayload>> getThreadsPayloads(final List<Integer> openDevices) {
346 final String editContentString;
348 if (stream == null) {
349 editContentString = Files.toString(editContent, StandardCharsets.UTF_8);
351 editContentString = CharStreams.toString(new InputStreamReader(stream, StandardCharsets.UTF_8));
353 } catch (final IOException e) {
354 throw new IllegalArgumentException("Cannot read content of " + editContent);
359 Iterator<Integer> iterator;
361 final ArrayList<ArrayList<Execution.DestToPayload>> allThreadsPayloads = new ArrayList<>();
362 if (generateConfigBatchSize > 1) {
364 final int batchedRequests = openDevices.size() / generateConfigBatchSize;
365 final int batchedRequestsPerThread = batchedRequests / threadAmount;
366 final int leftoverBatchedRequests = (batchedRequests) % threadAmount;
367 final int leftoverRequests = openDevices.size() - (batchedRequests * generateConfigBatchSize);
369 final StringBuilder destBuilder = new StringBuilder(DEST);
370 destBuilder.replace(destBuilder.indexOf(ADDRESS_PORT),
371 destBuilder.indexOf(ADDRESS_PORT) + ADDRESS_PORT.length(),
372 controllerDestination);
374 for (int l = 0; l < threadAmount; l++) {
375 from = l * (batchedRequests * batchedRequestsPerThread);
376 to = from + (batchedRequests * batchedRequestsPerThread);
377 iterator = openDevices.subList(from, to).iterator();
378 allThreadsPayloads.add(createBatchedPayloads(batchedRequestsPerThread, iterator, editContentString,
379 destBuilder.toString()));
381 ArrayList<Execution.DestToPayload> payloads = null;
382 if (leftoverBatchedRequests > 0) {
383 from = threadAmount * (batchedRequests * batchedRequestsPerThread);
384 to = from + (batchedRequests * batchedRequestsPerThread);
385 iterator = openDevices.subList(from, to).iterator();
386 payloads = createBatchedPayloads(leftoverBatchedRequests, iterator, editContentString,
387 destBuilder.toString());
391 for (int j = 0; j < leftoverRequests; j++) {
392 from = openDevices.size() - leftoverRequests;
393 to = openDevices.size();
394 iterator = openDevices.subList(from, to).iterator();
395 final StringBuilder payloadBuilder = new StringBuilder(
396 prepareMessage(iterator.next(), editContentString));
397 payload += modifyMessage(payloadBuilder, j, leftoverRequests);
399 if (leftoverRequests > 0 || leftoverBatchedRequests > 0) {
401 if (payloads != null) {
402 payloads.add(new Execution.DestToPayload(destBuilder.toString(), payload));
404 allThreadsPayloads.add(payloads);
407 final int requestPerThreads = openDevices.size() / threadAmount;
408 final int leftoverRequests = openDevices.size() % threadAmount;
410 for (int i = 0; i < threadAmount; i++) {
411 from = i * requestPerThreads;
412 to = from + requestPerThreads;
413 iterator = openDevices.subList(from, to).iterator();
414 allThreadsPayloads.add(createPayloads(iterator, editContentString));
417 if (leftoverRequests > 0) {
418 from = (threadAmount) * requestPerThreads;
419 to = from + leftoverRequests;
420 iterator = openDevices.subList(from, to).iterator();
421 allThreadsPayloads.add(createPayloads(iterator, editContentString));
424 return allThreadsPayloads;
427 private String prepareMessage(final int openDevice, final String editContentString) {
428 final StringBuilder messageBuilder = new StringBuilder(editContentString);
430 if (editContentString.contains(HOST_KEY)) {
431 messageBuilder.replace(messageBuilder.indexOf(HOST_KEY),
432 messageBuilder.indexOf(HOST_KEY) + HOST_KEY.length(),
433 generateConfigsAddress);
435 if (editContentString.contains(PORT_KEY)) {
436 while (messageBuilder.indexOf(PORT_KEY) != -1) {
437 messageBuilder.replace(messageBuilder.indexOf(PORT_KEY),
438 messageBuilder.indexOf(PORT_KEY) + PORT_KEY.length(),
439 Integer.toString(openDevice));
442 if (editContentString.contains(TCP_ONLY)) {
443 messageBuilder.replace(messageBuilder.indexOf(TCP_ONLY),
444 messageBuilder.indexOf(TCP_ONLY) + TCP_ONLY.length(),
445 Boolean.toString(!ssh));
447 return messageBuilder.toString();
450 private ArrayList<Execution.DestToPayload> createPayloads(final Iterator<Integer> openDevices,
451 final String editContentString) {
452 final ArrayList<Execution.DestToPayload> payloads = new ArrayList<>();
454 while (openDevices.hasNext()) {
455 final StringBuilder destBuilder = new StringBuilder(DEST);
456 destBuilder.replace(destBuilder.indexOf(ADDRESS_PORT),
457 destBuilder.indexOf(ADDRESS_PORT) + ADDRESS_PORT.length(), controllerDestination);
458 payloads.add(new Execution.DestToPayload(
459 destBuilder.toString(), prepareMessage(openDevices.next(), editContentString)));
464 private ArrayList<Execution.DestToPayload> createBatchedPayloads(final int batchedRequestsCount,
465 final Iterator<Integer> openDevices, final String editContentString, final String destination) {
466 final ArrayList<Execution.DestToPayload> payloads = new ArrayList<>();
468 for (int i = 0; i < batchedRequestsCount; i++) {
470 for (int j = 0; j < generateConfigBatchSize; j++) {
471 final StringBuilder payloadBuilder = new StringBuilder(
472 prepareMessage(openDevices.next(), editContentString));
473 payload += modifyMessage(payloadBuilder, j, generateConfigBatchSize);
475 payloads.add(new Execution.DestToPayload(destination, payload));
481 public String toString() {
482 final List<Field> fields = Arrays.asList(this.getClass().getDeclaredFields());
483 final StringJoiner joiner = new StringJoiner(", \n", "TesttoolParameters{", "}\n");
485 .filter(field -> field.getAnnotation(Arg.class) != null)
486 .map(this::getFieldString)
487 .forEach(joiner::add);
488 return joiner.toString();
491 private String getFieldString(final Field field) {
493 return field.getName() + "='" + field.get(this) + "'";
494 } catch (final IllegalAccessException e) {
495 return field.getName() + "= UNKNOWN";