package org.jboss.jmx.adaptor.snmp.agent;
import java.net.InetAddress;
import javax.management.MBeanServer;
import org.jboss.logging.Logger;
import org.opennms.protocols.snmp.SnmpAgentSession;
import org.opennms.protocols.snmp.SnmpObjectId;
import org.opennms.protocols.snmp.SnmpOctetString;
import org.opennms.protocols.snmp.SnmpPduPacket;
import org.opennms.protocols.snmp.SnmpPduRequest;
import org.opennms.protocols.snmp.SnmpSyntax;
import org.opennms.protocols.snmp.SnmpVarBind;
public class RequestHandlerSupport
implements RequestHandler
{
protected Logger log;
protected MBeanServer server;
protected String resourceName;
protected Clock clock;
public RequestHandlerSupport()
{
}
public void initialize(String resourceName, MBeanServer server, Logger log, Clock uptime)
throws Exception
{
this.resourceName = resourceName;
this.server = server;
this.log = log;
this.clock = uptime;
}
public SnmpPduRequest snmpReceivedGet(SnmpPduPacket pdu, boolean getNext)
{
SnmpPduRequest response = null;
int pduLength = pdu.getLength();
log.debug("requestId=" + pdu.getRequestId() + ", pduLength=" + pduLength);
SnmpVarBind[] vblist = new SnmpVarBind[pduLength];
int errorStatus = SnmpPduPacket.ErrNoError;
int errorIndex = 0;
for (int i = 0; i < pduLength ; i++ )
{
SnmpVarBind vb = pdu.getVarBindAt(i);
SnmpObjectId oid = vb.getName();
if (getNext)
{
log.debug(
"Should call getNextOid() to find out what is the next valid OID " +
"instance in the supported MIB tree. Assign that OID to the VB List " +
"and then proceed same as that of get request" );
}
vblist[i] = new SnmpVarBind(oid);
log.debug("oid=" + oid.toString());
log.debug("Should call the respective interface to retrieve current value for this OID" );
SnmpSyntax result = null;
if (result == null)
{
errorStatus = SnmpPduPacket.ErrNoSuchName;
errorIndex = i + 1;
}
else
{
vblist[i].setValue(result);
log.debug("Varbind[" + i + "] := " + vblist[i].getName().toString());
log.debug(" --> " + vblist[i].getValue().toString());
}
}
response = new SnmpPduRequest(SnmpPduPacket.RESPONSE, vblist);
response.setErrorStatus(errorStatus);
response.setErrorIndex(errorIndex);
return response;
}
public SnmpPduRequest snmpReceivedSet(SnmpPduPacket pdu)
{
SnmpPduRequest response = null;
int errorStatus = SnmpPduPacket.ErrNoError;
int errorIndex = 0;
int k = pdu.getLength();
SnmpVarBind[] vblist = new SnmpVarBind[k];
for (int i = 0; i < k ; i++ )
{
SnmpVarBind vb = pdu.getVarBindAt(i);
vblist[i] = new SnmpVarBind(vb);
SnmpObjectId oid = vb.getName();
SnmpSyntax result = null;
log.debug("Should call the respective interface to assign a value for this OID" );
if (result != null)
{
errorStatus = SnmpPduPacket.ErrReadOnly;
errorIndex = i + 1;
log.debug("Error occured " + vb.getName().toString());
}
log.debug("Varbind[" + i + "] := " + vb.getName().toString());
log.debug(" --> " + vb.getValue().toString());
}
response = new SnmpPduRequest(SnmpPduPacket.RESPONSE, vblist);
response.setErrorStatus(errorStatus);
response.setErrorIndex(errorIndex);
return response;
}
public void snmpReceivedPdu(SnmpAgentSession session, InetAddress manager, int port,
SnmpOctetString community, SnmpPduPacket pdu)
{
log.error("Message from manager " + manager.toString() + " on port " + port);
int cmd = pdu.getCommand();
log.error("Unsupported PDU command......... " + cmd);
}
public void SnmpAgentSessionError(SnmpAgentSession session, int error, Object ref)
{
log.error("An error occured in the trap session");
log.error("Session error code = " + error);
if(ref != null)
{
log.error("Session error reference: " + ref.toString());
}
if(error == SnmpAgentSession.ERROR_EXCEPTION)
{
synchronized(session)
{
session.notify(); }
}
}
}