@Test(expected = CloudRuntimeException.class) public void testPrepareTemplateNoHostConnectedToPool() { final VMTemplateVO mockTemplate = mock(VMTemplateVO.class); when(mockTemplate.getId()).thenReturn(202l); final StoragePoolVO mockPool = mock(StoragePoolVO.class); when(mockPool.getId()).thenReturn(2l); final PrimaryDataStore mockPrimaryDataStore = mock(PrimaryDataStore.class); when(mockPrimaryDataStore.getId()).thenReturn(2l); when(mockPrimaryDataStore.getDataCenterId()).thenReturn(1l); final TemplateDataStoreVO mockTemplateDataStore = mock(TemplateDataStoreVO.class); when(dataStoreManager.getPrimaryDataStore(anyLong())).thenReturn(mockPrimaryDataStore); when(vmTemplateDao.findById(anyLong(), anyBoolean())).thenReturn(mockTemplate); when(vmTemplatePoolDao.findByPoolTemplate(anyLong(), anyLong())).thenReturn(null); when(templateDataStoreDao.findByTemplateZoneDownloadStatus(202l, 1l, VMTemplateStatus.DOWNLOADED)).thenReturn(mockTemplateDataStore); when(storagePoolHostDao.listByHostStatus(2l, HostStatus.Up)).thenReturn(null); templateManager.prepareTemplateForCreate(mockTemplate, (StoragePool) mockPrimaryDataStore); }
@Override @DB public VMTemplateStoragePoolVO prepareTemplateForCreate(final VMTemplateVO templ, final StoragePool pool) { final VMTemplateVO template = this._tmpltDao.findById(templ.getId(), true); final long poolId = pool.getId(); final long templateId = template.getId(); final VMTemplateStoragePoolVO templateStoragePoolRef; final TemplateDataStoreVO templateStoreRef; templateStoragePoolRef = this._tmpltPoolDao.findByPoolTemplate(poolId, templateId); if (templateStoragePoolRef != null) { templateStoragePoolRef.setMarkedForGC(false); this._tmpltPoolDao.update(templateStoragePoolRef.getId(), templateStoragePoolRef); if (templateStoragePoolRef.getDownloadState() == VMTemplateStatus.DOWNLOADED) { if (s_logger.isDebugEnabled()) { s_logger.debug("Template " + templateId + " has already been downloaded to pool " + poolId); } return templateStoragePoolRef; } } templateStoreRef = this._tmplStoreDao.findByTemplateZoneDownloadStatus(templateId, pool.getDataCenterId(), VMTemplateStatus.DOWNLOADED); if (templateStoreRef == null) { s_logger.error("Unable to find a secondary storage host who has completely downloaded the template."); return null; } final List<StoragePoolHostVO> vos = this._poolHostDao.listByHostStatus(poolId, HostStatus.Up); if (vos == null || vos.isEmpty()) { throw new CloudRuntimeException("Cannot download " + templateId + " to poolId " + poolId + " since there is no host in the Up state connected to this pool"); } if (templateStoragePoolRef == null) { if (s_logger.isDebugEnabled()) { s_logger.debug("Downloading template " + templateId + " to pool " + poolId); } final DataStore srcSecStore = this._dataStoreMgr.getDataStore(templateStoreRef.getDataStoreId(), DataStoreRole.Image); final TemplateInfo srcTemplate = this._tmplFactory.getTemplate(templateId, srcSecStore); final AsyncCallFuture<TemplateApiResult> future = this._tmpltSvr.prepareTemplateOnPrimary(srcTemplate, pool); try { final TemplateApiResult result = future.get(); if (result.isFailed()) { s_logger.debug("prepare template failed:" + result.getResult()); return null; } return this._tmpltPoolDao.findByPoolTemplate(poolId, templateId); } catch (final Exception ex) { s_logger.debug("failed to copy template from image store:" + srcSecStore.getName() + " to primary storage"); } } return null; }
TemplateManagerImpl extends ManagerBase implements TemplateManager, TemplateApiService, Configurable { @Override @DB public VMTemplateStoragePoolVO prepareTemplateForCreate(final VMTemplateVO templ, final StoragePool pool) { final VMTemplateVO template = this._tmpltDao.findById(templ.getId(), true); final long poolId = pool.getId(); final long templateId = template.getId(); final VMTemplateStoragePoolVO templateStoragePoolRef; final TemplateDataStoreVO templateStoreRef; templateStoragePoolRef = this._tmpltPoolDao.findByPoolTemplate(poolId, templateId); if (templateStoragePoolRef != null) { templateStoragePoolRef.setMarkedForGC(false); this._tmpltPoolDao.update(templateStoragePoolRef.getId(), templateStoragePoolRef); if (templateStoragePoolRef.getDownloadState() == VMTemplateStatus.DOWNLOADED) { if (s_logger.isDebugEnabled()) { s_logger.debug("Template " + templateId + " has already been downloaded to pool " + poolId); } return templateStoragePoolRef; } } templateStoreRef = this._tmplStoreDao.findByTemplateZoneDownloadStatus(templateId, pool.getDataCenterId(), VMTemplateStatus.DOWNLOADED); if (templateStoreRef == null) { s_logger.error("Unable to find a secondary storage host who has completely downloaded the template."); return null; } final List<StoragePoolHostVO> vos = this._poolHostDao.listByHostStatus(poolId, HostStatus.Up); if (vos == null || vos.isEmpty()) { throw new CloudRuntimeException("Cannot download " + templateId + " to poolId " + poolId + " since there is no host in the Up state connected to this pool"); } if (templateStoragePoolRef == null) { if (s_logger.isDebugEnabled()) { s_logger.debug("Downloading template " + templateId + " to pool " + poolId); } final DataStore srcSecStore = this._dataStoreMgr.getDataStore(templateStoreRef.getDataStoreId(), DataStoreRole.Image); final TemplateInfo srcTemplate = this._tmplFactory.getTemplate(templateId, srcSecStore); final AsyncCallFuture<TemplateApiResult> future = this._tmpltSvr.prepareTemplateOnPrimary(srcTemplate, pool); try { final TemplateApiResult result = future.get(); if (result.isFailed()) { s_logger.debug("prepare template failed:" + result.getResult()); return null; } return this._tmpltPoolDao.findByPoolTemplate(poolId, templateId); } catch (final Exception ex) { s_logger.debug("failed to copy template from image store:" + srcSecStore.getName() + " to primary storage"); } } return null; } }
TemplateManagerImpl extends ManagerBase implements TemplateManager, TemplateApiService, Configurable { @Override @DB public VMTemplateStoragePoolVO prepareTemplateForCreate(final VMTemplateVO templ, final StoragePool pool) { final VMTemplateVO template = this._tmpltDao.findById(templ.getId(), true); final long poolId = pool.getId(); final long templateId = template.getId(); final VMTemplateStoragePoolVO templateStoragePoolRef; final TemplateDataStoreVO templateStoreRef; templateStoragePoolRef = this._tmpltPoolDao.findByPoolTemplate(poolId, templateId); if (templateStoragePoolRef != null) { templateStoragePoolRef.setMarkedForGC(false); this._tmpltPoolDao.update(templateStoragePoolRef.getId(), templateStoragePoolRef); if (templateStoragePoolRef.getDownloadState() == VMTemplateStatus.DOWNLOADED) { if (s_logger.isDebugEnabled()) { s_logger.debug("Template " + templateId + " has already been downloaded to pool " + poolId); } return templateStoragePoolRef; } } templateStoreRef = this._tmplStoreDao.findByTemplateZoneDownloadStatus(templateId, pool.getDataCenterId(), VMTemplateStatus.DOWNLOADED); if (templateStoreRef == null) { s_logger.error("Unable to find a secondary storage host who has completely downloaded the template."); return null; } final List<StoragePoolHostVO> vos = this._poolHostDao.listByHostStatus(poolId, HostStatus.Up); if (vos == null || vos.isEmpty()) { throw new CloudRuntimeException("Cannot download " + templateId + " to poolId " + poolId + " since there is no host in the Up state connected to this pool"); } if (templateStoragePoolRef == null) { if (s_logger.isDebugEnabled()) { s_logger.debug("Downloading template " + templateId + " to pool " + poolId); } final DataStore srcSecStore = this._dataStoreMgr.getDataStore(templateStoreRef.getDataStoreId(), DataStoreRole.Image); final TemplateInfo srcTemplate = this._tmplFactory.getTemplate(templateId, srcSecStore); final AsyncCallFuture<TemplateApiResult> future = this._tmpltSvr.prepareTemplateOnPrimary(srcTemplate, pool); try { final TemplateApiResult result = future.get(); if (result.isFailed()) { s_logger.debug("prepare template failed:" + result.getResult()); return null; } return this._tmpltPoolDao.findByPoolTemplate(poolId, templateId); } catch (final Exception ex) { s_logger.debug("failed to copy template from image store:" + srcSecStore.getName() + " to primary storage"); } } return null; } protected  TemplateManagerImpl(); }
TemplateManagerImpl extends ManagerBase implements TemplateManager, TemplateApiService, Configurable { @Override @DB public VMTemplateStoragePoolVO prepareTemplateForCreate(final VMTemplateVO templ, final StoragePool pool) { final VMTemplateVO template = this._tmpltDao.findById(templ.getId(), true); final long poolId = pool.getId(); final long templateId = template.getId(); final VMTemplateStoragePoolVO templateStoragePoolRef; final TemplateDataStoreVO templateStoreRef; templateStoragePoolRef = this._tmpltPoolDao.findByPoolTemplate(poolId, templateId); if (templateStoragePoolRef != null) { templateStoragePoolRef.setMarkedForGC(false); this._tmpltPoolDao.update(templateStoragePoolRef.getId(), templateStoragePoolRef); if (templateStoragePoolRef.getDownloadState() == VMTemplateStatus.DOWNLOADED) { if (s_logger.isDebugEnabled()) { s_logger.debug("Template " + templateId + " has already been downloaded to pool " + poolId); } return templateStoragePoolRef; } } templateStoreRef = this._tmplStoreDao.findByTemplateZoneDownloadStatus(templateId, pool.getDataCenterId(), VMTemplateStatus.DOWNLOADED); if (templateStoreRef == null) { s_logger.error("Unable to find a secondary storage host who has completely downloaded the template."); return null; } final List<StoragePoolHostVO> vos = this._poolHostDao.listByHostStatus(poolId, HostStatus.Up); if (vos == null || vos.isEmpty()) { throw new CloudRuntimeException("Cannot download " + templateId + " to poolId " + poolId + " since there is no host in the Up state connected to this pool"); } if (templateStoragePoolRef == null) { if (s_logger.isDebugEnabled()) { s_logger.debug("Downloading template " + templateId + " to pool " + poolId); } final DataStore srcSecStore = this._dataStoreMgr.getDataStore(templateStoreRef.getDataStoreId(), DataStoreRole.Image); final TemplateInfo srcTemplate = this._tmplFactory.getTemplate(templateId, srcSecStore); final AsyncCallFuture<TemplateApiResult> future = this._tmpltSvr.prepareTemplateOnPrimary(srcTemplate, pool); try { final TemplateApiResult result = future.get(); if (result.isFailed()) { s_logger.debug("prepare template failed:" + result.getResult()); return null; } return this._tmpltPoolDao.findByPoolTemplate(poolId, templateId); } catch (final Exception ex) { s_logger.debug("failed to copy template from image store:" + srcSecStore.getName() + " to primary storage"); } } return null; } protected  TemplateManagerImpl(); @Override @ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_CREATE, eventDescription = "creating template") VirtualMachineTemplate registerTemplate(final RegisterTemplateCmd cmd); @Override @ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_CREATE, eventDescription = "creating post upload template") GetUploadParamsResponse registerTemplateForPostUpload(final GetUploadParamsForTemplateCmd cmd); @Override @ActionEvent(eventType = EventTypes.EVENT_ISO_CREATE, eventDescription = "creating iso") VirtualMachineTemplate registerIso(final RegisterIsoCmd cmd); @Override @ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_COPY, eventDescription = "copying template", async = true) VirtualMachineTemplate copyTemplate(final CopyTemplateCmd cmd); @Override VirtualMachineTemplate prepareTemplate(final long templateId, final long zoneId, final Long storageId); void prepareTemplateInAllStoragePools(final VMTemplateVO template, final long zoneId); @Override @DB VMTemplateStoragePoolVO prepareTemplateForCreate(final VMTemplateVO templ, final StoragePool pool); @Override @DB boolean resetTemplateDownloadStateOnPool(final long templateStoragePoolRefId); @Override @DB boolean copy(final long userId, final VMTemplateVO template, final DataStore srcSecStore, final DataCenterVO dstZone); @Override boolean delete(final long userId, final long templateId, final Long zoneId); @Override List<VMTemplateStoragePoolVO> getUnusedTemplatesInPool(final StoragePoolVO pool); @Override @DB void evictTemplateFromStoragePool(final VMTemplateStoragePoolVO templatePoolVO); @Override boolean templateIsDeleteable(final VMTemplateHostVO templateHostRef); @Override boolean templateIsDeleteable(final long templateId); @Override Pair<String, String> getAbsoluteIsoPath(final long templateId, final long dataCenterId); @Override String getSecondaryStorageURL(final long zoneId); @Override DataStore getImageStore(final long zoneId, final long tmpltId); @Override DataStore getImageStore(final long tmpltId); @Override Long getTemplateSize(final long templateId, final long zoneId); @Override DataStore getImageStore(final String storeUuid, final Long zoneId); @Override String getChecksum(final DataStore store, final String templatePath); @Override List<DataStore> getImageStoreByTemplate(final long templateId, final Long zoneId); @Override TemplateInfo prepareIso(final long isoId, final long dcId); @Override void prepareIsoForVmProfile(final VirtualMachineProfile profile); @Override @ActionEvent(eventType = EventTypes.EVENT_ISO_DETACH, eventDescription = "detaching ISO", async = true) boolean detachIso(final long vmId); @Override @ActionEvent(eventType = EventTypes.EVENT_ISO_ATTACH, eventDescription = "attaching ISO", async = true) boolean attachIso(final long isoId, final long vmId); @Override @ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_DELETE, eventDescription = "deleting template", async = true) boolean deleteTemplate(final DeleteTemplateCmd cmd); @Override @ActionEvent(eventType = EventTypes.EVENT_ISO_DELETE, eventDescription = "deleting iso", async = true) boolean deleteIso(final DeleteIsoCmd cmd); @Override @ActionEvent(eventType = EventTypes.EVENT_ISO_EXTRACT, eventDescription = "extracting ISO", async = true) String extract(final ExtractIsoCmd cmd); @Override @ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_EXTRACT, eventDescription = "extracting template", async = true) String extract(final ExtractTemplateCmd cmd); @Override List<String> listTemplatePermissions(final BaseListTemplateOrIsoPermissionsCmd cmd); @DB @Override boolean updateTemplateOrIsoPermissions(final BaseUpdateTemplateOrIsoPermissionsCmd cmd); @Override @ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_CREATE, eventDescription = "creating template", create = true) VMTemplateVO createPrivateTemplateRecord(final CreateTemplateCmd cmd, final Account templateOwner); @Override @DB @ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_CREATE, eventDescription = "creating template", async = true) VirtualMachineTemplate createPrivateTemplate(final CreateTemplateCmd command); @Override VMTemplateVO updateTemplate(final UpdateIsoCmd cmd); @Override VMTemplateVO updateTemplate(final UpdateTemplateCmd cmd); @Override boolean configure(final String name, final Map<String, Object> params); @Override boolean start(); @Override boolean stop(); @Override String getConfigComponentName(); @Override ConfigKey<?>[] getConfigKeys(); @Inject void setTemplateAdapters(final List<TemplateAdapter> adapters); }
TemplateManagerImpl extends ManagerBase implements TemplateManager, TemplateApiService, Configurable { @Override @DB public VMTemplateStoragePoolVO prepareTemplateForCreate(final VMTemplateVO templ, final StoragePool pool) { final VMTemplateVO template = this._tmpltDao.findById(templ.getId(), true); final long poolId = pool.getId(); final long templateId = template.getId(); final VMTemplateStoragePoolVO templateStoragePoolRef; final TemplateDataStoreVO templateStoreRef; templateStoragePoolRef = this._tmpltPoolDao.findByPoolTemplate(poolId, templateId); if (templateStoragePoolRef != null) { templateStoragePoolRef.setMarkedForGC(false); this._tmpltPoolDao.update(templateStoragePoolRef.getId(), templateStoragePoolRef); if (templateStoragePoolRef.getDownloadState() == VMTemplateStatus.DOWNLOADED) { if (s_logger.isDebugEnabled()) { s_logger.debug("Template " + templateId + " has already been downloaded to pool " + poolId); } return templateStoragePoolRef; } } templateStoreRef = this._tmplStoreDao.findByTemplateZoneDownloadStatus(templateId, pool.getDataCenterId(), VMTemplateStatus.DOWNLOADED); if (templateStoreRef == null) { s_logger.error("Unable to find a secondary storage host who has completely downloaded the template."); return null; } final List<StoragePoolHostVO> vos = this._poolHostDao.listByHostStatus(poolId, HostStatus.Up); if (vos == null || vos.isEmpty()) { throw new CloudRuntimeException("Cannot download " + templateId + " to poolId " + poolId + " since there is no host in the Up state connected to this pool"); } if (templateStoragePoolRef == null) { if (s_logger.isDebugEnabled()) { s_logger.debug("Downloading template " + templateId + " to pool " + poolId); } final DataStore srcSecStore = this._dataStoreMgr.getDataStore(templateStoreRef.getDataStoreId(), DataStoreRole.Image); final TemplateInfo srcTemplate = this._tmplFactory.getTemplate(templateId, srcSecStore); final AsyncCallFuture<TemplateApiResult> future = this._tmpltSvr.prepareTemplateOnPrimary(srcTemplate, pool); try { final TemplateApiResult result = future.get(); if (result.isFailed()) { s_logger.debug("prepare template failed:" + result.getResult()); return null; } return this._tmpltPoolDao.findByPoolTemplate(poolId, templateId); } catch (final Exception ex) { s_logger.debug("failed to copy template from image store:" + srcSecStore.getName() + " to primary storage"); } } return null; } protected  TemplateManagerImpl(); @Override @ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_CREATE, eventDescription = "creating template") VirtualMachineTemplate registerTemplate(final RegisterTemplateCmd cmd); @Override @ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_CREATE, eventDescription = "creating post upload template") GetUploadParamsResponse registerTemplateForPostUpload(final GetUploadParamsForTemplateCmd cmd); @Override @ActionEvent(eventType = EventTypes.EVENT_ISO_CREATE, eventDescription = "creating iso") VirtualMachineTemplate registerIso(final RegisterIsoCmd cmd); @Override @ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_COPY, eventDescription = "copying template", async = true) VirtualMachineTemplate copyTemplate(final CopyTemplateCmd cmd); @Override VirtualMachineTemplate prepareTemplate(final long templateId, final long zoneId, final Long storageId); void prepareTemplateInAllStoragePools(final VMTemplateVO template, final long zoneId); @Override @DB VMTemplateStoragePoolVO prepareTemplateForCreate(final VMTemplateVO templ, final StoragePool pool); @Override @DB boolean resetTemplateDownloadStateOnPool(final long templateStoragePoolRefId); @Override @DB boolean copy(final long userId, final VMTemplateVO template, final DataStore srcSecStore, final DataCenterVO dstZone); @Override boolean delete(final long userId, final long templateId, final Long zoneId); @Override List<VMTemplateStoragePoolVO> getUnusedTemplatesInPool(final StoragePoolVO pool); @Override @DB void evictTemplateFromStoragePool(final VMTemplateStoragePoolVO templatePoolVO); @Override boolean templateIsDeleteable(final VMTemplateHostVO templateHostRef); @Override boolean templateIsDeleteable(final long templateId); @Override Pair<String, String> getAbsoluteIsoPath(final long templateId, final long dataCenterId); @Override String getSecondaryStorageURL(final long zoneId); @Override DataStore getImageStore(final long zoneId, final long tmpltId); @Override DataStore getImageStore(final long tmpltId); @Override Long getTemplateSize(final long templateId, final long zoneId); @Override DataStore getImageStore(final String storeUuid, final Long zoneId); @Override String getChecksum(final DataStore store, final String templatePath); @Override List<DataStore> getImageStoreByTemplate(final long templateId, final Long zoneId); @Override TemplateInfo prepareIso(final long isoId, final long dcId); @Override void prepareIsoForVmProfile(final VirtualMachineProfile profile); @Override @ActionEvent(eventType = EventTypes.EVENT_ISO_DETACH, eventDescription = "detaching ISO", async = true) boolean detachIso(final long vmId); @Override @ActionEvent(eventType = EventTypes.EVENT_ISO_ATTACH, eventDescription = "attaching ISO", async = true) boolean attachIso(final long isoId, final long vmId); @Override @ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_DELETE, eventDescription = "deleting template", async = true) boolean deleteTemplate(final DeleteTemplateCmd cmd); @Override @ActionEvent(eventType = EventTypes.EVENT_ISO_DELETE, eventDescription = "deleting iso", async = true) boolean deleteIso(final DeleteIsoCmd cmd); @Override @ActionEvent(eventType = EventTypes.EVENT_ISO_EXTRACT, eventDescription = "extracting ISO", async = true) String extract(final ExtractIsoCmd cmd); @Override @ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_EXTRACT, eventDescription = "extracting template", async = true) String extract(final ExtractTemplateCmd cmd); @Override List<String> listTemplatePermissions(final BaseListTemplateOrIsoPermissionsCmd cmd); @DB @Override boolean updateTemplateOrIsoPermissions(final BaseUpdateTemplateOrIsoPermissionsCmd cmd); @Override @ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_CREATE, eventDescription = "creating template", create = true) VMTemplateVO createPrivateTemplateRecord(final CreateTemplateCmd cmd, final Account templateOwner); @Override @DB @ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_CREATE, eventDescription = "creating template", async = true) VirtualMachineTemplate createPrivateTemplate(final CreateTemplateCmd command); @Override VMTemplateVO updateTemplate(final UpdateIsoCmd cmd); @Override VMTemplateVO updateTemplate(final UpdateTemplateCmd cmd); @Override boolean configure(final String name, final Map<String, Object> params); @Override boolean start(); @Override boolean stop(); @Override String getConfigComponentName(); @Override ConfigKey<?>[] getConfigKeys(); @Inject void setTemplateAdapters(final List<TemplateAdapter> adapters);  }