Search in sources :

Example 96 with CloudRuntimeException

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

the class CapacityDaoImpl method orderHostsByFreeCapacity.

@Override
public List<Long> orderHostsByFreeCapacity(final Long clusterId, final short capacityTypeForOrdering) {
    final TransactionLegacy txn = TransactionLegacy.currentTxn();
    PreparedStatement pstmt = null;
    final List<Long> result = new ArrayList<>();
    final StringBuilder sql = new StringBuilder(ORDER_HOSTS_BY_FREE_CAPACITY_PART1);
    if (clusterId != null) {
        sql.append("AND cluster_id = ?");
    }
    sql.append(ORDER_HOSTS_BY_FREE_CAPACITY_PART2);
    try {
        pstmt = txn.prepareAutoCloseStatement(sql.toString());
        pstmt.setShort(1, capacityTypeForOrdering);
        if (clusterId != null) {
            pstmt.setLong(2, clusterId);
        }
        final ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            result.add(rs.getLong(1));
        }
        return result;
    } catch (final SQLException e) {
        throw new CloudRuntimeException("DB Exception on: " + sql, e);
    }
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) SQLException(java.sql.SQLException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 97 with CloudRuntimeException

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

the class DataCenterIpAddressDaoImpl method addIpRange.

@Override
@DB
public void addIpRange(final long dcId, final long podId, final String start, final String end) {
    final TransactionLegacy txn = TransactionLegacy.currentTxn();
    final String insertSql = "INSERT INTO `cloud`.`op_dc_ip_address_alloc` (ip_address, data_center_id, pod_id, mac_address) " + "VALUES (?, ?, ?, (select mac_address from `cloud`.`data_center` where id=?))";
    final String updateSql = "UPDATE `cloud`.`data_center` set mac_address = mac_address+1 where id=?";
    long startIP = NetUtils.ip2Long(start);
    final long endIP = NetUtils.ip2Long(end);
    try {
        txn.start();
        while (startIP <= endIP) {
            try (PreparedStatement insertPstmt = txn.prepareStatement(insertSql)) {
                insertPstmt.setString(1, NetUtils.long2Ip(startIP++));
                insertPstmt.setLong(2, dcId);
                insertPstmt.setLong(3, podId);
                insertPstmt.setLong(4, dcId);
                insertPstmt.executeUpdate();
            }
            try (PreparedStatement updatePstmt = txn.prepareStatement(updateSql)) {
                updatePstmt.setLong(1, dcId);
                updatePstmt.executeUpdate();
            }
        }
        txn.commit();
    } catch (final SQLException ex) {
        throw new CloudRuntimeException("Unable to persist ip address range ", ex);
    }
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) SQLException(java.sql.SQLException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) PreparedStatement(java.sql.PreparedStatement) DB(com.cloud.utils.db.DB)

Example 98 with CloudRuntimeException

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

the class DataCenterLinkLocalIpAddressDaoImpl method addIpRange.

@Override
@DB
public void addIpRange(final long dcId, final long podId, final String start, final String end) {
    final String insertSql = "INSERT INTO `cloud`.`op_dc_link_local_ip_address_alloc` (ip_address, data_center_id, pod_id) VALUES (?, ?, ?)";
    PreparedStatement stmt = null;
    long startIP = NetUtils.ip2Long(start);
    final long endIP = NetUtils.ip2Long(end);
    final TransactionLegacy txn = TransactionLegacy.currentTxn();
    try {
        txn.start();
        stmt = txn.prepareAutoCloseStatement(insertSql);
        while (startIP <= endIP) {
            stmt.setString(1, NetUtils.long2Ip(startIP++));
            stmt.setLong(2, dcId);
            stmt.setLong(3, podId);
            stmt.addBatch();
        }
        stmt.executeBatch();
        txn.commit();
    } catch (final SQLException e) {
        throw new CloudRuntimeException("Unable to insert", e);
    }
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) SQLException(java.sql.SQLException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) PreparedStatement(java.sql.PreparedStatement) DB(com.cloud.utils.db.DB)

Example 99 with CloudRuntimeException

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

the class ClusterDaoImpl method getPodClusterIdMap.

@Override
public Map<Long, List<Long>> getPodClusterIdMap(final List<Long> clusterIds) {
    final TransactionLegacy txn = TransactionLegacy.currentTxn();
    PreparedStatement pstmt = null;
    final Map<Long, List<Long>> result = new HashMap<>();
    try {
        final StringBuilder sql = new StringBuilder(GET_POD_CLUSTER_MAP_PREFIX);
        if (clusterIds.size() > 0) {
            for (final Long clusterId : clusterIds) {
                sql.append(clusterId).append(",");
            }
            sql.delete(sql.length() - 1, sql.length());
            sql.append(GET_POD_CLUSTER_MAP_SUFFIX);
        }
        pstmt = txn.prepareAutoCloseStatement(sql.toString());
        final ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            final Long podId = rs.getLong(1);
            final Long clusterIdInPod = rs.getLong(2);
            if (result.containsKey(podId)) {
                final List<Long> clusterList = result.get(podId);
                clusterList.add(clusterIdInPod);
                result.put(podId, clusterList);
            } else {
                final List<Long> clusterList = new ArrayList<>();
                clusterList.add(clusterIdInPod);
                result.put(podId, clusterList);
            }
        }
        return result;
    } catch (final SQLException e) {
        throw new CloudRuntimeException("DB Exception on: " + GET_POD_CLUSTER_MAP_PREFIX, e);
    }
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) ArrayList(java.util.ArrayList) List(java.util.List)

Example 100 with CloudRuntimeException

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

the class HostDetailsDaoImpl method persist.

@Override
public void persist(final long hostId, final Map<String, String> details) {
    final String InsertOrUpdateSql = "INSERT INTO `cloud`.`host_details` (host_id, name, value) VALUES (?,?,?) ON DUPLICATE KEY UPDATE value=?";
    final TransactionLegacy txn = TransactionLegacy.currentTxn();
    txn.start();
    for (final Map.Entry<String, String> detail : details.entrySet()) {
        String value = detail.getValue();
        if ("password".equals(detail.getKey())) {
            value = DBEncryptionUtil.encrypt(value);
        }
        try {
            final PreparedStatement pstmt = txn.prepareAutoCloseStatement(InsertOrUpdateSql);
            pstmt.setLong(1, hostId);
            pstmt.setString(2, detail.getKey());
            pstmt.setString(3, value);
            pstmt.setString(4, value);
            pstmt.executeUpdate();
        } catch (final SQLException e) {
            throw new CloudRuntimeException("Unable to persist the host_details key: " + detail.getKey() + " for host id: " + hostId, e);
        }
    }
    txn.commit();
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) SQLException(java.sql.SQLException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) PreparedStatement(java.sql.PreparedStatement) HashMap(java.util.HashMap) Map(java.util.Map)

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