3838import com .cloud .network .Networks .BroadcastDomainType ;
3939import com .cloud .network .Networks .IsolationType ;
4040import com .cloud .network .PublicIpAddress ;
41+ import com .cloud .network .dao .IPAddressDao ;
42+ import com .cloud .network .dao .IPAddressVO ;
4143import com .cloud .network .router .VirtualRouter ;
4244import com .cloud .network .vpc .VpcManager ;
4345import com .cloud .network .vpc .VpcVO ;
@@ -159,6 +161,8 @@ private Pair<Map<String, PublicIpAddress>, Map<String, PublicIpAddress>> getNics
159161
160162 VpcManager vpcMgr = visitor .getVirtualNetworkApplianceFactory ().getVpcMgr ();
161163 NicDao nicDao = visitor .getVirtualNetworkApplianceFactory ().getNicDao ();
164+ IPAddressDao ipAddressDao = visitor .getVirtualNetworkApplianceFactory ().getIpAddressDao ();
165+
162166 // find out nics to unplug
163167 for (PublicIpAddress ip : _ipAddresses ) {
164168 long publicNtwkId = ip .getNetworkId ();
@@ -170,10 +174,23 @@ private Pair<Map<String, PublicIpAddress>, Map<String, PublicIpAddress>> getNics
170174 }
171175
172176 if (ip .getState () == IpAddress .State .Releasing ) {
173- Nic nic = nicDao .findByIp4AddressAndNetworkIdAndInstanceId (publicNtwkId , _router .getId (), ip .getAddress ().addr ());
177+ NicVO nic = nicDao .findByIp4AddressAndNetworkIdAndInstanceId (publicNtwkId , _router .getId (), ip .getAddress ().addr ());
174178 if (nic != null ) {
175- nicsToUnplug .put (ip .getVlanTag (), ip );
176- s_logger .debug ("Need to unplug the nic for ip=" + ip + "; vlan=" + ip .getVlanTag () + " in public network id =" + publicNtwkId );
179+ final List <IPAddressVO > allIps = ipAddressDao .listByAssociatedVpc (ip .getVpcId (), null );
180+ boolean ipUpdated = false ;
181+ for (IPAddressVO allIp : allIps ) {
182+ if (allIp .getId () != ip .getId () && allIp .getVlanId () == ip .getVlanId () && allIp .getVmIp () != null ) {
183+ s_logger .debug ("Updating the nic " + nic + " with new ip address " + allIp .getAddress ().addr ());
184+ nic .setIPv4Address (allIp .getAddress ().addr ());
185+ nicDao .update (nic .getId (), nic );
186+ ipUpdated = true ;
187+ break ;
188+ }
189+ }
190+ if (!ipUpdated ) {
191+ nicsToUnplug .put (ip .getVlanTag (), ip );
192+ s_logger .debug ("Need to unplug the nic for ip=" + ip + "; vlan=" + ip .getVlanTag () + " in public network id =" + publicNtwkId );
193+ }
177194 }
178195 }
179196 }
@@ -215,4 +232,4 @@ private Pair<Map<String, PublicIpAddress>, Map<String, PublicIpAddress>> getNics
215232
216233 return nicsToChange ;
217234 }
218- }
235+ }
0 commit comments