Обработчик активации сервисов по snmp, зависящий от статуса договора
Материал из BiTel WiKi
Версия от 07:18, 8 ноября 2012; Stark (Обсуждение | вклад)
Код обработчика
package ru.maglan.bgbilling.modules.inet.dyn.device.switchDlink; import java.net.InetSocketAddress; import java.sql.Connection; import java.util.Date; import java.util.List; import org.apache.log4j.Logger; import ru.bitel.bgbilling.kernel.network.snmp.SnmpClient; import ru.bitel.bgbilling.modules.inet.access.sa.ServiceActivator; import ru.bitel.bgbilling.modules.inet.access.sa.ServiceActivatorAdapter; import ru.bitel.bgbilling.modules.inet.access.sa.ServiceActivatorEvent; import ru.bitel.bgbilling.modules.inet.api.common.bean.InetDevice; import ru.bitel.bgbilling.modules.inet.api.common.bean.InetDeviceType; import ru.bitel.bgbilling.modules.inet.api.common.bean.InetServ; import ru.bitel.bgbilling.server.util.ServerUtils; import ru.bitel.bgbilling.server.util.Setup; import ru.bitel.common.ParameterMap; import uk.co.westhawk.snmp.stack.AsnObjectId; import bitel.billing.common.KernelConst; import bitel.billing.server.contract.bean.ContractStatus; import bitel.billing.server.contract.bean.ContractStatusManager; public class SwitchServiceActivator extends ServiceActivatorAdapter implements ServiceActivator { private static Logger log = Logger.getLogger( SwitchServiceActivator.class ); private static String PDU_PREFIX = "1.3.6.1.2.1.2.2.1.7"; private InetDevice inetDevice; private String host; private ParameterMap parameterMap; private Setup setup; private SnmpClient snmpClient; @Override public Object init( Setup setup, int moduleId, InetDevice inetDevice, InetDeviceType inetDeviceType, ParameterMap parameterMap ) throws Exception { this.inetDevice = inetDevice; this.parameterMap = parameterMap; this.setup = setup; return true; } @Override public Object connect() throws Exception { List< InetSocketAddress> hosts = inetDevice.getHosts(); host = hosts.get( 0 ).getHostName(); int port = hosts.get( 0 ).getPort(); String secret = inetDevice.getSecret(); int snmpVersion = parameterMap.getInt( "snmp.version", 1 );; if( log.isDebugEnabled() ) { log.debug( "SNMP version: " + snmpVersion ); } if( log.isDebugEnabled() ) { log.debug( inetDevice.getId() + " device: " + host + ":" + port + " commutinty " + secret ); } snmpClient = new SnmpClient( host, port, snmpVersion, secret ); log.debug ( "SnmpClient created" ); return null; } private Object proccesServ( ServiceActivatorEvent serviceActivatorEvent, InetServ inetServ ) throws Exception { Connection con = null; //InetServ inetServ = serviceActivatorEvent.getNewInetServ(); int contractId = inetServ.getContractId(); int port = inetServ.getInterfaceId(); log.debug( "serviceModify" ); try { con = setup.getDBConnectionFromPool(); ContractStatusManager statusManager = new ContractStatusManager( con ); ContractStatus contractStatus = statusManager.getStatus( contractId, new Date() ); log.debug( "port=" + port ); if( port >= 0 ) { int value = 2; if( contractStatus == null || contractStatus.getStatus() == KernelConst.CONTRACT_STATUS_ACTIVE ) { value = 1; } else { value = 2; } snmpClient.set( new AsnObjectId( PDU_PREFIX + "." + port).getOid(), -1, value ); if( log.isDebugEnabled() ) { log.debug( "Add oid " + host + " " + PDU_PREFIX + "." + port + " i " + value ); } } } finally { ServerUtils.closeConnection( con ); } return null; } @Override public Object serviceModify( ServiceActivatorEvent serviceActivatorEvent ) throws Exception { InetServ inetServ = serviceActivatorEvent.getNewInetServ(); return proccesServ( serviceActivatorEvent, inetServ ); } @Override public Object disconnect() throws Exception { if( snmpClient != null ) { snmpClient.destroy(); snmpClient = null; } return null; } public Object serviceCreate( ServiceActivatorEvent e ) throws Exception { log.debug( "serviceCreate" ); return serviceModify( e ); } @Override public Object serviceCancel( ServiceActivatorEvent e ) throws Exception { log.debug( "serviceCancel" ); InetServ serv = e.getOldInetServ(); return proccesServ( e, serv ); } }