diff --git a/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDao.java b/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDao.java index 7f1ec4d40da..e5843b69499 100644 --- a/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDao.java +++ b/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDao.java @@ -43,6 +43,8 @@ public interface DataCenterIpAddressDao extends GenericDao AllFieldsSearch; private final GenericSearchBuilder AllIpCount; + private final GenericSearchBuilder AllIpCountForDc; private final GenericSearchBuilder AllAllocatedIpCount; + private final GenericSearchBuilder AllAllocatedIpCountForDc; @Override @DB @@ -221,6 +223,20 @@ public class DataCenterIpAddressDaoImpl extends GenericDaoBase sc; + if (onlyCountAllocated) { + sc = AllAllocatedIpCountForDc.create(); + } else { + sc = AllIpCountForDc.create(); + } + + sc.setParameters("data_center_id", dcId); + List count = customSearch(sc, null); + return count.get(0); + } + public DataCenterIpAddressDaoImpl() { super(); @@ -239,10 +255,21 @@ public class DataCenterIpAddressDaoImpl extends GenericDaoBase { List listStaticNatPublicIps(long networkId); + int countIPs(long dcId, boolean onlyCountAllocated); + int countIPs(long dcId, long vlanDbId, boolean onlyCountAllocated); int countIPs(long dcId, Long accountId, String vlanId, String vlanGateway, String vlanNetmask); diff --git a/engine/schema/src/com/cloud/network/dao/IPAddressDaoImpl.java b/engine/schema/src/com/cloud/network/dao/IPAddressDaoImpl.java index 5122876adf8..61bc1b7b27c 100644 --- a/engine/schema/src/com/cloud/network/dao/IPAddressDaoImpl.java +++ b/engine/schema/src/com/cloud/network/dao/IPAddressDaoImpl.java @@ -55,7 +55,9 @@ public class IPAddressDaoImpl extends GenericDaoBase implemen protected SearchBuilder AllFieldsSearch; protected SearchBuilder VlanDbIdSearchUnallocated; protected GenericSearchBuilder AllIpCount; + protected GenericSearchBuilder AllIpCountForDc; protected GenericSearchBuilder AllocatedIpCount; + protected GenericSearchBuilder AllocatedIpCountForDc; protected GenericSearchBuilder AllIpCountForDashboard; protected SearchBuilder DeleteAllExceptGivenIp; protected GenericSearchBuilder AllocatedIpCountForAccount; @@ -100,6 +102,11 @@ public class IPAddressDaoImpl extends GenericDaoBase implemen AllIpCount.and("vlan", AllIpCount.entity().getVlanId(), Op.EQ); AllIpCount.done(); + AllIpCountForDc = createSearchBuilder(Integer.class); + AllIpCountForDc.select(null, Func.COUNT, AllIpCountForDc.entity().getAddress()); + AllIpCountForDc.and("dc", AllIpCountForDc.entity().getDataCenterId(), Op.EQ); + AllIpCountForDc.done(); + AllocatedIpCount = createSearchBuilder(Integer.class); AllocatedIpCount.select(null, Func.COUNT, AllocatedIpCount.entity().getAddress()); AllocatedIpCount.and("dc", AllocatedIpCount.entity().getDataCenterId(), Op.EQ); @@ -107,6 +114,12 @@ public class IPAddressDaoImpl extends GenericDaoBase implemen AllocatedIpCount.and("allocated", AllocatedIpCount.entity().getAllocatedTime(), Op.NNULL); AllocatedIpCount.done(); + AllocatedIpCountForDc = createSearchBuilder(Integer.class); + AllocatedIpCountForDc.select(null, Func.COUNT, AllocatedIpCountForDc.entity().getAddress()); + AllocatedIpCountForDc.and("dc", AllocatedIpCountForDc.entity().getDataCenterId(), Op.EQ); + AllocatedIpCountForDc.and("allocated", AllocatedIpCountForDc.entity().getAllocatedTime(), Op.NNULL); + AllocatedIpCountForDc.done(); + AllIpCountForDashboard = createSearchBuilder(Integer.class); AllIpCountForDashboard.select(null, Func.COUNT, AllIpCountForDashboard.entity().getAddress()); AllIpCountForDashboard.and("dc", AllIpCountForDashboard.entity().getDataCenterId(), Op.EQ); @@ -281,6 +294,14 @@ public class IPAddressDaoImpl extends GenericDaoBase implemen return findOneBy(sc); } + @Override + public int countIPs(long dcId, boolean onlyCountAllocated) { + SearchCriteria sc = onlyCountAllocated ? AllocatedIpCountForDc.create() : AllIpCountForDc.create(); + sc.setParameters("dc", dcId); + + return customSearch(sc, null).get(0); + } + @Override public int countIPs(long dcId, long vlanId, boolean onlyCountAllocated) { SearchCriteria sc = onlyCountAllocated ? AllocatedIpCount.create() : AllIpCount.create(); diff --git a/engine/schema/src/com/cloud/storage/dao/VolumeDao.java b/engine/schema/src/com/cloud/storage/dao/VolumeDao.java index 343ec2a56de..4959ce4d63b 100644 --- a/engine/schema/src/com/cloud/storage/dao/VolumeDao.java +++ b/engine/schema/src/com/cloud/storage/dao/VolumeDao.java @@ -45,6 +45,10 @@ public interface VolumeDao extends GenericDao, StateDao findByInstanceIdDestroyed(long vmId); + List findByPod(long podId); + + List findByDc(long dcId); + List findByAccountAndPod(long accountId, long podId); List findByTemplateAndZone(long templateId, long zoneId); diff --git a/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java b/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java index 85d08b854c0..f5738477540 100644 --- a/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java +++ b/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java @@ -189,6 +189,22 @@ public class VolumeDaoImpl extends GenericDaoBase implements Vol return listBy(sc); } + @Override + public List findByPod(long podId) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("pod", podId); + + return listBy(sc); + } + + @Override + public List findByDc(long dcId) { + SearchCriteria sc = AllFieldsSearch.create(); + sc.setParameters("dcId", dcId); + + return listBy(sc); + } + @Override public List findByAccountAndPod(long accountId, long podId) { SearchCriteria sc = AllFieldsSearch.create(); @@ -306,6 +322,7 @@ public class VolumeDaoImpl extends GenericDaoBase implements Vol AllFieldsSearch = createSearchBuilder(); AllFieldsSearch.and("state", AllFieldsSearch.entity().getState(), Op.EQ); AllFieldsSearch.and("accountId", AllFieldsSearch.entity().getAccountId(), Op.EQ); + AllFieldsSearch.and("dcId", AllFieldsSearch.entity().getDataCenterId(), Op.EQ); AllFieldsSearch.and("pod", AllFieldsSearch.entity().getPodId(), Op.EQ); AllFieldsSearch.and("instanceId", AllFieldsSearch.entity().getInstanceId(), Op.EQ); AllFieldsSearch.and("deviceId", AllFieldsSearch.entity().getDeviceId(), Op.EQ); diff --git a/engine/schema/src/com/cloud/vm/dao/VMInstanceDaoImpl.java b/engine/schema/src/com/cloud/vm/dao/VMInstanceDaoImpl.java index be0e3668e7a..427c534a094 100644 --- a/engine/schema/src/com/cloud/vm/dao/VMInstanceDaoImpl.java +++ b/engine/schema/src/com/cloud/vm/dao/VMInstanceDaoImpl.java @@ -155,6 +155,7 @@ public class VMInstanceDaoImpl extends GenericDaoBase implem AllFieldsSearch.and("lastHost", AllFieldsSearch.entity().getLastHostId(), Op.EQ); AllFieldsSearch.and("state", AllFieldsSearch.entity().getState(), Op.EQ); AllFieldsSearch.and("zone", AllFieldsSearch.entity().getDataCenterId(), Op.EQ); + AllFieldsSearch.and("pod", AllFieldsSearch.entity().getPodIdToDeployIn(), Op.EQ); AllFieldsSearch.and("type", AllFieldsSearch.entity().getType(), Op.EQ); AllFieldsSearch.and("account", AllFieldsSearch.entity().getAccountId(), Op.EQ); AllFieldsSearch.done(); diff --git a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java index fc7bff9c2aa..69e70e6cd9e 100644 --- a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java +++ b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java @@ -19,8 +19,6 @@ package com.cloud.configuration; import java.net.URI; import java.sql.Date; import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -134,6 +132,7 @@ import com.cloud.exception.PermissionDeniedException; import com.cloud.exception.ResourceAllocationException; import com.cloud.exception.ResourceUnavailableException; import com.cloud.gpu.GPU; +import com.cloud.host.dao.HostDao; import com.cloud.hypervisor.Hypervisor.HypervisorType; import com.cloud.network.IpAddressManager; import com.cloud.network.Network; @@ -182,6 +181,7 @@ import com.cloud.storage.Storage.ProvisioningType; import com.cloud.storage.Storage.StoragePoolType; import com.cloud.storage.StorageManager; import com.cloud.storage.dao.DiskOfferingDao; +import com.cloud.storage.dao.VolumeDao; import com.cloud.test.IPRangeConfig; import com.cloud.user.Account; import com.cloud.user.AccountDetailVO; @@ -214,6 +214,7 @@ import com.cloud.vm.dao.NicDao; import com.cloud.vm.dao.NicIpAliasDao; import com.cloud.vm.dao.NicIpAliasVO; import com.cloud.vm.dao.NicSecondaryIpDao; +import com.cloud.vm.dao.VMInstanceDao; @Local(value = {ConfigurationManager.class, ConfigurationService.class}) public class ConfigurationManagerImpl extends ManagerBase implements ConfigurationManager, ConfigurationService, Configurable { @@ -228,6 +229,14 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati @Inject HostPodDao _podDao; @Inject + HostDao _hostDao; + @Inject + VolumeDao _volumeDao; + @Inject + VMInstanceDao _vmInstanceDao; + //@Inject + //VmwareDatacenterZoneMapDao _vmwareDatacenterZoneMapDao; + @Inject AccountVlanMapDao _accountVlanMapDao; @Inject PodVlanMapDao _podVlanMapDao; @@ -878,81 +887,34 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati return count > 0; } - @DB protected void checkIfPodIsDeletable(final long podId) { - final List> tablesToCheck = new ArrayList>(); - final HostPodVO pod = _podDao.findById(podId); + final String errorMsg = "The pod cannot be deleted because "; + // Check if there are allocated private IP addresses in the pod if (_privateIpAddressDao.countIPs(podId, pod.getDataCenterId(), true) != 0) { - throw new CloudRuntimeException("There are private IP addresses allocated for this pod"); + throw new CloudRuntimeException(errorMsg + "there are private IP addresses allocated in this pod."); } - final List volumes = new ArrayList(); - volumes.add(0, "volumes"); - volumes.add(1, "pod_id"); - volumes.add(2, "there are storage volumes for this pod"); - tablesToCheck.add(volumes); + // Check if there are any non-removed volumes in the pod. + if (!_volumeDao.findByPod(podId).isEmpty()) { + throw new CloudRuntimeException(errorMsg + "there are storage volumes in this pod."); + } - final List host = new ArrayList(); - host.add(0, "host"); - host.add(1, "pod_id"); - host.add(2, "there are servers running in this pod"); - tablesToCheck.add(host); + // Check if there are any non-removed hosts in the pod. + if (!_hostDao.findByPodId(podId).isEmpty()) { + throw new CloudRuntimeException(errorMsg + "there are servers in this pod."); + } - final List vmInstance = new ArrayList(); - vmInstance.add(0, "vm_instance"); - vmInstance.add(1, "pod_id"); - vmInstance.add(2, "there are virtual machines running in this pod"); - tablesToCheck.add(vmInstance); + // Check if there are any non-removed vms in the pod. + if (!_vmInstanceDao.listByPodId(podId).isEmpty()) { + throw new CloudRuntimeException(errorMsg + "there are virtual machines in this pod."); + } - final List cluster = new ArrayList(); - cluster.add(0, "cluster"); - cluster.add(1, "pod_id"); - cluster.add(2, "there are clusters in this pod"); - tablesToCheck.add(cluster); - - for (final List table : tablesToCheck) { - final String tableName = table.get(0); - final String column = table.get(1); - final String errorMsg = table.get(2); - - String dbName; - if (tableName.equals("event") || tableName.equals("cloud_usage") || tableName.equals("usage_vm_instance") || tableName.equals("usage_ip_address") - || tableName.equals("usage_network") || tableName.equals("usage_job") || tableName.equals("account") || tableName.equals("user_statistics")) { - dbName = "cloud_usage"; - } else { - dbName = "cloud"; - } - - String selectSql = "SELECT * FROM `?`.`?` WHERE ? = ?"; - - if(tableName.equals("vm_instance")) { - selectSql += " AND state != ? AND removed IS NULL"; - } - - if (tableName.equals("host") || tableName.equals("cluster") || tableName.equals("volumes")) { - selectSql += " and removed IS NULL"; - } - - final TransactionLegacy txn = TransactionLegacy.currentTxn(); - try { - final PreparedStatement stmt = txn.prepareAutoCloseStatement(selectSql); - stmt.setString(1,dbName); - stmt.setString(2,tableName); - stmt.setString(3,column); - stmt.setLong(4, podId); - if(tableName.equals("vm_instance")) { - stmt.setString(5, VirtualMachine.State.Expunging.toString()); - } - final ResultSet rs = stmt.executeQuery(); - if (rs != null && rs.next()) { - throw new CloudRuntimeException("The pod cannot be deleted because " + errorMsg); - } - } catch (final SQLException ex) { - throw new CloudRuntimeException("The Management Server failed to detect if pod is deletable. Please contact Cloud Support."); - } + // Check if there are any non-removed clusters in the pod. + if (!_clusterDao.listByPodId(podId).isEmpty()) { + throw new CloudRuntimeException(errorMsg + "there are clusters in this pod."); } } @@ -1334,104 +1296,49 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati @DB protected void checkIfZoneIsDeletable(final long zoneId) { - final List> tablesToCheck = new ArrayList>(); + final String errorMsg = "The zone cannot be deleted because "; - final List host = new ArrayList(); - host.add(0, "host"); - host.add(1, "data_center_id"); - host.add(2, "there are servers running in this zone"); - tablesToCheck.add(host); - final List hostPodRef = new ArrayList(); - hostPodRef.add(0, "host_pod_ref"); - hostPodRef.add(1, "data_center_id"); - hostPodRef.add(2, "there are pods in this zone"); - tablesToCheck.add(hostPodRef); - final List privateIP = new ArrayList(); - privateIP.add(0, "op_dc_ip_address_alloc"); - privateIP.add(1, "data_center_id"); - privateIP.add(2, "there are private IP addresses allocated for this zone"); - tablesToCheck.add(privateIP); - - final List publicIP = new ArrayList(); - publicIP.add(0, "user_ip_address"); - publicIP.add(1, "data_center_id"); - publicIP.add(2, "there are public IP addresses allocated for this zone"); - tablesToCheck.add(publicIP); - - final List vmInstance = new ArrayList(); - vmInstance.add(0, "vm_instance"); - vmInstance.add(1, "data_center_id"); - vmInstance.add(2, "there are virtual machines running in this zone"); - tablesToCheck.add(vmInstance); - - final List volumes = new ArrayList(); - volumes.add(0, "volumes"); - volumes.add(1, "data_center_id"); - volumes.add(2, "there are storage volumes for this zone"); - tablesToCheck.add(volumes); - - final List physicalNetworks = new ArrayList(); - physicalNetworks.add(0, "physical_network"); - physicalNetworks.add(1, "data_center_id"); - physicalNetworks.add(2, "there are physical networks in this zone"); - tablesToCheck.add(physicalNetworks); - - final List vmwareDcs = new ArrayList(); - vmwareDcs.add(0, "vmware_data_center_zone_map"); - vmwareDcs.add(1, "zone_id"); - vmwareDcs.add(2, "there are VMware datacenters associated with this zone. Remove VMware DC from this zone."); - tablesToCheck.add(vmwareDcs); - - for (final List table : tablesToCheck) { - final String tableName = table.get(0); - final String column = table.get(1); - final String errorMsg = table.get(2); - - final String dbName = "cloud"; - - String selectSql = "SELECT * FROM `?`.`?` WHERE ? = ?"; - - if (tableName.equals("op_dc_vnet_alloc")) { - selectSql += " AND taken IS NOT NULL"; - } - - if (tableName.equals("user_ip_address")) { - selectSql += " AND state!='Free'"; - } - - if (tableName.equals("op_dc_ip_address_alloc")) { - selectSql += " AND taken IS NOT NULL"; - } - - if (tableName.equals("host_pod_ref") || tableName.equals("host") || tableName.equals("volumes") || tableName.equals("physical_network")) { - selectSql += " AND removed is NULL"; - } - - if (tableName.equals("vm_instance")) { - selectSql += " AND state != ? AND removed IS NULL"; - } - - final TransactionLegacy txn = TransactionLegacy.currentTxn(); - try { - final PreparedStatement stmt = txn.prepareAutoCloseStatement(selectSql); - stmt.setString(1,dbName); - stmt.setString(2,tableName); - stmt.setString(3,column); - stmt.setLong(4, zoneId); - if (tableName.equals("vm_instance")) { - stmt.setString(5, VirtualMachine.State.Expunging.toString()); - } - final ResultSet rs = stmt.executeQuery(); - if (rs != null && rs.next()) { - throw new CloudRuntimeException("The zone is not deletable because " + errorMsg); - } - } catch (final SQLException ex) { - throw new CloudRuntimeException("The Management Server failed to detect if zone is deletable. Please contact Cloud Support."); - } + // Check if there are any non-removed hosts in the zone. + if (!_hostDao.listByDataCenterId(zoneId).isEmpty()) { + throw new CloudRuntimeException(errorMsg + "there are servers in this zone."); } + // Check if there are any non-removed pods in the zone. + if (!_podDao.listByDataCenterId(zoneId).isEmpty()) { + throw new CloudRuntimeException(errorMsg + "there are pods in this zone."); + } + + // Check if there are allocated private IP addresses in the zone. + if (_privateIpAddressDao.countIPs(zoneId, true) != 0) { + throw new CloudRuntimeException(errorMsg + "there are private IP addresses allocated in this zone."); + } + + // Check if there are allocated public IP addresses in the zone. + if (_publicIpAddressDao.countIPs(zoneId, true) != 0) { + throw new CloudRuntimeException(errorMsg + "there are public IP addresses allocated in this zone."); + } + + // Check if there are any non-removed vms in the zone. + if (!_vmInstanceDao.listByZoneId(zoneId).isEmpty()) { + throw new CloudRuntimeException(errorMsg + "there are virtual machines in this zone."); + } + + // Check if there are any non-removed volumes in the zone. + if (!_volumeDao.findByDc(zoneId).isEmpty()) { + throw new CloudRuntimeException(errorMsg + "there are storage volumes in this zone."); + } + + // Check if there are any non-removed physical networks in the zone. + if (!_physicalNetworkDao.listByZone(zoneId).isEmpty()) { + throw new CloudRuntimeException(errorMsg + "there are physical networks in this zone."); + } + + // Check if there are any non-removed VMware datacenters in the zone. + //if (_vmwareDatacenterZoneMapDao.findByZoneId(zoneId) != null) { + // throw new CloudRuntimeException(errorMsg + "there are VMware datacenters in this zone."); + //} } private void checkZoneParameters(final String zoneName, final String dns1, final String dns2, final String internalDns1, final String internalDns2, final boolean checkForDuplicates, final Long domainId, diff --git a/server/test/com/cloud/configuration/ConfigurationManagerTest.java b/server/test/com/cloud/configuration/ConfigurationManagerTest.java index c43e3fa4898..d30dacf9a69 100644 --- a/server/test/com/cloud/configuration/ConfigurationManagerTest.java +++ b/server/test/com/cloud/configuration/ConfigurationManagerTest.java @@ -30,6 +30,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Random; import java.util.UUID; import com.cloud.user.User; @@ -48,14 +49,21 @@ import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationSe import com.cloud.configuration.Resource.ResourceType; import com.cloud.dc.AccountVlanMapVO; +import com.cloud.dc.ClusterVO; import com.cloud.dc.DataCenter.NetworkType; import com.cloud.dc.DataCenterVO; +import com.cloud.dc.HostPodVO; import com.cloud.dc.Vlan; import com.cloud.dc.VlanVO; import com.cloud.dc.dao.AccountVlanMapDao; +import com.cloud.dc.dao.ClusterDao; import com.cloud.dc.dao.DataCenterDao; +import com.cloud.dc.dao.DataCenterIpAddressDao; +import com.cloud.dc.dao.HostPodDao; import com.cloud.dc.dao.VlanDao; import com.cloud.exception.InvalidParameterValueException; +import com.cloud.host.HostVO; +import com.cloud.host.dao.HostDao; import com.cloud.network.IpAddressManager; import com.cloud.network.Network; import com.cloud.network.NetworkModel; @@ -63,7 +71,11 @@ import com.cloud.network.Network.Capability; import com.cloud.network.dao.FirewallRulesDao; import com.cloud.network.dao.IPAddressDao; import com.cloud.network.dao.IPAddressVO; +import com.cloud.network.dao.PhysicalNetworkDao; +import com.cloud.network.dao.PhysicalNetworkVO; import com.cloud.projects.ProjectManager; +import com.cloud.storage.VolumeVO; +import com.cloud.storage.dao.VolumeDao; import com.cloud.user.Account; import com.cloud.user.AccountManager; import com.cloud.user.AccountVO; @@ -71,7 +83,10 @@ import com.cloud.user.ResourceLimitService; import com.cloud.user.UserVO; import com.cloud.user.dao.AccountDao; import com.cloud.utils.db.TransactionLegacy; +import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.net.Ip; +import com.cloud.vm.VMInstanceVO; +import com.cloud.vm.dao.VMInstanceDao; public class ConfigurationManagerTest { @@ -109,12 +124,25 @@ public class ConfigurationManagerTest { IpAddressManager _ipAddrMgr; @Mock NetworkModel _networkModel; + @Mock + DataCenterIpAddressDao _privateIpAddressDao; + @Mock + VolumeDao _volumeDao; + @Mock + HostDao _hostDao; + @Mock + VMInstanceDao _vmInstanceDao; + @Mock + ClusterDao _clusterDao; + @Mock + HostPodDao _podDao; + @Mock + PhysicalNetworkDao _physicalNetworkDao; VlanVO vlan = new VlanVO(Vlan.VlanType.VirtualNetwork, "vlantag", "vlangateway", "vlannetmask", 1L, "iprange", 1L, 1L, null, null, null); @Mock Network network; - @Mock Account account; @@ -133,6 +161,14 @@ public class ConfigurationManagerTest { configurationMgr._firewallDao = _firewallDao; configurationMgr._ipAddrMgr = _ipAddrMgr; configurationMgr._networkModel = _networkModel; + configurationMgr._privateIpAddressDao = _privateIpAddressDao; + configurationMgr._volumeDao = _volumeDao; + configurationMgr._hostDao = _hostDao; + configurationMgr._vmInstanceDao = _vmInstanceDao; + configurationMgr._clusterDao = _clusterDao; + configurationMgr._podDao = _podDao; + configurationMgr._physicalNetworkDao = _physicalNetworkDao; + Account account = new AccountVO("testaccount", 1, "networkdomain", (short)0, UUID.randomUUID().toString()); when(configurationMgr._accountMgr.getAccount(anyLong())).thenReturn(account); @@ -533,4 +569,230 @@ public class ConfigurationManagerTest { Mockito.when(_accountMgr.getAccount(1l)).thenReturn(account); Assert.assertNotNull(configurationMgr.getVlanAccount(42l)); } -} + + @Test + public void checkIfPodIsDeletableSuccessTest() { + HostPodVO hostPodVO = Mockito.mock(HostPodVO.class); + Mockito.when(hostPodVO.getDataCenterId()).thenReturn(new Random().nextLong()); + Mockito.when(_podDao.findById(anyLong())).thenReturn(hostPodVO); + + Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyLong(), anyBoolean())).thenReturn(0); + Mockito.when(_volumeDao.findByPod(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_hostDao.findByPodId(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_vmInstanceDao.listByPodId(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_clusterDao.listByPodId(anyLong())).thenReturn(new ArrayList()); + + configurationMgr.checkIfPodIsDeletable(new Random().nextLong()); + } + + @Test(expected = CloudRuntimeException.class) + public void checkIfPodIsDeletableFailureOnPrivateIpAddressTest() { + HostPodVO hostPodVO = Mockito.mock(HostPodVO.class); + Mockito.when(hostPodVO.getDataCenterId()).thenReturn(new Random().nextLong()); + Mockito.when(_podDao.findById(anyLong())).thenReturn(hostPodVO); + + Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyLong(), anyBoolean())).thenReturn(1); + Mockito.when(_volumeDao.findByPod(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_hostDao.findByPodId(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_vmInstanceDao.listByPodId(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_clusterDao.listByPodId(anyLong())).thenReturn(new ArrayList()); + + configurationMgr.checkIfPodIsDeletable(new Random().nextLong()); + } + + @Test(expected = CloudRuntimeException.class) + public void checkIfPodIsDeletableFailureOnVolumeTest() { + HostPodVO hostPodVO = Mockito.mock(HostPodVO.class); + Mockito.when(hostPodVO.getDataCenterId()).thenReturn(new Random().nextLong()); + Mockito.when(_podDao.findById(anyLong())).thenReturn(hostPodVO); + + VolumeVO volumeVO = Mockito.mock(VolumeVO.class); + ArrayList arrayList = new ArrayList(); + arrayList.add(volumeVO); + Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyLong(), anyBoolean())).thenReturn(0); + Mockito.when(_volumeDao.findByPod(anyLong())).thenReturn(arrayList); + Mockito.when(_hostDao.findByPodId(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_vmInstanceDao.listByPodId(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_clusterDao.listByPodId(anyLong())).thenReturn(new ArrayList()); + + configurationMgr.checkIfPodIsDeletable(new Random().nextLong()); + } + + @Test(expected = CloudRuntimeException.class) + public void checkIfPodIsDeletableFailureOnHostTest() { + HostPodVO hostPodVO = Mockito.mock(HostPodVO.class); + Mockito.when(hostPodVO.getDataCenterId()).thenReturn(new Random().nextLong()); + Mockito.when(_podDao.findById(anyLong())).thenReturn(hostPodVO); + + HostVO hostVO = Mockito.mock(HostVO.class); + ArrayList arrayList = new ArrayList(); + arrayList.add(hostVO); + Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyLong(), anyBoolean())).thenReturn(0); + Mockito.when(_volumeDao.findByPod(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_hostDao.findByPodId(anyLong())).thenReturn(arrayList); + Mockito.when(_vmInstanceDao.listByPodId(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_clusterDao.listByPodId(anyLong())).thenReturn(new ArrayList()); + + configurationMgr.checkIfPodIsDeletable(new Random().nextLong()); + } + + @Test(expected = CloudRuntimeException.class) + public void checkIfPodIsDeletableFailureOnVmInstanceTest() { + HostPodVO hostPodVO = Mockito.mock(HostPodVO.class); + Mockito.when(hostPodVO.getDataCenterId()).thenReturn(new Random().nextLong()); + Mockito.when(_podDao.findById(anyLong())).thenReturn(hostPodVO); + + VMInstanceVO vMInstanceVO = Mockito.mock(VMInstanceVO.class); + ArrayList arrayList = new ArrayList(); + arrayList.add(vMInstanceVO); + Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyLong(), anyBoolean())).thenReturn(0); + Mockito.when(_volumeDao.findByPod(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_hostDao.findByPodId(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_vmInstanceDao.listByPodId(anyLong())).thenReturn(arrayList); + Mockito.when(_clusterDao.listByPodId(anyLong())).thenReturn(new ArrayList()); + + configurationMgr.checkIfPodIsDeletable(new Random().nextLong()); + } + + @Test(expected = CloudRuntimeException.class) + public void checkIfPodIsDeletableFailureOnClusterTest() { + HostPodVO hostPodVO = Mockito.mock(HostPodVO.class); + Mockito.when(hostPodVO.getDataCenterId()).thenReturn(new Random().nextLong()); + Mockito.when(_podDao.findById(anyLong())).thenReturn(hostPodVO); + + ClusterVO clusterVO = Mockito.mock(ClusterVO.class); + ArrayList arrayList = new ArrayList(); + arrayList.add(clusterVO); + Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyLong(), anyBoolean())).thenReturn(0); + Mockito.when(_volumeDao.findByPod(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_hostDao.findByPodId(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_vmInstanceDao.listByPodId(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_clusterDao.listByPodId(anyLong())).thenReturn(arrayList); + + configurationMgr.checkIfPodIsDeletable(new Random().nextLong()); + } + + @Test + public void checkIfZoneIsDeletableSuccessTest() { + Mockito.when(_hostDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_podDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0); + Mockito.when(_publicIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0); + Mockito.when(_vmInstanceDao.listByZoneId(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_volumeDao.findByDc(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_physicalNetworkDao.listByZone(anyLong())).thenReturn(new ArrayList()); + + configurationMgr.checkIfZoneIsDeletable(new Random().nextLong()); + } + + @Test(expected = CloudRuntimeException.class) + public void checkIfZoneIsDeletableFailureOnHostTest() { + HostVO hostVO = Mockito.mock(HostVO.class); + ArrayList arrayList = new ArrayList(); + arrayList.add(hostVO); + + Mockito.when(_hostDao.listByDataCenterId(anyLong())).thenReturn(arrayList); + Mockito.when(_podDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0); + Mockito.when(_publicIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0); + Mockito.when(_vmInstanceDao.listByZoneId(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_volumeDao.findByDc(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_physicalNetworkDao.listByZone(anyLong())).thenReturn(new ArrayList()); + + configurationMgr.checkIfZoneIsDeletable(new Random().nextLong()); + } + + @Test(expected = CloudRuntimeException.class) + public void checkIfZoneIsDeletableFailureOnPodTest() { + HostPodVO hostPodVO = Mockito.mock(HostPodVO.class); + ArrayList arrayList = new ArrayList(); + arrayList.add(hostPodVO); + + Mockito.when(_hostDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_podDao.listByDataCenterId(anyLong())).thenReturn(arrayList); + Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0); + Mockito.when(_publicIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0); + Mockito.when(_vmInstanceDao.listByZoneId(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_volumeDao.findByDc(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_physicalNetworkDao.listByZone(anyLong())).thenReturn(new ArrayList()); + + configurationMgr.checkIfZoneIsDeletable(new Random().nextLong()); + } + + @Test(expected = CloudRuntimeException.class) + public void checkIfZoneIsDeletableFailureOnPrivateIpAddressTest() { + Mockito.when(_hostDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_podDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(1); + Mockito.when(_publicIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0); + Mockito.when(_vmInstanceDao.listByZoneId(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_volumeDao.findByDc(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_physicalNetworkDao.listByZone(anyLong())).thenReturn(new ArrayList()); + + configurationMgr.checkIfZoneIsDeletable(new Random().nextLong()); + } + + @Test(expected = CloudRuntimeException.class) + public void checkIfZoneIsDeletableFailureOnPublicIpAddressTest() { + Mockito.when(_hostDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_podDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0); + Mockito.when(_publicIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(1); + Mockito.when(_vmInstanceDao.listByZoneId(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_volumeDao.findByDc(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_physicalNetworkDao.listByZone(anyLong())).thenReturn(new ArrayList()); + + configurationMgr.checkIfZoneIsDeletable(new Random().nextLong()); + } + + @Test(expected = CloudRuntimeException.class) + public void checkIfZoneIsDeletableFailureOnVmInstanceTest() { + VMInstanceVO vMInstanceVO = Mockito.mock(VMInstanceVO.class); + ArrayList arrayList = new ArrayList(); + arrayList.add(vMInstanceVO); + + Mockito.when(_hostDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_podDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0); + Mockito.when(_publicIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0); + Mockito.when(_vmInstanceDao.listByZoneId(anyLong())).thenReturn(arrayList); + Mockito.when(_volumeDao.findByDc(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_physicalNetworkDao.listByZone(anyLong())).thenReturn(new ArrayList()); + + configurationMgr.checkIfZoneIsDeletable(new Random().nextLong()); + } + + @Test(expected = CloudRuntimeException.class) + public void checkIfZoneIsDeletableFailureOnVolumeTest() { + VolumeVO volumeVO = Mockito.mock(VolumeVO.class); + ArrayList arrayList = new ArrayList(); + arrayList.add(volumeVO); + + Mockito.when(_hostDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_podDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0); + Mockito.when(_publicIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0); + Mockito.when(_vmInstanceDao.listByZoneId(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_volumeDao.findByDc(anyLong())).thenReturn(arrayList); + Mockito.when(_physicalNetworkDao.listByZone(anyLong())).thenReturn(new ArrayList()); + + configurationMgr.checkIfZoneIsDeletable(new Random().nextLong()); + } + + @Test(expected = CloudRuntimeException.class) + public void checkIfZoneIsDeletableFailureOnPhysicalNetworkTest() { + PhysicalNetworkVO physicalNetworkVO = Mockito.mock(PhysicalNetworkVO.class); + ArrayList arrayList = new ArrayList(); + arrayList.add(physicalNetworkVO); + + Mockito.when(_hostDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_podDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0); + Mockito.when(_publicIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0); + Mockito.when(_vmInstanceDao.listByZoneId(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_volumeDao.findByDc(anyLong())).thenReturn(new ArrayList()); + Mockito.when(_physicalNetworkDao.listByZone(anyLong())).thenReturn(arrayList); + + configurationMgr.checkIfZoneIsDeletable(new Random().nextLong()); + } +} \ No newline at end of file