Exemplo n.º 1
0
  /* (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;
  }
Exemplo n.º 2
0
  protected SmiParser createParser() throws Exception {
    ClassPathURLListFactory urlListFactory = new ClassPathURLListFactory(LIBSMI_MIBS_URL + "/ietf");

    if (m_version == null || m_version == SmiVersion.V1) {
      urlListFactory.add("RFC1155-SMI");
    }
    if (m_version == null || m_version == SmiVersion.V2) {
      urlListFactory.add("SNMPv2-SMI");
      urlListFactory.add("SNMPv2-TC");
      urlListFactory.add("SNMPv2-CONF");
      urlListFactory.add("SNMPv2-MIB");
    }

    List<URL> urls = urlListFactory.create();
    for (String resource : getResources()) {
      urls.add(getClass().getClassLoader().getResource(resource));
    }

    m_parser = new SmiDefaultParser();
    m_parser.getFileParserPhase().setInputUrls(urls);
    return m_parser;
  }