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
3132import com .cloud .host .Status ;
3233import com .cloud .hypervisor .Hypervisor .HypervisorType ;
3334import com .cloud .storage .ScopeType ;
35+ import com .cloud .storage .StoragePoolHostVO ;
3436import com .cloud .storage .StoragePoolStatus ;
37+ import com .cloud .storage .dao .StoragePoolHostDao ;
3538import com .cloud .utils .db .DB ;
3639import com .cloud .utils .db .GenericDaoBase ;
3740import com .cloud .utils .db .GenericSearchBuilder ;
41+ import com .cloud .utils .db .JoinBuilder ;
3842import com .cloud .utils .db .QueryBuilder ;
3943import com .cloud .utils .db .SearchBuilder ;
4044import com .cloud .utils .db .SearchCriteria ;
@@ -51,9 +55,14 @@ public class PrimaryDataStoreDaoImpl extends GenericDaoBase<StoragePoolVO, Long>
5155 protected final SearchBuilder <StoragePoolVO > DcPodAnyClusterSearch ;
5256 protected final SearchBuilder <StoragePoolVO > DeleteLvmSearch ;
5357 protected final GenericSearchBuilder <StoragePoolVO , Long > StatusCountSearch ;
58+ protected SearchBuilder <StoragePoolVO > HostSearch ;
59+ protected SearchBuilder <StoragePoolHostVO > HostPoolSearch ;
60+ protected SearchBuilder <StoragePoolDetailVO > TagPoolSearch ;
5461
5562 @ Inject
5663 protected StoragePoolDetailsDao _detailsDao ;
64+ @ Inject
65+ protected StoragePoolHostDao _hostDao ;
5766
5867 private final String DetailsSqlPrefix =
5968 "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 (" ;
@@ -111,6 +120,26 @@ public PrimaryDataStoreDaoImpl() {
111120
112121 }
113122
123+ @ PostConstruct
124+ void init () {
125+ HostSearch = createSearchBuilder ();
126+ TagPoolSearch = _detailsDao .createSearchBuilder ();
127+ HostPoolSearch = _hostDao .createSearchBuilder ();
128+ // Search for pools on the host
129+ HostPoolSearch .and ("hostId" , HostPoolSearch .entity ().getHostId (), Op .EQ );
130+ // Set criteria for pools
131+ HostSearch .and ("scope" , HostSearch .entity ().getScope (), Op .EQ );
132+ HostSearch .and ("removed" , HostSearch .entity ().getRemoved (), Op .NULL );
133+ HostSearch .and ("status" , HostSearch .entity ().getStatus (), Op .EQ );
134+ HostSearch .join ("hostJoin" , HostPoolSearch , HostSearch .entity ().getId (), HostPoolSearch .entity ().getPoolId (), JoinBuilder .JoinType .INNER );
135+ // Set criteria for tags
136+ TagPoolSearch .and ("name" , TagPoolSearch .entity ().getName (), Op .EQ );
137+ TagPoolSearch .and ("value" , TagPoolSearch .entity ().getValue (), Op .EQ );
138+
139+ HostSearch .join ("tagJoin" , TagPoolSearch , HostSearch .entity ().getId (), TagPoolSearch .entity ().getResourceId (), JoinBuilder .JoinType .INNER );
140+ HostSearch .done ();
141+ }
142+
114143 @ Override
115144 public List <StoragePoolVO > findPoolByName (String name ) {
116145 SearchCriteria <StoragePoolVO > sc = AllFieldSearch .create ();
@@ -314,6 +343,23 @@ public List<StoragePoolVO> findLocalStoragePoolsByTags(long dcId, long podId, Lo
314343 return storagePools ;
315344 }
316345
346+ @ Override
347+ public List <StoragePoolVO > findLocalStoragePoolsByHostAndTags (long hostId , String [] tags ) {
348+
349+ SearchCriteria <StoragePoolVO > sc = HostSearch .create ();
350+ sc .setJoinParameters ("hostJoin" , "hostId" , hostId );
351+ sc .setParameters ("scope" , ScopeType .HOST .toString ());
352+ sc .setParameters ("status" , Status .Up .toString ());
353+ if (!(tags == null || tags .length == 0 )) {
354+ Map <String , String > details = tagsToDetails (tags );
355+ for (Map .Entry <String , String > detail : details .entrySet ()) {
356+ sc .setJoinParameters ("tagJoin" ,"name" , detail .getKey ());
357+ sc .setJoinParameters ("tagJoin" , "value" , detail .getValue ());
358+ }
359+ }
360+ return listBy (sc );
361+ }
362+
317363 @ Override
318364 public List <StoragePoolVO > findZoneWideStoragePoolsByTags (long dcId , String [] tags ) {
319365 List <StoragePoolVO > storagePools = null ;
0 commit comments