Search in sources :

Example 1 with ExcludeRange

use of org.opennms.netmgt.config.poller.ExcludeRange 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)

Aggregations

InetAddress (java.net.InetAddress)1 ByteArrayComparator (org.opennms.core.utils.ByteArrayComparator)1 ExcludeRange (org.opennms.netmgt.config.poller.ExcludeRange)1 IncludeRange (org.opennms.netmgt.config.poller.IncludeRange)1