Introduce zone (datacenter) details

This commit is contained in:
Chiradeep Vittal 2011-03-09 15:40:56 -08:00
parent d2f92f1c76
commit 8d791777db
8 changed files with 267 additions and 1 deletions

View File

@ -3,6 +3,8 @@
*/ */
package com.cloud.dc; package com.cloud.dc;
import java.util.Map;
import com.cloud.org.Grouping; import com.cloud.org.Grouping;
/** /**
@ -34,5 +36,7 @@ public interface DataCenter extends Grouping {
String getUserDataProvider(); String getUserDataProvider();
String getVpnProvider(); String getVpnProvider();
boolean isSecurityGroupEnabled(); boolean isSecurityGroupEnabled();
Map<String, String> getDetails();
void setDetails(Map<String, String> details);
} }

View File

@ -18,6 +18,8 @@
package com.cloud.dc; package com.cloud.dc;
import java.util.Map;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.EnumType; import javax.persistence.EnumType;
@ -27,6 +29,7 @@ import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.TableGenerator; import javax.persistence.TableGenerator;
import javax.persistence.Transient;
import com.cloud.network.Network.Provider; 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) @TableGenerator(name="mac_address_sq", table="data_center", pkColumnName="id", valueColumnName="mac_address", allocationSize=1)
private long macAddress = 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<String, String> details;
@Override @Override
public String getDnsProvider() { public String getDnsProvider() {
return dnsProvider; return dnsProvider;
@ -324,4 +333,26 @@ public class DataCenterVO implements DataCenter {
public void setSecurityGroupEnabled(boolean enabled) { public void setSecurityGroupEnabled(boolean enabled) {
this.securityGroupEnabled = enabled; this.securityGroupEnabled = enabled;
} }
@Override
public Map<String, String> getDetails() {
return details;
}
@Override
public void setDetails(Map<String, String> 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);
}
} }

View File

@ -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 <http://www.gnu.org/licenses/>.
*
*/
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;
}
}

View File

@ -67,4 +67,7 @@ public interface DataCenterDao extends GenericDao<DataCenterVO, Long> {
List<DataCenterVO> findChildZones(Object[] ids); List<DataCenterVO> findChildZones(Object[] ids);
List<DataCenterVO> listSecurityGroupEnabledZones(); List<DataCenterVO> listSecurityGroupEnabledZones();
void loadDetails(DataCenterVO zone);
void saveDetails(DataCenterVO zone);
} }

View File

@ -67,6 +67,9 @@ public class DataCenterDaoImpl extends GenericDaoBase<DataCenterVO, Long> implem
protected Random _rand = new Random(System.currentTimeMillis()); protected Random _rand = new Random(System.currentTimeMillis());
protected TableGenerator _tgMacAddress; protected TableGenerator _tgMacAddress;
protected final DcDetailsDaoImpl _detailsDao = ComponentLocator.inject(DcDetailsDaoImpl.class);
@Override @Override
public DataCenterVO findByName(String name) { public DataCenterVO findByName(String name) {
SearchCriteria<DataCenterVO> sc = NameSearch.create(); SearchCriteria<DataCenterVO> sc = NameSearch.create();
@ -266,4 +269,19 @@ public class DataCenterDaoImpl extends GenericDaoBase<DataCenterVO, Long> implem
_tgMacAddress = _tgs.get("macAddress"); _tgMacAddress = _tgs.get("macAddress");
assert _tgMacAddress != null : "Couldn't get mac address table generator"; assert _tgMacAddress != null : "Couldn't get mac address table generator";
} }
@Override
public void loadDetails(DataCenterVO zone) {
Map<String, String> details =_detailsDao.findDetails(zone.getId());
zone.setDetails(details);
}
@Override
public void saveDetails(DataCenterVO zone) {
Map<String, String> details = zone.getDetails();
if (details == null) {
return;
}
_detailsDao.persist(zone.getId(), details);
}
} }

View File

@ -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 <http://www.gnu.org/licenses/>.
*
*/
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<DcDetailVO, Long> {
Map<String, String> findDetails(long hostId);
void persist(long hostId, Map<String, String> details);
DcDetailVO findDetail(long hostId, String name);
void deleteDetails(long hostId);
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*
*/
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<DcDetailVO, Long> implements DcDetailsDao {
protected final SearchBuilder<DcDetailVO> DcSearch;
protected final SearchBuilder<DcDetailVO> 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<DcDetailVO> sc = DetailSearch.create();
sc.setParameters("dcId", dcId);
sc.setParameters("name", name);
return findOneIncludingRemovedBy(sc);
}
@Override
public Map<String, String> findDetails(long dcId) {
SearchCriteria<DcDetailVO> sc = DcSearch.create();
sc.setParameters("dcId", dcId);
List<DcDetailVO> results = search(sc, null);
Map<String, String> details = new HashMap<String, String>(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<DcDetailVO> results = search(sc, null);
for (DcDetailVO result : results) {
remove(result.getId());
}
}
@Override
public void persist(long dcId, Map<String, String> details) {
Transaction txn = Transaction.currentTxn();
txn.start();
SearchCriteria<DcDetailVO> sc = DcSearch.create();
sc.setParameters("dcId", dcId);
expunge(sc);
for (Map.Entry<String, String> detail : details.entrySet()) {
DcDetailVO vo = new DcDetailVO(dcId, detail.getKey(), detail.getValue());
persist(vo);
}
txn.commit();
}
}

View File

@ -113,6 +113,7 @@ DROP TABLE IF EXISTS `cloud`.`stack_maid`;
DROP TABLE IF EXISTS `cloud`.`storage_pool_work`; DROP TABLE IF EXISTS `cloud`.`storage_pool_work`;
DROP TABLE IF EXISTS `cloud`.`user_vm_details`; DROP TABLE IF EXISTS `cloud`.`user_vm_details`;
DROP TABLE IF EXISTS `cloud`.`vpn_users`; DROP TABLE IF EXISTS `cloud`.`vpn_users`;
DROP TABLE IF EXISTS `cloud`.`data_center_details`;
CREATE TABLE `cloud`.`version` ( CREATE TABLE `cloud`.`version` (
`id` bigint unsigned NOT NULL UNIQUE AUTO_INCREMENT COMMENT 'id', `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) UNIQUE (pool_id,vm_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; ) 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; SET foreign_key_checks = 1;