diff --git a/server/src/com/cloud/network/NetworkManagerImpl.java b/server/src/com/cloud/network/NetworkManagerImpl.java index 01feba21471..873c98208ff 100755 --- a/server/src/com/cloud/network/NetworkManagerImpl.java +++ b/server/src/com/cloud/network/NetworkManagerImpl.java @@ -5213,7 +5213,8 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag try { txn.start(); // Create the new physical network in the database - PhysicalNetworkVO pNetwork = new PhysicalNetworkVO(zoneId, vnetRange, networkSpeed, domainId, broadcastDomainRange, name); + long id = _physicalNetworkDao.getNextInSequence(Long.class, "id"); + PhysicalNetworkVO pNetwork = new PhysicalNetworkVO(id, zoneId, vnetRange, networkSpeed, domainId, broadcastDomainRange, name); pNetwork.setTags(tags); pNetwork.setIsolationMethods(isolationMethods); @@ -6590,7 +6591,10 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag physicalNetworkId = getNonGuestNetworkPhysicalNetworkId(network); } else { NetworkOffering offering = _configMgr.getNetworkOffering(network.getNetworkOfferingId()); - physicalNetworkId = findPhysicalNetworkId(network.getDataCenterId(), offering.getTags(), offering.getTrafficType()); + physicalNetworkId = network.getPhysicalNetworkId(); + if(physicalNetworkId == null){ + physicalNetworkId = findPhysicalNetworkId(network.getDataCenterId(), offering.getTags(), offering.getTrafficType()); + } } if (physicalNetworkId == null) { diff --git a/server/src/com/cloud/network/PhysicalNetworkVO.java b/server/src/com/cloud/network/PhysicalNetworkVO.java index 1d753d715e4..84371fb0be5 100644 --- a/server/src/com/cloud/network/PhysicalNetworkVO.java +++ b/server/src/com/cloud/network/PhysicalNetworkVO.java @@ -94,7 +94,7 @@ public class PhysicalNetworkVO implements PhysicalNetwork { } - public PhysicalNetworkVO(long dataCenterId, String vnet, String speed, Long domainId, BroadcastDomainRange broadcastDomainRange, String name) { + public PhysicalNetworkVO(long id, long dataCenterId, String vnet, String speed, Long domainId, BroadcastDomainRange broadcastDomainRange, String name) { this.dataCenterId = dataCenterId; this.setVnet(vnet); this.setSpeed(speed); @@ -107,6 +107,7 @@ public class PhysicalNetworkVO implements PhysicalNetwork { this.state = State.Disabled; this.uuid = UUID.randomUUID().toString(); this.name = name; + this.id = id; } @Override diff --git a/server/src/com/cloud/upgrade/dao/Upgrade2214to30.java b/server/src/com/cloud/upgrade/dao/Upgrade2214to30.java index 43e2c000f26..8c4dbecd905 100755 --- a/server/src/com/cloud/upgrade/dao/Upgrade2214to30.java +++ b/server/src/com/cloud/upgrade/dao/Upgrade2214to30.java @@ -37,7 +37,7 @@ import com.cloud.utils.crypt.EncryptionSecretKeyChecker; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.script.Script; -public class Upgrade2214to30 implements DbUpgrade { +public class Upgrade2214to30 extends Upgrade30xBase implements DbUpgrade { final static Logger s_logger = Logger.getLogger(Upgrade2214to30.class); @Override @@ -110,211 +110,6 @@ public class Upgrade2214to30 implements DbUpgrade { return new File[] { new File(script) }; } - private long addPhysicalNetworkToZone(Connection conn, long zoneId, String zoneName, String networkType, String vnet, Long domainId){ - - String getNextNetworkSequenceSql = "SELECT value from `cloud`.`sequence` where name='physical_networks_seq'"; - String advanceNetworkSequenceSql = "UPDATE `cloud`.`sequence` set value=value+1 where name='physical_networks_seq'"; - PreparedStatement pstmtUpdate = null, pstmt2 = null; - // add p.network - try{ - pstmt2 = conn.prepareStatement(getNextNetworkSequenceSql); - - ResultSet rsSeq = pstmt2.executeQuery(); - rsSeq.next(); - - long physicalNetworkId = rsSeq.getLong(1); - rsSeq.close(); - pstmt2.close(); - pstmt2 = conn.prepareStatement(advanceNetworkSequenceSql); - pstmt2.executeUpdate(); - pstmt2.close(); - - String uuid = UUID.randomUUID().toString(); - String broadcastDomainRange = "POD"; - if ("Advanced".equals(networkType)) { - broadcastDomainRange = "ZONE"; - } - - s_logger.debug("Adding PhysicalNetwork " + physicalNetworkId + " for Zone id " + zoneId); - String sql = "INSERT INTO `cloud`.`physical_network` (id, uuid, data_center_id, vnet, broadcast_domain_range, state, name) VALUES (?,?,?,?,?,?,?)"; - - pstmtUpdate = conn.prepareStatement(sql); - pstmtUpdate.setLong(1, physicalNetworkId); - pstmtUpdate.setString(2, uuid); - pstmtUpdate.setLong(3, zoneId); - pstmtUpdate.setString(4, vnet); - pstmtUpdate.setString(5, broadcastDomainRange); - pstmtUpdate.setString(6, "Enabled"); - zoneName = zoneName + "-pNtwk"; - pstmtUpdate.setString(7, zoneName); - s_logger.warn("Statement is " + pstmtUpdate.toString()); - pstmtUpdate.executeUpdate(); - pstmtUpdate.close(); - - if (domainId != null && domainId.longValue() != 0) { - s_logger.debug("Updating domain_id for physical network id=" + physicalNetworkId); - sql = "UPDATE `cloud`.`physical_network` set domain_id=? where id=?"; - pstmtUpdate = conn.prepareStatement(sql); - pstmtUpdate.setLong(1, domainId); - pstmtUpdate.setLong(2, physicalNetworkId); - pstmtUpdate.executeUpdate(); - pstmtUpdate.close(); - } - - return physicalNetworkId; - } catch (SQLException e) { - throw new CloudRuntimeException("Exception while adding PhysicalNetworks", e); - } finally { - if (pstmtUpdate != null) { - try { - pstmtUpdate.close(); - } catch (SQLException e) { - } - } - if (pstmt2 != null) { - try { - pstmt2.close(); - } catch (SQLException e) { - } - } - - } - } - - private void addTrafficType(Connection conn, long physicalNetworkId, String trafficType, String xenPublicLabel, String kvmPublicLabel, String vmwarePublicLabel){ - // add traffic types - PreparedStatement pstmtUpdate = null; - try{ - s_logger.debug("Adding PhysicalNetwork traffic types"); - String insertTraficType = "INSERT INTO `cloud`.`physical_network_traffic_types` (physical_network_id, traffic_type, xen_network_label, kvm_network_label, vmware_network_label, uuid) VALUES ( ?, ?, ?, ?, ?, ?)"; - pstmtUpdate = conn.prepareStatement(insertTraficType); - pstmtUpdate.setLong(1, physicalNetworkId); - pstmtUpdate.setString(2, trafficType); - pstmtUpdate.setString(3, xenPublicLabel); - pstmtUpdate.setString(4, kvmPublicLabel); - pstmtUpdate.setString(5, vmwarePublicLabel); - pstmtUpdate.setString(6, UUID.randomUUID().toString()); - pstmtUpdate.executeUpdate(); - pstmtUpdate.close(); - }catch (SQLException e) { - throw new CloudRuntimeException("Exception while adding PhysicalNetworks", e); - } finally { - if (pstmtUpdate != null) { - try { - pstmtUpdate.close(); - } catch (SQLException e) { - } - } - } - } - - private void addDefaultServiceProviders(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)"; - - pstmtUpdate = conn.prepareStatement(insertPNSP); - pstmtUpdate.setString(1, UUID.randomUUID().toString()); - pstmtUpdate.setLong(2, physicalNetworkId); - pstmtUpdate.setString(3, "VirtualRouter"); - pstmtUpdate.setString(4, "Enabled"); - pstmtUpdate.executeUpdate(); - pstmtUpdate.close(); - - //add security group service provider (if security group service is enabled for at least one guest network) - String selectSG = "SELECT * from `cloud`.`networks` where is_security_group_enabled=1 and data_center_id=?"; - pstmt2 = conn.prepareStatement(selectSG); - pstmt2.setLong(1, zoneId); - ResultSet sgDcSet = pstmt2.executeQuery(); - while (sgDcSet.next()) { - s_logger.debug("Adding PhysicalNetworkServiceProvider SecurityGroupProvider to the physical network id=" + physicalNetworkId); - 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,0,0,0,0,0,0,0,0,0,0,1)"; - pstmtUpdate = conn.prepareStatement(insertPNSP); - pstmtUpdate.setString(1, UUID.randomUUID().toString()); - pstmtUpdate.setLong(2, physicalNetworkId); - pstmtUpdate.setString(3, "SecurityGroupProvider"); - pstmtUpdate.setString(4, "Enabled"); - pstmtUpdate.executeUpdate(); - pstmtUpdate.close(); - } - pstmt2.close(); - - // add virtual_router_element - String fetchNSPid = "SELECT id from `cloud`.`physical_network_service_providers` where physical_network_id=" + physicalNetworkId; - pstmt2 = conn.prepareStatement(fetchNSPid); - 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 { - if (pstmtUpdate != null) { - try { - pstmtUpdate.close(); - } catch (SQLException e) { - } - } - if (pstmt2 != null) { - try { - pstmt2.close(); - } catch (SQLException e) { - } - } - } - } - - private void addPhysicalNtwk_To_Ntwk_IP_Vlan(Connection conn, long physicalNetworkId, long networkId){ - PreparedStatement pstmtUpdate = null; - try{ - // add physicalNetworkId to vlan for this zone - String updateVLAN = "UPDATE `cloud`.`vlan` SET physical_network_id = " + physicalNetworkId + " WHERE network_id = " + networkId; - pstmtUpdate = conn.prepareStatement(updateVLAN); - pstmtUpdate.executeUpdate(); - pstmtUpdate.close(); - - // add physicalNetworkId to user_ip_address for this zone - String updateUsrIp = "UPDATE `cloud`.`user_ip_address` SET physical_network_id = " + physicalNetworkId + " WHERE source_network_id = " + networkId; - pstmtUpdate = conn.prepareStatement(updateUsrIp); - pstmtUpdate.executeUpdate(); - pstmtUpdate.close(); - - // add physicalNetworkId to guest networks for this zone - String updateNet = "UPDATE `cloud`.`networks` SET physical_network_id = " + physicalNetworkId + " WHERE id = " + networkId + " AND traffic_type = 'Guest'"; - pstmtUpdate = conn.prepareStatement(updateNet); - pstmtUpdate.executeUpdate(); - pstmtUpdate.close(); - }catch (SQLException e) { - throw new CloudRuntimeException("Exception while adding PhysicalNetworks", e); - } finally { - if (pstmtUpdate != null) { - try { - pstmtUpdate.close(); - } catch (SQLException e) { - } - } - } - - } - private void setupPhysicalNetworks(Connection conn) { /** * for each zone: @@ -348,7 +143,7 @@ public class Upgrade2214to30 implements DbUpgrade { String vmwarePrivateLabel = getNetworkLabelFromConfig(conn, "vmware.private.vswitch"); String vmwareGuestLabel = getNetworkLabelFromConfig(conn, "vmware.guest.vswitch"); - pstmt = conn.prepareStatement("SELECT id, domain_id, networktype, vnet, name FROM `cloud`.`data_center`"); + pstmt = conn.prepareStatement("SELECT id, domain_id, networktype, vnet, name, removed FROM `cloud`.`data_center`"); rs = pstmt.executeQuery(); while (rs.next()) { long zoneId = rs.getLong(1); @@ -356,15 +151,85 @@ public class Upgrade2214to30 implements DbUpgrade { String networkType = rs.getString(3); String vnet = rs.getString(4); String zoneName = rs.getString(5); + String removed = rs.getString(6); + + //set uuid for the zone + String uuid = UUID.randomUUID().toString(); + String updateUuid = "UPDATE `cloud`.`data_center` SET uuid = ? WHERE id = ?"; + pstmtUpdate = conn.prepareStatement(updateUuid); + pstmtUpdate.setString(1, uuid); + pstmtUpdate.setLong(2, zoneId); + pstmtUpdate.executeUpdate(); + pstmtUpdate.close(); //check if there are multiple guest networks configured using network_tags - PreparedStatement pstmt2 = conn.prepareStatement("SELECT distinct tag FROM `cloud`.`network_tags` t JOIN `cloud`.`networks` n where t.network_id = n.id and n.data_center_id = "+zoneId); + PreparedStatement pstmt2 = conn.prepareStatement("SELECT distinct tag FROM `cloud`.`network_tags` t JOIN `cloud`.`networks` n ON t.network_id = n.id WHERE n.data_center_id = ? and n.removed IS NULL"); + pstmt2.setLong(1, zoneId); ResultSet rsTags = pstmt2.executeQuery(); if(rsTags.next()){ + s_logger.debug("Network tags are not empty, might have to create more than one physical network..."); + //make sure setup does not use guest vnets + + if(vnet != null){ + //check if any vnet is allocated and guest networks are using vnets. + PreparedStatement pstmt4 = conn.prepareStatement("SELECT v.* FROM `cloud`.`op_dc_vnet_alloc` v JOIN `cloud`.`networks` n ON CONCAT('vlan://' , v.vnet) = n.broadcast_uri WHERE v.taken IS NOT NULL AND v.data_center_id = ? AND n.removed IS NULL"); + pstmt4.setLong(1, zoneId); + ResultSet rsVNet = pstmt4.executeQuery(); + + if(rsVNet.next()){ + String message = "Cannot upgrade. Your setup has multiple Physical Networks and is using guest Vnet that is assigned wrongly. To upgrade, first correct the setup by doing the following: \n" + + "1. Please rollback to your 2.2.14 setup\n" + + "2. Please stop all VMs using isolated(virtual) networks through CloudStack\n" + + "3. Run following query to find if any networks still have nics allocated:\n\t"+ + "a) check if any virtual guest networks still have allocated nics by running:\n\t" + + "SELECT DISTINCT op.id from `cloud`.`op_networks` op JOIN `cloud`.`networks` n on op.id=n.id WHERE nics_count != 0 AND guest_type = 'Virtual';\n\t"+ + "b) If this returns any networkd ids, then ensure that all VMs are stopped, no new VM is being started, and then shutdown management server\n\t"+ + "c) Clean up the nics count for the 'virtual' network id's returned in step (a) by running this:\n\t"+ + "UPDATE `cloud`.`op_networks` SET nics_count = 0 WHERE id = \n\t"+ + "d) Restart management server and wait for all networks to shutdown. [Networks shutdown will be determined by network.gc.interval and network.gc.wait seconds] \n"+ + "4. Please ensure all networks are shutdown and all guest Vnet's are free.\n" + + "5. Run upgrade. This will allocate all your guest vnet range to first physical network. \n" + + "6. Reconfigure the vnet ranges for each physical network as desired by using updatePhysicalNetwork API \n" + + "7. Start all your VMs"; + + s_logger.error(message); + + throw new CloudRuntimeException("Cannot upgrade this setup since it uses guest vnet and will have multiple physical networks. Please check the logs for details on how to proceed"); + } + rsVNet.close(); + pstmt4.close(); + + //Clean up any vnets that have no live networks/nics + pstmt4 = conn.prepareStatement("SELECT v.id, v.vnet, v.reservation_id FROM `cloud`.`op_dc_vnet_alloc` v LEFT JOIN networks n ON CONCAT('vlan://' , v.vnet) = n.broadcast_uri WHERE v.taken IS NOT NULL AND v.data_center_id = ? AND n.broadcast_uri IS NULL AND n.removed IS NULL"); + pstmt4.setLong(1, zoneId); + rsVNet = pstmt4.executeQuery(); + while(rsVNet.next()){ + Long vnet_id = rsVNet.getLong(1); + String vnetValue = rsVNet.getString(2); + String reservationId = rsVNet.getString(3); + //does this vnet have any nic associated? + PreparedStatement pstmt5 = conn.prepareStatement("SELECT id, instance_id FROM `cloud`.`nics` where broadcast_uri = ? and removed IS NULL"); + String uri = "vlan://"+vnetValue; + pstmt5.setString(1, uri); + ResultSet rsNic = pstmt5.executeQuery(); + Long nic_id = rsNic.getLong(1); + Long instance_id = rsNic.getLong(2); + if(rsNic.next()){ + throw new CloudRuntimeException("Cannot upgrade. Please cleanup the guest vnet: "+ vnetValue +" , it is being used by nic_id: "+ nic_id +" , instance_id: " + instance_id ); + } + + //free this vnet + String freeVnet = "UPDATE `cloud`.`op_dc_vnet_alloc` SET account_id = NULL, taken = NULL, reservation_id = NULL WHERE id = ?"; + pstmtUpdate = conn.prepareStatement(freeVnet); + pstmtUpdate.setLong(1, vnet_id); + pstmtUpdate.executeUpdate(); + pstmtUpdate.close(); + } + } + boolean isFirstPhysicalNtwk = true; do{ - s_logger.debug("Network tags are not empty, might have to create more than one physical network..."); //create one physical network per tag String guestNetworkTag = rsTags.getString(1); long physicalNetworkId = addPhysicalNetworkToZone(conn, zoneId, zoneName, networkType, (isFirstPhysicalNtwk) ? vnet : null, domainId); @@ -375,7 +240,8 @@ public class Upgrade2214to30 implements DbUpgrade { addTrafficType(conn, physicalNetworkId, "Storage", xenStorageLabel, null, null); } addTrafficType(conn, physicalNetworkId, "Guest", guestNetworkTag, kvmGuestLabel, vmwareGuestLabel); - addDefaultServiceProviders(conn, physicalNetworkId, zoneId); + addDefaultVRProvider(conn, physicalNetworkId, zoneId); + addDefaultSGProvider(conn, physicalNetworkId, zoneId, networkType, false); //for all networks with this tag, add physical_network_id PreparedStatement pstmt3 = conn.prepareStatement("SELECT network_id FROM `cloud`.`network_tags` where tag = '" + guestNetworkTag + "'"); @@ -408,7 +274,9 @@ public class Upgrade2214to30 implements DbUpgrade { addTrafficType(conn, physicalNetworkId, "Management", xenPrivateLabel, kvmPrivateLabel, vmwarePrivateLabel); addTrafficType(conn, physicalNetworkId, "Storage", xenStorageLabel, null, null); addTrafficType(conn, physicalNetworkId, "Guest", xenGuestLabel, kvmGuestLabel, vmwareGuestLabel); - addDefaultServiceProviders(conn, physicalNetworkId, zoneId); + addDefaultVRProvider(conn, physicalNetworkId, zoneId); + addDefaultSGProvider(conn, physicalNetworkId, zoneId, networkType, false); + // add physicalNetworkId to op_dc_vnet_alloc for this zone s_logger.debug("Adding PhysicalNetwork to op_dc_vnet_alloc"); @@ -437,6 +305,15 @@ public class Upgrade2214to30 implements DbUpgrade { pstmtUpdate = conn.prepareStatement(updateNet); pstmtUpdate.executeUpdate(); pstmtUpdate.close(); + + + //mark this physical network as removed if the zone is removed. + if(removed != null){ + pstmtUpdate = conn.prepareStatement("UPDATE `cloud`.`physical_network` SET removed = now() WHERE id = ?"); + pstmtUpdate.setLong(1, physicalNetworkId); + pstmtUpdate.executeUpdate(); + pstmtUpdate.close(); + } } } @@ -466,36 +343,6 @@ public class Upgrade2214to30 implements DbUpgrade { } - private String getNetworkLabelFromConfig(Connection conn, String name){ - String sql = "SELECT value FROM `cloud`.`configuration` where name = '"+name+"'"; - String networkLabel = null; - PreparedStatement pstmt = null; - ResultSet rs = null; - try{ - pstmt = conn.prepareStatement(sql); - rs = pstmt.executeQuery(); - if (rs.next()) { - networkLabel = rs.getString(1); - } - }catch (SQLException e) { - throw new CloudRuntimeException("Unable to fetch network label from configuration", e); - }finally{ - if (rs != null) { - try { - rs.close(); - } catch (SQLException e) { - } - } - if (pstmt != null) { - try { - pstmt.close(); - } catch (SQLException e) { - } - } - } - return networkLabel; - } - private void encryptData(Connection conn) { s_logger.debug("Encrypting the data..."); encryptConfigValues(conn); diff --git a/server/src/com/cloud/upgrade/dao/Upgrade302to40.java b/server/src/com/cloud/upgrade/dao/Upgrade302to40.java index 98daaa01a1d..5b7ad0a0baf 100644 --- a/server/src/com/cloud/upgrade/dao/Upgrade302to40.java +++ b/server/src/com/cloud/upgrade/dao/Upgrade302to40.java @@ -24,13 +24,15 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; +import java.util.UUID; import org.apache.log4j.Logger; +import com.cloud.utils.crypt.DBEncryptionUtil; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.script.Script; -public class Upgrade302to40 implements DbUpgrade { +public class Upgrade302to40 extends Upgrade30xBase implements DbUpgrade { final static Logger s_logger = Logger.getLogger(Upgrade302to40.class); @Override @@ -60,6 +62,9 @@ public class Upgrade302to40 implements DbUpgrade { @Override public void performDataMigration(Connection conn) { + correctVRProviders(conn); + correctMultiplePhysicaNetworkSetups(conn); + addVpcProvider(conn); updateRouterNetworkRef(conn); fixForeignKeys(conn); @@ -75,6 +80,433 @@ public class Upgrade302to40 implements DbUpgrade { return new File[] { new File(script) }; } + private void correctVRProviders(Connection conn) { + PreparedStatement pstmtVR = null; + ResultSet rsVR = null; + PreparedStatement pstmt = null; + ResultSet rs = null; + + try{ + pstmtVR = conn.prepareStatement("SELECT id, nsp_id FROM `cloud`.`virtual_router_providers` where type = 'VirtualRouter' AND removed IS NULL"); + rsVR = pstmtVR.executeQuery(); + while (rsVR.next()) { + long vrId = rsVR.getLong(1); + long nspId = rsVR.getLong(2); + + //check that this nspId points to a VR provider. + pstmt = conn.prepareStatement("SELECT physical_network_id, provider_name FROM `cloud`.`physical_network_service_providers` where id = ?"); + pstmt.setLong(1, nspId); + rs = pstmt.executeQuery(); + if(rs.next()){ + long physicalNetworkId = rs.getLong(1); + String providerName = rs.getString(2); + if(!providerName.equalsIgnoreCase("VirtualRouter")){ + //mismatch, correct the nsp_id in VR + PreparedStatement pstmt1 = null; + ResultSet rs1 = null; + pstmt1 = conn.prepareStatement("SELECT id FROM `cloud`.`physical_network_service_providers` where physical_network_id = ? AND provider_name = ? AND removed IS NULL"); + pstmt1.setLong(1, physicalNetworkId); + pstmt1.setString(2, "VirtualRouter"); + rs1 = pstmt1.executeQuery(); + if(rs1.next()){ + long correctNSPId = rs1.getLong(1); + + //update VR entry + PreparedStatement pstmtUpdate = null; + String updateNSPId = "UPDATE `cloud`.`virtual_router_providers` SET nsp_id = ? WHERE id = ?"; + pstmtUpdate = conn.prepareStatement(updateNSPId); + pstmtUpdate.setLong(1, correctNSPId); + pstmtUpdate.setLong(2, vrId); + pstmtUpdate.executeUpdate(); + pstmtUpdate.close(); + } + rs1.close(); + pstmt1.close(); + } + } + rs.close(); + pstmt.close(); + } + }catch (SQLException e) { + throw new CloudRuntimeException("Exception while correcting Virtual Router Entries", e); + } finally { + if (rsVR != null) { + try { + rsVR.close(); + }catch (SQLException e) { + } + } + + if (pstmtVR != null) { + try { + pstmtVR.close(); + } catch (SQLException e) { + } + } + + if (rs != null) { + try { + rs.close(); + }catch (SQLException e) { + } + } + + if (pstmt != null) { + try { + pstmt.close(); + } catch (SQLException e) { + } + } + } + + } + + private void correctMultiplePhysicaNetworkSetups(Connection conn) { + PreparedStatement pstmtZone = null; + ResultSet rsZone = null; + PreparedStatement pstmt = null; + ResultSet rs = null; + + try{ + + //check if multiple physical networks with 'Guest' Traffic types are present + //Yes: + //1) check if there are guest networks without tags, if yes then add a new physical network with default tag for them + //2) Check if there are physical network tags present + //No: Add unique tag to each physical network + //3) Get all guest networks unique network offering id's + + //Clone each for each physical network and add the tag. + //add ntwk service map entries + //update all guest networks of 1 physical network having this offering id to this new offering id + + pstmtZone = conn.prepareStatement("SELECT id, domain_id, networktype, name, uuid FROM `cloud`.`data_center`"); + rsZone = pstmtZone.executeQuery(); + while (rsZone.next()) { + long zoneId = rsZone.getLong(1); + Long domainId = rsZone.getLong(2); + String networkType = rsZone.getString(3); + String zoneName = rsZone.getString(4); + String uuid = rsZone.getString(5); + + PreparedStatement pstmtUpdate = null; + if(uuid == null){ + uuid = UUID.randomUUID().toString(); + String updateUuid = "UPDATE `cloud`.`data_center` SET uuid = ? WHERE id = ?"; + pstmtUpdate = conn.prepareStatement(updateUuid); + pstmtUpdate.setString(1, uuid); + pstmtUpdate.setLong(2, zoneId); + pstmtUpdate.executeUpdate(); + pstmtUpdate.close(); + } + + //check if any networks were untagged and remaining to be mapped to a physical network + + pstmt = conn.prepareStatement("SELECT count(n.id) FROM networks n WHERE n.physical_network_id IS NULL AND n.traffic_type = 'Guest' and n.data_center_id = ? and n.removed is null"); + pstmt.setLong(1, zoneId); + rs = pstmt.executeQuery(); + if(rs.next()){ + Long count = rs.getLong(1); + if(count > 0){ + // find the default tag to use from global config or use 'cloud-private' + String xenGuestLabel = getNetworkLabelFromConfig(conn, "xen.guest.network.device"); + //Decrypt this value. + xenGuestLabel = DBEncryptionUtil.decrypt(xenGuestLabel); + + //make sure that no physical network with this traffic label already exists. if yes, error out. + if(xenGuestLabel != null){ + PreparedStatement pstmt5 = conn.prepareStatement("SELECT count(*) FROM `cloud`.`physical_network_traffic_types` pntt JOIN `cloud`.`physical_network` pn ON pntt.physical_network_id = pn.id WHERE pntt.traffic_type ='Guest' AND pn.data_center_id = ? AND pntt.xen_network_label = ?"); + pstmt5.setLong(1, zoneId); + pstmt5.setString(2, xenGuestLabel); + ResultSet rsSameLabel = pstmt5.executeQuery(); + + if(rsSameLabel.next()){ + Long sameLabelcount = rsSameLabel.getLong(1); + if(sameLabelcount > 0){ + s_logger.error("There are untagged networks for which we need to add a physical network with Xen traffic label = 'xen.guest.network.device' config value, which is: "+xenGuestLabel); + s_logger.error("However already there are "+sameLabelcount+" physical networks setup with same traffic label, cannot upgrade"); + throw new CloudRuntimeException("Cannot upgrade this setup since a physical network with same traffic label: "+xenGuestLabel+" already exists, Please check logs and contact Support."); + } + } + } + + //Create a physical network with guest traffic type and this tag + long physicalNetworkId = addPhysicalNetworkToZone(conn, zoneId, zoneName, networkType, null, domainId); + addTrafficType(conn, physicalNetworkId, "Guest", xenGuestLabel, null, null); + addDefaultVRProvider(conn, physicalNetworkId, zoneId); + addDefaultSGProvider(conn, physicalNetworkId, zoneId, networkType, true); + + PreparedStatement pstmt3 = conn.prepareStatement("SELECT n.id FROM networks n WHERE n.physical_network_id IS NULL AND n.traffic_type = 'Guest' and n.data_center_id = ? and n.removed is null"); + pstmt3.setLong(1, zoneId); + ResultSet rsNet = pstmt3.executeQuery(); + s_logger.debug("Adding PhysicalNetwork to VLAN"); + s_logger.debug("Adding PhysicalNetwork to user_ip_address"); + s_logger.debug("Adding PhysicalNetwork to networks"); + while(rsNet.next()){ + Long networkId = rsNet.getLong(1); + addPhysicalNtwk_To_Ntwk_IP_Vlan(conn, physicalNetworkId,networkId); + } + rsNet.close(); + pstmt3.close(); + } + } + rs.close(); + pstmt.close(); + + + boolean multiplePhysicalNetworks = false; + + pstmt = conn.prepareStatement("SELECT count(*) FROM `cloud`.`physical_network_traffic_types` pntt JOIN `cloud`.`physical_network` pn ON pntt.physical_network_id = pn.id WHERE pntt.traffic_type ='Guest' and pn.data_center_id = ?"); + pstmt.setLong(1, zoneId); + rs = pstmt.executeQuery(); + if(rs.next()){ + Long count = rs.getLong(1); + if(count > 1){ + s_logger.debug("There are "+count+" physical networks setup"); + multiplePhysicalNetworks = true; + } + } + rs.close(); + pstmt.close(); + + if(multiplePhysicalNetworks){ + //check if guest vnet is wrongly configured by earlier upgrade. If yes error out + //check if any vnet is allocated and guest networks are using vnet But the physical network id does not match on the vnet and guest network. + PreparedStatement pstmt4 = conn.prepareStatement("SELECT v.id, v.vnet, v.reservation_id, v.physical_network_id as vpid, n.id, n.physical_network_id as npid FROM `cloud`.`op_dc_vnet_alloc` v JOIN `cloud`.`networks` n ON CONCAT('vlan://' , v.vnet) = n.broadcast_uri WHERE v.taken IS NOT NULL AND v.data_center_id = ? AND n.removed IS NULL AND v.physical_network_id != n.physical_network_id"); + pstmt4.setLong(1, zoneId); + ResultSet rsVNet = pstmt4.executeQuery(); + if(rsVNet.next()){ + String vnet = rsVNet.getString(2); + String networkId = rsVNet.getString(5); + String vpid = rsVNet.getString(4); + String npid = rsVNet.getString(6); + s_logger.error("Guest Vnet assignment is set wrongly . Cannot upgrade until that is corrected. Example- Vnet: "+ vnet +" has physical network id: " + vpid +" ,but the guest network: " +networkId+" that uses it has physical network id: " +npid ); + + String message = "Cannot upgrade. Your setup has multiple Physical Networks and is using guest Vnet that is assigned wrongly. To upgrade, first correct the setup by doing the following: \n" + + "1. Please rollback to your 2.2.14 setup\n" + + "2. Please stop all VMs using isolated(virtual) networks through CloudStack\n" + + "3. Run following query to find if any networks still have nics allocated:\n\t"+ + "a) check if any virtual guest networks still have allocated nics by running:\n\t" + + "SELECT DISTINCT op.id from `cloud`.`op_networks` op JOIN `cloud`.`networks` n on op.id=n.id WHERE nics_count != 0 AND guest_type = 'Virtual';\n\t"+ + "b) If this returns any networkd ids, then ensure that all VMs are stopped, no new VM is being started, and then shutdown management server\n\t"+ + "c) Clean up the nics count for the 'virtual' network id's returned in step (a) by running this:\n\t"+ + "UPDATE `cloud`.`op_networks` SET nics_count = 0 WHERE id = \n\t"+ + "d) Restart management server and wait for all networks to shutdown. [Networks shutdown will be determined by network.gc.interval and network.gc.wait seconds] \n"+ + "4. Please ensure all networks are shutdown and all guest Vnet's are free.\n" + + "5. Run upgrade. This will allocate all your guest vnet range to first physical network. \n" + + "6. Reconfigure the vnet ranges for each physical network as desired by using updatePhysicalNetwork API \n" + + "7. Start all your VMs"; + + s_logger.error(message); + throw new CloudRuntimeException("Cannot upgrade this setup since Guest Vnet assignment to the multiple physical networks is incorrect. Please check the logs for details on how to proceed"); + + } + rsVNet.close(); + pstmt4.close(); + + //Clean up any vnets that have no live networks/nics + pstmt4 = conn.prepareStatement("SELECT v.id, v.vnet, v.reservation_id FROM `cloud`.`op_dc_vnet_alloc` v LEFT JOIN networks n ON CONCAT('vlan://' , v.vnet) = n.broadcast_uri WHERE v.taken IS NOT NULL AND v.data_center_id = ? AND n.broadcast_uri IS NULL AND n.removed IS NULL"); + pstmt4.setLong(1, zoneId); + rsVNet = pstmt4.executeQuery(); + while(rsVNet.next()){ + Long vnet_id = rsVNet.getLong(1); + String vnetValue = rsVNet.getString(2); + String reservationId = rsVNet.getString(3); + //does this vnet have any nic associated? + PreparedStatement pstmt5 = conn.prepareStatement("SELECT id, instance_id FROM `cloud`.`nics` where broadcast_uri = ? and removed IS NULL"); + String uri = "vlan://"+vnetValue; + pstmt5.setString(1, uri); + ResultSet rsNic = pstmt5.executeQuery(); + Long nic_id = rsNic.getLong(1); + Long instance_id = rsNic.getLong(2); + if(rsNic.next()){ + throw new CloudRuntimeException("Cannot upgrade. Please cleanup the guest vnet: "+ vnetValue +" , it is being used by nic_id: "+ nic_id +" , instance_id: " + instance_id ); + } + + //free this vnet + String freeVnet = "UPDATE `cloud`.`op_dc_vnet_alloc` SET account_id = NULL, taken = NULL, reservation_id = NULL WHERE id = ?"; + pstmtUpdate = conn.prepareStatement(freeVnet); + pstmtUpdate.setLong(1, vnet_id); + pstmtUpdate.executeUpdate(); + pstmtUpdate.close(); + } + rsVNet.close(); + pstmt4.close(); + + + //add tags to the physical networks if not present and clone offerings + + pstmt = conn.prepareStatement("SELECT pn.id as pid , ptag.tag as tag FROM `cloud`.`physical_network` pn LEFT JOIN `cloud`.`physical_network_tags` ptag ON pn.id = ptag.physical_network_id where pn.data_center_id = ?"); + pstmt.setLong(1, zoneId); + rs = pstmt.executeQuery(); + while(rs.next()){ + long physicalNetworkId = rs.getLong("pid"); + String tag = rs.getString("tag"); + if(tag == null){ + //need to add unique tag + String newTag = "pNtwk-tag-" + physicalNetworkId; + + String updateVnet = "INSERT INTO `cloud`.`physical_network_tags`(tag, physical_network_id) VALUES( ?, ? )"; + pstmtUpdate = conn.prepareStatement(updateVnet); + pstmtUpdate.setString(1, newTag); + pstmtUpdate.setLong(2, physicalNetworkId); + pstmtUpdate.executeUpdate(); + pstmtUpdate.close(); + + //clone offerings and tag them with this new tag, if there are any guest networks for this physical network + + PreparedStatement pstmt2 = null; + ResultSet rs2 = null; + + pstmt2 = conn.prepareStatement("SELECT distinct network_offering_id FROM `cloud`.`networks` where traffic_type= 'Guest' and physical_network_id = ? and removed is null"); + pstmt2.setLong(1, physicalNetworkId); + rs2 = pstmt2.executeQuery(); + + while(rs2.next()){ + //clone each offering, add new tag, clone offering-svc-map, update guest networks with new offering id + long networkOfferingId = rs2.getLong(1); + cloneOfferingAndAddTag(conn, networkOfferingId, physicalNetworkId, newTag); + } + rs2.close(); + pstmt2.close(); + } + } + rs.close(); + pstmt.close(); + } + } + } catch (SQLException e) { + throw new CloudRuntimeException("Exception while correcting PhysicalNetwork setup", e); + } finally { + if (rsZone != null) { + try { + rsZone.close(); + }catch (SQLException e) { + } + } + + if (pstmtZone != null) { + try { + pstmtZone.close(); + } catch (SQLException e) { + } + } + + if (rs != null) { + try { + rs.close(); + }catch (SQLException e) { + } + } + + if (pstmt != null) { + try { + pstmt.close(); + } catch (SQLException e) { + } + } + } + } + + + private void cloneOfferingAndAddTag(Connection conn, long networkOfferingId, long physicalNetworkId, String newTag) { + + + PreparedStatement pstmt = null; + ResultSet rs = null; + try{ + pstmt = conn.prepareStatement("select count(*) from `cloud`.`network_offerings`"); + rs = pstmt.executeQuery(); + long ntwkOffCount = 0; + while (rs.next()) { + ntwkOffCount = rs.getLong(1); + } + rs.close(); + pstmt.close(); + + pstmt = conn.prepareStatement("DROP TEMPORARY TABLE IF EXISTS `cloud`.`network_offerings2`"); + pstmt.executeUpdate(); + + pstmt = conn.prepareStatement("CREATE TEMPORARY TABLE `cloud`.`network_offerings2` ENGINE=MEMORY SELECT * FROM `cloud`.`network_offerings` WHERE id=1"); + pstmt.executeUpdate(); + pstmt.close(); + + // clone the record to + pstmt = conn.prepareStatement("INSERT INTO `cloud`.`network_offerings2` SELECT * FROM `cloud`.`network_offerings` WHERE id=?"); + pstmt.setLong(1, networkOfferingId); + pstmt.executeUpdate(); + pstmt.close(); + + pstmt = conn.prepareStatement("SELECT unique_name FROM `cloud`.`network_offerings` WHERE id=?"); + pstmt.setLong(1, networkOfferingId); + rs = pstmt.executeQuery(); + String uniqueName = null; + while (rs.next()) { + uniqueName = rs.getString(1) + "-" + physicalNetworkId; + } + rs.close(); + pstmt.close(); + + + pstmt = conn.prepareStatement("UPDATE `cloud`.`network_offerings2` SET id=?, unique_name=?, name=?, tags=?, uuid=? WHERE id=?"); + ntwkOffCount = ntwkOffCount + 1; + long newNetworkOfferingId = ntwkOffCount; + pstmt.setLong(1, newNetworkOfferingId); + pstmt.setString(2, uniqueName); + pstmt.setString(3, uniqueName); + pstmt.setString(4, newTag); + String uuid = UUID.randomUUID().toString(); + pstmt.setString(5, uuid); + pstmt.setLong(6, networkOfferingId); + pstmt.executeUpdate(); + pstmt.close(); + + pstmt = conn.prepareStatement("INSERT INTO `cloud`.`network_offerings` SELECT * from `cloud`.`network_offerings2` WHERE id=" + newNetworkOfferingId); + pstmt.executeUpdate(); + pstmt.close(); + + //clone service map + pstmt = conn.prepareStatement("select service, provider from `cloud`.`ntwk_offering_service_map` where network_offering_id=?"); + pstmt.setLong(1, networkOfferingId); + rs = pstmt.executeQuery(); + while (rs.next()) { + String service = rs.getString(1); + String provider = rs.getString(2); + pstmt = conn.prepareStatement("INSERT INTO `cloud`.`ntwk_offering_service_map` (`network_offering_id`, `service`, `provider`, `created`) values (?,?,?, now())"); + pstmt.setLong(1, newNetworkOfferingId); + pstmt.setString(2, service); + pstmt.setString(3, provider); + pstmt.executeUpdate(); + } + rs.close(); + pstmt.close(); + + pstmt = conn.prepareStatement("UPDATE `cloud`.`networks` SET network_offering_id=? where physical_network_id=? and traffic_type ='Guest' and network_offering_id="+networkOfferingId); + pstmt.setLong(1, newNetworkOfferingId); + pstmt.setLong(2, physicalNetworkId); + pstmt.executeUpdate(); + pstmt.close(); + + }catch (SQLException e) { + throw new CloudRuntimeException("Exception while cloning NetworkOffering", e); + } finally { + try { + pstmt = conn.prepareStatement("DROP TEMPORARY TABLE `cloud`.`network_offerings2`"); + pstmt.executeUpdate(); + + if (rs != null) { + rs.close(); + } + + if (pstmt != null) { + pstmt.close(); + } + }catch (SQLException e) { + } + } + } + + private void addVpcProvider(Connection conn){ //Encrypt config params and change category to Hidden s_logger.debug("Adding vpc provider to all physical networks in the system"); diff --git a/server/src/com/cloud/upgrade/dao/Upgrade30xBase.java b/server/src/com/cloud/upgrade/dao/Upgrade30xBase.java new file mode 100644 index 00000000000..63047a2bcde --- /dev/null +++ b/server/src/com/cloud/upgrade/dao/Upgrade30xBase.java @@ -0,0 +1,308 @@ +/*Copyright 2012 Citrix Systems, Inc. Licensed under the +Apache License, Version 2.0 (the "License"); you may not use this +file except in compliance with the License. Citrix Systems, Inc. +reserves all rights not expressly granted by the License. +You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License.*/ + +package com.cloud.upgrade.dao; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.UUID; + +import org.apache.log4j.Logger; + +import com.cloud.utils.exception.CloudRuntimeException; + +public abstract class Upgrade30xBase implements DbUpgrade{ + + final static Logger s_logger = Logger.getLogger(Upgrade30xBase.class); + + protected String getNetworkLabelFromConfig(Connection conn, String name){ + String sql = "SELECT value FROM `cloud`.`configuration` where name = '"+name+"'"; + String networkLabel = null; + PreparedStatement pstmt = null; + ResultSet rs = null; + try{ + pstmt = conn.prepareStatement(sql); + rs = pstmt.executeQuery(); + if (rs.next()) { + networkLabel = rs.getString(1); + } + }catch (SQLException e) { + throw new CloudRuntimeException("Unable to fetch network label from configuration", e); + }finally{ + if (rs != null) { + try { + rs.close(); + } catch (SQLException e) { + } + } + if (pstmt != null) { + try { + pstmt.close(); + } catch (SQLException e) { + } + } + } + return networkLabel; + } + + + protected long addPhysicalNetworkToZone(Connection conn, long zoneId, String zoneName, String networkType, String vnet, Long domainId){ + + String getNextNetworkSequenceSql = "SELECT value from `cloud`.`sequence` where name='physical_networks_seq'"; + String advanceNetworkSequenceSql = "UPDATE `cloud`.`sequence` set value=value+1 where name='physical_networks_seq'"; + PreparedStatement pstmtUpdate = null, pstmt2 = null; + // add p.network + try{ + pstmt2 = conn.prepareStatement(getNextNetworkSequenceSql); + + ResultSet rsSeq = pstmt2.executeQuery(); + rsSeq.next(); + + long physicalNetworkId = rsSeq.getLong(1); + rsSeq.close(); + pstmt2.close(); + pstmt2 = conn.prepareStatement(advanceNetworkSequenceSql); + pstmt2.executeUpdate(); + pstmt2.close(); + + String uuid = UUID.randomUUID().toString(); + String broadcastDomainRange = "POD"; + if ("Advanced".equals(networkType)) { + broadcastDomainRange = "ZONE"; + } + + s_logger.debug("Adding PhysicalNetwork " + physicalNetworkId + " for Zone id " + zoneId); + String sql = "INSERT INTO `cloud`.`physical_network` (id, uuid, data_center_id, vnet, broadcast_domain_range, state, name) VALUES (?,?,?,?,?,?,?)"; + + pstmtUpdate = conn.prepareStatement(sql); + pstmtUpdate.setLong(1, physicalNetworkId); + pstmtUpdate.setString(2, uuid); + pstmtUpdate.setLong(3, zoneId); + pstmtUpdate.setString(4, vnet); + pstmtUpdate.setString(5, broadcastDomainRange); + pstmtUpdate.setString(6, "Enabled"); + zoneName = zoneName + "-pNtwk" +physicalNetworkId; + pstmtUpdate.setString(7, zoneName); + s_logger.warn("Statement is " + pstmtUpdate.toString()); + pstmtUpdate.executeUpdate(); + pstmtUpdate.close(); + + if (domainId != null && domainId.longValue() != 0) { + s_logger.debug("Updating domain_id for physical network id=" + physicalNetworkId); + sql = "UPDATE `cloud`.`physical_network` set domain_id=? where id=?"; + pstmtUpdate = conn.prepareStatement(sql); + pstmtUpdate.setLong(1, domainId); + pstmtUpdate.setLong(2, physicalNetworkId); + pstmtUpdate.executeUpdate(); + pstmtUpdate.close(); + } + + return physicalNetworkId; + } catch (SQLException e) { + throw new CloudRuntimeException("Exception while adding PhysicalNetworks", e); + } finally { + if (pstmtUpdate != null) { + try { + pstmtUpdate.close(); + } catch (SQLException e) { + } + } + if (pstmt2 != null) { + try { + pstmt2.close(); + } catch (SQLException e) { + } + } + + } + } + + protected void addTrafficType(Connection conn, long physicalNetworkId, String trafficType, String xenPublicLabel, String kvmPublicLabel, String vmwarePublicLabel){ + // add traffic types + PreparedStatement pstmtUpdate = null; + try{ + s_logger.debug("Adding PhysicalNetwork traffic types"); + String insertTraficType = "INSERT INTO `cloud`.`physical_network_traffic_types` (physical_network_id, traffic_type, xen_network_label, kvm_network_label, vmware_network_label, uuid) VALUES ( ?, ?, ?, ?, ?, ?)"; + pstmtUpdate = conn.prepareStatement(insertTraficType); + pstmtUpdate.setLong(1, physicalNetworkId); + pstmtUpdate.setString(2, trafficType); + pstmtUpdate.setString(3, xenPublicLabel); + pstmtUpdate.setString(4, kvmPublicLabel); + pstmtUpdate.setString(5, vmwarePublicLabel); + pstmtUpdate.setString(6, UUID.randomUUID().toString()); + pstmtUpdate.executeUpdate(); + pstmtUpdate.close(); + }catch (SQLException e) { + throw new CloudRuntimeException("Exception while adding PhysicalNetworks", e); + } finally { + if (pstmtUpdate != null) { + try { + pstmtUpdate.close(); + } catch (SQLException e) { + } + } + } + } + + + protected void addDefaultSGProvider(Connection conn, long physicalNetworkId, long zoneId, String networkType, boolean is304){ + PreparedStatement pstmtUpdate = null, pstmt2 = null; + try{ + //add security group service provider (if security group service is enabled for at least one guest network) + boolean isSGServiceEnabled = false; + String selectSG = ""; + + if(is304){ + selectSG = "SELECT nm.* FROM `cloud`.`ntwk_service_map` nm JOIN `cloud`.`networks` n ON nm.network_id = n.id where n.data_center_id = ? and nm.service='SecurityGroup'"; + }else{ + selectSG = "SELECT * from `cloud`.`networks` where is_security_group_enabled=1 and data_center_id=?"; + } + + pstmt2 = conn.prepareStatement(selectSG); + pstmt2.setLong(1, zoneId); + ResultSet sgDcSet = pstmt2.executeQuery(); + if (sgDcSet.next()) { + isSGServiceEnabled = true; + } + sgDcSet.close(); + pstmt2.close(); + + if(isSGServiceEnabled){ + s_logger.debug("Adding PhysicalNetworkServiceProvider SecurityGroupProvider to the physical network id=" + physicalNetworkId); + 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,0,0,0,0,0,0,0,0,0,0,1)"; + pstmtUpdate = conn.prepareStatement(insertPNSP); + pstmtUpdate.setString(1, UUID.randomUUID().toString()); + pstmtUpdate.setLong(2, physicalNetworkId); + pstmtUpdate.setString(3, "SecurityGroupProvider"); + if ("Advanced".equals(networkType)) { + pstmtUpdate.setString(4, "Disabled"); + }else{ + pstmtUpdate.setString(4, "Enabled"); + } + pstmtUpdate.executeUpdate(); + pstmtUpdate.close(); + } + + }catch (SQLException e) { + throw new CloudRuntimeException("Exception while adding default Security Group Provider", e); + } finally { + if (pstmtUpdate != null) { + try { + pstmtUpdate.close(); + } catch (SQLException e) { + } + } + if (pstmt2 != null) { + try { + pstmt2.close(); + } catch (SQLException e) { + } + } + } + } + + 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 { + if (pstmtUpdate != null) { + try { + pstmtUpdate.close(); + } catch (SQLException e) { + } + } + if (pstmt2 != null) { + try { + pstmt2.close(); + } catch (SQLException e) { + } + } + } + } + + protected void addPhysicalNtwk_To_Ntwk_IP_Vlan(Connection conn, long physicalNetworkId, long networkId){ + PreparedStatement pstmtUpdate = null; + try{ + // add physicalNetworkId to vlan for this zone + String updateVLAN = "UPDATE `cloud`.`vlan` SET physical_network_id = " + physicalNetworkId + " WHERE network_id = " + networkId; + pstmtUpdate = conn.prepareStatement(updateVLAN); + pstmtUpdate.executeUpdate(); + pstmtUpdate.close(); + + // add physicalNetworkId to user_ip_address for this zone + String updateUsrIp = "UPDATE `cloud`.`user_ip_address` SET physical_network_id = " + physicalNetworkId + " WHERE source_network_id = " + networkId; + pstmtUpdate = conn.prepareStatement(updateUsrIp); + pstmtUpdate.executeUpdate(); + pstmtUpdate.close(); + + // add physicalNetworkId to guest networks for this zone + String updateNet = "UPDATE `cloud`.`networks` SET physical_network_id = " + physicalNetworkId + " WHERE id = " + networkId + " AND traffic_type = 'Guest'"; + pstmtUpdate = conn.prepareStatement(updateNet); + pstmtUpdate.executeUpdate(); + pstmtUpdate.close(); + }catch (SQLException e) { + throw new CloudRuntimeException("Exception while adding PhysicalNetworks", e); + } finally { + if (pstmtUpdate != null) { + try { + pstmtUpdate.close(); + } catch (SQLException e) { + } + } + } + + } + + + + +}