Search in sources :

Example 56 with TransactionCallbackNoReturn

use of com.cloud.utils.db.TransactionCallbackNoReturn in project cloudstack by apache.

the class VpcManagerImpl method destroyVpc.

@Override
@DB
public boolean destroyVpc(final Vpc vpc, final Account caller, final Long callerUserId) throws ConcurrentOperationException, ResourceUnavailableException {
    s_logger.debug("Destroying vpc " + vpc);
    // don't allow to delete vpc if it's in use by existing non system
    // networks (system networks are networks of a private gateway of the
    // VPC,
    // and they will get removed as a part of VPC cleanup
    final int networksCount = _ntwkDao.getNonSystemNetworkCountByVpcId(vpc.getId());
    if (networksCount > 0) {
        throw new InvalidParameterValueException("Can't delete VPC " + vpc + " as its used by " + networksCount + " networks");
    }
    // mark VPC as inactive
    if (vpc.getState() != Vpc.State.Inactive) {
        s_logger.debug("Updating VPC " + vpc + " with state " + Vpc.State.Inactive + " as a part of vpc delete");
        final VpcVO vpcVO = _vpcDao.findById(vpc.getId());
        vpcVO.setState(Vpc.State.Inactive);
        Transaction.execute(new TransactionCallbackNoReturn() {

            @Override
            public void doInTransactionWithoutResult(final TransactionStatus status) {
                _vpcDao.update(vpc.getId(), vpcVO);
                // decrement resource count
                _resourceLimitMgr.decrementResourceCount(vpc.getAccountId(), ResourceType.vpc);
            }
        });
    }
    // shutdown VPC
    if (!shutdownVpc(vpc.getId())) {
        s_logger.warn("Failed to shutdown vpc " + vpc + " as a part of vpc destroy process");
        return false;
    }
    // cleanup vpc resources
    if (!cleanupVpcResources(vpc.getId(), caller, callerUserId)) {
        s_logger.warn("Failed to cleanup resources for vpc " + vpc);
        return false;
    }
    // executed successfully
    if (_vpcDao.remove(vpc.getId())) {
        s_logger.debug("Vpc " + vpc + " is destroyed succesfully");
        return true;
    } else {
        s_logger.warn("Vpc " + vpc + " failed to destroy");
        return false;
    }
}
Also used : InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) DB(com.cloud.utils.db.DB)

Example 57 with TransactionCallbackNoReturn

use of com.cloud.utils.db.TransactionCallbackNoReturn in project cloudstack by apache.

the class VpcManagerImpl method deleteVpcPrivateGateway.

@Override
@ActionEvent(eventType = EventTypes.EVENT_PRIVATE_GATEWAY_DELETE, eventDescription = "deleting private gateway")
@DB
public boolean deleteVpcPrivateGateway(final long gatewayId) throws ConcurrentOperationException, ResourceUnavailableException {
    final VpcGatewayVO gatewayVO = _vpcGatewayDao.acquireInLockTable(gatewayId);
    if (gatewayVO == null || gatewayVO.getType() != VpcGateway.Type.Private) {
        throw new ConcurrentOperationException("Unable to lock gateway " + gatewayId);
    }
    try {
        Transaction.execute(new TransactionCallbackNoReturn() {

            @Override
            public void doInTransactionWithoutResult(final TransactionStatus status) {
                // don't allow to remove gateway when there are static
                // routes associated with it
                final long routeCount = _staticRouteDao.countRoutesByGateway(gatewayVO.getId());
                if (routeCount > 0) {
                    throw new CloudRuntimeException("Can't delete private gateway " + gatewayVO + " as it has " + routeCount + " static routes applied. Remove the routes first");
                }
                gatewayVO.setState(VpcGateway.State.Deleting);
                _vpcGatewayDao.update(gatewayVO.getId(), gatewayVO);
                s_logger.debug("Marked gateway " + gatewayVO + " with state " + VpcGateway.State.Deleting);
            }
        });
        // 1) delete the gateway on the backend
        final List<Provider> providersToImplement = getVpcProviders(gatewayVO.getVpcId());
        final PrivateGateway gateway = getVpcPrivateGateway(gatewayId);
        for (final VpcProvider provider : getVpcElements()) {
            if (providersToImplement.contains(provider.getProvider())) {
                if (provider.deletePrivateGateway(gateway)) {
                    s_logger.debug("Private gateway " + gateway + " was applied succesfully on the backend");
                } else {
                    s_logger.warn("Private gateway " + gateway + " failed to apply on the backend");
                    gatewayVO.setState(VpcGateway.State.Ready);
                    _vpcGatewayDao.update(gatewayVO.getId(), gatewayVO);
                    s_logger.debug("Marked gateway " + gatewayVO + " with state " + VpcGateway.State.Ready);
                    return false;
                }
            }
        }
        // 2) Delete private gateway from the DB
        return deletePrivateGatewayFromTheDB(gateway);
    } finally {
        if (gatewayVO != null) {
            _vpcGatewayDao.releaseFromLockTable(gatewayId);
        }
    }
}
Also used : VpcProvider(com.cloud.network.element.VpcProvider) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) StaticNatServiceProvider(com.cloud.network.element.StaticNatServiceProvider) VpcProvider(com.cloud.network.element.VpcProvider) Provider(com.cloud.network.Network.Provider) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 58 with TransactionCallbackNoReturn

use of com.cloud.utils.db.TransactionCallbackNoReturn in project cloudstack by apache.

the class AsyncJobManagerImpl method updateAsyncJobAttachment.

@Override
@DB
public void updateAsyncJobAttachment(final long jobId, final String instanceType, final Long instanceId) {
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Update async-job attachment, job-" + jobId + ", instanceType: " + instanceType + ", instanceId: " + instanceId);
    }
    final AsyncJobVO job = _jobDao.findById(jobId);
    publishOnEventBus(job, "update");
    Transaction.execute(new TransactionCallbackNoReturn() {

        @Override
        public void doInTransactionWithoutResult(TransactionStatus status) {
            AsyncJobVO job = _jobDao.createForUpdate();
            job.setInstanceType(instanceType);
            job.setInstanceId(instanceId);
            job.setLastUpdated(DateUtil.currentGMTTime());
            _jobDao.update(jobId, job);
        }
    });
}
Also used : TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) DB(com.cloud.utils.db.DB)

Example 59 with TransactionCallbackNoReturn

use of com.cloud.utils.db.TransactionCallbackNoReturn in project cloudstack by apache.

the class AsyncJobManagerImpl method updateAsyncJobStatus.

@Override
@DB
public void updateAsyncJobStatus(final long jobId, final int processStatus, final String resultObject) {
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Update async-job progress, job-" + jobId + ", processStatus: " + processStatus + ", result: " + resultObject);
    }
    final AsyncJobVO job = _jobDao.findById(jobId);
    if (job == null) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("job-" + jobId + " no longer exists, we just log progress info here. progress status: " + processStatus);
        }
        return;
    }
    publishOnEventBus(job, "update");
    Transaction.execute(new TransactionCallbackNoReturn() {

        @Override
        public void doInTransactionWithoutResult(TransactionStatus status) {
            job.setProcessStatus(processStatus);
            if (resultObject != null) {
                job.setResult(resultObject);
            }
            job.setLastUpdated(DateUtil.currentGMTTime());
            _jobDao.update(jobId, job);
        }
    });
}
Also used : TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) DB(com.cloud.utils.db.DB)

Example 60 with TransactionCallbackNoReturn

use of com.cloud.utils.db.TransactionCallbackNoReturn in project cloudstack by apache.

the class CiscoNexusVSMDeviceManagerImpl method deleteCiscoNexusVSM.

@DB
public boolean deleteCiscoNexusVSM(final long vsmId) throws ResourceInUseException {
    CiscoNexusVSMDeviceVO cisconexusvsm = _ciscoNexusVSMDeviceDao.findById(vsmId);
    if (cisconexusvsm == null) {
        // This entry is already not present. Return success.
        return true;
    }
    // First, check whether this VSM is part of any non-empty cluster.
    // Search ClusterVSMMap's table for a list of clusters using this vsmId.
    List<ClusterVSMMapVO> clusterList = _clusterVSMDao.listByVSMId(vsmId);
    if (clusterList != null) {
        for (ClusterVSMMapVO record : clusterList) {
            // If this cluster id has any hosts in it, fail this operation.
            Long clusterId = record.getClusterId();
            List<HostVO> hosts = _resourceMgr.listAllHostsInCluster(clusterId);
            if (hosts != null && hosts.size() > 0) {
                for (Host host : hosts) {
                    if (host.getType() == Host.Type.Routing) {
                        s_logger.info("Non-empty cluster with id" + clusterId + "still has a host that uses this VSM. Please empty the cluster first");
                        throw new ResourceInUseException("Non-empty cluster with id" + clusterId + "still has a host that uses this VSM. Please empty the cluster first");
                    }
                }
            }
        }
    }
    // Iterate through the cluster list again, this time, delete the VSM.
    Transaction.execute(new TransactionCallbackNoReturn() {

        @Override
        public void doInTransactionWithoutResult(TransactionStatus status) {
            // Remove the VSM entry in CiscoNexusVSMDeviceVO's table.
            _ciscoNexusVSMDeviceDao.remove(vsmId);
            // Remove the current record as well from ClusterVSMMapVO's table.
            _clusterVSMDao.removeByVsmId(vsmId);
        }
    });
    return true;
}
Also used : ClusterVSMMapVO(com.cloud.dc.ClusterVSMMapVO) ResourceInUseException(com.cloud.exception.ResourceInUseException) TransactionStatus(com.cloud.utils.db.TransactionStatus) Host(com.cloud.host.Host) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) HostVO(com.cloud.host.HostVO) DB(com.cloud.utils.db.DB)

Aggregations

TransactionCallbackNoReturn (com.cloud.utils.db.TransactionCallbackNoReturn)93 TransactionStatus (com.cloud.utils.db.TransactionStatus)93 DB (com.cloud.utils.db.DB)73 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)50 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)34 ArrayList (java.util.ArrayList)30 List (java.util.List)21 ActionEvent (com.cloud.event.ActionEvent)20 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)19 Account (com.cloud.user.Account)18 ConfigurationException (javax.naming.ConfigurationException)15 IPAddressVO (com.cloud.network.dao.IPAddressVO)14 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)11 Network (com.cloud.network.Network)10 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)9 HostVO (com.cloud.host.HostVO)9 HashMap (java.util.HashMap)9 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)8 DataCenter (com.cloud.dc.DataCenter)7 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)7