/* (non-Javadoc)
  * @see org.opennms.features.vaadin.mibcompiler.api.MibParser#getPrefabGraphs()
  */
 @Override
 public List<PrefabGraph> getPrefabGraphs() {
   if (module == null) {
     return null;
   }
   List<PrefabGraph> graphs = new ArrayList<PrefabGraph>();
   LogUtils.infof(this, "Generating graph templates for %s", module.getId());
   NameCutter cutter = new NameCutter();
   try {
     for (SmiVariable v : module.getVariables()) {
       String groupName = getGroupName(v);
       String resourceType = getResourceType(v);
       if (resourceType == null) resourceType = "nodeSnmp";
       String typeName = getMetricType(v.getType().getPrimitiveType());
       if (v.getId().contains("Index")) { // Treat SNMP Indexes as strings.
         typeName = "string";
       }
       int order = 1;
       if (typeName != null && !typeName.toLowerCase().contains("string")) {
         String name = groupName + '.' + v.getId();
         String alias =
             cutter.trimByCamelCase(v.getId(), 19); // RRDtool/JRobin DS size restriction.
         String descr = v.getDescription().replaceAll("[\n\r]", "").replaceAll("\\s+", " ");
         StringBuffer sb = new StringBuffer();
         sb.append("--title=\"").append(v.getId()).append("\" \\\n");
         sb.append(" DEF:var={rrd1}:").append(alias).append(":AVERAGE \\\n");
         sb.append(" LINE1:var#0000ff:\"").append(v.getId()).append("\" \\\n");
         sb.append(" GPRINT:var:AVERAGE:\"Avg\\\\: %8.2lf %s\" \\\n");
         sb.append(" GPRINT:var:MIN:\"Min\\\\: %8.2lf %s\" \\\n");
         sb.append(" GPRINT:var:MAX:\"Max\\\\: %8.2lf %s\\n\"");
         sb.append("\n\n");
         PrefabGraph graph =
             new PrefabGraph(
                 name,
                 descr,
                 new String[] {alias},
                 sb.toString(),
                 new String[0],
                 new String[0],
                 order++,
                 new String[] {resourceType},
                 descr,
                 null,
                 null,
                 new String[0]);
         graphs.add(graph);
       }
     }
   } catch (Throwable e) {
     String errors = e.getMessage();
     if (errors == null || errors.trim().equals(""))
       errors =
           "An unknown error accured when generating graph templates from the MIB "
               + module.getId();
     LogUtils.errorf(this, e, "Graph templates parsing error: %s", errors);
     errorHandler.addError(errors);
     return null;
   }
   return graphs;
 }
 /**
  * Gets the module.
  *
  * @param mibObject the MIB object
  * @param mibFile the MIB file
  * @return the module
  */
 private SmiModule getModule(SmiMib mibObject, File mibFile) {
   for (SmiModule m : mibObject.getModules()) {
     if (m.getIdToken().getLocation().getSource().contains(mibFile.getAbsolutePath())) {
       return m;
     }
   }
   errorHandler.addError("Can't find the MIB module for " + mibFile);
   return null;
 }
 /* (non-Javadoc)
  * @see org.opennms.features.vaadin.mibcompiler.api.MibParser#getDataCollection()
  */
 public DatacollectionGroup getDataCollection() {
   if (module == null) {
     return null;
   }
   LogUtils.infof(this, "Generating data collection configuration for %s", module.getId());
   DatacollectionGroup dcGroup = new DatacollectionGroup();
   dcGroup.setName(module.getId());
   NameCutter cutter = new NameCutter();
   try {
     for (SmiVariable v : module.getVariables()) {
       String groupName = getGroupName(v);
       String resourceType = getResourceType(v);
       Group group = getGroup(dcGroup, groupName, resourceType);
       String typeName = getMetricType(v.getType().getPrimitiveType());
       if (typeName != null) {
         String alias =
             cutter.trimByCamelCase(v.getId(), 19); // RRDtool/JRobin DS size restriction.
         MibObj mibObj = new MibObj();
         mibObj.setOid('.' + v.getOidStr());
         mibObj.setInstance(resourceType == null ? "0" : resourceType);
         mibObj.setAlias(alias);
         mibObj.setType(typeName);
         group.addMibObj(mibObj);
         if (typeName.equals("string") && resourceType != null) {
           for (ResourceType rs : dcGroup.getResourceTypeCollection()) {
             if (rs.getName().equals(resourceType) && rs.getResourceLabel().equals("${index}")) {
               rs.setResourceLabel("${" + v.getId() + "} (${index})");
             }
           }
         }
       }
     }
   } catch (Throwable e) {
     String errors = e.getMessage();
     if (errors == null || errors.trim().equals(""))
       errors =
           "An unknown error accured when generating data collection objects from the MIB "
               + module.getId();
     LogUtils.errorf(this, e, "Data Collection parsing error: %s", errors);
     errorHandler.addError(errors);
     return null;
   }
   return dcGroup;
 }
  /* (non-Javadoc)
   * @see org.opennms.features.vaadin.mibcompiler.MibParser#parseMib(java.io.File)
   */
  public boolean parseMib(File mibFile) {
    // Validate MIB Directory
    if (mibDirectory == null) {
      errorHandler.addError("MIB directory has not been set.");
      return false;
    }

    // Reset error handler and dependencies tracker
    missingDependencies.clear();

    // Set UP the MIB Queue MIB to be parsed
    List<URL> queue = new ArrayList<URL>();
    parser.getFileParserPhase().setInputUrls(queue);

    // Create a cache of filenames to do case-insensitive lookups
    final Map<String, File> mibDirectoryFiles = new HashMap<String, File>();
    for (final File file : mibDirectory.listFiles()) {
      mibDirectoryFiles.put(file.getName().toLowerCase(), file);
    }

    // Parse MIB
    LogUtils.debugf(this, "Parsing %s", mibFile.getAbsolutePath());
    SmiMib mib = null;
    addFileToQueue(queue, mibFile);
    while (true) {
      errorHandler.reset();
      try {
        mib = parser.parse();
      } catch (Exception e) {
        LogUtils.errorf(this, e, "Can't compile %s", mibFile);
        errorHandler.addError(e.getMessage());
        return false;
      }
      if (errorHandler.isOk()) {
        break;
      } else {
        List<String> dependencies = errorHandler.getDependencies();
        if (dependencies.isEmpty()) // No dependencies, everything is fine.
        break;
        missingDependencies.addAll(dependencies);
        if (!addDependencyToQueue(queue, mibDirectoryFiles)) break;
      }
    }
    if (errorHandler.isNotOk()) // There are still non-dependency related problems.
    return false;

    // Extracting the module from compiled MIB.
    LogUtils.infof(this, "The MIB %s has been parsed successfully.", mibFile.getAbsolutePath());
    module = getModule(mib, mibFile);
    return module != null;
  }
 /* (non-Javadoc)
  * @see org.opennms.features.vaadin.mibcompiler.services.MibParser#getEvents(java.lang.String)
  */
 public Events getEvents(String ueibase) {
   if (module == null) {
     return null;
   }
   LogUtils.infof(
       this, "Generating events for %s using the following UEI Base: %s", module.getId(), ueibase);
   try {
     return convertMibToEvents(module, ueibase);
   } catch (Throwable e) {
     String errors = e.getMessage();
     if (errors == null || errors.trim().equals(""))
       errors =
           "An unknown error accured when generating events objects from the MIB "
               + module.getId();
     LogUtils.errorf(this, e, "Event parsing error: %s", errors);
     errorHandler.addError(errors);
     return null;
   }
 }
 /* (non-Javadoc)
  * @see org.opennms.features.vaadin.mibcompiler.MibParser#getFormattedErrors()
  */
 public String getFormattedErrors() {
   return errorHandler.getMessages();
 }