/ Docs / FAQ / Java API

Frequently Asked Questions - Java API

This document attempts to cover the most common questions regarding the Mibble Java API.

Q1: How do I load a MIB file?

MIB files are loaded by the MibLoader class. This class allows loading MIB files in various ways - by file name, MIB name or from within zip files. Mibble also comes bundled with the standard IANA and IETF MIB files, allowing them to be loaded using only their corresponding MIB names.

See the loadMib() method in the brief introduction for a source code example of simple MIB file loading.

In order to load MIB files from a zip file, it must be present on the CLASSPATH. Thereafter the MibLoader.addResourceDir method is used to specify the subdirectory in the zip file where the MIB files are located.

Q2: How does the MibLoader search for MIB files?

The MibLoader class searches for MIB files in the following order:

  • File names in the search path - The search path is a list of directories where the MibLoader looks for files having the same name as the MIB module to be loaded. The comparison is made on case-insensitive file name, ignoring any file name extensions.
  • Files in the resource path - The resource path is a list of directories for Java ClassLoader resources. The MibLoader looks for a resource with the exact MIB module name in each of these directories.
  • File content in the search path - As a final measure all files in the search path are opened and the first few lines are read to identify their MIB module names. This search is generally slow and should be avoided by naming MIB files after their MIB module name.

Q3: How do I get the OID from a MibSymbol?

An OID is stored in the ObjectIdentifierValue class. Only MibValueSymbols have a value and even then not all values are OID:s. The proper way to get the OID is to first check for a MibValueSymbol and then for an ObjectIdentiferValue with the instanceof operator.

See the extractOid() method in the brief introduction for a source code example.

Q4: How do I find a symbol from an OID?

If you know in which MIB the OID belongs, you can use the Mib.getSymbolByOid() method. Otherwise you will have to search all loaded MIB:s for the best (longest) match using the same method.

Q5: How do I walk the OID tree?

The ObjectIdentifierValue class structures the OID:s in a tree structure. It also provides methods for retrieving the parent and child OID:s, making it possible to traverse the whole OID tree.

If you only wish to traverse the OID tree for a certain MIB file, care must be taken to examine the symbols associated with each ObjectIdentifierValue. The OID tree is linked together between all the MIB:s loaded by the same MibLoader, so any OID:s not belonging to the right MIB must be filtered out. This can be done by examining the symbols returned from ObjectIdentifierValue.getSymbol() and MibSymbol.getMib() methods.

Q6: How do I know the type of a MibSymbol?

In general, you use the instanceof operator. That is, first you must check if the MibSymbol is a MibTypeSymbol or a MibValueSymbol by using instanceof.

Once established which of the two, you can get the MibType through the getType() method in either of the two classes. The exact MibType can then be established in the following ways:

  • Using instanceof to determine subclass - With the instanceof operator the correct class the type object can be determined (see the MibType API for a list of all subclasses).
  • Checking the type tag - Each type has one or more associated MibTypeTags. These are unique for most of the standard ASN.1 types and can be checked with the MibType.hasTag() method.

If you wish to find the SNMP macro type of a symbol, use the first of the above methods as the SNMP macro types generally have no type tags. On the other side, to determine the SYNTAX of an OBJECT-TYPE, you should use type tags as they make it possible to distinguish between different INTEGER types like TimeTick, Counter32 and Gauge32.

Q7: How do I recognize an IpAddress or TimeTick type?

Use MibTypeTags to distinguish them from the base INTEGER or OCTET STRING types. Both have APPLICATION type tags set, whereas the basic ASN.1 types only have a UNIVERSAL type tag.

You can check the exact type tag present by first looking into either the RFC1155-SMI or SNMPv2-SMI MIB files to check which type tag is used for the type. Then use the MibType.hasTag() method to check the type in your code.

Q8: How do I distinguish between Gauge32 and Unsigned32?

Normally it is not necessary to distinguish between the Gauge32 and Unsigned32 types as they have the exact same definition and are therefore identical when sent over the wire. In some cases, it is desireable to separate them in a manager for semantical reasons, however.

The only way to do this in Mibble is by checking the list of type references, i.e. the list of definitions that was used to resolve a particular type. In this case, a call to MibType.hasReferenceTo("Gauge32") would only return true for a Gauge32 type and not for an Unsigned32.

Q9: How do I get the fields from an OBJECT-TYPE symbol?

First use the instanceof operator to determine that the symbol is indeed a MibValueSymbol. Thereafter you must check, again with instanceof, that the MibValueSymbol.getType() returns an SnmpObjectType. Once you have an SnmpObjectType object it is easy to get the individual fields, as all have their own accessor methods in that class.

Q10: How do I get the value definitions for a bitset or an enumeration?

The BitSetType and IntegerType can both be constrained to an enumerated set of values. The values are retrieved by calling the getAllSymbols() method, which returns an unordered array of enumeration symbols.

The enumeration names and values are available in the symbols, through the normal MibValueSymbol API and the MibValue.toObject() method.


Copyright © 2003-2007 Per Cederberg. Permission is granted to copy this document verbatim in any medium, provided that this copyright notice is left intact.

Designed & Hosted by Liquid Site Hosting.