- private static Params parseArgs(final String[] args, final ArgumentParser parser) {
- final Params opt = new Params();
- try {
- parser.parseArgs(args, opt);
- return opt;
- } catch (final ArgumentParserException e) {
- parser.handleError(e);
- }
-
- System.exit(1);
- return null;
- }
-
- static class ConfigGenerator {
- 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_";
- private static final String ETC_KARAF_PATH = "etc/";
- private static final String ETC_OPENDAYLIGHT_KARAF_PATH = ETC_KARAF_PATH + "opendaylight/karaf/";
-
- public static final String NETCONF_CONNECTOR_ALL_FEATURE = "odl-netconf-connector-all";
- private static final String ORG_OPS4J_PAX_URL_MVN_CFG = "org.ops4j.pax.url.mvn.cfg";
-
- private final File configDir;
- private final List<Integer> openDevices;
- private final List<File> ncFeatureFiles;
- private final File etcDir;
- private final File loadOrderCfgFile;
-
- public ConfigGenerator(final File directory, final List<Integer> openDevices) {
- 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.ncFeatureFiles = getFeatureFile(directory, "features-netconf-connector", "xml");
- this.openDevices = openDevices;
- }
-
- public List<File> generate(final boolean useSsh, final int batchSize,
- final int generateConfigsTimeout, final String address,
- final int devicesPerPort) {
- if(configDir.exists() == false) {
- Preconditions.checkState(configDir.mkdirs(), "Unable to create directory " + configDir);
- }
-
- for (final File file : configDir.listFiles(new FileFilter() {
- @Override
- public boolean accept(final File pathname) {
- return !pathname.isDirectory() && pathname.getName().startsWith(SIM_DEVICE_CFG_PREFIX);
- }
- })) {
- Preconditions.checkState(file.delete(), "Unable to clean previous generated file %s", file);
- }
-
- try(InputStream stream = Main.class.getResourceAsStream(NETCONF_CONNECTOR_XML)) {
- checkNotNull(stream, "Cannot load %s", NETCONF_CONNECTOR_XML);
- String configBlueprint = CharStreams.toString(new InputStreamReader(stream, Charsets.UTF_8));
-
- 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());
-
- int connectorCount = 0;
- Integer batchStart = null;
- StringBuilder b = new StringBuilder();
- b.append(before);
-
- final List<File> generatedConfigs = Lists.newArrayList();
-
- for (final Integer openDevice : openDevices) {
- if(batchStart == null) {
- batchStart = openDevice;
- }
-
- for (int i = 0; i < devicesPerPort; i++) {
- final String name = String.valueOf(openDevice) + SIM_DEVICE_SUFFIX + (i == 0 ? "" : "-" + String.valueOf(i));
- String configContent = String.format(middleBlueprint, name, address, String.valueOf(openDevice), String.valueOf(!useSsh));
- configContent = String.format("%s%s%d%s\n%s\n", configContent, "<connection-timeout-millis>", generateConfigsTimeout, "</connection-timeout-millis>", "</module>");
-
- b.append(configContent);
- connectorCount++;
- if(connectorCount == batchSize) {
- b.append(after);
- final File to = new File(configDir, String.format(SIM_DEVICE_CFG_PREFIX + "%d-%d.xml", batchStart, openDevice));
- generatedConfigs.add(to);
- Files.write(b.toString(), to, Charsets.UTF_8);
- connectorCount = 0;
- b = new StringBuilder();
- b.append(before);
- batchStart = null;
- }
- }
- }
-
- // Write remaining
- if(connectorCount != 0) {
- b.append(after);
- final File to = new File(configDir, String.format(SIM_DEVICE_CFG_PREFIX + "%d-%d.xml", batchStart, openDevices.get(openDevices.size() - 1)));
- generatedConfigs.add(to);
- Files.write(b.toString(), to, Charsets.UTF_8);
- }
-
- LOG.info("Config files generated in {}", configDir);
- return generatedConfigs;
- } catch (final IOException e) {
- throw new RuntimeException("Unable to generate config files", e);
- }
- }
-
-
- public void updateFeatureFile(final List<File> generated) {
- // TODO karaf core contains jaxb for feature files, use that for
- // modification
- try {
- 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;
- }
-
- 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), 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) {
- throw new RuntimeException("Unable to parse features file");
- }
- }
-
-
- 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));
-
- //check if beryllium path exists, if it doesnt check for lithium and fail/succeed after
- File netconfConnectorFeaturesParentDir = new File(systemDir, "org/opendaylight/netconf/" + featureName);
- if (!netconfConnectorFeaturesParentDir.exists() || !netconfConnectorFeaturesParentDir.isDirectory()) {
- netconfConnectorFeaturesParentDir = checkExistingDir(new File(systemDir, "org/opendaylight/controller/" + featureName), String.format("Karaf distro in %s does not contain netconf-connector features", distroFolder));
- }
-
- // Find newest version for features
- final File newestVersionDir = Collections.max(
- Lists.newArrayList(netconfConnectorFeaturesParentDir.listFiles(new FileFilter() {
- @Override
- public boolean accept(final File pathname) {
- return pathname.isDirectory();
- }
- })), new Comparator<File>() {
- @Override
- public int compare(final File o1, final File o2) {
- return o1.getName().compareTo(o2.getName());
- }
- });
-
- return Lists.newArrayList(newestVersionDir.listFiles(new FileFilter() {
- @Override
- public boolean accept(final File pathname) {
- return pathname.getName().contains(featureName)
- && Files.getFileExtension(pathname.getName()).equals(suffix);
- }
- }));
- }
-
- private static File checkExistingDir(final File folder, final String msg) {
- Preconditions.checkArgument(folder.exists(), msg);
- Preconditions.checkArgument(folder.isDirectory(), msg);
- return folder;
- }
-
- public void changeLoadOrder() {
- try {
- Files.write(ByteStreams.toByteArray(getClass().getResourceAsStream("/" +ORG_OPS4J_PAX_URL_MVN_CFG)), loadOrderCfgFile);
- LOG.info("Load order changed to prefer local bundles/features by rewriting file {}", loadOrderCfgFile);
- } catch (IOException e) {
- throw new RuntimeException("Unable to rewrite features file " + loadOrderCfgFile, e);
- }
- }