2 * Copyright (c) 2014 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.controller.netconf.test.tool;
11 import static com.google.common.base.Preconditions.checkArgument;
12 import static com.google.common.base.Preconditions.checkNotNull;
13 import static com.google.common.base.Preconditions.checkState;
15 import ch.qos.logback.classic.Level;
16 import com.google.common.base.Charsets;
17 import com.google.common.base.Preconditions;
18 import com.google.common.collect.Lists;
19 import com.google.common.io.ByteStreams;
20 import com.google.common.io.CharStreams;
21 import com.google.common.io.Files;
23 import java.io.FileFilter;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.util.Collections;
28 import java.util.Comparator;
29 import java.util.List;
30 import java.util.concurrent.TimeUnit;
31 import net.sourceforge.argparse4j.ArgumentParsers;
32 import net.sourceforge.argparse4j.annotation.Arg;
33 import net.sourceforge.argparse4j.inf.ArgumentParser;
34 import net.sourceforge.argparse4j.inf.ArgumentParserException;
35 import org.opendaylight.controller.netconf.util.xml.XmlElement;
36 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.w3c.dom.Document;
40 import org.w3c.dom.Element;
41 import org.w3c.dom.Node;
42 import org.w3c.dom.NodeList;
43 import org.xml.sax.SAXException;
45 public final class Main {
47 private static final Logger LOG = LoggerFactory.getLogger(Main.class);
49 public static class Params {
51 @Arg(dest = "schemas-dir")
52 public File schemasDir;
54 @Arg(dest = "devices-count")
55 public int deviceCount;
57 @Arg(dest = "starting-port")
58 public int startingPort;
60 @Arg(dest = "generate-config-connection-timeout")
61 public int generateConfigsTimeout;
63 @Arg(dest = "generate-config-address")
64 public String generateConfigsAddress;
66 @Arg(dest = "distro-folder")
67 public File distroFolder;
69 @Arg(dest = "generate-configs-batch-size")
70 public int generateConfigBatchSize;
81 @Arg(dest = "notification-file")
82 public File notificationFile;
84 static ArgumentParser getParser() {
85 final ArgumentParser parser = ArgumentParsers.newArgumentParser("netconf testool");
87 parser.description("Netconf device simulator. Detailed info can be found at https://wiki.opendaylight.org/view/OpenDaylight_Controller:Netconf:Testtool#Building_testtool");
89 parser.addArgument("--device-count")
93 .help("Number of simulated netconf devices to spin")
94 .dest("devices-count");
96 parser.addArgument("--schemas-dir")
98 .help("Directory containing yang schemas to describe simulated devices. Some schemas e.g. netconf monitoring and inet types are included by default")
101 parser.addArgument("--notification-file")
103 .help("Xml file containing notifications that should be sent to clients after create subscription is called")
104 .dest("notification-file");
106 parser.addArgument("--starting-port")
109 .help("First port for simulated device. Each other device will have previous+1 port number")
110 .dest("starting-port");
112 parser.addArgument("--generate-config-connection-timeout")
114 .setDefault((int)TimeUnit.MINUTES.toMillis(30))
115 .help("Timeout to be generated in initial config files")
116 .dest("generate-config-connection-timeout");
118 parser.addArgument("--generate-config-address")
120 .setDefault("127.0.0.1")
121 .help("Address to be placed in generated configs")
122 .dest("generate-config-address");
124 parser.addArgument("--generate-configs-batch-size")
127 .help("Number of connector configs per generated file")
128 .dest("generate-configs-batch-size");
130 parser.addArgument("--distribution-folder")
132 .help("Directory where the karaf distribution for controller is located")
133 .dest("distro-folder");
135 parser.addArgument("--ssh")
138 .help("Whether to use ssh for transport or just pure tcp")
141 parser.addArgument("--exi")
144 .help("Whether to use exi to transport xml content")
147 parser.addArgument("--debug")
150 .help("Whether to use debug log level instead of INFO")
157 checkArgument(deviceCount > 0, "Device count has to be > 0");
158 checkArgument(startingPort > 1023, "Starting port has to be > 1023");
160 if(schemasDir != null) {
161 checkArgument(schemasDir.exists(), "Schemas dir has to exist");
162 checkArgument(schemasDir.isDirectory(), "Schemas dir has to be a directory");
163 checkArgument(schemasDir.canRead(), "Schemas dir has to be readable");
168 public static void main(final String[] args) {
169 final Params params = parseArgs(args, Params.getParser());
172 final ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
173 root.setLevel(params.debug ? Level.DEBUG : Level.INFO);
175 final NetconfDeviceSimulator netconfDeviceSimulator = new NetconfDeviceSimulator();
177 final List<Integer> openDevices = netconfDeviceSimulator.start(params);
178 if (openDevices.size() == 0) {
179 LOG.error("Failed to start any simulated devices, exiting...");
182 if(params.distroFolder != null) {
183 final ConfigGenerator configGenerator = new ConfigGenerator(params.distroFolder, openDevices);
184 final List<File> generated = configGenerator.generate(params.ssh, params.generateConfigBatchSize, params.generateConfigsTimeout, params.generateConfigsAddress);
185 configGenerator.updateFeatureFile(generated);
186 configGenerator.changeLoadOrder();
188 } catch (final Exception e) {
189 LOG.error("Unhandled exception", e);
190 netconfDeviceSimulator.close();
195 synchronized (netconfDeviceSimulator) {
197 netconfDeviceSimulator.wait();
198 } catch (final InterruptedException e) {
199 throw new RuntimeException(e);
204 private static Params parseArgs(final String[] args, final ArgumentParser parser) {
205 final Params opt = new Params();
207 parser.parseArgs(args, opt);
209 } catch (final ArgumentParserException e) {
210 parser.handleError(e);
217 private static class ConfigGenerator {
218 public static final String NETCONF_CONNECTOR_XML = "/initial/99-netconf-connector.xml";
219 public static final String NETCONF_CONNECTOR_NAME = "controller-config";
220 public static final String NETCONF_CONNECTOR_PORT = "1830";
221 public static final String NETCONF_CONNECTOR_ADDRESS = "127.0.0.1";
222 public static final String NETCONF_USE_SSH = "false";
223 public static final String SIM_DEVICE_SUFFIX = "-sim-device";
225 private static final String SIM_DEVICE_CFG_PREFIX = "simulated-devices_";
226 private static final String ETC_KARAF_PATH = "etc/";
227 private static final String ETC_OPENDAYLIGHT_KARAF_PATH = ETC_KARAF_PATH + "opendaylight/karaf/";
229 public static final String NETCONF_CONNECTOR_ALL_FEATURE = "odl-netconf-connector-all";
230 private static final String ORG_OPS4J_PAX_URL_MVN_CFG = "org.ops4j.pax.url.mvn.cfg";
232 private final File configDir;
233 private final List<Integer> openDevices;
234 private final List<File> ncFeatureFiles;
235 private final File etcDir;
236 private final File loadOrderCfgFile;
238 public ConfigGenerator(final File directory, final List<Integer> openDevices) {
239 this.configDir = new File(directory, ETC_OPENDAYLIGHT_KARAF_PATH);
240 this.etcDir = new File(directory, ETC_KARAF_PATH);
241 this.loadOrderCfgFile = new File(etcDir, ORG_OPS4J_PAX_URL_MVN_CFG);
242 this.ncFeatureFiles = getFeatureFile(directory, "features-netconf-connector", "xml");
243 this.openDevices = openDevices;
246 public List<File> generate(final boolean useSsh, final int batchSize, final int generateConfigsTimeout, final String address) {
247 if(configDir.exists() == false) {
248 Preconditions.checkState(configDir.mkdirs(), "Unable to create directory " + configDir);
251 for (final File file : configDir.listFiles(new FileFilter() {
253 public boolean accept(final File pathname) {
254 return !pathname.isDirectory() && pathname.getName().startsWith(SIM_DEVICE_CFG_PREFIX);
257 Preconditions.checkState(file.delete(), "Unable to clean previous generated file %s", file);
260 try(InputStream stream = Main.class.getResourceAsStream(NETCONF_CONNECTOR_XML)) {
261 checkNotNull(stream, "Cannot load %s", NETCONF_CONNECTOR_XML);
262 String configBlueprint = CharStreams.toString(new InputStreamReader(stream, Charsets.UTF_8));
264 checkState(configBlueprint.contains(NETCONF_CONNECTOR_NAME));
265 checkState(configBlueprint.contains(NETCONF_CONNECTOR_PORT));
266 checkState(configBlueprint.contains(NETCONF_USE_SSH));
267 checkState(configBlueprint.contains(NETCONF_CONNECTOR_ADDRESS));
268 configBlueprint = configBlueprint.replace(NETCONF_CONNECTOR_NAME, "%s");
269 configBlueprint = configBlueprint.replace(NETCONF_CONNECTOR_ADDRESS, "%s");
270 configBlueprint = configBlueprint.replace(NETCONF_CONNECTOR_PORT, "%s");
271 configBlueprint = configBlueprint.replace(NETCONF_USE_SSH, "%s");
273 final String before = configBlueprint.substring(0, configBlueprint.indexOf("<module>"));
274 final String middleBlueprint = configBlueprint.substring(configBlueprint.indexOf("<module>"), configBlueprint.indexOf("</module>"));
275 final String after = configBlueprint.substring(configBlueprint.indexOf("</module>") + "</module>".length());
277 int connectorCount = 0;
278 Integer batchStart = null;
279 StringBuilder b = new StringBuilder();
282 final List<File> generatedConfigs = Lists.newArrayList();
284 for (final Integer openDevice : openDevices) {
285 if(batchStart == null) {
286 batchStart = openDevice;
289 final String name = String.valueOf(openDevice) + SIM_DEVICE_SUFFIX;
290 String configContent = String.format(middleBlueprint, name, address, String.valueOf(openDevice), String.valueOf(!useSsh));
291 configContent = String.format("%s%s%d%s\n%s\n", configContent, "<connection-timeout-millis>", generateConfigsTimeout, "</connection-timeout-millis>", "</module>");
293 b.append(configContent);
295 if(connectorCount == batchSize) {
297 final File to = new File(configDir, String.format(SIM_DEVICE_CFG_PREFIX + "%d-%d.xml", batchStart, openDevice));
298 generatedConfigs.add(to);
299 Files.write(b.toString(), to, Charsets.UTF_8);
301 b = new StringBuilder();
308 if(connectorCount != 0) {
310 final File to = new File(configDir, String.format(SIM_DEVICE_CFG_PREFIX + "%d-%d.xml", batchStart, openDevices.get(openDevices.size() - 1)));
311 generatedConfigs.add(to);
312 Files.write(b.toString(), to, Charsets.UTF_8);
315 LOG.info("Config files generated in {}", configDir);
316 return generatedConfigs;
317 } catch (final IOException e) {
318 throw new RuntimeException("Unable to generate config files", e);
323 public void updateFeatureFile(final List<File> generated) {
324 // TODO karaf core contains jaxb for feature files, use that for
327 for (final File featureFile : ncFeatureFiles) {
328 final Document document = XmlUtil.readXmlToDocument(Files
329 .toString(featureFile, Charsets.UTF_8));
330 final NodeList childNodes = document.getDocumentElement().getChildNodes();
332 for (int i = 0; i < childNodes.getLength(); i++) {
333 final Node item = childNodes.item(i);
334 if (item instanceof Element == false) {
337 if (item.getLocalName().equals("feature") == false) {
341 if (NETCONF_CONNECTOR_ALL_FEATURE
342 .equals(((Element) item).getAttribute("name"))) {
343 final Element ncAllFeatureDefinition = (Element) item;
344 // Clean previous generated files
345 for (final XmlElement configfile : XmlElement
346 .fromDomElement(ncAllFeatureDefinition)
347 .getChildElements("configfile")) {
348 ncAllFeatureDefinition.removeChild(configfile.getDomElement());
350 for (final File file : generated) {
351 final Element configfile = document.createElement("configfile");
352 configfile.setTextContent("file:"
353 + ETC_OPENDAYLIGHT_KARAF_PATH
355 configfile.setAttribute(
357 ETC_OPENDAYLIGHT_KARAF_PATH
359 ncAllFeatureDefinition.appendChild(configfile);
364 Files.write(XmlUtil.toString(document), featureFile,Charsets.UTF_8);
365 LOG.info("Feature file {} updated", featureFile);
367 } catch (final IOException e) {
368 throw new RuntimeException("Unable to load features file as a resource");
369 } catch (final SAXException e) {
370 throw new RuntimeException("Unable to parse features file");
375 private static List<File> getFeatureFile(final File distroFolder, final String featureName, final String suffix) {
376 checkExistingDir(distroFolder, String.format("Folder %s does not exist", distroFolder));
378 final File systemDir = checkExistingDir(new File(distroFolder, "system"), String.format("Folder %s does not contain a karaf distro, folder system is missing", distroFolder));
379 final File netconfConnectorFeaturesParentDir = checkExistingDir(new File(systemDir, "org/opendaylight/controller/" + featureName), String.format("Karaf distro in %s does not contain netconf-connector features", distroFolder));
381 // Find newest version for features
382 final File newestVersionDir = Collections.max(
383 Lists.newArrayList(netconfConnectorFeaturesParentDir.listFiles(new FileFilter() {
385 public boolean accept(final File pathname) {
386 return pathname.isDirectory();
388 })), new Comparator<File>() {
390 public int compare(final File o1, final File o2) {
391 return o1.getName().compareTo(o2.getName());
395 return Lists.newArrayList(newestVersionDir.listFiles(new FileFilter() {
397 public boolean accept(final File pathname) {
398 return pathname.getName().contains(featureName)
399 && Files.getFileExtension(pathname.getName()).equals(suffix);
404 private static File checkExistingDir(final File folder, final String msg) {
405 Preconditions.checkArgument(folder.exists(), msg);
406 Preconditions.checkArgument(folder.isDirectory(), msg);
410 public void changeLoadOrder() {
412 Files.write(ByteStreams.toByteArray(getClass().getResourceAsStream("/" +ORG_OPS4J_PAX_URL_MVN_CFG)), loadOrderCfgFile);
413 LOG.info("Load order changed to prefer local bundles/features by rewriting file {}", loadOrderCfgFile);
414 } catch (IOException e) {
415 throw new RuntimeException("Unable to rewrite features file " + loadOrderCfgFile, e);