Search in sources :

Example 1 with UsageStats

use of com.emc.storageos.cinder.model.UsageStats in project coprhd-controller by CoprHD.

the class QuotaHelper method getStorageStats.

/**
 * Get usage statistics(like total number of volumes, snapshots and total size) for given vpool
 *
 * @prereq none
 *
 * @param vpool
 *
 * @brief get statistics
 * @return UsageStats
 */
public UsageStats getStorageStats(URI vpool, URI projectId) {
    UsageStats objStats = new UsageStats();
    double totalSnapshotsUsed = 0;
    double totalSizeUsed = 0;
    long totalVolumesUsed = 0;
    URIQueryResultList uris = new URIQueryResultList();
    if (vpool != null) {
        URIQueryResultList volUris = new URIQueryResultList();
        _dbClient.queryByConstraint(ContainmentConstraint.Factory.getVirtualPoolVolumeConstraint(vpool), volUris);
        for (URI voluri : volUris) {
            Volume volume = _dbClient.queryObject(Volume.class, voluri);
            if (volume != null && !volume.getInactive() && (volume.getProject().getURI().toString().equals(projectId.toString()))) {
                totalSizeUsed += ((double) volume.getCapacity() / GB);
                totalVolumesUsed++;
            }
            URIQueryResultList snapList = new URIQueryResultList();
            _dbClient.queryByConstraint(ContainmentConstraint.Factory.getVolumeSnapshotConstraint(voluri), snapList);
            for (URI snapUri : snapList) {
                BlockSnapshot blockSnap = _dbClient.queryObject(BlockSnapshot.class, snapUri);
                if (blockSnap != null && !blockSnap.getInactive()) {
                    _log.info("ProvisionedCapacity = {} ", blockSnap.getProvisionedCapacity());
                    totalSizeUsed += ((double) blockSnap.getProvisionedCapacity() / GB);
                    totalSnapshotsUsed++;
                }
            }
        }
    } else {
        _dbClient.queryByConstraint(ContainmentConstraint.Factory.getProjectVolumeConstraint(projectId), uris);
        for (URI volUri : uris) {
            Volume volume = _dbClient.queryObject(Volume.class, volUri);
            if (volume != null && !volume.getInactive()) {
                totalSizeUsed += ((double) volume.getCapacity() / GB);
                totalVolumesUsed++;
            }
            URIQueryResultList snapList = new URIQueryResultList();
            _dbClient.queryByConstraint(ContainmentConstraint.Factory.getVolumeSnapshotConstraint(volUri), snapList);
            for (URI snapUri : snapList) {
                BlockSnapshot blockSnap = _dbClient.queryObject(BlockSnapshot.class, snapUri);
                if (blockSnap != null && !blockSnap.getInactive()) {
                    totalSizeUsed += ((double) blockSnap.getProvisionedCapacity() / GB);
                    totalSnapshotsUsed++;
                }
            }
        }
    }
    objStats.snapshots = (long) totalSnapshotsUsed;
    objStats.volumes = totalVolumesUsed;
    objStats.spaceUsed = (long) (Math.round(totalSizeUsed));
    return objStats;
}
Also used : Volume(com.emc.storageos.db.client.model.Volume) BlockSnapshot(com.emc.storageos.db.client.model.BlockSnapshot) UsageStats(com.emc.storageos.cinder.model.UsageStats) URI(java.net.URI) URIQueryResultList(com.emc.storageos.db.client.constraint.URIQueryResultList)

Example 2 with UsageStats

use of com.emc.storageos.cinder.model.UsageStats in project coprhd-controller by CoprHD.

the class QuotaHelper method getUsageStatistics.

/**
 * Get usage statistics in terms of quota attributes like gigabytes,snapshots, volumes.
 * The details include in_use,reserved,limit.
 *
 * @prereq none
 *
 * @param tenantId
 * @param quotaMap of project and vpools
 * @param proj project under consideration
 *
 * @brief get usage statistics in terms of project and volume types.
 * @return CinderUsage
 */
public CinderUsage getUsageStatistics(URI tenantId, HashMap<String, String> quotaMap, Project proj) {
    CinderUsage objCinderUsage = new CinderUsage();
    List<URI> vpools = _dbClient.queryByType(VirtualPool.class, true);
    for (URI vpool : vpools) {
        VirtualPool pool = _dbClient.queryObject(VirtualPool.class, vpool);
        _log.debug("Looking up vpool {}", pool.getLabel());
        if (pool != null && pool.getType().equalsIgnoreCase(VirtualPool.Type.block.name())) {
            if (_permissionsHelper.tenantHasUsageACL(tenantId, pool)) {
                UsageStats stats = getStorageStats(pool.getId(), proj.getId());
                UsageAndLimits objSpaceUsage = new UsageAndLimits();
                objSpaceUsage.setIn_use(stats.spaceUsed);
                objSpaceUsage.setLimit(Long.parseLong(quotaMap.get("gigabytes" + "_" + pool.getLabel())));
                UsageAndLimits objVolsUsage = new UsageAndLimits();
                objVolsUsage.setIn_use(stats.volumes);
                objVolsUsage.setLimit(Long.parseLong(quotaMap.get("volumes" + "_" + pool.getLabel())));
                UsageAndLimits objSnapsUsage = new UsageAndLimits();
                objSnapsUsage.setIn_use(stats.snapshots);
                objSnapsUsage.setLimit(Long.parseLong(quotaMap.get("snapshots" + "_" + pool.getLabel())));
                objCinderUsage.getQuota_set().put("gigabytes" + "_" + pool.getLabel(), objSpaceUsage);
                objCinderUsage.getQuota_set().put("snapshots" + "_" + pool.getLabel(), objSnapsUsage);
                objCinderUsage.getQuota_set().put("volumes" + "_" + pool.getLabel(), objVolsUsage);
            }
        }
    }
    // now get the usage information for the project
    UsageStats stats = getStorageStats(null, proj.getId());
    UsageAndLimits objSpaceUsage = new UsageAndLimits();
    objSpaceUsage.setIn_use(stats.spaceUsed);
    objSpaceUsage.setLimit(Long.parseLong(quotaMap.get("gigabytes")));
    UsageAndLimits objVolsUsage = new UsageAndLimits();
    objVolsUsage.setIn_use(stats.volumes);
    objVolsUsage.setLimit(Long.parseLong(quotaMap.get("volumes")));
    UsageAndLimits objSnapsUsage = new UsageAndLimits();
    objSnapsUsage.setIn_use(stats.snapshots);
    objSnapsUsage.setLimit(Long.parseLong(quotaMap.get("snapshots")));
    objCinderUsage.getQuota_set().put("gigabytes", objSpaceUsage);
    objCinderUsage.getQuota_set().put("snapshots", objSnapsUsage);
    objCinderUsage.getQuota_set().put("volumes", objVolsUsage);
    return objCinderUsage;
}
Also used : CinderUsage(com.emc.storageos.cinder.model.CinderUsage) UsageAndLimits(com.emc.storageos.cinder.model.UsageAndLimits) VirtualPool(com.emc.storageos.db.client.model.VirtualPool) UsageStats(com.emc.storageos.cinder.model.UsageStats) URI(java.net.URI)

Example 3 with UsageStats

use of com.emc.storageos.cinder.model.UsageStats in project coprhd-controller by CoprHD.

the class VolumeService method validateVolumeCreate.

private boolean validateVolumeCreate(String openstackTenantId, VirtualPool pool, long requestedSize) {
    QuotaOfCinder objQuota = null;
    boolean isValidVolume = false;
    if (pool == null)
        objQuota = getQuotaHelper().getProjectQuota(openstackTenantId, getUserFromContext());
    else
        objQuota = getQuotaHelper().getVPoolQuota(openstackTenantId, pool, getUserFromContext());
    if (objQuota == null) {
        _log.info("Unable to retrive the Quota information");
        return false;
    }
    Project proj = getCinderHelper().getProject(openstackTenantId, getUserFromContext());
    long totalVolumesUsed = 0;
    long totalSizeUsed = 0;
    UsageStats stats = null;
    if (pool != null)
        stats = getQuotaHelper().getStorageStats(pool.getId(), proj.getId());
    else
        stats = getQuotaHelper().getStorageStats(null, proj.getId());
    totalVolumesUsed = stats.volumes;
    totalSizeUsed = stats.spaceUsed;
    _log.info(String.format("VolumesLimit():%s ,TotalQuota:%s , TotalSizeUsed:%s, TotalVolumesUsed:%s, RequestedConsumption:%s", objQuota.getVolumesLimit(), objQuota.getTotalQuota(), totalSizeUsed, totalVolumesUsed, (totalSizeUsed + requestedSize / GB)));
    if ((objQuota.getVolumesLimit() != QuotaService.DEFAULT_VOLUME_TYPE_VOLUMES_QUOTA) && (objQuota.getVolumesLimit() <= totalVolumesUsed)) {
        return isValidVolume;
    } else if ((objQuota.getTotalQuota() != QuotaService.DEFAULT_VOLUME_TYPE_TOTALGB_QUOTA) && (objQuota.getTotalQuota() <= (totalSizeUsed + requestedSize / GB))) {
        return isValidVolume;
    } else {
        isValidVolume = true;
        return isValidVolume;
    }
}
Also used : Project(com.emc.storageos.db.client.model.Project) QuotaOfCinder(com.emc.storageos.db.client.model.QuotaOfCinder) UsageStats(com.emc.storageos.cinder.model.UsageStats)

Example 4 with UsageStats

use of com.emc.storageos.cinder.model.UsageStats in project coprhd-controller by CoprHD.

the class MiscService method getLimits.

/**
 * Get Limits
 *
 * @prereq none
 * @param tenant_id the URN of the tenant
 * @brief Get Limits
 * @return limits
 */
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Path("/limits")
@CheckPermission(roles = { Role.SYSTEM_MONITOR, Role.TENANT_ADMIN }, acls = { ACL.ANY })
public Response getLimits(@PathParam("tenant_id") String openstack_tenant_id, @Context HttpHeaders header) {
    _log.info("START get limits");
    CinderLimits limitsResp = new CinderLimits();
    Project project = getCinderHelper().getProject(openstack_tenant_id.toString(), getUserFromContext());
    if (project == null) {
        throw APIException.badRequests.projectWithTagNonexistent(openstack_tenant_id);
    }
    HashMap<String, String> defaultQuotaMap = getQuotaHelper().loadDefaultsMapFromDb();
    int totalSizeUsed = 0;
    int maxQuota = Long.valueOf(defaultQuotaMap.get(CinderConstants.ResourceQuotaDefaults.GIGABYTES.getResource())).intValue();
    int maxTotalVolumes = 0;
    int maxTotalSnapshots = 0;
    int totalVolumesUsed = 0;
    int totalSnapshotsUsed = 0;
    if (project.getQuotaEnabled()) {
        maxQuota = (int) (project.getQuota().intValue());
    }
    UsageStats objUsageStats = new UsageStats();
    objUsageStats = getQuotaHelper().getStorageStats(null, project.getId());
    totalVolumesUsed = (int) objUsageStats.volumes;
    totalSnapshotsUsed = (int) objUsageStats.snapshots;
    totalSizeUsed = (int) objUsageStats.spaceUsed;
    QuotaOfCinder projQuota = getQuotaHelper().getProjectQuota(openstack_tenant_id, getUserFromContext());
    if (projQuota != null) {
        maxTotalVolumes = projQuota.getVolumesLimit().intValue();
        maxTotalSnapshots = (int) projQuota.getSnapshotsLimit().intValue();
    } else {
        QuotaOfCinder quotaObj = new QuotaOfCinder();
        quotaObj.setId(URI.create(UUID.randomUUID().toString()));
        quotaObj.setProject(project.getId());
        quotaObj.setVolumesLimit(Long.valueOf(defaultQuotaMap.get(CinderConstants.ResourceQuotaDefaults.VOLUMES.getResource())));
        quotaObj.setSnapshotsLimit(Long.valueOf(defaultQuotaMap.get(CinderConstants.ResourceQuotaDefaults.SNAPSHOTS.getResource())));
        quotaObj.setTotalQuota((long) maxQuota);
        _dbClient.createObject(quotaObj);
        maxTotalSnapshots = quotaObj.getSnapshotsLimit().intValue();
        maxTotalVolumes = quotaObj.getVolumesLimit().intValue();
    }
    Map<String, Integer> absoluteDetailsMap = new HashMap<String, Integer>();
    absoluteDetailsMap.put("totalSnapshotsUsed", totalSnapshotsUsed);
    absoluteDetailsMap.put("maxTotalVolumeGigabytes", maxQuota);
    absoluteDetailsMap.put("totalGigabytesUsed", totalSizeUsed);
    absoluteDetailsMap.put("maxTotalSnapshots", maxTotalSnapshots);
    absoluteDetailsMap.put("totalVolumesUsed", totalVolumesUsed);
    absoluteDetailsMap.put("maxTotalVolumes", maxTotalVolumes);
    limitsResp.absolute = absoluteDetailsMap;
    _log.info("END get limits");
    return CinderApiUtils.getCinderResponse(limitsResp, header, true, CinderConstants.STATUS_OK);
}
Also used : Project(com.emc.storageos.db.client.model.Project) QuotaOfCinder(com.emc.storageos.db.client.model.QuotaOfCinder) HashMap(java.util.HashMap) UsageStats(com.emc.storageos.cinder.model.UsageStats) CinderLimits(com.emc.storageos.cinder.model.CinderLimits) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) CheckPermission(com.emc.storageos.security.authorization.CheckPermission)

Example 5 with UsageStats

use of com.emc.storageos.cinder.model.UsageStats in project coprhd-controller by CoprHD.

the class SnapshotService method validateSnapshotCreate.

private boolean validateSnapshotCreate(String openstack_tenant_id, VirtualPool pool, long requestedSize) {
    _log.info("requestedSize {}", requestedSize);
    QuotaOfCinder objQuota = null;
    if (pool == null) {
        objQuota = getQuotaHelper().getProjectQuota(openstack_tenant_id, getUserFromContext());
    } else {
        objQuota = getQuotaHelper().getVPoolQuota(openstack_tenant_id, pool, getUserFromContext());
    }
    Project proj = getCinderHelper().getProject(openstack_tenant_id, getUserFromContext());
    if (proj == null) {
        throw APIException.badRequests.projectWithTagNonexistent(openstack_tenant_id);
    }
    long totalSnapshotsUsed = 0;
    long totalSizeUsed = 0;
    UsageStats stats = null;
    if (pool != null) {
        stats = getQuotaHelper().getStorageStats(pool.getId(), proj.getId());
    } else {
        stats = getQuotaHelper().getStorageStats(null, proj.getId());
    }
    totalSnapshotsUsed = stats.snapshots;
    totalSizeUsed = stats.spaceUsed;
    _log.info(String.format("objQuota.getVolumesLimit():%s ,objQuota.getSnapshotsLimit():%s,objQuota.getTotalQuota():%s,totalSizeUsed:%s,totalSnapshotsUsed:%s,willconsume:%s", objQuota.getVolumesLimit(), objQuota.getSnapshotsLimit(), objQuota.getTotalQuota(), totalSizeUsed, totalSnapshotsUsed, (totalSizeUsed + (long) (requestedSize / GB))));
    if ((objQuota.getSnapshotsLimit() != -1) && (objQuota.getSnapshotsLimit() <= totalSnapshotsUsed)) {
        return false;
    } else if ((objQuota.getTotalQuota() != -1) && (objQuota.getTotalQuota() <= (totalSizeUsed + (long) (requestedSize / GB)))) {
        return false;
    } else {
        return true;
    }
}
Also used : Project(com.emc.storageos.db.client.model.Project) QuotaOfCinder(com.emc.storageos.db.client.model.QuotaOfCinder) UsageStats(com.emc.storageos.cinder.model.UsageStats)

Aggregations

UsageStats (com.emc.storageos.cinder.model.UsageStats)6 QuotaOfCinder (com.emc.storageos.db.client.model.QuotaOfCinder)4 Project (com.emc.storageos.db.client.model.Project)3 URI (java.net.URI)2 CinderLimits (com.emc.storageos.cinder.model.CinderLimits)1 CinderUsage (com.emc.storageos.cinder.model.CinderUsage)1 UsageAndLimits (com.emc.storageos.cinder.model.UsageAndLimits)1 URIQueryResultList (com.emc.storageos.db.client.constraint.URIQueryResultList)1 BlockSnapshot (com.emc.storageos.db.client.model.BlockSnapshot)1 VirtualPool (com.emc.storageos.db.client.model.VirtualPool)1 Volume (com.emc.storageos.db.client.model.Volume)1 CheckPermission (com.emc.storageos.security.authorization.CheckPermission)1 HashMap (java.util.HashMap)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1