Search in sources :

Example 1 with ByteArrayComparator

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

the class NodeLabelDaoImpl method selectPrimaryAddress.

/**
     * Returns the primary interface from a list of addresses based on the
     * specified selection method.
     * 
     * @param ipv4AddrList
     *            List of addresses from which to select the primary interface.
     * @param method
     *            String (either "min" or "max") which indicates how the primary
     *            interface is to be selected.
     * 
     * @return The InetAddress object from the address list which has been
     *         selected as the primary interface.
     */
private static InetAddress selectPrimaryAddress(List<InetAddress> ipv4AddrList, String method) {
    // Determine which interface is the primary interface
    // (ie, the interface whose IP address when converted to an
    // integer is the smallest or largest depending upon the
    // configured selection method.)
    InetAddress primaryAddr = null;
    Iterator<InetAddress> iter = ipv4AddrList.iterator();
    while (iter.hasNext()) {
        if (primaryAddr == null) {
            primaryAddr = iter.next();
        } else {
            InetAddress currentAddr = iter.next();
            byte[] current = currentAddr.getAddress();
            byte[] primary = primaryAddr.getAddress();
            if (method.equals(SELECT_METHOD_MIN)) {
                // Smallest address wins
                if (new ByteArrayComparator().compare(current, primary) < 0) {
                    primaryAddr = currentAddr;
                }
            } else {
                // Largest address wins
                if (new ByteArrayComparator().compare(current, primary) > 0) {
                    primaryAddr = currentAddr;
                }
            }
        }
    }
    return primaryAddr;
}
Also used : InetAddress(java.net.InetAddress) ByteArrayComparator(org.opennms.core.utils.ByteArrayComparator)

Example 2 with ByteArrayComparator

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

the class PollerConfigManager method isInterfaceInPackage.

/**
     * {@inheritDoc}
     *
     * This method is used to determine if the named interface is included in
     * the passed package definition. If the interface belongs to the package
     * then a value of true is returned. If the interface does not belong to the
     * package a false value is returned.
     *
     * <strong>Note: </strong>Evaluation of the interface against a package
     * filter will only work if the IP is already in the database.
     */
@Override
public boolean isInterfaceInPackage(final String iface, final Package pkg) {
    boolean filterPassed = false;
    final InetAddress ifaceAddr = addr(iface);
    // get list of IPs in this package
    final List<InetAddress> ipList = m_pkgIpMap.get().get(pkg);
    if (ipList != null && ipList.size() > 0) {
        filterPassed = ipList.contains(ifaceAddr);
    }
    LOG.debug("interfaceInPackage: Interface {} passed filter for package {}?: {}", iface, pkg.getName(), Boolean.valueOf(filterPassed));
    if (!filterPassed)
        return false;
    //
    // Ensure that the interface is in the specific list or
    // that it is in the include range and is not excluded
    //
    boolean has_specific = false;
    boolean has_range_include = false;
    boolean has_range_exclude = false;
    // if there are NO include ranges then treat act as if the user include
    // the range of all valid addresses (0.0.0.0 - 255.255.255.255, ::1 - ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff)
    has_range_include = pkg.getIncludeRanges().size() == 0 && pkg.getSpecifics().size() == 0 && pkg.getIncludeUrls().size() == 0;
    final byte[] addr = toIpAddrBytes(iface);
    for (final IncludeRange rng : pkg.getIncludeRanges()) {
        int comparison = new ByteArrayComparator().compare(addr, toIpAddrBytes(rng.getBegin()));
        if (comparison > 0) {
            int endComparison = new ByteArrayComparator().compare(addr, toIpAddrBytes(rng.getEnd()));
            if (endComparison <= 0) {
                has_range_include = true;
                break;
            }
        } else if (comparison == 0) {
            has_range_include = true;
            break;
        }
    }
    for (final String spec : pkg.getSpecifics()) {
        if (new ByteArrayComparator().compare(addr, toIpAddrBytes(spec)) == 0) {
            has_specific = true;
            LOG.debug("interfaceInPackage: Interface {} defined as 'specific'", iface);
            break;
        }
    }
    for (final String includeUrl : pkg.getIncludeUrls()) {
        if (interfaceInUrl(iface, includeUrl)) {
            has_specific = true;
            LOG.debug("interfaceInPackage: Interface {} exist on {}", iface, includeUrl);
            break;
        }
    }
    if (!has_specific) {
        for (final ExcludeRange rng : pkg.getExcludeRanges()) {
            int comparison = new ByteArrayComparator().compare(addr, toIpAddrBytes(rng.getBegin()));
            if (comparison > 0) {
                int endComparison = new ByteArrayComparator().compare(addr, toIpAddrBytes(rng.getEnd()));
                if (endComparison <= 0) {
                    LOG.debug("interfaceInPackage: Interface {} matches an exclude range", iface);
                    has_range_exclude = true;
                    break;
                }
            } else if (comparison == 0) {
                LOG.debug("interfaceInPackage: Interface {} matches an exclude range", iface);
                has_range_exclude = true;
                break;
            }
        }
    }
    return has_specific || (has_range_include && !has_range_exclude);
}
Also used : IncludeRange(org.opennms.netmgt.config.poller.IncludeRange) InetAddress(java.net.InetAddress) ByteArrayComparator(org.opennms.core.utils.ByteArrayComparator) ExcludeRange(org.opennms.netmgt.config.poller.ExcludeRange)

Example 3 with ByteArrayComparator

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

the class RangeComparator method compare.

/**
     * <p>compare</p>
     *
     * @param rng1 a {@link org.opennms.netmgt.config.common.Range} object.
     * @param rng2 a {@link org.opennms.netmgt.config.common.Range} object.
     * @return a int.
     */
@Override
public int compare(final Range rng1, final Range rng2) {
    final InetAddress addr1 = InetAddressUtils.addr(rng1.getBegin());
    final InetAddress addr2 = InetAddressUtils.addr(rng2.getBegin());
    return new ByteArrayComparator().compare(addr1 == null ? null : addr1.getAddress(), addr2 == null ? null : addr2.getAddress());
}
Also used : InetAddress(java.net.InetAddress) ByteArrayComparator(org.opennms.core.utils.ByteArrayComparator)

Example 4 with ByteArrayComparator

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

the class SpecificComparator method compare.

/**
     * returns the difference of spec1 - spec2
     *
     * @param spec1 a {@link java.lang.String} object.
     * @param spec2 a {@link java.lang.String} object.
     * @return -1 for spec1 < spec2, 0 for spec1 == spec2, 1 for spec1 > spec2
     */
@Override
public int compare(final String spec1, final String spec2) {
    final InetAddress addr1 = InetAddressUtils.addr(spec1);
    final InetAddress addr2 = InetAddressUtils.addr(spec2);
    return new ByteArrayComparator().compare(addr1 == null ? null : addr1.getAddress(), addr2 == null ? null : addr2.getAddress());
}
Also used : InetAddress(java.net.InetAddress) ByteArrayComparator(org.opennms.core.utils.ByteArrayComparator)

Example 5 with ByteArrayComparator

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

the class RancidAdapterConfigManager method interfaceInPackage.

/**
     * This method is used to determine if the named interface is included in
     * the passed package definition. If the interface belongs to the package
     * then a value of true is returned. If the interface does not belong to the
     * package a false value is returned.
     * 
     * <strong>Note: </strong>Evaluation of the interface against a package
     * filter will only work if the IP is already in the database.
     * 
     * TODO: Factor this method out so that it can be reused? Or use an existing
     * utility method if one exists?
     * 
     * @param iface
     *            The interface to test against the package.
     * @param pkg
     *            The package to check for the inclusion of the interface.
     * 
     * @return True if the interface is included in the package, false
     *         otherwise.
     */
private boolean interfaceInPackage(final String iface, final Package pkg) {
    boolean filterPassed = false;
    final InetAddress ifaceAddr = addr(iface);
    // get list of IPs in this package
    final List<InetAddress> ipList = m_pkgIpMap.get(pkg);
    if (ipList != null && ipList.size() > 0) {
        filterPassed = ipList.contains(ifaceAddr);
    }
    LOG.debug("interfaceInPackage: Interface {} passed filter for package {}?: {}", iface, pkg.getName(), Boolean.valueOf(filterPassed));
    if (!filterPassed)
        return false;
    //
    // Ensure that the interface is in the specific list or
    // that it is in the include range and is not excluded
    //
    boolean has_specific = false;
    boolean has_range_include = false;
    boolean has_range_exclude = false;
    // if there are NO include ranges then treat act as if the user include
    // the range 0.0.0.0 - 255.255.255.255
    has_range_include = pkg.getIncludeRanges().size() == 0 && pkg.getSpecifics().size() == 0;
    for (final IncludeRange rng : pkg.getIncludeRanges()) {
        if (isInetAddressInRange(iface, rng.getBegin(), rng.getEnd())) {
            has_range_include = true;
            break;
        }
    }
    byte[] addr = toIpAddrBytes(iface);
    for (final String spec : pkg.getSpecifics()) {
        byte[] speca = toIpAddrBytes(spec);
        if (new ByteArrayComparator().compare(speca, addr) == 0) {
            has_specific = true;
            break;
        }
    }
    Iterator<String> eurl = pkg.getIncludeUrls().iterator();
    while (!has_specific && eurl.hasNext()) {
        has_specific = interfaceInUrl(iface, eurl.next());
    }
    for (final ExcludeRange rng : pkg.getExcludeRanges()) {
        if (isInetAddressInRange(iface, rng.getBegin(), rng.getEnd())) {
            has_range_exclude = true;
            break;
        }
    }
    return has_specific || (has_range_include && !has_range_exclude);
}
Also used : IncludeRange(org.opennms.netmgt.config.rancid.adapter.IncludeRange) InetAddress(java.net.InetAddress) ByteArrayComparator(org.opennms.core.utils.ByteArrayComparator) ExcludeRange(org.opennms.netmgt.config.rancid.adapter.ExcludeRange)

Aggregations

ByteArrayComparator (org.opennms.core.utils.ByteArrayComparator)10 InetAddress (java.net.InetAddress)9 BigInteger (java.math.BigInteger)1 Inet4Address (java.net.Inet4Address)1 Inet6Address (java.net.Inet6Address)1 UnknownHostException (java.net.UnknownHostException)1 ExcludeRange (org.opennms.netmgt.config.poller.ExcludeRange)1 IncludeRange (org.opennms.netmgt.config.poller.IncludeRange)1 ExcludeRange (org.opennms.netmgt.config.rancid.adapter.ExcludeRange)1 IncludeRange (org.opennms.netmgt.config.rancid.adapter.IncludeRange)1 Range (org.opennms.netmgt.config.snmp.Range)1 ExcludeRange (org.opennms.netmgt.config.snmpinterfacepoller.ExcludeRange)1 IncludeRange (org.opennms.netmgt.config.snmpinterfacepoller.IncludeRange)1 ExcludeRange (org.opennms.netmgt.config.threshd.ExcludeRange)1 IncludeRange (org.opennms.netmgt.config.threshd.IncludeRange)1