Search in sources :

Example 1 with StorageNetworkIpRangeVO

use of com.cloud.dc.StorageNetworkIpRangeVO 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 2 with StorageNetworkIpRangeVO

use of com.cloud.dc.StorageNetworkIpRangeVO 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 3 with StorageNetworkIpRangeVO

use of com.cloud.dc.StorageNetworkIpRangeVO 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.utils.exception.CloudRuntimeException) StorageNetworkIpAddressVO(com.cloud.dc.StorageNetworkIpAddressVO)

Example 4 with StorageNetworkIpRangeVO

use of com.cloud.dc.StorageNetworkIpRangeVO 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.utils.exception.CloudRuntimeException) DB(com.cloud.utils.db.DB)

Example 5 with StorageNetworkIpRangeVO

use of com.cloud.dc.StorageNetworkIpRangeVO in project cloudstack by apache.

the class StorageNetworkManagerImpl method acquireIpAddress.

@Override
public StorageNetworkIpAddressVO acquireIpAddress(long podId) {
    List<StorageNetworkIpRangeVO> ranges = _sNwIpRangeDao.listByPodId(podId);
    for (StorageNetworkIpRangeVO r : ranges) {
        try {
            Long rangeId = r.getId();
            r = _sNwIpRangeDao.acquireInLockTable(rangeId);
            if (r == null) {
                String msg = "Unable to acquire lock on storage network ip range id=" + rangeId + ", delete failed";
                s_logger.warn(msg);
                throw new CloudRuntimeException(msg);
            }
            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.utils.exception.CloudRuntimeException) StorageNetworkIpAddressVO(com.cloud.dc.StorageNetworkIpAddressVO)

Aggregations

StorageNetworkIpRangeVO (com.cloud.dc.StorageNetworkIpRangeVO)8 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)8 DB (com.cloud.utils.db.DB)6 TransactionStatus (com.cloud.utils.db.TransactionStatus)6 TransactionCallbackNoReturn (com.cloud.utils.db.TransactionCallbackNoReturn)4 HostPodVO (com.cloud.dc.HostPodVO)2 StorageNetworkIpAddressVO (com.cloud.dc.StorageNetworkIpAddressVO)2 NetworkVO (com.cloud.network.dao.NetworkVO)2 SQLException (java.sql.SQLException)2