diff --git a/server/src/com/cloud/network/NetworkManager.java b/server/src/com/cloud/network/NetworkManager.java index 3d450a9ac45..b34d9ecdf41 100755 --- a/server/src/com/cloud/network/NetworkManager.java +++ b/server/src/com/cloud/network/NetworkManager.java @@ -454,6 +454,7 @@ public interface NetworkManager extends NetworkService { * @param requested * @param context * @param vmProfile + * @param prepare TODO * @return * @throws InsufficientVirtualNetworkCapcityException * @throws InsufficientAddressCapacityException @@ -461,7 +462,7 @@ public interface NetworkManager extends NetworkService { * @throws InsufficientCapacityException * @throws ResourceUnavailableException */ - NicProfile allocateAndPrepareNic(Network network, NicProfile requested, ReservationContext context, VirtualMachineProfileImpl vmProfile) throws InsufficientVirtualNetworkCapcityException, + NicProfile createNicForVm(Network network, NicProfile requested, ReservationContext context, VirtualMachineProfileImpl vmProfile, boolean prepare) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException, ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException; diff --git a/server/src/com/cloud/network/NetworkManagerImpl.java b/server/src/com/cloud/network/NetworkManagerImpl.java index 37824af84e7..be1648d94a2 100755 --- a/server/src/com/cloud/network/NetworkManagerImpl.java +++ b/server/src/com/cloud/network/NetworkManagerImpl.java @@ -7344,8 +7344,8 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } @Override - public NicProfile allocateAndPrepareNic(Network network, NicProfile requested, ReservationContext context, - VirtualMachineProfileImpl vmProfile) + public NicProfile createNicForVm(Network network, NicProfile requested, ReservationContext context, + VirtualMachineProfileImpl vmProfile, boolean prepare) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException, ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException { @@ -7355,20 +7355,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag Host host = _hostDao.findById(vm.getHostId()); DeployDestination dest = new DeployDestination(dc, null, null, host); - NicProfile nic = null; - String broadcastUri = null; - if (requested != null && requested.getBroadCastUri() != null) { - broadcastUri = requested.getBroadCastUri().toString(); - NicVO nicVO = _nicDao.findByInstanceIdNetworkIdAndBroadcastUri(network.getId(), vm.getId(), broadcastUri); - if (nicVO != null) { - nic = getNicProfile(vm, network.getId()); - } - } else { - NicVO nicVO = _nicDao.findByInstanceIdAndNetworkId(network.getId(), vm.getId()); - if (nicVO != null) { - nic = getNicProfile(vm, network.getId()); - } - } + NicProfile nic = getNicProfileForVm(network, requested, vm); //1) allocate nic (if needed) if (nic == null) { @@ -7382,16 +7369,32 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag throw new CloudRuntimeException("Failed to allocate nic for vm " + vm + " in network " + network); } - s_logger.debug("Nic is allocated successfully for vm " + vm + " in network " + network); - - - - s_logger.debug("Nic is prepared successfully for vm " + vm + " in network " + network); + s_logger.debug("Nic is allocated successfully for vm " + vm + " in network " + network); } //2) prepare nic - nic = prepareNic(vmProfile, dest, context, nic.getId(), networkVO); + if (prepare) { + nic = prepareNic(vmProfile, dest, context, nic.getId(), networkVO); + s_logger.debug("Nic is prepared successfully for vm " + vm + " in network " + network); + } return nic; } + + private NicProfile getNicProfileForVm(Network network, NicProfile requested, VirtualMachine vm) { + NicProfile nic = null; + if (requested != null && requested.getBroadCastUri() != null) { + String broadcastUri = requested.getBroadCastUri().toString(); + NicVO nicVO = _nicDao.findByInstanceIdNetworkIdAndBroadcastUri(network.getId(), vm.getId(), broadcastUri); + if (nicVO != null) { + nic = getNicProfile(vm, network.getId()); + } + } else { + NicVO nicVO = _nicDao.findByInstanceIdAndNetworkId(network.getId(), vm.getId()); + if (nicVO != null) { + nic = getNicProfile(vm, network.getId()); + } + } + return nic; + } } diff --git a/server/src/com/cloud/network/router/VpcVirtualNetworkApplianceManagerImpl.java b/server/src/com/cloud/network/router/VpcVirtualNetworkApplianceManagerImpl.java index aedcbc1bd31..84e2e20ea07 100644 --- a/server/src/com/cloud/network/router/VpcVirtualNetworkApplianceManagerImpl.java +++ b/server/src/com/cloud/network/router/VpcVirtualNetworkApplianceManagerImpl.java @@ -424,9 +424,6 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian throw new AgentUnavailableException("Unable to plug nic for router " + vm.getHostName() + " in network " + network, dest.getHost().getId(), e); } - } else if (router.getState() == State.Stopped || router.getState() == State.Stopping) { - s_logger.debug("Router " + router.getInstanceName() + " is in " + router.getState() + - ", so not sending PlugNic command to the backend"); } else { s_logger.warn("Unable to apply PlugNic, vm " + router + " is not in the right state " + router.getState()); @@ -477,22 +474,30 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian protected boolean setupVpcGuestNetwork(Network network, VirtualRouter router, boolean add, NicProfile guestNic) throws ConcurrentOperationException, ResourceUnavailableException{ - boolean result = true; - - SetupGuestNetworkCommand setupCmd = createSetupGuestNetworkCommand(router, add, guestNic); + boolean result = true; + if (router.getState() == State.Running) { + SetupGuestNetworkCommand setupCmd = createSetupGuestNetworkCommand(router, add, guestNic); - Commands cmds = new Commands(OnError.Stop); - cmds.addCommand("setupguestnetwork", setupCmd); - sendCommandsToRouter(router, cmds); - - SetupGuestNetworkAnswer setupAnswer = cmds.getAnswer(SetupGuestNetworkAnswer.class); - String setup = add ? "set" : "destroy"; - if (!(setupAnswer != null && setupAnswer.getResult())) { - s_logger.warn("Unable to " + setup + " guest network on router " + router); - result = false; - } - - return result; + Commands cmds = new Commands(OnError.Stop); + cmds.addCommand("setupguestnetwork", setupCmd); + sendCommandsToRouter(router, cmds); + + SetupGuestNetworkAnswer setupAnswer = cmds.getAnswer(SetupGuestNetworkAnswer.class); + String setup = add ? "set" : "destroy"; + if (!(setupAnswer != null && setupAnswer.getResult())) { + s_logger.warn("Unable to " + setup + " guest network on router " + router); + result = false; + } + return result; + } else if (router.getState() == State.Stopped || router.getState() == State.Stopping) { + s_logger.debug("Router " + router.getInstanceName() + " is in " + router.getState() + + ", so not sending setup guest network command to the backend"); + return true; + } else { + s_logger.warn("Unable to setup guest network on virtual router " + router + " is not in the right state " + router.getState()); + throw new ResourceUnavailableException("Unable to setup guest network on the backend," + + " virtual router " + router + " is not in the right state", DataCenter.class, router.getDataCenterIdToDeployIn()); + } } protected SetupGuestNetworkCommand createSetupGuestNetworkCommand(VirtualRouter router, boolean add, NicProfile guestNic) { @@ -516,7 +521,6 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian String networkDomain = network.getNetworkDomain(); String dhcpRange = getGuestDhcpRange(guestNic, network, _configMgr.getZone(network.getDataCenterId())); - VirtualMachine vm = _vmDao.findById(router.getId()); NicProfile nicProfile = _networkMgr.getNicProfile(router, nic.getNetworkId()); SetupGuestNetworkCommand setupCmd = new SetupGuestNetworkCommand(dhcpRange, networkDomain, false, null, @@ -971,9 +975,7 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian protected boolean setupVpcPrivateNetwork(VirtualRouter router, boolean add, NicProfile privateNic) throws ResourceUnavailableException { - if (router.getState() == State.Running) { - PrivateIpVO ipVO = _privateIpDao.findByIpAndSourceNetworkId(privateNic.getNetworkId(), privateNic.getIp4Address()); Network network = _networkDao.findById(privateNic.getNetworkId()); String vlanTag = network.getBroadcastUri().getHost(); diff --git a/server/src/com/cloud/vm/VirtualMachineManagerImpl.java b/server/src/com/cloud/vm/VirtualMachineManagerImpl.java index 51aacbd72d3..4bbb39ae216 100755 --- a/server/src/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/server/src/com/cloud/vm/VirtualMachineManagerImpl.java @@ -2459,26 +2459,36 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene Host host = _hostDao.findById(vm.getHostId()); DeployDestination dest = new DeployDestination(dc, null, null, host); - //1) allocate and prepare nic - NicProfile nic = _networkMgr.allocateAndPrepareNic(network, requested, context, vmProfile); - - //2) Convert vmProfile to vmTO - HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vmProfile.getVirtualMachine().getHypervisorType()); - VirtualMachineTO vmTO = hvGuru.implement(vmProfile); - - //3) Convert nicProfile to NicTO - NicTO nicTO = toNicTO(nic, vmProfile.getVirtualMachine().getHypervisorType()); - - //4) plug the nic to the vm - VirtualMachineGuru vmGuru = getVmGuru(vmVO); - - s_logger.debug("Plugging nic for vm " + vm + " in network " + network); - if (vmGuru.plugNic(network, nicTO, vmTO, context, dest)) { - s_logger.debug("Nic is plugged successfully for vm " + vm + " in network " + network + ". Vm is a part of network now"); - return nic; + //check vm state + if (vm.getState() == State.Running) { + //1) allocate and prepare nic + NicProfile nic = _networkMgr.createNicForVm(network, requested, context, vmProfile, true); + + //2) Convert vmProfile to vmTO + HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vmProfile.getVirtualMachine().getHypervisorType()); + VirtualMachineTO vmTO = hvGuru.implement(vmProfile); + + //3) Convert nicProfile to NicTO + NicTO nicTO = toNicTO(nic, vmProfile.getVirtualMachine().getHypervisorType()); + + //4) plug the nic to the vm + VirtualMachineGuru vmGuru = getVmGuru(vmVO); + + s_logger.debug("Plugging nic for vm " + vm + " in network " + network); + if (vmGuru.plugNic(network, nicTO, vmTO, context, dest)) { + s_logger.debug("Nic is plugged successfully for vm " + vm + " in network " + network + ". Vm is a part of network now"); + return nic; + } else { + s_logger.warn("Failed to plug nic to the vm " + vm + " in network " + network); + return null; + } + } else if (vm.getState() == State.Stopped) { + //1) allocate nic + return _networkMgr.createNicForVm(network, requested, context, vmProfile, false); } else { - s_logger.warn("Failed to plug nic to the vm " + vm + " in network " + network); - return null; + s_logger.warn("Unable to add vm " + vm + " to network " + network); + throw new ResourceUnavailableException("Unable to add vm " + vm + " to network, is not in the right state", + DataCenter.class, vm.getDataCenterIdToDeployIn()); } } @@ -2507,7 +2517,14 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vmProfile.getVirtualMachine().getHypervisorType()); VirtualMachineTO vmTO = hvGuru.implement(vmProfile); - Nic nic = _networkMgr.getNicInNetwork(vm.getId(), network.getId()); + Nic nic = null; + + if (broadcastUri != null) { + nic = _nicsDao.findByInstanceIdNetworkIdAndBroadcastUri(network.getId(), vm.getId(), broadcastUri.getHost()); + } else { + nic = _networkMgr.getNicInNetwork(vm.getId(), network.getId()); + } + NicProfile nicProfile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), _networkMgr.getNetworkRate(network.getId(), vm.getId()), _networkMgr.isSecurityGroupSupportedInNetwork(network), diff --git a/server/src/com/cloud/vm/dao/DomainRouterDaoImpl.java b/server/src/com/cloud/vm/dao/DomainRouterDaoImpl.java index 2d5445b502e..fc76d221ea5 100755 --- a/server/src/com/cloud/vm/dao/DomainRouterDaoImpl.java +++ b/server/src/com/cloud/vm/dao/DomainRouterDaoImpl.java @@ -278,7 +278,7 @@ public class DomainRouterDaoImpl extends GenericDaoBase im @DB public void addRouterToGuestNetwork(VirtualRouter router, Network guestNetwork) { if (_routerNetworkDao.findByRouterAndNetwork(router.getId(), guestNetwork.getId()) == null && - guestNetwork.getName() != NetworkOffering.SystemPrivateGatewayNetworkOffering) { + !guestNetwork.getName().equalsIgnoreCase(NetworkOffering.SystemPrivateGatewayNetworkOffering)) { Transaction txn = Transaction.currentTxn(); txn.start(); //1) add router to network