/** {@inheritDoc} */
  @Override
  public List<OnmsResource> getResourcesFromGraphs(List<Graph> graphs) {
    Assert.notNull(graphs, "graph argument cannot be null");
    List<OnmsResource> resources = new LinkedList<OnmsResource>();
    HashMap<String, List<OnmsResource>> resourcesMap = new HashMap<String, List<OnmsResource>>();
    for (Graph graph : graphs) {
      String resourceId = getResourceIdForGraph(graph);

      if (resourceId != null) {
        String[] resourceParts = DefaultGraphResultsService.parseResourceId(resourceId);
        if (resourceParts == null) {
          log().warn("getResourcesFromGraphs: unparsable resourceId, skipping: " + resourceId);
          continue;
        }

        String parent = resourceParts[0];
        String childType = resourceParts[1];
        String childName = resourceParts[2];

        List<OnmsResource> resourcesForParent = resourcesMap.get(parent);
        if (resourcesForParent == null) {
          try {
            resourcesForParent = getResourceService().getResourceListById(resourceId);
            if (resourcesForParent == null) {
              log().warn("getResourcesFromGraphs: no resources found for parent " + parent);
              continue;
            } else {
              resourcesMap.put(parent, resourcesForParent);
              log().debug("getResourcesFromGraphs: add resourceList to map for " + parent);
            }
          } catch (Throwable e) {
            log()
                .warn(
                    "getResourcesFromGraphs: unexpected exception thrown while fetching resource list for \""
                        + parent
                        + "\", skipping resource",
                    e);
            continue;
          }
        }

        for (OnmsResource r : resourcesForParent) {
          if (childType.equals(r.getResourceType().getName()) && childName.equals(r.getName())) {
            resources.add(r);
            log().debug("getResourcesFromGraphs: found resource in map" + r.toString());
            break;
          }
        }
      }
    }
    return resources;
  }
  private ArrayList<OnmsResource> populateResourceList(
      File parent, File relPath, OnmsNode node, Boolean isForeign) {

    ArrayList<OnmsResource> resources = new ArrayList<OnmsResource>();

    File[] intfDirs = parent.listFiles(RrdFileConstants.INTERFACE_DIRECTORY_FILTER);

    Set<OnmsSnmpInterface> snmpInterfaces = node.getSnmpInterfaces();
    Map<String, OnmsSnmpInterface> intfMap = new HashMap<String, OnmsSnmpInterface>();

    for (OnmsSnmpInterface snmpInterface : snmpInterfaces) {
      /*
       * When Cisco Express Forwarding (CEF) or some ATM encapsulations
       * (AAL5) are used on Cisco routers, an additional entry might be
       * in the ifTable for these sub-interfaces, but there is no
       * performance data available for collection.  This check excludes
       * ifTable entries where ifDescr contains "-cef".  See bug #803.
       */
      if (snmpInterface.getIfDescr() != null) {
        if (Pattern.matches(".*-cef.*", snmpInterface.getIfDescr())) {
          continue;
        }
      }

      String replacedIfName = AlphaNumeric.parseAndReplace(snmpInterface.getIfName(), '_');
      String replacedIfDescr = AlphaNumeric.parseAndReplace(snmpInterface.getIfDescr(), '_');

      String[] keys =
          new String[] {
            replacedIfName + "-",
            replacedIfDescr + "-",
            replacedIfName + "-" + snmpInterface.getPhysAddr(),
            replacedIfDescr + "-" + snmpInterface.getPhysAddr()
          };

      for (String key : keys) {
        if (!intfMap.containsKey(key)) {
          intfMap.put(key, snmpInterface);
        }
      }
    }

    for (File intfDir : intfDirs) {
      String name = intfDir.getName();

      String desc = name;
      String mac = "";

      // Strip off the MAC address from the end, if there is one
      int dashIndex = name.lastIndexOf('-');

      if (dashIndex >= 0) {
        desc = name.substring(0, dashIndex);
        mac = name.substring(dashIndex + 1, name.length());
      }

      String key = desc + "-" + mac;
      OnmsSnmpInterface snmpInterface = intfMap.get(key);

      String label;
      Long ifSpeed = null;
      String ifSpeedFriendly = null;
      if (snmpInterface == null) {
        label = name + " (*)";
      } else {
        StringBuffer descr = new StringBuffer();
        StringBuffer parenString = new StringBuffer();

        if (snmpInterface.getIfAlias() != null) {
          parenString.append(snmpInterface.getIfAlias());
        }
        // Append all of the IP addresses on this ifindex
        for (OnmsIpInterface ipif : snmpInterface.getIpInterfaces()) {
          String ipaddr = InetAddressUtils.str(ipif.getIpAddress());
          if (!"0.0.0.0".equals(ipaddr)) {
            if (parenString.length() > 0) {
              parenString.append(", ");
            }
            parenString.append(ipaddr);
          }
        }
        if ((snmpInterface.getIfSpeed() != null) && (snmpInterface.getIfSpeed() != 0)) {
          ifSpeed = snmpInterface.getIfSpeed();
          ifSpeedFriendly = SIUtils.getHumanReadableIfSpeed(ifSpeed);
          if (parenString.length() > 0) {
            parenString.append(", ");
          }
          parenString.append(ifSpeedFriendly);
        }

        if (snmpInterface.getIfName() != null) {
          descr.append(snmpInterface.getIfName());
        } else if (snmpInterface.getIfDescr() != null) {
          descr.append(snmpInterface.getIfDescr());
        } else {
          /*
           * Should never reach this point, since ifLabel is based on
           * the values of ifName and ifDescr but better safe than sorry.
           */
          descr.append(name);
        }

        /* Add the extended information in parenthesis after the ifLabel,
         * if such information was found.
         */
        if (parenString.length() > 0) {
          descr.append(" (");
          descr.append(parenString);
          descr.append(")");
        }

        label = descr.toString();
      }

      OnmsResource resource = null;
      if (isForeign) {
        resource =
            getResourceByNodeSourceAndInterface(
                relPath.toString(), intfDir.getName(), label, ifSpeed, ifSpeedFriendly);
      } else {
        resource =
            getResourceByNodeAndInterface(
                node.getId(), intfDir.getName(), label, ifSpeed, ifSpeedFriendly);
      }
      if (snmpInterface != null) {
        Set<OnmsIpInterface> ipInterfaces = snmpInterface.getIpInterfaces();
        if (ipInterfaces.size() > 0) {
          int id = ipInterfaces.iterator().next().getId();
          resource.setLink("element/interface.jsp?ipinterfaceid=" + id);
        } else {
          int ifIndex = snmpInterface.getIfIndex();
          if (ifIndex > -1) {
            resource.setLink(
                "element/snmpinterface.jsp?node=" + node.getNodeId() + "&ifindex=" + ifIndex);
          }
        }

        resource.setEntity(snmpInterface);
      } else {
        LOG.debug("populateResourceList: snmpInterface is null");
      }
      LOG.debug("populateResourceList: adding resource toString {}", resource.toString());
      resources.add(resource);
    }

    return resources;
  }
  @Override
  public GraphResults findResults(
      String[] resourceIds, String[] reports, long start, long end, String relativeTime) {
    if (resourceIds == null) {
      throw new IllegalArgumentException("resourceIds argument cannot be null");
    }
    if (reports == null) {
      throw new IllegalArgumentException("reports argument cannot be null");
    }
    if (end < start) {
      throw new IllegalArgumentException("end time cannot be before start time");
    }

    GraphResults graphResults = new GraphResults();
    graphResults.setStart(new Date(start));
    graphResults.setEnd(new Date(end));
    graphResults.setRelativeTime(relativeTime);
    graphResults.setRelativeTimePeriods(m_periods);
    graphResults.setReports(reports);

    HashMap<String, List<OnmsResource>> resourcesMap = new HashMap<String, List<OnmsResource>>();

    for (String resourceId : resourceIds) {
      String[] values = parseResourceId(resourceId);
      if (values == null) {
        continue;
      }
      String parent = values[0];
      String childType = values[1];
      String childName = values[2];
      LOG.debug(
          "findResults: parent, childType, childName = {}, {}, {}",
          values[0],
          values[1],
          values[2]);
      OnmsResource resource = null;
      if (!resourcesMap.containsKey(parent)) {
        List<OnmsResource> resourceList =
            m_resourceDao.getResourceById(resourceId).getChildResources();
        if (resourceList == null) {
          LOG.warn("findResults: zero child resources found for {}", parent);
        } else {
          resourcesMap.put(parent, resourceList);
          LOG.debug("findResults: add resourceList to map for {}", parent);
        }
      }
      for (OnmsResource r : resourcesMap.get(parent)) {
        if (childType.equals(r.getResourceType().getName()) && childName.equals(r.getName())) {
          resource = r;
          LOG.debug("findResults: found resource in map{}", r.toString());
          break;
        }
      }
      try {
        graphResults.addGraphResultSet(
            createGraphResultSet(resourceId, resource, reports, graphResults));
      } catch (IllegalArgumentException e) {
        LOG.warn(e.getMessage(), e);
        continue;
      }
    }

    graphResults.setGraphTopOffsetWithText(m_rrdDao.getGraphTopOffsetWithText());
    graphResults.setGraphLeftOffset(m_rrdDao.getGraphLeftOffset());
    graphResults.setGraphRightOffset(m_rrdDao.getGraphRightOffset());

    return graphResults;
  }