diff --git a/server/src/com/cloud/storage/dao/VMTemplateDaoImpl.java b/server/src/com/cloud/storage/dao/VMTemplateDaoImpl.java index 7e49832104e..36d80df01e3 100755 --- a/server/src/com/cloud/storage/dao/VMTemplateDaoImpl.java +++ b/server/src/com/cloud/storage/dao/VMTemplateDaoImpl.java @@ -37,6 +37,7 @@ import com.cloud.domain.DomainVO; import com.cloud.host.Host; import com.cloud.host.HostVO; import com.cloud.host.dao.HostDao; +import com.cloud.hypervisor.Hypervisor; import com.cloud.hypervisor.Hypervisor.HypervisorType; import com.cloud.storage.Storage; import com.cloud.storage.Storage.TemplateType; @@ -75,7 +76,9 @@ public class VMTemplateDaoImpl extends GenericDaoBase implem protected SearchBuilder TemplateNameSearch; protected SearchBuilder UniqueNameSearch; protected SearchBuilder tmpltTypeSearch; - protected SearchBuilder tmpltTypeHyperSearch; + protected SearchBuilder tmpltTypeHyperSearch; + protected SearchBuilder tmpltTypeHyperSearch2; + protected SearchBuilder AccountIdSearch; protected SearchBuilder NameSearch; protected SearchBuilder TmpltsInZoneSearch; @@ -221,6 +224,11 @@ public class VMTemplateDaoImpl extends GenericDaoBase implem hostHyperSearch.done(); tmpltTypeHyperSearch.done(); + tmpltTypeHyperSearch2 = createSearchBuilder(); + tmpltTypeHyperSearch2.and("templateType", tmpltTypeHyperSearch2.entity().getTemplateType(), SearchCriteria.Op.EQ); + tmpltTypeHyperSearch2.and("hypervisorType", tmpltTypeHyperSearch2.entity().getHypervisorType(), SearchCriteria.Op.EQ); + + tmpltTypeSearch = createSearchBuilder(); tmpltTypeSearch.and("templateType", tmpltTypeSearch.entity().getTemplateType(), SearchCriteria.Op.EQ); @@ -443,6 +451,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase implem sc.setJoinParameters("tmplHyper", "type", Host.Type.Routing); sc.setJoinParameters("tmplHyper", "zoneId", zoneId); + //order by descending order of id and select the first (this is going to be the latest) List tmplts = listBy(sc, new Filter(VMTemplateVO.class, "id", false, null, 1l)); if (tmplts.size() > 0) { @@ -452,14 +461,36 @@ public class VMTemplateDaoImpl extends GenericDaoBase implem } } - @Override - public VMTemplateVO findRoutingTemplate(HypervisorType type) { - List templates = listAllSystemVMTemplates(); - for (VMTemplateVO template : templates) { - if (template.getHypervisorType() == type) { - return template; + public VMTemplateVO findSystemVMTemplate(long zoneId, HypervisorType hType) { + SearchCriteria sc = tmpltTypeHyperSearch.create(); + sc.setParameters("templateType", Storage.TemplateType.SYSTEM); + sc.setJoinParameters("tmplHyper", "type", Host.Type.Routing); + sc.setJoinParameters("tmplHyper", "zoneId", zoneId); + + //order by descending order of id + List tmplts = listBy(sc, new Filter(VMTemplateVO.class, "id", false, null, null)); + + for (VMTemplateVO tmplt: tmplts) { + if (tmplt.getHypervisorType() == hType) { + return tmplt; } } return null; + } + + @Override + public VMTemplateVO findRoutingTemplate(HypervisorType hType) { + SearchCriteria sc = tmpltTypeHyperSearch2.create(); + sc.setParameters("templateType", Storage.TemplateType.SYSTEM); + sc.setParameters("hypervisorType", hType); + + //order by descending order of id and select the first (this is going to be the latest) + List tmplts = listBy(sc, new Filter(VMTemplateVO.class, "id", false, null, 1l)); + + if (tmplts.size() > 0) { + return tmplts.get(0); + } else { + return null; + } } }