PageRenderTime 31ms CodeModel.GetById 14ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 29package org.opennms.netmgt.provision.service.vmware;
 30
 31import java.io.File;
 32import java.io.InputStream;
 33import java.io.PrintWriter;
 34import java.net.URL;
 35import java.util.List;
 36
 37import org.apache.commons.cli.CommandLine;
 38import org.apache.commons.cli.CommandLineParser;
 39import org.apache.commons.cli.HelpFormatter;
 40import org.apache.commons.cli.Options;
 41import org.apache.commons.cli.PosixParser;
 42import org.apache.commons.io.IOUtils;
 43import org.opennms.core.utils.ConfigFileConstants;
 44import org.opennms.core.xml.JaxbUtils;
 45import org.opennms.netmgt.config.vmware.VmwareConfig;
 46import org.opennms.netmgt.config.vmware.VmwareServer;
 47import org.opennms.netmgt.provision.persist.requisition.Requisition;
 48
 49/**
 50 * The Class VmwareRequisitionTool
 51 * 
 52 * @author Alejandro Galue <agalue@opennms.org>
 53 */
 54public abstract class VmwareRequisitionTool {
 55
 56    public static void main(String[] args) throws Exception {
 57        final Options options = new Options();
 58
 59        final CommandLineParser parser = new PosixParser();
 60        final CommandLine cmd = parser.parse(options, args);
 61
 62        @SuppressWarnings("unchecked")
 63        List<String> arguments = (List<String>) cmd.getArgList();
 64
 65        if (arguments.size() < 1) {
 66            usage(options, cmd);
 67            System.exit(1);
 68        }
 69
 70        String urlString = arguments.remove(0).replaceFirst("vmware", "http"); // Internal trick to avoid confusions.
 71        URL url = new URL(urlString);
 72
 73        // Parse vmware-config.xml and retrieve the credentials to avoid initialize Spring
 74        if (url.getUserInfo() == null) {
 75            File cfg = new File(ConfigFileConstants.getFilePathString(), "vmware-config.xml");
 76            if (cfg.exists()) {
 77                String username = null;
 78                String password = null;
 79                VmwareConfig config = JaxbUtils.unmarshal(VmwareConfig.class, cfg);
 80                for (VmwareServer srv : config.getVmwareServerCollection()) {
 81                    if (srv.getHostname().equals(url.getHost())) {
 82                        username = srv.getUsername();
 83                        password = srv.getPassword();
 84                    }
 85                }
 86                if (username == null || password == null) {
 87                    throw new IllegalArgumentException("Can't retrieve credentials for " + url.getHost() + " from " + cfg);
 88                }
 89                int i = urlString.lastIndexOf("//");
 90                if (i > 0) {
 91                    urlString = urlString.substring(0, i + 2) + username + ':' + password + '@' + urlString.substring(i + 2);
 92                }
 93                url = new URL(urlString);
 94            }
 95        }
 96
 97        VmwareRequisitionUrlConnection c = new VmwareRequisitionUrlConnection(url) {
 98            @Override
 99            protected Requisition getExistingRequisition() {
100                // This is not elegant but it is necessary to avoid booting Spring
101                File req = new File(ConfigFileConstants.getFilePathString(), "imports" + File.separator + m_foreignSource + ".xml");
102                if (req.exists()) {
103                    return JaxbUtils.unmarshal(Requisition.class, req);
104                }
105                return null;
106            }
107        };
108        c.connect();
109        InputStream is = c.getInputStream();
110        if (is == null) {
111            System.err.println("Couldn't generate requisition from " +  urlString);
112            System.exit(1);
113        } else {
114            System.out.println(IOUtils.toString(is, "UTF-8"));
115        }
116    }
117
118    private static void usage(final Options options, final CommandLine cmd, final String error, final Exception e) {
119        final HelpFormatter formatter = new HelpFormatter();
120        final PrintWriter pw = new PrintWriter(System.out);
121        if (error != null) {
122            pw.println("An error occurred: " + error + "\n");
123        }
124        StringBuffer sb = new StringBuffer();
125        sb.append("Usage: VmwareRequisitionTool vmware://username:password@host[/foreign-source]?keyA=valueA;keyB=valueB;...\n");
126        sb.append(" Note: in case the credentials are not specified, they should exist on vmware.config.xml\n");
127
128        formatter.printHelp(sb.toString(), options);
129
130        if (e != null) {
131            pw.println(e.getMessage());
132            e.printStackTrace(pw);
133        }
134
135        pw.close();
136    }
137
138    private static void usage(final Options options, final CommandLine cmd) {
139        usage(options, cmd, null, null);
140    }
141
142}