INTEGER (Enumerated)

 

Enumerated integer specifies a list of labeled integer values. For example, INTEGER{min(1), max(32)} is an enumeration of integer.

 

It should not be equated as range of numbers between 1 to 32. We cannot set number other than 1 and 32. If we try to set it will throw DataException. To set all numbers between 1 to 32, we must define the syntax as range of integer. The range of Integer should be defined as INTEGER(1..32), only then we can set value of 1 to 32.

 

Example

INTEGER {up(1), down(2), testing(3)}

 

Create Variable

 

Using SnmpVar class

 

The SnmpVar class is the base class for all SNMP variable classes. The createVariable(String value, byte type)api.jpg method creates a new SnmpVar object with the specified type and value.

 

String value = "2143483508";

SnmpVar snmpvar = SnmpVar.createVariable(value, SnmpAPI.INTEGER)

 

The string value should only be in decimal.

 

Valid Usage

 

SnmpVar snmpvar = SnmpVar.createVariable("123456", SnmpAPI.INTEGER);

SnmpVar snmpvar = SnmpVar.createVariable("-12345", SnmpAPI.INTEGER);

 

Invalid Usage

 

SnmpVar snmpvar = SnmpVar.createVariable("test", SnmpAPI.INTEGER);

 

The above usage is invalid because only decimal values are accepted and not any type of strings.

 

SnmpVar snmpvar = SnmpVar.createVariable("0x12abcd", SnmpAPI.INTEGER);

 

The above usage is invalid because only decimal values are accepted and not any hexadecimal values.

 

SnmpVar snmpvar = SnmpVar.createVariable("12345678901234567", SnmpAPI.INTEGER);

 

The above usage is invalid because the value exceeds the maximum limit of an integer. For all of the above invalid usages, an SnmpException is thrown.

 

Using SnmpInt class

 

The constructor of this class creates an SnmpIntapi.jpg object by taking integer value as argument.

 

int value = 2143483508;

SnmpInt snmpint = new SnmpInt(value);

 

Using mibs package

 

Let us now create an SnmpVar object of type enumerated integer using the LeafSyntaxapi.jpg class.

 

String mibFile = "<mib name>";

String moduleName = "RFC1213-MIB";

String nodeName = "ifOperStatus";

MibOperations mibOps = new MibOperations();

mibOps.loadMibModule(mibFile);

MibModule module = mibOps.getMibModule(moduleName);

SnmpOID snmpoid = mibOps.getSnmpOID(nodeName);

MibNode node = module.getMibNode(snmpoid);

LeafSyntax syntax = node.getSyntax();

String value = "up";

SnmpVar snmpvar = syntax.createVariable(value);

 

Valid Usage

 

SnmpVar snmpvar = syntax.createVariable("1");

SnmpVar snmpvar = syntax.createVariable("up");

 

note.jpg

Note: Binary and hex values cannot be given for creating enumerated integers.

 

Retrieve Value from Variable

 

Using SnmpVar Class

 

Following are the methods of SnmpIntapi.jpg class that are used to retrieve value from the created variable.

 

1. The getVarObject() method returns the integer value as Integer object.

 

int value = 12345; // 0x3039

SnmpInt snmpint = new SnmpInt(value);

Integer obj = (Integer)snmpint.getVarObject();

 

2. The toValue() method returns the integer value as Integer object.

 

Integer obj = (Integer)snmpint.toValue();

 

3. The toBytes() method returns the integer as a byte array of length 4.

 

byte[] array = snmpint.toBytes();

 

This byte array contains the following values.

array[0] = 0x00;

array[1] = 0x00;

array[2] = 0x30;

array[3] = 0x39;

 

4. The toString() method returns the integer value in decimal as a printable form.

 

String value = snmpint.toString();

 

5. The toTagString() method returns the integer value in decimal with tag 'INTEGER' attached to it.

 

String tagstring = snmpint.toTagString();

 

This string contain the value "INTEGER: 12345".

 

6. The intValue() method returns the integer value as "int"

 

int value = snmpint.intValue();

 

Using mibs package

 

The toString()api.jpg and toByteString()api.jpg methods of the MibOperations class are used to retrieve the value of the variable as String.

 

The following commands retrieve the value of the created variable.

 

String value = mibOps.toString(snmpvar, snmpoid);

 

This string contains the value "up(1)".

 

String value = mibOps.toString(snmpvarbind);

 

This string contains the value "ifOperStatus:-->up(1)".

 

String value = mibOps.toByteString(snmpvar, snmpoid);

 

This string contains the value "up(1)".

 

String value = mibOps.toByteString(snmpvarbind);

 

This string contains the value "ifOperStatus:-->up(1)".

 

String value = mibOps.toString(snmppdu);

 

This string contains the following value.

SNMP PDU (type unknown/unspecified)

SNMP Version: Version 1

Remote Port: 0

Community: null

Request ID: 0

Timeout: 0

Retries: 0

Error Status: no error

SNMP PDU Variable Bindings:

Object ID:

.iso.org.dod.internet.mgmt.mib-2.interfaces.ifTable.ifEntry.ifOperStatus

INTEGER: up(1)

 

String value = mibOps.toByteString(snmppdu);

 

This string contains the following value.

SNMP PDU (type unknown/unspecified)

SNMP Version: Version 1

Remote Port: 0

Community: null

Request ID: 0

Timeout: 0

Retries: 0

Error Status: no error

SNMP PDU Variable Bindings:

Object ID:

.iso.org.dod.internet.mgmt.mib-2.interfaces.ifTable.ifEntry.ifOperStatus

INTEGER: up(1)

 

String value = mibOps.toTagString(snmpvarbind);

 

This string contains the following value.

Object ID:

.iso.org.dod.internet.mgmt.mib-2.interfaces.ifTable.ifEntry.ifOperStatus

INTEGER: up(1)

 

The getEnumlabels() gets the enumeration labels from LeafSyntax object.

 

String[] labels = syntax.getEnumlabels();

for(int i =0; i< labels.length; i++)

{

System.out.println("enum labels: "+labels[i]);

}

 

The getEnumint() method is used to get the enumerated integers.

 

int[] values = syntax.getEnumint();

for(int j =0; j< values.length; j++)

{

System.out.println("enum int: "+values[j]);

}

 

The getLabel() method of the LeafSyntax class gets the label corresponding to integer value. The getInt() method gets the integer value corresponding to label.

 

note.jpg

Note: In high-level API, these objects do not have separate classes because they are handled internally.



Copyright © 2012, ZOHO Corp. All Rights Reserved.