Module org.snmp4j

Class OID

All Implemented Interfaces:
Serializable, Cloneable, 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 OID class allows definition and manipulation of object identifiers.

Version:
3.0
Author:
Frank Fock
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
     
    static final int
     

    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 an OID from an array of integer values.
    OID(int[] prefixOID, int suffixID)
    Constructs an OID from 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 an OID from 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 an OID from an array of integer values.
    OID(String oid)
    Constructs an OID from a dotted string.
    OID(OID other)
    Copy constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    final OID
    append(int subID)
    Appends a sub-identifier to this OID.
    final OID
    Appends a dotted String OID to this OID.
    final OID
    append(OID oid)
    Appends an OID to this OID.
    final OID
    appendUnsigned(long subID)
    Appends an unsigned long sub-identifier value to this OID.
    Clones this variable.
    final int
     
    void
    Decodes a Variable from an BERInputStream.
    void
    encodeBER(OutputStream outputStream)
    Encodes a Variable to an OutputStream.
    final boolean
     
    Format the OID as text.
    final void
    fromSubIndex(OID subIndex, boolean impliedLength)
    Sets the value of this Variable from the supplied (sub-)index.
    final int
    get(int index)
    Gets the sub-identifier value at the specified position.
    int
    Returns the length of this Variable in bytes when encoded according to the Basic Encoding Rules (BER).
    getSuffix(OID prefix)
    Get the suffix of this OID that exceeds the given OID.
    final int
    Gets the ASN.1 syntax identifier value of this SNMP variable.
    final long
    getUnsigned(int index)
    Gets the unsigned sub-identifier value at the specified position.
    final int[]
    Gets all sub-identifiers as an int array.
    int
     
    boolean
    Checks whether this OID can be BER encoded.
    final int
    Returns the last sub-identifier as an integer value.
    final long
    Returns the last sub-identifier as an unsigned long value.
    int
    leftMostCompare(int n, OID other)
    Compares the n leftmost sub-identifiers with the given OID in left-to-right direction.
    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 OID
    max(OID a, OID b)
    Returns the greater of the two OID values.
    static OID
    min(OID a, OID b)
    Returns the lesser of the two OID values.
    final OID
    Returns the next following OID with the same or lesser size (length).
    final OID
    Returns the predecessor OID for this OID.
    int
    Removes the last sub-identifier (if available) from this OID and returns it.
    int
    rightMostCompare(int n, OID other)
    Compares the n rightmost sub-identifiers in direction right-to-left with those of the given OID.
    final void
    set(int index, int value)
    Sets the sub-identifier at the specified position.
    final void
    setValue(int[] value)
    Sets the value from an array of integer values.
    void
     
    final int
    Returns the number of sub-identifiers in this OID.
    boolean
    startsWith(OID other)
    Check if the OID starts with the given OID.
    subOID(int beginIndex)
    Returns a new OID that is a sub-sequence of this OID starting at beginIndex until the end of this OID.
    subOID(int beginIndex, int endIndex)
    Returns a new OID that is a sub-sequence of this OID.
    final OID
    Returns the successor OID for this OID.
    byte[]
    Returns the content of the as a byte array.
    Returns the OID as dotted string (e.g., "1.3.6.1.4.1") regardless of what OIDTextFormat instance is set in SNMP4JSettings.
    int
    Returns an integer representation of this variable if such a representation exists.
    int[]
    Returns the value of this object as an int array.
    long
    Returns a long representation of this variable if such a representation exists.
    Return a string representation that can be parsed again to this OID by OID(String).
    final OID
    toSubIndex(boolean impliedLength)
    Converts the value of this Variable to a (sub-)index value.
    long[]
    Return this OID as array of unsigned long values.
    Returns a new copy of this OID with the last sub-indentifier removed.
    void
    trim(int n)
    Remove the n rightmost subidentifiers from this OID.

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

  • Constructor Details

    • OID

      public OID()
      Constructs a zero length OID.
    • OID

      public OID(String oid)
      Constructs an OID from 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 an OID from an array of integer values.
      Parameters:
      rawOID - an array of int values. The array is copied. Later changes to rawOID will therefore not affect the OID's value.
    • OID

      public OID(int[] prefixOID, int[] suffixOID)
      Constructs an OID from 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 of int values. The array is copied. Later changes to prefixOID will therefore not affect the OID's value.
      suffixOID - an array of int values which will be appended to the prefixOID OID. The array is copied. Later changes to suffixOID will therefore not affect the OID's value.
      Since:
      1.8
    • OID

      public OID(int[] prefixOID, int suffixID)
      Constructs an OID from 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 of int values. The array is copied. Later changes to prefixOID will therefore not affect the OID's value.
      suffixID - an int value that will be appended to the prefixOID OID. The array is copied. Later changes to prefixOID will therefore not affect the OID's value.
      Since:
      2.2.6
    • OID

      public OID(int[] rawOID, int offset, int length)
      Constructs an OID from an array of integer values.
      Parameters:
      rawOID - an array of int values. The array is copied. Later changes to rawOID will therefore not affect the OID's value.
      offset - the zero based offset into the rawOID that points to the first sub-identifier of the new OID.
      length - the length of the new OID, where offset + length must be less or equal the length of rawOID. Otherwise an IndexOutOfBoundsException is thrown.
    • OID

      public OID(OID other)
      Copy constructor.
      Parameters:
      other - OID
  • Method Details

    • getSyntax

      public final int getSyntax()
      Description copied from class: AbstractVariable
      Gets the ASN.1 syntax identifier value of this SNMP variable.
      Specified by:
      getSyntax in interface Variable
      Specified by:
      getSyntax in class AbstractVariable
      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:
      hashCode in interface Variable
      Specified by:
      hashCode in class AbstractVariable
    • equals

      public final boolean equals(Object o)
      Specified by:
      equals in interface Variable
      Specified by:
      equals in class AbstractVariable
    • 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:
      compareTo in interface Comparable<Variable>
      Specified by:
      compareTo in interface Variable
      Specified by:
      compareTo in class AbstractVariable
    • toString

      public String toString()
      Return a string representation that can be parsed again to this OID by OID(String).
      Specified by:
      toString in interface Variable
      Specified by:
      toString in class AbstractVariable
      Returns:
      a formatted string representation of this OID (e.g. "ifDescr.1") that can be parsed again as defined by OIDTextFormat.formatForRoundTrip(int[]) in SNMP4JSettings.
    • format

      public String format()
      Format the OID as text. This could return to same result as toString() but also fully converted index-to-text values like snmp4jLogLoggerIndex.org.snmp4j.MessageDispatcherImpl.
      Returns:
      a string representation of this OID as defined by the OIDTextFormat.format(int[]) in SNMP4JSettings.
      Since:
      2.2
    • toDottedString

      public String toDottedString()
      Returns the OID as dotted string (e.g., "1.3.6.1.4.1") regardless of what OIDTextFormat instance is set in SNMP4JSettings.
      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 an OctetString or IpAddress instance.
      Returns:
      the sub-identifies of this OID as a byte array. Each sub-identifier value is masked with 0xFF to form a byte value.
      Since:
      1.2
    • encodeBER

      public void encodeBER(OutputStream outputStream) throws IOException
      Description copied from class: AbstractVariable
      Encodes a Variable to an OutputStream.
      Specified by:
      encodeBER in interface BERSerializable
      Specified by:
      encodeBER in class AbstractVariable
      Parameters:
      outputStream - an OutputStream.
      Throws:
      IOException - if an error occurs while writing to the stream.
    • getBERLength

      public int getBERLength()
      Description copied from class: AbstractVariable
      Returns the length of this Variable in bytes when encoded according to the Basic Encoding Rules (BER).
      Specified by:
      getBERLength in interface BERSerializable
      Specified by:
      getBERLength in class AbstractVariable
      Returns:
      the BER encoded length of this variable.
    • decodeBER

      public void decodeBER(BERInputStream inputStream) throws IOException
      Description copied from class: AbstractVariable
      Decodes a Variable from an BERInputStream.
      Specified by:
      decodeBER in interface BERSerializable
      Specified by:
      decodeBER in class AbstractVariable
      Parameters:
      inputStream - an BERInputStream containing a BER encoded byte stream.
      Throws:
      IOException - if the stream could not be decoded by using BER rules.
    • setValue

      public void setValue(String value)
      Specified by:
      setValue in interface AssignableFromString
    • setValue

      public final void setValue(int[] value)
      Sets the value from an array of integer values.
      Specified by:
      setValue in interface AssignableFromIntArray
      Parameters:
      value - The new value
      Throws:
      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 the OID.
      Returns:
      the sub-indentifier value at index. NOTE: The returned value may be negative if the sub-identifier value is greater than 2^31.
      Throws:
      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 index as 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 the OID.
      value - a 32bit unsigned integer value.
      Throws:
      ArrayIndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()).
    • append

      public final OID append(String oid)
      Appends a dotted String OID to this OID.
      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 an OID to this OID.
      Parameters:
      oid - an OID instance.
      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 this OID can be BER encoded.
      Returns:
      true if 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 this OID.
      Returns:
      an integer value between 0 and 128.
    • leftMostCompare

      public int leftMostCompare(int n, OID other)
      Compares the n leftmost sub-identifiers with the given OID in left-to-right direction.
      Parameters:
      n - the number of sub-identifiers to compare.
      other - an OID to compare with this OID.
      Returns:
      • 0 if the first n sub-identifiers are the same.
      • <0 if the first n sub-identifiers of this OID are lexicographic less than those of the comparand.
      • >0 if the first n sub-identifiers of this OID are lexicographic greater than those of the comparand.
    • rightMostCompare

      public int rightMostCompare(int n, OID other)
      Compares the n rightmost sub-identifiers in direction right-to-left with those of the given OID.
      Parameters:
      n - the number of sub-identifiers to compare.
      other - an OID to compare with this OID.
      Returns:
      • 0 if the first n sub-identifiers are the same.
      • <0 if the first n sub-identifiers of this OID are lexicographic less than those of the comparand.
      • >0 if the first n sub-identifiers of this OID are lexicographic greater than those of the comparand.
    • 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 Object clone()
      Description copied from interface: Variable
      Clones 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:
      clone in interface Variable
      Specified by:
      clone in class AbstractVariable
      Returns:
      a new instance of this Variable with 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 a NoSuchElementException is 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 a NoSuchElementException is 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 this OID and 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. If n is zero or negative then this OID will not be changed. If n is greater than size() 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-1 sub-identifiers where n is 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: AbstractVariable
      Returns an integer representation of this variable if such a representation exists.
      Specified by:
      toInt in interface Variable
      Specified by:
      toInt in class AbstractVariable
      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: AbstractVariable
      Returns a long representation of this variable if such a representation exists.
      Specified by:
      toLong in interface Variable
      Specified by:
      toLong in class AbstractVariable
      Returns:
      a long value.
    • toSubIndex

      public final OID toSubIndex(boolean impliedLength)
      Description copied from class: AbstractVariable
      Converts the value of this Variable to a (sub-)index value.
      Specified by:
      toSubIndex in interface Variable
      Specified by:
      toSubIndex in class AbstractVariable
      Parameters:
      impliedLength - specifies if the sub-index has an implied length. This parameter applies to variable length variables only (e.g. OctetString and OID). 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: AbstractVariable
      Sets the value of this Variable from the supplied (sub-)index.
      Specified by:
      fromSubIndex in interface Variable
      Specified by:
      fromSubIndex in class AbstractVariable
      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. OctetString and OID). 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_LEN sub-identifier OID is returned where each sub-ID for index greater or equal to size() is set to MAX_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:
      a if a >= b, b otherwise.
      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:
      a if a <= b, b otherwise.
      Since:
      1.7
    • toIntArray

      public int[] toIntArray()
      Description copied from interface: AssignableFromIntArray
      Returns the value of this object as an int array.
      Specified by:
      toIntArray in interface AssignableFromIntArray
      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 example getSuffix(OID) on the OID ifDescr.2 with the prefix parameter ifDescr will return the OID 2. Likewise, getSuffix(OID) on the OID 1.3.6.1.4 with prefix parameter 1.3.6 will return 1.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, null is returned. Otherwise, the suffix that extends this OID compared with prefix is 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 at beginIndex until 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