Реализация обработчика активации сервисов для коммутаторов DES-3526, DES-3550, DES-3828, DES-3852, DGS-3200-10 и им подобных
Материал из BiTel WiKi
(Различия между версиями)
Stark (Обсуждение | вклад) м (переименовал «Реалиазация обработчика для коммутаторов DES-3526, DES-3550, DES-3828, DES-3852, DGS-3200-10 и им подобных» в «[[Реализация обработчика активаци) |
Simpl3x (Обсуждение | вклад) |
||
Строка 1: | Строка 1: | ||
- | |||
Этот обработчик получен из скриптового шлюза IPN: | Этот обработчик получен из скриптового шлюза IPN: | ||
Строка 87: | Строка 86: | ||
session = new TelnetSession( host, port); | session = new TelnetSession( host, port); | ||
+ | int timeout = 10000; | ||
+ | session.setTimeout( timeout ); | ||
session.setEndString( ":" ); | session.setEndString( ":" ); | ||
Строка 113: | Строка 114: | ||
} | } | ||
- | + | @Override | |
public Object serviceModify( ServiceActivatorEvent serviceActivatorEvent ) | public Object serviceModify( ServiceActivatorEvent serviceActivatorEvent ) | ||
throws Exception | throws Exception | ||
{ | { | ||
+ | InetServ serv = serviceActivatorEvent.getNewInetServ(); | ||
+ | proccesServ( serviceActivatorEvent, serv ); | ||
+ | |||
+ | |||
+ | return null; | ||
+ | } | ||
+ | |||
+ | private Object proccesServ( ServiceActivatorEvent serviceActivatorEvent, InetServ serv ) | ||
+ | throws Exception | ||
+ | { | ||
log.debug( "newState= " + serviceActivatorEvent.getNewState() ); | log.debug( "newState= " + serviceActivatorEvent.getNewState() ); | ||
if ( serviceActivatorEvent.getNewState() != serviceActivatorEvent.getOldState() ) | if ( serviceActivatorEvent.getNewState() != serviceActivatorEvent.getOldState() ) | ||
Строка 124: | Строка 135: | ||
try | try | ||
{ | { | ||
- | |||
log.debug( "execute commands" ); | log.debug( "execute commands" ); | ||
Строка 132: | Строка 142: | ||
log.debug( "openAddress = " + openAddress ); | log.debug( "openAddress = " + openAddress ); | ||
- | + | ||
- | List<Integer>uplinks = Utils.toListInt( parameterMap.get( "uplink", "" ) ); | + | List<Integer> uplinks = Utils.toListInt( parameterMap.get( "uplink", "" ) ); |
- | + | ||
- | + | //List<UserPortStatus> ports = new ArrayList<UserPortStatus>(); | |
- | + | ||
//List ports = getUserList( statusList ); | //List ports = getUserList( statusList ); | ||
Строка 146: | Строка 155: | ||
// разрешающие правила добавляются с номером порта клиента | // разрешающие правила добавляются с номером порта клиента | ||
"config access_profile profile_id 20 add access_id ${port} ip source_ip ${ip} destination_ip 0.0.0.0 port ${port} permit"}; | "config access_profile profile_id 20 add access_id ${port} ip source_ip ${ip} destination_ip 0.0.0.0 port ${port} permit"}; | ||
- | |||
Строка 165: | Строка 173: | ||
//for( UserPortStatus port : ports ) | //for( UserPortStatus port : ports ) | ||
//{ | //{ | ||
- | + | ||
- | + | ||
log.debug("port.ipAddr=" + IpAddress.toString( serv.getAddressFrom() ) ); | log.debug("port.ipAddr=" + IpAddress.toString( serv.getAddressFrom() ) ); | ||
//удаляюшие правила | //удаляюшие правила | ||
Строка 280: | Строка 287: | ||
} | } | ||
} | } | ||
+ | |||
+ | log.debug( "processed!" ); | ||
return null; | return null; | ||
- | + | } | |
- | + | ||
- | + | ||
@Override | @Override | ||
Строка 296: | Строка 303: | ||
throws Exception | throws Exception | ||
{ | { | ||
- | return | + | log.debug( "serviceCreate" ); |
+ | return serviceModify( serviceActivatorEvent1 ); | ||
} | } | ||
+ | |||
+ | |||
+ | @Override | ||
+ | public Object serviceCancel( ServiceActivatorEvent serviceActivatorEvent1 ) | ||
+ | throws Exception | ||
+ | { | ||
+ | log.debug( "serviceCancel-New" ); | ||
+ | InetServ serv = serviceActivatorEvent1.getOldInetServ(); | ||
+ | return proccesServ( serviceActivatorEvent1, serv ); | ||
+ | } | ||
@Override | @Override | ||
Строка 330: | Строка 348: | ||
} | } | ||
- | |||
</source> | </source> |
Версия 12:02, 19 декабря 2012
Этот обработчик получен из скриптового шлюза IPN:
в процессе перехода с IPN на INET.
Код обработчика
package ru.gigacom.inet.dyn.device.dlink; import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; import org.apache.log4j.Logger; 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.Setup; import ru.bitel.common.ParameterMap; import ru.bitel.common.Preferences; import ru.bitel.common.Utils; import ru.bitel.common.inet.IpAddress; import bitel.billing.common.module.ipn.IPNContractStatus; import bitel.billing.server.ipn.UserStatus; import bitel.billing.server.ipn.dlink.UserPortStatus; import bitel.billing.server.util.telnet.TelnetSession; /** * Dlink 3026,3028,1228ME * @author stark * */ public class DlinkTelnetServiceActivator extends ServiceActivatorAdapter implements ServiceActivator { private InetDevice inetDevice; private static final Logger log = Logger.getLogger( DlinkTelnetServiceActivator.class ); private TelnetSession session; private StringBuffer result ; private ParameterMap parameterMap; @Override public Object init( Setup setup, int moduleId, InetDevice inetDevice, InetDeviceType inetDeviceType, ParameterMap parameterMap ) throws Exception { this.inetDevice = inetDevice; this.parameterMap = parameterMap; return true; } @Override public Object connect() throws Exception { List< InetSocketAddress> hosts = inetDevice.getHosts(); String host = hosts.get( 0 ).getHostName(); int port = hosts.get( 0 ).getPort(); String pswd = inetDevice.getPassword(); String login = inetDevice.getUsername(); String enablepwd = inetDevice.getSecret(); result = new StringBuffer(); if( log.isDebugEnabled() ) { log.debug( inetDevice.getId() + " gate: " + host + ":" + port + " login: " + login + " pswd: " + pswd ); } session = new TelnetSession( host, port); int timeout = 10000; session.setTimeout( timeout ); session.setEndString( ":" ); log.debug( "before connect" ); session.connect(); log.debug( "after connect" ); result.append( session.doCommand( login ) ); log.debug( "after command" ); session.setEndString( "#" ); result.append( session.doCommand( pswd ) ); log.debug( "before pswd" ); log.debug( "before enable" ); session.setEndString( ":" ); result.append( session.doCommand( "enable admin" ) ); log.debug( "before enablepwd" ); session.setEndString( "#" ); result.append( session.doCommand( enablepwd ) ); log.debug( "after enable" ); session.setEndString( "#" ); return true; } @Override public Object serviceModify( ServiceActivatorEvent serviceActivatorEvent ) throws Exception { InetServ serv = serviceActivatorEvent.getNewInetServ(); proccesServ( serviceActivatorEvent, serv ); return null; } private Object proccesServ( ServiceActivatorEvent serviceActivatorEvent, InetServ serv ) throws Exception { log.debug( "newState= " + serviceActivatorEvent.getNewState() ); if ( serviceActivatorEvent.getNewState() != serviceActivatorEvent.getOldState() ) { int state = serviceActivatorEvent.getNewState(); try { log.debug( "execute commands" ); String openAddress = parameterMap.get( "open.address", "255.255.255.255" ) ; log.debug( "openAddress = " + openAddress ); List<Integer> uplinks = Utils.toListInt( parameterMap.get( "uplink", "" ) ); //List<UserPortStatus> ports = new ArrayList<UserPortStatus>(); //List ports = getUserList( statusList ); String [] openRules = new String [] { //Профиль 20 Разрешаем клиенту видеть мир // разрешающие правила добавляются с номером порта клиента "config access_profile profile_id 20 add access_id ${port} ip source_ip ${ip} destination_ip 0.0.0.0 port ${port} permit"}; String [] closeRules = new String [] { // Профиль 8 разрешаем клиенту видеть Стат+ДНС сервер // разрешающие правила добавляются с номером порта клиента "config access_profile profile_id 8 add access_id ${port} ip source_ip ${ip} destination_ip ${openAdress} port ${port} permit", //Профиль 12 Разрешаем ИП запросы на аплинках на адрес клиента со статистики "config access_profile profile_id 12 add access_id ${mod}${port} ip source_ip ${openAdress} destination_ip ${ip} port ${upport} permit", //16 Запрещаем ИП запросы на аплинках на адрес клиента "config access_profile profile_id 16 add access_id ${mod}${port} ip source_ip 0.0.0.0 destination_ip ${ip} port ${upport} deny"}; //for( UserPortStatus port : ports ) //{ log.debug("port.ipAddr=" + IpAddress.toString( serv.getAddressFrom() ) ); //удаляюшие правила if ( state == InetServ.STATE_DELETED ) { int [] profiles = new int [] { 8, 20 }; for (int profile : profiles) { String rule = "config access_profile profile_id " + profile + " delete access_id " + serv.getInterfaceId(); log.debug("rule=" + rule); result.append( session.doCommand( rule ) ); } profiles = new int [] { 12, 16 }; for (int profile : profiles) { for (int j = 0; j < uplinks.size(); j++) { Integer upport = uplinks.get(j); String pref = j > 0 ? String.valueOf( j * 10 ) : ""; String rule = "config access_profile profile_id " + profile + " delete access_id " + pref + serv.getInterfaceId(); log.debug("rule=" + rule); result.append( session.doCommand( rule ) ); } } } else if ( state == InetServ.STATE_ENABLE ) { int [] profiles = new int [] { 8 }; for (int profile : profiles) { String rule = "config access_profile profile_id " + profile + " delete access_id " + serv.getInterfaceId(); log.debug("rule=" + rule); result.append( session.doCommand( rule ) ); } profiles = new int [] { 12, 16 }; for (int profile : profiles) { for (int j = 0; j < uplinks.size(); j++) { Integer upport = uplinks.get(j); String pref = j > 0 ? String.valueOf( j * 10 ) : ""; String rule = "config access_profile profile_id " + profile + " delete access_id " + pref + serv.getInterfaceId(); log.debug("rule=" + rule); result.append( session.doCommand( rule ) ); } } //20 String rule = openRules[0]; rule = setValues( rule, serv.getInterfaceId(), IpAddress.toString( serv.getAddressFrom() ) , openAddress, "" ); log.info("rule=" + rule); result.append( session.doCommand( rule ) ); } if ( state == InetServ.STATE_DISABLE ) { int [] profiles = new int [] { 20 }; for (int profile : profiles) { String rule = "config access_profile profile_id " + profile + " delete access_id " + serv.getInterfaceId(); log.debug("rule=" + rule); result.append( session.doCommand( rule ) ); } //8 String rule = closeRules[0]; rule = setValues( rule, serv.getInterfaceId(), IpAddress.toString( serv.getAddressFrom() ), openAddress, "" ); log.info("rule=" + rule); result.append( session.doCommand( rule ) ); //12, 16 for (int j = 0; j < uplinks.size(); j++) { Integer upport = uplinks.get(j); for (int i = 1; i < 3; i++) { rule = closeRules[i]; rule = setValues( rule, serv.getInterfaceId(), IpAddress.toString( serv.getAddressFrom() ), openAddress, String.valueOf( upport ) ); rule = rule.replaceAll( "\\$\\{mod\\}" , String.valueOf( j * 10 ) ); log.info("rule=" + rule); result.append( session.doCommand( rule ) ); } } } } catch (Exception e) { log.error( "", e ); } } log.debug( "processed!" ); return null; } @Override public Object connectionClose( ServiceActivatorEvent serviceActivatorEvent1 ) throws Exception { return null; } @Override public Object serviceCreate( ServiceActivatorEvent serviceActivatorEvent1 ) throws Exception { log.debug( "serviceCreate" ); return serviceModify( serviceActivatorEvent1 ); } @Override public Object serviceCancel( ServiceActivatorEvent serviceActivatorEvent1 ) throws Exception { log.debug( "serviceCancel-New" ); InetServ serv = serviceActivatorEvent1.getOldInetServ(); return proccesServ( serviceActivatorEvent1, serv ); } @Override public Object disconnect() throws Exception { session.setEndString( "#" ); log.debug( "SAVE" ); result.append( session.doCommand( "save" ) ); log.debug( "AFTER SAVE" ); session.setEndString ( "****" ); result.append( session.doCommand( "logout" ) ); log.info( result.toString() ); log.debug( "ok" ); return true; } private String setValues( String rule, int port, String ip, String openAddress, String upport ) { rule = rule.replaceAll( "\\$\\{port\\}", String.valueOf( port ) ); rule = rule.replaceAll( "\\$\\{ip\\}" , ip ); rule = rule.replaceAll( "\\$\\{openAdress\\}", openAddress ); rule = rule.replaceAll( "\\$\\{upport\\}", upport ); return rule; } }