/** {@inheritDoc} */
  public synchronized String getPackageName(String ipaddr) {
    for (Package pkg : packages()) {

      if (interfaceInPackage(ipaddr, pkg)) {
        return pkg.getName();
      }
    }
    return null;
  }
  /**
   * getPackage
   *
   * @param name a {@link java.lang.String} object.
   * @return a {@link org.opennms.netmgt.config.snmpinterfacepoller.Package} object.
   */
  public synchronized Package getPackage(final String name) {

    for (Package pkg : packages()) {

      if (pkg.getName().equals(name)) {
        return pkg;
      }
    }
    return null;
  }
  /**
   * {@inheritDoc}
   *
   * <p>Returns a list of package names that the IP belongs to, null if none.
   *
   * <p><strong>Note: </strong>Evaluation of the interface against a package filter will only work
   * if the IP is already in the database.
   */
  public synchronized List<String> getAllPackageMatches(String ipaddr) {

    List<String> matchingPkgs = new ArrayList<String>();

    for (Package pkg : packages()) {

      boolean inPkg = interfaceInPackage(ipaddr, pkg);
      if (inPkg) {
        matchingPkgs.add(pkg.getName());
      }
    }

    return matchingPkgs;
  }
 /**
  * getIpList
  *
  * @param pkg a {@link org.opennms.netmgt.config.snmpinterfacepoller.Package} object.
  * @return a {@link java.util.List} object.
  */
 public List<InetAddress> getIpList(Package pkg) {
   StringBuffer filterRules = new StringBuffer(pkg.getFilter().getContent());
   if (m_verifyServer) {
     filterRules.append(" & (serverName == ");
     filterRules.append('\"');
     filterRules.append(m_localServer);
     filterRules.append('\"');
     filterRules.append(")");
   }
   if (log().isDebugEnabled())
     log()
         .debug(
             "createPackageIpMap: package is "
                 + pkg.getName()
                 + ". filer rules are  "
                 + filterRules.toString());
   List<InetAddress> ipList =
       FilterDaoFactory.getInstance().getActiveIPAddressList(filterRules.toString());
   return ipList;
 }
  /**
   * This method is used to establish package against IP list mapping, with which, the IP list is
   * selected per package via the configured filter rules from the database.
   */
  private void createPackageIpListMap() {
    m_pkgIpMap = new HashMap<Package, List<InetAddress>>();
    m_pkgIntMap = new HashMap<String, Map<String, Interface>>();

    for (Package pkg : packages()) {

      Map<String, Interface> interfaceMap = new HashMap<String, Interface>();
      for (Interface interf : pkg.getInterfaceCollection()) {
        interfaceMap.put(interf.getName(), interf);
      }
      m_pkgIntMap.put(pkg.getName(), interfaceMap);
      // Get a list of IP addresses per package against the filter rules from
      // database and populate the package, IP list map.
      //
      try {
        List<InetAddress> ipList = getIpList(pkg);
        if (log().isDebugEnabled())
          log()
              .debug(
                  "createPackageIpMap: package "
                      + pkg.getName()
                      + ": ipList size =  "
                      + ipList.size());

        if (ipList.size() > 0) {
          if (log().isDebugEnabled())
            log()
                .debug(
                    "createPackageIpMap: package "
                        + pkg.getName()
                        + ". IpList size is "
                        + ipList.size());
          m_pkgIpMap.put(pkg, ipList);
        }
      } catch (Throwable t) {
        log()
            .error(
                "createPackageIpMap: failed to map package: "
                    + pkg.getName()
                    + " to an IP List: "
                    + t,
                t);
      }
    }
  }
 /**
  * includeURLs
  *
  * @param pkg a {@link org.opennms.netmgt.config.snmpinterfacepoller.Package} object.
  * @return a {@link java.lang.Iterable} object.
  */
 public Iterable<String> includeURLs(Package pkg) {
   return pkg.getIncludeUrlCollection();
 }
  /**
   * 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.
   *
   * <p><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);
    ThreadCategory log = log();

    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);
    }

    if (log.isDebugEnabled())
      log.debug(
          "interfaceInPackage: Interface "
              + iface
              + " passed filter for package "
              + pkg.getName()
              + "?: "
              + 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.getIncludeRangeCount() == 0 && pkg.getSpecificCount() == 0;

    for (IncludeRange rng : pkg.getIncludeRangeCollection()) {
      if (isInetAddressInRange(iface, rng.getBegin(), rng.getEnd())) {
        has_range_include = true;
        break;
      }
    }

    byte[] addr = toIpAddrBytes(iface);

    for (String spec : pkg.getSpecificCollection()) {
      byte[] speca = toIpAddrBytes(spec);
      if (new ByteArrayComparator().compare(speca, addr) == 0) {
        has_specific = true;
        break;
      }
    }

    Enumeration<String> eurl = pkg.enumerateIncludeUrl();
    while (!has_specific && eurl.hasMoreElements()) {
      has_specific = interfaceInUrl(iface, eurl.nextElement());
    }

    for (ExcludeRange rng : pkg.getExcludeRangeCollection()) {
      if (isInetAddressInRange(iface, rng.getBegin(), rng.getEnd())) {
        has_range_exclude = true;
        break;
      }
    }

    return has_specific || (has_range_include && !has_range_exclude);
  }