BUG-3335 Disable keepalives in netconf testtool
[controller.git] / opendaylight / netconf / netconf-testtool / src / main / java / org / opendaylight / controller / netconf / test / tool / Main.java
index 681b9a6a2d1e0915599a36eeb86035c8b2ba72bb..e273254e0ed3bc8520cdcaf910957b982958f3b9 100644 (file)
@@ -10,7 +10,6 @@ package org.opendaylight.controller.netconf.test.tool;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
-import static com.google.common.base.Preconditions.checkState;
 
 import ch.qos.logback.classic.Level;
 import com.google.common.base.Charsets;
@@ -46,7 +45,7 @@ public final class Main {
 
     private static final Logger LOG = LoggerFactory.getLogger(Main.class);
 
-    static class Params {
+    public static class Params {
 
         @Arg(dest = "schemas-dir")
         public File schemasDir;
@@ -78,6 +77,9 @@ public final class Main {
         @Arg(dest = "debug")
         public boolean debug;
 
+        @Arg(dest = "notification-file")
+        public File notificationFile;
+
         static ArgumentParser getParser() {
             final ArgumentParser parser = ArgumentParsers.newArgumentParser("netconf testool");
 
@@ -95,6 +97,11 @@ public final class Main {
                     .help("Directory containing yang schemas to describe simulated devices. Some schemas e.g. netconf monitoring and inet types are included by default")
                     .dest("schemas-dir");
 
+            parser.addArgument("--notification-file")
+                    .type(File.class)
+                    .help("Xml file containing notifications that should be sent to clients after create subscription is called")
+                    .dest("notification-file");
+
             parser.addArgument("--starting-port")
                     .type(Integer.class)
                     .setDefault(17830)
@@ -147,7 +154,7 @@ public final class Main {
 
         void validate() {
             checkArgument(deviceCount > 0, "Device count has to be > 0");
-            checkArgument(startingPort > 1024, "Starting port has to be > 1024");
+            checkArgument(startingPort > 1023, "Starting port has to be > 1023");
 
             if(schemasDir != null) {
                 checkArgument(schemasDir.exists(), "Schemas dir has to exist");
@@ -167,6 +174,10 @@ public final class Main {
         final NetconfDeviceSimulator netconfDeviceSimulator = new NetconfDeviceSimulator();
         try {
             final List<Integer> openDevices = netconfDeviceSimulator.start(params);
+            if (openDevices.size() == 0) {
+                LOG.error("Failed to start any simulated devices, exiting...");
+                System.exit(1);
+            }
             if(params.distroFolder != null) {
                 final ConfigGenerator configGenerator = new ConfigGenerator(params.distroFolder, openDevices);
                 final List<File> generated = configGenerator.generate(params.ssh, params.generateConfigBatchSize, params.generateConfigsTimeout, params.generateConfigsAddress);
@@ -203,11 +214,7 @@ public final class Main {
     }
 
     private static class ConfigGenerator {
-        public static final String NETCONF_CONNECTOR_XML = "/initial/99-netconf-connector.xml";
-        public static final String NETCONF_CONNECTOR_NAME = "controller-config";
-        public static final String NETCONF_CONNECTOR_PORT = "1830";
-        public static final String NETCONF_CONNECTOR_ADDRESS = "127.0.0.1";
-        public static final String NETCONF_USE_SSH = "false";
+        public static final String NETCONF_CONNECTOR_XML = "/99-netconf-connector-simulated.xml";
         public static final String SIM_DEVICE_SUFFIX = "-sim-device";
 
         private static final String SIM_DEVICE_CFG_PREFIX = "simulated-devices_";
@@ -219,7 +226,7 @@ public final class Main {
 
         private final File configDir;
         private final List<Integer> openDevices;
-        private final File ncFeatureFile;
+        private final List<File> ncFeatureFiles;
         private final File etcDir;
         private final File loadOrderCfgFile;
 
@@ -227,7 +234,7 @@ public final class Main {
             this.configDir = new File(directory, ETC_OPENDAYLIGHT_KARAF_PATH);
             this.etcDir = new File(directory, ETC_KARAF_PATH);
             this.loadOrderCfgFile = new File(etcDir, ORG_OPS4J_PAX_URL_MVN_CFG);
-            this.ncFeatureFile = getFeatureFile(directory, "features-netconf-connector");
+            this.ncFeatureFiles = getFeatureFile(directory, "features-netconf-connector", "xml");
             this.openDevices = openDevices;
         }
 
@@ -249,15 +256,6 @@ public final class Main {
                 checkNotNull(stream, "Cannot load %s", NETCONF_CONNECTOR_XML);
                 String configBlueprint = CharStreams.toString(new InputStreamReader(stream, Charsets.UTF_8));
 
-                checkState(configBlueprint.contains(NETCONF_CONNECTOR_NAME));
-                checkState(configBlueprint.contains(NETCONF_CONNECTOR_PORT));
-                checkState(configBlueprint.contains(NETCONF_USE_SSH));
-                checkState(configBlueprint.contains(NETCONF_CONNECTOR_ADDRESS));
-                configBlueprint = configBlueprint.replace(NETCONF_CONNECTOR_NAME, "%s");
-                configBlueprint = configBlueprint.replace(NETCONF_CONNECTOR_ADDRESS, "%s");
-                configBlueprint = configBlueprint.replace(NETCONF_CONNECTOR_PORT, "%s");
-                configBlueprint = configBlueprint.replace(NETCONF_USE_SSH, "%s");
-
                 final String before = configBlueprint.substring(0, configBlueprint.indexOf("<module>"));
                 final String middleBlueprint = configBlueprint.substring(configBlueprint.indexOf("<module>"), configBlueprint.indexOf("</module>"));
                 final String after = configBlueprint.substring(configBlueprint.indexOf("</module>") + "</module>".length());
@@ -309,37 +307,49 @@ public final class Main {
 
 
         public void updateFeatureFile(final List<File> generated) {
-            // TODO karaf core contains jaxb for feature files, use that for modification
+            // TODO karaf core contains jaxb for feature files, use that for
+            // modification
             try {
-                final Document document = XmlUtil.readXmlToDocument(Files.toString(ncFeatureFile, Charsets.UTF_8));
-                final NodeList childNodes = document.getDocumentElement().getChildNodes();
-
-                for (int i = 0; i < childNodes.getLength(); i++) {
-                    final Node item = childNodes.item(i);
-                    if(item instanceof Element == false) {
-                        continue;
-                    }
-                    if(item.getLocalName().equals("feature") ==false) {
-                        continue;
-                    }
-
-                    if(NETCONF_CONNECTOR_ALL_FEATURE.equals(((Element) item).getAttribute("name"))) {
-                        final Element ncAllFeatureDefinition = (Element) item;
-                        // Clean previous generated files
-                        for (final XmlElement configfile : XmlElement.fromDomElement(ncAllFeatureDefinition).getChildElements("configfile")) {
-                            ncAllFeatureDefinition.removeChild(configfile.getDomElement());
+                for (final File featureFile : ncFeatureFiles) {
+                    final Document document = XmlUtil.readXmlToDocument(Files
+                            .toString(featureFile, Charsets.UTF_8));
+                    final NodeList childNodes = document.getDocumentElement().getChildNodes();
+
+                    for (int i = 0; i < childNodes.getLength(); i++) {
+                        final Node item = childNodes.item(i);
+                        if (item instanceof Element == false) {
+                            continue;
+                        }
+                        if (item.getLocalName().equals("feature") == false) {
+                            continue;
                         }
-                        for (final File file : generated) {
-                            final Element configfile = document.createElement("configfile");
-                            configfile.setTextContent("file:" + ETC_OPENDAYLIGHT_KARAF_PATH + file.getName());
-                            configfile.setAttribute("finalname", ETC_OPENDAYLIGHT_KARAF_PATH + file.getName());
-                            ncAllFeatureDefinition.appendChild(configfile);
+
+                        if (NETCONF_CONNECTOR_ALL_FEATURE
+                                .equals(((Element) item).getAttribute("name"))) {
+                            final Element ncAllFeatureDefinition = (Element) item;
+                            // Clean previous generated files
+                            for (final XmlElement configfile : XmlElement
+                                    .fromDomElement(ncAllFeatureDefinition)
+                                    .getChildElements("configfile")) {
+                                ncAllFeatureDefinition.removeChild(configfile.getDomElement());
+                            }
+                            for (final File file : generated) {
+                                final Element configfile = document.createElement("configfile");
+                                configfile.setTextContent("file:"
+                                        + ETC_OPENDAYLIGHT_KARAF_PATH
+                                        + file.getName());
+                                configfile.setAttribute(
+                                        "finalname",
+                                        ETC_OPENDAYLIGHT_KARAF_PATH
+                                                + file.getName());
+                                ncAllFeatureDefinition.appendChild(configfile);
+                            }
                         }
                     }
-                }
 
-                Files.write(XmlUtil.toString(document), ncFeatureFile, Charsets.UTF_8);
-                LOG.info("Feature file {} updated", ncFeatureFile);
+                    Files.write(XmlUtil.toString(document), featureFile,Charsets.UTF_8);
+                    LOG.info("Feature file {} updated", featureFile);
+                }
             } catch (final IOException e) {
                 throw new RuntimeException("Unable to load features file as a resource");
             } catch (final SAXException e) {
@@ -348,7 +358,7 @@ public final class Main {
         }
 
 
-        private static File getFeatureFile(final File distroFolder, final String featureName) {
+        private static List<File> getFeatureFile(final File distroFolder, final String featureName, final String suffix) {
             checkExistingDir(distroFolder, String.format("Folder %s does not exist", distroFolder));
 
             final File systemDir = checkExistingDir(new File(distroFolder, "system"), String.format("Folder %s does not contain a karaf distro, folder system is missing", distroFolder));
@@ -368,12 +378,13 @@ public final class Main {
                         }
                     });
 
-            return newestVersionDir.listFiles(new FileFilter() {
+            return Lists.newArrayList(newestVersionDir.listFiles(new FileFilter() {
                 @Override
                 public boolean accept(final File pathname) {
-                    return pathname.getName().contains(featureName);
+                    return pathname.getName().contains(featureName)
+                            && Files.getFileExtension(pathname.getName()).equals(suffix);
                 }
-            })[0];
+            }));
         }
 
         private static File checkExistingDir(final File folder, final String msg) {