From f28cefc4c3df708ad492a941ea54cd830f5e53a4 Mon Sep 17 00:00:00 2001 From: Alena Prokharchyk Date: Thu, 24 May 2012 15:33:52 -0700 Subject: [PATCH] Added support for network shutdown in VPC guest networks --- .../com/cloud/agent/api/PlugNicAnswer.java | 24 ++++ .../com/cloud/agent/api/PlugNicCommand.java | 4 + .../com/cloud/agent/api/UnPlugNicAnswer.java | 24 ++++ .../com/cloud/agent/api/UnPlugNicCommand.java | 45 ++++++++ .../cloud/network/element/VpcProvider.java | 8 ++ api/src/com/cloud/network/vpc/Vpc.java | 8 +- .../VpcVirtualNetworkApplianceService.java | 4 +- .../src/com/cloud/network/NetworkManager.java | 19 ++++ .../com/cloud/network/NetworkManagerImpl.java | 59 +++++++--- .../cloud/network/RouterNetworkDaoImpl.java | 15 +++ .../com/cloud/network/RouterNetworkVO.java | 4 + .../element/VpcVirtualRouterElement.java | 107 ++++++++++++------ ...VpcVirtualNetworkApplianceManagerImpl.java | 82 +++++++++----- server/src/com/cloud/network/vpc/VpcVO.java | 4 + .../com/cloud/vm/VirtualMachineManager.java | 4 +- .../cloud/vm/VirtualMachineManagerImpl.java | 63 +++++++++-- .../src/com/cloud/vm/dao/DomainRouterDao.java | 6 + .../com/cloud/vm/dao/DomainRouterDaoImpl.java | 10 +- .../vm/MockVirtualMachineManagerImpl.java | 19 ++++ wscript | 2 +- 20 files changed, 414 insertions(+), 97 deletions(-) create mode 100644 api/src/com/cloud/agent/api/PlugNicAnswer.java create mode 100644 api/src/com/cloud/agent/api/UnPlugNicAnswer.java create mode 100644 api/src/com/cloud/agent/api/UnPlugNicCommand.java diff --git a/api/src/com/cloud/agent/api/PlugNicAnswer.java b/api/src/com/cloud/agent/api/PlugNicAnswer.java new file mode 100644 index 00000000000..3a76240ef22 --- /dev/null +++ b/api/src/com/cloud/agent/api/PlugNicAnswer.java @@ -0,0 +1,24 @@ +// Copyright 2012 Citrix Systems, Inc. Licensed under the +// Apache License, Version 2.0 (the "License"); you may not use this +// file except in compliance with the License. Citrix Systems, Inc. +// reserves all rights not expressly granted by the License. +// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Automatically generated by addcopyright.py at 04/03/2012 +package com.cloud.agent.api; + +/** + * @author Alena Prokharchyk + */ +public class PlugNicAnswer extends Answer{ + public PlugNicAnswer() {} + + public PlugNicAnswer(PlugNicCommand cmd, boolean success, String result) { + super(cmd, success, result); + } +} diff --git a/api/src/com/cloud/agent/api/PlugNicCommand.java b/api/src/com/cloud/agent/api/PlugNicCommand.java index 041d373b8eb..f2f36cb56ef 100644 --- a/api/src/com/cloud/agent/api/PlugNicCommand.java +++ b/api/src/com/cloud/agent/api/PlugNicCommand.java @@ -34,6 +34,10 @@ public class PlugNicCommand extends Command { return vm; } + public NicTO getNic() { + return nic; + } + @Override public boolean executeInSequence() { return true; diff --git a/api/src/com/cloud/agent/api/UnPlugNicAnswer.java b/api/src/com/cloud/agent/api/UnPlugNicAnswer.java new file mode 100644 index 00000000000..cd87c722697 --- /dev/null +++ b/api/src/com/cloud/agent/api/UnPlugNicAnswer.java @@ -0,0 +1,24 @@ +// Copyright 2012 Citrix Systems, Inc. Licensed under the +// Apache License, Version 2.0 (the "License"); you may not use this +// file except in compliance with the License. Citrix Systems, Inc. +// reserves all rights not expressly granted by the License. +// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Automatically generated by addcopyright.py at 04/03/2012 +package com.cloud.agent.api; + +/** + * @author Alena Prokharchyk + */ +public class UnPlugNicAnswer extends Answer{ + public UnPlugNicAnswer() {} + + public UnPlugNicAnswer(UnPlugNicCommand cmd, boolean success, String result) { + super(cmd, success, result); + } +} diff --git a/api/src/com/cloud/agent/api/UnPlugNicCommand.java b/api/src/com/cloud/agent/api/UnPlugNicCommand.java new file mode 100644 index 00000000000..26d9441092f --- /dev/null +++ b/api/src/com/cloud/agent/api/UnPlugNicCommand.java @@ -0,0 +1,45 @@ +// Copyright 2012 Citrix Systems, Inc. Licensed under the +// Apache License, Version 2.0 (the "License"); you may not use this +// file except in compliance with the License. Citrix Systems, Inc. +// reserves all rights not expressly granted by the License. +// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Automatically generated by addcopyright.py at 04/03/2012 +package com.cloud.agent.api; + +import com.cloud.agent.api.to.NicTO; +import com.cloud.agent.api.to.VirtualMachineTO; + +/** + * @author Alena Prokharchyk + */ +public class UnPlugNicCommand extends Command{ + VirtualMachineTO vm; + NicTO nic; + + public VirtualMachineTO getVirtualMachine() { + return vm; + } + + public NicTO getNic() { + return nic; + } + + @Override + public boolean executeInSequence() { + return true; + } + + protected UnPlugNicCommand() { + } + + public UnPlugNicCommand(VirtualMachineTO vm, NicTO nic) { + this.vm = vm; + this.nic = nic; + } +} diff --git a/api/src/com/cloud/network/element/VpcProvider.java b/api/src/com/cloud/network/element/VpcProvider.java index 246ac0bdf4c..9243836e667 100644 --- a/api/src/com/cloud/network/element/VpcProvider.java +++ b/api/src/com/cloud/network/element/VpcProvider.java @@ -35,6 +35,14 @@ public interface VpcProvider extends NetworkElement{ */ boolean startVpc(Vpc vpc, DeployDestination dest, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException; + + /** + * @param vpc + * @return + * @throws ConcurrentOperationException + * @throws ResourceUnavailableException + */ + boolean stopVpc(Vpc vpc) throws ConcurrentOperationException, ResourceUnavailableException; } diff --git a/api/src/com/cloud/network/vpc/Vpc.java b/api/src/com/cloud/network/vpc/Vpc.java index 88fee8c76b5..f62a15f3efe 100644 --- a/api/src/com/cloud/network/vpc/Vpc.java +++ b/api/src/com/cloud/network/vpc/Vpc.java @@ -12,11 +12,8 @@ // Automatically generated by addcopyright.py at 04/03/2012 package com.cloud.network.vpc; -import java.util.List; - import com.cloud.acl.ControlledEntity; import com.cloud.network.Network; -import com.cloud.network.Network.Service; /** @@ -47,5 +44,10 @@ public interface Vpc extends ControlledEntity{ long getVpcOfferingId(); String getDisplayText(); + +/** + * @return + */ +String getNetworkDomain(); } diff --git a/api/src/com/cloud/network/vpc/VpcVirtualNetworkApplianceService.java b/api/src/com/cloud/network/vpc/VpcVirtualNetworkApplianceService.java index 4de760ffba4..08c3b21d087 100644 --- a/api/src/com/cloud/network/vpc/VpcVirtualNetworkApplianceService.java +++ b/api/src/com/cloud/network/vpc/VpcVirtualNetworkApplianceService.java @@ -38,6 +38,8 @@ public interface VpcVirtualNetworkApplianceService { * @param router * @param network * @return + * @throws ResourceUnavailableException + * @throws ConcurrentOperationException */ - boolean removeVmFromNetwork(VirtualRouter router, Network network); + boolean removeVmFromNetwork(VirtualRouter router, Network network) throws ConcurrentOperationException, ResourceUnavailableException; } diff --git a/server/src/com/cloud/network/NetworkManager.java b/server/src/com/cloud/network/NetworkManager.java index 80346d998cb..9c7b3878dca 100755 --- a/server/src/com/cloud/network/NetworkManager.java +++ b/server/src/com/cloud/network/NetworkManager.java @@ -49,6 +49,7 @@ import com.cloud.user.Account; import com.cloud.utils.Pair; import com.cloud.vm.Nic; import com.cloud.vm.NicProfile; +import com.cloud.vm.NicVO; import com.cloud.vm.ReservationContext; import com.cloud.vm.VMInstanceVO; import com.cloud.vm.VirtualMachine; @@ -372,4 +373,22 @@ public interface NetworkManager extends NetworkService { */ NicProfile prepareNic(VirtualMachineProfile vmProfile, DeployDestination dest, ReservationContext context, long nicId, NetworkVO network) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException, ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException; + + + /** + * @param vmProfile + * @param network + * @return TODO + * @throws ConcurrentOperationException + * @throws ResourceUnavailableException + */ + NicProfile releaseNic(VirtualMachineProfile vmProfile, NetworkVO network) throws ConcurrentOperationException, ResourceUnavailableException; + + + /** + * @param vm + * @param network + */ + void removeNic(VirtualMachineProfile vm, Network network); + } diff --git a/server/src/com/cloud/network/NetworkManagerImpl.java b/server/src/com/cloud/network/NetworkManagerImpl.java index ce1e1a35ce2..a01b355a712 100755 --- a/server/src/com/cloud/network/NetworkManagerImpl.java +++ b/server/src/com/cloud/network/NetworkManagerImpl.java @@ -2022,11 +2022,14 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } @Override + @DB public NicProfile prepareNic(VirtualMachineProfile vmProfile, DeployDestination dest, ReservationContext context, long nicId, NetworkVO network) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException, ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException { + + Integer networkRate = getNetworkRate(network.getId(), vmProfile.getId()); NetworkGuru guru = _networkGurus.get(network.getGuruName()); NicVO nic = _nicDao.findById(nicId); @@ -2090,7 +2093,8 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag Integer networkRate = getNetworkRate(network.getId(), vm.getId()); NetworkGuru guru = _networkGurus.get(network.getGuruName()); - NicProfile profile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), networkRate, isSecurityGroupSupportedInNetwork(network), getNetworkTag(vm.getHypervisorType(), network)); + NicProfile profile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), networkRate, + isSecurityGroupSupportedInNetwork(network), getNetworkTag(vm.getHypervisorType(), network)); guru.updateNicProfile(profile, network); vm.addNic(profile); } @@ -2105,6 +2109,18 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag releaseNic(vmProfile, nic, network); } } + + @Override + public NicProfile releaseNic(VirtualMachineProfile vmProfile, NetworkVO network) + throws ConcurrentOperationException, ResourceUnavailableException { + NicVO nic = _nicDao.findByInstanceIdAndNetworkId(network.getId(), vmProfile.getId()); + releaseNic(vmProfile, nic, network); + + NicProfile profile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), null, + isSecurityGroupSupportedInNetwork(network), getNetworkTag(vmProfile.getVirtualMachine().getHypervisorType(), network)); + return profile; + } + protected void releaseNic(VirtualMachineProfile vmProfile, NicVO nic, NetworkVO network) throws ConcurrentOperationException, ResourceUnavailableException { @@ -2283,15 +2299,27 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag List nics = _nicDao.listByVmId(vm.getId()); for (NicVO nic : nics) { - nic.setState(Nic.State.Deallocating); - _nicDao.update(nic.getId(), nic); - NetworkVO network = _networksDao.findById(nic.getNetworkId()); - NicProfile profile = new NicProfile(nic, network, null, null, null, isSecurityGroupSupportedInNetwork(network), getNetworkTag(vm.getHypervisorType(), network)); - NetworkGuru guru = _networkGurus.get(network.getGuruName()); - guru.deallocate(network, profile, vm); - _nicDao.remove(nic.getId()); + removeNic(vm, nic); } } + + @Override + public void removeNic(VirtualMachineProfile vm, Network network) { + NicVO nic = _nicDao.findByInstanceIdAndNetworkId(network.getId(), vm.getVirtualMachine().getId()); + removeNic(vm, nic); + } + + protected void removeNic(VirtualMachineProfile vm, NicVO nic) { + nic.setState(Nic.State.Deallocating); + _nicDao.update(nic.getId(), nic); + NetworkVO network = _networksDao.findById(nic.getNetworkId()); + NicProfile profile = new NicProfile(nic, network, null, null, null, + isSecurityGroupSupportedInNetwork(network), getNetworkTag(vm.getHypervisorType(), network)); + NetworkGuru guru = _networkGurus.get(network.getGuruName()); + guru.deallocate(network, profile, vm); + _nicDao.remove(nic.getId()); + s_logger.debug("Removed nic id=" + nic.getId()); + } @Override public void expungeNics(VirtualMachineProfile vm) { @@ -2629,21 +2657,18 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag PhysicalNetwork pNtwk, long zoneId, ACLType aclType, Boolean subdomainAccess, long vpcId) throws ConcurrentOperationException, InsufficientCapacityException, ResourceAllocationException { + Vpc vpc = _vpcMgr.getVpc(vpcId); //1) Validate if network can be created for VPC - _vpcMgr.validateGuestNtkwForVpc(_configMgr.getNetworkOffering(ntwkOffId), cidr, networkDomain, owner, - _vpcMgr.getVpc(vpcId)); + _vpcMgr.validateGuestNtkwForVpc(_configMgr.getNetworkOffering(ntwkOffId), cidr, networkDomain, owner, vpc); + + if (networkDomain == null) { + networkDomain = vpc.getNetworkDomain(); + } //2) Create network Network guestNetwork = createGuestNetwork(ntwkOffId, name, displayText, gateway, cidr, vlanId, networkDomain, owner, domainId, pNtwk, zoneId, aclType, subdomainAccess, vpcId); - //3) Add network to all VPC's routers - List routers = _routerDao.listRoutersByVpcId(vpcId); - for (DomainRouterVO router : routers) { - s_logger.debug("Adding router " + router + " to network " + guestNetwork); - _routerDao.addRouterToNetwork(router, guestNetwork); - } - return guestNetwork; } diff --git a/server/src/com/cloud/network/RouterNetworkDaoImpl.java b/server/src/com/cloud/network/RouterNetworkDaoImpl.java index 9cbd308733d..dcb6583a313 100644 --- a/server/src/com/cloud/network/RouterNetworkDaoImpl.java +++ b/server/src/com/cloud/network/RouterNetworkDaoImpl.java @@ -17,6 +17,7 @@ import java.util.List; import com.cloud.utils.db.GenericDao; import com.cloud.utils.db.GenericDaoBase; import com.cloud.utils.db.GenericSearchBuilder; +import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Op; @@ -25,6 +26,7 @@ import com.cloud.utils.db.SearchCriteria.Op; */ public class RouterNetworkDaoImpl extends GenericDaoBase implements GenericDao{ protected final GenericSearchBuilder RouterNetworksSearch; + protected final SearchBuilder AllFieldsSearch; public RouterNetworkDaoImpl() { super(); @@ -33,6 +35,11 @@ public class RouterNetworkDaoImpl extends GenericDaoBase RouterNetworksSearch.selectField(RouterNetworksSearch.entity().getNetworkId()); RouterNetworksSearch.and("routerId", RouterNetworksSearch.entity().getRouterId(), Op.EQ); RouterNetworksSearch.done(); + + AllFieldsSearch = createSearchBuilder(); + AllFieldsSearch.and("routerId", AllFieldsSearch.entity().getRouterId(), Op.EQ); + AllFieldsSearch.and("networkId", AllFieldsSearch.entity().getNetworkId(), Op.EQ); + AllFieldsSearch.done(); } public List getRouterNetworks(long routerId) { @@ -40,4 +47,12 @@ public class RouterNetworkDaoImpl extends GenericDaoBase sc.setParameters("routerId", routerId); return customSearch(sc, null); } + + public RouterNetworkVO findByRouterAndNetwork (long routerId, long networkId) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("routerId", routerId); + sc.setParameters("networkId", networkId); + return findOneBy(sc); + } + } diff --git a/server/src/com/cloud/network/RouterNetworkVO.java b/server/src/com/cloud/network/RouterNetworkVO.java index bcbdc940f94..629a5f438ee 100644 --- a/server/src/com/cloud/network/RouterNetworkVO.java +++ b/server/src/com/cloud/network/RouterNetworkVO.java @@ -65,4 +65,8 @@ public class RouterNetworkVO{ public Network.GuestType getGuestType() { return guestType; } + + public long getId() { + return id; + } } diff --git a/server/src/com/cloud/network/element/VpcVirtualRouterElement.java b/server/src/com/cloud/network/element/VpcVirtualRouterElement.java index 712e90d3074..8f6b43526b7 100644 --- a/server/src/com/cloud/network/element/VpcVirtualRouterElement.java +++ b/server/src/com/cloud/network/element/VpcVirtualRouterElement.java @@ -21,6 +21,7 @@ import javax.ejb.Local; import org.apache.log4j.Logger; +import com.cloud.dc.DataCenter; import com.cloud.deploy.DeployDestination; import com.cloud.exception.ConcurrentOperationException; import com.cloud.exception.InsufficientCapacityException; @@ -33,9 +34,11 @@ import com.cloud.network.NetworkService; import com.cloud.network.router.VirtualRouter; import com.cloud.network.router.VpcVirtualNetworkApplianceManager; import com.cloud.network.vpc.Vpc; +import com.cloud.network.vpc.VpcService; import com.cloud.network.vpc.VpcVirtualNetworkApplianceService; import com.cloud.offering.NetworkOffering; import com.cloud.utils.component.Inject; +import com.cloud.vm.DomainRouterVO; import com.cloud.vm.NicProfile; import com.cloud.vm.ReservationContext; import com.cloud.vm.VirtualMachine; @@ -51,6 +54,8 @@ public class VpcVirtualRouterElement extends VirtualRouterElement implements Vpc NetworkService _ntwkService; @Inject VpcVirtualNetworkApplianceService _vpcElementService; + @Inject + VpcService _vpcService; private static final Map> capabilities = setCapabilities(); @@ -70,6 +75,19 @@ public class VpcVirtualRouterElement extends VirtualRouterElement implements Vpc return true; } + @Override + public boolean stopVpc(Vpc vpc) throws ConcurrentOperationException, ResourceUnavailableException { + List routers = _routerDao.listRoutersByVpcId(vpc.getId()); + if (routers == null || routers.isEmpty()) { + return true; + } + boolean result = true; + for (DomainRouterVO router : routers) { + result = result && (_routerMgr.destroyRouter(router.getId()) != null); + } + return result; + } + @Override public boolean implement(Network network, NetworkOffering offering, DeployDestination dest, ReservationContext context) throws ResourceUnavailableException, ConcurrentOperationException, @@ -81,26 +99,34 @@ public class VpcVirtualRouterElement extends VirtualRouterElement implements Vpc return false; } - boolean success = super.implement(network, offering, dest, context); + Vpc vpc = _vpcService.getVpc(vpcId); - if (success) { - List routers = _routerDao.listRoutersByVpcId(vpcId); - for (VirtualRouter router : routers) { - //1) Check if router is already a part of the network - if (_ntwkService.isVmPartOfNetwork(router.getId(), network.getId())) { - s_logger.debug("Router " + router + " is already part of the network " + network); - continue; - } - //2) Call plugNics in the network service - success = success && _vpcElementService.addVmToNetwork(router, network); - } - - if (!success) { - s_logger.warn("Failed to plug nic in network " + network + " for virtual router in vpc id=" + vpcId); - } else { - s_logger.debug("Successfully plugged nic in network " + network + " for virtual router in vpc id=" + vpcId); - } + Map params = new HashMap(1); + params.put(VirtualMachineProfile.Param.ReProgramGuestNetworks, true); + + List routers = _vpcRouterMgr.deployVirtualRouterInVpc(vpc, dest, _accountMgr.getAccount(vpc.getAccountId()), params); + if ((routers == null) || (routers.size() == 0)) { + throw new ResourceUnavailableException("Can't find at least one running router!", + DataCenter.class, network.getDataCenterId()); } + + boolean success = true; + for (VirtualRouter router : routers) { + //1) Check if router is already a part of the network + if (_ntwkService.isVmPartOfNetwork(router.getId(), network.getId())) { + s_logger.debug("Router " + router + " is already part of the network " + network); + continue; + } + //2) Call plugNics in the network service + success = success && _vpcElementService.addVmToNetwork(router, network); + } + + if (!success) { + s_logger.warn("Failed to plug nic in network " + network + " for virtual router in vpc id=" + vpcId); + } else { + s_logger.debug("Successfully plugged nic in network " + network + " for virtual router in vpc id=" + vpcId); + } + return success; } @@ -116,26 +142,35 @@ public class VpcVirtualRouterElement extends VirtualRouterElement implements Vpc return false; } - boolean success = super.prepare(network, nic, vm, dest, context); + Vpc vpc = _vpcService.getVpc(vpcId); - if (success) { - List routers = _routerDao.listRoutersByVpcId(vpcId); - for (VirtualRouter router : routers) { - //1) Check if router is already a part of the network - if (_ntwkService.isVmPartOfNetwork(router.getId(), network.getId())) { - s_logger.debug("Router " + router + " is already part of the network " + network); - continue; - } - //2) Call plugNics in the network service - success = success && _vpcElementService.addVmToNetwork(router, network); - } - - if (!success) { - s_logger.warn("Failed to plug nic in network " + network + " for virtual router in vpc id=" + vpcId); - } else { - s_logger.debug("Successfully plugged nic in network " + network + " for virtual router in vpc id=" + vpcId); - } + Map params = new HashMap(1); + params.put(VirtualMachineProfile.Param.ReProgramGuestNetworks, true); + + List routers = _vpcRouterMgr.deployVirtualRouterInVpc(vpc, dest, _accountMgr.getAccount(vpc.getAccountId()), params); + if ((routers == null) || (routers.size() == 0)) { + throw new ResourceUnavailableException("Can't find at least one running router!", + DataCenter.class, network.getDataCenterId()); } + + boolean success = true; + for (VirtualRouter router : routers) { + //1) Check if router is already a part of the network + if (_ntwkService.isVmPartOfNetwork(router.getId(), network.getId())) { + s_logger.debug("Router " + router + " is already part of the network " + network); + continue; + } + //2) Call plugNics in the network service + success = success && _vpcElementService.addVmToNetwork(router, network); + } + + if (!success) { + s_logger.warn("Failed to plug nic in network " + network + " for virtual router in vpc id=" + vpcId); + } else { + s_logger.debug("Successfully plugged nic in network " + network + " for virtual router in vpc id=" + vpcId); + } + + return success; } diff --git a/server/src/com/cloud/network/router/VpcVirtualNetworkApplianceManagerImpl.java b/server/src/com/cloud/network/router/VpcVirtualNetworkApplianceManagerImpl.java index b24ea65c91a..df1177d86a8 100644 --- a/server/src/com/cloud/network/router/VpcVirtualNetworkApplianceManagerImpl.java +++ b/server/src/com/cloud/network/router/VpcVirtualNetworkApplianceManagerImpl.java @@ -12,7 +12,6 @@ // Automatically generated by addcopyright.py at 04/03/2012 package com.cloud.network.router; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -20,19 +19,13 @@ import javax.ejb.Local; import org.apache.log4j.Logger; -import com.cloud.agent.AgentManager.OnError; -import com.cloud.agent.api.Answer; -import com.cloud.agent.api.PlugNicCommand; import com.cloud.agent.api.to.NicTO; import com.cloud.agent.api.to.VirtualMachineTO; -import com.cloud.agent.manager.Commands; import com.cloud.deploy.DataCenterDeployment; import com.cloud.deploy.DeployDestination; import com.cloud.deploy.DeploymentPlan; -import com.cloud.exception.AgentUnavailableException; import com.cloud.exception.ConcurrentOperationException; import com.cloud.exception.InsufficientCapacityException; -import com.cloud.exception.OperationTimedoutException; import com.cloud.exception.ResourceUnavailableException; import com.cloud.network.Network; import com.cloud.network.NetworkService; @@ -152,27 +145,34 @@ VpcVirtualNetworkApplianceManager{ boolean result = true; + //add router to network + DomainRouterVO router = _routerDao.findById(vm.getId()); + s_logger.debug("Adding router " + router + " to network " + network); + _routerDao.addRouterToNetwork(router, network); + + //FIXME - Anthony, here I send plug nic command - try { - Map params = new HashMap(); - params.put(PlugNicCommand.Param.NetworkDomain, networkDomain); - params.put(PlugNicCommand.Param.DhcpRange, dhcpRange); - - PlugNicCommand plugNicCmd = new PlugNicCommand(vm, nic, params); - - Commands cmds = new Commands(OnError.Stop); - cmds.addCommand("plugnic", plugNicCmd); - _agentMgr.send(dest.getHost().getId(), cmds); - - Answer plugNicAnswer = cmds.getAnswer(Answer.class); - if (!(plugNicAnswer != null && plugNicAnswer.getResult())) { - s_logger.warn("Unable to plug nic for vm " + vm.getHostName()); - result = false; - } - - } catch (OperationTimedoutException e) { - throw new AgentUnavailableException("Unable to plug nic for vm " + vm.getHostName(), dest.getHost().getId(), e); - } +// try { +// Map params = new HashMap(); +// params.put(PlugNicCommand.Param.NetworkDomain, networkDomain); +// params.put(PlugNicCommand.Param.DhcpRange, dhcpRange); +// +// PlugNicCommand plugNicCmd = new PlugNicCommand(vm, nic, params); +// +// Commands cmds = new Commands(OnError.Stop); +// cmds.addCommand("plugnic", plugNicCmd); +// _agentMgr.send(dest.getHost().getId(), cmds); +// +// PlugNicAnswer plugNicAnswer = cmds.getAnswer(PlugNicAnswer.class); +// if (!(plugNicAnswer != null && plugNicAnswer.getResult())) { +// s_logger.warn("Unable to plug nic for vm " + vm.getHostName()); +// result = false; +// } +// +// } catch (OperationTimedoutException e) { +// throw new AgentUnavailableException("Unable to plug nic for vm " + vm.getHostName() + " in network " + network, +// dest.getHost().getId(), e); +// } return result; } @@ -182,7 +182,30 @@ VpcVirtualNetworkApplianceManager{ ReservationContext context, DeployDestination dest) throws ConcurrentOperationException, ResourceUnavailableException { //FIXME - Anthony, add unplug nic agent command - return true; + boolean result = true; +// try { +// UnPlugNicCommand unplugNicCmd = new UnPlugNicCommand(vm, nic); +// Commands cmds = new Commands(OnError.Stop); +// cmds.addCommand("unplugnic", unplugNicCmd); +// _agentMgr.send(dest.getHost().getId(), cmds); +// +// UnPlugNicAnswer unplugNicAnswer = cmds.getAnswer(UnPlugNicAnswer.class); +// if (!(unplugNicAnswer != null && unplugNicAnswer.getResult())) { +// s_logger.warn("Unable to unplug nic from vm " + vm.getHostName()); +// result = false; +// } +// +// } catch (OperationTimedoutException e) { +// throw new AgentUnavailableException("Unable to unplug nic from vm " + vm.getHostName() + " from network " + network, +// dest.getHost().getId(), e); +// } +// + if (result) { + s_logger.debug("Removing router " + vm.getHostName() + " from network " + network); + _routerDao.removeRouterFromNetwork(vm.getId(), network.getId()); + } + + return result; } @Override @@ -193,7 +216,8 @@ VpcVirtualNetworkApplianceManager{ @Override - public boolean removeVmFromNetwork(VirtualRouter router, Network network) { + public boolean removeVmFromNetwork(VirtualRouter router, Network network) + throws ConcurrentOperationException, ResourceUnavailableException { return _itMgr.removeVmFromNetwork(router, network); } } diff --git a/server/src/com/cloud/network/vpc/VpcVO.java b/server/src/com/cloud/network/vpc/VpcVO.java index 9f6c9379040..ad12cc014d5 100644 --- a/server/src/com/cloud/network/vpc/VpcVO.java +++ b/server/src/com/cloud/network/vpc/VpcVO.java @@ -172,4 +172,8 @@ public class VpcVO implements Vpc, Identity { return buf.append(id).append("-").append(name).append("]").toString(); } + @Override + public String getNetworkDomain() { + return networkDomain; + } } diff --git a/server/src/com/cloud/vm/VirtualMachineManager.java b/server/src/com/cloud/vm/VirtualMachineManager.java index a7f89ba5cd1..f43de6e1a8b 100644 --- a/server/src/com/cloud/vm/VirtualMachineManager.java +++ b/server/src/com/cloud/vm/VirtualMachineManager.java @@ -149,7 +149,9 @@ public interface VirtualMachineManager extends Manager { * @param vm * @param network * @return + * @throws ResourceUnavailableException + * @throws ConcurrentOperationException */ - boolean removeVmFromNetwork(VirtualMachine vm, Network network); + boolean removeVmFromNetwork(VirtualMachine vm, Network network) throws ConcurrentOperationException, ResourceUnavailableException; } diff --git a/server/src/com/cloud/vm/VirtualMachineManagerImpl.java b/server/src/com/cloud/vm/VirtualMachineManagerImpl.java index c2809153240..798a977fabc 100755 --- a/server/src/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/server/src/com/cloud/vm/VirtualMachineManagerImpl.java @@ -2431,6 +2431,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } @Override + @DB public boolean addVmToNetwork(VirtualMachine vm, Network network) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException { @@ -2448,34 +2449,80 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene s_logger.debug("Adding vm " + vm + " to network " + network); + Transaction txn = Transaction.currentTxn(); + txn.start(); //1) allocate nic NicProfile nic = _networkMgr.allocateNic(null, network, false, 100, vmProfile).first(); + s_logger.debug("Nic is allocated successfully for vm " + vm + " in network " + network); + //2) Prepare nic nic = _networkMgr.prepareNic(vmProfile, dest, context, nic.getId(), networkVO); - //3) plug the nic to the vm - VirtualMachineGuru vmGuru = getVmGuru(vmVO); + s_logger.debug("Nic is prepared successfully for vm " + vm + " in network " + network); - //4) Convert vmProfile to vmTO + txn.commit(); + + //3) Convert vmProfile to vmTO HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vmProfile.getVirtualMachine().getHypervisorType()); VirtualMachineTO vmTO = hvGuru.implement(vmProfile); - //5) Convert nicProfile to NicTO + //4) Convert nicProfile to NicTO NicTO nicTO = hvGuru.toNicTO(nic); - return vmGuru.plugNic(network, nicTO, vmTO, context, null); + //5) plug the nic to the vm + VirtualMachineGuru vmGuru = getVmGuru(vmVO); + + 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 true; + } else { + s_logger.warn("Failed to plug nic to the vm " + vm + " in network " + network); + return false; + } } @Override - public boolean removeVmFromNetwork(VirtualMachine vm, Network network) { - //1) TODO - release the nic + public boolean removeVmFromNetwork(VirtualMachine vm, Network network) throws ConcurrentOperationException, ResourceUnavailableException { + VMInstanceVO vmVO = _vmDao.findById(vm.getId()); + NetworkVO networkVO = _networkDao.findById(network.getId()); + ReservationContext context = new ReservationContextImpl(null, null, _accountMgr.getActiveUser(User.UID_SYSTEM), + _accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM)); + + VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vmVO, null, + null, null, null); + + DataCenter dc = _configMgr.getZone(network.getDataCenterId()); + Host host = _hostDao.findById(vm.getHostId()); + DeployDestination dest = new DeployDestination(dc, null, null, host); + + //1) Release the nic + NicProfile nic = _networkMgr.releaseNic(vmProfile, networkVO); //2) TODO - unplug the nic + VirtualMachineGuru vmGuru = getVmGuru(vmVO); - return true; + //3) Convert vmProfile to vmTO + HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vmProfile.getVirtualMachine().getHypervisorType()); + VirtualMachineTO vmTO = hvGuru.implement(vmProfile); + + //4) Convert nicProfile to NicTO + NicTO nicTO = hvGuru.toNicTO(nic); + + boolean result = vmGuru.unplugNic(network, nicTO, vmTO, context, dest); + //5) Unplug the nic + if (result) { + s_logger.debug("Nic is unplugged successfully for vm " + vm + " in network " + network ); + } else { + s_logger.warn("Failed to unplug nic for the vm " + vm + " from network " + network); + return false; + } + + //6) Remove the nic + _networkMgr.removeNic(vmProfile, network); + return result; } } diff --git a/server/src/com/cloud/vm/dao/DomainRouterDao.java b/server/src/com/cloud/vm/dao/DomainRouterDao.java index 36ee86ce329..3dd615b1906 100755 --- a/server/src/com/cloud/vm/dao/DomainRouterDao.java +++ b/server/src/com/cloud/vm/dao/DomainRouterDao.java @@ -117,4 +117,10 @@ public interface DomainRouterDao extends GenericDao { * @param guestNetwork */ void addRouterToNetwork(DomainRouterVO router, Network guestNetwork); + + /** + * @param routerId + * @param guestNetworkId + */ + void removeRouterFromNetwork(long routerId, long guestNetworkId); } diff --git a/server/src/com/cloud/vm/dao/DomainRouterDaoImpl.java b/server/src/com/cloud/vm/dao/DomainRouterDaoImpl.java index bac97751df3..a3285f03fcc 100755 --- a/server/src/com/cloud/vm/dao/DomainRouterDaoImpl.java +++ b/server/src/com/cloud/vm/dao/DomainRouterDaoImpl.java @@ -275,7 +275,8 @@ public class DomainRouterDaoImpl extends GenericDaoBase im @Override @DB public void addRouterToNetwork(DomainRouterVO router, Network guestNetwork) { - + Transaction txn = Transaction.currentTxn(); + txn.start(); //1) add router to network RouterNetworkVO routerNtwkMap = new RouterNetworkVO(router.getId(), guestNetwork.getId(), guestNetwork.getGuestType()); _routerNetworkDao.persist(routerNtwkMap); @@ -287,6 +288,13 @@ public class DomainRouterDaoImpl extends GenericDaoBase im router.getType().toString(), guestNetwork.getId()); _userStatsDao.persist(stats); } + txn.commit(); + } + + @Override + public void removeRouterFromNetwork(long routerId, long guestNetworkId) { + RouterNetworkVO routerNtwkMap = _routerNetworkDao.findByRouterAndNetwork(routerId, guestNetworkId); + _routerNetworkDao.remove(routerNtwkMap.getId()); } @Override diff --git a/server/test/com/cloud/vm/MockVirtualMachineManagerImpl.java b/server/test/com/cloud/vm/MockVirtualMachineManagerImpl.java index 5a65a6d13f9..f72e7e85cfc 100755 --- a/server/test/com/cloud/vm/MockVirtualMachineManagerImpl.java +++ b/server/test/com/cloud/vm/MockVirtualMachineManagerImpl.java @@ -29,6 +29,7 @@ import com.cloud.exception.OperationTimedoutException; import com.cloud.exception.ResourceUnavailableException; import com.cloud.exception.VirtualMachineMigrationException; import com.cloud.hypervisor.Hypervisor.HypervisorType; +import com.cloud.network.Network; import com.cloud.network.NetworkVO; import com.cloud.offering.ServiceOffering; import com.cloud.service.ServiceOfferingVO; @@ -235,4 +236,22 @@ public class MockVirtualMachineManagerImpl implements VirtualMachineManager { return false; } + /* (non-Javadoc) + * @see com.cloud.vm.VirtualMachineManager#addVmToNetwork(com.cloud.vm.VirtualMachine, com.cloud.network.Network) + */ + @Override + public boolean addVmToNetwork(VirtualMachine vm, Network network) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see com.cloud.vm.VirtualMachineManager#removeVmFromNetwork(com.cloud.vm.VirtualMachine, com.cloud.network.Network) + */ + @Override + public boolean removeVmFromNetwork(VirtualMachine vm, Network network) { + // TODO Auto-generated method stub + return false; + } + } diff --git a/wscript b/wscript index e84a62c69da..8e426ddda6b 100644 --- a/wscript +++ b/wscript @@ -3,7 +3,7 @@ # the following two variables are used by the target "waf dist" # if you change 'em here, you need to change it also in cloud.spec, add a %changelog entry there, and add an entry in debian/changelog -VERSION = '3.0.3.2012-05-23T23:17:18Z' +VERSION = '3.0.3.2012-05-24T22:26:07Z' APPNAME = 'cloud' import shutil,os