[jboss-cvs] JBossAS SVN: r110426 - branches/snmp4j-integration-1.11.1/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Fri Jan 21 15:57:30 EST 2011
Author: thauser at redhat.com
Date: 2011-01-21 15:57:29 -0500 (Fri, 21 Jan 2011)
New Revision: 110426
Added:
branches/snmp4j-integration-1.11.1/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/NoSuchInstanceException.java
branches/snmp4j-integration-1.11.1/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/NoSuchObjectException.java
Modified:
branches/snmp4j-integration-1.11.1/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandlerImpl.java
Log:
RFC-1905 Protocol matching.
Added: branches/snmp4j-integration-1.11.1/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/NoSuchInstanceException.java
===================================================================
--- branches/snmp4j-integration-1.11.1/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/NoSuchInstanceException.java (rev 0)
+++ branches/snmp4j-integration-1.11.1/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/NoSuchInstanceException.java 2011-01-21 20:57:29 UTC (rev 110426)
@@ -0,0 +1,20 @@
+package org.jboss.jmx.adaptor.snmp.agent;
+
+import org.snmp4j.smi.OID;
+
+/** This exception is thrown when a manager attempts to retrieve an instance of an object
+ * that does not exist.
+ *
+ * @author Thomas Hauser <a href="mailto:thauser at redhat.com"></a>
+ */
+
+public class NoSuchInstanceException extends Exception {
+
+ public NoSuchInstanceException(){
+ super("Error: Requested an Instance of an Object that does not exist.");
+ }
+
+ public NoSuchInstanceException(OID oid){
+ super("Error: The following Instance does not exist: "+oid);
+ }
+}
Copied: branches/snmp4j-integration-1.11.1/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/NoSuchObjectException.java (from rev 110415, branches/snmp4j-integration-1.11.1/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/NoSuchOidException.java)
===================================================================
--- branches/snmp4j-integration-1.11.1/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/NoSuchObjectException.java (rev 0)
+++ branches/snmp4j-integration-1.11.1/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/NoSuchObjectException.java 2011-01-21 20:57:29 UTC (rev 110426)
@@ -0,0 +1,20 @@
+package org.jboss.jmx.adaptor.snmp.agent;
+
+import org.snmp4j.smi.OID;
+
+/** This exception is thrown when a manager attempts to retrieve information
+ * about an OID that does not exist in the attributes.xml
+ *
+ * @author Thomas Hauser <a href="mailto:thauser at redhat.com"></a>
+ */
+
+public class NoSuchObjectException extends Exception {
+
+ public NoSuchObjectException(){
+ super("Error: Requested an OID that does not exist.");
+ }
+
+ public NoSuchObjectException(OID oid){
+ super("Error: The following OID does not exist: "+oid);
+ }
+}
Modified: branches/snmp4j-integration-1.11.1/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandlerImpl.java
===================================================================
--- branches/snmp4j-integration-1.11.1/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandlerImpl.java 2011-01-21 20:23:28 UTC (rev 110425)
+++ branches/snmp4j-integration-1.11.1/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandlerImpl.java 2011-01-21 20:57:29 UTC (rev 110426)
@@ -160,6 +160,8 @@
* The first nonRepeaters elements in the VariableBinding list are retrieved with a simple GET snmp call.
* The remaining (size - nonRepeaters) elements in the VariableBinding list have maxRepetitions GETNEXT calls
* upon them. All of these VariableBindings are added into the response.
+ * The resultant response will have N + (M*R) VariableBindings
+ *
* </P>
* @param pdu
* contains the following:
@@ -188,6 +190,7 @@
response = new PDU();
}
response.setType(PDU.RESPONSE);
+ response.setRequestID(pdu.getRequestID());
final boolean trace = log.isTraceEnabled();
@@ -196,77 +199,83 @@
+ pdu.size());
}
if (pdu != null){
+ int counter = 1;
int nonRepeaters = pdu.getNonRepeaters();
+ if (nonRepeaters<0)
+ nonRepeaters=0;
+
int maxRepetitions = pdu.getMaxRepetitions();
+ if (maxRepetitions<0)
+ maxRepetitions=0;
Vector<VariableBinding> it = pdu.getVariableBindings();
VariableBinding vb = null;
Variable var = null;
if (it.size() == 0){ // we were given an empty list in the PDU
log.debug("snmpReceivedGetBulk: No VariableBindings in received PDU");
- makeErrorPdu(new OID(""), SnmpConstants.SNMP_ERROR_GENERAL_ERROR, 0, response);
+ makeErrorPdu(response, pdu, counter);
return response;
}
- // for the first nonRepeater Bindings, we simply get their value. if nonRepeaters is 0,
- // we do a GET on every element, and ignore the next loop.
+
for (int i=0; i < Math.min(nonRepeaters, it.size());i++){
vb = (VariableBinding)it.get(i);
OID oid = vb.getOid();
+ OID noid = null;
+
+ try {
+ noid = getNextOid(oid);
+ }
+ catch (EndOfMibViewException e){
+ log.debug("snmpReceivedGetBulk: End of MIB View in NonRepeaters.");
+ var = Null.endOfMibView;
+ response.add(new VariableBinding(oid, var));
+ continue;
+ }
try {
- var = getValueFor(oid);
+ var = getValueFor(noid);
}
- catch (NoSuchOidException e) {
+ catch (NoSuchInstanceException e) {
// getBulk allows Null values and errors in it's return.
- log.debug("snmpReceivedGetBulk: An OID didn't exist. Null substituted in it's place.");
- response.add(new VariableBinding(oid, Null.noSuchObject));
+ // this won't happen because of using getnext;
+ log.debug("snmpReceivedGetBulk: An instance of an OID didn't exist.");
+ response.add(new VariableBinding(oid, Null.noSuchInstance));
continue;
}
catch (VariableTypeException e){
- log.debug("snmpReceivedGetBulk: Couldn't convert a Variable to a correct type. adding a null value instead.");
- response.add(new VariableBinding(oid, Null.noSuchInstance));
- continue;
+ log.debug("snmpReceivedGetBulk: Couldn't convert a Variable to a correct type.");
+ makeErrorPdu(response, pdu, counter);
+ return response;
}
- response.add(new VariableBinding(oid,var));
+ response.add(new VariableBinding(noid,var));
+ counter++;
}
// for the remaining it.size() bindings, we perform maxRepetitions successive getNext calls
for (int i = nonRepeaters; i < it.size(); i++){
vb = (VariableBinding)it.get(i);
+ OID oid = vb.getOid();
OID noid = null;
try {
- noid = getNextOid(vb.getOid());
+ noid = getNextOid(oid);
}
- catch (NoSuchOidException e) {
- // if this OID didn't exist, the GETNEXT surely does not exist. try the next one.
- log.debug("snmpReceivedGetBulk: An OID didn't exist. Null value given.");
- response.add(new VariableBinding(vb.getOid(), Null.noSuchObject));
- continue;
- }
+
catch (EndOfMibViewException e) {
- //not an error here. we skip this one and go to the next Repeater.
+ // we know that doing more getnext in this case will just give more end of MIB view results. so we try the next VB.
+ response.add(new VariableBinding(oid, Null.endOfMibView));
continue;
}
//FIXME: returns null when maxReptitions goes into OIDs that don't exist.
for (int j = 0; j < maxRepetitions; j++){
- /* if (noid == null){ // reached the end of the repitions.
- log.debug("snmpReceivedGetBulk: Attempt to GETBULK on non-existent non-scalar OID.");
- continue;
- response.setErrorStatus(PDU.noSuchName);
- response.setErrorIndex(i+1);
- response.add(new VariableBinding(new OID(), Null.noSuchObject));
- // TODO: should we fail out here?
- return response;
- }*/
try {
var = getValueFor(noid);
}
- catch (NoSuchOidException e) {
- log.debug("snmpReceivedGetBulk: An OID didn't exist. Null substituted in it's place.");
+ // since we're using getNext to retrieve the OIDs, this can't happen. we'll always have an instance.
+ catch (NoSuchInstanceException e) {
var = Null.noSuchObject;
}
catch (VariableTypeException e) {
- log.debug("snmpReceivedGetBulk: Couldn't convert a Variable to a correct type. adding a null value instead.");
+ makeErrorPdu(response, pdu, counter);
var = Null.noSuchInstance;
}
@@ -275,18 +284,13 @@
try {
noid = getNextOid(noid);
}
- catch (NoSuchOidException e) {
- // error. break out and try the next one
- log.debug("snmpReceivedGetBulk: An OID didn't exist. Null substituted in it's place.");
- break;
- }
catch (EndOfMibViewException e) {
- // not an error. we're finished with the repeaters here.
+ response.add(new VariableBinding(noid, Null.endOfMibView));
break;
}
}
}
-
+ counter++;
}
return response;
}
@@ -310,6 +314,8 @@
PDU response;
// this counts the number of VariableBindings and indicates which one caused a problem, if any
int counter = 1;
+ // flags the end of the MIB
+ boolean endOfMib = false;
if (pdu instanceof ScopedPDU){
response = DefaultPDUFactory.createPDU(SnmpConstants.version3);
@@ -319,6 +325,8 @@
response = new PDU();
}
response.setType(PDU.RESPONSE);
+ //indicate by copying the Req ID that this response is for the PDU sent with that ReqID.
+ response.setRequestID(pdu.getRequestID());
final boolean trace = log.isTraceEnabled();
@@ -343,59 +351,60 @@
try{
noid = getNextOid(oid);
}
- catch(NoSuchOidException e){
- log.debug("snmpReceivedGet: GETNEXT operation couldn't find given oid.");
- makeErrorPdu(noid,PDU.noSuchName,counter,response);
- return response;
- }
// if this OID is the last one possible, we throw this exception.
catch(EndOfMibViewException e){
- log.debug("snmpReceivedGet: GETNEXT operation left the OID subtree");
- makeErrorPdu(oid,PDU.noAccess, counter,response);
- return response;
+ log.debug("snmpReceivedGet: GETNEXT operation left the MIB View");
+ endOfMib=true;
+ //makeErrorPdu(oid,PDU.noAccess, counter,response);
+
}
-/* if (noid == null){
- // this reaction should be factored out.
- log.debug("snmpReceivedGet: GETNEXT operation returned null. No such OID.");
- response.setErrorStatus(PDU.noSuchName);
- response.setErrorIndex(counter);
- response.add(new VariableBinding(noid, Null.noSuchObject));
- return response;
- }*/
newVB = new VariableBinding(noid);
- try {
- var = getValueFor(noid);
+ if (endOfMib)
+ var = Null.endOfMibView;
+ else{
+ try {
+ var = getValueFor(noid);
+ }
+ catch (NoSuchInstanceException e) {
+ log.debug("snmpReceivedGet: GETNEXT operation returned null. No such OID.");
+ var = Null.noSuchInstance;
+ //makeErrorPdu(noid, PDU.noSuchName, counter,response);
+
+ }
+ catch (VariableTypeException e) {
+ log.debug("snmpReceivedGet: GETNEXT operation could not convert the returned value for " + noid + " into an appropriate type.");
+ //makeErrorPdu(noid,PDU.badValue, counter,response);
+ return response;
+
+ }
}
- catch (NoSuchOidException e) {
- log.debug("snmpReceivedGet: GETNEXT operation returned null. No such OID.");
- makeErrorPdu(noid, PDU.noSuchName, counter,response);
- return response;
- }
- catch (VariableTypeException e) {
- log.debug("snmpReceivedGet: GETNEXT operation could not convert the returned value for " + noid + " into an appropriate type.");
- makeErrorPdu(noid,PDU.badValue, counter,response);
- return response;
-
- }
-
-
}
else {
newVB = new VariableBinding(oid);
-
- try {
- var = getValueFor(oid);
+ var = null;
+ if (checkObject(oid)){
+ try {
+ var = getValueFor(oid);
+ }
+ catch (NoSuchInstanceException e) {
+ log.debug("snmpReceivedGet: GET operation returned null. No such Instance.");
+ var = Null.noSuchInstance;
+ //makeErrorPdu(oid, PDU.noSuchName, counter, response);
+ }
+ catch (VariableTypeException e) {
+ log.debug("snmpReceivedGet: GET operation could not convert the returned value for " + oid + " into an appropriate type.");
+ makeErrorPdu(response, pdu, counter);
+ return response;
+
+
+ }
+
}
- catch (NoSuchOidException e) {
- log.debug("snmpReceivedGet: GET operation returned null. No such OID.");
- makeErrorPdu(oid, PDU.noSuchName, counter, response);
- return response;
+ // if we get here, there's no such object.
+ else{
+ var = Null.noSuchObject;
}
- catch (VariableTypeException e) {
- log.debug("snmpReceivedGet: GET operation could not convert the returned value for " + oid + " into an appropriate type.");
- makeErrorPdu(oid, PDU.badValue, counter,response);
- return response;
- }
+
}
newVB.setVariable(var);
response.add(newVB);
@@ -403,6 +412,7 @@
}
}
+ //TODO: check size constraints of the sender
return response;
}
@@ -466,22 +476,22 @@
modified.add(new VariableBinding(oid, oldVar)); // keep a record of the old variable binding.
var = setValueFor(oid,newVal);
}
- catch (NoSuchOidException e){
+ catch (NoSuchInstanceException e){
log.debug("snmpReceivedSet: attempt to set a non-existent OID " + oid);
undoSets(modified);
- makeErrorPdu(oid, PDU.noSuchName,counter,response);
+ makeErrorPdu(response, pdu, counter);
return response;
}
catch (VariableTypeException e){
log.debug("snmpReceievedSet: could not convert the given value into an appropriate type: " +newVal);
undoSets(modified);
- makeErrorPdu(oid, PDU.badValue, counter, response);
+ makeErrorPdu(response, pdu, counter);
return response;
}
catch (ReadOnlyException e){
log.debug("snmpReceivedSet: attempt to set a read-only attribute: " + newVB);
undoSets(modified);
- makeErrorPdu(oid, PDU.readOnly, counter, response);
+ makeErrorPdu(response, pdu, counter);
return response;
}
@@ -737,14 +747,32 @@
oidKeys.add(coid);
}
+
/**
+ * This method checks the existence of an object of a GET / SET request.
+ * This is as opposed to a specific instance, which has a value tied to it. this method simply
+ * returns a boolean.
+ *
+ * @param oid
+ * @return
+ * @throws NoSuchObjectException
+ * @throws VariableTypeException
+ */
+
+ private boolean checkObject(final OID oid) {
+ OID coid = oid;
+ coid.removeLast();
+ return oidKeys.contains(coid);
+ }
+
+ /**
* Return the current value for the given oid
*
* @param oid
* The oid we want a value for
* @return Null if no value present
*/
- private Variable getValueFor(final OID oid) throws NoSuchOidException, VariableTypeException {
+ private Variable getValueFor(final OID oid) throws NoSuchInstanceException, VariableTypeException {
BindEntry be = findBindEntryForOid(oid);
Variable ssy = null;
@@ -763,7 +791,7 @@
}
}
catch (VariableTypeException e){
- log.debug("setValueFor: didn't find a suitable data type for the requested data");
+ log.debug("getValueFor: didn't find a suitable data type for the requested data");
throw e;
}
catch (Exception e)
@@ -776,7 +804,7 @@
{
log.debug("getValueFor: " + NO_ENTRY_FOUND_FOR_OID + oid);
- throw new NoSuchOidException();
+ throw new NoSuchInstanceException();
}
return ssy;
}
@@ -881,7 +909,7 @@
//TODO: decide what to do in the case of a non writeable entry. (Exception thrown)
private Variable setValueFor(final OID oid, final Variable newVal)
- throws ReadOnlyException, VariableTypeException, NoSuchOidException {
+ throws ReadOnlyException, VariableTypeException, NoSuchInstanceException {
final boolean trace = log.isTraceEnabled();
BindEntry be = findBindEntryForOid(oid);
@@ -930,7 +958,7 @@
}
else
{
- throw new NoSuchOidException();
+ throw new NoSuchInstanceException();
/*ssy = newVal;
log.info("setValueFor: " + NO_ENTRY_FOUND_FOR_OID + oid + " on the mbean server");*/
}
@@ -953,7 +981,7 @@
Variable var = vb.getVariable();
setValueFor(oid,var);// this will not fail, because it succeeded earlier.
}
- catch(NoSuchOidException e){
+ catch(NoSuchInstanceException e){
//impossible
}
catch(VariableTypeException e){
@@ -1012,18 +1040,14 @@
* Return the next oid that is larger than ours.
* @param oid the starting oid
* @return the next oid or null if none found.
- * @throws NoSuchOidException if request is given an OID that doesn't exist in the first place
- * @throws EndOfMibViewException if there is no greater OID than the given, valid, oid.
+ * * @throws EndOfMibViewException if there is no greater OID than the given, valid, oid.
*/
- private OID getNextOid(final OID oid) throws NoSuchOidException, EndOfMibViewException {
+ private OID getNextOid(final OID oid) throws EndOfMibViewException {
OID coid = new OID(oid);
- if (coid.last() == 0)
- coid.removeLast();
-
SortedSet ret;
- ret= oidKeys.tailSet(oid); // get oids >= oid
+ ret=oidKeys.tailSet(oid); // get oids >= oid
Iterator it = ret.iterator();
OID roid=null;
@@ -1038,12 +1062,11 @@
roid = (OID)it.next(); // oid
}
- //catch (NoSuchElementException e){
- //log.debug("getNextOid: Placeholder. The OID did not exist");
- else{
- log.debug("getNextOid: Placeholder. The OID did not exist");
- throw new NoSuchOidException();
- }
+ // if the tailSet is empty...there aren't any OID larger than the one given. so we're at the End of the MIB.
+ else{
+ log.debug("getNextOid: Placeholder. There is no lexically larger OID than the input.");
+ throw new EndOfMibViewException();
+ }
if (roid.compareTo(coid)==0) // input elment
{
@@ -1083,44 +1106,12 @@
* @param errNo The error number defined in the PDU class that indicates a given failure
* @param errInd the VariableBinding in the PDU that caused the error.
*/
- private void makeErrorPdu(OID oid, int errNo, int errInd, PDU response){
- OID retOID;
- response.clear(); // prep PDU for being an error PDU
- if (oid == null){
- retOID=new OID("");
- }
- else{
- retOID = oid;
- }
- response.setErrorStatus(errNo);
- response.setErrorIndex(errInd);
- switch (errNo){
- case PDU.noError:
- break;
- case PDU.noSuchName:
- response.add(new VariableBinding(retOID, Null.noSuchObject));
- break;
- case PDU.badValue:
- case PDU.wrongValue:
- response.add(new VariableBinding(retOID, Null.noSuchInstance));
- break;
- case PDU.noAccess:
- response.add(new VariableBinding(retOID, Null.endOfMibView));
- case PDU.readOnly:
- case PDU.tooBig:
- case PDU.genErr:
- case PDU.wrongType:
- case PDU.wrongLength:
- case PDU.wrongEncoding:
- case PDU.resourceUnavailable:
- case PDU.commitFailed:
- case PDU.undoFailed:
- case PDU.authorizationError:
- case PDU.notWritable:
- case PDU.inconsistentName:
- response.add(new VariableBinding(retOID, Null.instance));
- break;
- }
+ private void makeErrorPdu(PDU response, PDU pdu, int counter){
+ response.clear();
+ response.setRequestID(pdu.getRequestID());
+ response.addAll(pdu.toArray());
+ response.setErrorIndex(counter);
+ response.setErrorStatus(PDU.genErr);
}
// Inner Class ---------------------------------------------------
More information about the jboss-cvs-commits
mailing list