Search in sources :

Example 66 with CloudRuntimeException

use of com.cloud.utils.exception.CloudRuntimeException in project cloudstack by apache.

the class Upgrade30xBase method addDefaultVRProvider.

protected void addDefaultVRProvider(Connection conn, long physicalNetworkId, long zoneId) {
    PreparedStatement pstmtUpdate = null, pstmt2 = null;
    try {
        // add physical network service provider - VirtualRouter
        s_logger.debug("Adding PhysicalNetworkServiceProvider VirtualRouter");
        String insertPNSP = "INSERT INTO `cloud`.`physical_network_service_providers` (`uuid`, `physical_network_id` , `provider_name`, `state` ," + "`destination_physical_network_id`, `vpn_service_provided`, `dhcp_service_provided`, `dns_service_provided`, `gateway_service_provided`," + "`firewall_service_provided`, `source_nat_service_provided`, `load_balance_service_provided`, `static_nat_service_provided`," + "`port_forwarding_service_provided`, `user_data_service_provided`, `security_group_service_provided`) VALUES (?,?,?,?,0,1,1,1,1,1,1,1,1,1,1,0)";
        String routerUUID = UUID.randomUUID().toString();
        pstmtUpdate = conn.prepareStatement(insertPNSP);
        pstmtUpdate.setString(1, routerUUID);
        pstmtUpdate.setLong(2, physicalNetworkId);
        pstmtUpdate.setString(3, "VirtualRouter");
        pstmtUpdate.setString(4, "Enabled");
        pstmtUpdate.executeUpdate();
        pstmtUpdate.close();
        // add virtual_router_element
        String fetchNSPid = "SELECT id from `cloud`.`physical_network_service_providers` where physical_network_id=" + physicalNetworkId + " AND provider_name = 'VirtualRouter' AND uuid = ?";
        pstmt2 = conn.prepareStatement(fetchNSPid);
        pstmt2.setString(1, routerUUID);
        ResultSet rsNSPid = pstmt2.executeQuery();
        rsNSPid.next();
        long nspId = rsNSPid.getLong(1);
        pstmt2.close();
        String insertRouter = "INSERT INTO `cloud`.`virtual_router_providers` (`nsp_id`, `uuid` , `type` , `enabled`) " + "VALUES (?,?,?,?)";
        pstmtUpdate = conn.prepareStatement(insertRouter);
        pstmtUpdate.setLong(1, nspId);
        pstmtUpdate.setString(2, UUID.randomUUID().toString());
        pstmtUpdate.setString(3, "VirtualRouter");
        pstmtUpdate.setInt(4, 1);
        pstmtUpdate.executeUpdate();
        pstmtUpdate.close();
    } catch (SQLException e) {
        throw new CloudRuntimeException("Exception while adding PhysicalNetworks", e);
    } finally {
        closeAutoCloseable(pstmt2);
        closeAutoCloseable(pstmtUpdate);
    }
}
Also used : SQLException(java.sql.SQLException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 67 with CloudRuntimeException

use of com.cloud.utils.exception.CloudRuntimeException in project cloudstack by apache.

the class Upgrade40to41 method updateRegionEntries.

private void updateRegionEntries(Connection conn) {
    final Properties dbProps = DbProperties.getDbProperties();
    int region_id = 1;
    String regionId = dbProps.getProperty("region.id");
    if (regionId != null) {
        region_id = Integer.parseInt(regionId);
    }
    try (PreparedStatement pstmt = conn.prepareStatement("update `cloud`.`region` set id = ?")) {
        //Update regionId in region table
        s_logger.debug("Updating region table with Id: " + region_id);
        pstmt.setInt(1, region_id);
        pstmt.executeUpdate();
    } catch (SQLException e) {
        throw new CloudRuntimeException("Error while updating region entries", e);
    }
}
Also used : SQLException(java.sql.SQLException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties) DbProperties(com.cloud.utils.db.DbProperties)

Example 68 with CloudRuntimeException

use of com.cloud.utils.exception.CloudRuntimeException in project cloudstack by apache.

the class Upgrade40to41 method upgradeEgressFirewallRules.

private void upgradeEgressFirewallRules(Connection conn) {
    // update the existing ingress rules traffic type
    try (PreparedStatement updateNwpstmt = conn.prepareStatement("update `cloud`.`firewall_rules`  set traffic_type='Ingress' where purpose='Firewall' and ip_address_id is " + "not null and traffic_type is null")) {
        updateNwpstmt.executeUpdate();
        s_logger.debug("Updating firewall Ingress rule traffic type: " + updateNwpstmt);
    } catch (SQLException e) {
        throw new CloudRuntimeException("Unable to update ingress firewall rules ", e);
    }
    try (PreparedStatement vrNwpstmt = conn.prepareStatement("select network_id FROM `cloud`.`ntwk_service_map` where service='Firewall' and provider='VirtualRouter' ");
        ResultSet vrNwsRs = vrNwpstmt.executeQuery()) {
        while (vrNwsRs.next()) {
            long netId = vrNwsRs.getLong(1);
            //after this. So checking for Isolated OR Virtual
            try (PreparedStatement NwAcctDomIdpstmt = conn.prepareStatement("select account_id, domain_id FROM `cloud`.`networks` where (guest_type='Isolated' OR " + "guest_type='Virtual') and traffic_type='Guest' and vpc_id is NULL and " + "(state='implemented' OR state='Shutdown') and id=? ")) {
                NwAcctDomIdpstmt.setLong(1, netId);
                try (ResultSet NwAcctDomIdps = NwAcctDomIdpstmt.executeQuery()) {
                    s_logger.debug("Getting account_id, domain_id from networks table: " + NwAcctDomIdpstmt);
                    if (NwAcctDomIdps.next()) {
                        long accountId = NwAcctDomIdps.getLong(1);
                        long domainId = NwAcctDomIdps.getLong(2);
                        //Add new rule for the existing networks
                        s_logger.debug("Adding default egress firewall rule for network " + netId);
                        try (PreparedStatement fwRulespstmt = conn.prepareStatement("INSERT INTO firewall_rules " + " (uuid, state, protocol, purpose, account_id, domain_id, network_id, xid, created," + " traffic_type) VALUES (?, 'Active', 'all', 'Firewall', ?, ?, ?, ?, now(), " + "'Egress')")) {
                            fwRulespstmt.setString(1, UUID.randomUUID().toString());
                            fwRulespstmt.setLong(2, accountId);
                            fwRulespstmt.setLong(3, domainId);
                            fwRulespstmt.setLong(4, netId);
                            fwRulespstmt.setString(5, UUID.randomUUID().toString());
                            s_logger.debug("Inserting default egress firewall rule " + fwRulespstmt);
                            fwRulespstmt.executeUpdate();
                        } catch (SQLException e) {
                            throw new CloudRuntimeException("failed to insert default egress firewall rule ", e);
                        }
                        try (PreparedStatement protoAllpstmt = conn.prepareStatement("select id from firewall_rules where protocol='all' and network_id=?")) {
                            protoAllpstmt.setLong(1, netId);
                            try (ResultSet protoAllRs = protoAllpstmt.executeQuery()) {
                                long firewallRuleId;
                                if (protoAllRs.next()) {
                                    firewallRuleId = protoAllRs.getLong(1);
                                    try (PreparedStatement fwCidrsPstmt = conn.prepareStatement("insert into firewall_rules_cidrs (firewall_rule_id,source_cidr) values (?, '0.0.0.0/0')")) {
                                        fwCidrsPstmt.setLong(1, firewallRuleId);
                                        s_logger.debug("Inserting rule for cidr 0.0.0.0/0 for the new Firewall rule id=" + firewallRuleId + " with statement " + fwCidrsPstmt);
                                        fwCidrsPstmt.executeUpdate();
                                    } catch (SQLException e) {
                                        throw new CloudRuntimeException("Unable to set egress firewall rules ", e);
                                    }
                                }
                            } catch (SQLException e) {
                                throw new CloudRuntimeException("Unable to set egress firewall rules ", e);
                            }
                        } catch (SQLException e) {
                            throw new CloudRuntimeException("Unable to set egress firewall rules ", e);
                        }
                    }
                //if
                } catch (SQLException e) {
                    throw new CloudRuntimeException("Unable execute update query ", e);
                }
            } catch (SQLException e) {
                throw new CloudRuntimeException("Unable to get account id domainid of networks ", e);
            }
        }
    //while
    } catch (SQLException e) {
        throw new CloudRuntimeException("Unable to set egress firewall rules ", e);
    }
}
Also used : SQLException(java.sql.SQLException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 69 with CloudRuntimeException

use of com.cloud.utils.exception.CloudRuntimeException in project cloudstack by apache.

the class Upgrade305to306 method upgradeEIPNetworkOfferings.

private void upgradeEIPNetworkOfferings(Connection conn) {
    try (PreparedStatement pstmt = conn.prepareStatement("select id, elastic_ip_service from `cloud`.`network_offerings` where traffic_type='Guest'");
        PreparedStatement pstmt1 = conn.prepareStatement("UPDATE `cloud`.`network_offerings` set eip_associate_public_ip=? where id=?");
        ResultSet rs = pstmt.executeQuery()) {
        while (rs.next()) {
            long id = rs.getLong(1);
            // check if elastic IP service is enabled for network offering
            if (rs.getLong(2) != 0) {
                //update network offering with eip_associate_public_ip set to true
                pstmt1.setBoolean(1, true);
                pstmt1.setLong(2, id);
                pstmt1.executeUpdate();
            }
        }
    } catch (SQLException e) {
        throw new CloudRuntimeException("Unable to set eip_associate_public_ip for network offerings with EIP service enabled.", e);
    }
}
Also used : SQLException(java.sql.SQLException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 70 with CloudRuntimeException

use of com.cloud.utils.exception.CloudRuntimeException in project cloudstack by apache.

the class Upgrade305to306 method upgradeEgressFirewallRules.

private void upgradeEgressFirewallRules(Connection conn) {
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    ResultSet rsId = null;
    ResultSet rsNw = null;
    try {
        // update the existing ingress rules traffic type
        pstmt = conn.prepareStatement("update `cloud`.`firewall_rules`" + "  set traffic_type='Ingress' where purpose='Firewall' and ip_address_id is not null and traffic_type is null");
        s_logger.debug("Updating firewall Ingress rule traffic type: " + pstmt);
        pstmt.executeUpdate();
        pstmt = conn.prepareStatement("select network_id FROM `cloud`.`ntwk_service_map` where service='Firewall' and provider='VirtualRouter' ");
        rs = pstmt.executeQuery();
        while (rs.next()) {
            long netId = rs.getLong(1);
            //When upgraded from 2.2.14 to 3.0.6 guest_type is updated to Isolated in the 2214to30 clean up sql. clean up executes
            //after this. So checking for Isolated OR Virtual
            pstmt = conn.prepareStatement("select account_id, domain_id FROM `cloud`.`networks` where (guest_type='Isolated' OR guest_type='" + "Virtual') and traffic_type='Guest' and vpc_id is NULL and (state='implemented' OR state='Shutdown') and id=? ");
            pstmt.setLong(1, netId);
            s_logger.debug("Getting account_id, domain_id from networks table: " + pstmt);
            rsNw = pstmt.executeQuery();
            if (rsNw.next()) {
                long accountId = rsNw.getLong(1);
                long domainId = rsNw.getLong(2);
                //Add new rule for the existing networks
                s_logger.debug("Adding default egress firewall rule for network " + netId);
                pstmt = conn.prepareStatement("INSERT INTO firewall_rules (uuid, state, protocol, purpose, account_id, domain_id, network_id, xid, created,  traffic_type) VALUES (?, 'Active', 'all', 'Firewall', ?, ?, ?, ?, now(), 'Egress')");
                pstmt.setString(1, UUID.randomUUID().toString());
                pstmt.setLong(2, accountId);
                pstmt.setLong(3, domainId);
                pstmt.setLong(4, netId);
                pstmt.setString(5, UUID.randomUUID().toString());
                s_logger.debug("Inserting default egress firewall rule " + pstmt);
                pstmt.executeUpdate();
                pstmt = conn.prepareStatement("select id from firewall_rules where protocol='all' and network_id=?");
                pstmt.setLong(1, netId);
                rsId = pstmt.executeQuery();
                long firewallRuleId;
                if (rsId.next()) {
                    firewallRuleId = rsId.getLong(1);
                    pstmt = conn.prepareStatement("insert into firewall_rules_cidrs (firewall_rule_id,source_cidr) values (?, '0.0.0.0/0')");
                    pstmt.setLong(1, firewallRuleId);
                    s_logger.debug("Inserting rule for cidr 0.0.0.0/0 for the new Firewall rule id=" + firewallRuleId + " with statement " + pstmt);
                    pstmt.executeUpdate();
                }
            }
        }
    } catch (SQLException e) {
        throw new CloudRuntimeException("Unable to set egress firewall rules ", e);
    } finally {
        closeAutoCloseable(rs);
        closeAutoCloseable(pstmt);
    }
}
Also used : SQLException(java.sql.SQLException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1279 PreparedStatement (java.sql.PreparedStatement)320 SQLException (java.sql.SQLException)320 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)236 ResultSet (java.sql.ResultSet)217 ArrayList (java.util.ArrayList)217 ConfigurationException (javax.naming.ConfigurationException)171 HashMap (java.util.HashMap)129 DB (com.cloud.utils.db.DB)118 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)115 IOException (java.io.IOException)95 HostVO (com.cloud.host.HostVO)94 Answer (com.cloud.agent.api.Answer)84 Account (com.cloud.user.Account)84 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)82 URISyntaxException (java.net.URISyntaxException)82 ActionEvent (com.cloud.event.ActionEvent)70 TransactionStatus (com.cloud.utils.db.TransactionStatus)65 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)63 InternalErrorException (com.cloud.exception.InternalErrorException)57