2424import java .util .List ;
2525import java .util .Map ;
2626
27+ import javax .annotation .PostConstruct ;
2728import javax .ejb .Local ;
2829import javax .inject .Inject ;
2930import javax .naming .ConfigurationException ;
3031
31- import org .springframework .stereotype .Component ;
32-
3332import com .cloud .host .Status ;
3433import com .cloud .hypervisor .Hypervisor .HypervisorType ;
3534import com .cloud .storage .ScopeType ;
35+ import com .cloud .storage .StoragePoolHostVO ;
3636import com .cloud .storage .StoragePoolStatus ;
37+ import com .cloud .storage .dao .StoragePoolHostDao ;
3738import com .cloud .utils .db .DB ;
3839import com .cloud .utils .db .GenericDaoBase ;
3940import com .cloud .utils .db .GenericSearchBuilder ;
41+ import com .cloud .utils .db .JoinBuilder ;
4042import com .cloud .utils .db .QueryBuilder ;
4143import com .cloud .utils .db .SearchBuilder ;
4244import com .cloud .utils .db .SearchCriteria ;
4547import com .cloud .utils .db .TransactionLegacy ;
4648import com .cloud .utils .exception .CloudRuntimeException ;
4749
48-
4950@ Local (value = { PrimaryDataStoreDao .class })
5051@ DB ()
5152public class PrimaryDataStoreDaoImpl extends GenericDaoBase <StoragePoolVO , Long > implements PrimaryDataStoreDao {
@@ -54,8 +55,12 @@ public class PrimaryDataStoreDaoImpl extends GenericDaoBase<StoragePoolVO, Long>
5455 protected final SearchBuilder <StoragePoolVO > DcPodAnyClusterSearch ;
5556 protected final SearchBuilder <StoragePoolVO > DeleteLvmSearch ;
5657 protected final GenericSearchBuilder <StoragePoolVO , Long > StatusCountSearch ;
58+ protected SearchBuilder <StoragePoolVO > HostSearch ;
59+ protected SearchBuilder <StoragePoolHostVO > HostPoolSearch ;
60+ protected SearchBuilder <StoragePoolDetailVO > TagPoolSearch ;
5761
5862 @ Inject protected StoragePoolDetailsDao _detailsDao ;
63+ @ Inject protected StoragePoolHostDao _hostDao ;
5964
6065 private final String DetailsSqlPrefix = "SELECT storage_pool.* from storage_pool LEFT JOIN storage_pool_details ON storage_pool.id = storage_pool_details.pool_id WHERE storage_pool.removed is null and storage_pool.status = 'Up' and storage_pool.data_center_id = ? and (storage_pool.pod_id = ? or storage_pool.pod_id is null) and storage_pool.scope = ? and (" ;
6166 private final String DetailsSqlSuffix = ") GROUP BY storage_pool_details.pool_id HAVING COUNT(storage_pool_details.name) >= ?" ;
@@ -112,6 +117,26 @@ public PrimaryDataStoreDaoImpl() {
112117
113118 }
114119
120+ @ PostConstruct
121+ void init () {
122+ HostSearch = createSearchBuilder ();
123+ TagPoolSearch = _detailsDao .createSearchBuilder ();
124+ HostPoolSearch = _hostDao .createSearchBuilder ();
125+ // Search for pools on the host
126+ HostPoolSearch .and ("hostId" , HostPoolSearch .entity ().getHostId (), Op .EQ );
127+ // Set criteria for pools
128+ HostSearch .and ("scope" , HostSearch .entity ().getScope (), Op .EQ );
129+ HostSearch .and ("removed" , HostSearch .entity ().getRemoved (), Op .NULL );
130+ HostSearch .and ("status" , HostSearch .entity ().getStatus (), Op .EQ );
131+ HostSearch .join ("hostJoin" , HostPoolSearch , HostSearch .entity ().getId (), HostPoolSearch .entity ().getPoolId (), JoinBuilder .JoinType .INNER );
132+ // Set criteria for tags
133+ TagPoolSearch .and ("name" , TagPoolSearch .entity ().getName (), Op .EQ );
134+ TagPoolSearch .and ("value" , TagPoolSearch .entity ().getValue (), Op .EQ );
135+
136+ HostSearch .join ("tagJoin" , TagPoolSearch , HostSearch .entity ().getId (), TagPoolSearch .entity ().getResourceId (), JoinBuilder .JoinType .INNER );
137+ HostSearch .done ();
138+ }
139+
115140 @ Override
116141 public List <StoragePoolVO > findPoolByName (String name ) {
117142 SearchCriteria <StoragePoolVO > sc = AllFieldSearch .create ();
@@ -353,6 +378,23 @@ public List<StoragePoolVO> findZoneWideStoragePoolsByTags(long dcId, String[] ta
353378 }
354379 }
355380
381+ @ Override
382+ public List <StoragePoolVO > findLocalStoragePoolsByHostAndTags (long hostId , String [] tags ) {
383+
384+ SearchCriteria <StoragePoolVO > sc = HostSearch .create ();
385+ sc .setJoinParameters ("hostJoin" , "hostId" , hostId );
386+ sc .setParameters ("scope" , ScopeType .HOST .toString ());
387+ sc .setParameters ("status" , Status .Up .toString ());
388+ if (!(tags == null || tags .length == 0 )) {
389+ Map <String , String > details = tagsToDetails (tags );
390+ for (Map .Entry <String , String > detail : details .entrySet ()) {
391+ sc .setJoinParameters ("tagJoin" ,"name" , detail .getKey ());
392+ sc .setJoinParameters ("tagJoin" , "value" , detail .getValue ());
393+ }
394+ }
395+ return listBy (sc );
396+ }
397+
356398 @ Override
357399 @ DB
358400 public List <String > searchForStoragePoolDetails (long poolId , String value ) {
0 commit comments