public final class SmiManager extends Object implements OIDTextFormat, VariableTextFormat, SmiCompiler
SmiManager Pro class manages the Structure of Management Information
(SMI) specifications. SMIv1 and v2 MIB modules can be parsed and compiled to
a MIB repository which provides its content to SNMP4J through a OID and Variable
formatter and parser.| Modifier and Type | Class and Description |
|---|---|
static class |
SmiManager.NonPrintableStringFormat
Defines the format for non-printable strings in formatted object identifiers (OIDs).
|
static class |
SmiManager.OctetStringDefaultFormat |
static class |
SmiManager.OIDFormat
The OIDFormat defines the formatting of
OID values. |
SmiCompiler.OverwriteMode, SmiCompiler.Strictness, SmiCompiler.TargetMode| Constructor and Description |
|---|
SmiManager(String licenseKey,
File repositoryDirectory)
Creates a
SmiManager that provides the MIB information to SNMP4J through
implementations of the OIDTextFormat and VariableTextFormat
interfaces. |
SmiManager(String licenseKey,
RepositoryDriver repositoryDriver)
Creates a
SmiManager that provides the MIB information to SNMP4J through
implementations of the OIDTextFormat and VariableTextFormat
interfaces. |
| Modifier and Type | Method and Description |
|---|---|
String[] |
compile(File textOrZipFile)
Compiles a MIB module from a given text or ZIP file.
|
List<CompilationResult> |
compile(File[] files,
CompilationMonitor compilationMonitor,
boolean loadIntoRepository,
boolean updateExistent,
boolean compileLeniently)
Compiles an array of text or ZIP files provided as
Files. |
String[] |
compile(NamedInputStream mibFile)
Compiles a MIB module from a given InputStream and returns the names
of the compiled MIB modules from the stream.
|
List<CompilationResult> |
compile(NamedInputStream[] files,
CompilationMonitor compilationMonitor,
boolean loadIntoRepository,
boolean updateExistent,
boolean compileLeniently)
Deprecated.
Use
compile(NamedInputStream[], CompilationMonitor, com.snmp4j.smi.SmiCompiler.TargetMode,
com.snmp4j.smi.SmiCompiler.OverwriteMode, com.snmp4j.smi.SmiCompiler.Strictness) instead which provides a cleaner
interface. |
List<CompilationResult> |
compile(NamedInputStream[] files,
CompilationMonitor compilationMonitor,
SmiCompiler.TargetMode compilerTargetMode,
SmiCompiler.OverwriteMode overwriteMode,
SmiCompiler.Strictness strictness)
Compiles an array of text or ZIP files provided as
NamedInputStreams. |
List<String> |
deleteModule(String moduleName,
boolean forceDeletion)
Deletes a MIB module from the MIB repository directory if it is not used/imported
by other MIB modules in the repository.
|
SmiObject |
findRootSmiObject()
Gets the SMI object with the lexicographic smallest OID within the currently loaded MIB modules.
|
SmiModule |
findSmiModule(String moduleName)
Gets the
SmiModule with the specified module name. |
SmiObject |
findSmiObject(OID oid)
Gets the SMI object defined for the specified
OID. |
<S extends SmiObject> |
findSmiObject(String moduleName,
SmiObjectFilter<S> filter)
Gets the SMI object defined for the specified
OID. |
SmiObject |
findSmiObject(String moduleName,
String objectName)
Gets the SMI object defined for the specified object name.
|
String |
format(int[] value) |
String |
format(OID instanceOID,
Variable variable,
boolean withOID) |
String |
formatForRoundTrip(int[] value) |
String |
getIndexStringFromVariables(OID table,
List<VariableBinding> indexObjects) |
Variable[] |
getIndexVariables(OID table,
OID index)
Gets the variable values for all sub-index objects for the specified table entry.
|
String[] |
getLoadedModuleNames()
Returns the MIB module names of the MIB modules loaded into the
SmiManager. |
SmiManager.OctetStringDefaultFormat |
getOctetStringDisplayHint()
Gets the current OCTET STRING default display format.
|
char |
getOidFixedLengthStringQuote()
Gets the quote character for fixed length formatted sub-index OIDs.
|
String |
getOidFixedLengthStringQuoteEscapeSequence()
Gets the sequence to escape character sequence for fixed length formatted sub-index OID quotes.
|
SmiManager.OIDFormat |
getOidFormat()
Gets the current
SmiManager.OIDFormat (default is
SmiManager.OIDFormat.ObjectNameAndDecodedIndex4RoundTrip). |
SmiManager.NonPrintableStringFormat |
getOidFormatNonPrintableStrings()
Gets the
SmiManager.NonPrintableStringFormat for strings in formatted OIDs. |
char |
getOidStringQuoteEscapeCharacter()
Gets the OID string escape character for quotes.
|
char |
getOidVariableLengthStringQuote()
Gets the quote character for variable length formatted sub-index OIDs.
|
String |
getOidVariableLengthStringQuoteEscapeSequence()
Gets the sequence to escape character sequence for variable length formatted sub-index OID quotes.
|
boolean |
isIncludeUnknownErrorsCause()
Returns the flag that controls whether exceptions or other unknown error messages during parsing and storing
of MIB modules to the repository should be included in the error message or not.
|
boolean |
isModuleLoaded(String moduleName)
Checks if the MIB module with the specified name is already loaded into the SmiManager
from disk.
|
String[] |
listModules()
Returns the MIB module names of the MIB modules available in the repository directory.
|
boolean |
loadModule(String moduleName)
Loads a MIB module from disk (MIB repository) into memory.
|
Variable |
parse(int smiSyntax,
String text) |
Variable |
parse(OID classOrInstanceOID,
String text) |
int[] |
parse(String text) |
VariableBinding |
parseVariableBinding(String text) |
void |
resetOidQuoting()
Resets the OID quotation characters and escape sequences to the default (which is highly recommended to be used).
|
void |
setIncludeUnknownErrorsCause(boolean includeUnknownErrorsCause)
Sets the flag that controls whether exceptions or other unknown error messages during parsing and storing
of MIB modules to the repository should be included in the error message or not.
|
void |
setOctetStringDisplayHint(SmiManager.OctetStringDefaultFormat octetStringDisplayHint)
Sets the display format for
OctetString values as
defined by the enumeration SmiManager.OctetStringDefaultFormat. |
void |
setOidFixedLengthStringQuote(char oidFixedLengthStringQuote)
Sets the quote character for fixed length formatted sub-index OIDs.
|
void |
setOidFixedLengthStringQuoteEscapeSequence(String oidFixedLengthStringQuoteEscapeSequence)
Sets the sequence to escape character sequence for fixed length formatted sub-index OID quotes.
|
void |
setOidFormat(SmiManager.OIDFormat oidFormat)
Sets the
SmiManager.OIDFormat to be used to render object identifiers. |
void |
setOidFormatNonPrintableStrings(SmiManager.NonPrintableStringFormat oidFormatNonPrintableStrings)
Sets the
SmiManager.NonPrintableStringFormat for strings in formatted OIDs. |
void |
setOidStringQuoteEscapeCharacter(char oidStringQuoteEscapeCharacter)
Sets the OID string escape character for quotes.
|
void |
setOidVariableLengthStringQuote(char oidVariableLengthStringQuote)
Sets the quote character for variable length formatted sub-index OIDs.
|
void |
setOidVariableLengthStringQuoteEscapeSequence(String oidVariableLengthStringQuoteEscapeSequence)
Sets the sequence to escape character sequence for variable length formatted sub-index OID quotes.
|
void |
setSmiErrorTextResourceBundle(String resourceBundleClassName)
Sets the class name of the custom resource bundle for SMI error texts.
|
static List<String> |
tokenizeQuotedString(String text,
String quotes,
char escapeChar,
String delimiter,
boolean includeQuotes)
Tokenize a string by a delimiter string but do not split quoted strings with the supplied quote characters.
|
boolean |
unloadModule(String moduleName)
Unload a MIB module from the
SmiManager memory. |
public SmiManager(String licenseKey, File repositoryDirectory) throws IOException
SmiManager that provides the MIB information to SNMP4J through
implementations of the OIDTextFormat and VariableTextFormat
interfaces.
In addition, MIB information can be retrieved through the methods of this class too.
To add MIB support to your SNMP4J based application, simply add the following code in your main class:
static {
SmiManager smiManager = new SmiManager("myLicenseKey", new File("myEmptyDirectory"));
SNMP4JSettings.setOIDTextFormat(smiManager);
SNMP4JSettings.setVariableTextFormat(smiManager);
// If you need to disable full index formatting,
// then choose a different format below and uncomment the line:
// smiManager.setOidFormat(OIDFormat.ObjectNameAndDecodedIndex4RoundTrip);
}
licenseKey - the license key string you received with the purchase a SNMP4J-SMI license or its evaluation.
You may provide null to use SNMP4J-SMI for free. But then you may not use any
MIB modules of the "enterprise" OID subtree.repositoryDirectory - an empty directory where the SmiManager can read and store compiled MIB modules persistently.
This directory may contain files from an existing MIB repository of AGENT++ AgenPro, MIB Designer, or
MIB Explorer. It must not contain any plain text (i.e., uncompiled) MIB files.IOException - when the repositoryDirectory is not accessible.public SmiManager(String licenseKey, RepositoryDriver repositoryDriver)
SmiManager that provides the MIB information to SNMP4J through
implementations of the OIDTextFormat and VariableTextFormat
interfaces.
In addition, MIB information can be retrieved through the methods of this class too.
To add MIB support to your SNMP4J based application, simply add the following code in your main class:
static {
SmiManager smiManager = new SmiManager("myLicenseKey", new File("myEmptyDirectory"));
SNMP4JSettings.setOIDTextFormat(smiManager);
SNMP4JSettings.setVariableTextFormat(smiManager);
}
licenseKey - the license key string you received with the purchase a SNMP4J-SMI license or its evaluation.repositoryDriver - a RepositoryDriver instance which handles the reading and writing of MIB modules to disk,
database, or other persistent storage.public SmiManager.OIDFormat getOidFormat()
SmiManager.OIDFormat (default is
SmiManager.OIDFormat.ObjectNameAndDecodedIndex4RoundTrip).setOidFormat(com.snmp4j.smi.SmiManager.OIDFormat).public void setOidFormat(SmiManager.OIDFormat oidFormat)
SmiManager.OIDFormat to be used to render object identifiers.
To get numeric OIDs (e.g., "1.3.6.1.2.1") use SmiManager.OIDFormat.Numbers.
To get object name prefix and numeric suffix (e.g., "ifDescr.0") use
SmiManager.OIDFormat.ObjectNameNumIndex, which is the default.
To get object name prefix and decoded index suffix (e.g.,
"vacmViewTreeFamilyType.demoReadView.mib-2" instead of
"1.3.6.1.6.3.16.1.5.2.1.4.12.100.101.109.111.82.101.97.100.86.105.101.119.6.1.3.6.1.2.1")
use SmiManager.OIDFormat.ObjectNameAndDecodedIndex.
To get object name prefix and decoded index suffix for round-trip (e.g.,
"vacmViewTreeFamilyType."demoReadView".'mib-2'" instead of
"1.3.6.1.6.3.16.1.5.2.1.4.12.100.101.109.111.82.101.97.100.86.105.101.119.6.1.3.6.1.2.1")
use SmiManager.OIDFormat.ObjectNameAndDecodedIndex4RoundTrip.oidFormat - an SmiManager.OIDFormat value.public SmiManager.OctetStringDefaultFormat getOctetStringDisplayHint()
SmiManager.OctetStringDefaultFormat which is used to display
OCTET STRING (and enumerated) values, if there is no MIB defined DISPLAY-HINT
available. The default is SmiManager.OctetStringDefaultFormat.ASCII.public void setOctetStringDisplayHint(SmiManager.OctetStringDefaultFormat octetStringDisplayHint)
OctetString values as
defined by the enumeration SmiManager.OctetStringDefaultFormat.
If this value is set to any SmiManager.OctetStringDefaultFormat other than
SmiManager.OctetStringDefaultFormat.MIB then enumerated values
(syntax INTEGER with labels) will be formatted by
format(org.snmp4j.smi.OID, org.snmp4j.smi.Variable, boolean) as number. Otherwise
as label(n) where n is the INTEGER number and label the
enum label.octetStringDisplayHint - the display format. SmiManager.OctetStringDefaultFormat.MIB is the default
and displays the values as defined in the MIB. If there is no DISPLAY-HINT
in the MIB format, OCTET STRING values will be displayed in ASCII format.public final String[] listModules() throws IOException
IOException - if the repository directory cannot be read and listed.public final String[] getLoadedModuleNames() throws IOException
SmiManager.IOExceptionpublic final boolean loadModule(String moduleName)
SmiManager instance.moduleName - the MIB module name (e.g. "SNMPv2-MIB").true if the module and all the MIB objects it is importing from other modules could be
loaded successfully, false otherwise.public final boolean unloadModule(String moduleName)
SmiManager memory. The corresponding MIB module in
the MIB repository directory will not be modified.moduleName - a MIB module name.true if the MIB module was in memory and is now removed, false otherwise.public final boolean isModuleLoaded(String moduleName)
moduleName - a MIB module name.true if the MIB module is loaded, false otherwise.public final List<String> deleteModule(String moduleName, boolean forceDeletion)
moduleName - the MIB module name of the module which should be deleted.forceDeletion - if true, it will not be checked if other MIB modules in the repository
import from the MIB module with name moduleName, otherwise that check
is performed and the MIB module will not be deleted if it is still used by depending
MIB modules.
WARNING: Deleting a MIB module which is used by others can lead to errors
while accessing those modules.moduleName.
If the list contains only one element and that is the module's name, then
the RepositoryDriver did not allow to delete the MIB module or an IO error
occurred.public final String[] compile(File textOrZipFile) throws SmiParseException, FileNotFoundException
textOrZipFile - an existing text or ZIP file. A ZIP file must end with ".ZIP" or ".zip" to be recognized as ZIP file.SmiParseException - if the MIB module contains a syntax or semantic error.FileNotFoundException - if the specified cannot be found or opened for reading.public void resetOidQuoting()
oidFixedLengthStringQuote = '\'';
oidVariableLengthStringQuote = '"';
oidFixedLengthStringQuoteEscapeSequence = "\\'";
oidVariableLengthStringQuoteEscapeSequence = "\\\"";
oidStringQuoteEscapeCharacter = '\\';
public char getOidFixedLengthStringQuote()
public void setOidFixedLengthStringQuote(char oidFixedLengthStringQuote)
oidFixedLengthStringQuote - a quote character.public char getOidVariableLengthStringQuote()
public void setOidVariableLengthStringQuote(char oidVariableLengthStringQuote)
oidVariableLengthStringQuote - a quote character.public String getOidFixedLengthStringQuoteEscapeSequence()
public void setOidFixedLengthStringQuoteEscapeSequence(String oidFixedLengthStringQuoteEscapeSequence)
oidFixedLengthStringQuoteEscapeSequence - a quote escape sequence.public String getOidVariableLengthStringQuoteEscapeSequence()
public void setOidVariableLengthStringQuoteEscapeSequence(String oidVariableLengthStringQuoteEscapeSequence)
oidVariableLengthStringQuoteEscapeSequence - a quote escape sequence.public char getOidStringQuoteEscapeCharacter()
getOidFixedLengthStringQuoteEscapeSequence() and getOidVariableLengthStringQuoteEscapeSequence().
Otherwise the results of formatting and parsing of OIDs are undefined.public void setOidStringQuoteEscapeCharacter(char oidStringQuoteEscapeCharacter)
getOidFixedLengthStringQuoteEscapeSequence() and getOidVariableLengthStringQuoteEscapeSequence().
Otherwise the results of formatting and parsing of OIDs are undefined!oidStringQuoteEscapeCharacter - the escape character (default is the backslash character) for escaping OID string quotes.public final String[] compile(NamedInputStream mibFile) throws SmiParseException
mibFile - a stream containing the contents of one or more MIB module specifications.SmiParseException - if the specification has SMI syntax or semantic errors.public List<CompilationResult> compile(File[] files, CompilationMonitor compilationMonitor, boolean loadIntoRepository, boolean updateExistent, boolean compileLeniently) throws IOException
Files. The compilation is executed in
three phases:
CompilationMonitor interface.files - an array of File instances where each instance may represent a text or ZIP file. ZIP file names have to
end with ".ZIP" or ".zip".compilationMonitor - an optional CompilationMonitor implementation to monitor the progress of the compilation.loadIntoRepository - if true the compiled MIB modules will not only be stored into the internal MIB repository,
but also directly loaded into the MIB cache so that they can be used right after this method call completes.updateExistent - if true compiled MIB modules will replace existing MIB modules in the MIB repository with the
same module names.compileLeniently - if true syntax and semantic checks are limited to the necessary minimum. A MIB module
that can be successfully parsed in lenient mode may contain severe standard violations and even errors
that can affect proper formatting and value parsing support for SNMP4J. Use this option only if you can
handle the risk or if you are not able to fix or let fix the errors in the MIB specification(s).CompilationResult instances. Each instance represents an input file. For ZIP files,
a CompilationResult instance is returned for each ZIP file entry.IOException - if any IO operation fails when accessing the input files (InputStreams).public List<CompilationResult> compile(NamedInputStream[] files, CompilationMonitor compilationMonitor, SmiCompiler.TargetMode compilerTargetMode, SmiCompiler.OverwriteMode overwriteMode, SmiCompiler.Strictness strictness) throws IOException
SmiCompilerNamedInputStreams. The compilation is executed in
three phases:
NamedInputStreams are loaded into memory.CompilationMonitor interface.compile in interface SmiCompilerfiles - an array of NamedInputStream instances where each instance may represent a text or ZIP file.compilationMonitor - an optional CompilationMonitor implementation to monitor the progress of the compilation.compilerTargetMode - with the mode SmiCompiler.TargetMode.storeIntoRepositoryAndLoad the compiled MIB modules will not only be stored
into the internal MIB repository, but also directly loaded into the MIB cache so that they can be used right
after this method call completes. With SmiCompiler.TargetMode.dryRun no modifications to the MIB repository or MIB
cache are done and only the result of the syntax check is returned.overwriteMode - specifies whether compiled MIB modules will replace existing MIB modules in the MIB repository as well as
in the cache with the same module names or not.strictness - if SmiCompiler.Strictness.lenient is used then syntax and semantic checks are
limited to the necessary minimum. A MIB module that can be successfully parsed in lenient mode may contain
severe standard violations and even errors
that can affect proper formatting and value parsing support for SNMP4J. Use this option only if you can
handle the risk or if you are not able to fix or let fix the errors in the MIB specification(s).CompilationResult instances. Each instance represents an input file. For ZIP files,
a CompilationResult instance is returned for each ZIP file entry.IOException - if any IO operation fails when accessing the input files (InputStreams).public List<CompilationResult> compile(NamedInputStream[] files, CompilationMonitor compilationMonitor, boolean loadIntoRepository, boolean updateExistent, boolean compileLeniently) throws IOException
compile(NamedInputStream[], CompilationMonitor, com.snmp4j.smi.SmiCompiler.TargetMode,
com.snmp4j.smi.SmiCompiler.OverwriteMode, com.snmp4j.smi.SmiCompiler.Strictness) instead which provides a cleaner
interface.NamedInputStreams. The compilation is executed in
three phases:
NamedInputStreams are loaded into memory.CompilationMonitor interface.files - an array of NamedInputStream instances where each instance may represent a text or ZIP file.compilationMonitor - an optional CompilationMonitor implementation to monitor the progress of the compilation.loadIntoRepository - if true the compiled MIB modules will not only be stored into the internal MIB repository,
but also directly loaded into the MIB cache so that they can be used right after this method call completes.updateExistent - if true compiled MIB modules will replace existing MIB modules in the MIB repository with the
same module names.compileLeniently - if true syntax and semantic checks are limited to the necessary minimum. A MIB module
that can be successfully parsed in lenient mode may contain severe standard violations and even errors
that can affect proper formatting and value parsing support for SNMP4J. Use this option only if you can
handle the risk or if you are not able to fix or let fix the errors in the MIB specification(s).CompilationResult instances. Each instance represents an input file. For ZIP files,
a CompilationResult instance is returned for each ZIP file entry.IOException - if any IO operation fails when accessing the input files (InputStreams).public final String format(OID instanceOID, Variable variable, boolean withOID)
format in interface VariableTextFormatpublic final VariableBinding parseVariableBinding(String text) throws ParseException
parseVariableBinding in interface VariableTextFormatParseExceptionpublic final Variable parse(OID classOrInstanceOID, String text) throws ParseException
parse in interface VariableTextFormatParseExceptionpublic final Variable parse(int smiSyntax, String text) throws ParseException
parse in interface VariableTextFormatParseExceptionpublic final String format(int[] value)
format in interface OIDTextFormatpublic String formatForRoundTrip(int[] value)
formatForRoundTrip in interface OIDTextFormatpublic final int[] parse(String text) throws ParseException
parse in interface OIDTextFormatParseExceptionpublic static List<String> tokenizeQuotedString(String text, String quotes, char escapeChar, String delimiter, boolean includeQuotes)
text - the text to tokenize (split into pieces).quotes - the list of quote characters to process.escapeChar - the escape character that can be used to escape quotes.delimiter - the delimiter string that separates the tokens.includeQuotes - if true quotes will be return as part of the returned tokens, otherwise
they will be removed from the token boundaries.public SmiManager.NonPrintableStringFormat getOidFormatNonPrintableStrings()
SmiManager.NonPrintableStringFormat for strings in formatted OIDs. A string is not printable
if it contains any ISO control characters as defined by Character.isISOControl(char)
except if these ISO control characters are all whitespace characters as defined by
Character.isWhitespace(char) and not '\u001C'-'\u001F'.public void setOidFormatNonPrintableStrings(SmiManager.NonPrintableStringFormat oidFormatNonPrintableStrings)
SmiManager.NonPrintableStringFormat for strings in formatted OIDs. The default is
SmiManager.NonPrintableStringFormat.asIsoidFormatNonPrintableStrings - the new format for non-prinatable strings (must not be null).public Variable[] getIndexVariables(OID table, OID index)
table - the table entry OID (= "tableOID.1") the index OID belongs to.index - the index OID which should be split into its sub-index values (variables).null if the table entry OID cannot be resolved, otherwise the index
OID is split into the sub-index variables which are then returned as an array.public String getIndexStringFromVariables(OID table, List<VariableBinding> indexObjects)
public SmiObject findSmiObject(OID oid)
OID. For SMI objects that have no OID,
i.e. TEXTUAL CONVENTIONS you will have to use findSmiObject(String, String) instead.oid - the object identifier of the SMI object to return.public SmiObject findRootSmiObject()
SmiTextualConvention
instances must not be returned) within all loaded MIB modules. If no module with an object with OID is
loaded then null will be returned.public SmiModule findSmiModule(String moduleName)
SmiModule with the specified module name. Note: The module name
is an uppercase name and is always different from the MODULE-IDENTITY object name which starts with a lower case
letter.
For SMIv2 the returned SmiModule is also a SmiModuleIdentity!
moduleName - a MIB module name (e.g. SNMPv2-MIB).SmiModule with the module name moduleName or null
if such a module has not been loaded yet with loadModule(String).public SmiObject findSmiObject(String moduleName, String objectName)
findSmiObject(org.snmp4j.smi.OID) instead
if possible in order to avoid ambiguities when not able to provide a MIB module name.moduleName - the name of the MIB module, the object is defined in.objectName - the name of the SMI object to return.public <S extends SmiObject> List<S> findSmiObject(String moduleName, SmiObjectFilter<S> filter)
OID. For SMI objects that have an OID,
i.e. OBJECT-TYPE it is recommend to use the findSmiObject(org.snmp4j.smi.OID) instead
if possible in order to avoid ambiguities when not able to provide a MIB module name.moduleName - the name of the MIB module, the object is defined in or null when objects of all
modules should be returned.public void setIncludeUnknownErrorsCause(boolean includeUnknownErrorsCause)
includeUnknownErrorsCause - if true the message text of caught exceptions during MIB compilation will be included in
the "errUnknownError" message, false otherwise (the default).public boolean isIncludeUnknownErrorsCause()
true if the message text of caught exceptions during MIB compilation will be included in
the "errUnknownError" message, false otherwise (the default).public void setSmiErrorTextResourceBundle(String resourceBundleClassName)
resourceBundleClassName - the class name (including package name) of the resource bundle with the SMI error
texts.
For an example message resource bundle, see the "examples" directory of the
distribution.Copyright © 2015 SNMP4J.com. All Rights Reserved.