diff --git a/server/src/com/cloud/upgrade/dao/Upgrade218to22.java b/server/src/com/cloud/upgrade/dao/Upgrade218to22.java index 49c976c1438..881c7128edb 100644 --- a/server/src/com/cloud/upgrade/dao/Upgrade218to22.java +++ b/server/src/com/cloud/upgrade/dao/Upgrade218to22.java @@ -49,34 +49,29 @@ public class Upgrade218to22 implements DbUpgrade { @Override public File[] getPrepareScripts() { - String file = Script.findScript("","db/schema-21to22.sql"); + String file = Script.findScript("", "db/schema-21to22.sql"); if (file == null) { throw new CloudRuntimeException("Unable to find the upgrade script, schema-21to22.sql"); } - - return new File[] {new File(file)}; + + return new File[] { new File(file) }; } - + protected void upgradeStoragePools(Connection conn) { PreparedStatement pstmt; try { pstmt = conn.prepareStatement("UPDATE storage_pool SET status='Up'"); pstmt.executeUpdate(); pstmt.close(); - } catch(SQLException e) { + } catch (SQLException e) { throw new CloudRuntimeException("Can't upgrade storage pool ", e); } } - - protected long insertNetworkOffering(Connection conn, String name, String displayText, - String trafficType, boolean systemOnly, boolean defaultNetworkOffering, - String availability, boolean dns_service, boolean gateway_service, - boolean firewall_service, boolean lb_service, - boolean userdata_service, boolean vpn_service, - boolean dhcp_service) { - String insertSql = - "INSERT INTO network_offerings (name, display_text, nw_rate, mc_rate, concurrent_connections, traffic_type, tags, system_only, specify_vlan, service_offering_id, created, removed, `default`, availability, dns_service, gateway_service, firewall_service, lb_service, userdata_service, vpn_service, dhcp_service) " + - "VALUES (?, ?, NULL, NULL, NULL, ?, NULL, ?, 0, NULL, now(), NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + + protected long insertNetworkOffering(Connection conn, String name, String displayText, String trafficType, boolean systemOnly, boolean defaultNetworkOffering, String availability, + boolean dns_service, boolean gateway_service, boolean firewall_service, boolean lb_service, boolean userdata_service, boolean vpn_service, boolean dhcp_service) { + String insertSql = "INSERT INTO network_offerings (name, display_text, nw_rate, mc_rate, concurrent_connections, traffic_type, tags, system_only, specify_vlan, service_offering_id, created, removed, `default`, availability, dns_service, gateway_service, firewall_service, lb_service, userdata_service, vpn_service, dhcp_service) " + + "VALUES (?, ?, NULL, NULL, NULL, ?, NULL, ?, 0, NULL, now(), NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; try { PreparedStatement pstmt = conn.prepareStatement(insertSql, Statement.RETURN_GENERATED_KEYS); int i = 1; @@ -102,55 +97,55 @@ public class Upgrade218to22 implements DbUpgrade { return id; } catch (SQLException e) { throw new CloudRuntimeException("Unable to insert network offering ", e); - } + } } - - protected void upgradeInstanceGroups(Connection conn){ + + protected void upgradeInstanceGroups(Connection conn) { try { - - //Create instance groups - duplicated names are allowed across accounts + + // Create instance groups - duplicated names are allowed across accounts PreparedStatement pstmt = conn.prepareStatement("SELECT DISTINCT v.group, v.account_id from vm_instance v where v.group is not null"); ResultSet rs = pstmt.executeQuery(); ArrayList groups = new ArrayList(); while (rs.next()) { Object[] group = new Object[10]; group[0] = rs.getString(1); // group name - group[1] = rs.getLong(2); // accountId + group[1] = rs.getLong(2); // accountId groups.add(group); } rs.close(); pstmt.close(); - + for (Object[] group : groups) { - String groupName = (String)group[0]; - Long accountId = (Long)group[1]; + String groupName = (String) group[0]; + Long accountId = (Long) group[1]; createInstanceGroups(conn, groupName, accountId); - } - - //update instance_group_vm_map + } + + // update instance_group_vm_map pstmt = conn.prepareStatement("SELECT g.id, v.id from vm_instance v, instance_group g where g.name=v.group and g.account_id=v.account_id and v.group is not null"); rs = pstmt.executeQuery(); ArrayList groupVmMaps = new ArrayList(); while (rs.next()) { Object[] groupMaps = new Object[10]; groupMaps[0] = rs.getLong(1); // vmId - groupMaps[1] = rs.getLong(2); // groupId + groupMaps[1] = rs.getLong(2); // groupId groupVmMaps.add(groupMaps); } rs.close(); pstmt.close(); - + for (Object[] groupMap : groupVmMaps) { - Long groupId = (Long)groupMap[0]; - Long instanceId = (Long)groupMap[1]; + Long groupId = (Long) groupMap[0]; + Long instanceId = (Long) groupMap[1]; createInstanceGroupVmMaps(conn, groupId, instanceId); - } + } } catch (SQLException e) { throw new CloudRuntimeException("Can't update instance groups ", e); } - + } - + protected void createInstanceGroups(Connection conn, String groupName, long accountId) throws SQLException { PreparedStatement pstmt = conn.prepareStatement("INSERT INTO instance_group (account_id, name, created) values (?, ?, now()) "); pstmt.setLong(1, accountId); @@ -158,7 +153,7 @@ public class Upgrade218to22 implements DbUpgrade { pstmt.executeUpdate(); pstmt.close(); } - + protected void createInstanceGroupVmMaps(Connection conn, long groupId, long instanceId) throws SQLException { PreparedStatement pstmt = conn.prepareStatement("INSERT INTO instance_group_vm_map (group_id, instance_id) values (?, ?) "); pstmt.setLong(1, groupId); @@ -166,14 +161,17 @@ public class Upgrade218to22 implements DbUpgrade { pstmt.executeUpdate(); pstmt.close(); } - - protected long insertNic(Connection conn, long networkId, long instanceId, boolean running, String macAddress, String ipAddress, String netmask, String strategy, String gateway, String vnet, String guru, boolean defNic, int deviceId, String mode, String reservationId) throws SQLException { - PreparedStatement pstmt = conn.prepareStatement( - "INSERT INTO nics (instance_id, network_id, mac_address, ip4_address, netmask, strategy, ip_type, broadcast_uri, mode, reserver_name, reservation_id, device_id, update_time, isolation_uri, ip6_address, default_nic, created, removed, state, gateway) " + - "VALUES (?, ?, ?, ?, ?, ?, 'Ip4', ?, ?, ?, ?, ?, now(), ?, NULL, ?, now(), NULL, ?, ?)", Statement.RETURN_GENERATED_KEYS); + + protected long insertNic(Connection conn, long networkId, long instanceId, boolean running, String macAddress, String ipAddress, String netmask, String strategy, String gateway, String vnet, + String guru, boolean defNic, int deviceId, String mode, String reservationId) throws SQLException { + PreparedStatement pstmt = conn + .prepareStatement( + "INSERT INTO nics (instance_id, network_id, mac_address, ip4_address, netmask, strategy, ip_type, broadcast_uri, mode, reserver_name, reservation_id, device_id, update_time, isolation_uri, ip6_address, default_nic, created, removed, state, gateway) " + + "VALUES (?, ?, ?, ?, ?, ?, 'Ip4', ?, ?, ?, ?, ?, now(), ?, NULL, ?, now(), NULL, ?, ?)", + Statement.RETURN_GENERATED_KEYS); int i = 1; String isolationUri = null; - + String broadcast = null; if (vnet != null) { broadcast = "vlan://" + vnet; @@ -209,17 +207,18 @@ public class Upgrade218to22 implements DbUpgrade { pstmt.close(); return nicId; } - + protected void upgradeDomR(Connection conn, long dcId, long domrId, Long publicNetworkId, long guestNetworkId, long controlNetworkId, String zoneType) throws SQLException { s_logger.debug("Upgrading domR" + domrId); - PreparedStatement pstmt = conn.prepareStatement("SELECT vm_instance.id, vm_instance.state, vm_instance.private_mac_address, vm_instance.private_ip_address, vm_instance.private_netmask, domain_router.public_mac_address, domain_router.public_ip_address, domain_router.public_netmask, domain_router.guest_mac_address, domain_router.guest_ip_address, domain_router.guest_netmask, domain_router.vnet, domain_router.gateway, vm_instance.type FROM vm_instance INNER JOIN domain_router ON vm_instance.id=domain_router.id WHERE vm_instance.removed is NULL AND vm_instance.id=?"); + PreparedStatement pstmt = conn + .prepareStatement("SELECT vm_instance.id, vm_instance.state, vm_instance.private_mac_address, vm_instance.private_ip_address, vm_instance.private_netmask, domain_router.public_mac_address, domain_router.public_ip_address, domain_router.public_netmask, domain_router.guest_mac_address, domain_router.guest_ip_address, domain_router.guest_netmask, domain_router.vnet, domain_router.gateway, vm_instance.type FROM vm_instance INNER JOIN domain_router ON vm_instance.id=domain_router.id WHERE vm_instance.removed is NULL AND vm_instance.id=?"); pstmt.setLong(1, domrId); ResultSet rs = pstmt.executeQuery(); - + if (!rs.next()) { throw new CloudRuntimeException("Unable to find router " + domrId); } - + long id = rs.getLong(1); String state = rs.getString(2); boolean running = state.equals("Running") | state.equals("Starting") | state.equals("Stopping"); @@ -237,19 +236,19 @@ public class Upgrade218to22 implements DbUpgrade { String type = rs.getString(14); rs.close(); pstmt.close(); - - + pstmt = conn.prepareStatement("SELECT v.vlan_id from vlan v, user_ip_address u where v.id=u.vlan_db_id and u.public_ip_address=?"); pstmt.setString(1, publicIp); rs = pstmt.executeQuery(); - + String publicVlan = null; while (rs.next()) { publicVlan = rs.getString(1); } - + if (zoneType.equalsIgnoreCase("Basic")) { - long controlNicId = insertNic(conn, controlNetworkId, domrId, running, privateMac, privateIp, privateNetmask, "Start", "169.254.0.1", null, "ControlNetworkGuru", false, 1, "Static", privateIp != null ? (domrId + privateIp) : null); + long controlNicId = insertNic(conn, controlNetworkId, domrId, running, privateMac, privateIp, privateNetmask, "Start", "169.254.0.1", null, "ControlNetworkGuru", false, 1, "Static", + privateIp != null ? (domrId + privateIp) : null); if (privateIp != null) { pstmt = conn.prepareStatement("UPDATE op_dc_link_local_ip_address_alloc SET instance_id=? WHERE ip_address=? AND data_center_id=?"); pstmt.setLong(1, controlNicId); @@ -258,11 +257,12 @@ public class Upgrade218to22 implements DbUpgrade { pstmt.executeUpdate(); pstmt.close(); } - + insertNic(conn, guestNetworkId, domrId, running, guestMac, guestIp, guestNetmask, "Start", gateway, vnet, "DirectPodBasedNetworkGuru", false, 0, "Static", null); } else { insertNic(conn, publicNetworkId, domrId, running, publicMac, publicIp, publicNetmask, "Create", gateway, publicVlan, "PublicNetworkGuru", true, 2, "Static", null); - long controlNicId = insertNic(conn, controlNetworkId, domrId, running, privateMac, privateIp, privateNetmask, "Start", "169.254.0.1", null, "ControlNetworkGuru", false, 1, "Static", privateIp != null ? (domrId + privateIp) : null); + long controlNicId = insertNic(conn, controlNetworkId, domrId, running, privateMac, privateIp, privateNetmask, "Start", "169.254.0.1", null, "ControlNetworkGuru", false, 1, "Static", + privateIp != null ? (domrId + privateIp) : null); if (privateIp != null) { pstmt = conn.prepareStatement("UPDATE op_dc_link_local_ip_address_alloc SET instance_id=? WHERE ip_address=? AND data_center_id=?"); pstmt.setLong(1, controlNicId); @@ -273,21 +273,21 @@ public class Upgrade218to22 implements DbUpgrade { } insertNic(conn, guestNetworkId, domrId, running, guestMac, guestIp, guestNetmask, "Start", null, vnet, "ExternalGuestNetworkGuru", false, 0, "Static", null); } - - + } - + protected void upgradeSsvm(Connection conn, long dataCenterId, long publicNetworkId, long managementNetworkId, long controlNetworkId, String zoneType) throws SQLException { s_logger.debug("Upgrading ssvm in " + dataCenterId); - PreparedStatement pstmt = conn.prepareStatement("SELECT vm_instance.id, vm_instance.state, vm_instance.private_mac_address, vm_instance.private_ip_address, vm_instance.private_netmask, secondary_storage_vm.public_mac_address, secondary_storage_vm.public_ip_address, secondary_storage_vm.public_netmask, secondary_storage_vm.guest_mac_address, secondary_storage_vm.guest_ip_address, secondary_storage_vm.guest_netmask, secondary_storage_vm.gateway, vm_instance.type FROM vm_instance INNER JOIN secondary_storage_vm ON vm_instance.id=secondary_storage_vm.id WHERE vm_instance.removed is NULL AND vm_instance.data_center_id=? AND vm_instance.type='SecondaryStorageVm'"); + PreparedStatement pstmt = conn + .prepareStatement("SELECT vm_instance.id, vm_instance.state, vm_instance.private_mac_address, vm_instance.private_ip_address, vm_instance.private_netmask, secondary_storage_vm.public_mac_address, secondary_storage_vm.public_ip_address, secondary_storage_vm.public_netmask, secondary_storage_vm.guest_mac_address, secondary_storage_vm.guest_ip_address, secondary_storage_vm.guest_netmask, secondary_storage_vm.gateway, vm_instance.type FROM vm_instance INNER JOIN secondary_storage_vm ON vm_instance.id=secondary_storage_vm.id WHERE vm_instance.removed is NULL AND vm_instance.data_center_id=? AND vm_instance.type='SecondaryStorageVm'"); pstmt.setLong(1, dataCenterId); ResultSet rs = pstmt.executeQuery(); - + if (!rs.next()) { s_logger.debug("Unable to find ssvm in data center " + dataCenterId); return; } - + long ssvmId = rs.getLong(1); String state = rs.getString(2); boolean running = state.equals("Running") | state.equals("Starting") | state.equals("Stopping"); @@ -304,43 +304,44 @@ public class Upgrade218to22 implements DbUpgrade { String type = rs.getString(13); rs.close(); pstmt.close(); - - pstmt = conn.prepareStatement("SELECT host_pod_ref.gateway from host_pod_ref INNER JOIN vm_instance ON vm_instance.pod_id=host_pod_ref.id WHERE vm_instance.removed is NULL AND vm_instance.data_center_id=? AND vm_instance.type='SecondaryStorageVm'"); + + pstmt = conn + .prepareStatement("SELECT host_pod_ref.gateway from host_pod_ref INNER JOIN vm_instance ON vm_instance.pod_id=host_pod_ref.id WHERE vm_instance.removed is NULL AND vm_instance.data_center_id=? AND vm_instance.type='SecondaryStorageVm'"); pstmt.setLong(1, dataCenterId); rs = pstmt.executeQuery(); - + if (!rs.next()) { s_logger.debug("Unable to find ssvm in data center " + dataCenterId); return; } - + String podGateway = rs.getString(1); rs.close(); pstmt.close(); - + s_logger.debug("Gateway is " + podGateway); - + pstmt = conn.prepareStatement("SELECT v.vlan_id from vlan v, user_ip_address u where v.id=u.vlan_db_id and u.public_ip_address=?"); pstmt.setString(1, publicIp); rs = pstmt.executeQuery(); - + String publicVlan = null; while (rs.next()) { publicVlan = rs.getString(1); } - + rs.close(); pstmt.close(); - - + if (zoneType.equalsIgnoreCase("Basic")) { insertNic(conn, publicNetworkId, ssvmId, running, publicMac, publicIp, publicNetmask, "Start", gateway, publicVlan, "DirectPodBasedNetworkGuru", true, 2, "Static", null); } else { insertNic(conn, publicNetworkId, ssvmId, running, publicMac, publicIp, publicNetmask, "Create", gateway, publicVlan, "PublicNetworkGuru", true, 2, "Static", null); } - - long controlNicId = insertNic(conn, controlNetworkId, ssvmId, running, guestMac, guestIp, guestNetmask, "Start", "169.254.0.1", null, "ControlNetworkGuru", false, 0, "Static", guestIp != null ? (ssvmId + guestIp) : null); + + long controlNicId = insertNic(conn, controlNetworkId, ssvmId, running, guestMac, guestIp, guestNetmask, "Start", "169.254.0.1", null, "ControlNetworkGuru", false, 0, "Static", + guestIp != null ? (ssvmId + guestIp) : null); if (guestIp != null) { pstmt = conn.prepareStatement("UPDATE op_dc_link_local_ip_address_alloc SET instance_id=? WHERE ip_address=? AND data_center_id=?"); pstmt.setLong(1, controlNicId); @@ -349,7 +350,7 @@ public class Upgrade218to22 implements DbUpgrade { pstmt.executeUpdate(); pstmt.close(); } - + long mgmtNicId = insertNic(conn, managementNetworkId, ssvmId, running, privateMac, privateIp, privateNetmask, "Start", podGateway, null, "PodBasedNetworkGuru", false, 1, "Static", null); if (privateIp != null) { pstmt = conn.prepareStatement("UPDATE op_dc_ip_address_alloc SET instance_id=? WHERE ip_address=? AND data_center_id=?"); @@ -360,17 +361,18 @@ public class Upgrade218to22 implements DbUpgrade { pstmt.close(); } } - + protected void upgradeConsoleProxy(Connection conn, long dcId, long cpId, long publicNetworkId, long managementNetworkId, long controlNetworkId, String zoneType) throws SQLException { s_logger.debug("Upgrading cp" + cpId); - PreparedStatement pstmt = conn.prepareStatement("SELECT vm_instance.id, vm_instance.state, vm_instance.private_mac_address, vm_instance.private_ip_address, vm_instance.private_netmask, console_proxy.public_mac_address, console_proxy.public_ip_address, console_proxy.public_netmask, console_proxy.guest_mac_address, console_proxy.guest_ip_address, console_proxy.guest_netmask, console_proxy.gateway, vm_instance.type FROM vm_instance INNER JOIN console_proxy ON vm_instance.id=console_proxy.id WHERE vm_instance.removed is NULL AND vm_instance.id=?"); + PreparedStatement pstmt = conn + .prepareStatement("SELECT vm_instance.id, vm_instance.state, vm_instance.private_mac_address, vm_instance.private_ip_address, vm_instance.private_netmask, console_proxy.public_mac_address, console_proxy.public_ip_address, console_proxy.public_netmask, console_proxy.guest_mac_address, console_proxy.guest_ip_address, console_proxy.guest_netmask, console_proxy.gateway, vm_instance.type FROM vm_instance INNER JOIN console_proxy ON vm_instance.id=console_proxy.id WHERE vm_instance.removed is NULL AND vm_instance.id=?"); pstmt.setLong(1, cpId); ResultSet rs = pstmt.executeQuery(); - + if (!rs.next()) { throw new CloudRuntimeException("Unable to find cp " + cpId); } - + long id = rs.getLong(1); String state = rs.getString(2); boolean running = state.equals("Running") | state.equals("Starting") | state.equals("Stopping"); @@ -387,38 +389,39 @@ public class Upgrade218to22 implements DbUpgrade { String type = rs.getString(13); rs.close(); pstmt.close(); - + pstmt = conn.prepareStatement("SELECT host_pod_ref.gateway from host_pod_ref INNER JOIN vm_instance ON vm_instance.pod_id=host_pod_ref.id WHERE vm_instance.id=?"); pstmt.setLong(1, cpId); rs = pstmt.executeQuery(); - + if (!rs.next()) { throw new CloudRuntimeException("Unable to find cp " + cpId); } - + String podGateway = rs.getString(1); rs.close(); pstmt.close(); - + pstmt = conn.prepareStatement("SELECT v.vlan_id from vlan v, user_ip_address u where v.id=u.vlan_db_id and u.public_ip_address=?"); pstmt.setString(1, publicIp); rs = pstmt.executeQuery(); - + String publicVlan = null; while (rs.next()) { publicVlan = rs.getString(1); } - + rs.close(); pstmt.close(); - + if (zoneType.equalsIgnoreCase("Basic")) { insertNic(conn, publicNetworkId, cpId, running, publicMac, publicIp, publicNetmask, "Start", gateway, publicVlan, "DirectPodBasedNetworkGuru", true, 2, "Static", null); } else { insertNic(conn, publicNetworkId, cpId, running, publicMac, publicIp, publicNetmask, "Create", gateway, publicVlan, "PublicNetworkGuru", true, 2, "Static", null); } - - long controlNicId = insertNic(conn, controlNetworkId, cpId, running, guestMac, guestIp, guestNetmask, "Start", "169.254.0.1", null, "ControlNetworkGuru", false, 0, "Static", guestIp != null ? (cpId + guestIp) : null); + + long controlNicId = insertNic(conn, controlNetworkId, cpId, running, guestMac, guestIp, guestNetmask, "Start", "169.254.0.1", null, "ControlNetworkGuru", false, 0, "Static", + guestIp != null ? (cpId + guestIp) : null); if (guestIp != null) { pstmt = conn.prepareStatement("UPDATE op_dc_link_local_ip_address_alloc SET instance_id=? WHERE ip_address=? AND data_center_id=?"); pstmt.setLong(1, controlNicId); @@ -427,7 +430,8 @@ public class Upgrade218to22 implements DbUpgrade { pstmt.executeUpdate(); pstmt.close(); } - long mgmtNicId = insertNic(conn, managementNetworkId, cpId, running, privateMac, privateIp, privateNetmask, "Start", podGateway, null, "PodBasedNetworkGuru", false, 1, "Static", privateIp != null ? (cpId + privateIp) : null); + long mgmtNicId = insertNic(conn, managementNetworkId, cpId, running, privateMac, privateIp, privateNetmask, "Start", podGateway, null, "PodBasedNetworkGuru", false, 1, "Static", + privateIp != null ? (cpId + privateIp) : null); if (privateIp != null) { pstmt = conn.prepareStatement("UPDATE op_dc_ip_address_alloc SET instance_id=? WHERE ip_address=? AND data_center_id=?"); pstmt.setLong(1, mgmtNicId); @@ -437,9 +441,10 @@ public class Upgrade218to22 implements DbUpgrade { pstmt.close(); } } - + protected void upgradeVirtualUserVms(Connection conn, long domainRouterId, long networkId, String gateway, String vnet) throws SQLException { - PreparedStatement pstmt = conn.prepareStatement("SELECT vm_instance.id, vm_instance.private_mac_address, vm_instance.private_ip_address, vm_instance.private_netmask, vm_instance.state, vm_instance.type FROM vm_instance INNER JOIN user_vm ON vm_instance.id=user_vm.id WHERE user_vm.domain_router_id=? and vm_instance.removed IS NULL"); + PreparedStatement pstmt = conn + .prepareStatement("SELECT vm_instance.id, vm_instance.private_mac_address, vm_instance.private_ip_address, vm_instance.private_netmask, vm_instance.state, vm_instance.type FROM vm_instance INNER JOIN user_vm ON vm_instance.id=user_vm.id WHERE user_vm.domain_router_id=? and vm_instance.removed IS NULL"); pstmt.setLong(1, domainRouterId); ResultSet rs = pstmt.executeQuery(); List vms = new ArrayList(); @@ -449,68 +454,66 @@ public class Upgrade218to22 implements DbUpgrade { vm[1] = rs.getString(2); // mac address vm[2] = rs.getString(3); // ip address vm[3] = rs.getString(4); // netmask - vm[4] = rs.getString(5); // vm state + vm[4] = rs.getString(5); // vm state vms.add(vm); } rs.close(); pstmt.close(); - + s_logger.debug("Upgrading " + vms.size() + " vms for router " + domainRouterId); - + for (Object[] vm : vms) { - String state = (String)vm[4]; - + String state = (String) vm[4]; + boolean running = false; if (state.equals("Running") || state.equals("Starting") || state.equals("Stopping")) { running = true; } - - insertNic(conn, networkId, (Long)vm[0], running, (String)vm[1], (String)vm[2], (String)vm[3], "Start", gateway, vnet, "ExternalGuestNetworkGuru", true, 0, "Dhcp", null); - } - } - - - protected void upgradeBasicUserVms(Connection conn, long domainRouterId, long networkId, String gateway, String vnet) throws SQLException { - PreparedStatement pstmt = conn.prepareStatement("SELECT vm_instance.id, vm_instance.private_mac_address, vm_instance.private_ip_address, vm_instance.private_netmask, vm_instance.state, vm_instance.type FROM vm_instance INNER JOIN user_vm ON vm_instance.id=user_vm.id WHERE user_vm.domain_router_id=? and vm_instance.removed IS NULL"); - pstmt.setLong(1, domainRouterId); - ResultSet rs = pstmt.executeQuery(); - List vms = new ArrayList(); - while (rs.next()) { - Object[] vm = new Object[10]; - vm[0] = rs.getLong(1); // vm id - vm[1] = rs.getString(2); // mac address - vm[2] = rs.getString(3); // ip address - vm[3] = rs.getString(4); // netmask - vm[4] = rs.getString(5); // vm state - vm[5] = rs.getString(6); // vm type - vms.add(vm); - } - rs.close(); - pstmt.close(); - - s_logger.debug("Upgrading " + vms.size() + " vms for router " + domainRouterId); - - for (Object[] vm : vms) { - String state = (String)vm[4]; - - boolean running = false; - if (state.equals("Running") || state.equals("Starting") || state.equals("Stopping")) { - running = true; - } - insertNic(conn, networkId, (Long)vm[0], running, (String)vm[1], (String)vm[2], (String)vm[3], "Start", gateway, vnet, "DirectPodBasedNetworkGuru", true, 0, "Dhcp", null); - + + insertNic(conn, networkId, (Long) vm[0], running, (String) vm[1], (String) vm[2], (String) vm[3], "Start", gateway, vnet, "ExternalGuestNetworkGuru", true, 0, "Dhcp", null); } } - - protected long insertNetwork(Connection conn, String name, String displayText, String trafficType, String broadcastDomainType, String broadcastUri, - String gateway, String cidr, String mode, long networkOfferingId, long dataCenterId, String guruName, - String state, long domainId, long accountId, String dns1, String dns2, String guestType, boolean shared, - String networkDomain, boolean isDefault, String reservationId) { + protected void upgradeBasicUserVms(Connection conn, long domainRouterId, long networkId, String gateway, String vnet) throws SQLException { + PreparedStatement pstmt = conn + .prepareStatement("SELECT vm_instance.id, vm_instance.private_mac_address, vm_instance.private_ip_address, vm_instance.private_netmask, vm_instance.state, vm_instance.type FROM vm_instance INNER JOIN user_vm ON vm_instance.id=user_vm.id WHERE user_vm.domain_router_id=? and vm_instance.removed IS NULL"); + pstmt.setLong(1, domainRouterId); + ResultSet rs = pstmt.executeQuery(); + List vms = new ArrayList(); + while (rs.next()) { + Object[] vm = new Object[10]; + vm[0] = rs.getLong(1); // vm id + vm[1] = rs.getString(2); // mac address + vm[2] = rs.getString(3); // ip address + vm[3] = rs.getString(4); // netmask + vm[4] = rs.getString(5); // vm state + vm[5] = rs.getString(6); // vm type + vms.add(vm); + } + rs.close(); + pstmt.close(); + + s_logger.debug("Upgrading " + vms.size() + " vms for router " + domainRouterId); + + for (Object[] vm : vms) { + String state = (String) vm[4]; + + boolean running = false; + if (state.equals("Running") || state.equals("Starting") || state.equals("Stopping")) { + running = true; + } + insertNic(conn, networkId, (Long) vm[0], running, (String) vm[1], (String) vm[2], (String) vm[3], "Start", gateway, vnet, "DirectPodBasedNetworkGuru", true, 0, "Dhcp", null); + + } + } + + protected long insertNetwork(Connection conn, String name, String displayText, String trafficType, String broadcastDomainType, String broadcastUri, String gateway, String cidr, String mode, + long networkOfferingId, long dataCenterId, String guruName, String state, long domainId, long accountId, String dns1, String dns2, String guestType, boolean shared, String networkDomain, + boolean isDefault, String reservationId) { String getNextNetworkSequenceSql = "SELECT value from sequence where name='networks_seq'"; String advanceNetworkSequenceSql = "UPDATE sequence set value=value+1 where name='networks_seq'"; - String insertNetworkSql = "INSERT INTO networks(id, name, display_text, traffic_type, broadcast_domain_type, gateway, cidr, mode, network_offering_id, data_center_id, guru_name, state, domain_id, account_id, dns1, dns2, guest_type, shared, is_default, created, network_domain, related, reservation_id, broadcast_uri) " + - "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), ?, ?, ?, ?)"; + String insertNetworkSql = "INSERT INTO networks(id, name, display_text, traffic_type, broadcast_domain_type, gateway, cidr, mode, network_offering_id, data_center_id, guru_name, state, domain_id, account_id, dns1, dns2, guest_type, shared, is_default, created, network_domain, related, reservation_id, broadcast_uri) " + + "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), ?, ?, ?, ?)"; try { PreparedStatement pstmt = conn.prepareStatement(getNextNetworkSequenceSql); ResultSet rs = pstmt.executeQuery(); @@ -518,11 +521,11 @@ public class Upgrade218to22 implements DbUpgrade { long seq = rs.getLong(1); rs.close(); pstmt.close(); - + pstmt = conn.prepareStatement(advanceNetworkSequenceSql); pstmt.executeUpdate(); pstmt.close(); - + pstmt = conn.prepareStatement(insertNetworkSql); int i = 1; pstmt.setLong(i++, seq); @@ -549,18 +552,18 @@ public class Upgrade218to22 implements DbUpgrade { pstmt.setString(i++, reservationId); pstmt.setString(i++, broadcastUri); pstmt.executeUpdate(); - + pstmt = conn.prepareStatement("INSERT INTO account_network_ref (account_id, network_id, is_owner) VALUES (?, ?, 1)"); pstmt.setLong(1, accountId); pstmt.setLong(2, seq); pstmt.executeUpdate(); - + return seq; } catch (SQLException e) { throw new CloudRuntimeException("Unable to create network", e); } } - + protected void upgradeManagementIpAddress(Connection conn, long dcId) throws SQLException { PreparedStatement pstmt = conn.prepareStatement("SELECT op_dc_ip_address_alloc.id FROM op_dc_ip_address_alloc WHERE data_center_id=?"); pstmt.setLong(1, dcId); @@ -568,12 +571,12 @@ public class Upgrade218to22 implements DbUpgrade { ArrayList allocatedIps = new ArrayList(); while (rs.next()) { Object[] ip = new Object[10]; - ip[0] = rs.getLong(1); // id + ip[0] = rs.getLong(1); // id allocatedIps.add(ip); } rs.close(); pstmt.close(); - + for (Object[] allocatedIp : allocatedIps) { pstmt = conn.prepareStatement("SELECT mac_address FROM data_center WHERE id = ?"); pstmt.setLong(1, dcId); @@ -584,37 +587,39 @@ public class Upgrade218to22 implements DbUpgrade { long mac = rs.getLong(1); rs.close(); pstmt.close(); - + pstmt = conn.prepareStatement("UPDATE data_center SET mac_address=mac_address+1 WHERE id = ?"); pstmt.setLong(1, dcId); pstmt.executeUpdate(); pstmt.close(); - + pstmt = conn.prepareStatement("UPDATE op_dc_ip_address_alloc SET mac_address=? WHERE id=?"); pstmt.setLong(1, mac); - pstmt.setLong(2, (Long)allocatedIp[0]); + pstmt.setLong(2, (Long) allocatedIp[0]); pstmt.executeUpdate(); pstmt.close(); } } - + protected void upgradeDirectUserIpAddress(Connection conn, long dcId, long networkId, String vlanType) throws SQLException { s_logger.debug("Upgrading user ip address for data center " + dcId + " network " + networkId + " vlan type " + vlanType); - PreparedStatement pstmt = conn.prepareStatement("UPDATE user_ip_address INNER JOIN vlan ON user_ip_address.vlan_db_id=vlan.id SET user_ip_address.source_network_id=vlan.network_id WHERE user_ip_address.data_center_id=? AND vlan.vlan_type=?"); + PreparedStatement pstmt = conn + .prepareStatement("UPDATE user_ip_address INNER JOIN vlan ON user_ip_address.vlan_db_id=vlan.id SET user_ip_address.source_network_id=vlan.network_id WHERE user_ip_address.data_center_id=? AND vlan.vlan_type=?"); pstmt.setLong(1, dcId); pstmt.setString(2, vlanType); pstmt.executeUpdate(); pstmt.close(); - - pstmt = conn.prepareStatement("SELECT user_ip_address.id, user_ip_address.public_ip_address, user_ip_address.account_id, user_ip_address.allocated FROM user_ip_address INNER JOIN vlan ON vlan.id=user_ip_address.vlan_db_id WHERE user_ip_address.data_center_id = ? AND vlan.vlan_type=?"); + + pstmt = conn + .prepareStatement("SELECT user_ip_address.id, user_ip_address.public_ip_address, user_ip_address.account_id, user_ip_address.allocated FROM user_ip_address INNER JOIN vlan ON vlan.id=user_ip_address.vlan_db_id WHERE user_ip_address.data_center_id = ? AND vlan.vlan_type=?"); pstmt.setLong(1, dcId); pstmt.setString(2, vlanType); ResultSet rs = pstmt.executeQuery(); ArrayList allocatedIps = new ArrayList(); while (rs.next()) { Object[] ip = new Object[10]; - ip[0] = rs.getLong(1); // id + ip[0] = rs.getLong(1); // id ip[1] = rs.getString(2); // ip address ip[2] = rs.getLong(3); // account id ip[3] = rs.getDate(4); // allocated @@ -622,9 +627,9 @@ public class Upgrade218to22 implements DbUpgrade { } rs.close(); pstmt.close(); - + s_logger.debug("Marking " + allocatedIps.size() + " ip addresses to belong to network " + networkId); - + for (Object[] allocatedIp : allocatedIps) { s_logger.debug("Updating mac addresses for data center id=" + dcId); pstmt = conn.prepareStatement("SELECT mac_address FROM data_center WHERE id = ?"); @@ -636,43 +641,45 @@ public class Upgrade218to22 implements DbUpgrade { long mac = rs.getLong(1); rs.close(); pstmt.close(); - + pstmt = conn.prepareStatement("UPDATE data_center SET mac_address=mac_address+1 WHERE id = ?"); pstmt.setLong(1, dcId); pstmt.executeUpdate(); pstmt.close(); - + pstmt = conn.prepareStatement("UPDATE user_ip_address SET mac_address=? WHERE id=?"); pstmt.setLong(1, mac); - pstmt.setLong(2, (Long)allocatedIp[0]); + pstmt.setLong(2, (Long) allocatedIp[0]); pstmt.executeUpdate(); pstmt.close(); } } - + protected void upgradePublicUserIpAddress(Connection conn, long dcId, long networkId, String vlanType) throws SQLException { s_logger.debug("Upgrading user ip address for data center " + dcId + " network " + networkId + " vlan type " + vlanType); - PreparedStatement pstmt = conn.prepareStatement("UPDATE user_ip_address INNER JOIN vlan ON user_ip_address.vlan_db_id=vlan.id SET source_network_id=? WHERE user_ip_address.data_center_id=? AND vlan.vlan_type=?"); + PreparedStatement pstmt = conn + .prepareStatement("UPDATE user_ip_address INNER JOIN vlan ON user_ip_address.vlan_db_id=vlan.id SET source_network_id=? WHERE user_ip_address.data_center_id=? AND vlan.vlan_type=?"); pstmt.setLong(1, networkId); pstmt.setLong(2, dcId); pstmt.setString(3, vlanType); pstmt.executeUpdate(); pstmt.close(); - + pstmt = conn.prepareStatement("UPDATE vlan SET network_id = ? WHERE data_center_id=? AND vlan_type=?"); pstmt.setLong(1, networkId); pstmt.setLong(2, dcId); pstmt.setString(3, vlanType); pstmt.executeUpdate(); pstmt.close(); - - pstmt = conn.prepareStatement("SELECT user_ip_address.id, user_ip_address.public_ip_address, user_ip_address.account_id, user_ip_address.allocated FROM user_ip_address INNER JOIN vlan ON vlan.id=user_ip_address.vlan_db_id WHERE user_ip_address.data_center_id = ? AND vlan.vlan_type='VirtualNetwork'"); + + pstmt = conn + .prepareStatement("SELECT user_ip_address.id, user_ip_address.public_ip_address, user_ip_address.account_id, user_ip_address.allocated FROM user_ip_address INNER JOIN vlan ON vlan.id=user_ip_address.vlan_db_id WHERE user_ip_address.data_center_id = ? AND vlan.vlan_type='VirtualNetwork'"); pstmt.setLong(1, dcId); ResultSet rs = pstmt.executeQuery(); ArrayList allocatedIps = new ArrayList(); while (rs.next()) { Object[] ip = new Object[10]; - ip[0] = rs.getLong(1); // id + ip[0] = rs.getLong(1); // id ip[1] = rs.getString(2); // ip address ip[2] = rs.getLong(3); // account id ip[3] = rs.getDate(4); // allocated @@ -680,7 +687,7 @@ public class Upgrade218to22 implements DbUpgrade { } rs.close(); pstmt.close(); - + for (Object[] allocatedIp : allocatedIps) { pstmt = conn.prepareStatement("SELECT mac_address FROM data_center WHERE id = ?"); pstmt.setLong(1, dcId); @@ -691,17 +698,17 @@ public class Upgrade218to22 implements DbUpgrade { long mac = rs.getLong(1); rs.close(); pstmt.close(); - + pstmt = conn.prepareStatement("UPDATE data_center SET mac_address=mac_address+1 WHERE id = ?"); pstmt.setLong(1, dcId); pstmt.executeUpdate(); pstmt.close(); - + Long associatedNetworkId = null; if (allocatedIp[3] != null && allocatedIp[2] != null) { pstmt = conn.prepareStatement("SELECT id FROM networks WHERE data_center_id=? AND account_id=?"); pstmt.setLong(1, dcId); - pstmt.setLong(2, (Long)allocatedIp[2]); + pstmt.setLong(2, (Long) allocatedIp[2]); rs = pstmt.executeQuery(); if (!rs.next()) { throw new CloudRuntimeException("Unable to find a network for account " + allocatedIp[2] + " in dc " + dcId); @@ -709,7 +716,7 @@ public class Upgrade218to22 implements DbUpgrade { associatedNetworkId = rs.getLong(1); rs.close(); pstmt.close(); - } + } pstmt = conn.prepareStatement("UPDATE user_ip_address SET mac_address=?, network_id=? WHERE id=?"); pstmt.setLong(1, mac); if (associatedNetworkId != null) { @@ -717,13 +724,13 @@ public class Upgrade218to22 implements DbUpgrade { } else { pstmt.setObject(2, null); } - pstmt.setLong(3, (Long)allocatedIp[0]); + pstmt.setLong(3, (Long) allocatedIp[0]); pstmt.executeUpdate(); pstmt.close(); } } - + protected void upgradeDataCenter(Connection conn) { PreparedStatement pstmt; try { @@ -732,7 +739,8 @@ public class Upgrade218to22 implements DbUpgrade { _basicZone = !rs.next() || Boolean.parseBoolean(rs.getString(1)); rs.close(); pstmt.close(); - pstmt = conn.prepareStatement("UPDATE data_center SET networktype=?, dns_provider=?, gateway_provider=?, firewall_provider=?, dhcp_provider=?, lb_provider=?, vpn_provider=?, userdata_provider=?"); + pstmt = conn + .prepareStatement("UPDATE data_center SET networktype=?, dns_provider=?, gateway_provider=?, firewall_provider=?, dhcp_provider=?, lb_provider=?, vpn_provider=?, userdata_provider=?"); if (_basicZone) { pstmt.setString(1, "Basic"); pstmt.setString(2, "DhcpServer"); @@ -754,9 +762,9 @@ public class Upgrade218to22 implements DbUpgrade { } pstmt.executeUpdate(); pstmt.close(); - - //For basic zone vnet field should be NULL - + + // For basic zone vnet field should be NULL + if (_basicZone) { pstmt = conn.prepareStatement("UPDATE data_center SET vnet=?, guest_network_cidr=?"); pstmt.setString(1, null); @@ -764,20 +772,20 @@ public class Upgrade218to22 implements DbUpgrade { pstmt.executeUpdate(); pstmt.close(); } - + pstmt = conn.prepareStatement("SELECT id, guest_network_cidr, domain FROM data_center"); rs = pstmt.executeQuery(); ArrayList dcs = new ArrayList(); while (rs.next()) { Object[] dc = new Object[10]; - dc[0] = rs.getLong(1); // data center id - dc[1] = rs.getString(2); // guest network cidr - dc[2] = rs.getString(3); // network domain + dc[0] = rs.getLong(1); // data center id + dc[1] = rs.getString(2); // guest network cidr + dc[2] = rs.getString(3); // network domain dcs.add(dc); } rs.close(); pstmt.close(); - + pstmt = conn.prepareStatement("SELECT id FROM network_offerings WHERE name='System-Management-Network'"); rs = pstmt.executeQuery(); if (!rs.next()) { @@ -786,7 +794,7 @@ public class Upgrade218to22 implements DbUpgrade { long managementNetworkOfferingId = rs.getLong(1); rs.close(); pstmt.close(); - + pstmt = conn.prepareStatement("SELECT id FROM network_offerings WHERE name='System-Public-Network'"); rs = pstmt.executeQuery(); if (!rs.next()) { @@ -795,7 +803,7 @@ public class Upgrade218to22 implements DbUpgrade { long publicNetworkOfferingId = rs.getLong(1); rs.close(); pstmt.close(); - + pstmt = conn.prepareStatement("SELECT id FROM network_offerings WHERE name='System-Control-Network'"); rs = pstmt.executeQuery(); if (!rs.next()) { @@ -804,7 +812,7 @@ public class Upgrade218to22 implements DbUpgrade { long controlNetworkOfferingId = rs.getLong(1); rs.close(); pstmt.close(); - + pstmt = conn.prepareStatement("SELECT id FROM network_offerings WHERE name='System-Storage-Network'"); rs = pstmt.executeQuery(); if (!rs.next()) { @@ -813,34 +821,39 @@ public class Upgrade218to22 implements DbUpgrade { long storageNetworkOfferingId = rs.getLong(1); rs.close(); pstmt.close(); - + if (_basicZone) { for (Object[] dc : dcs) { - Long dcId = (Long)dc[0]; - long mgmtNetworkId = insertNetwork(conn, "ManagementNetwork" + dcId, "Management Network created for Zone " + dcId, "Management", "Native", null, null, null, "Static", managementNetworkOfferingId, dcId, "PodBasedNetworkGuru", "Setup", 1, 1, null, null, null, true, null, false, null); - long storageNetworkId = insertNetwork(conn, "StorageNetwork" + dcId, "Storage Network created for Zone " + dcId, "Storage", "Native", null, null, null, "Static", storageNetworkOfferingId, dcId, "PodBasedNetworkGuru", "Setup", 1, 1, null, null, null, true, null, false, null); - long controlNetworkId = insertNetwork(conn, "ControlNetwork" + dcId, "Control Network created for Zone " + dcId, "Control", "LinkLocal", null, NetUtils.getLinkLocalGateway(), NetUtils.getLinkLocalCIDR(), "Static", controlNetworkOfferingId, dcId, "ControlNetworkGuru", "Setup", 1, 1, null, null, null, true, null, false, null); + Long dcId = (Long) dc[0]; + long mgmtNetworkId = insertNetwork(conn, "ManagementNetwork" + dcId, "Management Network created for Zone " + dcId, "Management", "Native", null, null, null, "Static", + managementNetworkOfferingId, dcId, "PodBasedNetworkGuru", "Setup", 1, 1, null, null, null, true, null, false, null); + long storageNetworkId = insertNetwork(conn, "StorageNetwork" + dcId, "Storage Network created for Zone " + dcId, "Storage", "Native", null, null, null, "Static", + storageNetworkOfferingId, dcId, "PodBasedNetworkGuru", "Setup", 1, 1, null, null, null, true, null, false, null); + long controlNetworkId = insertNetwork(conn, "ControlNetwork" + dcId, "Control Network created for Zone " + dcId, "Control", "LinkLocal", null, NetUtils.getLinkLocalGateway(), + NetUtils.getLinkLocalCIDR(), "Static", controlNetworkOfferingId, dcId, "ControlNetworkGuru", "Setup", 1, 1, null, null, null, true, null, false, null); upgradeManagementIpAddress(conn, dcId); - long basicDefaultDirectNetworkId = insertNetwork(conn, "BasicZoneDirectNetwork" + dcId, "Basic Zone Direct Network created for Zone " + dcId, "Guest", "Native", null, null, null, "Dhcp", 5, dcId, "DirectPodBasedNetworkGuru", "Setup", 1, 1, null, null, "Direct", true, null, true, null); - - + long basicDefaultDirectNetworkId = insertNetwork(conn, "BasicZoneDirectNetwork" + dcId, "Basic Zone Direct Network created for Zone " + dcId, "Guest", "Native", null, null, null, + "Dhcp", 5, dcId, "DirectPodBasedNetworkGuru", "Setup", 1, 1, null, null, "Direct", true, null, true, null); + pstmt = conn.prepareStatement("SELECT id FROM vlan WHERE vlan_type='DirectAttached' AND data_center_id=?"); pstmt.setLong(1, dcId); rs = pstmt.executeQuery(); while (rs.next()) { long vlanId = rs.getLong(1); - + pstmt = conn.prepareStatement("UPDATE vlan SET network_id=? WHERE id=?"); pstmt.setLong(1, basicDefaultDirectNetworkId); pstmt.setLong(2, vlanId); pstmt.executeUpdate(); pstmt.close(); - + upgradeDirectUserIpAddress(conn, dcId, basicDefaultDirectNetworkId, "DirectAttached"); } - - //update Dhcp servers information in domain_router and vm_instance tables; all domRs belong to the same network - pstmt = conn.prepareStatement("SELECT vm_instance.id, vm_instance.domain_id, vm_instance.account_id, domain_router.gateway, domain_router.guest_ip_address, domain_router.domain, domain_router.dns1, domain_router.dns2, domain_router.vnet FROM vm_instance INNER JOIN domain_router ON vm_instance.id=domain_router.id WHERE vm_instance.removed IS NULL AND vm_instance.type='DomainRouter' AND vm_instance.data_center_id=?"); + + // update Dhcp servers information in domain_router and vm_instance tables; all domRs belong to the same + // network + pstmt = conn + .prepareStatement("SELECT vm_instance.id, vm_instance.domain_id, vm_instance.account_id, domain_router.gateway, domain_router.guest_ip_address, domain_router.domain, domain_router.dns1, domain_router.dns2, domain_router.vnet FROM vm_instance INNER JOIN domain_router ON vm_instance.id=domain_router.id WHERE vm_instance.removed IS NULL AND vm_instance.type='DomainRouter' AND vm_instance.data_center_id=?"); pstmt.setLong(1, dcId); rs = pstmt.executeQuery(); ArrayList routers = new ArrayList(); @@ -852,39 +865,44 @@ public class Upgrade218to22 implements DbUpgrade { } rs.close(); pstmt.close(); - + for (Object[] router : routers) { s_logger.debug("Updating domR with network id in basic zone id=" + dcId); pstmt = conn.prepareStatement("UPDATE domain_router SET network_id = ? wHERE id = ? "); pstmt.setLong(1, basicDefaultDirectNetworkId); - pstmt.setLong(2, (Long)router[0]); + pstmt.setLong(2, (Long) router[0]); pstmt.executeUpdate(); pstmt.close(); - - upgradeBasicUserVms(conn, (Long)router[0], basicDefaultDirectNetworkId, (String)router[1], "untagged"); - upgradeDomR(conn, dcId, (Long)router[0], null, basicDefaultDirectNetworkId, controlNetworkId, "Basic"); + + upgradeBasicUserVms(conn, (Long) router[0], basicDefaultDirectNetworkId, (String) router[1], "untagged"); + upgradeDomR(conn, dcId, (Long) router[0], null, basicDefaultDirectNetworkId, controlNetworkId, "Basic"); } - + upgradeSsvm(conn, dcId, basicDefaultDirectNetworkId, mgmtNetworkId, controlNetworkId, "Basic"); - + pstmt = conn.prepareStatement("SELECT vm_instance.id FROM vm_instance WHERE removed IS NULL AND type='ConsoleProxy' AND data_center_id=?"); pstmt.setLong(1, dcId); rs = pstmt.executeQuery(); while (rs.next()) { upgradeConsoleProxy(conn, dcId, rs.getLong(1), basicDefaultDirectNetworkId, mgmtNetworkId, controlNetworkId, "Basic"); } - - } + + } } else { for (Object[] dc : dcs) { - Long dcId = (Long)dc[0]; - long mgmtNetworkId = insertNetwork(conn, "ManagementNetwork" + dcId, "Management Network created for Zone " + dcId, "Management", "Native", null, null, null, "Static", managementNetworkOfferingId, dcId, "PodBasedNetworkGuru", "Setup", 1, 1, null, null, null, true, null, false, null); - long storageNetworkId = insertNetwork(conn, "StorageNetwork" + dcId, "Storage Network created for Zone " + dcId, "Storage", "Native", null, null, null, "Static", storageNetworkOfferingId, dcId, "PodBasedNetworkGuru", "Setup", 1, 1, null, null, null, true, null, false, null); - long controlNetworkId = insertNetwork(conn, "ControlNetwork" + dcId, "Control Network created for Zone " + dcId, "Control", "Native", null, null, null, "Static", controlNetworkOfferingId, dcId, "ControlNetworkGuru", "Setup", 1, 1, null, null, null, true, null, false, null); + Long dcId = (Long) dc[0]; + long mgmtNetworkId = insertNetwork(conn, "ManagementNetwork" + dcId, "Management Network created for Zone " + dcId, "Management", "Native", null, null, null, "Static", + managementNetworkOfferingId, dcId, "PodBasedNetworkGuru", "Setup", 1, 1, null, null, null, true, null, false, null); + long storageNetworkId = insertNetwork(conn, "StorageNetwork" + dcId, "Storage Network created for Zone " + dcId, "Storage", "Native", null, null, null, "Static", + storageNetworkOfferingId, dcId, "PodBasedNetworkGuru", "Setup", 1, 1, null, null, null, true, null, false, null); + long controlNetworkId = insertNetwork(conn, "ControlNetwork" + dcId, "Control Network created for Zone " + dcId, "Control", "Native", null, null, null, "Static", + controlNetworkOfferingId, dcId, "ControlNetworkGuru", "Setup", 1, 1, null, null, null, true, null, false, null); upgradeManagementIpAddress(conn, dcId); - long publicNetworkId = insertNetwork(conn, "PublicNetwork" + dcId, "Public Network Created for Zone " + dcId, "Public", "Vlan", null, null, null, "Static", publicNetworkOfferingId, dcId, "PublicNetworkGuru", "Setup", 1,1, null, null, null, true, null, false, null); - - pstmt = conn.prepareStatement("SELECT vm_instance.id, vm_instance.domain_id, vm_instance.account_id, domain_router.guest_ip_address, domain_router.domain, domain_router.dns1, domain_router.dns2, domain_router.vnet FROM vm_instance INNER JOIN domain_router ON vm_instance.id=domain_router.id WHERE vm_instance.removed IS NULL AND vm_instance.type='DomainRouter' AND vm_instance.data_center_id=?"); + long publicNetworkId = insertNetwork(conn, "PublicNetwork" + dcId, "Public Network Created for Zone " + dcId, "Public", "Vlan", null, null, null, "Static", + publicNetworkOfferingId, dcId, "PublicNetworkGuru", "Setup", 1, 1, null, null, null, true, null, false, null); + + pstmt = conn + .prepareStatement("SELECT vm_instance.id, vm_instance.domain_id, vm_instance.account_id, domain_router.guest_ip_address, domain_router.domain, domain_router.dns1, domain_router.dns2, domain_router.vnet FROM vm_instance INNER JOIN domain_router ON vm_instance.id=domain_router.id WHERE vm_instance.removed IS NULL AND vm_instance.type='DomainRouter' AND vm_instance.data_center_id=?"); pstmt.setLong(1, dcId); rs = pstmt.executeQuery(); ArrayList routers = new ArrayList(); @@ -902,30 +920,31 @@ public class Upgrade218to22 implements DbUpgrade { } rs.close(); pstmt.close(); - + for (Object[] router : routers) { - String vnet = (String)router[7]; + String vnet = (String) router[7]; String reservationId = null; String state = "Allocated"; if (vnet != null) { reservationId = dcId + "-" + vnet; state = "Implemented"; } - long virtualNetworkId = insertNetwork(conn, "VirtualNetwork" + router[0], "Virtual Network for " + router[0], "Guest", "Vlan", "vlan://" + vnet, (String)router[3], (String)dc[1], "Dhcp", 6, dcId, "ExternalGuestNetworkGuru", state, (Long)router[1], (Long)router[2], (String)router[5], (String)router[6], "Virtual", false, (String)router[4], true, reservationId); + long virtualNetworkId = insertNetwork(conn, "VirtualNetwork" + router[0], "Virtual Network for " + router[0], "Guest", "Vlan", "vlan://" + vnet, (String) router[3], + (String) dc[1], "Dhcp", 6, dcId, "ExternalGuestNetworkGuru", state, (Long) router[1], (Long) router[2], (String) router[5], (String) router[6], "Virtual", false, + (String) router[4], true, reservationId); pstmt = conn.prepareStatement("UPDATE domain_router SET network_id = ? wHERE id = ? "); pstmt.setLong(1, virtualNetworkId); - pstmt.setLong(2, (Long)router[0]); + pstmt.setLong(2, (Long) router[0]); pstmt.executeUpdate(); pstmt.close(); s_logger.debug("Network inserted for " + router[0] + " id = " + virtualNetworkId); - - upgradeVirtualUserVms(conn, (Long)router[0], virtualNetworkId, (String)router[3], vnet); - upgradeDomR(conn, dcId, (Long)router[0], publicNetworkId, virtualNetworkId, controlNetworkId, "Advanced"); + + upgradeVirtualUserVms(conn, (Long) router[0], virtualNetworkId, (String) router[3], vnet); + upgradeDomR(conn, dcId, (Long) router[0], publicNetworkId, virtualNetworkId, controlNetworkId, "Advanced"); } - + upgradePublicUserIpAddress(conn, dcId, publicNetworkId, "VirtualNetwork"); - - + pstmt = conn.prepareStatement("SELECT id, vlan_id, vlan_gateway, vlan_netmask FROM vlan WHERE vlan_type='DirectAttached' AND data_center_id=?"); pstmt.setLong(1, dcId); rs = pstmt.executeQuery(); @@ -936,15 +955,15 @@ public class Upgrade218to22 implements DbUpgrade { String gateway = rs.getString(3); String netmask = rs.getString(4); String cidr = NetUtils.getCidrFromGatewayAndNetmask(gateway, netmask); - - //Get the owner of the network + + // Get the owner of the network Long accountId = 1L; Long domainId = 1L; boolean isShared = true; pstmt = conn.prepareStatement("SELECT account_id FROM account_vlan_map WHERE account_id IS NOT NULL AND vlan_db_id=?"); pstmt.setLong(1, vlanId); ResultSet accountRs = pstmt.executeQuery(); - while(accountRs.next()) { + while (accountRs.next()) { isShared = false; accountId = accountRs.getLong(1); pstmt = conn.prepareStatement("SELECT domain_id FROM account WHERE id=?"); @@ -953,42 +972,46 @@ public class Upgrade218to22 implements DbUpgrade { while (domainRs.next()) { domainId = domainRs.getLong(1); } - } - + } + if (vlanNetworkMap.get(tag) == null) { - long directNetworkId = insertNetwork(conn, "DirectNetwork" + vlanId, "Direct network created for " + vlanId, "Guest", "Vlan", "vlan://" + tag, gateway, cidr, "Dhcp", 7, dcId, "DirectNetworkGuru", "Setup", domainId, accountId, null, null, "Direct", isShared, (String)dc[2], true, null); + long directNetworkId = insertNetwork(conn, "DirectNetwork" + vlanId, "Direct network created for " + vlanId, "Guest", "Vlan", "vlan://" + tag, gateway, cidr, "Dhcp", 7, + dcId, "DirectNetworkGuru", "Setup", domainId, accountId, null, null, "Direct", isShared, (String) dc[2], true, null); vlanNetworkMap.put(tag, directNetworkId); } - + pstmt = conn.prepareStatement("UPDATE vlan SET network_id=? WHERE id=?"); pstmt.setLong(1, vlanNetworkMap.get(tag)); pstmt.setLong(2, vlanId); pstmt.executeUpdate(); - - - + pstmt.close(); - + upgradeDirectUserIpAddress(conn, dcId, vlanNetworkMap.get(tag), "DirectAttached"); } - + upgradeSsvm(conn, dcId, publicNetworkId, mgmtNetworkId, controlNetworkId, "Advanced"); - + pstmt = conn.prepareStatement("SELECT vm_instance.id FROM vm_instance WHERE removed IS NULL AND type='ConsoleProxy' AND data_center_id=?"); pstmt.setLong(1, dcId); rs = pstmt.executeQuery(); while (rs.next()) { upgradeConsoleProxy(conn, dcId, rs.getLong(1), publicNetworkId, mgmtNetworkId, controlNetworkId, "Advanced"); } - + pstmt.close(); } - + + // Update hypervisor type for user vm to be consistent with original 2.2.4 + pstmt = conn.prepareStatement("UPDATE vm_instance SET hypervisor_type='XenServer' WHERE hypervisor_type='xenserver'"); + pstmt.executeUpdate(); + pstmt.close(); + } } catch (SQLException e) { throw new CloudRuntimeException("Can't update data center ", e); } } - + public void upgradePortForwardingRules(Connection conn) { try { PreparedStatement pstmt = conn.prepareStatement("SELECT id, public_ip_address, public_port, private_ip_address, private_port, protocol FROM ip_forwarding WHERE forwarding=1"); @@ -996,44 +1019,45 @@ public class Upgrade218to22 implements DbUpgrade { ArrayList rules = new ArrayList(); while (rs.next()) { Object[] rule = new Object[10]; - rule[0] = rs.getLong(1); //rule id - rule[1] = rs.getString(2); //rule public IP - rule[2] = rs.getString(3); //rule public port - rule[3] = rs.getString(4); //rule private Ip - rule[4] = rs.getString(5); //rule private port - rule[5] = rs.getString(6); //rule protocol + rule[0] = rs.getLong(1); // rule id + rule[1] = rs.getString(2); // rule public IP + rule[2] = rs.getString(3); // rule public port + rule[3] = rs.getString(4); // rule private Ip + rule[4] = rs.getString(5); // rule private port + rule[5] = rs.getString(6); // rule protocol rules.add(rule); } rs.close(); pstmt.close(); - + if (!rules.isEmpty()) { s_logger.debug("Found " + rules.size() + " port forwarding rules to upgrade"); for (Object[] rule : rules) { - long id = (Long)rule[0]; - String sourcePort = (String)rule[2]; - String protocol = (String)rule[5]; - String publicIp = (String)rule[1]; - + long id = (Long) rule[0]; + String sourcePort = (String) rule[2]; + String protocol = (String) rule[5]; + String publicIp = (String) rule[1]; + pstmt = conn.prepareStatement("SELECT id, account_id, domain_id, network_id FROM user_ip_address WHERE public_ip_address=?"); pstmt.setString(1, publicIp); rs = pstmt.executeQuery(); - + if (!rs.next()) { throw new CloudRuntimeException("Unable to find public IP address " + publicIp); } - + int ipAddressId = rs.getInt(1); long accountId = rs.getLong(2); long domainId = rs.getLong(3); long networkId = rs.getLong(4); - + rs.close(); - pstmt.close(); - - //update firewall_rules table + pstmt.close(); + + // update firewall_rules table s_logger.trace("Updating firewall_rules table as a part of PF rules upgrade..."); - pstmt = conn.prepareStatement("INSERT INTO firewall_rules (id, ip_address_id, start_port, end_port, state, protocol, purpose, account_id, domain_id, network_id, xid, is_static_nat, created) VALUES (?, ?, ?, ?, 'Active', ?, 'PortForwarding', ?, ?, ?, ?, 0, now())"); + pstmt = conn + .prepareStatement("INSERT INTO firewall_rules (id, ip_address_id, start_port, end_port, state, protocol, purpose, account_id, domain_id, network_id, xid, is_static_nat, created) VALUES (?, ?, ?, ?, 'Active', ?, 'PortForwarding', ?, ?, ?, ?, 0, now())"); pstmt.setLong(1, id); pstmt.setInt(2, ipAddressId); pstmt.setInt(3, Integer.valueOf(sourcePort)); @@ -1046,25 +1070,25 @@ public class Upgrade218to22 implements DbUpgrade { pstmt.executeUpdate(); pstmt.close(); s_logger.trace("firewall_rules table is updated as a part of PF rules upgrade"); - - //update port_forwarding_rules table + + // update port_forwarding_rules table s_logger.trace("Updating port_forwarding_rules table..."); - String privateIp = (String)rule[3]; + String privateIp = (String) rule[3]; pstmt = conn.prepareStatement("SELECT instance_id FROM nics where network_id=? AND ip4_address=?"); pstmt.setLong(1, networkId); pstmt.setString(2, privateIp); rs = pstmt.executeQuery(); - + if (!rs.next()) { - throw new CloudRuntimeException("Unable to find vmId for private ip address " + privateIp + " for account id=" + accountId) ; + throw new CloudRuntimeException("Unable to find vmId for private ip address " + privateIp + " for account id=" + accountId); } - - long instanceId = rs.getLong(1);; + + long instanceId = rs.getLong(1); + ; rs.close(); pstmt.close(); - - - String privatePort = (String)rule[4]; + + String privatePort = (String) rule[4]; pstmt = conn.prepareStatement("INSERT INTO port_forwarding_rules VALUES (?, ?, ?, ?, ?)"); pstmt.setLong(1, id); pstmt.setLong(2, instanceId); @@ -1081,8 +1105,7 @@ public class Upgrade218to22 implements DbUpgrade { throw new CloudRuntimeException("Can't update port forwarding rules ", e); } } - - + public void upgradeLoadBalancingRules(Connection conn) { try { PreparedStatement pstmt = conn.prepareStatement("SELECT name, ip_address, public_port, private_port, algorithm, id FROM load_balancer"); @@ -1090,17 +1113,17 @@ public class Upgrade218to22 implements DbUpgrade { ArrayList lbs = new ArrayList(); while (rs.next()) { Object[] lb = new Object[10]; - lb[0] = rs.getString(1); //lb name - lb[1] = rs.getString(2); //lb public IP - lb[2] = rs.getString(3); //lb public port - lb[3] = rs.getString(4); //lb private port - lb[4] = rs.getString(5); //lb algorithm - lb[5] = rs.getLong(6); // lb Id + lb[0] = rs.getString(1); // lb name + lb[1] = rs.getString(2); // lb public IP + lb[2] = rs.getString(3); // lb public port + lb[3] = rs.getString(4); // lb private port + lb[4] = rs.getString(5); // lb algorithm + lb[5] = rs.getLong(6); // lb Id lbs.add(lb); } rs.close(); pstmt.close(); - + if (!lbs.isEmpty()) { s_logger.debug("Found " + lbs.size() + " lb rules to upgrade"); pstmt = conn.prepareStatement("SELECT id FROM firewall_rules"); @@ -1111,36 +1134,36 @@ public class Upgrade218to22 implements DbUpgrade { } rs.close(); pstmt.close(); - + for (Object[] lb : lbs) { - String name = (String)lb[0]; - String publicIp = (String)lb[1]; - String sourcePort = (String)lb[2]; - String destPort = (String)lb[3]; - String algorithm = (String)lb[4]; - Long originalLbId = (Long)lb[5]; - newLbId = newLbId+1; - - + String name = (String) lb[0]; + String publicIp = (String) lb[1]; + String sourcePort = (String) lb[2]; + String destPort = (String) lb[3]; + String algorithm = (String) lb[4]; + Long originalLbId = (Long) lb[5]; + newLbId = newLbId + 1; + pstmt = conn.prepareStatement("SELECT id, account_id, domain_id, network_id FROM user_ip_address WHERE public_ip_address=?"); pstmt.setString(1, publicIp); rs = pstmt.executeQuery(); - + if (!rs.next()) { throw new CloudRuntimeException("Unable to find public IP address " + publicIp); } - + int ipAddressId = rs.getInt(1); long accountId = rs.getLong(2); long domainId = rs.getLong(3); long networkId = rs.getLong(4); - + rs.close(); - pstmt.close(); - - //update firewall_rules table + pstmt.close(); + + // update firewall_rules table s_logger.trace("Updating firewall_rules table as a part of LB rules upgrade..."); - pstmt = conn.prepareStatement("INSERT INTO firewall_rules (id, ip_address_id, start_port, end_port, state, protocol, purpose, account_id, domain_id, network_id, xid, is_static_nat, created) VALUES (?, ?, ?, ?, 'Active', ?, 'LoadBalancing', ?, ?, ?, ?, 0, now())"); + pstmt = conn + .prepareStatement("INSERT INTO firewall_rules (id, ip_address_id, start_port, end_port, state, protocol, purpose, account_id, domain_id, network_id, xid, is_static_nat, created) VALUES (?, ?, ?, ?, 'Active', ?, 'LoadBalancing', ?, ?, ?, ?, 0, now())"); pstmt.setLong(1, newLbId); pstmt.setInt(2, ipAddressId); pstmt.setInt(3, Integer.valueOf(sourcePort)); @@ -1153,8 +1176,8 @@ public class Upgrade218to22 implements DbUpgrade { pstmt.executeUpdate(); pstmt.close(); s_logger.trace("firewall_rules table is updated as a part of LB rules upgrade"); - - //update load_balancing_rules + + // update load_balancing_rules s_logger.trace("Updating load_balancing_rules table as a part of LB rules upgrade..."); pstmt = conn.prepareStatement("INSERT INTO load_balancing_rules VALUES (?, ?, NULL, ?, ?, ?)"); pstmt.setLong(1, newLbId); @@ -1165,8 +1188,8 @@ public class Upgrade218to22 implements DbUpgrade { pstmt.executeUpdate(); pstmt.close(); s_logger.trace("load_balancing_rules table is updated as a part of LB rules upgrade"); - - //update load_balancer_vm_map table + + // update load_balancer_vm_map table s_logger.trace("Updating load_balancer_vm_map table as a part of LB rules upgrade..."); pstmt = conn.prepareStatement("SELECT instance_id FROM load_balancer_vm_map WHERE load_balancer_id=?"); pstmt.setLong(1, originalLbId); @@ -1174,19 +1197,18 @@ public class Upgrade218to22 implements DbUpgrade { ArrayList lbMaps = new ArrayList(); while (rs.next()) { Object[] lbMap = new Object[10]; - lbMap[0] = rs.getLong(1); //instanceId + lbMap[0] = rs.getLong(1); // instanceId lbMaps.add(lbMap); } rs.close(); pstmt.close(); - - + pstmt = conn.prepareStatement("UPDATE load_balancer_vm_map SET load_balancer_id=? WHERE load_balancer_id=?"); pstmt.setLong(1, newLbId); pstmt.setLong(2, originalLbId); pstmt.executeUpdate(); pstmt.close(); - + s_logger.trace("load_balancer_vm_map table is updated as a part of LB rules upgrade"); } } @@ -1195,8 +1217,8 @@ public class Upgrade218to22 implements DbUpgrade { throw new CloudRuntimeException("Can't update LB rules ", e); } } - - private void migrateEvents(Connection conn){ + + private void migrateEvents(Connection conn) { try { PreparedStatement pstmt1 = conn.prepareStatement("SHOW DATABASES LIKE 'cloud_usage'"); ResultSet rs1 = pstmt1.executeQuery(); @@ -1205,9 +1227,9 @@ public class Upgrade218to22 implements DbUpgrade { return; } - //get last processed event Id - Long lastProcessedEvent = getMostRecentEvent(conn); - //Events not yet processed + // get last processed event Id + Long lastProcessedEvent = getMostRecentEvent(conn); + // Events not yet processed String sql = "SELECT type, description, user_id, account_id, created, level, parameters FROM cloud.event vmevt WHERE vmevt.id > ? and vmevt.state = 'Completed' "; if (lastProcessedEvent == null) { s_logger.trace("no events are processed earlier, copying all events"); @@ -1239,7 +1261,7 @@ public class Upgrade218to22 implements DbUpgrade { throw new CloudRuntimeException("Failed to migrate usage events: ", e); } } - + private Long getMostRecentEvent(Connection conn) { PreparedStatement pstmt = null; String sql = "SELECT id FROM cloud_usage.event ORDER BY created DESC LIMIT 1"; @@ -1250,11 +1272,11 @@ public class Upgrade218to22 implements DbUpgrade { return rs.getLong(1); } } catch (Exception ex) { - throw new CloudRuntimeException("error getting most recent event date: "+ ex.getMessage()); + throw new CloudRuntimeException("error getting most recent event date: " + ex.getMessage()); } return null; } - + private void convertEvent(EventVO event, Connection conn) throws IOException, SQLException { // we only create usage for success cases as error cases mean // the event didn't happen, so it couldn't result in usage @@ -1264,9 +1286,9 @@ public class Upgrade218to22 implements DbUpgrade { String eventType = event.getType(); UsageEventVO usageEvent = null; if (isVMEvent(eventType)) { - usageEvent= convertVMEvent(event); + usageEvent = convertVMEvent(event); } else if (isIPEvent(eventType)) { - usageEvent= convertIPEvent(event); + usageEvent = convertIPEvent(event); } else if (isVolumeEvent(eventType)) { usageEvent = convertVolumeEvent(event); } else if (isTemplateEvent(eventType)) { @@ -1275,42 +1297,42 @@ public class Upgrade218to22 implements DbUpgrade { usageEvent = convertISOEvent(event); } else if (isSnapshotEvent(eventType)) { usageEvent = convertSnapshotEvent(event); - } /*else if (isSecurityGrpEvent(eventType)) { - usageEvent = convertSecurityGrpEvent(event); - } else if (isLoadBalancerEvent(eventType)) { - usageEvent = convertLoadBalancerEvent(event); - }*/ - if(usageEvent != null){ + } /* + * else if (isSecurityGrpEvent(eventType)) { usageEvent = convertSecurityGrpEvent(event); } else if + * (isLoadBalancerEvent(eventType)) { usageEvent = convertLoadBalancerEvent(event); } + */ + if (usageEvent != null) { usageEvent.setCreatedDate(event.getCreateDate()); - if(usageEvent.getZoneId() == -1){ + if (usageEvent.getZoneId() == -1) { usageEvent.setZoneId(0); } - //update firewall_rules table + // update firewall_rules table PreparedStatement pstmt = null; - pstmt = conn.prepareStatement("INSERT INTO usage_event (usage_event.type, usage_event.created, usage_event.account_id, usage_event.zone_id, usage_event.resource_id, usage_event.resource_name," + - " usage_event.offering_id, usage_event.template_id, usage_event.size) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"); + pstmt = conn + .prepareStatement("INSERT INTO usage_event (usage_event.type, usage_event.created, usage_event.account_id, usage_event.zone_id, usage_event.resource_id, usage_event.resource_name," + + " usage_event.offering_id, usage_event.template_id, usage_event.size) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"); pstmt.setString(1, usageEvent.getType()); pstmt.setString(2, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), usageEvent.getCreateDate())); pstmt.setLong(3, usageEvent.getAccountId()); pstmt.setLong(4, usageEvent.getZoneId()); pstmt.setLong(5, usageEvent.getResourceId()); pstmt.setString(6, usageEvent.getResourceName()); - if(usageEvent.getOfferingId() != null){ + if (usageEvent.getOfferingId() != null) { pstmt.setLong(7, usageEvent.getOfferingId()); } else { pstmt.setNull(7, Types.BIGINT); } - if(usageEvent.getTemplateId() != null){ + if (usageEvent.getTemplateId() != null) { pstmt.setLong(8, usageEvent.getTemplateId()); } else { pstmt.setNull(8, Types.BIGINT); } - if(usageEvent.getSize() != null){ + if (usageEvent.getSize() != null) { pstmt.setLong(9, usageEvent.getSize()); } else { pstmt.setNull(9, Types.BIGINT); } - //pstmt.setString(10, usageEvent.getResourceType()); + // pstmt.setString(10, usageEvent.getResourceType()); pstmt.executeUpdate(); pstmt.close(); } @@ -1329,50 +1351,44 @@ public class Upgrade218to22 implements DbUpgrade { } return eventType.startsWith("NET.IP"); } - + private boolean isVolumeEvent(String eventType) { if (eventType == null) { return false; } - return (eventType.equals(EventTypes.EVENT_VOLUME_CREATE) || - eventType.equals(EventTypes.EVENT_VOLUME_DELETE)); + return (eventType.equals(EventTypes.EVENT_VOLUME_CREATE) || eventType.equals(EventTypes.EVENT_VOLUME_DELETE)); } private boolean isTemplateEvent(String eventType) { if (eventType == null) { return false; } - return (eventType.equals(EventTypes.EVENT_TEMPLATE_CREATE) || - eventType.equals(EventTypes.EVENT_TEMPLATE_COPY) || - eventType.equals(EventTypes.EVENT_TEMPLATE_DELETE)); + return (eventType.equals(EventTypes.EVENT_TEMPLATE_CREATE) || eventType.equals(EventTypes.EVENT_TEMPLATE_COPY) || eventType.equals(EventTypes.EVENT_TEMPLATE_DELETE)); } - + private boolean isISOEvent(String eventType) { if (eventType == null) { return false; } - return (eventType.equals(EventTypes.EVENT_ISO_CREATE) || - eventType.equals(EventTypes.EVENT_ISO_COPY) || - eventType.equals(EventTypes.EVENT_ISO_DELETE)); + return (eventType.equals(EventTypes.EVENT_ISO_CREATE) || eventType.equals(EventTypes.EVENT_ISO_COPY) || eventType.equals(EventTypes.EVENT_ISO_DELETE)); } - + private boolean isSnapshotEvent(String eventType) { if (eventType == null) { return false; } - return (eventType.equals(EventTypes.EVENT_SNAPSHOT_CREATE) || - eventType.equals(EventTypes.EVENT_SNAPSHOT_DELETE)); + return (eventType.equals(EventTypes.EVENT_SNAPSHOT_CREATE) || eventType.equals(EventTypes.EVENT_SNAPSHOT_DELETE)); } - + private boolean isLoadBalancerEvent(String eventType) { if (eventType == null) { return false; } return eventType.startsWith("LB."); } - + private UsageEventVO convertVMEvent(EventVO event) throws IOException { - + Properties vmEventParams = new Properties(); UsageEventVO usageEvent = null; long vmId = -1L; @@ -1405,13 +1421,13 @@ public class Upgrade218to22 implements DbUpgrade { usageEvent = new UsageEventVO(EventTypes.EVENT_VM_CREATE, event.getAccountId(), zoneId, vmId, vmEventParams.getProperty("vmName"), soId, templateId, ""); } else if (EventTypes.EVENT_VM_DESTROY.equals(event.getType())) { - + } return usageEvent; } private UsageEventVO convertIPEvent(EventVO event) throws IOException { - + Properties ipEventParams = new Properties(); String ipAddress = null; boolean isSourceNat = false; @@ -1426,8 +1442,7 @@ public class Upgrade218to22 implements DbUpgrade { } } isSourceNat = Boolean.parseBoolean(ipEventParams.getProperty("sourceNat")); - if (isSourceNat) - { + if (isSourceNat) { return null; // skip source nat IP addresses as we don't charge for them } @@ -1436,7 +1451,8 @@ public class Upgrade218to22 implements DbUpgrade { usageEvent = new UsageEventVO(EventTypes.EVENT_NET_IP_ASSIGN, event.getAccountId(), zoneId, 0L, ipAddress, 0L); } else if (EventTypes.EVENT_NET_IP_RELEASE.equals(event.getType())) { if (!isSourceNat) { - // at this point it's not a sourceNat IP, so find the usage record with this IP and a null released date, update the released date + // at this point it's not a sourceNat IP, so find the usage record with this IP and a null released date, update + // the released date usageEvent = new UsageEventVO(EventTypes.EVENT_NET_IP_RELEASE, event.getAccountId(), 0L, 0L, ipAddress, 0L); } } @@ -1444,7 +1460,7 @@ public class Upgrade218to22 implements DbUpgrade { } private UsageEventVO convertVolumeEvent(EventVO event) throws IOException { - + Properties volEventParams = new Properties(); long volId = -1L; Long doId = -1L; @@ -1460,27 +1476,25 @@ public class Upgrade218to22 implements DbUpgrade { templateId = Long.parseLong(volEventParams.getProperty("tId")); size = Long.parseLong(volEventParams.getProperty("size")); size = (size * 1048576); - if(doId == -1){ + if (doId == -1) { doId = null; } - if(templateId == -1){ + if (templateId == -1) { templateId = null; } } if (EventTypes.EVENT_VOLUME_CREATE.equals(event.getType())) { - usageEvent = new UsageEventVO(EventTypes.EVENT_VOLUME_CREATE, event.getAccountId(), zoneId, volId, - "", doId, templateId, size); + usageEvent = new UsageEventVO(EventTypes.EVENT_VOLUME_CREATE, event.getAccountId(), zoneId, volId, "", doId, templateId, size); } else if (EventTypes.EVENT_VOLUME_DELETE.equals(event.getType())) { - usageEvent = new UsageEventVO(EventTypes.EVENT_VOLUME_DELETE, event.getAccountId(), 0, volId, - ""); - + usageEvent = new UsageEventVO(EventTypes.EVENT_VOLUME_DELETE, event.getAccountId(), 0, volId, ""); + } return usageEvent; } private UsageEventVO convertTemplateEvent(EventVO event) throws IOException { - + Properties templateEventParams = new Properties(); long templateId = -1L; long zoneId = -1L; @@ -1489,24 +1503,24 @@ public class Upgrade218to22 implements DbUpgrade { templateEventParams.load(new StringReader(event.getParameters())); templateId = Long.parseLong(templateEventParams.getProperty("id")); - if(templateEventParams.getProperty("dcId") != null){ + if (templateEventParams.getProperty("dcId") != null) { zoneId = Long.parseLong(templateEventParams.getProperty("dcId")); } if (EventTypes.EVENT_TEMPLATE_CREATE.equals(event.getType()) || EventTypes.EVENT_TEMPLATE_COPY.equals(event.getType())) { templateSize = Long.parseLong(templateEventParams.getProperty("size")); - if(templateSize < 1){ + if (templateSize < 1) { return null; } - if(zoneId == -1L){ + if (zoneId == -1L) { return null; } - usageEvent = new UsageEventVO(event.getType(), event.getAccountId(), zoneId, templateId, "", null, null , templateSize); + usageEvent = new UsageEventVO(event.getType(), event.getAccountId(), zoneId, templateId, "", null, null, templateSize); } else if (EventTypes.EVENT_TEMPLATE_DELETE.equals(event.getType())) { usageEvent = new UsageEventVO(event.getType(), event.getAccountId(), zoneId, templateId, null); } return usageEvent; } - + private UsageEventVO convertISOEvent(EventVO event) throws IOException { Properties isoEventParams = new Properties(); long isoId = -1L; @@ -1516,19 +1530,19 @@ public class Upgrade218to22 implements DbUpgrade { isoEventParams.load(new StringReader(event.getParameters())); isoId = Long.parseLong(isoEventParams.getProperty("id")); - if(isoEventParams.getProperty("dcId") != null){ + if (isoEventParams.getProperty("dcId") != null) { zoneId = Long.parseLong(isoEventParams.getProperty("dcId")); } if (EventTypes.EVENT_ISO_CREATE.equals(event.getType()) || EventTypes.EVENT_ISO_COPY.equals(event.getType())) { isoSize = Long.parseLong(isoEventParams.getProperty("size")); - usageEvent = new UsageEventVO(event.getType(), event.getAccountId(), zoneId, isoId, "", null, null , isoSize); + usageEvent = new UsageEventVO(event.getType(), event.getAccountId(), zoneId, isoId, "", null, null, isoSize); } else if (EventTypes.EVENT_ISO_DELETE.equals(event.getType())) { usageEvent = new UsageEventVO(event.getType(), event.getAccountId(), zoneId, isoId, null); } return usageEvent; } - + private UsageEventVO convertSnapshotEvent(EventVO event) throws IOException { Properties snapEventParams = new Properties(); long snapId = -1L; @@ -1550,7 +1564,6 @@ public class Upgrade218to22 implements DbUpgrade { return usageEvent; } - @Override public void performDataMigration(Connection conn) { upgradeDataCenter(conn); @@ -1567,7 +1580,7 @@ public class Upgrade218to22 implements DbUpgrade { if (file == null) { throw new CloudRuntimeException("Unable to find the upgrade script, schema-21to22-cleanup.sql"); } - + return new File[] { new File(file) }; } @@ -1580,7 +1593,7 @@ public class Upgrade218to22 implements DbUpgrade { public String getUpgradedVersion() { return "2.2.1"; } - + @Override public boolean supportsRollingUpgrade() { return false;