/integrations/opennms-vmware/src/main/java/org/opennms/netmgt/provision/service/vmware/VmwareRequisitionTool.java

https://github.com/ajakubo1/opennms · Java · 142 lines · 91 code · 17 blank · 34 comment · 19 complexity · 1451a3775a67fbd5d82070ae3ad218a7 MD5 · raw file

  1. /*******************************************************************************
  2. * This file is part of OpenNMS(R).
  3. *
  4. * Copyright (C) 2012 The OpenNMS Group, Inc.
  5. * OpenNMS(R) is Copyright (C) 1999-2012 The OpenNMS Group, Inc.
  6. *
  7. * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
  8. *
  9. * OpenNMS(R) is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published
  11. * by the Free Software Foundation, either version 3 of the License,
  12. * or (at your option) any later version.
  13. *
  14. * OpenNMS(R) is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with OpenNMS(R). If not, see:
  21. * http://www.gnu.org/licenses/
  22. *
  23. * For more information contact:
  24. * OpenNMS(R) Licensing <license@opennms.org>
  25. * http://www.opennms.org/
  26. * http://www.opennms.com/
  27. *******************************************************************************/
  28. package org.opennms.netmgt.provision.service.vmware;
  29. import java.io.File;
  30. import java.io.InputStream;
  31. import java.io.PrintWriter;
  32. import java.net.URL;
  33. import java.util.List;
  34. import org.apache.commons.cli.CommandLine;
  35. import org.apache.commons.cli.CommandLineParser;
  36. import org.apache.commons.cli.HelpFormatter;
  37. import org.apache.commons.cli.Options;
  38. import org.apache.commons.cli.PosixParser;
  39. import org.apache.commons.io.IOUtils;
  40. import org.opennms.core.utils.ConfigFileConstants;
  41. import org.opennms.core.xml.JaxbUtils;
  42. import org.opennms.netmgt.config.vmware.VmwareConfig;
  43. import org.opennms.netmgt.config.vmware.VmwareServer;
  44. import org.opennms.netmgt.provision.persist.requisition.Requisition;
  45. /**
  46. * The Class VmwareRequisitionTool
  47. *
  48. * @author Alejandro Galue <agalue@opennms.org>
  49. */
  50. public abstract class VmwareRequisitionTool {
  51. public static void main(String[] args) throws Exception {
  52. final Options options = new Options();
  53. final CommandLineParser parser = new PosixParser();
  54. final CommandLine cmd = parser.parse(options, args);
  55. @SuppressWarnings("unchecked")
  56. List<String> arguments = (List<String>) cmd.getArgList();
  57. if (arguments.size() < 1) {
  58. usage(options, cmd);
  59. System.exit(1);
  60. }
  61. String urlString = arguments.remove(0).replaceFirst("vmware", "http"); // Internal trick to avoid confusions.
  62. URL url = new URL(urlString);
  63. // Parse vmware-config.xml and retrieve the credentials to avoid initialize Spring
  64. if (url.getUserInfo() == null) {
  65. File cfg = new File(ConfigFileConstants.getFilePathString(), "vmware-config.xml");
  66. if (cfg.exists()) {
  67. String username = null;
  68. String password = null;
  69. VmwareConfig config = JaxbUtils.unmarshal(VmwareConfig.class, cfg);
  70. for (VmwareServer srv : config.getVmwareServerCollection()) {
  71. if (srv.getHostname().equals(url.getHost())) {
  72. username = srv.getUsername();
  73. password = srv.getPassword();
  74. }
  75. }
  76. if (username == null || password == null) {
  77. throw new IllegalArgumentException("Can't retrieve credentials for " + url.getHost() + " from " + cfg);
  78. }
  79. int i = urlString.lastIndexOf("//");
  80. if (i > 0) {
  81. urlString = urlString.substring(0, i + 2) + username + ':' + password + '@' + urlString.substring(i + 2);
  82. }
  83. url = new URL(urlString);
  84. }
  85. }
  86. VmwareRequisitionUrlConnection c = new VmwareRequisitionUrlConnection(url) {
  87. @Override
  88. protected Requisition getExistingRequisition() {
  89. // This is not elegant but it is necessary to avoid booting Spring
  90. File req = new File(ConfigFileConstants.getFilePathString(), "imports" + File.separator + m_foreignSource + ".xml");
  91. if (req.exists()) {
  92. return JaxbUtils.unmarshal(Requisition.class, req);
  93. }
  94. return null;
  95. }
  96. };
  97. c.connect();
  98. InputStream is = c.getInputStream();
  99. if (is == null) {
  100. System.err.println("Couldn't generate requisition from " + urlString);
  101. System.exit(1);
  102. } else {
  103. System.out.println(IOUtils.toString(is, "UTF-8"));
  104. }
  105. }
  106. private static void usage(final Options options, final CommandLine cmd, final String error, final Exception e) {
  107. final HelpFormatter formatter = new HelpFormatter();
  108. final PrintWriter pw = new PrintWriter(System.out);
  109. if (error != null) {
  110. pw.println("An error occurred: " + error + "\n");
  111. }
  112. StringBuffer sb = new StringBuffer();
  113. sb.append("Usage: VmwareRequisitionTool vmware://username:password@host[/foreign-source]?keyA=valueA;keyB=valueB;...\n");
  114. sb.append(" Note: in case the credentials are not specified, they should exist on vmware.config.xml\n");
  115. formatter.printHelp(sb.toString(), options);
  116. if (e != null) {
  117. pw.println(e.getMessage());
  118. e.printStackTrace(pw);
  119. }
  120. pw.close();
  121. }
  122. private static void usage(final Options options, final CommandLine cmd) {
  123. usage(options, cmd, null, null);
  124. }
  125. }