private static SnmpCollection getSnmpCollection(
     final FileReloadContainer<DatacollectionConfig> container, final String collectionName) {
   for (final SnmpCollection collection : container.getObject().getSnmpCollection()) {
     if (collection.getName().equals(collectionName)) return collection;
   }
   return null;
 }
  @Override
  public Map<String, ResourceType> getConfiguredResourceTypes() {
    final Map<String, ResourceType> map = new HashMap<String, ResourceType>();

    final Collection<SnmpCollection> snmpCollections =
        getContainer().getObject().getSnmpCollectionCollection();
    for (final SnmpCollection collection : snmpCollections) {
      for (final ResourceType resourceType : collection.getResourceTypeCollection()) {
        map.put(resourceType.getName(), resourceType);
      }
    }

    // FIXME: I guarantee there's a cleaner way to do this, but I didn't want to refactor everything
    // that calls this just to optimize out validation.
    if (!m_validated) {
      try {
        validateResourceTypes(getContainer(), map.keySet());
      } catch (final RuntimeException e) {
        m_validationException = e;
        throw e;
      }
    } else {
      if (m_validationException != null) {
        throw m_validationException;
      }
    }
    return Collections.unmodifiableMap(map);
  }
 @Override
 public List<String> getAvailableMibGroups() {
   List<String> groups = new ArrayList<String>();
   for (final SnmpCollection collection :
       getContainer().getObject().getSnmpCollectionCollection()) {
     if (collection.getGroups() != null) {
       for (final Group group : collection.getGroups().getGroupCollection()) {
         groups.add(group.getName());
       }
     }
   }
   return groups;
 }
 @Override
 public List<String> getAvailableSystemDefs() {
   List<String> systemDefs = new ArrayList<String>();
   for (final SnmpCollection collection :
       getContainer().getObject().getSnmpCollectionCollection()) {
     if (collection.getSystems() != null) {
       for (final SystemDef systemDef : collection.getSystems().getSystemDefCollection()) {
         systemDefs.add(systemDef.getName());
       }
     }
   }
   return systemDefs;
 }
  private static void validateResourceTypes(
      final FileReloadContainer<DatacollectionConfig> container,
      final Set<String> allowedResourceTypes) {
    final String configuredString;
    if (allowedResourceTypes.size() == 0) {
      configuredString = "(none)";
    } else {
      configuredString = StringUtils.join(allowedResourceTypes, ", ");
    }

    final String allowableValues =
        "any positive number, 'ifIndex', or any of the configured resourceTypes: "
            + configuredString;
    for (final SnmpCollection collection : container.getObject().getSnmpCollectionCollection()) {
      final Groups groups = collection.getGroups();
      if (groups != null) {
        for (final Group group : groups.getGroupCollection()) {
          for (final MibObj mibObj : group.getMibObjCollection()) {
            final String instance = mibObj.getInstance();
            if (instance == null) continue;
            if (MibObject.INSTANCE_IFINDEX.equals(instance)) continue;
            if (allowedResourceTypes.contains(instance)) continue;
            try {
              // Check to see if the value is a non-negative integer
              if (Integer.parseInt(instance.trim()) >= 0) {
                continue;
              }
            } catch (NumberFormatException e) {
            }

            // XXX this should be a better exception
            throw new IllegalArgumentException(
                "instance '"
                    + instance
                    + "' invalid in mibObj definition for OID '"
                    + mibObj.getOid()
                    + "' in collection '"
                    + collection.getName()
                    + "' for group '"
                    + group.getName()
                    + "'.  Allowable instance values: "
                    + allowableValues);
          }
        }
      }
    }
  }
  private static Map<String, Map<String, Group>> getCollectionGroupMap(
      FileReloadContainer<DatacollectionConfig> container) {
    // Build collection map which is a hash map of Collection
    // objects indexed by collection name...also build
    // collection group map which is a hash map indexed
    // by collection name with a hash map as the value
    // containing a map of the collections's group names
    // to the Group object containing all the information
    // for that group. So the associations are:
    //
    // CollectionMap
    // collectionName -> Collection
    //
    // CollectionGroupMap
    // collectionName -> groupMap
    //
    // GroupMap
    // groupMapName -> Group
    //
    // This is parsed and built at initialization for
    // faster processing at run-timne.
    //
    final Map<String, Map<String, Group>> collectionGroupMap =
        new HashMap<String, Map<String, Group>>();

    for (final SnmpCollection collection : container.getObject().getSnmpCollectionCollection()) {
      // Build group map for this collection
      final Map<String, Group> groupMap = new HashMap<String, Group>();

      final Groups groups = collection.getGroups();
      if (groups != null) {
        for (final Group group : groups.getGroupCollection()) {
          groupMap.put(group.getName(), group);
        }
      }
      collectionGroupMap.put(collection.getName(), groupMap);
    }
    return Collections.unmodifiableMap(collectionGroupMap);
  }
  @Override
  protected DatacollectionConfig translateConfig(final DatacollectionConfig config) {
    final DataCollectionConfigParser parser = new DataCollectionConfigParser(getConfigDirectory());

    // Updating Configured Collections
    for (final SnmpCollection collection : config.getSnmpCollectionCollection()) {
      parser.parseCollection(collection);
    }

    // Create a special collection to hold all resource types, because they should be defined only
    // once.
    final SnmpCollection resourceTypeCollection = new SnmpCollection();
    resourceTypeCollection.setName("__resource_type_collection");
    for (final ResourceType rt : parser.getAllResourceTypes()) {
      resourceTypeCollection.addResourceType(rt);
    }
    resourceTypeCollection.setGroups(new Groups());
    resourceTypeCollection.setSystems(new Systems());
    config.getSnmpCollectionCollection().add(0, resourceTypeCollection);
    dataCollectionGroups.clear();
    dataCollectionGroups.addAll(parser.getExternalGroupMap().keySet());
    return config;
  }
 @Override
 public List<String> getRRAList(final String collectionName) {
   final SnmpCollection collection = getSnmpCollection(getContainer(), collectionName);
   return collection == null ? null : collection.getRrd().getRraCollection();
 }
 @Override
 public int getStep(final String collectionName) {
   final SnmpCollection collection = getSnmpCollection(getContainer(), collectionName);
   return collection == null ? -1 : collection.getRrd().getStep();
 }
  @Override
  public List<MibObject> getMibObjectList(
      final String cName, final String aSysoid, final String anAddress, final int ifType) {
    if (log().isDebugEnabled())
      log()
          .debug(
              "getMibObjectList: collection: "
                  + cName
                  + " sysoid: "
                  + aSysoid
                  + " address: "
                  + anAddress
                  + " ifType: "
                  + ifType);

    if (aSysoid == null) {
      if (log().isDebugEnabled()) log().debug("getMibObjectList: aSysoid parameter is NULL...");
      return new ArrayList<MibObject>();
    }

    // Retrieve the appropriate Collection object
    final SnmpCollection collection = getSnmpCollection(getContainer(), cName);
    if (collection == null) {
      return Collections.emptyList();
    }

    final Systems systems = collection.getSystems();
    if (systems == null) {
      return Collections.emptyList();
    }

    // First build a list of SystemDef objects which "match" the passed
    // sysoid and IP address parameters. The SystemDef object must match
    // on both the sysoid AND the IP address.
    //
    // SYSOID MATCH
    //
    // A SystemDef object's sysoid value may be a complete system object
    // identifier or it may be a mask (a partial sysoid).
    //
    // If the sysoid is not a mask, the 'aSysoid' string must equal the
    // sysoid value exactly in order to match.
    //
    // If the sysoid is a mask, the 'aSysoid' string need only start with
    // the sysoid mask value in order to match
    //
    // For example, a sysoid mask of ".1.3.6.1.4.1.11." would match any
    // Hewlett-Packard product which had this sysoid prefix (which should
    // include all of them).
    //
    // IPADDRESS MATCH
    //
    // In order to match on IP Address one of the following must be true:
    //
    // The SystemDef's IP address list (ipList) must contain the 'anAddress'
    // parm (must be an exact match)
    //
    // OR
    //
    // The 'anAddress' parm must have the same prefix as one of the
    // SystemDef's IP address mask list (maskList) entries.
    //
    // NOTE: A SystemDef object which contains an empty IP list and
    // an empty Mask list matches ALL IP addresses (default is INCLUDE).

    final List<SystemDef> systemList = new ArrayList<SystemDef>();

    for (final SystemDef system : systems.getSystemDefCollection()) {
      // Match on sysoid?
      boolean bMatchSysoid = false;

      // Retrieve sysoid for this SystemDef and/ set the isMask boolean.
      boolean isMask = false;
      String currSysoid = null;
      SystemDefChoice sysChoice = system.getSystemDefChoice();

      if (sysChoice.getSysoid() != null) {
        currSysoid = sysChoice.getSysoid();
      } else if (sysChoice.getSysoidMask() != null) {
        currSysoid = sysChoice.getSysoidMask();
        isMask = true;
      }

      if (currSysoid != null) {
        if (isMask) {
          // SystemDef's sysoid is a mask, 'aSysoid' need only
          // start with the sysoid mask in order to match
          if (aSysoid.startsWith(currSysoid)) {
            if (log().isDebugEnabled())
              log()
                  .debug(
                      "getMibObjectList: includes sysoid "
                          + aSysoid
                          + " for system <name>: "
                          + system.getName());
            bMatchSysoid = true;
          }
        } else {
          // System's sysoid is not a mask, 'aSysoid' must
          // match the sysoid exactly.
          if (aSysoid.equals(currSysoid)) {
            if (log().isDebugEnabled())
              log()
                  .debug(
                      "getMibObjectList: includes sysoid "
                          + aSysoid
                          + " for system <name>: "
                          + system.getName());
            bMatchSysoid = true;
          }
        }
      }

      // Match on ipAddress?
      boolean bMatchIPAddress = true; // default is INCLUDE
      if (bMatchSysoid == true) {
        if (anAddress != null) {
          List<String> addrList = null;
          List<String> maskList = null;
          if (system.getIpList() != null) {
            addrList = system.getIpList().getIpAddrCollection();
            maskList = system.getIpList().getIpAddrMaskCollection();
          }

          // If either Address list or Mask list exist then 'anAddress'
          // must be included by one of them
          if (addrList != null && addrList.size() > 0 || maskList != null && maskList.size() > 0) {
            bMatchIPAddress = false;
          }

          // First see if address is in list of specific addresses
          if (addrList != null && addrList.size() > 0) {
            if (addrList.contains(anAddress)) {
              if (log().isDebugEnabled())
                log()
                    .debug(
                        "getMibObjectList: addrList exists and does include IP address "
                            + anAddress
                            + " for system <name>: "
                            + system.getName());
              bMatchIPAddress = true;
            }
          }

          // If still no match, see if address matches any of the masks
          if (bMatchIPAddress == false) {

            if (maskList != null && maskList.size() > 0) {
              for (final String currMask : maskList) {
                if (anAddress.indexOf(currMask) == 0) {
                  if (log().isDebugEnabled())
                    log()
                        .debug(
                            "getMibObjectList: anAddress '"
                                + anAddress
                                + "' matches mask '"
                                + currMask
                                + "'");
                  bMatchIPAddress = true;
                  break;
                }
              }
            }
          }
        }
      }

      if (bMatchSysoid && bMatchIPAddress) {
        if (log().isDebugEnabled())
          log().debug("getMibObjectList: MATCH!! adding system '" + system.getName() + "'");
        systemList.add(system);
      }
    }

    // Next build list of Mib objects to collect from the list of matching SystemDefs
    final List<MibObject> mibObjectList = new ArrayList<MibObject>();

    for (final SystemDef system : systemList) {
      // Next process each of the SystemDef's groups
      for (final String grpName : system.getCollect().getIncludeGroupCollection()) {
        processGroupName(cName, grpName, ifType, mibObjectList);
      }
    }

    return mibObjectList;
  }
 @Override
 public String getSnmpStorageFlag(final String collectionName) {
   final SnmpCollection collection = getSnmpCollection(getContainer(), collectionName);
   return collection == null ? null : collection.getSnmpStorageFlag();
 }