// from the SNMPSample program, from Drexel University,
// edited into a simple version of snmpget

// Main classes used:
// 		SNMPv1CommunicationInterface
//		getMIBEntry()
//		retrieveMIBTable
//		SNMPVarBindList			(for lists of (OID,value) pairs)
//		SNMPSequence 			(for (OID,value) pairs)
// 		SNMPObjectIdentifier	(for OIDs)

import snmp.*;
import java.math.*;
import java.net.*;
import java.util.ArrayList;


public class tableget1
{

	private static String ulam2 = "ulam2.cs.luc.edu";	// two interfaces
	private static String ulam3 = "10.38.2.67";			// seven interfaces; may be firewalled
	private static String localhost = "127.0.0.1";		// use off-campus

	private static String snmphost = localhost;	// change to localhost if off-campus

    public static void main(String args[])
    {

        try
        {

        // create a communications interface to a remote SNMP-capable device;
        // need to provide the remote host's InetAddress and the community
        // name for the device; in addition, need to  supply the version number
        // for the SNMP messages to be sent (the value 0 corresponding to SNMP
        // version 1)

        InetAddress hostAddress;
        hostAddress = InetAddress.getByName(snmphost);
        String community = "public";
        int version = 0;    // SNMPv1

        SNMPv1CommunicationInterface comInterface
            	= new SNMPv1CommunicationInterface(version, hostAddress, community);

	String mib2 = "1.3.6.1.2.1";
	String ifgroup = mib2 + ".2";
	String ifTable = ifgroup + ".2";
        String itemID = ifTable;

        if (args.length != 0) itemID = args[0];			// uncomment as desired


        System.out.println("Retrieving value corresponding to OID " + itemID);

       // get the table
        // this is essentially a Vector of SNMP (OID,value) pairs.

        SNMPVarBindList tableList = comInterface.retrieveMIBTable(itemID);

	System.out.println("length of table is " + tableList.size());

	int i;
	for (i=0; i<tableList.size(); i++) {
		SNMPSequence pair = (SNMPSequence)(tableList.getSNMPObjectAt(i));

		// extract the object identifier from the pair; it's the first element in the sequence
            	SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier)pair.getSNMPObjectAt(0);
            	// extract the corresponding value from the pair; it's the second element
		SNMPObject snmpValue = pair.getSNMPObjectAt(1);
		// print out the String representation of the retrieved value
		System.out.println("Retrieved value " + i + ": type " 
			+ snmpValue.getClass().getName()
			+ ", value " + snmpValue.toString());
	}
	System.out.println();

	ArrayList<SNMPSequence> tableAList = makeArrayList(tableList);

        }
        catch(Exception e)
        {
            System.out.println("Exception during SNMP operation:  " + e + "\n");
        }

    }

    // your stuff might go here

    static public ArrayList<SNMPSequence> makeArrayList(SNMPVarBindList list) {

		ArrayList<SNMPSequence> alist = new ArrayList<SNMPSequence>();

		for (int i=0; i<list.size(); i++) {
			SNMPObject so = list.getSNMPObjectAt(i);	// guaranteed type
			if (so instanceof SNMPSequence) {		// should be this type
				alist.add((SNMPSequence) so);
			} else {
				System.err.println("makeArrayList(): object "+i+" not an SNMPSequence: " + so);
			}
		}
		return alist;
	}


}
