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.netconf.test.tool;
11 import static com.google.common.base.Preconditions.checkArgument;
12 import static com.google.common.base.Preconditions.checkNotNull;
14 import ch.qos.logback.classic.Level;
15 import com.google.common.base.Charsets;
16 import com.google.common.base.Preconditions;
17 import com.google.common.collect.Lists;
18 import com.google.common.io.ByteStreams;
19 import com.google.common.io.CharStreams;
20 import com.google.common.io.Files;
22 import java.io.FileFilter;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26 import java.util.Collections;
27 import java.util.Comparator;
28 import java.util.List;
29 import java.util.concurrent.TimeUnit;
30 import net.sourceforge.argparse4j.ArgumentParsers;
31 import net.sourceforge.argparse4j.annotation.Arg;
32 import net.sourceforge.argparse4j.inf.ArgumentParser;
33 import net.sourceforge.argparse4j.inf.ArgumentParserException;
34 import org.opendaylight.controller.config.util.xml.XmlElement;
35 import org.opendaylight.controller.config.util.xml.XmlUtil;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.w3c.dom.Document;
39 import org.w3c.dom.Element;
40 import org.w3c.dom.Node;
41 import org.w3c.dom.NodeList;
42 import org.xml.sax.SAXException;
44 public final class Main {
46 private static final Logger LOG = LoggerFactory.getLogger(Main.class);
48 public static class Params {
50 @Arg(dest = "schemas-dir")
51 public File schemasDir;
53 @Arg(dest = "devices-count")
54 public int deviceCount;
56 @Arg(dest = "devices-per-port")
57 public int devicesPerPort;
59 @Arg(dest = "starting-port")
60 public int startingPort;
62 @Arg(dest = "generate-config-connection-timeout")
63 public int generateConfigsTimeout;
65 @Arg(dest = "generate-config-address")
66 public String generateConfigsAddress;
68 @Arg(dest = "distro-folder")
69 public File distroFolder;
71 @Arg(dest = "generate-configs-batch-size")
72 public int generateConfigBatchSize;
83 @Arg(dest = "notification-file")
84 public File notificationFile;
89 @Arg(dest = "initial-config-xml-file")
90 public File initialConfigXMLFile;
92 static ArgumentParser getParser() {
93 final ArgumentParser parser = ArgumentParsers.newArgumentParser("netconf testool");
95 parser.description("Netconf device simulator. Detailed info can be found at https://wiki.opendaylight.org/view/OpenDaylight_Controller:Netconf:Testtool#Building_testtool");
97 parser.addArgument("--device-count")
100 .help("Number of simulated netconf devices to spin. This is the number of actual ports open for the devices.")
101 .dest("devices-count");
103 parser.addArgument("--devices-per-port")
106 .help("Amount of config files generated per port to spoof more devices then are actually running")
107 .dest("devices-per-port");
109 parser.addArgument("--schemas-dir")
111 .help("Directory containing yang schemas to describe simulated devices. Some schemas e.g. netconf monitoring and inet types are included by default")
112 .dest("schemas-dir");
114 parser.addArgument("--notification-file")
116 .help("Xml file containing notifications that should be sent to clients after create subscription is called")
117 .dest("notification-file");
119 parser.addArgument("--initial-config-xml-file")
121 .help("Xml file containing initial simulatted configuration to be returned via get-config rpc")
122 .dest("initial-config-xml-file");
124 parser.addArgument("--starting-port")
127 .help("First port for simulated device. Each other device will have previous+1 port number")
128 .dest("starting-port");
130 parser.addArgument("--generate-config-connection-timeout")
132 .setDefault((int)TimeUnit.MINUTES.toMillis(30))
133 .help("Timeout to be generated in initial config files")
134 .dest("generate-config-connection-timeout");
136 parser.addArgument("--generate-config-address")
138 .setDefault("127.0.0.1")
139 .help("Address to be placed in generated configs")
140 .dest("generate-config-address");
142 parser.addArgument("--generate-configs-batch-size")
145 .help("Number of connector configs per generated file")
146 .dest("generate-configs-batch-size");
148 parser.addArgument("--distribution-folder")
150 .help("Directory where the karaf distribution for controller is located")
151 .dest("distro-folder");
153 parser.addArgument("--ssh")
156 .help("Whether to use ssh for transport or just pure tcp")
159 parser.addArgument("--exi")
162 .help("Whether to use exi to transport xml content")
165 parser.addArgument("--debug")
168 .help("Whether to use debug log level instead of INFO")
171 parser.addArgument("--md-sal")
174 .help("Whether to use md-sal datastore instead of default simulated datastore.")
181 checkArgument(deviceCount > 0, "Device count has to be > 0");
182 checkArgument(startingPort > 1023, "Starting port has to be > 1023");
183 checkArgument(devicesPerPort > 0, "Atleast one device per port needed");
185 if(schemasDir != null) {
186 checkArgument(schemasDir.exists(), "Schemas dir has to exist");
187 checkArgument(schemasDir.isDirectory(), "Schemas dir has to be a directory");
188 checkArgument(schemasDir.canRead(), "Schemas dir has to be readable");
193 public static void main(final String[] args) {
194 final Params params = parseArgs(args, Params.getParser());
197 final ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
198 root.setLevel(params.debug ? Level.DEBUG : Level.INFO);
200 final NetconfDeviceSimulator netconfDeviceSimulator = new NetconfDeviceSimulator();
202 final List<Integer> openDevices = netconfDeviceSimulator.start(params);
203 if (openDevices.size() == 0) {
204 LOG.error("Failed to start any simulated devices, exiting...");
207 if(params.distroFolder != null) {
208 final ConfigGenerator configGenerator = new ConfigGenerator(params.distroFolder, openDevices);
209 final List<File> generated = configGenerator.generate(
210 params.ssh, params.generateConfigBatchSize,
211 params.generateConfigsTimeout, params.generateConfigsAddress,
212 params.devicesPerPort);
213 configGenerator.updateFeatureFile(generated);
214 configGenerator.changeLoadOrder();
216 } catch (final Exception e) {
217 LOG.error("Unhandled exception", e);
218 netconfDeviceSimulator.close();
223 synchronized (netconfDeviceSimulator) {
225 netconfDeviceSimulator.wait();
226 } catch (final InterruptedException e) {
227 throw new RuntimeException(e);
232 private static Params parseArgs(final String[] args, final ArgumentParser parser) {
233 final Params opt = new Params();
235 parser.parseArgs(args, opt);
237 } catch (final ArgumentParserException e) {
238 parser.handleError(e);
245 static class ConfigGenerator {
246 public static final String NETCONF_CONNECTOR_XML = "/99-netconf-connector-simulated.xml";
247 public static final String SIM_DEVICE_SUFFIX = "-sim-device";
249 private static final String SIM_DEVICE_CFG_PREFIX = "simulated-devices_";
250 private static final String ETC_KARAF_PATH = "etc/";
251 private static final String ETC_OPENDAYLIGHT_KARAF_PATH = ETC_KARAF_PATH + "opendaylight/karaf/";
253 public static final String NETCONF_CONNECTOR_ALL_FEATURE = "odl-netconf-connector-all";
254 private static final String ORG_OPS4J_PAX_URL_MVN_CFG = "org.ops4j.pax.url.mvn.cfg";
256 private final File configDir;
257 private final List<Integer> openDevices;
258 private final List<File> ncFeatureFiles;
259 private final File etcDir;
260 private final File loadOrderCfgFile;
262 public ConfigGenerator(final File directory, final List<Integer> openDevices) {
263 this.configDir = new File(directory, ETC_OPENDAYLIGHT_KARAF_PATH);
264 this.etcDir = new File(directory, ETC_KARAF_PATH);
265 this.loadOrderCfgFile = new File(etcDir, ORG_OPS4J_PAX_URL_MVN_CFG);
266 this.ncFeatureFiles = getFeatureFile(directory, "features-netconf-connector", "xml");
267 this.openDevices = openDevices;
270 public List<File> generate(final boolean useSsh, final int batchSize,
271 final int generateConfigsTimeout, final String address,
272 final int devicesPerPort) {
273 if(configDir.exists() == false) {
274 Preconditions.checkState(configDir.mkdirs(), "Unable to create directory " + configDir);
277 for (final File file : configDir.listFiles(new FileFilter() {
279 public boolean accept(final File pathname) {
280 return !pathname.isDirectory() && pathname.getName().startsWith(SIM_DEVICE_CFG_PREFIX);
283 Preconditions.checkState(file.delete(), "Unable to clean previous generated file %s", file);
286 try(InputStream stream = Main.class.getResourceAsStream(NETCONF_CONNECTOR_XML)) {
287 checkNotNull(stream, "Cannot load %s", NETCONF_CONNECTOR_XML);
288 String configBlueprint = CharStreams.toString(new InputStreamReader(stream, Charsets.UTF_8));
290 final String before = configBlueprint.substring(0, configBlueprint.indexOf("<module>"));
291 final String middleBlueprint = configBlueprint.substring(configBlueprint.indexOf("<module>"), configBlueprint.indexOf("</module>"));
292 final String after = configBlueprint.substring(configBlueprint.indexOf("</module>") + "</module>".length());
294 int connectorCount = 0;
295 Integer batchStart = null;
296 StringBuilder b = new StringBuilder();
299 final List<File> generatedConfigs = Lists.newArrayList();
301 for (final Integer openDevice : openDevices) {
302 if(batchStart == null) {
303 batchStart = openDevice;
306 for (int i = 0; i < devicesPerPort; i++) {
307 final String name = String.valueOf(openDevice) + SIM_DEVICE_SUFFIX + (i == 0 ? "" : "-" + String.valueOf(i));
308 String configContent = String.format(middleBlueprint, name, address, String.valueOf(openDevice), String.valueOf(!useSsh));
309 configContent = String.format("%s%s%d%s\n%s\n", configContent, "<connection-timeout-millis>", generateConfigsTimeout, "</connection-timeout-millis>", "</module>");
311 b.append(configContent);
313 if(connectorCount == batchSize) {
315 final File to = new File(configDir, String.format(SIM_DEVICE_CFG_PREFIX + "%d-%d.xml", batchStart, openDevice));
316 generatedConfigs.add(to);
317 Files.write(b.toString(), to, Charsets.UTF_8);
319 b = new StringBuilder();
327 if(connectorCount != 0) {
329 final File to = new File(configDir, String.format(SIM_DEVICE_CFG_PREFIX + "%d-%d.xml", batchStart, openDevices.get(openDevices.size() - 1)));
330 generatedConfigs.add(to);
331 Files.write(b.toString(), to, Charsets.UTF_8);
334 LOG.info("Config files generated in {}", configDir);
335 return generatedConfigs;
336 } catch (final IOException e) {
337 throw new RuntimeException("Unable to generate config files", e);
342 public void updateFeatureFile(final List<File> generated) {
343 // TODO karaf core contains jaxb for feature files, use that for
346 for (final File featureFile : ncFeatureFiles) {
347 final Document document = XmlUtil.readXmlToDocument(Files
348 .toString(featureFile, Charsets.UTF_8));
349 final NodeList childNodes = document.getDocumentElement().getChildNodes();
351 for (int i = 0; i < childNodes.getLength(); i++) {
352 final Node item = childNodes.item(i);
353 if (item instanceof Element == false) {
356 if (item.getLocalName().equals("feature") == false) {
360 if (NETCONF_CONNECTOR_ALL_FEATURE
361 .equals(((Element) item).getAttribute("name"))) {
362 final Element ncAllFeatureDefinition = (Element) item;
363 // Clean previous generated files
364 for (final XmlElement configfile : XmlElement
365 .fromDomElement(ncAllFeatureDefinition)
366 .getChildElements("configfile")) {
367 ncAllFeatureDefinition.removeChild(configfile.getDomElement());
369 for (final File file : generated) {
370 final Element configfile = document.createElement("configfile");
371 configfile.setTextContent("file:"
372 + ETC_OPENDAYLIGHT_KARAF_PATH
374 configfile.setAttribute(
376 ETC_OPENDAYLIGHT_KARAF_PATH
378 ncAllFeatureDefinition.appendChild(configfile);
383 Files.write(XmlUtil.toString(document), featureFile,Charsets.UTF_8);
384 LOG.info("Feature file {} updated", featureFile);
386 } catch (final IOException e) {
387 throw new RuntimeException("Unable to load features file as a resource");
388 } catch (final SAXException e) {
389 throw new RuntimeException("Unable to parse features file");
394 private static List<File> getFeatureFile(final File distroFolder, final String featureName, final String suffix) {
395 checkExistingDir(distroFolder, String.format("Folder %s does not exist", distroFolder));
397 final File systemDir = checkExistingDir(new File(distroFolder, "system"), String.format("Folder %s does not contain a karaf distro, folder system is missing", distroFolder));
399 //check if beryllium path exists, if it doesnt check for lithium and fail/succeed after
400 File netconfConnectorFeaturesParentDir = new File(systemDir, "org/opendaylight/netconf/" + featureName);
401 if (!netconfConnectorFeaturesParentDir.exists() || !netconfConnectorFeaturesParentDir.isDirectory()) {
402 netconfConnectorFeaturesParentDir = checkExistingDir(new File(systemDir, "org/opendaylight/controller/" + featureName), String.format("Karaf distro in %s does not contain netconf-connector features", distroFolder));
405 // Find newest version for features
406 final File newestVersionDir = Collections.max(
407 Lists.newArrayList(netconfConnectorFeaturesParentDir.listFiles(new FileFilter() {
409 public boolean accept(final File pathname) {
410 return pathname.isDirectory();
412 })), new Comparator<File>() {
414 public int compare(final File o1, final File o2) {
415 return o1.getName().compareTo(o2.getName());
419 return Lists.newArrayList(newestVersionDir.listFiles(new FileFilter() {
421 public boolean accept(final File pathname) {
422 return pathname.getName().contains(featureName)
423 && Files.getFileExtension(pathname.getName()).equals(suffix);
428 private static File checkExistingDir(final File folder, final String msg) {
429 Preconditions.checkArgument(folder.exists(), msg);
430 Preconditions.checkArgument(folder.isDirectory(), msg);
434 public void changeLoadOrder() {
436 Files.write(ByteStreams.toByteArray(getClass().getResourceAsStream("/" +ORG_OPS4J_PAX_URL_MVN_CFG)), loadOrderCfgFile);
437 LOG.info("Load order changed to prefer local bundles/features by rewriting file {}", loadOrderCfgFile);
438 } catch (IOException e) {
439 throw new RuntimeException("Unable to rewrite features file " + loadOrderCfgFile, e);