From cae4124dc66a6d7b962f5d57dfe3afb861dacbc4 Mon Sep 17 00:00:00 2001 From: Jayapal Date: Wed, 20 Aug 2014 16:03:10 +0530 Subject: [PATCH] Fixed few coverity issues --- .../firewall/CreateEgressFirewallRuleCmd.java | 6 +- .../firewall/ListEgressFirewallRulesCmd.java | 12 +- .../api/command/user/vm/ListNicsCmd.java | 14 +- .../com/cloud/upgrade/dao/Upgrade40to41.java | 130 ++++++++++-------- 4 files changed, 96 insertions(+), 66 deletions(-) diff --git a/api/src/org/apache/cloudstack/api/command/user/firewall/CreateEgressFirewallRuleCmd.java b/api/src/org/apache/cloudstack/api/command/user/firewall/CreateEgressFirewallRuleCmd.java index 48ee0dfc9ab..59af6933528 100644 --- a/api/src/org/apache/cloudstack/api/command/user/firewall/CreateEgressFirewallRuleCmd.java +++ b/api/src/org/apache/cloudstack/api/command/user/firewall/CreateEgressFirewallRuleCmd.java @@ -260,8 +260,10 @@ public class CreateEgressFirewallRuleCmd extends BaseAsyncCreateCmd implements F try { FirewallRule result = _firewallService.createEgressFirewallRule(this); - setEntityId(result.getId()); - setEntityUuid(result.getUuid()); + if (result != null) { + setEntityId(result.getId()); + setEntityUuid(result.getUuid()); + } } catch (NetworkRuleConflictException ex) { s_logger.info("Network rule conflict: " + ex.getMessage()); s_logger.trace("Network Rule Conflict: ", ex); diff --git a/api/src/org/apache/cloudstack/api/command/user/firewall/ListEgressFirewallRulesCmd.java b/api/src/org/apache/cloudstack/api/command/user/firewall/ListEgressFirewallRulesCmd.java index e31496e4747..9e163259e80 100644 --- a/api/src/org/apache/cloudstack/api/command/user/firewall/ListEgressFirewallRulesCmd.java +++ b/api/src/org/apache/cloudstack/api/command/user/firewall/ListEgressFirewallRulesCmd.java @@ -85,12 +85,14 @@ public class ListEgressFirewallRulesCmd extends ListFirewallRulesCmd { ListResponse response = new ListResponse(); List fwResponses = new ArrayList(); - for (FirewallRule fwRule : result.first()) { - FirewallResponse ruleData = _responseGenerator.createFirewallResponse(fwRule); - ruleData.setObjectName("firewallrule"); - fwResponses.add(ruleData); + if (result != null) { + for (FirewallRule fwRule : result.first()) { + FirewallResponse ruleData = _responseGenerator.createFirewallResponse(fwRule); + ruleData.setObjectName("firewallrule"); + fwResponses.add(ruleData); + } + response.setResponses(fwResponses, result.second()); } - response.setResponses(fwResponses, result.second()); response.setResponseName(getCommandName()); setResponseObject(response); } diff --git a/api/src/org/apache/cloudstack/api/command/user/vm/ListNicsCmd.java b/api/src/org/apache/cloudstack/api/command/user/vm/ListNicsCmd.java index 49402dc70d2..ccec3e6ac13 100644 --- a/api/src/org/apache/cloudstack/api/command/user/vm/ListNicsCmd.java +++ b/api/src/org/apache/cloudstack/api/command/user/vm/ListNicsCmd.java @@ -124,11 +124,15 @@ public class ListNicsCmd extends BaseListCmd { try { List results = _networkService.listNics(this); ListResponse response = new ListResponse(); - List resList = new ArrayList(results.size()); - for (Nic r : results) { - NicResponse resp = _responseGenerator.createNicResponse(r); - resp.setObjectName("nic"); - resList.add(resp); + List resList = null; + if (results != null) { + resList = new ArrayList(results.size()); + for (Nic r : results) { + NicResponse resp = _responseGenerator.createNicResponse(r); + resp.setObjectName("nic"); + resList.add(resp); + } + response.setResponses(resList); } response.setResponses(resList); response.setResponseName(getCommandName()); diff --git a/engine/schema/src/com/cloud/upgrade/dao/Upgrade40to41.java b/engine/schema/src/com/cloud/upgrade/dao/Upgrade40to41.java index 77e2a6f3ace..29bd67e713b 100644 --- a/engine/schema/src/com/cloud/upgrade/dao/Upgrade40to41.java +++ b/engine/schema/src/com/cloud/upgrade/dao/Upgrade40to41.java @@ -103,72 +103,94 @@ public class Upgrade40to41 implements DbUpgrade { } 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); + // 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); //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(); + 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); - if (rsNw.next()) { - long accountId = rsNw.getLong(1); - long domainId = rsNw.getLong(2); + try (ResultSet NwAcctDomIdps = NwAcctDomIdpstmt.executeQuery();) { + s_logger.debug("Getting account_id, domain_id from networks table: " + NwAcctDomIdpstmt); - //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(); + 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); + } - pstmt = conn.prepareStatement("select id from firewall_rules where protocol='all' and network_id=?"); - pstmt.setLong(1, netId); - rsId = pstmt.executeQuery(); + try (PreparedStatement protoAllpstmt = conn.prepareStatement("select id from firewall_rules where protocol='all' and network_id=?");) + { + protoAllpstmt.setLong(1, netId); - 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(); + 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); - } finally { - try { - if (rs != null) { - rs.close(); - } - if (pstmt != null) { - pstmt.close(); - } - } catch (SQLException e) { - } + } } } +