From 9ca379fc61e3e9370806cbba4c8ac17afbcd6c01 Mon Sep 17 00:00:00 2001 From: Alena Prokharchyk Date: Fri, 11 Nov 2011 14:04:10 -0800 Subject: [PATCH] Changes to updateNetwork command: * update network with new networkOfferingId only after the network is shutdown * fixed NPE happenining when updateNetwork with the networkOffering w/o soucrceNat service in Advance zone --- .../api/response/PhysicalNetworkResponse.java | 1 + .../com/cloud/network/NetworkManagerImpl.java | 65 ++++++++++--------- .../VirtualNetworkApplianceManagerImpl.java | 2 +- 3 files changed, 37 insertions(+), 31 deletions(-) diff --git a/api/src/com/cloud/api/response/PhysicalNetworkResponse.java b/api/src/com/cloud/api/response/PhysicalNetworkResponse.java index fa55b62cf6b..be37d960675 100644 --- a/api/src/com/cloud/api/response/PhysicalNetworkResponse.java +++ b/api/src/com/cloud/api/response/PhysicalNetworkResponse.java @@ -25,6 +25,7 @@ import com.cloud.api.IdentityProxy; import com.cloud.serializer.Param; import com.google.gson.annotations.SerializedName; +@SuppressWarnings("unused") public class PhysicalNetworkResponse extends BaseResponse{ @SerializedName(ApiConstants.ID) @Param(description="the uuid of the physical network") diff --git a/server/src/com/cloud/network/NetworkManagerImpl.java b/server/src/com/cloud/network/NetworkManagerImpl.java index 5c1e1e2046c..fb3f282e467 100755 --- a/server/src/com/cloud/network/NetworkManagerImpl.java +++ b/server/src/com/cloud/network/NetworkManagerImpl.java @@ -2610,7 +2610,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag _accountMgr.checkAccess(callerAccount, null, network); - boolean success = restartNetwork(networkId, callerAccount, callerUser, null, cleanup); + boolean success = restartNetwork(networkId, callerAccount, callerUser, cleanup); if (success) { s_logger.debug("Network id=" + networkId + " is restarted successfully."); @@ -2641,7 +2641,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } } - private boolean restartNetwork(long networkId, Account callerAccount, User callerUser, Long newNetworkOfferingId, boolean cleanup) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException { + private boolean restartNetwork(long networkId, Account callerAccount, User callerUser, boolean cleanup) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException { NetworkVO network = _networksDao.findById(networkId); @@ -2656,13 +2656,6 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag return false; } - //Only after network was shutdown properly, change the network offering - if (newNetworkOfferingId != null) { - s_logger.debug("Updating network " + network + " with the new network offering id=" + newNetworkOfferingId + " as a part of network restart"); - network.setNetworkOfferingId(newNetworkOfferingId); - _networksDao.update(networkId, network, finalizeServicesAndProvidersForNetwork(_configMgr.getNetworkOffering(newNetworkOfferingId), network.getPhysicalNetworkId())); - } - //implement the network elements and rules again DeployDestination dest = new DeployDestination(_dcDao.findById(network.getDataCenterId()), null, null, null); @@ -3332,31 +3325,43 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag //have to restart the network restartNetwork = true; } + + //1) Shutdown all the elements and cleanup all the rules + ReservationContext context = new ReservationContextImpl(null, null, callerUser, callerAccount); + if (restartNetwork) { + s_logger.debug("Shutting down elements and resources for network id=" + networkId + " as a part of network update"); - _networksDao.update(networkId, network); - - boolean success = true; - if (restartNetwork && (network.getState() == Network.State.Implemented || network.getState() == Network.State.Setup)) { - //network offering id will be updated in the restartNetowrk call aftet the network elements are shutdown properly - s_logger.info("Restarting network " + network + " as a part of update network call"); - - try { - success = restartNetwork(networkId, callerAccount, callerUser, networkOfferingId, true); - } catch (Exception e) { - success = false; + if (!shutdownNetworkElementsAndResources(context, true, network)) { + s_logger.warn("Failed to shutdown the network elements and resources as a part of network restart: " + network.getState()); + throw new CloudRuntimeException("Failed to shutdown the network elements and resources as a part of network restart: " + network.getState()); } - - if (success) { - s_logger.debug("Successully restarted the network " + network + " as a part of updateNetwork call"); - } else { - s_logger.warn("Failed to restart the network " + network + " as a part of updateNetwork call"); - } - } else if (networkOfferingId != null) { - network.setNetworkOfferingId(networkOfferingId); - _networksDao.update(networkId, network, finalizeServicesAndProvidersForNetwork(_configMgr.getNetworkOffering(networkOfferingId), network.getPhysicalNetworkId())); } - return network; + //2) Only after all the elements and rules are shutdown properly, update the network VO + if (networkOfferingId != null) { + network.setNetworkOfferingId(networkOfferingId); + _networksDao.update(networkId, network, finalizeServicesAndProvidersForNetwork(_configMgr.getNetworkOffering(networkOfferingId), network.getPhysicalNetworkId())); + } else { + _networksDao.update(networkId, network); + } + + //get updated network + network = _networksDao.findById(networkId); + + //3) Implement the elements and rules again + if (restartNetwork) { + DeployDestination dest = new DeployDestination(_dcDao.findById(network.getDataCenterId()), null, null, null); + + s_logger.debug("Implementing the network " + network + " elements and resources as a part of network update"); + try { + implementNetworkElementsAndResources(dest, context, network, _networkOfferingDao.findById(network.getNetworkOfferingId())); + } catch (Exception ex) { + s_logger.warn("Failed to implement network " + network + " elements and resources as a part of network update due to ", ex); + throw new CloudRuntimeException("Failed to implement network " + network + " elements and resources as a part of network update"); + } + } + + return getNetwork(network.getId()); } @Override diff --git a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java index f004f6a7b07..6231038f9c8 100755 --- a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java +++ b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java @@ -1156,8 +1156,8 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian if (publicNetwork) { routers = _routerDao.listByNetworkAndRole(guestNetwork.getId(), Role.VIRTUAL_ROUTER); } else { - Long podId = dest.getPod().getId(); if (isPodBased) { + Long podId = dest.getPod().getId(); routers = _routerDao.listByNetworkAndPodAndRole(guestNetwork.getId(), podId, Role.VIRTUAL_ROUTER); plan = new DataCenterDeployment(dcId, podId, null, null, null, null); } else {