- java.lang.Object
-
- org.snmp4j.smi.AbstractVariable
-
- org.snmp4j.smi.OID
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Cloneable,java.lang.Comparable<Variable>,BERSerializable,AssignableFromIntArray,AssignableFromString,Variable
public class OID extends AbstractVariable implements AssignableFromString, AssignableFromIntArray
The Object Identifier Class.The Object Identifier (OID) class is the encapsulation of an SMI object identifier. The SMI object is a data identifier for a data element found in a Management Information Base (MIB), as defined by a MIB definition. The
OIDclass allows definition and manipulation of object identifiers.- Version:
- 3.0
- Author:
- Frank Fock
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static intMAX_OID_LENstatic intMAX_SUBID_VALUE-
Fields inherited from class org.snmp4j.smi.AbstractVariable
SMISYNTAXES_PROPERTIES
-
-
Constructor Summary
Constructors Constructor Description OID()Constructs a zero length OID.OID(int[] rawOID)Constructs anOIDfrom an array of integer values.OID(int[] prefixOID, int suffixID)Constructs anOIDfrom two arrays of integer values where the first represents the OID prefix (i.e., the object class ID) and the second one represents the OID suffix (i.e., the instance identifier).OID(int[] prefixOID, int[] suffixOID)Constructs anOIDfrom two arrays of integer values where the first represents the OID prefix (i.e., the object class ID) and the second one represents the OID suffix (i.e., the instance identifier).OID(int[] rawOID, int offset, int length)Constructs anOIDfrom an array of integer values.OID(java.lang.String oid)Constructs anOIDfrom a dotted string.OID(OID other)Copy constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description OIDappend(int subID)Appends a sub-identifier to this OID.OIDappend(java.lang.String oid)Appends a dotted String OID to thisOID.OIDappend(OID oid)Appends anOIDto this OID.OIDappendUnsigned(long subID)Appends an unsigned long sub-identifier value to this OID.java.lang.Objectclone()Clones this variable.intcompareTo(Variable o)voiddecodeBER(BERInputStream inputStream)Decodes aVariablefrom anBERInputStream.voidencodeBER(java.io.OutputStream outputStream)Encodes aVariableto anOutputStream.booleanequals(java.lang.Object o)java.lang.Stringformat()Format the OID as text.voidfromSubIndex(OID subIndex, boolean impliedLength)Sets the value of thisVariablefrom the supplied (sub-)index.intget(int index)Gets the sub-identifier value at the specified position.intgetBERLength()Returns the length of thisVariablein bytes when encoded according to the Basic Encoding Rules (BER).OIDgetSuffix(OID prefix)Get the suffix of this OID that exceeds the given OID.intgetSyntax()Gets the ASN.1 syntax identifier value of this SNMP variable.longgetUnsigned(int index)Gets the unsigned sub-identifier value at the specified position.int[]getValue()Gets all sub-identifiers as an int array.inthashCode()booleanisValid()Checks whether thisOIDcan be BER encoded.intlast()Returns the last sub-identifier as an integer value.longlastUnsigned()Returns the last sub-identifier as an unsigned long value.intleftMostCompare(int n, OID other)Compares the n leftmost sub-identifiers with the givenOIDin left-to-right direction.OIDmask(OctetString mask)Returns a copy of this OID where sub-identifiers have been set to zero for all n-th sub-identifier where the n-th bit of mask is zero.static OIDmax(OID a, OID b)Returns the greater of the two OID values.static OIDmin(OID a, OID b)Returns the lesser of the two OID values.OIDnextPeer()Returns the next following OID with the same or lesser size (length).OIDpredecessor()Returns the predecessor OID for this OID.intremoveLast()Removes the last sub-identifier (if available) from thisOIDand returns it.intrightMostCompare(int n, OID other)Compares the n rightmost sub-identifiers in direction right-to-left with those of the givenOID.voidset(int index, int value)Sets the sub-identifier at the specified position.voidsetValue(int[] value)Sets the value from an array of integer values.voidsetValue(java.lang.String value)intsize()Returns the number of sub-identifiers in thisOID.booleanstartsWith(OID other)Check if the OID starts with the given OID.OIDsubOID(int beginIndex)Returns a new OID that is a sub-sequence of this OID starting atbeginIndexuntil the end of this OID.OIDsubOID(int beginIndex, int endIndex)Returns a new OID that is a sub-sequence of this OID.OIDsuccessor()Returns the successor OID for this OID.byte[]toByteArray()Returns the content of the as a byte array.java.lang.StringtoDottedString()Returns the OID as dotted string (e.g., "1.3.6.1.4.1") regardless of whatOIDTextFormatinstance is set inSNMP4JSettings.inttoInt()Returns an integer representation of this variable if such a representation exists.int[]toIntArray()Returns the value of this object as an int array.longtoLong()Returns a long representation of this variable if such a representation exists.java.lang.StringtoString()Return a string representation that can be parsed again to thisOIDbyOID(String).OIDtoSubIndex(boolean impliedLength)Converts the value of thisVariableto a (sub-)index value.long[]toUnsignedLongArray()Return this OID as array of unsigned long values.OIDtrim()Returns a new copy of this OID with the last sub-indentifier removed.voidtrim(int n)Remove the n rightmost subidentifiers from this OID.-
Methods inherited from class org.snmp4j.smi.AbstractVariable
createFromBER, createFromSyntax, equal, getBERPayloadLength, getSyntaxFromString, getSyntaxString, getSyntaxString, isDynamic, isException
-
-
-
-
Field Detail
-
MAX_OID_LEN
public static final int MAX_OID_LEN
- See Also:
- Constant Field Values
-
MAX_SUBID_VALUE
public static final int MAX_SUBID_VALUE
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
OID
public OID()
Constructs a zero length OID.
-
OID
public OID(java.lang.String oid)
Constructs anOIDfrom a dotted string. The string can contain embedded strings enclosed by a single quote (') that are converted to the corresponding OIO value. For example the following OID pairs are equal:OID a = new OID("1.3.6.2.1.5.'hallo'.1"); OID b = new OID("1.3.6.2.1.5.104.97.108.108.111.1"); assertEquals(a, b); a = new OID("1.3.6.2.1.5.'hal.lo'.1"); b = new OID("1.3.6.2.1.5.104.97.108.46.108.111.1"); assertEquals(a, b); a = new OID("1.3.6.2.1.5.'hal.'.'''.'lo'.1"); b = new OID("1.3.6.2.1.5.104.97.108.46.39.108.111.1");- Parameters:
oid- a dotted OID String, for example "1.3.6.1.2.2.1.0"
-
OID
public OID(int[] rawOID)
Constructs anOIDfrom an array of integer values.- Parameters:
rawOID- an array ofintvalues. The array is copied. Later changes torawOIDwill therefore not affect the OID's value.
-
OID
public OID(int[] prefixOID, int[] suffixOID)Constructs anOIDfrom two arrays of integer values where the first represents the OID prefix (i.e., the object class ID) and the second one represents the OID suffix (i.e., the instance identifier).- Parameters:
prefixOID- an array ofintvalues. The array is copied. Later changes toprefixOIDwill therefore not affect the OID's value.suffixOID- an array ofintvalues which will be appended to theprefixOIDOID. The array is copied. Later changes tosuffixOIDwill therefore not affect the OID's value.- Since:
- 1.8
-
OID
public OID(int[] prefixOID, int suffixID)Constructs anOIDfrom two arrays of integer values where the first represents the OID prefix (i.e., the object class ID) and the second one represents the OID suffix (i.e., the instance identifier).- Parameters:
prefixOID- an array ofintvalues. The array is copied. Later changes toprefixOIDwill therefore not affect the OID's value.suffixID- anintvalue that will be appended to theprefixOIDOID. The array is copied. Later changes toprefixOIDwill therefore not affect the OID's value.- Since:
- 2.2.6
-
OID
public OID(int[] rawOID, int offset, int length)Constructs anOIDfrom an array of integer values.- Parameters:
rawOID- an array ofintvalues. The array is copied. Later changes torawOIDwill therefore not affect the OID's value.offset- the zero based offset into therawOIDthat points to the first sub-identifier of the new OID.length- the length of the new OID, whereoffset + lengthmust be less or equal the length ofrawOID. Otherwise anIndexOutOfBoundsExceptionis thrown.
-
OID
public OID(OID other)
Copy constructor.- Parameters:
other- OID
-
-
Method Detail
-
getSyntax
public final int getSyntax()
Description copied from class:AbstractVariableGets the ASN.1 syntax identifier value of this SNMP variable.- Specified by:
getSyntaxin interfaceVariable- Specified by:
getSyntaxin classAbstractVariable- Returns:
- an integer value less than 128 for regular SMI objects and a value greater or equal than 128 for exception values like noSuchObject, noSuchInstance, and endOfMibView.
-
hashCode
public int hashCode()
- Specified by:
hashCodein interfaceVariable- Specified by:
hashCodein classAbstractVariable
-
equals
public final boolean equals(java.lang.Object o)
- Specified by:
equalsin interfaceVariable- Specified by:
equalsin classAbstractVariable
-
mask
public OID mask(OctetString mask)
Returns a copy of this OID where sub-identifiers have been set to zero for all n-th sub-identifier where the n-th bit of mask is zero.- Parameters:
mask- a mask where the n-th bit corresponds to the n-th sub-identifier.- Returns:
- the masked OID.
- Since:
- 1.5
-
compareTo
public final int compareTo(Variable o)
- Specified by:
compareToin interfacejava.lang.Comparable<Variable>- Specified by:
compareToin interfaceVariable- Specified by:
compareToin classAbstractVariable
-
toString
public java.lang.String toString()
Return a string representation that can be parsed again to thisOIDbyOID(String).- Specified by:
toStringin interfaceVariable- Specified by:
toStringin classAbstractVariable- Returns:
- a formatted string representation of this OID (e.g. "ifDescr.1") that
can be parsed again as defined by
OIDTextFormat.formatForRoundTrip(int[])inSNMP4JSettings.
-
format
public java.lang.String format()
Format the OID as text. This could return to same result astoString()but also fully converted index-to-text values likesnmp4jLogLoggerIndex.org.snmp4j.MessageDispatcherImpl.- Returns:
- a string representation of this OID as defined by the
OIDTextFormat.format(int[])inSNMP4JSettings. - Since:
- 2.2
-
toDottedString
public java.lang.String toDottedString()
Returns the OID as dotted string (e.g., "1.3.6.1.4.1") regardless of whatOIDTextFormatinstance is set inSNMP4JSettings.- Returns:
- a dotted string representation of this OID value.
- Since:
- 2.2
-
toByteArray
public byte[] toByteArray()
Returns the content of the as a byte array. This method can be used to convert an index value to anOctetStringorIpAddressinstance.- Returns:
- the sub-identifies of this
OIDas a byte array. Each sub-identifier value is masked with 0xFF to form a byte value. - Since:
- 1.2
-
encodeBER
public void encodeBER(java.io.OutputStream outputStream) throws java.io.IOExceptionDescription copied from class:AbstractVariableEncodes aVariableto anOutputStream.- Specified by:
encodeBERin interfaceBERSerializable- Specified by:
encodeBERin classAbstractVariable- Parameters:
outputStream- anOutputStream.- Throws:
java.io.IOException- if an error occurs while writing to the stream.
-
getBERLength
public int getBERLength()
Description copied from class:AbstractVariableReturns the length of thisVariablein bytes when encoded according to the Basic Encoding Rules (BER).- Specified by:
getBERLengthin interfaceBERSerializable- Specified by:
getBERLengthin classAbstractVariable- Returns:
- the BER encoded length of this variable.
-
decodeBER
public void decodeBER(BERInputStream inputStream) throws java.io.IOException
Description copied from class:AbstractVariableDecodes aVariablefrom anBERInputStream.- Specified by:
decodeBERin interfaceBERSerializable- Specified by:
decodeBERin classAbstractVariable- Parameters:
inputStream- anBERInputStreamcontaining a BER encoded byte stream.- Throws:
java.io.IOException- if the stream could not be decoded by using BER rules.
-
setValue
public void setValue(java.lang.String value)
- Specified by:
setValuein interfaceAssignableFromString
-
setValue
public final void setValue(int[] value)
Sets the value from an array of integer values.- Specified by:
setValuein interfaceAssignableFromIntArray- Parameters:
value- The new value- Throws:
java.lang.IllegalArgumentException- if value == null.
-
getValue
public final int[] getValue()
Gets all sub-identifiers as an int array.- Returns:
- int arry of all sub-identifiers
-
get
public final int get(int index)
Gets the sub-identifier value at the specified position.- Parameters:
index- a zero-based index into theOID.- Returns:
- the sub-indentifier value at
index. NOTE: The returned value may be negative if the sub-identifier value is greater than2^31. - Throws:
java.lang.ArrayIndexOutOfBoundsException- if the index is out of range (index < 0 || index >= size()).
-
getUnsigned
public final long getUnsigned(int index)
Gets the unsigned sub-identifier value at the specified position.- Parameters:
index- int- Returns:
- the sub-identifier value at
indexas an unsigned long value.
-
set
public final void set(int index, int value)Sets the sub-identifier at the specified position.- Parameters:
index- a zero-based index into theOID.value- a 32bit unsigned integer value.- Throws:
java.lang.ArrayIndexOutOfBoundsException- if the index is out of range (index < 0 || index >= size()).
-
append
public final OID append(java.lang.String oid)
Appends a dotted String OID to thisOID.- Parameters:
oid- a dotted String with numerical sub-identifiers.- Returns:
- a pointer to this OID instance (useful for chaining).
-
append
public final OID append(OID oid)
Appends anOIDto this OID.- Parameters:
oid- anOIDinstance.- Returns:
- a pointer to this OID instance (useful for chaining).
-
append
public final OID append(int subID)
Appends a sub-identifier to this OID.- Parameters:
subID- an integer value.- Returns:
- a pointer to this OID instance (useful for chaining).
-
appendUnsigned
public final OID appendUnsigned(long subID)
Appends an unsigned long sub-identifier value to this OID.- Parameters:
subID- an unsigned long value less or equal to 2^32-1.- Returns:
- a pointer to this OID instance (useful for chaining).
- Since:
- 1.2
-
isValid
public boolean isValid()
Checks whether thisOIDcan be BER encoded.- Returns:
trueif size() >= 2 and size() <= 128 and if the first two sub-identifiers are less than 3 and 40 respectively.
-
size
public final int size()
Returns the number of sub-identifiers in thisOID.- Returns:
- an integer value between 0 and 128.
-
leftMostCompare
public int leftMostCompare(int n, OID other)Compares the n leftmost sub-identifiers with the givenOIDin left-to-right direction.- Parameters:
n- the number of sub-identifiers to compare.other- anOIDto compare with thisOID.- Returns:
- 0 if the first
nsub-identifiers are the same. - <0 if the first
nsub-identifiers of thisOIDare lexicographic less than those of the comparand. - >0 if the first
nsub-identifiers of thisOIDare lexicographic greater than those of the comparand.
- 0 if the first
-
rightMostCompare
public int rightMostCompare(int n, OID other)Compares the n rightmost sub-identifiers in direction right-to-left with those of the givenOID.- Parameters:
n- the number of sub-identifiers to compare.other- anOIDto compare with thisOID.- Returns:
- 0 if the first
nsub-identifiers are the same. - <0 if the first
nsub-identifiers of thisOIDare lexicographic less than those of the comparand. - >0 if the first
nsub-identifiers of thisOIDare lexicographic greater than those of the comparand.
- 0 if the first
-
startsWith
public boolean startsWith(OID other)
Check if the OID starts with the given OID.- Parameters:
other- the OID to compare to- Returns:
- false if the sub-identifiers do not match.
-
clone
public java.lang.Object clone()
Description copied from interface:VariableClones this variable. Cloning can be used by the SNMP4J API to better support concurrency by creating a clone for internal processing. The content of this object is independent to the content of the clone. Thus, changes to the clone will have no effect to this object.- Specified by:
clonein interfaceVariable- Specified by:
clonein classAbstractVariable- Returns:
- a new instance of this
Variablewith the same value.
-
last
public final int last()
Returns the last sub-identifier as an integer value. If this OID is empty (i.e. has no sub-identifiers) then aNoSuchElementExceptionis thrown- Returns:
- the value of the last sub-identifier of this OID as an integer value. Sub-identifier values greater than 2^31-1 will be returned as negative values!
- Since:
- 1.2
-
lastUnsigned
public final long lastUnsigned()
Returns the last sub-identifier as an unsigned long value. If this OID is empty (i.e. has no sub-identifiers) then aNoSuchElementExceptionis thrown- Returns:
- the value of the last sub-identifier of this OID as an unsigned long.
- Since:
- 1.2
-
removeLast
public int removeLast()
Removes the last sub-identifier (if available) from thisOIDand returns it.- Returns:
- the last sub-identifier or -1 if there is no sub-identifier left in
this
OID.
-
trim
public void trim(int n)
Remove the n rightmost subidentifiers from this OID.- Parameters:
n- the number of subidentifiers to remove. Ifnis zero or negative then this OID will not be changed. Ifnis greater thansize()all subidentifiers will be removed from this OID.
-
trim
public OID trim()
Returns a new copy of this OID with the last sub-indentifier removed.- Returns:
- a copy of this OID with
n-1sub-identifiers wherenis the size of this OID and greater than zero, otherwise a zero length OID is returned. - Since:
- 1.11
-
toInt
public int toInt()
Description copied from class:AbstractVariableReturns an integer representation of this variable if such a representation exists.- Specified by:
toIntin interfaceVariable- Specified by:
toIntin classAbstractVariable- Returns:
- an integer value (if the native representation of this variable would be a long, then the long value will be casted to int).
-
toLong
public long toLong()
Description copied from class:AbstractVariableReturns a long representation of this variable if such a representation exists.- Specified by:
toLongin interfaceVariable- Specified by:
toLongin classAbstractVariable- Returns:
- a long value.
-
toSubIndex
public final OID toSubIndex(boolean impliedLength)
Description copied from class:AbstractVariableConverts the value of thisVariableto a (sub-)index value.- Specified by:
toSubIndexin interfaceVariable- Specified by:
toSubIndexin classAbstractVariable- Parameters:
impliedLength- specifies if the sub-index has an implied length. This parameter applies to variable length variables only (e.g.OctetStringandOID). For other variables it has no effect.- Returns:
- an OID that represents this value as an (sub-)index.
-
fromSubIndex
public final void fromSubIndex(OID subIndex, boolean impliedLength)
Description copied from class:AbstractVariableSets the value of thisVariablefrom the supplied (sub-)index.- Specified by:
fromSubIndexin interfaceVariable- Specified by:
fromSubIndexin classAbstractVariable- Parameters:
subIndex- the sub-index OID.impliedLength- specifies if the sub-index has an implied length. This parameter applies to variable length variables only (e.g.OctetStringandOID). For other variables it has no effect.
-
successor
public final OID successor()
Returns the successor OID for this OID.- Returns:
- an OID clone of this OID with a zero sub-identifier appended.
- Since:
- 1.7
-
predecessor
public final OID predecessor()
Returns the predecessor OID for this OID.- Returns:
- if this OID ends on 0, then a
MAX_OID_LENsub-identifier OID is returned where each sub-ID for index greater or equal tosize()is set toMAX_SUBID_VALUE. - Since:
- 1.7
-
nextPeer
public final OID nextPeer()
Returns the next following OID with the same or lesser size (length).- Returns:
- OID the next OID on the same or upper level or a clone of this OID, if it has a zero length or is 2^32-1.
- Since:
- 1.7
-
max
public static OID max(OID a, OID b)
Returns the greater of the two OID values.- Parameters:
a- an OID.b- an OID.- Returns:
aif a >= b,botherwise.- Since:
- 1.7
-
min
public static OID min(OID a, OID b)
Returns the lesser of the two OID values.- Parameters:
a- an OID.b- an OID.- Returns:
aif a <= b,botherwise.- Since:
- 1.7
-
toIntArray
public int[] toIntArray()
Description copied from interface:AssignableFromIntArrayReturns the value of this object as an int array.- Specified by:
toIntArrayin interfaceAssignableFromIntArray- Returns:
- an int array.
-
toUnsignedLongArray
public long[] toUnsignedLongArray()
Return this OID as array of unsigned long values.- Returns:
- an array of unsigned long values.
- Since:
- 3.0
-
getSuffix
public OID getSuffix(OID prefix)
Get the suffix of this OID that exceeds the given OID. This operation can be used to determine the row index of a table cell. For examplegetSuffix(OID)on the OIDifDescr.2with the prefix parameterifDescrwill return the OID 2. Likewise,getSuffix(OID)on the OID1.3.6.1.4with prefix parameter1.3.6will return1.4.- Parameters:
prefix- the prefix to left-most trim from this OID to form the result. This OID will not be modified though.- Returns:
- If this OID does not start with prefix,
nullis returned. Otherwise, the suffix that extends this OID compared withprefixis returned. If both OIDs equal, a zero length OID is returned. - Since:
- 3.0
-
subOID
public OID subOID(int beginIndex)
Returns a new OID that is a sub-sequence of this OID starting atbeginIndexuntil the end of this OID.- Parameters:
beginIndex- the begin index, inclusive.- Returns:
- the specified sub OID.
- Since:
- 3.4.1
-
subOID
public OID subOID(int beginIndex, int endIndex)
Returns a new OID that is a sub-sequence of this OID.- Parameters:
beginIndex- the begin index, inclusive.endIndex- the end index, exclusive.- Returns:
- the specified sub OID.
- Since:
- 3.4.1
-
-