Search in sources :

Example 41 with Quota

use of org.ovirt.engine.core.common.businessentities.Quota in project ovirt-engine by oVirt.

the class QuotaManager method validateAndSetClusterQuota.

private boolean validateAndSetClusterQuota(QuotaConsumptionParametersWrapper parameters, Pair<AuditLogType, AuditLogableBase> auditLogPair) {
    boolean result = true;
    List<QuotaClusterConsumptionParameter> executed = new ArrayList<>();
    for (QuotaConsumptionParameter parameter : parameters.getParameters()) {
        QuotaClusterConsumptionParameter clusterConsumptionParameter;
        if (parameter.getParameterType() != QuotaConsumptionParameter.ParameterType.CLUSTER) {
            continue;
        } else {
            clusterConsumptionParameter = (QuotaClusterConsumptionParameter) parameter;
        }
        Quota quota = parameter.getQuota();
        QuotaCluster quotaCluster = null;
        if (quota.getGlobalQuotaCluster() != null) {
            // global cluster quota
            quotaCluster = quota.getGlobalQuotaCluster();
        } else {
            for (QuotaCluster cluster : quota.getQuotaClusters()) {
                if (cluster.getClusterId().equals(clusterConsumptionParameter.getClusterId())) {
                    quotaCluster = cluster;
                    break;
                }
            }
        }
        if (quotaCluster == null) {
            parameters.getValidationMessages().add(EngineMessage.ACTION_TYPE_FAILED_QUOTA_IS_NOT_VALID.toString());
            result = false;
            break;
        }
        long requestedMemory = clusterConsumptionParameter.getQuotaAction() == QuotaConsumptionParameter.QuotaAction.CONSUME ? clusterConsumptionParameter.getRequestedMemory() : -clusterConsumptionParameter.getRequestedMemory();
        int requestedCpu = clusterConsumptionParameter.getQuotaAction() == QuotaConsumptionParameter.QuotaAction.CONSUME ? clusterConsumptionParameter.getRequestedCpu() : -clusterConsumptionParameter.getRequestedCpu();
        if (checkQuotaClusterLimits(parameters.getAuditLogable().getStoragePool().getQuotaEnforcementType(), quota, quotaCluster, requestedMemory, requestedCpu, parameters.getValidationMessages(), auditLogPair)) {
            executed.add(clusterConsumptionParameter);
        } else {
            result = false;
            break;
        }
    }
    // if result is false (one or more parameters did not pass) - roll back the parameters that did pass
    if (!result) {
        rollBackClusterConsumptionParameters(executed);
    }
    return result;
}
Also used : Quota(org.ovirt.engine.core.common.businessentities.Quota) ArrayList(java.util.ArrayList) QuotaCluster(org.ovirt.engine.core.common.businessentities.QuotaCluster)

Example 42 with Quota

use of org.ovirt.engine.core.common.businessentities.Quota in project ovirt-engine by oVirt.

the class QuotaManager method generatePerUserUsageReport.

/**
 * Return a list of QuotaUsagePerUser representing the status of all the quotas available for a specific user
 *
 * @param quotaIdsList
 *            - quotas available for user
 * @return - list of QuotaUsagePerUser
 */
public Map<Guid, QuotaUsagePerUser> generatePerUserUsageReport(List<Quota> quotaIdsList) {
    Map<Guid, QuotaUsagePerUser> quotaPerUserUsageEntityMap = new HashMap<>();
    List<Quota> needToCache = new ArrayList<>();
    if (quotaIdsList != null) {
        lock.readLock().lock();
        try {
            for (Quota quotaExternal : quotaIdsList) {
                // look for the quota in the cache
                Map<Guid, Quota> quotaMap = storagePoolQuotaMap.get(quotaExternal.getStoragePoolId());
                Quota quota = null;
                if (quotaMap != null) {
                    quota = quotaMap.get(quotaExternal.getId());
                }
                // if quota not in cache look for it in DB and add it to cache
                if (quota == null) {
                    needToCache.add(quotaExternal);
                } else {
                    QuotaUsagePerUser usagePerUser = addQuotaEntry(quota);
                    if (usagePerUser != null) {
                        quotaPerUserUsageEntityMap.put(quota.getId(), usagePerUser);
                    }
                }
            }
        } finally {
            lock.readLock().unlock();
        }
        if (!needToCache.isEmpty()) {
            lock.writeLock().lock();
            try {
                for (Quota quotaExternal : needToCache) {
                    // look for the quota in the cache again (it may have been added by now)
                    addStoragePoolToCache(quotaExternal.getStoragePoolId());
                    Quota quota = fetchQuotaFromCache(quotaExternal.getId(), quotaExternal.getStoragePoolId());
                    QuotaUsagePerUser usagePerUser = addQuotaEntry(quota);
                    if (usagePerUser != null) {
                        quotaPerUserUsageEntityMap.put(quota.getId(), usagePerUser);
                    }
                }
            } finally {
                lock.writeLock().unlock();
            }
        }
    }
    return quotaPerUserUsageEntityMap;
}
Also used : Quota(org.ovirt.engine.core.common.businessentities.Quota) HashMap(java.util.HashMap) QuotaUsagePerUser(org.ovirt.engine.core.common.businessentities.QuotaUsagePerUser) ArrayList(java.util.ArrayList) Guid(org.ovirt.engine.core.compat.Guid)

Example 43 with Quota

use of org.ovirt.engine.core.common.businessentities.Quota in project ovirt-engine by oVirt.

the class QuotaManager method checkStoragePoolMatchQuota.

// In case this param is a QuotaStorageConsumptionParameter - check that it has a valid
// storage domain id which is handled by this quota
private boolean checkStoragePoolMatchQuota(QuotaConsumptionParametersWrapper parameters, QuotaStorageConsumptionParameter param) {
    Quota quota = param.getQuota();
    if (param.getStorageDomainId() == null) {
        parameters.getValidationMessages().add(EngineMessage.ACTION_TYPE_FAILED_QUOTA_IS_NOT_VALID.toString());
        log.error("Quota storage parameters from command '{}' are missing storage domain id", parameters.getAuditLogable().getClass().getName());
        return false;
    }
    boolean storageDomainInQuota = false;
    if (quota.getGlobalQuotaStorage() != null) {
        storageDomainInQuota = true;
    } else {
        for (QuotaStorage quotaStorage : quota.getQuotaStorages()) {
            if (quotaStorage.getStorageId().equals(param.getStorageDomainId())) {
                storageDomainInQuota = true;
                break;
            }
        }
    }
    if (!storageDomainInQuota) {
        parameters.getValidationMessages().add(EngineMessage.ACTION_TYPE_FAILED_NO_QUOTA_SET_FOR_DOMAIN.toString());
        log.error("Quota storage parameters from command '{}'. Storage domain does not match quota", parameters.getAuditLogable().getClass().getName());
        return false;
    }
    return true;
}
Also used : QuotaStorage(org.ovirt.engine.core.common.businessentities.QuotaStorage) Quota(org.ovirt.engine.core.common.businessentities.Quota)

Example 44 with Quota

use of org.ovirt.engine.core.common.businessentities.Quota in project ovirt-engine by oVirt.

the class QuotaManager method fetchQuotaFromCache.

/**
 * Get Quota by Id. If in cache - get from cache. else get from Dao and add to cache.
 *
 * @param quotaId - quota id
 * @param storagePoolId - storage pool containing this quota
 * @return - found quota. null if not found.
 */
private Quota fetchQuotaFromCache(Guid quotaId, Guid storagePoolId) throws InvalidQuotaParametersException {
    Map<Guid, Quota> quotaMap = storagePoolQuotaMap.get(storagePoolId);
    Quota quota = quotaMap.get(quotaId);
    // if quota was not found in cache - look for it in DB
    if (quota == null) {
        quota = getQuotaDao().getById(quotaId);
        if (quota != null) {
            // cache in quota map
            if (storagePoolId.equals(quota.getStoragePoolId())) {
                quotaMap.put(quotaId, quota);
            } else {
                throw new InvalidQuotaParametersException(String.format("Quota %s does not match storage pool %s", quotaId.toString(), storagePoolId.toString()));
            }
        }
    }
    return quota;
}
Also used : Quota(org.ovirt.engine.core.common.businessentities.Quota) Guid(org.ovirt.engine.core.compat.Guid)

Example 45 with Quota

use of org.ovirt.engine.core.common.businessentities.Quota in project ovirt-engine by oVirt.

the class UpdateQuotaCommandTest method testExecuteCommand.

@Test
public void testExecuteCommand() {
    // Execute the command
    command.executeCommand();
    Quota parameterQuota = command.getParameters().getQuota();
    Guid quotaId = parameterQuota.getId();
    for (QuotaStorage quotaStorage : parameterQuota.getQuotaStorages()) {
        assertNotNull("Quota Storage should have been assigned an ID", quotaStorage.getQuotaStorageId());
        assertEquals("Wrong Qutoa ID on Quota Storage", quotaId, quotaStorage.getQuotaId());
    }
    for (QuotaCluster quotaCluster : parameterQuota.getQuotaClusters()) {
        assertNotNull("Quota Cluster should have been assigned an ID", quotaCluster.getQuotaClusterId());
        assertEquals("Wrong Qutoa ID on Quota Cluster", quotaId, quotaCluster.getQuotaId());
    }
    // Verify the quota was updated in the database
    verify(quotaDao).update(parameterQuota);
    // Assert the return value
    assertTrue("Execution should be successful", command.getReturnValue().getSucceeded());
}
Also used : QuotaStorage(org.ovirt.engine.core.common.businessentities.QuotaStorage) Quota(org.ovirt.engine.core.common.businessentities.Quota) QuotaCluster(org.ovirt.engine.core.common.businessentities.QuotaCluster) Guid(org.ovirt.engine.core.compat.Guid) Test(org.junit.Test)

Aggregations

Quota (org.ovirt.engine.core.common.businessentities.Quota)101 ArrayList (java.util.ArrayList)23 Guid (org.ovirt.engine.core.compat.Guid)22 Test (org.junit.Test)17 QuotaCluster (org.ovirt.engine.core.common.businessentities.QuotaCluster)17 QuotaStorage (org.ovirt.engine.core.common.businessentities.QuotaStorage)16 IdQueryParameters (org.ovirt.engine.core.common.queries.IdQueryParameters)12 HashMap (java.util.HashMap)9 List (java.util.List)9 QueryType (org.ovirt.engine.core.common.queries.QueryType)9 Cluster (org.ovirt.engine.core.common.businessentities.Cluster)8 StorageDomain (org.ovirt.engine.core.common.businessentities.StorageDomain)8 QueryReturnValue (org.ovirt.engine.core.common.queries.QueryReturnValue)8 ActionParametersBase (org.ovirt.engine.core.common.action.ActionParametersBase)6 QuotaCRUDParameters (org.ovirt.engine.core.common.action.QuotaCRUDParameters)6 StoragePool (org.ovirt.engine.core.common.businessentities.StoragePool)6 Map (java.util.Map)5 RepoImage (org.ovirt.engine.core.common.businessentities.storage.RepoImage)5 UICommand (org.ovirt.engine.ui.uicommonweb.UICommand)5 ConfirmationModel (org.ovirt.engine.ui.uicommonweb.models.ConfirmationModel)5