From 23cfbb6cab64fb5422e9cffd59c45e47c4f27b79 Mon Sep 17 00:00:00 2001 From: Nitin Mehta Date: Mon, 23 Jan 2012 20:32:50 +0530 Subject: [PATCH] Bug 13059: Make zoneId in ListCapacity call optional. When no parameter is passed list capacity for all zones. Reviewed-By: Kishan --- .../cloud/api/commands/ListCapacityCmd.java | 6 ++--- .../cloud/capacity/dao/CapacityDaoImpl.java | 18 +++++++++++-- .../cloud/server/ManagementServerImpl.java | 27 ++++++++++++++----- 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/api/src/com/cloud/api/commands/ListCapacityCmd.java b/api/src/com/cloud/api/commands/ListCapacityCmd.java index 3bf727430e9..e179b2d293f 100755 --- a/api/src/com/cloud/api/commands/ListCapacityCmd.java +++ b/api/src/com/cloud/api/commands/ListCapacityCmd.java @@ -33,7 +33,7 @@ import com.cloud.api.response.CapacityResponse; import com.cloud.api.response.ListResponse; import com.cloud.capacity.Capacity; -@Implementation(description="Lists capacity.", responseObject=CapacityResponse.class) +@Implementation(description="Lists all the system wide capacities.", responseObject=CapacityResponse.class) public class ListCapacityCmd extends BaseListCmd { public static final Logger s_logger = Logger.getLogger(ListCapacityCmd.class.getName()); @@ -46,7 +46,7 @@ public class ListCapacityCmd extends BaseListCmd { ///////////////////////////////////////////////////// @IdentityMapper(entityTableName="data_center") - @Parameter(name=ApiConstants.ZONE_ID, type=CommandType.LONG, required=true, description="lists capacity by the Zone ID") + @Parameter(name=ApiConstants.ZONE_ID, type=CommandType.LONG, description="lists capacity by the Zone ID") private Long zoneId; @IdentityMapper(entityTableName="host_pod_ref") @@ -57,7 +57,7 @@ public class ListCapacityCmd extends BaseListCmd { @Parameter(name=ApiConstants.CLUSTER_ID, type=CommandType.LONG, description="lists capacity by the Cluster ID") private Long clusterId; - @Parameter(name=ApiConstants.FETCH_LATEST, type=CommandType.BOOLEAN, description="recalculate capacities") + @Parameter(name=ApiConstants.FETCH_LATEST, type=CommandType.BOOLEAN, description="recalculate capacities and fetch the latest") private Boolean fetchLatest; @Parameter(name=ApiConstants.TYPE, type=CommandType.INTEGER, description="lists capacity by type" + diff --git a/server/src/com/cloud/capacity/dao/CapacityDaoImpl.java b/server/src/com/cloud/capacity/dao/CapacityDaoImpl.java index d6dbefd1197..48f0c975037 100755 --- a/server/src/com/cloud/capacity/dao/CapacityDaoImpl.java +++ b/server/src/com/cloud/capacity/dao/CapacityDaoImpl.java @@ -108,12 +108,17 @@ public class CapacityDaoImpl extends GenericDaoBase implements public List findCapacityBy(Integer capacityType, Long zoneId, Long podId, Long clusterId){ SummedCapacitySearch = createSearchBuilder(SummedCapacity.class); + SummedCapacitySearch.select("dcId", Func.NATIVE, SummedCapacitySearch.entity().getDataCenterId()); SummedCapacitySearch.select("sumUsed", Func.SUM, SummedCapacitySearch.entity().getUsedCapacity()); SummedCapacitySearch.select("sumReserved", Func.SUM, SummedCapacitySearch.entity().getReservedCapacity()); SummedCapacitySearch.select("sumTotal", Func.SUM, SummedCapacitySearch.entity().getTotalCapacity()); SummedCapacitySearch.select("capacityType", Func.NATIVE, SummedCapacitySearch.entity().getCapacityType()); - - SummedCapacitySearch.groupBy(SummedCapacitySearch.entity().getCapacityType()); + + if (zoneId==null && podId==null && clusterId==null){ // List all the capacities grouped by zone, capacity Type + SummedCapacitySearch.groupBy(SummedCapacitySearch.entity().getDataCenterId(), SummedCapacitySearch.entity().getCapacityType()); + }else { + SummedCapacitySearch.groupBy(SummedCapacitySearch.entity().getCapacityType()); + } if (zoneId != null){ SummedCapacitySearch.and("dcId", SummedCapacitySearch.entity().getDataCenterId(), Op.EQ); @@ -262,6 +267,7 @@ public class CapacityDaoImpl extends GenericDaoBase implements public short capacityType; public Long clusterId; public Long podId; + public Long dcId; public SummedCapacity() { } public SummedCapacity(long sumUsed, long sumReserved, long sumTotal, @@ -274,6 +280,11 @@ public class CapacityDaoImpl extends GenericDaoBase implements this.clusterId = clusterId; this.podId = podId; } + public SummedCapacity(long sumUsed, long sumReserved, long sumTotal, + short capacityType, Long clusterId, Long podId, Long zoneId) { + this(sumUsed, sumReserved, sumTotal, capacityType, clusterId, podId); + this.dcId = zoneId; + } public Short getCapacityType() { return capacityType; } @@ -286,6 +297,9 @@ public class CapacityDaoImpl extends GenericDaoBase implements public Long getTotalCapacity() { return sumTotal; } + public Long getDataCenterId() { + return dcId; + } } public List findByClusterPodZone(Long zoneId, Long podId, Long clusterId){ diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index 31a9f9da544..bb35af0b4e7 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -1939,7 +1939,7 @@ public class ManagementServerImpl implements ManagementServer { List capacities = new ArrayList(); for (SummedCapacity summedCapacity : summedCapacities){ - CapacityVO capacity = new CapacityVO(null, zoneId, podId, clusterId, + CapacityVO capacity = new CapacityVO(null, summedCapacity.getDataCenterId(), podId, clusterId, summedCapacity.getUsedCapacity() + summedCapacity.getReservedCapacity(), summedCapacity.getTotalCapacity(), summedCapacity.getCapacityType()); @@ -1950,14 +1950,27 @@ public class ManagementServerImpl implements ManagementServer { } // op_host_Capacity contains only allocated stats and the real time stats are stored "in memory". - //Show Sec. Storage only when the api is invoked for the zone layer. - if ((capacityType == null || capacityType == Capacity.CAPACITY_TYPE_SECONDARY_STORAGE) && podId == null && clusterId == null) { - capacities.add(_storageMgr.getSecondaryStorageUsedStats(null, zoneId)); + // Show Sec. Storage only when the api is invoked for the zone layer. + List dcList = new ArrayList(); + if (zoneId==null && podId==null && clusterId==null){ + dcList = ApiDBUtils.listZones(); + }else if (zoneId != null){ + dcList.add(ApiDBUtils.findZoneById(zoneId)); + }else{ + if (capacityType == null || capacityType == Capacity.CAPACITY_TYPE_STORAGE) { + capacities.add(_storageMgr.getStoragePoolUsedStats(null, clusterId, podId, zoneId)); + } } - if (capacityType == null || capacityType == Capacity.CAPACITY_TYPE_STORAGE) { - capacities.add(_storageMgr.getStoragePoolUsedStats(null, clusterId, podId, zoneId)); + + for(DataCenterVO zone : dcList){ + zoneId = zone.getId(); + if ((capacityType == null || capacityType == Capacity.CAPACITY_TYPE_SECONDARY_STORAGE) && podId == null && clusterId == null) { + capacities.add(_storageMgr.getSecondaryStorageUsedStats(null, zoneId)); + } + if (capacityType == null || capacityType == Capacity.CAPACITY_TYPE_STORAGE) { + capacities.add(_storageMgr.getStoragePoolUsedStats(null, clusterId, podId, zoneId)); + } } - return capacities; }