8282public class VirtualRoutingResource implements Manager {
8383 private static final Logger s_logger = Logger .getLogger (VirtualRoutingResource .class );
8484 private String _savepasswordPath ; // This script saves a random password to the DomR file system
85- private String _ipassocPath ;
8685 private String _publicIpAddress ;
8786 private String _firewallPath ;
8887 private String _loadbPath ;
8988 private String _dhcpEntryPath ;
9089 private String _vmDataPath ;
9190 private String _publicEthIf ;
9291 private String _privateEthIf ;
93- private String _getRouterStatusPath ;
9492 private String _bumpUpPriorityPath ;
95- private String _l2tpVpnPath ;
96- private String _getDomRVersionPath ;
97-
93+ private String _routerProxyPath ;
9894
9995 private int _timeout ;
10096 private int _startTimeout ;
@@ -146,37 +142,41 @@ public Answer executeRequest(final Command cmd) {
146142 }
147143
148144 private Answer execute (VpnUsersCfgCommand cmd ) {
149- for (VpnUsersCfgCommand .UsernamePassword userpwd : cmd .getUserpwds ()) {
150- Script command = new Script (_l2tpVpnPath , _timeout , s_logger );
151- command .add (cmd .getAccessDetail (NetworkElementCommand .ROUTER_IP ));
145+ for (VpnUsersCfgCommand .UsernamePassword userpwd : cmd .getUserpwds ()) {
146+ String args = "" ;
152147 if (!userpwd .isAdd ()) {
153- command .add ("-U " , userpwd .getUsername ());
148+ args +="-U " ;
149+ args +=userpwd .getUsername ();
154150 } else {
155- command .add ("-u " , userpwd .getUsernamePassword ());
151+ args +="-u " ;
152+ args += userpwd .getUsernamePassword ();
156153 }
157- String result = command . execute ( );
154+ String result = routerProxy ( "vpn_l2tp.sh" , cmd . getAccessDetail ( NetworkElementCommand . ROUTER_IP ), args );
158155 if (result != null ) {
159156 return new Answer (cmd , false , "Configure VPN user failed for user " + userpwd .getUsername ());
160157 }
161- }
162-
158+ }
163159 return new Answer (cmd );
164160 }
165161
166162 private Answer execute (RemoteAccessVpnCfgCommand cmd ) {
167- Script command = new Script (_l2tpVpnPath , _timeout , s_logger );
168- command .add (cmd .getAccessDetail (NetworkElementCommand .ROUTER_IP ));
163+ String args = "" ;
169164 if (cmd .isCreate ()) {
170- command .add ("-r " , cmd .getIpRange ());
171- command .add ("-p " , cmd .getPresharedKey ());
172- command .add ("-s " , cmd .getVpnServerIp ());
173- command .add ("-l " , cmd .getLocalIp ());
174- command .add ("-c " );
165+ args += "-r " ;
166+ args += cmd .getIpRange ();
167+ args += " -p " ;
168+ args += cmd .getPresharedKey ();
169+ args += " -s " ;
170+ args += cmd .getVpnServerIp ();
171+ args += " -l " ;
172+ args += cmd .getLocalIp ();
173+ args += " -c " ;
175174 } else {
176- command .add ("-d " );
177- command .add ("-s " , cmd .getVpnServerIp ());
175+ args +="-d " ;
176+ args += " -s " ;
177+ args += cmd .getVpnServerIp ();
178178 }
179- String result = command . execute ( );
179+ String result = routerProxy ( "vpn_l2tp.sh" , cmd . getAccessDetail ( NetworkElementCommand . ROUTER_IP ), args );
180180 if (result != null ) {
181181 return new Answer (cmd , false , "Configure VPN failed" );
182182 }
@@ -474,9 +474,18 @@ protected synchronized Answer execute (final DhcpEntryCommand cmd) {
474474 }
475475
476476 public String getRouterStatus (String routerIP ) {
477- final Script command = new Script (_getRouterStatusPath , _timeout , s_logger );
477+ return routerProxy ("checkrouter.sh" , routerIP , null );
478+ }
479+
480+
481+ public String routerProxy (String script , String routerIP , String args ) {
482+ final Script command = new Script (_routerProxyPath , _timeout , s_logger );
478483 final OutputInterpreter .OneLineParser parser = new OutputInterpreter .OneLineParser ();
484+ command .add (script );
479485 command .add (routerIP );
486+ if ( args != null ) {
487+ command .add (args );
488+ }
480489 String result = command .execute (parser );
481490 if (result == null ) {
482491 return parser .getLine ();
@@ -507,14 +516,7 @@ protected Answer execute(BumpUpPriorityCommand cmd) {
507516 }
508517
509518 protected String getDomRVersion (String routerIP ) {
510- final Script command = new Script (_getDomRVersionPath , _timeout , s_logger );
511- final OutputInterpreter .OneLineParser parser = new OutputInterpreter .OneLineParser ();
512- command .add (routerIP );
513- String result = command .execute (parser );
514- if (result == null ) {
515- return parser .getLine ();
516- }
517- return null ;
519+ return routerProxy ("netusage.sh" , routerIP , null );
518520 }
519521
520522 protected Answer execute (GetDomRVersionCmd cmd ) {
@@ -592,16 +594,17 @@ public synchronized String savePassword(final String privateIpAddress, final Str
592594
593595
594596 public String assignPublicIpAddress (final String vmName , final long id , final String vnet , final String privateIpAddress , final String macAddress , final String publicIpAddress ) {
595-
596- final Script command = new Script (_ipassocPath , _timeout , s_logger );
597- command .add ("-A" );
598- command .add ("-f" ); //first ip is source nat ip
599- command .add ("-r" , vmName );
600- command .add ("-i" , privateIpAddress );
601- command .add ("-a" , macAddress );
602- command .add ("-l" , publicIpAddress );
603-
604- return command .execute ();
597+ String args ="-A" ;
598+ args += " -f" ; //first ip is source nat ip
599+ args += " -r " ;
600+ args += vmName ;
601+ args += " -i " ;
602+ args += privateIpAddress ;
603+ args += " -a " ;
604+ args += macAddress ;
605+ args += " -l " ;
606+ args += publicIpAddress ;
607+ return routerProxy ("ipassoc.sh" , privateIpAddress , args );
605608 }
606609
607610 public String assignPublicIpAddress (final String vmName ,
@@ -610,30 +613,29 @@ public String assignPublicIpAddress(final String vmName,
610613 final String vlanId , final String vlanGateway ,
611614 final String vlanNetmask , final String vifMacAddress , String guestIp , int nicNum ){
612615
613- final Script command = new Script (_ipassocPath , _timeout , s_logger );
614- command .add ( privateIpAddress );
616+ String args = "" ;
615617 if (add ) {
616- command . add ( "-A" ) ;
618+ args += "-A" ;
617619 } else {
618- command . add ( "-D" ) ;
620+ args += "-D" ;
619621 }
620-
622+ String cidrSize = Long . toString ( NetUtils . getCidrSize ( vlanNetmask ));
621623 if (sourceNat ) {
622- command . add ( " -s") ;
623- }
624+ args += " -s" ;
625+ }
624626 if (firstIP ) {
625- command .add ( "-f" );
626-
627+ args += " -f" ;
627628 }
628- String cidrSize = Long .toString (NetUtils .getCidrSize (vlanNetmask ));
629- command .add ( "-l" , publicIpAddress + "/" + cidrSize );
629+ args += " -l " ;
630+ args += publicIpAddress + "/" + cidrSize ;
631+
630632 String publicNic = "eth" + nicNum ;
631- command .add ("-c" , publicNic );
632-
633- command .add ("-g" , vlanGateway );
634-
633+ args += " -c " ;
634+ args += publicNic ;
635635
636- return command .execute ();
636+ args +=" -g " ;
637+ args += vlanGateway ;
638+ return routerProxy ("ipassoc.sh" , privateIpAddress , args );
637639 }
638640
639641 private void deletExitingLinkLocalRoutTable (String linkLocalBr ) {
@@ -801,12 +803,6 @@ public boolean configure(final String name, final Map<String, Object> params) th
801803 value = (String )params .get ("ssh.port" );
802804 _port = NumbersUtil .parseInt (value , 3922 );
803805
804- _ipassocPath = findScript ("ipassoc.sh" );
805- if (_ipassocPath == null ) {
806- throw new ConfigurationException ("Unable to find the ipassoc.sh" );
807- }
808- s_logger .info ("ipassoc.sh found in " + _ipassocPath );
809-
810806 _publicIpAddress = (String )params .get ("public.ip.address" );
811807 if (_publicIpAddress != null ) {
812808 s_logger .warn ("Incoming public ip address is overriden. Will always be using the same ip address: " + _publicIpAddress );
@@ -837,11 +833,6 @@ public boolean configure(final String name, final Map<String, Object> params) th
837833 throw new ConfigurationException ("Unable to find user_data.sh" );
838834 }
839835
840- _getRouterStatusPath = findScript ("getRouterStatus.sh" );
841- if (_getRouterStatusPath == null ) {
842- throw new ConfigurationException ("Unable to find getRouterStatus.sh" );
843- }
844-
845836 _publicEthIf = (String )params .get ("public.network.device" );
846837 if (_publicEthIf == null ) {
847838 _publicEthIf = "xenbr1" ;
@@ -859,14 +850,9 @@ public boolean configure(final String name, final Map<String, Object> params) th
859850 throw new ConfigurationException ("Unable to find bumpUpPriority.sh" );
860851 }
861852
862- _l2tpVpnPath = findScript ("l2tp_vpn.sh" );
863- if (_l2tpVpnPath == null ) {
864- throw new ConfigurationException ("Unable to find l2tp_vpn.sh" );
865- }
866-
867- _getDomRVersionPath = findScript ("getDomRVersion.sh" );
868- if (_getDomRVersionPath == null ) {
869- throw new ConfigurationException ("Unable to find getDomRVersion.sh" );
853+ _routerProxyPath = findScript ("routerProxy.sh" );
854+ if (_routerProxyPath == null ) {
855+ throw new ConfigurationException ("Unable to find routerProxy.sh" );
870856 }
871857
872858 return true ;
0 commit comments