Search in sources :

Example 1 with Range

use of org.opennms.netmgt.config.nsclient.Range in project opennms by OpenNMS.

the class NSClientPeerFactory method getAgentConfig.

/**
     * <p>getAgentConfig</p>
     *
     * @param agentInetAddress a {@link java.net.InetAddress} object.
     * @return a {@link org.opennms.protocols.nsclient.NSClientAgentConfig} object.
     */
public NSClientAgentConfig getAgentConfig(final InetAddress agentInetAddress) {
    getReadLock().lock();
    try {
        if (m_config == null) {
            return new NSClientAgentConfig(agentInetAddress);
        }
        final NSClientAgentConfig agentConfig = new NSClientAgentConfig(agentInetAddress);
        //Now set the defaults from the m_config
        setNSClientAgentConfig(agentConfig, new Definition());
        // Attempt to locate the node
        DEFLOOP: for (final Definition def : m_config.getDefinition()) {
            for (final String saddr : def.getSpecific()) {
                final InetAddress addr = InetAddressUtils.addr(saddr);
                if (addr.equals(agentConfig.getAddress())) {
                    setNSClientAgentConfig(agentConfig, def);
                    break DEFLOOP;
                }
            }
            //
            for (final Range rng : def.getRange()) {
                if (InetAddressUtils.isInetAddressInRange(InetAddressUtils.str(agentConfig.getAddress()), rng.getBegin(), rng.getEnd())) {
                    setNSClientAgentConfig(agentConfig, def);
                    break DEFLOOP;
                }
            }
            // check the matching IP expressions
            for (final String ipMatch : def.getIpMatch()) {
                if (IPLike.matches(InetAddressUtils.str(agentInetAddress), ipMatch)) {
                    setNSClientAgentConfig(agentConfig, def);
                    break DEFLOOP;
                }
            }
        }
        if (agentConfig == null) {
            setNSClientAgentConfig(agentConfig, new Definition());
        }
        return agentConfig;
    } finally {
        getReadLock().unlock();
    }
}
Also used : NSClientAgentConfig(org.opennms.protocols.nsclient.NSClientAgentConfig) Definition(org.opennms.netmgt.config.nsclient.Definition) Range(org.opennms.netmgt.config.nsclient.Range) InetAddress(java.net.InetAddress)

Example 2 with Range

use of org.opennms.netmgt.config.nsclient.Range 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)2 Definition (org.opennms.netmgt.config.nsclient.Definition)2 Range (org.opennms.netmgt.config.nsclient.Range)2 ArrayList (java.util.ArrayList)1 TreeMap (java.util.TreeMap)1 InetAddressComparator (org.opennms.core.utils.InetAddressComparator)1 NSClientAgentConfig (org.opennms.protocols.nsclient.NSClientAgentConfig)1