TemplateManagerImpl extends ManagerBase implements TemplateManager, TemplateApiService, Configurable { @Override public VirtualMachineTemplate prepareTemplate(final long templateId, final long zoneId, final Long storageId) { final VMTemplateVO vmTemplate = _tmpltDao.findById(templateId); if (vmTemplate == null) { throw new InvalidParameterValueException("Unable to find template id=" + templateId); } _accountMgr.checkAccess(CallContext.current().getCallingAccount(), AccessType.OperateEntry, true, vmTemplate); if (storageId != null) { final StoragePoolVO pool = _poolDao.findById(storageId); if (pool != null) { if (pool.getStatus() == StoragePoolStatus.Up && pool.getDataCenterId() == zoneId) { prepareTemplateInOneStoragePool(vmTemplate, pool); } else { s_logger.warn("Skip loading template " + vmTemplate.getId() + " into primary storage " + pool.getId() + " as either the pool zone " + pool.getDataCenterId() + " is different from the requested zone " + zoneId + " or the pool is currently not available."); } } } else { prepareTemplateInAllStoragePools(vmTemplate, zoneId); } return vmTemplate; } protected  TemplateManagerImpl(); @Override @ActionEvent(eventType = EventTypes.EVENT_ISO_CREATE, eventDescription = "creating iso") VirtualMachineTemplate registerIso(final RegisterIsoCmd cmd); @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 DataStore getImageStore(final String storeUuid, final Long zoneId); @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 VirtualMachineTemplate prepareTemplate(final long templateId, final long zoneId, final Long storageId); @Override void prepareIsoForVmProfile(final VirtualMachineProfile profile); void prepareTemplateInAllStoragePools(final VMTemplateVO template, final long zoneId); @Override @DB VMTemplateStoragePoolVO prepareTemplateForCreate(final VMTemplateVO templ, final StoragePool pool); @Override String getChecksum(final DataStore store, final String templatePath); @Override @DB boolean resetTemplateDownloadStateOnPool(final long templateStoragePoolRefId); @Override @DB boolean copy(final long userId, final VMTemplateVO template, final DataStore srcSecStore, final DataCenterVO dstZone); @Override @ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_COPY, eventDescription = "copying template", async = true) VirtualMachineTemplate copyTemplate(final CopyTemplateCmd cmd); @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 start(); @Override boolean stop(); @Override boolean configure(final String name, final Map<String, Object> params); @Override boolean templateIsDeleteable(final VMTemplateHostVO templateHostRef); @Override boolean templateIsDeleteable(final long templateId); @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 TemplateInfo prepareIso(final long isoId, final long dcId); @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 List<String> listTemplatePermissions(final BaseListTemplateOrIsoPermissionsCmd cmd); @DB @Override boolean updateTemplateOrIsoPermissions(final BaseUpdateTemplateOrIsoPermissionsCmd cmd); @Override @DB @ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_CREATE, eventDescription = "creating template", async = true) VirtualMachineTemplate createPrivateTemplate(final CreateTemplateCmd command); @Override @ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_CREATE, eventDescription = "creating template", create = true) VMTemplateVO createPrivateTemplateRecord(final CreateTemplateCmd cmd, final Account templateOwner); @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 List<DataStore> getImageStoreByTemplate(final long templateId, final Long zoneId); @Override VMTemplateVO updateTemplate(final UpdateIsoCmd cmd); @Override VMTemplateVO updateTemplate(final UpdateTemplateCmd cmd); @Override String getConfigComponentName(); @Override ConfigKey<?>[] getConfigKeys(); List<TemplateAdapter> getTemplateAdapters(); @Inject void setTemplateAdapters(final List<TemplateAdapter> adapters);  }
@Test(expected = InvalidParameterValueException.class) public void testPrepareTemplateInvalidTemplate() { when(vmTemplateDao.findById(anyLong())).thenReturn(null); templateManager.prepareTemplate(202, 1, null); }
@Test public void testTemplateScheduledForDownloadInOnePool() { VMTemplateVO mockTemplate = mock(VMTemplateVO.class); StoragePoolVO mockPool = mock(StoragePoolVO.class); PrimaryDataStore mockPrimaryDataStore = mock(PrimaryDataStore.class); VMTemplateStoragePoolVO mockTemplateStore = mock(VMTemplateStoragePoolVO.class); when(mockPrimaryDataStore.getId()).thenReturn(2l); when(mockPool.getId()).thenReturn(2l); when(mockPool.getStatus()).thenReturn(StoragePoolStatus.Up); when(mockPool.getDataCenterId()).thenReturn(1l); when(mockTemplate.getId()).thenReturn(202l); when(mockTemplateStore.getDownloadState()).thenReturn(VMTemplateStorageResourceAssoc.Status.DOWNLOADED); when(vmTemplateDao.findById(anyLong())).thenReturn(mockTemplate); when(dataStoreManager.getPrimaryDataStore(anyLong())).thenReturn(mockPrimaryDataStore); when(vmTemplateDao.findById(anyLong(), anyBoolean())).thenReturn(mockTemplate); when(vmTemplatePoolDao.findByPoolTemplate(anyLong(), anyLong())).thenReturn(mockTemplateStore); when(primaryDataStoreDao.findById(anyLong())).thenReturn(mockPool); doNothing().when(mockTemplateStore).setMarkedForGC(anyBoolean()); ExecutorService preloadExecutor = new CustomThreadPoolExecutor(8, 8, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(), new NamedThreadFactory("Template-Preloader")); templateManager._preloadExecutor = preloadExecutor; templateManager.prepareTemplate(202, 1, 2l); assertTrue("Test template is scheduled for seeding to on pool", ((CustomThreadPoolExecutor)preloadExecutor).getCount() == 1); }
@Test public void testTemplateScheduledForDownloadInDisabledPool() { VMTemplateVO mockTemplate = mock(VMTemplateVO.class); StoragePoolVO mockPool = mock(StoragePoolVO.class); PrimaryDataStore mockPrimaryDataStore = mock(PrimaryDataStore.class); VMTemplateStoragePoolVO mockTemplateStore = mock(VMTemplateStoragePoolVO.class); when(mockPrimaryDataStore.getId()).thenReturn(2l); when(mockPool.getId()).thenReturn(2l); when(mockPool.getStatus()).thenReturn(StoragePoolStatus.Disabled); when(mockPool.getDataCenterId()).thenReturn(1l); when(mockTemplate.getId()).thenReturn(202l); when(mockTemplateStore.getDownloadState()).thenReturn(VMTemplateStorageResourceAssoc.Status.DOWNLOADED); when(vmTemplateDao.findById(anyLong())).thenReturn(mockTemplate); when(dataStoreManager.getPrimaryDataStore(anyLong())).thenReturn(mockPrimaryDataStore); when(vmTemplateDao.findById(anyLong(), anyBoolean())).thenReturn(mockTemplate); when(vmTemplatePoolDao.findByPoolTemplate(anyLong(), anyLong())).thenReturn(mockTemplateStore); when(primaryDataStoreDao.findById(anyLong())).thenReturn(mockPool); doNothing().when(mockTemplateStore).setMarkedForGC(anyBoolean()); ExecutorService preloadExecutor = new CustomThreadPoolExecutor(8, 8, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(), new NamedThreadFactory("Template-Preloader")); templateManager._preloadExecutor = preloadExecutor; templateManager.prepareTemplate(202, 1, 2l); assertTrue("Test template is not scheduled for seeding on disabled pool", ((CustomThreadPoolExecutor)preloadExecutor).getCount() == 0); }
@Test public void testTemplateScheduledForDownloadInMultiplePool() { VMTemplateVO mockTemplate = mock(VMTemplateVO.class); PrimaryDataStore mockPrimaryDataStore = mock(PrimaryDataStore.class); VMTemplateStoragePoolVO mockTemplateStore = mock(VMTemplateStoragePoolVO.class); List<StoragePoolVO> pools = new ArrayList<StoragePoolVO>(); StoragePoolVO mockPool1 = mock(StoragePoolVO.class); when(mockPool1.getId()).thenReturn(2l); when(mockPool1.getStatus()).thenReturn(StoragePoolStatus.Up); when(mockPool1.getDataCenterId()).thenReturn(1l); StoragePoolVO mockPool2 = mock(StoragePoolVO.class); when(mockPool2.getId()).thenReturn(3l); when(mockPool2.getStatus()).thenReturn(StoragePoolStatus.Up); when(mockPool2.getDataCenterId()).thenReturn(1l); StoragePoolVO mockPool3 = mock(StoragePoolVO.class); when(mockPool3.getId()).thenReturn(4l); when(mockPool3.getStatus()).thenReturn(StoragePoolStatus.Up); when(mockPool3.getDataCenterId()).thenReturn(2l); pools.add(mockPool1); pools.add(mockPool2); pools.add(mockPool3); when(mockPrimaryDataStore.getId()).thenReturn(2l); when(mockTemplate.getId()).thenReturn(202l); when(mockTemplateStore.getDownloadState()).thenReturn(VMTemplateStorageResourceAssoc.Status.DOWNLOADED); when(vmTemplateDao.findById(anyLong())).thenReturn(mockTemplate); when(dataStoreManager.getPrimaryDataStore(anyLong())).thenReturn(mockPrimaryDataStore); when(vmTemplateDao.findById(anyLong(), anyBoolean())).thenReturn(mockTemplate); when(vmTemplatePoolDao.findByPoolTemplate(anyLong(), anyLong())).thenReturn(mockTemplateStore); when(primaryDataStoreDao.findById(2l)).thenReturn(mockPool1); when(primaryDataStoreDao.findById(3l)).thenReturn(mockPool2); when(primaryDataStoreDao.findById(4l)).thenReturn(mockPool3); when(primaryDataStoreDao.listByStatus(StoragePoolStatus.Up)).thenReturn(pools); doNothing().when(mockTemplateStore).setMarkedForGC(anyBoolean()); ExecutorService preloadExecutor = new CustomThreadPoolExecutor(8, 8, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(), new NamedThreadFactory("Template-Preloader")); templateManager._preloadExecutor = preloadExecutor; templateManager.prepareTemplate(202, 1, null); assertTrue("Test template is scheduled for seeding to on pool", ((CustomThreadPoolExecutor) preloadExecutor).getCount() == 2); }