BUG 3274 - Generate multiple configs for same port
[netconf.git] / opendaylight / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / Main.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netconf.test.tool;
10
11 import static com.google.common.base.Preconditions.checkArgument;
12 import static com.google.common.base.Preconditions.checkNotNull;
13
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;
21 import java.io.File;
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;
43
44 public final class Main {
45
46     private static final Logger LOG = LoggerFactory.getLogger(Main.class);
47
48     public static class Params {
49
50         @Arg(dest = "schemas-dir")
51         public File schemasDir;
52
53         @Arg(dest = "devices-count")
54         public int deviceCount;
55
56         @Arg(dest = "devices-per-port")
57         public int devicesPerPort;
58
59         @Arg(dest = "starting-port")
60         public int startingPort;
61
62         @Arg(dest = "generate-config-connection-timeout")
63         public int generateConfigsTimeout;
64
65         @Arg(dest = "generate-config-address")
66         public String generateConfigsAddress;
67
68         @Arg(dest = "distro-folder")
69         public File distroFolder;
70
71         @Arg(dest = "generate-configs-batch-size")
72         public int generateConfigBatchSize;
73
74         @Arg(dest = "ssh")
75         public boolean ssh;
76
77         @Arg(dest = "exi")
78         public boolean exi;
79
80         @Arg(dest = "debug")
81         public boolean debug;
82
83         @Arg(dest = "notification-file")
84         public File notificationFile;
85
86         @Arg(dest = "md-sal")
87         public boolean mdSal;
88
89         @Arg(dest = "initial-config-xml-file")
90         public File initialConfigXMLFile;
91
92         static ArgumentParser getParser() {
93             final ArgumentParser parser = ArgumentParsers.newArgumentParser("netconf testool");
94
95             parser.description("Netconf device simulator. Detailed info can be found at https://wiki.opendaylight.org/view/OpenDaylight_Controller:Netconf:Testtool#Building_testtool");
96
97             parser.addArgument("--device-count")
98                     .type(Integer.class)
99                     .setDefault(1)
100                     .help("Number of simulated netconf devices to spin. This is the number of actual ports open for the devices.")
101                     .dest("devices-count");
102
103             parser.addArgument("--devices-per-port")
104                     .type(Integer.class)
105                     .setDefault(1)
106                     .help("Amount of config files generated per port to spoof more devices then are actually running")
107                     .dest("devices-per-port");
108
109             parser.addArgument("--schemas-dir")
110                     .type(File.class)
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");
113
114             parser.addArgument("--notification-file")
115                     .type(File.class)
116                     .help("Xml file containing notifications that should be sent to clients after create subscription is called")
117                     .dest("notification-file");
118
119             parser.addArgument("--initial-config-xml-file")
120                     .type(File.class)
121                     .help("Xml file containing initial simulatted configuration to be returned via get-config rpc")
122                     .dest("initial-config-xml-file");
123
124             parser.addArgument("--starting-port")
125                     .type(Integer.class)
126                     .setDefault(17830)
127                     .help("First port for simulated device. Each other device will have previous+1 port number")
128                     .dest("starting-port");
129
130             parser.addArgument("--generate-config-connection-timeout")
131                     .type(Integer.class)
132                     .setDefault((int)TimeUnit.MINUTES.toMillis(30))
133                     .help("Timeout to be generated in initial config files")
134                     .dest("generate-config-connection-timeout");
135
136             parser.addArgument("--generate-config-address")
137                     .type(String.class)
138                     .setDefault("127.0.0.1")
139                     .help("Address to be placed in generated configs")
140                     .dest("generate-config-address");
141
142             parser.addArgument("--generate-configs-batch-size")
143                     .type(Integer.class)
144                     .setDefault(4000)
145                     .help("Number of connector configs per generated file")
146                     .dest("generate-configs-batch-size");
147
148             parser.addArgument("--distribution-folder")
149                     .type(File.class)
150                     .help("Directory where the karaf distribution for controller is located")
151                     .dest("distro-folder");
152
153             parser.addArgument("--ssh")
154                     .type(Boolean.class)
155                     .setDefault(true)
156                     .help("Whether to use ssh for transport or just pure tcp")
157                     .dest("ssh");
158
159             parser.addArgument("--exi")
160                     .type(Boolean.class)
161                     .setDefault(true)
162                     .help("Whether to use exi to transport xml content")
163                     .dest("exi");
164
165             parser.addArgument("--debug")
166                     .type(Boolean.class)
167                     .setDefault(false)
168                     .help("Whether to use debug log level instead of INFO")
169                     .dest("debug");
170
171             parser.addArgument("--md-sal")
172                     .type(Boolean.class)
173                     .setDefault(false)
174                     .help("Whether to use md-sal datastore instead of default simulated datastore.")
175                     .dest("md-sal");
176
177             return parser;
178         }
179
180         void validate() {
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");
184
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");
189             }
190         }
191     }
192
193     public static void main(final String[] args) {
194         final Params params = parseArgs(args, Params.getParser());
195         params.validate();
196
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);
199
200         final NetconfDeviceSimulator netconfDeviceSimulator = new NetconfDeviceSimulator();
201         try {
202             final List<Integer> openDevices = netconfDeviceSimulator.start(params);
203             if (openDevices.size() == 0) {
204                 LOG.error("Failed to start any simulated devices, exiting...");
205                 System.exit(1);
206             }
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();
215             }
216         } catch (final Exception e) {
217             LOG.error("Unhandled exception", e);
218             netconfDeviceSimulator.close();
219             System.exit(1);
220         }
221
222         // Block main thread
223         synchronized (netconfDeviceSimulator) {
224             try {
225                 netconfDeviceSimulator.wait();
226             } catch (final InterruptedException e) {
227                 throw new RuntimeException(e);
228             }
229         }
230     }
231
232     private static Params parseArgs(final String[] args, final ArgumentParser parser) {
233         final Params opt = new Params();
234         try {
235             parser.parseArgs(args, opt);
236             return opt;
237         } catch (final ArgumentParserException e) {
238             parser.handleError(e);
239         }
240
241         System.exit(1);
242         return null;
243     }
244
245     private 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";
248
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/";
252
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";
255
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;
261
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;
268         }
269
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);
275             }
276
277             for (final File file : configDir.listFiles(new FileFilter() {
278                 @Override
279                 public boolean accept(final File pathname) {
280                     return !pathname.isDirectory() && pathname.getName().startsWith(SIM_DEVICE_CFG_PREFIX);
281                 }
282             })) {
283                 Preconditions.checkState(file.delete(), "Unable to clean previous generated file %s", file);
284             }
285
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));
289
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());
293
294                 int connectorCount = 0;
295                 Integer batchStart = null;
296                 StringBuilder b = new StringBuilder();
297                 b.append(before);
298
299                 final List<File> generatedConfigs = Lists.newArrayList();
300
301                 for (final Integer openDevice : openDevices) {
302                     if(batchStart == null) {
303                         batchStart = openDevice;
304                     }
305
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>");
310
311                         b.append(configContent);
312                         connectorCount++;
313                         if(connectorCount == batchSize) {
314                             b.append(after);
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);
318                             connectorCount = 0;
319                             b = new StringBuilder();
320                             b.append(before);
321                             batchStart = null;
322                         }
323                     }
324                 }
325
326                 // Write remaining
327                 if(connectorCount != 0) {
328                     b.append(after);
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);
332                 }
333
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);
338             }
339         }
340
341
342         public void updateFeatureFile(final List<File> generated) {
343             // TODO karaf core contains jaxb for feature files, use that for
344             // modification
345             try {
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();
350
351                     for (int i = 0; i < childNodes.getLength(); i++) {
352                         final Node item = childNodes.item(i);
353                         if (item instanceof Element == false) {
354                             continue;
355                         }
356                         if (item.getLocalName().equals("feature") == false) {
357                             continue;
358                         }
359
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());
368                             }
369                             for (final File file : generated) {
370                                 final Element configfile = document.createElement("configfile");
371                                 configfile.setTextContent("file:"
372                                         + ETC_OPENDAYLIGHT_KARAF_PATH
373                                         + file.getName());
374                                 configfile.setAttribute(
375                                         "finalname",
376                                         ETC_OPENDAYLIGHT_KARAF_PATH
377                                                 + file.getName());
378                                 ncAllFeatureDefinition.appendChild(configfile);
379                             }
380                         }
381                     }
382
383                     Files.write(XmlUtil.toString(document), featureFile,Charsets.UTF_8);
384                     LOG.info("Feature file {} updated", featureFile);
385                 }
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");
390             }
391         }
392
393
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));
396
397             final File systemDir = checkExistingDir(new File(distroFolder, "system"), String.format("Folder %s does not contain a karaf distro, folder system is missing", distroFolder));
398
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));
403             }
404
405             // Find newest version for features
406             final File newestVersionDir = Collections.max(
407                     Lists.newArrayList(netconfConnectorFeaturesParentDir.listFiles(new FileFilter() {
408                         @Override
409                         public boolean accept(final File pathname) {
410                             return pathname.isDirectory();
411                         }
412                     })), new Comparator<File>() {
413                         @Override
414                         public int compare(final File o1, final File o2) {
415                             return o1.getName().compareTo(o2.getName());
416                         }
417                     });
418
419             return Lists.newArrayList(newestVersionDir.listFiles(new FileFilter() {
420                 @Override
421                 public boolean accept(final File pathname) {
422                     return pathname.getName().contains(featureName)
423                             && Files.getFileExtension(pathname.getName()).equals(suffix);
424                 }
425             }));
426         }
427
428         private static File checkExistingDir(final File folder, final String msg) {
429             Preconditions.checkArgument(folder.exists(), msg);
430             Preconditions.checkArgument(folder.isDirectory(), msg);
431             return folder;
432         }
433
434         public void changeLoadOrder() {
435             try {
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);
440             }
441         }
442     }
443 }