Реализация шлюза EdgeCore
Материал из BiTel WiKi
(Различия между версиями)
Stark (Обсуждение | вклад) |
Stark (Обсуждение | вклад) |
||
Строка 41: | Строка 41: | ||
result.append( session.doCommand( "speed-duplex " + speedCur ) ); | result.append( session.doCommand( "speed-duplex " + speedCur ) ); | ||
result.append( session.doCommand( "exit" ) ); | result.append( session.doCommand( "exit" ) ); | ||
- | </source | + | </source> |
Текущая версия на 10:52, 11 декабря 2009
Шлюз для управления оборудованием EdgeCore. конфигурация типа шлюза :
user_rule.editor.class=bitel.billing.module.services.ipn.editor.ManadContractRuleEditor gate_manager.class=bitel.billing.server.ipn.ManadGateWorker use.script=1
Команды типа шлюза :
[DEFAULT] [OPEN] <LOOP_PORT> int eth 1/{PORT} ip access-group on in no service-policy input off1 </LOOP_PORT> [/OPEN] [CLOSE] <LOOP_PORT> int eth 1/{PORT} ip access-group off in service-policy input off1 </LOOP_PORT> [/CLOSE] [/DEFAULT]
Это команды на открытие/закрытие доступа. Помимо этого шлюз еще управляет скоростью абонента - если есть абонплата , значит скорость нужно поставить 100 мбит, если нет , то 10 мбит .
команды задания скорости вшиты в самом шлюзе в блоке :
result.append( session.doCommand( "int e 1/" + port ) ); result.append( session.doCommand( "speed-duplex " + speedCur ) ); result.append( session.doCommand( "exit" ) );
Управление шлюзом происходит по telnet. Код :
import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.StringTokenizer; 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.*; import ru.bitel.bgbilling.server.util.DefaultSetup; import bitel.billing.server.util.Utils; import bitel.billing.server.util.telnet.OperationTimedoutException; import bitel.billing.server.util.telnet.TelnetSession; import ru.bitel.bgbilling.common.DefaultSetup; import bitel.billing.common.IPUtils; import bitel.billing.server.util.Utils; import bitel.billing.server.util.telnet.OperationTimedoutException; import bitel.billing.server.util.telnet.TelnetSession; import bitel.billing.server.util.telnet.ansi.TelnetAnsiSession; import ru.bitel.bgbilling.modules.ipn.server.bean.GatePortManager; import java.sql.*; //includeBGBS( "bgbs://ru.bitel.bgbilling.kernel.script.common.bean.ScriptLibrary/manad" ); protected void doSync() { log.info( "start of cisco........................................................"); host = gate.getHost(); port = gate.getPort(); DefaultSetup gateSetup = new DefaultSetup( gate.getConfig(), "\r\n" ); pswd = gate.getKeyword(); login = gateSetup.getStringValue( "login"); timeout = gateSetup.getIntValue( "timeout", 2000 ); result = new StringBuffer(); if( log.isDebugEnabled() ) { log.debug( gate.getId() + " gate: " + host + ":" + port + " login: " + login + " passwd: " + pswd ); } log.debug("before connect"); TelnetAnsiSession session = new TelnetAnsiSession( host, port); session.setTimeout( timeout ); session.setSocketTimeTimeout( timeout ); session.setLoginPromptSequence( ": " ); session.connect(); log.debug("after connect"); result.append( session.doCommand( login ) ); log.debug("after login"); session.setLoginPromptSequence( "#" ); result.append( session.doCommand( pswd ) ); log.debug("after pswd"); log.debug( "execute commands" ); doCommands( session, result, gateSetup ); //выход из режима config result.append( session.doCommand( "exit" ) ); session.doCommandAsync( "exit" ); log.info("!!!!!!!!!!!!!!!!start of edgecore!!!!!!!!!!!!!!!!"); log.info( result ); log.info("!!!!!!!!!!!!!!! end of edgecore !!!!!!!!!!!!!!!!!"); log.debug( "ok" ); log.info( "end of cisco........................................................"); } private void doCommands( TelnetAnsiSession session, StringBuffer result, gateSetup) throws IOException, OperationTimedoutException { result.append( session.doCommand( "terminal length 0" ) ); onList = gateSetup.getStringValue( "on.list", "on"); offList = gateSetup.getStringValue( "off.list", "off" ); portResult = getPortResult( session ); speedResult = getSpeedResult( session ); result.append( session.doCommand( "config" ) ); log.debug("after config"); log.info( "!!!!!!!!!!!!!!!!!!!!!!!!!!portResult!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" ); log.info( portResult ); log.info( "!!!!!!!!!!!!!!!!!!!!!!!!!! end portResult!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" ); log.info( "!!!!!!!!!!!!!!!!!!!!!!!!!!speedResult!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" ); log.info( speedResult ); log.info( "!!!!!!!!!!!!!!!!!!!!!!!!!! end speedResult!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" ); GatePortManager man = new GatePortManager( con, mid ); for( UserStatus status : statusList ) { gateId = gate.getId(); log.info("gateId=" + gateId); ports = man.getGatePorts( status.contractId, gateId ); boolean allInOpened = true; //если хотя бы один порт не открыт, то считает что не открыт for ( port : ports ) { allInOpened = portExist(port, portResult, onList); if ( !allInOpened ) { break; } } boolean allInClosed = true; for ( port : ports ) { allInClosed = portExist(port, portResult, offList ); if ( !allInClosed ) { break; } } boolean allSpeedCorrect = true; speedCur = getCurrentSpeed( gateSetup, status.contractId ); log.debug( "speedCur=" + speedCur ); for ( port : ports ) { speed = getSpeed( port, speedResult, offList ); if (! speed.equals( speedCur ) ) { allSpeedCorrect = false; break; } } log.debug( "allInOpened=" + allInOpened ); log.debug( "allInClosed=" + allInClosed ); log.debug( "allSpeedCorrect=" + allSpeedCorrect ); List rules = null; if (status.status == IPNContractStatus.STATUS_OPEN && !allInOpened ) { rules = getOpenRules( status, ports ); } else if (status.status != IPNContractStatus.STATUS_OPEN && !allInClosed ) { rules = getCloseRules( status, ports ); } //sendRules( rules, session, result); if (rules != null ) { for ( String rule : rules ) { log.debug ("command=" + rule ); result.append( session.doCommand( rule ) ); } } if ( !allSpeedCorrect ) { log.debug( "correcting speed..." ); for ( port : ports ) { result.append( session.doCommand( "int e 1/" + port ) ); result.append( session.doCommand( "speed-duplex " + speedCur ) ); result.append( session.doCommand( "exit" ) ); } } } } getOpenRules( status, ports ) { return getRules( status, "\\[OPEN\\](.*)\\[/OPEN\\]", ports ); } getCloseRules( status, ports ) { return getRules( status, "\\[CLOSE\\](.*)\\[/CLOSE\\]", ports ); } getRules( status, template, ports ) { // пользовательское правило, без типа - то все оставляем как есть rule = status.rule.getRuleText(); log.info("rule=" + rule); //типизированное правило if( status.ruleType != null ) { rule = generateRule( rule, status.gateType, status.ruleType, ports ); } log.info("rule=" + rule); pattern = Pattern.compile( template, Pattern.DOTALL ); m = pattern.matcher( rule ); if (m.find()) { rule = m.group( 1 ); } rule.replaceAll( "\r", "" ); parts = rule.split( "\n" ); result = new ArrayList(); for ( String part : parts ) { if ( !Utils.isEmptyString( part )) { result.add( part ); } } return result; } generateRule( addresses, gateType, ruleType, ports ) { ruleText = ManadUtils.getRule( gateType, ruleType ); replacements = new HashMap(); loops = ManadUtils.getAddresLoops( addresses ); String [] ports_ = new String[ports.size()]; for ( int i = 0; i < ports_.length; i++) { ports_[i] = String.valueOf( ports.get(i) ); } //адреса p = new LoopPattern(); p.setLoopPatern( "LOOP_PORT" ); p.setReplace( "\\{PORT\\}" ); p.setObjects( ports_ ); loops.add( p ); return ManadUtils.generateRule( ruleText, replacements, ruleType, loops ); } private portExist ( port, buffer, list ) { Pattern pattern = Pattern.compile( ".*Interface ethernet 1/" + port +"\r\n IP access-group " + list + " in" ); Matcher m = pattern.matcher( buffer ); found = m.find(); log.debug( found ? "port found" : "port not found" ); return found; } private String getSpeed ( port, buffer, list ) { speed = ""; Pattern pattern = Pattern.compile( ".*Eth 1/ " + port + "\\s+.+\\s+\\d+\\s+\\d+\\s+(.+?)\\s+" ); Matcher m = pattern.matcher( buffer ); log.debug( "getting speed.." ); if ( m.find() ) { speed = m.group( 1 ); log.debug( "speed=" + speed + ";port=" + port ); } return speed; } private getPortResult( session ) { return session.doCommand( "show ip access-group"); } private getSpeedResult( session ) { return session.doCommand( "show interface brief"); } private getCurrentSpeed( gateSetup, cid ) { lowSpeed = gateSetup.getStringValue( "speed.low", "10full"); highSpeed = gateSetup.getStringValue( "speed.high", "100full"); mid = gateSetup.getIntValue( "npay.mid", 4 ); sid = gateSetup.getIntValue( "npay.sid", 11 ); String result = lowSpeed; String query = "Select * from npay_service_object_" + mid + " WHERE cid = " + cid + " AND sid = " + sid; PreparedStatement ps = con.prepareStatement( query ); ResultSet rs = ps.executeQuery(); if ( rs. next() ) { result = highSpeed; } return result; }