Search in sources :

Example 21 with TransactionCallbackNoReturn

use of com.cloud.utils.db.TransactionCallbackNoReturn in project cosmic by MissionCriticalCloud.

the class NetworkServiceImpl method addOrRemoveVnets.

@DB
private void addOrRemoveVnets(final String[] listOfRanges, final PhysicalNetworkVO network) {
    List<String> addVnets = null;
    List<String> removeVnets = null;
    final HashSet<String> tempVnets = new HashSet<>();
    final HashSet<String> vnetsInDb = new HashSet<>();
    List<Pair<Integer, Integer>> vnetranges = null;
    String comaSeperatedStingOfVnetRanges = null;
    int i = 0;
    if (listOfRanges.length != 0) {
        _physicalNetworkDao.acquireInLockTable(network.getId(), 10);
        vnetranges = validateVlanRange(network, listOfRanges);
        // computing vnets to be removed.
        removeVnets = getVnetsToremove(network, vnetranges);
        // computing vnets to add
        vnetsInDb.addAll(_datacneterVnet.listVnetsByPhysicalNetworkAndDataCenter(network.getDataCenterId(), network.getId()));
        tempVnets.addAll(vnetsInDb);
        for (final Pair<Integer, Integer> vlan : vnetranges) {
            for (i = vlan.first(); i <= vlan.second(); i++) {
                tempVnets.add(Integer.toString(i));
            }
        }
        tempVnets.removeAll(vnetsInDb);
        // adding and removing vnets from vnetsInDb
        if (removeVnets != null && removeVnets.size() != 0) {
            vnetsInDb.removeAll(removeVnets);
        }
        if (tempVnets.size() != 0) {
            addVnets = new ArrayList<>();
            addVnets.addAll(tempVnets);
            vnetsInDb.addAll(tempVnets);
        }
        // sorting the vnets in Db to generate a coma seperated list of  the vnet string.
        if (vnetsInDb.size() != 0) {
            comaSeperatedStingOfVnetRanges = generateVnetString(new ArrayList<>(vnetsInDb));
        }
        network.setVnet(comaSeperatedStingOfVnetRanges);
        final List<String> addVnetsFinal = addVnets;
        final List<String> removeVnetsFinal = removeVnets;
        Transaction.execute(new TransactionCallbackNoReturn() {

            @Override
            public void doInTransactionWithoutResult(final TransactionStatus status) {
                if (addVnetsFinal != null) {
                    s_logger.debug("Adding vnet range " + addVnetsFinal.toString() + " for the physicalNetwork id= " + network.getId() + " and zone id=" + network.getDataCenterId() + " as a part of updatePhysicalNetwork call");
                    // add vnet takes a list of strings to be added. each string is a vnet.
                    _dcDao.addVnet(network.getDataCenterId(), network.getId(), addVnetsFinal);
                }
                if (removeVnetsFinal != null) {
                    s_logger.debug("removing vnet range " + removeVnetsFinal.toString() + " for the physicalNetwork id= " + network.getId() + " and zone id=" + network.getDataCenterId() + " as a part of updatePhysicalNetwork call");
                    // deleteVnets  takes a list of strings to be removed. each string is a vnet.
                    _datacneterVnet.deleteVnets(TransactionLegacy.currentTxn(), network.getDataCenterId(), network.getId(), removeVnetsFinal);
                }
                _physicalNetworkDao.update(network.getId(), network);
            }
        });
        _physicalNetworkDao.releaseFromLockTable(network.getId());
    }
}
Also used : ArrayList(java.util.ArrayList) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) HashSet(java.util.HashSet) Pair(com.cloud.utils.Pair) DB(com.cloud.utils.db.DB)

Example 22 with TransactionCallbackNoReturn

use of com.cloud.utils.db.TransactionCallbackNoReturn in project cosmic by MissionCriticalCloud.

the class NetworkServiceImpl method removeNicSecondaryIP.

private boolean removeNicSecondaryIP(final NicSecondaryIpVO ipVO, final boolean lastIp) {
    final long nicId = ipVO.getNicId();
    final NicVO nic = _nicDao.findById(nicId);
    Transaction.execute(new TransactionCallbackNoReturn() {

        @Override
        public void doInTransactionWithoutResult(final TransactionStatus status) {
            if (lastIp) {
                nic.setSecondaryIp(false);
                s_logger.debug("Setting nics secondary ip to false ...");
                _nicDao.update(nicId, nic);
            }
            s_logger.debug("Revoving nic secondary ip entry ...");
            _nicSecondaryIpDao.remove(ipVO.getId());
        }
    });
    return true;
}
Also used : TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) NicVO(com.cloud.vm.NicVO)

Example 23 with TransactionCallbackNoReturn

use of com.cloud.utils.db.TransactionCallbackNoReturn in project cosmic by MissionCriticalCloud.

the class StorageNetworkManagerImpl method updateIpRange.

@Override
@DB
public StorageNetworkIpRange updateIpRange(final UpdateStorageNetworkIpRangeCmd cmd) {
    final Integer vlan = cmd.getVlan();
    final Long rangeId = cmd.getId();
    String startIp = cmd.getStartIp();
    String endIp = cmd.getEndIp();
    final String netmask = cmd.getNetmask();
    if (netmask != null && !NetUtils.isValidIp4Netmask(netmask)) {
        throw new CloudRuntimeException("Invalid netmask:" + netmask);
    }
    if (_sNwIpDao.countInUseIpByRangeId(rangeId) > 0) {
        throw new CloudRuntimeException("Cannot update the range," + getInUseIpAddress(rangeId));
    }
    final StorageNetworkIpRangeVO range = _sNwIpRangeDao.findById(rangeId);
    if (range == null) {
        throw new CloudRuntimeException("Cannot find storage ip range " + rangeId);
    }
    if (startIp != null || endIp != null) {
        final long podId = range.getPodId();
        startIp = startIp == null ? range.getStartIp() : startIp;
        endIp = endIp == null ? range.getEndIp() : endIp;
        checkOverlapPrivateIpRange(podId, startIp, endIp);
        checkOverlapStorageIpRange(podId, startIp, endIp);
    }
    final String startIpFinal = startIp;
    final String endIpFinal = endIp;
    Transaction.execute(new TransactionCallbackNoReturn() {

        @Override
        public void doInTransactionWithoutResult(final TransactionStatus status) {
            StorageNetworkIpRangeVO range = null;
            try {
                range = _sNwIpRangeDao.acquireInLockTable(rangeId);
                if (range == null) {
                    throw new CloudRuntimeException("Cannot acquire lock on storage ip range " + rangeId);
                }
                final StorageNetworkIpRangeVO vo = _sNwIpRangeDao.createForUpdate();
                if (vlan != null) {
                    vo.setVlan(vlan);
                }
                if (startIpFinal != null) {
                    vo.setStartIp(startIpFinal);
                }
                if (endIpFinal != null) {
                    vo.setEndIp(endIpFinal);
                }
                if (netmask != null) {
                    vo.setNetmask(netmask);
                }
                _sNwIpRangeDao.update(rangeId, vo);
            } finally {
                if (range != null) {
                    _sNwIpRangeDao.releaseFromLockTable(range.getId());
                }
            }
        }
    });
    return _sNwIpRangeDao.findById(rangeId);
}
Also used : StorageNetworkIpRangeVO(com.cloud.dc.StorageNetworkIpRangeVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) DB(com.cloud.utils.db.DB)

Example 24 with TransactionCallbackNoReturn

use of com.cloud.utils.db.TransactionCallbackNoReturn in project cosmic by MissionCriticalCloud.

the class StorageNetworkManagerImpl method deleteIpRange.

@Override
@DB
public void deleteIpRange(final DeleteStorageNetworkIpRangeCmd cmd) {
    final long rangeId = cmd.getId();
    final StorageNetworkIpRangeVO range = _sNwIpRangeDao.findById(rangeId);
    if (range == null) {
        throw new CloudRuntimeException("Can not find storage network ip range " + rangeId);
    }
    if (_sNwIpDao.countInUseIpByRangeId(rangeId) > 0) {
        throw new CloudRuntimeException(getInUseIpAddress(rangeId));
    }
    Transaction.execute(new TransactionCallbackNoReturn() {

        @Override
        public void doInTransactionWithoutResult(final TransactionStatus status) {
            StorageNetworkIpRangeVO range = null;
            try {
                range = _sNwIpRangeDao.acquireInLockTable(rangeId);
                if (range == null) {
                    final String msg = "Unable to acquire lock on storage network ip range id=" + rangeId + ", delete failed";
                    s_logger.warn(msg);
                    throw new CloudRuntimeException(msg);
                }
                /*
                     * entries in op_dc_storage_network_ip_address will be deleted automatically due to
                     * fk_storage_ip_address__range_id constraint key
                     */
                _sNwIpRangeDao.remove(rangeId);
            } finally {
                if (range != null) {
                    _sNwIpRangeDao.releaseFromLockTable(rangeId);
                }
            }
        }
    });
}
Also used : StorageNetworkIpRangeVO(com.cloud.dc.StorageNetworkIpRangeVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) DB(com.cloud.utils.db.DB)

Example 25 with TransactionCallbackNoReturn

use of com.cloud.utils.db.TransactionCallbackNoReturn in project cosmic by MissionCriticalCloud.

the class ManagementServerImpl method updateDomain.

@Override
@DB
public DomainVO updateDomain(final UpdateDomainCmd cmd) {
    final Long domainId = cmd.getId();
    final String domainName = cmd.getDomainName();
    final String networkDomain = cmd.getNetworkDomain();
    // check if domain exists in the system
    final DomainVO domain = _domainDao.findById(domainId);
    if (domain == null) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find domain with specified domain id");
        ex.addProxyObject(domainId.toString(), "domainId");
        throw ex;
    } else if (domain.getParent() == null && domainName != null) {
        // name
        throw new InvalidParameterValueException("ROOT domain can not be edited with a new name");
    }
    final Account caller = getCaller();
    _accountMgr.checkAccess(caller, domain);
    // domain name is unique under the parent domain
    if (domainName != null) {
        final SearchCriteria<DomainVO> sc = _domainDao.createSearchCriteria();
        sc.addAnd("name", SearchCriteria.Op.EQ, domainName);
        sc.addAnd("parent", SearchCriteria.Op.EQ, domain.getParent());
        final List<DomainVO> domains = _domainDao.search(sc, null);
        final boolean sameDomain = domains.size() == 1 && domains.get(0).getId() == domainId;
        if (!domains.isEmpty() && !sameDomain) {
            final InvalidParameterValueException ex = new InvalidParameterValueException("Failed to update specified domain id with name '" + domainName + "' since it already exists in the system");
            ex.addProxyObject(domain.getUuid(), "domainId");
            throw ex;
        }
    }
    // validate network domain
    if (networkDomain != null && !networkDomain.isEmpty()) {
        if (!NetUtils.verifyDomainName(networkDomain)) {
            throw new InvalidParameterValueException("Invalid network domain. Total length shouldn't exceed 190 chars. Each domain label must be between 1 and 63 characters long, can contain ASCII letters " + "'a' through 'z', the digits '0' through '9', " + "and the hyphen ('-'); can't start or end with \"-\"");
        }
    }
    Transaction.execute(new TransactionCallbackNoReturn() {

        @Override
        public void doInTransactionWithoutResult(final TransactionStatus status) {
            if (domainName != null) {
                final String updatedDomainPath = getUpdatedDomainPath(domain.getPath(), domainName);
                updateDomainChildren(domain, updatedDomainPath);
                domain.setName(domainName);
                domain.setPath(updatedDomainPath);
            }
            if (networkDomain != null) {
                if (networkDomain.isEmpty()) {
                    domain.setNetworkDomain(null);
                } else {
                    domain.setNetworkDomain(networkDomain);
                }
            }
            _domainDao.update(domainId, domain);
        }
    });
    return _domainDao.findById(domainId);
}
Also used : DomainVO(com.cloud.domain.DomainVO) Account(com.cloud.user.Account) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) DB(com.cloud.utils.db.DB)

Aggregations

TransactionCallbackNoReturn (com.cloud.utils.db.TransactionCallbackNoReturn)172 TransactionStatus (com.cloud.utils.db.TransactionStatus)172 DB (com.cloud.utils.db.DB)133 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)71 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)60 ArrayList (java.util.ArrayList)57 ActionEvent (com.cloud.event.ActionEvent)42 List (java.util.List)42 Account (com.cloud.user.Account)39 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)38 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)36 ConfigurationException (javax.naming.ConfigurationException)32 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)26 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)24 IPAddressVO (com.cloud.network.dao.IPAddressVO)24 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)23 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)18 HostVO (com.cloud.host.HostVO)18 HashMap (java.util.HashMap)18 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)17