|
16 | 16 | // under the License. |
17 | 17 | package com.cloud.vm.dao; |
18 | 18 |
|
| 19 | +import java.net.URI; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import javax.annotation.PostConstruct; |
| 23 | +import javax.ejb.Local; |
| 24 | +import javax.inject.Inject; |
| 25 | + |
| 26 | +import org.springframework.stereotype.Component; |
| 27 | + |
19 | 28 | import com.cloud.utils.db.GenericDaoBase; |
20 | 29 | import com.cloud.utils.db.GenericSearchBuilder; |
| 30 | +import com.cloud.utils.db.JoinBuilder; |
21 | 31 | import com.cloud.utils.db.SearchBuilder; |
22 | 32 | import com.cloud.utils.db.SearchCriteria; |
23 | 33 | import com.cloud.utils.db.SearchCriteria.Func; |
24 | 34 | import com.cloud.utils.db.SearchCriteria.Op; |
25 | 35 | import com.cloud.vm.Nic; |
26 | 36 | import com.cloud.vm.Nic.State; |
27 | 37 | import com.cloud.vm.NicVO; |
| 38 | +import com.cloud.vm.VMInstanceVO; |
28 | 39 | import com.cloud.vm.VirtualMachine; |
29 | | -import org.springframework.stereotype.Component; |
30 | | - |
31 | | -import javax.ejb.Local; |
32 | | -import java.net.URI; |
33 | | -import java.util.List; |
34 | 40 |
|
35 | 41 | @Component |
36 | 42 | @Local(value=NicDao.class) |
37 | 43 | public class NicDaoImpl extends GenericDaoBase<NicVO, Long> implements NicDao { |
38 | | - private final SearchBuilder<NicVO> AllFieldsSearch; |
39 | | - private final GenericSearchBuilder<NicVO, String> IpSearch; |
40 | | - private final SearchBuilder<NicVO> NonReleasedSearch; |
41 | | - final GenericSearchBuilder<NicVO, Integer> CountBy; |
| 44 | + private SearchBuilder<NicVO> AllFieldsSearch; |
| 45 | + private GenericSearchBuilder<NicVO, String> IpSearch; |
| 46 | + private SearchBuilder<NicVO> NonReleasedSearch; |
| 47 | + private GenericSearchBuilder<NicVO, Integer> CountBy; |
| 48 | + private GenericSearchBuilder<NicVO, Integer> CountByForStartingVms; |
42 | 49 |
|
| 50 | + @Inject |
| 51 | + VMInstanceDao _vmDao; |
43 | 52 |
|
44 | | - public NicDaoImpl() { |
45 | | - super(); |
| 53 | + public NicDaoImpl() { |
46 | 54 |
|
| 55 | + } |
| 56 | + |
| 57 | + @PostConstruct |
| 58 | + protected void init() { |
47 | 59 | AllFieldsSearch = createSearchBuilder(); |
48 | 60 | AllFieldsSearch.and("instance", AllFieldsSearch.entity().getInstanceId(), Op.EQ); |
49 | 61 | AllFieldsSearch.and("network", AllFieldsSearch.entity().getNetworkId(), Op.EQ); |
@@ -73,6 +85,15 @@ public NicDaoImpl() { |
73 | 85 | CountBy.and("vmId", CountBy.entity().getInstanceId(), Op.EQ); |
74 | 86 | CountBy.and("removed", CountBy.entity().getRemoved(), Op.NULL); |
75 | 87 | CountBy.done(); |
| 88 | + |
| 89 | + CountByForStartingVms = createSearchBuilder(Integer.class); |
| 90 | + CountByForStartingVms.select(null, Func.COUNT, CountByForStartingVms.entity().getId()); |
| 91 | + CountByForStartingVms.and("networkId", CountByForStartingVms.entity().getNetworkId(), Op.EQ); |
| 92 | + CountByForStartingVms.and("removed", CountByForStartingVms.entity().getRemoved(), Op.NULL); |
| 93 | + SearchBuilder<VMInstanceVO> join1 = _vmDao.createSearchBuilder(); |
| 94 | + join1.and("state", join1.entity().getState(), Op.EQ); |
| 95 | + CountByForStartingVms.join("vm", join1, CountByForStartingVms.entity().getInstanceId(), join1.entity().getId(), JoinBuilder.JoinType.INNER); |
| 96 | + CountByForStartingVms.done(); |
76 | 97 | } |
77 | 98 |
|
78 | 99 | @Override |
@@ -256,4 +277,13 @@ public List<NicVO> listPlaceholderNicsByNetworkIdAndVmType(long networkId, Virtu |
256 | 277 | return listBy(sc); |
257 | 278 | } |
258 | 279 |
|
| 280 | + @Override |
| 281 | + public int countNicsForStartingVms(long networkId) { |
| 282 | + SearchCriteria<Integer> sc = CountByForStartingVms.create(); |
| 283 | + sc.setParameters("networkId", networkId); |
| 284 | + sc.setJoinParameters("vm", "state", VirtualMachine.State.Starting); |
| 285 | + List<Integer> results = customSearch(sc, null); |
| 286 | + return results.get(0); |
| 287 | + } |
| 288 | + |
259 | 289 | } |
0 commit comments