Search in sources :

Example 6 with ByteArrayComparator

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

the class IPAddressRangeTest method testConvertBigIntegerIntoInetAddress.

public void testConvertBigIntegerIntoInetAddress() throws UnknownHostException {
    assertEquals(0, new ByteArrayComparator().compare(zero.toOctets(), InetAddressUtils.convertBigIntegerIntoInetAddress(zero.toBigInteger()).getAddress()));
    assertEquals(0, new ByteArrayComparator().compare(one.toOctets(), InetAddressUtils.convertBigIntegerIntoInetAddress(zero.incr().toBigInteger()).getAddress()));
    assertEquals(0, new ByteArrayComparator().compare(zero.toOctets(), InetAddressUtils.convertBigIntegerIntoInetAddress(one.decr().toBigInteger()).getAddress()));
    assertEquals(0, new ByteArrayComparator().compare(one.toOctets(), InetAddressUtils.convertBigIntegerIntoInetAddress(one.toBigInteger()).getAddress()));
    assertEquals(0, new ByteArrayComparator().compare(one.toOctets(), InetAddressUtils.convertBigIntegerIntoInetAddress(one.toBigInteger()).getAddress()));
    assertEquals(0, new ByteArrayComparator().compare(maxOneOctet.toOctets(), InetAddressUtils.convertBigIntegerIntoInetAddress(maxOneOctet.toBigInteger()).getAddress()));
    assertEquals(0, new ByteArrayComparator().compare(maxTwoOctet.toOctets(), InetAddressUtils.convertBigIntegerIntoInetAddress(maxTwoOctet.toBigInteger()).getAddress()));
    assertEquals(0, new ByteArrayComparator().compare(maxThreeOctet.toOctets(), InetAddressUtils.convertBigIntegerIntoInetAddress(maxThreeOctet.toBigInteger()).getAddress()));
    assertEquals(0, new ByteArrayComparator().compare(thirtyBitNumber.toOctets(), InetAddressUtils.convertBigIntegerIntoInetAddress(thirtyBitNumber.toBigInteger()).getAddress()));
    assertEquals(0, new ByteArrayComparator().compare(thirtyOneBitNumber.toOctets(), InetAddressUtils.convertBigIntegerIntoInetAddress(thirtyOneBitNumber.toBigInteger()).getAddress()));
    assertEquals(0, new ByteArrayComparator().compare(thirtyTwoBit.toOctets(), InetAddressUtils.convertBigIntegerIntoInetAddress(thirtyTwoBit.toBigInteger()).getAddress()));
    assertEquals(0, new ByteArrayComparator().compare(maxFourOctet.toOctets(), InetAddressUtils.convertBigIntegerIntoInetAddress(maxFourOctet.toBigInteger()).getAddress()));
    InetAddress maxIPv4Addr = InetAddressUtils.convertBigIntegerIntoInetAddress(maxIPv4.toBigInteger());
    assertTrue(maxIPv4Addr instanceof Inet4Address);
    assertFalse(maxIPv4Addr instanceof Inet6Address);
    assertEquals(0, new ByteArrayComparator().compare(maxIPv4.toOctets(), maxIPv4Addr.getAddress()));
    InetAddress maxIPv6Addr = InetAddressUtils.convertBigIntegerIntoInetAddress(maxIPv6.toBigInteger());
    assertTrue(maxIPv6Addr instanceof Inet6Address);
    assertFalse(maxIPv6Addr instanceof Inet4Address);
    assertEquals(0, new ByteArrayComparator().compare(maxIPv6.toOctets(), maxIPv6Addr.getAddress()));
    assertEquals(0, new ByteArrayComparator().compare(maxIPv6MinusFive.toOctets(), InetAddressUtils.convertBigIntegerIntoInetAddress(maxIPv6MinusFive.toBigInteger()).getAddress()));
    try {
        InetAddressUtils.convertBigIntegerIntoInetAddress(new BigInteger("-1"));
        fail("Failed to catch exception for negative value.");
    } catch (IllegalArgumentException e) {
    // Expected case
    }
    try {
        InetAddressUtils.convertBigIntegerIntoInetAddress(maxIPv6.incr().toBigInteger());
        fail("Failed to catch exception for overflow value.");
    } catch (IllegalStateException e) {
    // Expected case
    }
}
Also used : Inet4Address(java.net.Inet4Address) BigInteger(java.math.BigInteger) Inet6Address(java.net.Inet6Address) ByteArrayComparator(org.opennms.core.utils.ByteArrayComparator) InetAddress(java.net.InetAddress)

Example 7 with ByteArrayComparator

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

the class SnmpInterfacePollerConfigManager 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.
 *
 * @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.
 */
public synchronized boolean interfaceInPackage(String iface, Package pkg) {
    final InetAddress ifaceAddr = addr(iface);
    boolean filterPassed = false;
    // get list of IPs in this package
    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 {}?: {}", filterPassed, iface, pkg.getName());
    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 (IncludeRange rng : pkg.getIncludeRanges()) {
        if (isInetAddressInRange(iface, rng.getBegin(), rng.getEnd())) {
            has_range_include = true;
            break;
        }
    }
    byte[] addr = toIpAddrBytes(iface);
    for (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 (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.snmpinterfacepoller.IncludeRange) InetAddress(java.net.InetAddress) ByteArrayComparator(org.opennms.core.utils.ByteArrayComparator) ExcludeRange(org.opennms.netmgt.config.snmpinterfacepoller.ExcludeRange)

Example 8 with ByteArrayComparator

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

the class ThreshdConfigManager 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.
 *
 * @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.
 */
public synchronized boolean interfaceInPackage(String iface, org.opennms.netmgt.config.threshd.Package pkg) {
    final InetAddress ifaceAddr = addr(iface);
    boolean filterPassed = false;
    // get list of IPs in this package
    java.util.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 {}?: {}", filterPassed, iface, pkg.getName());
    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;
    has_range_include = pkg.getIncludeRanges().size() == 0 && pkg.getSpecifics().size() == 0;
    for (IncludeRange rng : pkg.getIncludeRanges()) {
        if (isInetAddressInRange(iface, rng.getBegin(), rng.getEnd())) {
            has_range_include = true;
            break;
        }
    }
    byte[] addr = toIpAddrBytes(iface);
    for (String spec : pkg.getSpecifics()) {
        byte[] speca = toIpAddrBytes(spec);
        if (new ByteArrayComparator().compare(speca, addr) == 0) {
            has_specific = true;
            break;
        }
    }
    final Iterator<String> eurl = pkg.getIncludeUrls().iterator();
    while (!has_specific && eurl.hasNext()) {
        has_specific = interfaceInUrl(iface, eurl.next());
    }
    for (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.threshd.IncludeRange) InetAddress(java.net.InetAddress) ByteArrayComparator(org.opennms.core.utils.ByteArrayComparator) ExcludeRange(org.opennms.netmgt.config.threshd.ExcludeRange)

Example 9 with ByteArrayComparator

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

the class NodeLabelJDBCImpl 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 10 with ByteArrayComparator

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

the class MergeableRange method removeSpecificFromRange.

/**
     * Changes the current Range by moving the end before the specific and
     * creates a new range to the right of the specific ending with the
     * current Range's end address.
     *
     * @return a new Range to be added to Definition
     * @param spec a {@link java.lang.String} object.
     */
protected Range removeSpecificFromRange(final String spec) {
    if (!coversSpecific(spec))
        throw new IllegalArgumentException("Specific: " + spec + ", doesn't affect range: ");
    MergeableSpecific specific = new MergeableSpecific(spec);
    Range newRange = null;
    ByteArrayComparator comparator = new ByteArrayComparator();
    try {
        if (comparator.compare(specific.getValue(), getFirst().getValue()) == 0) {
            getRange().setBegin(InetAddressUtils.incr(specific.getSpecific()));
        } else if (comparator.compare(specific.getValue(), getLast().getValue()) == 0) {
            getRange().setEnd(InetAddressUtils.decr(specific.getSpecific()));
        } else {
            newRange = new Range();
            newRange.setBegin(InetAddressUtils.incr(specific.getSpecific()));
            newRange.setEnd(getRange().getEnd());
            getRange().setEnd(InetAddressUtils.decr(specific.getSpecific()));
        }
    } catch (UnknownHostException e) {
        LOG.error("Error converting string to IP address", e);
    }
    return newRange;
}
Also used : UnknownHostException(java.net.UnknownHostException) Range(org.opennms.netmgt.config.snmp.Range) ByteArrayComparator(org.opennms.core.utils.ByteArrayComparator)

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