/*
*
* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
// Copyright (c) 1995-96 by Cisco Systems, Inc.
package com.sun.jmx.snmp;
// java imports
//
import java.io.Serializable;
/**
* This class holds information for a MIB variable contained in an {@link com.sun.jmx.snmp.SnmpVarBindList}.
* An SnmpVarBind
consists of three parts:
*
SnmpVarBind
which specifies whether the agent responded with an
* exception condition for this variable such as noSuchInstance
, endOfMibView
,
* or noSuchObject
.
* This API is a Sun Microsystems internal API and is subject * to change without notice.
*/ public class SnmpVarBind implements SnmpDataTypeEnums, Cloneable, Serializable { private static final long serialVersionUID = 491778383240759376L; // PUBLIC VARIABLES //----------------- /** * Keeps the legend for the value part of theSnmpVarBind
.
*/
static final private String statusLegend[] = { "Status Mapper", "Value not initialized",
"Valid Value", "No such object",
"No such Instance", "End of Mib View" } ;
/**
* Useful constant indicating that the status of the SnmpVarBind
object is not initialized.
*/
static final public int stValueUnspecified = 1 ;
/**
* Useful constant indicating that the status of the SnmpVarBind
object is valid.
*/
static final public int stValueOk = 2 ;
/**
* Useful constant indicating that the status of the SnmpVarBind
object is noSuchObject
.
* Status of SnmpVarBind
as returned by the SNMPv2 agent.
*/
static final public int stValueNoSuchObject = 3 ;
/**
* Useful constant indicating that the status of the SnmpVarBind
object is
* noSuchInstance
.
* Status of SnmpVarBind
as returned by the SNMPv2 agent.
* In the SNMPv1 context, this is appropriate when noSuchName
is returned in response to the
* SnmpGet
request.
*/
static final public int stValueNoSuchInstance = 4 ;
/**
* Useful constant indicating that the status of the SnmpVarBind
object is endOfMibView
.
* Status of SnmpVarBind
as returned by the SNMPv2 agent.
* In the SNMPv1 context, this is appropriate when noSuchName
is returned in response to the
* SnmpGetNext
request.
*/
static final public int stValueEndOfMibView = 5 ;
//
// These are predefined values for SNMP V2 variables
//
/**
* Error code value as defined in RFC 1448 for: noSuchObject
.
*/
public final static SnmpNull noSuchObject = new SnmpNull(errNoSuchObjectTag) ;
/**
* Error code value as defined in RFC 1448 for: noSuchInstance
.
*/
public final static SnmpNull noSuchInstance = new SnmpNull(errNoSuchInstanceTag) ;
/**
* Error code value as defined in RFC 1448 for: endOfMibView
.
*/
public final static SnmpNull endOfMibView = new SnmpNull(errEndOfMibViewTag) ;
/**
* The OID of the SnmpVarBind
.
* The default value is null.
* Reserved for internal use:
* As of Java Dynamic Management Kit 5.0, use instead getOid
and setOid
SnmpVarBind
.
* The default value is null.
* Reserved for internal use:
* As of Java Dynamic Management Kit 5.0, use instead getSnmpValue
and setSnmpValue
SnmpVarBind
.
* The default value is stValueUnspecified
.
* This attribute is updated internally and should not be changed otherwise.
*/
public int status = stValueUnspecified ;
// CONSTRUCTORS
//-------------
/**
* Default constructor.
*/
public SnmpVarBind() {
}
/**
* Constructs a new SnmpVarBind
object from the specified SnmpOid
value.
* @param oid The OID part of the SnmpVarBind
.
*/
public SnmpVarBind(SnmpOid oid) {
this.oid = oid ;
}
/**
* Constructs a new SnmpVarBind
object from the specified SnmpOid
and
* SnmpValue
.
* @param oid The OID part of the SnmpVarBind
.
* @param val The value part of the SnmpVarBind
.
*/
public SnmpVarBind(SnmpOid oid, SnmpValue val) {
this.oid = oid ;
this.setSnmpValue(val) ;
}
/**
* Constructs a new SnmpVarBind
object from the specified String
value.
* If the name is a MIB variable, it resolves the name with the MIB database.
* @param name The MIB variable name or a dot-formatted OID String
.
* @exception SnmpStatusException An error occurred while resolving the MIB variable name.
*/
public SnmpVarBind(String name) throws SnmpStatusException {
if (name.startsWith(".")) {
this.oid = new SnmpOid(name) ;
} else {
SnmpOidRecord record= null;
try {
int index = name.indexOf('.') ;
handleLong(name, index);
this.oid = new SnmpOid(name);
}
catch(NumberFormatException e) {
int index = name.indexOf('.') ;
if (index <= 0) {
record = resolveVarName(name) ;
this.oid = new SnmpOid(record.getName()) ;
} else {
record = resolveVarName(name.substring(0, index)) ;
this.oid = new SnmpOid(record.getName() + name.substring(index)) ;
}
}
}
}
// GETTER/SETTER
//--------------
/**
* Returns the complete OID part associated with this SnmpVarBind
.
* @return The SnmpOid
for this variable.
*/
final public SnmpOid getOid() {
return this.oid ;
}
/**
* Sets the SnmpOid
part associated with this SnmpVarBind
with the specified OID.
* The value part of this SnmpVarBind
will automatically be nulled.
* @param oid The new OID.
*/
final public void setOid(SnmpOid oid) {
this.oid = oid ;
clearValue() ;
}
/**
* Returns the value part associated with this SnmpVarBind
.
* @return The SnmpValue
for this variable.
*/
final synchronized public SnmpValue getSnmpValue() {
return this.value ;
}
/**
* Sets the SnmpValue
part associated with this SnmpVarBind
with the specified value.
* The status is updated to indicate that the value is valid.
* @param val The new value.
*/
final public void setSnmpValue(SnmpValue val) {
this.value= val ;
setValueValid();
}
/**
* Returns the value part associated with this SnmpVarBind
.
* @return The SnmpCounter64
value for this variable.
* @exception ClassCastException An attempt has been made to cast an object to a subclass of which
* it is not an instance.
*/
final public SnmpCounter64 getSnmpCounter64Value() throws ClassCastException {
return (SnmpCounter64)this.value ;
}
/**
* Sets the SnmpCounter64
value part associated with this SnmpVarBind
* with the specified counter 64 value.
* The status is updated to indicate that the value is valid.
* @param val The new counter 64 value.
* @exception IllegalArgumentException The specified value is negative or larger than Long.MAX_VALUE
.
* @see SnmpCounter64
*/
final public void setSnmpCounter64Value(long val) throws IllegalArgumentException {
clearValue() ;
this.value = new SnmpCounter64(val) ;
setValueValid() ;
}
/**
* Returns the value part associated with this SnmpVarBind
.
* @return The SnmpInt
value for this variable.
* @exception ClassCastException An attempt has been made to cast an object to a subclass of which
* it is not an instance.
*/
final public SnmpInt getSnmpIntValue() throws ClassCastException {
return (SnmpInt)this.value ;
}
/**
* Sets the SnmpInt
value part associated with this SnmpVarBind
* with the specified integer value.
* The status is updated to indicate that the value is valid.
* @param val The new integer value.
* @exception IllegalArgumentException The specified value is smaller than Integer.MIN_VALUE
* or larger than Integer.MAX_VALUE
.
* @see SnmpInt
*/
final public void setSnmpIntValue(long val) throws IllegalArgumentException {
clearValue() ;
this.value = new SnmpInt(val) ;
setValueValid() ;
}
/**
* Returns the value part associated with this SnmpVarBind
.
* @return The SnmpCounter
value for this variable.
* @exception ClassCastException An attempt has been made to cast an object to a subclass of which
* it is not an instance.
*/
final public SnmpCounter getSnmpCounterValue() throws ClassCastException {
return (SnmpCounter)this.value ;
}
/**
* Sets the SnmpCounter
value part associated with this SnmpVarBind
* with the specified counter value.
* The status is updated to indicate that the value is valid.
* @param val The new counter value.
* @exception IllegalArgumentException The specified value is negative or larger than
* SnmpUnsignedInt.MAX_VALUE
.
* @see SnmpCounter
*/
final public void setSnmpCounterValue(long val) throws IllegalArgumentException {
clearValue() ;
this.value = new SnmpCounter(val) ;
setValueValid() ;
}
/**
* Returns the value part associated with this SnmpVarBind
.
* @return The SnmpGauge
value for this variable.
* @exception ClassCastException An attempt has been made to cast an object to a subclass of which
* it is not an instance.
*/
final public SnmpGauge getSnmpGaugeValue() throws ClassCastException {
return (SnmpGauge)this.value ;
}
/**
* Sets the SnmpGauge
value part associated with this SnmpVarBind
* with the specified gauge value.
* The status is updated to indicate that the value is valid.
* @param val The new gauge value.
* @exception IllegalArgumentException The specified value is negative or larger than
* SnmpUnsignedInt.MAX_VALUE
.
* @see SnmpGauge
*/
final public void setSnmpGaugeValue(long val) throws IllegalArgumentException {
clearValue() ;
this.value = new SnmpGauge(val) ;
setValueValid() ;
}
/**
* Returns the value part associated with this SnmpVarBind
.
* @return The SnmpTimeticks
value for this variable.
* @exception ClassCastException An attempt has been made to cast an object to a subclass of which
* it is not an instance.
*/
final public SnmpTimeticks getSnmpTimeticksValue() throws ClassCastException {
return (SnmpTimeticks)this.value ;
}
/**
* Sets the SnmpTimeticks
value part associated with this SnmpVarBind
* with the specified timeticks value.
* The status is updated to indicate that the value is valid.
* @param val The new timeticks value.
* @exception IllegalArgumentException The specified value is negative or larger than
* SnmpUnsignedInt.MAX_VALUE
.
* @see SnmpTimeticks
*/
final public void setSnmpTimeticksValue(long val) throws IllegalArgumentException {
clearValue() ;
this.value = new SnmpTimeticks(val) ;
setValueValid() ;
}
/**
* Returns the value part associated with this SnmpVarBind
.
* @return The SnmpOid
value for this variable.
* @exception ClassCastException An attempt has been made to cast an object to a subclass of which
* it is not an instance.
*/
final public SnmpOid getSnmpOidValue() throws ClassCastException {
return (SnmpOid)this.value ;
}
/**
* Sets the SnmpOid
value part associated with this SnmpVarBind
* with the specified OID value.
* The status is updated to indicate that the value is valid.
* @param val The new OID value.
* @exception IllegalArgumentException The specified value is neither a numeric String
* nor a String
of the MIB database.
* @see SnmpOid
*/
final public void setSnmpOidValue(String val) throws IllegalArgumentException {
clearValue() ;
this.value = new SnmpOid(val) ;
setValueValid() ;
}
/**
* Returns the value part associated with this SnmpVarBind
.
* @return The SnmpIpAddress
value for this variable.
* @exception ClassCastException An attempt has been made to cast an object to a subclass of which
* it is not an instance.
*/
final public SnmpIpAddress getSnmpIpAddressValue() throws ClassCastException {
return (SnmpIpAddress)this.value ;
}
/**
* Sets the SnmpIpAddress
value part associated with this SnmpVarBind
* with the specified ipAddress value.
* The status is updated to indicate that the value is valid.
* @param val The new IP address value.
* @exception IllegalArgumentException The specified value does not correspond to an IP address.
* @see SnmpIpAddress
*/
final public void setSnmpIpAddressValue(String val) throws IllegalArgumentException {
clearValue() ;
this.value = new SnmpIpAddress(val) ;
setValueValid() ;
}
/**
* Returns the value part associated with this SnmpVarBind
.
* @return The SnmpString
value for this variable.
* @exception ClassCastException An attempt has been made to cast an object to a subclass of which
* it is not an instance.
*/
final public SnmpString getSnmpStringValue() throws ClassCastException {
return (SnmpString)this.value ;
}
/**
* Sets the SnmpString
value part associated with this SnmpVarBind
* with the specified string value.
* The status is updated to indicate that the value is valid.
* @param val The new string value.
* @see SnmpString
*/
final public void setSnmpStringValue(String val) {
clearValue() ;
this.value = new SnmpString(val) ;
setValueValid() ;
}
/**
* Returns the value part associated with this SnmpVarBind
.
* @return The SnmpOpaque
value for this variable.
* @exception ClassCastException An attempt has been made to cast an object to a subclass of which
* it is not an instance.
*/
final public SnmpOpaque getSnmpOpaqueValue() throws ClassCastException {
return (SnmpOpaque)this.value ;
}
/**
* Sets the SnmpOpaque
value part associated with this SnmpVarBind
* with the specified bytes array values.
* The status is updated to indicate that the value is valid.
* @param val The new bytes array value.
* @see SnmpOpaque
*/
final public void setSnmpOpaqueValue(byte[] val) {
clearValue() ;
this.value = new SnmpOpaque(val) ;
setValueValid() ;
}
/**
* Returns the value part associated with this SnmpVarBind
.
* @return The SnmpStringFixed
value for this variable.
* @exception ClassCastException An attempt has been made to cast an object to a subclass of which
* it is not an instance.
*/
final public SnmpStringFixed getSnmpStringFixedValue() throws ClassCastException {
return (SnmpStringFixed)this.value ;
}
/**
* Sets the SnmpStringFixed
value part associated with this SnmpVarBind
* with the specified string value.
* The status is updated to indicate that the value is valid.
* @param val The new string value.
* @see SnmpStringFixed
*/
final public void setSnmpStringFixedValue(String val) {
clearValue() ;
this.value = new SnmpStringFixed(val) ;
setValueValid() ;
}
// PUBLIC METHODS
//---------------
/**
* Consults the MIB table storage to resolve the name to its OID type structure.
* @param name The MIB variable name or a dot-formatted OID String
.
* @return The SnmpOidRecord
object containing information on the MIB variable.
* @exception SnmpStatusException An error occurred while resolving the MIB variable name.
*/
public SnmpOidRecord resolveVarName(String name) throws SnmpStatusException {
SnmpOidTable mibTable = SnmpOid.getSnmpOidTable();
if (mibTable == null)
throw new SnmpStatusException(SnmpStatusException.noSuchName);
int index = name.indexOf('.');
if (index < 0) {
return mibTable.resolveVarName(name);
} else {
return mibTable.resolveVarOid(name);
}
}
/**
* Returns the status of the value associated with this SnmpVarBind
as an integer.
* This value is one of {@link #stValueUnspecified}, {@link #stValueOk}, {@link #stValueNoSuchObject},
* {@link #stValueNoSuchInstance}, {@link #stValueEndOfMibView}.
* @return The status of the associated value.
*/
final public int getValueStatus() {
return status ;
}
/**
* Returns the status of the value associated with this SnmpVarBind
as a String
.
* This value is a displayable representation of the status integer value.
* It is one of Value not initialized
, Valid Value
, No such object
,
* No such Instance
, End of Mib View
.
* @return The status of the associated value.
*/
final public String getValueStatusLegend() {
return statusLegend[status] ;
}
/**
* Checks whether the object contains a valid accessible value.
* @return true
if the associated value is valid, false
otherwise.
*/
final public boolean isValidValue() {
return (status == stValueOk) ;
}
/**
* Checks whether the value associated with this SnmpVarBind
is unspecified.
* @return true
if the status is unspecified, false
otherwise.
*/
final public boolean isUnspecifiedValue() {
return (status == stValueUnspecified) ;
}
/**
* Clears the value associated with this SnmpVarBind
and sets the status to
* stValueUnspecified
.
*/
final public void clearValue() {
this.value = null ;
status = stValueUnspecified ;
}
/**
* Checks whether the OID for this variable completely matches the OID part of the specified
* SnmpVarBind
object.
* @param var The object whose OID part is to be matched.
* @return true
if the OID part matches exactly, false
otherwise.
*/
final public boolean isOidEqual(SnmpVarBind var) {
return this.oid.equals(var.oid) ;
}
/**
* Adds an instance part to the OID in the SnmpOid
object.
* Note that there is no getInstance
method.
* This method will directly add the instance to the SnmpOid
object.
* @param inst The sub-identifier to be appended to the OID.
*/
final public void addInstance(long inst) {
oid.append(inst) ;
return ;
}
/**
* Adds an instance part to the OID in the SnmpOid
object.
* Note that there is no getInstance
method.
* This method will directly add the instance to the SnmpOid
object.
* @param inst The sub-identifier array to be appended to the OID.
* @exception SnmpStatusException An error occurred while accessing a MIB node.
*/
final public void addInstance(long[] inst) throws SnmpStatusException {
oid.addToOid(inst) ;
return ;
}
/**
* Adds an instance part to the OID in the SnmpOid
object.
* Note that there is no getInstance
method.
* This method will directly add the instance to the SnmpOid
object.
* @param inst Dot-formatted sub-identifier String
to be appended to the OID.
* @exception SnmpStatusException An error occurred while accessing a MIB node.
*/
final public void addInstance(String inst) throws SnmpStatusException {
if (inst != null) {
oid.addToOid(inst) ;
}
return ;
}
/**
* Inserts a sub-id at the beginning of the OID of this SnmpVarBind
.
* @param oid The sub-id to insert.
*/
public void insertInOid(int oid) {
this.oid.insert(oid) ;
}
/**
* Appends the specified SnmpOid
to the end of the OID of this SnmpVarBind
.
* @param oid The OID to append.
*/
public void appendInOid(SnmpOid oid) {
this.oid.append(oid) ;
}
/**
* Determines whether the SnmpVarBind
has an SNMP exception
* (generated by agent in response to a request).
* @return true
if the SnmpVarBind
has an SNMP response exception,
* false
otherwise.
*/
final public synchronized boolean hasVarBindException() {
switch (status) {
case stValueUnspecified :
case stValueNoSuchObject :
case stValueNoSuchInstance :
case stValueEndOfMibView :
return true ;
}
return false ;
}
/**
* Clones and copies the OID and value part from another SnmpVarBind
object.
* @param var The SnmpVarBind
clone.
*/
public void copyValueAndOid(SnmpVarBind var) {
setOid((SnmpOid) (var.oid.clone())) ;
copyValue(var) ;
}
/**
* Clones and copies only the value part from another SnmpVarBind
object.
* @param var The SnmpVarBind
clone.
*/
public void copyValue(SnmpVarBind var) {
if (var.isValidValue()) {
this.value = var.getSnmpValue().duplicate() ;
setValueValid() ;
} else {
status = var.getValueStatus() ;
if (status == stValueEndOfMibView) value=endOfMibView;
else if (status == stValueNoSuchObject) value=noSuchObject;
else if (status == stValueNoSuchInstance) value=noSuchInstance;
}
}
/**
* Clones the SNMP variable. It does not clone the value portion.
* @return A new object with the value part set to null.
*/
public Object cloneWithoutValue() {
SnmpOid noid = (SnmpOid)this.oid.clone() ;
return new SnmpVarBind(noid) ;
}
/**
* Clones the SNMP variable (including value).
* @return The SNMP variable clone.
*/
@Override
public Object clone() {
// FindBugs will issue a warning here, because super.clone()
// is not called. But this is actually OK because we construct
// a brand new object.
SnmpVarBind v = new SnmpVarBind() ;
v.copyValueAndOid(this) ;
return v ;
}
/**
* Returns the printable ASCII representation for the corresponding variable value.
* @return The printable ASCII representation.
*/
final public String getStringValue() {
return this.value.toString() ;
}
/**
* Set the value to {@link #noSuchObject}. This is equivalent to
* setSnmpValue(SnmpVarBind.noSuchObject)
.
**/
final public void setNoSuchObject() {
value=noSuchObject;
status=stValueNoSuchObject;
}
/**
* Set the value to {@link #noSuchInstance}. This is equivalent to
* setSnmpValue(SnmpVarBind.noSuchInstance)
.
**/
final public void setNoSuchInstance() {
value=noSuchInstance;
status=stValueNoSuchInstance;
}
/**
* Set the value to {@link #endOfMibView}. This is equivalent to
* setSnmpValue(SnmpVarBind.endOfMibView)
.
**/
final public void setEndOfMibView() {
value=endOfMibView;
status=stValueEndOfMibView;
}
/**
* Returns the printable ASCII representation of this SnmpVarBind
.
* @return The printable ASCII representation.
*/
@Override
final public String toString() {
StringBuffer s = new StringBuffer(400) ;
s.append("Object ID : " + this.oid.toString()) ;
if (isValidValue()) {
s.append(" (Syntax : " + this.value.getTypeName() + ")\n") ;
s.append("Value : " + this.value.toString()) ;
} else {
s.append("\n" + "Value Exception : " + getValueStatusLegend()) ;
}
return s.toString() ;
}
// PRIVATE METHODS
//----------------
/**
* Sets the status to indicate that the value for this SnmpVarBind
is valid.
*/
private void setValueValid() {
if (value == endOfMibView) status=stValueEndOfMibView;
else if (value == noSuchObject) status=stValueNoSuchObject;
else if (value == noSuchInstance) status=stValueNoSuchInstance;
else status = stValueOk ;
}
private void handleLong(String oid, int index) throws NumberFormatException, SnmpStatusException {
String str;
if (index >0) {
str= oid.substring(0, index);
} else {
str= oid ;
}
// just parse the element.
//
Long.parseLong(str);
}
}