2323import javax .inject .Inject ;
2424import javax .naming .ConfigurationException ;
2525
26- import org .apache .cloudstack .annotation .AnnotationService ;
27- import org .apache .cloudstack .annotation .dao .AnnotationDao ;
26+ import org .apache .commons .collections .CollectionUtils ;
2827import org .springframework .stereotype .Component ;
2928
29+ import org .apache .cloudstack .annotation .AnnotationService ;
30+ import org .apache .cloudstack .annotation .dao .AnnotationDao ;
3031import org .apache .cloudstack .api .command .user .vpn .CreateVpnConnectionCmd ;
3132import org .apache .cloudstack .api .command .user .vpn .CreateVpnCustomerGatewayCmd ;
3233import org .apache .cloudstack .api .command .user .vpn .CreateVpnGatewayCmd ;
4546import com .cloud .event .ActionEvent ;
4647import com .cloud .event .EventTypes ;
4748import com .cloud .exception .InvalidParameterValueException ;
48- import com .cloud .exception .NetworkRuleConflictException ;
4949import com .cloud .exception .PermissionDeniedException ;
5050import com .cloud .exception .ResourceUnavailableException ;
5151import com .cloud .network .Site2SiteCustomerGateway ;
@@ -106,7 +106,6 @@ public class Site2SiteVpnManagerImpl extends ManagerBase implements Site2SiteVpn
106106 @ Inject
107107 private AnnotationDao annotationDao ;
108108
109- String _name ;
110109 int _connLimit ;
111110 int _subnetsLimit ;
112111
@@ -253,35 +252,23 @@ public Site2SiteCustomerGateway createCustomerGateway(CreateVpnCustomerGatewayCm
253252
254253 @ Override
255254 @ ActionEvent (eventType = EventTypes .EVENT_S2S_VPN_CONNECTION_CREATE , eventDescription = "creating s2s vpn connection" , create = true )
256- public Site2SiteVpnConnection createVpnConnection (CreateVpnConnectionCmd cmd ) throws NetworkRuleConflictException {
255+ public Site2SiteVpnConnection createVpnConnection (CreateVpnConnectionCmd cmd ) {
257256 Account caller = CallContext .current ().getCallingAccount ();
258257 Account owner = _accountMgr .getAccount (cmd .getEntityOwnerId ());
259258
260259 //Verify that caller can perform actions in behalf of vpc owner
261260 _accountMgr .checkAccess (caller , null , false , owner );
262261
263262 Long customerGatewayId = cmd .getCustomerGatewayId ();
264- Site2SiteCustomerGateway customerGateway = _customerGatewayDao .findById (customerGatewayId );
265- if (customerGateway == null ) {
266- throw new InvalidParameterValueException ("Unable to found specified Site to Site VPN customer gateway " + customerGatewayId + " !" );
267- }
268- _accountMgr .checkAccess (caller , null , false , customerGateway );
263+ Site2SiteCustomerGateway customerGateway = getAndValidateSite2SiteCustomerGateway (customerGatewayId , caller );
269264
270265 Long vpnGatewayId = cmd .getVpnGatewayId ();
271- Site2SiteVpnGateway vpnGateway = _vpnGatewayDao .findById (vpnGatewayId );
272- if (vpnGateway == null ) {
273- throw new InvalidParameterValueException ("Unable to found specified Site to Site VPN gateway " + vpnGatewayId + " !" );
274- }
275- _accountMgr .checkAccess (caller , null , false , vpnGateway );
266+ Site2SiteVpnGateway vpnGateway = getAndValidateSite2SiteVpnGateway (vpnGatewayId , caller );
276267
277- if (customerGateway . getAccountId () != vpnGateway . getAccountId () || customerGateway . getDomainId () != vpnGateway . getDomainId ()) {
278- throw new InvalidParameterValueException ( "VPN connection can only be esitablished between same account's VPN gateway and customer gateway!" );
279- }
268+ validateVpnConnectionOfTheRightAccount (customerGateway , vpnGateway );
269+ validateVpnConnectionDoesntExist ( vpnGatewayId , customerGatewayId );
270+ validatePrerequisiteVpnGateway ( vpnGateway );
280271
281- if (_vpnConnectionDao .findByVpnGatewayIdAndCustomerGatewayId (vpnGatewayId , customerGatewayId ) != null ) {
282- throw new InvalidParameterValueException ("The vpn connection with customer gateway id " + customerGatewayId + " and vpn gateway id " + vpnGatewayId +
283- " already existed!" );
284- }
285272 String [] cidrList = customerGateway .getGuestCidrList ().split ("," );
286273
287274 // Remote sub nets cannot overlap VPC's sub net
@@ -324,13 +311,51 @@ public Site2SiteVpnConnection createVpnConnection(CreateVpnConnectionCmd cmd) th
324311 return conn ;
325312 }
326313
314+ private Site2SiteCustomerGateway getAndValidateSite2SiteCustomerGateway (Long customerGatewayId , Account caller ) {
315+ Site2SiteCustomerGateway customerGateway = _customerGatewayDao .findById (customerGatewayId );
316+ if (customerGateway == null ) {
317+ throw new InvalidParameterValueException (String .format ("Unable to find specified Site to Site VPN customer gateway %s !" , customerGatewayId ));
318+ }
319+ _accountMgr .checkAccess (caller , null , false , customerGateway );
320+ return customerGateway ;
321+ }
322+
323+ private Site2SiteVpnGateway getAndValidateSite2SiteVpnGateway (Long vpnGatewayId , Account caller ) {
324+ Site2SiteVpnGateway vpnGateway = _vpnGatewayDao .findById (vpnGatewayId );
325+ if (vpnGateway == null ) {
326+ throw new InvalidParameterValueException (String .format ("Unable to find specified Site to Site VPN gateway %s !" , vpnGatewayId ));
327+ }
328+ _accountMgr .checkAccess (caller , null , false , vpnGateway );
329+ return vpnGateway ;
330+ }
331+
332+ private void validateVpnConnectionOfTheRightAccount (Site2SiteCustomerGateway customerGateway , Site2SiteVpnGateway vpnGateway ) {
333+ if (customerGateway .getAccountId () != vpnGateway .getAccountId () || customerGateway .getDomainId () != vpnGateway .getDomainId ()) {
334+ throw new InvalidParameterValueException ("VPN connection can only be established between same account's VPN gateway and customer gateway!" );
335+ }
336+ }
337+
338+ private void validateVpnConnectionDoesntExist (Long vpnGatewayId , Long customerGatewayId ) {
339+ if (_vpnConnectionDao .findByVpnGatewayIdAndCustomerGatewayId (vpnGatewayId , customerGatewayId ) != null ) {
340+ throw new InvalidParameterValueException ("The vpn connection with customer gateway id " + customerGatewayId + " and vpn gateway id " + vpnGatewayId +
341+ " already existed!" );
342+ }
343+ }
344+
345+ private void validatePrerequisiteVpnGateway (Site2SiteVpnGateway vpnGateway ) {
346+ // check if gateway has been defined on the VPC
347+ if (_vpnGatewayDao .findByVpcId (vpnGateway .getVpcId ()) == null ) {
348+ throw new InvalidParameterValueException ("we can not create a VPN connection for a VPC that does not have a VPN gateway defined" );
349+ }
350+ }
351+
327352 @ Override
328353 @ DB
329354 @ ActionEvent (eventType = EventTypes .EVENT_S2S_VPN_CONNECTION_CREATE , eventDescription = "starting s2s vpn connection" , async = true )
330355 public Site2SiteVpnConnection startVpnConnection (long id ) throws ResourceUnavailableException {
331356 Site2SiteVpnConnectionVO conn = _vpnConnectionDao .acquireInLockTable (id );
332357 if (conn == null ) {
333- throw new CloudRuntimeException ("Unable to acquire lock on " + conn );
358+ throw new CloudRuntimeException ("Unable to acquire lock for starting of VPN connection with ID " + id );
334359 }
335360 try {
336361 if (conn .getState () != State .Pending && conn .getState () != State .Disconnected ) {
@@ -380,19 +405,15 @@ public boolean deleteCustomerGateway(DeleteVpnCustomerGatewayCmd cmd) {
380405 Account caller = CallContext .current ().getCallingAccount ();
381406
382407 Long id = cmd .getId ();
383- Site2SiteCustomerGateway customerGateway = _customerGatewayDao .findById (id );
384- if (customerGateway == null ) {
385- throw new InvalidParameterValueException ("Fail to find customer gateway with " + id + " !" );
386- }
387- _accountMgr .checkAccess (caller , null , false , customerGateway );
408+ Site2SiteCustomerGateway customerGateway = getAndValidateSite2SiteCustomerGateway (id , caller );
388409
389410 return doDeleteCustomerGateway (customerGateway );
390411 }
391412
392413 protected boolean doDeleteCustomerGateway (Site2SiteCustomerGateway gw ) {
393414 long id = gw .getId ();
394415 List <Site2SiteVpnConnectionVO > vpnConnections = _vpnConnectionDao .listByCustomerGatewayId (id );
395- if (vpnConnections != null && vpnConnections . size () != 0 ) {
416+ if (! CollectionUtils . isEmpty ( vpnConnections ) ) {
396417 throw new InvalidParameterValueException ("Unable to delete VPN customer gateway with id " + id + " because there is still related VPN connections!" );
397418 }
398419 annotationDao .removeByEntityType (AnnotationService .EntityType .VPN_CUSTOMER_GATEWAY .name (), gw .getUuid ());
@@ -402,7 +423,7 @@ protected boolean doDeleteCustomerGateway(Site2SiteCustomerGateway gw) {
402423
403424 protected void doDeleteVpnGateway (Site2SiteVpnGateway gw ) {
404425 List <Site2SiteVpnConnectionVO > conns = _vpnConnectionDao .listByVpnGatewayId (gw .getId ());
405- if (conns != null && conns . size () != 0 ) {
426+ if (! CollectionUtils . isEmpty ( conns ) ) {
406427 throw new InvalidParameterValueException ("Unable to delete VPN gateway " + gw .getId () + " because there is still related VPN connections!" );
407428 }
408429 _vpnGatewayDao .remove (gw .getId ());
@@ -415,12 +436,7 @@ public boolean deleteVpnGateway(DeleteVpnGatewayCmd cmd) {
415436 Account caller = CallContext .current ().getCallingAccount ();
416437
417438 Long id = cmd .getId ();
418- Site2SiteVpnGateway vpnGateway = _vpnGatewayDao .findById (id );
419- if (vpnGateway == null ) {
420- throw new InvalidParameterValueException ("Fail to find vpn gateway with " + id + " !" );
421- }
422-
423- _accountMgr .checkAccess (caller , null , false , vpnGateway );
439+ Site2SiteVpnGateway vpnGateway = getAndValidateSite2SiteVpnGateway (id , caller );
424440
425441 doDeleteVpnGateway (vpnGateway );
426442 return true ;
@@ -576,7 +592,7 @@ public boolean deleteVpnConnection(DeleteVpnConnectionCmd cmd) throws ResourceUn
576592 private void stopVpnConnection (Long id ) throws ResourceUnavailableException {
577593 Site2SiteVpnConnectionVO conn = _vpnConnectionDao .acquireInLockTable (id );
578594 if (conn == null ) {
579- throw new CloudRuntimeException ("Unable to acquire lock on " + conn );
595+ throw new CloudRuntimeException ("Unable to acquire lock for stopping of VPN connection with ID " + id );
580596 }
581597 try {
582598 if (conn .getState () == State .Pending ) {
@@ -637,10 +653,9 @@ public Pair<List<? extends Site2SiteCustomerGateway>, Integer> searchForCustomer
637653 String keyword = cmd .getKeyword ();
638654
639655 Account caller = CallContext .current ().getCallingAccount ();
640- List <Long > permittedAccounts = new ArrayList <Long >();
656+ List <Long > permittedAccounts = new ArrayList <>();
641657
642- Ternary <Long , Boolean , ListProjectResourcesCriteria > domainIdRecursiveListProject = new Ternary <Long , Boolean ,
643- ListProjectResourcesCriteria >(domainId , isRecursive , null );
658+ Ternary <Long , Boolean , ListProjectResourcesCriteria > domainIdRecursiveListProject = new Ternary <>(domainId , isRecursive , null );
644659 _accountMgr .buildACLSearchParameters (caller , id , accountName , cmd .getProjectId (), permittedAccounts , domainIdRecursiveListProject , listAll , false );
645660 domainId = domainIdRecursiveListProject .first ();
646661 isRecursive = domainIdRecursiveListProject .second ();
@@ -664,7 +679,7 @@ public Pair<List<? extends Site2SiteCustomerGateway>, Integer> searchForCustomer
664679 }
665680
666681 Pair <List <Site2SiteCustomerGatewayVO >, Integer > result = _customerGatewayDao .searchAndCount (sc , searchFilter );
667- return new Pair <List <? extends Site2SiteCustomerGateway >, Integer >(result .first (), result .second ());
682+ return new Pair <>(result .first (), result .second ());
668683 }
669684
670685 @ Override
@@ -681,10 +696,9 @@ public Pair<List<? extends Site2SiteVpnGateway>, Integer> searchForVpnGateways(L
681696 long pageSizeVal = cmd .getPageSizeVal ();
682697
683698 Account caller = CallContext .current ().getCallingAccount ();
684- List <Long > permittedAccounts = new ArrayList <Long >();
699+ List <Long > permittedAccounts = new ArrayList <>();
685700
686- Ternary <Long , Boolean , ListProjectResourcesCriteria > domainIdRecursiveListProject = new Ternary <Long , Boolean ,
687- ListProjectResourcesCriteria >(domainId , isRecursive , null );
701+ Ternary <Long , Boolean , ListProjectResourcesCriteria > domainIdRecursiveListProject = new Ternary <>(domainId , isRecursive , null );
688702 _accountMgr .buildACLSearchParameters (caller , id , accountName , cmd .getProjectId (), permittedAccounts , domainIdRecursiveListProject , listAll , false );
689703 domainId = domainIdRecursiveListProject .first ();
690704 isRecursive = domainIdRecursiveListProject .second ();
@@ -714,7 +728,7 @@ public Pair<List<? extends Site2SiteVpnGateway>, Integer> searchForVpnGateways(L
714728 }
715729
716730 Pair <List <Site2SiteVpnGatewayVO >, Integer > result = _vpnGatewayDao .searchAndCount (sc , searchFilter );
717- return new Pair <List <? extends Site2SiteVpnGateway >, Integer >(result .first (), result .second ());
731+ return new Pair <>(result .first (), result .second ());
718732 }
719733
720734 @ Override
@@ -731,10 +745,9 @@ public Pair<List<? extends Site2SiteVpnConnection>, Integer> searchForVpnConnect
731745 long pageSizeVal = cmd .getPageSizeVal ();
732746
733747 Account caller = CallContext .current ().getCallingAccount ();
734- List <Long > permittedAccounts = new ArrayList <Long >();
748+ List <Long > permittedAccounts = new ArrayList <>();
735749
736- Ternary <Long , Boolean , ListProjectResourcesCriteria > domainIdRecursiveListProject = new Ternary <Long , Boolean ,
737- ListProjectResourcesCriteria >(domainId , isRecursive , null );
750+ Ternary <Long , Boolean , ListProjectResourcesCriteria > domainIdRecursiveListProject = new Ternary <>(domainId , isRecursive , null );
738751 _accountMgr .buildACLSearchParameters (caller , id , accountName , cmd .getProjectId (), permittedAccounts , domainIdRecursiveListProject , listAll , false );
739752 domainId = domainIdRecursiveListProject .first ();
740753 isRecursive = domainIdRecursiveListProject .second ();
@@ -768,7 +781,7 @@ public Pair<List<? extends Site2SiteVpnConnection>, Integer> searchForVpnConnect
768781 }
769782
770783 Pair <List <Site2SiteVpnConnectionVO >, Integer > result = _vpnConnectionDao .searchAndCount (sc , searchFilter );
771- return new Pair <List <? extends Site2SiteVpnConnection >, Integer >(result .first (), result .second ());
784+ return new Pair <>(result .first (), result .second ());
772785 }
773786
774787 @ Override
@@ -815,7 +828,7 @@ public void markDisconnectVpnConnByVpc(long vpcId) {
815828
816829 @ Override
817830 public List <Site2SiteVpnConnectionVO > getConnectionsForRouter (DomainRouterVO router ) {
818- List <Site2SiteVpnConnectionVO > conns = new ArrayList <Site2SiteVpnConnectionVO >();
831+ List <Site2SiteVpnConnectionVO > conns = new ArrayList <>();
819832 // One router for one VPC
820833 Long vpcId = router .getVpcId ();
821834 if (router .getVpcId () == null ) {
@@ -828,7 +841,6 @@ public List<Site2SiteVpnConnectionVO> getConnectionsForRouter(DomainRouterVO rou
828841 @ Override
829842 public boolean deleteCustomerGatewayByAccount (long accountId ) {
830843 boolean result = true ;
831- ;
832844 List <Site2SiteCustomerGatewayVO > gws = _customerGatewayDao .listByAccountId (accountId );
833845 for (Site2SiteCustomerGatewayVO gw : gws ) {
834846 result = result & doDeleteCustomerGateway (gw );
0 commit comments