private void completeEventListAndAddToEvents(
      Events events, List<Event> eventList, LatencyInterface latIface)
      throws ThresholdingException {
    for (Event event : eventList) {
      event.setNodeid((long) latIface.getNodeId());
      event.setInterfaceAddress(latIface.getInetAddress());
      event.setService(latIface.getServiceName());

      events.addEvent(event);
    }
  }
  /**
   * {@inheritDoc}
   *
   * <p>Perform threshold checking.
   */
  public int check(ThresholdNetworkInterface iface, EventProxy eproxy, Map<?, ?> parameters) {
    LatencyInterface latIface = new LatencyInterface(iface, m_svcName);
    LatencyParameters latParms = new LatencyParameters(parameters, m_svcName);

    try {

      // Get configuration parameters
      //
      // NodeId attribute
      if (log().isDebugEnabled())
        log()
            .debug(
                "check: service= "
                    + m_svcName
                    + " interface= "
                    + latIface.getHostAddress()
                    + " nodeId= "
                    + latIface.getNodeId()
                    + " thresholding-group="
                    + latParms.getGroupName()
                    + " interval="
                    + latParms.getInterval()
                    + "ms");

      // RRD Repository attribute
      //
      // Create empty Events object to hold any threshold
      // events generated during the thresholding check...
      Events events = checkRrdDir(latIface, latParms);

      // Send created events
      //
      sendEvents(eproxy, events);

      // return the status of the threshold check
      //
      return THRESHOLDING_SUCCEEDED;

    } catch (ThresholdingException e) {
      log().error(e.getMessage());
      return e.getFailureCode();
    } catch (EventProxyException e) {
      log().error("check: Failed sending threshold events via event proxy...", e);
      return THRESHOLDING_FAILED;
    }
  }
  /**
   * Performs threshold checking on an directory which contains one or more RRD files containing
   * latency/response time information. ThresholdEntity objects are stored for performing threshold
   * checking.
   *
   * @param latIface TODO
   * @param latParms TODO
   * @param parameters
   * @param iface
   * @param directory RRD repository directory
   * @param m_nodeId Node identifier of interface being checked
   * @param ipAddr IP address of the interface being checked
   * @param interval Configured thresholding interval
   * @param date Source for timestamp to be used for all generated events
   * @param thresholdMap Map of configured interface level ThresholdEntity objects keyed by
   *     datasource name.
   * @param events Castor events object containing any events to be generated as a result of
   *     threshold checking.
   * @throws IllegalArgumentException if path parameter is not a directory.
   * @throws ThresholdingException
   */
  Events checkRrdDir(LatencyInterface latIface, LatencyParameters latParms)
      throws IllegalArgumentException, ThresholdingException {
    Map<String, ThresholdEntity> thresholdMap = latIface.getThresholdMap();

    // Sanity Check
    if (latIface.getInetAddress() == null || thresholdMap == null) {
      throw new ThresholdingException(
          "check: Threshold checking failed for " + m_svcName + "/" + latIface.getHostAddress(),
          THRESHOLDING_FAILED);
    }

    Events events = new Events();
    Date date = new Date();

    for (Iterator<String> it = thresholdMap.keySet().iterator(); it.hasNext(); ) {
      String datasource = it.next();
      ThresholdEntity threshold = thresholdMap.get(datasource);
      if (threshold != null) {
        Double dsValue = threshold.fetchLastValue(latIface, latParms);
        Map<String, Double> dsValues = new HashMap<String, Double>();
        dsValues.put(datasource, dsValue);
        List<Event> eventList = threshold.evaluateAndCreateEvents(dsValues, date);
        if (eventList.size() == 0) {
          // Nothing to do, so continue
          continue;
        }

        completeEventListAndAddToEvents(events, eventList, latIface);

        /*
        threshold.evaluateThreshold(dsValue, events, date, latIface);
        */
      }
    }

    return events;
  }
  /**
   * fetchLastValue
   *
   * @param latIface a {@link org.opennms.netmgt.threshd.LatencyInterface} object.
   * @param latParms a {@link org.opennms.netmgt.threshd.LatencyParameters} object.
   * @return a {@link java.lang.Double} object.
   * @throws org.opennms.netmgt.threshd.ThresholdingException if any.
   */
  public Double fetchLastValue(LatencyInterface latIface, LatencyParameters latParms)
      throws ThresholdingException {
    // Assume that this only happens on a simple "Threshold", not an "Expression"
    // If it is an Expression, then we don't yet know what to do - this will likely just fail with
    // some sort of exception.
    // perhaps we should figure out how to expand it (or at least use code elsewhere to do so
    // sensibly)
    String datasource = getDataSourceExpression();

    // Use RRD strategy to "fetch" value of the datasource from the RRD file
    Double dsValue = null;
    try {
      if (getDatasourceType().equals("if")) {
        if (log().isDebugEnabled()) {
          log().debug("Fetching last value from dataSource '" + datasource + "'");
        }

        File rrdFile = new File(latIface.getLatencyDir(), datasource + RrdUtils.getExtension());
        if (!rrdFile.exists()) {
          log().info("rrd file " + rrdFile + " does not exist");
          return null;
        }

        if (!rrdFile.canRead()) {
          log().error("Unable to read existing rrd file " + rrdFile);
          return null;
        }

        if (latParms.getRange() == 0) {
          dsValue =
              RrdUtils.fetchLastValue(
                  rrdFile.getAbsolutePath(), datasource, latParms.getInterval());
        } else {
          dsValue =
              RrdUtils.fetchLastValueInRange(
                  rrdFile.getAbsolutePath(),
                  datasource,
                  latParms.getInterval(),
                  latParms.getRange());
        }
      } else {
        throw new ThresholdingException(
            "expr types not yet implemented", LatencyThresholder.THRESHOLDING_FAILED);
      }

      if (log().isDebugEnabled()) {
        log().debug("Last value from dataSource '" + datasource + "' was " + dsValue);
      }
    } catch (NumberFormatException nfe) {
      log()
          .warn(
              "Unable to convert retrieved value for datasource '"
                  + datasource
                  + "' to a double, skipping evaluation.");
    } catch (RrdException e) {
      log()
          .error(
              "An error occurred retriving the last value for datasource '"
                  + datasource
                  + "': "
                  + e,
              e);
    }

    return dsValue;
  }