Search in sources :

Example 76 with CloudRuntimeException

use of com.cloud.legacymodel.exceptions.CloudRuntimeException in project cosmic by MissionCriticalCloud.

the class StorageNetworkManagerImpl method createIpRange.

@Override
@DB
public StorageNetworkIpRange createIpRange(final CreateStorageNetworkIpRangeCmd cmd) throws SQLException {
    final Long podId = cmd.getPodId();
    final String startIp = cmd.getStartIp();
    String endIp = cmd.getEndIp();
    final Integer vlan = cmd.getVlan();
    final String netmask = cmd.getNetmask();
    if (endIp == null) {
        endIp = startIp;
    }
    if (!NetUtils.isValidIp4Netmask(netmask)) {
        throw new CloudRuntimeException("Invalid netmask:" + netmask);
    }
    final HostPodVO pod = _podDao.findById(podId);
    if (pod == null) {
        throw new CloudRuntimeException("Cannot find pod " + podId);
    }
    final Long zoneId = pod.getDataCenterId();
    final List<NetworkVO> nws = _networkDao.listByZoneAndTrafficType(zoneId, TrafficType.Storage);
    if (nws.size() == 0) {
        throw new CloudRuntimeException("Cannot find storage network in zone " + zoneId);
    }
    if (nws.size() > 1) {
        throw new CloudRuntimeException("Find more than one storage network in zone " + zoneId + "," + nws.size() + " found");
    }
    final NetworkVO nw = nws.get(0);
    checkOverlapPrivateIpRange(podId, startIp, endIp);
    checkOverlapStorageIpRange(podId, startIp, endIp);
    final StorageNetworkIpRangeVO range = null;
    final String endIpFinal = endIp;
    return Transaction.execute(new TransactionCallbackWithException<StorageNetworkIpRangeVO, SQLException>() {

        @Override
        public StorageNetworkIpRangeVO doInTransaction(final TransactionStatus status) throws SQLException {
            final StorageNetworkIpRangeVO range = new StorageNetworkIpRangeVO(zoneId, podId, nw.getId(), startIp, endIpFinal, vlan, netmask, cmd.getGateWay());
            _sNwIpRangeDao.persist(range);
            try {
                createStorageIpEntires(TransactionLegacy.currentTxn(), range.getId(), startIp, endIpFinal, zoneId);
            } catch (final SQLException e) {
                final StringBuilder err = new StringBuilder();
                err.append("Create storage network range failed.");
                err.append("startIp=" + startIp);
                err.append("endIp=" + endIpFinal);
                err.append("netmask=" + netmask);
                err.append("zoneId=" + zoneId);
                s_logger.debug(err.toString(), e);
                throw e;
            }
            return range;
        }
    });
}
Also used : StorageNetworkIpRangeVO(com.cloud.dc.StorageNetworkIpRangeVO) NetworkVO(com.cloud.network.dao.NetworkVO) SQLException(java.sql.SQLException) TransactionStatus(com.cloud.utils.db.TransactionStatus) HostPodVO(com.cloud.dc.HostPodVO) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) DB(com.cloud.utils.db.DB)

Example 77 with CloudRuntimeException

use of com.cloud.legacymodel.exceptions.CloudRuntimeException in project cosmic by MissionCriticalCloud.

the class StorageNetworkManagerImpl method acquireIpAddress.

@Override
public StorageNetworkIpAddressVO acquireIpAddress(final long podId) {
    final List<StorageNetworkIpRangeVO> ranges = _sNwIpRangeDao.listByPodId(podId);
    for (StorageNetworkIpRangeVO r : ranges) {
        try {
            final Long rangeId = r.getId();
            r = _sNwIpRangeDao.acquireInLockTable(rangeId);
            if (r == 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);
            }
            final StorageNetworkIpAddressVO ip = _sNwIpDao.takeIpAddress(r.getId());
            if (ip != null) {
                return ip;
            }
        } finally {
            if (r != null) {
                _sNwIpRangeDao.releaseFromLockTable(r.getId());
            }
        }
    }
    return null;
}
Also used : StorageNetworkIpRangeVO(com.cloud.dc.StorageNetworkIpRangeVO) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) StorageNetworkIpAddressVO(com.cloud.dc.StorageNetworkIpAddressVO)

Example 78 with CloudRuntimeException

use of com.cloud.legacymodel.exceptions.CloudRuntimeException 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.legacymodel.exceptions.CloudRuntimeException) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) DB(com.cloud.utils.db.DB)

Example 79 with CloudRuntimeException

use of com.cloud.legacymodel.exceptions.CloudRuntimeException 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.legacymodel.exceptions.CloudRuntimeException) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) DB(com.cloud.utils.db.DB)

Example 80 with CloudRuntimeException

use of com.cloud.legacymodel.exceptions.CloudRuntimeException in project cosmic by MissionCriticalCloud.

the class NetworkServiceImpl method createPrivateNetwork.

@Override
@DB
public Network createPrivateNetwork(final String networkName, final String displayText, final long physicalNetworkId, final String broadcastUriString, final String startIp, String endIp, final String gateway, final String netmask, final long networkOwnerId, final Long vpcId, final Boolean sourceNat, final Long networkOfferingId) throws ResourceAllocationException, ConcurrentOperationException, InsufficientCapacityException {
    final Account owner = _accountMgr.getAccount(networkOwnerId);
    // Get system network offering
    NetworkOfferingVO ntwkOff = null;
    if (networkOfferingId != null) {
        ntwkOff = _networkOfferingDao.findById(networkOfferingId);
    }
    if (ntwkOff == null) {
        ntwkOff = findSystemNetworkOffering(NetworkOffering.DefaultPrivateGatewayNetworkOffering);
    }
    // Validate physical network
    final PhysicalNetwork pNtwk = _physicalNetworkDao.findById(physicalNetworkId);
    if (pNtwk == null) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find a physical network" + " having the given id");
        ex.addProxyObject(String.valueOf(physicalNetworkId), "physicalNetworkId");
        throw ex;
    }
    // if end ip is not specified, default it to startIp
    if (!NetUtils.isValidIp4(startIp)) {
        throw new InvalidParameterValueException("Invalid format for the ip address parameter");
    }
    if (endIp == null) {
        endIp = startIp;
    } else if (!NetUtils.isValidIp4(endIp)) {
        throw new InvalidParameterValueException("Invalid format for the endIp address parameter");
    }
    if (!NetUtils.isValidIp4(gateway)) {
        throw new InvalidParameterValueException("Invalid gateway");
    }
    if (!NetUtils.isValidIp4Netmask(netmask)) {
        throw new InvalidParameterValueException("Invalid netmask");
    }
    final String cidr = NetUtils.ipAndNetMaskToCidr(gateway, netmask);
    final URI uri = BroadcastDomainType.fromString(broadcastUriString);
    final String uriString = uri.toString();
    final BroadcastDomainType tiep = BroadcastDomainType.getSchemeValue(uri);
    // TODO make a test for any supported scheme
    if (!(tiep == BroadcastDomainType.Vlan || tiep == BroadcastDomainType.Lswitch)) {
        throw new InvalidParameterValueException("unsupported type of broadcastUri specified: " + broadcastUriString);
    }
    final NetworkOfferingVO ntwkOffFinal = ntwkOff;
    try {
        return Transaction.execute(new TransactionCallbackWithException<Network, Exception>() {

            @Override
            public Network doInTransaction(final TransactionStatus status) throws ResourceAllocationException, InsufficientCapacityException {
                // lock datacenter as we need to get mac address seq from there
                final DataCenterVO dc = _dcDao.lockRow(pNtwk.getDataCenterId(), true);
                // check if we need to create guest network
                Network privateNetwork = _networksDao.getPrivateNetwork(uriString, cidr, networkOwnerId, pNtwk.getDataCenterId(), networkOfferingId);
                if (privateNetwork == null) {
                    // create Guest network
                    privateNetwork = _networkMgr.createGuestNetwork(ntwkOffFinal.getId(), networkName, displayText, gateway, cidr, uriString, null, owner, null, pNtwk, pNtwk.getDataCenterId(), ACLType.Account, null, vpcId, null, null, true, null, dc.getDns1(), dc.getDns2(), null, null, null);
                    if (privateNetwork != null) {
                        s_logger.debug("Successfully created guest network " + privateNetwork);
                    } else {
                        throw new CloudRuntimeException("Creating guest network failed");
                    }
                } else {
                    s_logger.debug("Private network already exists: " + privateNetwork);
                    // Do not allow multiple private gateways with same Vlan within a VPC
                    if (vpcId != null && vpcId.equals(privateNetwork.getVpcId())) {
                        throw new InvalidParameterValueException("Private network for the vlan: " + uriString + " and cidr  " + cidr + "  already exists " + "for Vpc " + vpcId + " in zone " + _entityMgr.findById(DataCenter.class, pNtwk.getDataCenterId()).getName());
                    }
                }
                if (vpcId != null) {
                    // add entry to private_ip_address table
                    PrivateIpVO privateIp = _privateIpDao.findByIpAndSourceNetworkIdAndVpcId(privateNetwork.getId(), startIp, vpcId);
                    if (privateIp != null) {
                        throw new InvalidParameterValueException("Private ip address " + startIp + " already used for private gateway" + " in zone " + _entityMgr.findById(DataCenter.class, pNtwk.getDataCenterId()).getName());
                    }
                    final Long mac = dc.getMacAddress();
                    final Long nextMac = mac + 1;
                    dc.setMacAddress(nextMac);
                    privateIp = new PrivateIpVO(startIp, privateNetwork.getId(), nextMac, vpcId, sourceNat);
                    _privateIpDao.persist(privateIp);
                    _dcDao.update(dc.getId(), dc);
                }
                s_logger.debug("Private network " + privateNetwork + " is created");
                return privateNetwork;
            }
        });
    } catch (final Exception e) {
        ExceptionUtil.rethrowRuntime(e);
        ExceptionUtil.rethrow(e, ResourceAllocationException.class);
        ExceptionUtil.rethrow(e, InsufficientCapacityException.class);
        throw new IllegalStateException(e);
    }
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) Account(com.cloud.legacymodel.user.Account) TransactionStatus(com.cloud.utils.db.TransactionStatus) PrivateIpVO(com.cloud.network.vpc.PrivateIpVO) URI(java.net.URI) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) PermissionDeniedException(com.cloud.legacymodel.exceptions.PermissionDeniedException) InvalidParameterException(java.security.InvalidParameterException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) ResourceAllocationException(com.cloud.legacymodel.exceptions.ResourceAllocationException) SQLException(java.sql.SQLException) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) UnknownHostException(java.net.UnknownHostException) InsufficientAddressCapacityException(com.cloud.legacymodel.exceptions.InsufficientAddressCapacityException) InsufficientCapacityException(com.cloud.legacymodel.exceptions.InsufficientCapacityException) UnsupportedServiceException(com.cloud.legacymodel.exceptions.UnsupportedServiceException) ConfigurationException(javax.naming.ConfigurationException) ResourceUnavailableException(com.cloud.legacymodel.exceptions.ResourceUnavailableException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) BroadcastDomainType(com.cloud.model.enumeration.BroadcastDomainType) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) Network(com.cloud.legacymodel.network.Network) NetworkOfferingVO(com.cloud.offerings.NetworkOfferingVO) ResourceAllocationException(com.cloud.legacymodel.exceptions.ResourceAllocationException) InsufficientCapacityException(com.cloud.legacymodel.exceptions.InsufficientCapacityException) DB(com.cloud.utils.db.DB)

Aggregations

CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)587 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)159 ArrayList (java.util.ArrayList)110 DB (com.cloud.utils.db.DB)90 Account (com.cloud.legacymodel.user.Account)84 SQLException (java.sql.SQLException)84 ActionEvent (com.cloud.event.ActionEvent)73 ConfigurationException (javax.naming.ConfigurationException)73 PreparedStatement (java.sql.PreparedStatement)68 HashMap (java.util.HashMap)68 ResourceUnavailableException (com.cloud.legacymodel.exceptions.ResourceUnavailableException)62 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)52 HostVO (com.cloud.host.HostVO)50 ConcurrentOperationException (com.cloud.legacymodel.exceptions.ConcurrentOperationException)50 NoTransitionException (com.cloud.legacymodel.exceptions.NoTransitionException)50 XenAPIException (com.xensource.xenapi.Types.XenAPIException)47 Answer (com.cloud.legacymodel.communication.answer.Answer)45 XmlRpcException (org.apache.xmlrpc.XmlRpcException)45 TransactionStatus (com.cloud.utils.db.TransactionStatus)44 IOException (java.io.IOException)44