forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStoragePoolHostDaoImpl.java
More file actions
184 lines (159 loc) · 6.96 KB
/
Copy pathStoragePoolHostDaoImpl.java
File metadata and controls
184 lines (159 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.storage.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Local;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import com.cloud.host.Status;
import com.cloud.storage.StoragePoolHostVO;
import com.cloud.utils.Pair;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.db.Transaction;
@Component
@Local(value = { StoragePoolHostDao.class })
public class StoragePoolHostDaoImpl extends GenericDaoBase<StoragePoolHostVO, Long> implements StoragePoolHostDao {
public static final Logger s_logger = Logger.getLogger(StoragePoolHostDaoImpl.class.getName());
protected final SearchBuilder<StoragePoolHostVO> PoolSearch;
protected final SearchBuilder<StoragePoolHostVO> HostSearch;
protected final SearchBuilder<StoragePoolHostVO> PoolHostSearch;
protected static final String HOST_FOR_POOL_SEARCH = "SELECT * FROM storage_pool_host_ref ph, host h where ph.host_id = h.id and ph.pool_id=? and h.status=? ";
protected static final String STORAGE_POOL_HOST_INFO = "SELECT p.data_center_id, count(ph.host_id) " + " FROM storage_pool p, storage_pool_host_ref ph "
+ " WHERE p.id = ph.pool_id AND p.data_center_id = ? " + " GROUP by p.data_center_id";
protected static final String SHARED_STORAGE_POOL_HOST_INFO = "SELECT p.data_center_id, count(ph.host_id) " + " FROM storage_pool p, storage_pool_host_ref ph "
+ " WHERE p.id = ph.pool_id AND p.data_center_id = ? " + " AND p.pool_type NOT IN ('LVM', 'Filesystem')" + " GROUP by p.data_center_id";
protected static final String DELETE_PRIMARY_RECORDS = "DELETE " + "FROM storage_pool_host_ref " + "WHERE host_id = ?";
public StoragePoolHostDaoImpl() {
PoolSearch = createSearchBuilder();
PoolSearch.and("pool_id", PoolSearch.entity().getPoolId(), SearchCriteria.Op.EQ);
PoolSearch.done();
HostSearch = createSearchBuilder();
HostSearch.and("host_id", HostSearch.entity().getHostId(), SearchCriteria.Op.EQ);
HostSearch.done();
PoolHostSearch = createSearchBuilder();
PoolHostSearch.and("pool_id", PoolHostSearch.entity().getPoolId(), SearchCriteria.Op.EQ);
PoolHostSearch.and("host_id", PoolHostSearch.entity().getHostId(), SearchCriteria.Op.EQ);
PoolHostSearch.done();
}
@Override
public List<StoragePoolHostVO> listByPoolId(long id) {
SearchCriteria<StoragePoolHostVO> sc = PoolSearch.create();
sc.setParameters("pool_id", id);
return listIncludingRemovedBy(sc);
}
@Override
public List<StoragePoolHostVO> listByHostIdIncludingRemoved(long hostId) {
SearchCriteria<StoragePoolHostVO> sc = HostSearch.create();
sc.setParameters("host_id", hostId);
return listIncludingRemovedBy(sc);
}
@Override
public List<StoragePoolHostVO> listByHostId(long hostId) {
SearchCriteria<StoragePoolHostVO> sc = HostSearch.create();
sc.setParameters("host_id", hostId);
return listBy(sc);
}
@Override
public StoragePoolHostVO findByPoolHost(long poolId, long hostId) {
SearchCriteria<StoragePoolHostVO> sc = PoolHostSearch.create();
sc.setParameters("pool_id", poolId);
sc.setParameters("host_id", hostId);
return findOneIncludingRemovedBy(sc);
}
@Override
public List<StoragePoolHostVO> listByHostStatus(long poolId, Status hostStatus) {
Transaction txn = Transaction.currentTxn();
PreparedStatement pstmt = null;
List<StoragePoolHostVO> result = new ArrayList<StoragePoolHostVO>();
ResultSet rs = null;
try {
String sql = HOST_FOR_POOL_SEARCH;
pstmt = txn.prepareStatement(sql);
pstmt.setLong(1, poolId);
pstmt.setString(2, hostStatus.toString());
rs = pstmt.executeQuery();
while (rs.next()) {
// result.add(toEntityBean(rs, false)); TODO: this is buggy in GenericDaoBase for hand constructed queries
long id = rs.getLong(1); // ID column
result.add(findById(id));
}
} catch (Exception e) {
s_logger.warn("Exception: ", e);
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
}
}
return result;
}
@Override
public List<Pair<Long, Integer>> getDatacenterStoragePoolHostInfo(long dcId, boolean sharedOnly) {
ArrayList<Pair<Long, Integer>> l = new ArrayList<Pair<Long, Integer>>();
String sql = sharedOnly ? SHARED_STORAGE_POOL_HOST_INFO : STORAGE_POOL_HOST_INFO;
Transaction txn = Transaction.currentTxn();
;
PreparedStatement pstmt = null;
try {
pstmt = txn.prepareAutoCloseStatement(sql);
pstmt.setLong(1, dcId);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
l.add(new Pair<Long, Integer>(rs.getLong(1), rs.getInt(2)));
}
} catch (SQLException e) {
} catch (Throwable e) {
}
return l;
}
/**
* This method deletes the primary records from the host
*
* @param hostId
* -- id of the host
*/
@Override
public void deletePrimaryRecordsForHost(long hostId) {
SearchCriteria<StoragePoolHostVO> sc = HostSearch.create();
sc.setParameters("host_id", hostId);
Transaction txn = Transaction.currentTxn();
txn.start();
remove(sc);
txn.commit();
}
@Override
public void deleteStoragePoolHostDetails(long hostId, long poolId) {
SearchCriteria<StoragePoolHostVO> sc = PoolHostSearch.create();
sc.setParameters("host_id", hostId);
sc.setParameters("pool_id", poolId);
Transaction txn = Transaction.currentTxn();
txn.start();
remove(sc);
txn.commit();
}
}