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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
17 import java.io.BufferedReader;
19 import java.io.FileReader;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.InputStreamReader;
23 import java.lang.reflect.Field;
24 import java.nio.charset.StandardCharsets;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.Objects;
31 import java.util.StringJoiner;
32 import java.util.concurrent.TimeUnit;
33 import java.util.regex.Matcher;
34 import java.util.regex.Pattern;
35 import net.sourceforge.argparse4j.ArgumentParsers;
36 import net.sourceforge.argparse4j.annotation.Arg;
37 import net.sourceforge.argparse4j.inf.ArgumentParser;
38 import net.sourceforge.argparse4j.inf.ArgumentParserException;
40 @SuppressFBWarnings({"DM_EXIT", "DM_DEFAULT_ENCODING"})
41 public class TesttoolParameters {
43 private static final String HOST_KEY = "{HOST}";
44 private static final String PORT_KEY = "{PORT}";
45 private static final String TCP_ONLY = "{TCP_ONLY}";
46 private static final String ADDRESS_PORT = "{ADDRESS:PORT}";
47 private static final String DEST =
48 "http://{ADDRESS:PORT}/restconf/config/network-topology:network-topology/topology/topology-netconf/";
49 private static final Pattern YANG_FILENAME_PATTERN = Pattern
50 .compile("(?<name>.*)@(?<revision>\\d{4}-\\d{2}-\\d{2})\\.yang");
51 private static final Pattern REVISION_DATE_PATTERN = Pattern.compile("revision\\s+\"?(\\d{4}-\\d{2}-\\d{2})\"?");
53 private static final String RESOURCE = "/config-template.json";
54 @Arg(dest = "edit-content")
55 public File editContent;
58 @Arg(dest = "thread-amount")
59 public int threadAmount;
60 @Arg(dest = "throttle")
63 public ArrayList<String> auth;
64 @Arg(dest = "controller-destination")
65 public String controllerDestination;
66 @Arg(dest = "schemas-dir")
67 public File schemasDir;
68 @Arg(dest = "devices-count")
69 public int deviceCount;
70 @Arg(dest = "devices-per-port")
71 public int devicesPerPort;
72 @Arg(dest = "starting-port")
73 public int startingPort;
74 @Arg(dest = "generate-config-connection-timeout")
75 public int generateConfigsTimeout;
76 @Arg(dest = "generate-config-address")
77 public String generateConfigsAddress;
78 @Arg(dest = "distro-folder")
79 public File distroFolder;
80 @Arg(dest = "generate-configs-batch-size")
81 public int generateConfigBatchSize;
85 public boolean exi = true;
88 @Arg(dest = "notification-file")
89 public File notificationFile;
92 @Arg(dest = "initial-config-xml-file")
93 public File initialConfigXMLFile;
94 @Arg(dest = "time-out")
96 private InputStream stream;
99 @Arg(dest = "thread-pool-size")
100 public int threadPoolSize;
101 @Arg(dest = "rpc-config")
102 public File rpcConfig;
104 @SuppressWarnings("checkstyle:lineLength")
105 static ArgumentParser getParser() {
106 final ArgumentParser parser = ArgumentParsers.newArgumentParser("netconf testtool");
108 parser.description("netconf testtool");
110 parser.addArgument("--edit-content")
112 .dest("edit-content");
114 parser.addArgument("--async-requests")
116 .setDefault(Boolean.FALSE)
119 parser.addArgument("--thread-amount")
122 .dest("thread-amount")
123 .help("The number of threads to use for configuring devices.");
125 parser.addArgument("--throttle")
128 .help("Maximum amount of async requests that can be open at a time, "
129 + "with mutltiple threads this gets divided among all threads")
132 parser.addArgument("--auth")
134 .help("Username and password for HTTP basic authentication in order username password.")
137 parser.addArgument("--controller-destination")
139 .help("Ip address and port of controller. Must be in following format <ip>:<port> "
140 + "if available it will be used for spawning netconf connectors via topology configuration as "
141 + "a part of URI. Example (http://<controller destination>/restconf/config/network-topology:network-topology/topology/topology-netconf/node/<node-id>)"
142 + "otherwise it will just start simulated devices and skip the execution of PUT requests")
143 .dest("controller-destination");
145 parser.addArgument("--device-count")
148 .help("Number of simulated netconf devices to spin. This is the number of actual ports open for the devices.")
149 .dest("devices-count");
151 parser.addArgument("--devices-per-port")
154 .help("Amount of config files generated per port to spoof more devices then are actually running")
155 .dest("devices-per-port");
157 parser.addArgument("--schemas-dir")
159 .help("Directory containing yang schemas to describe simulated devices. Some schemas e.g. netconf monitoring and inet types are included by default")
160 .dest("schemas-dir");
162 parser.addArgument("--notification-file")
164 .help("Xml file containing notifications that should be sent to clients after create subscription is called")
165 .dest("notification-file");
167 parser.addArgument("--initial-config-xml-file")
169 .help("Xml file containing initial simulatted configuration to be returned via get-config rpc")
170 .dest("initial-config-xml-file");
172 parser.addArgument("--starting-port")
175 .help("First port for simulated device. Each other device will have previous+1 port number")
176 .dest("starting-port");
178 parser.addArgument("--generate-config-connection-timeout")
180 .setDefault((int) TimeUnit.MINUTES.toMillis(30))
181 .help("Timeout to be generated in initial config files")
182 .dest("generate-config-connection-timeout");
184 parser.addArgument("--generate-config-address")
186 .setDefault("127.0.0.1")
187 .help("Address to be placed in generated configs")
188 .dest("generate-config-address");
190 parser.addArgument("--generate-configs-batch-size")
193 .help("Number of connector configs per generated file")
194 .dest("generate-configs-batch-size");
196 parser.addArgument("--distribution-folder")
198 .help("Directory where the karaf distribution for controller is located")
199 .dest("distro-folder");
201 parser.addArgument("--ssh")
203 .setDefault(Boolean.TRUE)
204 .help("Whether to use ssh for transport or just pure tcp")
207 parser.addArgument("--exi")
209 .setDefault(Boolean.TRUE)
210 .help("Whether to use exi to transport xml content")
213 parser.addArgument("--debug")
215 .setDefault(Boolean.FALSE)
216 .help("Whether to use debug log level instead of INFO")
219 parser.addArgument("--md-sal")
221 .setDefault(Boolean.FALSE)
222 .help("Whether to use md-sal datastore instead of default simulated datastore.")
225 parser.addArgument("--time-out")
228 .help("the maximum time in seconds for executing each PUT request")
231 parser.addArgument("-ip")
233 .setDefault("0.0.0.0")
234 .help("Ip address which will be used for creating a socket address."
235 + "It can either be a machine name, such as "
236 + "java.sun.com, or a textual representation of its IP address.")
239 parser.addArgument("--thread-pool-size")
242 .help("The number of threads to keep in the pool, when creating a device simulator. Even if they are idle.")
243 .dest("thread-pool-size");
244 parser.addArgument("--rpc-config")
246 .help("Rpc config file. It can be used to define custom rpc behavior, or override the default one."
247 + "Usable for testing buggy device behavior.")
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);
266 private static String modifyMessage(final StringBuilder payloadBuilder, final int payloadPosition, final int size) {
268 return payloadBuilder.toString();
271 if (payloadPosition == 0) {
272 payloadBuilder.insert(payloadBuilder.toString().indexOf('{', 2), "[");
273 payloadBuilder.replace(payloadBuilder.length() - 1, payloadBuilder.length(), ",");
274 } else if (payloadPosition + 1 == size) {
275 payloadBuilder.delete(0, payloadBuilder.toString().indexOf(':') + 1);
276 payloadBuilder.insert(payloadBuilder.toString().indexOf('}', 2) + 1, "]");
278 payloadBuilder.delete(0, payloadBuilder.toString().indexOf(':') + 1);
279 payloadBuilder.replace(payloadBuilder.length() - 2, payloadBuilder.length() - 1, ",");
280 payloadBuilder.deleteCharAt(payloadBuilder.toString().lastIndexOf('}'));
282 return payloadBuilder.toString();
285 @SuppressWarnings("checkstyle:regexpSinglelineJava")
287 if (editContent == null) {
288 stream = TesttoolParameters.class.getResourceAsStream(RESOURCE);
290 Preconditions.checkArgument(!editContent.isDirectory(), "Edit content file is a dir");
291 Preconditions.checkArgument(editContent.canRead(), "Edit content file is unreadable");
294 if (controllerDestination != null) {
295 Preconditions.checkArgument(controllerDestination.contains(":"),
296 "Controller Destination needs to be in a following format <ip>:<port>");
297 final String[] parts = controllerDestination.split(Pattern.quote(":"));
298 Preconditions.checkArgument(Integer.parseInt(parts[1]) > 0, "Port =< 0");
301 checkArgument(deviceCount > 0, "Device count has to be > 0");
302 checkArgument(startingPort > 1023, "Starting port has to be > 1023");
303 checkArgument(devicesPerPort > 0, "Atleast one device per port needed");
305 if (schemasDir != null) {
306 checkArgument(schemasDir.exists(), "Schemas dir has to exist");
307 checkArgument(schemasDir.isDirectory(), "Schemas dir has to be a directory");
308 checkArgument(schemasDir.canRead(), "Schemas dir has to be readable");
310 final File[] filesArray = schemasDir.listFiles();
311 final List<File> files = filesArray != null ? Arrays.asList(filesArray) : Collections.emptyList();
312 for (final File file : files) {
313 final Matcher matcher = YANG_FILENAME_PATTERN.matcher(file.getName());
314 if (!matcher.matches()) {
315 try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
316 String line = reader.readLine();
317 while (Objects.nonNull(line) && !REVISION_DATE_PATTERN.matcher(line).find()) {
318 line = reader.readLine();
320 if (Objects.nonNull(line)) {
321 final Matcher m = REVISION_DATE_PATTERN.matcher(line);
322 Preconditions.checkState(m.find());
323 String moduleName = file.getAbsolutePath();
324 if (file.getName().endsWith(".yang")) {
325 moduleName = moduleName.substring(0, moduleName.length() - 5);
327 final String revision = m.group(1);
328 final String correctName = moduleName + "@" + revision + ".yang";
329 final File correctNameFile = new File(correctName);
330 if (!file.renameTo(correctNameFile)) {
331 throw new IllegalStateException("Failed to rename '%s'." + file);
334 } catch (final IOException e) {
335 // print error to console (test tool is running from console)
341 if (rpcConfig != null) {
342 checkArgument(rpcConfig.exists(), "Rpc config file has to exist");
343 checkArgument(!rpcConfig.isDirectory(), "Rpc config file can't be a directory");
344 checkArgument(rpcConfig.canRead(), "Rpc config file to be readable");
348 public ArrayList<ArrayList<Execution.DestToPayload>> getThreadsPayloads(final List<Integer> openDevices) {
349 final String editContentString;
351 if (stream == null) {
352 editContentString = Files.asCharSource(editContent, StandardCharsets.UTF_8).read();
354 editContentString = CharStreams.toString(new InputStreamReader(stream, StandardCharsets.UTF_8));
356 } catch (final IOException e) {
357 throw new IllegalArgumentException("Cannot read content of " + editContent, e);
362 Iterator<Integer> iterator;
364 final ArrayList<ArrayList<Execution.DestToPayload>> allThreadsPayloads = new ArrayList<>();
365 if (generateConfigBatchSize > 1) {
367 final int batchedRequests = openDevices.size() / generateConfigBatchSize;
368 final int batchedRequestsPerThread = batchedRequests / threadAmount;
369 final int leftoverBatchedRequests = batchedRequests % threadAmount;
370 final int leftoverRequests = openDevices.size() - batchedRequests * generateConfigBatchSize;
372 final StringBuilder destBuilder = new StringBuilder(DEST);
373 destBuilder.replace(destBuilder.indexOf(ADDRESS_PORT),
374 destBuilder.indexOf(ADDRESS_PORT) + ADDRESS_PORT.length(),
375 controllerDestination);
377 for (int l = 0; l < threadAmount; l++) {
378 from = l * batchedRequests * batchedRequestsPerThread;
379 to = from + batchedRequests * batchedRequestsPerThread;
380 iterator = openDevices.subList(from, to).iterator();
381 allThreadsPayloads.add(createBatchedPayloads(batchedRequestsPerThread, iterator, editContentString,
382 destBuilder.toString()));
384 ArrayList<Execution.DestToPayload> payloads = null;
385 if (leftoverBatchedRequests > 0) {
386 from = threadAmount * batchedRequests * batchedRequestsPerThread;
387 to = from + batchedRequests * batchedRequestsPerThread;
388 iterator = openDevices.subList(from, to).iterator();
389 payloads = createBatchedPayloads(leftoverBatchedRequests, iterator, editContentString,
390 destBuilder.toString());
394 for (int j = 0; j < leftoverRequests; j++) {
395 from = openDevices.size() - leftoverRequests;
396 to = openDevices.size();
397 iterator = openDevices.subList(from, to).iterator();
398 final StringBuilder payloadBuilder = new StringBuilder(
399 prepareMessage(iterator.next(), editContentString));
400 payload += modifyMessage(payloadBuilder, j, leftoverRequests);
402 if (leftoverRequests > 0 || leftoverBatchedRequests > 0) {
404 if (payloads != null) {
405 payloads.add(new Execution.DestToPayload(destBuilder.toString(), payload));
407 allThreadsPayloads.add(payloads);
410 final int requestPerThreads = openDevices.size() / threadAmount;
411 final int leftoverRequests = openDevices.size() % threadAmount;
413 for (int i = 0; i < threadAmount; i++) {
414 from = i * requestPerThreads;
415 to = from + requestPerThreads;
416 iterator = openDevices.subList(from, to).iterator();
417 allThreadsPayloads.add(createPayloads(iterator, editContentString));
420 if (leftoverRequests > 0) {
421 from = threadAmount * requestPerThreads;
422 to = from + leftoverRequests;
423 iterator = openDevices.subList(from, to).iterator();
424 allThreadsPayloads.add(createPayloads(iterator, editContentString));
427 return allThreadsPayloads;
430 private String prepareMessage(final int openDevice, final String editContentString) {
431 final StringBuilder messageBuilder = new StringBuilder(editContentString);
433 if (editContentString.contains(HOST_KEY)) {
434 messageBuilder.replace(messageBuilder.indexOf(HOST_KEY),
435 messageBuilder.indexOf(HOST_KEY) + HOST_KEY.length(),
436 generateConfigsAddress);
438 if (editContentString.contains(PORT_KEY)) {
439 while (messageBuilder.indexOf(PORT_KEY) != -1) {
440 messageBuilder.replace(messageBuilder.indexOf(PORT_KEY),
441 messageBuilder.indexOf(PORT_KEY) + PORT_KEY.length(),
442 Integer.toString(openDevice));
445 if (editContentString.contains(TCP_ONLY)) {
446 messageBuilder.replace(messageBuilder.indexOf(TCP_ONLY),
447 messageBuilder.indexOf(TCP_ONLY) + TCP_ONLY.length(),
448 Boolean.toString(!ssh));
450 return messageBuilder.toString();
453 private ArrayList<Execution.DestToPayload> createPayloads(final Iterator<Integer> openDevices,
454 final String editContentString) {
455 final ArrayList<Execution.DestToPayload> payloads = new ArrayList<>();
457 while (openDevices.hasNext()) {
458 final StringBuilder destBuilder = new StringBuilder(DEST);
459 destBuilder.replace(destBuilder.indexOf(ADDRESS_PORT),
460 destBuilder.indexOf(ADDRESS_PORT) + ADDRESS_PORT.length(), controllerDestination);
461 payloads.add(new Execution.DestToPayload(
462 destBuilder.toString(), prepareMessage(openDevices.next(), editContentString)));
467 private ArrayList<Execution.DestToPayload> createBatchedPayloads(final int batchedRequestsCount,
468 final Iterator<Integer> openDevices, final String editContentString, final String destination) {
469 final ArrayList<Execution.DestToPayload> payloads = new ArrayList<>();
471 for (int i = 0; i < batchedRequestsCount; i++) {
472 StringBuilder payload = new StringBuilder();
473 for (int j = 0; j < generateConfigBatchSize; j++) {
474 final StringBuilder payloadBuilder = new StringBuilder(
475 prepareMessage(openDevices.next(), editContentString));
476 payload.append(modifyMessage(payloadBuilder, j, generateConfigBatchSize));
478 payloads.add(new Execution.DestToPayload(destination, payload.toString()));
484 public String toString() {
485 final List<Field> fields = Arrays.asList(this.getClass().getDeclaredFields());
486 final StringJoiner joiner = new StringJoiner(", \n", "TesttoolParameters{", "}\n");
488 .filter(field -> field.getAnnotation(Arg.class) != null)
489 .map(this::getFieldString)
490 .forEach(joiner::add);
491 return joiner.toString();
494 private String getFieldString(final Field field) {
496 return field.getName() + "='" + field.get(this) + "'";
497 } catch (final IllegalAccessException e) {
498 return field.getName() + "= UNKNOWN";