From 8d791777dbbd9862e67dbfc1ab4bc9acbb1d4516 Mon Sep 17 00:00:00 2001 From: Chiradeep Vittal Date: Wed, 9 Mar 2011 15:40:56 -0800 Subject: [PATCH] Introduce zone (datacenter) details --- api/src/com/cloud/dc/DataCenter.java | 4 + server/src/com/cloud/dc/DataCenterVO.java | 31 ++++++ server/src/com/cloud/dc/DcDetailVO.java | 72 ++++++++++++++ .../src/com/cloud/dc/dao/DataCenterDao.java | 3 + .../com/cloud/dc/dao/DataCenterDaoImpl.java | 20 +++- server/src/com/cloud/dc/dao/DcDetailsDao.java | 33 +++++++ .../com/cloud/dc/dao/DcDetailsDaoImpl.java | 95 +++++++++++++++++++ setup/db/create-schema.sql | 10 ++ 8 files changed, 267 insertions(+), 1 deletion(-) create mode 100644 server/src/com/cloud/dc/DcDetailVO.java create mode 100644 server/src/com/cloud/dc/dao/DcDetailsDao.java create mode 100644 server/src/com/cloud/dc/dao/DcDetailsDaoImpl.java diff --git a/api/src/com/cloud/dc/DataCenter.java b/api/src/com/cloud/dc/DataCenter.java index 0abb243d2dc..7b38318bc6f 100644 --- a/api/src/com/cloud/dc/DataCenter.java +++ b/api/src/com/cloud/dc/DataCenter.java @@ -3,6 +3,8 @@ */ package com.cloud.dc; +import java.util.Map; + import com.cloud.org.Grouping; /** @@ -34,5 +36,7 @@ public interface DataCenter extends Grouping { String getUserDataProvider(); String getVpnProvider(); boolean isSecurityGroupEnabled(); + Map getDetails(); + void setDetails(Map details); } diff --git a/server/src/com/cloud/dc/DataCenterVO.java b/server/src/com/cloud/dc/DataCenterVO.java index e26802b515b..ab437c3f718 100644 --- a/server/src/com/cloud/dc/DataCenterVO.java +++ b/server/src/com/cloud/dc/DataCenterVO.java @@ -18,6 +18,8 @@ package com.cloud.dc; +import java.util.Map; + import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.EnumType; @@ -27,6 +29,7 @@ import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.TableGenerator; +import javax.persistence.Transient; import com.cloud.network.Network.Provider; @@ -104,6 +107,12 @@ public class DataCenterVO implements DataCenter { @TableGenerator(name="mac_address_sq", table="data_center", pkColumnName="id", valueColumnName="mac_address", allocationSize=1) private long macAddress = 1; + // This is a delayed load value. If the value is null, + // then this field has not been loaded yet. + // Call the dao to load it. + @Transient + Map details; + @Override public String getDnsProvider() { return dnsProvider; @@ -323,5 +332,27 @@ public class DataCenterVO implements DataCenter { public void setSecurityGroupEnabled(boolean enabled) { this.securityGroupEnabled = enabled; + } + + @Override + public Map getDetails() { + return details; + } + + @Override + public void setDetails(Map details2) { + details = details2; + } + + public String getDetail(String name) { + assert (details != null) : "Did you forget to load the details?"; + + return details != null ? details.get(name) : null; + } + + public void setDetail(String name, String value) { + assert (details != null) : "Did you forget to load the details?"; + + details.put(name, value); } } diff --git a/server/src/com/cloud/dc/DcDetailVO.java b/server/src/com/cloud/dc/DcDetailVO.java new file mode 100644 index 00000000000..1d3c841adac --- /dev/null +++ b/server/src/com/cloud/dc/DcDetailVO.java @@ -0,0 +1,72 @@ +/** + * Copyright (C) 2010 Cloud.com, Inc. All rights reserved. + * + * This software is licensed under the GNU General Public License v3 or later. + * + * It is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +package com.cloud.dc; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name="datacenter_details") +public class DcDetailVO { + @Id + @GeneratedValue(strategy=GenerationType.IDENTITY) + @Column(name="id") + private long id; + + @Column(name="dc_id") + private long dcId; + + @Column(name="name") + private String name; + + @Column(name="value") + private String value; + + protected DcDetailVO() { + } + + public DcDetailVO(long dcId, String name, String value) { + this.dcId = dcId; + this.name = name; + this.value = value; + } + + public long getDcId() { + return dcId; + } + + public String getName() { + return name; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + public long getId() { + return id; + } +} diff --git a/server/src/com/cloud/dc/dao/DataCenterDao.java b/server/src/com/cloud/dc/dao/DataCenterDao.java index e280adfed8e..3062e7d43c3 100644 --- a/server/src/com/cloud/dc/dao/DataCenterDao.java +++ b/server/src/com/cloud/dc/dao/DataCenterDao.java @@ -67,4 +67,7 @@ public interface DataCenterDao extends GenericDao { List findChildZones(Object[] ids); List listSecurityGroupEnabledZones(); + + void loadDetails(DataCenterVO zone); + void saveDetails(DataCenterVO zone); } diff --git a/server/src/com/cloud/dc/dao/DataCenterDaoImpl.java b/server/src/com/cloud/dc/dao/DataCenterDaoImpl.java index 32b17df2e1d..db96e3b63ec 100644 --- a/server/src/com/cloud/dc/dao/DataCenterDaoImpl.java +++ b/server/src/com/cloud/dc/dao/DataCenterDaoImpl.java @@ -65,7 +65,10 @@ public class DataCenterDaoImpl extends GenericDaoBase implem protected final PodVlanDaoImpl _podVlanAllocDao = ComponentLocator.inject(PodVlanDaoImpl.class); protected long _prefix; protected Random _rand = new Random(System.currentTimeMillis()); - protected TableGenerator _tgMacAddress; + protected TableGenerator _tgMacAddress; + + protected final DcDetailsDaoImpl _detailsDao = ComponentLocator.inject(DcDetailsDaoImpl.class); + @Override public DataCenterVO findByName(String name) { @@ -265,5 +268,20 @@ public class DataCenterDaoImpl extends GenericDaoBase implem _tgMacAddress = _tgs.get("macAddress"); assert _tgMacAddress != null : "Couldn't get mac address table generator"; + } + + @Override + public void loadDetails(DataCenterVO zone) { + Map details =_detailsDao.findDetails(zone.getId()); + zone.setDetails(details); + } + + @Override + public void saveDetails(DataCenterVO zone) { + Map details = zone.getDetails(); + if (details == null) { + return; + } + _detailsDao.persist(zone.getId(), details); } } diff --git a/server/src/com/cloud/dc/dao/DcDetailsDao.java b/server/src/com/cloud/dc/dao/DcDetailsDao.java new file mode 100644 index 00000000000..f5ee8d60dec --- /dev/null +++ b/server/src/com/cloud/dc/dao/DcDetailsDao.java @@ -0,0 +1,33 @@ +/** + * Copyright (C) 2011 Cloud.com, Inc. All rights reserved. + * + * This software is licensed under the GNU General Public License v3 or later. + * + * It is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +package com.cloud.dc.dao; + +import java.util.Map; + +import com.cloud.dc.DcDetailVO; +import com.cloud.utils.db.GenericDao; + +public interface DcDetailsDao extends GenericDao { + Map findDetails(long hostId); + + void persist(long hostId, Map details); + + DcDetailVO findDetail(long hostId, String name); + + void deleteDetails(long hostId); +} \ No newline at end of file diff --git a/server/src/com/cloud/dc/dao/DcDetailsDaoImpl.java b/server/src/com/cloud/dc/dao/DcDetailsDaoImpl.java new file mode 100644 index 00000000000..7bb38268e1c --- /dev/null +++ b/server/src/com/cloud/dc/dao/DcDetailsDaoImpl.java @@ -0,0 +1,95 @@ +/** + * Copyright (C) 2011 Cloud.com, Inc. All rights reserved. + * + * This software is licensed under the GNU General Public License v3 or later. + * + * It is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +package com.cloud.dc.dao; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.ejb.Local; + +import com.cloud.dc.DcDetailVO; +import com.cloud.utils.db.GenericDaoBase; +import com.cloud.utils.db.SearchBuilder; +import com.cloud.utils.db.SearchCriteria; +import com.cloud.utils.db.Transaction; + +@Local(value=DcDetailsDao.class) +public class DcDetailsDaoImpl extends GenericDaoBase implements DcDetailsDao { + protected final SearchBuilder DcSearch; + protected final SearchBuilder DetailSearch; + + protected DcDetailsDaoImpl() { + DcSearch = createSearchBuilder(); + DcSearch.and("dcId", DcSearch.entity().getDcId(), SearchCriteria.Op.EQ); + DcSearch.done(); + + DetailSearch = createSearchBuilder(); + DetailSearch.and("dcId", DetailSearch.entity().getDcId(), SearchCriteria.Op.EQ); + DetailSearch.and("name", DetailSearch.entity().getName(), SearchCriteria.Op.EQ); + DetailSearch.done(); + } + + @Override + public DcDetailVO findDetail(long dcId, String name) { + SearchCriteria sc = DetailSearch.create(); + sc.setParameters("dcId", dcId); + sc.setParameters("name", name); + + return findOneIncludingRemovedBy(sc); + } + + @Override + public Map findDetails(long dcId) { + SearchCriteria sc = DcSearch.create(); + sc.setParameters("dcId", dcId); + + List results = search(sc, null); + Map details = new HashMap(results.size()); + for (DcDetailVO result : results) { + details.put(result.getName(), result.getValue()); + } + return details; + } + + @Override + public void deleteDetails(long dcId) { + SearchCriteria sc = DcSearch.create(); + sc.setParameters("dcId", dcId); + + List results = search(sc, null); + for (DcDetailVO result : results) { + remove(result.getId()); + } + } + + @Override + public void persist(long dcId, Map details) { + Transaction txn = Transaction.currentTxn(); + txn.start(); + SearchCriteria sc = DcSearch.create(); + sc.setParameters("dcId", dcId); + expunge(sc); + + for (Map.Entry detail : details.entrySet()) { + DcDetailVO vo = new DcDetailVO(dcId, detail.getKey(), detail.getValue()); + persist(vo); + } + txn.commit(); + } +} diff --git a/setup/db/create-schema.sql b/setup/db/create-schema.sql index 815b8ba9db8..f963d98398e 100755 --- a/setup/db/create-schema.sql +++ b/setup/db/create-schema.sql @@ -113,6 +113,7 @@ DROP TABLE IF EXISTS `cloud`.`stack_maid`; DROP TABLE IF EXISTS `cloud`.`storage_pool_work`; DROP TABLE IF EXISTS `cloud`.`user_vm_details`; DROP TABLE IF EXISTS `cloud`.`vpn_users`; +DROP TABLE IF EXISTS `cloud`.`data_center_details`; CREATE TABLE `cloud`.`version` ( `id` bigint unsigned NOT NULL UNIQUE AUTO_INCREMENT COMMENT 'id', @@ -1447,4 +1448,13 @@ CREATE TABLE `cloud`.`storage_pool_work` ( UNIQUE (pool_id,vm_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; +CREATE TABLE `cloud`.`data_center_details` ( + `id` bigint unsigned NOT NULL auto_increment, + `dc_id` bigint unsigned NOT NULL COMMENT 'dc id', + `name` varchar(255) NOT NULL, + `value` varchar(255) NOT NULL, + PRIMARY KEY (`id`), + CONSTRAINT `fk_dc_details__dc_id` FOREIGN KEY (`dc_id`) REFERENCES `data_center`(`id`) ON DELETE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + SET foreign_key_checks = 1;