Skip to content

Commit b28749f

Browse files
Murali ReddyMurali Reddy
authored andcommitted
upgrade external devices support from 2.2.14 to network offering framework
1 parent bfd6c67 commit b28749f

1 file changed

Lines changed: 348 additions & 0 deletions

File tree

‎server/src/com/cloud/upgrade/dao/Upgrade302to40.java‎

Lines changed: 348 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ public void performDataMigration(Connection conn) {
6868
addVpcProvider(conn);
6969
updateRouterNetworkRef(conn);
7070
fixForeignKeys(conn);
71+
setupExternalNetworkDevices(conn);
72+
fixZoneUsingExternalDevices(conn);
7173
}
7274

7375
@Override
@@ -681,4 +683,350 @@ private void fixForeignKeys(Connection conn) {
681683
throw new CloudRuntimeException("Unable to execute ssh_keypairs table update for adding domain_id foreign key", e);
682684
}
683685
}
686+
687+
// upgrades deployment with F5 and SRX devices, to 3.0's Network offerings & service providers paradigm
688+
private void setupExternalNetworkDevices(Connection conn) {
689+
PreparedStatement zoneSearchStmt = null, pNetworkStmt = null, f5DevicesStmt = null, srxDevicesStmt = null;
690+
ResultSet zoneResults = null, pNetworksResults = null, f5DevicesResult = null, srxDevicesResult = null;
691+
692+
try {
693+
zoneSearchStmt = conn.prepareStatement("SELECT id, networktype FROM `cloud`.`data_center`");
694+
zoneResults = zoneSearchStmt.executeQuery();
695+
while (zoneResults.next()) {
696+
long zoneId = zoneResults.getLong(1);
697+
String networkType = zoneResults.getString(2);
698+
699+
if (!NetworkType.Advanced.toString().equalsIgnoreCase(networkType)) {
700+
continue;
701+
}
702+
703+
pNetworkStmt = conn.prepareStatement("SELECT id FROM `cloud`.`physical_network` where data_center_id=?");
704+
pNetworkStmt.setLong(1, zoneId);
705+
pNetworksResults = pNetworkStmt.executeQuery();
706+
while (pNetworksResults.next()) {
707+
long physicalNetworkId = pNetworksResults.getLong(1);
708+
PreparedStatement fetchF5NspStmt = conn.prepareStatement("SELECT id from `cloud`.`physical_network_service_providers` where physical_network_id=" + physicalNetworkId
709+
+ " and provider_name = 'F5BigIp'");
710+
ResultSet rsF5NSP = fetchF5NspStmt.executeQuery();
711+
boolean hasF5Nsp = rsF5NSP.next();
712+
fetchF5NspStmt.close();
713+
714+
if (!hasF5Nsp) {
715+
f5DevicesStmt = conn.prepareStatement("SELECT id FROM host WHERE data_center_id=? AND type = 'ExternalLoadBalancer' AND removed IS NULL");
716+
f5DevicesStmt.setLong(1, zoneId);
717+
f5DevicesResult = f5DevicesStmt.executeQuery();
718+
719+
while (f5DevicesResult.next()) {
720+
long f5HostId = f5DevicesResult.getLong(1);;
721+
// add F5BigIP provider and provider instance to physical network
722+
addF5ServiceProvider(conn, physicalNetworkId, zoneId);
723+
addF5LoadBalancer(conn, f5HostId, physicalNetworkId);
724+
}
725+
}
726+
727+
PreparedStatement fetchSRXNspStmt = conn.prepareStatement("SELECT id from `cloud`.`physical_network_service_providers` where physical_network_id=" + physicalNetworkId
728+
+ " and provider_name = 'JuniperSRX'");
729+
ResultSet rsSRXNSP = fetchSRXNspStmt.executeQuery();
730+
boolean hasSrxNsp = rsSRXNSP.next();
731+
fetchSRXNspStmt.close();
732+
733+
if (!hasSrxNsp) {
734+
srxDevicesStmt = conn.prepareStatement("SELECT id FROM host WHERE data_center_id=? AND type = 'ExternalFirewall' AND removed IS NULL");
735+
srxDevicesStmt.setLong(1, zoneId);
736+
srxDevicesResult = srxDevicesStmt.executeQuery();
737+
738+
while (srxDevicesResult.next()) {
739+
long srxHostId = srxDevicesResult.getLong(1);
740+
// add SRX provider and provider instance to physical network
741+
addSrxServiceProvider(conn, physicalNetworkId, zoneId);
742+
addSrxFirewall(conn, srxHostId, physicalNetworkId);
743+
}
744+
}
745+
}
746+
}
747+
748+
if (zoneResults != null) {
749+
try {
750+
zoneResults.close();
751+
} catch (SQLException e) {
752+
}
753+
}
754+
if (zoneSearchStmt != null) {
755+
try {
756+
zoneSearchStmt.close();
757+
} catch (SQLException e) {
758+
}
759+
}
760+
} catch (SQLException e) {
761+
throw new CloudRuntimeException("Exception while adding PhysicalNetworks", e);
762+
} finally {
763+
764+
}
765+
}
766+
767+
private void addF5LoadBalancer(Connection conn, long hostId, long physicalNetworkId){
768+
PreparedStatement pstmtUpdate = null;
769+
try{
770+
s_logger.debug("Adding F5 Big IP load balancer with host id " + hostId + " in to physical network" + physicalNetworkId);
771+
String insertF5 = "INSERT INTO `cloud`.`external_load_balancer_devices` (physical_network_id, host_id, provider_name, " +
772+
"device_name, capacity, is_dedicated, device_state, allocation_state, is_inline, is_managed, uuid) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
773+
pstmtUpdate = conn.prepareStatement(insertF5);
774+
pstmtUpdate.setLong(1, physicalNetworkId);
775+
pstmtUpdate.setLong(2, hostId);
776+
pstmtUpdate.setString(3, "F5BigIp");
777+
pstmtUpdate.setString(4, "F5BigIpLoadBalancer");
778+
pstmtUpdate.setLong(5, 0);
779+
pstmtUpdate.setBoolean(6, false);
780+
pstmtUpdate.setString(7, "Enabled");
781+
pstmtUpdate.setString(8, "Shared");
782+
pstmtUpdate.setBoolean(9, false);
783+
pstmtUpdate.setBoolean(10, false);
784+
pstmtUpdate.setString(11, UUID.randomUUID().toString());
785+
pstmtUpdate.executeUpdate();
786+
}catch (SQLException e) {
787+
throw new CloudRuntimeException("Exception while adding F5 load balancer device" , e);
788+
} finally {
789+
if (pstmtUpdate != null) {
790+
try {
791+
pstmtUpdate.close();
792+
} catch (SQLException e) {
793+
}
794+
}
795+
}
796+
}
797+
798+
private void addSrxFirewall(Connection conn, long hostId, long physicalNetworkId){
799+
PreparedStatement pstmtUpdate = null;
800+
try{
801+
s_logger.debug("Adding SRX firewall device with host id " + hostId + " in to physical network" + physicalNetworkId);
802+
String insertSrx = "INSERT INTO `cloud`.`external_firewall_devices` (physical_network_id, host_id, provider_name, " +
803+
"device_name, capacity, is_dedicated, device_state, allocation_state, uuid) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?)";
804+
pstmtUpdate = conn.prepareStatement(insertSrx);
805+
pstmtUpdate.setLong(1, physicalNetworkId);
806+
pstmtUpdate.setLong(2, hostId);
807+
pstmtUpdate.setString(3, "JuniperSRX");
808+
pstmtUpdate.setString(4, "JuniperSRXFirewall");
809+
pstmtUpdate.setLong(5, 0);
810+
pstmtUpdate.setBoolean(6, false);
811+
pstmtUpdate.setString(7, "Enabled");
812+
pstmtUpdate.setString(8, "Shared");
813+
pstmtUpdate.setString(9, UUID.randomUUID().toString());
814+
pstmtUpdate.executeUpdate();
815+
}catch (SQLException e) {
816+
throw new CloudRuntimeException("Exception while adding SRX firewall device ", e);
817+
} finally {
818+
if (pstmtUpdate != null) {
819+
try {
820+
pstmtUpdate.close();
821+
} catch (SQLException e) {
822+
}
823+
}
824+
}
825+
}
826+
827+
private void addF5ServiceProvider(Connection conn, long physicalNetworkId, long zoneId){
828+
PreparedStatement pstmtUpdate = null;
829+
try{
830+
// add physical network service provider - F5BigIp
831+
s_logger.debug("Adding PhysicalNetworkServiceProvider F5BigIp" + " in to physical network" + physicalNetworkId);
832+
String insertPNSP = "INSERT INTO `cloud`.`physical_network_service_providers` (`uuid`, `physical_network_id` , `provider_name`, `state` ," +
833+
"`destination_physical_network_id`, `vpn_service_provided`, `dhcp_service_provided`, `dns_service_provided`, `gateway_service_provided`," +
834+
"`firewall_service_provided`, `source_nat_service_provided`, `load_balance_service_provided`, `static_nat_service_provided`," +
835+
"`port_forwarding_service_provided`, `user_data_service_provided`, `security_group_service_provided`) VALUES (?,?,?,?,0,0,0,0,0,0,0,1,0,0,0,0)";
836+
837+
pstmtUpdate = conn.prepareStatement(insertPNSP);
838+
pstmtUpdate.setString(1, UUID.randomUUID().toString());
839+
pstmtUpdate.setLong(2, physicalNetworkId);
840+
pstmtUpdate.setString(3, "F5BigIp");
841+
pstmtUpdate.setString(4, "Enabled");
842+
pstmtUpdate.executeUpdate();
843+
}catch (SQLException e) {
844+
throw new CloudRuntimeException("Exception while adding PhysicalNetworkServiceProvider F5BigIp", e);
845+
} finally {
846+
if (pstmtUpdate != null) {
847+
try {
848+
pstmtUpdate.close();
849+
} catch (SQLException e) {
850+
}
851+
}
852+
}
853+
}
854+
855+
private void addSrxServiceProvider(Connection conn, long physicalNetworkId, long zoneId){
856+
PreparedStatement pstmtUpdate = null;
857+
try{
858+
// add physical network service provider - JuniperSRX
859+
s_logger.debug("Adding PhysicalNetworkServiceProvider JuniperSRX");
860+
String insertPNSP = "INSERT INTO `cloud`.`physical_network_service_providers` (`uuid`, `physical_network_id` , `provider_name`, `state` ," +
861+
"`destination_physical_network_id`, `vpn_service_provided`, `dhcp_service_provided`, `dns_service_provided`, `gateway_service_provided`," +
862+
"`firewall_service_provided`, `source_nat_service_provided`, `load_balance_service_provided`, `static_nat_service_provided`," +
863+
"`port_forwarding_service_provided`, `user_data_service_provided`, `security_group_service_provided`) VALUES (?,?,?,?,0,0,0,0,1,1,1,0,1,1,0,0)";
864+
865+
pstmtUpdate = conn.prepareStatement(insertPNSP);
866+
pstmtUpdate.setString(1, UUID.randomUUID().toString());
867+
pstmtUpdate.setLong(2, physicalNetworkId);
868+
pstmtUpdate.setString(3, "JuniperSRX");
869+
pstmtUpdate.setString(4, "Enabled");
870+
pstmtUpdate.executeUpdate();
871+
}catch (SQLException e) {
872+
throw new CloudRuntimeException("Exception while adding PhysicalNetworkServiceProvider JuniperSRX" , e);
873+
} finally {
874+
if (pstmtUpdate != null) {
875+
try {
876+
pstmtUpdate.close();
877+
} catch (SQLException e) {
878+
}
879+
}
880+
}
881+
}
882+
883+
// 1) ensure that networks using external load balancer/firewall in 2.2.14 or prior releases deployments
884+
// has entry in network_external_lb_device_map and network_external_firewall_device_map
885+
//
886+
// 2) Some keys of host details for F5 and SRX devices were stored in Camel Case in 2.x releases. From 3.0
887+
// they are made in lowercase. On upgrade change the host details name to lower case
888+
private void fixZoneUsingExternalDevices(Connection conn) {
889+
//Get zones to upgrade
890+
List<Long> zoneIds = new ArrayList<Long>();
891+
PreparedStatement pstmt = null;
892+
PreparedStatement pstmtUpdate = null;
893+
ResultSet rs = null;
894+
long networkOfferingId, networkId;
895+
long f5DeviceId, f5HostId;
896+
long srxDevivceId, srxHostId;
897+
898+
try {
899+
pstmt = conn.prepareStatement("select id from `cloud`.`data_center` where lb_provider='F5BigIp' or firewall_provider='JuniperSRX' or gateway_provider='JuniperSRX'");
900+
rs = pstmt.executeQuery();
901+
while (rs.next()) {
902+
zoneIds.add(rs.getLong(1));
903+
}
904+
} catch (SQLException e) {
905+
throw new CloudRuntimeException("Unable to create network to LB & firewalla device mapping for networks that use them", e);
906+
}
907+
908+
if (zoneIds.size() == 0) {
909+
return; // no zones using F5 and SRX devices so return
910+
}
911+
912+
// find the default network offering created for external devices during upgrade from 2.2.14
913+
try {
914+
pstmt = conn.prepareStatement("select id from `cloud`.`network_offerings` where unique_name='Isolated with external providers' ");
915+
rs = pstmt.executeQuery();
916+
if (rs.first()) {
917+
networkOfferingId = rs.getLong(1);
918+
} else {
919+
throw new CloudRuntimeException("Cannot upgrade as there is no 'Isolated with external providers' network offering crearted .");
920+
}
921+
} catch (SQLException e) {
922+
throw new CloudRuntimeException("Unable to create network to LB & firewalla device mapping for networks that use them", e);
923+
}
924+
925+
for (Long zoneId : zoneIds) {
926+
try {
927+
// find the F5 device id in the zone
928+
pstmt = conn.prepareStatement("SELECT id FROM host WHERE data_center_id=? AND type = 'ExternalLoadBalancer' AND removed IS NULL");
929+
pstmt.setLong(1, zoneId);
930+
rs = pstmt.executeQuery();
931+
if (rs.first()) {
932+
f5HostId = rs.getLong(1);
933+
} else {
934+
throw new CloudRuntimeException("Cannot upgrade as there is no F5 load balancer device found in data center " + zoneId);
935+
}
936+
pstmt = conn.prepareStatement("SELECT id FROM external_load_balancer_devices WHERE host_id=?");
937+
pstmt.setLong(1, f5HostId);
938+
rs = pstmt.executeQuery();
939+
if (rs.first()) {
940+
f5DeviceId = rs.getLong(1);
941+
} else {
942+
throw new CloudRuntimeException("Cannot upgrade as there is no F5 load balancer device with host ID " + f5HostId + " found in external_load_balancer_device");
943+
}
944+
945+
// find the SRX device id in the zone
946+
pstmt = conn.prepareStatement("SELECT id FROM host WHERE data_center_id=? AND type = 'ExternalFirewall' AND removed IS NULL");
947+
pstmt.setLong(1, zoneId);
948+
rs = pstmt.executeQuery();
949+
if (rs.first()) {
950+
srxHostId = rs.getLong(1);
951+
} else {
952+
throw new CloudRuntimeException("Cannot upgrade as there is no SRX firewall device found in data center " + zoneId);
953+
}
954+
pstmt = conn.prepareStatement("SELECT id FROM external_firewall_devices WHERE host_id=?");
955+
pstmt.setLong(1, srxHostId);
956+
rs = pstmt.executeQuery();
957+
if (rs.first()) {
958+
srxDevivceId = rs.getLong(1);
959+
} else {
960+
throw new CloudRuntimeException("Cannot upgrade as there is no SRX firewall device found with host ID " + srxHostId + " found in external_firewall_devices");
961+
}
962+
963+
// check if network any uses F5 or SRX devices in the zone
964+
pstmt = conn.prepareStatement("select id from `cloud`.`networks` where guest_type='Virtual' and data_center_id=? and network_offering_id=? and removed IS NULL");
965+
pstmt.setLong(1, zoneId);
966+
pstmt.setLong(2, networkOfferingId);
967+
rs = pstmt.executeQuery();
968+
while (rs.next()) {
969+
// get the network Id
970+
networkId = rs.getLong(1);
971+
972+
// add mapping for the network in network_external_lb_device_map
973+
String insertLbMapping = "INSERT INTO `cloud`.`network_external_lb_device_map` (uuid, network_id, external_load_balancer_device_id, created) VALUES ( ?, ?, ?, now())";
974+
pstmtUpdate = conn.prepareStatement(insertLbMapping);
975+
pstmtUpdate.setString(1, UUID.randomUUID().toString());
976+
pstmtUpdate.setLong(2, networkId);
977+
pstmtUpdate.setLong(3, f5DeviceId);
978+
pstmtUpdate.executeUpdate();
979+
s_logger.debug("Successfully added entry in network_external_lb_device_map for network " + networkId + " and F5 device ID " + f5DeviceId);
980+
981+
// add mapping for the network in network_external_firewall_device_map
982+
String insertFwMapping = "INSERT INTO `cloud`.`network_external_firewall_device_map` (uuid, network_id, external_firewall_device_id, created) VALUES ( ?, ?, ?, now())";
983+
pstmtUpdate = conn.prepareStatement(insertFwMapping);
984+
pstmtUpdate.setString(1, UUID.randomUUID().toString());
985+
pstmtUpdate.setLong(2, networkId);
986+
pstmtUpdate.setLong(3, srxDevivceId);
987+
pstmtUpdate.executeUpdate();
988+
s_logger.debug("Successfully added entry in network_external_firewall_device_map for network " + networkId + " and SRX device ID " + srxDevivceId);
989+
}
990+
991+
// update host details for F5 and SRX devices
992+
s_logger.debug("Updating the host details for F5 and SRX devices");
993+
pstmt = conn.prepareStatement("SELECT host_id, name FROM `cloud`.`host_details` WHERE host_id=? OR host_id=?");
994+
pstmt.setLong(1, f5HostId);
995+
pstmt.setLong(2, srxHostId);
996+
rs = pstmt.executeQuery();
997+
while (rs.next()) {
998+
long hostId = rs.getLong(1);
999+
String camlCaseName = rs.getString(2);
1000+
if (!(camlCaseName.equalsIgnoreCase("numRetries") ||
1001+
camlCaseName.equalsIgnoreCase("publicZone") ||
1002+
camlCaseName.equalsIgnoreCase("privateZone") ||
1003+
camlCaseName.equalsIgnoreCase("publicInterface") ||
1004+
camlCaseName.equalsIgnoreCase("privateInterface") ||
1005+
camlCaseName.equalsIgnoreCase("usageInterface") )) {
1006+
continue;
1007+
}
1008+
String lowerCaseName = camlCaseName.toLowerCase();
1009+
pstmt = conn.prepareStatement("update `cloud`.`host_details` set name=? where host_id=? AND name=?");
1010+
pstmt.setString(1, lowerCaseName);
1011+
pstmt.setLong(2, hostId);
1012+
pstmt.setString(3, camlCaseName);
1013+
pstmt.executeUpdate();
1014+
}
1015+
s_logger.debug("Successfully updated host details for F5 and SRX devices");
1016+
} catch (SQLException e) {
1017+
throw new CloudRuntimeException("Unable create a mapping for the networks in network_external_lb_device_map and network_external_firewall_device_map", e);
1018+
} finally {
1019+
try {
1020+
if (rs != null) {
1021+
rs.close();
1022+
}
1023+
if (pstmt != null) {
1024+
pstmt.close();
1025+
}
1026+
} catch (SQLException e) {
1027+
}
1028+
}
1029+
s_logger.info("Successfully upgraded networks using F5 and SRX devices to have a entry in the network_external_lb_device_map and network_external_firewall_device_map");
1030+
}
1031+
}
6841032
}

0 commit comments

Comments
 (0)