You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by wi...@apache.org on 2015/09/23 14:00:11 UTC

[1/9] git commit: updated refs/heads/master to a0f8f56

Repository: cloudstack
Updated Branches:
  refs/heads/master a601ae445 -> a0f8f56a5


Added findByPodId(Long podId) to HostDao and HostDaoImpl.


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/0648cb98
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/0648cb98
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/0648cb98

Branch: refs/heads/master
Commit: 0648cb98042eba19eb581c9d7f5b4d0c69ccd4e1
Parents: 1525ac0
Author: Boris Schrijver <bo...@pcextreme.nl>
Authored: Wed Sep 16 22:13:10 2015 +0200
Committer: Boris Schrijver <bo...@pcextreme.nl>
Committed: Wed Sep 16 22:13:10 2015 +0200

----------------------------------------------------------------------
 engine/schema/src/com/cloud/storage/dao/VolumeDao.java     | 2 ++
 engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java | 8 ++++++++
 2 files changed, 10 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/0648cb98/engine/schema/src/com/cloud/storage/dao/VolumeDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/storage/dao/VolumeDao.java b/engine/schema/src/com/cloud/storage/dao/VolumeDao.java
index 343ec2a..05aa3cf 100644
--- a/engine/schema/src/com/cloud/storage/dao/VolumeDao.java
+++ b/engine/schema/src/com/cloud/storage/dao/VolumeDao.java
@@ -45,6 +45,8 @@ public interface VolumeDao extends GenericDao<VolumeVO, Long>, StateDao<Volume.S
 
     List<VolumeVO> findByInstanceIdDestroyed(long vmId);
 
+    List<VolumeVO> findByPod(long podId);
+
     List<VolumeVO> findByAccountAndPod(long accountId, long podId);
 
     List<VolumeVO> findByTemplateAndZone(long templateId, long zoneId);

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/0648cb98/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java b/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java
index 85d08b8..56e92cf 100644
--- a/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java
+++ b/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java
@@ -190,6 +190,14 @@ public class VolumeDaoImpl extends GenericDaoBase<VolumeVO, Long> implements Vol
     }
 
     @Override
+    public List<VolumeVO> findByPod(long podId) {
+        SearchCriteria<VolumeVO> sc = AllFieldsSearch.create();
+        sc.setParameters("pod", podId);
+
+        return listBy(sc);
+    }
+
+    @Override
     public List<VolumeVO> findByAccountAndPod(long accountId, long podId) {
         SearchCriteria<VolumeVO> sc = AllFieldsSearch.create();
         sc.setParameters("accountId", accountId);


[5/9] git commit: updated refs/heads/master to a0f8f56

Posted by wi...@apache.org.
Added findByDc(long dcId) to VolumeDao and VolumeDaoImpl.


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/0df3357c
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/0df3357c
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/0df3357c

Branch: refs/heads/master
Commit: 0df3357cac73a2191cf4e01d5ef8db48a0bcbc64
Parents: 12fc2b4
Author: Boris Schrijver <bo...@pcextreme.nl>
Authored: Wed Sep 16 22:17:27 2015 +0200
Committer: Boris Schrijver <bo...@pcextreme.nl>
Committed: Wed Sep 16 22:17:27 2015 +0200

----------------------------------------------------------------------
 engine/schema/src/com/cloud/storage/dao/VolumeDao.java     | 2 ++
 engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java | 8 ++++++++
 2 files changed, 10 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/0df3357c/engine/schema/src/com/cloud/storage/dao/VolumeDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/storage/dao/VolumeDao.java b/engine/schema/src/com/cloud/storage/dao/VolumeDao.java
index 05aa3cf..4959ce4 100644
--- a/engine/schema/src/com/cloud/storage/dao/VolumeDao.java
+++ b/engine/schema/src/com/cloud/storage/dao/VolumeDao.java
@@ -47,6 +47,8 @@ public interface VolumeDao extends GenericDao<VolumeVO, Long>, StateDao<Volume.S
 
     List<VolumeVO> findByPod(long podId);
 
+    List<VolumeVO> findByDc(long dcId);
+
     List<VolumeVO> findByAccountAndPod(long accountId, long podId);
 
     List<VolumeVO> findByTemplateAndZone(long templateId, long zoneId);

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/0df3357c/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java b/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java
index 56e92cf..7d0c9c3 100644
--- a/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java
+++ b/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java
@@ -198,6 +198,14 @@ public class VolumeDaoImpl extends GenericDaoBase<VolumeVO, Long> implements Vol
     }
 
     @Override
+    public List<VolumeVO> findByDc(long dcId) {
+        SearchCriteria<VolumeVO> sc = AllFieldsSearch.create();
+        sc.setParameters("data_center_id", dcId);
+
+        return listBy(sc);
+    }
+
+    @Override
     public List<VolumeVO> findByAccountAndPod(long accountId, long podId) {
         SearchCriteria<VolumeVO> sc = AllFieldsSearch.create();
         sc.setParameters("accountId", accountId);


[8/9] git commit: updated refs/heads/master to a0f8f56

Posted by wi...@apache.org.
Added unit tests for checkIfPodIsDeletable() and checkIfZoneIsDeletable().


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/5a3a3d64
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/5a3a3d64
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/5a3a3d64

Branch: refs/heads/master
Commit: 5a3a3d641ddac3575e4d371004b0b38e28c6e897
Parents: fa5f388
Author: Boris Schrijver <bo...@pcextreme.nl>
Authored: Thu Sep 17 17:28:29 2015 +0200
Committer: Boris Schrijver <bo...@pcextreme.nl>
Committed: Thu Sep 17 17:28:29 2015 +0200

----------------------------------------------------------------------
 .../configuration/ConfigurationManagerTest.java | 266 ++++++++++++++++++-
 1 file changed, 264 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/5a3a3d64/server/test/com/cloud/configuration/ConfigurationManagerTest.java
----------------------------------------------------------------------
diff --git a/server/test/com/cloud/configuration/ConfigurationManagerTest.java b/server/test/com/cloud/configuration/ConfigurationManagerTest.java
index c43e3fa..d30dacf 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<VolumeVO>());
+        Mockito.when(_hostDao.findByPodId(anyLong())).thenReturn(new ArrayList<HostVO>());
+        Mockito.when(_vmInstanceDao.listByPodId(anyLong())).thenReturn(new ArrayList<VMInstanceVO>());
+        Mockito.when(_clusterDao.listByPodId(anyLong())).thenReturn(new ArrayList<ClusterVO>());
+
+        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<VolumeVO>());
+        Mockito.when(_hostDao.findByPodId(anyLong())).thenReturn(new ArrayList<HostVO>());
+        Mockito.when(_vmInstanceDao.listByPodId(anyLong())).thenReturn(new ArrayList<VMInstanceVO>());
+        Mockito.when(_clusterDao.listByPodId(anyLong())).thenReturn(new ArrayList<ClusterVO>());
+
+        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<VolumeVO> arrayList = new ArrayList<VolumeVO>();
+        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<HostVO>());
+        Mockito.when(_vmInstanceDao.listByPodId(anyLong())).thenReturn(new ArrayList<VMInstanceVO>());
+        Mockito.when(_clusterDao.listByPodId(anyLong())).thenReturn(new ArrayList<ClusterVO>());
+
+        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<HostVO> arrayList = new ArrayList<HostVO>();
+        arrayList.add(hostVO);
+        Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyLong(), anyBoolean())).thenReturn(0);
+        Mockito.when(_volumeDao.findByPod(anyLong())).thenReturn(new ArrayList<VolumeVO>());
+        Mockito.when(_hostDao.findByPodId(anyLong())).thenReturn(arrayList);
+        Mockito.when(_vmInstanceDao.listByPodId(anyLong())).thenReturn(new ArrayList<VMInstanceVO>());
+        Mockito.when(_clusterDao.listByPodId(anyLong())).thenReturn(new ArrayList<ClusterVO>());
+
+        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<VMInstanceVO> arrayList = new ArrayList<VMInstanceVO>();
+        arrayList.add(vMInstanceVO);
+        Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyLong(), anyBoolean())).thenReturn(0);
+        Mockito.when(_volumeDao.findByPod(anyLong())).thenReturn(new ArrayList<VolumeVO>());
+        Mockito.when(_hostDao.findByPodId(anyLong())).thenReturn(new ArrayList<HostVO>());
+        Mockito.when(_vmInstanceDao.listByPodId(anyLong())).thenReturn(arrayList);
+        Mockito.when(_clusterDao.listByPodId(anyLong())).thenReturn(new ArrayList<ClusterVO>());
+
+        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<ClusterVO> arrayList = new ArrayList<ClusterVO>();
+        arrayList.add(clusterVO);
+        Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyLong(), anyBoolean())).thenReturn(0);
+        Mockito.when(_volumeDao.findByPod(anyLong())).thenReturn(new ArrayList<VolumeVO>());
+        Mockito.when(_hostDao.findByPodId(anyLong())).thenReturn(new ArrayList<HostVO>());
+        Mockito.when(_vmInstanceDao.listByPodId(anyLong())).thenReturn(new ArrayList<VMInstanceVO>());
+        Mockito.when(_clusterDao.listByPodId(anyLong())).thenReturn(arrayList);
+
+        configurationMgr.checkIfPodIsDeletable(new Random().nextLong());
+    }
+
+    @Test
+    public void checkIfZoneIsDeletableSuccessTest() {
+        Mockito.when(_hostDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList<HostVO>());
+        Mockito.when(_podDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList<HostPodVO>());
+        Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
+        Mockito.when(_publicIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
+        Mockito.when(_vmInstanceDao.listByZoneId(anyLong())).thenReturn(new ArrayList<VMInstanceVO>());
+        Mockito.when(_volumeDao.findByDc(anyLong())).thenReturn(new ArrayList<VolumeVO>());
+        Mockito.when(_physicalNetworkDao.listByZone(anyLong())).thenReturn(new ArrayList<PhysicalNetworkVO>());
+
+        configurationMgr.checkIfZoneIsDeletable(new Random().nextLong());
+    }
+
+    @Test(expected = CloudRuntimeException.class)
+    public void checkIfZoneIsDeletableFailureOnHostTest() {
+        HostVO hostVO = Mockito.mock(HostVO.class);
+        ArrayList<HostVO> arrayList = new ArrayList<HostVO>();
+        arrayList.add(hostVO);
+
+        Mockito.when(_hostDao.listByDataCenterId(anyLong())).thenReturn(arrayList);
+        Mockito.when(_podDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList<HostPodVO>());
+        Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
+        Mockito.when(_publicIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
+        Mockito.when(_vmInstanceDao.listByZoneId(anyLong())).thenReturn(new ArrayList<VMInstanceVO>());
+        Mockito.when(_volumeDao.findByDc(anyLong())).thenReturn(new ArrayList<VolumeVO>());
+        Mockito.when(_physicalNetworkDao.listByZone(anyLong())).thenReturn(new ArrayList<PhysicalNetworkVO>());
+
+        configurationMgr.checkIfZoneIsDeletable(new Random().nextLong());
+    }
+
+    @Test(expected = CloudRuntimeException.class)
+    public void checkIfZoneIsDeletableFailureOnPodTest() {
+        HostPodVO hostPodVO = Mockito.mock(HostPodVO.class);
+        ArrayList<HostPodVO> arrayList = new ArrayList<HostPodVO>();
+        arrayList.add(hostPodVO);
+
+        Mockito.when(_hostDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList<HostVO>());
+        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<VMInstanceVO>());
+        Mockito.when(_volumeDao.findByDc(anyLong())).thenReturn(new ArrayList<VolumeVO>());
+        Mockito.when(_physicalNetworkDao.listByZone(anyLong())).thenReturn(new ArrayList<PhysicalNetworkVO>());
+
+        configurationMgr.checkIfZoneIsDeletable(new Random().nextLong());
+    }
+
+    @Test(expected = CloudRuntimeException.class)
+    public void checkIfZoneIsDeletableFailureOnPrivateIpAddressTest() {
+        Mockito.when(_hostDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList<HostVO>());
+        Mockito.when(_podDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList<HostPodVO>());
+        Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(1);
+        Mockito.when(_publicIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
+        Mockito.when(_vmInstanceDao.listByZoneId(anyLong())).thenReturn(new ArrayList<VMInstanceVO>());
+        Mockito.when(_volumeDao.findByDc(anyLong())).thenReturn(new ArrayList<VolumeVO>());
+        Mockito.when(_physicalNetworkDao.listByZone(anyLong())).thenReturn(new ArrayList<PhysicalNetworkVO>());
+
+        configurationMgr.checkIfZoneIsDeletable(new Random().nextLong());
+    }
+
+    @Test(expected = CloudRuntimeException.class)
+    public void checkIfZoneIsDeletableFailureOnPublicIpAddressTest() {
+        Mockito.when(_hostDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList<HostVO>());
+        Mockito.when(_podDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList<HostPodVO>());
+        Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
+        Mockito.when(_publicIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(1);
+        Mockito.when(_vmInstanceDao.listByZoneId(anyLong())).thenReturn(new ArrayList<VMInstanceVO>());
+        Mockito.when(_volumeDao.findByDc(anyLong())).thenReturn(new ArrayList<VolumeVO>());
+        Mockito.when(_physicalNetworkDao.listByZone(anyLong())).thenReturn(new ArrayList<PhysicalNetworkVO>());
+
+        configurationMgr.checkIfZoneIsDeletable(new Random().nextLong());
+    }
+
+    @Test(expected = CloudRuntimeException.class)
+    public void checkIfZoneIsDeletableFailureOnVmInstanceTest() {
+        VMInstanceVO vMInstanceVO = Mockito.mock(VMInstanceVO.class);
+        ArrayList<VMInstanceVO> arrayList = new ArrayList<VMInstanceVO>();
+        arrayList.add(vMInstanceVO);
+
+        Mockito.when(_hostDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList<HostVO>());
+        Mockito.when(_podDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList<HostPodVO>());
+        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<VolumeVO>());
+        Mockito.when(_physicalNetworkDao.listByZone(anyLong())).thenReturn(new ArrayList<PhysicalNetworkVO>());
+
+        configurationMgr.checkIfZoneIsDeletable(new Random().nextLong());
+    }
+
+    @Test(expected = CloudRuntimeException.class)
+    public void checkIfZoneIsDeletableFailureOnVolumeTest() {
+        VolumeVO volumeVO = Mockito.mock(VolumeVO.class);
+        ArrayList<VolumeVO> arrayList = new ArrayList<VolumeVO>();
+        arrayList.add(volumeVO);
+
+        Mockito.when(_hostDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList<HostVO>());
+        Mockito.when(_podDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList<HostPodVO>());
+        Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
+        Mockito.when(_publicIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
+        Mockito.when(_vmInstanceDao.listByZoneId(anyLong())).thenReturn(new ArrayList<VMInstanceVO>());
+        Mockito.when(_volumeDao.findByDc(anyLong())).thenReturn(arrayList);
+        Mockito.when(_physicalNetworkDao.listByZone(anyLong())).thenReturn(new ArrayList<PhysicalNetworkVO>());
+
+        configurationMgr.checkIfZoneIsDeletable(new Random().nextLong());
+    }
+
+    @Test(expected = CloudRuntimeException.class)
+    public void checkIfZoneIsDeletableFailureOnPhysicalNetworkTest() {
+        PhysicalNetworkVO physicalNetworkVO = Mockito.mock(PhysicalNetworkVO.class);
+        ArrayList<PhysicalNetworkVO> arrayList = new ArrayList<PhysicalNetworkVO>();
+        arrayList.add(physicalNetworkVO);
+
+        Mockito.when(_hostDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList<HostVO>());
+        Mockito.when(_podDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList<HostPodVO>());
+        Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
+        Mockito.when(_publicIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
+        Mockito.when(_vmInstanceDao.listByZoneId(anyLong())).thenReturn(new ArrayList<VMInstanceVO>());
+        Mockito.when(_volumeDao.findByDc(anyLong())).thenReturn(new ArrayList<VolumeVO>());
+        Mockito.when(_physicalNetworkDao.listByZone(anyLong())).thenReturn(arrayList);
+
+        configurationMgr.checkIfZoneIsDeletable(new Random().nextLong());
+    }
+}
\ No newline at end of file


[6/9] git commit: updated refs/heads/master to a0f8f56

Posted by wi...@apache.org.
Refactored checkIfZoneIsDeletable().

It now makes use of various DAO DB function instead of running a self build query.


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/b31e64d4
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/b31e64d4
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/b31e64d4

Branch: refs/heads/master
Commit: b31e64d49df4f9cf1e16179354bf7916f19b0b3b
Parents: 0df3357
Author: Boris Schrijver <bo...@pcextreme.nl>
Authored: Wed Sep 16 22:19:14 2015 +0200
Committer: Boris Schrijver <bo...@pcextreme.nl>
Committed: Wed Sep 16 22:19:14 2015 +0200

----------------------------------------------------------------------
 .../configuration/ConfigurationManagerImpl.java | 125 ++++++-------------
 1 file changed, 35 insertions(+), 90 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b31e64d4/server/src/com/cloud/configuration/ConfigurationManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java
index 462d6e9..69e70e6 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;
@@ -236,6 +234,8 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati
     VolumeDao _volumeDao;
     @Inject
     VMInstanceDao _vmInstanceDao;
+    //@Inject
+    //VmwareDatacenterZoneMapDao _vmwareDatacenterZoneMapDao;
     @Inject
     AccountVlanMapDao _accountVlanMapDao;
     @Inject
@@ -894,7 +894,7 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati
 
         // Check if there are allocated private IP addresses in the pod
         if (_privateIpAddressDao.countIPs(podId, pod.getDataCenterId(), true) != 0) {
-            throw new CloudRuntimeException(errorMsg + "there are private IP addresses allocated in this pod");
+            throw new CloudRuntimeException(errorMsg + "there are private IP addresses allocated in this pod.");
         }
 
         // Check if there are any non-removed volumes in the pod.
@@ -1296,104 +1296,49 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati
 
     @DB
     protected void checkIfZoneIsDeletable(final long zoneId) {
-        final List<List<String>> tablesToCheck = new ArrayList<List<String>>();
+        final String errorMsg = "The zone cannot be deleted because ";
 
-        final List<String> host = new ArrayList<String>();
-        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<String> hostPodRef = new ArrayList<String>();
-        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<String> privateIP = new ArrayList<String>();
-        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<String> publicIP = new ArrayList<String>();
-        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<String> vmInstance = new ArrayList<String>();
-        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<String> volumes = new ArrayList<String>();
-        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<String> physicalNetworks = new ArrayList<String>();
-        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<String> vmwareDcs = new ArrayList<String>();
-        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<String> 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 ? = ?";
+        // 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.");
+        }
 
-            if (tableName.equals("op_dc_vnet_alloc")) {
-                selectSql += " AND taken IS NOT NULL";
-            }
+        // 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.");
+        }
 
-            if (tableName.equals("user_ip_address")) {
-                selectSql += " AND state!='Free'";
-            }
+        // 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.");
+        }
 
-            if (tableName.equals("op_dc_ip_address_alloc")) {
-                selectSql += " AND taken IS NOT NULL";
-            }
+        // 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.");
+        }
 
-            if (tableName.equals("host_pod_ref") || tableName.equals("host") || tableName.equals("volumes") || tableName.equals("physical_network")) {
-                selectSql += " AND removed is NULL";
-            }
+        // 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.");
+        }
 
-            if (tableName.equals("vm_instance")) {
-                selectSql += " AND state != ? AND removed IS NULL";
-            }
+        // 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.");
+        }
 
-            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 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,


[9/9] git commit: updated refs/heads/master to a0f8f56

Posted by wi...@apache.org.
Merge pull request #845 from borisroman/CLOUDSTACK-8763

[4.6][BLOCKER]CLOUDSTACK-8763: Resolved POD/ZONE deletion failure.Instead of having both checkIfPodIsDeletable() and checkIfZoneIsDeletable have there own SQL query, I've refactored them so they use DAO SQL Queries.

This resolves the SQL Exception thrown by both classes.

Test to confirm working order:
- deploy ACS
- Add zones / pods. -> Try to delete without resources. -> Direct success.
- Add resources to zones / pods. -> Try to delete with resources in the pod / zone. -> Correct exception thrown. (Error message is why it cannot remove the zone / pod. IE: There is still a hosts or vm or .... )

* pr/845:
  Added unit tests for checkIfPodIsDeletable() and checkIfZoneIsDeletable().
  Updated Dao classes with correct field names.
  Refactored checkIfZoneIsDeletable().
  Added findByDc(long dcId) to VolumeDao and VolumeDaoImpl.
  Added countIPs(long dcId, boolean onlyCountAllocated) to IPAddressDao and IPAddressDaoImpl.
  Added countIPs(long dcId, boolean onlyCountAllocated) to DataCenterIpAddressDao and DataCenterIpAddressDaoImpl.
  Refactored checkIfPodIsDeletable().
  Added findByPodId(Long podId) to HostDao and HostDaoImpl.

Signed-off-by: Wido den Hollander <wi...@widodh.nl>


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/a0f8f56a
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/a0f8f56a
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/a0f8f56a

Branch: refs/heads/master
Commit: a0f8f56a5dd3a323a72eb2a145d64cb1b5abea49
Parents: a601ae4 5a3a3d6
Author: Wido den Hollander <wi...@widodh.nl>
Authored: Wed Sep 23 14:00:04 2015 +0200
Committer: Wido den Hollander <wi...@widodh.nl>
Committed: Wed Sep 23 14:00:04 2015 +0200

----------------------------------------------------------------------
 .../cloud/dc/dao/DataCenterIpAddressDao.java    |   2 +
 .../dc/dao/DataCenterIpAddressDaoImpl.java      |  27 ++
 .../src/com/cloud/network/dao/IPAddressDao.java |   2 +
 .../com/cloud/network/dao/IPAddressDaoImpl.java |  21 ++
 .../src/com/cloud/storage/dao/VolumeDao.java    |   4 +
 .../com/cloud/storage/dao/VolumeDaoImpl.java    |  17 ++
 .../src/com/cloud/vm/dao/VMInstanceDaoImpl.java |   1 +
 .../configuration/ConfigurationManagerImpl.java | 217 +++++----------
 .../configuration/ConfigurationManagerTest.java | 266 ++++++++++++++++++-
 9 files changed, 400 insertions(+), 157 deletions(-)
----------------------------------------------------------------------



[3/9] git commit: updated refs/heads/master to a0f8f56

Posted by wi...@apache.org.
Added countIPs(long dcId, boolean onlyCountAllocated) to DataCenterIpAddressDao and DataCenterIpAddressDaoImpl.


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/473f1937
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/473f1937
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/473f1937

Branch: refs/heads/master
Commit: 473f1937e2c13bc7cca0b42c7585fe16accf7415
Parents: 45861fa
Author: Boris Schrijver <bo...@pcextreme.nl>
Authored: Wed Sep 16 22:15:00 2015 +0200
Committer: Boris Schrijver <bo...@pcextreme.nl>
Committed: Wed Sep 16 22:15:00 2015 +0200

----------------------------------------------------------------------
 .../cloud/dc/dao/DataCenterIpAddressDao.java    |  2 ++
 .../dc/dao/DataCenterIpAddressDaoImpl.java      | 21 ++++++++++++++++++++
 2 files changed, 23 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/473f1937/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDao.java b/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDao.java
index 7f1ec4d..e5843b6 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<DataCenterIpAddressVO
 
     int countIPs(long podId, long dcId, boolean onlyCountAllocated);
 
+    int countIPs(long dcId, boolean onlyCountAllocated);
+
     boolean deleteIpAddressByPod(long podId);
 
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/473f1937/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDaoImpl.java
index ca79eed..cf68322 100644
--- a/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDaoImpl.java
+++ b/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDaoImpl.java
@@ -46,6 +46,7 @@ public class DataCenterIpAddressDaoImpl extends GenericDaoBase<DataCenterIpAddre
     private final SearchBuilder<DataCenterIpAddressVO> AllFieldsSearch;
     private final GenericSearchBuilder<DataCenterIpAddressVO, Integer> AllIpCount;
     private final GenericSearchBuilder<DataCenterIpAddressVO, Integer> AllAllocatedIpCount;
+    private final GenericSearchBuilder<DataCenterIpAddressVO, Integer> AllAllocatedIpCountForDc;
 
     @Override
     @DB
@@ -221,6 +222,20 @@ public class DataCenterIpAddressDaoImpl extends GenericDaoBase<DataCenterIpAddre
         return count.get(0);
     }
 
+    @Override
+    public int countIPs(long dcId, boolean onlyCountAllocated) {
+        SearchCriteria<Integer> sc;
+        if (onlyCountAllocated) {
+            sc = AllAllocatedIpCountForDc.create();
+        } else {
+            sc = AllIpCount.create();
+        }
+
+        sc.setParameters("data_center_id", dcId);
+        List<Integer> count = customSearch(sc, null);
+        return count.get(0);
+    }
+
     public DataCenterIpAddressDaoImpl() {
         super();
 
@@ -244,5 +259,11 @@ public class DataCenterIpAddressDaoImpl extends GenericDaoBase<DataCenterIpAddre
         AllAllocatedIpCount.and("pod", AllAllocatedIpCount.entity().getPodId(), SearchCriteria.Op.EQ);
         AllAllocatedIpCount.and("removed", AllAllocatedIpCount.entity().getTakenAt(), SearchCriteria.Op.NNULL);
         AllAllocatedIpCount.done();
+
+        AllAllocatedIpCountForDc = createSearchBuilder(Integer.class);
+        AllAllocatedIpCountForDc.select(null, Func.COUNT, AllAllocatedIpCountForDc.entity().getId());
+        AllAllocatedIpCountForDc.and("data_center_id", AllAllocatedIpCountForDc.entity().getDataCenterId(), SearchCriteria.Op.EQ);
+        AllAllocatedIpCountForDc.and("removed", AllAllocatedIpCountForDc.entity().getTakenAt(), SearchCriteria.Op.NNULL);
+        AllAllocatedIpCountForDc.done();
     }
 }


[7/9] git commit: updated refs/heads/master to a0f8f56

Posted by wi...@apache.org.
Updated Dao classes with correct field names.


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/fa5f388f
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/fa5f388f
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/fa5f388f

Branch: refs/heads/master
Commit: fa5f388fe91bc2fd477b1f6752c2a31ac69d5051
Parents: b31e64d
Author: Boris Schrijver <bo...@pcextreme.nl>
Authored: Thu Sep 17 10:54:26 2015 +0200
Committer: Boris Schrijver <bo...@pcextreme.nl>
Committed: Thu Sep 17 15:54:29 2015 +0200

----------------------------------------------------------------------
 .../src/com/cloud/dc/dao/DataCenterIpAddressDaoImpl.java     | 8 +++++++-
 .../schema/src/com/cloud/network/dao/IPAddressDaoImpl.java   | 8 +++++++-
 engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java   | 3 ++-
 engine/schema/src/com/cloud/vm/dao/VMInstanceDaoImpl.java    | 1 +
 4 files changed, 17 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/fa5f388f/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDaoImpl.java
index cf68322..9486650 100644
--- a/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDaoImpl.java
+++ b/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDaoImpl.java
@@ -45,6 +45,7 @@ public class DataCenterIpAddressDaoImpl extends GenericDaoBase<DataCenterIpAddre
 
     private final SearchBuilder<DataCenterIpAddressVO> AllFieldsSearch;
     private final GenericSearchBuilder<DataCenterIpAddressVO, Integer> AllIpCount;
+    private final GenericSearchBuilder<DataCenterIpAddressVO, Integer> AllIpCountForDc;
     private final GenericSearchBuilder<DataCenterIpAddressVO, Integer> AllAllocatedIpCount;
     private final GenericSearchBuilder<DataCenterIpAddressVO, Integer> AllAllocatedIpCountForDc;
 
@@ -228,7 +229,7 @@ public class DataCenterIpAddressDaoImpl extends GenericDaoBase<DataCenterIpAddre
         if (onlyCountAllocated) {
             sc = AllAllocatedIpCountForDc.create();
         } else {
-            sc = AllIpCount.create();
+            sc = AllIpCountForDc.create();
         }
 
         sc.setParameters("data_center_id", dcId);
@@ -254,6 +255,11 @@ public class DataCenterIpAddressDaoImpl extends GenericDaoBase<DataCenterIpAddre
         AllIpCount.and("pod", AllIpCount.entity().getPodId(), SearchCriteria.Op.EQ);
         AllIpCount.done();
 
+        AllIpCountForDc = createSearchBuilder(Integer.class);
+        AllIpCountForDc.select(null, Func.COUNT, AllIpCountForDc.entity().getId());
+        AllIpCountForDc.and("data_center_id", AllIpCountForDc.entity().getPodId(), SearchCriteria.Op.EQ);
+        AllIpCountForDc.done();
+
         AllAllocatedIpCount = createSearchBuilder(Integer.class);
         AllAllocatedIpCount.select(null, Func.COUNT, AllAllocatedIpCount.entity().getId());
         AllAllocatedIpCount.and("pod", AllAllocatedIpCount.entity().getPodId(), SearchCriteria.Op.EQ);

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/fa5f388f/engine/schema/src/com/cloud/network/dao/IPAddressDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/IPAddressDaoImpl.java b/engine/schema/src/com/cloud/network/dao/IPAddressDaoImpl.java
index 5c8b9b1..61bc1b7 100644
--- a/engine/schema/src/com/cloud/network/dao/IPAddressDaoImpl.java
+++ b/engine/schema/src/com/cloud/network/dao/IPAddressDaoImpl.java
@@ -55,6 +55,7 @@ public class IPAddressDaoImpl extends GenericDaoBase<IPAddressVO, Long> implemen
     protected SearchBuilder<IPAddressVO> AllFieldsSearch;
     protected SearchBuilder<IPAddressVO> VlanDbIdSearchUnallocated;
     protected GenericSearchBuilder<IPAddressVO, Integer> AllIpCount;
+    protected GenericSearchBuilder<IPAddressVO, Integer> AllIpCountForDc;
     protected GenericSearchBuilder<IPAddressVO, Integer> AllocatedIpCount;
     protected GenericSearchBuilder<IPAddressVO, Integer> AllocatedIpCountForDc;
     protected GenericSearchBuilder<IPAddressVO, Integer> AllIpCountForDashboard;
@@ -101,6 +102,11 @@ public class IPAddressDaoImpl extends GenericDaoBase<IPAddressVO, Long> 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);
@@ -290,7 +296,7 @@ public class IPAddressDaoImpl extends GenericDaoBase<IPAddressVO, Long> implemen
 
     @Override
     public int countIPs(long dcId, boolean onlyCountAllocated) {
-        SearchCriteria<Integer> sc = onlyCountAllocated ? AllocatedIpCount.create() : AllIpCount.create();
+        SearchCriteria<Integer> sc = onlyCountAllocated ? AllocatedIpCountForDc.create() : AllIpCountForDc.create();
         sc.setParameters("dc", dcId);
 
         return customSearch(sc, null).get(0);

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/fa5f388f/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java b/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java
index 7d0c9c3..f573847 100644
--- a/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java
+++ b/engine/schema/src/com/cloud/storage/dao/VolumeDaoImpl.java
@@ -200,7 +200,7 @@ public class VolumeDaoImpl extends GenericDaoBase<VolumeVO, Long> implements Vol
     @Override
     public List<VolumeVO> findByDc(long dcId) {
         SearchCriteria<VolumeVO> sc = AllFieldsSearch.create();
-        sc.setParameters("data_center_id", dcId);
+        sc.setParameters("dcId", dcId);
 
         return listBy(sc);
     }
@@ -322,6 +322,7 @@ public class VolumeDaoImpl extends GenericDaoBase<VolumeVO, Long> 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);

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/fa5f388f/engine/schema/src/com/cloud/vm/dao/VMInstanceDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/vm/dao/VMInstanceDaoImpl.java b/engine/schema/src/com/cloud/vm/dao/VMInstanceDaoImpl.java
index be0e366..427c534 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<VMInstanceVO, Long> 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();


[2/9] git commit: updated refs/heads/master to a0f8f56

Posted by wi...@apache.org.
Refactored checkIfPodIsDeletable().

It now makes use of various DAO DB function instead of running a self build query.


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/45861fa1
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/45861fa1
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/45861fa1

Branch: refs/heads/master
Commit: 45861fa1b7e82250f404e22dd13ef4925c60da4e
Parents: 0648cb9
Author: Boris Schrijver <bo...@pcextreme.nl>
Authored: Wed Sep 16 22:14:05 2015 +0200
Committer: Boris Schrijver <bo...@pcextreme.nl>
Committed: Wed Sep 16 22:14:05 2015 +0200

----------------------------------------------------------------------
 .../configuration/ConfigurationManagerImpl.java | 92 ++++++--------------
 1 file changed, 27 insertions(+), 65 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/45861fa1/server/src/com/cloud/configuration/ConfigurationManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java
index fc7bff9..462d6e9 100644
--- a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java
+++ b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java
@@ -134,6 +134,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 +183,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 +216,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 +231,12 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati
     @Inject
     HostPodDao _podDao;
     @Inject
+    HostDao _hostDao;
+    @Inject
+    VolumeDao _volumeDao;
+    @Inject
+    VMInstanceDao _vmInstanceDao;
+    @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<List<String>> tablesToCheck = new ArrayList<List<String>>();
-
         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<String> volumes = new ArrayList<String>();
-        volumes.add(0, "volumes");
-        volumes.add(1, "pod_id");
-        volumes.add(2, "there are storage volumes for this pod");
-        tablesToCheck.add(volumes);
-
-        final List<String> host = new ArrayList<String>();
-        host.add(0, "host");
-        host.add(1, "pod_id");
-        host.add(2, "there are servers running in this pod");
-        tablesToCheck.add(host);
-
-        final List<String> vmInstance = new ArrayList<String>();
-        vmInstance.add(0, "vm_instance");
-        vmInstance.add(1, "pod_id");
-        vmInstance.add(2, "there are virtual machines running in this pod");
-        tablesToCheck.add(vmInstance);
-
-        final List<String> cluster = new ArrayList<String>();
-        cluster.add(0, "cluster");
-        cluster.add(1, "pod_id");
-        cluster.add(2, "there are clusters in this pod");
-        tablesToCheck.add(cluster);
-
-        for (final List<String> 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 ? = ?";
+        // 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.");
+        }
 
-            if(tableName.equals("vm_instance")) {
-                selectSql += " AND state != ? AND removed IS NULL";
-            }
+        // 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.");
+        }
 
-            if (tableName.equals("host") || tableName.equals("cluster") || tableName.equals("volumes")) {
-                selectSql += " and removed IS NULL";
-            }
+        // 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 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.");
         }
     }
 


[4/9] git commit: updated refs/heads/master to a0f8f56

Posted by wi...@apache.org.
Added countIPs(long dcId, boolean onlyCountAllocated) to IPAddressDao and IPAddressDaoImpl.


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/12fc2b4c
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/12fc2b4c
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/12fc2b4c

Branch: refs/heads/master
Commit: 12fc2b4c26145721622b34f1a4f40d4e06faea93
Parents: 473f193
Author: Boris Schrijver <bo...@pcextreme.nl>
Authored: Wed Sep 16 22:15:53 2015 +0200
Committer: Boris Schrijver <bo...@pcextreme.nl>
Committed: Wed Sep 16 22:15:53 2015 +0200

----------------------------------------------------------------------
 .../src/com/cloud/network/dao/IPAddressDao.java      |  2 ++
 .../src/com/cloud/network/dao/IPAddressDaoImpl.java  | 15 +++++++++++++++
 2 files changed, 17 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/12fc2b4c/engine/schema/src/com/cloud/network/dao/IPAddressDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/IPAddressDao.java b/engine/schema/src/com/cloud/network/dao/IPAddressDao.java
index cb07656..fbf5c57 100644
--- a/engine/schema/src/com/cloud/network/dao/IPAddressDao.java
+++ b/engine/schema/src/com/cloud/network/dao/IPAddressDao.java
@@ -40,6 +40,8 @@ public interface IPAddressDao extends GenericDao<IPAddressVO, Long> {
 
     List<IPAddressVO> 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);

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/12fc2b4c/engine/schema/src/com/cloud/network/dao/IPAddressDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/network/dao/IPAddressDaoImpl.java b/engine/schema/src/com/cloud/network/dao/IPAddressDaoImpl.java
index 5122876..5c8b9b1 100644
--- a/engine/schema/src/com/cloud/network/dao/IPAddressDaoImpl.java
+++ b/engine/schema/src/com/cloud/network/dao/IPAddressDaoImpl.java
@@ -56,6 +56,7 @@ public class IPAddressDaoImpl extends GenericDaoBase<IPAddressVO, Long> implemen
     protected SearchBuilder<IPAddressVO> VlanDbIdSearchUnallocated;
     protected GenericSearchBuilder<IPAddressVO, Integer> AllIpCount;
     protected GenericSearchBuilder<IPAddressVO, Integer> AllocatedIpCount;
+    protected GenericSearchBuilder<IPAddressVO, Integer> AllocatedIpCountForDc;
     protected GenericSearchBuilder<IPAddressVO, Integer> AllIpCountForDashboard;
     protected SearchBuilder<IPAddressVO> DeleteAllExceptGivenIp;
     protected GenericSearchBuilder<IPAddressVO, Long> AllocatedIpCountForAccount;
@@ -107,6 +108,12 @@ public class IPAddressDaoImpl extends GenericDaoBase<IPAddressVO, Long> 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);
@@ -282,6 +289,14 @@ public class IPAddressDaoImpl extends GenericDaoBase<IPAddressVO, Long> implemen
     }
 
     @Override
+    public int countIPs(long dcId, boolean onlyCountAllocated) {
+        SearchCriteria<Integer> sc = onlyCountAllocated ? AllocatedIpCount.create() : AllIpCount.create();
+        sc.setParameters("dc", dcId);
+
+        return customSearch(sc, null).get(0);
+    }
+
+    @Override
     public int countIPs(long dcId, long vlanId, boolean onlyCountAllocated) {
         SearchCriteria<Integer> sc = onlyCountAllocated ? AllocatedIpCount.create() : AllIpCount.create();
         sc.setParameters("dc", dcId);