* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.netconf.test.tool;
import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkState;
-import com.google.common.base.Preconditions;
-import com.google.common.io.CharStreams;
-import com.google.common.io.Files;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
-import java.util.ArrayList;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.Collections;
-import java.util.Iterator;
import java.util.List;
import java.util.StringJoiner;
import java.util.concurrent.TimeUnit;
import net.sourceforge.argparse4j.annotation.Arg;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
+import org.opendaylight.yangtools.yang.common.YangConstants;
-@SuppressFBWarnings({"DM_EXIT", "DM_DEFAULT_ENCODING"})
-public class TesttoolParameters {
-
- private static final String HOST_KEY = "{HOST}";
- private static final String PORT_KEY = "{PORT}";
- private static final String TCP_ONLY = "{TCP_ONLY}";
- private static final String ADDRESS_PORT = "{ADDRESS:PORT}";
- private static final String DEST =
- "http://{ADDRESS:PORT}/restconf/config/network-topology:network-topology/topology/topology-netconf/";
+@SuppressFBWarnings({"DM_EXIT"})
+public final class TesttoolParameters {
private static final Pattern YANG_FILENAME_PATTERN = Pattern
- .compile("(?<name>.*)@(?<revision>\\d{4}-\\d{2}-\\d{2})\\.yang");
- private static final Pattern DATE_PATTERN = Pattern.compile("(\\d{4}-\\d{2}-\\d{2})");
+ .compile("(?<name>.*)@(?<revision>\\d{4}-\\d{2}-\\d{2})\\.yang");
+ private static final Pattern REVISION_DATE_PATTERN = Pattern.compile("revision\\s+\"?(\\d{4}-\\d{2}-\\d{2})\"?");
- private static final String RESOURCE = "/config-template.json";
- @Arg(dest = "edit-content")
- public File editContent;
@Arg(dest = "async")
public boolean async;
@Arg(dest = "thread-amount")
public int threadAmount;
@Arg(dest = "throttle")
public int throttle;
- @Arg(dest = "auth")
- public ArrayList<String> auth;
- @Arg(dest = "controller-destination")
- public String controllerDestination;
+ @Arg(dest = "controller-auth-username")
+ public String controllerAuthUsername;
+ @Arg(dest = "controller-auth-password")
+ public String controllerAuthPassword;
+ @Arg(dest = "controller-ip")
+ public String controllerIp;
+ @Arg(dest = "controller-port")
+ public Integer controllerPort;
@Arg(dest = "schemas-dir")
public File schemasDir;
@Arg(dest = "devices-count")
@Arg(dest = "ssh")
public boolean ssh;
@Arg(dest = "exi")
- public boolean exi;
+ public boolean exi = true;
@Arg(dest = "debug")
public boolean debug;
@Arg(dest = "notification-file")
public File initialConfigXMLFile;
@Arg(dest = "time-out")
public long timeOut;
- private InputStream stream;
@Arg(dest = "ip")
public String ip;
@Arg(dest = "thread-pool-size")
parser.addArgument("--async-requests")
.type(Boolean.class)
- .setDefault(false)
+ .setDefault(Boolean.FALSE)
.dest("async");
parser.addArgument("--thread-amount")
+ "with mutltiple threads this gets divided among all threads")
.dest("throttle");
- parser.addArgument("--auth")
- .nargs(2)
- .help("Username and password for HTTP basic authentication in order username password.")
- .dest("auth");
+ parser.addArgument("--controller-auth-username")
+ .type(String.class)
+ .setDefault("admin")
+ .help("Username for HTTP basic authentication to destination controller.")
+ .dest("controller-auth-username");
+
+ parser.addArgument("--controller-auth-password")
+ .type(String.class)
+ .setDefault("admin")
+ .help("Password for HTTP basic authentication to destination controller.")
+ .dest("controller-auth-password");
- parser.addArgument("--controller-destination")
+ parser.addArgument("--controller-ip")
.type(String.class)
- .help("Ip address and port of controller. Must be in following format <ip>:<port> "
- + "if available it will be used for spawning netconf connectors via topology configuration as "
- + "a part of URI. Example (http://<controller destination>/restconf/config/network-topology:network-topology/topology/topology-netconf/node/<node-id>)"
- + "otherwise it will just start simulated devices and skip the execution of PUT requests")
- .dest("controller-destination");
+ .help("Ip of controller if available it will be used for spawning netconf connectors via topology"
+ + " configuration as a part of"
+ + " URI(http://<controller-ip>:<controller-port>/restconf/config/...)"
+ + " otherwise it will just start simulated devices and skip the execution of PATCH requests")
+ .dest("controller-ip");
+
+ parser.addArgument("--controller-port")
+ .type(Integer.class)
+ .help("Port of controller if available it will be used for spawning netconf connectors via topology "
+ + "configuration as a part of"
+ + " URI(http://<controller-ip>:<controller-port>/restconf/config/...) "
+ + "otherwise it will just start simulated devices and skip the execution of PATCH requests")
+ .dest("controller-port");
parser.addArgument("--device-count")
.type(Integer.class)
parser.addArgument("--devices-per-port")
.type(Integer.class)
.setDefault(1)
- .help("Amount of config files generated per port to spoof more devices then are actually running")
+ .help("Amount of config files generated per port to spoof more devices than are actually running")
.dest("devices-per-port");
parser.addArgument("--schemas-dir")
parser.addArgument("--ssh")
.type(Boolean.class)
- .setDefault(true)
+ .setDefault(Boolean.TRUE)
.help("Whether to use ssh for transport or just pure tcp")
.dest("ssh");
parser.addArgument("--exi")
.type(Boolean.class)
- .setDefault(true)
+ .setDefault(Boolean.TRUE)
.help("Whether to use exi to transport xml content")
.dest("exi");
parser.addArgument("--debug")
.type(Boolean.class)
- .setDefault(false)
+ .setDefault(Boolean.FALSE)
.help("Whether to use debug log level instead of INFO")
.dest("debug");
parser.addArgument("--md-sal")
.type(Boolean.class)
- .setDefault(false)
+ .setDefault(Boolean.FALSE)
.help("Whether to use md-sal datastore instead of default simulated datastore.")
.dest("md-sal");
parser.addArgument("--time-out")
.type(long.class)
.setDefault(20)
- .help("the maximum time in seconds for executing each PUT request")
+ .help("the maximum time in seconds for executing each PATCH request")
.dest("time-out");
parser.addArgument("-ip")
return parser;
}
- public static TesttoolParameters parseArgs(final String[] args, final ArgumentParser parser) {
+ static TesttoolParameters parseArgs(final String[] args, final ArgumentParser parser) {
final TesttoolParameters opt = new TesttoolParameters();
try {
parser.parseArgs(args, opt);
return null;
}
- private static String modifyMessage(final StringBuilder payloadBuilder, final int payloadPosition, final int size) {
- if (size == 1) {
- return payloadBuilder.toString();
- }
-
- if (payloadPosition == 0) {
- payloadBuilder.insert(payloadBuilder.toString().indexOf('{', 2), "[");
- payloadBuilder.replace(payloadBuilder.length() - 1, payloadBuilder.length(), ",");
- } else if (payloadPosition + 1 == size) {
- payloadBuilder.delete(0, payloadBuilder.toString().indexOf(':') + 1);
- payloadBuilder.insert(payloadBuilder.toString().indexOf('}', 2) + 1, "]");
- } else {
- payloadBuilder.delete(0, payloadBuilder.toString().indexOf(':') + 1);
- payloadBuilder.replace(payloadBuilder.length() - 2, payloadBuilder.length() - 1, ",");
- payloadBuilder.deleteCharAt(payloadBuilder.toString().lastIndexOf('}'));
- }
- return payloadBuilder.toString();
- }
-
@SuppressWarnings("checkstyle:regexpSinglelineJava")
void validate() {
- if (editContent == null) {
- stream = TesttoolParameters.class.getResourceAsStream(RESOURCE);
+ if (controllerIp != null) {
+ //FIXME Ip validation
+ checkArgument(controllerPort != null, "Controller port is missing");
+ //FIXME Is there specific bound
+ checkArgument(controllerPort >= 0, "Controller port should be non-negative integer");
+ checkArgument(controllerPort < 65354, "Controller port should be less than 65354");
} else {
- Preconditions.checkArgument(!editContent.isDirectory(), "Edit content file is a dir");
- Preconditions.checkArgument(editContent.canRead(), "Edit content file is unreadable");
- }
-
- if (controllerDestination != null) {
- Preconditions.checkArgument(controllerDestination.contains(":"),
- "Controller Destination needs to be in a following format <ip>:<port>");
- final String[] parts = controllerDestination.split(Pattern.quote(":"));
- Preconditions.checkArgument(Integer.parseInt(parts[1]) > 0, "Port =< 0");
+ checkArgument(controllerPort == null, "Controller ip is missing");
}
checkArgument(deviceCount > 0, "Device count has to be > 0");
checkArgument(startingPort > 1023, "Starting port has to be > 1023");
- checkArgument(devicesPerPort > 0, "Atleast one device per port needed");
+ checkArgument(devicesPerPort > 0, "At least one device per port needed");
if (schemasDir != null) {
checkArgument(schemasDir.exists(), "Schemas dir has to exist");
for (final File file : files) {
final Matcher matcher = YANG_FILENAME_PATTERN.matcher(file.getName());
if (!matcher.matches()) {
- try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
- String line = reader.readLine();
- while (!DATE_PATTERN.matcher(line).find()) {
- line = reader.readLine();
- }
- final Matcher m = DATE_PATTERN.matcher(line);
-
- if (m.find()) {
- String moduleName = file.getAbsolutePath();
- if (file.getName().endsWith(".yang")) {
- moduleName = moduleName.substring(0, moduleName.length() - 5);
- }
- final String revision = m.group(1);
- final String correctName = moduleName + "@" + revision + ".yang";
- final File correctNameFile = new File(correctName);
- if (!file.renameTo(correctNameFile)) {
- System.err.println("Failed to rename " + file);
- }
+ try {
+ final String correctName = correctedName(file);
+ if (correctName != null) {
+ Files.move(file.toPath(), Paths.get(correctName), StandardCopyOption.ATOMIC_MOVE);
}
} catch (final IOException e) {
// print error to console (test tool is running from console)
}
}
- public ArrayList<ArrayList<Execution.DestToPayload>> getThreadsPayloads(final List<Integer> openDevices) {
- final String editContentString;
- try {
- if (stream == null) {
- editContentString = Files.toString(editContent, StandardCharsets.UTF_8);
- } else {
- editContentString = CharStreams.toString(new InputStreamReader(stream, StandardCharsets.UTF_8));
- }
- } catch (final IOException e) {
- throw new IllegalArgumentException("Cannot read content of " + editContent, e);
- }
-
- int from;
- int to;
- Iterator<Integer> iterator;
-
- final ArrayList<ArrayList<Execution.DestToPayload>> allThreadsPayloads = new ArrayList<>();
- if (generateConfigBatchSize > 1) {
-
- final int batchedRequests = openDevices.size() / generateConfigBatchSize;
- final int batchedRequestsPerThread = batchedRequests / threadAmount;
- final int leftoverBatchedRequests = batchedRequests % threadAmount;
- final int leftoverRequests = openDevices.size() - batchedRequests * generateConfigBatchSize;
-
- final StringBuilder destBuilder = new StringBuilder(DEST);
- destBuilder.replace(destBuilder.indexOf(ADDRESS_PORT),
- destBuilder.indexOf(ADDRESS_PORT) + ADDRESS_PORT.length(),
- controllerDestination);
-
- for (int l = 0; l < threadAmount; l++) {
- from = l * batchedRequests * batchedRequestsPerThread;
- to = from + batchedRequests * batchedRequestsPerThread;
- iterator = openDevices.subList(from, to).iterator();
- allThreadsPayloads.add(createBatchedPayloads(batchedRequestsPerThread, iterator, editContentString,
- destBuilder.toString()));
- }
- ArrayList<Execution.DestToPayload> payloads = null;
- if (leftoverBatchedRequests > 0) {
- from = threadAmount * batchedRequests * batchedRequestsPerThread;
- to = from + batchedRequests * batchedRequestsPerThread;
- iterator = openDevices.subList(from, to).iterator();
- payloads = createBatchedPayloads(leftoverBatchedRequests, iterator, editContentString,
- destBuilder.toString());
- }
- String payload = "";
-
- for (int j = 0; j < leftoverRequests; j++) {
- from = openDevices.size() - leftoverRequests;
- to = openDevices.size();
- iterator = openDevices.subList(from, to).iterator();
- final StringBuilder payloadBuilder = new StringBuilder(
- prepareMessage(iterator.next(), editContentString));
- payload += modifyMessage(payloadBuilder, j, leftoverRequests);
+ private static String correctedName(final File file) throws IOException {
+ try (BufferedReader reader = new BufferedReader(new FileReader(file, StandardCharsets.UTF_8))) {
+ String line = reader.readLine();
+ while (line != null && !REVISION_DATE_PATTERN.matcher(line).find()) {
+ line = reader.readLine();
}
- if (leftoverRequests > 0 || leftoverBatchedRequests > 0) {
-
- if (payloads != null) {
- payloads.add(new Execution.DestToPayload(destBuilder.toString(), payload));
+ if (line != null) {
+ final Matcher m = REVISION_DATE_PATTERN.matcher(line);
+ checkState(m.find(), "Revision pattern %s did not match line %s", REVISION_DATE_PATTERN, line);
+ String moduleName = file.getAbsolutePath();
+ if (file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)) {
+ moduleName = moduleName.substring(0, moduleName.length() - 5);
}
- allThreadsPayloads.add(payloads);
- }
- } else {
- final int requestPerThreads = openDevices.size() / threadAmount;
- final int leftoverRequests = openDevices.size() % threadAmount;
-
- for (int i = 0; i < threadAmount; i++) {
- from = i * requestPerThreads;
- to = from + requestPerThreads;
- iterator = openDevices.subList(from, to).iterator();
- allThreadsPayloads.add(createPayloads(iterator, editContentString));
- }
- if (leftoverRequests > 0) {
- from = threadAmount * requestPerThreads;
- to = from + leftoverRequests;
- iterator = openDevices.subList(from, to).iterator();
- allThreadsPayloads.add(createPayloads(iterator, editContentString));
+ return moduleName + "@" + m.group(1) + YangConstants.RFC6020_YANG_FILE_EXTENSION;
}
}
- return allThreadsPayloads;
- }
-
- private String prepareMessage(final int openDevice, final String editContentString) {
- final StringBuilder messageBuilder = new StringBuilder(editContentString);
-
- if (editContentString.contains(HOST_KEY)) {
- messageBuilder.replace(messageBuilder.indexOf(HOST_KEY),
- messageBuilder.indexOf(HOST_KEY) + HOST_KEY.length(),
- generateConfigsAddress);
- }
- if (editContentString.contains(PORT_KEY)) {
- while (messageBuilder.indexOf(PORT_KEY) != -1) {
- messageBuilder.replace(messageBuilder.indexOf(PORT_KEY),
- messageBuilder.indexOf(PORT_KEY) + PORT_KEY.length(),
- Integer.toString(openDevice));
- }
- }
- if (editContentString.contains(TCP_ONLY)) {
- messageBuilder.replace(messageBuilder.indexOf(TCP_ONLY),
- messageBuilder.indexOf(TCP_ONLY) + TCP_ONLY.length(),
- Boolean.toString(!ssh));
- }
- return messageBuilder.toString();
- }
-
- private ArrayList<Execution.DestToPayload> createPayloads(final Iterator<Integer> openDevices,
- final String editContentString) {
- final ArrayList<Execution.DestToPayload> payloads = new ArrayList<>();
-
- while (openDevices.hasNext()) {
- final StringBuilder destBuilder = new StringBuilder(DEST);
- destBuilder.replace(destBuilder.indexOf(ADDRESS_PORT),
- destBuilder.indexOf(ADDRESS_PORT) + ADDRESS_PORT.length(), controllerDestination);
- payloads.add(new Execution.DestToPayload(
- destBuilder.toString(), prepareMessage(openDevices.next(), editContentString)));
- }
- return payloads;
- }
-
- private ArrayList<Execution.DestToPayload> createBatchedPayloads(final int batchedRequestsCount,
- final Iterator<Integer> openDevices, final String editContentString, final String destination) {
- final ArrayList<Execution.DestToPayload> payloads = new ArrayList<>();
-
- for (int i = 0; i < batchedRequestsCount; i++) {
- StringBuilder payload = new StringBuilder();
- for (int j = 0; j < generateConfigBatchSize; j++) {
- final StringBuilder payloadBuilder = new StringBuilder(
- prepareMessage(openDevices.next(), editContentString));
- payload.append(modifyMessage(payloadBuilder, j, generateConfigBatchSize));
- }
- payloads.add(new Execution.DestToPayload(destination, payload.toString()));
- }
- return payloads;
+ return null;
}
@Override