forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVlanDaoImpl.java
More file actions
315 lines (263 loc) · 12.3 KB
/
Copy pathVlanDaoImpl.java
File metadata and controls
315 lines (263 loc) · 12.3 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// 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.dc.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.ejb.Local;
import javax.inject.Inject;
import javax.naming.ConfigurationException;
import org.springframework.stereotype.Component;
import com.cloud.dc.AccountVlanMapVO;
import com.cloud.dc.PodVlanMapVO;
import com.cloud.dc.Vlan;
import com.cloud.dc.Vlan.VlanType;
import com.cloud.dc.VlanVO;
import com.cloud.network.dao.IPAddressDao;
import com.cloud.utils.Pair;
import com.cloud.utils.db.DB;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.JoinBuilder;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.db.Transaction;
import com.cloud.utils.exception.CloudRuntimeException;
@Component
@Local(value={VlanDao.class})
public class VlanDaoImpl extends GenericDaoBase<VlanVO, Long> implements VlanDao {
private final String FindZoneWideVlans = "SELECT * FROM vlan WHERE data_center_id=? and vlan_type=? and vlan_id!=? and id not in (select vlan_db_id from account_vlan_map)";
protected SearchBuilder<VlanVO> ZoneVlanIdSearch;
protected SearchBuilder<VlanVO> ZoneSearch;
protected SearchBuilder<VlanVO> ZoneTypeSearch;
protected SearchBuilder<VlanVO> ZoneTypeAllPodsSearch;
protected SearchBuilder<VlanVO> ZoneTypePodSearch;
protected SearchBuilder<VlanVO> ZoneVlanSearch;
protected SearchBuilder<VlanVO> NetworkVlanSearch;
protected SearchBuilder<VlanVO> PhysicalNetworkVlanSearch;
@Inject protected PodVlanMapDao _podVlanMapDao;
@Inject protected AccountVlanMapDao _accountVlanMapDao;
@Inject protected IPAddressDao _ipAddressDao;
@Override
public VlanVO findByZoneAndVlanId(long zoneId, String vlanId) {
SearchCriteria<VlanVO> sc = ZoneVlanIdSearch.create();
sc.setParameters("zoneId", zoneId);
sc.setParameters("vlanId", vlanId);
return findOneBy(sc);
}
@Override
public List<VlanVO> listByZone(long zoneId) {
SearchCriteria<VlanVO> sc = ZoneSearch.create();
sc.setParameters("zoneId", zoneId);
return listBy(sc);
}
public VlanDaoImpl() {
ZoneVlanIdSearch = createSearchBuilder();
ZoneVlanIdSearch.and("zoneId", ZoneVlanIdSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
ZoneVlanIdSearch.and("vlanId", ZoneVlanIdSearch.entity().getVlanTag(), SearchCriteria.Op.EQ);
ZoneVlanIdSearch.done();
ZoneSearch = createSearchBuilder();
ZoneSearch.and("zoneId", ZoneSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
ZoneSearch.done();
ZoneTypeSearch = createSearchBuilder();
ZoneTypeSearch.and("zoneId", ZoneTypeSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
ZoneTypeSearch.and("vlanType", ZoneTypeSearch.entity().getVlanType(), SearchCriteria.Op.EQ);
ZoneTypeSearch.done();
NetworkVlanSearch = createSearchBuilder();
NetworkVlanSearch.and("networkOfferingId", NetworkVlanSearch.entity().getNetworkId(), SearchCriteria.Op.EQ);
NetworkVlanSearch.done();
PhysicalNetworkVlanSearch = createSearchBuilder();
PhysicalNetworkVlanSearch.and("physicalNetworkId", PhysicalNetworkVlanSearch.entity().getPhysicalNetworkId(), SearchCriteria.Op.EQ);
PhysicalNetworkVlanSearch.done();
}
@Override
public List<VlanVO> listZoneWideVlans(long zoneId, VlanType vlanType, String vlanId){
SearchCriteria<VlanVO> sc = ZoneVlanSearch.create();
sc.setParameters("zoneId", zoneId);
sc.setParameters("vlanId", vlanId);
sc.setParameters("vlanType", vlanType);
return listBy(sc);
}
@Override
public List<VlanVO> listByZoneAndType(long zoneId, VlanType vlanType) {
SearchCriteria<VlanVO> sc = ZoneTypeSearch.create();
sc.setParameters("zoneId", zoneId);
sc.setParameters("vlanType", vlanType);
return listBy(sc);
}
@Override
public List<VlanVO> listByType(VlanType vlanType) {
SearchCriteria<VlanVO> sc = ZoneTypeSearch.create();
sc.setParameters("vlanType", vlanType);
return listBy(sc);
}
@Override
public List<VlanVO> listVlansForPod(long podId) {
//FIXME: use a join statement to improve the performance (should be minor since we expect only one or two
List<PodVlanMapVO> vlanMaps = _podVlanMapDao.listPodVlanMapsByPod(podId);
List<VlanVO> result = new ArrayList<VlanVO>();
for (PodVlanMapVO pvmvo: vlanMaps) {
result.add(findById(pvmvo.getVlanDbId()));
}
return result;
}
@Override
public List<VlanVO> listVlansForPodByType(long podId, VlanType vlanType) {
//FIXME: use a join statement to improve the performance (should be minor since we expect only one or two)
List<PodVlanMapVO> vlanMaps = _podVlanMapDao.listPodVlanMapsByPod(podId);
List<VlanVO> result = new ArrayList<VlanVO>();
for (PodVlanMapVO pvmvo: vlanMaps) {
VlanVO vlan =findById(pvmvo.getVlanDbId());
if (vlan.getVlanType() == vlanType) {
result.add(vlan);
}
}
return result;
}
@Override
public List<VlanVO> listVlansForAccountByType(Long zoneId, long accountId, VlanType vlanType) {
//FIXME: use a join statement to improve the performance (should be minor since we expect only one or two)
List<AccountVlanMapVO> vlanMaps = _accountVlanMapDao.listAccountVlanMapsByAccount(accountId);
List<VlanVO> result = new ArrayList<VlanVO>();
for (AccountVlanMapVO acvmvo: vlanMaps) {
VlanVO vlan =findById(acvmvo.getVlanDbId());
if (vlan.getVlanType() == vlanType && (zoneId == null || vlan.getDataCenterId() == zoneId)) {
result.add(vlan);
}
}
return result;
}
@Override
public void addToPod(long podId, long vlanDbId) {
PodVlanMapVO pvmvo = new PodVlanMapVO(podId, vlanDbId);
_podVlanMapDao.persist(pvmvo);
}
@Override
public boolean configure(String name, Map<String, Object> params)
throws ConfigurationException {
boolean result = super.configure(name, params);
ZoneTypeAllPodsSearch = createSearchBuilder();
ZoneTypeAllPodsSearch.and("zoneId", ZoneTypeAllPodsSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
ZoneTypeAllPodsSearch.and("vlanType", ZoneTypeAllPodsSearch.entity().getVlanType(), SearchCriteria.Op.EQ);
SearchBuilder<PodVlanMapVO> PodVlanSearch = _podVlanMapDao.createSearchBuilder();
PodVlanSearch.and("podId", PodVlanSearch.entity().getPodId(), SearchCriteria.Op.NNULL);
ZoneTypeAllPodsSearch.join("vlan", PodVlanSearch, PodVlanSearch.entity().getVlanDbId(), ZoneTypeAllPodsSearch.entity().getId(), JoinBuilder.JoinType.INNER);
ZoneTypeAllPodsSearch.done();
PodVlanSearch.done();
ZoneTypePodSearch = createSearchBuilder();
ZoneTypePodSearch.and("zoneId", ZoneTypePodSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
ZoneTypePodSearch.and("vlanType", ZoneTypePodSearch.entity().getVlanType(), SearchCriteria.Op.EQ);
SearchBuilder<PodVlanMapVO> PodVlanSearch2 = _podVlanMapDao.createSearchBuilder();
PodVlanSearch2.and("podId", PodVlanSearch2.entity().getPodId(), SearchCriteria.Op.EQ);
ZoneTypePodSearch.join("vlan", PodVlanSearch2, PodVlanSearch2.entity().getVlanDbId(), ZoneTypePodSearch.entity().getId(), JoinBuilder.JoinType.INNER);
PodVlanSearch2.done();
ZoneTypePodSearch.done();
return result;
}
private VlanVO findNextVlan(long zoneId, Vlan.VlanType vlanType) {
List<VlanVO> allVlans = listByZoneAndType(zoneId, vlanType);
List<VlanVO> emptyVlans = new ArrayList<VlanVO>();
List<VlanVO> fullVlans = new ArrayList<VlanVO>();
// Try to find a VLAN that is partially allocated
for (VlanVO vlan : allVlans) {
long vlanDbId = vlan.getId();
int countOfAllocatedIps = _ipAddressDao.countIPs(zoneId, vlanDbId, true);
int countOfAllIps = _ipAddressDao.countIPs(zoneId, vlanDbId, false);
if ((countOfAllocatedIps > 0) && (countOfAllocatedIps < countOfAllIps)) {
return vlan;
} else if (countOfAllocatedIps == 0) {
emptyVlans.add(vlan);
} else if (countOfAllocatedIps == countOfAllIps) {
fullVlans.add(vlan);
}
}
if (emptyVlans.isEmpty()) {
return null;
}
// Try to find an empty VLAN with the same tag/subnet as a VLAN that is full
for (VlanVO fullVlan : fullVlans) {
for (VlanVO emptyVlan : emptyVlans) {
if (fullVlan.getVlanTag().equals(emptyVlan.getVlanTag()) &&
fullVlan.getVlanGateway().equals(emptyVlan.getVlanGateway()) &&
fullVlan.getVlanNetmask().equals(emptyVlan.getVlanNetmask())) {
return emptyVlan;
}
}
}
// Return a random empty VLAN
return emptyVlans.get(0);
}
@Override
public boolean zoneHasDirectAttachUntaggedVlans(long zoneId) {
SearchCriteria<VlanVO> sc = ZoneTypeAllPodsSearch.create();
sc.setParameters("zoneId", zoneId);
sc.setParameters("vlanType", VlanType.DirectAttached);
return listIncludingRemovedBy(sc).size() > 0;
}
public Pair<String, VlanVO> assignPodDirectAttachIpAddress(long zoneId,
long podId, long accountId, long domainId) {
SearchCriteria<VlanVO> sc = ZoneTypePodSearch.create();
sc.setParameters("zoneId", zoneId);
sc.setParameters("vlanType", VlanType.DirectAttached);
sc.setJoinParameters("vlan", "podId", podId);
VlanVO vlan = findOneIncludingRemovedBy(sc);
if (vlan == null) {
return null;
}
return null;
// String ipAddress = _ipAddressDao.assignIpAddress(accountId, domainId, vlan.getId(), false).getAddress();
// if (ipAddress == null) {
// return null;
// }
// return new Pair<String, VlanVO>(ipAddress, vlan);
}
@Override
@DB
public List<VlanVO> searchForZoneWideVlans(long dcId, String vlanType, String vlanId){
StringBuilder sql = new StringBuilder(FindZoneWideVlans);
Transaction txn = Transaction.currentTxn();
PreparedStatement pstmt = null;
try {
pstmt = txn.prepareAutoCloseStatement(sql.toString());
pstmt.setLong(1, dcId);
pstmt.setString(2, vlanType);
pstmt.setString(3, vlanId);
ResultSet rs = pstmt.executeQuery();
List<VlanVO> zoneWideVlans = new ArrayList<VlanVO>();
while (rs.next()) {
zoneWideVlans.add(toEntityBean(rs, false));
}
return zoneWideVlans;
} catch (SQLException e) {
throw new CloudRuntimeException("Unable to execute " + pstmt.toString(), e);
}
}
@Override
public List<VlanVO> listVlansByNetworkId(long networkOfferingId) {
SearchCriteria<VlanVO> sc = NetworkVlanSearch.create();
sc.setParameters("networkOfferingId", networkOfferingId);
return listBy(sc);
}
@Override
public List<VlanVO> listVlansByPhysicalNetworkId(long physicalNetworkId) {
SearchCriteria<VlanVO> sc = PhysicalNetworkVlanSearch.create();
sc.setParameters("physicalNetworkId", physicalNetworkId);
return listBy(sc);
}
}