[jboss-cvs] JBossAS SVN: r111548 - in branches/snmp4j-int/varia: src/assembly and 2 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Jun 7 15:27:27 EDT 2011


Author: thauser at redhat.com
Date: 2011-06-07 15:27:27 -0400 (Tue, 07 Jun 2011)
New Revision: 111548

Added:
   branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/
   branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/Generator.java
   branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/MIBGenerator.java
   branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/Parser.java
   branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/ParserBindings.java
Modified:
   branches/snmp4j-int/varia/pom.xml
   branches/snmp4j-int/varia/src/assembly/snmp-adaptor.xml
Log:
First Commits with working MIB Generator. Much room for improvement.


Modified: branches/snmp4j-int/varia/pom.xml
===================================================================
--- branches/snmp4j-int/varia/pom.xml	2011-06-07 19:18:19 UTC (rev 111547)
+++ branches/snmp4j-int/varia/pom.xml	2011-06-07 19:27:27 UTC (rev 111548)
@@ -87,6 +87,23 @@
               </descriptors>
             </configuration>
           </execution>
+          <execution>
+           <id>MIBGenerator</id>
+            <goals>
+             <goal>single</goal>
+            </goals>
+           <phase>package</phase>
+	   <configuration>
+	    <descriptors>
+	     <descriptor>src/assembly/mib-generator.xml</descriptor>
+	    </descriptors>
+	    <archive>
+	     <manifest>
+	      <mainClass>org.jboss.jmx.adaptor.snmp.generator.MIBGenerator</mainClass>
+	     </manifest>
+            </archive>
+           </configuration>
+          </execution> 
         </executions>
       </plugin>
       <plugin>

Modified: branches/snmp4j-int/varia/src/assembly/snmp-adaptor.xml
===================================================================
--- branches/snmp4j-int/varia/src/assembly/snmp-adaptor.xml	2011-06-07 19:18:19 UTC (rev 111547)
+++ branches/snmp4j-int/varia/src/assembly/snmp-adaptor.xml	2011-06-07 19:27:27 UTC (rev 111548)
@@ -11,6 +11,9 @@
       <includes>
         <include>org/jboss/jmx/adaptor/snmp/**</include>
       </includes>
+      <excludes>
+        <exclude>org/jboss/jmx/adaptor/snmp/generator/**</exclude>
+      </excludes>
     </fileSet>
   </fileSets>
-</assembly>
\ No newline at end of file
+</assembly>

Copied: branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/Generator.java (from rev 111505, 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/generator/Generator.java	                        (rev 0)
+++ branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/Generator.java	2011-06-07 19:27:27 UTC (rev 111548)
@@ -0,0 +1,382 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.jmx.adaptor.snmp.generator;
+
+
+import java.io.*;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.Set;
+import java.util.HashMap;
+
+import org.jboss.jmx.adaptor.snmp.config.attribute.MappedAttribute;
+import org.jboss.jmx.adaptor.snmp.config.attribute.AttributeMappings;
+import org.jboss.jmx.adaptor.snmp.config.attribute.ManagedBean;
+
+import javax.management.ObjectName;
+
+import org.snmp4j.smi.TimeTicks;
+import org.snmp4j.smi.Counter32;
+import org.snmp4j.smi.OID;
+
+
+/** 
+* This class will be able to generate MIBs or XMLs, depending on the invocation by the user. 
+* It will utilize mibble libraries to parse MIBs to generate xmls from them, and will use 
+* objects gathered from the parsing of XMLs to generate MIBs. Types are determined by either an
+* attribute in the attributes.xml (snmp-type), or, if missing, a check on the attribute's type after 
+* querying the server. 
+* 
+* TODO:
+* 1) Parse the XML itself, given input files. (Done in Parser.java)
+* 2) Create an entrypoint into the program and create a JAR during build of JBPAPP_5_1
+* 3) Enable user to define the names of OIDs themselves somehow.
+* 
+* 
+*  @author <a href="mailto:tom.hauser at gmail.com">Tom Hauser</a>
+**/
+
+public class Generator {
+	private String outputResName; // the name of the output file (.mib or .xml)
+	private AttributeMappings mbList;
+	private ArrayList<MIBObject> miboList; //internal list of MIBObjects
+	private ArrayList<MappedAttribute> maList;
+	private HashMap<String, OIDDef> oidDefMap;
+	
+	// need some way to prevent collisions
+		
+	
+	public Generator(){
+		this.outputResName = null;
+		this.mbList = null;
+		this.miboList = null;
+		this.maList = null;
+	}
+	
+	public Generator(String outputResName, ArrayList<MappedAttribute> maList, AttributeMappings mbList){
+		this.outputResName = outputResName;
+		this.mbList = mbList;
+		this.miboList = new ArrayList<MIBObject>();
+		this.maList = maList;
+		this.oidDefMap = new HashMap<String, OIDDef>();
+		
+	}
+	
+	//mutators
+	public String getOutputResName(){
+		return this.outputResName;
+	}
+	
+	public void setOutputResName(String outputResName){
+		this.outputResName = outputResName;
+	}
+	
+	public ArrayList<MappedAttribute> getMaList(){
+		return this.maList;
+	}
+	
+	public void setMaList(ArrayList<MappedAttribute> maList){
+		this.maList = maList;
+	}
+	
+	public AttributeMappings getMbList(){
+		return this.mbList;
+	}
+	
+	public void setMbList(AttributeMappings mbList){
+		this.mbList = mbList;
+	}
+		
+/*	public MBeanServer getMBeanServer(){
+		return this.server;
+	}
+	
+	public void setMBeanServer(MBeanServer server){
+		this.server = server;
+	}*/
+	
+	public HashMap<String, OIDDef> getOidDefMap(){
+		return this.oidDefMap;
+	}
+	
+	public void setOidDefMap(HashMap<String, OIDDef> oidDefMap){
+		this.oidDefMap = oidDefMap;
+	}
+	//we're done gathering attributes. write the MIB.
+	public void writeFile(){
+		try{
+			PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(outputResName)));
+			if (outputResName.contains(".mib")){
+				createOidDefinitions();
+				createMibObjects();
+				writeMibHeader(out);
+				writeMibImports(out);
+				writeMibDefinitions(out);
+				writeMibObjects(out);
+			}
+			else if (outputResName.contains(".xml")){
+				writeXmlHeader(out);
+				writeXmlDefinitions(out);
+			}
+			out.close();
+		}
+		catch (Exception e){e.printStackTrace();}
+	}
+	
+	
+	private void writeMibHeader(PrintWriter out){
+		out.println("-- This MIB Generated by the JBoss MIB Generator");
+		out.println();
+		out.println("JBOSS-AS-MIB DEFINITIONS ::=BEGIN");
+		out.println();
+		// maybe add more here, if notrrayList we don't need this to be a seperate method.
+	}
+	
+	private void writeMibImports(PrintWriter out){
+		out.println("IMPORTS");
+		out.println("\tOBJECT-TYPE,");
+		out.println("\tNOTIFICATION-TYPE,");
+		out.println("\tCounter32,");
+		out.println("\tGauge32,");
+		out.println("\tCounter64,");
+		out.println("\tTimeTicks");
+		out.println("\t\tFROM SNMPv2-SMI");
+		out.println("\tDisplayString,");
+		out.println("\tTruthValue");
+		out.println("\t\tFROM SNMPv2-TC;");	
+		out.println();
+	}
+	
+	/** This method is used to write the definitions of our SYNTAX inside the JBoss MIB
+	 *  For use when we figure out what OIDs we want our metrics to be. 
+	 * @param attrList list of MBeans that we're interested in
+	 * @param out The PrintWriter that writes to the file we need it to
+	 * @author Tom Hauser
+	 */	
+	private void writeMibDefinitions(PrintWriter out){
+		/*org             OBJECT IDENTIFIER ::= { iso 3 }
+		dod             OBJECT IDENTIFIER ::= { org 6 }
+		internet        OBJECT IDENTIFIER ::= { dod 1 }
+
+		private         OBJECT IDENTIFIER ::= { internet 4 }
+		enterprise      OBJECT IDENTIFIER ::= { private 1 }
+		redhat          OBJECT IDENTIFIER ::= { enterprise 2312 }
+		jboss           OBJECT IDENTIFIER ::= { redhat 100 }
+		as              OBJECT IDENTIFIER ::= { jboss 1 }*/
+		Set<String> oidKeySet = oidDefMap.keySet();
+		Iterator<String> oidIt = oidKeySet.iterator();
+		while (oidIt.hasNext()){
+			out.println(oidDefMap.get(oidIt.next()));
+		}
+	}
+	
+	private void writeMibObjects(PrintWriter out){
+		Iterator<MIBObject> aIt = miboList.iterator();
+		while (aIt.hasNext()){
+			out.println(aIt.next());
+		}
+		out.println("END");
+	}
+	
+	private void createMibObjects(){
+		Iterator<MappedAttribute> aIt = maList.iterator();
+		while (aIt.hasNext()){
+			MIBObject mibO = new MIBObject(aIt.next());
+			miboList.add(mibO);
+		}
+	}		
+	
+	private void createOidDefinitions(){		
+		// expansion: make a way to discover these, rather than hardcoding them. 
+		// suggestion: add a map of oid-prefixes from the attributes.xml and create OIDDefs out of those.
+		// then how do we get names? :( 
+		// create ones we know about first
+		String as = "1.3.6.1.4.1.2312.100.1";
+		String system = "1.3.6.1.2.1.1";
+		OIDDef jbossas = new OIDDef("as", as);
+		OIDDef mib2system = new OIDDef("system", system);
+		this.oidDefMap.put(as, jbossas);
+		this.oidDefMap.put(system, mib2system);
+	}
+	
+	private void writeXmlHeader(PrintWriter out){}
+	private void writeXmlDefinitions(PrintWriter out){}
+	/* Internal Classes ----- */
+	
+	private class MIBObject{
+		private String name;
+		private String syntax;
+		private String maxAccess;
+		private String status;
+		private String description;
+		private String objectId; 
+		private String oidDef;
+				
+		//need to modify the information saved in mapped attribute maybe.
+		//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.
+			
+		MIBObject(MappedAttribute ma){
+			this.name = ma.getName().substring(0,1).toLowerCase() + ma.getName().substring(1);
+			
+			if (ma.getSnmpType()!= null)
+				this.syntax = ma.getSnmpType();
+			else
+				this.syntax = "OCTET STRING (SIZE(0..255))";
+					
+			if (ma.isReadWrite()) // since we only have "rw" and "ro", this needs expansion.
+				maxAccess = "read-write";
+			else
+				maxAccess = "read-only";
+			
+			this.status = "current"; 
+			this.description = ""; 
+			String[] temp = ma.getOid().split("\\."); // this will contain the full numerical OID.
+			this.objectId = temp[temp.length-1];
+			
+			if (oidDefMap.containsKey(ma.getOidPrefix())){
+				this.oidDef = oidDefMap.get(ma.getOidPrefix()).getName();
+			}
+			// if they aren't part of the Map, then we add them to the map with a filler name "CHANGE-ME"
+			// as there is no way to glean this name from the attributes.xml without 
+			// complexifying it a great deal
+			else{
+				OIDDef newOidDef = new OIDDef("CHANGE-ME", ma.getOidPrefix());
+				oidDefMap.put(ma.getOidPrefix(), newOidDef);
+			}
+		}
+		
+/*		public String findType(MappedAttribute ma){
+			Object o = null;
+			try{
+			o = server.getAttribute(new ObjectName(ma.getMbean()), ma.getName());
+			}catch (Exception e){return "OCTET STRING (SIZE(0..255))";}				
+
+			if (o instanceof Long){
+				return "DisplayString";
+			}
+			else if (o instanceof String){
+				return "DisplayString";
+			}
+			else if (o instanceof Integer){
+				return "INTEGER";
+			}
+			else if (o instanceof OID){
+				return "OBJECT IDENTIFIER";
+			}
+			else if (o instanceof TimeTicks){
+				return "TimeTicks";
+			}
+			else if (o instanceof Counter32){
+				return "Counter32";
+			}
+			else{
+				return "OCTET STRING (SIZE(0..255))";
+			}
+		}*/
+
+		@Override
+		public String toString(){
+			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(this.oidDef + " " + this.objectId  + " }");
+			buf.append("\n");
+			return buf.toString();
+		}
+	}
+	
+		private class OIDDef{
+			private String name;
+			private String definition;
+			private String rawOid;
+			
+			public OIDDef(String name, String oid){
+				this.name = name;
+				this.rawOid = oid;
+				this.definition = parseDefinition(oid);
+			}
+			
+			public String getName(){
+				return this.name;	
+			}
+			
+			public void setName(String name){
+				this.name = name;
+			}
+			
+			public String getDefinition(){
+				return this.definition;
+			}
+			 
+			public void setDefinition(String def){
+				this.definition = def;
+			}
+			
+			public String parseDefinition(String oid){
+				String[] tokens = oid.split("\\.");
+				String temp = "{ ";
+				for (int i = 0; i < tokens.length;i++){
+					temp += tokens[i]+" ";
+				}
+				temp += "}";
+				return temp;
+			}
+			
+			@Override
+			public String toString(){
+				StringBuffer buf = new StringBuffer();
+				buf.append(this.name);
+				buf.append("\t\tOBJECT IDENTIFIER ::= ");
+				buf.append(this.definition);
+				return buf.toString();
+			}
+			
+			@Override
+			public boolean equals(Object other){
+			    if (other == null) return false;
+			    if (other == this) return true;
+			    if (this.getClass() != other.getClass())return false;
+			    OIDDef otherDef = (OIDDef)other;
+			    if(otherDef.rawOid == this.rawOid)
+			    	return true;
+			    else
+			    	return false;
+			}
+
+
+		}//end OIDDef
+	
+
+
+}// end MIB Generator
\ No newline at end of file

Added: branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/MIBGenerator.java
===================================================================
--- branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/MIBGenerator.java	                        (rev 0)
+++ branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/MIBGenerator.java	2011-06-07 19:27:27 UTC (rev 111548)
@@ -0,0 +1,61 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+//package  org.jboss.jmx.adaptor.snmp.generator;
+
+/**
+ * This class serves as the entry point for MIB Generation. It recieves input from the CLI, 
+ * parses the xml and creates an appropriate Generator object to actually write the MIB.
+ * For now, only xml > mib conversions are supported.
+ * 
+ * TODO: Add switch for MIB to XML conversions, using Mibble
+ * @author thauser
+ *
+ */
+
+package org.jboss.jmx.adaptor.snmp.generator;
+
+public class MIBGenerator {
+	static String outputFileName; // the name of the output file (*.mib or *.xml)
+	static String inputFileName; // input filename (*.mib or *.xml)
+	static Parser parser; // XMLParser instantiation that parses the xml
+	static Generator generator; // generator that outputs the mib or xml
+	
+	/**
+	 *
+	 * Get the filenames from the command line. Parse the input file, whether it be an mib or an xml. 
+	 * Create the needed objects for either one, and create a new generator object with the information.
+	 * Write the file using the generator. 
+	 * TODO: add checks for the input arguments
+	 * 
+	 * @param args
+	 * @author Tom Hauser
+	 */
+	public static void main (String [] args){
+		 inputFileName = args[0];
+		 outputFileName = args[1];
+		 parser = new Parser(inputFileName);
+		 parser.parse();
+		 generator = new Generator(outputFileName, parser.getMaList(), parser.getMbList());
+		 generator.writeFile();		
+	}
+	
+}
\ No newline at end of file

Added: branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/Parser.java
===================================================================
--- branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/Parser.java	                        (rev 0)
+++ branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/Parser.java	2011-06-07 19:27:27 UTC (rev 111548)
@@ -0,0 +1,179 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package  org.jboss.jmx.adaptor.snmp.generator;
+
+import java.io.InputStream;
+import java.io.FileInputStream;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+
+import org.jboss.jmx.adaptor.snmp.config.attribute.MappedAttribute;
+import org.jboss.jmx.adaptor.snmp.config.attribute.AttributeMappings;
+import org.jboss.jmx.adaptor.snmp.config.attribute.ManagedBean;
+import org.jboss.jmx.adaptor.snmp.generator.ParserBindings;
+
+import org.jboss.xb.binding.ObjectModelFactory;
+import org.jboss.xb.binding.Unmarshaller;
+import org.jboss.xb.binding.UnmarshallerFactory;
+
+/**
+ * This class is used by the MIBGenerator to get the initial list of MappedAttributes out of an input file. 
+ * 
+ * @author thauser
+ *
+ */
+
+public class Parser {
+	String inputFile;
+	ArrayList<MappedAttribute> maList; 
+	AttributeMappings mbList; 
+	
+	public Parser(String inputFile){
+		this.inputFile = inputFile;
+		maList = new ArrayList<MappedAttribute>(10);
+		mbList = new AttributeMappings();
+	}
+	 
+	public String getInputFile(){
+		return this.inputFile;
+	}
+	
+	public void setInputFile(String inputFile){
+		this.inputFile = inputFile;		
+	}
+	
+	public ArrayList<MappedAttribute> getMaList(){
+		return this.maList;
+	}
+	
+	public void setMaList(ArrayList<MappedAttribute> maList){
+		this.maList = maList;
+	}
+	
+	public AttributeMappings getMbList(){
+		return this.mbList;
+	}
+	
+	public void setMbList(AttributeMappings mbList){
+		this.mbList = mbList;
+	}
+
+	/**
+	 * Workhorse method. Based on the name of the input / output file, we either parse
+	 * an MIB or an XML, and from this construct a nescessary ArrayList of ManagedAttributes to write out	 
+	 * */
+	
+	public void parse(){
+		// TODO: refine the checks here. they are not enough 
+		if (inputFile.contains(".mib")){
+			parseMib();
+		}
+		else if (inputFile.contains(".xml")){
+			try {
+			parseXml();
+			}
+			catch (Exception e){
+				System.out.println("Exception in parseXml:");
+				e.printStackTrace();
+				System.exit(1);				
+			}
+		}		
+	}
+	
+	private void parseXml() throws Exception{
+		ObjectModelFactory omf = new ParserBindings();
+		InputStream is = null;
+		AttributeMappings mappings = null;
+		try{
+		   // locate resource
+		   is = new FileInputStream(this.inputFile);
+		   
+		   // create unmarshaller
+		   Unmarshaller unmarshaller = UnmarshallerFactory.newInstance().newUnmarshaller();
+
+		   // let JBossXB do it's magic using the AttributeMappingsBinding
+		   mappings = (AttributeMappings)unmarshaller.unmarshal(is, omf, null);         
+		}
+		catch (Exception e){
+		   throw e;
+		}
+		finally{
+		   if (is != null){
+			   try{
+			   is.close();
+			   }
+			   catch (Exception e){
+				   throw e;
+			   }
+		   }
+		}
+		if (mappings == null){
+		   return;         
+		}
+			/**
+			 * We have the MBeans now. Put them into the bindungs.
+			 */
+
+		 Iterator it = mappings.iterator();
+			while (it.hasNext()){
+			   ManagedBean mmb = (ManagedBean)it.next();
+			   String oidPrefix = mmb.getOidPrefix();
+			   List attrs = mmb.getAttributes();
+			   Iterator aIt = attrs.iterator();
+			   while (aIt.hasNext()){
+				  Object check = aIt.next();
+				  
+				  MappedAttribute ma = (MappedAttribute)check;
+
+				  if (oidPrefix != null){
+					  ma.setOidPrefix(oidPrefix);
+				  }
+				  else{
+					  ma.setOidPrefix(removeLast(ma.getOid()));
+				  }
+				  // for the MIB Generator
+				  ma.setMbean(mmb.getName());
+				  maList.add(ma);	  
+		   }
+		}
+	}//end parseXml
+	
+	/**
+	 * This private method removes the last element of a dotted string like
+	 * 1.3.6.1.2.1.1, and returns it.
+	 */
+	private String removeLast(String oid){
+		String [] split = oid.split("\\.");
+		String retVal = "";
+		for (int i = 0; i < split.length-1; i++){
+			if (i == split.length-2)
+				retVal+=split[i];
+			else
+				retVal+=split[i]+".";
+		}
+		return retVal;		
+	}
+	
+	private void parseMib(){}
+}// end Parser
\ No newline at end of file

Added: branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/ParserBindings.java
===================================================================
--- branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/ParserBindings.java	                        (rev 0)
+++ branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/ParserBindings.java	2011-06-07 19:27:27 UTC (rev 111548)
@@ -0,0 +1,130 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.jmx.adaptor.snmp.generator;
+
+import java.util.ArrayList;
+
+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.xb.binding.ObjectModelFactory;
+import org.jboss.xb.binding.UnmarshallingContext;
+import org.xml.sax.Attributes;
+
+/**
+ * Parse the mapping of JMX mbean attributes to SNMP OIDs
+ * 
+ * TODO: extend this parsing. 
+ * 		-Add "table" element. This is not represented by any MBean attribute but is still recorded by the system,
+ *       as a a way to conceptually organize the desired exposed JMX metrics. This element would have the oid-prefix
+ *       attribute, which would replace the mbean oid-prefix 
+ *      -Remove the ability for there to be an oid-prefix for the MBeans. instead, but this into the "table" element.
+ *       the reason we should do this is because SNMP works with "Objects" and "Instances". The way it is currently
+ *       parsed makes it seem like each MBean is a table, and we do not want to force this behavior.
+ *      
+ *      New scheme for parsing: 
+ *      <mbean name=...> (MBean we're interested in.)
+ *         <attribute name=... oid="1.2.3.4.1.0"/> (This attribute is a scalar, because it has no prefix. it's Object is 1.2.3.4.1 and the instance is 0) 
+ *         ..
+ *         <table name=... oid-prefix="1.2.3.4.1.8"/>  (This indicates the creation of a conceptual table.)
+ *         	 <row name=... oid=".1"/> (This is a row in the table) neither this nor the table are directly accessible in SNMP.
+ *            <attribute name=... oid=".1"/> (this is an actual instance of the row (a single column in the row.) 
+ *            								 This attribute is accessed by 1.2.3.4.1.8.1.1 <tableOID><rowOID><instanceOID>
+ *          ..
+ *      </mbean>
+ * 
+ * @author <a href="mailto:hwr at pilhuhn.de">Heiko W. Rupp</a>
+ * @version $Revision: 111505 $
+ */
+public class ParserBindings implements ObjectModelFactory
+{
+	
+	public Object newRoot(Object root, UnmarshallingContext ctx,
+			String namespaceURI, String localName, Attributes attrs)
+   {
+	   if (!localName.equals("attribute-mappings"))
+      {
+	      throw new IllegalStateException("Unexpected root " + localName + ". Expected <attribute-mappings>");
+		}
+	   return new AttributeMappings();
+	}
+
+	public Object completeRoot(Object root, UnmarshallingContext ctx, String uri, String name)
+   {
+	   return root;
+	}
+
+	public void setValue(AttributeMappings mappings, UnmarshallingContext navigator,
+		      String namespaceUri, String localName, String value)
+	{
+	}	
+	
+	public Object newChild(AttributeMappings mappings, UnmarshallingContext navigator,
+			String namespaceUri, String localName, Attributes attrs)
+	{
+		if ("mbean".equals(localName))
+      {
+			String name = attrs.getValue("name");
+			String oidPrefix = attrs.getValue("oid-prefix");
+			ManagedBean child = new ManagedBean();
+			child.setName(name);
+			child.setOidPrefix(oidPrefix);
+			return child;
+		}
+		return null;
+	}
+	
+	public void addChild(AttributeMappings mappings, ManagedBean mbean,
+			UnmarshallingContext navigator, String namespaceURI, String localName) 
+	{
+		mappings.addMonitoredMBean(mbean);
+	}
+	
+	public Object newChild(ManagedBean mbean, UnmarshallingContext navigator,
+			String namespaceUri, String localName, Attributes attrs)
+	{
+		
+		MappedAttribute attribute = null;
+		if ("attribute".equals(localName)) {
+			String oid = attrs.getValue("oid");
+			String name = attrs.getValue("name");
+			String mode = attrs.getValue("mode");
+			String snmpType = attrs.getValue("snmp-type");
+			attribute = new MappedAttribute();
+			if ("rw".equalsIgnoreCase(mode))
+            attribute.setReadWrite(true);
+			attribute.setName(name);
+			attribute.setOid(oid);
+			attribute.setSnmpType(snmpType);
+		}
+		return attribute;
+	}
+	
+	public void addChild(ManagedBean mbean, MappedAttribute attribute,
+			UnmarshallingContext navigator, String namespaceURI, String localName)
+	{
+		if (mbean.getAttributes() == null)
+         mbean.setAttributes(new ArrayList());
+		
+		mbean.getAttributes().add(attribute);
+	}
+}



More information about the jboss-cvs-commits mailing list