diff --git a/server/src/com/cloud/upgrade/DatabaseUpgradeChecker.java b/server/src/com/cloud/upgrade/DatabaseUpgradeChecker.java index 4bf11cc198c..f6d67111ed5 100644 --- a/server/src/com/cloud/upgrade/DatabaseUpgradeChecker.java +++ b/server/src/com/cloud/upgrade/DatabaseUpgradeChecker.java @@ -25,8 +25,11 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; import java.util.Date; import java.util.HashMap; +import java.util.List; +import java.util.TreeMap; import org.apache.log4j.Logger; @@ -63,7 +66,7 @@ public class DatabaseUpgradeChecker implements SystemIntegrityChecker { _upgradeMap.put("2.1.7", new DbUpgrade[] { new Upgrade217to218(), new Upgrade218to22(), new Upgrade221to222(), new UpgradeSnapshot217to224(), new Upgrade222to224() }); _upgradeMap.put("2.1.8", new DbUpgrade[] { new Upgrade218to22(), new Upgrade221to222(), new UpgradeSnapshot217to224(), new Upgrade222to224(), new Upgrade218to224DomainVlans() }); _upgradeMap.put("2.1.9", new DbUpgrade[] { new Upgrade218to22(), new Upgrade221to222(), new UpgradeSnapshot217to224(), new Upgrade222to224(), new Upgrade218to224DomainVlans() }); - _upgradeMap.put("2.2.1", new DbUpgrade[] { new Upgrade221to222(), new UpgradeSnapshot223to224(), new Upgrade222to224()}); + _upgradeMap.put("2.2.1", new DbUpgrade[] { new Upgrade221to222(), new UpgradeSnapshot223to224(), new Upgrade222to224() }); _upgradeMap.put("2.2.2", new DbUpgrade[] { new UpgradeSnapshot223to224(), new Upgrade222to224() }); _upgradeMap.put("2.2.3", new DbUpgrade[] { new UpgradeSnapshot223to224(), new Upgrade222to224() }); } @@ -169,31 +172,55 @@ public class DatabaseUpgradeChecker implements SystemIntegrityChecker { if (!ClusterManagerImpl.arePeersRunning(trimmedCurrentVersion)) { s_logger.info("Cleaning upgrades because all management server are now at the same version"); - for (DbUpgrade upgrade : upgrades) { - s_logger.info("Cleanup upgrade " + upgrade.getClass().getSimpleName() + " to upgrade from " + upgrade.getUpgradableVersionRange()[0] + "-" + upgrade.getUpgradableVersionRange()[1] - + " to " + upgrade.getUpgradedVersion()); - VersionVO version = _dao.findByVersion(upgrade.getUpgradedVersion(), Step.Upgrade); + TreeMap> upgradedVersions = new TreeMap>(); - if (version != null) { - Transaction txn = Transaction.open("Cleanup"); - txn.start(); - try { - File[] scripts = upgrade.getCleanupScripts(); - if (scripts != null) { - for (File script : scripts) { - runScript(script); + for (DbUpgrade upgrade : upgrades) { + String upgradedVerson = upgrade.getUpgradedVersion(); + List upgradeList = upgradedVersions.get(upgradedVerson); + if (upgradeList == null) { + upgradeList = new ArrayList(); + } + upgradeList.add(upgrade); + upgradedVersions.put(upgradedVerson, upgradeList); + } + + for (String upgradedVersion : upgradedVersions.keySet()) { + List versionUpgrades = upgradedVersions.get(upgradedVersion); + VersionVO version = _dao.findByVersion(upgradedVersion, Step.Upgrade); + s_logger.debug("Upgrading to version " + upgradedVersion + "..."); + + Transaction txn = Transaction.open("Cleanup"); + try { + if (version != null) { + for (DbUpgrade upgrade : versionUpgrades) { + s_logger.info("Cleanup upgrade " + upgrade.getClass().getSimpleName() + " to upgrade from " + upgrade.getUpgradableVersionRange()[0] + "-" + + upgrade.getUpgradableVersionRange()[1] + " to " + upgrade.getUpgradedVersion()); + + txn.start(); + + File[] scripts = upgrade.getCleanupScripts(); + if (scripts != null) { + for (File script : scripts) { + runScript(script); + s_logger.debug("Cleanup script " + script.getAbsolutePath() + " is executed successfully"); + } } + txn.commit(); } + + txn.start(); version.setStep(Step.Complete); + s_logger.debug("Upgrade completed for version " + upgradedVersion); version.setUpdated(new Date()); _dao.update(version.getId(), version); txn.commit(); - } finally { - txn.close(); } + } finally { + txn.close(); } } } + } @Override diff --git a/server/test/com/cloud/upgrade/Sanity220To224UpgradeTest.java b/server/test/com/cloud/upgrade/Sanity220To224UpgradeTest.java index 2fa2436662e..2951702754b 100644 --- a/server/test/com/cloud/upgrade/Sanity220To224UpgradeTest.java +++ b/server/test/com/cloud/upgrade/Sanity220To224UpgradeTest.java @@ -49,7 +49,7 @@ public class Sanity220To224UpgradeTest extends TestCase { public void test217to22Upgrade() throws SQLException { s_logger.debug("Finding sample data from 2.2.1"); - DbTestUtils.executeScript("/home/alena/Downloads/before/cloud_report_dump_220_April_26th_1521hrs.sql", false, true); + DbTestUtils.executeScript("PreviousDatabaseSchema/2.2.0/2.2.0.sample.sql", false, true); Connection conn; PreparedStatement pstmt; diff --git a/setup/db/221to222.sql b/setup/db/221to222.sql deleted file mode 100644 index 76dba6bd91a..00000000000 --- a/setup/db/221to222.sql +++ /dev/null @@ -1,30 +0,0 @@ -update firewall_rules set purpose='StaticNat' where is_static_nat=1; -alter table firewall_rules drop column is_static_nat; -alter table user_ip_address add CONSTRAINT `fk_user_ip_address__vm_id` FOREIGN KEY (`vm_id`) REFERENCES `vm_instance`(`id`); -update network_offerings set system_only=1 where name='System-Guest-Network'; -update network_offerings set dns_service=1,userdata_service=1,dhcp_service=1 where system_only=0; -update network_offerings set firewall_service=1, lb_service=1,vpn_service=1,gateway_service=1 where traffic_type='guest' and system_only=0; -alter table domain add column `state` char(32) NOT NULL default 'Active' COMMENT 'state of the domain'; -alter table nics add column `vm_type` char(32); -update nics set vm_type=(select type from vm_instance where vm_instance.id=nics.instance_id); -INSERT INTO configuration (`category`, `instance`, `component`, `name`, `value`, `description`) VALUES ('Network','DEFAULT','none','network.guest.cidr.limit','22','size limit for guest cidr; cant be less than this value'); -alter table user_statistics add column `network_id` bigint unsigned; -update op_networks set nics_count=(nics_count-1) where id in (select d.network_id from domain_router d, vm_instance i where i.state='Running' and i.id=d.id); -update network_offerings set traffic_type='Guest' where system_only=0; -alter table network_offerings add column `guest_type` char(32); -update network_offerings set guest_type='Direct' where id=5; -update network_offerings set guest_type='Virtual' where id=6; -update network_offerings set guest_type='Direct' where id=7; -alter table op_it_work add column `vm_type` char(32) NOT NULL; -update op_it_work set vm_type=(select type from vm_instance where vm_instance.id=op_it_work.instance_id); -alter table networks add column `is_security_group_enabled` tinyint NOT NULL DEFAULT 0 COMMENT '1: enabled, 0: not'; -update networks set is_security_group_enabled=0; -alter table data_center add column `is_security_group_enabled` tinyint NOT NULL DEFAULT 0 COMMENT '1: enabled, 0: not'; -update data_center set is_security_group_enabled=0; -update data_center set dns_provider='DhcpServer', dhcp_provider='DhcpServer', userdata_provider='DhcpServer', lb_provider=null, firewall_provider=null, vpn_provider=null, gateway_provider=null where networktype='Basic'; -update network_offerings set specify_vlan=1 where system_only=0 and guest_type='Direct'; -update networks set traffic_type='Guest' where network_offering_id in (select id from network_offerings where system_only=0); -update network_offerings set availability='Optional' where id=7; -delete from configuration where name='router.cleanup.interval'; -INSERT INTO configuration (category, instance, component, name, value, description) - VALUES ('Advanced', 'DEFAULT', 'management-server', 'management.network.cidr', NULL, 'The cidr of management server network'); diff --git a/setup/db/221to222_usage.sql b/setup/db/221to222_usage.sql deleted file mode 100644 index cd4a13f86e5..00000000000 --- a/setup/db/221to222_usage.sql +++ /dev/null @@ -1,3 +0,0 @@ -alter table cloud_usage add column `network_id` bigint unsigned; -alter table usage_network add column `network_id` bigint unsigned; -alter table user_statistics add column `network_id` bigint unsigned; diff --git a/setup/db/create-schema.sql b/setup/db/create-schema.sql index bd4a345f969..d2e326a1b60 100755 --- a/setup/db/create-schema.sql +++ b/setup/db/create-schema.sql @@ -95,7 +95,6 @@ DROP TABLE IF EXISTS `cloud`.`account_vlan_map`; DROP TABLE IF EXISTS `cloud`.`cluster_details`; DROP TABLE IF EXISTS `cloud`.`guest_os_category`; DROP TABLE IF EXISTS `cloud`.`guest_os_hypervisor`; -DROP TABLE IF EXISTS `cloud`.`host_master`; DROP TABLE IF EXISTS `cloud`.`op_dc_link_local_ip_address_alloc`; DROP TABLE IF EXISTS `cloud`.`op_host`; DROP TABLE IF EXISTS `cloud`.`op_nwgrp_work`; diff --git a/setup/db/db/schema-21to22-cleanup.sql b/setup/db/db/schema-21to22-cleanup.sql index c93c9049dab..03612136adf 100644 --- a/setup/db/db/schema-21to22-cleanup.sql +++ b/setup/db/db/schema-21to22-cleanup.sql @@ -19,7 +19,6 @@ ALTER TABLE `cloud`.`vm_instance` DROP COLUMN `iso_id`; ALTER TABLE `cloud`.`vm_instance` DROP COLUMN `display_name`; ALTER TABLE `cloud`.`vm_instance` DROP COLUMN `group`; ALTER TABLE `cloud`.`vm_instance` DROP COLUMN `storage_ip`; -ALTER TABLE `cloud`.`vm_instance` DROP COLUMN `mirrored_vols`; DROP TABLE `cloud`.`pricing`; @@ -50,10 +49,8 @@ ALTER TABLE `cloud`.`domain_router` DROP INDEX `i_domain_router__account_id`; ALTER TABLE `cloud`.`domain_router` DROP INDEX `i_domain_router__public_ip_address`; ALTER TABLE `cloud`.`domain_router` DROP INDEX `i_domain_router__vlan_id`; ALTER TABLE `cloud`.`domain_router` DROP COLUMN `gateway`; -ALTER TABLE `cloud`.`domain_router` DROP COLUMN `ram_size`; ALTER TABLE `cloud`.`domain_router` DROP COLUMN `dns1`; ALTER TABLE `cloud`.`domain_router` DROP COLUMN `dns2`; -ALTER TABLE `cloud`.`domain_router` DROP COLUMN `guest_mac_address`; ALTER TABLE `cloud`.`domain_router` DROP COLUMN `guest_dc_mac_address`; ALTER TABLE `cloud`.`domain_router` DROP COLUMN `vnet`; ALTER TABLE `cloud`.`domain_router` DROP COLUMN `dc_vlan`; @@ -62,7 +59,6 @@ ALTER TABLE `cloud`.`domain_router` DROP COLUMN `vlan_id`; ALTER TABLE `cloud`.`domain_router` DROP COLUMN `domain_id`; ALTER TABLE `cloud`.`domain_router` DROP COLUMN `account_id`; ALTER TABLE `cloud`.`domain_router` DROP COLUMN `dhcp_ip_address`; -ALTER TABLE `cloud`.`domain_router` DROP COLUMN `domain`; ALTER TABLE `cloud`.`console_proxy` DROP FOREIGN KEY `fk_console_proxy__vlan_id`; ALTER TABLE `cloud`.`console_proxy` DROP INDEX `i_console_proxy__vlan_id`; @@ -75,7 +71,6 @@ ALTER TABLE `cloud`.`console_proxy` DROP COLUMN `guest_ip_address`; ALTER TABLE `cloud`.`console_proxy` DROP COLUMN `guest_netmask`; ALTER TABLE `cloud`.`console_proxy` DROP COLUMN `vlan_db_id`; ALTER TABLE `cloud`.`console_proxy` DROP COLUMN `vlan_id`; -ALTER TABLE `cloud`.`console_proxy` DROP COLUMN `ram_size`; ALTER TABLE `cloud`.`secondary_storage_vm` DROP COLUMN `gateway`; ALTER TABLE `cloud`.`secondary_storage_vm` DROP COLUMN `dns1`; @@ -86,7 +81,6 @@ ALTER TABLE `cloud`.`secondary_storage_vm` DROP COLUMN `guest_ip_address`; ALTER TABLE `cloud`.`secondary_storage_vm` DROP COLUMN `guest_netmask`; ALTER TABLE `cloud`.`secondary_storage_vm` DROP COLUMN `vlan_db_id`; ALTER TABLE `cloud`.`secondary_storage_vm` DROP COLUMN `vlan_id`; -ALTER TABLE `cloud`.`secondary_storage_vm` DROP COLUMN `ram_size`; #ALTER TABLE `cloud`.`secondary_storage_vm` DROP COLUMN `guid`; #ALTER TABLE `cloud`.`vlan` ADD CONSTRAINT `fk_vlan__network_id` FOREIGN KEY `fk_vlan__network_id`(`network_id`) REFERENCES `networks`(`id`); @@ -95,8 +89,6 @@ DROP TABLE `cloud`.`vm_disk`; ALTER TABLE `cloud`.`disk_offering` DROP COLUMN `mirrored`; -ALTER TABLE `cloud`.`service_offering` DROP COLUMN `guest_ip_type`; - ALTER TABLE `cloud`.`load_balancer_vm_map` DROP COLUMN `pending`; ALTER TABLE `cloud`.`account_vlan_map` MODIFY COLUMN `account_id` bigint unsigned NOT NULL; @@ -121,5 +113,5 @@ ALTER TABLE `cloud`.`template_spool_ref` ADD CONSTRAINT `fk_template_spool_ref__ ALTER TABLE `cloud`.`volumes` MODIFY COLUMN `state` VARCHAR(32) NOT NULL; -ALTER TABLE op_dc_ip_address_alloc ADD CONSTRAINT `fk_op_dc_ip_address_alloc__data_center_id` FOREIGN KEY (`data_center_id`) REFERENCES `data_center`(`id`) ON DELETE CASCADE; +ALTER TABLE `cloud`.`snapshot_policy` ADD KEY `volume_id` (`volume_id`); diff --git a/setup/db/db/schema-21to22.sql b/setup/db/db/schema-21to22.sql index 84af927d2bd..c073780443b 100755 --- a/setup/db/db/schema-21to22.sql +++ b/setup/db/db/schema-21to22.sql @@ -75,8 +75,7 @@ CREATE TABLE `cloud`.`network_offerings` ( `userdata_service` int(1) unsigned NOT NULL DEFAULT 0 COMMENT 'true if network offering provides user data service', `vpn_service` int(1) unsigned NOT NULL DEFAULT 0 COMMENT 'true if network offering provides vpn service', `dhcp_service` int(1) unsigned NOT NULL DEFAULT 0 COMMENT 'true if network offering provides dhcp service', - PRIMARY KEY (`id`), - INDEX `i_network_offerings__removed`(`removed`) + PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `cloud`.`networks` ( @@ -107,13 +106,7 @@ CREATE TABLE `cloud`.`networks` ( `is_default` int(1) unsigned NOT NULL DEFAULT 0 COMMENT '1 if network is default', `created` datetime NOT NULL COMMENT 'date created', `removed` datetime COMMENT 'date removed if not null', - PRIMARY KEY (`id`), - CONSTRAINT `fk_networks__network_offering_id` FOREIGN KEY (`network_offering_id`) REFERENCES `network_offerings`(`id`), - CONSTRAINT `fk_networks__data_center_id` FOREIGN KEY (`data_center_id`) REFERENCES `data_center`(`id`), - CONSTRAINT `fk_networks__related` FOREIGN KEY(`related`) REFERENCES `networks`(`id`), - CONSTRAINT `fk_networks__account_id` FOREIGN KEY(`account_id`) REFERENCES `account`(`id`), - CONSTRAINT `fk_networks__domain_id` FOREIGN KEY(`domain_id`) REFERENCES `domain`(`id`), - INDEX `i_networks__removed`(`removed`) + PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `cloud`.`op_networks`( @@ -170,8 +163,7 @@ CREATE TABLE `cloud`.`nics` ( `removed` datetime COMMENT 'date removed if not null', PRIMARY KEY (`id`), CONSTRAINT `fk_nics__instance_id` FOREIGN KEY `fk_nics__instance_id`(`instance_id`) REFERENCES `vm_instance`(`id`) ON DELETE CASCADE, - CONSTRAINT `fk_nics__networks_id` FOREIGN KEY `fk_nics__networks_id`(`network_id`) REFERENCES `networks`(`id`), - INDEX `i_nics__removed`(`removed`) + CONSTRAINT `fk_nics__networks_id` FOREIGN KEY `fk_nics__networks_id`(`network_id`) REFERENCES `networks`(`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO `network_offerings` VALUES (1,'System-Public-Network','System Offering for System-Public-Network',NULL,NULL,NULL,'Public',NULL,1,0,NULL,now(),NULL,0,'Required',0,0,0,0,0,0,0),(2,'System-Management-Network','System Offering for System-Management-Network',NULL,NULL,NULL,'Management',NULL,1,0,NULL,now(),NULL,0,'Required',0,0,0,0,0,0,0),(3,'System-Control-Network','System Offering for System-Control-Network',NULL,NULL,NULL,'Control',NULL,1,0,NULL,now(),NULL,0,'Required',0,0,0,0,0,0,0),(4,'System-Storage-Network','System Offering for System-Storage-Network',NULL,NULL,NULL,'Storage',NULL,1,0,NULL,now(),NULL,0,'Required',0,0,0,0,0,0,0),(5,'System-Guest-Network','System-Guest-Network',NULL,NULL,NULL,'Guest',NULL,1,0,NULL,now(),NULL,1,'Required',1,0,0,0,1,0,1),(6,'DefaultVirtualizedNetworkOffering','Virtual Vlan',NULL,NULL,NULL,'Guest',NULL,0,0,NULL,now(),NULL,1,'Required',1,1,1,1,1,1,1),(7,'DefaultDirectNetworkOffering','Direct',NULL,NULL,NULL,'Public',NULL,0,0,NULL,now(),NULL,1,'Required',1,0,0,0,1,0,1); @@ -273,7 +265,6 @@ UPDATE `cloud`.`volumes` SET state='Destroy' WHERE removed IS NOT NULL OR destro UPDATE `cloud`.`volumes` SET state='Ready' WHERE removed IS NULL AND (destroyed=0 OR destroyed is NULL) AND status='Created'; ALTER TABLE `cloud`.`vlan` ADD COLUMN `network_id` bigint unsigned NOT NULL; -ALTER TABLE `cloud`.`vlan` ADD CONSTRAINT `fk_vlan__data_center_id` FOREIGN KEY `fk_vlan__data_center_id`(`data_center_id`) REFERENCES `data_center`(`id`); ALTER TABLE `cloud`.`data_center` ADD COLUMN `domain` varchar(100); ALTER TABLE `cloud`.`data_center` ADD COLUMN `domain_id` bigint unsigned; @@ -287,8 +278,6 @@ ALTER TABLE `cloud`.`data_center` ADD COLUMN `vpn_provider` char(64) DEFAULT 'Vi ALTER TABLE `cloud`.`data_center` ADD COLUMN `userdata_provider` char(64) DEFAULT 'VirtualRouter'; ALTER TABLE `cloud`.`data_center` ADD COLUMN `enable` tinyint NOT NULL DEFAULT 1; -ALTER TABLE `cloud`.`data_center` ADD CONSTRAINT `fk_data_center__domain_id` FOREIGN KEY (`domain_id`) REFERENCES `domain`(`id`); - ALTER TABLE `cloud`.`op_dc_ip_address_alloc` ADD COLUMN `reservation_id` char(40) NULL; ALTER TABLE `cloud`.`op_dc_ip_address_alloc` ADD COLUMN `mac_address` bigint unsigned NOT NULL; UPDATE `cloud`.`op_dc_ip_address_alloc` SET reservation_id=concat(cast(instance_id as CHAR), ip_address) WHERE taken is NOT NULL; @@ -968,7 +957,6 @@ ALTER TABLE `cloud`.`instance_group` ADD CONSTRAINT `fk_instance_group__account_ ALTER TABLE `cloud`.`instance_group_vm_map` ADD CONSTRAINT `fk_instance_group_vm_map___group_id` FOREIGN KEY `fk_instance_group_vm_map___group_id` (`group_id`) REFERENCES `instance_group` (`id`) ON DELETE CASCADE; ALTER TABLE `cloud`.`instance_group_vm_map` ADD CONSTRAINT `fk_instance_group_vm_map___instance_id` FOREIGN KEY `fk_instance_group_vm_map___instance_id` (`instance_id`) REFERENCES `user_vm` (`id`) ON DELETE CASCADE; ALTER TABLE `cloud`.`secondary_storage_vm` MODIFY COLUMN `guid` varchar(255); -ALTER TABLE `cloud`.`domain` MODIFY COLUMN `path` varchar(255) NOT NULL; ALTER TABLE `cloud`.`ssh_keypairs` ADD CONSTRAINT `fk_ssh_keypairs__account_id` FOREIGN KEY `fk_ssh_keypair__account_id` (`account_id`) REFERENCES `account` (`id`) ON DELETE CASCADE; ALTER TABLE `cloud`.`ssh_keypairs` ADD CONSTRAINT `fk_ssh_keypairs__domain_id` FOREIGN KEY `fk_ssh_keypair__domain_id` (`domain_id`) REFERENCES `domain` (`id`) ON DELETE CASCADE; @@ -979,3 +967,5 @@ UPDATE vm_template SET hypervisor_type='None' WHERE hypervisor_type IS NULL AND ALTER TABLE `cloud`.`volumes` ADD COLUMN `source_id` bigint unsigned COMMENT 'id for the source'; ALTER TABLE `cloud`.`volumes` ADD COLUMN `source_type` varchar(32) COMMENT 'source from which the volume is created -- snapshot, diskoffering, template, blank'; + +ALTER TABLE `cloud`.`user_statistics` ADD UNIQUE KEY `account_id` (`account_id`,`data_center_id`); diff --git a/setup/db/db/schema-221to222-cleanup.sql b/setup/db/db/schema-221to222-cleanup.sql index ec7a76b718f..987335addbe 100644 --- a/setup/db/db/schema-221to222-cleanup.sql +++ b/setup/db/db/schema-221to222-cleanup.sql @@ -1,2 +1,14 @@ alter table firewall_rules drop column is_static_nat; delete from configuration where name='router.cleanup'; + +ALTER TABLE `cloud`.`console_proxy` DROP COLUMN `ram_size`; + +ALTER TABLE `cloud`.`domain_router` DROP COLUMN `ram_size`; +ALTER TABLE `cloud`.`domain_router` DROP COLUMN `domain`; +ALTER TABLE `cloud`.`domain_router` DROP COLUMN `guest_mac_address`; + +ALTER TABLE `cloud`.`secondary_storage_vm` DROP COLUMN `ram_size`; + +ALTER TABLE `cloud`.`service_offering` DROP COLUMN `guest_ip_type`; + +ALTER TABLE `cloud`.`vm_instance` DROP COLUMN `mirrored_vols`; diff --git a/setup/db/db/schema-221to222.sql b/setup/db/db/schema-221to222.sql index 41b68f71897..30f34410144 100644 --- a/setup/db/db/schema-221to222.sql +++ b/setup/db/db/schema-221to222.sql @@ -34,4 +34,23 @@ CREATE TABLE IF NOT EXISTS `cloud`.`version` ( `step` char(32) NOT NULL COMMENT 'Step in the upgrade to this version', PRIMARY KEY (`id`), INDEX `i_version__version`(`version`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; \ No newline at end of file +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + + +ALTER TABLE `cloud`.`data_center` ADD CONSTRAINT `fk_data_center__domain_id` FOREIGN KEY (`domain_id`) REFERENCES `domain`(`id`); +ALTER TABLE `cloud`.`data_center` ADD INDEX `i_data_center__domain_id`(`domain_id`); + +ALTER TABLE `cloud`.`networks` ADD CONSTRAINT `fk_networks__data_center_id` FOREIGN KEY (`data_center_id`) REFERENCES `data_center` (`id`); +ALTER TABLE `cloud`.`networks` ADD CONSTRAINT `fk_networks__related` FOREIGN KEY (`related`) REFERENCES `networks` (`id`) ON DELETE CASCADE; +ALTER TABLE `cloud`.`networks` ADD CONSTRAINT `fk_networks__account_id` FOREIGN KEY (`account_id`) REFERENCES `account` (`id`); +ALTER TABLE `cloud`.`networks` ADD CONSTRAINT `fk_networks__domain_id` FOREIGN KEY (`domain_id`) REFERENCES `domain` (`id`); + +ALTER TABLE `cloud`.`networks` ADD INDEX `fk_networks__network_offering_id` (`network_offering_id`); +ALTER TABLE `cloud`.`networks` ADD INDEX `fk_networks__data_center_id` (`data_center_id`); +ALTER TABLE `cloud`.`networks` ADD INDEX `fk_networks__account_id` (`account_id`); +ALTER TABLE `cloud`.`networks` ADD INDEX `fk_networks__domain_id` (`domain_id`); +ALTER TABLE `cloud`.`networks` ADD INDEX `i_networks__removed` (`removed`); + +ALTER TABLE `cloud`.`op_dc_ip_address_alloc` ADD CONSTRAINT `fk_op_dc_ip_address_alloc__data_center_id` FOREIGN KEY (`data_center_id`) REFERENCES `data_center`(`id`) ON DELETE CASCADE; + +ALTER TABLE `cloud`.`vlan` ADD CONSTRAINT `fk_vlan__data_center_id` FOREIGN KEY `fk_vlan__data_center_id`(`data_center_id`) REFERENCES `data_center`(`id`); diff --git a/setup/db/db/schema-222to224-cleanup.sql b/setup/db/db/schema-222to224-cleanup.sql index f10a2f6ec9f..b5680b42513 100644 --- a/setup/db/db/schema-222to224-cleanup.sql +++ b/setup/db/db/schema-222to224-cleanup.sql @@ -1,3 +1,6 @@ +DROP TABLE IF EXISTS `cloud`.`host_master`; +DROP TABLE IF EXISTS `cloud`.`hypervsior_properties`; + ALTER TABLE `cloud`.`data_center` DROP COLUMN `enable`; ALTER TABLE `cloud`.`host_pod_ref` DROP COLUMN `enabled`; DELETE FROM `cloud`.`configuration` WHERE name in ('direct.attach.security.groups.enabled', 'direct.attach.untagged.vlan.enabled', 'hypervisor.type', 'management-server', 'max.volume.size.gb', 'multicast.throttling.rate', 'network.type', 'xen.preallocated.lun.size.range'); @@ -10,3 +13,10 @@ ALTER TABLE `cloud`.`volumes` DROP COLUMN `source_type`; ALTER TABLE `cloud`.`user_vm` DROP COLUMN guest_ip_address; ALTER TABLE `cloud`.`user_vm` DROP COLUMN guest_mac_address; ALTER TABLE `cloud`.`user_vm` DROP COLUMN guest_netmask; + +ALTER TABLE `cloud`.`secondary_storage_vm` DROP KEY `public_ip_address_2`; + +ALTER TABLE `cloud`.`snapshot_policy` DROP KEY `volume_id`; +ALTER TABLE `cloud`.`snapshot_policy` DROP KEY `i_snapshot_policy__volume_id`; + + diff --git a/setup/db/db/schema-222to224.sql b/setup/db/db/schema-222to224.sql index 5881fd22c28..ffa3f35721f 100644 --- a/setup/db/db/schema-222to224.sql +++ b/setup/db/db/schema-222to224.sql @@ -137,5 +137,22 @@ UPDATE `cloud`.`host` SET resource='com.cloud.hypervisor.xen.resource.XenServer5 UPDATE `cloud`.`nics` SET ip_type='Ip4'; UPDATE `cloud`.`nics` SET broadcast_uri='vlan://untagged', isolation_uri='ec2://untagged', strategy='Create' where reserver_name='DirectPodBasedNetworkGuru'; + DELETE FROM `cloud`.`configuration` where name='schema.level'; DELETE FROM `cloud`.`configuration` where name='direct.attach.security.groups.enabled'; + +ALTER TABLE `cloud`.`domain` MODIFY COLUMN `path` varchar(255) NOT NULL; + +ALTER TABLE `cloud`.`network_offerings` ADD INDEX `i_network_offerings__removed`(`removed`); + +ALTER TABLE `cloud`.`nics` ADD INDEX `i_nics__removed`(`removed`); + +ALTER TABLE `cloud`.`snapshot_schedule` DROP KEY `volume_id`; +ALTER TABLE `cloud`.`snapshot_schedule` ADD UNIQUE KEY `volume_id` (`volume_id`,`policy_id`); + +ALTER TABLE `cloud`.`storage_pool` MODIFY COLUMN `uuid` varchar(255) UNIQUE; + +ALTER TABLE `cloud`.`user_statistics` DROP KEY `account_id`; +ALTER TABLE `cloud`.`user_statistics` ADD UNIQUE KEY `account_id` (`account_id`,`data_center_id`, `public_ip_address`, `device_id`,`device_type`); + + diff --git a/utils/src/com/cloud/utils/net/NetUtils.java b/utils/src/com/cloud/utils/net/NetUtils.java index ba153c45cc3..b73ae061bf5 100755 --- a/utils/src/com/cloud/utils/net/NetUtils.java +++ b/utils/src/com/cloud/utils/net/NetUtils.java @@ -48,143 +48,142 @@ public class NetUtils { public final static int VPN_NATT_PORT = 4500; public final static int VPN_L2TP_PORT = 1701; - public final static String UDP_PROTO = "udp"; public final static String TCP_PROTO = "tcp"; public final static String ANY_PROTO = "any"; public final static String ICMP_PROTO = "icmp"; - + private final static Random _rand = new Random(System.currentTimeMillis()); public static long createSequenceBasedMacAddress(long macAddress) { - return macAddress | 0x060000000000l | (((long)_rand.nextInt(32768) << 25) & 0x00fffe000000l); + return macAddress | 0x060000000000l | (((long) _rand.nextInt(32768) << 25) & 0x00fffe000000l); } - - public static String getHostName() { + + public static String getHostName() { try { InetAddress localAddr = InetAddress.getLocalHost(); - if(localAddr != null) { - return localAddr.getHostName(); + if (localAddr != null) { + return localAddr.getHostName(); } - } catch(UnknownHostException e) { - s_logger.warn("UnknownHostException when trying to get host name. ", e); + } catch (UnknownHostException e) { + s_logger.warn("UnknownHostException when trying to get host name. ", e); } return "localhost"; - } - + } + public static InetAddress getLocalInetAddress() { try { return InetAddress.getLocalHost(); - } catch(UnknownHostException e) { - s_logger.warn("UnknownHostException in getLocalInetAddress().", e); + } catch (UnknownHostException e) { + s_logger.warn("UnknownHostException in getLocalInetAddress().", e); return null; } } - + public static String resolveToIp(String host) { - try { - InetAddress addr = InetAddress.getByName(host); - return ipFromInetAddress(addr); - } catch (UnknownHostException e) { - s_logger.warn("Unable to resolve " + host + " to IP due to UnknownHostException"); - return null; - } + try { + InetAddress addr = InetAddress.getByName(host); + return ipFromInetAddress(addr); + } catch (UnknownHostException e) { + s_logger.warn("Unable to resolve " + host + " to IP due to UnknownHostException"); + return null; + } } - + public static InetAddress[] getAllLocalInetAddresses() { - List addrList = new ArrayList(); - try { - for(NetworkInterface ifc : IteratorUtil.enumerationAsIterable(NetworkInterface.getNetworkInterfaces())) { - if(ifc.isUp() && !ifc.isVirtual()) { - for(InetAddress addr : IteratorUtil.enumerationAsIterable(ifc.getInetAddresses())) { - addrList.add(addr); - } - } - } - } catch (SocketException e) { - s_logger.warn("SocketException in getAllLocalInetAddresses().", e); - } - - InetAddress[] addrs = new InetAddress[addrList.size()]; - if(addrList.size() > 0) { + List addrList = new ArrayList(); + try { + for (NetworkInterface ifc : IteratorUtil.enumerationAsIterable(NetworkInterface.getNetworkInterfaces())) { + if (ifc.isUp() && !ifc.isVirtual()) { + for (InetAddress addr : IteratorUtil.enumerationAsIterable(ifc.getInetAddresses())) { + addrList.add(addr); + } + } + } + } catch (SocketException e) { + s_logger.warn("SocketException in getAllLocalInetAddresses().", e); + } + + InetAddress[] addrs = new InetAddress[addrList.size()]; + if (addrList.size() > 0) { System.arraycopy(addrList.toArray(), 0, addrs, 0, addrList.size()); } - return addrs; + return addrs; } - - public static String[] getLocalCidrs() { - List cidrList = new ArrayList(); - try { - for(NetworkInterface ifc : IteratorUtil.enumerationAsIterable(NetworkInterface.getNetworkInterfaces())) { - if(ifc.isUp() && !ifc.isVirtual() && !ifc.isLoopback()) { - for (InterfaceAddress address : ifc.getInterfaceAddresses()) { - InetAddress addr = address.getAddress(); - int prefixLength = address.getNetworkPrefixLength(); - if(prefixLength < 32 && prefixLength > 0) { - String ip = ipFromInetAddress(addr); - cidrList.add(ipAndNetMaskToCidr(ip, getCidrNetmask(prefixLength))); - } - } - } - } - } catch(SocketException e) { - s_logger.warn("UnknownHostException in getLocalCidrs().", e); - } - - return cidrList.toArray(new String[0]); + + public static String[] getLocalCidrs() { + List cidrList = new ArrayList(); + try { + for (NetworkInterface ifc : IteratorUtil.enumerationAsIterable(NetworkInterface.getNetworkInterfaces())) { + if (ifc.isUp() && !ifc.isVirtual() && !ifc.isLoopback()) { + for (InterfaceAddress address : ifc.getInterfaceAddresses()) { + InetAddress addr = address.getAddress(); + int prefixLength = address.getNetworkPrefixLength(); + if (prefixLength < 32 && prefixLength > 0) { + String ip = ipFromInetAddress(addr); + cidrList.add(ipAndNetMaskToCidr(ip, getCidrNetmask(prefixLength))); + } + } + } + } + } catch (SocketException e) { + s_logger.warn("UnknownHostException in getLocalCidrs().", e); + } + + return cidrList.toArray(new String[0]); } - + public static InetAddress getFirstNonLoopbackLocalInetAddress() { - InetAddress[] addrs = getAllLocalInetAddresses(); - if(addrs != null) { - for(InetAddress addr : addrs) { - if(s_logger.isInfoEnabled()) { + InetAddress[] addrs = getAllLocalInetAddresses(); + if (addrs != null) { + for (InetAddress addr : addrs) { + if (s_logger.isInfoEnabled()) { s_logger.info("Check local InetAddress : " + addr.toString() + ", total count :" + addrs.length); } - - if(!addr.isLoopbackAddress()) { + + if (!addr.isLoopbackAddress()) { return addr; } - } - } - - s_logger.warn("Unable to determine a non-loopback address, local inet address count :" + addrs.length); - return null; + } + } + + s_logger.warn("Unable to determine a non-loopback address, local inet address count :" + addrs.length); + return null; } - + public static InetAddress[] getInterfaceInetAddresses(String ifName) { - List addrList = new ArrayList(); - try { - for(NetworkInterface ifc : IteratorUtil.enumerationAsIterable(NetworkInterface.getNetworkInterfaces())) { - if(ifc.isUp() && !ifc.isVirtual() && ifc.getName().equals(ifName)) { - for(InetAddress addr : IteratorUtil.enumerationAsIterable(ifc.getInetAddresses())) { - addrList.add(addr); - } - } - } - } catch (SocketException e) { - s_logger.warn("SocketException in getAllLocalInetAddresses().", e); - } - - InetAddress[] addrs = new InetAddress[addrList.size()]; - if(addrList.size() > 0) { + List addrList = new ArrayList(); + try { + for (NetworkInterface ifc : IteratorUtil.enumerationAsIterable(NetworkInterface.getNetworkInterfaces())) { + if (ifc.isUp() && !ifc.isVirtual() && ifc.getName().equals(ifName)) { + for (InetAddress addr : IteratorUtil.enumerationAsIterable(ifc.getInetAddresses())) { + addrList.add(addr); + } + } + } + } catch (SocketException e) { + s_logger.warn("SocketException in getAllLocalInetAddresses().", e); + } + + InetAddress[] addrs = new InetAddress[addrList.size()]; + if (addrList.size() > 0) { System.arraycopy(addrList.toArray(), 0, addrs, 0, addrList.size()); } - return addrs; + return addrs; } - + public static String getLocalIPString() { InetAddress addr = getLocalInetAddress(); - if(addr != null) { - return ipFromInetAddress(addr); + if (addr != null) { + return ipFromInetAddress(addr); } return new String("127.0.0.1"); } - + public static String ipFromInetAddress(InetAddress addr) { - assert(addr != null); - + assert (addr != null); + byte[] ipBytes = addr.getAddress(); StringBuffer sb = new StringBuffer(); sb.append(ipBytes[0] & 0xff).append("."); @@ -196,92 +195,92 @@ public class NetUtils { } public static boolean isLocalAddress(InetAddress addr) { - InetAddress[] addrs = getAllLocalInetAddresses(); - - if(addrs != null) { - for(InetAddress self : addrs) { - if(self.equals(addr)) { + InetAddress[] addrs = getAllLocalInetAddresses(); + + if (addrs != null) { + for (InetAddress self : addrs) { + if (self.equals(addr)) { return true; } - } - } - return false; - } - - public static boolean isLocalAddress(String strAddress) { - - InetAddress addr; - try { - addr = InetAddress.getByName(strAddress); - return isLocalAddress(addr); - } catch (UnknownHostException e) { - } - return false; - } - - public static String getMacAddress(InetAddress address) { - StringBuffer sb = new StringBuffer(); - Formatter formatter = new Formatter(sb); - try { - NetworkInterface ni = NetworkInterface.getByInetAddress(address); - byte[] mac = ni.getHardwareAddress(); - - for (int i = 0; i < mac.length; i++) { - formatter.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : ""); - } - } catch (SocketException e) { - s_logger.error("SocketException when trying to retrieve MAC address", e); - } - return sb.toString(); - } - - public static long getMacAddressAsLong(InetAddress address) { - long macAddressAsLong = 0; - try { - NetworkInterface ni = NetworkInterface.getByInetAddress(address); - byte[] mac = ni.getHardwareAddress(); - - for (int i = 0; i < mac.length; i++) { - macAddressAsLong |= ((long)(mac[i] & 0xff) << (mac.length - i - 1)*8); } - - } catch (SocketException e) { - s_logger.error("SocketException when trying to retrieve MAC address", e); - } - - return macAddressAsLong; + } + return false; } - + + public static boolean isLocalAddress(String strAddress) { + + InetAddress addr; + try { + addr = InetAddress.getByName(strAddress); + return isLocalAddress(addr); + } catch (UnknownHostException e) { + } + return false; + } + + public static String getMacAddress(InetAddress address) { + StringBuffer sb = new StringBuffer(); + Formatter formatter = new Formatter(sb); + try { + NetworkInterface ni = NetworkInterface.getByInetAddress(address); + byte[] mac = ni.getHardwareAddress(); + + for (int i = 0; i < mac.length; i++) { + formatter.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : ""); + } + } catch (SocketException e) { + s_logger.error("SocketException when trying to retrieve MAC address", e); + } + return sb.toString(); + } + + public static long getMacAddressAsLong(InetAddress address) { + long macAddressAsLong = 0; + try { + NetworkInterface ni = NetworkInterface.getByInetAddress(address); + byte[] mac = ni.getHardwareAddress(); + + for (int i = 0; i < mac.length; i++) { + macAddressAsLong |= ((long) (mac[i] & 0xff) << (mac.length - i - 1) * 8); + } + + } catch (SocketException e) { + s_logger.error("SocketException when trying to retrieve MAC address", e); + } + + return macAddressAsLong; + } + public static boolean ipRangesOverlap(String startIp1, String endIp1, String startIp2, String endIp2) { - long startIp1Long = ip2Long(startIp1); - long endIp1Long = startIp1Long; - if (endIp1 != null) { - endIp1Long = ip2Long(endIp1); - } - long startIp2Long = ip2Long(startIp2); - long endIp2Long = startIp2Long; - if (endIp2 != null) { - endIp2Long = ip2Long(endIp2); - } - - if (startIp1Long == startIp2Long || startIp1Long == endIp2Long || endIp1Long == startIp2Long || endIp1Long == endIp2Long) { - return true; - } else if (startIp1Long > startIp2Long && startIp1Long < endIp2Long) { - return true; - } else if (endIp1Long > startIp2Long && endIp1Long < endIp2Long) { - return true; - } else if (startIp2Long > startIp1Long && startIp2Long < endIp1Long) { - return true; - } else if (endIp2Long > startIp1Long && endIp2Long < endIp1Long) { - return true; - } else { - return false; - } + long startIp1Long = ip2Long(startIp1); + long endIp1Long = startIp1Long; + if (endIp1 != null) { + endIp1Long = ip2Long(endIp1); + } + long startIp2Long = ip2Long(startIp2); + long endIp2Long = startIp2Long; + if (endIp2 != null) { + endIp2Long = ip2Long(endIp2); + } + + if (startIp1Long == startIp2Long || startIp1Long == endIp2Long || endIp1Long == startIp2Long || endIp1Long == endIp2Long) { + return true; + } else if (startIp1Long > startIp2Long && startIp1Long < endIp2Long) { + return true; + } else if (endIp1Long > startIp2Long && endIp1Long < endIp2Long) { + return true; + } else if (startIp2Long > startIp1Long && startIp2Long < endIp1Long) { + return true; + } else if (endIp2Long > startIp1Long && endIp2Long < endIp1Long) { + return true; + } else { + return false; + } } - + public static long ip2Long(String ip) { String[] tokens = ip.split("[.]"); - assert(tokens.length == 4); + assert (tokens.length == 4); long result = 0; for (int i = 0; i < tokens.length; i++) { try { @@ -290,23 +289,23 @@ public class NetUtils { throw new RuntimeException("Incorrect number", e); } } - + return result; } - + public static String long2Ip(long ip) { StringBuilder result = new StringBuilder(15); result.append((ip >> 24 & 0xff)).append("."); result.append((ip >> 16 & 0xff)).append("."); result.append((ip >> 8 & 0xff)).append("."); result.append(ip & 0xff); - + return result.toString(); } - + public static long mac2Long(String macAddress) { String[] tokens = macAddress.split(":"); - assert(tokens.length == 6); + assert (tokens.length == 6); long result = 0; for (int i = 0; i < tokens.length; i++) { result = result << 8; @@ -314,7 +313,7 @@ public class NetUtils { } return result; } - + public static String[] getNicParams(String nicName) { try { NetworkInterface nic = NetworkInterface.getByName(nicName); @@ -323,7 +322,7 @@ public class NetUtils { return null; } } - + public static String[] getNetworkParams(NetworkInterface nic) { List addrs = nic.getInterfaceAddresses(); if (addrs == null || addrs.size() == 0) { @@ -332,10 +331,7 @@ public class NetUtils { InterfaceAddress addr = null; for (InterfaceAddress iaddr : addrs) { InetAddress inet = iaddr.getAddress(); - if (!inet.isLinkLocalAddress() && - !inet.isLoopbackAddress() && - !inet.isMulticastAddress() && - inet.getAddress().length == 4) { + if (!inet.isLinkLocalAddress() && !inet.isLoopbackAddress() && !inet.isMulticastAddress() && inet.getAddress().length == 4) { addr = iaddr; break; } @@ -350,20 +346,20 @@ public class NetUtils { result[1] = byte2Mac(mac); } catch (Exception e) { } - + result[2] = prefix2Netmask(addr.getNetworkPrefixLength()); return result; } - + public static String prefix2Netmask(short prefix) { long addr = 0; for (int i = 0; i < prefix; i++) { addr = addr | (1 << (31 - i)); } - + return long2Ip(addr); } - + public static String byte2Mac(byte[] m) { StringBuilder result = new StringBuilder(17); Formatter formatter = new Formatter(result); @@ -374,95 +370,90 @@ public class NetUtils { public static String long2Mac(long macAddress) { StringBuilder result = new StringBuilder(17); Formatter formatter = new Formatter(result); - formatter.format("%02x:%02x:%02x:%02x:%02x:%02x", - (macAddress >> 40) & 0xff, - (macAddress >> 32) & 0xff, - (macAddress >> 24) & 0xff, - (macAddress >> 16) & 0xff, - (macAddress >> 8) & 0xff, - (macAddress & 0xff)); - + formatter.format("%02x:%02x:%02x:%02x:%02x:%02x", (macAddress >> 40) & 0xff, (macAddress >> 32) & 0xff, (macAddress >> 24) & 0xff, (macAddress >> 16) & 0xff, (macAddress >> 8) & 0xff, + (macAddress & 0xff)); + return result.toString(); } - + public static boolean isValidPrivateIp(String ipAddress, String guestIPAddress) { - - InetAddress privIp = parseIpAddress(ipAddress); - if (privIp == null) { + + InetAddress privIp = parseIpAddress(ipAddress); + if (privIp == null) { return false; } - if (!privIp.isSiteLocalAddress()) { - return false; - } - - String firstGuestOctet = "10"; - if (guestIPAddress != null && !guestIPAddress.isEmpty()) { - String[] guestIPList = guestIPAddress.split("\\."); - firstGuestOctet = guestIPList[0]; - } - - String[] ipList = ipAddress.split("\\."); - if (!ipList[0].equals(firstGuestOctet)) { + if (!privIp.isSiteLocalAddress()) { return false; } - - return true; + + String firstGuestOctet = "10"; + if (guestIPAddress != null && !guestIPAddress.isEmpty()) { + String[] guestIPList = guestIPAddress.split("\\."); + firstGuestOctet = guestIPList[0]; + } + + String[] ipList = ipAddress.split("\\."); + if (!ipList[0].equals(firstGuestOctet)) { + return false; + } + + return true; } - + public static boolean isSiteLocalAddress(String ipAddress) { - if (ipAddress == null) { - return false; - } else { - InetAddress ip = parseIpAddress(ipAddress); - return ip.isSiteLocalAddress(); - } + if (ipAddress == null) { + return false; + } else { + InetAddress ip = parseIpAddress(ipAddress); + return ip.isSiteLocalAddress(); + } } - + public static boolean validIpRange(String startIP, String endIP) { - if (endIP == null || endIP.isEmpty()) { + if (endIP == null || endIP.isEmpty()) { return true; } - - long startIPLong = NetUtils.ip2Long(startIP); - long endIPLong = NetUtils.ip2Long(endIP); - return (startIPLong <= endIPLong); + + long startIPLong = NetUtils.ip2Long(startIP); + long endIPLong = NetUtils.ip2Long(endIP); + return (startIPLong <= endIPLong); } - - public static boolean isValidIp(final String ip) { - final String[] ipAsList = ip.split("\\."); - - // The IP address must have four octets - if (Array.getLength(ipAsList) != 4) { - return false; - } - - for (int i = 0; i < 4; i++) { - // Each octet must be an integer - final String octetString = ipAsList[i]; - int octet; - try { - octet = Integer.parseInt(octetString); - } catch(final Exception e) { - return false; - } - // Each octet must be between 0 and 255, inclusive - if (octet < 0 || octet > 255) { + + public static boolean isValidIp(final String ip) { + final String[] ipAsList = ip.split("\\."); + + // The IP address must have four octets + if (Array.getLength(ipAsList) != 4) { + return false; + } + + for (int i = 0; i < 4; i++) { + // Each octet must be an integer + final String octetString = ipAsList[i]; + int octet; + try { + octet = Integer.parseInt(octetString); + } catch (final Exception e) { + return false; + } + // Each octet must be between 0 and 255, inclusive + if (octet < 0 || octet > 255) { return false; } - // Each octetString must have between 1 and 3 characters - if (octetString.length() < 1 || octetString.length() > 3) { + // Each octetString must have between 1 and 3 characters + if (octetString.length() < 1 || octetString.length() > 3) { return false; } - - } - - // IP is good, return true - return true; - } - - public static boolean isValidCIDR(final String cidr) { - if (cidr == null || cidr.isEmpty()) { + + } + + // IP is good, return true + return true; + } + + public static boolean isValidCIDR(final String cidr) { + if (cidr == null || cidr.isEmpty()) { return false; } String[] cidrPair = cidr.split("\\/"); @@ -475,65 +466,65 @@ public class NetUtils { return false; } int cidrSizeNum = -1; - + try { - cidrSizeNum = Integer.parseInt(cidrSize); + cidrSizeNum = Integer.parseInt(cidrSize); } catch (Exception e) { - return false; + return false; } - + if (cidrSizeNum < 0 || cidrSizeNum > 32) { return false; } - + return true; - } - - public static boolean isValidNetmask(String netmask) { - if (!isValidIp(netmask)) { + } + + public static boolean isValidNetmask(String netmask) { + if (!isValidIp(netmask)) { return false; } - - long ip = ip2Long(netmask); - int count = 0; - boolean finished = false; - for (int i = 31; i >= 0; i--) { - if (((ip >> i) & 0x1) == 0) { - finished = true; - } else { - if (finished) { + + long ip = ip2Long(netmask); + int count = 0; + boolean finished = false; + for (int i = 31; i >= 0; i--) { + if (((ip >> i) & 0x1) == 0) { + finished = true; + } else { + if (finished) { return false; } - count += 1; - } - } - - if (count == 0) { + count += 1; + } + } + + if (count == 0) { return false; } - - return true; - } - - private static InetAddress parseIpAddress(String address) { - StringTokenizer st = new StringTokenizer(address, "."); - byte[] bytes = new byte[4]; - if (st.countTokens() == 4) { - try { - for (int i = 0; i < 4; i++){ - bytes[i] = (byte)Integer.parseInt(st.nextToken()); - } - return InetAddress.getByAddress(address, bytes); - } catch (NumberFormatException nfe) { - return null; - } catch (UnknownHostException uhe){ - return null; - } - } - return null; + return true; } - + + private static InetAddress parseIpAddress(String address) { + StringTokenizer st = new StringTokenizer(address, "."); + byte[] bytes = new byte[4]; + + if (st.countTokens() == 4) { + try { + for (int i = 0; i < 4; i++) { + bytes[i] = (byte) Integer.parseInt(st.nextToken()); + } + return InetAddress.getByAddress(address, bytes); + } catch (NumberFormatException nfe) { + return null; + } catch (UnknownHostException uhe) { + return null; + } + } + return null; + } + public static String getCidrFromGatewayAndNetmask(String gatewayStr, String netmaskStr) { long netmask = ip2Long(netmaskStr); long gateway = ip2Long(gatewayStr); @@ -541,7 +532,7 @@ public class NetUtils { long size = getCidrSize(netmaskStr); return long2Ip(firstPart) + "/" + size; } - + public static String[] getIpRangeFromCidr(String cidr, long size) { assert (size < 32) : "You do know this is not for ipv6 right? Keep it smaller than 32 but you have " + size; String[] result = new String[2]; @@ -549,18 +540,18 @@ public class NetUtils { long startNetMask = ip2Long(getCidrNetmask(size)); long start = (ip & startNetMask) + 1; long end = start; - + end = end >> (32 - size); - + end++; end = (end << (32 - size)) - 2; - + result[0] = long2Ip(start); result[1] = long2Ip(end); - + return result; } - + public static Set getAllIpsFromCidr(String cidr, long size) { assert (size < 32) : "You do know this is not for ipv6 right? Keep it smaller than 32 but you have " + size; Set result = new TreeSet(); @@ -568,120 +559,119 @@ public class NetUtils { long startNetMask = ip2Long(getCidrNetmask(size)); long start = (ip & startNetMask) + 2; long end = start; - + end = end >> (32 - size); - + end++; end = (end << (32 - size)) - 2; while (start <= end) { - result.add(start); - start++; + result.add(start); + start++; } - - + return result; } - + public static String getIpRangeStartIpFromCidr(String cidr, long size) { long ip = ip2Long(cidr); long startNetMask = ip2Long(getCidrNetmask(size)); long start = (ip & startNetMask) + 1; return long2Ip(start); } - + public static String getIpRangeEndIpFromCidr(String cidr, long size) { long ip = ip2Long(cidr); long startNetMask = ip2Long(getCidrNetmask(size)); long start = (ip & startNetMask) + 1; long end = start; end = end >> (32 - size); - + end++; end = (end << (32 - size)) - 2; - return long2Ip(end); + return long2Ip(end); } - + public static boolean sameSubnet(final String ip1, final String ip2, final String netmask) { - if (ip1 == null || ip1.isEmpty() || ip2 == null || ip2.isEmpty()) { + if (ip1 == null || ip1.isEmpty() || ip2 == null || ip2.isEmpty()) { return true; } - String subnet1 = NetUtils.getSubNet(ip1, netmask); - String subnet2 = NetUtils.getSubNet(ip2, netmask); - - return (subnet1.equals(subnet2)); + String subnet1 = NetUtils.getSubNet(ip1, netmask); + String subnet2 = NetUtils.getSubNet(ip2, netmask); + + return (subnet1.equals(subnet2)); } - + public static boolean sameSubnetCIDR(final String ip1, final String ip2, final long cidrSize) { - if (ip1 == null || ip1.isEmpty() || ip2 == null || ip2.isEmpty()) { + if (ip1 == null || ip1.isEmpty() || ip2 == null || ip2.isEmpty()) { return true; } - String subnet1 = NetUtils.getCidrSubNet(ip1, cidrSize); - String subnet2 = NetUtils.getCidrSubNet(ip2, cidrSize); - - return (subnet1.equals(subnet2)); + String subnet1 = NetUtils.getCidrSubNet(ip1, cidrSize); + String subnet2 = NetUtils.getCidrSubNet(ip2, cidrSize); + + return (subnet1.equals(subnet2)); } - + public static String getSubNet(String ip, String netmask) { long ipAddr = ip2Long(ip); long subnet = ip2Long(netmask); long result = ipAddr & subnet; return long2Ip(result); } - + public static String getCidrSubNet(String ip, long cidrSize) { - long numericNetmask = (0xffffffff >> (32 - cidrSize)) << (32 - cidrSize); - String netmask = NetUtils.long2Ip(numericNetmask); - return getSubNet(ip, netmask); + long numericNetmask = (0xffffffff >> (32 - cidrSize)) << (32 - cidrSize); + String netmask = NetUtils.long2Ip(numericNetmask); + return getSubNet(ip, netmask); } - + public static String ipAndNetMaskToCidr(String ip, String netmask) { - long ipAddr = ip2Long(ip); - long subnet = ip2Long(netmask); - long result = ipAddr & subnet; - int bits = (subnet == 0)?0:1; - long subnet2 = subnet; - while ((subnet2 = (subnet2 >> 1) & subnet) != 0 ) { + long ipAddr = ip2Long(ip); + long subnet = ip2Long(netmask); + long result = ipAddr & subnet; + int bits = (subnet == 0) ? 0 : 1; + long subnet2 = subnet; + while ((subnet2 = (subnet2 >> 1) & subnet) != 0) { bits++; } - return long2Ip(result) + "/" + Integer.toString(bits); + return long2Ip(result) + "/" + Integer.toString(bits); } - - public static String [] ipAndNetMaskToRange(String ip, String netmask) { - long ipAddr = ip2Long(ip); - long subnet = ip2Long(netmask); - long start = (ipAddr & subnet) + 1; + + public static String[] ipAndNetMaskToRange(String ip, String netmask) { + long ipAddr = ip2Long(ip); + long subnet = ip2Long(netmask); + long start = (ipAddr & subnet) + 1; long end = start; - int bits = (subnet == 0)?0:1; - while ((subnet = (subnet >> 1) & subnet) != 0 ) { + int bits = (subnet == 0) ? 0 : 1; + while ((subnet = (subnet >> 1) & subnet) != 0) { bits++; } end = end >> (32 - bits); - + end++; end = (end << (32 - bits)) - 2; - - return new String[] {long2Ip(start), long2Ip(end)}; - + + return new String[] { long2Ip(start), long2Ip(end) }; + } - + public static Pair getCidr(String cidr) { String[] tokens = cidr.split("/"); return new Pair(tokens[0], Integer.parseInt(tokens[1])); } - + public static boolean isNetworkAWithinNetworkB(String cidrA, String cidrB) { - Long[] cidrALong = cidrToLong(cidrA); - Long[] cidrBLong = cidrToLong(cidrB); - if (cidrALong == null || cidrBLong == null) { - return false; - } - long shift = 32 - cidrBLong[1]; - return ((cidrALong[0]>>shift) == (cidrBLong[0]>>shift)); + Long[] cidrALong = cidrToLong(cidrA); + Long[] cidrBLong = cidrToLong(cidrB); + if (cidrALong == null || cidrBLong == null) { + return false; + } + long shift = 32 - cidrBLong[1]; + return ((cidrALong[0] >> shift) == (cidrBLong[0] >> shift)); } - + public static Long[] cidrToLong(String cidr) { - if (cidr == null || cidr.isEmpty()) { + if (cidr == null || cidr.isEmpty()) { return null; } String[] cidrPair = cidr.split("\\/"); @@ -694,21 +684,21 @@ public class NetUtils { return null; } int cidrSizeNum = -1; - + try { - cidrSizeNum = Integer.parseInt(cidrSize); + cidrSizeNum = Integer.parseInt(cidrSize); } catch (Exception e) { - return null; + return null; } - long numericNetmask = (0xffffffff >> (32 - cidrSizeNum)) << (32 - cidrSizeNum); - long ipAddr = ip2Long(cidrAddress); - Long[] cidrlong = {ipAddr & numericNetmask, (long)cidrSizeNum}; - return cidrlong; + long numericNetmask = (0xffffffff >> (32 - cidrSizeNum)) << (32 - cidrSizeNum); + long ipAddr = ip2Long(cidrAddress); + Long[] cidrlong = { ipAddr & numericNetmask, (long) cidrSizeNum }; + return cidrlong; } - + public static String getCidrSubNet(String cidr) { - if (cidr == null || cidr.isEmpty()) { + if (cidr == null || cidr.isEmpty()) { return null; } String[] cidrPair = cidr.split("\\/"); @@ -721,149 +711,149 @@ public class NetUtils { return null; } int cidrSizeNum = -1; - + try { - cidrSizeNum = Integer.parseInt(cidrSize); + cidrSizeNum = Integer.parseInt(cidrSize); } catch (Exception e) { - return null; + return null; } - long numericNetmask = (0xffffffff >> (32 - cidrSizeNum)) << (32 - cidrSizeNum); - String netmask = NetUtils.long2Ip(numericNetmask); - return getSubNet(cidrAddress, netmask); + long numericNetmask = (0xffffffff >> (32 - cidrSizeNum)) << (32 - cidrSizeNum); + String netmask = NetUtils.long2Ip(numericNetmask); + return getSubNet(cidrAddress, netmask); } - + public static String getCidrNetmask(long cidrSize) { - long numericNetmask = (0xffffffff >> (32 - cidrSize)) << (32 - cidrSize); - return long2Ip(numericNetmask); + long numericNetmask = (0xffffffff >> (32 - cidrSize)) << (32 - cidrSize); + return long2Ip(numericNetmask); } - + public static String getCidrNetmask(String cidr) { String[] cidrPair = cidr.split("\\/"); long guestCidrSize = Long.parseLong(cidrPair[1]); return getCidrNetmask(guestCidrSize); } - + public static String cidr2Netmask(String cidr) { String[] tokens = cidr.split("\\/"); return getCidrNetmask(Integer.parseInt(tokens[1])); } - + public static long getCidrSize(String netmask) { - long ip = ip2Long(netmask); - int count = 0; - for (int i = 0; i < 32; i++) { - if (((ip >> i) & 0x1) == 0) { - count++; - } else { - break; - } - } - - return 32 - count; + long ip = ip2Long(netmask); + int count = 0; + for (int i = 0; i < 32; i++) { + if (((ip >> i) & 0x1) == 0) { + count++; + } else { + break; + } + } + + return 32 - count; } - + public static boolean isValidPort(String p) { - try { - int port = Integer.parseInt(p); - return !(port > 65535 || port < 1); - } catch (NumberFormatException e) { - return false; - } + try { + int port = Integer.parseInt(p); + return !(port > 65535 || port < 1); + } catch (NumberFormatException e) { + return false; + } } - + public static boolean isValidPort(int p) { return !(p > 65535 || p < 1); } - + public static boolean isValidLBPort(String p) { - try { - int port = Integer.parseInt(p); - return !(port > 65535 || port < 1); - } catch (NumberFormatException e) { - return false; - } + try { + int port = Integer.parseInt(p); + return !(port > 65535 || port < 1); + } catch (NumberFormatException e) { + return false; + } } - + public static boolean isValidProto(String p) { - String proto = p.toLowerCase(); - return (proto.equals("tcp") || proto.equals("udp") || proto.equals("icmp")); + String proto = p.toLowerCase(); + return (proto.equals("tcp") || proto.equals("udp") || proto.equals("icmp")); } - + public static boolean isValidSecurityGroupProto(String p) { - String proto = p.toLowerCase(); - return (proto.equals("tcp") || proto.equals("udp") || proto.equals("icmp") || proto.equals("all")); + String proto = p.toLowerCase(); + return (proto.equals("tcp") || proto.equals("udp") || proto.equals("icmp") || proto.equals("all")); } - + public static boolean isValidAlgorithm(String p) { - String algo = p.toLowerCase(); - return (algo.equals("roundrobin") || algo.equals("leastconn") || algo.equals("source")); + String algo = p.toLowerCase(); + return (algo.equals("roundrobin") || algo.equals("leastconn") || algo.equals("source")); } - + public static String getLinkLocalNetMask() { - return "255.255.0.0"; + return "255.255.0.0"; } - + public static String getLinkLocalGateway() { - return "169.254.0.1"; + return "169.254.0.1"; } - + public static String getLinkLocalCIDR() { - return "169.254.0.0/16"; + return "169.254.0.0/16"; } - + public static String[] getLinkLocalIPRange(int size) { - if (size > 16 || size <= 0) { - return null; - } - /*reserve gateway*/ - String[] range = getIpRangeFromCidr(getLinkLocalGateway(), 32 - size); - - if (range[0].equalsIgnoreCase(getLinkLocalGateway())) { - /*remove the gateway*/ - long ip = ip2Long(range[0]); - ip += 1; - range[0] = long2Ip(ip); - } - return range; + if (size > 16 || size <= 0) { + return null; + } + /* reserve gateway */ + String[] range = getIpRangeFromCidr(getLinkLocalGateway(), 32 - size); + + if (range[0].equalsIgnoreCase(getLinkLocalGateway())) { + /* remove the gateway */ + long ip = ip2Long(range[0]); + ip += 1; + range[0] = long2Ip(ip); + } + return range; } - + public static String getLinkLocalIpEnd() { - String[] cidrPair = getLinkLocalCIDR().split("\\/"); - String cidr = cidrPair[0]; - - return getIpRangeEndIpFromCidr(cidr, 32 - Long.parseLong(cidrPair[1])); + String[] cidrPair = getLinkLocalCIDR().split("\\/"); + String cidr = cidrPair[0]; + + return getIpRangeEndIpFromCidr(cidr, 32 - Long.parseLong(cidrPair[1])); } - - public static String portRangeToString(int portRange[]){ + + public static String portRangeToString(int portRange[]) { return Integer.toString(portRange[0]) + ":" + Integer.toString(portRange[1]); } // test only - private static void configLog4j() { - URL configUrl = System.class.getResource("/conf/log4j-cloud.xml"); - if(configUrl != null) { - System.out.println("Configure log4j using log4j-cloud.xml"); + private static void configLog4j() { + URL configUrl = System.class.getResource("/conf/log4j-cloud.xml"); + if (configUrl != null) { + System.out.println("Configure log4j using log4j-cloud.xml"); + + try { + File file = new File(configUrl.toURI()); + + System.out.println("Log4j configuration from : " + file.getAbsolutePath()); + DOMConfigurator.configureAndWatch(file.getAbsolutePath(), 10000); + } catch (URISyntaxException e) { + System.out.println("Unable to convert log4j configuration Url to URI"); + } + // DOMConfigurator.configure(configUrl); + } else { + System.out.println("Configure log4j with default properties"); + } + } - try { - File file = new File(configUrl.toURI()); - - System.out.println("Log4j configuration from : " + file.getAbsolutePath()); - DOMConfigurator.configureAndWatch(file.getAbsolutePath(), 10000); - } catch (URISyntaxException e) { - System.out.println("Unable to convert log4j configuration Url to URI"); - } - // DOMConfigurator.configure(configUrl); - } else { - System.out.println("Configure log4j with default properties"); - } - } - public static void main(String[] args) { - configLog4j(); - - if (args.length == 0) { - System.out.println("Must specify at least one parameter"); - } + configLog4j(); + + if (args.length == 0) { + System.out.println("Must specify at least one parameter"); + } if (args[0].equals("m2l")) { System.out.println(mac2Long(args[1])); } else if (args[0].equals("l2m")) { @@ -889,7 +879,7 @@ public class NetUtils { System.out.println("Netmask: " + result[2]); } } - } else if (args[0].equals("range")){ + } else if (args[0].equals("range")) { if (args.length < 4) { String[] result = getIpRangeFromCidr(args[1], Long.parseLong(args[2])); System.out.println("Range is " + result[0] + "-" + result[1]); @@ -897,35 +887,36 @@ public class NetUtils { System.err.println("Needs 3 parameters: " + args.length); } } else if (args[0].equals("ip2")) { - Set result = getAllIpsFromCidr("10.1.1.192", 24); - System.out.println("Number of ips: " + result.size()); - + Set result = getAllIpsFromCidr("10.1.1.192", 24); + System.out.println("Number of ips: " + result.size()); + } else if (args[0].equals("within")) { - String cidrA = args[1]; - String cidrB = args[2]; - System.out.println(NetUtils.isNetworkAWithinNetworkB(cidrA, cidrB)); - - } else if (args[0].equals("tocidr")) { - String ip = args[1]; - String mask = args[2]; - System.out.println(NetUtils.ipAndNetMaskToCidr(ip, mask)); - } else if (args[0].equals("ipmasktorange")){ - String ip = args[1]; - String mask = args[2]; - String [] range = NetUtils.ipAndNetMaskToRange(ip, mask); - - System.out.println(range[0] + " : " + range[1]); + String cidrA = args[1]; + String cidrB = args[2]; + System.out.println(NetUtils.isNetworkAWithinNetworkB(cidrA, cidrB)); + + } else if (args[0].equals("tocidr")) { + String ip = args[1]; + String mask = args[2]; + System.out.println(NetUtils.ipAndNetMaskToCidr(ip, mask)); + } else if (args[0].equals("ipmasktorange")) { + String ip = args[1]; + String mask = args[2]; + String[] range = NetUtils.ipAndNetMaskToRange(ip, mask); + + System.out.println(range[0] + " : " + range[1]); } else { System.out.println(long2Ip(NumbersUtil.parseLong(args[1], 0))); } } - + public static boolean verifyDomainNameLabel(String hostName, boolean isHostName) { - //must be between 1 and 63 characters long and may contain only the ASCII letters 'a' through 'z' (in a case-insensitive manner), - //the digits '0' through '9', and the hyphen ('-'). - //Can not start with a hyphen and digit, and must not end with a hyphen - //If it's a host name, don't allow to start with digit - + // must be between 1 and 63 characters long and may contain only the ASCII letters 'a' through 'z' (in a + // case-insensitive manner), + // the digits '0' through '9', and the hyphen ('-'). + // Can not start with a hyphen and digit, and must not end with a hyphen + // If it's a host name, don't allow to start with digit + if (hostName.length() > 63 || hostName.length() < 1) { s_logger.warn("Domain name label must be between 1 and 63 characters long"); return false; @@ -942,53 +933,53 @@ public class NetUtils { return true; } - + public static boolean verifyDomainName(String domainName) { - //don't allow domain name length to exceed 190 chars (190 + 63 (max host name length) = 253 = max domainName length + // don't allow domain name length to exceed 190 chars (190 + 63 (max host name length) = 253 = max domainName length if (domainName.length() < 1 || domainName.length() > 190) { s_logger.trace("Domain name must be between 1 and 190 characters long"); return false; } - + if (domainName.startsWith(".") || domainName.endsWith(".")) { s_logger.trace("Domain name can't start or end with ."); return false; } - + String[] domainNameLabels = domainName.split("\\."); - + for (int i = 0; i < domainNameLabels.length; i++) { if (!verifyDomainNameLabel(domainNameLabels[i], false)) { s_logger.warn("Domain name label " + domainNameLabels[i] + " is incorrect"); return false; } } - + return true; } - + public static String getDhcpRange(String cidr) { String[] splitResult = cidr.split("\\/"); long size = Long.valueOf(splitResult[1]); return NetUtils.getIpRangeStartIpFromCidr(splitResult[0], size); } - + public static boolean validateGuestCidr(String cidr) { - //RFC 1918 - The Internet Assigned Numbers Authority (IANA) has reserved the - //following three blocks of the IP address space for private internets: - //10.0.0.0 - 10.255.255.255 (10/8 prefix) - //172.16.0.0 - 172.31.255.255 (172.16/12 prefix) - //192.168.0.0 - 192.168.255.255 (192.168/16 prefix) + // RFC 1918 - The Internet Assigned Numbers Authority (IANA) has reserved the + // following three blocks of the IP address space for private internets: + // 10.0.0.0 - 10.255.255.255 (10/8 prefix) + // 172.16.0.0 - 172.31.255.255 (172.16/12 prefix) + // 192.168.0.0 - 192.168.255.255 (192.168/16 prefix) String cidr1 = "10.0.0.0/8"; String cidr2 = "172.16.0.0/12"; String cidr3 = "192.168.0.0/16"; - + if (!isValidCIDR(cidr)) { s_logger.warn("Cidr " + cidr + " is not valid"); return false; } - + if (isNetworkAWithinNetworkB(cidr, cidr1) || isNetworkAWithinNetworkB(cidr, cidr2) || isNetworkAWithinNetworkB(cidr, cidr3)) { return true; } else { @@ -996,6 +987,5 @@ public class NetUtils { return false; } } - -} +}