Search in sources :

Example 1 with InetAddressComparator

use of org.opennms.core.utils.InetAddressComparator in project opennms by OpenNMS.

the class JdbcFilterDao method getIPAddressServiceMap.

/**
 * {@inheritDoc}
 */
@Override
public Map<InetAddress, Set<String>> getIPAddressServiceMap(final String rule) throws FilterParseException {
    final Map<InetAddress, Set<String>> ipServices = new TreeMap<InetAddress, Set<String>>(new InetAddressComparator());
    String sqlString;
    LOG.debug("Filter.getIPAddressServiceMap({})", rule);
    // get the database connection
    Connection conn = null;
    final DBUtils d = new DBUtils(getClass());
    try {
        conn = getDataSource().getConnection();
        d.watch(conn);
        // parse the rule and get the sql select statement
        sqlString = getIPServiceMappingStatement(rule);
        LOG.debug("Filter.getIPAddressServiceMap({}): SQL statement: {}", rule, sqlString);
        // execute query
        final Statement stmt = conn.createStatement();
        d.watch(stmt);
        final ResultSet rset = stmt.executeQuery(sqlString);
        d.watch(rset);
        // fill up the array list if the result set has values
        if (rset != null) {
            // Iterate through the result and build the array list
            while (rset.next()) {
                final InetAddress ipaddr = addr(rset.getString(1));
                if (ipaddr != null) {
                    if (!ipServices.containsKey(ipaddr)) {
                        ipServices.put(ipaddr, new TreeSet<String>());
                    }
                    ipServices.get(ipaddr).add(rset.getString(2));
                }
            }
        }
    } catch (final FilterParseException e) {
        LOG.warn("Filter Parse Exception occurred getting IP Service List.", e);
        throw new FilterParseException("Filter Parse Exception occurred getting IP Service List: " + e.getLocalizedMessage(), e);
    } catch (final SQLException e) {
        LOG.warn("SQL Exception occurred getting IP Service List.", e);
        throw new FilterParseException("SQL Exception occurred getting IP Service List: " + e.getLocalizedMessage(), e);
    } catch (final RuntimeException e) {
        LOG.error("Unexpected exception getting database connection.", e);
        throw e;
    } catch (final Error e) {
        LOG.error("Unexpected exception getting database connection.", e);
        throw e;
    } finally {
        d.cleanUp();
    }
    return ipServices;
}
Also used : TreeSet(java.util.TreeSet) ResultSet(java.sql.ResultSet) Set(java.util.Set) FilterParseException(org.opennms.netmgt.filter.api.FilterParseException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) TreeMap(java.util.TreeMap) InetAddressComparator(org.opennms.core.utils.InetAddressComparator) DBUtils(org.opennms.core.utils.DBUtils) ResultSet(java.sql.ResultSet) InetAddress(java.net.InetAddress)

Example 2 with InetAddressComparator

use of org.opennms.core.utils.InetAddressComparator in project opennms by OpenNMS.

the class WmiPeerFactory method optimize.

/**
 * Combine specific and range elements so that WMIPeerFactory has to spend
 * less time iterating all these elements.
 * TODO This really should be pulled up into PeerFactory somehow, but I'm not sure how (given that "Definition" is different for both
 * SNMP and WMI.  Maybe some sort of visitor methodology would work.  The basic logic should be fine as it's all IP address manipulation
 *
 * Calls should be preceded by a getWriteLock().lock() and wrapped in a try / finally block with getWriteLock().unlock().
 * @throws UnknownHostException
 */
public void optimize() throws UnknownHostException {
    // First pass: Remove empty definition elements
    for (Iterator<Definition> definitionsIterator = getConfig().getDefinitions().iterator(); definitionsIterator.hasNext(); ) {
        final Definition definition = definitionsIterator.next();
        if (definition.getSpecifics().size() == 0 && definition.getRanges().size() == 0) {
            LOG.debug("optimize: Removing empty definition element");
            definitionsIterator.remove();
        }
    }
    // Second pass: Replace single IP range elements with specific elements
    for (Definition definition : getConfig().getDefinitions()) {
        synchronized (definition) {
            for (Iterator<Range> rangesIterator = definition.getRanges().iterator(); rangesIterator.hasNext(); ) {
                Range range = rangesIterator.next();
                if (range.getBegin().equals(range.getEnd())) {
                    definition.addSpecific(range.getBegin());
                    rangesIterator.remove();
                }
            }
        }
    }
    // readability and then combine them into fewer elements where possible
    for (Iterator<Definition> defIterator = getConfig().getDefinitions().iterator(); defIterator.hasNext(); ) {
        Definition definition = defIterator.next();
        // Sort specifics
        final TreeMap<InetAddress, String> specificsMap = new TreeMap<InetAddress, String>(new InetAddressComparator());
        for (String specific : definition.getSpecifics()) {
            specificsMap.put(InetAddressUtils.getInetAddress(specific), specific.trim());
        }
        // Sort ranges
        final TreeMap<InetAddress, Range> rangesMap = new TreeMap<InetAddress, Range>(new InetAddressComparator());
        for (Range range : definition.getRanges()) {
            rangesMap.put(InetAddressUtils.getInetAddress(range.getBegin()), range);
        }
        // Combine consecutive specifics into ranges
        InetAddress priorSpecific = null;
        Range addedRange = null;
        for (final InetAddress specific : specificsMap.keySet()) {
            if (priorSpecific == null) {
                priorSpecific = specific;
                continue;
            }
            if (BigInteger.ONE.equals(InetAddressUtils.difference(specific, priorSpecific)) && InetAddressUtils.inSameScope(specific, priorSpecific)) {
                if (addedRange == null) {
                    addedRange = new Range();
                    addedRange.setBegin(InetAddressUtils.toIpAddrString(priorSpecific));
                    rangesMap.put(priorSpecific, addedRange);
                    specificsMap.remove(priorSpecific);
                }
                addedRange.setEnd(InetAddressUtils.toIpAddrString(specific));
                specificsMap.remove(specific);
            } else {
                addedRange = null;
            }
            priorSpecific = specific;
        }
        // Move specifics to ranges
        for (final InetAddress specific : new ArrayList<InetAddress>(specificsMap.keySet())) {
            for (final InetAddress begin : new ArrayList<InetAddress>(rangesMap.keySet())) {
                if (!InetAddressUtils.inSameScope(begin, specific)) {
                    continue;
                }
                if (InetAddressUtils.toInteger(begin).subtract(BigInteger.ONE).compareTo(InetAddressUtils.toInteger(specific)) > 0) {
                    continue;
                }
                Range range = rangesMap.get(begin);
                final InetAddress end = InetAddressUtils.getInetAddress(range.getEnd());
                if (InetAddressUtils.toInteger(end).add(BigInteger.ONE).compareTo(InetAddressUtils.toInteger(specific)) < 0) {
                    continue;
                }
                if (InetAddressUtils.toInteger(specific).compareTo(InetAddressUtils.toInteger(begin)) >= 0 && InetAddressUtils.toInteger(specific).compareTo(InetAddressUtils.toInteger(end)) <= 0) {
                    specificsMap.remove(specific);
                    break;
                }
                if (InetAddressUtils.toInteger(begin).subtract(BigInteger.ONE).equals(InetAddressUtils.toInteger(specific))) {
                    rangesMap.remove(begin);
                    rangesMap.put(specific, range);
                    range.setBegin(InetAddressUtils.toIpAddrString(specific));
                    specificsMap.remove(specific);
                    break;
                }
                if (InetAddressUtils.toInteger(end).add(BigInteger.ONE).equals(InetAddressUtils.toInteger(specific))) {
                    range.setEnd(InetAddressUtils.toIpAddrString(specific));
                    specificsMap.remove(specific);
                    break;
                }
            }
        }
        // Combine consecutive ranges
        Range priorRange = null;
        InetAddress priorBegin = null;
        InetAddress priorEnd = null;
        for (final Iterator<InetAddress> rangesIterator = rangesMap.keySet().iterator(); rangesIterator.hasNext(); ) {
            final InetAddress beginAddress = rangesIterator.next();
            final Range range = rangesMap.get(beginAddress);
            final InetAddress endAddress = InetAddressUtils.getInetAddress(range.getEnd());
            if (priorRange != null) {
                if (InetAddressUtils.inSameScope(beginAddress, priorEnd) && InetAddressUtils.difference(beginAddress, priorEnd).compareTo(BigInteger.ONE) <= 0) {
                    priorBegin = new InetAddressComparator().compare(priorBegin, beginAddress) < 0 ? priorBegin : beginAddress;
                    priorRange.setBegin(InetAddressUtils.toIpAddrString(priorBegin));
                    priorEnd = new InetAddressComparator().compare(priorEnd, endAddress) > 0 ? priorEnd : endAddress;
                    priorRange.setEnd(InetAddressUtils.toIpAddrString(priorEnd));
                    rangesIterator.remove();
                    continue;
                }
            }
            priorRange = range;
            priorBegin = beginAddress;
            priorEnd = endAddress;
        }
        // Update changes made to sorted maps
        definition.setSpecifics(new ArrayList<>(specificsMap.values()));
        definition.setRanges(new ArrayList<>(rangesMap.values()));
    }
}
Also used : Definition(org.opennms.netmgt.config.wmi.agent.Definition) ArrayList(java.util.ArrayList) Range(org.opennms.netmgt.config.wmi.agent.Range) TreeMap(java.util.TreeMap) InetAddressComparator(org.opennms.core.utils.InetAddressComparator) InetAddress(java.net.InetAddress)

Example 3 with InetAddressComparator

use of org.opennms.core.utils.InetAddressComparator in project opennms by OpenNMS.

the class AmiPeerFactory method optimize.

/**
 * Combine specific and range elements so that AMIPeerFactory has to spend
 * less time iterating all these elements.
 * TODO This really should be pulled up into PeerFactory somehow, but I'm not sure how (given that "Definition" is different for both
 * SNMP and AMI.  Maybe some sort of visitor methodology would work.  The basic logic should be fine as it's all IP address manipulation
 *
 * @throws UnknownHostException
 */
void optimize() throws UnknownHostException {
    getWriteLock().lock();
    try {
        // First pass: Remove empty definition elements
        for (final Iterator<Definition> definitionsIterator = m_config.getDefinitions().iterator(); definitionsIterator.hasNext(); ) {
            final Definition definition = definitionsIterator.next();
            if (definition.getSpecifics().size() == 0 && definition.getRanges().size() == 0) {
                LOG.debug("optimize: Removing empty definition element");
                definitionsIterator.remove();
            }
        }
        // Second pass: Replace single IP range elements with specific elements
        for (Definition definition : m_config.getDefinitions()) {
            for (Iterator<Range> rangesIterator = definition.getRanges().iterator(); rangesIterator.hasNext(); ) {
                Range range = rangesIterator.next();
                if (range.getBegin().equals(range.getEnd())) {
                    definition.addSpecific(range.getBegin());
                    rangesIterator.remove();
                }
            }
        }
        // readability and then combine them into fewer elements where possible
        for (final Definition definition : m_config.getDefinitions()) {
            // Sort specifics
            final TreeMap<InetAddress, String> specificsMap = new TreeMap<InetAddress, String>(new InetAddressComparator());
            for (final String specific : definition.getSpecifics()) {
                specificsMap.put(InetAddressUtils.getInetAddress(specific), specific.trim());
            }
            // Sort ranges
            final TreeMap<InetAddress, Range> rangesMap = new TreeMap<InetAddress, Range>(new InetAddressComparator());
            for (final Range range : definition.getRanges()) {
                rangesMap.put(InetAddressUtils.getInetAddress(range.getBegin()), range);
            }
            // Combine consecutive specifics into ranges
            InetAddress priorSpecific = null;
            Range addedRange = null;
            for (final InetAddress specific : specificsMap.keySet()) {
                if (priorSpecific == null) {
                    priorSpecific = specific;
                    continue;
                }
                if (BigInteger.ONE.equals(InetAddressUtils.difference(specific, priorSpecific)) && InetAddressUtils.inSameScope(specific, priorSpecific)) {
                    if (addedRange == null) {
                        addedRange = new Range();
                        addedRange.setBegin(InetAddressUtils.toIpAddrString(priorSpecific));
                        rangesMap.put(priorSpecific, addedRange);
                        specificsMap.remove(priorSpecific);
                    }
                    addedRange.setEnd(InetAddressUtils.toIpAddrString(specific));
                    specificsMap.remove(specific);
                } else {
                    addedRange = null;
                }
                priorSpecific = specific;
            }
            // Move specifics to ranges
            for (final InetAddress specific : new ArrayList<InetAddress>(specificsMap.keySet())) {
                for (final InetAddress begin : new ArrayList<InetAddress>(rangesMap.keySet())) {
                    if (!InetAddressUtils.inSameScope(begin, specific)) {
                        continue;
                    }
                    if (InetAddressUtils.toInteger(begin).subtract(BigInteger.ONE).compareTo(InetAddressUtils.toInteger(specific)) > 0) {
                        continue;
                    }
                    final Range range = rangesMap.get(begin);
                    final InetAddress end = InetAddressUtils.getInetAddress(range.getEnd());
                    if (InetAddressUtils.toInteger(end).add(BigInteger.ONE).compareTo(InetAddressUtils.toInteger(specific)) < 0) {
                        continue;
                    }
                    if (InetAddressUtils.toInteger(specific).compareTo(InetAddressUtils.toInteger(begin)) >= 0 && InetAddressUtils.toInteger(specific).compareTo(InetAddressUtils.toInteger(end)) <= 0) {
                        specificsMap.remove(specific);
                        break;
                    }
                    if (InetAddressUtils.toInteger(begin).subtract(BigInteger.ONE).equals(InetAddressUtils.toInteger(specific))) {
                        rangesMap.remove(begin);
                        rangesMap.put(specific, range);
                        range.setBegin(InetAddressUtils.toIpAddrString(specific));
                        specificsMap.remove(specific);
                        break;
                    }
                    if (InetAddressUtils.toInteger(end).add(BigInteger.ONE).equals(InetAddressUtils.toInteger(specific))) {
                        range.setEnd(InetAddressUtils.toIpAddrString(specific));
                        specificsMap.remove(specific);
                        break;
                    }
                }
            }
            // Combine consecutive ranges
            Range priorRange = null;
            InetAddress priorBegin = null;
            InetAddress priorEnd = null;
            for (final Iterator<InetAddress> rangesIterator = rangesMap.keySet().iterator(); rangesIterator.hasNext(); ) {
                final InetAddress beginAddress = rangesIterator.next();
                final Range range = rangesMap.get(beginAddress);
                final InetAddress endAddress = InetAddressUtils.getInetAddress(range.getEnd());
                if (priorRange != null) {
                    if (InetAddressUtils.inSameScope(beginAddress, priorEnd) && InetAddressUtils.difference(beginAddress, priorEnd).compareTo(BigInteger.ONE) <= 0) {
                        priorBegin = new InetAddressComparator().compare(priorBegin, beginAddress) < 0 ? priorBegin : beginAddress;
                        priorRange.setBegin(InetAddressUtils.toIpAddrString(priorBegin));
                        priorEnd = new InetAddressComparator().compare(priorEnd, endAddress) > 0 ? priorEnd : endAddress;
                        priorRange.setEnd(InetAddressUtils.toIpAddrString(priorEnd));
                        rangesIterator.remove();
                        continue;
                    }
                }
                priorRange = range;
                priorBegin = beginAddress;
                priorEnd = endAddress;
            }
            // Update changes made to sorted maps
            definition.setSpecifics(new ArrayList<>(specificsMap.values()));
            definition.setRanges(new ArrayList<>(rangesMap.values()));
        }
    } finally {
        getWriteLock().unlock();
    }
}
Also used : Definition(org.opennms.netmgt.config.ami.Definition) ArrayList(java.util.ArrayList) Range(org.opennms.netmgt.config.ami.Range) TreeMap(java.util.TreeMap) InetAddressComparator(org.opennms.core.utils.InetAddressComparator) InetAddress(java.net.InetAddress)

Example 4 with InetAddressComparator

use of org.opennms.core.utils.InetAddressComparator in project opennms by OpenNMS.

the class NSClientPeerFactory method optimize.

/**
 * Combine specific and range elements so that NSClientPeerFactory has to spend
 * less time iterating all these elements.
 * TODO This really should be pulled up into PeerFactory somehow, but I'm not sure how (given that "Definition" is different for both
 * SNMP and NSClient.  Maybe some sort of visitor methodology would work.  The basic logic should be fine as it's all IP address manipulation
 */
void optimize() throws UnknownHostException {
    getWriteLock().lock();
    try {
        // First pass: Remove empty definition elements
        for (final Iterator<Definition> definitionsIterator = m_config.getDefinition().iterator(); definitionsIterator.hasNext(); ) {
            final Definition definition = definitionsIterator.next();
            if (definition.getSpecific().size() == 0 && definition.getRange().size() == 0) {
                LOG.debug("optimize: Removing empty definition element");
                definitionsIterator.remove();
            }
        }
        // Second pass: Replace single IP range elements with specific elements
        for (final Definition definition : m_config.getDefinition()) {
            for (final Iterator<Range> rangesIterator = definition.getRange().iterator(); rangesIterator.hasNext(); ) {
                final Range range = rangesIterator.next();
                if (range.getBegin().equals(range.getEnd())) {
                    definition.getSpecific().add(range.getBegin());
                    rangesIterator.remove();
                }
            }
        }
        // readability and then combine them into fewer elements where possible
        for (final Definition definition : m_config.getDefinition()) {
            // Sort specifics
            final TreeMap<InetAddress, String> specificsMap = new TreeMap<InetAddress, String>(new InetAddressComparator());
            for (final String specific : definition.getSpecific()) {
                specificsMap.put(InetAddressUtils.getInetAddress(specific), specific.trim());
            }
            // Sort ranges
            final TreeMap<InetAddress, Range> rangesMap = new TreeMap<InetAddress, Range>(new InetAddressComparator());
            for (final Range range : definition.getRange()) {
                rangesMap.put(InetAddressUtils.getInetAddress(range.getBegin()), range);
            }
            // Combine consecutive specifics into ranges
            InetAddress priorSpecific = null;
            Range addedRange = null;
            for (final InetAddress specific : specificsMap.keySet()) {
                if (priorSpecific == null) {
                    priorSpecific = specific;
                    continue;
                }
                if (BigInteger.ONE.equals(InetAddressUtils.difference(specific, priorSpecific)) && InetAddressUtils.inSameScope(specific, priorSpecific)) {
                    if (addedRange == null) {
                        addedRange = new Range();
                        addedRange.setBegin(InetAddressUtils.toIpAddrString(priorSpecific));
                        rangesMap.put(priorSpecific, addedRange);
                        specificsMap.remove(priorSpecific);
                    }
                    addedRange.setEnd(InetAddressUtils.toIpAddrString(specific));
                    specificsMap.remove(specific);
                } else {
                    addedRange = null;
                }
                priorSpecific = specific;
            }
            // Move specifics to ranges
            for (final InetAddress specific : new ArrayList<InetAddress>(specificsMap.keySet())) {
                for (final InetAddress begin : new ArrayList<InetAddress>(rangesMap.keySet())) {
                    if (!InetAddressUtils.inSameScope(begin, specific)) {
                        continue;
                    }
                    if (InetAddressUtils.toInteger(begin).subtract(BigInteger.ONE).compareTo(InetAddressUtils.toInteger(specific)) > 0) {
                        continue;
                    }
                    final Range range = rangesMap.get(begin);
                    final InetAddress end = InetAddressUtils.getInetAddress(range.getEnd());
                    if (InetAddressUtils.toInteger(end).add(BigInteger.ONE).compareTo(InetAddressUtils.toInteger(specific)) < 0) {
                        continue;
                    }
                    if (InetAddressUtils.toInteger(specific).compareTo(InetAddressUtils.toInteger(begin)) >= 0 && InetAddressUtils.toInteger(specific).compareTo(InetAddressUtils.toInteger(end)) <= 0) {
                        specificsMap.remove(specific);
                        break;
                    }
                    if (InetAddressUtils.toInteger(begin).subtract(BigInteger.ONE).equals(InetAddressUtils.toInteger(specific))) {
                        rangesMap.remove(begin);
                        rangesMap.put(specific, range);
                        range.setBegin(InetAddressUtils.toIpAddrString(specific));
                        specificsMap.remove(specific);
                        break;
                    }
                    if (InetAddressUtils.toInteger(end).add(BigInteger.ONE).equals(InetAddressUtils.toInteger(specific))) {
                        range.setEnd(InetAddressUtils.toIpAddrString(specific));
                        specificsMap.remove(specific);
                        break;
                    }
                }
            }
            // Combine consecutive ranges
            Range priorRange = null;
            InetAddress priorBegin = null;
            InetAddress priorEnd = null;
            for (final Iterator<InetAddress> rangesIterator = rangesMap.keySet().iterator(); rangesIterator.hasNext(); ) {
                final InetAddress beginAddress = rangesIterator.next();
                final Range range = rangesMap.get(beginAddress);
                final InetAddress endAddress = InetAddressUtils.getInetAddress(range.getEnd());
                if (priorRange != null) {
                    if (InetAddressUtils.inSameScope(beginAddress, priorEnd) && InetAddressUtils.difference(beginAddress, priorEnd).compareTo(BigInteger.ONE) <= 0) {
                        priorBegin = new InetAddressComparator().compare(priorBegin, beginAddress) < 0 ? priorBegin : beginAddress;
                        priorRange.setBegin(InetAddressUtils.toIpAddrString(priorBegin));
                        priorEnd = new InetAddressComparator().compare(priorEnd, endAddress) > 0 ? priorEnd : endAddress;
                        priorRange.setEnd(InetAddressUtils.toIpAddrString(priorEnd));
                        rangesIterator.remove();
                        continue;
                    }
                }
                priorRange = range;
                priorBegin = beginAddress;
                priorEnd = endAddress;
            }
            // Update changes made to sorted maps
            definition.getSpecific().clear();
            definition.getSpecific().addAll(specificsMap.values());
            definition.getRange().clear();
            definition.getRange().addAll(rangesMap.values());
        }
    } finally {
        getWriteLock().unlock();
    }
}
Also used : InetAddressComparator(org.opennms.core.utils.InetAddressComparator) Definition(org.opennms.netmgt.config.nsclient.Definition) ArrayList(java.util.ArrayList) Range(org.opennms.netmgt.config.nsclient.Range) TreeMap(java.util.TreeMap) InetAddress(java.net.InetAddress)

Aggregations

InetAddress (java.net.InetAddress)4 TreeMap (java.util.TreeMap)4 InetAddressComparator (org.opennms.core.utils.InetAddressComparator)4 ArrayList (java.util.ArrayList)3 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 DBUtils (org.opennms.core.utils.DBUtils)1 Definition (org.opennms.netmgt.config.ami.Definition)1 Range (org.opennms.netmgt.config.ami.Range)1 Definition (org.opennms.netmgt.config.nsclient.Definition)1 Range (org.opennms.netmgt.config.nsclient.Range)1 Definition (org.opennms.netmgt.config.wmi.agent.Definition)1 Range (org.opennms.netmgt.config.wmi.agent.Range)1 FilterParseException (org.opennms.netmgt.filter.api.FilterParseException)1