Search in sources :

Example 1 with IncludeRange

use of org.opennms.netmgt.config.rancid.adapter.IncludeRange 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

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