forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNicDaoImpl.java
More file actions
205 lines (176 loc) · 7.92 KB
/
Copy pathNicDaoImpl.java
File metadata and controls
205 lines (176 loc) · 7.92 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
// 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.vm.dao;
import java.util.List;
import javax.ejb.Local;
import org.springframework.stereotype.Component;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.GenericSearchBuilder;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.db.SearchCriteria.Func;
import com.cloud.utils.db.SearchCriteria.Op;
import com.cloud.vm.Nic;
import com.cloud.vm.Nic.State;
import com.cloud.vm.NicVO;
import com.cloud.vm.VirtualMachine;
@Component
@Local(value=NicDao.class)
public class NicDaoImpl extends GenericDaoBase<NicVO, Long> implements NicDao {
private final SearchBuilder<NicVO> AllFieldsSearch;
private final GenericSearchBuilder<NicVO, String> IpSearch;
private final SearchBuilder<NicVO> NonReleasedSearch;
final GenericSearchBuilder<NicVO, Integer> CountBy;
public NicDaoImpl() {
super();
AllFieldsSearch = createSearchBuilder();
AllFieldsSearch.and("instance", AllFieldsSearch.entity().getInstanceId(), Op.EQ);
AllFieldsSearch.and("network", AllFieldsSearch.entity().getNetworkId(), Op.EQ);
AllFieldsSearch.and("gateway", AllFieldsSearch.entity().getGateway(), Op.EQ);
AllFieldsSearch.and("vmType", AllFieldsSearch.entity().getVmType(), Op.EQ);
AllFieldsSearch.and("address", AllFieldsSearch.entity().getIp4Address(), Op.EQ);
AllFieldsSearch.and("isDefault", AllFieldsSearch.entity().isDefaultNic(), Op.EQ);
AllFieldsSearch.and("broadcastUri", AllFieldsSearch.entity().getBroadcastUri(), Op.EQ);
AllFieldsSearch.done();
IpSearch = createSearchBuilder(String.class);
IpSearch.select(null, Func.DISTINCT, IpSearch.entity().getIp4Address());
IpSearch.and("network", IpSearch.entity().getNetworkId(), Op.EQ);
IpSearch.and("address", IpSearch.entity().getIp4Address(), Op.NNULL);
IpSearch.done();
NonReleasedSearch = createSearchBuilder();
NonReleasedSearch.and("instance", NonReleasedSearch.entity().getInstanceId(), Op.EQ);
NonReleasedSearch.and("network", NonReleasedSearch.entity().getNetworkId(), Op.EQ);
NonReleasedSearch.and("state", NonReleasedSearch.entity().getState(), Op.NOTIN);
NonReleasedSearch.done();
CountBy = createSearchBuilder(Integer.class);
CountBy.select(null, Func.COUNT, CountBy.entity().getId());
CountBy.and("vmId", CountBy.entity().getInstanceId(), Op.EQ);
CountBy.and("removed", CountBy.entity().getRemoved(), Op.NULL);
CountBy.done();
}
@Override
public void removeNicsForInstance(long instanceId) {
SearchCriteria<NicVO> sc = AllFieldsSearch.create();
sc.setParameters("instance", instanceId);
remove(sc);
}
@Override
public List<NicVO> listByVmId(long instanceId) {
SearchCriteria<NicVO> sc = AllFieldsSearch.create();
sc.setParameters("instance", instanceId);
return listBy(sc);
}
@Override
public List<NicVO> listByVmIdIncludingRemoved(long instanceId) {
SearchCriteria<NicVO> sc = AllFieldsSearch.create();
sc.setParameters("instance", instanceId);
return listIncludingRemovedBy(sc);
}
@Override
public List<String> listIpAddressInNetwork(long networkId) {
SearchCriteria<String> sc = IpSearch.create();
sc.setParameters("network", networkId);
return customSearch(sc, null);
}
@Override
public List<NicVO> listByNetworkId(long networkId) {
SearchCriteria<NicVO> sc = AllFieldsSearch.create();
sc.setParameters("network", networkId);
return listBy(sc);
}
@Override
public NicVO findByInstanceIdAndNetworkId(long networkId, long instanceId) {
SearchCriteria<NicVO> sc = AllFieldsSearch.create();
sc.setParameters("network", networkId);
sc.setParameters("instance", instanceId);
return findOneBy(sc);
}
@Override
public NicVO findByInstanceIdAndNetworkIdIncludingRemoved(long networkId, long instanceId) {
SearchCriteria<NicVO> sc = createSearchCriteria();
sc.addAnd("networkId", SearchCriteria.Op.EQ, networkId);
sc.addAnd("instanceId", SearchCriteria.Op.EQ, instanceId);
return findOneIncludingRemovedBy(sc);
}
@Override
public NicVO findByNetworkIdAndType(long networkId, VirtualMachine.Type vmType) {
SearchCriteria<NicVO> sc = AllFieldsSearch.create();
sc.setParameters("network", networkId);
sc.setParameters("vmType", vmType);
return findOneBy(sc);
}
@Override
public NicVO findByNetworkIdTypeAndGateway(long networkId, VirtualMachine.Type vmType, String gateway) {
SearchCriteria<NicVO> sc = AllFieldsSearch.create();
sc.setParameters("network", networkId);
sc.setParameters("vmType", vmType);
sc.setParameters("gateway", gateway);
return findOneBy(sc);
}
@Override
public NicVO findByIp4AddressAndNetworkId(String ip4Address, long networkId) {
SearchCriteria<NicVO> sc = AllFieldsSearch.create();
sc.setParameters("address", ip4Address);
sc.setParameters("network", networkId);
return findOneBy(sc);
}
@Override
public NicVO findDefaultNicForVM(long instanceId) {
SearchCriteria<NicVO> sc = AllFieldsSearch.create();
sc.setParameters("instance", instanceId);
sc.setParameters("isDefault", 1);
return findOneBy(sc);
}
@Override
public NicVO findNonReleasedByInstanceIdAndNetworkId(long networkId, long instanceId) {
SearchCriteria<NicVO> sc = NonReleasedSearch.create();
sc.setParameters("network", networkId);
sc.setParameters("instance", instanceId);
sc.setParameters("state", State.Releasing, Nic.State.Deallocating);
return findOneBy(sc);
}
@Override
public String getIpAddress(long networkId, long instanceId) {
SearchCriteria<NicVO> sc = AllFieldsSearch.create();
sc.setParameters("network", networkId);
sc.setParameters("instance", instanceId);
return findOneBy(sc).getIp4Address();
}
@Override
public int countNics(long instanceId) {
SearchCriteria<Integer> sc = CountBy.create();
sc.setParameters("vmId", instanceId);
List<Integer> results = customSearch(sc, null);
return results.get(0);
}
@Override
public NicVO findByNetworkIdInstanceIdAndBroadcastUri(long networkId, long instanceId, String broadcastUri) {
SearchCriteria<NicVO> sc = AllFieldsSearch.create();
sc.setParameters("network", networkId);
sc.setParameters("instance", instanceId);
sc.setParameters("broadcastUri", broadcastUri);
return findOneBy(sc);
}
@Override
public NicVO findByIp4AddressAndNetworkIdAndInstanceId(long networkId, long instanceId, String ip4Address) {
SearchCriteria<NicVO> sc = AllFieldsSearch.create();
sc.setParameters("network", networkId);
sc.setParameters("instance", instanceId);
sc.setParameters("address", ip4Address);
return findOneBy(sc);
}
}