www.pudn.com > sxg.rar > ContextSetRequest.java


/*
 * Copyright (c) 2003 Jens Mueller
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/**
	Data class representig set-requests on a single context / SNMP-agent.
 	@author Jens Mueller
 */

import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Vector;
import java.util.Iterator;

public class ContextSetRequest
{
	private String _ipaddr;
	private InetAddress _inetAddress;
	private int _port;
	private String _commString;
	/**
	 	contains all GwComplexType this request is made of 	 */
	public Vector _gwComplexTypeVector;
	String debugstr;

	public ContextSetRequest( String ipaddr, InetAddress inetAddress, int port, String commString )
	{
		_ipaddr 		= ipaddr;
		_inetAddress	= inetAddress;
		_port 			= port;
		_commString 	= commString;
		_gwComplexTypeVector = new Vector();
		debugstr = "

performed requests:
"; } public String getIPaddr() { return _ipaddr; } public int getPort() { return _port; } public InetAddress getInetAddress() { return _inetAddress; } /** executes the request by performing SNMP-communication to the agent. @throws RequestExecutionException */ public void execute() throws RequestExecutionException { int[] timeout = new int[ 1 ]; timeout[ 0 ] = 5000; String retval = ""; SnmpHandler snmpHandler = new SnmpHandler( "Agent", _inetAddress, _port, timeout, _commString ); try { snmpHandler.connect(); } catch( SocketException e ) { throw new RequestExecutionException( "Unable to connect to Agent:" + " Could not create Socket"); } GwComplexType currComplexType; debugstr +="execute: "; for( Iterator iterator = _gwComplexTypeVector.iterator(); iterator.hasNext(); ) { debugstr +="iterating cp: "; currComplexType = ( GwComplexType )iterator.next(); if( currComplexType.isTable() ) { debugstr +="table "; String res = setTable( currComplexType, snmpHandler ); if( res != "" && retval == "" ) retval = res; } else { debugstr +="scalargroup "; String res = setScalarGroup( currComplexType, snmpHandler ); if( res != "" && retval == "" ) retval = res; } //delete cached content CacheHandler.removeFromCache( currComplexType, this ); } if( retval != "" ) throw new RequestExecutionException( "Error(s) during execution, not all submitted" + " data has been set. Last error was: "+retval ); } /** performs SNMP-communication to modify values in a SNMP-table. First it searches the accordant tablerow by iterating the table using getnext. The row ist found if all XML-attributes match their aquivalent SNMP-row-objects. If found, this method performs SNMP-set-requests using the via getnext resolved OBJECT IDENTIFIERS. Only for SNMP-Objects allowed to be modificated a SNMP-set-request is performed. @param complexType the table-type to be set. @param snmpHandler the hanlder used for SNMP-communication @return an empty String if all data has been set sucessfully or a message describing the occured error. */ private String setTable( GwComplexType complexType, SnmpHandler snmpHandler ) { //check if all required attributes are set for( Iterator iterator = complexType._typeAttributes.iterator(); iterator.hasNext(); ) { GwElement currAttr = ( GwElement )iterator.next(); if( currAttr.getUse() != null && currAttr.getUse().equals( "required" ) && currAttr.getSnmpSetData() == null ) return "not all required attributes set"; } //find tablerow via getnext boolean found = false; try { while( ! found ) { GwElement typeElement; // request elements for ( Iterator typeIter = complexType._typeElements.iterator(); typeIter.hasNext(); ) { typeElement = ( GwElement )typeIter.next(); typeElement.setExecutionResult( snmpHandler.getNextRequest( typeElement.getOid().toString() ) ); typeElement.setOid( typeElement.getExecutionResult().getOid() ); } // request attributes for ( Iterator typeIter = complexType._typeAttributes.iterator(); typeIter.hasNext(); ) { typeElement = ( GwElement )typeIter.next(); typeElement.setExecutionResult( snmpHandler.getNextRequest( typeElement.getOid().toString() ) ); typeElement.setOid( typeElement.getExecutionResult().getOid() ); } //check if all requested attribute-values match their set-value found = true; for ( Iterator typeIter = complexType._typeAttributes.iterator(); typeIter.hasNext(); ) { typeElement = ( GwElement )typeIter.next(); if( typeElement.getUse() != null && typeElement.getUse().equals( "required" ) && ! typeElement.getEncodedExecutionResult().equals( typeElement.getSnmpSetData() ) ) { found = false; break; } } // last request ran over table, searched row not found if ( !complexType.allTypeOidsPrefixed() ) break; } } catch( SnmpAccessException e ) { // table not traversable, skip this complextype return "Could not iterate table"; } String errorStr = ""; if( found ) { GwElement typeElement; // set elements for ( Iterator typeIter = complexType._typeElements.iterator(); typeIter.hasNext(); ) { typeElement = ( GwElement )typeIter.next(); if( typeElement.getSnmpSetData() != null && ( typeElement.getMaxAccess() != null && ( typeElement.getMaxAccess().equals("read-write") || typeElement.getMaxAccess().equals("read-create") ) ) ) { String data = ""; try { debugstr += "Decoding of "+typeElement.getSnmpSetData()+" ..."; data = typeElement.getDecodedExecutionString(); String castType = typeElement.getCastType(); if( castType.equals( "list" ) ) { errorStr = "setting of list-types is currently not provided"; } else { debugstr += "setRequest( "+typeElement.getOid().toString() + ", "+data+", "+typeElement.getCastType()+" )
"; snmpHandler.setRequest( typeElement.getOid().toString(), data, castType ); } } catch( IllegalArgumentException e ) { errorStr = e.getMessage(); } catch( SnmpAccessException e ) { errorStr = "Could not set value \""+data+"\" for \""+typeElement.getName()+"\" "; } } } // set attributes for ( Iterator typeIter = complexType._typeAttributes.iterator(); typeIter.hasNext(); ) { typeElement = ( GwElement )typeIter.next(); if( typeElement.getSnmpSetData() != null && ( typeElement.getMaxAccess() != null && ( typeElement.getMaxAccess().equals("read-write") || typeElement.getMaxAccess().equals("read-create") ) ) ) { String data = ""; try { debugstr += "Decoding of "+typeElement.getSnmpSetData()+" ..."; data = typeElement.getDecodedExecutionString(); String castType = typeElement.getCastType(); if( castType.equals( "list" ) ) { errorStr = "setting of list-types is currently not provided"; } else { debugstr += "setRequest( "+typeElement.getOid().toString() + ", "+data+", "+typeElement.getCastType()+" )
"; snmpHandler.setRequest( typeElement.getOid().toString(), data, castType ); } } catch( IllegalArgumentException e ) { errorStr = e.getMessage(); } catch( SnmpAccessException e ) { errorStr = "Could not set value \""+data+"\" for \""+typeElement.getName()+"\" "; } } } return errorStr; } else return( complexType.getName()+" with specified attributes not found " ); } /** performs SNMP-communication to modify values in a SNMP-scalargroup. Only for SNMP-Objects allowed to be modificated a SNMP-set-request is performed. @param complexType the scalargroup-type to be set. @param snmpHandler the hanlder used for SNMP-communication @return an empty String if all data has been set sucessfully or a message describing the occured error. */ private String setScalarGroup( GwComplexType complexType, SnmpHandler snmpHandler ) { GwElement currElement; String errorStr = ""; for( Iterator iterator = complexType._typeElements.iterator(); iterator.hasNext(); ) { currElement = ( GwElement )iterator.next(); debugstr +="testing "; //only process objects that have data and are allowed to set if( currElement.getSnmpSetData() != null && ( currElement.getMaxAccess() != null && ( currElement.getMaxAccess().equals("read-write") || currElement.getMaxAccess().equals("read-create") ) ) ) { debugstr += "Decoding of "+currElement.getSnmpSetData()+" ..."; String data = ""; try { data = currElement.getDecodedExecutionString(); String castType = currElement.getCastType(); if( castType.equals( "list" ) ) { errorStr = "setting of list-types is currently not provided"; } else { debugstr += "setRequest( "+currElement.getOid().toString()+".0" + ", "+data+", "+currElement.getCastType()+" )
"; snmpHandler.setRequest( currElement.getOid().toString()+".0", data, castType ); } } catch( IllegalArgumentException e ) { errorStr = e.getMessage(); } catch( SnmpAccessException e ) { errorStr = "Could not set value \""+data+"\" for \""+currElement.getName()+"\" "; } } } return errorStr; } /** writes textual representation of this request to writer for debugging */ public void dump( PrintWriter writer ) { writer.println( "
Context: "+_inetAddress.getHostName()+"
" ); for( Iterator iterator = _gwComplexTypeVector.iterator(); iterator.hasNext(); ) { GwComplexType currComplexType = ( GwComplexType )iterator.next(); writer.println( "  "+currComplexType.getName()+"
" ); for( Iterator it = currComplexType._typeElements.iterator(); it.hasNext(); ) { String data; GwElement currElement = ( GwElement )it.next(); if( currElement.getSnmpSetData() == null ) data = "null"; else data = currElement.getSnmpSetData(); writer.println( "    "+currElement.getName() + " ( "+currElement.getMaxAccess()+" ) : "+data+"
" ); } } writer.println( "
"+debugstr ); } }