cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mc...@apache.org
Subject [1/2] First draft of register template using image store.
Date Thu, 11 Apr 2013 01:03:16 GMT
Updated Branches:
  refs/heads/object_store 3897590bb -> 02686583c


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/02686583/server/src/com/cloud/storage/download/DownloadMonitorImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/storage/download/DownloadMonitorImpl.java b/server/src/com/cloud/storage/download/DownloadMonitorImpl.java
index 7ed6f3f..70254de 100755
--- a/server/src/com/cloud/storage/download/DownloadMonitorImpl.java
+++ b/server/src/com/cloud/storage/download/DownloadMonitorImpl.java
@@ -30,6 +30,12 @@ import java.util.concurrent.ConcurrentHashMap;
 import javax.ejb.Local;
 import javax.inject.Inject;
 
+import org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult;
+import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
+import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager;
+import org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope;
+import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreDao;
+import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO;
 import org.apache.log4j.Logger;
 import org.springframework.stereotype.Component;
 
@@ -118,15 +124,20 @@ import com.cloud.vm.dao.UserVmDao;
 
 import edu.emory.mathcs.backport.java.util.Collections;
 
+import org.apache.cloudstack.engine.subsystem.api.storage.ObjectInDataStoreStateMachine;
+import org.apache.cloudstack.framework.async.AsyncCompletionCallback;
+
 
 @Component
 @Local(value={DownloadMonitor.class})
 public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor {
     static final Logger s_logger = Logger.getLogger(DownloadMonitorImpl.class);
-	
-    @Inject 
+
+    @Inject
     VMTemplateHostDao _vmTemplateHostDao;
-    @Inject 
+    @Inject
+    TemplateDataStoreDao _vmTemplateStoreDao;
+    @Inject
     VMTemplateZoneDao _vmTemplateZoneDao;
     @Inject
 	VMTemplatePoolDao _vmTemplatePoolDao;
@@ -148,7 +159,7 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
     SecondaryStorageVmManager _ssvmMgr;
     @Inject
     StorageManager _storageMgr ;
-    
+
     @Inject
     private final DataCenterDao _dcDao = null;
     @Inject
@@ -163,10 +174,10 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
 
     @Inject TemplateManager templateMgr;
 
-    
-    @Inject 
+
+    @Inject
     private UsageEventDao _usageEventDao;
-    
+
 
     @Inject
     private ClusterDao _clusterDao;
@@ -186,10 +197,13 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
 	private Boolean _sslCopy = new Boolean(false);
 	private String _copyAuthPasswd;
 	private String _proxy = null;
-    protected SearchBuilder<VMTemplateHostVO> ReadyTemplateStatesSearch;
+    protected SearchBuilder<TemplateDataStoreVO> ReadyTemplateStatesSearch;
 
 	Timer _timer;
 
+    @Inject DataStoreManager storeMgr;
+
+    final Map<TemplateDataStoreVO, DownloadListener> _listenerTemplateMap = new ConcurrentHashMap<TemplateDataStoreVO, DownloadListener>();
 	final Map<VMTemplateHostVO, DownloadListener> _listenerMap = new ConcurrentHashMap<VMTemplateHostVO, DownloadListener>();
 	final Map<VolumeHostVO, DownloadListener> _listenerVolumeMap = new ConcurrentHashMap<VolumeHostVO, DownloadListener>();
 
@@ -203,20 +217,20 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
         final Map<String, String> configs = _configDao.getConfiguration("ManagementServer", params);
         _sslCopy = Boolean.parseBoolean(configs.get("secstorage.encrypt.copy"));
         _proxy = configs.get(Config.SecStorageProxy.key());
-        
+
         String cert = configs.get("secstorage.ssl.cert.domain");
         if (!"realhostip.com".equalsIgnoreCase(cert)) {
         	s_logger.warn("Only realhostip.com ssl cert is supported, ignoring self-signed and other certs");
         }
-        
+
         _copyAuthPasswd = configs.get("secstorage.copy.password");
-        
+
         _agentMgr.registerForHostEvents(new DownloadListener(this), true, false, false);
-        
-        ReadyTemplateStatesSearch = _vmTemplateHostDao.createSearchBuilder();
-        ReadyTemplateStatesSearch.and("download_state", ReadyTemplateStatesSearch.entity().getDownloadState(), SearchCriteria.Op.EQ);
+
+        ReadyTemplateStatesSearch = _vmTemplateStoreDao.createSearchBuilder();
+        ReadyTemplateStatesSearch.and("state", ReadyTemplateStatesSearch.entity().getState(), SearchCriteria.Op.EQ);
         ReadyTemplateStatesSearch.and("destroyed", ReadyTemplateStatesSearch.entity().getDestroyed(), SearchCriteria.Op.EQ);
-        ReadyTemplateStatesSearch.and("host_id", ReadyTemplateStatesSearch.entity().getHostId(), SearchCriteria.Op.EQ);
+        ReadyTemplateStatesSearch.and("store_id", ReadyTemplateStatesSearch.entity().getDataStoreId(), SearchCriteria.Op.EQ);
 
         SearchBuilder<VMTemplateVO> TemplatesWithNoChecksumSearch = _templateDao.createSearchBuilder();
         TemplatesWithNoChecksumSearch.and("checksum", TemplatesWithNoChecksumSearch.entity().getChecksum(), SearchCriteria.Op.NULL);
@@ -225,7 +239,7 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
                 ReadyTemplateStatesSearch.entity().getTemplateId(), JoinBuilder.JoinType.INNER);
         TemplatesWithNoChecksumSearch.done();
         ReadyTemplateStatesSearch.done();
-               
+
 		return true;
 	}
 
@@ -239,13 +253,13 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
 	public boolean stop() {
 		return true;
 	}
-	
-	public boolean isTemplateUpdateable(Long templateId, Long hostId) {
-		List<VMTemplateHostVO> downloadsInProgress =
-			_vmTemplateHostDao.listByTemplateHostStatus(templateId.longValue(), hostId.longValue(), VMTemplateHostVO.Status.DOWNLOAD_IN_PROGRESS, VMTemplateHostVO.Status.DOWNLOADED);
+
+	public boolean isTemplateUpdateable(Long templateId, Long storeId) {
+		List<TemplateDataStoreVO> downloadsInProgress =
+			_vmTemplateStoreDao.listByTemplateStoreStatus(templateId, storeId, ObjectInDataStoreStateMachine.State.Creating, ObjectInDataStoreStateMachine.State.Creating2, ObjectInDataStoreStateMachine.State.Ready);
 		return (downloadsInProgress.size() == 0);
 	}
-	
+
 	@Override
     public boolean copyTemplate(VMTemplateVO template, HostVO sourceServer, HostVO destServer) throws StorageUnavailableException{
 
@@ -278,12 +292,12 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
         	throw new CloudRuntimeException("Cant copy the template as the template's size " +srcTmpltHost.getSize()+
         			" is greater than max.template.iso.size " + maxTemplateSizeInBytes);
         }
-        
+
 		if(destTmpltHost != null) {
 		    start();
             String sourceChecksum = this.templateMgr.getChecksum(srcTmpltHost.getHostId(), srcTmpltHost.getInstallPath());
-			DownloadCommand dcmd =  
-              new DownloadCommand(destServer.getStorageUrl(), url, template, TemplateConstants.DEFAULT_HTTP_AUTH_USER, _copyAuthPasswd, maxTemplateSizeInBytes); 
+			DownloadCommand dcmd =
+              new DownloadCommand(destServer.getStorageUrl(), url, template, TemplateConstants.DEFAULT_HTTP_AUTH_USER, _copyAuthPasswd, maxTemplateSizeInBytes);
 			dcmd.setProxy(getHttpProxy());
 			if (downloadJobExists) {
 				dcmd = new DownloadProgressCommand(dcmd, destTmpltHost.getJobId(), RequestType.GET_OR_RESTART);
@@ -305,7 +319,7 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
 			if( old != null ) {
 			    old.abandon();
 			}
-			
+
             try {
 	            send(ssAhost.getId(), dcmd, dl);
 	            return true;
@@ -316,10 +330,10 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
 	            e.printStackTrace();
             }
 		}
-		
+
 		return false;
 	}
-	
+
 	private String generateCopyUrl(String ipAddress, String dir, String path){
 		String hostname = ipAddress;
 		String scheme = "http";
@@ -328,9 +342,9 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
 			hostname = hostname + ".realhostip.com";
 			scheme = "https";
 		}
-		return scheme + "://" + hostname + "/copy/SecStorage/" + dir + "/" + path; 
+		return scheme + "://" + hostname + "/copy/SecStorage/" + dir + "/" + path;
 	}
-	
+
 	private String generateCopyUrl(HostVO sourceServer, VMTemplateHostVO srcTmpltHost) {
 		List<SecondaryStorageVmVO> ssVms = _secStorageVmDao.getSecStorageVmListInStates(SecondaryStorageVm.Role.templateProcessor, sourceServer.getDataCenterId(), State.Running);
 		if (ssVms.size() > 0) {
@@ -341,7 +355,7 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
 			}
 			return generateCopyUrl(ssVm.getPublicIpAddress(), sourceServer.getParent(), srcTmpltHost.getInstallPath());
 		}
-		
+
 		VMTemplateVO tmplt = _templateDao.findById(srcTmpltHost.getTemplateId());
 		HypervisorType hyperType = tmplt.getHypervisorType();
 		/*No secondary storage vm yet*/
@@ -351,43 +365,43 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
 		return null;
 	}
 
-	private void downloadTemplateToStorage(VMTemplateVO template, HostVO sserver) {
+	private void initiateTemplateDownload(VMTemplateVO template, DataStore store, AsyncCompletionCallback<CreateCmdResult> callback) {
 		boolean downloadJobExists = false;
-        VMTemplateHostVO vmTemplateHost = null;
+        TemplateDataStoreVO vmTemplateStore = null;
 
-        vmTemplateHost = _vmTemplateHostDao.findByHostTemplate(sserver.getId(), template.getId());
-        if (vmTemplateHost == null) {
-            vmTemplateHost = new VMTemplateHostVO(sserver.getId(), template.getId(), new Date(), 0, VMTemplateStorageResourceAssoc.Status.NOT_DOWNLOADED, null, null, "jobid0000", null, template.getUrl());
-            _vmTemplateHostDao.persist(vmTemplateHost);
-        } else if ((vmTemplateHost.getJobId() != null) && (vmTemplateHost.getJobId().length() > 2)) {
+        vmTemplateStore = _vmTemplateStoreDao.findByStoreTemplate(store.getId(), template.getId());
+        // we should already persist one entry in template_store_ref table, so vmTemplateStore should not be null
+        if ((vmTemplateStore.getJobId() != null) && (vmTemplateStore.getJobId().length() > 2)) {
             downloadJobExists = true;
         }
-                
+
         Long maxTemplateSizeInBytes = getMaxTemplateSizeInBytes();
-        String secUrl = sserver.getStorageUrl();
-		if(vmTemplateHost != null) {
+        String secUrl = store.getUri();
+		if(vmTemplateStore != null) {
 		    start();
 			DownloadCommand dcmd =
              new DownloadCommand(secUrl, template, maxTemplateSizeInBytes);
 			dcmd.setProxy(getHttpProxy());
 	        if (downloadJobExists) {
-	            dcmd = new DownloadProgressCommand(dcmd, vmTemplateHost.getJobId(), RequestType.GET_OR_RESTART);
+	            dcmd = new DownloadProgressCommand(dcmd, vmTemplateStore.getJobId(), RequestType.GET_OR_RESTART);
 	        }
-			if (vmTemplateHost.isCopy()) {
+			if (vmTemplateStore.isCopy()) {
 				dcmd.setCreds(TemplateConstants.DEFAULT_HTTP_AUTH_USER, _copyAuthPasswd);
 			}
-			HostVO ssAhost = _ssvmMgr.pickSsvmHost(sserver);
+			HostVO ssAhost = _ssvmMgr.pickSsvmHost(store);
 			if( ssAhost == null ) {
-	             s_logger.warn("There is no secondary storage VM for secondary storage host " + sserver.getName());
+	             s_logger.warn("There is no secondary storage VM for downloading template to image store " + store.getName());
 	             return;
 			}
-			DownloadListener dl = new DownloadListener(ssAhost, sserver, template, _timer, _vmTemplateHostDao, vmTemplateHost.getId(), this, dcmd, _templateDao, _resourceLimitMgr, _alertMgr, _accountMgr);
+			DownloadListener dl = new DownloadListener(ssAhost, store, template, _timer, _vmTemplateStoreDao, vmTemplateStore.getId(), this, dcmd, _templateDao, _resourceLimitMgr, _alertMgr, _accountMgr, callback);
 			if (downloadJobExists) {
-				dl.setCurrState(vmTemplateHost.getDownloadState());
+			    // due to handling existing download job issues, we still keep downloadState in template_store_ref to avoid big change in DownloadListener to use
+			    // new ObjectInDataStore.State transition. TODO: fix this later to be able to remove downloadState from template_store_ref.
+				dl.setCurrState(vmTemplateStore.getDownloadState());
 	 		}
             DownloadListener old = null;
-            synchronized (_listenerMap) {
-                old = _listenerMap.put(vmTemplateHost, dl);
+            synchronized (_listenerTemplateMap) {
+                old = _listenerTemplateMap.put(vmTemplateStore, dl);
             }
             if( old != null ) {
                 old.abandon();
@@ -396,7 +410,7 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
 			try {
 	            send(ssAhost.getId(), dcmd, dl);
             } catch (AgentUnavailableException e) {
-				s_logger.warn("Unable to start /resume download of template " + template.getUniqueName() + " to " + sserver.getName(), e);
+				s_logger.warn("Unable to start /resume download of template " + template.getUniqueName() + " to " + store.getName(), e);
 				dl.setDisconnected();
 				dl.scheduleStatusCheck(RequestType.GET_OR_RESTART);
             }
@@ -406,39 +420,26 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
 
 
 	@Override
-	public boolean downloadTemplateToStorage(VMTemplateVO template, Long zoneId) {
-        List<DataCenterVO> dcs = new ArrayList<DataCenterVO>();       
-        if (zoneId == null) {
-            dcs.addAll(_dcDao.listAll());
-        } else {
-            dcs.add(_dcDao.findById(zoneId));
-        }
+	public boolean downloadTemplateToStorage(VMTemplateVO template, DataStore store, AsyncCompletionCallback<CreateCmdResult> callback) {
         long templateId = template.getId();
-        boolean isPublic = template.isFeatured() || template.isPublicTemplate(); 
-        for ( DataCenterVO dc : dcs ) {
-    	    List<HostVO> ssHosts = _ssvmMgr.listAllTypesSecondaryStorageHostsInOneZone(dc.getId());
-    	    for ( HostVO ssHost : ssHosts ) {
-        		if (isTemplateUpdateable(templateId, ssHost.getId())) {
-       				initiateTemplateDownload(templateId, ssHost);
-       				if (! isPublic ) {
-       				    break;
-       				}
-        		}
-    	    }
-	    }
+        if (isTemplateUpdateable(templateId, store.getId())) {
+            if ( template != null && template.getUrl() != null ){
+                initiateTemplateDownload(template, store, callback);
+            }
+        }
 	    return true;
 	}
-	
+
 	@Override
 	public boolean downloadVolumeToStorage(VolumeVO volume, Long zoneId, String url, String checkSum, ImageFormat format) {
-                                
+
 	    List<HostVO> ssHosts = _ssvmMgr.listAllTypesSecondaryStorageHostsInOneZone(zoneId);
 	    Collections.shuffle(ssHosts);
 	    HostVO ssHost = ssHosts.get(0);
 	    downloadVolumeToStorage(volume, ssHost, url, checkSum, format);
 	    return true;
 	}
-	
+
 	private void downloadVolumeToStorage(VolumeVO volume, HostVO sserver, String url, String checkSum, ImageFormat format) {
 		boolean downloadJobExists = false;
         VolumeHostVO volumeHost = null;
@@ -451,7 +452,7 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
         } else if ((volumeHost.getJobId() != null) && (volumeHost.getJobId().length() > 2)) {
             downloadJobExists = true;
         }
-        
+
 
         Long maxVolumeSizeInBytes = getMaxVolumeSizeInBytes();
         String secUrl = sserver.getStorageUrl();
@@ -463,7 +464,7 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
 	            dcmd = new DownloadProgressCommand(dcmd, volumeHost.getJobId(), RequestType.GET_OR_RESTART);
 	            dcmd.setResourceType(ResourceType.VOLUME);
 	        }
-			
+
 			HostVO ssvm = _ssvmMgr.pickSsvmHost(sserver);
 			if( ssvm == null ) {
 	             s_logger.warn("There is no secondary storage VM for secondary storage host " + sserver.getName());
@@ -471,7 +472,7 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
 			}
 			DownloadListener dl = new DownloadListener(ssvm, sserver, volume, _timer, _volumeHostDao, volumeHost.getId(),
 					this, dcmd, _volumeDao, _storageMgr, _resourceLimitMgr, _alertMgr, _accountMgr);
-			
+
 			if (downloadJobExists) {
 				dl.setCurrState(volumeHost.getDownloadState());
 	 		}
@@ -493,15 +494,16 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
 		}
 	}
 
-
-	private void initiateTemplateDownload(Long templateId, HostVO ssHost) {
+/*
+	private void initiateTemplateDownload(Long templateId, DataStore store) {
 		VMTemplateVO template = _templateDao.findById(templateId);
 		if (template != null && (template.getUrl() != null)) {
 			//find all storage hosts and tell them to initiate download
-    		downloadTemplateToStorage(template, ssHost);
+    		downloadTemplateToStorage(template, store);
 		}
-		
+
 	}
+	*/
 
 	@DB
 	public void handleDownloadEvent(HostVO host, VMTemplateVO template, Status dnldStatus) {
@@ -511,11 +513,11 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
 	            _listenerMap.remove(vmTemplateHost);
 	        }
 		}
-		
+
 		VMTemplateHostVO vmTemplateHost = _vmTemplateHostDao.findByHostTemplate(host.getId(), template.getId());
-		
+
 		Transaction txn = Transaction.currentTxn();
-        txn.start();		
+        txn.start();
 
         if (dnldStatus == Status.DOWNLOADED) {
             long size = -1;
@@ -548,14 +550,14 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
 	        	_listenerVolumeMap.remove(volumeHost);
 	        }
 		}
-		
+
 		VolumeHostVO volumeHost = _volumeHostDao.findByHostVolume(host.getId(), volume.getId());
-		
+
 		Transaction txn = Transaction.currentTxn();
-        txn.start();		
+        txn.start();
 
         if (dnldStatus == Status.DOWNLOADED) {
-			
+
 			//Create usage event
             long size = -1;
             if(volumeHost!=null){
@@ -590,38 +592,45 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
 	    }
 
 	    Set<VMTemplateVO> toBeDownloaded = new HashSet<VMTemplateVO>();
+	    List<DataStore> ssHosts = this.storeMgr.getImageStoresByScope(new ZoneScope(host.getDataCenterId()));
+	    if (ssHosts == null || ssHosts.isEmpty()){
+	        return;
+	    }
+	       /*
 	    List<HostVO> ssHosts = _resourceMgr.listAllUpAndEnabledHostsInOneZoneByType(Host.Type.SecondaryStorage, host.getDataCenterId());
 	    if (ssHosts == null || ssHosts.isEmpty()) {
 	        return;
 	    }
+	    */
 	    /*Download all the templates in zone with the same hypervisortype*/
-        for ( HostVO ssHost : ssHosts) {
+        for ( DataStore ssHost : ssHosts) {
     	    List<VMTemplateVO> rtngTmplts = _templateDao.listAllSystemVMTemplates();
     	    List<VMTemplateVO> defaultBuiltin = _templateDao.listDefaultBuiltinTemplates();
-    
-    
+
+
     	    for (VMTemplateVO rtngTmplt : rtngTmplts) {
     	        if (rtngTmplt.getHypervisorType() == hostHyper) {
     	            toBeDownloaded.add(rtngTmplt);
     	        }
     	    }
-    
+
     	    for (VMTemplateVO builtinTmplt : defaultBuiltin) {
     	        if (builtinTmplt.getHypervisorType() == hostHyper) {
     	            toBeDownloaded.add(builtinTmplt);
     	        }
     	    }
-    
+
     	    for (VMTemplateVO template: toBeDownloaded) {
     	        VMTemplateHostVO tmpltHost = _vmTemplateHostDao.findByHostTemplate(ssHost.getId(), template.getId());
     	        if (tmpltHost == null || tmpltHost.getDownloadState() != Status.DOWNLOADED) {
-    	            downloadTemplateToStorage(template, ssHost);
+    	            //TODO: pass callback here
+    	            initiateTemplateDownload(template, ssHost, null);
     	        }
     	    }
         }
 	}
-    
-    @Override	
+
+    @Override
 	public void addSystemVMTemplatesToHost(HostVO host, Map<String, TemplateInfo> templateInfos){
 	    if ( templateInfos == null ) {
 	        return;
@@ -642,21 +651,26 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
 	        }
 	    }
 	}
-	
+
     @Override
     public void handleSync(Long dcId) {
         if (dcId != null) {
             List<HostVO> ssHosts = _ssvmMgr.listSecondaryStorageHostsInOneZone(dcId);
             for (HostVO ssHost : ssHosts) {
-                handleTemplateSync(ssHost);
+                //handleTemplateSync(ssHost);
                 handleVolumeSync(ssHost);
             }
         }
+        List<DataStore> imageStores = this.storeMgr.getImageStoresByScope(new ZoneScope(dcId));
+        for (DataStore store : imageStores){
+            handleTemplateSync(store);
+        }
     }
-    
-    private Map<String, TemplateInfo> listTemplate(HostVO ssHost) {
-        ListTemplateCommand cmd = new ListTemplateCommand(ssHost.getStorageUrl());
-        Answer answer = _agentMgr.sendToSecStorage(ssHost, cmd);
+
+    private Map<String, TemplateInfo> listTemplate(DataStore ssHost) {
+        ListTemplateCommand cmd = new ListTemplateCommand(ssHost.getUri());
+        HostVO ssAhost = _ssvmMgr.pickSsvmHost(ssHost);
+        Answer answer = _agentMgr.sendToSecStorage(ssAhost, cmd);
         if (answer != null && answer.getResult()) {
             ListTemplateAnswer tanswer = (ListTemplateAnswer)answer;
             return tanswer.getTemplateInfo();
@@ -664,11 +678,11 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
             if (s_logger.isDebugEnabled()) {
                 s_logger.debug("can not list template for secondary storage host " + ssHost.getId());
             }
-        } 
-        
+        }
+
         return null;
     }
-    
+
     private Map<Long, TemplateInfo> listVolume(HostVO ssHost) {
     	ListVolumeCommand cmd = new ListVolumeCommand(ssHost.getStorageUrl());
         Answer answer = _agentMgr.sendToSecStorage(ssHost, cmd);
@@ -679,11 +693,11 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
             if (s_logger.isDebugEnabled()) {
                 s_logger.debug("Can not list volumes for secondary storage host " + ssHost.getId());
             }
-        } 
-        
+        }
+
         return null;
     }
-    
+
     private Map<String, TemplateInfo> listTemplate(SwiftVO swift) {
         if (swift == null) {
             return null;
@@ -700,14 +714,14 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
         }
         return null;
     }
-    
+
     @Override
     public void handleVolumeSync(HostVO ssHost) {
         if (ssHost == null) {
             s_logger.warn("Huh? ssHost is null");
             return;
         }
-        long sserverId = ssHost.getId();        
+        long sserverId = ssHost.getId();
         if (!(ssHost.getType() == Host.Type.SecondaryStorage || ssHost.getType() == Host.Type.LocalSecondaryStorage)) {
             s_logger.warn("Huh? Agent id " + sserverId + " is not secondary storage host");
             return;
@@ -717,7 +731,7 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
         if (volumeInfos == null) {
             return;
         }
-        
+
         List<VolumeHostVO> dbVolumes = _volumeHostDao.listBySecStorage(sserverId);
         List<VolumeHostVO> toBeDownloaded = new ArrayList<VolumeHostVO>(dbVolumes);
         for (VolumeHostVO volumeHost : dbVolumes){
@@ -779,17 +793,17 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
         	if (volumeHost.getDownloadState() != Status.DOWNLOADED) {
                 s_logger.info("Volume Sync did not find " + volume.getName() + " ready on server " + sserverId + ", will request download to start/resume shortly");
                 toBeDownloaded.add(volumeHost);
-            } 
+            }
         }
-        
+
         //Download volumes which haven't been downloaded yet.
         if (toBeDownloaded.size() > 0) {
             for (VolumeHostVO volumeHost : toBeDownloaded) {
                 if (volumeHost.getDownloadUrl() == null) { // If url is null we can't initiate the download
                     continue;
-                }                                  
+                }
                 s_logger.debug("Volume " + volumeHost.getVolumeId() + " needs to be downloaded to " + ssHost.getName());
-                downloadVolumeToStorage(_volumeDao.findById(volumeHost.getVolumeId()), ssHost,  volumeHost.getDownloadUrl(), volumeHost.getChecksum(), volumeHost.getFormat());                
+                downloadVolumeToStorage(_volumeDao.findById(volumeHost.getVolumeId()), ssHost,  volumeHost.getDownloadUrl(), volumeHost.getChecksum(), volumeHost.getFormat());
             }
         }
 
@@ -804,32 +818,36 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
                 s_logger.error(err);
                 return;
             }
-            
+
             String description = "Deleted volume " + vInfo.getTemplateName() + " on secondary storage " + sserverId + " since it isn't in the database";
             s_logger.info(description);
         }
     }
-    
+
 	@Override
-    public void handleTemplateSync(HostVO ssHost) {
-        if (ssHost == null) {
-            s_logger.warn("Huh? ssHost is null");
-            return;
-        }
-        long sserverId = ssHost.getId();
-        long zoneId = ssHost.getDataCenterId();
-        if (!(ssHost.getType() == Host.Type.SecondaryStorage || ssHost.getType() == Host.Type.LocalSecondaryStorage)) {
-            s_logger.warn("Huh? Agent id " + sserverId + " is not secondary storage host");
+    public void handleTemplateSync(DataStore ssStore) {
+        if (ssStore == null) {
+            s_logger.warn("Huh? image store is null");
             return;
         }
+        long storeId = ssStore.getId();
+        Long zoneId = ssStore.getScope().getScopeId();
 
-        Map<String, TemplateInfo> templateInfos = listTemplate(ssHost);
+        Map<String, TemplateInfo> templateInfos = listTemplate(ssStore);
         if (templateInfos == null) {
             return;
         }
 
         Set<VMTemplateVO> toBeDownloaded = new HashSet<VMTemplateVO>();
-        List<VMTemplateVO> allTemplates = _templateDao.listAllInZone(zoneId);
+        List<VMTemplateVO> allTemplates = null;
+        if (zoneId == null){
+            // region wide store
+            allTemplates = _templateDao.listAll();
+        }
+        else{
+            // zone wide store
+            allTemplates = _templateDao.listAllInZone(zoneId);
+        }
         List<VMTemplateVO> rtngTmplts = _templateDao.listAllSystemVMTemplates();
         List<VMTemplateVO> defaultBuiltin = _templateDao.listDefaultBuiltinTemplates();
 
@@ -853,34 +871,34 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
 
         for (VMTemplateVO tmplt : allTemplates) {
             String uniqueName = tmplt.getUniqueName();
-            VMTemplateHostVO tmpltHost = _vmTemplateHostDao.findByHostTemplate(sserverId, tmplt.getId());
+            TemplateDataStoreVO tmpltStore = _vmTemplateStoreDao.findByStoreTemplate(storeId, tmplt.getId());
             if (templateInfos.containsKey(uniqueName)) {
                 TemplateInfo tmpltInfo = templateInfos.remove(uniqueName);
                 toBeDownloaded.remove(tmplt);
-                if (tmpltHost != null) {
+                if (tmpltStore != null) {
                     s_logger.info("Template Sync found " + uniqueName + " already in the template host table");
-                    if (tmpltHost.getDownloadState() != Status.DOWNLOADED) {
-                        tmpltHost.setErrorString("");
+                    if (tmpltStore.getDownloadState() != Status.DOWNLOADED) {
+                        tmpltStore.setErrorString("");
                     }
                     if (tmpltInfo.isCorrupted()) {
-                        tmpltHost.setDownloadState(Status.DOWNLOAD_ERROR);
-                        String msg = "Template " + tmplt.getName() + ":" + tmplt.getId() + " is corrupted on secondary storage " + tmpltHost.getId();
-                        tmpltHost.setErrorString(msg);
+                        tmpltStore.setDownloadState(Status.DOWNLOAD_ERROR);
+                        String msg = "Template " + tmplt.getName() + ":" + tmplt.getId() + " is corrupted on secondary storage " + tmpltStore.getId();
+                        tmpltStore.setErrorString(msg);
                         s_logger.info("msg");
                         if (tmplt.getUrl() == null) {
-                            msg = "Private Template (" + tmplt + ") with install path " + tmpltInfo.getInstallPath() + "is corrupted, please check in secondary storage: " + tmpltHost.getHostId();
+                            msg = "Private Template (" + tmplt + ") with install path " + tmpltInfo.getInstallPath() + "is corrupted, please check in image store: " + tmpltStore.getDataStoreId();
                             s_logger.warn(msg);
                         } else {
                             toBeDownloaded.add(tmplt);
                         }
 
                     } else {
-                        tmpltHost.setDownloadPercent(100);
-                        tmpltHost.setDownloadState(Status.DOWNLOADED);
-                        tmpltHost.setInstallPath(tmpltInfo.getInstallPath());
-                        tmpltHost.setSize(tmpltInfo.getSize());
-                        tmpltHost.setPhysicalSize(tmpltInfo.getPhysicalSize());
-                        tmpltHost.setLastUpdated(new Date());
+                        tmpltStore.setDownloadPercent(100);
+                        tmpltStore.setDownloadState(Status.DOWNLOADED);
+                        tmpltStore.setInstallPath(tmpltInfo.getInstallPath());
+                        tmpltStore.setSize(tmpltInfo.getSize());
+                        tmpltStore.setPhysicalSize(tmpltInfo.getPhysicalSize());
+                        tmpltStore.setLastUpdated(new Date());
 
                         if (tmpltInfo.getSize() > 0) {
                             long accountId = tmplt.getAccountId();
@@ -890,7 +908,7 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
                                         tmpltInfo.getSize() - UriUtils.getRemoteSize(tmplt.getUrl()));
                             } catch (ResourceAllocationException e) {
                                 s_logger.warn(e.getMessage());
-                                _alertMgr.sendAlert(_alertMgr.ALERT_TYPE_RESOURCE_LIMIT_EXCEEDED, ssHost.getDataCenterId(),
+                                _alertMgr.sendAlert(_alertMgr.ALERT_TYPE_RESOURCE_LIMIT_EXCEEDED, zoneId,
                                         null, e.getMessage(), e.getMessage());
                             } finally {
                                 _resourceLimitMgr.recalculateResourceCount(accountId, _accountMgr.getAccount(accountId).getDomainId(),
@@ -898,39 +916,62 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
                             }
                         }
                     }
-                    _vmTemplateHostDao.update(tmpltHost.getId(), tmpltHost);
+                    _vmTemplateStoreDao.update(tmpltStore.getId(), tmpltStore);
                 } else {
-                    tmpltHost = new VMTemplateHostVO(sserverId, tmplt.getId(), new Date(), 100, Status.DOWNLOADED, null, null, null, tmpltInfo.getInstallPath(), tmplt.getUrl());
-                    tmpltHost.setSize(tmpltInfo.getSize());
-                    tmpltHost.setPhysicalSize(tmpltInfo.getPhysicalSize());
-                    _vmTemplateHostDao.persist(tmpltHost);
-                    VMTemplateZoneVO tmpltZoneVO = _vmTemplateZoneDao.findByZoneTemplate(zoneId, tmplt.getId());
+                    tmpltStore = new TemplateDataStoreVO(storeId, tmplt.getId(), new Date(), 100, Status.DOWNLOADED, null, null, null, tmpltInfo.getInstallPath(), tmplt.getUrl());
+                    tmpltStore.setSize(tmpltInfo.getSize());
+                    tmpltStore.setPhysicalSize(tmpltInfo.getPhysicalSize());
+                    _vmTemplateStoreDao.persist(tmpltStore);
+                    List<Long> dcs = new ArrayList<Long>();
+                    if (zoneId != null ){
+                        dcs.add(zoneId);
+                    }
+                    else{
+                        List<DataCenterVO> zones = _dcDao.listAll();
+                        for (DataCenterVO zone : zones){
+                            dcs.add(zone.getId());
+                        }
+                    }
+                    for (Long id : dcs) {
+                        VMTemplateZoneVO tmpltZoneVO = _vmTemplateZoneDao.findByZoneTemplate(id, tmplt.getId());
+                        if (tmpltZoneVO == null) {
+                            tmpltZoneVO = new VMTemplateZoneVO(id, tmplt.getId(), new Date());
+                            _vmTemplateZoneDao.persist(tmpltZoneVO);
+                        } else {
+                            tmpltZoneVO.setLastUpdated(new Date());
+                            _vmTemplateZoneDao.update(tmpltZoneVO.getId(), tmpltZoneVO);
+                        }
+                    }
+                }
+
+                continue;
+            }
+            if (tmpltStore != null && tmpltStore.getDownloadState() != Status.DOWNLOADED) {
+                s_logger.info("Template Sync did not find " + uniqueName + " ready on server " + storeId + ", will request download to start/resume shortly");
+
+            } else if (tmpltStore == null) {
+                s_logger.info("Template Sync did not find " + uniqueName + " on the server " + storeId + ", will request download shortly");
+                TemplateDataStoreVO templtStore = new TemplateDataStoreVO(storeId, tmplt.getId(), new Date(), 0, Status.NOT_DOWNLOADED, null, null, null, null, tmplt.getUrl());
+                _vmTemplateStoreDao.persist(templtStore);
+                List<Long> dcs = new ArrayList<Long>();
+                if (zoneId != null ){
+                    dcs.add(zoneId);
+                }
+                else{
+                    List<DataCenterVO> zones = _dcDao.listAll();
+                    for (DataCenterVO zone : zones){
+                        dcs.add(zone.getId());
+                    }
+                }
+                for (Long id : dcs) {
+                    VMTemplateZoneVO tmpltZoneVO = _vmTemplateZoneDao.findByZoneTemplate(id, tmplt.getId());
                     if (tmpltZoneVO == null) {
-                        tmpltZoneVO = new VMTemplateZoneVO(zoneId, tmplt.getId(), new Date());
+                        tmpltZoneVO = new VMTemplateZoneVO(id, tmplt.getId(), new Date());
                         _vmTemplateZoneDao.persist(tmpltZoneVO);
                     } else {
                         tmpltZoneVO.setLastUpdated(new Date());
                         _vmTemplateZoneDao.update(tmpltZoneVO.getId(), tmpltZoneVO);
                     }
-
-                }
-
-                continue;
-            }
-            if (tmpltHost != null && tmpltHost.getDownloadState() != Status.DOWNLOADED) {
-                s_logger.info("Template Sync did not find " + uniqueName + " ready on server " + sserverId + ", will request download to start/resume shortly");
-
-            } else if (tmpltHost == null) {
-                s_logger.info("Template Sync did not find " + uniqueName + " on the server " + sserverId + ", will request download shortly");
-                VMTemplateHostVO templtHost = new VMTemplateHostVO(sserverId, tmplt.getId(), new Date(), 0, Status.NOT_DOWNLOADED, null, null, null, null, tmplt.getUrl());
-                _vmTemplateHostDao.persist(templtHost);
-                VMTemplateZoneVO tmpltZoneVO = _vmTemplateZoneDao.findByZoneTemplate(zoneId, tmplt.getId());
-                if (tmpltZoneVO == null) {
-                    tmpltZoneVO = new VMTemplateZoneVO(zoneId, tmplt.getId(), new Date());
-                    _vmTemplateZoneDao.persist(tmpltZoneVO);
-                } else {
-                    tmpltZoneVO.setLastUpdated(new Date());
-                    _vmTemplateZoneDao.update(tmpltZoneVO.getId(), tmpltZoneVO);
                 }
             }
 
@@ -958,7 +999,7 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
                 // if this is private template, and there is no record for this
                 // template in this sHost, skip
                 if (!tmplt.isPublicTemplate() && !tmplt.isFeatured()) {
-                    VMTemplateHostVO tmpltHost = _vmTemplateHostDao.findByHostTemplate(sserverId, tmplt.getId());
+                    VMTemplateHostVO tmpltHost = _vmTemplateHostDao.findByHostTemplate(storeId, tmplt.getId());
                     if (tmpltHost == null) {
                         continue;
                     }
@@ -969,8 +1010,9 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
                             continue;
                         }
                     }
-                    s_logger.debug("Template " + tmplt.getName() + " needs to be downloaded to " + ssHost.getName());
-                    downloadTemplateToStorage(tmplt, ssHost);
+                    s_logger.debug("Template " + tmplt.getName() + " needs to be downloaded to " + ssStore.getName());
+                    //TODO: we should pass a callback here
+                    initiateTemplateDownload(tmplt, ssStore, null);
                 }
             }
         }
@@ -980,16 +1022,17 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
             List<UserVmVO> userVmUsingIso = _userVmDao.listByIsoId(tInfo.getId());
             //check if there is any Vm using this ISO.
             if (userVmUsingIso == null || userVmUsingIso.isEmpty()) {
-                DeleteTemplateCommand dtCommand = new DeleteTemplateCommand(ssHost.getStorageUrl(), tInfo.getInstallPath());
+                DeleteTemplateCommand dtCommand = new DeleteTemplateCommand(ssStore.getUri(), tInfo.getInstallPath());
                 try {
-                    _agentMgr.sendToSecStorage(ssHost, dtCommand, null);
+                    HostVO ssAhost = _ssvmMgr.pickSsvmHost(ssStore);
+                    _agentMgr.sendToSecStorage(ssAhost, dtCommand, null);
                 } catch (AgentUnavailableException e) {
-                    String err = "Failed to delete " + tInfo.getTemplateName() + " on secondary storage " + sserverId + " which isn't in the database";
+                    String err = "Failed to delete " + tInfo.getTemplateName() + " on secondary storage " + storeId + " which isn't in the database";
                     s_logger.error(err);
                     return;
                 }
 
-                String description = "Deleted template " + tInfo.getTemplateName() + " on secondary storage " + sserverId + " since it isn't in the database";
+                String description = "Deleted template " + tInfo.getTemplateName() + " on secondary storage " + storeId + " since it isn't in the database";
                 s_logger.info(description);
             }
         }
@@ -1012,10 +1055,11 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
 			}
 		}
 	}
-	
+
+	/*
 	private void checksumSync(long hostId){
-        SearchCriteria<VMTemplateHostVO> sc = ReadyTemplateStatesSearch.create();
-        sc.setParameters("download_state", com.cloud.storage.VMTemplateStorageResourceAssoc.Status.DOWNLOADED);
+        SearchCriteria<TemplateDataStoreVO> sc = ReadyTemplateStatesSearch.create();
+        sc.setParameters("state", ObjectInDataStoreStateMachine.State.Ready);
         sc.setParameters("host_id", hostId);
 
         List<VMTemplateHostVO> templateHostRefList = _vmTemplateHostDao.search(sc, null);
@@ -1030,7 +1074,8 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
         }
 
 	}
-	
+	*/
+
 	private Long getMaxTemplateSizeInBytes() {
 		try {
 			return Long.parseLong(_configDao.getValue("max.template.iso.size")) * 1024L * 1024L * 1024L;
@@ -1038,7 +1083,7 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
 			return null;
 		}
 	}
-	
+
 	private Long getMaxVolumeSizeInBytes() {
 		try {
 			return Long.parseLong(_configDao.getValue("storage.max.volume.upload.size")) * 1024L * 1024L * 1024L;
@@ -1046,7 +1091,7 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
 			return null;
 		}
 	}
-	
+
 	private Proxy getHttpProxy() {
 		if (_proxy == null) {
 			return null;
@@ -1058,7 +1103,7 @@ public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor
 		} catch (URISyntaxException e) {
 			return null;
 		}
-	}	
-	
+	}
+
 }
-	
+

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/02686583/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java b/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java
index c94224b..bdac44d 100755
--- a/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java
+++ b/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java
@@ -5,7 +5,7 @@
 // to you under the Apache License, Version 2.0 (the
 // "License"); you may not use this file except in compliance
 // with the License.  You may obtain a copy of the License at
-// 
+//
 //   http://www.apache.org/licenses/LICENSE-2.0
 //
 // Unless required by applicable law or agreed to in writing,
@@ -29,6 +29,9 @@ import javax.ejb.Local;
 import javax.inject.Inject;
 import javax.naming.ConfigurationException;
 
+import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
+import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreRole;
+import org.apache.cloudstack.engine.subsystem.api.storage.Scope;
 import org.apache.log4j.Logger;
 import org.springframework.context.annotation.Primary;
 import org.springframework.stereotype.Component;
@@ -95,6 +98,7 @@ import com.cloud.resource.ServerResource;
 import com.cloud.resource.UnableDeleteHostException;
 import com.cloud.service.ServiceOfferingVO;
 import com.cloud.service.dao.ServiceOfferingDao;
+import com.cloud.storage.ScopeType;
 import com.cloud.storage.SnapshotVO;
 import com.cloud.storage.Storage;
 import com.cloud.storage.VMTemplateHostVO;
@@ -199,7 +203,7 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
     protected NetworkModel _networkModel;
     @Inject
     protected SnapshotDao _snapshotDao;
-    
+
     @Inject
     private ClusterManager _clusterMgr;
 
@@ -221,7 +225,7 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
     protected CapacityDao                         _capacityDao;
     @Inject
     UserVmDetailsDao _vmDetailsDao;
-    @Inject 
+    @Inject
     protected ResourceManager _resourceMgr;
     //@Inject			// TODO this is a very strange usage, a singleton class need to inject itself?
     protected SecondaryStorageVmManager _ssvmMgr;
@@ -233,7 +237,7 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
     protected IPAddressDao _ipAddressDao = null;
     @Inject
     protected RulesManager _rulesMgr;
-    
+
     @Inject
     KeystoreManager _keystoreMgr;
     private long _capacityScanInterval = DEFAULT_CAPACITY_SCAN_INTERVAL;
@@ -254,7 +258,7 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
     public SecondaryStorageManagerImpl() {
     	_ssvmMgr = this;
     }
-    
+
     @Override
     public SecondaryStorageVmVO startSecStorageVm(long secStorageVmId) {
         try {
@@ -283,13 +287,13 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
         }
         return null;
     }
-    
+
     @Override
     public boolean generateSetupCommand(Long ssHostId) {
         HostVO cssHost = _hostDao.findById(ssHostId);
         Long zoneId = cssHost.getDataCenterId();
         if( cssHost.getType() == Host.Type.SecondaryStorageVM ) {
-    
+
             SecondaryStorageVmVO secStorageVm = _secStorageVmDao.findByInstanceName(cssHost.getName());
             if (secStorageVm == null) {
                 s_logger.warn("secondary storage VM " + cssHost.getName() + " doesn't exist");
@@ -306,7 +310,7 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
                 	Certificates certs = _keystoreMgr.getCertificates(ConsoleProxyManager.CERTIFICATE_NAME);
                 	setupCmd = new SecStorageSetupCommand(secUrl, certs);
                 }
-                
+
                 Answer answer = _agentMgr.easySend(ssHostId, setupCmd);
                 if (answer != null && answer.getResult()) {
                     SecStorageSetupAnswer an = (SecStorageSetupAnswer) answer;
@@ -338,14 +342,14 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
                         s_logger.debug("Successfully programmed secondary storage " + host.getName() + " in secondary storage VM " + ssVm.getInstanceName());
                     }
                     return false;
-                }               
+                }
             }
         }
         return true;
     }
-    
-    
-    @Override 
+
+
+    @Override
     public boolean deleteHost(Long hostId) {
         List<SnapshotVO> snapshots = _snapshotDao.listByHostId(hostId);
         if( snapshots != null && !snapshots.isEmpty()) {
@@ -362,10 +366,10 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
         host.setGuid(null);
         _hostDao.update(hostId, host);
         _hostDao.remove(hostId);
-        
+
         return true;
     }
-    
+
     @Override
     public boolean generateVMSetupCommand(Long ssAHostId) {
         HostVO ssAHost = _hostDao.findById(ssAHostId);
@@ -377,7 +381,7 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
             s_logger.warn("secondary storage VM " + ssAHost.getName() + " doesn't exist");
             return false;
         }
-      
+
         SecStorageVMSetupCommand setupCmd = new SecStorageVMSetupCommand();
         if (_allowedInternalSites != null) {
             List<String> allowedCidrs = new ArrayList<String>();
@@ -419,7 +423,7 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
         }
         HostVO ssAHost = _hostDao.findById(ssAHostId);
         SecondaryStorageVmVO thisSecStorageVm = _secStorageVmDao.findByInstanceName(ssAHost.getName());
-        
+
         if (thisSecStorageVm == null) {
             s_logger.warn("secondary storage VM " + ssAHost.getName() + " doesn't exist");
             return false;
@@ -428,7 +432,7 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
         String copyPort = _useSSlCopy? "443" : Integer.toString(TemplateConstants.DEFAULT_TMPLT_COPY_PORT);
         SecStorageFirewallCfgCommand thiscpc = new SecStorageFirewallCfgCommand(true);
         thiscpc.addPortConfig(thisSecStorageVm.getPublicIpAddress(), copyPort, true, TemplateConstants.DEFAULT_TMPLT_COPY_INTF);
-        
+
         SearchCriteriaService<HostVO, HostVO> sc = SearchCriteria2.create(HostVO.class);
         sc.addAnd(sc.getEntity().getType(), Op.EQ, Host.Type.SecondaryStorageVM);
         sc.addAnd(sc.getEntity().getStatus(), Op.IN, com.cloud.host.Status.Up, com.cloud.host.Status.Connecting);
@@ -449,7 +453,7 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
                 return false;
             }
         }
-        
+
         SecStorageFirewallCfgCommand allSSVMIpList = new SecStorageFirewallCfgCommand(false);
         for (HostVO ssvm : ssvms) {
         	if (ssvm.getId() == ssAHostId) {
@@ -457,7 +461,7 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
         	}
         	allSSVMIpList.addPortConfig(ssvm.getPublicIpAddress(), copyPort, true, TemplateConstants.DEFAULT_TMPLT_COPY_INTF);
         }
-        
+
         Answer answer = _agentMgr.easySend(ssAHostId, allSSVMIpList);
         if (answer != null && answer.getResult()) {
             if (s_logger.isDebugEnabled()) {
@@ -469,7 +473,7 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
             }
             return false;
         }
-        
+
         return true;
 
     }
@@ -539,19 +543,19 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
 
         DataCenterDeployment plan = new DataCenterDeployment(dataCenterId);
         DataCenter dc = _dcDao.findById(plan.getDataCenterId());
-        
+
         TrafficType defaultTrafficType = TrafficType.Public;
         if (dc.getNetworkType() == NetworkType.Basic || dc.isSecurityGroupEnabled()) {
         	defaultTrafficType = TrafficType.Guest;
         }
-        
+
         List<NetworkVO> defaultNetworks = _networkDao.listByZoneAndTrafficType(dataCenterId, defaultTrafficType);
-        
+
         //api should never allow this situation to happen
         if (defaultNetworks.size() != 1) {
         	throw new CloudRuntimeException("Found " + defaultNetworks.size() + " networks of type " + defaultTrafficType + " when expect to find 1");
         }
-        
+
         NetworkVO defaultNetwork = defaultNetworks.get(0);
 
         List<? extends NetworkOffering> offerings = _networkModel.getSystemAccountNetworkOfferings(NetworkOfferingVO.SystemControlNetwork, NetworkOfferingVO.SystemManagementNetwork, NetworkOfferingVO.SystemStorageNetwork);
@@ -570,7 +574,7 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
         }
 
         HypervisorType hypeType = _resourceMgr.getAvailableHypervisor(dataCenterId);
-        
+
         VMTemplateVO template = _templateDao.findSystemVMTemplate(dataCenterId, hypeType);
         if (template == null) {
             s_logger.debug("Can't find a template to start");
@@ -596,7 +600,7 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
         // for now, only one adapter is supported
     	if(_ssVmAllocators.size() > 0)
     		return _ssVmAllocators.get(0);
-    	
+
         return null;
     }
 
@@ -652,8 +656,8 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
             }
             return;
         }
-        
-        
+
+
         boolean secStorageVmFromStoppedPool = false;
         SecondaryStorageVmVO secStorageVm = assignSecStorageVmFromStoppedPool(dataCenterId, role);
         if (secStorageVm == null) {
@@ -798,7 +802,7 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
         }
 
         Map<String, String> configs = _configDao.getConfiguration("management-server", params);
-        
+
         _secStorageVmMtuSize = NumbersUtil.parseInt(configs.get("secstorage.vm.mtu.size"), DEFAULT_SS_VM_MTUSIZE);
         String useServiceVM = _configDao.getValue("secondary.storage.vm");
         boolean _useServiceVM = false;
@@ -834,7 +838,7 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
         _agentMgr.registerForHostEvents(_listener, true, false, true);
 
         _itMgr.registerGuru(VirtualMachine.Type.SecondaryStorageVm, this);
-        
+
         //check if there is a default service offering configured
         String ssvmSrvcOffIdStr = configs.get(Config.SecondaryStorageServiceOffering.key());
         if (ssvmSrvcOffIdStr != null) {
@@ -852,7 +856,7 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
             _serviceOffering = new ServiceOfferingVO("System Offering For Secondary Storage VM", 1, ramSize, cpuFreq, null, null, false, null, _useLocalStorage, true, null, true, VirtualMachine.Type.SecondaryStorageVm, true);
             _serviceOffering.setUniqueName(ServiceOffering.ssvmDefaultOffUniqueName);
             _serviceOffering = _offeringDao.persistSystemServiceOffering(_serviceOffering);
-            
+
             // this can sometimes happen, if DB is manually or programmatically manipulated
             if(_serviceOffering == null) {
                 String msg = "Data integrity problem : System Offering For Secondary Storage VM has been removed?";
@@ -865,7 +869,7 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
             _loadScanner = new SystemVmLoadScanner<Long>(this);
             _loadScanner.initScan(STARTUP_DELAY, _capacityScanInterval);
         }
-        
+
         _httpProxy = configs.get(Config.SecStorageProxy.key());
         if (_httpProxy != null) {
         	boolean valid = true;
@@ -989,14 +993,14 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
         try {
             boolean result = _itMgr.expunge(ssvm, _accountMgr.getSystemUser(), _accountMgr.getSystemAccount());
             if (result) {
-                HostVO host = _hostDao.findByTypeNameAndZoneId(ssvm.getDataCenterId(), ssvm.getHostName(), 
+                HostVO host = _hostDao.findByTypeNameAndZoneId(ssvm.getDataCenterId(), ssvm.getHostName(),
                         Host.Type.SecondaryStorageVM);
                 if (host != null) {
                     s_logger.debug("Removing host entry for ssvm id=" + vmId);
                     result = result && _hostDao.remove(host.getId());
                 }
             }
-            
+
             return result;
         } catch (ResourceUnavailableException e) {
             s_logger.warn("Unable to expunge " + ssvm, e);
@@ -1042,7 +1046,7 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
     	SecondaryStorageVmVO vm = profile.getVirtualMachine();
         Map<String, String> details = _vmDetailsDao.findDetails(vm.getId());
         vm.setDetails(details);
-    	
+
         HostVO secHost = _ssvmMgr.findSecondaryStorageHost(dest.getDataCenter().getId());
         assert (secHost != null);
 
@@ -1081,7 +1085,7 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
         if (externalDhcpStr != null && externalDhcpStr.equalsIgnoreCase("true")) {
             externalDhcp = true;
         }
-        
+
         if (Boolean.valueOf(_configDao.getValue("system.vm.random.password"))) {
         	buf.append(" vmpassword=").append(_configDao.getValue("system.vm.password"));
         }
@@ -1289,7 +1293,7 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
         List<SecondaryStorageVmVO> ssVms = _secStorageVmDao.getSecStorageVmListInStates(SecondaryStorageVm.Role.templateProcessor, dataCenterId, State.Running, State.Migrating,
                 State.Starting,  State.Stopped, State.Stopping );
         int vmSize = (ssVms == null)? 0 : ssVms.size();
-        List<HostVO> ssHosts = _ssvmMgr.listSecondaryStorageHostsInOneZone(dataCenterId);        
+        List<HostVO> ssHosts = _ssvmMgr.listSecondaryStorageHostsInOneZone(dataCenterId);
         int hostSize = (ssHosts == null)? 0 : ssHosts.size();
         if ( hostSize > vmSize ) {
             s_logger.info("No secondary storage vms found in datacenter id=" + dataCenterId + ", starting a new one");
@@ -1320,9 +1324,9 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
 	    if (!(firstCmd instanceof StartupSecondaryStorageCommand)) {
 	    	return null;
 	    }
-	    
+
 		host.setType( com.cloud.host.Host.Type.SecondaryStorageVM);
-		return host;	
+		return host;
     }
 
 	@Override
@@ -1368,7 +1372,7 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
 				host.setStorageUrl(ssCmd.getNfsShare());
 			}
 		}
-		
+
 		return host;
     }
 
@@ -1452,7 +1456,28 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
         }
         return null;
     }
-	
+
+
+    @Override
+    public HostVO pickSsvmHost(DataStore store) {
+        if ( store.getRole() == DataStoreRole.Image){
+            Long dcId = null;
+            Scope storeScope = store.getScope();
+            if ( storeScope.getScopeType() == ScopeType.ZONE ){
+                dcId = storeScope.getScopeId();
+            }
+            // find ssvm that can be used to download data to store. For zone-wide image store, use SSVM for that zone. For region-wide store,
+            // we can arbitrarily pick one ssvm to do that task
+            List<HostVO> ssAHosts = listUpAndConnectingSecondaryStorageVmHost(dcId);
+            if (ssAHosts == null || ssAHosts.isEmpty() ) {
+                return null;
+            }
+            Collections.shuffle(ssAHosts);
+            return ssAHosts.get(0);
+        }
+        return null;
+    }
+
     @Override
     public boolean plugNic(Network network, NicTO nic, VirtualMachineTO vm,
             ReservationContext context, DeployDestination dest) throws ConcurrentOperationException, ResourceUnavailableException,
@@ -1471,6 +1496,6 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
 
 	@Override
 	public void prepareStop(VirtualMachineProfile<SecondaryStorageVmVO> profile) {
-		
+
 	}
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/02686583/server/src/com/cloud/storage/secondary/SecondaryStorageVmManager.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/storage/secondary/SecondaryStorageVmManager.java b/server/src/com/cloud/storage/secondary/SecondaryStorageVmManager.java
index d315d22..8565c38 100755
--- a/server/src/com/cloud/storage/secondary/SecondaryStorageVmManager.java
+++ b/server/src/com/cloud/storage/secondary/SecondaryStorageVmManager.java
@@ -18,6 +18,8 @@ package com.cloud.storage.secondary;
 
 import java.util.List;
 
+import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
+
 import com.cloud.agent.api.Command;
 import com.cloud.agent.api.StartupCommand;
 import com.cloud.host.HostVO;
@@ -29,12 +31,12 @@ public interface SecondaryStorageVmManager extends Manager {
 
 	public static final int DEFAULT_SS_VM_RAMSIZE = 256;			// 256M
 	public static final int DEFAULT_SS_VM_CPUMHZ = 500;				// 500 MHz
-	public static final int DEFAULT_SS_VM_MTUSIZE = 1500;			
+	public static final int DEFAULT_SS_VM_MTUSIZE = 1500;
     public static final int DEFAULT_SS_VM_CAPACITY = 50;			// max command execution session per SSVM
     public static final int DEFAULT_STANDBY_CAPACITY = 10;			// standy capacity to reserve per zone
-	
+
 	public static final String ALERT_SUBJECT = "secondarystoragevm-alert";
-		
+
 	public SecondaryStorageVmVO startSecStorageVm(long ssVmVmId);
 	public boolean stopSecStorageVm(long ssVmVmId);
 	public boolean rebootSecStorageVm(long ssVmVmId);
@@ -42,7 +44,7 @@ public interface SecondaryStorageVmManager extends Manager {
 	public void onAgentConnect(Long dcId, StartupCommand cmd);
 	public boolean  generateFirewallConfiguration(Long agentId);
 	public boolean generateVMSetupCommand(Long hostId);
-	
+
 	public Pair<HostVO, SecondaryStorageVmVO> assignSecStorageVm(long zoneId, Command cmd);
     boolean generateSetupCommand(Long hostId);
     boolean deleteHost(Long hostId);
@@ -53,4 +55,5 @@ public interface SecondaryStorageVmManager extends Manager {
     public List<HostVO> listAllTypesSecondaryStorageHostsInOneZone(long dataCenterId);
     public List<HostVO> listUpAndConnectingSecondaryStorageVmHost(Long dcId);
     public HostVO pickSsvmHost(HostVO ssHost);
+    public HostVO pickSsvmHost(DataStore store);
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/02686583/server/src/com/cloud/template/HypervisorTemplateAdapter.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/template/HypervisorTemplateAdapter.java b/server/src/com/cloud/template/HypervisorTemplateAdapter.java
index 491900b..709288e 100755
--- a/server/src/com/cloud/template/HypervisorTemplateAdapter.java
+++ b/server/src/com/cloud/template/HypervisorTemplateAdapter.java
@@ -37,6 +37,7 @@ import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager;
 import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreRole;
 import org.apache.cloudstack.engine.subsystem.api.storage.ImageDataFactory;
 import org.apache.cloudstack.engine.subsystem.api.storage.ImageService;
+import org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope;
 import org.apache.cloudstack.framework.async.AsyncCallFuture;
 import org.apache.log4j.Logger;
 
@@ -50,6 +51,7 @@ import com.cloud.event.UsageEventUtils;
 import com.cloud.exception.InvalidParameterValueException;
 import com.cloud.exception.ResourceAllocationException;
 import com.cloud.host.HostVO;
+import com.cloud.storage.ScopeType;
 import com.cloud.storage.Storage.ImageFormat;
 import com.cloud.storage.Storage.TemplateType;
 import com.cloud.storage.TemplateProfile;
@@ -81,7 +83,7 @@ public class HypervisorTemplateAdapter extends TemplateAdapterBase implements Te
     public String getName() {
         return TemplateAdapterType.Hypervisor.getName();
     }
-	
+
 	private String validateUrl(String url) {
 		try {
 			URI uri = new URI(url);
@@ -164,23 +166,30 @@ public class HypervisorTemplateAdapter extends TemplateAdapterBase implements Te
 
 	@Override
 	public VMTemplateVO create(TemplateProfile profile) {
+	    // persist entry in vm_template, vm_template_details and template_zone_ref tables
 		VMTemplateVO template = persistTemplate(profile);
 
 		if (template == null) {
 			throw new CloudRuntimeException("Unable to persist the template " + profile.getTemplate());
 		}
 
-		DataStore imageStore = this.storeMgr.getDataStore(profile.getImageStoreId(), DataStoreRole.Image);
-		
-		AsyncCallFuture<CommandResult> future = this.imageService.createTemplateAsync(this.imageFactory.getTemplate(template.getId()), imageStore);
-		try {
-            future.get();
-        } catch (InterruptedException e) {
-            s_logger.debug("create template Failed", e);
-            throw new CloudRuntimeException("create template Failed", e);
-        } catch (ExecutionException e) {
-            s_logger.debug("create template Failed", e);
-            throw new CloudRuntimeException("create template Failed", e);
+		// find all eligible image stores for this zone scope
+		List<DataStore> imageStores = this.storeMgr.getImageStoresByScope(new ZoneScope(profile.getZoneId()));
+		if ( imageStores == null || imageStores.size() == 0 ){
+		    throw new CloudRuntimeException("Unable to find image store to download template "+ profile.getTemplate());
+		}
+        for (DataStore imageStore : imageStores) {
+            AsyncCallFuture<CommandResult> future = this.imageService
+                    .createTemplateAsync(this.imageFactory.getTemplate(template.getId()), imageStore);
+            try {
+                future.get();
+            } catch (InterruptedException e) {
+                s_logger.debug("create template Failed", e);
+                throw new CloudRuntimeException("create template Failed", e);
+            } catch (ExecutionException e) {
+                s_logger.debug("create template Failed", e);
+                throw new CloudRuntimeException("create template Failed", e);
+            }
         }
         _resourceLimitMgr.incrementResourceCount(profile.getAccountId(), ResourceType.template);
         _resourceLimitMgr.incrementResourceCount(profile.getAccountId(), ResourceType.secondary_storage,

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/02686583/server/src/com/cloud/template/TemplateAdapter.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/template/TemplateAdapter.java b/server/src/com/cloud/template/TemplateAdapter.java
index 1f8f491..766afa8 100755
--- a/server/src/com/cloud/template/TemplateAdapter.java
+++ b/server/src/com/cloud/template/TemplateAdapter.java
@@ -33,19 +33,19 @@ import com.cloud.utils.component.Adapter;
 public interface TemplateAdapter extends Adapter {
 	public static class TemplateAdapterType {
 		String _name;
-		
+
 		public static final TemplateAdapterType Hypervisor = new TemplateAdapterType("HypervisorAdapter");
 		public static final TemplateAdapterType BareMetal = new TemplateAdapterType("BareMetalAdapter");
-		
+
 		public TemplateAdapterType(String name) {
 			_name = name;
 		}
-		
+
 		public String getName() {
 			return _name;
 		}
 	}
-	
+
 	public TemplateProfile prepare(RegisterTemplateCmd cmd) throws ResourceAllocationException;
 
 	public TemplateProfile prepare(RegisterIsoCmd cmd) throws ResourceAllocationException;
@@ -57,14 +57,14 @@ public interface TemplateAdapter extends Adapter {
 	public TemplateProfile prepareDelete(DeleteIsoCmd cmd);
 
 	public boolean delete(TemplateProfile profile);
-	
+
 	public TemplateProfile prepare(boolean isIso, Long userId, String name, String displayText, Integer bits,
             Boolean passwordEnabled, Boolean requiresHVM, String url, Boolean isPublic, Boolean featured,
             Boolean isExtractable, String format, Long guestOSId, Long zoneId, HypervisorType hypervisorType,
             String accountName, Long domainId, String chksum, Boolean bootable, Map details) throws ResourceAllocationException;
-	
+
     public TemplateProfile prepare(boolean isIso, long userId, String name, String displayText, Integer bits,
             Boolean passwordEnabled, Boolean requiresHVM, String url, Boolean isPublic, Boolean featured,
             Boolean isExtractable, String format, Long guestOSId, Long zoneId, HypervisorType hypervisorType,
-            String chksum, Boolean bootable, String templateTag, Account templateOwner, Map details, Boolean sshKeyEnabled, String imageStoreUuid) throws ResourceAllocationException;	
+            String chksum, Boolean bootable, String templateTag, Account templateOwner, Map details, Boolean sshKeyEnabled) throws ResourceAllocationException;
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/02686583/server/src/com/cloud/template/TemplateAdapterBase.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/template/TemplateAdapterBase.java b/server/src/com/cloud/template/TemplateAdapterBase.java
index 1b11425..1e79b60 100755
--- a/server/src/com/cloud/template/TemplateAdapterBase.java
+++ b/server/src/com/cloud/template/TemplateAdapterBase.java
@@ -82,12 +82,12 @@ public abstract class TemplateAdapterBase extends AdapterBase implements Templat
 	protected @Inject ResourceLimitService _resourceLimitMgr;
 	protected @Inject DataStoreManager storeMgr;
 	@Inject TemplateManager templateMgr;
-	
+
 	@Override
 	public boolean stop() {
 		return true;
 	}
-	
+
 	private static boolean isAdmin(short accountType) {
 	    return ((accountType == Account.ACCOUNT_TYPE_ADMIN) ||
 	    		(accountType == Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN) ||
@@ -100,34 +100,24 @@ public abstract class TemplateAdapterBase extends AdapterBase implements Templat
             Boolean isExtractable, String format, Long guestOSId, Long zoneId, HypervisorType hypervisorType,
             String accountName, Long domainId, String chksum, Boolean bootable, Map details) throws ResourceAllocationException {
 	    return prepare(isIso, userId, name, displayText, bits, passwordEnabled, requiresHVM, url, isPublic, featured, isExtractable, format, guestOSId, zoneId, hypervisorType,
-	            chksum, bootable, null, null, details, false, null);
+	            chksum, bootable, null, null, details, false);
 	}
-	
+
 	public TemplateProfile prepare(boolean isIso, long userId, String name, String displayText, Integer bits,
 			Boolean passwordEnabled, Boolean requiresHVM, String url, Boolean isPublic, Boolean featured,
 			Boolean isExtractable, String format, Long guestOSId, Long zoneId, HypervisorType hypervisorType,
-			String chksum, Boolean bootable, String templateTag, Account templateOwner, Map details, Boolean sshkeyEnabled,
-			String imageStoreUuid) throws ResourceAllocationException {
-		//Long accountId = null;
+			String chksum, Boolean bootable, String templateTag, Account templateOwner, Map details, Boolean sshkeyEnabled) throws ResourceAllocationException {
+
 		// parameters verification
-		
-	    String storeUuid = imageStoreUuid;
-        if (storeUuid != null) {
-            DataStore store = this.storeMgr.getDataStore(storeUuid, DataStoreRole.Image);
-            if (store == null) {
-                throw new InvalidParameterValueException("invalide image store uuid" + storeUuid);
-            }
-            
-        }
-        
+
 		if (isPublic == null) {
 			isPublic = Boolean.FALSE;
 		}
-		
+
 		if (zoneId.longValue() == -1) {
 			zoneId = null;
 		}
-		
+
 		if (isIso) {
 	        if (bootable == null) {
 	        	bootable = Boolean.TRUE;
@@ -148,9 +138,9 @@ public abstract class TemplateAdapterBase extends AdapterBase implements Templat
 			}
 			if (requiresHVM == null) {
 				requiresHVM = true;
-			}	
+			}
 		}
-		
+
         if (isExtractable == null) {
             isExtractable = Boolean.FALSE;
         }
@@ -167,7 +157,7 @@ public abstract class TemplateAdapterBase extends AdapterBase implements Templat
 		if (url.toLowerCase().contains("file://")) {
 			throw new InvalidParameterValueException("File:// type urls are currently unsupported");
 		}
-		
+
 		boolean allowPublicUserTemplates = Boolean.parseBoolean(_configDao.getValue("allow.public.user.templates"));
 		if (!isAdmin && !allowPublicUserTemplates && isPublic) {
 			throw new InvalidParameterValueException("Only private templates/ISO can be created.");
@@ -176,24 +166,24 @@ public abstract class TemplateAdapterBase extends AdapterBase implements Templat
 		if (!isAdmin || featured == null) {
 			featured = Boolean.FALSE;
 		}
-		
+
 		ImageFormat imgfmt = ImageFormat.valueOf(format.toUpperCase());
 		if (imgfmt == null) {
 			throw new IllegalArgumentException("Image format is incorrect " + format + ". Supported formats are " + EnumUtils.listValues(ImageFormat.values()));
 		}
-         
+
         // Check that the resource limit for templates/ISOs won't be exceeded
         UserVO user = _userDao.findById(userId);
         if (user == null) {
             throw new IllegalArgumentException("Unable to find user with id " + userId);
         }
-        
+
         _resourceLimitMgr.checkResourceLimit(templateOwner, ResourceType.template);
-        
+
         if (templateOwner.getType() != Account.ACCOUNT_TYPE_ADMIN && zoneId == null) {
         	throw new IllegalArgumentException("Only admins can create templates in all zones");
         }
-        
+
         // If a zoneId is specified, make sure it is valid
         if (zoneId != null) {
         	DataCenterVO zone = _dcDao.findById(zoneId);
@@ -205,39 +195,33 @@ public abstract class TemplateAdapterBase extends AdapterBase implements Templat
     			throw new PermissionDeniedException("Cannot perform this operation, Zone is currently disabled: "+ zoneId );
     		}
         }
-       
+
         List<VMTemplateVO> systemvmTmplts = _tmpltDao.listAllSystemVMTemplates();
         for (VMTemplateVO template : systemvmTmplts) {
             if (template.getName().equalsIgnoreCase(name) || template.getDisplayText().equalsIgnoreCase(displayText)) {
                 throw new IllegalArgumentException("Cannot use reserved names for templates");
             }
         }
-        
-        DataStore imageStore = this.templateMgr.getImageStore(imageStoreUuid, zoneId);
-        if (imageStore == null) {
-            throw new IllegalArgumentException("Cann't find an image store");
-        }
-        Long imageStoreId = imageStore.getId();
-        
+
         Long id = _tmpltDao.getNextInSequence(Long.class, "id");
         UserContext.current().setEventDetails("Id: " +id+ " name: " + name);
 		return new TemplateProfile(id, userId, name, displayText, bits, passwordEnabled, requiresHVM, url, isPublic,
-				featured, isExtractable, imgfmt, guestOSId, zoneId, hypervisorType, templateOwner.getAccountName(), templateOwner.getDomainId(), templateOwner.getAccountId(), chksum, bootable, templateTag, details, sshkeyEnabled, imageStoreId);
+				featured, isExtractable, imgfmt, guestOSId, zoneId, hypervisorType, templateOwner.getAccountName(), templateOwner.getDomainId(), templateOwner.getAccountId(), chksum, bootable, templateTag, details, sshkeyEnabled);
 	}
-	
+
 	@Override
 	public TemplateProfile prepare(RegisterTemplateCmd cmd) throws ResourceAllocationException {
 	    //check if the caller can operate with the template owner
         Account caller = UserContext.current().getCaller();
         Account owner = _accountMgr.getAccount(cmd.getEntityOwnerId());
         _accountMgr.checkAccess(caller, null, true, owner);
-	    
 
-        
+
+
 		return prepare(false, UserContext.current().getCallerUserId(), cmd.getTemplateName(), cmd.getDisplayText(),
 				cmd.getBits(), cmd.isPasswordEnabled(), cmd.getRequiresHvm(), cmd.getUrl(), cmd.isPublic(), cmd.isFeatured(),
 				cmd.isExtractable(), cmd.getFormat(), cmd.getOsTypeId(), cmd.getZoneId(), HypervisorType.getType(cmd.getHypervisor()),
-				cmd.getChecksum(), true, cmd.getTemplateTag(), owner, cmd.getDetails(), cmd.isSshKeyEnabled(), cmd.getImageStoreUuid());
+				cmd.getChecksum(), true, cmd.getTemplateTag(), owner, cmd.getDetails(), cmd.isSshKeyEnabled());
 	}
 
 	public TemplateProfile prepare(RegisterIsoCmd cmd) throws ResourceAllocationException {
@@ -245,24 +229,23 @@ public abstract class TemplateAdapterBase extends AdapterBase implements Templat
 	    Account caller = UserContext.current().getCaller();
 	    Account owner = _accountMgr.getAccount(cmd.getEntityOwnerId());
 	    _accountMgr.checkAccess(caller, null, true, owner);
-	   
+
 		return prepare(true, UserContext.current().getCallerUserId(), cmd.getIsoName(), cmd.getDisplayText(), 64, false,
 					true, cmd.getUrl(), cmd.isPublic(), cmd.isFeatured(), cmd.isExtractable(), ImageFormat.ISO.toString(), cmd.getOsTypeId(),
-					cmd.getZoneId(), HypervisorType.None, cmd.getChecksum(), cmd.isBootable(), null, owner, null, false, cmd.getImageStoreUuid());
+					cmd.getZoneId(), HypervisorType.None, cmd.getChecksum(), cmd.isBootable(), null, owner, null, false);
 	}
-	
+
 	protected VMTemplateVO persistTemplate(TemplateProfile profile) {
 		Long zoneId = profile.getZoneId();
 		VMTemplateVO template = new VMTemplateVO(profile.getTemplateId(), profile.getName(), profile.getFormat(), profile.getIsPublic(),
 				profile.getFeatured(), profile.getIsExtractable(), TemplateType.USER, profile.getUrl(), profile.getRequiresHVM(),
 				profile.getBits(), profile.getAccountId(), profile.getCheckSum(), profile.getDisplayText(),
-				profile.getPasswordEnabled(), profile.getGuestOsId(), profile.getBootable(), profile.getHypervisorType(), profile.getTemplateTag(), 
+				profile.getPasswordEnabled(), profile.getGuestOsId(), profile.getBootable(), profile.getHypervisorType(), profile.getTemplateTag(),
 				profile.getDetails(), profile.getSshKeyEnabled());
 
-		template.setImageDataStoreId(profile.getImageStoreId());
 		if (zoneId == null || zoneId.longValue() == -1) {
             List<DataCenterVO> dcs = _dcDao.listAll();
-            
+
             if (dcs.isEmpty()) {
             	throw new CloudRuntimeException("No zones are present in the system, can't add template");
             }
@@ -271,13 +254,13 @@ public abstract class TemplateAdapterBase extends AdapterBase implements Templat
         	for (DataCenterVO dc: dcs) {
     			_tmpltDao.addTemplateToZone(template, dc.getId());
     		}
-        	
+
         } else {
 			_tmpltDao.addTemplateToZone(template, zoneId);
         }
 		return _tmpltDao.findById(template.getId());
 	}
-	
+
 
 	private Long accountAndUserValidation(Account account, long userId, UserVmVO vmInstanceCheck, VMTemplateVO template, String msg)
 			throws PermissionDeniedException {
@@ -311,7 +294,7 @@ public abstract class TemplateAdapterBase extends AdapterBase implements Templat
 
 		return userId;
 	}
-	
+
 	public TemplateProfile prepareDelete(DeleteTemplateCmd cmd) {
 		Long templateId = cmd.getId();
 		Long userId = UserContext.current().getCallerUserId();
@@ -342,23 +325,23 @@ public abstract class TemplateAdapterBase extends AdapterBase implements Templat
         Long userId = UserContext.current().getCallerUserId();
         Account account = UserContext.current().getCaller();
         Long zoneId = cmd.getZoneId();
-        
+
         VMTemplateVO template = _tmpltDao.findById(templateId.longValue());
         if (template == null) {
             throw new InvalidParameterValueException("unable to find iso with id " + templateId);
         }
-        
+
         userId = accountAndUserValidation(account, userId, null, template, "Unable to delete iso " );
-        
+
     	UserVO user = _userDao.findById(userId);
     	if (user == null) {
     		throw new InvalidParameterValueException("Please specify a valid user.");
     	}
-    	
+
     	if (template.getFormat() != ImageFormat.ISO) {
     		throw new InvalidParameterValueException("Please specify a valid iso.");
     	}
- 	
+
     	return new TemplateProfile(userId, template, zoneId);
 	}
 


Mime
View raw message