Реализация стандартного шлюза коммутатора Zyxel (под Cisco2) на BeanShell
Материал из BiTel WiKi
конфигурация типа шлюза :
user_rule.editor.class=bitel.billing.module.services.ipn.editor.vlan.CiscoSSHSwitchRuleEditor gate_manager.class=bitel.billing.server.ipn.vlan.CiscoSSHSwitchGateWorker use.script=1
Примеры команд шлюза :
[DEFAULT] [ADD] [/ADD] [REMOVE] [/REMOVE] [OPEN] <LOOP_PORT> interface port-channel {PORT} pvid {VID} exit </LOOP_PORT> vlan {VID} name "abonent {VID}" normal "" forbidden 1-24 fixed 25-26 <LOOP_PORT> fixed {PORT} </LOOP_PORT> untagged 1-24 exit [/OPEN] [CLOSE] no vlan {VID} [/CLOSE] [/DEFAULT]
Шлюз управляется по ssh. Код :
import java.util.*; import java.util.regex.*; import java.util.regex.Pattern; import bitel.billing.common.module.ipn.IPNContractStatus; import bitel.billing.server.ipn.GateWorker; import bitel.billing.server.ipn.UserStatus; import bitel.billing.server.ipn.bean.ManadUtils; import bitel.billing.server.ipn.bean.VlanManager; import bitel.billing.server.util.DefaultServerSetup; import bitel.billing.server.util.ssh.SSHSession; import bitel.billing.server.ipn.UserStatus; import ru.bitel.bgbilling.common.DefaultSetup; import bitel.billing.server.ipn.bean.GateType; import bitel.billing.server.ipn.bean.LoopPattern; import bitel.billing.server.ipn.bean.RuleType; protected void doSync() { log.info( "start of SSH_SWITCH........................................................"); host = gate.getHost(); port = gate.getPort(); gateSetup = new DefaultSetup( gate.getConfig(), "\r\n" ); login = gateSetup.getStringValue( "login", "root" ); timeout = gateSetup.getIntValue( "timeout", 2000 ); pswd = gate.getKeyword(); if( log.isDebugEnabled() ) { log.info( " gate: " + host + ":" + port + " login: " + login + " pswd: " + pswd ); } session = null; result = new StringBuffer(); try { session = new SSHSession( host, port, login, pswd ); session.setTimeout( timeout ); session.connect(); result.append( session.command( "configure" ) ); VlanManager manager = new VlanManager(mid, con); log.info( "running commands"); for( UserStatus status : statusList ) { //берем vlan у родительской циски int vid = manager.getVlan( gate.getParentId(), status.contractId ); if ( status.status == IPNContractStatus.STATUS_OPEN ) { rules = getOpenRules( status, vid ); for ( String rule : rules ) { result.append( session.command( rule ) ); } } else { rules = getCloseRules( status, vid ); for ( String rule : rules ) { result.append( session.command( rule ) ); } } } result.append( session.command( "exit" ) ); log.info("!!!!!!!!!!!!!!SSH Switch!!!!!!!!!!!!!!!!!!"); log.info( result ); log.info("!!!!!!!!!!!!!!!SSH Switch!!!!!!!!!!!!!!!!!"); } catch ( Exception e ) { log.error("", e ); throw new RuntimeException ( e ); } finally { if( session != null ) { session.disconnect(); } log.info( "end of SSH_SWITCH........................................................"); } } getOpenRules( UserStatus status, int vid ) { return getRules( status, "\\[OPEN\\]((.|\n)*)\\[/OPEN\\]", vid ); } getCloseRules( UserStatus status, int vid ) { return getRules( status, "\\[CLOSE\\]((.|\n)*)\\[/CLOSE\\]", vid ); } getRules( status, template, vid ) { // пользовательское правило, без типа - то все оставляем как есть rule = status.rule.getRuleText(); //порты идут до #, а адреса идут после String [] parts = rule.split( "#" ); portsStr = ""; addresesStr = ""; if ( parts.length > 0 ) { portsStr = parts[0]; } if ( parts.length > 1 ) { addresesStr = parts[1]; } //типизированное правило if( status.ruleType != null ) { rule = generateRule( addresesStr, portsStr, vid, status.gateType, status.ruleType ); } pattern = Pattern.compile( template ); m = pattern.matcher( rule ); if (m.find()) { rule = m.group( 1 ); } rule.replaceAll( "\r", "" ); parts = rule.split( "\n" ); return parts; } generateRule( addreses, ports, vid, gateType, ruleType ) { ports_ = getPorts( ports ); ruleText = ManadUtils.getRule( gateType, ruleType ); replacements = new HashMap(); if ( vid > 0) { replacements.put( "\\{VID\\}", String.valueOf( vid ) ); } loops = ManadUtils.getAddresLoops( addreses ); //адреса p = new LoopPattern(); p.setLoopPatern( "LOOP_PORT" ); p.setReplace( "\\{PORT\\}" ); p.setObjects( ports_ ); loops.add( p ); return ManadUtils.generateRule( ruleText, replacements, ruleType, loops ); } getPorts( ports ) { portList = new ArrayList<String>(); parts = ports.split( ";" ); for (String part : parts ) { parts2 = part.split( ":" ); if ( parts2.length < 2) { continue; } port = parts2 [1]; portList.add( port ); } String [] ports_ = new String [portList.size()]; for (int i = 0; i < ports_.length; i++) { ports_[i] = portList.get( i ); } return ports_; }