[jboss-cvs] JBossAS SVN: r111482 - in branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp: config/attribute and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon May 30 17:10:41 EDT 2011


Author: thauser at redhat.com
Date: 2011-05-30 17:10:41 -0400 (Mon, 30 May 2011)
New Revision: 111482

Modified:
   branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandler.java
   branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandlerImpl.java
   branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandlerSupport.java
   branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/SnmpAgentService.java
   branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/SnmpAgentServiceMBean.java
   branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/config/attribute/MIBGenerator.java
   branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/config/attribute/MappedAttribute.java
Log:
mib generator work started.

Modified: branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandler.java
===================================================================
--- branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandler.java	2011-05-30 17:03:51 UTC (rev 111481)
+++ branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandler.java	2011-05-30 21:10:41 UTC (rev 111482)
@@ -45,7 +45,7 @@
     * @param uptime the snmp agent uptime
     * @throws Exception in case of initialization problem
     */
-   public void initialize(String resourceName, MBeanServer server, Logger loggger, Clock uptime)
+   public void initialize(String resourceName, MBeanServer server, Logger loggger, Clock uptime, String mibResName)
       throws Exception;
 
 } // RequestHandler

Modified: branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandlerImpl.java
===================================================================
--- branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandlerImpl.java	2011-05-30 17:03:51 UTC (rev 111481)
+++ branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandlerImpl.java	2011-05-30 21:10:41 UTC (rev 111482)
@@ -23,6 +23,7 @@
 
 import java.io.InputStream;
 import java.net.InetAddress;
+import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
@@ -33,12 +34,16 @@
 import java.util.Vector;
 
 import javax.management.Attribute;
+import javax.management.MBeanAttributeInfo;
 import javax.management.MBeanServer;
+import javax.management.MBeanServerConnection;
+import javax.management.MBeanInfo;
 import javax.management.ObjectName;
 
 import org.jboss.jmx.adaptor.snmp.config.attribute.AttributeMappings;
 import org.jboss.jmx.adaptor.snmp.config.attribute.ManagedBean;
 import org.jboss.jmx.adaptor.snmp.config.attribute.MappedAttribute;
+import org.jboss.jmx.adaptor.snmp.config.attribute.MIBGenerator;
 import org.jboss.logging.Logger;
 import org.jboss.xb.binding.ObjectModelFactory;
 import org.jboss.xb.binding.Unmarshaller;
@@ -88,6 +93,11 @@
 
 	/** Has this RequestHandler instance been initialized? */
 	private boolean initialized = false;
+	
+	/** MIB Generator file name **/
+	private String mibResName = null;
+	
+	private MIBGenerator mibGenerator = null;
 
 
 	// Constructors --------------------------------------------------
@@ -100,6 +110,7 @@
 		bindings = new TreeMap();
 		oidKeys = new TreeSet();
 		objectKeys = new TreeSet();
+		mibGenerator = new MIBGenerator();
 	}
 
 	// RequestHandler Implementation ---------------------------------
@@ -112,11 +123,12 @@
 	 * @param log The logger we use
 	 * @param uptime The uptime of the snmp-agent subsystem.
 	 */
-	public void initialize(String resourceName, MBeanServer server, Logger log, Clock uptime)
+	public void initialize(String resourceName, MBeanServer server, Logger log, Clock uptime, String mibResName)
       throws Exception
    {
       log.debug("initialize() with res=" + resourceName);
-	   super.initialize(resourceName, server, log, uptime);
+	   this.mibResName = mibResName;
+	   super.initialize(resourceName, server, log, uptime, mibResName);
 		if (resourceName != null)
 			initializeBindings();
 		else
@@ -125,9 +137,7 @@
 		initialized = true;
 	}
 
-   // TODO: change all error handling to Exceptions. Much more extensible. 
-   //		this must be done because the use of return values could be much more useful if exceptions were thrown instead.
-   // TODO: getValueFor, setValueFor, and getNextOid all need to throw appropriate exceptions. 
+   //  
    // Reconfigurable Implementation ---------------------------------
    /**
     * Reconfigures the RequestHandler
@@ -593,6 +603,10 @@
    {
       log.debug("Reading resource: '" + resourceName + "'");
       
+      // arraylists for the MIB Generator, to query things like description and type
+      ArrayList<MBeanAttributeInfo[]> attrInfo = new ArrayList<MBeanAttributeInfo[]>();
+      ArrayList<MappedAttribute> maList = new ArrayList<MappedAttribute>();
+      
       ObjectModelFactory omf = new AttributeMappingsBinding();
       InputStream is = null;
       AttributeMappings mappings = null;
@@ -630,20 +644,24 @@
 		 * We have the MBeans now. Put them into the bindungs.
 		 */
 
-		Iterator it = mappings.iterator();
+	   Iterator it = mappings.iterator();
 		while (it.hasNext())
       {
 		   ManagedBean mmb = (ManagedBean)it.next();
 		   String oidPrefix = mmb.getOidPrefix();
 		   List attrs = mmb.getAttributes();
+		   //we need this info for the MIB Generator 
+		   MBeanInfo mbInfo = server.getMBeanInfo(new ObjectName(mmb.getName()));
+
+		   attrInfo.add(mbInfo.getAttributes());
+		   
 		   Iterator aIt = attrs.iterator();
 		   while (aIt.hasNext())
 		   {
 			  Object check = aIt.next();
- 
-			  
+ 			  
 			  MappedAttribute ma = (MappedAttribute)check;
-			  		  
+
 			  String oid;
 			  if (oidPrefix != null){
 				  oid = oidPrefix + ma.getOid();
@@ -654,14 +672,26 @@
 				  OID objectOID = new OID(oid);
       			  addObjectEntry(objectOID.trim());
 			  }
-			 
-			  addBindEntry(oid, mmb.getName(), ma.getName(),ma.isReadWrite());
-
-			  
+			  // for the MIB Generator
+			  maList.add(ma);
+			  addBindEntry(oid, mmb.getName(), ma.getName(),ma.isReadWrite());			  
   		   }
       }
+	//last step, to be indicated in choice somewhere. 
+    generateMib(mibResName, attrInfo, maList);
+		
    }
 	
+	/** This method starts the MIB generation process 
+	 * @param String name of the MIB File to generate
+	 * @param ArrayList<MBeanAttributeInfo[]> information about the attributes gathered during initialization
+	 * @param ArrayList<MappedAttribute> information about the attributes in the .xml, namely the desired OID value and r/w perms
+	 */
+	private void generateMib(String mibResName, ArrayList<MBeanAttributeInfo[]> attrList, ArrayList<MappedAttribute> maList){
+		this.mibGenerator = new MIBGenerator(mibResName, attrList, maList);
+		mibGenerator.writeMib();
+	}
+	
 	/** This method adds a new ObjectEntry to the set of Object OIDs.
 	 * @param String representation of the OID to add. 
 	 * 

Modified: branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandlerSupport.java
===================================================================
--- branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandlerSupport.java	2011-05-30 17:03:51 UTC (rev 111481)
+++ branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandlerSupport.java	2011-05-30 21:10:41 UTC (rev 111482)
@@ -70,7 +70,7 @@
    /**
     * Initialize
     */
-   public void initialize(String resourceName, MBeanServer server, Logger log, Clock uptime)
+   public void initialize(String resourceName, MBeanServer server, Logger log, Clock uptime, String mibResName)
       throws Exception
    {
       this.resourceName = resourceName;

Modified: branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/SnmpAgentService.java
===================================================================
--- branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/SnmpAgentService.java	2011-05-30 17:03:51 UTC (rev 111481)
+++ branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/SnmpAgentService.java	2011-05-30 21:10:41 UTC (rev 111482)
@@ -91,7 +91,7 @@
 
    /** Name of the file containing SNMP users */
    private String usersResName = null;
-   
+ 
    /** Name of the file containing SNMP manager specifications */
    private String managersResName = null;  
 
@@ -101,6 +101,9 @@
    /** Name of resource file containing get/set mappings */
    private String requestHandlerResName = null;
    
+   /** Name of MIB output file name */
+   private String mibResName = null;
+   
    /** Name of the trap factory class to be utilised */
    private String trapFactoryClassName = null;
 
@@ -161,7 +164,7 @@
    
    private MessageDispatcherImpl dispatcher;
    
-   // Constructors --------------------------------------------------
+    // Constructors --------------------------------------------------
    
    /**
     * Default CTOR
@@ -627,7 +630,7 @@
 	   this.requestHandler.initialize(this.requestHandlerResName, 
 			   				     this.getServer(), 
 			   				     this.log, 
-			   				     this.clock); 
+			   				     this.clock, this.mibResName); 
    }
    
    private void initTransportMapping(){
@@ -665,7 +668,7 @@
 	   ArrayList<User> userList = null;
 	   InputStream is = null;
 	   try {
-		   // locate managers.xml
+		   // locate users.xml
 		   final String resName = this.usersResName;
 		   is = SecurityActions.getThreadContextClassLoaderResource(resName);
          
@@ -696,7 +699,7 @@
     	  this.usm.addUser(usmUser.getSecurityName(), usm.getLocalEngineID(),usmUser);
       }
    }
-
+   
    //TODO: for v3 support we need to have this Snmp object have a USM (user security model) associated with it
    // as well as the appropriate MP. 
    // the new Snmp(TransportMapping) constructor already initializes a MessageDispatcherImpl with all MP's associated. 
@@ -727,7 +730,21 @@
       else
          return InetAddress.getByName(host);
    }
+	/**
+	 * @param usersResName the usersResName to set
+	 */
+	public void setMibResName(String mibResName) {
+		this.mibResName = mibResName;
+	}
+	
+	/**
+	 * @return the usersResName
+	 */
+	public String getMibResName() {
+		return mibResName;
+	}
 
+
 	/**
 	 * @param usersResName the usersResName to set
 	 */

Modified: branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/SnmpAgentServiceMBean.java
===================================================================
--- branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/SnmpAgentServiceMBean.java	2011-05-30 17:03:51 UTC (rev 111481)
+++ branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/SnmpAgentServiceMBean.java	2011-05-30 21:10:41 UTC (rev 111482)
@@ -48,6 +48,10 @@
    /** The name of the file containing SNMPv3 users */
    void setUsersResName(String usersResName);
    String getUsersResName();
+   
+   /** The name of the file containing the MIB Output file name */
+   void setMibResName(String mibResName);
+   String getMibResName();
 
    /** The name of the file containing the notification/trap mappings */
    void setNotificationMapResName(String notificationMapResName);

Modified: branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/config/attribute/MIBGenerator.java
===================================================================
--- branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/config/attribute/MIBGenerator.java	2011-05-30 17:03:51 UTC (rev 111481)
+++ branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/config/attribute/MIBGenerator.java	2011-05-30 21:10:41 UTC (rev 111482)
@@ -21,9 +21,15 @@
  */
 package org.jboss.jmx.adaptor.snmp.config.attribute;
 
+
+import java.io.*;
 import java.util.ArrayList;
+import java.util.Iterator;
 
+import org.jboss.jmx.adaptor.snmp.config.attribute.MappedAttribute;
+import javax.management.MBeanAttributeInfo;
 
+
 /** 
 * The goal of this class is to be able to, from information gathered from attributes.xml(s), 
 * generate an MIB that is able to be loaded by external managers.
@@ -32,44 +38,121 @@
 **/
 
 public class MIBGenerator {
-
-	String mibResName; // the name of the output MIB file
-	ArrayList<MappedAttribute>(10) maList; // a list of all mapped attributes algamated from all attributes.xml
+	private String mibResName; // the name of the output MIB file
+	private ArrayList<MBeanAttributeInfo> attrInfo;
+	private ArrayList<MappedAttribute> maList; // a list of all mapped attributes algamated from all attributes.xml
+	private ArrayList<MIBObject> miboList; //internal list of MIBObjects
 	
+	public MIBGenerator(){
+		mibResName = null;
+		this.attrInfo = null;
+		this.maList = null;
+		this.miboList = null;
+	}
 	
+	public MIBGenerator(String mibResName, ArrayList<MBeanAttributeInfo[]> attrInfo, ArrayList<MappedAttribute> maList){
+		ArrayList<MBeanAttributeInfo[]> tempAttrInfo = attrInfo;
+		this.mibResName = mibResName;
+		this.maList = maList;
+		this.attrInfo = processMBeanInfo(tempAttrInfo); //modify attrInfo to be a large 1-d array of MBeanAttributeInfo objects
+		addDescriptions(); // insert the proper description into each MappedAttribute so we can create MIBObjects from them
+	}
+	
 	//mutators
 	public String getMibResName(){
 		return this.mibResName;
 	}
 	
-	public void setMibRedName(String mrn){
+	public void setMibResName(String mrn){
 		this.mibResName = mrn;
-	}	
-	//adds a given attribute to the list, so that the generated MIB will contain it.
-	public void addAttribute(MappedAttribute ma){
-		maList.add(ma);
 	}
 	
+	public ArrayList<MappedAttribute> getMaList(){
+		return this.maList;
+	}
+	
+	public void setMaList(ArrayList<MappedAttribute> maList){
+		this.maList = maList;
+	}
+	
+	public ArrayList<MBeanAttributeInfo> getAttrInfo(){
+		return this.attrInfo;
+	}
+	
+	public void setAttrInfo(ArrayList<MBeanAttributeInfo> attrInfo){
+		this.attrInfo = attrInfo;
+	}
+	
 	//we're done gathering attributes. write the MIB.
 	public void writeMib(){
-		
+		try{
+		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("mibResName")));
+		writeHeader(out);
+		writeDefinitions(miboList,out);
+		out.println("END");
+		out.close();
+		}
+		catch (Exception e){
+			System.out.println("herpderp!");
+		}
 	}
 	
-	public void writeHeader(){
-		new PrintWriter(new BufferedWriter(new FileWriter("mibRedName")));
-		PrintWriter.
+	private void addDescriptions(){
+		Iterator<MappedAttribute> maIt = this.maList.iterator();
+		while (maIt.hasNext()){
+			MappedAttribute tempMa = maIt.next();
+			Iterator<MBeanAttributeInfo> mbaIt = attrInfo.iterator();
+			while (mbaIt.hasNext()){
+				MBeanAttributeInfo tempMba = mbaIt.next();
+				if (tempMba.getName() == tempMa.getName()){
+					tempMa.setDesc(tempMba.getDescription());
+				}
+			}
+		}	
 	}
 	
-	public void writeImports(){
-		
+	private ArrayList<MBeanAttributeInfo> processMBeanInfo(ArrayList<MBeanAttributeInfo[]> attrInfo){
+		ArrayList<MBeanAttributeInfo> retval = new ArrayList<MBeanAttributeInfo>(10);
+		Iterator<MBeanAttributeInfo[]> mbaIt = attrInfo.iterator();
+		while (mbaIt.hasNext()){
+			MBeanAttributeInfo[] temp = mbaIt.next();
+			for (int i=0;i<temp.length;i++){
+				retval.add(temp[i]);//add all entries of all attributes to the list.
+			}
+		}
+		//done adding all MBeanAttributeInfos
+		return retval;
 	}
 	
+	private void writeHeader(PrintWriter out){
+		out.println("-- This MIB Generated by the SNMP MIB Generator");
+		out.println();
+		out.println("JBOSS-AS-MIB DEFINITIONS ::=BEGIN");
+		// maybe add more here, if not we don't need this to be a seperate method.
+	}
 	
+	private void writeDefinitions(ArrayList<MIBObject>miboList, PrintWriter out){
+		createMIBObjects(this.maList);
+		Iterator<MIBObject> aIt = miboList.iterator();
+		while (aIt.hasNext()){
+			out.println(aIt.next());
+		}
+	}
 	
-	
+	private void createMIBObjects(ArrayList<MappedAttribute> maList){
+		Iterator<MappedAttribute> aIt = maList.iterator();
+		while (aIt.hasNext()){
+			MIBObject mibO = new MIBObject(aIt.next());
+			miboList.add(mibO);
+		}		
+	}
 	/* Internal Classes ----- */
 	
-	private class MIBElement{
+	private class MIBNotification{
+		MIBNotification(){} // this will model a NOTIFICATION-TYPE for the generated MIB
+	}
+	
+	private class MIBObject{
 		private String name;
 		private String syntax;
 		private String maxAccess;
@@ -81,9 +164,10 @@
 		//or just make a new class to hold this info in, instead of an internal one.
 		//we need to get the information out of the MappedAttribute and format it 
 		//correctly for use in an MIB.
-		MIBElement(MappedAttribute ma){
-			this.name = ma.getName().substring(ma.lastIndexOf('='), ma.length());
+		MIBObject(MappedAttribute ma){
+			this.name = ma.getName().substring(ma.getName().lastIndexOf('='), ma.getName().length());
 			this.syntax = "STRING"; // definitely need a better way to find this out.
+			// maybe need to parse notifications.xml to find out trap objects
 		
 			if (ma.isReadWrite()) // since we only have "rw" and "ro", this needs expansion.
 				maxAccess = "read-write";
@@ -91,24 +175,31 @@
 				maxAccess = "read-only";
 			
 			this.status = "mandatory"; // again, need expansions to attributes.xml to account for this
-			this.description = "TEST"; // see above.
+			this.description = ma.getDesc(); // see above.
 			this.objectId = ma.getOid(); // this will contain the full numerical OID.
 			
 		}
 	
 		public String toString(){
-			return this.name + " OBJECT-TYPE\n" 
-			     + "\tSYNTAX "+ syntax +"\n"
-			     + "\tACCESS "+ maxAccess +"\n"
-			     + "\tSTATUS "+ status +"\n"
-			     + "\tDESCRIPTION "+ "\n\t\t" 
-			     + description + "\n"
-			     + "::= { "+ checkOIDReplacement +" "
-			     + objectId + " }"+"\n"; 
+			StringBuffer buf = new StringBuffer();
+			buf.append(this.name+" OBJECT-TYPE\n");
+			buf.append("\tSYNTAX ").append(this.syntax);
+			buf.append("\n");
+			buf.append("\tACCESS ").append(this.maxAccess);
+			buf.append("\n");
+			buf.append("\tSTATUS ").append(this.status);
+			buf.append("\n");
+			buf.append("\tDESCRIPTION ");
+			buf.append("\n\t\t");
+			buf.append(this.description);
+			buf.append("\n");
+			buf.append("::= {").append(" ");
+			buf.append(objectId + " }");
+			return buf.toString();
 		}
 	
 	
 	
-}
+	}//end MIBObject
 
-}
\ No newline at end of file
+}// end MIB Generator
\ No newline at end of file

Modified: branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/config/attribute/MappedAttribute.java
===================================================================
--- branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/config/attribute/MappedAttribute.java	2011-05-30 17:03:51 UTC (rev 111481)
+++ branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/config/attribute/MappedAttribute.java	2011-05-30 21:10:41 UTC (rev 111482)
@@ -22,7 +22,7 @@
 package org.jboss.jmx.adaptor.snmp.config.attribute;
 
 /**
- * An attribute mapping, by default readonly.
+ * An attribute mapping, by default readonly, and a blank description
  * 
  * @author <a href="mailto:hwr at pilhuhn.de">Heiko W. Rupp</a>
  * @version $Revision$
@@ -32,6 +32,7 @@
 	private String name;
 	private String oid;
 	private boolean isReadWrite = false;
+	private String desc = "";
 	
 	/** Attribute name */
 	public String getName()
@@ -54,7 +55,15 @@
    {
 		this.oid = oid;
 	}
+	
+	public String getDesc(){
+		return desc;
+	}
 
+	public void setDesc(String desc){
+		this.desc = desc;
+	}
+	
    /** Attribute mode (ro/rw) */
    public boolean isReadWrite()
    {



More information about the jboss-cvs-commits mailing list