4646import com .cloud .exception .InvalidParameterValueException ;
4747import com .cloud .exception .PermissionDeniedException ;
4848import com .cloud .exception .ResourceUnavailableException ;
49+ import com .cloud .network .NetworkManager ;
50+ import com .cloud .network .dao .NetworkDomainDao ;
4951import com .cloud .projects .ProjectManager ;
5052import com .cloud .projects .ProjectVO ;
5153import com .cloud .projects .dao .ProjectDao ;
6365import com .cloud .utils .db .Transaction ;
6466import com .cloud .utils .exception .CloudRuntimeException ;
6567import com .cloud .utils .net .NetUtils ;
68+ import com .cloud .vm .ReservationContext ;
69+ import com .cloud .vm .ReservationContextImpl ;
6670
6771@ Component
6872@ Local (value = { DomainManager .class , DomainService .class })
@@ -91,6 +95,10 @@ public class DomainManagerImpl extends ManagerBase implements DomainManager, Dom
9195 private ResourceLimitDao _resourceLimitDao ;
9296 @ Inject
9397 private DedicatedResourceDao _dedicatedDao ;
98+ @ Inject
99+ private NetworkManager _networkMgr ;
100+ @ Inject
101+ private NetworkDomainDao _networkDomainDao ;
94102
95103 @ Override
96104 public Domain getDomain (long domainId ) {
@@ -229,13 +237,16 @@ public boolean deleteDomain(DomainVO domain, Boolean cleanup) {
229237 long ownerId = domain .getAccountId ();
230238 if ((cleanup != null ) && cleanup .booleanValue ()) {
231239 if (!cleanupDomain (domain .getId (), ownerId )) {
240+ rollBackState =true ;
232241 CloudRuntimeException e = new CloudRuntimeException ("Failed to clean up domain resources and sub domains, delete failed on domain " + domain .getName () + " (id: " + domain .getId () + ")." );
233242 e .addProxyObject (domain .getUuid (), "domainId" );
234243 throw e ;
235244 }
236245 } else {
246+ //don't delete the domain if there are accounts set for cleanup, or non-removed networks exist
247+ List <Long > networkIds = _networkDomainDao .listNetworkIdsByDomain (domain .getId ());
237248 List <AccountVO > accountsForCleanup = _accountDao .findCleanupsForRemovedAccounts (domain .getId ());
238- if (accountsForCleanup .isEmpty ()) {
249+ if (accountsForCleanup .isEmpty () && networkIds . isEmpty () ) {
239250 if (!_domainDao .remove (domain .getId ())) {
240251 rollBackState = true ;
241252 CloudRuntimeException e = new CloudRuntimeException ("Delete failed on domain " + domain .getName () + " (id: " + domain .getId () + "); Please make sure all users and sub domains have been removed from the domain before deleting" );
@@ -255,7 +266,14 @@ public boolean deleteDomain(DomainVO domain, Boolean cleanup) {
255266 }
256267 } else {
257268 rollBackState = true ;
258- CloudRuntimeException e = new CloudRuntimeException ("Can't delete the domain yet because it has " + accountsForCleanup .size () + "accounts that need a cleanup" );
269+ String msg = null ;
270+ if (!accountsForCleanup .isEmpty ()) {
271+ msg = accountsForCleanup .size () + " accounts to cleanup" ;
272+ } else if (!networkIds .isEmpty ()) {
273+ msg = networkIds .size () + " non-removed networks" ;
274+ }
275+
276+ CloudRuntimeException e = new CloudRuntimeException ("Can't delete the domain yet because it has " + msg );
259277 e .addProxyObject (domain .getUuid (), "domainId" );
260278 throw e ;
261279 }
@@ -330,19 +348,44 @@ private boolean cleanupDomain(Long domainId, Long ownerId) throws ConcurrentOper
330348 for (AccountVO account : accounts ) {
331349 if (account .getType () != Account .ACCOUNT_TYPE_PROJECT ) {
332350 s_logger .debug ("Deleting account " + account + " as a part of domain id=" + domainId + " cleanup" );
333- success = ( success && _accountMgr .deleteAccount (account , UserContext .current ().getCallerUserId (), UserContext .current ().getCaller () ));
334- if (!success ) {
351+ boolean deleteAccount = _accountMgr .deleteAccount (account , UserContext .current ().getCallerUserId (), UserContext .current ().getCaller ());
352+ if (!deleteAccount ) {
335353 s_logger .warn ("Failed to cleanup account id=" + account .getId () + " as a part of domain cleanup" );
336354 }
355+ success = (success && deleteAccount );
337356 } else {
338357 ProjectVO project = _projectDao .findByProjectAccountId (account .getId ());
339358 s_logger .debug ("Deleting project " + project + " as a part of domain id=" + domainId + " cleanup" );
340- success = ( success && _projectMgr .deleteProject (UserContext .current ().getCaller (), UserContext .current ().getCallerUserId (), project ) );
341- if (!success ) {
359+ boolean deleteProject = _projectMgr .deleteProject (UserContext .current ().getCaller (), UserContext .current ().getCallerUserId (), project );
360+ if (!deleteProject ) {
342361 s_logger .warn ("Failed to cleanup project " + project + " as a part of domain cleanup" );
343362 }
363+ success = (success && deleteProject );
344364 }
345365 }
366+
367+ //delete the domain shared networks
368+ boolean networksDeleted = true ;
369+ s_logger .debug ("Deleting networks for domain id=" + domainId );
370+ List <Long > networkIds = _networkDomainDao .listNetworkIdsByDomain (domainId );
371+ UserContext ctx = UserContext .current ();
372+ ReservationContext context = new ReservationContextImpl (null , null , _accountMgr .getActiveUser (ctx .getCallerUserId ()), ctx .getCaller ());
373+ for (Long networkId : networkIds ) {
374+ s_logger .debug ("Deleting network id=" + networkId + " as a part of domain id=" + domainId + " cleanup" );
375+
376+ if (!_networkMgr .destroyNetwork (networkId , context )) {
377+ s_logger .warn ("Unable to destroy network id=" + networkId + " as a part of domain id=" + domainId + " cleanup." );
378+ networksDeleted = false ;
379+ } else {
380+ s_logger .debug ("Network " + networkId + " successfully deleted as a part of domain id=" + domainId + " cleanup." );
381+ }
382+ }
383+
384+ //don't proceed if networks failed to cleanup. The cleanup will be performed for inactive domain once again
385+ if (!networksDeleted ) {
386+ s_logger .debug ("Failed to delete the shared networks as a part of domain id=" + domainId + " clenaup" );
387+ return false ;
388+ }
346389
347390 // don't remove the domain if there are accounts required cleanup
348391 boolean deleteDomainSuccess = true ;
@@ -366,6 +409,7 @@ private boolean cleanupDomain(Long domainId, Long ownerId) throws ConcurrentOper
366409 _resourceLimitDao .removeEntriesByOwner (domainId , ResourceOwnerType .Domain );
367410 } else {
368411 s_logger .debug ("Can't delete the domain yet because it has " + accountsForCleanup .size () + "accounts that need a cleanup" );
412+ return false ;
369413 }
370414
371415 return success && deleteDomainSuccess ;
0 commit comments