[jboss-cvs] JBossAS SVN: r101130 - in branches/snmp4j-integration/varia/src: main/java/org/jboss/jmx/adaptor/snmp/test and 2 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Thu Feb 18 15:22:43 EST 2010
Author: bshim
Date: 2010-02-18 15:22:43 -0500 (Thu, 18 Feb 2010)
New Revision: 101130
Modified:
branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandler.java
branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandlerImpl.java
branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandlerSupport.java
branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/SnmpAgentService.java
branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/test/NotificationProducerService.java
branches/snmp4j-integration/varia/src/resources/services/snmp/deploy/META-INF/jboss-service.xml
branches/snmp4j-integration/varia/src/resources/services/snmp/deploy/notifications.xml
Log:
worked on creation of response PDU for GET request
Modified: branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandler.java
===================================================================
--- branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandler.java 2010-02-18 17:43:56 UTC (rev 101129)
+++ branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandler.java 2010-02-18 20:22:43 UTC (rev 101130)
@@ -24,7 +24,7 @@
import javax.management.MBeanServer;
import org.jboss.logging.Logger;
-import org.opennms.protocols.snmp.SnmpAgentHandler;
+//import org.opennms.protocols.snmp.SnmpAgentHandler;
/**
* Interface that must be implemented by classes
@@ -33,7 +33,7 @@
* @author <a href="mailto:dimitris at jboss.org">Dimitris Andreadis</a>
* @version $Revision$
*/
-public interface RequestHandler extends SnmpAgentHandler
+public interface RequestHandler extends SnmpAgentRequestHandler
{
/**
* Perform initialization, possibly by parsing
Modified: branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandlerImpl.java
===================================================================
--- branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandlerImpl.java 2010-02-18 17:43:56 UTC (rev 101129)
+++ branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandlerImpl.java 2010-02-18 20:22:43 UTC (rev 101130)
@@ -29,6 +29,7 @@
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
+import java.util.Vector;
import javax.management.Attribute;
import javax.management.MBeanServer;
@@ -41,7 +42,19 @@
import org.jboss.xb.binding.ObjectModelFactory;
import org.jboss.xb.binding.Unmarshaller;
import org.jboss.xb.binding.UnmarshallerFactory;
-import org.opennms.protocols.snmp.*;
+//import org.opennms.protocols.snmp.*;
+import org.snmp4j.PDU;
+import org.snmp4j.Snmp;
+import org.snmp4j.event.ResponseEvent;
+import org.snmp4j.smi.Counter32;
+import org.snmp4j.smi.Counter64;
+import org.snmp4j.smi.Integer32;
+import org.snmp4j.smi.Null;
+import org.snmp4j.smi.OID;
+import org.snmp4j.smi.OctetString;
+import org.snmp4j.smi.TimeTicks;
+import org.snmp4j.smi.Variable;
+import org.snmp4j.smi.VariableBinding;
/**
* Implement RequestHandler with mapping of snmp get/set requests
@@ -141,87 +154,129 @@
* @return SnmpPduRequest filled in with the proper response, or null if
* cannot process NOTE: this might be changed to throw an exception.
*/
- public SnmpPduRequest snmpReceivedGet(SnmpPduPacket pdu, boolean getNext)
+ public PDU snmpReceivedGet(PDU pdu, boolean getNext)
{
- try
- {
- SnmpPduRequest response = null;
- int pduLength = pdu.getLength();
- final boolean trace = log.isTraceEnabled();
-
- if (trace)
- log.trace("requestId=" + pdu.getRequestId() + ", pduLength="
- + pduLength + ", getNext=" + getNext);
-
- SnmpVarBind[] vblist = new SnmpVarBind[pduLength];
- int errorStatus = SnmpPduPacket.ErrNoError;
- int errorIndex = 0;
-
- // Process for each varibind in the request
- for (int i = 0; i < pduLength; i++)
- {
- boolean good = true;
- SnmpVarBind vb = pdu.getVarBindAt(i);
- SnmpObjectId oid = vb.getName();
- if (getNext)
- {
- /*
- * We call getNextOid() to find out what is the next valid OID
- * instance in the supported MIB (sub-)tree. Assign that OID to the
- * VB List and then proceed same as that of get request. If the
- * passed oid is already the last, we flag it.
- */
- ComparableSnmpObjectId coid = new ComparableSnmpObjectId(oid);
- oid = getNextOid(coid, true);
- if (oid == null)
- {
- good = false;
- }
- else
- {
- pdu.setVarBindAt(i, new SnmpVarBind(oid));
- }
+ //later on should be able to get rid of getNext because pdu.getType() should have it
+
+ PDU response = new PDU();
+ response.setType(PDU.RESPONSE);
+ //Start of MINE
+ //response = (PDU)pdu.clone();
+
+ final boolean trace = log.isTraceEnabled();
+
+ if (trace) {
+ log.trace("requestID=" + pdu.getRequestID() + ", elementCount="
+ + pdu.size() + ", getNext=" + getNext);
+ }
+
+ if (pdu != null){
+
+ if (getNext){
+ //TODO do the GETNEXT case
}
- if (oid!=null)
- vblist[i] = new SnmpVarBind(oid);
- else
- vblist[i] = new SnmpVarBind(vb.getName()); // oid passed in
+ Iterator it = pdu.getVariableBindings().iterator();
+
+ while (it.hasNext()){
+ VariableBinding vb = (VariableBinding)it.next();
+ OID oid = vb.getOid();
+ //setup the new variable binding to put into the response pdu
+ VariableBinding newVB = new VariableBinding(oid);
+
+ //some errorchecking ....
+ //else is good and do:
+ Variable var = getValueFor(oid);
+ newVB.setVariable(var);
+ response.add(newVB);
+ }
+ }
+ //END IF MINE
- if (trace)
- log.trace("oid=" + oid);
-
- SnmpSyntax result = null;
- if (good && bindings != null)
- result = getValueFor(oid);
-
- if (trace)
- log.trace("got result of " + result);
-
- if (result == null || !good)
- {
- errorStatus = SnmpPduPacket.ErrNoSuchName;
- errorIndex = i + 1;
- log.debug("Error Occured " + vb.getName().toString());
- }
- else
- {
- vblist[i].setValue(result);
- log.debug("Varbind[" + i + "] := "
- + vblist[i].getName().toString());
- log.debug(" --> " + vblist[i].getValue().toString());
- }
- } // for ...
- response = new SnmpPduRequest(SnmpPduPacket.RESPONSE, vblist);
- response.setErrorStatus(errorStatus);
- response.setErrorIndex(errorIndex);
- return response;
- } catch (Exception e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return null;
- }
+ return response;
+
+
+// try
+// {
+// SnmpPduRequest response = null;
+// int pduLength = pdu.getLength();
+// final boolean trace = log.isTraceEnabled();
+//
+// if (trace)
+// log.trace("requestId=" + pdu.getRequestId() + ", pduLength="
+// + pduLength + ", getNext=" + getNext);
+//
+// SnmpVarBind[] vblist = new SnmpVarBind[pduLength];
+// int errorStatus = SnmpPduPacket.ErrNoError;
+// int errorIndex = 0;
+//
+// // Process for each varibind in the request
+// for (int i = 0; i < pduLength; i++)
+// {
+// boolean good = true;
+// SnmpVarBind vb = pdu.getVarBindAt(i);
+// SnmpObjectId oid = vb.getName();
+ //START OF GETNEXT
+// if (getNext) //i don't care about GETNEXT, doing GET first
+// {
+// /*
+// * We call getNextOid() to find out what is the next valid OID
+// * instance in the supported MIB (sub-)tree. Assign that OID to the
+// * VB List and then proceed same as that of get request. If the
+// * passed oid is already the last, we flag it.
+// */
+// ComparableSnmpObjectId coid = new ComparableSnmpObjectId(oid);
+// oid = getNextOid(coid, true);
+// if (oid == null)
+// {
+// good = false;
+// }
+// else
+// {
+// pdu.setVarBindAt(i, new SnmpVarBind(oid));
+// }
+// }
+ //End of GETNEXT
+// if (oid!=null)
+// vblist[i] = new SnmpVarBind(oid);
+// else
+// vblist[i] = new SnmpVarBind(vb.getName()); // oid passed in
+//
+//
+// if (trace)
+// log.trace("oid=" + oid);
+//
+// SnmpSyntax result = null;
+// if (good && bindings != null)
+// result = getValueFor(oid);
+//
+// if (trace)
+// log.trace("got result of " + result);
+//
+// if (result == null || !good)
+// {
+// errorStatus = SnmpPduPacket.ErrNoSuchName;
+// errorIndex = i + 1;
+// log.debug("Error Occured " + vb.getName().toString());
+// }
+// else
+// {
+// vblist[i].setValue(result);
+// log.debug("Varbind[" + i + "] := "
+// + vblist[i].getName().toString());
+// log.debug(" --> " + vblist[i].getValue().toString());
+// }
+// } // for ...
+// response = new SnmpPduRequest(SnmpPduPacket.RESPONSE, vblist);
+// response.setErrorStatus(errorStatus);
+// response.setErrorIndex(errorIndex);
+// return response;
+// } catch (Exception e)
+// {
+// // TODO Auto-generated catch block
+// e.printStackTrace();
+// return null;
+// }
}
/**
@@ -237,52 +292,52 @@
* @return SnmpPduRequest filled in with the proper response, or null if
* cannot process NOTE: this might be changed to throw an exception.
*/
- public SnmpPduRequest snmpReceivedSet(SnmpPduPacket pdu)
- {
- final boolean trace = log.isTraceEnabled();
- 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 newVal = vb.getValue();
- if (trace)
- log.trace("set: received oid " + oid.toString() + " with value " + newVal.toString());
- SnmpSyntax result = null;
- try
- {
- result = setValueFor(oid,newVal);
- }
- catch (ReadOnlyException e)
- {
- errorStatus = SnmpPduPacket.ErrReadOnly;
- errorIndex = i + 1;
- }
-
- if (result != null)
- {
- errorStatus = SnmpPduPacket.ErrReadOnly;
- errorIndex = i + 1;
- log.debug("Error occured " + vb.getName().toString());
- }
-
- if (trace)
- {
- log.trace("Varbind[" + i + "] := " + vb.getName().toString());
- log.trace(" --> " + vb.getValue().toString());
- }
- }
- response = new SnmpPduRequest(SnmpPduPacket.RESPONSE, vblist);
- response.setErrorStatus(errorStatus);
- response.setErrorIndex(errorIndex);
-
- return response;
+ public PDU snmpReceivedSet(PDU pdu)
+ {return null;
+// final boolean trace = log.isTraceEnabled();
+// 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 newVal = vb.getValue();
+// if (trace)
+// log.trace("set: received oid " + oid.toString() + " with value " + newVal.toString());
+// SnmpSyntax result = null;
+// try
+// {
+// result = setValueFor(oid,newVal);
+// }
+// catch (ReadOnlyException e)
+// {
+// errorStatus = SnmpPduPacket.ErrReadOnly;
+// errorIndex = i + 1;
+// }
+//
+// if (result != null)
+// {
+// errorStatus = SnmpPduPacket.ErrReadOnly;
+// errorIndex = i + 1;
+// log.debug("Error occured " + vb.getName().toString());
+// }
+//
+// if (trace)
+// {
+// log.trace("Varbind[" + i + "] := " + vb.getName().toString());
+// log.trace(" --> " + vb.getValue().toString());
+// }
+// }
+// response = new SnmpPduRequest(SnmpPduPacket.RESPONSE, vblist);
+// response.setErrorStatus(errorStatus);
+// response.setErrorIndex(errorIndex);
+//
+// return response;
}
/**
@@ -304,12 +359,12 @@
* The SNMP pdu
*
*/
- public void snmpReceivedPdu(SnmpAgentSession session, InetAddress manager,
- int port, SnmpOctetString community, SnmpPduPacket pdu)
+ public void snmpReceivedPdu(Snmp session, InetAddress manager,
+ int port, OctetString community, PDU pdu)
{
- log.error("Message from manager " + manager.toString() + " on port " + port);
- int cmd = pdu.getCommand();
- log.error("Unsupported PDU command......... " + cmd);
+// log.error("Message from manager " + manager.toString() + " on port " + port);
+// int cmd = pdu.getCommand();
+// log.error("Unsupported PDU command......... " + cmd);
}
/**
@@ -336,22 +391,22 @@
* The PDU reference, or potentially null. It may also be an
* exception.
*/
- public void SnmpAgentSessionError(SnmpAgentSession session, int error, Object ref)
+ public void SnmpAgentSessionError(Snmp 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(); // close the session
- }
- }
+// 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(); // close the session
+// }
+// }
}
// Private -------------------------------------------------------
@@ -419,8 +474,8 @@
BindEntry be = new BindEntry(oid, mmb.getName(), ma.getName());
be.isReadWrite = ma.isReadWrite();
- ComparableSnmpObjectId coid = new ComparableSnmpObjectId(oid);
-
+ //ComparableSnmpObjectId coid = new ComparableSnmpObjectId(oid);
+ OID coid = new OID(oid);
if (log.isTraceEnabled())
log.trace("New bind entry " + be);
if (bindings.containsKey(coid)) {
@@ -443,7 +498,7 @@
}
}
}
-
+
/**
* Return the current value for the given oid
*
@@ -451,132 +506,196 @@
* The oid we want a value for
* @return SnmpNull if no value present
*/
- private SnmpSyntax getValueFor(final SnmpObjectId oid) {
+ private Variable getValueFor(final OID oid) {
BindEntry be = findBindEntryForOid(oid);
- SnmpSyntax ssy = null;
+ Variable ssy = null;
if (be != null)
- {
+ {
if (log.isTraceEnabled())
log.trace("Found entry " + be.toString() + " for oid " + oid);
-
+
try
- {
+ {
Object val = server.getAttribute(be.mbean, be.attr.getName());
if (val instanceof Long)
- {
+ {
Long uin = (Long) val;
- ssy = new SnmpUInt32(uin);
+
+ //consider Counter64 if there's problems
+ ssy = new Counter32(uin);
}
- else if (val instanceof String)
- {
+ else if (val instanceof String)
+ {
String in = (String) val;
- ssy = new SnmpOctetString(in.getBytes());
+ ssy = new OctetString(in.getBytes());
}
- else if (val instanceof Integer)
- {
+ else if (val instanceof Integer)
+ {
Integer in = (Integer) val;
- ssy = new SnmpInt32(in);
+ ssy = new Integer32(in);
}
- else if (val instanceof SnmpObjectId)
- {
- ssy = (SnmpObjectId)val;
- }
- else if (val instanceof SnmpTimeTicks)
- {
- ssy = (SnmpTimeTicks)val;
- }
- else
+
+ //not sure about how to handle this case yet
+// else if (val instanceof SnmpObjectId)
+// {
+// ssy = (SnmpObjectId)val;
+// }
+ else if (val instanceof TimeTicks)
+ {
+ ssy = (TimeTicks)val;
+ }
+ else
log.info("Unknown type for " + be);
}
- catch (Exception e)
- {
+ catch (Exception e)
+ {
log.warn("getValueFor (" + be.mbean.toString() + ", "
+ be.attr.getName() + ": " + e.toString());
- }
- }
- else
- {
- ssy = new SnmpNull();
+ }
+ }
+ else
+ {
+ ssy = new Null();
log.info(NO_ENTRY_FOUND_FOR_OID + oid);
}
return ssy;
}
+
+// /**
+// * Return the current value for the given oid
+// *
+// * @param oid
+// * The oid we want a value for
+// * @return SnmpNull if no value present
+// */
+// private SnmpSyntax getValueFor(final SnmpObjectId oid) {
+//
+// BindEntry be = findBindEntryForOid(oid);
+// SnmpSyntax ssy = null;
+// if (be != null)
+// {
+// if (log.isTraceEnabled())
+// log.trace("Found entry " + be.toString() + " for oid " + oid);
+//
+// try
+// {
+// Object val = server.getAttribute(be.mbean, be.attr.getName());
+//
+// if (val instanceof Long)
+// {
+// Long uin = (Long) val;
+// ssy = new SnmpUInt32(uin);
+// }
+// else if (val instanceof String)
+// {
+// String in = (String) val;
+// ssy = new SnmpOctetString(in.getBytes());
+// }
+// else if (val instanceof Integer)
+// {
+// Integer in = (Integer) val;
+// ssy = new SnmpInt32(in);
+// }
+// else if (val instanceof SnmpObjectId)
+// {
+// ssy = (SnmpObjectId)val;
+// }
+// else if (val instanceof SnmpTimeTicks)
+// {
+// ssy = (SnmpTimeTicks)val;
+// }
+// else
+// log.info("Unknown type for " + be);
+// }
+// catch (Exception e)
+// {
+// log.warn("getValueFor (" + be.mbean.toString() + ", "
+// + be.attr.getName() + ": " + e.toString());
+// }
+// }
+// else
+// {
+// ssy = new SnmpNull();
+// log.info(NO_ENTRY_FOUND_FOR_OID + oid);
+// }
+// return ssy;
+// }
- /**
- * Set a jmx attribute
- * @param oid The oid to set. This is translated into a mbean / attribute pair
- * @param newVal The new value to set
- * @return null on success, non-null on failure
- * @throws ReadOnlyException If the referred entry is read only.
- */
- private SnmpSyntax setValueFor(final SnmpObjectId oid, final SnmpSyntax newVal) throws ReadOnlyException
- {
- final boolean trace = log.isTraceEnabled();
-
- BindEntry be = findBindEntryForOid(oid);
-
- if (trace)
- log.trace("setValueFor: found bind entry for " + oid);
-
- SnmpSyntax ssy = null;
- if (be != null)
- {
- if (trace)
- log.trace("setValueFor: " + be.toString());
-
- if (be.isReadWrite == false)
- {
- if (trace)
- log.trace("setValueFor: this is marked read only");
-
- throw new ReadOnlyException(oid);
- }
- try
- {
- Object val = null;
- if (newVal instanceof SnmpOctetString)
- {
- val = newVal.toString();
- }
- else if (newVal instanceof SnmpInt32)
- {
- val = new Integer(((SnmpInt32)newVal).getValue());
- }
- else if (newVal instanceof SnmpUInt32)
- {
- val = new Long(((SnmpUInt32)newVal).getValue());
- }
- // TODO do more mumbo jumbo for type casting / changing
-
- if (val != null)
- {
- Attribute at = new Attribute(be.attr.getName(), val);
- server.setAttribute(be.mbean, at);
- if (trace)
- log.trace("setValueFor: set attribute in mbean-Server");
- }
- else
- {
- log.debug("Did not find a suitable data type for newVal " + newVal);
- ssy = new SnmpNull();
- }
- // TODO
- }
- catch (Exception e )
- {
- log.debug("setValueFor: exception " + e.getMessage());
- ssy = new SnmpNull();
- }
- }
- else
- {
- ssy = new SnmpNull();
- log.info(NO_ENTRY_FOUND_FOR_OID + oid);
- }
- return ssy;
- }
+// /**
+// * Set a jmx attribute
+// * @param oid The oid to set. This is translated into a mbean / attribute pair
+// * @param newVal The new value to set
+// * @return null on success, non-null on failure
+// * @throws ReadOnlyException If the referred entry is read only.
+// */
+// private SnmpSyntax setValueFor(final SnmpObjectId oid, final SnmpSyntax newVal) throws ReadOnlyException
+// {
+// final boolean trace = log.isTraceEnabled();
+//
+// BindEntry be = findBindEntryForOid(oid);
+//
+// if (trace)
+// log.trace("setValueFor: found bind entry for " + oid);
+//
+// SnmpSyntax ssy = null;
+// if (be != null)
+// {
+// if (trace)
+// log.trace("setValueFor: " + be.toString());
+//
+// if (be.isReadWrite == false)
+// {
+// if (trace)
+// log.trace("setValueFor: this is marked read only");
+//
+// throw new ReadOnlyException(oid);
+// }
+// try
+// {
+// Object val = null;
+// if (newVal instanceof SnmpOctetString)
+// {
+// val = newVal.toString();
+// }
+// else if (newVal instanceof SnmpInt32)
+// {
+// val = new Integer(((SnmpInt32)newVal).getValue());
+// }
+// else if (newVal instanceof SnmpUInt32)
+// {
+// val = new Long(((SnmpUInt32)newVal).getValue());
+// }
+// // TODO do more mumbo jumbo for type casting / changing
+//
+// if (val != null)
+// {
+// Attribute at = new Attribute(be.attr.getName(), val);
+// server.setAttribute(be.mbean, at);
+// if (trace)
+// log.trace("setValueFor: set attribute in mbean-Server");
+// }
+// else
+// {
+// log.debug("Did not find a suitable data type for newVal " + newVal);
+// ssy = new SnmpNull();
+// }
+// // TODO
+// }
+// catch (Exception e )
+// {
+// log.debug("setValueFor: exception " + e.getMessage());
+// ssy = new SnmpNull();
+// }
+// }
+// else
+// {
+// ssy = new SnmpNull();
+// log.info(NO_ENTRY_FOUND_FOR_OID + oid);
+// }
+// return ssy;
+// }
/**
@@ -585,18 +704,39 @@
* @param oid The oid look up.
* @return a bind entry or null.
*/
- private BindEntry findBindEntryForOid(final SnmpObjectId oid) {
+ private BindEntry findBindEntryForOid(final OID oid) {
- ComparableSnmpObjectId coid= new ComparableSnmpObjectId(oid);
+ //param probably not supposed to be OID type
+// ComparableSnmpObjectId coid= new ComparableSnmpObjectId(oid.toString());
- if (coid.isLeaf())
- {
- coid = coid.removeLastPart();
- }
- BindEntry be = (BindEntry)bindings.get(coid);
+ //add possible oid format checking
+// if (coid.isLeaf())
+// {
+// coid = coid.removeLastPart();
+// }
+ BindEntry be = (BindEntry)bindings.get(oid);
return be;
}
+
+// /**
+// * Lookup a BinEntry on the given oid. If the oid ends in .0,
+// * then the .0 will be stripped of before the search.
+// * @param oid The oid look up.
+// * @return a bind entry or null.
+// */
+// private BindEntry findBindEntryForOid(final SnmpObjectId oid) {
+//
+// ComparableSnmpObjectId coid= new ComparableSnmpObjectId(oid);
+//
+// if (coid.isLeaf())
+// {
+// coid = coid.removeLastPart();
+// }
+// BindEntry be = (BindEntry)bindings.get(coid);
+//
+// return be;
+// }
/**
* Return the next oid that is larger than ours.
@@ -668,7 +808,7 @@
* @author <a href="mailto:pilhuhn at user.sf.net>">Heiko W. Rupp</a>
*/
private class BindEntry implements Comparable {
- private final ComparableSnmpObjectId oid;
+ private final OID oid;
private ObjectName mbean;
private Attribute attr;
@@ -687,7 +827,7 @@
* The name of the attribute to query
*/
BindEntry(final String oidString, final String mbName, final String attrName) {
- this(new ComparableSnmpObjectId(oidString),
+ this(new OID(oidString),
mbName,
attrName);
}
@@ -698,7 +838,7 @@
* @param mbName The name of an MBean with attribute to query
* @param attrName The name of the attribute to query
*/
- BindEntry(final ComparableSnmpObjectId coid, final String mbName, final String attrName) {
+ BindEntry(final OID coid, final String mbName, final String attrName) {
oid = coid;
this.mName = mbName;
this.aName = attrName;
@@ -739,7 +879,7 @@
return mbean;
}
- public ComparableSnmpObjectId getOid()
+ public OID getOid()
{
return oid;
}
@@ -765,10 +905,10 @@
return 0;
BindEntry obe = (BindEntry) other;
- if (getOid().equals(obe.getOid()))
- return 0;
+// if (getOid().equals(obe.getOid()))
+// return 0;
- int res =oid.compare(obe.getOid());
+ int res =oid.compareTo(obe.getOid());
return res;
}
Modified: branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandlerSupport.java
===================================================================
--- branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandlerSupport.java 2010-02-18 17:43:56 UTC (rev 101129)
+++ branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandlerSupport.java 2010-02-18 20:22:43 UTC (rev 101130)
@@ -26,13 +26,14 @@
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;
+//import org.opennms.protocols.snmp.SnmpAgentSession;
+//import org.opennms.protocols.snmp.SnmpObjectId;
+//import org.opennms.protocols.snmp.SnmpPduRequest;
+//import org.opennms.protocols.snmp.SnmpSyntax;
+//import org.opennms.protocols.snmp.SnmpVarBind;
+import org.snmp4j.PDU;
+import org.snmp4j.Snmp;
+import org.snmp4j.smi.OctetString;
/**
* Implement RequestHandler with simple tracing of incoming requests.
@@ -98,54 +99,54 @@
* @return SnmpPduRequest filled in with the proper response, or null if cannot process
* NOTE: this might be changed to throw an exception.
*/
- 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;
-
- //Process for each varibind in the request
- 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;
- //log.debug("Error Occured " + vb.getName().toString());
- }
- 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 PDU snmpReceivedGet(PDU pdu, boolean getNext)
+ {return null;
+// 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;
+//
+// //Process for each varibind in the request
+// 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;
+// //log.debug("Error Occured " + vb.getName().toString());
+// }
+// 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;
}
/**
@@ -159,39 +160,41 @@
* @return SnmpPduRequest filled in with the proper response, or null if cannot process
* NOTE: this might be changed to throw an exception.
*/
- 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 PDU snmpReceivedSet(PDU pdu)
+ {
+
+ return null;
+// 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;
}
/**
@@ -207,12 +210,12 @@
* @param pdu The SNMP pdu
*
*/
- public void snmpReceivedPdu(SnmpAgentSession session, InetAddress manager, int port,
- SnmpOctetString community, SnmpPduPacket pdu)
+ public void snmpReceivedPdu(Snmp session, InetAddress manager, int port,
+ OctetString community, PDU pdu)
{
- log.error("Message from manager " + manager.toString() + " on port " + port);
- int cmd = pdu.getCommand();
- log.error("Unsupported PDU command......... " + cmd);
+// log.error("Message from manager " + manager.toString() + " on port " + port);
+// int cmd = pdu.getCommand();
+// log.error("Unsupported PDU command......... " + cmd);
}
/**
@@ -235,21 +238,21 @@
* @param ref The PDU reference, or potentially null.
* It may also be an exception.
*/
- public void SnmpAgentSessionError(SnmpAgentSession session, int error, Object ref)
+ public void SnmpAgentSessionError(Snmp 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(); // close the session
- }
- }
+// 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(); // close the session
+// }
+// }
}
}
Modified: branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/SnmpAgentService.java
===================================================================
--- branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/SnmpAgentService.java 2010-02-18 17:43:56 UTC (rev 101129)
+++ branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/SnmpAgentService.java 2010-02-18 20:22:43 UTC (rev 101130)
@@ -32,10 +32,13 @@
//import org.opennms.protocols.snmp.SnmpSMI;
//SNMP4J IMPORTS
+import org.snmp4j.CommandResponder;
+import org.snmp4j.CommandResponderEvent;
import org.snmp4j.Snmp;
import org.snmp4j.smi.*;
import org.snmp4j.transport.DefaultTcpTransportMapping;
import org.snmp4j.transport.DefaultUdpTransportMapping;
+import org.snmp4j.PDU;
/**
* <tt>SnmpAgentService</tt> is an MBean class implementing an SNMP agent.
@@ -54,7 +57,7 @@
* @version $Revision$
*/
public class SnmpAgentService extends ListenerServiceMBeanSupport
- implements SnmpAgentServiceMBean
+ implements SnmpAgentServiceMBean, CommandResponder
{
/** Supported versions */
public static final int SNMPV1 = 1;
@@ -576,6 +579,14 @@
throws Exception
{
+ // Instantiate and initialize the RequestHandler implementation
+ requestHandler = (RequestHandler)Class.forName(
+ this.requestHandlerClassName, true, this.getClass().getClassLoader()).newInstance();
+ requestHandler.initialize(this.requestHandlerResName, this.getServer(), this.log, this.clock);
+
+ Snmp snmp = new Snmp(new DefaultTcpTransportMapping(new TcpAddress(this.bindAddress, this.port)));
+ snmp.addCommandResponder(this);
+ snmp.listen();
/********************************************************************
*
* ORIG!!!!
@@ -616,11 +627,10 @@
// peer.getParameters().setWriteCommunity(this.writeCommunity);
// peer.getParameters().setVersion(this.snmpVersion == SNMPV2 ? SnmpSMI.SNMPV2 : SnmpSMI.SNMPV1);
//
-// // Instantiate and initialize the RequestHandler implementation
-// requestHandler = (RequestHandler)Class.forName(
-// this.requestHandlerClassName, true, this.getClass().getClassLoader()).newInstance();
-// requestHandler.initialize(this.requestHandlerResName, this.getServer(), this.log, this.clock);
-//
+
+
+
+
// // Instantiate the AgentSession with an optional thread pool
// this.agentSession = this.numberOfThreads > 1
// ? new SnmpAgentSession(requestHandler, peer, this.numberOfThreads)
@@ -639,4 +649,29 @@
return InetAddress.getByName(host);
}
+ @Override
+ public void processPdu(CommandResponderEvent event) {
+ PDU pdu = event.getPDU();
+
+ if (pdu == null)
+ return;
+
+ int type = pdu.getType();
+ //switch based on pdu.getType() == ___
+
+ switch (type) {
+ case PDU.GET:
+ requestHandler.snmpReceivedGet(pdu, false);
+ case PDU.GETBULK:
+ //TODO maybe make a method called snmpReceivedGetBulk in requesthandler
+ // requestHandler.snmpReceivedGet(pdu);
+ case PDU.GETNEXT:
+ requestHandler.snmpReceivedGet(pdu, true);
+ case PDU.SET:
+ requestHandler.snmpReceivedSet(pdu);
+ default:
+ //invalid pdu type
+ }
+ }
+
}
Modified: branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/test/NotificationProducerService.java
===================================================================
--- branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/test/NotificationProducerService.java 2010-02-18 17:43:56 UTC (rev 101129)
+++ branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/test/NotificationProducerService.java 2010-02-18 20:22:43 UTC (rev 101130)
@@ -19,9 +19,23 @@
**/
package org.jboss.jmx.adaptor.snmp.test;
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
import javax.management.Notification;
import org.jboss.jmx.adaptor.snmp.agent.EventTypes;
import org.jboss.system.ServiceMBeanSupport;
+import org.snmp4j.CommunityTarget;
+import org.snmp4j.PDU;
+import org.snmp4j.Snmp;
+import org.snmp4j.Target;
+import org.snmp4j.event.ResponseEvent;
+import org.snmp4j.smi.Address;
+import org.snmp4j.smi.OID;
+import org.snmp4j.smi.TcpAddress;
+import org.snmp4j.smi.VariableBinding;
+import org.snmp4j.transport.DefaultTcpTransportMapping;
/**
* <tt>NotificationProducerService</tt> is a test class with an MBean interface
@@ -84,5 +98,42 @@
"V3 test notifications"));
}
+
+ /**
+ * Sends a test GET request
+ */
+ public void get(){
+ PDU pdu = new PDU();
+ pdu.setType(PDU.GET);
+ pdu.add(new VariableBinding(new OID("1.2.3.4.5.6.7.6")));
+
+ Target target = new CommunityTarget();
+ try {
+ target.setAddress(new TcpAddress(InetAddress.getByName("127.0.0.1"), 1161));
+ DefaultTcpTransportMapping transport = new DefaultTcpTransportMapping();
+ Snmp snmp = new Snmp(transport);
+ ResponseEvent responseEvent = snmp.send(pdu, target);
+ System.out.println("response " + responseEvent.toString());
+ // extract the response PDU (could be null if timed out)
+ PDU responsePDU = responseEvent.getResponse();
+ System.out.println("responsePdu = " + responsePDU);
+
+ } catch (UnknownHostException e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ } catch (IOException e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ }
+
+ }
+
+ /**
+ * Sends a test SET request
+ */
+ public void set(){
+
+ }
+
} // class NotificationProducerService
Modified: branches/snmp4j-integration/varia/src/resources/services/snmp/deploy/META-INF/jboss-service.xml
===================================================================
--- branches/snmp4j-integration/varia/src/resources/services/snmp/deploy/META-INF/jboss-service.xml 2010-02-18 17:43:56 UTC (rev 101129)
+++ branches/snmp4j-integration/varia/src/resources/services/snmp/deploy/META-INF/jboss-service.xml 2010-02-18 20:22:43 UTC (rev 101130)
@@ -85,10 +85,10 @@
</attribute>
</mbean>
- <!-- Used for internal testing - ignore
+ <!-- Used for internal testing - ignore -->
<mbean code="org.jboss.jmx.adaptor.snmp.test.NotificationProducerService"
name="jboss.jmx:name=SnmpAgent,service=notification,type=producer"/>
- -->
+
<!-- The SNMP adaptor MBean -->
<mbean code="org.jboss.jmx.adaptor.snmp.agent.SnmpAgentService"
@@ -124,13 +124,13 @@
<notification type="jboss.snmp.agent.heartbeat"/>
</mbean>
- <!-- Monitor NotificationProducer test events (internal testing) -ignore
+ <!-- Monitor NotificationProducer test events (internal testing) -ignore -->
<mbean name="jboss.jmx:name=SnmpAgent,service=notification,type=producer">
<notification type="jboss.snmp.agent.v1test"/>
<notification type="jboss.snmp.agent.v2test"/>
<notification type="jboss.snmp.agent.v3test"/>
</mbean>
- -->
+
<!-- Valid JMX ObjectName patterns may be used as well, for example
<mbean name="*:service=invoker,*"/>
will subscribe to all matching mbeans for all notification types -->
Modified: branches/snmp4j-integration/varia/src/resources/services/snmp/deploy/notifications.xml
===================================================================
--- branches/snmp4j-integration/varia/src/resources/services/snmp/deploy/notifications.xml 2010-02-18 17:43:56 UTC (rev 101129)
+++ branches/snmp4j-integration/varia/src/resources/services/snmp/deploy/notifications.xml 2010-02-18 20:22:43 UTC (rev 101130)
@@ -64,7 +64,7 @@
</var-bind-list>
</mapping>
-<!-- Used for internal testing -ignore
+<!-- Used for internal testing -ignore -->
<mapping>
<notification-type>jboss.snmp.agent.v1test</notification-type>
<generic>10</generic>
@@ -115,6 +115,6 @@
</var-bind>
</var-bind-list>
</mapping>
--->
+
</notification-map-list>
More information about the jboss-cvs-commits
mailing list