Реализация стандартного шлюза коммутатора Zyxel (под Cisco2) на BeanShell

Материал из BiTel WiKi

(Различия между версиями)
Перейти к: навигация, поиск
(Новая: конфигурация типа шлюза : <source lang="bash"> user_rule.editor.class=bitel.billing.module.services.ipn.editor.vlan.CiscoSSHSwitchRuleEditor gate_manager.class=bite...)
Строка 44: Строка 44:
</source>
</source>
-
Код :
+
Шлюз управляется по ssh. Код :
<source lang="java">
<source lang="java">
import java.util.HashMap;
import java.util.HashMap;

Версия 13:49, 4 сентября 2008

конфигурация типа шлюза :

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>
    interface port-channel {PORT}
    pvid {VID} 
    exit 
 </LOOP>
 
  vlan  {VID}
  name "abonent  {VID}" 
  normal "" 
  forbidden 1-24
  fixed 25-26  
  <LOOP>
    fixed  {PORT}
  </LOOP>        
  untagged 1-24
   exit
[/OPEN]
 
[CLOSE]
no vlan  {VID}
[/CLOSE]
 
[/DEFAULT]

Шлюз управляется по ssh. Код :

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
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;
protected void doSync()
{		
	log.info( "start of SSH_SWITCH........................................................");
    String host = gate.getHost();
    int port = gate.getPort();
 
 
    DefaultServerSetup gateSetup = new DefaultServerSetup( gate.getConfig(), "\r\n" );        
 
    String login = gateSetup.getStringValue( "login", "root" );
    int timeout = gateSetup.getIntValue( "timeout", 2000 );
 
    String pswd = gate.getKeyword();
 
    if( log.isDebugEnabled() )
    {
        log.info( " gate: " + host + ":" + port + " login: " + login + " pswd: " + pswd );
    }
 
    SSHSession session = null;
 
    StringBuffer 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 )
			{
				String[]  rules  = getOpenRules( status, vid );
				for ( String rule : rules )
				{
					result.append( session.command( rule ) );	
				}
 
			}
			else
			{
				String[] 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........................................................");
    }	
 
 
}
 
private String[]  getOpenRules( UserStatus status, int vid )
{
 
	return getRules( status, "\\[OPEN\\]((.|\n)*)\\[/OPEN\\]", vid );
}
 
private String[]  getCloseRules( UserStatus status, int vid )
{
 
 
	return getRules( status, "\\[CLOSE\\]((.|\n)*)\\[/CLOSE\\]", vid );
}
 
 
 
 
private String [] getRules(  UserStatus status, String template, int vid )
{
 
	// пользовательское правило, без типа - то все оставляем как есть
	String rule = status.rule.getRuleText();
	log.info("rule=" + rule);
 
	List portList = new ArrayList();
	String [] parts = rule.split( ";" );
 
	for (String part : parts )
	{
 
		String [] parts2  = part.split( ":" );			
 
		if ( parts2.length <  2)
		{
				continue;
		}
 
		String 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 );
	}		
 
 
 
	//типизированное правило
	if( status.ruleType != null )
 
	{			
       String ruleText = ManadUtils.getRule( status.gateType, status.ruleType );
 
		log.info("ruleText=" + ruleText);
 
 
		Map replacements =  new HashMap ();
 
		if ( vid > 0)
		{
			replacements.put( "\\{VID\\}", String.valueOf( vid ) );			
		}	
 
		rule = ManadUtils.generateRule( ruleText, replacements, status.ruleType, "\\{PORT\\}", ports );
 
	}
 
 
	Pattern pattern = Pattern.compile( template );
	Matcher m = pattern.matcher( rule );
	if (m.find())
	{
		rule = m.group( 1 );
	}		
 
	rule.replaceAll( "\r", "" );
 
 
	parts  = rule.split( "\n" );
 
 
	return parts;
}
Личные инструменты