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 ();
@@ -665,7 +680,7 @@ public Pair<List<? extends Site2SiteCustomerGateway>, Integer> searchForCustomer
665680 }
666681
667682 Pair <List <Site2SiteCustomerGatewayVO >, Integer > result = _customerGatewayDao .searchAndCount (sc , searchFilter );
668- return new Pair <List <? extends Site2SiteCustomerGateway >, Integer >(result .first (), result .second ());
683+ return new Pair <>(result .first (), result .second ());
669684 }
670685
671686 @ Override
@@ -682,10 +697,9 @@ public Pair<List<? extends Site2SiteVpnGateway>, Integer> searchForVpnGateways(L
682697 long pageSizeVal = cmd .getPageSizeVal ();
683698
684699 Account caller = CallContext .current ().getCallingAccount ();
685- List <Long > permittedAccounts = new ArrayList <Long >();
700+ List <Long > permittedAccounts = new ArrayList <>();
686701
687- Ternary <Long , Boolean , ListProjectResourcesCriteria > domainIdRecursiveListProject = new Ternary <Long , Boolean ,
688- ListProjectResourcesCriteria >(domainId , isRecursive , null );
702+ Ternary <Long , Boolean , ListProjectResourcesCriteria > domainIdRecursiveListProject = new Ternary <>(domainId , isRecursive , null );
689703 _accountMgr .buildACLSearchParameters (caller , id , accountName , cmd .getProjectId (), permittedAccounts , domainIdRecursiveListProject , listAll , false );
690704 domainId = domainIdRecursiveListProject .first ();
691705 isRecursive = domainIdRecursiveListProject .second ();
@@ -715,7 +729,7 @@ public Pair<List<? extends Site2SiteVpnGateway>, Integer> searchForVpnGateways(L
715729 }
716730
717731 Pair <List <Site2SiteVpnGatewayVO >, Integer > result = _vpnGatewayDao .searchAndCount (sc , searchFilter );
718- return new Pair <List <? extends Site2SiteVpnGateway >, Integer >(result .first (), result .second ());
732+ return new Pair <>(result .first (), result .second ());
719733 }
720734
721735 @ Override
@@ -732,10 +746,9 @@ public Pair<List<? extends Site2SiteVpnConnection>, Integer> searchForVpnConnect
732746 long pageSizeVal = cmd .getPageSizeVal ();
733747
734748 Account caller = CallContext .current ().getCallingAccount ();
735- List <Long > permittedAccounts = new ArrayList <Long >();
749+ List <Long > permittedAccounts = new ArrayList <>();
736750
737- Ternary <Long , Boolean , ListProjectResourcesCriteria > domainIdRecursiveListProject = new Ternary <Long , Boolean ,
738- ListProjectResourcesCriteria >(domainId , isRecursive , null );
751+ Ternary <Long , Boolean , ListProjectResourcesCriteria > domainIdRecursiveListProject = new Ternary <>(domainId , isRecursive , null );
739752 _accountMgr .buildACLSearchParameters (caller , id , accountName , cmd .getProjectId (), permittedAccounts , domainIdRecursiveListProject , listAll , false );
740753 domainId = domainIdRecursiveListProject .first ();
741754 isRecursive = domainIdRecursiveListProject .second ();
@@ -769,7 +782,7 @@ public Pair<List<? extends Site2SiteVpnConnection>, Integer> searchForVpnConnect
769782 }
770783
771784 Pair <List <Site2SiteVpnConnectionVO >, Integer > result = _vpnConnectionDao .searchAndCount (sc , searchFilter );
772- return new Pair <List <? extends Site2SiteVpnConnection >, Integer >(result .first (), result .second ());
785+ return new Pair <>(result .first (), result .second ());
773786 }
774787
775788 @ Override
@@ -816,7 +829,7 @@ public void markDisconnectVpnConnByVpc(long vpcId) {
816829
817830 @ Override
818831 public List <Site2SiteVpnConnectionVO > getConnectionsForRouter (DomainRouterVO router ) {
819- List <Site2SiteVpnConnectionVO > conns = new ArrayList <Site2SiteVpnConnectionVO >();
832+ List <Site2SiteVpnConnectionVO > conns = new ArrayList <>();
820833 // One router for one VPC
821834 Long vpcId = router .getVpcId ();
822835 if (router .getVpcId () == null ) {
@@ -829,7 +842,6 @@ public List<Site2SiteVpnConnectionVO> getConnectionsForRouter(DomainRouterVO rou
829842 @ Override
830843 public boolean deleteCustomerGatewayByAccount (long accountId ) {
831844 boolean result = true ;
832- ;
833845 List <Site2SiteCustomerGatewayVO > gws = _customerGatewayDao .listByAccountId (accountId );
834846 for (Site2SiteCustomerGatewayVO gw : gws ) {
835847 result = result & doDeleteCustomerGateway (gw );
0 commit comments