Skip to content

Commit 8d79177

Browse files
author
Chiradeep Vittal
committed
Introduce zone (datacenter) details
1 parent d2f92f1 commit 8d79177

8 files changed

Lines changed: 267 additions & 1 deletion

File tree

api/src/com/cloud/dc/DataCenter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
*/
44
package com.cloud.dc;
55

6+
import java.util.Map;
7+
68
import com.cloud.org.Grouping;
79

810
/**
@@ -34,5 +36,7 @@ public enum NetworkType {
3436
String getUserDataProvider();
3537
String getVpnProvider();
3638
boolean isSecurityGroupEnabled();
39+
Map<String, String> getDetails();
40+
void setDetails(Map<String, String> details);
3741

3842
}

server/src/com/cloud/dc/DataCenterVO.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
package com.cloud.dc;
2020

21+
import java.util.Map;
22+
2123
import javax.persistence.Column;
2224
import javax.persistence.Entity;
2325
import javax.persistence.EnumType;
@@ -27,6 +29,7 @@
2729
import javax.persistence.Id;
2830
import javax.persistence.Table;
2931
import javax.persistence.TableGenerator;
32+
import javax.persistence.Transient;
3033

3134
import com.cloud.network.Network.Provider;
3235

@@ -104,6 +107,12 @@ public class DataCenterVO implements DataCenter {
104107
@TableGenerator(name="mac_address_sq", table="data_center", pkColumnName="id", valueColumnName="mac_address", allocationSize=1)
105108
private long macAddress = 1;
106109

110+
// This is a delayed load value. If the value is null,
111+
// then this field has not been loaded yet.
112+
// Call the dao to load it.
113+
@Transient
114+
Map<String, String> details;
115+
107116
@Override
108117
public String getDnsProvider() {
109118
return dnsProvider;
@@ -323,5 +332,27 @@ public boolean isSecurityGroupEnabled() {
323332

324333
public void setSecurityGroupEnabled(boolean enabled) {
325334
this.securityGroupEnabled = enabled;
335+
}
336+
337+
@Override
338+
public Map<String, String> getDetails() {
339+
return details;
340+
}
341+
342+
@Override
343+
public void setDetails(Map<String, String> details2) {
344+
details = details2;
345+
}
346+
347+
public String getDetail(String name) {
348+
assert (details != null) : "Did you forget to load the details?";
349+
350+
return details != null ? details.get(name) : null;
351+
}
352+
353+
public void setDetail(String name, String value) {
354+
assert (details != null) : "Did you forget to load the details?";
355+
356+
details.put(name, value);
326357
}
327358
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Copyright (C) 2010 Cloud.com, Inc. All rights reserved.
3+
*
4+
* This software is licensed under the GNU General Public License v3 or later.
5+
*
6+
* It is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or any later version.
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*
17+
*/
18+
package com.cloud.dc;
19+
20+
import javax.persistence.Column;
21+
import javax.persistence.Entity;
22+
import javax.persistence.GeneratedValue;
23+
import javax.persistence.GenerationType;
24+
import javax.persistence.Id;
25+
import javax.persistence.Table;
26+
27+
@Entity
28+
@Table(name="datacenter_details")
29+
public class DcDetailVO {
30+
@Id
31+
@GeneratedValue(strategy=GenerationType.IDENTITY)
32+
@Column(name="id")
33+
private long id;
34+
35+
@Column(name="dc_id")
36+
private long dcId;
37+
38+
@Column(name="name")
39+
private String name;
40+
41+
@Column(name="value")
42+
private String value;
43+
44+
protected DcDetailVO() {
45+
}
46+
47+
public DcDetailVO(long dcId, String name, String value) {
48+
this.dcId = dcId;
49+
this.name = name;
50+
this.value = value;
51+
}
52+
53+
public long getDcId() {
54+
return dcId;
55+
}
56+
57+
public String getName() {
58+
return name;
59+
}
60+
61+
public String getValue() {
62+
return value;
63+
}
64+
65+
public void setValue(String value) {
66+
this.value = value;
67+
}
68+
69+
public long getId() {
70+
return id;
71+
}
72+
}

server/src/com/cloud/dc/dao/DataCenterDao.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,7 @@ public interface DataCenterDao extends GenericDao<DataCenterVO, Long> {
6767
List<DataCenterVO> findChildZones(Object[] ids);
6868

6969
List<DataCenterVO> listSecurityGroupEnabledZones();
70+
71+
void loadDetails(DataCenterVO zone);
72+
void saveDetails(DataCenterVO zone);
7073
}

server/src/com/cloud/dc/dao/DataCenterDaoImpl.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ public class DataCenterDaoImpl extends GenericDaoBase<DataCenterVO, Long> implem
6565
protected final PodVlanDaoImpl _podVlanAllocDao = ComponentLocator.inject(PodVlanDaoImpl.class);
6666
protected long _prefix;
6767
protected Random _rand = new Random(System.currentTimeMillis());
68-
protected TableGenerator _tgMacAddress;
68+
protected TableGenerator _tgMacAddress;
69+
70+
protected final DcDetailsDaoImpl _detailsDao = ComponentLocator.inject(DcDetailsDaoImpl.class);
71+
6972

7073
@Override
7174
public DataCenterVO findByName(String name) {
@@ -265,5 +268,20 @@ protected DataCenterDaoImpl() {
265268

266269
_tgMacAddress = _tgs.get("macAddress");
267270
assert _tgMacAddress != null : "Couldn't get mac address table generator";
271+
}
272+
273+
@Override
274+
public void loadDetails(DataCenterVO zone) {
275+
Map<String, String> details =_detailsDao.findDetails(zone.getId());
276+
zone.setDetails(details);
277+
}
278+
279+
@Override
280+
public void saveDetails(DataCenterVO zone) {
281+
Map<String, String> details = zone.getDetails();
282+
if (details == null) {
283+
return;
284+
}
285+
_detailsDao.persist(zone.getId(), details);
268286
}
269287
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright (C) 2011 Cloud.com, Inc. All rights reserved.
3+
*
4+
* This software is licensed under the GNU General Public License v3 or later.
5+
*
6+
* It is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or any later version.
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*
17+
*/
18+
package com.cloud.dc.dao;
19+
20+
import java.util.Map;
21+
22+
import com.cloud.dc.DcDetailVO;
23+
import com.cloud.utils.db.GenericDao;
24+
25+
public interface DcDetailsDao extends GenericDao<DcDetailVO, Long> {
26+
Map<String, String> findDetails(long hostId);
27+
28+
void persist(long hostId, Map<String, String> details);
29+
30+
DcDetailVO findDetail(long hostId, String name);
31+
32+
void deleteDetails(long hostId);
33+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* Copyright (C) 2011 Cloud.com, Inc. All rights reserved.
3+
*
4+
* This software is licensed under the GNU General Public License v3 or later.
5+
*
6+
* It is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or any later version.
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*
17+
*/
18+
package com.cloud.dc.dao;
19+
20+
import java.util.HashMap;
21+
import java.util.List;
22+
import java.util.Map;
23+
24+
import javax.ejb.Local;
25+
26+
import com.cloud.dc.DcDetailVO;
27+
import com.cloud.utils.db.GenericDaoBase;
28+
import com.cloud.utils.db.SearchBuilder;
29+
import com.cloud.utils.db.SearchCriteria;
30+
import com.cloud.utils.db.Transaction;
31+
32+
@Local(value=DcDetailsDao.class)
33+
public class DcDetailsDaoImpl extends GenericDaoBase<DcDetailVO, Long> implements DcDetailsDao {
34+
protected final SearchBuilder<DcDetailVO> DcSearch;
35+
protected final SearchBuilder<DcDetailVO> DetailSearch;
36+
37+
protected DcDetailsDaoImpl() {
38+
DcSearch = createSearchBuilder();
39+
DcSearch.and("dcId", DcSearch.entity().getDcId(), SearchCriteria.Op.EQ);
40+
DcSearch.done();
41+
42+
DetailSearch = createSearchBuilder();
43+
DetailSearch.and("dcId", DetailSearch.entity().getDcId(), SearchCriteria.Op.EQ);
44+
DetailSearch.and("name", DetailSearch.entity().getName(), SearchCriteria.Op.EQ);
45+
DetailSearch.done();
46+
}
47+
48+
@Override
49+
public DcDetailVO findDetail(long dcId, String name) {
50+
SearchCriteria<DcDetailVO> sc = DetailSearch.create();
51+
sc.setParameters("dcId", dcId);
52+
sc.setParameters("name", name);
53+
54+
return findOneIncludingRemovedBy(sc);
55+
}
56+
57+
@Override
58+
public Map<String, String> findDetails(long dcId) {
59+
SearchCriteria<DcDetailVO> sc = DcSearch.create();
60+
sc.setParameters("dcId", dcId);
61+
62+
List<DcDetailVO> results = search(sc, null);
63+
Map<String, String> details = new HashMap<String, String>(results.size());
64+
for (DcDetailVO result : results) {
65+
details.put(result.getName(), result.getValue());
66+
}
67+
return details;
68+
}
69+
70+
@Override
71+
public void deleteDetails(long dcId) {
72+
SearchCriteria sc = DcSearch.create();
73+
sc.setParameters("dcId", dcId);
74+
75+
List<DcDetailVO> results = search(sc, null);
76+
for (DcDetailVO result : results) {
77+
remove(result.getId());
78+
}
79+
}
80+
81+
@Override
82+
public void persist(long dcId, Map<String, String> details) {
83+
Transaction txn = Transaction.currentTxn();
84+
txn.start();
85+
SearchCriteria<DcDetailVO> sc = DcSearch.create();
86+
sc.setParameters("dcId", dcId);
87+
expunge(sc);
88+
89+
for (Map.Entry<String, String> detail : details.entrySet()) {
90+
DcDetailVO vo = new DcDetailVO(dcId, detail.getKey(), detail.getValue());
91+
persist(vo);
92+
}
93+
txn.commit();
94+
}
95+
}

setup/db/create-schema.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ DROP TABLE IF EXISTS `cloud`.`stack_maid`;
113113
DROP TABLE IF EXISTS `cloud`.`storage_pool_work`;
114114
DROP TABLE IF EXISTS `cloud`.`user_vm_details`;
115115
DROP TABLE IF EXISTS `cloud`.`vpn_users`;
116+
DROP TABLE IF EXISTS `cloud`.`data_center_details`;
116117

117118
CREATE TABLE `cloud`.`version` (
118119
`id` bigint unsigned NOT NULL UNIQUE AUTO_INCREMENT COMMENT 'id',
@@ -1447,4 +1448,13 @@ CREATE TABLE `cloud`.`storage_pool_work` (
14471448
UNIQUE (pool_id,vm_id)
14481449
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
14491450

1451+
CREATE TABLE `cloud`.`data_center_details` (
1452+
`id` bigint unsigned NOT NULL auto_increment,
1453+
`dc_id` bigint unsigned NOT NULL COMMENT 'dc id',
1454+
`name` varchar(255) NOT NULL,
1455+
`value` varchar(255) NOT NULL,
1456+
PRIMARY KEY (`id`),
1457+
CONSTRAINT `fk_dc_details__dc_id` FOREIGN KEY (`dc_id`) REFERENCES `data_center`(`id`) ON DELETE CASCADE
1458+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
1459+
14501460
SET foreign_key_checks = 1;

0 commit comments

Comments
 (0)