Search in sources :

Example 11 with OnmsUpgradeException

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

the class EventParameterMigratorOffline method execute.

@Override
public void execute() throws OnmsUpgradeException {
    long eventCount = 0, parameterCount = 0;
    try (final Connection connection = DataSourceFactory.getInstance().getConnection()) {
        connection.setAutoCommit(false);
        do {
            try (final Statement selectStatement = connection.createStatement();
                final ResultSet resultSet = selectStatement.executeQuery("SELECT eventid, eventparms FROM events WHERE eventparms IS NOT NULL LIMIT " + BATCH_SIZE)) {
                if (!resultSet.next()) {
                    break;
                }
                try (final PreparedStatement insertStatement = connection.prepareStatement("INSERT INTO event_parameters (eventid, name, value, type) VALUES  (?,?,?,?)");
                    final PreparedStatement nullifyStatement = connection.prepareStatement("UPDATE events SET eventparms=NULL WHERE eventid=?")) {
                    do {
                        final Integer eventId = resultSet.getInt("eventid");
                        final String eventParms = resultSet.getString("eventparms");
                        final List<Parm> parmList = EventParameterUtils.decode(eventParms);
                        if (parmList != null) {
                            final Map<String, Parm> parmMap = EventParameterUtils.normalize(parmList);
                            for (Map.Entry<String, Parm> entry : parmMap.entrySet()) {
                                insertStatement.setInt(1, eventId);
                                insertStatement.setString(2, entry.getValue().getParmName());
                                insertStatement.setString(3, entry.getValue().getValue().getContent());
                                insertStatement.setString(4, entry.getValue().getValue().getType());
                                insertStatement.execute();
                                parameterCount++;
                            }
                        }
                        nullifyStatement.setInt(1, eventId);
                        nullifyStatement.execute();
                        eventCount++;
                    } while (resultSet.next());
                    log("Processed %d eventparms entries, %d event parameters inserted...\n", eventCount, parameterCount);
                    connection.commit();
                } catch (SQLException e) {
                    connection.rollback();
                    connection.setAutoCommit(true);
                    throw e;
                }
            }
        } while (true);
        connection.setAutoCommit(true);
        log("Rows migrated. Dropping column 'eventparms'...\n");
        final Statement postMigrationStatement = connection.createStatement();
        postMigrationStatement.execute("ALTER TABLE events DROP COLUMN eventparms");
    } catch (Throwable e) {
        throw new OnmsUpgradeException("Can't move event parameters to table: " + e.getMessage(), e);
    }
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) Parm(org.opennms.netmgt.xml.event.Parm) PreparedStatement(java.sql.PreparedStatement) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException) ResultSet(java.sql.ResultSet) Map(java.util.Map)

Example 12 with OnmsUpgradeException

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

the class Upgrade method getUpgradeObjects.

/**
 * Gets the upgrade objects.
 *
 * @return the upgrade objects
 * @throws OnmsUpgradeException the OpenNMS upgrade exception
 */
protected List<OnmsUpgrade> getUpgradeObjects() throws OnmsUpgradeException {
    List<OnmsUpgrade> upgrades = new ArrayList<>();
    try {
        ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true);
        provider.addIncludeFilter(new AssignableTypeFilter(OnmsUpgrade.class));
        Set<BeanDefinition> components = provider.findCandidateComponents(getClassScope());
        for (BeanDefinition component : components) {
            if (component.isAbstract()) {
                continue;
            }
            Class<?> cls = Class.forName(component.getBeanClassName());
            if (cls.getAnnotation(Ignore.class) != null) {
                continue;
            }
            OnmsUpgrade upgrade = (OnmsUpgrade) cls.newInstance();
            upgrades.add(upgrade);
            log("Found upgrade task %s\n", upgrade.getId());
        }
        Collections.sort(upgrades, new OnmsUpgradeComparator());
    } catch (Exception e) {
        throw new OnmsUpgradeException("  Can't find the upgrade classes because: " + e.getMessage(), e);
    }
    return upgrades;
}
Also used : Ignore(org.opennms.upgrade.api.Ignore) ClassPathScanningCandidateComponentProvider(org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider) ArrayList(java.util.ArrayList) OnmsUpgrade(org.opennms.upgrade.api.OnmsUpgrade) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) OnmsUpgradeComparator(org.opennms.upgrade.api.OnmsUpgradeComparator) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException) AssignableTypeFilter(org.springframework.core.type.filter.AssignableTypeFilter)

Example 13 with OnmsUpgradeException

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

the class DataCollectionConfigMigrator17OfflineTest method canFix.

/**
 * Can fix.
 *
 * @throws OnmsUpgradeException the OpenNMS upgrade exception
 * @throws IOException Signals that an I/O exception has occurred.
 */
@Test
public void canFix() throws OnmsUpgradeException, IOException {
    DataCollectionConfigMigrator17Offline task = new DataCollectionConfigMigrator17Offline();
    task.preExecute();
    task.execute();
    task.postExecute();
    DatacollectionConfig config = JaxbUtils.unmarshal(DatacollectionConfig.class, new File(tempFolder.getRoot(), "etc/datacollection-config.xml"));
    Assert.assertNotNull(config);
    config.getSnmpCollections().forEach(s -> {
        Assert.assertTrue(s.getIncludeCollections().stream().filter(i -> i.getDataCollectionGroup().equals("SNMP-Informant")).findFirst().isPresent());
    });
}
Also used : DatacollectionConfig(org.opennms.netmgt.config.datacollection.DatacollectionConfig) Rule(org.junit.Rule) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException) JaxbUtils(org.opennms.core.xml.JaxbUtils) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) Assert(org.junit.Assert) File(java.io.File) TemporaryFolder(org.junit.rules.TemporaryFolder) Before(org.junit.Before) DatacollectionConfig(org.opennms.netmgt.config.datacollection.DatacollectionConfig) File(java.io.File) Test(org.junit.Test)

Example 14 with OnmsUpgradeException

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

the class SnmpInterfaceRrdMigratorOnline method preExecute.

/* (non-Javadoc)
     * @see org.opennms.upgrade.api.OnmsUpgrade#preExecute()
     */
public void preExecute() throws OnmsUpgradeException {
    printMainSettings();
    if (getRrdExtension() == null) {
        throw new OnmsUpgradeException("Can't find the configured extension for JRB/RRD.");
    }
    try {
        // Manually initialization of the DataCollectionConfigDao to avoid bootstrap Spring Framework and create a new connection pool.
        File cfgFile = ConfigFileConstants.getFile(ConfigFileConstants.DATA_COLLECTION_CONF_FILE_NAME);
        DefaultDataCollectionConfigDao config = new DefaultDataCollectionConfigDao();
        config.setConfigResource(new FileSystemResource(cfgFile));
        config.afterPropertiesSet();
        config.getConfiguredResourceTypes();
        DataCollectionConfigFactory.setInstance(config);
    } catch (Exception e) {
        throw new OnmsUpgradeException("Can't initialize datacollection-config.xml because " + e.getMessage());
    }
    interfacesToMerge = getInterfacesToMerge();
}
Also used : FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException) DefaultDataCollectionConfigDao(org.opennms.netmgt.config.DefaultDataCollectionConfigDao) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException) IOException(java.io.IOException)

Example 15 with OnmsUpgradeException

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

the class EOLServiceConfigMigratorOffline method execute.

/* (non-Javadoc)
     * @see org.opennms.upgrade.api.OnmsUpgrade#execute()
     */
@Override
public void execute() throws OnmsUpgradeException {
    final String[] eol = { "OpenNMS:Name=Linkd", "OpenNMS:Name=Xmlrpcd", "OpenNMS:Name=XmlrpcProvisioner", "OpenNMS:Name=AccessPointMonitor" };
    try {
        final ServiceConfiguration currentCfg = JaxbUtils.unmarshal(ServiceConfiguration.class, configFile);
        // Remove any end-of-life'd daemons from service configuration
        for (final String serviceName : eol) {
            final Service eolService = getService(currentCfg, serviceName);
            if (eolService == null) {
                continue;
            }
            final String eolServiceName = eolService.getName();
            if (eolServiceName.equals(serviceName)) {
                final String displayName = serviceName.replace("OpenNMS:Name=", "");
                log("Disabling EOL service: " + displayName + "\n");
                eolService.setEnabled(false);
            }
        }
        final StringWriter sw = new StringWriter();
        sw.write("<?xml version=\"1.0\"?>\n");
        sw.write("<!-- NOTE!!!!!!!!!!!!!!!!!!!\n");
        sw.write("The order in which these services are specified is important - for example, Eventd\n");
        sw.write("will need to come up last so that none of the event topic subcribers loose any event.\n");
        sw.write("\nWhen splitting services to run on mutiple VMs, the order of the services should be\n");
        sw.write("maintained\n");
        sw.write("-->\n");
        JaxbUtils.marshal(currentCfg, sw);
        final FileWriter fw = new FileWriter(configFile);
        fw.write(sw.toString());
        fw.close();
    } catch (final Exception e) {
        throw new OnmsUpgradeException("Can't fix services configuration because " + e.getMessage(), e);
    }
}
Also used : ServiceConfiguration(org.opennms.netmgt.config.service.ServiceConfiguration) StringWriter(java.io.StringWriter) FileWriter(java.io.FileWriter) Service(org.opennms.netmgt.config.service.Service) 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