259259import com .googlecode .ipv6 .IPv6Address ;
260260import org .jetbrains .annotations .NotNull ;
261261
262+ import static com .cloud .configuration .ConfigurationManager .MESSAGE_DELETE_VLAN_IP_RANGE_EVENT ;
263+
262264/**
263265 * NetworkManagerImpl implements NetworkManager.
264266 */
@@ -3324,17 +3326,17 @@ public boolean destroyNetwork(final long networkId, final ReservationContext con
33243326
33253327 final NetworkVO networkFinal = network ;
33263328 try {
3327- Transaction .execute (new TransactionCallbackNoReturn () {
3329+ final List < VlanVO > deletedVlanRangeToPublish = Transaction .execute (new TransactionCallback < List < VlanVO >> () {
33283330 @ Override
3329- public void doInTransactionWithoutResult ( final TransactionStatus status ) {
3331+ public List < VlanVO > doInTransaction ( TransactionStatus status ) {
33303332 final NetworkGuru guru = AdapterBase .getAdapterByName (networkGurus , networkFinal .getGuruName ());
33313333
33323334 if (!guru .trash (networkFinal , _networkOfferingDao .findById (networkFinal .getNetworkOfferingId ()))) {
33333335 throw new CloudRuntimeException ("Failed to trash network." );
33343336 }
3335-
3336- if (!deleteVlansInNetwork ( networkFinal , context . getCaller (). getId (), callerAccount )) {
3337- logger .warn ("Failed to delete network {} ; was unable to cleanup corresponding ip ranges" , networkFinal );
3337+ Pair < Boolean , List < VlanVO >> deletedVlans = deleteVlansInNetwork ( networkFinal , context . getCaller (). getId (), callerAccount );
3338+ if (!deletedVlans . first ( )) {
3339+ logger .warn ("Failed to delete network " + networkFinal + " ; was unable to cleanup corresponding ip ranges" );
33383340 throw new CloudRuntimeException ("Failed to delete network " + networkFinal + "; was unable to cleanup corresponding ip ranges" );
33393341 } else {
33403342 // commit transaction only when ips and vlans for the network are released successfully
@@ -3367,8 +3369,10 @@ public void doInTransactionWithoutResult(final TransactionStatus status) {
33673369 _resourceLimitMgr .decrementResourceCount (networkFinal .getAccountId (), ResourceType .network , networkFinal .getDisplayNetwork ());
33683370 }
33693371 }
3372+ return deletedVlans .second ();
33703373 }
33713374 });
3375+ publishDeletedVlanRanges (deletedVlanRangeToPublish );
33723376 if (_networksDao .findById (network .getId ()) == null ) {
33733377 // remove its related ACL permission
33743378 final Pair <Class <?>, Long > networkMsg = new Pair <Class <?>, Long >(Network .class , networkFinal .getId ());
@@ -3386,22 +3390,34 @@ public void doInTransactionWithoutResult(final TransactionStatus status) {
33863390 return success ;
33873391 }
33883392
3393+ private void publishDeletedVlanRanges (List <VlanVO > deletedVlanRangeToPublish ) {
3394+ if (CollectionUtils .isNotEmpty (deletedVlanRangeToPublish )) {
3395+ for (VlanVO vlan : deletedVlanRangeToPublish ) {
3396+ _messageBus .publish (_name , MESSAGE_DELETE_VLAN_IP_RANGE_EVENT , PublishScope .LOCAL , vlan );
3397+ }
3398+ }
3399+ }
3400+
33893401 @ Override
33903402 public boolean resourceCountNeedsUpdate (final NetworkOffering ntwkOff , final ACLType aclType ) {
33913403 //Update resource count only for Isolated account specific non-system networks
33923404 final boolean updateResourceCount = ntwkOff .getGuestType () == GuestType .Isolated && !ntwkOff .isSystemOnly () && aclType == ACLType .Account ;
33933405 return updateResourceCount ;
33943406 }
33953407
3396- protected boolean deleteVlansInNetwork (final NetworkVO network , final long userId , final Account callerAccount ) {
3408+ protected Pair < Boolean , List < VlanVO >> deleteVlansInNetwork (final NetworkVO network , final long userId , final Account callerAccount ) {
33973409 final long networkId = network .getId ();
33983410 //cleanup Public vlans
33993411 final List <VlanVO > publicVlans = _vlanDao .listVlansByNetworkId (networkId );
3412+ List <VlanVO > deletedPublicVlanRange = new ArrayList <>();
34003413 boolean result = true ;
34013414 for (final VlanVO vlan : publicVlans ) {
3402- if (!_configMgr .deleteVlanAndPublicIpRange (userId , vlan .getId (), callerAccount )) {
3403- logger .warn ("Failed to delete vlan {});" , vlan .getId ());
3415+ VlanVO vlanRange = _configMgr .deleteVlanAndPublicIpRange (userId , vlan .getId (), callerAccount );
3416+ if (vlanRange == null ) {
3417+ logger .warn ("Failed to delete vlan " + vlan .getId () + ");" );
34043418 result = false ;
3419+ } else {
3420+ deletedPublicVlanRange .add (vlanRange );
34053421 }
34063422 }
34073423
@@ -3421,7 +3437,7 @@ protected boolean deleteVlansInNetwork(final NetworkVO network, final long userI
34213437 _dcDao .releaseVnet (BroadcastDomainType .getValue (network .getBroadcastUri ()), network .getDataCenterId (),
34223438 network .getPhysicalNetworkId (), network .getAccountId (), network .getReservationId ());
34233439 }
3424- return result ;
3440+ return new Pair <>( result , deletedPublicVlanRange ) ;
34253441 }
34263442
34273443 public class NetworkGarbageCollector extends ManagedContextRunnable {
0 commit comments