X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=netconf%2Ftools%2Fnetconf-testtool%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fnetconf%2Ftest%2Ftool%2FTesttoolParameters.java;h=6522de76751fb342a0de420499d11b1bb13b88c3;hb=0ddc89fb63ac86e87835388609efa6ef905b2bae;hp=0ad0f1831c8d5dd3b4606b518c9d537c55301554;hpb=f46a22d9b6117b3e5da7ca40c0312ca53afcf584;p=netconf.git diff --git a/netconf/tools/netconf-testtool/src/main/java/org/opendaylight/netconf/test/tool/TesttoolParameters.java b/netconf/tools/netconf-testtool/src/main/java/org/opendaylight/netconf/test/tool/TesttoolParameters.java index 0ad0f1831c..6522de7675 100644 --- a/netconf/tools/netconf-testtool/src/main/java/org/opendaylight/netconf/test/tool/TesttoolParameters.java +++ b/netconf/tools/netconf-testtool/src/main/java/org/opendaylight/netconf/test/tool/TesttoolParameters.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, @@ -10,103 +10,98 @@ package org.opendaylight.netconf.test.tool; import static com.google.common.base.Preconditions.checkArgument; -import com.google.common.base.Charsets; 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.util.Arrays; +import java.util.Collections; +import java.util.Iterator; import java.util.List; +import java.util.Objects; +import java.util.StringJoiner; import java.util.concurrent.TimeUnit; +import java.util.regex.Matcher; import java.util.regex.Pattern; import net.sourceforge.argparse4j.ArgumentParsers; import net.sourceforge.argparse4j.annotation.Arg; import net.sourceforge.argparse4j.inf.ArgumentParser; import net.sourceforge.argparse4j.inf.ArgumentParserException; +@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 SSH = "{SSH}"; + 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/node/{PORT}-sim-device"; - - private static final String RESOURCE = "/config-template.xml"; - private InputStream stream; + private static final String DEST = + "http://{ADDRESS:PORT}/restconf/config/network-topology:network-topology/topology/topology-netconf/"; + private static final Pattern YANG_FILENAME_PATTERN = Pattern + .compile("(?.*)@(?\\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 auth; - @Arg(dest = "controller-destination") public String controllerDestination; - @Arg(dest = "schemas-dir") public File schemasDir; - @Arg(dest = "devices-count") public int deviceCount; - @Arg(dest = "devices-per-port") public int devicesPerPort; - @Arg(dest = "starting-port") public int startingPort; - @Arg(dest = "generate-config-connection-timeout") public int generateConfigsTimeout; - @Arg(dest = "generate-config-address") public String generateConfigsAddress; - @Arg(dest = "distro-folder") public File distroFolder; - @Arg(dest = "generate-configs-batch-size") public int generateConfigBatchSize; - @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 notificationFile; - @Arg(dest = "md-sal") public boolean mdSal; - @Arg(dest = "initial-config-xml-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") public int threadPoolSize; + @Arg(dest = "rpc-config") + public File rpcConfig; + @SuppressWarnings("checkstyle:lineLength") static ArgumentParser getParser() { final ArgumentParser parser = ArgumentParsers.newArgumentParser("netconf testtool"); @@ -118,7 +113,7 @@ public class TesttoolParameters { parser.addArgument("--async-requests") .type(Boolean.class) - .setDefault(false) + .setDefault(Boolean.FALSE) .dest("async"); parser.addArgument("--thread-amount") @@ -130,8 +125,8 @@ public class TesttoolParameters { parser.addArgument("--throttle") .type(Integer.class) .setDefault(5000) - .help("Maximum amount of async requests that can be open at a time, " + - "with mutltiple threads this gets divided among all threads") + .help("Maximum amount of async requests that can be open at a time, " + + "with mutltiple threads this gets divided among all threads") .dest("throttle"); parser.addArgument("--auth") @@ -141,10 +136,10 @@ public class TesttoolParameters { parser.addArgument("--controller-destination") .type(String.class) - .help("Ip address and port of controller. Must be in following format : "+ - "if available it will be used for spawning netconf connectors via topology configuration as "+ - "a part of URI. Example (http:///restconf/config/network-topology:network-topology/topology/topology-netconf/node/)"+ - "otherwise it will just start simulated devices and skip the execution of PUT requests") + .help("Ip address and port of controller. Must be in following format : " + + "if available it will be used for spawning netconf connectors via topology configuration as " + + "a part of URI. Example (http:///restconf/config/network-topology:network-topology/topology/topology-netconf/node/)" + + "otherwise it will just start simulated devices and skip the execution of PUT requests") .dest("controller-destination"); parser.addArgument("--device-count") @@ -194,7 +189,7 @@ public class TesttoolParameters { parser.addArgument("--generate-configs-batch-size") .type(Integer.class) - .setDefault(4000) + .setDefault(1) .help("Number of connector configs per generated file") .dest("generate-configs-batch-size"); @@ -205,25 +200,25 @@ public class TesttoolParameters { 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"); @@ -236,9 +231,9 @@ public class TesttoolParameters { parser.addArgument("-ip") .type(String.class) .setDefault("0.0.0.0") - .help("Ip address which will be used for creating a socket address." + - "It can either be a machine name, such as " + - "java.sun.com, or a textual representation of its IP address.") + .help("Ip address which will be used for creating a socket address." + + "It can either be a machine name, such as " + + "java.sun.com, or a textual representation of its IP address.") .dest("ip"); parser.addArgument("--thread-pool-size") @@ -246,6 +241,11 @@ public class TesttoolParameters { .setDefault(8) .help("The number of threads to keep in the pool, when creating a device simulator. Even if they are idle.") .dest("thread-pool-size"); + parser.addArgument("--rpc-config") + .type(File.class) + .help("Rpc config file. It can be used to define custom rpc behavior, or override the default one." + + "Usable for testing buggy device behavior.") + .dest("rpc-config"); return parser; } @@ -263,6 +263,26 @@ public class TesttoolParameters { 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); @@ -272,8 +292,9 @@ public class TesttoolParameters { } if (controllerDestination != null) { - Preconditions.checkArgument(controllerDestination.contains(":"), "Controller Destination needs to be in a following format :"); - String[] parts = controllerDestination.split(Pattern.quote(":")); + Preconditions.checkArgument(controllerDestination.contains(":"), + "Controller Destination needs to be in a following format :"); + final String[] parts = controllerDestination.split(Pattern.quote(":")); Preconditions.checkArgument(Integer.parseInt(parts[1]) > 0, "Port =< 0"); } @@ -285,50 +306,196 @@ public class TesttoolParameters { checkArgument(schemasDir.exists(), "Schemas dir has to exist"); checkArgument(schemasDir.isDirectory(), "Schemas dir has to be a directory"); checkArgument(schemasDir.canRead(), "Schemas dir has to be readable"); + + final File[] filesArray = schemasDir.listFiles(); + final List files = filesArray != null ? Arrays.asList(filesArray) : Collections.emptyList(); + 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 (Objects.nonNull(line) && !REVISION_DATE_PATTERN.matcher(line).find()) { + line = reader.readLine(); + } + if (Objects.nonNull(line)) { + final Matcher m = REVISION_DATE_PATTERN.matcher(line); + Preconditions.checkState(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)) { + throw new IllegalStateException("Failed to rename '%s'." + file); + } + } + } catch (final IOException e) { + // print error to console (test tool is running from console) + e.printStackTrace(); + } + } + } + } + if (rpcConfig != null) { + checkArgument(rpcConfig.exists(), "Rpc config file has to exist"); + checkArgument(!rpcConfig.isDirectory(), "Rpc config file can't be a directory"); + checkArgument(rpcConfig.canRead(), "Rpc config file to be readable"); } } - public ArrayList> getThreadsPayloads(List openDevices) { + public ArrayList> getThreadsPayloads(final List openDevices) { final String editContentString; try { - if(stream == null) - { - editContentString = Files.toString(editContent, Charsets.UTF_8); + if (stream == null) { + editContentString = Files.asCharSource(editContent, StandardCharsets.UTF_8).read(); } else { - editContentString = CharStreams.toString(new InputStreamReader(stream, Charsets.UTF_8)); + editContentString = CharStreams.toString(new InputStreamReader(stream, StandardCharsets.UTF_8)); } } catch (final IOException e) { - throw new IllegalArgumentException("Cannot read content of " + editContent); + throw new IllegalArgumentException("Cannot read content of " + editContent, e); } + int from; + int to; + Iterator iterator; + final ArrayList> allThreadsPayloads = new ArrayList<>(); - for (int i = 0; i < threadAmount; i++) { - final ArrayList payloads = new ArrayList<>(); - for (int j = 0; j < openDevices.size(); j++) { - final StringBuilder destBuilder = new StringBuilder(dest); - destBuilder.replace(destBuilder.indexOf(ADDRESS_PORT), destBuilder.indexOf(ADDRESS_PORT) + ADDRESS_PORT.length(), controllerDestination) - .replace(destBuilder.indexOf(PORT_KEY), destBuilder.indexOf(PORT_KEY) + PORT_KEY.length(), Integer.toString(openDevices.get(j))); - payloads.add(new Execution.DestToPayload(destBuilder.toString(), prepareMessage(openDevices.get(j), editContentString))); + 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 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); + } + if (leftoverRequests > 0 || leftoverBatchedRequests > 0) { + + if (payloads != null) { + payloads.add(new Execution.DestToPayload(destBuilder.toString(), payload)); + } + 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)); } - allThreadsPayloads.add(payloads); - } + if (leftoverRequests > 0) { + from = threadAmount * requestPerThreads; + to = from + leftoverRequests; + iterator = openDevices.subList(from, to).iterator(); + allThreadsPayloads.add(createPayloads(iterator, editContentString)); + } + } return allThreadsPayloads; } private String prepareMessage(final int openDevice, final String editContentString) { - StringBuilder messageBuilder = new StringBuilder(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); + 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)); + 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(SSH)) { - messageBuilder.replace(messageBuilder.indexOf(SSH), messageBuilder.indexOf(SSH) + SSH.length(), Boolean.toString(ssh)); + 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 createPayloads(final Iterator openDevices, + final String editContentString) { + final ArrayList 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 createBatchedPayloads(final int batchedRequestsCount, + final Iterator openDevices, final String editContentString, final String destination) { + final ArrayList 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; + } + + @Override + public String toString() { + final List fields = Arrays.asList(this.getClass().getDeclaredFields()); + final StringJoiner joiner = new StringJoiner(", \n", "TesttoolParameters{", "}\n"); + fields.stream() + .filter(field -> field.getAnnotation(Arg.class) != null) + .map(this::getFieldString) + .forEach(joiner::add); + return joiner.toString(); + } + + private String getFieldString(final Field field) { + try { + return field.getName() + "='" + field.get(this) + "'"; + } catch (final IllegalAccessException e) { + return field.getName() + "= UNKNOWN"; + } + } }