@@ -440,8 +440,8 @@ public boolean applyIpAssociations(Network network, boolean continueOnError) thr
440440 List <PublicIp > publicIps = new ArrayList <PublicIp >();
441441 if (userIps != null && !userIps .isEmpty ()) {
442442 for (IPAddressVO userIp : userIps ) {
443- PublicIp publicIp = new PublicIp (userIp , _vlanDao .findById (userIp .getVlanId ()), userIp .getMacAddress ());
444- publicIps .add (publicIp );
443+ PublicIp publicIp = new PublicIp (userIp , _vlanDao .findById (userIp .getVlanId ()), userIp .getMacAddress ());
444+ publicIps .add (publicIp );
445445 }
446446 }
447447
@@ -468,7 +468,13 @@ public boolean applyIpAssociations(Network network, boolean continueOnError) thr
468468 markPublicIpAsAllocated (addr );
469469
470470 } else if (addr .getState () == IpAddress .State .Releasing ) {
471- unassignPublicIpAddress (addr );
471+ //Cleanup all the resources for ip address if there are any, and only then unassign ip in the system
472+ if (cleanupIpResources (addr .getId (), Account .ACCOUNT_ID_SYSTEM , _accountMgr .getSystemAccount ())) {
473+ unassignPublicIpAddress (addr );
474+ } else {
475+ success = false ;
476+ s_logger .warn ("Failed to release resources for ip address id=" + addr .getId ());
477+ }
472478 }
473479 }
474480 }
@@ -608,8 +614,10 @@ public IpAddress associateIP(AssociateIPAddrCmd cmd) throws ResourceAllocationEx
608614
609615 @ Override
610616 public boolean releasePublicIpAddress (long addrId , long userId , Account caller ) {
617+
618+ //mark ip address as Releasing
611619 IPAddressVO ip = _ipAddressDao .markAsUnavailable (addrId );
612- assert (ip != null ) : "Unable to mark the ip address id=" + addrId + " owned by " + ip . getAccountId () + " as unavailable." ;
620+ assert (ip != null ) : "Unable to mark the ip address id=" + addrId + " as unavailable." ;
613621 if (ip == null ) {
614622 return true ;
615623 }
@@ -619,30 +627,24 @@ public boolean releasePublicIpAddress(long addrId, long userId, Account caller)
619627 }
620628
621629 boolean success = true ;
622- try {
623- if (!_rulesMgr .revokeAllRules (addrId , userId , caller )) {
624- s_logger .warn ("Unable to revoke all the port forwarding rules for ip " + ip );
625- success = false ;
626- }
627- } catch (ResourceUnavailableException e ) {
628- s_logger .warn ("Unable to revoke all the port forwarding rules for ip " + ip , e );
629- success = false ;
630- }
631-
632- if (!_lbMgr .removeAllLoadBalanacers (addrId , caller , userId )) {
633- s_logger .warn ("Unable to revoke all the load balancer rules for ip " + ip );
630+
631+ //Cleanup all ip address resources - PF/LB/Static nat rules
632+ if (cleanupIpResources (addrId , userId , caller )) {
633+ unassignPublicIpAddress (ip );
634+ } else {
634635 success = false ;
636+ s_logger .warn ("Failed to release resources for ip address id=" + addrId );
635637 }
636638
637- if (ip .getAssociatedWithNetworkId () != null ) {
639+ if (ip .getAssociatedWithNetworkId () != null ) {
638640 Network network = _networksDao .findById (ip .getAssociatedWithNetworkId ());
639641 try {
640642 if (!applyIpAssociations (network , true )) {
641643 s_logger .warn ("Unable to apply ip address associations for " + network );
642644 success = false ;
643645 }
644646 } catch (ResourceUnavailableException e ) {
645- throw new CloudRuntimeException ("We should nver get to here because we used true when applyIpAssociations" , e );
647+ throw new CloudRuntimeException ("We should never get to here because we used true when applyIpAssociations" , e );
646648 }
647649 }
648650
@@ -1828,7 +1830,7 @@ public boolean deleteNetwork(long networkId){
18281830 List <UserVmVO > userVms = _vmDao .listByNetworkId (networkId );
18291831
18301832 for (UserVmVO vm : userVms ) {
1831- if (!(vm .getState () == VirtualMachine .State .Error || vm .getState () == VirtualMachine .State .Expunging )) {
1833+ if (!(vm .getState () == VirtualMachine .State .Error || ( vm .getState () == VirtualMachine .State .Expunging && vm . getRemoved () != null ) )) {
18321834 throw new InvalidParameterValueException ("Can't delete the network, not all user vms are expunged" );
18331835 }
18341836 }
@@ -1907,8 +1909,7 @@ public void shutdownNetwork(long networkId, ReservationContext context) {
19071909 txn .commit ();
19081910 }
19091911
1910- @ DB
1911- @ Override
1912+ @ Override @ DB
19121913 public boolean destroyNetwork (long networkId , ReservationContext context ) {
19131914 Account callerAccount = _accountMgr .getAccount (context .getCaller ().getAccountId ());
19141915
@@ -1930,17 +1931,7 @@ public boolean destroyNetwork(long networkId, ReservationContext context) {
19301931
19311932 boolean success = true ;
19321933
1933- // release ip addresses associated with the network if there are any - for Virtual case
1934- List <IPAddressVO > ipsToRelease = _ipAddressDao .listByAssociatedNetwork (networkId );
1935- if (ipsToRelease != null && !ipsToRelease .isEmpty ()) {
1936- for (IPAddressVO ip : ipsToRelease ) {
1937- //delete load balancer rules associated with the ip address before unassigning it
1938- _lbMgr .removeAllLoadBalanacers (ip .getId (), callerAccount , context .getCaller ().getId ());
1939- unassignPublicIpAddress (ip );
1940- }
1941-
1942- s_logger .debug ("Ip addresses associated with network " + networkId + " are unassigned successfully as a part of network id=" + networkId + " destroy" );
1943- }
1934+ cleanupNetworkResources (networkId , callerAccount , context .getCaller ().getId ());
19441935
19451936 for (NetworkElement element : _networkElements ) {
19461937 try {
@@ -1985,6 +1976,52 @@ public boolean destroyNetwork(long networkId, ReservationContext context) {
19851976
19861977 return success ;
19871978 }
1979+
1980+ private boolean cleanupNetworkResources (long networkId , Account caller , long callerUserId ) {
1981+ boolean success = true ;
1982+ Network network = getNetwork (networkId );
1983+
1984+ //remove all PF/Static Nat rules for the network
1985+ try {
1986+ if (_rulesMgr .revokeAllRulesForNetwork (networkId , callerUserId , caller )) {
1987+ s_logger .debug ("Successfully cleaned up portForwarding/staticNat rules for network id=" + networkId );
1988+ } else {
1989+ success = false ;
1990+ s_logger .warn ("Failed to release portForwarding/StaticNat rules as a part of network id=" + networkId + " cleanup" );
1991+ }
1992+ } catch (ResourceUnavailableException ex ) {
1993+ success = false ;
1994+ //shouldn't even come here as network is being cleaned up after all network elements are shutdown
1995+ s_logger .warn ("Failed to release portForwarding/StaticNat rules as a part of network id=" + networkId + " cleanup due to resourceUnavailable " , ex );
1996+ }
1997+
1998+ //remove all LB rules for the network
1999+ if (_lbMgr .removeAllLoadBalanacersForNetwork (networkId , caller , callerUserId )) {
2000+ s_logger .debug ("Successfully cleaned up load balancing rules for network id=" + networkId );
2001+ } else {
2002+ //shouldn't even come here as network is being cleaned up after all network elements are shutdown
2003+ success = false ;
2004+ s_logger .warn ("Failed to cleanup LB rules as a part of network id=" + networkId + " cleanup" );
2005+ }
2006+
2007+ //release all ip addresses
2008+ List <IPAddressVO > ipsToRelease = _ipAddressDao .listByAssociatedNetwork (networkId );
2009+ for (IPAddressVO ipToRelease : ipsToRelease ) {
2010+ IPAddressVO ip = _ipAddressDao .markAsUnavailable (ipToRelease .getId ());
2011+ assert (ip != null ) : "Unable to mark the ip address id=" + ipToRelease .getId () + " as unavailable." ;
2012+ }
2013+
2014+ try {
2015+ if (!applyIpAssociations (network , true )) {
2016+ s_logger .warn ("Unable to apply ip address associations for " + network );
2017+ success = false ;
2018+ }
2019+ } catch (ResourceUnavailableException e ) {
2020+ throw new CloudRuntimeException ("We should never get to here because we used true when applyIpAssociations" , e );
2021+ }
2022+
2023+ return success ;
2024+ }
19882025
19892026 private boolean deleteVlansInNetwork (long networkId , long userId ) {
19902027 List <VlanVO > vlans = _vlanDao .listVlansByNetworkId (networkId );
@@ -2080,7 +2117,7 @@ public void run() {
20802117
20812118 @ Override @ ActionEvent (eventType =EventTypes .EVENT_NETWORK_RESTART , eventDescription ="restarting network" , async =true )
20822119 public boolean restartNetwork (RestartNetworkCmd cmd ) throws ConcurrentOperationException , ResourceUnavailableException , InsufficientCapacityException {
2083- // This method restarts all network elements belonging to the network
2120+ // This method restarts all network elements belonging to the network and re-applies all the rules
20842121 Long networkId = cmd .getNetworkId ();
20852122 NetworkVO network = _networksDao .findById (networkId );
20862123 Account owner = _accountMgr .getAccount (network .getAccountId ());
@@ -2090,51 +2127,61 @@ public boolean restartNetwork(RestartNetworkCmd cmd) throws ConcurrentOperationE
20902127 ReservationContext context = new ReservationContextImpl (null , null , caller , owner );
20912128
20922129 _accountMgr .checkAccess (callerAccount , network );
2130+
2131+ boolean success = true ;
20932132
20942133 s_logger .debug ("Restarting network " + networkId + "..." );
20952134 for (NetworkElement element : _networkElements ) {
20962135 //stop and start the network element
20972136 if (!element .restart (network , context )) {
20982137 s_logger .warn ("Failed to restart network element(s) as a part of network id" + networkId + " restart" );
2099- return false ;
2138+ success = false ;
21002139 }
21012140 }
21022141
21032142 //associate all ip addresses
21042143 if (!applyIpAssociations (network , false )) {
21052144 s_logger .warn ("Failed to apply ip addresses as a part of network id" + networkId + " restart" );
2106- return false ;
2145+ success = false ;
21072146 }
21082147
21092148 //apply port forwarding rules
21102149 if (!_rulesMgr .applyPortForwardingRulesForNetwork (networkId , false , context .getAccount ())) {
21112150 s_logger .warn ("Failed to reapply port forwarding rule(s) as a part of network id=" + networkId + " restart" );
2151+ success = false ;
21122152 }
21132153
21142154 //apply static nat rules
21152155 if (!_rulesMgr .applyStaticNatRulesForNetwork (networkId , false , context .getAccount ())) {
21162156 s_logger .warn ("Failed to reapply static nat rule(s) as a part of network id=" + networkId + " restart" );
2157+ success = false ;
21172158 }
21182159
21192160 //apply load balancer rules
21202161 if (!_lbMgr .applyLoadBalancersForNetwork (networkId )) {
21212162 s_logger .warn ("Failed to reapply load balancer rules as a part of network id=" + networkId + " restart" );
2122- return false ;
2163+ success = false ;
21232164 }
21242165
21252166 //apply vpn rules
21262167 List <? extends RemoteAccessVpn > vpnsToReapply = _vpnMgr .listRemoteAccessVpns (networkId );
21272168 if (vpnsToReapply != null ) {
21282169 for (RemoteAccessVpn vpn : vpnsToReapply ) {
2170+ //Start remote access vpn per ip
21292171 if (_vpnMgr .startRemoteAccessVpn (vpn .getServerAddressId ()) == null ) {
2130- s_logger .warn ("Failed to reapply load balancer rules as a part of network id=" + networkId + " restart" );
2131- return false ;
2172+ s_logger .warn ("Failed to reapply vpn rules as a part of network id=" + networkId + " restart" );
2173+ success = false ;
21322174 }
21332175 }
21342176 }
21352177
2136- s_logger .debug ("Network id=" + networkId + " is restarted successfully." );
2137- return true ;
2178+ if (success ) {
2179+ s_logger .debug ("Network id=" + networkId + " is restarted successfully." );
2180+ } else {
2181+ s_logger .warn ("Network id=" + networkId + " failed to restart." );
2182+ }
2183+
2184+ return success ;
21382185 }
21392186
21402187 @ Override
@@ -2451,4 +2498,37 @@ public boolean isServiceSupported(long networkId, Network.Service service) {
24512498
24522499 return false ;
24532500 }
2501+
2502+ private boolean cleanupIpResources (long ipId , long userId , Account caller ) {
2503+ boolean success = true ;
2504+
2505+ try {
2506+ s_logger .debug ("Revoking all PF/StaticNat rules as a part of public IP id=" + ipId + " release..." );
2507+ if (!_rulesMgr .revokeAllRulesForIp (ipId , userId , caller )) {
2508+ s_logger .warn ("Unable to revoke all the port forwarding rules for ip id=" + ipId + " as a part of ip release" );
2509+ success = false ;
2510+ }
2511+ } catch (ResourceUnavailableException e ) {
2512+ s_logger .warn ("Unable to revoke all the port forwarding rules for ip id=" + ipId + " as a part of ip release" , e );
2513+ success = false ;
2514+ }
2515+
2516+ s_logger .debug ("Revoking all LB rules as a part of public IP id=" + ipId + " release..." );
2517+ if (!_lbMgr .removeAllLoadBalanacersForIp (ipId , caller , userId )) {
2518+ s_logger .warn ("Unable to revoke all the load balancer rules for ip id=" + ipId + " as a part of ip release" );
2519+ success = false ;
2520+ }
2521+
2522+ //remote access vpn can be enabled only for static nat ip, so this part should never be executed under normal conditions
2523+ //only when ip address failed to be cleaned up as a part of account destroy and was marked as Releasing, this part of the code would be triggered
2524+ s_logger .debug ("Cleaning up remote access vpns as a part of public IP id=" + ipId + " release..." );
2525+ try {
2526+ _vpnMgr .destroyRemoteAccessVpn (ipId );
2527+ } catch (ResourceUnavailableException e ) {
2528+ s_logger .warn ("Unable to destroy remote access vpn for ip id=" + ipId + " as a part of ip release" , e );
2529+ success = false ;
2530+ }
2531+
2532+ return success ;
2533+ }
24542534}
0 commit comments