Bug-2674 Notification support for netconf testtool
[controller.git] / opendaylight / netconf / netconf-testtool / src / main / java / org / opendaylight / controller / 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.controller.netconf.test.tool;
10
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;
14
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;
22 import java.io.File;
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;
44
45 public final class Main {
46
47     private static final Logger LOG = LoggerFactory.getLogger(Main.class);
48
49     public static class Params {
50
51         @Arg(dest = "schemas-dir")
52         public File schemasDir;
53
54         @Arg(dest = "devices-count")
55         public int deviceCount;
56
57         @Arg(dest = "starting-port")
58         public int startingPort;
59
60         @Arg(dest = "generate-config-connection-timeout")
61         public int generateConfigsTimeout;
62
63         @Arg(dest = "generate-config-address")
64         public String generateConfigsAddress;
65
66         @Arg(dest = "distro-folder")
67         public File distroFolder;
68
69         @Arg(dest = "generate-configs-batch-size")
70         public int generateConfigBatchSize;
71
72         @Arg(dest = "ssh")
73         public boolean ssh;
74
75         @Arg(dest = "exi")
76         public boolean exi;
77
78         @Arg(dest = "debug")
79         public boolean debug;
80
81         @Arg(dest = "notification-file")
82         public File notificationFile;
83
84         static ArgumentParser getParser() {
85             final ArgumentParser parser = ArgumentParsers.newArgumentParser("netconf testool");
86
87             parser.description("Netconf device simulator. Detailed info can be found at https://wiki.opendaylight.org/view/OpenDaylight_Controller:Netconf:Testtool#Building_testtool");
88
89             parser.addArgument("--device-count")
90                     .type(Integer.class)
91                     .setDefault(1)
92                     .type(Integer.class)
93                     .help("Number of simulated netconf devices to spin")
94                     .dest("devices-count");
95
96             parser.addArgument("--schemas-dir")
97                     .type(File.class)
98                     .help("Directory containing yang schemas to describe simulated devices. Some schemas e.g. netconf monitoring and inet types are included by default")
99                     .dest("schemas-dir");
100
101             parser.addArgument("--notification-file")
102                     .type(File.class)
103                     .help("Xml file containing notifications that should be sent to clients after create subscription is called")
104                     .dest("notification-file");
105
106             parser.addArgument("--starting-port")
107                     .type(Integer.class)
108                     .setDefault(17830)
109                     .help("First port for simulated device. Each other device will have previous+1 port number")
110                     .dest("starting-port");
111
112             parser.addArgument("--generate-config-connection-timeout")
113                     .type(Integer.class)
114                     .setDefault((int)TimeUnit.MINUTES.toMillis(30))
115                     .help("Timeout to be generated in initial config files")
116                     .dest("generate-config-connection-timeout");
117
118             parser.addArgument("--generate-config-address")
119                     .type(String.class)
120                     .setDefault("127.0.0.1")
121                     .help("Address to be placed in generated configs")
122                     .dest("generate-config-address");
123
124             parser.addArgument("--generate-configs-batch-size")
125                     .type(Integer.class)
126                     .setDefault(4000)
127                     .help("Number of connector configs per generated file")
128                     .dest("generate-configs-batch-size");
129
130             parser.addArgument("--distribution-folder")
131                     .type(File.class)
132                     .help("Directory where the karaf distribution for controller is located")
133                     .dest("distro-folder");
134
135             parser.addArgument("--ssh")
136                     .type(Boolean.class)
137                     .setDefault(true)
138                     .help("Whether to use ssh for transport or just pure tcp")
139                     .dest("ssh");
140
141             parser.addArgument("--exi")
142                     .type(Boolean.class)
143                     .setDefault(true)
144                     .help("Whether to use exi to transport xml content")
145                     .dest("exi");
146
147             parser.addArgument("--debug")
148                     .type(Boolean.class)
149                     .setDefault(false)
150                     .help("Whether to use debug log level instead of INFO")
151                     .dest("debug");
152
153             return parser;
154         }
155
156         void validate() {
157             checkArgument(deviceCount > 0, "Device count has to be > 0");
158             checkArgument(startingPort > 1023, "Starting port has to be > 1023");
159
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");
164             }
165         }
166     }
167
168     public static void main(final String[] args) {
169         final Params params = parseArgs(args, Params.getParser());
170         params.validate();
171
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);
174
175         final NetconfDeviceSimulator netconfDeviceSimulator = new NetconfDeviceSimulator();
176         try {
177             final List<Integer> openDevices = netconfDeviceSimulator.start(params);
178             if (openDevices.size() == 0) {
179                 LOG.error("Failed to start any simulated devices, exiting...");
180                 System.exit(1);
181             }
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();
187             }
188         } catch (final Exception e) {
189             LOG.error("Unhandled exception", e);
190             netconfDeviceSimulator.close();
191             System.exit(1);
192         }
193
194         // Block main thread
195         synchronized (netconfDeviceSimulator) {
196             try {
197                 netconfDeviceSimulator.wait();
198             } catch (final InterruptedException e) {
199                 throw new RuntimeException(e);
200             }
201         }
202     }
203
204     private static Params parseArgs(final String[] args, final ArgumentParser parser) {
205         final Params opt = new Params();
206         try {
207             parser.parseArgs(args, opt);
208             return opt;
209         } catch (final ArgumentParserException e) {
210             parser.handleError(e);
211         }
212
213         System.exit(1);
214         return null;
215     }
216
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";
224
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/";
228
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";
231
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;
237
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;
244         }
245
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);
249             }
250
251             for (final File file : configDir.listFiles(new FileFilter() {
252                 @Override
253                 public boolean accept(final File pathname) {
254                     return !pathname.isDirectory() && pathname.getName().startsWith(SIM_DEVICE_CFG_PREFIX);
255                 }
256             })) {
257                 Preconditions.checkState(file.delete(), "Unable to clean previous generated file %s", file);
258             }
259
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));
263
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");
272
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());
276
277                 int connectorCount = 0;
278                 Integer batchStart = null;
279                 StringBuilder b = new StringBuilder();
280                 b.append(before);
281
282                 final List<File> generatedConfigs = Lists.newArrayList();
283
284                 for (final Integer openDevice : openDevices) {
285                     if(batchStart == null) {
286                         batchStart = openDevice;
287                     }
288
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>");
292
293                     b.append(configContent);
294                     connectorCount++;
295                     if(connectorCount == batchSize) {
296                         b.append(after);
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);
300                         connectorCount = 0;
301                         b = new StringBuilder();
302                         b.append(before);
303                         batchStart = null;
304                     }
305                 }
306
307                 // Write remaining
308                 if(connectorCount != 0) {
309                     b.append(after);
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);
313                 }
314
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);
319             }
320         }
321
322
323         public void updateFeatureFile(final List<File> generated) {
324             // TODO karaf core contains jaxb for feature files, use that for
325             // modification
326             try {
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();
331
332                     for (int i = 0; i < childNodes.getLength(); i++) {
333                         final Node item = childNodes.item(i);
334                         if (item instanceof Element == false) {
335                             continue;
336                         }
337                         if (item.getLocalName().equals("feature") == false) {
338                             continue;
339                         }
340
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());
349                             }
350                             for (final File file : generated) {
351                                 final Element configfile = document.createElement("configfile");
352                                 configfile.setTextContent("file:"
353                                         + ETC_OPENDAYLIGHT_KARAF_PATH
354                                         + file.getName());
355                                 configfile.setAttribute(
356                                         "finalname",
357                                         ETC_OPENDAYLIGHT_KARAF_PATH
358                                                 + file.getName());
359                                 ncAllFeatureDefinition.appendChild(configfile);
360                             }
361                         }
362                     }
363
364                     Files.write(XmlUtil.toString(document), featureFile,Charsets.UTF_8);
365                     LOG.info("Feature file {} updated", featureFile);
366                 }
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");
371             }
372         }
373
374
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));
377
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));
380
381             // Find newest version for features
382             final File newestVersionDir = Collections.max(
383                     Lists.newArrayList(netconfConnectorFeaturesParentDir.listFiles(new FileFilter() {
384                         @Override
385                         public boolean accept(final File pathname) {
386                             return pathname.isDirectory();
387                         }
388                     })), new Comparator<File>() {
389                         @Override
390                         public int compare(final File o1, final File o2) {
391                             return o1.getName().compareTo(o2.getName());
392                         }
393                     });
394
395             return Lists.newArrayList(newestVersionDir.listFiles(new FileFilter() {
396                 @Override
397                 public boolean accept(final File pathname) {
398                     return pathname.getName().contains(featureName)
399                             && Files.getFileExtension(pathname.getName()).equals(suffix);
400                 }
401             }));
402         }
403
404         private static File checkExistingDir(final File folder, final String msg) {
405             Preconditions.checkArgument(folder.exists(), msg);
406             Preconditions.checkArgument(folder.isDirectory(), msg);
407             return folder;
408         }
409
410         public void changeLoadOrder() {
411             try {
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);
416             }
417         }
418     }
419 }