3232
3333import org .apache .log4j .Logger ;
3434
35+ import com .cloud .host .Host ;
3536import com .cloud .host .HostVO ;
3637import com .cloud .host .dao .HostDao ;
3738import com .cloud .storage .VMTemplateHostVO ;
@@ -58,6 +59,7 @@ public class VMTemplateHostDaoImpl extends GenericDaoBase<VMTemplateHostVO, Long
5859 protected final SearchBuilder <VMTemplateHostVO > TemplateStatusSearch ;
5960 protected final SearchBuilder <VMTemplateHostVO > TemplateStatesSearch ;
6061 protected SearchBuilder <VMTemplateHostVO > ZONE_TEMPLATE_SEARCH ;
62+ protected SearchBuilder <VMTemplateHostVO > LOCAL_SECONDARY_STORAGE_SEARCH ;
6163
6264
6365 protected static final String UPDATE_TEMPLATE_HOST_REF =
@@ -132,6 +134,18 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
132134 hostSearch .and ("zone_id" , hostSearch .entity ().getDataCenterId (), SearchCriteria .Op .EQ );
133135 ZONE_TEMPLATE_SEARCH .join ("tmplHost" , hostSearch , hostSearch .entity ().getId (), ZONE_TEMPLATE_SEARCH .entity ().getHostId (), JoinBuilder .JoinType .INNER );
134136 ZONE_TEMPLATE_SEARCH .done ();
137+
138+ LOCAL_SECONDARY_STORAGE_SEARCH = createSearchBuilder ();
139+ LOCAL_SECONDARY_STORAGE_SEARCH .and ("template_id" , LOCAL_SECONDARY_STORAGE_SEARCH .entity ().getTemplateId (), SearchCriteria .Op .EQ );
140+ LOCAL_SECONDARY_STORAGE_SEARCH .and ("state" , LOCAL_SECONDARY_STORAGE_SEARCH .entity ().getDownloadState (), SearchCriteria .Op .EQ );
141+ SearchBuilder <HostVO > localSecondaryHost = _hostDao .createSearchBuilder ();
142+ localSecondaryHost .and ("private_ip_address" , localSecondaryHost .entity ().getPrivateIpAddress (), SearchCriteria .Op .EQ );
143+ localSecondaryHost .and ("state" , localSecondaryHost .entity ().getStatus (), SearchCriteria .Op .EQ );
144+ localSecondaryHost .and ("data_center_id" , localSecondaryHost .entity ().getDataCenterId (), SearchCriteria .Op .EQ );
145+ localSecondaryHost .and ("type" , localSecondaryHost .entity ().getType (), SearchCriteria .Op .EQ );
146+ LOCAL_SECONDARY_STORAGE_SEARCH .join ("host" , localSecondaryHost , localSecondaryHost .entity ().getId (), LOCAL_SECONDARY_STORAGE_SEARCH .entity ().getHostId (), JoinBuilder .JoinType .INNER );
147+ LOCAL_SECONDARY_STORAGE_SEARCH .done ();
148+
135149 return result ;
136150 }
137151 @ Override
@@ -296,12 +310,9 @@ public List<VMTemplateHostVO> listByZoneTemplate(long dcId, long templateId, boo
296310 sc .setJoinParameters ("tmplHost" , "zone_id" , dcId );
297311 if (readyOnly ) {
298312 sc .setParameters ("state" , VMTemplateHostVO .Status .DOWNLOADED );
299- List <VMTemplateHostVO > tmplHost = new ArrayList <VMTemplateHostVO >();
300- tmplHost .add (findOneBy (sc ));
301- return tmplHost ;
302- } else {
303- return listBy (sc );
304- }
313+ }
314+ return listBy (sc );
315+
305316 }
306317
307318 @ Override
@@ -330,6 +341,20 @@ public VMTemplateHostVO findByHostTemplate(long hostId, long templateId, boolean
330341 else
331342 return lockOneRandomRow (sc , true );
332343 }
344+
345+ //Based on computing node host id, and template id, find out the corresponding template_host_ref, assuming local secondary storage and computing node is in the same zone, and private ip
346+ @ Override
347+ public VMTemplateHostVO findLocalSecondaryStorageByHostTemplate (long hostId , long templateId ) {
348+ HostVO computingHost = _hostDao .findById (hostId );
349+ SearchCriteria <VMTemplateHostVO > sc = LOCAL_SECONDARY_STORAGE_SEARCH .create ();
350+ sc .setJoinParameters ("host" , "private_ip_address" , computingHost .getPrivateIpAddress ());
351+ sc .setJoinParameters ("host" , "state" , com .cloud .host .Status .Up );
352+ sc .setJoinParameters ("host" , "data_center_id" , computingHost .getDataCenterId ());
353+ sc .setJoinParameters ("host" , "type" , Host .Type .LocalSecondaryStorage );
354+ sc .setParameters ("template_id" , templateId );
355+ sc .setParameters ("state" , VMTemplateHostVO .Status .DOWNLOADED );
356+ return findOneBy (sc );
357+ }
333358
334359 @ Override
335360 public void deleteByHost (Long hostId ) {
0 commit comments