Реализация стандартного шлюза Cisco2 на BeanShell

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

Перейти к: навигация, поиск

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

user_rule.editor.class=bitel.billing.module.services.ipn.editor.vlan.CiscoVlanContactRuleEditor
gate_manager.class=bitel.billing.server.ipn.vlan.CiscoVlanGateWorker
use.script=1

Примеры команд шлюза :

[DEFAULT]
 
[REMOVE]
    no vlan {VID}
    no interface Vlan {VID}
[/REMOVE]
 
[OPEN]
    vlan {VID}
    interface Vlan {VID}
    no shutdown
    ip unnumbered gi2/1
    ip helper-address 192.168.0.183
    exit
[/OPEN]
 
[CLOSE]
    interface Vlan {VID}
    shutdown
    exit
[/CLOSE]
[/DEFAULT]

Шлюз управляется по 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.ManadUtils;
import bitel.billing.server.ipn.bean.VlanManager;
import bitel.billing.server.util.DefaultServerSetup;
import bitel.billing.server.util.Utils;
import bitel.billing.server.util.telnet.TelnetSession;
import ru.bitel.bgbilling.common.DefaultSetup;
import bitel.billing.common.IPUtils;
import bitel.billing.server.ipn.bean.GateType;
import bitel.billing.server.ipn.bean.RuleType;
import bitel.billing.server.ipn.bean.VlanManager;
import bitel.billing.server.util.Utils;
import bitel.billing.server.util.telnet.TelnetSession;
 
 
 
protected void doSync()
{
	try
	{
		log.info( "start of cisco........................................................");
		host = gate.getHost();
		port = gate.getPort();
 
 
		DefaultServerSetup gateSetup = new DefaultServerSetup( gate.getConfig(), "\r\n" );        
 
		pswdLogin = gate.getKeyword();
		pswdCfg = gateSetup.getStringValue( "cfg.pswd");
                timeout = gateSetup.getIntValue( "timeout", 0 );
 
 
		result = new StringBuffer();
 
 
		if( log.isDebugEnabled() )
		{
		    log.debug( gate.getId() + " gate: " + host + ":" + port  + " pswdLogin: " + pswdLogin + " pswdCfg: " + 		 pswdCfg );
		}
 
 
 
		session = new TelnetSession( host, port);
                session.setTimeout( timeout );
		session.setLoginPromptSequence( ":" );						
		session.connect();
 
		session.setLoginPromptSequence( ">" );
 
 
		result.append( session.doCommand( pswdLogin ) );
 
 
 
		result.append( session.doCommand( "terminal length 0" ) );
		result.append( session.doCommand( "terminal width 0" ) );
 
 
 
		session.setLoginPromptSequence( ":" );
		result.append( session.doCommand( "enable" ) );
 
 
		session.setLoginPromptSequence( "#" );
		result.append( session.doCommand( pswdCfg ) );
 
 
		result.append( session.doCommand( "configure terminal" ) );
 
		log.debug( "execute commands" );
		doCommands( session, result);
 
		result.append( session.doCommand( "exit" ) );
                session.doCommandAsync( "exit" );
 
		log.info("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
		log.info( result );
		log.info("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");		
 
		log.debug( "ok" );
	} catch (Exception e)
	{
 
		throw new RuntimeException ( e );
	} 
 
	log.info( "end of cisco........................................................");
}
 
protected void add()
{
 
}
 
 
private void doCommands( TelnetSession session, StringBuffer result) throws IOException
{		
	for( UserStatus status : statusList )
	{
		VlanManager manager = new VlanManager(status.mid, con); 
		gateId  = gate.getId();
		log.info("gateId=" + gateId);
		vid = manager.getVlan( gateId, status.contractId );
		log.info("vid=" + vid);	
 
		//TODO - подумать что сделать справилами на добавление
 
		rules = null;
		if (status.status == IPNContractStatus.STATUS_OPEN)
		{
 
			rules = getOpenRules( status, vid );
 
		} 			
		else if (status.status == IPNContractStatus.STATUS_REMOVED)
		{
			rules = getRemoveRules( status, vid );	
		}
		//if closed and etc 
		else 
		{
			rules = getCloseRules( status, vid );
 
		}
 
		if (vid > 0)
		{
			for ( String rule : rules )
			{					
				result.append(  session.doCommand( rule ) );				
			}
		}
 
	}		
 
}
 
getOpenRules( status, vid )
{
    return getRules( status, "\\[OPEN\\](.*)\\[/OPEN\\]", vid );
}
 
getCloseRules( status, vid )
{
    return getRules( status, "\\[CLOSE\\](.*)\\[/CLOSE\\]", vid );
}
 
getRemoveRules( UserStatus status, int vid )
{
    return getRules( status, "\\[REMOVE\\](.*)\\[/REMOVE\\]", vid  );
}
 
getRules(  status, template, vid )
{
	// пользовательское правило, без типа - то все оставляем как есть
	rule = status.rule.getRuleText();
	log.info("rule=" + rule);
 
	//типизированное правило
	if( status.ruleType != null )
 
	{	
	    rule = generateRule( rule, status.gateType, status.ruleType, vid );
	}
 
	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, int vid )
{		
    ruleText = ManadUtils.getRule( gateType, ruleType );
    replacements =  new HashMap ();
    if ( vid > 0)
    {
        replacements.put( "\\{VID\\}", String.valueOf( vid ) );
    }
    return ManadUtils.generateRule( ruleText, addresses, replacements, ruleType );		
}
Личные инструменты