[SNMP4J] Can you suggest me how to make a Table in which we can add rows dynamically?

Réda BENKIRANE reda.benkirane at hotmail.fr
Mon Dec 7 10:52:16 CET 2015


Hi,
I am working on an SNMP4J agent and I want this agent to be able to create a conceptual table. This conceptual table can have 0 rows or as much rows as I need. I want to add rows and fill those rows with values not from the agent but from a SNMP manager (client) at runtime (i.e. : The Manager sends a SET request to the agent and as a consequence of the set, a new row is added).
I managed to find a code with a class that contains methods to create, initialize and build an MOTable with snmp4j package. However, I don't know how to add rows to this table.
Can you suggest me how can I modify the following code ? Or what do I need to add in the agent or in the SNMP4J manager to do what I want ?
Here is the MOTableBuilder.java class : 
import java.util.ArrayList;import java.util.List;
import org.snmp4j.agent.MOAccess;import org.snmp4j.agent.mo.DefaultMOMutableRow2PC;import org.snmp4j.agent.mo.DefaultMOTable;import org.snmp4j.agent.mo.MOColumn;import org.snmp4j.agent.mo.MOMutableTableModel;import org.snmp4j.agent.mo.MOTable;import org.snmp4j.agent.mo.MOTableIndex;import org.snmp4j.agent.mo.MOTableSubIndex;import org.snmp4j.smi.OID;import org.snmp4j.smi.SMIConstants;import org.snmp4j.smi.Variable;

/** * <p>Utility class for adding dynamic data into an {@link MOTable}</p> *  *  <pre><code> MOTableBuilder builder = new MOTableBuilder(new OID(".1.3.6.1.2.1.2.2.1"))	.addColumnType(SMIConstants.SYNTAX_INTEGER,MOAccessImpl.ACCESS_READ_ONLY)	.addColumnType(SMIConstants.SYNTAX_OCTET_STRING,MOAccessImpl.ACCESS_READ_ONLY);	for(MyObject o: myObjects) {			builder.addRowValue(new Integer32(o.getId()))		.addRowValue(new OctetString(o.getName()));	}MOTable table = builder.build(); </code><pre>  * @author johanrask * */public class MOTableBuilder {
	private MOTableSubIndex[] subIndexes = new MOTableSubIndex[] { new MOTableSubIndex(			SMIConstants.SYNTAX_INTEGER) };	private MOTableIndex indexDef = new MOTableIndex(subIndexes, false);
	private final List<MOColumn> columns = new ArrayList<MOColumn>();	private final List<Variable[]> tableRows = new ArrayList<Variable[]>();	private int currentRow = 0;	private int currentCol = 0;
	private OID tableRootOid;
	//private int colTypeCnt = 0;
		/**	 * Specified oid is the root oid of this table	 */	public MOTableBuilder(OID oid) {		this.tableRootOid = oid;	}
	/**	 * Adds all column types {@link MOColumn} to this table.	 * Important to understand that you must add all types here before	 * adding any row values	 * 	 * @param syntax use {@link SMIConstants}	 * @param access	 * @return	 */	@SuppressWarnings("rawtypes")	public MOTableBuilder addColumnType(int ID, int syntax, MOAccess access) {		//colTypeCnt++;		columns.add(new MOColumn(ID, syntax, access));		return this;	}
		public MOTableBuilder addRowValue(Variable variable) {		if (tableRows.size() == currentRow) {			tableRows.add(new Variable[columns.size()]);		}		tableRows.get(currentRow)[currentCol] = variable;		currentCol++;		if (currentCol >= columns.size()) {			currentRow++;			currentCol = 0;		}		return this;	}
	public MOTable build() {		DefaultMOTable ifTable = new DefaultMOTable(tableRootOid, indexDef,				columns.toArray(new MOColumn[0]));		MOMutableTableModel model = (MOMutableTableModel) ifTable.getModel();		int i = 1;				for (Variable[] variables : tableRows) {			model.addRow(new DefaultMOMutableRow2PC(new OID(String.valueOf(i)),					variables));			i++;		}		ifTable.setVolatile(true);		return ifTable;	}}

Réda BENKIRANE 		 	   		  


More information about the SNMP4J mailing list