java.lang.Object
org.snmp4j.PDU
- All Implemented Interfaces:
Serializable
,BERSerializable
The
PDU
class represents a SNMP protocol data unit. The PDU
version supported by the BER decoding and encoding methods of this class
is v2.
The default PDU type is GET.
- Version:
- 3.0
- Author:
- Frank Fock
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Unauthorized access, see error index.static final int
Bad value in variable binding, see error index.static final int
Unable to commit a value, see error index.protected Integer32
protected Integer32
static final int
An unspecific error caused by a variable binding, see error index.static final int
Denotes a get PDU.static final int
Denotes a SNMPv2c/v3 getbulk PDU.static final int
Denotes a getnext (search) PDU.static final int
The specified object does not exists and presently it cannot be created, see error index.static final int
The variable binding's value is presently inconsistent with the current state of the target object, see error index.static final int
Denotes a SNMPv2c/v3 inform PDU (unprecisely also known as a confirmed notification).static final int
The variable binding is not accessible by the current MIB view, see error index.static final int
The specified object does not exists and cannot be created, see error index.static final int
Operation success (no error).static final int
No such variable binding name, see error index.static final int
Denotes a SNMPv2c/v3 notification PDU (undistinguishable from#NOTIFICATION
).static final int
The variable's value cannot be modified, see error index.static final int
The variable binding is read-only, see error index.static final int
Denotes a SNMPv3 report PDU.protected Integer32
static final int
The resource needed to assign a variable binding's value is presently unavailable, see error index.static final int
Denotes a response PDU.static final int
Denotes a set PDU.static final int
PDU encoding is too big for the transport used.static final int
Denotes a SNMPv2c/v3 notification PDU (undistinguishable from#TRAP
).protected int
static final int
Unable to undo a committed value, see error index.static final int
Denotes a SNMPv1 trap PDU.protected ArrayList
<VariableBinding> static final int
The variable binding's value has the wrong encoding, see error index.static final int
The variable binding's value has the wrong length, see error index.static final int
The variable binding's value has the wrong type, see error index.static final int
The variable binding's value has a value that could under no circumstances be assigned, see error index. -
Constructor Summary
ConstructorDescriptionPDU()
Default constructor.PDU
(int pduType, List<? extends VariableBinding> vbs) Constructs a new PDU from a type and a list ofVariableBinding
instances.Copy constructor which creates a deep copy (clone) of the other PDU. -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(VariableBinding vb) Adds a variable binding to this PDU.void
addAll
(List<? extends VariableBinding> vbs) Adds a list of variable bindings to this PDU (seeadd(VariableBinding vb)
).void
addAll
(VariableBinding[] vbs) Adds an array of variable bindings to this PDU (seeadd(VariableBinding vb)
).void
addAllOIDs
(VariableBinding[] vbs) Adds newVariableBindings
each with the OID of the corresponding variable binding of the supplied array to this PDU (seeaddOID(VariableBinding vb)
).void
Adds a new variable binding to this PDU by using the OID of the suppliedVariableBinding
.void
clear()
Removes all variable bindings from the PDU and sets the request ID to zero.clone()
void
decodeBER
(BERInputStream inputStream) Decodes aVariable
from anInputStream
.static ArrayList
<VariableBinding> decodeVariableBindings
(BERInputStream inputStream) void
encodeBER
(OutputStream outputStream) Encodes aVariable
to anOutputStream
.static void
encodeVariableBindings
(OutputStream outputStream, List<VariableBinding> variableBindings) boolean
get
(int index) Gets the variable binding at the specified position.Gets all theVariableBinding
s from this PDU whose object identifier (OID
) equals or has the same prefix than the specified object identifier or name.getAll()
Gets all theVariableBinding
s of this PDU.int
Returns the length of thisBERSerializable
object in bytes when encoded according to the Basic Encoding Rules (BER).static int
getBERLength
(List<? extends VariableBinding> variableBindings) Computes the length in bytes of the BER encoded variable bindings without including the length of BER sequence length.int
Returns the length of the payload of thisBERSerializable
object in bytes when encoded according to the Basic Encoding Rules (BER).protected int
getBindingList
(OID prefix) Gets a list ofVariableBinding
s whose OID prefix matches the supplied prefix.int
Gets the error index.int
Gets the error status of the PDU.Gets a textual description of the error status.int
Gets the maximum repetitions of repeatable variable bindings in GETBULK requests.int
Gets the number of non repeater variable bindings in a GETBULK PDU.Gets the request ID associated with this PDU.int
getType()
Gets the PDU type.static int
getTypeFromString
(String type) Gets the PDU type identifier for a string representation of the type.static String
getTypeString
(int type) Gets a string representation of the supplied PDU type.getVariable
(OID prefix) Gets the first variable whose OID starts with the specified OID.List
<? extends VariableBinding> Gets the variable binding vector.int
hashCode()
boolean
Checks whether this PDU is a confirmed class PDU.boolean
Checks whether this PDU is aRESPONSE
or [@link PDU#REPORT}.void
remove
(int index) Removes the variable binding at the supplied position.set
(int index, VariableBinding vb) Sets the variable binding at the specified position.void
setErrorIndex
(int errorIndex) Sets the error index.void
setErrorStatus
(int errorStatus) Sets the error status of the PDU.void
setMaxRepetitions
(int maxRepetitions) Sets the maximum repetitions of repeatable variable bindings in GETBULK requests.void
setNonRepeaters
(int nonRepeaters) Sets the number of non repeater variable bindings in a GETBULK PDU.void
setRequestID
(Integer32 requestID) Sets the request ID for this PDU.void
setType
(int type) Sets the PDU type.void
setVariableBindings
(List<? extends VariableBinding> vbs) Sets theVariableBinding
s for this PDU.int
size()
Gets the number of variable bindings in the PDU.toArray()
Returns an array with the variable bindings of this PDU.static String
toErrorStatusText
(int errorStatus) Returns textual description for the supplied error status value.toString()
Returns a string representation of the object.void
trim()
Remove the last variable binding from the PDU, if such an element exists.
-
Field Details
-
GET
public static final int GETDenotes a get PDU.- See Also:
-
GETNEXT
public static final int GETNEXTDenotes a getnext (search) PDU.- See Also:
-
RESPONSE
public static final int RESPONSEDenotes a response PDU.- See Also:
-
SET
public static final int SETDenotes a set PDU.- See Also:
-
V1TRAP
public static final int V1TRAPDenotes a SNMPv1 trap PDU. This type can only be used with instances of thePDUv1
class.- See Also:
-
GETBULK
public static final int GETBULKDenotes a SNMPv2c/v3 getbulk PDU.- See Also:
-
INFORM
public static final int INFORMDenotes a SNMPv2c/v3 inform PDU (unprecisely also known as a confirmed notification).- See Also:
-
TRAP
public static final int TRAPDenotes a SNMPv2c/v3 notification PDU (undistinguishable from#TRAP
).- See Also:
-
NOTIFICATION
public static final int NOTIFICATIONDenotes a SNMPv2c/v3 notification PDU (undistinguishable from#NOTIFICATION
).- See Also:
-
REPORT
public static final int REPORTDenotes a SNMPv3 report PDU.- See Also:
-
noError
public static final int noErrorOperation success (no error).- See Also:
-
tooBig
public static final int tooBigPDU encoding is too big for the transport used.- See Also:
-
noSuchName
public static final int noSuchNameNo such variable binding name, see error index.- See Also:
-
badValue
public static final int badValueBad value in variable binding, see error index.- See Also:
-
readOnly
public static final int readOnlyThe variable binding is read-only, see error index.- See Also:
-
genErr
public static final int genErrAn unspecific error caused by a variable binding, see error index.- See Also:
-
noAccess
public static final int noAccessThe variable binding is not accessible by the current MIB view, see error index.- See Also:
-
wrongType
public static final int wrongTypeThe variable binding's value has the wrong type, see error index.- See Also:
-
wrongLength
public static final int wrongLengthThe variable binding's value has the wrong length, see error index.- See Also:
-
wrongValue
public static final int wrongValueThe variable binding's value has a value that could under no circumstances be assigned, see error index.- See Also:
-
wrongEncoding
public static final int wrongEncodingThe variable binding's value has the wrong encoding, see error index.- See Also:
-
noCreation
public static final int noCreationThe specified object does not exists and cannot be created, see error index.- See Also:
-
inconsistentValue
public static final int inconsistentValueThe variable binding's value is presently inconsistent with the current state of the target object, see error index.- See Also:
-
commitFailed
public static final int commitFailedUnable to commit a value, see error index.- See Also:
-
undoFailed
public static final int undoFailedUnable to undo a committed value, see error index.- See Also:
-
authorizationError
public static final int authorizationErrorUnauthorized access, see error index.- See Also:
-
notWritable
public static final int notWritableThe variable's value cannot be modified, see error index.- See Also:
-
inconsistentName
public static final int inconsistentNameThe specified object does not exists and presently it cannot be created, see error index.- See Also:
-
variableBindings
-
errorStatus
-
errorIndex
-
requestID
-
type
protected int type
-
-
Constructor Details
-
PDU
public PDU()Default constructor. -
PDU
Copy constructor which creates a deep copy (clone) of the other PDU.- Parameters:
other
- thePDU
to copy from.
-
PDU
Constructs a new PDU from a type and a list ofVariableBinding
instances. The list will not be referenced, instead a deep copy of the variable bindings is executed (each variable binding will be cloned).- Parameters:
pduType
- the PDU type.vbs
- the variable bindings.- Since:
- 2.2.4
-
-
Method Details
-
add
Adds a variable binding to this PDU. ANullPointerException
is thrown ifVariableBinding
or itsVariable
isnull
.- Parameters:
vb
- aVariableBinding
instance.
-
addOID
Adds a new variable binding to this PDU by using the OID of the suppliedVariableBinding
. The value portion is thus set tonull
.This method should be used for GET type requests. For SET, TRAP and INFORM requests, the
add(org.snmp4j.smi.VariableBinding)
method should be used instead.- Parameters:
vb
- aVariableBinding
instance.- Since:
- 1.8
-
addAll
Adds an array of variable bindings to this PDU (seeadd(VariableBinding vb)
).- Parameters:
vbs
- an array ofVariableBinding
instances. The instances in the array will be appended to the current list of variable bindings in the PDU.
-
addAll
Adds a list of variable bindings to this PDU (seeadd(VariableBinding vb)
).- Parameters:
vbs
- a list ofVariableBinding
instances. The instances in the list will be appended to the current list of variable bindings in the PDU.- Since:
- 2.2.4
-
addAllOIDs
Adds newVariableBindings
each with the OID of the corresponding variable binding of the supplied array to this PDU (seeaddOID(VariableBinding vb)
).- Parameters:
vbs
- an array ofVariableBinding
instances. For each instance in the supplied array, a new VariableBinding created bynew VariableBinding(OID)
will be appended to the current list of variable bindings in the PDU.- Since:
- 1.8
-
get
Gets the variable binding at the specified position.- Parameters:
index
- a zero based positive integer (0 <= index < {@link #size()}
)- Returns:
- a VariableBinding instance. If
index
is out of bounds an exception is thrown.
-
get
Gets all theVariableBinding
s from this PDU whose object identifier (OID
) equals or has the same prefix than the specified object identifier or name.- Parameters:
objectIdentifierOrName
- a dotted numbered OID string like "1.3.6.1.4.1.4976" or an object name parsable by format as set bySNMP4JSettings.setOIDTextFormat(OIDTextFormat)
. If such a OID format is not set before this method is called, or if theobjectIdentifierOrName
could not be parsed/does not comply to the format rules, then aRuntimeException
will be thrown.- Returns:
- a list of
VariableBinding
s whoseOID
is a parent of the object identifier(s) given as argument. The list is ordered by the given prefix OIDs and within the matches of each prefix OID, the list is ordered by occurrence in thisPDU
. - Since:
- 3.5.0
-
getVariable
Gets the first variable whose OID starts with the specified OID.- Parameters:
prefix
- the searchOID
.- Returns:
- the
Variable
of the firstVariableBinding
whose prefix matchesoid
. If no such element could be found,null
is returned. - Since:
- 2.0
-
getBindingList
Gets a list ofVariableBinding
s whose OID prefix matches the supplied prefix.- Parameters:
prefix
- the searchOID
. Ifnull
, then all variable bindings will be returned.- Returns:
- a List of all
VariableBinding
s whose prefix matchesoid
. If no such element could be found, an empty List is returned.
-
set
Sets the variable binding at the specified position.- Parameters:
index
- a zero based positive integer (0 <= index <
size()
) Ifindex
is out of bounds an exception is thrown.vb
- a VariableBinding instance (null
is not allowed).- Returns:
- the variable binding that has been replaced.
-
remove
public void remove(int index) Removes the variable binding at the supplied position.- Parameters:
index
- a position >= 0 and <size()
.
-
size
public int size()Gets the number of variable bindings in the PDU.- Returns:
- the size of the PDU.
-
getVariableBindings
Gets the variable binding vector.- Returns:
- the internal
List
containing the PDU's variable bindings.
-
setVariableBindings
Sets theVariableBinding
s for this PDU.- Parameters:
vbs
- a list ofVariableBinding
instances which must not be null.- Since:
- 2.1
-
trim
public void trim()Remove the last variable binding from the PDU, if such an element exists. -
setErrorStatus
public void setErrorStatus(int errorStatus) Sets the error status of the PDU.- Parameters:
errorStatus
- a SNMP error status.- See Also:
-
getErrorStatus
public int getErrorStatus()Gets the error status of the PDU.- Returns:
- a SNMP error status.
- See Also:
-
getErrorStatusText
Gets a textual description of the error status.- Returns:
- a String containing an element of the
SnmpConstants.SNMP_ERROR_MESSAGES
array for a valid error status. "Unknown error: <errorStatusNumber>" is returned for any other value.
-
toErrorStatusText
Returns textual description for the supplied error status value.- Parameters:
errorStatus
- an error status.- Returns:
- a String containing an element of the
SnmpConstants.SNMP_ERROR_MESSAGES
array for a valid error status. "Unknown error: <errorStatusNumber>" is returned for any other value. - Since:
- 1.7
-
setErrorIndex
public void setErrorIndex(int errorIndex) Sets the error index.- Parameters:
errorIndex
- an integer value >= 0 where 1 denotes the first variable binding.
-
getErrorIndex
public int getErrorIndex()Gets the error index.- Returns:
- an integer value >= 0 where 1 denotes the first variable binding.
-
isConfirmedPdu
public boolean isConfirmedPdu()Checks whether this PDU is a confirmed class PDU.- Returns:
- boolean
-
isResponsePdu
public boolean isResponsePdu()Checks whether this PDU is aRESPONSE
or [@link PDU#REPORT}. -
getBERLength
public int getBERLength()Description copied from interface:BERSerializable
Returns the length of thisBERSerializable
object in bytes when encoded according to the Basic Encoding Rules (BER).- Specified by:
getBERLength
in interfaceBERSerializable
- Returns:
- the BER encoded length of this variable.
-
getBERPayloadLength
public int getBERPayloadLength()Description copied from interface:BERSerializable
Returns the length of the payload of thisBERSerializable
object in bytes when encoded according to the Basic Encoding Rules (BER).- Specified by:
getBERPayloadLength
in interfaceBERSerializable
- Returns:
- the BER encoded length of this variable.
-
decodeBER
Description copied from interface:BERSerializable
Decodes aVariable
from anInputStream
.- Specified by:
decodeBER
in interfaceBERSerializable
- Parameters:
inputStream
- anInputStream
containing a BER encoded byte stream.- Throws:
IOException
- if the stream could not be decoded by using BER rules.
-
decodeVariableBindings
public static ArrayList<VariableBinding> decodeVariableBindings(BERInputStream inputStream) throws IOException - Throws:
IOException
-
getBERLength
Computes the length in bytes of the BER encoded variable bindings without including the length of BER sequence length.- Parameters:
variableBindings
- a list of variable bindings.- Returns:
- the length in bytes of the BER encoded VB list.
-
getBERPayloadLengthPDU
protected int getBERPayloadLengthPDU() -
encodeBER
Description copied from interface:BERSerializable
Encodes aVariable
to anOutputStream
.- Specified by:
encodeBER
in interfaceBERSerializable
- Parameters:
outputStream
- anOutputStream
.- Throws:
IOException
- if an error occurs while writing to the stream.
-
encodeVariableBindings
public static void encodeVariableBindings(OutputStream outputStream, List<VariableBinding> variableBindings) throws IOException - Throws:
IOException
-
clear
public void clear()Removes all variable bindings from the PDU and sets the request ID to zero. This can be used to reuse a PDU for another request. -
setType
public void setType(int type) Sets the PDU type.- Parameters:
type
- the type of the PDU (e.g. GETNEXT, SET, etc.)
-
getType
public int getType()Gets the PDU type. The default isGETNEXT
.- Returns:
- the PDU's type.
-
clone
-
getRequestID
Gets the request ID associated with this PDU.- Returns:
- an
Integer32
instance.
-
setRequestID
Sets the request ID for this PDU. When the request ID is not set or set to zero, the message processing model will generate a unique request ID for thePDU
when sent.- Parameters:
requestID
- a unique request ID.
-
getTypeString
Gets a string representation of the supplied PDU type.- Parameters:
type
- a PDU type.- Returns:
- a string representation of
type
, for example "GET".
-
getTypeFromString
Gets the PDU type identifier for a string representation of the type.- Parameters:
type
- the string representation of a PDU type:GET, GETNEXT, GETBULK, SET, INFORM, RESPONSE, REPORT, TRAP, V1TRAP)
.- Returns:
- the corresponding PDU type constant, or
Integer.MIN_VALUE
of the supplied type is unknown.
-
toString
Returns a string representation of the object. -
getMaxRepetitions
public int getMaxRepetitions()Gets the maximum repetitions of repeatable variable bindings in GETBULK requests.- Returns:
- an integer value >= 0.
-
setMaxRepetitions
public void setMaxRepetitions(int maxRepetitions) Sets the maximum repetitions of repeatable variable bindings in GETBULK requests.- Parameters:
maxRepetitions
- an integer value >= 0.
-
getNonRepeaters
public int getNonRepeaters()Gets the number of non repeater variable bindings in a GETBULK PDU.- Returns:
- an integer value >= 0 and <=
size()
-
setNonRepeaters
public void setNonRepeaters(int nonRepeaters) Sets the number of non repeater variable bindings in a GETBULK PDU.- Parameters:
nonRepeaters
- an integer value >= 0 and <=size()
-
toArray
Returns an array with the variable bindings of this PDU.- Returns:
- an array of
VariableBinding
instances of this PDU in the same order as in the PDU.
-
hashCode
public int hashCode() -
equals
-
getAll
Gets all theVariableBinding
s of this PDU.- Returns:
- a possibly empty list of
VariableBinding
instances. - Since:
- 3.5.0
-