Previous Next Contents Index


17


The mibgen Compiler

The Java Dynamic Management Kit provides the mibgen compiler for compiling SNMP MIBs into Java source code. The mibgen compiler generates Java source code for agents and managers.


Using the mibgen Compiler

The mibgen compiler is a command line utility that enables you to generate Java source code that implements SNMP MIBs. MIBs can be expressed using SNMP v1 or SNMP v2 syntax. To use mibgen to compile your MIBs, type the command for your operating environment:

Options for mibgen

The mibgen tool can be invoked with the following options:

mibgen [-n] [-d dir] [-tp packageName] [-desc] [-a] [-m] [-mo]
       [-p prefix] [-help] mib1...mibN


-n

Parses the MIB files without generating Java code. Use this option to check the syntax of your MIB files.

-d dir

Specifies the destination directory for the generated code.

-tp packageName

Assigns the Java package packageName to each generated class.

-desc

Includes the comments, that is, the DESCRIPTION clause of OBJECT-TYPE in the MIB file, as Java comments in the generated Java code.

-a

Generates Java code for all the specified MIB files. If you do not specify this parameter, only the first MIB specified is used to generate code.

-m

Generates Java code for the SNMP manager and the agent. The -m option cannot be used with the -n option.

-mo

Generates Java code for the SNMP manager only. The -mo option cannot be used with the -n option.

-p prefix

Use the specified prefix when generating Java code.

-help

Prints a usage message describing the options for mibgen.

mib1...mibN

Specifies the list of MIB files to be compiled. Each MIB in the list must be separated from the one that precedes it by a space.


Output From the mibgen Compiler

The mibgen tool generates these types of Java source code:

FIGURE 17-1 shows the mibgen output from a MIB containing two groups and two tables.

FIGURE  17-1 Output From the mibgen Compiler
For information on how to start mibgen, see "Using the mibgen Compiler" on page 225.

Representation of the Whole MIB

The mibgen compiler generates a Java file that represents and initializes the whole MIB. This class extends the class SnmpMib. The class SnmpMib is an abstract Java class in the com.sun.jaw.snmp.agent.SnmpMib package and is a logical abstraction of an SNMP MIB. The SNMP adaptor uses the SnmpMib class to implement agent behavior. You can edit the generated file if, for example, you want to remove support for a whole group from your MIB.

Files representing whole MIBs are named using the module name specified in the MIB definition. Special characters are removed by mibgen and replaced with "_".

Representation of the Whole MIB in a MibStore

The mibgen compiler generates a Java file that contains the code required for representing a whole MIB in an SNMP manager MibStore. This class extends the com.sun.jaw.snmp.manager.MibStore class which maintains a database of management information base (MIB) variables. A name can be resolved against the database. The file is used by the SNMP manager API. It contains metadata definitions for the compiled MIB. The metadata can then be loaded into the SNMP manager MibStore.

The file is only generated when mibgen is invoked using the -m or -mo options and is called MIBnameStore.java.

Classes Representing SNMP Groups

For each SNMP group defined in the MIB, mibgen generates:

Skeletal M-Beans Representing Groups

The mibgen compiler generates an m-bean for each group defined in the MIB. These skeletal m-beans need to be completed by adding implementation-specific code (access methods). The generated code is initialized with default values for the different MIB variables. Therefore, if you compile the generated code directly, you will obtain a running agent. In this case, values returned by the agent when querying the MIBs will not be meaningful. See "Implementing Access Methods for Simple Groups" on page 237 for more information on how to add access methods.

M-beans generated from groups are named using the group names specified in the MIB definition.

Metadata Files

As well as generating skeletal m-beans to represent each group, mibgen generates a metadata file. The metadata file contains Java source code that provides the "SNMP view" of the m-bean. Metadata files do not need to be modified. For metadata files, the Meta suffix is added.

Classes Representing SNMP Tables

For each SNMP table defined in the MIB, mibgen generates:

Class Containing the SNMP View of a Table

This class contains all the management of the table index. The class is also prefixed with Table followed by the name of the table.

Skeletal M-Beans Representing SNMP Table Entries

For each table in a MIB, mibgen generates an m-bean representing a table entry. These skeletal m-beans need to be completed by adding implementation specific code (access methods). The generated code is initialized with default values for table entry fields. Therefore, if you compile the generated code directly, you will obtain a running agent. In this case, values returned by the agent when querying the MIBs will not be meaningful. See "Implementing Access Methods for Tables and Entries" on page 239 for more information on how to add access methods.

M-beans generated from table entries are named using the entry names specified in the MIB definition.

Metadata Files

In addition to generating skeletal m-beans to represent each table entry, mibgen generates a Java file containing the "SNMP view" of the m-bean. Metadata files do not need to be modified. For metadata files, the Meta suffix is added.

Classes Representing SNMP Enumerated Types

The mibgen compiler generates a specific class for each enumerated type defined in the MIB. This class contains all the possible values defined in the enumerated type. The generated class extends the generic class Enumerated, defined in the com.sun.jaw.reference.common package. The job tool and the HTML adaptor are able to use the Enumerated class to display all the labels contained in an enumeration. The mibgen tool is able to handle enumerated types defined as part of a type definition or in-line definition.

Generated code representing SNMP enumerated types is prefixed with Enum followed by the type name or the variable name for in-line definition.


Note - The mibgen tool has an option that allows you to prefix the names of all generated files with a specific string. See "Options for mibgen" on page 226.
In MIB II, TCP connection states are represented by an enumeration containing all the possible states for a TCP connection. The mibgen tool generates a Java class named EnumtcpConnState to represent the enumeration, as shown in CODE EXAMPLE 17-1.

CODE  EXAMPLE  17-1     Representing MIB II TCP Connection States 

/**
 * The class is used for representing "tcpConnState".
 */
public class EnumtcpConnState extends Enumerated implements Serializable {
   protected static Hashtable intTable= new Hashtable();
   protected static Hashtable stringTable= new Hashtable() ;
   static {
      intTable.put(new Integer(10), "closing");
      intTable.put(new Integer(12), "deleteTCB");
      intTable.put(new Integer(2), "listen");
      intTable.put(new Integer(3), "synSent");
      intTable.put(new Integer(1), "closed");
      intTable.put(new Integer(9), "lastAck");
      intTable.put(new Integer(5), "established");
      intTable.put(new Integer(7), "finWait2");
      intTable.put(new Integer(6), "finWait1");
      intTable.put(new Integer(4), "synReceived");
      intTable.put(new Integer(11), "timeWait");
      intTable.put(new Integer(8), "closeWait");
      stringTable.put("closing", new Integer(10));
      stringTable.put("deleteTCB", new Integer(12));
      stringTable.put("listen", new Integer(2));
      stringTable.put("synSent", new Integer(3));
      stringTable.put("closed", new Integer(1));
      stringTable.put("lastAck", new Integer(9));
      stringTable.put("established", new Integer(5));
      stringTable.put("finWait2", new Integer(7));
      stringTable.put("finWait1", new Integer(6));
      stringTable.put("synReceived", new Integer(4));
      stringTable.put("timeWait", new Integer(11));
      stringTable.put("closeWait", new Integer(8));
}


CODE EXAMPLE 17-2 shows how to create an enumerated value to represent the closed state of a TCP connection.


CODE  EXAMPLE  17-2     Representing the Closed State of a TCP Connection

EnumtcpConnState state= new EnumtcpConnState("closed");


Information Mapping

For each group defined in your MIB, mibgen generates an m-bean. Each variable in the group is represented as a property of the m-bean. If the MIB allows read access to a variable, mibgen generates a getter method for the corresponding property. If the MIB allows write access to a variable, mibgen generates a setter method for the property. Tables are seen as indexed properties whose type corresponds to the table entry type. The SNMP view of the table is maintained by a specific table object contained in the generated m-bean.The mibgen compiler maps the MIB variable syntax to a well-defined Java type. The mapping is performed according to the set of syntax mapping rules given in TABLE 17-1.

TABLE  17-1 Syntax Mapping Rules 

SNMP Syntax
M-Bean Syntax

Object identifier

Java String object

IP address

Java String object

Display String

Java String object

Opaque string

Array of Byte objects

Integer

Java Integer object

Counter

Java Integer object

Integer64

Java Long object

Counter64

Java Long object

TimeTicks

Java Integer object

Truth value

Java Boolean object

Enumerated List of Integers

Specific Enumerated class


The m-beans that mibgen generates do not have any dependency on specific SNMP objects. Therefore, they can very easily be browsed or integrated into the different Java Dynamic Management Kit components. The translation between the SNMP syntax and the m-bean syntax is performed by the metadata.


Note - To change the Java type of a specific MIB variable within a generated m-bean, edit the metadata file associated with the group that contains the variable.


Copyright 1998 Sun Microsystems, Inc. 901 San Antonio Road, Palo Alto, California 94303 U.S.A.
Copyright in French

Previous Next Contents Index