2323import java .util .Date ;
2424import java .util .List ;
2525import java .util .Map ;
26+ import java .util .Collections ;
2627
2728import javax .ejb .Local ;
2829import javax .inject .Inject ;
2930import javax .naming .ConfigurationException ;
3031
32+ import com .cloud .storage .VMTemplateStorageResourceAssoc ;
33+ import org .apache .cloudstack .storage .datastore .db .TemplateDataStoreDao ;
34+ import org .apache .cloudstack .storage .datastore .db .TemplateDataStoreVO ;
3135import org .apache .log4j .Logger ;
3236import org .springframework .stereotype .Component ;
3337
@@ -75,6 +79,8 @@ public class VMTemplateDaoImpl extends GenericDaoBase<VMTemplateVO, Long> implem
7579 DomainDao _domainDao ;
7680 @ Inject
7781 DataCenterDao _dcDao ;
82+ @ Inject
83+ TemplateDataStoreDao _templateDataStoreDao ;
7884
7985 private static final String SELECT_S3_CANDIDATE_TEMPLATES = "SELECT t.id, t.unique_name, t.name, t.public, t.featured, "
8086 + "t.type, t.hvm, t.bits, t.url, t.format, t.created, t.account_id, t.checksum, t.display_text, "
@@ -87,6 +93,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase<VMTemplateVO, Long> implem
8793 protected SearchBuilder <VMTemplateVO > UniqueNameSearch ;
8894 protected SearchBuilder <VMTemplateVO > tmpltTypeSearch ;
8995 protected SearchBuilder <VMTemplateVO > tmpltTypeHyperSearch ;
96+ protected SearchBuilder <VMTemplateVO > readySystemTemplateSearch ;
9097 protected SearchBuilder <VMTemplateVO > tmpltTypeHyperSearch2 ;
9198
9299 protected SearchBuilder <VMTemplateVO > AccountIdSearch ;
@@ -326,6 +333,23 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
326333 hostHyperSearch .done ();
327334 tmpltTypeHyperSearch .done ();
328335
336+ readySystemTemplateSearch = createSearchBuilder ();
337+ readySystemTemplateSearch .and ("templateType" , readySystemTemplateSearch .entity ().getTemplateType (), SearchCriteria .Op .EQ );
338+ SearchBuilder <TemplateDataStoreVO > templateDownloadSearch = _templateDataStoreDao .createSearchBuilder ();
339+ templateDownloadSearch .and ("downloadState" , templateDownloadSearch .entity ().getDownloadState (), SearchCriteria .Op .EQ );
340+ readySystemTemplateSearch .join ("vmTemplateJoinTemplateStoreRef" , templateDownloadSearch , templateDownloadSearch .entity ().getTemplateId (),
341+ readySystemTemplateSearch .entity ().getId (), JoinBuilder .JoinType .INNER );
342+ SearchBuilder <HostVO > hostHyperSearch2 = _hostDao .createSearchBuilder ();
343+ hostHyperSearch2 .and ("type" , hostHyperSearch2 .entity ().getType (), SearchCriteria .Op .EQ );
344+ hostHyperSearch2 .and ("zoneId" , hostHyperSearch2 .entity ().getDataCenterId (), SearchCriteria .Op .EQ );
345+ hostHyperSearch2 .and ("removed" , hostHyperSearch2 .entity ().getRemoved (), SearchCriteria .Op .NULL );
346+ hostHyperSearch2 .groupBy (hostHyperSearch2 .entity ().getHypervisorType ());
347+
348+ readySystemTemplateSearch .join ("tmplHyper" , hostHyperSearch2 , hostHyperSearch2 .entity ().getHypervisorType (),
349+ readySystemTemplateSearch .entity ().getHypervisorType (), JoinBuilder .JoinType .INNER );
350+ hostHyperSearch2 .done ();
351+ readySystemTemplateSearch .done ();
352+
329353 tmpltTypeHyperSearch2 = createSearchBuilder ();
330354 tmpltTypeHyperSearch2 .and ("templateType" , tmpltTypeHyperSearch2 .entity ().getTemplateType (),
331355 SearchCriteria .Op .EQ );
@@ -764,22 +788,27 @@ public VMTemplateVO findSystemVMTemplate(long zoneId) {
764788 }
765789
766790 @ Override
767- public VMTemplateVO findSystemVMTemplate (long zoneId , HypervisorType hType ) {
768- SearchCriteria <VMTemplateVO > sc = tmpltTypeHyperSearch .create ();
791+ public VMTemplateVO findSystemVMReadyTemplate (long zoneId , HypervisorType hypervisorType ) {
792+ SearchCriteria <VMTemplateVO > sc = readySystemTemplateSearch .create ();
769793 sc .setParameters ("templateType" , Storage .TemplateType .SYSTEM );
770794 sc .setJoinParameters ("tmplHyper" , "type" , Host .Type .Routing );
771795 sc .setJoinParameters ("tmplHyper" , "zoneId" , zoneId );
796+ sc .setJoinParameters ("vmTemplateJoinTemplateStoreRef" , "downloadState" , VMTemplateStorageResourceAssoc .Status .DOWNLOADED );
772797
773798 // order by descending order of id
774799 List <VMTemplateVO > tmplts = listBy (sc , new Filter (VMTemplateVO .class , "id" , false , null , null ));
775800
776- for (VMTemplateVO tmplt : tmplts ) {
777- if (tmplt .getHypervisorType () == hType ) {
778- return tmplt ;
801+ if (tmplts .size () > 0 ) {
802+ if (hypervisorType == HypervisorType .Any ) {
803+ Collections .shuffle (tmplts );
804+ return tmplts .get (0 );
779805 }
780- }
781- if (tmplts .size () > 0 && hType == HypervisorType .Any ) {
782- return tmplts .get (0 );
806+ for (VMTemplateVO tmplt : tmplts ) {
807+ if (tmplt .getHypervisorType () == hypervisorType ) {
808+ return tmplt ;
809+ }
810+ }
811+
783812 }
784813 return null ;
785814 }
0 commit comments