Repository navigation
Expand file tree
/
Copy pathPodVlanDaoImpl.java
More file actions
executable file
·136 lines (119 loc) · 5.11 KB
/
Copy pathPodVlanDaoImpl.java
File metadata and controls
executable file
·136 lines (119 loc) · 5.11 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
// 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.SQLException;
import java.util.Date;
import java.util.List;
import com.cloud.dc.PodVlanVO;
import com.cloud.utils.db.GenericDao;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.db.Transaction;
import com.cloud.utils.exception.CloudRuntimeException;
/**
* PodVlanDaoImpl maintains the one-to-many relationship between
*/
public class PodVlanDaoImpl extends GenericDaoBase<PodVlanVO, Long> implements GenericDao<PodVlanVO, Long> {
private final SearchBuilder<PodVlanVO> FreeVlanSearch;
private final SearchBuilder<PodVlanVO> VlanPodSearch;
private final SearchBuilder<PodVlanVO> PodSearchAllocated;
public List<PodVlanVO> listAllocatedVnets(long podId) {
SearchCriteria<PodVlanVO> sc = PodSearchAllocated.create();
sc.setParameters("podId", podId);
return listBy(sc);
}
public void add(long podId, int start, int end) {
String insertVnet = "INSERT INTO `cloud`.`op_pod_vlan_alloc` (vlan, pod_id) VALUES ( ?, ?)";
Transaction txn = Transaction.currentTxn();
try {
txn.start();
PreparedStatement stmt = txn.prepareAutoCloseStatement(insertVnet);
for (int i = start; i < end; i++) {
stmt.setString(1, String.valueOf(i));
stmt.setLong(2, podId);
stmt.addBatch();
}
stmt.executeBatch();
txn.commit();
} catch (SQLException e) {
throw new CloudRuntimeException("Exception caught adding vnet ", e);
}
}
public void delete(long podId) {
String deleteVnet = "DELETE FROM `cloud`.`op_pod_vlan_alloc` WHERE pod_id = ?";
Transaction txn = Transaction.currentTxn();
try {
PreparedStatement stmt = txn.prepareAutoCloseStatement(deleteVnet);
stmt.setLong(1, podId);
stmt.executeUpdate();
} catch (SQLException e) {
throw new CloudRuntimeException("Exception caught deleting vnet ", e);
}
}
public PodVlanVO take(long podId, long accountId) {
SearchCriteria<PodVlanVO> sc = FreeVlanSearch.create();
sc.setParameters("podId", podId);
Date now = new Date();
Transaction txn = Transaction.currentTxn();
try {
txn.start();
PodVlanVO vo = lockOneRandomRow(sc, true);
if (vo == null) {
return null;
}
vo.setTakenAt(now);
vo.setAccountId(accountId);
update(vo.getId(), vo);
txn.commit();
return vo;
} catch (Exception e) {
throw new CloudRuntimeException("Caught Exception ", e);
}
}
public void release(String vlan, long podId, long accountId) {
SearchCriteria<PodVlanVO> sc = VlanPodSearch.create();
sc.setParameters("vlan", vlan);
sc.setParameters("podId", podId);
sc.setParameters("account", accountId);
PodVlanVO vo = findOneIncludingRemovedBy(sc);
if (vo == null) {
return;
}
vo.setTakenAt(null);
vo.setAccountId(null);
update(vo.getId(), vo);
}
protected PodVlanDaoImpl() {
super();
PodSearchAllocated = createSearchBuilder();
PodSearchAllocated.and("podId", PodSearchAllocated.entity().getPodId(), SearchCriteria.Op.EQ);
PodSearchAllocated.and("allocated", PodSearchAllocated.entity().getTakenAt(), SearchCriteria.Op.NNULL);
PodSearchAllocated.done();
FreeVlanSearch = createSearchBuilder();
FreeVlanSearch.and("podId", FreeVlanSearch.entity().getPodId(), SearchCriteria.Op.EQ);
FreeVlanSearch.and("taken", FreeVlanSearch.entity().getTakenAt(), SearchCriteria.Op.NULL);
FreeVlanSearch.done();
VlanPodSearch = createSearchBuilder();
VlanPodSearch.and("vlan", VlanPodSearch.entity().getVlan(), SearchCriteria.Op.EQ);
VlanPodSearch.and("podId", VlanPodSearch.entity().getPodId(), SearchCriteria.Op.EQ);
VlanPodSearch.and("taken", VlanPodSearch.entity().getTakenAt(), SearchCriteria.Op.NNULL);
VlanPodSearch.and("account", VlanPodSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
VlanPodSearch.done();
}
}