cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From edi...@apache.org
Subject [1/2] refactor downloadlistener, sync system vm templates when adding a new image store
Date Mon, 22 Apr 2013 05:46:42 GMT
Updated Branches:
  refs/heads/object_store 0229c75b5 -> abf40435f


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/abf40435/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
index ab91059..36fcbb7 100755
--- a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
+++ b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
@@ -120,6 +120,7 @@ import com.cloud.exception.InvalidParameterValueException;
 import com.cloud.exception.OperationTimedoutException;
 import com.cloud.exception.ResourceUnavailableException;
 import com.cloud.exception.StorageUnavailableException;
+import com.cloud.host.Host;
 import com.cloud.host.HostVO;
 import com.cloud.host.Status;
 import com.cloud.host.dao.HostDao;
@@ -3539,7 +3540,7 @@ public class VirtualNetworkApplianceManagerImpl extends ManagerBase implements V
     }
 
     @Override
-    public void processConnect(HostVO host, StartupCommand cmd, boolean forRebalance) throws ConnectionException {
+    public void processConnect(Host host, StartupCommand cmd, boolean forRebalance) throws ConnectionException {
         UserContext context = UserContext.current();
         context.setAccountId(1);
         List<DomainRouterVO> routers = _routerDao.listIsolatedByHostId(host.getId());

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/abf40435/server/src/com/cloud/network/security/SecurityGroupListener.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/security/SecurityGroupListener.java b/server/src/com/cloud/network/security/SecurityGroupListener.java
index 3245253..0c101f2 100755
--- a/server/src/com/cloud/network/security/SecurityGroupListener.java
+++ b/server/src/com/cloud/network/security/SecurityGroupListener.java
@@ -38,7 +38,7 @@ import com.cloud.agent.api.StartupRoutingCommand;
 import com.cloud.agent.api.SecurityGroupRuleAnswer.FailureReason;
 import com.cloud.agent.manager.Commands;
 import com.cloud.exception.AgentUnavailableException;
-import com.cloud.host.HostVO;
+import com.cloud.host.Host;
 import com.cloud.host.Status;
 import com.cloud.network.security.SecurityGroupWork.Step;
 import com.cloud.network.security.dao.SecurityGroupWorkDao;
@@ -157,7 +157,7 @@ public class SecurityGroupListener implements Listener {
 
 
     @Override
-    public void processConnect(HostVO host, StartupCommand cmd, boolean forRebalance) {
+    public void processConnect(Host host, StartupCommand cmd, boolean forRebalance) {
         if(s_logger.isInfoEnabled())
             s_logger.info("Received a host startup notification");
 

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/abf40435/server/src/com/cloud/storage/LocalStoragePoolListener.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/storage/LocalStoragePoolListener.java b/server/src/com/cloud/storage/LocalStoragePoolListener.java
index 244f7fb..088d601 100755
--- a/server/src/com/cloud/storage/LocalStoragePoolListener.java
+++ b/server/src/com/cloud/storage/LocalStoragePoolListener.java
@@ -32,7 +32,7 @@ import com.cloud.agent.api.StoragePoolInfo;
 import com.cloud.capacity.dao.CapacityDao;
 import com.cloud.dc.dao.DataCenterDao;
 import com.cloud.exception.ConnectionException;
-import com.cloud.host.HostVO;
+import com.cloud.host.Host;
 import com.cloud.host.Status;
 import com.cloud.storage.dao.StoragePoolHostDao;
 import com.cloud.utils.db.DB;
@@ -67,7 +67,7 @@ public class LocalStoragePoolListener implements Listener {
     
     @Override
     @DB
-    public void processConnect(HostVO host, StartupCommand cmd, boolean forRebalance) throws ConnectionException {
+    public void processConnect(Host host, StartupCommand cmd, boolean forRebalance) throws ConnectionException {
         if (!(cmd instanceof StartupStorageCommand)) {
             return;
         }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/abf40435/server/src/com/cloud/storage/download/DownloadAbandonedState.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/storage/download/DownloadAbandonedState.java b/server/src/com/cloud/storage/download/DownloadAbandonedState.java
index 200683c..ef053ce 100644
--- a/server/src/com/cloud/storage/download/DownloadAbandonedState.java
+++ b/server/src/com/cloud/storage/download/DownloadAbandonedState.java
@@ -16,6 +16,7 @@
 // under the License.
 package com.cloud.storage.download;
 
+import com.cloud.agent.api.storage.DownloadAnswer;
 import com.cloud.agent.api.storage.DownloadProgressCommand.RequestType;
 import com.cloud.storage.VMTemplateStorageResourceAssoc.Status;
 
@@ -34,7 +35,8 @@ public class DownloadAbandonedState extends DownloadInactiveState {
 	public void onEntry(String prevState, DownloadEvent event, Object evtObj) {
 		super.onEntry(prevState, event, evtObj);
 		if (!prevState.equalsIgnoreCase(getName())){
-			getDownloadListener().updateDatabase(Status.ABANDONED, "Download canceled");
+			DownloadAnswer answer = new DownloadAnswer("Download canceled", Status.ABANDONED);
+			getDownloadListener().callback(answer);
 			getDownloadListener().cancelStatusTask();
 			getDownloadListener().cancelTimeoutTask();
 			getDownloadListener().sendCommand(RequestType.ABORT);

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/abf40435/server/src/com/cloud/storage/download/DownloadActiveState.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/storage/download/DownloadActiveState.java b/server/src/com/cloud/storage/download/DownloadActiveState.java
index f2cd5af..09d103e 100644
--- a/server/src/com/cloud/storage/download/DownloadActiveState.java
+++ b/server/src/com/cloud/storage/download/DownloadActiveState.java
@@ -64,7 +64,7 @@ public abstract class DownloadActiveState extends DownloadState {
 		}
 		
 		if (event==DownloadEvent.DOWNLOAD_ANSWER) {
-			getDownloadListener().updateDatabase((DownloadAnswer)evtObj);
+			getDownloadListener().callback((DownloadAnswer)evtObj);
 			getDownloadListener().setLastUpdated();
 		}
 		

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/abf40435/server/src/com/cloud/storage/download/DownloadErrorState.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/storage/download/DownloadErrorState.java b/server/src/com/cloud/storage/download/DownloadErrorState.java
index 0fdfd52..e5c8820 100644
--- a/server/src/com/cloud/storage/download/DownloadErrorState.java
+++ b/server/src/com/cloud/storage/download/DownloadErrorState.java
@@ -76,10 +76,12 @@ public class DownloadErrorState extends DownloadInactiveState {
 			getDownloadListener().logDisconnect();
 			getDownloadListener().cancelStatusTask();
 			getDownloadListener().cancelTimeoutTask();
-			getDownloadListener().updateDatabase(Status.DOWNLOAD_ERROR, "Storage agent or storage VM disconnected");  
+			DownloadAnswer answer = new DownloadAnswer("Storage agent or storage VM disconnected", Status.DOWNLOAD_ERROR);
+			getDownloadListener().callback(answer);  
 			getDownloadListener().log("Entering download error state because the storage host disconnected", Level.WARN);
 		} else if (event==DownloadEvent.TIMEOUT_CHECK){
-			getDownloadListener().updateDatabase(Status.DOWNLOAD_ERROR, "Timeout waiting for response from storage host");
+			DownloadAnswer answer = new DownloadAnswer("Timeout waiting for response from storage host", Status.DOWNLOAD_ERROR);
+			getDownloadListener().callback(answer);
 			getDownloadListener().log("Entering download error state: timeout waiting for response from storage host", Level.WARN);
 		}
 		getDownloadListener().setDownloadInactive(Status.DOWNLOAD_ERROR);

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/abf40435/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 3eb2233..e54e879 100755
--- a/server/src/com/cloud/storage/download/DownloadListener.java
+++ b/server/src/com/cloud/storage/download/DownloadListener.java
@@ -25,17 +25,15 @@ import java.util.TimerTask;
 
 import javax.inject.Inject;
 
-import org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult;
+import org.apache.cloudstack.engine.subsystem.api.storage.DataObject;
+import org.apache.cloudstack.engine.subsystem.api.storage.DataObjectType;
 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.EndPoint;
 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;
 
@@ -47,35 +45,19 @@ import com.cloud.agent.api.Command;
 import com.cloud.agent.api.StartupCommand;
 import com.cloud.agent.api.StartupRoutingCommand;
 import com.cloud.agent.api.StartupSecondaryStorageCommand;
-import com.cloud.agent.api.StartupStorageCommand;
 import com.cloud.agent.api.storage.DownloadAnswer;
 import com.cloud.agent.api.storage.DownloadCommand;
 import com.cloud.agent.api.storage.DownloadCommand.ResourceType;
 import com.cloud.agent.api.storage.DownloadProgressCommand;
 import com.cloud.agent.api.storage.DownloadProgressCommand.RequestType;
-import com.cloud.alert.AlertManager;
 import com.cloud.exception.AgentUnavailableException;
 import com.cloud.exception.ConnectionException;
-import com.cloud.exception.ResourceAllocationException;
-import com.cloud.host.HostVO;
-import com.cloud.host.dao.HostDao;
+import com.cloud.host.Host;
 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;
 import com.cloud.storage.VMTemplateStorageResourceAssoc.Status;
-import com.cloud.storage.VMTemplateVO;
-import com.cloud.storage.VolumeHostVO;
-import com.cloud.storage.VolumeVO;
-import com.cloud.storage.dao.VMTemplateDao;
-import com.cloud.storage.dao.VMTemplateHostDao;
-import com.cloud.storage.dao.VolumeDao;
-import com.cloud.storage.dao.VolumeHostDao;
 import com.cloud.storage.download.DownloadState.DownloadEvent;
-import com.cloud.user.AccountManager;
-import com.cloud.user.ResourceLimitService;
-import com.cloud.utils.UriUtils;
 import com.cloud.utils.exception.CloudRuntimeException;
 
 /**
@@ -125,25 +107,11 @@ public class DownloadListener implements Listener {
 	public static final String DOWNLOAD_IN_PROGRESS=Status.DOWNLOAD_IN_PROGRESS.toString();
 	public static final String DOWNLOAD_ABANDONED=Status.ABANDONED.toString();
 
+	private EndPoint _ssAgent;
 
-	private HostVO _sserver;
-	private HostVO _ssAgent;
+	private DataObject object;
 
-	private VMTemplateVO _template;
-	private VolumeVO _volume;
 	private boolean _downloadActive = true;
-
-	private VolumeHostDao _volumeHostDao;
-	private VolumeDataStoreDao _volumeStoreDao;
-	private VolumeDao _volumeDao;
-	private StorageManager _storageMgr;
-	private VMTemplateHostDao _vmTemplateHostDao;
-	private TemplateDataStoreDao _vmTemplateStoreDao;
-	private VMTemplateDao _vmTemplateDao;
-	private ResourceLimitService _resourceLimitMgr;
-	private AccountManager _accountMgr;
-	private AlertManager _alertMgr;
-
 	private final DownloadMonitorImpl _downloadMonitor;
 
 	private DownloadState _currState;
@@ -158,13 +126,7 @@ public class DownloadListener implements Listener {
 	private String _jobId;
 
 	private final Map<String,  DownloadState> _stateMap = new HashMap<String, DownloadState>();
-	private Long _templateHostId;
-	private Long _volumeHostId;
-
-    private DataStore _sstore;
-    private Long _templateStoreId;
-    private Long _volumeStoreId;
-	private AsyncCompletionCallback<CreateCmdResult> _callback;
+	private AsyncCompletionCallback<DownloadAnswer> _callback;
 
     @Inject
     private ResourceManager _resourceMgr;
@@ -175,71 +137,25 @@ public class DownloadListener implements Listener {
     @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;
-		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._vmTemplateDao = templateDao;
-		this._resourceLimitMgr = _resourceLimitMgr;
-		this._accountMgr = _accountMgr;
-		this._alertMgr = _alertMgr;
-		updateDatabase(Status.NOT_DOWNLOADED, "");
-	}
-
 	// 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) {
+    public DownloadListener(EndPoint ssAgent, DataStore store, DataObject object, Timer _timer, DownloadMonitorImpl downloadMonitor, DownloadCommand cmd, AsyncCompletionCallback<DownloadAnswer> callback) {
         this._ssAgent = ssAgent;
-        this._sstore = store;
-        this._template = template;
-        this._vmTemplateStoreDao = dao;
+        this.object = object;
         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._vmTemplateDao = templateDao;
-        this._resourceLimitMgr = _resourceLimitMgr;
-        this._accountMgr = _accountMgr;
-        this._alertMgr = _alertMgr;
         this._callback = callback;
-        updateDatabase(Status.NOT_DOWNLOADED, "");
+        DownloadAnswer answer = new DownloadAnswer("", Status.NOT_DOWNLOADED);
+        callback(answer);
+    }
+    
+    public AsyncCompletionCallback<DownloadAnswer> getCallback() {
+    	return this._callback;
     }
-
-
-	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._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());
@@ -264,7 +180,7 @@ public class DownloadListener implements Listener {
 			}
 			try {
 				DownloadProgressCommand dcmd = new DownloadProgressCommand(getCommand(), getJobId(), reqType);
-				if (_template == null){
+				if (this.object.getType() == DataObjectType.VOLUME) {
 					dcmd.setResourceType(ResourceType.VOLUME);
 				}
 	            _downloadMonitor.send(_ssAgent.getId(), dcmd, this);
@@ -285,59 +201,12 @@ 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());
-		}else {
-			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();
-			vo.setDownloadState(state);
-			vo.setLastUpdated(new Date());
-			vo.setErrorString(errorString);
-			_vmTemplateHostDao.update(getTemplateHostId(), vo);
-		}else {
-		    VolumeHostVO vo = _volumeHostDao.createForUpdate();
-			vo.setDownloadState(state);
-			vo.setLastUpdated(new Date());
-			vo.setErrorString(errorString);
-			_volumeHostDao.update(getVolumeHostId(), vo);
-		}
+			s_logger.warn("Unable to monitor download progress of " + this.object.getType() + ": " + 
+					this.object.getId() + " at host " + _ssAgent.getId());
 	}
 
 	public void log(String message, Level level) {
-		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());
-		}
-	}
-
-	private Long getTemplateHostId() {
-		if (_templateHostId == null){
-			VMTemplateHostVO templHost = _vmTemplateHostDao.findByHostTemplate(_sserver.getId(), _template.getId());
-			_templateHostId = templHost.getId();
-		}
-		return _templateHostId;
-	}
-
-    private Long getTemplateStoreId() {
-        if (_templateStoreId == null){
-            TemplateDataStoreVO templStore = _vmTemplateStoreDao.findByStoreTemplate(_sstore.getId(), _template.getId());
-            _templateStoreId = templStore.getId();
-        }
-        return _templateStoreId;
-    }
-
-	private Long getVolumeHostId() {
-		if (_volumeHostId == null){
-			VolumeHostVO volHost = _volumeHostDao.findByHostVolume(_sserver.getId(), _volume.getId());
-			_volumeHostId = volHost.getId();
-		}
-		return _volumeHostId;
+		s_logger.log(level, message + ", " + this.object.getType() + ": " + this.object.getId() + " at host " + _ssAgent.getId());
 	}
 
 	public DownloadListener(DownloadMonitorImpl monitor) {
@@ -388,112 +257,10 @@ public class DownloadListener implements Listener {
 		}
 	}
 
-	public synchronized void updateDatabase(DownloadAnswer answer) {
-		if (_template != null){
-	        TemplateDataStoreVO updateBuilder = _vmTemplateStoreDao.createForUpdate();
-			updateBuilder.setDownloadPercent(answer.getDownloadPct());
-			updateBuilder.setDownloadState(answer.getDownloadStatus());
-			updateBuilder.setLastUpdated(new Date());
-			updateBuilder.setErrorString(answer.getErrorString());
-			updateBuilder.setJobId(answer.getJobId());
-			updateBuilder.setLocalDownloadPath(answer.getDownloadPath());
-			updateBuilder.setInstallPath(answer.getInstallPath());
-			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);
-			}
-
-            if (answer.getTemplateSize() > 0) {
-                //long hostId = vmTemplateHostDao.findByTemplateId(template.getId()).getHostId();
-                long accountId = _template.getAccountId();
-                try {
-                    _resourceLimitMgr.checkResourceLimit(_accountMgr.getAccount(accountId),
-                            com.cloud.configuration.Resource.ResourceType.secondary_storage,
-                            answer.getTemplateSize() - UriUtils.getRemoteSize(_template.getUrl()));
-                } catch (ResourceAllocationException e) {
-                    s_logger.warn(e.getMessage());
-                    _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());
-                }
-            }
-
-		} else {
-	        VolumeHostVO updateBuilder = _volumeHostDao.createForUpdate();
-			updateBuilder.setDownloadPercent(answer.getDownloadPct());
-			updateBuilder.setDownloadState(answer.getDownloadStatus());
-			updateBuilder.setLastUpdated(new Date());
-			updateBuilder.setErrorString(answer.getErrorString());
-			updateBuilder.setJobId(answer.getJobId());
-			updateBuilder.setLocalDownloadPath(answer.getDownloadPath());
-			updateBuilder.setInstallPath(answer.getInstallPath());
-			updateBuilder.setSize(answer.getTemplateSize());
-			updateBuilder.setPhysicalSize(answer.getTemplatePhySicalSize());
-
-			_volumeHostDao.update(getVolumeHostId(), updateBuilder);
-
-			// Update volume size in Volume table.
-			VolumeVO updateVolume = _volumeDao.createForUpdate();
-			updateVolume.setSize(answer.getTemplateSize());
-			_volumeDao.update(_volume.getId(), updateVolume);
-
-            if (answer.getTemplateSize() > 0) {
-                try {
-                    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());
-                } finally {
-                    _resourceLimitMgr.recalculateResourceCount(_volume.getAccountId(), _volume.getDomainId(),
-                            com.cloud.configuration.Resource.ResourceType.secondary_storage.getOrdinal());
-                }
-            }
-
-			/*if (answer.getCheckSum() != null) {
-				VMTemplateVO templateDaoBuilder = _vmTemplateDao.createForUpdate();
-				templateDaoBuilder.setChecksum(answer.getCheckSum());
-				_vmTemplateDao.update(template.getId(), templateDaoBuilder);
-			}*/
-		}
- 	}
-
+	public void callback(DownloadAnswer answer) {
+		this._callback.complete(answer);
+	}
+	
 	@Override
 	public boolean processCommands(long agentId, long seq, Command[] req) {
 		return false;
@@ -511,7 +278,7 @@ public class DownloadListener implements Listener {
 	}
 
 	@Override
-	public void processConnect(HostVO agent, StartupCommand cmd, boolean forRebalance) throws ConnectionException {
+	public void processConnect(Host agent, StartupCommand cmd, boolean forRebalance) throws ConnectionException {
 	    if (cmd instanceof StartupRoutingCommand) {
 	        List<HypervisorType> hypers = _resourceMgr.listAvailHypervisorInZone(agent.getId(), agent.getDataCenterId());
 	        HypervisorType hostHyper = agent.getHypervisorType();
@@ -601,11 +368,6 @@ public class DownloadListener implements Listener {
 
 	public void setDownloadInactive(Status reason) {
 		_downloadActive=false;
-		if (_template != null){
-			_downloadMonitor.handleDownloadEvent(_sserver, _template, reason);
-		}else {
-			_downloadMonitor.handleDownloadEvent(_sserver, _volume, reason);
-		}
 	}
 
 	public void cancelTimeoutTask() {

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/abf40435/server/src/com/cloud/storage/download/DownloadMonitor.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/storage/download/DownloadMonitor.java b/server/src/com/cloud/storage/download/DownloadMonitor.java
index efbdbe2..7bc210f 100644
--- a/server/src/com/cloud/storage/download/DownloadMonitor.java
+++ b/server/src/com/cloud/storage/download/DownloadMonitor.java
@@ -16,15 +16,15 @@
 // under the License.
 package com.cloud.storage.download;
 
-import org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult;
+import org.apache.cloudstack.engine.subsystem.api.storage.DataObject;
 import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
 import org.apache.cloudstack.framework.async.AsyncCompletionCallback;
 
-
+import com.cloud.agent.api.storage.DownloadAnswer;
 import com.cloud.exception.StorageUnavailableException;
+import com.cloud.storage.Storage.ImageFormat;
 import com.cloud.storage.VMTemplateVO;
 import com.cloud.storage.VolumeVO;
-import com.cloud.storage.Storage.ImageFormat;
 import com.cloud.utils.component.Manager;
 
 /**
@@ -34,19 +34,15 @@ import com.cloud.utils.component.Manager;
 public interface DownloadMonitor extends Manager{
 
     // when ssvm is not available yet
-    public void downloadBootstrapSysTemplateToStorage(VMTemplateVO template, DataStore store, AsyncCompletionCallback<CreateCmdResult> callback);
-
-    public void downloadTemplateToStorage(VMTemplateVO template, DataStore store, AsyncCompletionCallback<CreateCmdResult> callback);
-
-	public void cancelAllDownloads(Long templateId);
+    public void downloadBootstrapSysTemplateToStorage(VMTemplateVO template, DataStore store, AsyncCompletionCallback<DownloadAnswer> callback);
 
-	public boolean copyTemplate(VMTemplateVO template, DataStore sourceStore, DataStore Store)
-			throws StorageUnavailableException;
+    public void downloadTemplateToStorage(DataObject template, DataStore store, AsyncCompletionCallback<DownloadAnswer> callback);
 
-    //void addSystemVMTemplatesToHost(HostVO host, Map<String, TemplateProp> templateInfos);
+	//public void cancelAllDownloads(Long templateId);
 
-	//public boolean downloadVolumeToStorage(VolumeVO volume, Long zoneId, String url, String checkSum, ImageFormat format);
+	//public boolean copyTemplate(VMTemplateVO template, DataStore sourceStore, DataStore Store)
+	//		throws StorageUnavailableException;
 
-	public void downloadVolumeToStorage(VolumeVO volume, DataStore store, String url, String checkSum, ImageFormat format, AsyncCompletionCallback<CreateCmdResult> callback);
+	public void downloadVolumeToStorage(DataObject volume, DataStore store, String url, String checkSum, ImageFormat format, AsyncCompletionCallback<DownloadAnswer> callback);
 
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/abf40435/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 c7b360a..b72c202 100755
--- a/server/src/com/cloud/storage/download/DownloadMonitorImpl.java
+++ b/server/src/com/cloud/storage/download/DownloadMonitorImpl.java
@@ -27,14 +27,14 @@ 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.DataObject;
 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.EndPoint;
 import org.apache.cloudstack.engine.subsystem.api.storage.EndPointSelector;
 import org.apache.cloudstack.engine.subsystem.api.storage.TemplateDataFactory;
+import org.apache.cloudstack.framework.async.AsyncCompletionCallback;
 import org.apache.cloudstack.storage.datastore.db.ImageStoreDao;
-import org.apache.cloudstack.storage.datastore.db.ImageStoreVO;
 import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreDao;
 import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO;
 import org.apache.cloudstack.storage.datastore.db.VolumeDataStoreDao;
@@ -45,55 +45,35 @@ import org.springframework.stereotype.Component;
 import com.cloud.agent.AgentManager;
 import com.cloud.agent.Listener;
 import com.cloud.agent.api.Command;
-
+import com.cloud.agent.api.storage.DownloadAnswer;
 import com.cloud.agent.api.storage.DownloadCommand;
-
 import com.cloud.agent.api.storage.DownloadCommand.Proxy;
 import com.cloud.agent.api.storage.DownloadCommand.ResourceType;
+import com.cloud.agent.api.storage.DownloadProgressCommand;
 import com.cloud.agent.api.storage.DownloadProgressCommand.RequestType;
 import com.cloud.agent.api.storage.DownloadSystemTemplateCommand;
-
-import com.cloud.agent.api.storage.DownloadProgressCommand;
 import com.cloud.agent.manager.Commands;
 import com.cloud.alert.AlertManager;
 import com.cloud.configuration.Config;
 import com.cloud.configuration.dao.ConfigurationDao;
-import com.cloud.dc.dao.ClusterDao;
-import com.cloud.dc.dao.DataCenterDao;
-import com.cloud.event.EventTypes;
-import com.cloud.event.UsageEventUtils;
-import com.cloud.event.dao.UsageEventDao;
 import com.cloud.exception.AgentUnavailableException;
-import com.cloud.exception.InvalidParameterValueException;
-import com.cloud.exception.StorageUnavailableException;
 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.ImageFormat;
-
 import com.cloud.storage.StorageManager;
 import com.cloud.storage.VMTemplateHostVO;
 import com.cloud.storage.VMTemplateStorageResourceAssoc;
-import com.cloud.storage.VMTemplateVO;
 import com.cloud.storage.VMTemplateStorageResourceAssoc.Status;
+import com.cloud.storage.VMTemplateVO;
+import com.cloud.storage.Volume;
 import com.cloud.storage.VolumeHostVO;
-import com.cloud.storage.VolumeVO;
-import com.cloud.storage.dao.StoragePoolHostDao;
-import com.cloud.storage.dao.SwiftDao;
 import com.cloud.storage.dao.VMTemplateDao;
-import com.cloud.storage.dao.VMTemplateHostDao;
-import com.cloud.storage.dao.VMTemplatePoolDao;
-import com.cloud.storage.dao.VMTemplateSwiftDao;
-import com.cloud.storage.dao.VMTemplateZoneDao;
 import com.cloud.storage.dao.VolumeDao;
 import com.cloud.storage.dao.VolumeHostDao;
-
 import com.cloud.storage.secondary.SecondaryStorageVmManager;
 import com.cloud.storage.swift.SwiftManager;
 import com.cloud.storage.template.TemplateConstants;
 import com.cloud.template.TemplateManager;
-import com.cloud.user.Account;
+import com.cloud.template.VirtualMachineTemplate;
 import com.cloud.user.AccountManager;
 import com.cloud.user.ResourceLimitService;
 import com.cloud.utils.component.ManagerBase;
@@ -101,37 +81,17 @@ import com.cloud.utils.db.DB;
 import com.cloud.utils.db.JoinBuilder;
 import com.cloud.utils.db.SearchBuilder;
 import com.cloud.utils.db.SearchCriteria;
-import com.cloud.utils.db.Transaction;
-import com.cloud.utils.exception.CloudRuntimeException;
-import com.cloud.vm.SecondaryStorageVm;
-import com.cloud.vm.SecondaryStorageVmVO;
 import com.cloud.vm.UserVmManager;
-import com.cloud.vm.VirtualMachine.State;
-import com.cloud.vm.dao.SecondaryStorageVmDao;
 import com.cloud.vm.dao.UserVmDao;
 
-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
-    VMTemplateHostDao _vmTemplateHostDao;
-    @Inject
     TemplateDataStoreDao _vmTemplateStoreDao;
     @Inject
-    VMTemplateZoneDao _vmTemplateZoneDao;
-    @Inject
-    VMTemplatePoolDao _vmTemplatePoolDao;
-    @Inject
-    VMTemplateSwiftDao _vmTemplateSwiftlDao;
-    @Inject
-    StoragePoolHostDao _poolHostDao;
-    @Inject
-    SecondaryStorageVmDao _secStorageVmDao;
-    @Inject
     ImageStoreDao _imageStoreDao;
     @Inject
     VolumeDao _volumeDao;
@@ -147,9 +107,6 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor
     SecondaryStorageVmManager _ssvmMgr;
     @Inject
     StorageManager _storageMgr;
-
-    @Inject
-    private final DataCenterDao _dcDao = null;
     @Inject
     VMTemplateDao _templateDao = null;
     @Inject
@@ -165,17 +122,6 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor
     TemplateManager templateMgr;
 
     @Inject
-    private UsageEventDao _usageEventDao;
-
-    @Inject
-    private ClusterDao _clusterDao;
-    @Inject
-    private HostDao _hostDao;
-    @Inject
-    private ResourceManager _resourceMgr;
-    @Inject
-    private SwiftDao _swiftDao;
-    @Inject
     protected ResourceLimitService _resourceLimitMgr;
     @Inject
     protected UserVmDao _userVmDao;
@@ -254,6 +200,7 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor
     }
 
     // TODO: consider using dataMotionStrategy later
+    /*
     @Override
     public boolean copyTemplate(VMTemplateVO template, DataStore sourceStore, DataStore destStore) throws StorageUnavailableException {
 
@@ -363,15 +310,15 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor
 
         VMTemplateVO tmplt = _templateDao.findById(srcTmpltStore.getTemplateId());
         HypervisorType hyperType = tmplt.getHypervisorType();
-        /*No secondary storage vm yet*/
+        
         if (hyperType != null && hyperType == HypervisorType.KVM) {
             //return "file://" + sourceServer.getParent() + "/" + srcTmpltStore.getInstallPath();
             return "file://"  + "/" + srcTmpltStore.getInstallPath();
         }
         return null;
-    }
+    }*/
 
-    private void initiateTemplateDownload(VMTemplateVO template, DataStore store, AsyncCompletionCallback<CreateCmdResult> callback) {
+    private void initiateTemplateDownload(DataObject template, DataStore store, AsyncCompletionCallback<DownloadAnswer> callback) {
         boolean downloadJobExists = false;
         TemplateDataStoreVO vmTemplateStore = null;
 
@@ -380,7 +327,7 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor
             // This method can be invoked other places, for example,
             // handleTemplateSync, in that case, vmTemplateStore may be null
             vmTemplateStore = new TemplateDataStoreVO(store.getId(), template.getId(), new Date(), 0,
-                    VMTemplateStorageResourceAssoc.Status.NOT_DOWNLOADED, null, null, "jobid0000", null, template.getUrl());
+                    VMTemplateStorageResourceAssoc.Status.NOT_DOWNLOADED, null, null, "jobid0000", null, template.getUri());
             _vmTemplateStoreDao.persist(vmTemplateStore);
         } else if ((vmTemplateStore.getJobId() != null) && (vmTemplateStore.getJobId().length() > 2)) {
             downloadJobExists = true;
@@ -390,7 +337,8 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor
         String secUrl = store.getUri();
         if (vmTemplateStore != null) {
             start();
-            DownloadCommand dcmd = new DownloadCommand(store.getTO(), secUrl, template, maxTemplateSizeInBytes);
+            VirtualMachineTemplate tmpl = this._templateDao.findById(template.getId());
+            DownloadCommand dcmd = new DownloadCommand(store.getTO(), secUrl, tmpl, maxTemplateSizeInBytes);
             dcmd.setProxy(getHttpProxy());
             if (downloadJobExists) {
                 dcmd = new DownloadProgressCommand(dcmd, vmTemplateStore.getJobId(), RequestType.GET_OR_RESTART);
@@ -398,13 +346,13 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor
             if (vmTemplateStore.isCopy()) {
                 dcmd.setCreds(TemplateConstants.DEFAULT_HTTP_AUTH_USER, _copyAuthPasswd);
             }
-            HostVO ssAhost = _ssvmMgr.pickSsvmHost(store);
-            if (ssAhost == null) {
+            EndPoint ep = _epSelector.select(template);
+            if (ep == null) {
                 s_logger.warn("There is no secondary storage VM for downloading template to image store " + store.getName());
                 return;
             }
-            DownloadListener dl = new DownloadListener(ssAhost, store, template, _timer, _vmTemplateStoreDao, vmTemplateStore.getId(), this, dcmd,
-                    _templateDao, _resourceLimitMgr, _alertMgr, _accountMgr, callback);
+            DownloadListener dl = new DownloadListener(ep, store, template, _timer, this, dcmd,
+                     callback);
             if (downloadJobExists) {
                 // due to handling existing download job issues, we still keep
                 // downloadState in template_store_ref to avoid big change in
@@ -422,9 +370,9 @@ 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 " + store.getName(), e);
+                ep.sendMessageAsyncWithListener(dcmd, dl);
+            } catch (Exception e) {
+                s_logger.warn("Unable to start /resume download of template " + template.getId() + " to " + store.getName(), e);
                 dl.setDisconnected();
                 dl.scheduleStatusCheck(RequestType.GET_OR_RESTART);
             }
@@ -433,7 +381,7 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor
 
 
     @Override
-    public void downloadBootstrapSysTemplateToStorage(VMTemplateVO template, DataStore store, AsyncCompletionCallback<CreateCmdResult> callback) {
+    public void downloadBootstrapSysTemplateToStorage(VMTemplateVO template, DataStore store, AsyncCompletionCallback<DownloadAnswer> callback) {
         boolean downloadJobExists = false;
         TemplateDataStoreVO vmTemplateStore = null;
 
@@ -495,18 +443,18 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor
     }
 
     @Override
-    public void downloadTemplateToStorage(VMTemplateVO template, DataStore store, AsyncCompletionCallback<CreateCmdResult> callback) {
+    public void downloadTemplateToStorage(DataObject template, DataStore store, AsyncCompletionCallback<DownloadAnswer> callback) {
         long templateId = template.getId();
         if (isTemplateUpdateable(templateId, store.getId())) {
-            if (template != null && template.getUrl() != null) {
+            if (template != null && template.getUri() != null) {
                 initiateTemplateDownload(template, store, callback);
             }
         }
     }
 
     @Override
-    public void downloadVolumeToStorage(VolumeVO volume, DataStore store, String url, String checkSum, ImageFormat format,
-            AsyncCompletionCallback<CreateCmdResult> callback) {
+    public void downloadVolumeToStorage(DataObject volume, DataStore store, String url, String checkSum, ImageFormat format,
+            AsyncCompletionCallback<DownloadAnswer> callback) {
         boolean downloadJobExists = false;
         VolumeDataStoreVO volumeHost = null;
 
@@ -523,20 +471,20 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor
         String secUrl = store.getUri();
         if (volumeHost != null) {
             start();
-            DownloadCommand dcmd = new DownloadCommand(secUrl, volume, maxVolumeSizeInBytes, checkSum, url, format);
+            Volume vol = this._volumeDao.findById(volume.getId());
+            DownloadCommand dcmd = new DownloadCommand(secUrl, vol, maxVolumeSizeInBytes, checkSum, url, format);
             dcmd.setProxy(getHttpProxy());
             if (downloadJobExists) {
                 dcmd = new DownloadProgressCommand(dcmd, volumeHost.getJobId(), RequestType.GET_OR_RESTART);
                 dcmd.setResourceType(ResourceType.VOLUME);
             }
 
-            HostVO ssAhost = _ssvmMgr.pickSsvmHost(store);
-            if (ssAhost == null) {
+            EndPoint ep = this._epSelector.select(volume);
+            if (ep == null) {
                 s_logger.warn("There is no secondary storage VM for image store " + store.getName());
                 return;
             }
-            DownloadListener dl = new DownloadListener(ssAhost, store, volume, _timer, _volumeStoreDao, volumeHost.getId(), this, dcmd, _volumeDao,
-                    _storageMgr, _resourceLimitMgr, _alertMgr, _accountMgr, callback);
+            DownloadListener dl = new DownloadListener(ep, store, volume, _timer, this, dcmd, callback);
 
             if (downloadJobExists) {
                 dl.setCurrState(volumeHost.getDownloadState());
@@ -550,59 +498,26 @@ 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 volume " + volume.getName() + " to " + store.getName(), e);
+                ep.sendMessageAsyncWithListener(dcmd, dl);
+            } catch (Exception e) {
+                s_logger.warn("Unable to start /resume download of volume " + volume.getId() + " to " + store.getName(), e);
                 dl.setDisconnected();
                 dl.scheduleStatusCheck(RequestType.GET_OR_RESTART);
             }
         }
     }
 
-    @DB
-    public void handleDownloadEvent(HostVO host, VMTemplateVO template, Status dnldStatus) {
-        if ((dnldStatus == VMTemplateStorageResourceAssoc.Status.DOWNLOADED) || (dnldStatus == Status.ABANDONED)) {
-            VMTemplateHostVO vmTemplateHost = new VMTemplateHostVO(host.getId(), template.getId());
-            synchronized (_listenerMap) {
-                _listenerMap.remove(vmTemplateHost);
-            }
-        }
-
-        VMTemplateHostVO vmTemplateHost = _vmTemplateHostDao.findByHostTemplate(host.getId(), template.getId());
-
-        Transaction txn = Transaction.currentTxn();
-        txn.start();
-
-        if (dnldStatus == Status.DOWNLOADED) {
-            long size = -1;
-            if (vmTemplateHost != null) {
-                size = vmTemplateHost.getPhysicalSize();
-                template.setSize(size);
-                this._templateDao.update(template.getId(), template);
-            } else {
-                s_logger.warn("Failed to get size for template" + template.getName());
-            }
-            String eventType = EventTypes.EVENT_TEMPLATE_CREATE;
-            if ((template.getFormat()).equals(ImageFormat.ISO)) {
-                eventType = EventTypes.EVENT_ISO_CREATE;
-            }
-            if (template.getAccountId() != Account.ACCOUNT_ID_SYSTEM) {
-                UsageEventUtils.publishUsageEvent(eventType, template.getAccountId(), host.getDataCenterId(), template.getId(), template.getName(),
-                        null, template.getSourceTemplateId(), size, template.getClass().getName(), template.getUuid());
-            }
-        }
-        txn.commit();
-    }
 
     @DB
-    public void handleDownloadEvent(HostVO host, VolumeVO volume, Status dnldStatus) {
-        if ((dnldStatus == VMTemplateStorageResourceAssoc.Status.DOWNLOADED) || (dnldStatus == Status.ABANDONED)) {
+    public void handleDownloadEvent(HostVO host, DataObject object, Status dnldStatus) {
+       /* if ((dnldStatus == VMTemplateStorageResourceAssoc.Status.DOWNLOADED) || (dnldStatus == Status.ABANDONED)) {
             VolumeHostVO volumeHost = new VolumeHostVO(host.getId(), volume.getId());
             synchronized (_listenerVolumeMap) {
                 _listenerVolumeMap.remove(volumeHost);
             }
-        }
+        }*/
 
+        /*
         VolumeHostVO volumeHost = _volumeHostDao.findByHostVolume(host.getId(), volume.getId());
 
         Transaction txn = Transaction.currentTxn();
@@ -631,6 +546,7 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor
                     com.cloud.configuration.Resource.ResourceType.secondary_storage.getOrdinal());
         }
         txn.commit();
+        */
     }
 
     /*
@@ -657,7 +573,7 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor
     }
     */
 
-    @Override
+    /*@Override
     public void cancelAllDownloads(Long templateId) {
         List<VMTemplateHostVO> downloadsInProgress = _vmTemplateHostDao.listByTemplateStates(templateId,
                 VMTemplateHostVO.Status.DOWNLOAD_IN_PROGRESS, VMTemplateHostVO.Status.NOT_DOWNLOADED);
@@ -673,7 +589,7 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor
                 }
             }
         }
-    }
+    }*/
 
     /*
     private void checksumSync(long hostId){

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/abf40435/server/src/com/cloud/storage/download/DownloadState.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/storage/download/DownloadState.java b/server/src/com/cloud/storage/download/DownloadState.java
index 471ab61..9d404f0 100644
--- a/server/src/com/cloud/storage/download/DownloadState.java
+++ b/server/src/com/cloud/storage/download/DownloadState.java
@@ -62,7 +62,7 @@ public abstract class DownloadState {
 			getDownloadListener().log("onEntry, event type=" + event + ", curr state=" + getName(), Level.TRACE);
 		}
 		if (event==DownloadEvent.DOWNLOAD_ANSWER) {
-			getDownloadListener().updateDatabase((DownloadAnswer)evtObj);
+			getDownloadListener().callback((DownloadAnswer)evtObj);
 		}
 	}
 	

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/abf40435/server/src/com/cloud/storage/listener/StoragePoolMonitor.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/storage/listener/StoragePoolMonitor.java b/server/src/com/cloud/storage/listener/StoragePoolMonitor.java
index e035fd7..8c89440 100755
--- a/server/src/com/cloud/storage/listener/StoragePoolMonitor.java
+++ b/server/src/com/cloud/storage/listener/StoragePoolMonitor.java
@@ -32,7 +32,7 @@ import com.cloud.agent.api.Command;
 import com.cloud.agent.api.StartupCommand;
 import com.cloud.agent.api.StartupRoutingCommand;
 import com.cloud.exception.ConnectionException;
-import com.cloud.host.HostVO;
+import com.cloud.host.Host;
 import com.cloud.host.Status;
 import com.cloud.hypervisor.Hypervisor.HypervisorType;
 import com.cloud.storage.OCFS2Manager;
@@ -71,7 +71,7 @@ public class StoragePoolMonitor implements Listener {
     }
 
     @Override
-    public void processConnect(HostVO host, StartupCommand cmd, boolean forRebalance) throws ConnectionException {
+    public void processConnect(Host host, StartupCommand cmd, boolean forRebalance) throws ConnectionException {
         if (cmd instanceof StartupRoutingCommand) {
             StartupRoutingCommand scCmd = (StartupRoutingCommand)cmd;
             if (scCmd.getHypervisorType() == HypervisorType.XenServer || scCmd.getHypervisorType() ==  HypervisorType.KVM ||

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/abf40435/server/src/com/cloud/storage/listener/StorageSyncListener.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/storage/listener/StorageSyncListener.java b/server/src/com/cloud/storage/listener/StorageSyncListener.java
index d9282a3..5b7c7f7 100755
--- a/server/src/com/cloud/storage/listener/StorageSyncListener.java
+++ b/server/src/com/cloud/storage/listener/StorageSyncListener.java
@@ -24,7 +24,7 @@ import com.cloud.agent.api.AgentControlCommand;
 import com.cloud.agent.api.Answer;
 import com.cloud.agent.api.Command;
 import com.cloud.agent.api.StartupCommand;
-import com.cloud.host.HostVO;
+import com.cloud.host.Host;
 import com.cloud.host.Status;
 
 public class StorageSyncListener implements Listener {
@@ -51,7 +51,7 @@ public class StorageSyncListener implements Listener {
     }
     
     @Override
-    public void processConnect(HostVO agent, StartupCommand cmd, boolean forRebalance) {
+    public void processConnect(Host agent, StartupCommand cmd, boolean forRebalance) {
     }
     
     @Override

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/abf40435/server/src/com/cloud/storage/secondary/SecondaryStorageListener.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/storage/secondary/SecondaryStorageListener.java b/server/src/com/cloud/storage/secondary/SecondaryStorageListener.java
index 6635b3c..d524f29 100755
--- a/server/src/com/cloud/storage/secondary/SecondaryStorageListener.java
+++ b/server/src/com/cloud/storage/secondary/SecondaryStorageListener.java
@@ -27,7 +27,7 @@ import com.cloud.agent.api.Command;
 import com.cloud.agent.api.StartupCommand;
 import com.cloud.agent.api.StartupSecondaryStorageCommand;
 import com.cloud.agent.api.StartupStorageCommand;
-import com.cloud.host.HostVO;
+import com.cloud.host.Host;
 import com.cloud.host.Status;
 import com.cloud.storage.Storage;
 
@@ -68,7 +68,7 @@ public class SecondaryStorageListener implements Listener {
     }
 
     @Override
-    public void processConnect(HostVO agent, StartupCommand cmd, boolean forRebalance) {
+    public void processConnect(Host agent, StartupCommand cmd, boolean forRebalance) {
         if ((cmd instanceof StartupStorageCommand) ) {
             StartupStorageCommand scmd = (StartupStorageCommand)cmd;
             if (scmd.getResourceType() ==  Storage.StorageResourceType.SECONDARY_STORAGE ) {

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/abf40435/server/src/com/cloud/storage/upload/UploadListener.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/storage/upload/UploadListener.java b/server/src/com/cloud/storage/upload/UploadListener.java
index ee13cf9..891610f 100755
--- a/server/src/com/cloud/storage/upload/UploadListener.java
+++ b/server/src/com/cloud/storage/upload/UploadListener.java
@@ -47,6 +47,7 @@ import com.cloud.api.ApiDBUtils;
 import com.cloud.async.AsyncJobManager;
 import com.cloud.async.AsyncJobResult;
 import com.cloud.exception.AgentUnavailableException;
+import com.cloud.host.Host;
 import com.cloud.host.HostVO;
 import com.cloud.storage.Storage;
 import com.cloud.storage.Upload.Status;
@@ -248,7 +249,7 @@ public class UploadListener implements Listener {
 	}
 
 	@Override
-	public void processConnect(HostVO agent, StartupCommand cmd, boolean forRebalance) {
+	public void processConnect(Host agent, StartupCommand cmd, boolean forRebalance) {
 		if (!(cmd instanceof StartupStorageCommand)) {
 	        return;
 	    }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/abf40435/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 34efdcb..cb0bee9 100755
--- a/server/src/com/cloud/template/HypervisorTemplateAdapter.java
+++ b/server/src/com/cloud/template/HypervisorTemplateAdapter.java
@@ -35,16 +35,21 @@ import org.apache.cloudstack.engine.subsystem.api.storage.CommandResult;
 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.TemplateDataFactory;
+import org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo;
 import org.apache.cloudstack.engine.subsystem.api.storage.TemplateService;
 import org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult;
 import org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope;
 import org.apache.cloudstack.framework.async.AsyncCallFuture;
+import org.apache.cloudstack.framework.async.AsyncCallbackDispatcher;
+import org.apache.cloudstack.framework.async.AsyncCompletionCallback;
+import org.apache.cloudstack.framework.async.AsyncRpcConext;
 import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO;
 import org.apache.log4j.Logger;
 
 import com.cloud.agent.AgentManager;
 import com.cloud.agent.api.Answer;
 import com.cloud.agent.api.storage.DeleteTemplateCommand;
+import com.cloud.alert.AlertManager;
 import com.cloud.configuration.Resource.ResourceType;
 import com.cloud.dc.DataCenterVO;
 import com.cloud.event.EventTypes;
@@ -80,6 +85,7 @@ public class HypervisorTemplateAdapter extends TemplateAdapterBase implements Te
     @Inject TemplateService imageService;
     @Inject TemplateDataFactory imageFactory;
     @Inject TemplateManager templateMgr;
+    @Inject AlertManager alertMgr;
 
     @Override
     public String getName() {
@@ -181,23 +187,37 @@ public class HypervisorTemplateAdapter extends TemplateAdapterBase implements Te
 		    throw new CloudRuntimeException("Unable to find image store to download template "+ profile.getTemplate());
 		}
         for (DataStore imageStore : imageStores) {
-            AsyncCallFuture<TemplateApiResult> future = this.imageService
-                    .createTemplateAsync(this.imageFactory.getTemplate(template.getId(), imageStore), 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);
-            }
+        	TemplateInfo tmpl = this.imageFactory.getTemplate(template.getId(), imageStore);
+        	CreateTemplateContext<TemplateApiResult> context = new CreateTemplateContext<TemplateApiResult>(null, tmpl);
+        	AsyncCallbackDispatcher<HypervisorTemplateAdapter, TemplateApiResult> caller = AsyncCallbackDispatcher.create(this);
+        	caller.setCallback(this.createTemplateAsyncCallBack(null, null));
+        	caller.setContext(context);
+           this.imageService
+                    .createTemplateAsync(tmpl, imageStore, caller);
         }
         _resourceLimitMgr.incrementResourceCount(profile.getAccountId(), ResourceType.template);
-        _resourceLimitMgr.incrementResourceCount(profile.getAccountId(), ResourceType.secondary_storage,
-                UriUtils.getRemoteSize(profile.getUrl()));
+       
         return template;
     }
+	
+	private class CreateTemplateContext<T> extends AsyncRpcConext<T> {
+		final TemplateInfo template;
+		public CreateTemplateContext(AsyncCompletionCallback<T> callback, TemplateInfo template) {
+			super(callback);
+			this.template = template;
+		}
+	}
+	
+	protected Void createTemplateAsyncCallBack(AsyncCallbackDispatcher<HypervisorTemplateAdapter, 
+			TemplateApiResult> callback, CreateTemplateContext<TemplateApiResult> context) {
+		TemplateInfo template = context.template;
+		VMTemplateVO tmplt = this._tmpltDao.findById(template.getId());
+		long accountId = tmplt.getAccountId();
+		_resourceLimitMgr.incrementResourceCount(accountId, ResourceType.secondary_storage,
+		              	template.getSize());
+		
+		return null;
+	}
 
 	@Override @DB
 	public boolean delete(TemplateProfile profile) {

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/abf40435/server/src/com/cloud/vm/VirtualMachineManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/vm/VirtualMachineManagerImpl.java b/server/src/com/cloud/vm/VirtualMachineManagerImpl.java
index af22716..ea20dd5 100755
--- a/server/src/com/cloud/vm/VirtualMachineManagerImpl.java
+++ b/server/src/com/cloud/vm/VirtualMachineManagerImpl.java
@@ -2285,7 +2285,7 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
     }
 
     @Override
-    public void processConnect(HostVO agent, StartupCommand cmd, boolean forRebalance) throws ConnectionException {
+    public void processConnect(Host agent, StartupCommand cmd, boolean forRebalance) throws ConnectionException {
         if (!(cmd instanceof StartupRoutingCommand)) {
             return;
         }


Mime
View raw message