cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mc...@apache.org
Subject [3/4] Refactor DownloadMonitorImpl code, move some functionalities to TemplateServiceImpl and VolumeServiceImpl.
Date Fri, 12 Apr 2013 01:10:13 GMT
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/bb646727/engine/storage/src/org/apache/cloudstack/storage/image/db/TemplateDataStoreDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/storage/src/org/apache/cloudstack/storage/image/db/TemplateDataStoreDaoImpl.java b/engine/storage/src/org/apache/cloudstack/storage/image/db/TemplateDataStoreDaoImpl.java
index a429dc9..e34a1ab 100644
--- a/engine/storage/src/org/apache/cloudstack/storage/image/db/TemplateDataStoreDaoImpl.java
+++ b/engine/storage/src/org/apache/cloudstack/storage/image/db/TemplateDataStoreDaoImpl.java
@@ -30,6 +30,7 @@ import org.apache.log4j.Logger;
 import org.springframework.stereotype.Component;
 
 import com.cloud.storage.VMTemplateHostVO;
+import com.cloud.storage.VMTemplateStorageResourceAssoc.Status;
 import com.cloud.utils.db.GenericDaoBase;
 import com.cloud.utils.db.SearchBuilder;
 import com.cloud.utils.db.SearchCriteria;
@@ -42,23 +43,20 @@ public class TemplateDataStoreDaoImpl extends GenericDaoBase<TemplateDataStoreVO
     private static final Logger s_logger = Logger.getLogger(TemplateDataStoreDaoImpl.class);
     private SearchBuilder<TemplateDataStoreVO> updateStateSearch;
     private SearchBuilder<TemplateDataStoreVO> storeSearch;
-    private SearchBuilder<TemplateDataStoreVO> liveStoreSearch;
     private SearchBuilder<TemplateDataStoreVO> storeTemplateStateSearch;
+    private SearchBuilder<TemplateDataStoreVO> storeTemplateDownloadStatusSearch;
     private SearchBuilder<TemplateDataStoreVO> storeTemplateSearch;
 
     @Override
     public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
     	super.configure(name, params);
 
+
         storeSearch = createSearchBuilder();
         storeSearch.and("store_id", storeSearch.entity().getDataStoreId(), SearchCriteria.Op.EQ);
+        storeSearch.and("destroyed", storeSearch.entity().getDestroyed(), SearchCriteria.Op.EQ);
         storeSearch.done();
 
-        liveStoreSearch = createSearchBuilder();
-        liveStoreSearch.and("store_id", liveStoreSearch.entity().getDataStoreId(), SearchCriteria.Op.EQ);
-        liveStoreSearch.and("destroyed", liveStoreSearch.entity().getDestroyed(), SearchCriteria.Op.EQ);
-        liveStoreSearch.done();
-
     	updateStateSearch = this.createSearchBuilder();
         updateStateSearch.and("id", updateStateSearch.entity().getId(), Op.EQ);
         updateStateSearch.and("state", updateStateSearch.entity().getState(), Op.EQ);
@@ -72,6 +70,14 @@ public class TemplateDataStoreDaoImpl extends GenericDaoBase<TemplateDataStoreVO
         storeTemplateStateSearch.and("destroyed", storeTemplateStateSearch.entity().getDestroyed(), SearchCriteria.Op.EQ);
         storeTemplateStateSearch.done();
 
+        storeTemplateDownloadStatusSearch = createSearchBuilder();
+        storeTemplateDownloadStatusSearch.and("template_id", storeTemplateDownloadStatusSearch.entity().getTemplateId(), SearchCriteria.Op.EQ);
+        storeTemplateDownloadStatusSearch.and("store_id", storeTemplateDownloadStatusSearch.entity().getDataStoreId(), SearchCriteria.Op.EQ);
+        storeTemplateDownloadStatusSearch.and("downloadState", storeTemplateDownloadStatusSearch.entity().getDownloadState(), SearchCriteria.Op.IN);
+        storeTemplateDownloadStatusSearch.and("destroyed", storeTemplateDownloadStatusSearch.entity().getDestroyed(), SearchCriteria.Op.EQ);
+        storeTemplateDownloadStatusSearch.done();
+
+
         storeTemplateSearch = createSearchBuilder();
         storeTemplateSearch.and("store_id", storeTemplateSearch.entity().getDataStoreId(), SearchCriteria.Op.EQ);
         storeTemplateSearch.and("template_id", storeTemplateSearch.entity().getTemplateId(), SearchCriteria.Op.EQ);
@@ -118,16 +124,11 @@ public class TemplateDataStoreDaoImpl extends GenericDaoBase<TemplateDataStoreVO
         return rows > 0;
     }
 
+
     @Override
     public List<TemplateDataStoreVO> listByStoreId(long id) {
         SearchCriteria<TemplateDataStoreVO> sc = storeSearch.create();
         sc.setParameters("store_id", id);
-        return listIncludingRemovedBy(sc);
-    }
-    @Override
-    public List<TemplateDataStoreVO> listLiveByStoreId(long id) {
-        SearchCriteria<TemplateDataStoreVO> sc = liveStoreSearch.create();
-        sc.setParameters("store_id", id);
         sc.setParameters("destroyed", false);
         return listIncludingRemovedBy(sc);
     }
@@ -152,6 +153,17 @@ public class TemplateDataStoreDaoImpl extends GenericDaoBase<TemplateDataStoreVO
         return search(sc, null);
     }
 
+
+
+    @Override
+    public List<TemplateDataStoreVO> listByTemplateStoreDownloadStatus(long templateId, long storeId, Status... status) {
+        SearchCriteria<TemplateDataStoreVO> sc = storeTemplateStateSearch.create();
+        sc.setParameters("template_id", templateId);
+        sc.setParameters("store_id", storeId);
+        sc.setParameters("downloadState", (Object[])status);
+        return search(sc, null);
+    }
+
     @Override
     public TemplateDataStoreVO findByStoreTemplate(long storeId, long templateId) {
         SearchCriteria<TemplateDataStoreVO> sc = storeTemplateSearch.create();

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/bb646727/engine/storage/src/org/apache/cloudstack/storage/image/db/VolumeDataStoreDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/storage/src/org/apache/cloudstack/storage/image/db/VolumeDataStoreDaoImpl.java b/engine/storage/src/org/apache/cloudstack/storage/image/db/VolumeDataStoreDaoImpl.java
index 9a93f98..dc6c235 100644
--- a/engine/storage/src/org/apache/cloudstack/storage/image/db/VolumeDataStoreDaoImpl.java
+++ b/engine/storage/src/org/apache/cloudstack/storage/image/db/VolumeDataStoreDaoImpl.java
@@ -31,6 +31,7 @@ import org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO;
 import org.apache.log4j.Logger;
 import org.springframework.stereotype.Component;
 
+import com.cloud.storage.VolumeHostVO;
 import com.cloud.utils.db.GenericDaoBase;
 import com.cloud.utils.db.SearchBuilder;
 import com.cloud.utils.db.SearchCriteria;
@@ -42,21 +43,30 @@ import com.cloud.utils.db.UpdateBuilder;
 public class VolumeDataStoreDaoImpl extends GenericDaoBase<VolumeDataStoreVO, Long> implements VolumeDataStoreDao {
     private static final Logger s_logger = Logger.getLogger(VolumeDataStoreDaoImpl.class);
     private SearchBuilder<VolumeDataStoreVO> updateStateSearch;
+    private SearchBuilder<VolumeDataStoreVO> volumeSearch;
     private SearchBuilder<VolumeDataStoreVO> storeSearch;
-    private SearchBuilder<VolumeDataStoreVO> liveStoreSearch;
+    private SearchBuilder<VolumeDataStoreVO> storeVolumeSearch;
 
     @Override
     public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
     	super.configure(name, params);
 
+
         storeSearch = createSearchBuilder();
         storeSearch.and("store_id", storeSearch.entity().getDataStoreId(), SearchCriteria.Op.EQ);
+        storeSearch.and("destroyed", storeSearch.entity().getDestroyed(), SearchCriteria.Op.EQ);
         storeSearch.done();
 
-        liveStoreSearch = createSearchBuilder();
-        liveStoreSearch.and("store_id", liveStoreSearch.entity().getDataStoreId(), SearchCriteria.Op.EQ);
-        liveStoreSearch.and("destroyed", liveStoreSearch.entity().getDestroyed(), SearchCriteria.Op.EQ);
-        liveStoreSearch.done();
+        volumeSearch = createSearchBuilder();
+        volumeSearch.and("volume_id", volumeSearch.entity().getVolumeId(), SearchCriteria.Op.EQ);
+        volumeSearch.and("destroyed", volumeSearch.entity().getDestroyed(), SearchCriteria.Op.EQ);
+        volumeSearch.done();
+
+        storeVolumeSearch = createSearchBuilder();
+        storeVolumeSearch.and("store_id", storeVolumeSearch.entity().getDataStoreId(), SearchCriteria.Op.EQ);
+        storeVolumeSearch.and("volume_id", storeVolumeSearch.entity().getVolumeId(), SearchCriteria.Op.EQ);
+        storeVolumeSearch.and("destroyed", storeVolumeSearch.entity().getDestroyed(), SearchCriteria.Op.EQ);
+        storeVolumeSearch.done();
 
         updateStateSearch = this.createSearchBuilder();
         updateStateSearch.and("id", updateStateSearch.entity().getId(), Op.EQ);
@@ -102,16 +112,11 @@ public class VolumeDataStoreDaoImpl extends GenericDaoBase<VolumeDataStoreVO, Lo
         return rows > 0;
     }
 
+
     @Override
     public List<VolumeDataStoreVO> listByStoreId(long id) {
         SearchCriteria<VolumeDataStoreVO> sc = storeSearch.create();
         sc.setParameters("store_id", id);
-        return listIncludingRemovedBy(sc);
-    }
-    @Override
-    public List<VolumeDataStoreVO> listLiveByStoreId(long id) {
-        SearchCriteria<VolumeDataStoreVO> sc = liveStoreSearch.create();
-        sc.setParameters("store_id", id);
         sc.setParameters("destroyed", false);
         return listIncludingRemovedBy(sc);
     }
@@ -125,4 +130,23 @@ public class VolumeDataStoreDaoImpl extends GenericDaoBase<VolumeDataStoreVO, Lo
         remove(sc);
         txn.commit();
     }
+
+
+    @Override
+    public VolumeDataStoreVO findByVolumeId(long volumeId) {
+        SearchCriteria<VolumeDataStoreVO> sc = volumeSearch.create();
+        sc.setParameters("volume_id", volumeId);
+        sc.setParameters("destroyed", false);
+        return findOneBy(sc);
+    }
+    @Override
+    public VolumeDataStoreVO findByStoreVolume(long storeId, long volumeId) {
+        SearchCriteria<VolumeDataStoreVO> sc = storeVolumeSearch.create();
+        sc.setParameters("store_id", storeId);
+        sc.setParameters("volume_id", volumeId);
+        sc.setParameters("destroyed", false);
+        return findOneBy(sc);
+    }
+
+
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/bb646727/engine/storage/volume/src/org/apache/cloudstack/storage/volume/VolumeObject.java
----------------------------------------------------------------------
diff --git a/engine/storage/volume/src/org/apache/cloudstack/storage/volume/VolumeObject.java b/engine/storage/volume/src/org/apache/cloudstack/storage/volume/VolumeObject.java
index cde576b..766d219 100644
--- a/engine/storage/volume/src/org/apache/cloudstack/storage/volume/VolumeObject.java
+++ b/engine/storage/volume/src/org/apache/cloudstack/storage/volume/VolumeObject.java
@@ -318,6 +318,10 @@ public class VolumeObject implements VolumeInfo {
        return this.payload;
     }
 
+    public VolumeVO getVolume(){
+        return this.volumeVO;
+    }
+
 	@Override
 	public HypervisorType getHypervisorType() {
 		return this.volumeDao.getHypervisorType(this.volumeVO.getId());

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/bb646727/engine/storage/volume/src/org/apache/cloudstack/storage/volume/VolumeServiceImpl.java
----------------------------------------------------------------------
diff --git a/engine/storage/volume/src/org/apache/cloudstack/storage/volume/VolumeServiceImpl.java b/engine/storage/volume/src/org/apache/cloudstack/storage/volume/VolumeServiceImpl.java
index 2b43ade..2da16c1 100644
--- a/engine/storage/volume/src/org/apache/cloudstack/storage/volume/VolumeServiceImpl.java
+++ b/engine/storage/volume/src/org/apache/cloudstack/storage/volume/VolumeServiceImpl.java
@@ -18,6 +18,11 @@
  */
 package org.apache.cloudstack.storage.volume;
 
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+
 import javax.inject.Inject;
 
 import org.apache.cloudstack.engine.cloud.entity.api.VolumeEntity;
@@ -41,19 +46,37 @@ import org.apache.cloudstack.storage.datastore.DataObjectManager;
 import org.apache.cloudstack.storage.datastore.ObjectInDataStoreManager;
 import org.apache.cloudstack.storage.datastore.PrimaryDataStore;
 import org.apache.cloudstack.storage.datastore.PrimaryDataStoreProviderManager;
+import org.apache.cloudstack.storage.datastore.db.VolumeDataStoreDao;
+import org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO;
 import org.apache.log4j.Logger;
 import org.springframework.stereotype.Component;
 
+import com.cloud.agent.AgentManager;
+import com.cloud.agent.api.Answer;
+import com.cloud.agent.api.storage.DeleteVolumeCommand;
+import com.cloud.agent.api.storage.ListVolumeAnswer;
+import com.cloud.agent.api.storage.ListVolumeCommand;
+import com.cloud.alert.AlertManager;
 import com.cloud.configuration.Config;
 import com.cloud.configuration.dao.ConfigurationDao;
+import com.cloud.exception.AgentUnavailableException;
 import com.cloud.exception.ConcurrentOperationException;
+import com.cloud.exception.ResourceAllocationException;
+import com.cloud.host.HostVO;
 import com.cloud.storage.StoragePool;
 import com.cloud.storage.Volume;
+import com.cloud.storage.VMTemplateStorageResourceAssoc.Status;
 import com.cloud.storage.Volume.Type;
 import com.cloud.storage.VolumeVO;
 import com.cloud.storage.dao.VolumeDao;
+import com.cloud.storage.download.DownloadMonitor;
+import com.cloud.storage.secondary.SecondaryStorageVmManager;
 import com.cloud.storage.snapshot.SnapshotManager;
+import com.cloud.storage.template.TemplateProp;
+import com.cloud.user.AccountManager;
+import com.cloud.user.ResourceLimitService;
 import com.cloud.utils.NumbersUtil;
+import com.cloud.utils.UriUtils;
 import com.cloud.utils.db.DB;
 import com.cloud.vm.VirtualMachine;
 import com.cloud.vm.dao.VMInstanceDao;
@@ -77,13 +100,29 @@ public class VolumeServiceImpl implements VolumeService {
     @Inject
     VolumeDataFactory volFactory;
     @Inject SnapshotManager snapshotMgr;
+    @Inject
+    ResourceLimitService _resourceLimitMgr;
+    @Inject
+    DownloadMonitor _dlMonitor;
+    @Inject
+    AccountManager _accountMgr;
+    @Inject
+    AlertManager _alertMgr;
     @Inject VMInstanceDao vmDao;
     @Inject
     ConfigurationDao configDao;
+    @Inject
+    SecondaryStorageVmManager _ssvmMgr;
+    @Inject
+    AgentManager _agentMgr;
+    @Inject
+    VolumeDataStoreDao _volumeStoreDao;
+    @Inject
+    VolumeDao _volumeDao;
 
     public VolumeServiceImpl() {
     }
-    
+
     private class CreateVolumeContext<T> extends AsyncRpcConext<T> {
 
         private DataObject volume;
@@ -96,17 +135,17 @@ public class VolumeServiceImpl implements VolumeService {
             this.volume = volume;
             this.future = future;
         }
-        
+
         public DataObject getVolume() {
             return this.volume;
         }
-        
+
         public AsyncCallFuture<VolumeApiResult> getFuture() {
             return this.future;
         }
-        
+
     }
-    
+
     @Override
     public AsyncCallFuture<VolumeApiResult> createVolumeAsync(VolumeInfo volume, DataStore dataStore) {
         AsyncCallFuture<VolumeApiResult> future = new AsyncCallFuture<VolumeApiResult>();
@@ -117,11 +156,11 @@ public class VolumeServiceImpl implements VolumeService {
         AsyncCallbackDispatcher<VolumeServiceImpl, CreateCmdResult> caller = AsyncCallbackDispatcher.create(this);
         caller.setCallback(caller.getTarget().createVolumeCallback(null, null))
         .setContext(context);
-        
+
         dataStore.getDriver().createAsync(volumeOnStore, caller);
         return future;
     }
-    
+
     protected Void createVolumeCallback(AsyncCallbackDispatcher<VolumeServiceImpl, CreateCmdResult> callback, CreateVolumeContext<VolumeApiResult> context) {
         CreateCmdResult result = callback.getResult();
         DataObject vo = context.getVolume();
@@ -139,7 +178,7 @@ public class VolumeServiceImpl implements VolumeService {
         context.getFuture().complete(volResult);
         return null;
     }
-    
+
     private class DeleteVolumeContext<T> extends AsyncRpcConext<T> {
         private final VolumeObject volume;
         private AsyncCallFuture<VolumeApiResult> future;
@@ -151,11 +190,11 @@ public class VolumeServiceImpl implements VolumeService {
             this.volume = volume;
             this.future = future;
         }
-        
+
         public VolumeObject getVolume() {
             return this.volume;
         }
-        
+
         public AsyncCallFuture<VolumeApiResult> getFuture() {
             return this.future;
         }
@@ -171,7 +210,7 @@ public class VolumeServiceImpl implements VolumeService {
             future.complete(result);
             return future;
         }
-        
+
         String vmName = null;
         VolumeVO vol = this.volDao.findById(volume.getId());
         if (vol.getVolumeType() == Type.ROOT && vol.getInstanceId() != null) {
@@ -194,19 +233,19 @@ public class VolumeServiceImpl implements VolumeService {
             return future;
         }
         VolumeObject vo = (VolumeObject)volume;
-      
+
         volume.processEvent(Event.ExpungeRequested);
-      
-        
+
+
         DeleteVolumeContext<VolumeApiResult> context = new DeleteVolumeContext<VolumeApiResult>(null, vo, future);
         AsyncCallbackDispatcher<VolumeServiceImpl, CommandResult> caller = AsyncCallbackDispatcher.create(this);
         caller.setCallback(caller.getTarget().deleteVolumeCallback(null, null))
             .setContext(context);
-        
+
         volume.getDataStore().getDriver().deleteAsync(volume, caller);
         return future;
     }
-    
+
     public Void deleteVolumeCallback(AsyncCallbackDispatcher<VolumeServiceImpl, CommandResult> callback, DeleteVolumeContext<VolumeApiResult> context) {
         CommandResult result = callback.getResult();
         VolumeObject vo = context.getVolume();
@@ -249,7 +288,7 @@ public class VolumeServiceImpl implements VolumeService {
         private final TemplateInfo srcTemplate;
         private final AsyncCallFuture<VolumeApiResult> future;
         final DataObject destObj;
-        public CreateBaseImageContext(AsyncCompletionCallback<T> callback, VolumeInfo volume, PrimaryDataStore datastore, 
+        public CreateBaseImageContext(AsyncCompletionCallback<T> callback, VolumeInfo volume, PrimaryDataStore datastore,
                 TemplateInfo srcTemplate,
                 AsyncCallFuture<VolumeApiResult> future, DataObject destObj) {
             super(callback);
@@ -259,11 +298,11 @@ public class VolumeServiceImpl implements VolumeService {
             this.srcTemplate = srcTemplate;
             this.destObj = destObj;
         }
-        
+
         public VolumeInfo getVolume() {
             return this.volume;
         }
-        
+
         public PrimaryDataStore getDataStore() {
             return this.dataStore;
         }
@@ -271,13 +310,13 @@ public class VolumeServiceImpl implements VolumeService {
         public TemplateInfo getSrcTemplate() {
             return this.srcTemplate;
         }
-        
+
         public AsyncCallFuture<VolumeApiResult> getFuture() {
             return this.future;
         }
-        
+
     }
-    
+
     static class CreateBaseImageResult extends CommandResult {
         final TemplateInfo template;
         public CreateBaseImageResult(TemplateInfo template) {
@@ -285,7 +324,7 @@ public class VolumeServiceImpl implements VolumeService {
             this.template = template;
         }
     }
-    
+
     private TemplateInfo waitForTemplateDownloaded(PrimaryDataStore store, TemplateInfo template) {
     	int storagePoolMaxWaitSeconds = NumbersUtil.parseInt(configDao.getValue(Config.StoragePoolMaxWaitSeconds.key()), 3600);
     	int sleepTime = 120;
@@ -306,16 +345,16 @@ public class VolumeServiceImpl implements VolumeService {
     }
     @DB
     protected void createBaseImageAsync(VolumeInfo volume, PrimaryDataStore dataStore, TemplateInfo template, AsyncCallFuture<VolumeApiResult> future) {
-       
+
         DataObject templateOnPrimaryStoreObj = dataStore.create(template);
-        CreateBaseImageContext<CreateCmdResult> context = new CreateBaseImageContext<CreateCmdResult>(null, volume, 
+        CreateBaseImageContext<CreateCmdResult> context = new CreateBaseImageContext<CreateCmdResult>(null, volume,
                 dataStore,
                 template,
                 future, templateOnPrimaryStoreObj);
         AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> caller = AsyncCallbackDispatcher.create(this);
         caller.setCallback(caller.getTarget().copyBaseImageCallback(null, null))
         .setContext(context);
-        
+
         try {
          	templateOnPrimaryStoreObj.processEvent(Event.CreateOnlyRequested);
         } catch (Exception e) {
@@ -330,7 +369,7 @@ public class VolumeServiceImpl implements VolumeService {
         		}
         	}
         }
-        
+
         try {
             motionSrv.copyAsync(template, templateOnPrimaryStoreObj, caller);
         } catch (Exception e) {
@@ -342,12 +381,12 @@ public class VolumeServiceImpl implements VolumeService {
         }
         return;
     }
-    
+
     @DB
     protected Void copyBaseImageCallback(AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> callback, CreateBaseImageContext<VolumeApiResult> context) {
         CopyCommandResult result = callback.getResult();
         VolumeApiResult res = new VolumeApiResult(context.getVolume());
-        
+
         AsyncCallFuture<VolumeApiResult> future = context.getFuture();
         DataObject templateOnPrimaryStoreObj = context.destObj;
         if (!result.isSuccess()) {
@@ -356,18 +395,18 @@ public class VolumeServiceImpl implements VolumeService {
             future.complete(res);
             return null;
         }
-        
+
         templateOnPrimaryStoreObj.processEvent(Event.OperationSuccessed);
         createVolumeFromBaseImageAsync(context.volume, templateOnPrimaryStoreObj, context.dataStore, future);
         return null;
     }
-    
+
     private class CreateVolumeFromBaseImageContext<T> extends AsyncRpcConext<T> {
         private final VolumeObject vo;
         private final AsyncCallFuture<VolumeApiResult> future;
         private final DataStore primaryStore;
         private final DataObject templateOnStore;
-        public CreateVolumeFromBaseImageContext(AsyncCompletionCallback<T> callback, VolumeObject vo, 
+        public CreateVolumeFromBaseImageContext(AsyncCompletionCallback<T> callback, VolumeObject vo,
                 DataStore primaryStore,
                 DataObject templateOnStore,
                 AsyncCallFuture<VolumeApiResult> future) {
@@ -377,13 +416,13 @@ public class VolumeServiceImpl implements VolumeService {
             this.primaryStore = primaryStore;
             this.templateOnStore = templateOnStore;
         }
-        
+
 
         public AsyncCallFuture<VolumeApiResult> getFuture() {
             return this.future;
         }
     }
-    
+
     @DB
     protected void createVolumeFromBaseImageAsync(VolumeInfo volume, DataObject templateOnPrimaryStore, PrimaryDataStore pd, AsyncCallFuture<VolumeApiResult> future) {
         VolumeObject vo = (VolumeObject)volume;
@@ -398,23 +437,23 @@ public class VolumeServiceImpl implements VolumeService {
         motionSrv.copyAsync(context.templateOnStore, volumeOnPrimaryStorage, caller);
         return;
     }
-    
+
     @DB
     public Void copyBaseImageCallBack(AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> callback, CreateVolumeFromBaseImageContext<VolumeApiResult> context) {
         VolumeObject vo = context.vo;
         CopyCommandResult result = callback.getResult();
         VolumeApiResult volResult = new VolumeApiResult(vo);
-       
+
         if (result.isSuccess()) {
             if (result.getPath() != null) {
                 vo.setPath(result.getPath());
             }
-            vo.processEvent(Event.OperationSuccessed); 
+            vo.processEvent(Event.OperationSuccessed);
         } else {
             vo.processEvent(Event.OperationFailed);
             volResult.setResult(result.getResult());
         }
-        
+
         AsyncCallFuture<VolumeApiResult> future = context.getFuture();
         future.complete(volResult);
         return null;
@@ -427,12 +466,12 @@ public class VolumeServiceImpl implements VolumeService {
         TemplateInfo templateOnPrimaryStore = pd.getTemplate(template.getId());
         AsyncCallFuture<VolumeApiResult> future = new AsyncCallFuture<VolumeApiResult>();
         VolumeApiResult result = new VolumeApiResult(volume);
-        
+
         if (templateOnPrimaryStore == null) {
             createBaseImageAsync(volume, pd, template, future);
             return future;
         }
-            
+
         createVolumeFromBaseImageAsync(volume, templateOnPrimaryStore, pd, future);
         return future;
     }
@@ -441,13 +480,13 @@ public class VolumeServiceImpl implements VolumeService {
     @DB
     public boolean destroyVolume(long volumeId)
             throws ConcurrentOperationException {
-        
+
         VolumeInfo vol = this.volFactory.getVolume(volumeId);
         vol.processEvent(Event.DestroyRequested);
         this.snapshotMgr.deletePoliciesForVolume(volumeId);
 
         vol.processEvent(Event.OperationSuccessed);
-             
+
         return true;
     }
 
@@ -455,11 +494,11 @@ public class VolumeServiceImpl implements VolumeService {
     public AsyncCallFuture<VolumeApiResult> createVolumeFromSnapshot(
             VolumeInfo volume, DataStore store, SnapshotInfo snapshot) {
         AsyncCallFuture<VolumeApiResult> future = new AsyncCallFuture<VolumeApiResult>();
-        
+
         try {
         	DataObject volumeOnStore = store.create(volume);
         	volume.processEvent(Event.CreateOnlyRequested);
-        	 CreateVolumeFromBaseImageContext<VolumeApiResult> context = new CreateVolumeFromBaseImageContext<VolumeApiResult>(null, 
+        	 CreateVolumeFromBaseImageContext<VolumeApiResult> context = new CreateVolumeFromBaseImageContext<VolumeApiResult>(null,
         			 (VolumeObject)volume, store, volumeOnStore, future);
              AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> caller =  AsyncCallbackDispatcher.create(this);
              caller.setCallback(caller.getTarget().createVolumeFromSnapshotCallback(null, null))
@@ -471,11 +510,11 @@ public class VolumeServiceImpl implements VolumeService {
         	result.setResult(e.toString());
         	future.complete(result);
         }
-        
+
         return future;
     }
-    
-    protected Void createVolumeFromSnapshotCallback(AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> callback, 
+
+    protected Void createVolumeFromSnapshotCallback(AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> callback,
     		CreateVolumeFromBaseImageContext<VolumeApiResult> context) {
     	CopyCommandResult result = callback.getResult();
     	VolumeInfo volume = context.vo;
@@ -487,19 +526,19 @@ public class VolumeServiceImpl implements VolumeService {
     	} else {
     		event = Event.OperationSuccessed;
     	}
-    	
+
     	try {
     		volume.processEvent(event);
     	} catch (Exception e) {
     		s_logger.debug("create volume from snapshot failed", e);
     		apiResult.setResult(e.toString());
     	}
-    	
+
     	AsyncCallFuture<VolumeApiResult> future = context.future;
     	future.complete(apiResult);
     	return null;
     }
-    
+
     protected VolumeVO duplicateVolumeOnAnotherStorage(Volume volume, StoragePool pool) {
         Long lastPoolId = volume.getPoolId();
         VolumeVO newVol = new VolumeVO(volume);
@@ -511,7 +550,7 @@ public class VolumeServiceImpl implements VolumeService {
         newVol.setPodId(pool.getPodId());
         return this.volDao.persist(newVol);
     }
-    
+
 
     private class CopyVolumeContext<T> extends AsyncRpcConext<T> {
         final VolumeInfo srcVolume;
@@ -529,7 +568,7 @@ public class VolumeServiceImpl implements VolumeService {
             this.destStore = destStore;
             this.future = future;
         }
-        
+
     }
     @Override
     public AsyncCallFuture<VolumeApiResult> copyVolume(VolumeInfo srcVolume,
@@ -540,7 +579,7 @@ public class VolumeServiceImpl implements VolumeService {
             if (!this.snapshotMgr.canOperateOnVolume(srcVolume)) {
                 s_logger.debug(
                         "There are snapshots creating on this volume, can not move this volume");
-                
+
                 res.setResult("There are snapshots creating on this volume, can not move this volume");
                 future.complete(res);
                 return future;
@@ -551,7 +590,7 @@ public class VolumeServiceImpl implements VolumeService {
             destVolume.processEvent(Event.CreateOnlyRequested);
             srcVolume.processEvent(Event.CopyingRequested);
 
-            CopyVolumeContext<VolumeApiResult> context = new CopyVolumeContext<VolumeApiResult>(null, future, srcVolume, 
+            CopyVolumeContext<VolumeApiResult> context = new CopyVolumeContext<VolumeApiResult>(null, future, srcVolume,
                     destVolume,
                     destStore);
             AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> caller = AsyncCallbackDispatcher.create(this);
@@ -565,7 +604,7 @@ public class VolumeServiceImpl implements VolumeService {
         }
         return future;
     }
-    
+
     protected Void copyVolumeCallBack(AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> callback, CopyVolumeContext<VolumeApiResult> context) {
         VolumeInfo srcVolume = context.srcVolume;
         VolumeInfo destVolume = context.destVolume;
@@ -593,13 +632,13 @@ public class VolumeServiceImpl implements VolumeService {
             res.setResult(e.toString());
             future.complete(res);
         }
-        
+
         return null;
     }
-    
+
     @Override
     public AsyncCallFuture<VolumeApiResult> registerVolume(VolumeInfo volume, DataStore store) {
-        
+
         AsyncCallFuture<VolumeApiResult> future = new AsyncCallFuture<VolumeApiResult>();
         VolumeObject vo = (VolumeObject) volume;
 
@@ -607,11 +646,11 @@ public class VolumeServiceImpl implements VolumeService {
         AsyncCallbackDispatcher<VolumeServiceImpl, CreateCmdResult> caller = AsyncCallbackDispatcher.create(this);
         caller.setCallback(caller.getTarget().registerVolumeCallback(null, null))
         .setContext(context);
-        
+
         dataObjectMgr.createAsync(volume, store, caller, true);
         return future;
     }
-    
+
     protected Void registerVolumeCallback(AsyncCallbackDispatcher<VolumeServiceImpl, CreateCmdResult> callback, CreateVolumeContext<VolumeApiResult> context) {
         CreateCmdResult result = callback.getResult();
         VolumeObject vo = (VolumeObject)context.volume;
@@ -624,7 +663,7 @@ public class VolumeServiceImpl implements VolumeService {
         context.future.complete(res);
         return null;
     }
-    
+
 
 	@Override
 	public AsyncCallFuture<VolumeApiResult> resize(VolumeInfo volume) {
@@ -645,12 +684,12 @@ public class VolumeServiceImpl implements VolumeService {
 		volume.getDataStore().getDriver().resize(volume, caller);
 		return future;
 	}
-	
+
 	protected Void resizeVolumeCallback(AsyncCallbackDispatcher<VolumeServiceImpl, CreateCmdResult> callback, CreateVolumeContext<VolumeApiResult> context) {
 		CreateCmdResult result = callback.getResult();
 		AsyncCallFuture<VolumeApiResult> future  = context.future;
 		VolumeInfo volume = (VolumeInfo)context.volume;
-		
+
 		if (result.isFailed()) {
 			try {
 				volume.processEvent(Event.OperationFailed);
@@ -662,7 +701,7 @@ public class VolumeServiceImpl implements VolumeService {
 			future.complete(res);
 			return null;
 		}
-		
+
 		try {
 			volume.processEvent(Event.OperationSuccessed);
 		} catch(Exception e) {
@@ -672,13 +711,137 @@ public class VolumeServiceImpl implements VolumeService {
 			future.complete(res);
 			return null;
 		}
-		
+
 		VolumeApiResult res = new VolumeApiResult(volume);
 		future.complete(res);
-		
+
 		return null;
 	}
-	
-	
+
+    @Override
+    public void handleVolumeSync(DataStore store) {
+        if (store == null) {
+            s_logger.warn("Huh? ssHost is null");
+            return;
+        }
+        long storeId = store.getId();
+        Long zoneId = store.getScope().getScopeId();
+
+
+        Map<Long, TemplateProp> volumeInfos = listVolume(store);
+        if (volumeInfos == null) {
+            return;
+        }
+
+        List<VolumeDataStoreVO> dbVolumes = _volumeStoreDao.listByStoreId(storeId);
+        List<VolumeDataStoreVO> toBeDownloaded = new ArrayList<VolumeDataStoreVO>(dbVolumes);
+        for (VolumeDataStoreVO volumeStore : dbVolumes){
+            VolumeVO volume = _volumeDao.findById(volumeStore.getVolumeId());
+            //Exists then don't download
+            if (volumeInfos.containsKey(volume.getId())){
+                TemplateProp volInfo = volumeInfos.remove(volume.getId());
+                toBeDownloaded.remove(volumeStore);
+                s_logger.info("Volume Sync found " + volume.getUuid() + " already in the volume image store table");
+                if (volumeStore.getDownloadState() != Status.DOWNLOADED) {
+                    volumeStore.setErrorString("");
+                }
+                if (volInfo.isCorrupted()) {
+                    volumeStore.setDownloadState(Status.DOWNLOAD_ERROR);
+                    String msg = "Volume " + volume.getUuid() + " is corrupted on image store ";
+                    volumeStore.setErrorString(msg);
+                    s_logger.info("msg");
+                    if (volumeStore.getDownloadUrl() == null) {
+                        msg = "Volume (" + volume.getUuid() + ") with install path " + volInfo.getInstallPath() + "is corrupted, please check in image store: " + volumeStore.getDataStoreId();
+                        s_logger.warn(msg);
+                    } else {
+                        toBeDownloaded.add(volumeStore);
+                    }
+
+                } else { // Put them in right status
+                    volumeStore.setDownloadPercent(100);
+                    volumeStore.setDownloadState(Status.DOWNLOADED);
+                    volumeStore.setInstallPath(volInfo.getInstallPath());
+                    volumeStore.setSize(volInfo.getSize());
+                    volumeStore.setPhysicalSize(volInfo.getPhysicalSize());
+                    volumeStore.setLastUpdated(new Date());
+                    _volumeStoreDao.update(volumeStore.getId(), volumeStore);
+
+                    if (volume.getSize() == 0) {
+                        // Set volume size in volumes table
+                        volume.setSize(volInfo.getSize());
+                        _volumeDao.update(volumeStore.getVolumeId(), volume);
+                    }
+
+                    if (volInfo.getSize() > 0) {
+                        try {
+                            String url = _volumeStoreDao.findByVolumeId(volume.getId()).getDownloadUrl();
+                            _resourceLimitMgr.checkResourceLimit(_accountMgr.getAccount(volume.getAccountId()),
+                                    com.cloud.configuration.Resource.ResourceType.secondary_storage,
+                                    volInfo.getSize() - UriUtils.getRemoteSize(url));
+                        } catch (ResourceAllocationException e) {
+                            s_logger.warn(e.getMessage());
+                            _alertMgr.sendAlert(_alertMgr.ALERT_TYPE_RESOURCE_LIMIT_EXCEEDED, volume.getDataCenterId(),
+                                    volume.getPodId(), e.getMessage(), e.getMessage());
+                        } finally {
+                            _resourceLimitMgr.recalculateResourceCount(volume.getAccountId(), volume.getDomainId(),
+                                    com.cloud.configuration.Resource.ResourceType.secondary_storage.getOrdinal());
+                        }
+                    }
+                }
+                continue;
+            }
+            // Volume is not on secondary but we should download.
+            if (volumeStore.getDownloadState() != Status.DOWNLOADED) {
+                s_logger.info("Volume Sync did not find " + volume.getName() + " ready on image store " + storeId + ", will request download to start/resume shortly");
+                toBeDownloaded.add(volumeStore);
+            }
+        }
+
+        //Download volumes which haven't been downloaded yet.
+        if (toBeDownloaded.size() > 0) {
+            for (VolumeDataStoreVO 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 " + store.getName());
+                //TODO: pass a callback later
+                _dlMonitor.downloadVolumeToStorage(_volumeDao.findById(volumeHost.getVolumeId()), store,  volumeHost.getDownloadUrl(), volumeHost.getChecksum(), volumeHost.getFormat(), null);
+            }
+        }
+
+        //Delete volumes which are not present on DB.
+        for (Long uniqueName : volumeInfos.keySet()) {
+            TemplateProp vInfo = volumeInfos.get(uniqueName);
+            DeleteVolumeCommand dtCommand = new DeleteVolumeCommand(store.getUri(), vInfo.getInstallPath());
+            try {
+                HostVO ssAhost = _ssvmMgr.pickSsvmHost(store);
+                _agentMgr.sendToSecStorage(ssAhost, dtCommand, null);
+            } catch (AgentUnavailableException e) {
+                String err = "Failed to delete " + vInfo.getTemplateName() + " on image store " + storeId + " which isn't in the database";
+                s_logger.error(err);
+                return;
+            }
+
+            String description = "Deleted volume " + vInfo.getTemplateName() + " on image store " + storeId + " since it isn't in the database";
+            s_logger.info(description);
+        }
+
+    }
+
+    private Map<Long, TemplateProp> listVolume(DataStore store) {
+        ListVolumeCommand cmd = new ListVolumeCommand(store.getUri());
+        HostVO ssAhost = _ssvmMgr.pickSsvmHost(store);
+        Answer answer = _agentMgr.sendToSecStorage(ssAhost, cmd);
+        if (answer != null && answer.getResult()) {
+            ListVolumeAnswer tanswer = (ListVolumeAnswer)answer;
+            return tanswer.getTemplateInfo();
+        } else {
+            if (s_logger.isDebugEnabled()) {
+                s_logger.debug("Can not list volumes for image store " + store.getId());
+            }
+        }
+
+        return null;
+    }
 
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/bb646727/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
index 362e0a5..706c9ef 100755
--- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
+++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
@@ -215,7 +215,7 @@ import com.cloud.storage.Volume;
 import com.cloud.storage.template.Processor;
 import com.cloud.storage.template.Processor.FormatInfo;
 import com.cloud.storage.template.QCOW2Processor;
-import com.cloud.storage.template.TemplateInfo;
+import com.cloud.storage.template.TemplateProp;
 import com.cloud.storage.template.TemplateLocation;
 import com.cloud.utils.NumbersUtil;
 import com.cloud.utils.Pair;
@@ -2381,7 +2381,7 @@ ServerResource {
             return new Answer(cmd, false, " Failed to create storage pool");
         }
 
-        Map<String, TemplateInfo> tInfo = new HashMap<String, TemplateInfo>();
+        Map<String, TemplateProp> tInfo = new HashMap<String, TemplateProp>();
         ModifyStoragePoolAnswer answer = new ModifyStoragePoolAnswer(cmd,
                 storagepool.getCapacity(), storagepool.getUsed(), tInfo);
 

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/bb646727/plugins/hypervisors/ovm/src/com/cloud/ovm/hypervisor/OvmResourceBase.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/ovm/src/com/cloud/ovm/hypervisor/OvmResourceBase.java b/plugins/hypervisors/ovm/src/com/cloud/ovm/hypervisor/OvmResourceBase.java
index a626e31..7d8d90c 100755
--- a/plugins/hypervisors/ovm/src/com/cloud/ovm/hypervisor/OvmResourceBase.java
+++ b/plugins/hypervisors/ovm/src/com/cloud/ovm/hypervisor/OvmResourceBase.java
@@ -114,7 +114,7 @@ import com.cloud.resource.hypervisor.HypervisorResource;
 import com.cloud.storage.Storage.ImageFormat;
 import com.cloud.storage.Storage.StoragePoolType;
 import com.cloud.storage.Volume;
-import com.cloud.storage.template.TemplateInfo;
+import com.cloud.storage.template.TemplateProp;
 import com.cloud.template.VirtualMachineTemplate.BootloaderType;
 import com.cloud.utils.Pair;
 import com.cloud.utils.Ternary;
@@ -458,7 +458,7 @@ public class OvmResourceBase implements ServerResource, HypervisorResource {
 			
 			setupHeartBeat(pool.getUuid());
 			OvmStoragePool.Details d = OvmStoragePool.getDetailsByUuid(_conn, pool.getUuid());
-			Map<String, TemplateInfo> tInfo = new HashMap<String, TemplateInfo>();
+			Map<String, TemplateProp> tInfo = new HashMap<String, TemplateProp>();
 			ModifyStoragePoolAnswer answer = new ModifyStoragePoolAnswer(cmd, d.totalSpace, d.freeSpace, tInfo);
 			return answer;
 		} catch (Exception e) {

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/bb646727/plugins/hypervisors/simulator/src/com/cloud/agent/manager/MockStorageManagerImpl.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/simulator/src/com/cloud/agent/manager/MockStorageManagerImpl.java b/plugins/hypervisors/simulator/src/com/cloud/agent/manager/MockStorageManagerImpl.java
index a50dff6..ae7cf29 100644
--- a/plugins/hypervisors/simulator/src/com/cloud/agent/manager/MockStorageManagerImpl.java
+++ b/plugins/hypervisors/simulator/src/com/cloud/agent/manager/MockStorageManagerImpl.java
@@ -74,7 +74,7 @@ import com.cloud.storage.Storage.ImageFormat;
 import com.cloud.storage.Storage.StoragePoolType;
 import com.cloud.storage.VMTemplateStorageResourceAssoc;
 import com.cloud.storage.VMTemplateStorageResourceAssoc.Status;
-import com.cloud.storage.template.TemplateInfo;
+import com.cloud.storage.template.TemplateProp;
 import com.cloud.utils.component.ManagerBase;
 import com.cloud.utils.db.Transaction;
 import com.cloud.utils.exception.CloudRuntimeException;
@@ -357,7 +357,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa
             txn = Transaction.open(Transaction.CLOUD_DB);
             txn.close();
         }
-        return new ModifyStoragePoolAnswer(cmd, storagePool.getCapacity(), 0, new HashMap<String, TemplateInfo>());
+        return new ModifyStoragePoolAnswer(cmd, storagePool.getCapacity(), 0, new HashMap<String, TemplateProp>());
     }
 
     @Override
@@ -398,7 +398,7 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa
             txn = Transaction.open(Transaction.CLOUD_DB);
             txn.close();
         }
-        return new ModifyStoragePoolAnswer(cmd, storagePool.getCapacity(), 0, new HashMap<String, TemplateInfo>());
+        return new ModifyStoragePoolAnswer(cmd, storagePool.getCapacity(), 0, new HashMap<String, TemplateProp>());
     }
 
     @Override
@@ -449,9 +449,9 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa
             List<MockVolumeVO> volumes = _mockVolumeDao.findByStorageIdAndType(storage.getId(),
                     MockVolumeType.VOLUME);
 
-            Map<Long, TemplateInfo> templateInfos = new HashMap<Long, TemplateInfo>();
+            Map<Long, TemplateProp> templateInfos = new HashMap<Long, TemplateProp>();
             for (MockVolumeVO volume : volumes) {
-                templateInfos.put(volume.getId(), new TemplateInfo(volume.getName(), volume.getPath()
+                templateInfos.put(volume.getId(), new TemplateProp(volume.getName(), volume.getPath()
                         .replaceAll(storage.getMountPoint(), ""), volume.getSize(), volume.getSize(), true, false));
             }
             txn.commit();
@@ -492,9 +492,9 @@ public class MockStorageManagerImpl extends ManagerBase implements MockStorageMa
             List<MockVolumeVO> templates = _mockVolumeDao.findByStorageIdAndType(storage.getId(),
                     MockVolumeType.TEMPLATE);
 
-            Map<String, TemplateInfo> templateInfos = new HashMap<String, TemplateInfo>();
+            Map<String, TemplateProp> templateInfos = new HashMap<String, TemplateProp>();
             for (MockVolumeVO template : templates) {
-                templateInfos.put(template.getName(), new TemplateInfo(template.getName(), template.getPath()
+                templateInfos.put(template.getName(), new TemplateProp(template.getName(), template.getPath()
                         .replaceAll(storage.getMountPoint(), ""), template.getSize(), template.getSize(), true, false));
             }
             txn.commit();

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/bb646727/plugins/hypervisors/simulator/src/com/cloud/resource/AgentRoutingResource.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/simulator/src/com/cloud/resource/AgentRoutingResource.java b/plugins/hypervisors/simulator/src/com/cloud/resource/AgentRoutingResource.java
index 46df50c..8a03c4b 100644
--- a/plugins/hypervisors/simulator/src/com/cloud/resource/AgentRoutingResource.java
+++ b/plugins/hypervisors/simulator/src/com/cloud/resource/AgentRoutingResource.java
@@ -51,7 +51,7 @@ import com.cloud.hypervisor.Hypervisor.HypervisorType;
 import com.cloud.network.Networks.RouterPrivateIpStrategy;
 import com.cloud.simulator.MockVMVO;
 import com.cloud.storage.Storage.StorageResourceType;
-import com.cloud.storage.template.TemplateInfo;
+import com.cloud.storage.template.TemplateProp;
 import com.cloud.utils.Pair;
 import com.cloud.vm.VirtualMachine.State;
 
@@ -168,7 +168,7 @@ public class AgentRoutingResource extends AgentStorageResource {
     }
 
     private StartupStorageCommand initializeLocalSR() {
-        Map<String, TemplateInfo> tInfo = new HashMap<String, TemplateInfo>();
+        Map<String, TemplateProp> tInfo = new HashMap<String, TemplateProp>();
 
         StoragePoolInfo poolInfo = _simMgr.getLocalStorage(hostGuid);
 

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/bb646727/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java b/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java
index 634827b..4079d0d 100755
--- a/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java
+++ b/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java
@@ -210,7 +210,7 @@ import com.cloud.storage.Storage;
 import com.cloud.storage.Storage.StoragePoolType;
 import com.cloud.storage.Volume;
 import com.cloud.storage.resource.StoragePoolResource;
-import com.cloud.storage.template.TemplateInfo;
+import com.cloud.storage.template.TemplateProp;
 import com.cloud.utils.DateUtil;
 import com.cloud.utils.Pair;
 import com.cloud.utils.StringUtils;
@@ -3137,7 +3137,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa
             DatastoreSummary summary = new DatastoreMO(getServiceContext(), morDatastore).getSummary();
             long capacity = summary.getCapacity();
             long available = summary.getFreeSpace();
-            Map<String, TemplateInfo> tInfo = new HashMap<String, TemplateInfo>();
+            Map<String, TemplateProp> tInfo = new HashMap<String, TemplateProp>();
             ModifyStoragePoolAnswer answer = new ModifyStoragePoolAnswer(cmd, capacity, available, tInfo);
             return answer;
         } catch (Throwable e) {

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/bb646727/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java b/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java
index c50f13c..046827d 100644
--- a/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java
+++ b/plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java
@@ -233,7 +233,7 @@ import com.cloud.storage.Storage.ImageFormat;
 import com.cloud.storage.Storage.StoragePoolType;
 import com.cloud.storage.Volume;
 import com.cloud.storage.VolumeVO;
-import com.cloud.storage.template.TemplateInfo;
+import com.cloud.storage.template.TemplateProp;
 import com.cloud.template.VirtualMachineTemplate.BootloaderType;
 import com.cloud.utils.NumbersUtil;
 import com.cloud.utils.Pair;
@@ -5287,7 +5287,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
                     s_logger.warn(msg);
                     return new Answer(cmd, false, msg);
                 }
-                Map<String, TemplateInfo> tInfo = new HashMap<String, TemplateInfo>();
+                Map<String, TemplateProp> tInfo = new HashMap<String, TemplateProp>();
                 ModifyStoragePoolAnswer answer = new ModifyStoragePoolAnswer(cmd, capacity, available, tInfo);
                 return answer;
             } catch (XenAPIException e) {

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/bb646727/plugins/storage/image/default/pom.xml
----------------------------------------------------------------------
diff --git a/plugins/storage/image/default/pom.xml b/plugins/storage/image/default/pom.xml
index e84eab4..d1b079d 100644
--- a/plugins/storage/image/default/pom.xml
+++ b/plugins/storage/image/default/pom.xml
@@ -26,6 +26,16 @@
       <version>${project.version}</version>
     </dependency>
     <dependency>
+      <groupId>org.apache.cloudstack</groupId>
+      <artifactId>cloud-engine-storage-volume</artifactId>
+      <version>${project.version}</version>
+    </dependency>    
+    <dependency>
+      <groupId>org.apache.cloudstack</groupId>
+      <artifactId>cloud-engine-storage-snapshot</artifactId>
+      <version>${project.version}</version>
+    </dependency>        
+    <dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
       <version>${cs.mysql.version}</version>

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/bb646727/plugins/storage/image/default/src/org/apache/cloudstack/storage/datastore/driver/CloudStackImageStoreDriverImpl.java
----------------------------------------------------------------------
diff --git a/plugins/storage/image/default/src/org/apache/cloudstack/storage/datastore/driver/CloudStackImageStoreDriverImpl.java b/plugins/storage/image/default/src/org/apache/cloudstack/storage/datastore/driver/CloudStackImageStoreDriverImpl.java
index d52af65..46c87b7 100644
--- a/plugins/storage/image/default/src/org/apache/cloudstack/storage/datastore/driver/CloudStackImageStoreDriverImpl.java
+++ b/plugins/storage/image/default/src/org/apache/cloudstack/storage/datastore/driver/CloudStackImageStoreDriverImpl.java
@@ -35,6 +35,7 @@ import org.apache.cloudstack.framework.async.AsyncCompletionCallback;
 import org.apache.cloudstack.framework.async.AsyncRpcConext;
 import org.apache.cloudstack.storage.image.ImageStoreDriver;
 import org.apache.cloudstack.storage.image.store.TemplateObject;
+import org.apache.cloudstack.storage.volume.VolumeObject;
 import org.apache.log4j.Logger;
 
 import com.cloud.agent.AgentManager;
@@ -118,11 +119,10 @@ public class CloudStackImageStoreDriverImpl implements ImageStoreDriver {
             TemplateObject tData = (TemplateObject)data;
             _downloadMonitor.downloadTemplateToStorage(tData.getImage(), tData.getDataStore(), callback);
         } else if (data.getType() == DataObjectType.VOLUME) {
-            VolumeVO vol = this.volumeDao.findById(data.getId());
-            VolumeInfo volInfo = (VolumeInfo)data;
+            VolumeObject volInfo = (VolumeObject)data;
             RegisterVolumePayload payload = (RegisterVolumePayload)volInfo.getpayload();
-            _downloadMonitor.downloadVolumeToStorage(vol, vol.getDataCenterId(), payload.getUrl(),
-                    payload.getChecksum(), ImageFormat.valueOf(payload.getFormat().toUpperCase()));
+            _downloadMonitor.downloadVolumeToStorage(volInfo.getVolume(), volInfo.getDataStore(), payload.getUrl(),
+                    payload.getChecksum(), ImageFormat.valueOf(payload.getFormat().toUpperCase()), callback);
         }
 
         CreateCmdResult result = new CreateCmdResult(null, null);

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/bb646727/plugins/storage/image/s3/pom.xml
----------------------------------------------------------------------
diff --git a/plugins/storage/image/s3/pom.xml b/plugins/storage/image/s3/pom.xml
index 7ab0d3e..7f5b9c4 100644
--- a/plugins/storage/image/s3/pom.xml
+++ b/plugins/storage/image/s3/pom.xml
@@ -32,6 +32,16 @@
       <artifactId>cloud-engine-storage-image</artifactId>
       <version>${project.version}</version>
     </dependency>
+    <dependency>
+      <groupId>org.apache.cloudstack</groupId>
+      <artifactId>cloud-engine-storage-volume</artifactId>
+      <version>${project.version}</version>
+    </dependency>    
+    <dependency>
+      <groupId>org.apache.cloudstack</groupId>
+      <artifactId>cloud-engine-storage-snapshot</artifactId>
+      <version>${project.version}</version>
+    </dependency>            
   </dependencies>
   <build>
       <defaultGoal>install</defaultGoal>

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/bb646727/plugins/storage/image/s3/src/org/apache/cloudstack/storage/datastore/driver/S3ImageStoreDriverImpl.java
----------------------------------------------------------------------
diff --git a/plugins/storage/image/s3/src/org/apache/cloudstack/storage/datastore/driver/S3ImageStoreDriverImpl.java b/plugins/storage/image/s3/src/org/apache/cloudstack/storage/datastore/driver/S3ImageStoreDriverImpl.java
index 0d84f58..50a82cc 100644
--- a/plugins/storage/image/s3/src/org/apache/cloudstack/storage/datastore/driver/S3ImageStoreDriverImpl.java
+++ b/plugins/storage/image/s3/src/org/apache/cloudstack/storage/datastore/driver/S3ImageStoreDriverImpl.java
@@ -35,6 +35,7 @@ import org.apache.cloudstack.framework.async.AsyncCompletionCallback;
 import org.apache.cloudstack.framework.async.AsyncRpcConext;
 import org.apache.cloudstack.storage.image.ImageStoreDriver;
 import org.apache.cloudstack.storage.image.store.TemplateObject;
+import org.apache.cloudstack.storage.volume.VolumeObject;
 import org.apache.log4j.Logger;
 
 import com.cloud.agent.AgentManager;
@@ -118,11 +119,10 @@ public class S3ImageStoreDriverImpl implements ImageStoreDriver {
             TemplateObject tData = (TemplateObject)data;
             _downloadMonitor.downloadTemplateToStorage(tData.getImage(), tData.getDataStore(), callback);
         } else if (data.getType() == DataObjectType.VOLUME) {
-            VolumeVO vol = this.volumeDao.findById(data.getId());
-            VolumeInfo volInfo = (VolumeInfo)data;
+            VolumeObject volInfo = (VolumeObject)data;
             RegisterVolumePayload payload = (RegisterVolumePayload)volInfo.getpayload();
-            _downloadMonitor.downloadVolumeToStorage(vol, vol.getDataCenterId(), payload.getUrl(),
-                    payload.getChecksum(), ImageFormat.valueOf(payload.getFormat().toUpperCase()));
+            _downloadMonitor.downloadVolumeToStorage(volInfo.getVolume(), volInfo.getDataStore(), payload.getUrl(),
+                    payload.getChecksum(), ImageFormat.valueOf(payload.getFormat().toUpperCase()), callback);
         }
 
         CreateCmdResult result = new CreateCmdResult(null, null);

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/bb646727/plugins/storage/image/sample/pom.xml
----------------------------------------------------------------------
diff --git a/plugins/storage/image/sample/pom.xml b/plugins/storage/image/sample/pom.xml
index e4d5ac2..d9eab9b 100644
--- a/plugins/storage/image/sample/pom.xml
+++ b/plugins/storage/image/sample/pom.xml
@@ -26,6 +26,16 @@
       <version>${project.version}</version>
     </dependency>
     <dependency>
+      <groupId>org.apache.cloudstack</groupId>
+      <artifactId>cloud-engine-storage-volume</artifactId>
+      <version>${project.version}</version>
+    </dependency>    
+    <dependency>
+      <groupId>org.apache.cloudstack</groupId>
+      <artifactId>cloud-engine-storage-snapshot</artifactId>
+      <version>${project.version}</version>
+    </dependency>            
+    <dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
       <version>${cs.mysql.version}</version>

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/bb646727/plugins/storage/image/swift/pom.xml
----------------------------------------------------------------------
diff --git a/plugins/storage/image/swift/pom.xml b/plugins/storage/image/swift/pom.xml
index 27ebbe5..6254cce 100644
--- a/plugins/storage/image/swift/pom.xml
+++ b/plugins/storage/image/swift/pom.xml
@@ -26,6 +26,16 @@
       <version>${project.version}</version>
     </dependency>
     <dependency>
+      <groupId>org.apache.cloudstack</groupId>
+      <artifactId>cloud-engine-storage-volume</artifactId>
+      <version>${project.version}</version>
+    </dependency>    
+    <dependency>
+      <groupId>org.apache.cloudstack</groupId>
+      <artifactId>cloud-engine-storage-snapshot</artifactId>
+      <version>${project.version}</version>
+    </dependency>            
+    <dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
       <version>${cs.mysql.version}</version>

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/bb646727/plugins/storage/image/swift/src/org/apache/cloudstack/storage/datastore/driver/SwiftImageStoreDriverImpl.java
----------------------------------------------------------------------
diff --git a/plugins/storage/image/swift/src/org/apache/cloudstack/storage/datastore/driver/SwiftImageStoreDriverImpl.java b/plugins/storage/image/swift/src/org/apache/cloudstack/storage/datastore/driver/SwiftImageStoreDriverImpl.java
index 36fd5fb..e539e5c 100644
--- a/plugins/storage/image/swift/src/org/apache/cloudstack/storage/datastore/driver/SwiftImageStoreDriverImpl.java
+++ b/plugins/storage/image/swift/src/org/apache/cloudstack/storage/datastore/driver/SwiftImageStoreDriverImpl.java
@@ -35,6 +35,7 @@ import org.apache.cloudstack.framework.async.AsyncCompletionCallback;
 import org.apache.cloudstack.framework.async.AsyncRpcConext;
 import org.apache.cloudstack.storage.image.ImageStoreDriver;
 import org.apache.cloudstack.storage.image.store.TemplateObject;
+import org.apache.cloudstack.storage.volume.VolumeObject;
 import org.apache.log4j.Logger;
 
 import com.cloud.agent.AgentManager;
@@ -118,11 +119,10 @@ public class SwiftImageStoreDriverImpl implements ImageStoreDriver {
             TemplateObject tData = (TemplateObject)data;
             _downloadMonitor.downloadTemplateToStorage(tData.getImage(), tData.getDataStore(), callback);
         } else if (data.getType() == DataObjectType.VOLUME) {
-            VolumeVO vol = this.volumeDao.findById(data.getId());
-            VolumeInfo volInfo = (VolumeInfo)data;
+            VolumeObject volInfo = (VolumeObject)data;
             RegisterVolumePayload payload = (RegisterVolumePayload)volInfo.getpayload();
-            _downloadMonitor.downloadVolumeToStorage(vol, vol.getDataCenterId(), payload.getUrl(),
-                    payload.getChecksum(), ImageFormat.valueOf(payload.getFormat().toUpperCase()));
+            _downloadMonitor.downloadVolumeToStorage(volInfo.getVolume(), volInfo.getDataStore(), payload.getUrl(),
+                    payload.getChecksum(), ImageFormat.valueOf(payload.getFormat().toUpperCase()), callback);
         }
 
         CreateCmdResult result = new CreateCmdResult(null, null);

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/bb646727/server/src/com/cloud/storage/StorageManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/storage/StorageManagerImpl.java b/server/src/com/cloud/storage/StorageManagerImpl.java
index 78b26bb..3b1a768 100755
--- a/server/src/com/cloud/storage/StorageManagerImpl.java
+++ b/server/src/com/cloud/storage/StorageManagerImpl.java
@@ -1993,15 +1993,15 @@ public class StorageManagerImpl extends ManagerBase implements StorageManager, C
         _accountMgr.checkAccessAndSpecifyAuthority(UserContext.current().getCaller(), store.getDataCenterId());
 
         // Verify that there are no live snapshot, template, volume on the image store to be deleted
-        List<SnapshotDataStoreVO> snapshots = _snapshotStoreDao.listLiveByStoreId(storeId);
+        List<SnapshotDataStoreVO> snapshots = _snapshotStoreDao.listByStoreId(storeId);
         if ( snapshots != null && snapshots.size() > 0 ){
             throw new CloudRuntimeException("Cannot delete image store with active snapshots backup!");
         }
-        List<VolumeDataStoreVO> volumes = _volumeStoreDao.listLiveByStoreId(storeId);
+        List<VolumeDataStoreVO> volumes = _volumeStoreDao.listByStoreId(storeId);
         if ( volumes != null && volumes.size() > 0 ){
             throw new CloudRuntimeException("Cannot delete image store with active volumes backup!");
         }
-        List<TemplateDataStoreVO> templates = _templateStoreDao.listLiveByStoreId(storeId);
+        List<TemplateDataStoreVO> templates = _templateStoreDao.listByStoreId(storeId);
         if ( templates != null && templates.size() > 0 ){
             throw new CloudRuntimeException("Cannot delete image store with active templates backup!");
         }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/bb646727/server/src/com/cloud/storage/download/DownloadListener.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/storage/download/DownloadListener.java b/server/src/com/cloud/storage/download/DownloadListener.java
index 8e979ab..3eb2233 100755
--- a/server/src/com/cloud/storage/download/DownloadListener.java
+++ b/server/src/com/cloud/storage/download/DownloadListener.java
@@ -18,6 +18,7 @@ package com.cloud.storage.download;
 
 import java.util.Date;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Timer;
 import java.util.TimerTask;
@@ -26,9 +27,15 @@ 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.TemplateService;
+import org.apache.cloudstack.engine.subsystem.api.storage.ObjectInDataStoreStateMachine;
+import org.apache.cloudstack.engine.subsystem.api.storage.VolumeService;
+import org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope;
 import org.apache.cloudstack.framework.async.AsyncCompletionCallback;
 import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreDao;
 import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO;
+import org.apache.cloudstack.storage.datastore.db.VolumeDataStoreDao;
 import org.apache.log4j.Level;
 import org.apache.log4j.Logger;
 
@@ -52,6 +59,8 @@ import com.cloud.exception.ConnectionException;
 import com.cloud.exception.ResourceAllocationException;
 import com.cloud.host.HostVO;
 import com.cloud.host.dao.HostDao;
+import com.cloud.hypervisor.Hypervisor.HypervisorType;
+import com.cloud.resource.ResourceManager;
 import com.cloud.storage.Storage;
 import com.cloud.storage.StorageManager;
 import com.cloud.storage.VMTemplateHostVO;
@@ -117,57 +126,68 @@ public class DownloadListener implements Listener {
 	public static final String DOWNLOAD_ABANDONED=Status.ABANDONED.toString();
 
 
-	private HostVO sserver;
-	private HostVO ssAgent;
+	private HostVO _sserver;
+	private HostVO _ssAgent;
 
-	private VMTemplateVO template;
-	private VolumeVO volume;
-	private boolean downloadActive = true;
+	private VMTemplateVO _template;
+	private VolumeVO _volume;
+	private boolean _downloadActive = true;
 
-	private VolumeHostDao volumeHostDao;
+	private VolumeHostDao _volumeHostDao;
+	private VolumeDataStoreDao _volumeStoreDao;
 	private VolumeDao _volumeDao;
 	private StorageManager _storageMgr;
-	private VMTemplateHostDao vmTemplateHostDao;
+	private VMTemplateHostDao _vmTemplateHostDao;
 	private TemplateDataStoreDao _vmTemplateStoreDao;
 	private VMTemplateDao _vmTemplateDao;
 	private ResourceLimitService _resourceLimitMgr;
 	private AccountManager _accountMgr;
 	private AlertManager _alertMgr;
 
-	private final DownloadMonitorImpl downloadMonitor;
+	private final DownloadMonitorImpl _downloadMonitor;
 
-	private DownloadState currState;
+	private DownloadState _currState;
 
-	private DownloadCommand cmd;
+	private DownloadCommand _cmd;
 
-	private Timer timer;
+	private Timer _timer;
 
-	private StatusTask statusTask;
-	private TimeoutTask timeoutTask;
-	private Date lastUpdated = new Date();
-	private String jobId;
+	private StatusTask _statusTask;
+	private TimeoutTask _timeoutTask;
+	private Date _lastUpdated = new Date();
+	private String _jobId;
 
-	private final Map<String,  DownloadState> stateMap = new HashMap<String, DownloadState>();
-	private Long templateHostId;
-	private Long volumeHostId;
+	private final Map<String,  DownloadState> _stateMap = new HashMap<String, DownloadState>();
+	private Long _templateHostId;
+	private Long _volumeHostId;
 
-    private DataStore sstore;
-    private Long templateStoreId;
-	private AsyncCompletionCallback<CreateCmdResult> callback;
+    private DataStore _sstore;
+    private Long _templateStoreId;
+    private Long _volumeStoreId;
+	private AsyncCompletionCallback<CreateCmdResult> _callback;
+
+    @Inject
+    private ResourceManager _resourceMgr;
+    @Inject
+    private TemplateService _imageSrv;
+    @Inject
+    private DataStoreManager _storeMgr;
+    @Inject
+    private VolumeService _volumeSrv;
 
 	public DownloadListener(HostVO ssAgent, HostVO host, VMTemplateVO template, Timer _timer, VMTemplateHostDao dao, Long templHostId, DownloadMonitorImpl downloadMonitor, DownloadCommand cmd, VMTemplateDao templateDao, ResourceLimitService _resourceLimitMgr, AlertManager _alertMgr, AccountManager _accountMgr) {
-	    this.ssAgent = ssAgent;
-        this.sserver = host;
-		this.template = template;
-		this.vmTemplateHostDao = dao;
-		this.downloadMonitor = downloadMonitor;
-		this.cmd = cmd;
-		this.templateHostId = templHostId;
+	    this._ssAgent = ssAgent;
+        this._sserver = host;
+		this._template = template;
+		this._vmTemplateHostDao = dao;
+		this._downloadMonitor = downloadMonitor;
+		this._cmd = cmd;
+		this._templateHostId = templHostId;
 		initStateMachine();
-		this.currState=getState(Status.NOT_DOWNLOADED.toString());
-		this.timer = _timer;
-		this.timeoutTask = new TimeoutTask(this);
-		this.timer.schedule(timeoutTask, 3*STATUS_POLL_INTERVAL);
+		this._currState=getState(Status.NOT_DOWNLOADED.toString());
+		this._timer = _timer;
+		this._timeoutTask = new TimeoutTask(this);
+		this._timer.schedule(_timeoutTask, 3*STATUS_POLL_INTERVAL);
 		this._vmTemplateDao = templateDao;
 		this._resourceLimitMgr = _resourceLimitMgr;
 		this._accountMgr = _accountMgr;
@@ -177,63 +197,64 @@ public class DownloadListener implements Listener {
 
 	// TODO: this constructor should be the one used for template only, remove other template constructor later
     public DownloadListener(HostVO ssAgent, DataStore store, VMTemplateVO template, Timer _timer, TemplateDataStoreDao dao, Long templStoreId, DownloadMonitorImpl downloadMonitor, DownloadCommand cmd, VMTemplateDao templateDao, ResourceLimitService _resourceLimitMgr, AlertManager _alertMgr, AccountManager _accountMgr, AsyncCompletionCallback<CreateCmdResult> callback) {
-        this.ssAgent = ssAgent;
-        this.sstore = store;
-        this.template = template;
+        this._ssAgent = ssAgent;
+        this._sstore = store;
+        this._template = template;
         this._vmTemplateStoreDao = dao;
-        this.downloadMonitor = downloadMonitor;
-        this.cmd = cmd;
-        this.templateStoreId = templStoreId;
+        this._downloadMonitor = downloadMonitor;
+        this._cmd = cmd;
+        this._templateStoreId = templStoreId;
         initStateMachine();
-        this.currState=getState(Status.NOT_DOWNLOADED.toString());
-        this.timer = _timer;
-        this.timeoutTask = new TimeoutTask(this);
-        this.timer.schedule(timeoutTask, 3*STATUS_POLL_INTERVAL);
+        this._currState=getState(Status.NOT_DOWNLOADED.toString());
+        this._timer = _timer;
+        this._timeoutTask = new TimeoutTask(this);
+        this._timer.schedule(_timeoutTask, 3*STATUS_POLL_INTERVAL);
         this._vmTemplateDao = templateDao;
         this._resourceLimitMgr = _resourceLimitMgr;
         this._accountMgr = _accountMgr;
         this._alertMgr = _alertMgr;
-        this.callback = callback;
+        this._callback = callback;
         updateDatabase(Status.NOT_DOWNLOADED, "");
     }
 
 
-	public DownloadListener(HostVO ssAgent, HostVO host, VolumeVO volume, Timer _timer, VolumeHostDao dao, Long volHostId, DownloadMonitorImpl downloadMonitor, DownloadCommand cmd, VolumeDao volumeDao, StorageManager storageMgr, ResourceLimitService _resourceLimitMgr, AlertManager _alertMgr, AccountManager _accountMgr) {
-	    this.ssAgent = ssAgent;
-        this.sserver = host;
-		this.volume = volume;
-		this.volumeHostDao = dao;
-		this.downloadMonitor = downloadMonitor;
-		this.cmd = cmd;
-		this.volumeHostId = volHostId;
+	public DownloadListener(HostVO ssAgent, DataStore store, VolumeVO volume, Timer _timer, VolumeDataStoreDao dao, Long volStoreId, DownloadMonitorImpl downloadMonitor, DownloadCommand cmd, VolumeDao volumeDao, StorageManager storageMgr, ResourceLimitService _resourceLimitMgr, AlertManager _alertMgr, AccountManager _accountMgr, AsyncCompletionCallback<CreateCmdResult> callback) {
+	    this._ssAgent = ssAgent;
+        this._sstore = store;
+		this._volume = volume;
+		this._volumeStoreDao = dao;
+		this._downloadMonitor = downloadMonitor;
+		this._cmd = cmd;
+		this._volumeStoreId = volStoreId;
 		initStateMachine();
-		this.currState=getState(Status.NOT_DOWNLOADED.toString());
-		this.timer = _timer;
-		this.timeoutTask = new TimeoutTask(this);
-		this.timer.schedule(timeoutTask, 3*STATUS_POLL_INTERVAL);
+		this._currState=getState(Status.NOT_DOWNLOADED.toString());
+		this._timer = _timer;
+		this._timeoutTask = new TimeoutTask(this);
+		this._timer.schedule(_timeoutTask, 3*STATUS_POLL_INTERVAL);
 		this._volumeDao = volumeDao;
 		this._storageMgr = storageMgr;
 		this._resourceLimitMgr = _resourceLimitMgr;
 		this._accountMgr = _accountMgr;
 		this._alertMgr = _alertMgr;
+		this._callback = callback;
 		updateDatabase(Status.NOT_DOWNLOADED, "");
 	}
 
 
 	public void setCurrState(VMTemplateHostVO.Status currState) {
-		this.currState = getState(currState.toString());
+		this._currState = getState(currState.toString());
 	}
 
 	private void initStateMachine() {
-		stateMap.put(Status.NOT_DOWNLOADED.toString(), new NotDownloadedState(this));
-		stateMap.put(Status.DOWNLOADED.toString(), new DownloadCompleteState(this));
-		stateMap.put(Status.DOWNLOAD_ERROR.toString(), new DownloadErrorState(this));
-		stateMap.put(Status.DOWNLOAD_IN_PROGRESS.toString(), new DownloadInProgressState(this));
-		stateMap.put(Status.ABANDONED.toString(), new DownloadAbandonedState(this));
+		_stateMap.put(Status.NOT_DOWNLOADED.toString(), new NotDownloadedState(this));
+		_stateMap.put(Status.DOWNLOADED.toString(), new DownloadCompleteState(this));
+		_stateMap.put(Status.DOWNLOAD_ERROR.toString(), new DownloadErrorState(this));
+		_stateMap.put(Status.DOWNLOAD_IN_PROGRESS.toString(), new DownloadInProgressState(this));
+		_stateMap.put(Status.ABANDONED.toString(), new DownloadAbandonedState(this));
 	}
 
 	private DownloadState getState(String stateName) {
-		return stateMap.get(stateName);
+		return _stateMap.get(stateName);
 	}
 
 	public void sendCommand(RequestType reqType) {
@@ -243,10 +264,10 @@ public class DownloadListener implements Listener {
 			}
 			try {
 				DownloadProgressCommand dcmd = new DownloadProgressCommand(getCommand(), getJobId(), reqType);
-				if (template == null){
+				if (_template == null){
 					dcmd.setResourceType(ResourceType.VOLUME);
 				}
-	            downloadMonitor.send(ssAgent.getId(), dcmd, this);
+	            _downloadMonitor.send(_ssAgent.getId(), dcmd, this);
             } catch (AgentUnavailableException e) {
             	s_logger.debug("Send command failed", e);
 				setDisconnected();
@@ -264,63 +285,63 @@ public class DownloadListener implements Listener {
 	}
 
 	public void logDisconnect() {
-		if (template != null){
-			s_logger.warn("Unable to monitor download progress of " + template.getName() + " at host " + sserver.getName());
+		if (_template != null){
+			s_logger.warn("Unable to monitor download progress of " + _template.getName() + " at host " + _sserver.getName());
 		}else {
-			s_logger.warn("Unable to monitor download progress of " + volume.getName() + " at host " + sserver.getName());
+			s_logger.warn("Unable to monitor download progress of " + _volume.getName() + " at host " + _sserver.getName());
 		}
 	}
 
 	public synchronized void updateDatabase(Status state, String errorString) {
-		if (template != null){
-		    VMTemplateHostVO vo = vmTemplateHostDao.createForUpdate();
+		if (_template != null){
+		    VMTemplateHostVO vo = _vmTemplateHostDao.createForUpdate();
 			vo.setDownloadState(state);
 			vo.setLastUpdated(new Date());
 			vo.setErrorString(errorString);
-			vmTemplateHostDao.update(getTemplateHostId(), vo);
+			_vmTemplateHostDao.update(getTemplateHostId(), vo);
 		}else {
-		    VolumeHostVO vo = volumeHostDao.createForUpdate();
+		    VolumeHostVO vo = _volumeHostDao.createForUpdate();
 			vo.setDownloadState(state);
 			vo.setLastUpdated(new Date());
 			vo.setErrorString(errorString);
-			volumeHostDao.update(getVolumeHostId(), vo);
+			_volumeHostDao.update(getVolumeHostId(), vo);
 		}
 	}
 
 	public void log(String message, Level level) {
-		if (template != null){
-			s_logger.log(level, message + ", template=" + template.getName() + " at host " + sserver.getName());
+		if (_template != null){
+			s_logger.log(level, message + ", template=" + _template.getName() + " at host " + _sserver.getName());
 		}else {
-			s_logger.log(level, message + ", volume=" + volume.getName() + " at host " + sserver.getName());
+			s_logger.log(level, message + ", volume=" + _volume.getName() + " at host " + _sserver.getName());
 		}
 	}
 
 	private Long getTemplateHostId() {
-		if (templateHostId == null){
-			VMTemplateHostVO templHost = vmTemplateHostDao.findByHostTemplate(sserver.getId(), template.getId());
-			templateHostId = templHost.getId();
+		if (_templateHostId == null){
+			VMTemplateHostVO templHost = _vmTemplateHostDao.findByHostTemplate(_sserver.getId(), _template.getId());
+			_templateHostId = templHost.getId();
 		}
-		return templateHostId;
+		return _templateHostId;
 	}
 
     private Long getTemplateStoreId() {
-        if (templateStoreId == null){
-            TemplateDataStoreVO templStore = _vmTemplateStoreDao.findByStoreTemplate(sstore.getId(), template.getId());
-            templateStoreId = templStore.getId();
+        if (_templateStoreId == null){
+            TemplateDataStoreVO templStore = _vmTemplateStoreDao.findByStoreTemplate(_sstore.getId(), _template.getId());
+            _templateStoreId = templStore.getId();
         }
-        return templateStoreId;
+        return _templateStoreId;
     }
 
 	private Long getVolumeHostId() {
-		if (volumeHostId == null){
-			VolumeHostVO volHost = volumeHostDao.findByHostVolume(sserver.getId(), volume.getId());
-			volumeHostId = volHost.getId();
+		if (_volumeHostId == null){
+			VolumeHostVO volHost = _volumeHostDao.findByHostVolume(_sserver.getId(), _volume.getId());
+			_volumeHostId = volHost.getId();
 		}
-		return volumeHostId;
+		return _volumeHostId;
 	}
 
 	public DownloadListener(DownloadMonitorImpl monitor) {
-	    downloadMonitor = monitor;
+	    _downloadMonitor = monitor;
 	}
 
 
@@ -350,15 +371,15 @@ public class DownloadListener implements Listener {
 	}
 
 	private synchronized void transition(DownloadEvent event, Object evtObj) {
-	    if (currState == null) {
+	    if (_currState == null) {
 	        return;
 	    }
-		String prevName = currState.getName();
-		String nextState = currState.handleEvent(event, evtObj);
+		String prevName = _currState.getName();
+		String nextState = _currState.handleEvent(event, evtObj);
 		if (nextState != null) {
-			currState = getState(nextState);
-			if (currState != null) {
-				currState.onEntry(prevName, event, evtObj);
+			_currState = getState(nextState);
+			if (_currState != null) {
+				_currState.onEntry(prevName, event, evtObj);
 			} else {
 				throw new CloudRuntimeException("Invalid next state: currState="+prevName+", evt="+event + ", next=" + nextState);
 			}
@@ -368,7 +389,7 @@ public class DownloadListener implements Listener {
 	}
 
 	public synchronized void updateDatabase(DownloadAnswer answer) {
-		if (template != null){
+		if (_template != null){
 	        TemplateDataStoreVO updateBuilder = _vmTemplateStoreDao.createForUpdate();
 			updateBuilder.setDownloadPercent(answer.getDownloadPct());
 			updateBuilder.setDownloadState(answer.getDownloadStatus());
@@ -380,40 +401,58 @@ public class DownloadListener implements Listener {
 			updateBuilder.setSize(answer.getTemplateSize());
 			updateBuilder.setPhysicalSize(answer.getTemplatePhySicalSize());
 
+            // only invoke callback when Download is completed or errored so that callback will update template_store_ref state column
+            Status dndStatus = answer.getDownloadStatus();
+           // if (dndStatus == Status.DOWNLOAD_ERROR || dndStatus == Status.DOWNLOADED ){
+                if ( _callback != null ){
+                    if (dndStatus == Status.DOWNLOAD_ERROR){
+                        CreateCmdResult result = new CreateCmdResult(null, null);
+                        result.setSucess(false);
+                        result.setResult("Download template failed");
+                        _callback.complete(result);
+                    } else if (dndStatus == Status.DOWNLOADED){
+                        CreateCmdResult result = new CreateCmdResult(null, null);
+                        _callback.complete(result);
+                    }
+                }
+                else{
+                    // no callback specified, just update state here
+                    if (dndStatus == Status.DOWNLOAD_ERROR){
+                        updateBuilder.setState(ObjectInDataStoreStateMachine.State.Failed);
+                    } else if (dndStatus == Status.DOWNLOAD_IN_PROGRESS){
+                        updateBuilder.setState(ObjectInDataStoreStateMachine.State.Creating2);
+                    } else if (dndStatus == Status.DOWNLOADED){
+                        updateBuilder.setState(ObjectInDataStoreStateMachine.State.Ready);
+                    }
+                }
+           // }
 			_vmTemplateStoreDao.update(getTemplateStoreId(), updateBuilder);
 
 			if (answer.getCheckSum() != null) {
 				VMTemplateVO templateDaoBuilder = _vmTemplateDao.createForUpdate();
 				templateDaoBuilder.setChecksum(answer.getCheckSum());
-				_vmTemplateDao.update(template.getId(), templateDaoBuilder);
+				_vmTemplateDao.update(_template.getId(), templateDaoBuilder);
 			}
 
             if (answer.getTemplateSize() > 0) {
                 //long hostId = vmTemplateHostDao.findByTemplateId(template.getId()).getHostId();
-                long accountId = template.getAccountId();
+                long accountId = _template.getAccountId();
                 try {
                     _resourceLimitMgr.checkResourceLimit(_accountMgr.getAccount(accountId),
                             com.cloud.configuration.Resource.ResourceType.secondary_storage,
-                            answer.getTemplateSize() - UriUtils.getRemoteSize(template.getUrl()));
+                            answer.getTemplateSize() - UriUtils.getRemoteSize(_template.getUrl()));
                 } catch (ResourceAllocationException e) {
                     s_logger.warn(e.getMessage());
-                    _alertMgr.sendAlert(_alertMgr.ALERT_TYPE_RESOURCE_LIMIT_EXCEEDED, sserver.getDataCenterId(),
+                    _alertMgr.sendAlert(_alertMgr.ALERT_TYPE_RESOURCE_LIMIT_EXCEEDED, _sserver.getDataCenterId(),
                             null, e.getMessage(), e.getMessage());
                 } finally {
                     _resourceLimitMgr.recalculateResourceCount(accountId, _accountMgr.getAccount(accountId).getDomainId(),
                             com.cloud.configuration.Resource.ResourceType.secondary_storage.getOrdinal());
                 }
             }
-            // only invoke callback when Download is completed or errored so that callback will update template_store_ref state column
-            Status dndStatus = answer.getDownloadStatus();
-            if (dndStatus == Status.DOWNLOAD_ERROR || dndStatus == Status.DOWNLOADED ){
-                if ( callback != null ){
-                    CreateCmdResult result = new CreateCmdResult(null, null);
-                    callback.complete(result);
-                }
-            }
+
 		} else {
-	        VolumeHostVO updateBuilder = volumeHostDao.createForUpdate();
+	        VolumeHostVO updateBuilder = _volumeHostDao.createForUpdate();
 			updateBuilder.setDownloadPercent(answer.getDownloadPct());
 			updateBuilder.setDownloadState(answer.getDownloadStatus());
 			updateBuilder.setLastUpdated(new Date());
@@ -424,25 +463,25 @@ public class DownloadListener implements Listener {
 			updateBuilder.setSize(answer.getTemplateSize());
 			updateBuilder.setPhysicalSize(answer.getTemplatePhySicalSize());
 
-			volumeHostDao.update(getVolumeHostId(), updateBuilder);
+			_volumeHostDao.update(getVolumeHostId(), updateBuilder);
 
 			// Update volume size in Volume table.
 			VolumeVO updateVolume = _volumeDao.createForUpdate();
 			updateVolume.setSize(answer.getTemplateSize());
-			_volumeDao.update(volume.getId(), updateVolume);
+			_volumeDao.update(_volume.getId(), updateVolume);
 
             if (answer.getTemplateSize() > 0) {
                 try {
-                    String url = volumeHostDao.findByVolumeId(volume.getId()).getDownloadUrl();
-                    _resourceLimitMgr.checkResourceLimit(_accountMgr.getAccount(volume.getAccountId()),
+                    String url = _volumeHostDao.findByVolumeId(_volume.getId()).getDownloadUrl();
+                    _resourceLimitMgr.checkResourceLimit(_accountMgr.getAccount(_volume.getAccountId()),
                             com.cloud.configuration.Resource.ResourceType.secondary_storage,
                             answer.getTemplateSize() - UriUtils.getRemoteSize(url));
                 } catch (ResourceAllocationException e) {
                     s_logger.warn(e.getMessage());
-                    _alertMgr.sendAlert(_alertMgr.ALERT_TYPE_RESOURCE_LIMIT_EXCEEDED, volume.getDataCenterId(),
-                            volume.getPodId(), e.getMessage(), e.getMessage());
+                    _alertMgr.sendAlert(_alertMgr.ALERT_TYPE_RESOURCE_LIMIT_EXCEEDED, _volume.getDataCenterId(),
+                            _volume.getPodId(), e.getMessage(), e.getMessage());
                 } finally {
-                    _resourceLimitMgr.recalculateResourceCount(volume.getAccountId(), volume.getDomainId(),
+                    _resourceLimitMgr.recalculateResourceCount(_volume.getAccountId(), _volume.getDomainId(),
                             com.cloud.configuration.Resource.ResourceType.secondary_storage.getOrdinal());
                 }
             }
@@ -474,8 +513,12 @@ public class DownloadListener implements Listener {
 	@Override
 	public void processConnect(HostVO agent, StartupCommand cmd, boolean forRebalance) throws ConnectionException {
 	    if (cmd instanceof StartupRoutingCommand) {
-
-	        downloadMonitor.handleSysTemplateDownload(agent);
+	        List<HypervisorType> hypers = _resourceMgr.listAvailHypervisorInZone(agent.getId(), agent.getDataCenterId());
+	        HypervisorType hostHyper = agent.getHypervisorType();
+	        if (hypers.contains(hostHyper)) {
+	            return;
+	        }
+	        _imageSrv.handleSysTemplateDownload(hostHyper, agent.getDataCenterId());
 	    }
 	    /* This can be removed since
 	    else if ( cmd instanceof StartupStorageCommand) {
@@ -488,16 +531,20 @@ public class DownloadListener implements Listener {
             }
 	    }*/
 	    else if ( cmd instanceof StartupSecondaryStorageCommand ) {
-	        downloadMonitor.handleSync(agent.getDataCenterId());
+	        List<DataStore> imageStores = this._storeMgr.getImageStoresByScope(new ZoneScope(agent.getDataCenterId()));
+	        for (DataStore store : imageStores){
+	            _volumeSrv.handleVolumeSync(store);
+	            _imageSrv.handleTemplateSync(store);
+	        }
 	    }
 	}
 
 	public void setCommand(DownloadCommand _cmd) {
-		this.cmd = _cmd;
+		this._cmd = _cmd;
 	}
 
 	public DownloadCommand getCommand() {
-		return cmd;
+		return _cmd;
 	}
 
 
@@ -506,63 +553,63 @@ public class DownloadListener implements Listener {
 	}
 
 	public void setJobId(String _jobId) {
-		this.jobId = _jobId;
+		this._jobId = _jobId;
 	}
 
 	public String getJobId() {
-		return jobId;
+		return _jobId;
 	}
 
 	public void scheduleStatusCheck(RequestType request) {
-		if (statusTask != null) statusTask.cancel();
+		if (_statusTask != null) _statusTask.cancel();
 
-		statusTask = new StatusTask(this, request);
-		timer.schedule(statusTask, STATUS_POLL_INTERVAL);
+		_statusTask = new StatusTask(this, request);
+		_timer.schedule(_statusTask, STATUS_POLL_INTERVAL);
 	}
 
 	public void scheduleTimeoutTask(long delay) {
-		if (timeoutTask != null) timeoutTask.cancel();
+		if (_timeoutTask != null) _timeoutTask.cancel();
 
-		timeoutTask = new TimeoutTask(this);
-		timer.schedule(timeoutTask, delay);
+		_timeoutTask = new TimeoutTask(this);
+		_timer.schedule(_timeoutTask, delay);
 		if (s_logger.isDebugEnabled()) {
 			log("Scheduling timeout at " + delay + " ms", Level.DEBUG);
 		}
 	}
 
 	public void scheduleImmediateStatusCheck(RequestType request) {
-		if (statusTask != null) statusTask.cancel();
-		statusTask = new StatusTask(this, request);
-		timer.schedule(statusTask, SMALL_DELAY);
+		if (_statusTask != null) _statusTask.cancel();
+		_statusTask = new StatusTask(this, request);
+		_timer.schedule(_statusTask, SMALL_DELAY);
 	}
 
 	public boolean isDownloadActive() {
-		return downloadActive;
+		return _downloadActive;
 	}
 
 	public void cancelStatusTask() {
-		if (statusTask != null) statusTask.cancel();
+		if (_statusTask != null) _statusTask.cancel();
 	}
 
 	public Date getLastUpdated() {
-		return lastUpdated;
+		return _lastUpdated;
 	}
 
 	public void setLastUpdated() {
-		lastUpdated  = new Date();
+		_lastUpdated  = new Date();
 	}
 
 	public void setDownloadInactive(Status reason) {
-		downloadActive=false;
-		if (template != null){
-			downloadMonitor.handleDownloadEvent(sserver, template, reason);
+		_downloadActive=false;
+		if (_template != null){
+			_downloadMonitor.handleDownloadEvent(_sserver, _template, reason);
 		}else {
-			downloadMonitor.handleDownloadEvent(sserver, volume, reason);
+			_downloadMonitor.handleDownloadEvent(_sserver, _volume, reason);
 		}
 	}
 
 	public void cancelTimeoutTask() {
-		if (timeoutTask != null) timeoutTask.cancel();
+		if (_timeoutTask != null) _timeoutTask.cancel();
 	}
 
 	public void logDownloadStart() {


Mime
View raw message