diff --git a/server/src/com/cloud/upgrade/dao/Upgrade2214to30.java b/server/src/com/cloud/upgrade/dao/Upgrade2214to30.java index af5da1d36e9..dfd285029f8 100644 --- a/server/src/com/cloud/upgrade/dao/Upgrade2214to30.java +++ b/server/src/com/cloud/upgrade/dao/Upgrade2214to30.java @@ -74,8 +74,10 @@ public class Upgrade2214to30 implements DbUpgrade { setupPhysicalNetworks(conn); // update domain network ref updateDomainNetworkRef(conn); - // update redundant routers + // update networks that use redundant routers to the new network offering updateReduntantRouters(conn); + // update networks that have to switch from Shared to Isolated network offerings + switchAccountSpecificNetworksToIsolated(conn); // create service/provider map for network offerings createNetworkOfferingServices(conn); // create service/provider map for networks @@ -724,12 +726,107 @@ public class Upgrade2214to30 implements DbUpgrade { s_logger.debug("Successfully updated network offering id=" + networkId + " with new network offering id " + newNetworkOfferingId); } - pstmt = conn.prepareStatement("DROP TABLE network_offerings2"); - pstmt.executeUpdate(); } catch (SQLException e) { - throw new CloudRuntimeException("Unable to redundant router networks", e); + throw new CloudRuntimeException("Unable to update redundant router networks", e); } finally { try { + pstmt = conn.prepareStatement("DROP TABLE network_offerings2"); + pstmt.executeUpdate(); + if (rs != null) { + rs.close(); + } + + if (rs1 != null) { + rs1.close(); + } + + if (pstmt != null) { + pstmt.close(); + } + } catch (SQLException e) { + } + } + } + + protected void switchAccountSpecificNetworksToIsolated(Connection conn) { + PreparedStatement pstmt = null; + ResultSet rs = null; + ResultSet rs1 = null; + try { + // get all networks that need to be updated to the redundant network offerings + pstmt = conn + .prepareStatement("select id, network_offering_id from networks where switch_to_isolated=1"); + rs = pstmt.executeQuery(); + pstmt = conn.prepareStatement("select count(*) from network_offerings"); + rs1 = pstmt.executeQuery(); + long ntwkOffCount = 0; + while (rs1.next()) { + ntwkOffCount = rs1.getLong(1); + } + + s_logger.debug("Have " + ntwkOffCount + " networkOfferings"); + pstmt = conn.prepareStatement("CREATE TEMPORARY TABLE network_offerings2 ENGINE=MEMORY SELECT * FROM network_offerings WHERE id=1"); + pstmt.executeUpdate(); + + HashMap newNetworkOfferingMap = new HashMap(); + + while (rs.next()) { + long networkId = rs.getLong(1); + long networkOfferingId = rs.getLong(2); + s_logger.debug("Updating network offering for the network id=" + networkId + " as it has switch_to_isolated=1"); + Long newNetworkOfferingId = null; + + if (!newNetworkOfferingMap.containsKey(networkOfferingId)) { + // clone the record to + pstmt = conn.prepareStatement("INSERT INTO network_offerings2 SELECT * FROM network_offerings WHERE id=?"); + pstmt.setLong(1, networkOfferingId); + pstmt.executeUpdate(); + + pstmt = conn.prepareStatement("UPDATE network_offerings2 SET id=?, guest_type='Isolated', unique_name=?, name=? WHERE id=?"); + ntwkOffCount = ntwkOffCount + 1; + newNetworkOfferingId = ntwkOffCount; + String uniqueName = "Isolated w/o source nat"; + pstmt.setLong(1, newNetworkOfferingId); + pstmt.setString(2, uniqueName); + pstmt.setString(3, uniqueName); + pstmt.setLong(4, networkOfferingId); + pstmt.executeUpdate(); + + pstmt = conn.prepareStatement("INSERT INTO network_offerings SELECT * from network_offerings2 WHERE id=" + newNetworkOfferingId); + pstmt.executeUpdate(); + + pstmt = conn.prepareStatement("UPDATE networks SET network_offering_id=? where id=?"); + pstmt.setLong(1, newNetworkOfferingId); + pstmt.setLong(2, networkId); + pstmt.executeUpdate(); + + newNetworkOfferingMap.put(networkOfferingId, ntwkOffCount); + } else { + pstmt = conn.prepareStatement("UPDATE networks SET network_offering_id=? where id=?"); + newNetworkOfferingId = newNetworkOfferingMap.get(networkOfferingId); + pstmt.setLong(1, newNetworkOfferingId); + pstmt.setLong(2, networkId); + pstmt.executeUpdate(); + } + + s_logger.debug("Successfully updated network offering id=" + networkId + " with new network offering id " + newNetworkOfferingId); + } + + try { + pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`networks` DROP COLUMN `switch_to_isolated`"); + pstmt.executeUpdate(); + } catch (Exception ex) { + // do nothing here + } + + } catch (SQLException e) { + throw new CloudRuntimeException("Unable to switch networks to isolated", e); + } finally { + try { + pstmt = conn.prepareStatement("DROP TABLE network_offerings2"); + pstmt.executeUpdate(); + pstmt = conn.prepareStatement("DROP TABLE network_offerings2"); + pstmt.executeUpdate(); if (rs != null) { rs.close(); }