Search in sources :

Example 21 with OnmsUpgradeException

use of org.opennms.upgrade.api.OnmsUpgradeException in project opennms by OpenNMS.

the class DiscoveryConfigurationMigratorOffline method execute.

@Override
public void execute() throws OnmsUpgradeException {
    Writer out = null;
    try {
        final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        final DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        final Document doc = docBuilder.parse(m_configFile);
        final NodeList found = doc.getElementsByTagName("discovery-configuration");
        if (found.getLength() == 1 && found.item(0) instanceof Element) {
            final Element el = (Element) found.item(0);
            el.removeAttribute("threads");
            final Transformer tf = TransformerFactory.newInstance().newTransformer();
            tf.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name());
            tf.setOutputProperty(OutputKeys.INDENT, "yes");
            out = new StringWriter();
            tf.transform(new DOMSource(doc), new StreamResult(out));
            FileUtils.write(m_configFile, out.toString());
        } else {
            throw new OnmsUpgradeException("Unsure how to handle XML node(s): " + found);
        }
    } catch (final IOException | SAXException | ParserConfigurationException | TransformerException e) {
        throw new OnmsUpgradeException("Failed to upgrade discovery-configuration.xml", e);
    } finally {
        IOUtils.closeQuietly(out);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) IOException(java.io.IOException) Document(org.w3c.dom.Document) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException) SAXException(org.xml.sax.SAXException) StringWriter(java.io.StringWriter) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) StringWriter(java.io.StringWriter) Writer(java.io.Writer) TransformerException(javax.xml.transform.TransformerException)

Example 22 with OnmsUpgradeException

use of org.opennms.upgrade.api.OnmsUpgradeException in project opennms by OpenNMS.

the class JmxRrdMigratorOffline method preExecute.

/* (non-Javadoc)
     * @see org.opennms.upgrade.api.OnmsUpgrade#preExecute()
     */
@Override
public void preExecute() throws OnmsUpgradeException {
    printMainSettings();
    if (isInstalledVersionGreaterOrEqual(1, 12, 2)) {
        try {
            jmxDataCollectionConfigDao.getConfig();
        } catch (Exception e) {
            throw new OnmsUpgradeException("Can't initialize jmx-datacollection-config.xml because " + e.getMessage());
        }
        for (File jmxResourceDir : getJmxResourceDirectories()) {
            log("Backing up %s\n", jmxResourceDir);
            zipDir(new File(jmxResourceDir.getAbsolutePath() + ZIP_EXT), jmxResourceDir);
        }
    } else {
        throw new OnmsUpgradeException("This upgrade procedure requires at least OpenNMS 1.12.2; the current version is " + getOpennmsVersion());
    }
}
Also used : File(java.io.File) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException) IOException(java.io.IOException) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException)

Example 23 with OnmsUpgradeException

use of org.opennms.upgrade.api.OnmsUpgradeException in project opennms by OpenNMS.

the class MonitoringLocationsMigratorOffline method execute.

@Override
public void execute() throws OnmsUpgradeException {
    if (monitoringLocationsConfig == null)
        return;
    log("Moving monitoring locations into the database...\n");
    long count = 0;
    try {
        Connection connection = null;
        final DBUtils dbUtils = new DBUtils(getClass());
        try {
            connection = DataSourceFactory.getInstance().getConnection();
            dbUtils.watch(connection);
            PreparedStatement insertLocation = connection.prepareStatement("INSERT INTO monitoringlocations (id, monitoringarea, geolocation, latitude, longitude, priority) VALUES (?,?,?,?,?,?)");
            PreparedStatement insertPollingPackage = connection.prepareStatement("INSERT INTO monitoringlocationspollingpackages (monitoringlocationid, packagename) VALUES (?,?)");
            PreparedStatement insertCollectionPackage = connection.prepareStatement("INSERT INTO monitoringlocationscollectionpackages (monitoringlocationid, packagename) VALUES (?,?)");
            PreparedStatement insertTag = connection.prepareStatement("INSERT INTO monitoringlocationstags (monitoringlocationid, tag) VALUES (?,?)");
            dbUtils.watch(insertLocation);
            dbUtils.watch(insertPollingPackage);
            dbUtils.watch(insertCollectionPackage);
            dbUtils.watch(insertTag);
            for (LocationDef location : monitoringLocationsConfig.getLocations()) {
                // id
                insertLocation.setString(1, location.getLocationName());
                // monitoringarea
                insertLocation.setString(2, location.getMonitoringArea());
                if (location.getGeolocation() != null && !"".equals(location.getGeolocation().trim())) {
                    // geolocation
                    insertLocation.setString(3, location.getGeolocation());
                } else {
                    insertLocation.setNull(3, Types.VARCHAR);
                }
                if (location.getCoordinates() != null && !"".equals(location.getCoordinates())) {
                    String[] latLong = location.getCoordinates().split(",");
                    if (latLong.length == 2) {
                        // latitude
                        insertLocation.setDouble(4, Double.valueOf(latLong[0]));
                        // longitude
                        insertLocation.setDouble(5, Double.valueOf(latLong[1]));
                    } else {
                        insertLocation.setNull(4, Types.DOUBLE);
                        insertLocation.setNull(5, Types.DOUBLE);
                    }
                } else {
                    insertLocation.setNull(4, Types.DOUBLE);
                    insertLocation.setNull(5, Types.DOUBLE);
                }
                if (location.getPriority() == null) {
                    // priority
                    insertLocation.setNull(6, Types.INTEGER);
                } else {
                    // priority
                    insertLocation.setLong(6, location.getPriority());
                }
                insertLocation.execute();
                count++;
                if (location.getPollingPackageName() != null && !"".equals(location.getPollingPackageName())) {
                    // monitoringlocationid
                    insertPollingPackage.setString(1, location.getLocationName());
                    // packagename
                    insertPollingPackage.setString(2, location.getPollingPackageName());
                    insertPollingPackage.execute();
                }
                if (location.getCollectionPackageName() != null && !"".equals(location.getCollectionPackageName())) {
                    // monitoringlocationid
                    insertCollectionPackage.setString(1, location.getLocationName());
                    // packagename
                    insertCollectionPackage.setString(2, location.getCollectionPackageName());
                    insertCollectionPackage.execute();
                }
                for (Tag tag : location.getTags()) {
                    if (tag.getName() != null && !"".equals(tag.getName().trim())) {
                        // monitoringlocationid
                        insertTag.setString(1, location.getLocationName());
                        // tag
                        insertTag.setString(2, tag.getName());
                        insertTag.execute();
                    }
                }
            }
        } finally {
            dbUtils.cleanUp();
        }
    } catch (Throwable e) {
        throw new OnmsUpgradeException("Can't fix services configuration because " + e.getMessage(), e);
    }
    log("Moved %d monitoring locations into the database\n", count);
}
Also used : LocationDef(org.opennms.upgrade.implementations.monitoringLocations16.LocationDef) Connection(java.sql.Connection) DBUtils(org.opennms.core.utils.DBUtils) PreparedStatement(java.sql.PreparedStatement) Tag(org.opennms.upgrade.implementations.monitoringLocations16.Tag) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException)

Example 24 with OnmsUpgradeException

use of org.opennms.upgrade.api.OnmsUpgradeException in project opennms by OpenNMS.

the class MonitoringLocationsMigratorOffline method preExecute.

@Override
public void preExecute() throws OnmsUpgradeException {
    if (monitoringLocationsConfig == null)
        return;
    try {
        log("Backing up %s\n", configFile);
        zipFile(configFile);
    } catch (Exception e) {
        throw new OnmsUpgradeException("Can't backup " + configFile + " because " + e.getMessage());
    }
}
Also used : OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException)

Example 25 with OnmsUpgradeException

use of org.opennms.upgrade.api.OnmsUpgradeException in project opennms by OpenNMS.

the class DataCollectionConfigMigrator17Offline method isConfigValid.

/**
 * Checks if is configuration valid.
 *
 * @return true, if is configuration valid
 * @throws OnmsUpgradeException the OpenNMS upgrade exception
 */
private boolean isConfigValid() throws OnmsUpgradeException {
    File configDirectory = new File(sourceFile.getParentFile().getAbsolutePath(), "datacollection");
    try {
        DefaultDataCollectionConfigDao dao = new DefaultDataCollectionConfigDao();
        dao.setConfigDirectory(configDirectory.getAbsolutePath());
        dao.setConfigResource(new FileSystemResource(sourceFile));
        dao.setReloadCheckInterval(new Long(0));
        dao.afterPropertiesSet();
    } catch (IllegalArgumentException e) {
        log("Found a problem: %s\n", e.getMessage());
        Matcher m = pattern.matcher(e.getMessage());
        if (m.find()) {
            try {
                Iterator<Path> paths = Files.list(configDirectory.toPath()).filter(f -> f.getFileName().toString().toLowerCase().endsWith(".xml")).iterator();
                for (; paths.hasNext(); ) {
                    String group = getGroupForResourceType(paths.next().toFile(), m.group(1));
                    if (group != null) {
                        updateDataCollectionConfig(m.group(2), group);
                        return false;
                    }
                }
            } catch (Exception ex) {
                throw new OnmsUpgradeException("Can't get datacollection-group files", ex);
            }
        }
    } catch (Exception e) {
        throw new OnmsUpgradeException("Can't process " + sourceFile, e);
    }
    return true;
}
Also used : Matcher(java.util.regex.Matcher) Iterator(java.util.Iterator) FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File) DefaultDataCollectionConfigDao(org.opennms.netmgt.config.DefaultDataCollectionConfigDao) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException) IOException(java.io.IOException) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException)

Aggregations

OnmsUpgradeException (org.opennms.upgrade.api.OnmsUpgradeException)31 IOException (java.io.IOException)23 File (java.io.File)14 FileWriter (java.io.FileWriter)8 StringWriter (java.io.StringWriter)6 ArrayList (java.util.ArrayList)5 Matcher (java.util.regex.Matcher)5 Map (java.util.Map)3 Pattern (java.util.regex.Pattern)3 LineIterator (org.apache.commons.io.LineIterator)3 DefaultDataCollectionConfigDao (org.opennms.netmgt.config.DefaultDataCollectionConfigDao)3 Service (org.opennms.netmgt.config.service.Service)3 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 Writer (java.io.Writer)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2