23
23
import javax .inject .Inject ;
24
24
import javax .naming .ConfigurationException ;
25
25
26
- import org .apache .cloudstack .annotation .AnnotationService ;
27
- import org .apache .cloudstack .annotation .dao .AnnotationDao ;
26
+ import org .apache .commons .collections .CollectionUtils ;
28
27
import org .springframework .stereotype .Component ;
29
28
29
+ import org .apache .cloudstack .annotation .AnnotationService ;
30
+ import org .apache .cloudstack .annotation .dao .AnnotationDao ;
30
31
import org .apache .cloudstack .api .command .user .vpn .CreateVpnConnectionCmd ;
31
32
import org .apache .cloudstack .api .command .user .vpn .CreateVpnCustomerGatewayCmd ;
32
33
import org .apache .cloudstack .api .command .user .vpn .CreateVpnGatewayCmd ;
45
46
import com .cloud .event .ActionEvent ;
46
47
import com .cloud .event .EventTypes ;
47
48
import com .cloud .exception .InvalidParameterValueException ;
48
- import com .cloud .exception .NetworkRuleConflictException ;
49
49
import com .cloud .exception .PermissionDeniedException ;
50
50
import com .cloud .exception .ResourceUnavailableException ;
51
51
import com .cloud .network .Site2SiteCustomerGateway ;
@@ -106,7 +106,6 @@ public class Site2SiteVpnManagerImpl extends ManagerBase implements Site2SiteVpn
106
106
@ Inject
107
107
private AnnotationDao annotationDao ;
108
108
109
- String _name ;
110
109
int _connLimit ;
111
110
int _subnetsLimit ;
112
111
@@ -253,35 +252,23 @@ public Site2SiteCustomerGateway createCustomerGateway(CreateVpnCustomerGatewayCm
253
252
254
253
@ Override
255
254
@ 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 ) {
257
256
Account caller = CallContext .current ().getCallingAccount ();
258
257
Account owner = _accountMgr .getAccount (cmd .getEntityOwnerId ());
259
258
260
259
//Verify that caller can perform actions in behalf of vpc owner
261
260
_accountMgr .checkAccess (caller , null , false , owner );
262
261
263
262
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 );
269
264
270
265
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 );
276
267
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 );
280
271
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
- }
285
272
String [] cidrList = customerGateway .getGuestCidrList ().split ("," );
286
273
287
274
// Remote sub nets cannot overlap VPC's sub net
@@ -324,13 +311,51 @@ public Site2SiteVpnConnection createVpnConnection(CreateVpnConnectionCmd cmd) th
324
311
return conn ;
325
312
}
326
313
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
+
327
352
@ Override
328
353
@ DB
329
354
@ ActionEvent (eventType = EventTypes .EVENT_S2S_VPN_CONNECTION_CREATE , eventDescription = "starting s2s vpn connection" , async = true )
330
355
public Site2SiteVpnConnection startVpnConnection (long id ) throws ResourceUnavailableException {
331
356
Site2SiteVpnConnectionVO conn = _vpnConnectionDao .acquireInLockTable (id );
332
357
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 );
334
359
}
335
360
try {
336
361
if (conn .getState () != State .Pending && conn .getState () != State .Disconnected ) {
@@ -380,19 +405,15 @@ public boolean deleteCustomerGateway(DeleteVpnCustomerGatewayCmd cmd) {
380
405
Account caller = CallContext .current ().getCallingAccount ();
381
406
382
407
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 );
388
409
389
410
return doDeleteCustomerGateway (customerGateway );
390
411
}
391
412
392
413
protected boolean doDeleteCustomerGateway (Site2SiteCustomerGateway gw ) {
393
414
long id = gw .getId ();
394
415
List <Site2SiteVpnConnectionVO > vpnConnections = _vpnConnectionDao .listByCustomerGatewayId (id );
395
- if (vpnConnections != null && vpnConnections . size () != 0 ) {
416
+ if (! CollectionUtils . isEmpty ( vpnConnections ) ) {
396
417
throw new InvalidParameterValueException ("Unable to delete VPN customer gateway with id " + id + " because there is still related VPN connections!" );
397
418
}
398
419
annotationDao .removeByEntityType (AnnotationService .EntityType .VPN_CUSTOMER_GATEWAY .name (), gw .getUuid ());
@@ -402,7 +423,7 @@ protected boolean doDeleteCustomerGateway(Site2SiteCustomerGateway gw) {
402
423
403
424
protected void doDeleteVpnGateway (Site2SiteVpnGateway gw ) {
404
425
List <Site2SiteVpnConnectionVO > conns = _vpnConnectionDao .listByVpnGatewayId (gw .getId ());
405
- if (conns != null && conns . size () != 0 ) {
426
+ if (! CollectionUtils . isEmpty ( conns ) ) {
406
427
throw new InvalidParameterValueException ("Unable to delete VPN gateway " + gw .getId () + " because there is still related VPN connections!" );
407
428
}
408
429
_vpnGatewayDao .remove (gw .getId ());
@@ -415,12 +436,7 @@ public boolean deleteVpnGateway(DeleteVpnGatewayCmd cmd) {
415
436
Account caller = CallContext .current ().getCallingAccount ();
416
437
417
438
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 );
424
440
425
441
doDeleteVpnGateway (vpnGateway );
426
442
return true ;
@@ -576,7 +592,7 @@ public boolean deleteVpnConnection(DeleteVpnConnectionCmd cmd) throws ResourceUn
576
592
private void stopVpnConnection (Long id ) throws ResourceUnavailableException {
577
593
Site2SiteVpnConnectionVO conn = _vpnConnectionDao .acquireInLockTable (id );
578
594
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 );
580
596
}
581
597
try {
582
598
if (conn .getState () == State .Pending ) {
@@ -637,10 +653,9 @@ public Pair<List<? extends Site2SiteCustomerGateway>, Integer> searchForCustomer
637
653
String keyword = cmd .getKeyword ();
638
654
639
655
Account caller = CallContext .current ().getCallingAccount ();
640
- List <Long > permittedAccounts = new ArrayList <Long >();
656
+ List <Long > permittedAccounts = new ArrayList <>();
641
657
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 );
644
659
_accountMgr .buildACLSearchParameters (caller , id , accountName , cmd .getProjectId (), permittedAccounts , domainIdRecursiveListProject , listAll , false );
645
660
domainId = domainIdRecursiveListProject .first ();
646
661
isRecursive = domainIdRecursiveListProject .second ();
@@ -665,7 +680,7 @@ public Pair<List<? extends Site2SiteCustomerGateway>, Integer> searchForCustomer
665
680
}
666
681
667
682
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 ());
669
684
}
670
685
671
686
@ Override
@@ -682,10 +697,9 @@ public Pair<List<? extends Site2SiteVpnGateway>, Integer> searchForVpnGateways(L
682
697
long pageSizeVal = cmd .getPageSizeVal ();
683
698
684
699
Account caller = CallContext .current ().getCallingAccount ();
685
- List <Long > permittedAccounts = new ArrayList <Long >();
700
+ List <Long > permittedAccounts = new ArrayList <>();
686
701
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 );
689
703
_accountMgr .buildACLSearchParameters (caller , id , accountName , cmd .getProjectId (), permittedAccounts , domainIdRecursiveListProject , listAll , false );
690
704
domainId = domainIdRecursiveListProject .first ();
691
705
isRecursive = domainIdRecursiveListProject .second ();
@@ -715,7 +729,7 @@ public Pair<List<? extends Site2SiteVpnGateway>, Integer> searchForVpnGateways(L
715
729
}
716
730
717
731
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 ());
719
733
}
720
734
721
735
@ Override
@@ -732,10 +746,9 @@ public Pair<List<? extends Site2SiteVpnConnection>, Integer> searchForVpnConnect
732
746
long pageSizeVal = cmd .getPageSizeVal ();
733
747
734
748
Account caller = CallContext .current ().getCallingAccount ();
735
- List <Long > permittedAccounts = new ArrayList <Long >();
749
+ List <Long > permittedAccounts = new ArrayList <>();
736
750
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 );
739
752
_accountMgr .buildACLSearchParameters (caller , id , accountName , cmd .getProjectId (), permittedAccounts , domainIdRecursiveListProject , listAll , false );
740
753
domainId = domainIdRecursiveListProject .first ();
741
754
isRecursive = domainIdRecursiveListProject .second ();
@@ -769,7 +782,7 @@ public Pair<List<? extends Site2SiteVpnConnection>, Integer> searchForVpnConnect
769
782
}
770
783
771
784
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 ());
773
786
}
774
787
775
788
@ Override
@@ -816,7 +829,7 @@ public void markDisconnectVpnConnByVpc(long vpcId) {
816
829
817
830
@ Override
818
831
public List <Site2SiteVpnConnectionVO > getConnectionsForRouter (DomainRouterVO router ) {
819
- List <Site2SiteVpnConnectionVO > conns = new ArrayList <Site2SiteVpnConnectionVO >();
832
+ List <Site2SiteVpnConnectionVO > conns = new ArrayList <>();
820
833
// One router for one VPC
821
834
Long vpcId = router .getVpcId ();
822
835
if (router .getVpcId () == null ) {
@@ -829,7 +842,6 @@ public List<Site2SiteVpnConnectionVO> getConnectionsForRouter(DomainRouterVO rou
829
842
@ Override
830
843
public boolean deleteCustomerGatewayByAccount (long accountId ) {
831
844
boolean result = true ;
832
- ;
833
845
List <Site2SiteCustomerGatewayVO > gws = _customerGatewayDao .listByAccountId (accountId );
834
846
for (Site2SiteCustomerGatewayVO gw : gws ) {
835
847
result = result & doDeleteCustomerGateway (gw );
0 commit comments