[jboss-cvs] JBossAS SVN: r103843 - in branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp: test and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Apr 12 11:49:46 EDT 2010


Author: bshim
Date: 2010-04-12 11:49:46 -0400 (Mon, 12 Apr 2010)
New Revision: 103843

Added:
   branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/SnmpAgentRequestHandler.java
Modified:
   branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/ReadOnlyException.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/SnmpAgentService.java
   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/NotificationProducerServiceMBean.java
Log:
setting up adaptor to recieve, process and send responses for GET requests

Modified: branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/ReadOnlyException.java
===================================================================
--- branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/ReadOnlyException.java	2010-04-12 15:44:18 UTC (rev 103842)
+++ branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/ReadOnlyException.java	2010-04-12 15:49:46 UTC (rev 103843)
@@ -21,7 +21,7 @@
  */
 package org.jboss.jmx.adaptor.snmp.agent;
 
-import org.opennms.protocols.snmp.SnmpObjectId;
+import org.snmp4j.smi.OID;
 
 /**
  * This exception is thrown when an entry is read only and
@@ -37,7 +37,7 @@
       super("OID " + oid + " is read only");
    }
    
-   public ReadOnlyException(SnmpObjectId oid)
+   public ReadOnlyException(OID oid)
    {
 	   super("OID " + oid + " is read only");
    }

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-04-12 15:44:18 UTC (rev 103842)
+++ branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandlerImpl.java	2010-04-12 15:49:46 UTC (rev 103843)
@@ -44,8 +44,11 @@
 import org.jboss.xb.binding.UnmarshallerFactory;
 //import org.opennms.protocols.snmp.*;
 import org.snmp4j.PDU;
+import org.snmp4j.PDUv1;
+import org.snmp4j.ScopedPDU;
 import org.snmp4j.Snmp;
 import org.snmp4j.event.ResponseEvent;
+import org.snmp4j.mp.SnmpConstants;
 import org.snmp4j.smi.Counter32;
 import org.snmp4j.smi.Counter64;
 import org.snmp4j.smi.Integer32;
@@ -55,6 +58,8 @@
 import org.snmp4j.smi.TimeTicks;
 import org.snmp4j.smi.Variable;
 import org.snmp4j.smi.VariableBinding;
+import org.snmp4j.util.DefaultPDUFactory;
+import org.snmp4j.util.PDUFactory;
 
 /**
  * Implement RequestHandler with mapping of snmp get/set requests
@@ -156,10 +161,17 @@
 	 */
 	public PDU snmpReceivedGet(PDU pdu, boolean getNext)
 	{
+		PDU response;
 		//later on should be able to get rid of getNext because pdu.getType() should have it
+		if (pdu instanceof ScopedPDU){
+			response = DefaultPDUFactory.createPDU(SnmpConstants.version3);
+		} else if (pdu instanceof PDUv1){
+			response = DefaultPDUFactory.createPDU(SnmpConstants.version1);
+		} else {
+			response = new PDU();
+		} 
+		response.setType(PDU.RESPONSE);
 		
-		PDU response = new PDU();
-		response.setType(PDU.RESPONSE);
 		//Start of MINE
 		//response = (PDU)pdu.clone();
 		
@@ -630,7 +642,7 @@
 //	 * @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
+//	private SnmpSyntax setValueFor(final OID oid, final SnmpSyntax newVal) throws ReadOnlyException
 //   {
 //		final boolean trace = log.isTraceEnabled();
 //		
@@ -639,7 +651,6 @@
 //		if (trace)
 //			log.trace("setValueFor: found bind entry for " + oid);
 //		
-//		SnmpSyntax ssy = null;
 //		if (be != null)
 //      {
 //			if (trace)
@@ -653,19 +664,20 @@
 //				throw new ReadOnlyException(oid);
 //			}
 //			try
-//         {
+//         {		
+//				
 //				Object val = null;
-//				if (newVal instanceof SnmpOctetString)
+//				if (newVal instanceof OctetString)
 //            {
 //					val = newVal.toString();
 //				}
-//				else if (newVal instanceof SnmpInt32)
+//				else if (newVal instanceof Integer32)
 //            {
-//					val = new Integer(((SnmpInt32)newVal).getValue());
+//					val = new Integer(((Integer32)newVal).getValue());
 //				}
-//				else if (newVal instanceof SnmpUInt32)
+//				else if (newVal instanceof Counter32)
 //            {
-//					val = new Long(((SnmpUInt32)newVal).getValue());
+//					val = new Long(((Counter32)newVal).getValue());
 //				}
 //				// TODO do more mumbo jumbo for type casting / changing
 //				

Added: branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/SnmpAgentRequestHandler.java
===================================================================
--- branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/SnmpAgentRequestHandler.java	                        (rev 0)
+++ branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/SnmpAgentRequestHandler.java	2010-04-12 15:49:46 UTC (rev 103843)
@@ -0,0 +1,88 @@
+package org.jboss.jmx.adaptor.snmp.agent;
+
+import java.net.InetAddress;
+
+import org.snmp4j.PDU;
+import org.snmp4j.Snmp;
+import org.snmp4j.smi.OctetString;
+
+/**
+* <P>The SnmpAgentRequestHandler interface is implemented by an object that
+* wishs to receive callbacks when an SNMP protocol data unit
+* is received from a manager.</P>
+*
+*/
+public interface SnmpAgentRequestHandler
+{
+/**
+* <P>This method is defined to handle SNMP requests
+* that are received by the session. The parameters
+* allow the handler to determine the host, port, and
+* community string of the received PDU</P>
+*
+* @param session The SNMP session
+* @param manager The remote sender
+* @param port    The remote senders port
+* @param community  The community string
+* @param pdu     The SNMP pdu
+*
+*/
+void snmpReceivedPdu(Snmp session,
+                 InetAddress      manager,
+                 int              port,
+                 OctetString  community,
+                 PDU    pdu);
+
+/**
+* <P>This method is defined to handle SNMP Get requests
+* that are received by the session. The request has already
+* been validated by the system.  This routine will build a
+* response and pass it back to the caller.</P>
+*
+* @param pdu     The SNMP pdu
+* @param getNext The agent is requesting the lexically NEXT item after each
+*                    item in the pdu.
+*
+* @return PDU filled in with the proper response, or null if cannot process
+* NOTE: this might be changed to throw an exception.
+*/
+PDU snmpReceivedGet(PDU pdu, boolean getNext);
+
+/**
+* <P>This method is defined to handle SNMP Set requests
+* that are received by the session. The request has already
+* been validated by the system.  This routine will build a
+* response and pass it back to the caller.</P>
+*
+* @param pdu     The SNMP pdu
+*
+* @return PDU filled in with the proper response, or null if cannot process
+* NOTE: this might be changed to throw an exception.
+*/
+PDU snmpReceivedSet(PDU pdu);
+//ResponseEvent maybe
+
+///**
+//* <P>This method is invoked if an error occurs in 
+//* the session. The error code that represents
+//* the failure will be passed in the second parameter,
+//* 'error'. The error codes can be found in the class
+//* SnmpAgentSession class.</P>
+//*
+//* <P>If a particular PDU is part of the error condition
+//* it will be passed in the third parameter, 'pdu'. The
+//* pdu will be of the type SnmpPduRequest or SnmpPduTrap
+//* object. The handler should use the "instanceof" operator
+//* to determine which type the object is. Also, the object
+//* may be null if the error condition is not associated
+//* with a particular PDU.</P>
+//*
+//* @param session The SNMP Session
+//* @param error   The error condition value.
+//* @param ref     The PDU reference, or potentially null.
+//*                It may also be an exception.
+//*/
+//void SnmpAgentSessionError(Snmp session, 
+//                       int              error,
+//                       Object           ref);
+}
\ No newline at end of file

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-04-12 15:44:18 UTC (rev 103842)
+++ branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/SnmpAgentService.java	2010-04-12 15:49:46 UTC (rev 103843)
@@ -20,6 +20,7 @@
 package org.jboss.jmx.adaptor.snmp.agent;
 
 import java.net.InetAddress;
+import java.net.InetSocketAddress;
 import java.net.UnknownHostException;
 
 import javax.management.Notification;
@@ -32,9 +33,17 @@
 //import org.opennms.protocols.snmp.SnmpSMI;
 
 //SNMP4J IMPORTS
+import org.snmp4j.AbstractTarget;
 import org.snmp4j.CommandResponder;
 import org.snmp4j.CommandResponderEvent;
+import org.snmp4j.MessageDispatcher;
+import org.snmp4j.MessageException;
 import org.snmp4j.Snmp;
+import org.snmp4j.Target;
+import org.snmp4j.event.CounterEvent;
+import org.snmp4j.mp.SnmpConstants;
+import org.snmp4j.mp.StateReference;
+import org.snmp4j.mp.StatusInformation;
 import org.snmp4j.smi.*;
 import org.snmp4j.transport.DefaultTcpTransportMapping;
 import org.snmp4j.transport.DefaultUdpTransportMapping;
@@ -652,26 +661,99 @@
 	@Override
 	public void processPdu(CommandResponderEvent event) {
 		PDU pdu = event.getPDU();
-		
+
 		if (pdu == null)
 			return;
 		
 		int type = pdu.getType();
 		//switch based on pdu.getType() == ___
+		PDU response = null;
+		try{
+			switch (type) {
+			case PDU.GET:
+				response = requestHandler.snmpReceivedGet(pdu, false);
+//				Address address = event.getPeerAddress();
+//				Snmp snmp = new Snmp(new DefaultTcpTransportMapping((TcpAddress)address));
+				break;
+			case PDU.GETBULK:
+				//TODO maybe make a method called snmpReceivedGetBulk in requesthandler 
+				// requestHandler.snmpReceivedGet(pdu);
+				break;
+			case PDU.GETNEXT:
+				response = requestHandler.snmpReceivedGet(pdu, true);
+				break;
+			case PDU.SET:
+				response = requestHandler.snmpReceivedSet(pdu);
+				break;
+			default:
+				log.warn("Invalid PDU type: " + PDU.getTypeString(type));
+				return;
+			}
+			if (response != null) {
+				sendResponse(event, response);
+			}
+		} catch (Exception e){
+			log.debug("Snmp request failed: "+ PDU.getTypeString(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
+	}
+	
+	private void sendResponse(CommandResponderEvent requestEvent, PDU response) {
+		//MessageDispatcher disp = requestEvent.getMessageDispatcher();
+		try {
+			if (response.getBERLength() > requestEvent.getMaxSizeResponsePDU()) {
+				// response is tooBig
+				if (response.getType() != PDU.REPORT) {
+					if (requestEvent.getPDU().getType() == PDU.GETBULK) {
+						while ((response.size() > 0) &&
+						   (response.getBERLength() >
+						    requestEvent.getMaxSizeResponsePDU())) 
+						{
+							response.trim();
+						}
+					}
+					else {
+						response.clear();
+						response.setRequestID(requestEvent.getPDU().getRequestID());
+						response.setErrorStatus(PDU.tooBig);
+					}
+				}
+				if (response.getBERLength() > requestEvent.getMaxSizeResponsePDU()) {
+					//still too big and can't send a response
+					return;
+				}
+			}
+//---start of my addition -- Mar 29
+
+
+//--end of my addition
+
+			StatusInformation status = new StatusInformation();
+			StateReference stateRef = requestEvent.getStateReference();
+			if (stateRef == null) {
+				log.warn("No state reference available for requestEvent="+	
+			    requestEvent+". Cannot return response="+response);
+			}
+			else {
+				stateRef.setTransportMapping(requestEvent.getTransportMapping());
+				int resp = requestEvent.getMessageDispatcher().returnResponsePdu(requestEvent.getMessageProcessingModel(),
+				               requestEvent.getSecurityModel(),
+				               requestEvent.getSecurityName(),
+				               requestEvent.getSecurityLevel(),
+				               response,
+				               requestEvent.getMaxSizeResponsePDU(),
+				               //requestEvent.getStateReference(),
+				               stateRef,
+				               status);
+				log.info("Response was OK? : "+resp +" Constant: "+SnmpConstants.SNMP_MP_OK);
+				log.info("Returned response for requestID: " + 
+						requestEvent.getPDU().getRequestID().toString() + 
+						"to peer: "+requestEvent.getPeerAddress().toString());
+			}
 		}
+		catch (MessageException ex) {
+			log.error("Failed to send response to request "+requestEvent, ex);
+		}
 	}
 
 }

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-04-12 15:44:18 UTC (rev 103842)
+++ branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/test/NotificationProducerService.java	2010-04-12 15:49:46 UTC (rev 103843)
@@ -101,23 +101,32 @@
    
    /**
     * Sends a test GET request
+    * 
+    * @jmx:managed-operation
     */
    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")));
+	   pdu.add(new VariableBinding(new OID("1.2.3.4.1.3")));
 	   
 	   Target target = new CommunityTarget();
 	   try {
-		target.setAddress(new TcpAddress(InetAddress.getByName("127.0.0.1"), 1161));
+		target.setAddress(new TcpAddress(InetAddress.getByName("127.0.0.1"), 1161));//could be 1162
 		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);
-		
+
+		PDU responsePDU = responseEvent.getResponse();
+		if (responsePDU == null){
+			System.out.println("Request Timed out ...");
+		}
+		else {
+			System.out.println("RESPONSE RECEIVED!");
+			System.out.println("responsePdu = " + responsePDU);
+			
+		}
 	} catch (UnknownHostException e1) {
 		// TODO Auto-generated catch block
 		e1.printStackTrace();
@@ -130,6 +139,8 @@
    
    /**
     * Sends a test SET request
+    * 
+    * @jmx:managed-operation
     */
    public void set(){
 	   

Modified: branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/test/NotificationProducerServiceMBean.java
===================================================================
--- branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/test/NotificationProducerServiceMBean.java	2010-04-12 15:44:18 UTC (rev 103842)
+++ branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/test/NotificationProducerServiceMBean.java	2010-04-12 15:49:46 UTC (rev 103843)
@@ -40,5 +40,15 @@
    * Sends a test Notification of type "V3"
    */
  void sendV3() throws java.lang.Exception;
+ 
+ /**
+  * Sends a test SNMP GET
+  */
+ void get();
+ 
+ /**
+  * Sends a test SNMP SET
+  */
+ void set();
 
 }




More information about the jboss-cvs-commits mailing list