@@ -634,17 +634,37 @@ public PublicIp assignDedicateIpAddress(Account owner, Long guestNtwkId, Long vp
634634 }
635635 }
636636
637-
638-
639637 @ Override
640- public boolean applyIpAssociations (Network network , boolean continueOnError ) throws ResourceUnavailableException {
638+ public boolean applyIpAssociations (Network network , boolean continueOnError , boolean reprogramNetworkRules ) throws ResourceUnavailableException {
641639 List <IPAddressVO > userIps = _ipAddressDao .listByAssociatedNetwork (network .getId (), null );
642640 boolean success = true ;
643641
644- // CloudStack will take a lazy approach to associate an acquired public IP to a network service provider as
642+ // applyIpAssociations is invoked in below cases
643+ // - associate/dis-associate ip address to a network
644+ // - network restart/implement/shutdown
645+ //
646+ // For network restart/implement operations that trigger reprogramming network rules run the the Ip Assoc
647+ // so that source nat ip is associated with source nat service provider.
648+ //
649+ // for the associate/disassociate ip take a lazy approach to associate a public IP to a network service provider as
645650 // it will not know what service an acquired IP will be used for. An IP is actually associated with a provider when first
646651 // rule is applied. Similarly when last rule on the acquired IP is revoked, IP is not associated with any provider
647- // but still be associated with the account. At this point just mark IP as allocated or released.
652+ // so at this point just mark IP as allocated or released.
653+
654+ if (reprogramNetworkRules ) {
655+ List <PublicIp > publicIps = new ArrayList <PublicIp >();
656+ if (userIps != null && !userIps .isEmpty ()) {
657+ for (IPAddressVO userIp : userIps ) {
658+ PublicIp publicIp = PublicIp .createFromAddrAndVlan (userIp , _vlanDao .findById (userIp .getVlanId ()));
659+ publicIps .add (publicIp );
660+ }
661+ }
662+ if (! applyIpAssociations (network , false , continueOnError , publicIps )) {
663+ return false ;
664+ }
665+ }
666+
667+
648668 for (IPAddressVO addr : userIps ) {
649669 if (addr .getState () == IpAddress .State .Allocating ) {
650670 addr .setAssociatedWithNetworkId (network .getId ());
@@ -982,7 +1002,7 @@ public IPAddressVO associateIPToGuestNetwork(long ipId, long networkId, boolean
9821002
9831003 boolean success = false ;
9841004 try {
985- success = applyIpAssociations (network , false );
1005+ success = applyIpAssociations (network , false , false );
9861006 if (success ) {
9871007 s_logger .debug ("Successfully associated ip address " + ip .getAddress ().addr () + " to network " + network );
9881008 } else {
@@ -995,7 +1015,7 @@ public IPAddressVO associateIPToGuestNetwork(long ipId, long networkId, boolean
9951015 try {
9961016 s_logger .warn ("Failed to associate ip address, so releasing ip from the database " + ip );
9971017 _ipAddressDao .markAsUnavailable (ip .getId ());
998- if (!applyIpAssociations (network , true )) {
1018+ if (!applyIpAssociations (network , true , false )) {
9991019 // if fail to apply ip assciations again, unassign ip address without updating resource
10001020 // count and generating usage event as there is no need to keep it in the db
10011021 _ipAddressDao .unassignIpAddress (ip .getId ());
@@ -1083,7 +1103,7 @@ public IPAddressVO disassociatePortableIPToGuestNetwork(long ipId, long networkI
10831103 _ipAddressDao .update (ipId , ip );
10841104
10851105 try {
1086- boolean success = applyIpAssociations (network , false );
1106+ boolean success = applyIpAssociations (network , false , false );
10871107 if (success ) {
10881108 s_logger .debug ("Successfully associated ip address " + ip .getAddress ().addr () + " to network " + network );
10891109 } else {
@@ -1224,7 +1244,7 @@ public boolean disassociatePublicIpAddress(long addrId, long userId, Account cal
12241244 if (ip .getAssociatedWithNetworkId () != null ) {
12251245 Network network = _networksDao .findById (ip .getAssociatedWithNetworkId ());
12261246 try {
1227- if (!applyIpAssociations (network , true )) {
1247+ if (!applyIpAssociations (network , true , false )) {
12281248 s_logger .warn ("Unable to apply ip address associations for " + network );
12291249 success = false ;
12301250 }
@@ -3362,7 +3382,7 @@ private void setRestartRequired(NetworkVO network, boolean restartRequired) {
33623382 protected boolean reprogramNetworkRules (long networkId , Account caller , NetworkVO network ) throws ResourceUnavailableException {
33633383 boolean success = true ;
33643384 // associate all ip addresses
3365- if (!applyIpAssociations (network , false )) {
3385+ if (!applyIpAssociations (network , false , true )) {
33663386 s_logger .warn ("Failed to apply ip addresses as a part of network id" + networkId + " restart" );
33673387 success = false ;
33683388 }
@@ -3982,7 +4002,7 @@ private boolean cleanupNetworkResources(long networkId, Account caller, long cal
39824002 }
39834003
39844004 try {
3985- if (!applyIpAssociations (network , true )) {
4005+ if (!applyIpAssociations (network , true , true )) {
39864006 s_logger .warn ("Unable to apply ip address associations for " + network );
39874007 success = false ;
39884008 }
0 commit comments