cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ahu...@apache.org
Subject [39/50] [abbrv] Merged
Date Fri, 10 May 2013 23:33:22 GMT
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/30479293/server/src/com/cloud/storage/download/DownloadMonitorImpl.java
----------------------------------------------------------------------
diff --cc server/src/com/cloud/storage/download/DownloadMonitorImpl.java
index 9168aad,220cbff..eace673
--- a/server/src/com/cloud/storage/download/DownloadMonitorImpl.java
+++ b/server/src/com/cloud/storage/download/DownloadMonitorImpl.java
@@@ -27,61 -30,106 +27,55 @@@ import java.util.concurrent.ConcurrentH
  import javax.ejb.Local;
  import javax.inject.Inject;
  
 +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.engine.subsystem.api.storage.VolumeInfo;
 +import org.apache.cloudstack.framework.async.AsyncCompletionCallback;
 +import org.apache.cloudstack.storage.command.DownloadCommand;
- import org.apache.cloudstack.storage.command.DownloadProgressCommand;
 +import org.apache.cloudstack.storage.command.DownloadCommand.ResourceType;
++import org.apache.cloudstack.storage.command.DownloadProgressCommand;
 +import org.apache.cloudstack.storage.command.DownloadProgressCommand.RequestType;
 +import org.apache.cloudstack.storage.datastore.db.ImageStoreDao;
 +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.cloudstack.storage.datastore.db.VolumeDataStoreVO;
++import org.apache.cloudstack.storage.to.TemplateObjectTO;
++import org.apache.cloudstack.storage.to.VolumeObjectTO;
  import org.apache.log4j.Logger;
  import org.springframework.stereotype.Component;
  
  import com.cloud.agent.AgentManager;
 -import com.cloud.agent.Listener;
 -import com.cloud.agent.api.Answer;
 -import com.cloud.agent.api.Command;
 -import com.cloud.agent.api.storage.DeleteTemplateCommand;
 -import com.cloud.agent.api.storage.DeleteVolumeCommand;
 -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.ListTemplateAnswer;
 -import com.cloud.agent.api.storage.ListTemplateCommand;
 -import com.cloud.agent.api.storage.ListVolumeAnswer;
 -import com.cloud.agent.api.storage.ListVolumeCommand;
 -import com.cloud.agent.manager.Commands;
 -import com.cloud.alert.AlertManager;
 +import com.cloud.agent.api.storage.DownloadAnswer;
 +import com.cloud.agent.api.storage.Proxy;
  import com.cloud.configuration.Config;
  import com.cloud.configuration.dao.ConfigurationDao;
 -import com.cloud.dc.DataCenterVO;
 -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.ResourceAllocationException;
 -import com.cloud.exception.StorageUnavailableException;
 -import com.cloud.host.Host;
 -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.RegisterVolumePayload;
  import com.cloud.storage.Storage.ImageFormat;
- import com.cloud.storage.VMTemplateHostVO;
- import com.cloud.storage.VMTemplateStorageResourceAssoc;
 -import com.cloud.storage.StorageManager;
 -import com.cloud.storage.SwiftVO;
 -import com.cloud.storage.VMTemplateHostVO;
 -import com.cloud.storage.VMTemplateStorageResourceAssoc;
  import com.cloud.storage.VMTemplateStorageResourceAssoc.Status;
--import com.cloud.storage.VMTemplateVO;
 -import com.cloud.storage.VMTemplateZoneVO;
 -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.Volume;
- import com.cloud.storage.VolumeHostVO;
  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.storage.template.TemplateInfo;
 -import com.cloud.template.TemplateManager;
 -import com.cloud.user.Account;
 -import com.cloud.user.AccountManager;
 -import com.cloud.user.ResourceLimitService;
 -import com.cloud.utils.UriUtils;
++import com.cloud.storage.upload.UploadListener;
 +import com.cloud.template.VirtualMachineTemplate;
 +import com.cloud.utils.component.ComponentContext;
  import com.cloud.utils.component.ManagerBase;
- import com.cloud.utils.db.JoinBuilder;
- import com.cloud.utils.db.SearchBuilder;
- import com.cloud.utils.db.SearchCriteria;
- import org.apache.cloudstack.storage.to.VolumeObjectTO;
- import org.apache.cloudstack.storage.to.TemplateObjectTO;
 -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.UserVmVO;
 -import com.cloud.vm.VirtualMachine.State;
 -import com.cloud.vm.dao.SecondaryStorageVmDao;
 -import com.cloud.vm.dao.UserVmDao;
 -
 -import edu.emory.mathcs.backport.java.util.Collections;
 -
  
  @Component
 -@Local(value={DownloadMonitor.class})
 -public class DownloadMonitorImpl extends ManagerBase implements  DownloadMonitor {
 +@Local(value = { DownloadMonitor.class })
 +public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor {
      static final Logger s_logger = Logger.getLogger(DownloadMonitorImpl.class);
 -	
 -    @Inject 
 -    VMTemplateHostDao _vmTemplateHostDao;
 -    @Inject 
 -    VMTemplateZoneDao _vmTemplateZoneDao;
 -    @Inject
 -	VMTemplatePoolDao _vmTemplatePoolDao;
 -    @Inject
 -    VMTemplateSwiftDao _vmTemplateSwiftlDao;
 +
      @Inject
 -    StoragePoolHostDao _poolHostDao;
 +    TemplateDataStoreDao _vmTemplateStoreDao;
      @Inject
 -    SecondaryStorageVmDao _secStorageVmDao;
 +    ImageStoreDao _imageStoreDao;
      @Inject
      VolumeDao _volumeDao;
      @Inject
@@@ -95,238 -153,845 +89,224 @@@
      @Inject
      ConfigurationDao _configDao;
      @Inject
 -    UserVmManager _vmMgr;
 -
 -    @Inject TemplateManager templateMgr;
 -
 -    
 -    @Inject 
 -    private UsageEventDao _usageEventDao;
 -    
 -
 -    @Inject
 -    private ClusterDao _clusterDao;
 -    @Inject
 -    private HostDao _hostDao;
 -    @Inject
 -    private ResourceManager _resourceMgr;
 -    @Inject
 -    private SwiftDao _swiftDao;
 +    EndPointSelector _epSelector;
      @Inject
 -    protected ResourceLimitService _resourceLimitMgr;
 -    @Inject
 -    protected UserVmDao _userVmDao;
 -    @Inject
 -    protected AccountManager _accountMgr;
 +    TemplateDataFactory tmplFactory;
  
 -	private Boolean _sslCopy = new Boolean(false);
 -	private String _copyAuthPasswd;
 -	private String _proxy = null;
 -    protected SearchBuilder<VMTemplateHostVO> ReadyTemplateStatesSearch;
 +    private Boolean _sslCopy = new Boolean(false);
 +    private String _copyAuthPasswd;
 +    private String _proxy = null;
-     protected SearchBuilder<TemplateDataStoreVO> ReadyTemplateStatesSearch;
  
 -	Timer _timer;
 +    Timer _timer;
  
 -	final Map<VMTemplateHostVO, DownloadListener> _listenerMap = new ConcurrentHashMap<VMTemplateHostVO, DownloadListener>();
 -	final Map<VolumeHostVO, DownloadListener> _listenerVolumeMap = new ConcurrentHashMap<VolumeHostVO, DownloadListener>();
 +    @Inject
 +    DataStoreManager storeMgr;
  
 +    final Map<TemplateDataStoreVO, DownloadListener> _listenerTemplateMap = new ConcurrentHashMap<TemplateDataStoreVO, DownloadListener>();
-     final Map<VMTemplateHostVO, DownloadListener> _listenerMap = new ConcurrentHashMap<VMTemplateHostVO, DownloadListener>();
-     final Map<VolumeHostVO, DownloadListener> _listenerVolumeMap = new ConcurrentHashMap<VolumeHostVO, DownloadListener>();
 +    final Map<VolumeDataStoreVO, DownloadListener> _listenerVolMap = new ConcurrentHashMap<VolumeDataStoreVO, DownloadListener>();
  
 -	public void send(Long hostId, Command cmd, Listener listener) throws AgentUnavailableException {
 -		_agentMgr.send(hostId, new Commands(cmd), listener);
 -	}
  
 -	@Override
 -	public boolean configure(String name, Map<String, Object> params) {
 +    @Override
 +    public boolean configure(String name, Map<String, Object> params) {
          final Map<String, String> configs = _configDao.getConfiguration("ManagementServer", params);
          _sslCopy = Boolean.parseBoolean(configs.get("secstorage.encrypt.copy"));
          _proxy = configs.get(Config.SecStorageProxy.key());
 -        
 +
          String cert = configs.get("secstorage.ssl.cert.domain");
          if (!"realhostip.com".equalsIgnoreCase(cert)) {
 -        	s_logger.warn("Only realhostip.com ssl cert is supported, ignoring self-signed and other certs");
 +            s_logger.warn("Only realhostip.com ssl cert is supported, ignoring self-signed and other certs");
          }
 -        
 -        _copyAuthPasswd = configs.get("secstorage.copy.password");
 -        
 -        _agentMgr.registerForHostEvents(new DownloadListener(this), true, false, false);
 -        
 -        ReadyTemplateStatesSearch = _vmTemplateHostDao.createSearchBuilder();
 -        ReadyTemplateStatesSearch.and("download_state", ReadyTemplateStatesSearch.entity().getDownloadState(), SearchCriteria.Op.EQ);
 -        ReadyTemplateStatesSearch.and("destroyed", ReadyTemplateStatesSearch.entity().getDestroyed(), SearchCriteria.Op.EQ);
 -        ReadyTemplateStatesSearch.and("host_id", ReadyTemplateStatesSearch.entity().getHostId(), SearchCriteria.Op.EQ);
  
 -        SearchBuilder<VMTemplateVO> TemplatesWithNoChecksumSearch = _templateDao.createSearchBuilder();
 -        TemplatesWithNoChecksumSearch.and("checksum", TemplatesWithNoChecksumSearch.entity().getChecksum(), SearchCriteria.Op.NULL);
 +        _copyAuthPasswd = configs.get("secstorage.copy.password");
  
 -        ReadyTemplateStatesSearch.join("vm_template", TemplatesWithNoChecksumSearch, TemplatesWithNoChecksumSearch.entity().getId(),
 -                ReadyTemplateStatesSearch.entity().getTemplateId(), JoinBuilder.JoinType.INNER);
 -        TemplatesWithNoChecksumSearch.done();
 -        ReadyTemplateStatesSearch.done();
 -               
 -		return true;
 -	}
 +        DownloadListener dl = new DownloadListener(this);
 +        ComponentContext.inject(dl);
 +        _agentMgr.registerForHostEvents(dl, true, false, false);
  
-         ReadyTemplateStatesSearch = _vmTemplateStoreDao.createSearchBuilder();
-         ReadyTemplateStatesSearch.and("state", ReadyTemplateStatesSearch.entity().getState(), SearchCriteria.Op.EQ);
-         ReadyTemplateStatesSearch.and("destroyed", ReadyTemplateStatesSearch.entity().getDestroyed(), SearchCriteria.Op.EQ);
-         ReadyTemplateStatesSearch.and("store_id", ReadyTemplateStatesSearch.entity().getDataStoreId(), SearchCriteria.Op.EQ);
- 
-         SearchBuilder<VMTemplateVO> TemplatesWithNoChecksumSearch = _templateDao.createSearchBuilder();
-         TemplatesWithNoChecksumSearch.and("checksum", TemplatesWithNoChecksumSearch.entity().getChecksum(), SearchCriteria.Op.NULL);
- 
-         ReadyTemplateStatesSearch.join("vm_template", TemplatesWithNoChecksumSearch, TemplatesWithNoChecksumSearch.entity().getId(),
-                 ReadyTemplateStatesSearch.entity().getTemplateId(), JoinBuilder.JoinType.INNER);
-         TemplatesWithNoChecksumSearch.done();
-         ReadyTemplateStatesSearch.done();
- 
-         return true;
 -	@Override
 -	public boolean start() {
 -		_timer = new Timer();
 -		return true;
 -	}
++       return true;
 +    }
  
 -	@Override
 -	public boolean stop() {
 -		return true;
 -	}
 -	
 -	public boolean isTemplateUpdateable(Long templateId, Long hostId) {
 -		List<VMTemplateHostVO> downloadsInProgress =
 -			_vmTemplateHostDao.listByTemplateHostStatus(templateId.longValue(), hostId.longValue(), VMTemplateHostVO.Status.DOWNLOAD_IN_PROGRESS, VMTemplateHostVO.Status.DOWNLOADED);
 -		return (downloadsInProgress.size() == 0);
 -	}
 -	
 -	@Override
 -    public boolean copyTemplate(VMTemplateVO template, HostVO sourceServer, HostVO destServer) throws StorageUnavailableException{
 +    @Override
 +    public boolean start() {
 +        _timer = new Timer();
 +        return true;
 +    }
  
 -		boolean downloadJobExists = false;
 -        VMTemplateHostVO destTmpltHost = null;
 -        VMTemplateHostVO srcTmpltHost = null;
 +    @Override
 +    public boolean stop() {
 +        return true;
 +    }
  
 -        srcTmpltHost = _vmTemplateHostDao.findByHostTemplate(sourceServer.getId(), template.getId());
 -        if (srcTmpltHost == null) {
 -        	throw new InvalidParameterValueException("Template " + template.getName() + " not associated with " + sourceServer.getName());
 -        }
 +    public boolean isTemplateUpdateable(Long templateId, Long storeId) {
 +        List<TemplateDataStoreVO> downloadsInProgress = _vmTemplateStoreDao.listByTemplateStoreDownloadStatus(templateId, storeId,
 +                Status.DOWNLOAD_IN_PROGRESS, Status.DOWNLOADED);
 +        return (downloadsInProgress.size() == 0);
 +    }
  
 -        String url = generateCopyUrl(sourceServer, srcTmpltHost);
 -	    if (url == null) {
 -			s_logger.warn("Unable to start/resume copy of template " + template.getUniqueName() + " to " + destServer.getName() + ", no secondary storage vm in running state in source zone");
 -			throw new CloudRuntimeException("No secondary VM in running state in zone " + sourceServer.getDataCenterId());
 -	    }
 -        destTmpltHost = _vmTemplateHostDao.findByHostTemplate(destServer.getId(), template.getId());
 -        if (destTmpltHost == null) {
 -            destTmpltHost = new VMTemplateHostVO(destServer.getId(), template.getId(), new Date(), 0, VMTemplateStorageResourceAssoc.Status.NOT_DOWNLOADED, null, null, "jobid0000", null, url);
 -            destTmpltHost.setCopy(true);
 -            destTmpltHost.setPhysicalSize(srcTmpltHost.getPhysicalSize());
 -            _vmTemplateHostDao.persist(destTmpltHost);
 -        } else if ((destTmpltHost.getJobId() != null) && (destTmpltHost.getJobId().length() > 2)) {
 +    private void initiateTemplateDownload(DataObject template, AsyncCompletionCallback<DownloadAnswer> callback) {
 +        boolean downloadJobExists = false;
 +        TemplateDataStoreVO vmTemplateStore = null;
 +        DataStore store = template.getDataStore();
 +
 +        vmTemplateStore = _vmTemplateStoreDao.findByStoreTemplate(store.getId(), template.getId());
 +        if (vmTemplateStore == null) {
 +            vmTemplateStore = new TemplateDataStoreVO(store.getId(), template.getId(), new Date(), 0,
-                     VMTemplateStorageResourceAssoc.Status.NOT_DOWNLOADED, null, null, "jobid0000", null, template.getUri());
++                    Status.NOT_DOWNLOADED, null, null, "jobid0000", null, template.getUri());
 +            vmTemplateStore.setDataStoreRole(store.getRole());
 +            _vmTemplateStoreDao.persist(vmTemplateStore);
 +        } else if ((vmTemplateStore.getJobId() != null) && (vmTemplateStore.getJobId().length() > 2)) {
              downloadJobExists = true;
          }
  
          Long maxTemplateSizeInBytes = getMaxTemplateSizeInBytes();
 -        if (srcTmpltHost.getSize() > maxTemplateSizeInBytes){
 -        	throw new CloudRuntimeException("Cant copy the template as the template's size " +srcTmpltHost.getSize()+
 -        			" is greater than max.template.iso.size " + maxTemplateSizeInBytes);
 -        }
 -        
 -		if(destTmpltHost != null) {
 -		    start();
 -            String sourceChecksum = this.templateMgr.getChecksum(srcTmpltHost.getHostId(), srcTmpltHost.getInstallPath());
 -			DownloadCommand dcmd =  
 -              new DownloadCommand(destServer.getStorageUrl(), url, template, TemplateConstants.DEFAULT_HTTP_AUTH_USER, _copyAuthPasswd, maxTemplateSizeInBytes); 
 -			if (downloadJobExists) {
 -				dcmd = new DownloadProgressCommand(dcmd, destTmpltHost.getJobId(), RequestType.GET_OR_RESTART);
 -	 		}
 +        if (vmTemplateStore != null) {
 +            start();
 +            VirtualMachineTemplate tmpl = this._templateDao.findById(template.getId());
 +            DownloadCommand dcmd = new DownloadCommand((TemplateObjectTO)(template.getTO()), maxTemplateSizeInBytes);
              dcmd.setProxy(getHttpProxy());
 -			dcmd.setChecksum(sourceChecksum); // We need to set the checksum as the source template might be a compressed url and have cksum for compressed image. Bug #10775
 -            HostVO ssAhost = _ssvmMgr.pickSsvmHost(destServer);
 -            if( ssAhost == null ) {
 -                 s_logger.warn("There is no secondary storage VM for secondary storage host " + destServer.getName());
 -                 return false;
 -            }
 -            DownloadListener dl = new DownloadListener(ssAhost, destServer, template, _timer, _vmTemplateHostDao, destTmpltHost.getId(), this, dcmd, _templateDao, _resourceLimitMgr, _alertMgr, _accountMgr);
              if (downloadJobExists) {
 -                dl.setCurrState(destTmpltHost.getDownloadState());
 +                dcmd = new DownloadProgressCommand(dcmd, vmTemplateStore.getJobId(), RequestType.GET_OR_RESTART);
              }
 -			DownloadListener old = null;
 -			synchronized (_listenerMap) {
 -			    old = _listenerMap.put(destTmpltHost, dl);
 -			}
 -			if( old != null ) {
 -			    old.abandon();
 -			}
 -			
 -            try {
 -	            send(ssAhost.getId(), dcmd, dl);
 -	            return true;
 -            } catch (AgentUnavailableException e) {
 -				s_logger.warn("Unable to start /resume COPY of template " + template.getUniqueName() + " to " + destServer.getName(), e);
 -				dl.setDisconnected();
 -				dl.scheduleStatusCheck(RequestType.GET_OR_RESTART);
 -	            e.printStackTrace();
 +            if (vmTemplateStore.isCopy()) {
 +                dcmd.setCreds(TemplateConstants.DEFAULT_HTTP_AUTH_USER, _copyAuthPasswd);
 +            }
 +            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(ep, store, template, _timer, this, dcmd,
 +                     callback);
 +            ComponentContext.inject(dl);  // initialize those auto-wired field in download listener.
 +            if (downloadJobExists) {
 +                // due to handling existing download job issues, we still keep
 +                // downloadState in template_store_ref to avoid big change in
 +                // DownloadListener to use
 +                // new ObjectInDataStore.State transition. TODO: fix this later
 +                // to be able to remove downloadState from template_store_ref.
 +                dl.setCurrState(vmTemplateStore.getDownloadState());
              }
 -		}
 -		
 -		return false;
 -	}
 -	
 -	private String generateCopyUrl(String ipAddress, String dir, String path){
 -		String hostname = ipAddress;
 -		String scheme = "http";
 -		if (_sslCopy) {
 -			hostname = ipAddress.replace(".", "-");
 -			hostname = hostname + ".realhostip.com";
 -			scheme = "https";
 -		}
 -		return scheme + "://" + hostname + "/copy/SecStorage/" + dir + "/" + path; 
 -	}
 -	
 -	private String generateCopyUrl(HostVO sourceServer, VMTemplateHostVO srcTmpltHost) {
 -		List<SecondaryStorageVmVO> ssVms = _secStorageVmDao.getSecStorageVmListInStates(SecondaryStorageVm.Role.templateProcessor, sourceServer.getDataCenterId(), State.Running);
 -		if (ssVms.size() > 0) {
 -			SecondaryStorageVmVO ssVm = ssVms.get(0);
 -			if (ssVm.getPublicIpAddress() == null) {
 -				s_logger.warn("A running secondary storage vm has a null public ip?");
 -				return null;
 -			}
 -			return generateCopyUrl(ssVm.getPublicIpAddress(), sourceServer.getParent(), srcTmpltHost.getInstallPath());
 -		}
 -		
 -		VMTemplateVO tmplt = _templateDao.findById(srcTmpltHost.getTemplateId());
 -		HypervisorType hyperType = tmplt.getHypervisorType();
 -		/*No secondary storage vm yet*/
 -		if (hyperType != null && hyperType == HypervisorType.KVM) {
 -			return "file://" + sourceServer.getParent() + "/" + srcTmpltHost.getInstallPath();
 -		}
 -		return null;
 -	}
+ 
 -	private void downloadTemplateToStorage(VMTemplateVO template, HostVO sserver) {
 -		boolean downloadJobExists = false;
 -        VMTemplateHostVO vmTemplateHost = null;
+ 
 -        vmTemplateHost = _vmTemplateHostDao.findByHostTemplate(sserver.getId(), template.getId());
 -        if (vmTemplateHost == null) {
 -            vmTemplateHost = new VMTemplateHostVO(sserver.getId(), template.getId(), new Date(), 0, VMTemplateStorageResourceAssoc.Status.NOT_DOWNLOADED, null, null, "jobid0000", null, template.getUrl());
 -            _vmTemplateHostDao.persist(vmTemplateHost);
 -        } else if ((vmTemplateHost.getJobId() != null) && (vmTemplateHost.getJobId().length() > 2)) {
 -            downloadJobExists = true;
 -        }
 -                
 -        Long maxTemplateSizeInBytes = getMaxTemplateSizeInBytes();
 -        String secUrl = sserver.getStorageUrl();
 -		if(vmTemplateHost != null) {
 -		    start();
 -			DownloadCommand dcmd =
 -             new DownloadCommand(secUrl, template, maxTemplateSizeInBytes);
 -	        if (downloadJobExists) {
 -	            dcmd = new DownloadProgressCommand(dcmd, vmTemplateHost.getJobId(), RequestType.GET_OR_RESTART);
 -	        }
 -            dcmd.setProxy(getHttpProxy());
 -			if (vmTemplateHost.isCopy()) {
 -				dcmd.setCreds(TemplateConstants.DEFAULT_HTTP_AUTH_USER, _copyAuthPasswd);
 -			}
 -			HostVO ssAhost = _ssvmMgr.pickSsvmHost(sserver);
 -			if( ssAhost == null ) {
 -	             s_logger.warn("There is no secondary storage VM for secondary storage host " + sserver.getName());
 -	             return;
 -			}
 -			DownloadListener dl = new DownloadListener(ssAhost, sserver, template, _timer, _vmTemplateHostDao, vmTemplateHost.getId(), this, dcmd, _templateDao, _resourceLimitMgr, _alertMgr, _accountMgr);
 -			if (downloadJobExists) {
 -				dl.setCurrState(vmTemplateHost.getDownloadState());
 -	 		}
              DownloadListener old = null;
 -            synchronized (_listenerMap) {
 -                old = _listenerMap.put(vmTemplateHost, dl);
 +            synchronized (_listenerTemplateMap) {
 +                old = _listenerTemplateMap.put(vmTemplateStore, dl);
              }
 -            if( old != null ) {
 +            if (old != null) {
                  old.abandon();
              }
  
 -			try {
 -	            send(ssAhost.getId(), dcmd, dl);
 -            } catch (AgentUnavailableException e) {
 -				s_logger.warn("Unable to start /resume download of template " + template.getUniqueName() + " to " + sserver.getName(), e);
 -				dl.setDisconnected();
 -				dl.scheduleStatusCheck(RequestType.GET_OR_RESTART);
 +            try {
-                 ep.sendMessageAsyncWithListener(dcmd, dl);
++                ep.sendMessageAsync(dcmd, new UploadListener.Callback(ep.getId(), 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);
              }
 -		}
 -	}
 -
 +        }
 +    }
  
  
 -	@Override
 -	public boolean downloadTemplateToStorage(VMTemplateVO template, Long zoneId) {
 -        List<DataCenterVO> dcs = new ArrayList<DataCenterVO>();       
 -        if (zoneId == null) {
 -            dcs.addAll(_dcDao.listAll());
 -        } else {
 -            dcs.add(_dcDao.findById(zoneId));
 -        }
 +    @Override
 +    public void downloadTemplateToStorage(DataObject template, AsyncCompletionCallback<DownloadAnswer> callback) {
          long templateId = template.getId();
 -        boolean isPublic = template.isFeatured() || template.isPublicTemplate(); 
 -        for ( DataCenterVO dc : dcs ) {
 -    	    List<HostVO> ssHosts = _ssvmMgr.listAllTypesSecondaryStorageHostsInOneZone(dc.getId());
 -    	    for ( HostVO ssHost : ssHosts ) {
 -        		if (isTemplateUpdateable(templateId, ssHost.getId())) {
 -       				initiateTemplateDownload(templateId, ssHost);
 -       				if (! isPublic ) {
 -       				    break;
 -       				}
 -        		}
 -    	    }
 -	    }
 -	    return true;
 -	}
 -	
 -	@Override
 -	public boolean downloadVolumeToStorage(VolumeVO volume, Long zoneId, String url, String checkSum, ImageFormat format) {
 -                                
 -	    List<HostVO> ssHosts = _ssvmMgr.listAllTypesSecondaryStorageHostsInOneZone(zoneId);
 -	    Collections.shuffle(ssHosts);
 -	    HostVO ssHost = ssHosts.get(0);
 -	    downloadVolumeToStorage(volume, ssHost, url, checkSum, format);
 -	    return true;
 -	}
 -	
 -	private void downloadVolumeToStorage(VolumeVO volume, HostVO sserver, String url, String checkSum, ImageFormat format) {
 -		boolean downloadJobExists = false;
 -        VolumeHostVO volumeHost = null;
 +        DataStore store = template.getDataStore();
 +        if (isTemplateUpdateable(templateId, store.getId())) {
 +            if (template != null && template.getUri() != null) {
 +                initiateTemplateDownload(template, callback);
 +            }
 +        }
 +    }
  
 -        volumeHost = _volumeHostDao.findByHostVolume(sserver.getId(), volume.getId());
 +    @Override
 +    public void downloadVolumeToStorage(DataObject volume, AsyncCompletionCallback<DownloadAnswer> callback) {
 +        boolean downloadJobExists = false;
 +        VolumeDataStoreVO volumeHost = null;
 +        DataStore store = volume.getDataStore();
 +        VolumeInfo volInfo = (VolumeInfo)volume;
 +        RegisterVolumePayload payload = (RegisterVolumePayload)volInfo.getpayload();
 +        String url = payload.getUrl();
 +        String checkSum = payload.getChecksum();
 +        ImageFormat format = ImageFormat.valueOf(payload.getFormat());
 +
 +        volumeHost = _volumeStoreDao.findByStoreVolume(store.getId(), volume.getId());
          if (volumeHost == null) {
-             volumeHost = new VolumeDataStoreVO(store.getId(), volume.getId(), new Date(), 0, VMTemplateStorageResourceAssoc.Status.NOT_DOWNLOADED,
 -            volumeHost = new VolumeHostVO(sserver.getId(), volume.getId(), sserver.getDataCenterId(), new Date(), 0, VMTemplateStorageResourceAssoc.Status.NOT_DOWNLOADED, null, null,
 -            		"jobid0000", null, url, checkSum, format);
 -            _volumeHostDao.persist(volumeHost);
++            volumeHost = new VolumeDataStoreVO(store.getId(), volume.getId(), new Date(), 0, Status.NOT_DOWNLOADED,
 +                    null, null, "jobid0000", null, url, checkSum, format);
 +            _volumeStoreDao.persist(volumeHost);
          } else if ((volumeHost.getJobId() != null) && (volumeHost.getJobId().length() > 2)) {
              downloadJobExists = true;
          }
 -        
  
          Long maxVolumeSizeInBytes = getMaxVolumeSizeInBytes();
 -        String secUrl = sserver.getStorageUrl();
 -		if(volumeHost != null) {
 -		    start();
 -			DownloadCommand dcmd = new DownloadCommand(secUrl, volume, maxVolumeSizeInBytes, checkSum, url, format);
 -	        if (downloadJobExists) {
 -	            dcmd = new DownloadProgressCommand(dcmd, volumeHost.getJobId(), RequestType.GET_OR_RESTART);
 -	            dcmd.setResourceType(ResourceType.VOLUME);
 -	        }
 +        if (volumeHost != null) {
 +            start();
 +            Volume vol = this._volumeDao.findById(volume.getId());
 +            DownloadCommand dcmd = new DownloadCommand((VolumeObjectTO)(volume.getTO()), maxVolumeSizeInBytes, checkSum, url, format);
              dcmd.setProxy(getHttpProxy());
 -			HostVO ssvm = _ssvmMgr.pickSsvmHost(sserver);
 -			if( ssvm == null ) {
 -	             s_logger.warn("There is no secondary storage VM for secondary storage host " + sserver.getName());
 -	             return;
 -			}
 -			DownloadListener dl = new DownloadListener(ssvm, sserver, volume, _timer, _volumeHostDao, volumeHost.getId(),
 -					this, dcmd, _volumeDao, _storageMgr, _resourceLimitMgr, _alertMgr, _accountMgr);
 -			
 -			if (downloadJobExists) {
 -				dl.setCurrState(volumeHost.getDownloadState());
 -	 		}
 -            DownloadListener old = null;
 -            synchronized (_listenerVolumeMap) {
 -                old = _listenerVolumeMap.put(volumeHost, dl);
 -            }
 -            if( old != null ) {
 -                old.abandon();
 -            }
 -
 -			try {
 -	            send(ssvm.getId(), dcmd, dl);
 -            } catch (AgentUnavailableException e) {
 -				s_logger.warn("Unable to start /resume download of volume " + volume.getName() + " to " + sserver.getName(), e);
 -				dl.setDisconnected();
 -				dl.scheduleStatusCheck(RequestType.GET_OR_RESTART);
 -            }
 -		}
 -	}
 -
 -
 -	private void initiateTemplateDownload(Long templateId, HostVO ssHost) {
 -		VMTemplateVO template = _templateDao.findById(templateId);
 -		if (template != null && (template.getUrl() != null)) {
 -			//find all storage hosts and tell them to initiate download
 -    		downloadTemplateToStorage(template, ssHost);
 -		}
 -		
 -	}
 -
 -	@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());
 +            if (downloadJobExists) {
 +                dcmd = new DownloadProgressCommand(dcmd, volumeHost.getJobId(), RequestType.GET_OR_RESTART);
 +                dcmd.setResourceType(ResourceType.VOLUME);
              }
 -        }
 -        txn.commit();
 -	}
  
 -	@DB
 -	public void handleDownloadEvent(HostVO host, VolumeVO volume, 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();
 -        txn.start();		
 -
 -        if (dnldStatus == Status.DOWNLOADED) {
 -			
 -			//Create usage event
 -            long size = -1;
 -            if(volumeHost!=null){
 -                size = volumeHost.getPhysicalSize();
 -                volume.setSize(size);
 -                this._volumeDao.update(volume.getId(), volume);
 -            }
 -            else{
 -                s_logger.warn("Failed to get size for volume" + volume.getName());
 -            }
 -            String eventType = EventTypes.EVENT_VOLUME_UPLOAD;
 -            if(volume.getAccountId() != Account.ACCOUNT_ID_SYSTEM){
 -                UsageEventUtils.publishUsageEvent(eventType, volume.getAccountId(), host.getDataCenterId(),
 -                        volume.getId(), volume.getName(), null, 0l, size, volume.getClass().getName(), volume.getUuid());
 +            EndPoint ep = this._epSelector.select(volume);
 +            if (ep == null) {
 +                s_logger.warn("There is no secondary storage VM for image store " + store.getName());
 +                return;
              }
 -        }else if (dnldStatus == Status.DOWNLOAD_ERROR || dnldStatus == Status.ABANDONED || dnldStatus == Status.UNKNOWN){
 -            //Decrement the volume and secondary storage space count
 -            _resourceLimitMgr.decrementResourceCount(volume.getAccountId(),
 -                    com.cloud.configuration.Resource.ResourceType.volume);
 -            _resourceLimitMgr.recalculateResourceCount(volume.getAccountId(), volume.getDomainId(),
 -                    com.cloud.configuration.Resource.ResourceType.secondary_storage.getOrdinal());
 -        }
 -        txn.commit();
 -	}
 -
 -	@Override
 -    public void handleSysTemplateDownload(HostVO host) {
 -	    List<HypervisorType> hypers = _resourceMgr.listAvailHypervisorInZone(host.getId(), host.getDataCenterId());
 -	    HypervisorType hostHyper = host.getHypervisorType();
 -	    if (hypers.contains(hostHyper)) {
 -	        return;
 -	    }
 +            DownloadListener dl = new DownloadListener(ep, store, volume, _timer, this, dcmd, callback);
 +            ComponentContext.inject(dl); // auto-wired those injected fields in DownloadListener
  
 -	    Set<VMTemplateVO> toBeDownloaded = new HashSet<VMTemplateVO>();
 -	    List<HostVO> ssHosts = _resourceMgr.listAllUpAndEnabledHostsInOneZoneByType(Host.Type.SecondaryStorage, host.getDataCenterId());
 -	    if (ssHosts == null || ssHosts.isEmpty()) {
 -	        return;
 -	    }
 -	    /*Download all the templates in zone with the same hypervisortype*/
 -        for ( HostVO ssHost : ssHosts) {
 -    	    List<VMTemplateVO> rtngTmplts = _templateDao.listAllSystemVMTemplates();
 -    	    List<VMTemplateVO> defaultBuiltin = _templateDao.listDefaultBuiltinTemplates();
 -    
 -    
 -    	    for (VMTemplateVO rtngTmplt : rtngTmplts) {
 -    	        if (rtngTmplt.getHypervisorType() == hostHyper) {
 -    	            toBeDownloaded.add(rtngTmplt);
 -    	        }
 -    	    }
 -    
 -    	    for (VMTemplateVO builtinTmplt : defaultBuiltin) {
 -    	        if (builtinTmplt.getHypervisorType() == hostHyper) {
 -    	            toBeDownloaded.add(builtinTmplt);
 -    	        }
 -    	    }
 -    
 -    	    for (VMTemplateVO template: toBeDownloaded) {
 -    	        VMTemplateHostVO tmpltHost = _vmTemplateHostDao.findByHostTemplate(ssHost.getId(), template.getId());
 -    	        if (tmpltHost == null || tmpltHost.getDownloadState() != Status.DOWNLOADED) {
 -    	            downloadTemplateToStorage(template, ssHost);
 -    	        }
 -    	    }
 -        }
 -	}
 -    
 -    @Override	
 -	public void addSystemVMTemplatesToHost(HostVO host, Map<String, TemplateInfo> templateInfos){
 -	    if ( templateInfos == null ) {
 -	        return;
 -	    }
 -	    Long hostId = host.getId();
 -	    List<VMTemplateVO> rtngTmplts = _templateDao.listAllSystemVMTemplates();
 -	    for ( VMTemplateVO tmplt : rtngTmplts ) {
 -	        TemplateInfo tmpltInfo = templateInfos.get(tmplt.getUniqueName());
 -	        if ( tmpltInfo == null ) {
 -	            continue;
 -	        }
 -	        VMTemplateHostVO tmpltHost = _vmTemplateHostDao.findByHostTemplate(hostId, tmplt.getId());
 -	        if ( tmpltHost == null ) {
 -                tmpltHost = new VMTemplateHostVO(hostId, tmplt.getId(), new Date(), 100, Status.DOWNLOADED, null, null, null, tmpltInfo.getInstallPath(), tmplt.getUrl());
 -	            tmpltHost.setSize(tmpltInfo.getSize());
 -	            tmpltHost.setPhysicalSize(tmpltInfo.getPhysicalSize());
 -	            _vmTemplateHostDao.persist(tmpltHost);
 -	        }
 -	    }
 -	}
 -	
 -    @Override
 -    public void handleSync(Long dcId) {
 -        if (dcId != null) {
 -            List<HostVO> ssHosts = _ssvmMgr.listSecondaryStorageHostsInOneZone(dcId);
 -            for (HostVO ssHost : ssHosts) {
 -                handleTemplateSync(ssHost);
 -                handleVolumeSync(ssHost);
 -            }
 -        }
 -    }
 -    
 -    private Map<String, TemplateInfo> listTemplate(HostVO ssHost) {
 -        ListTemplateCommand cmd = new ListTemplateCommand(ssHost.getStorageUrl());
 -        Answer answer = _agentMgr.sendToSecStorage(ssHost, cmd);
 -        if (answer != null && answer.getResult()) {
 -            ListTemplateAnswer tanswer = (ListTemplateAnswer)answer;
 -            return tanswer.getTemplateInfo();
 -        } else {
 -            if (s_logger.isDebugEnabled()) {
 -                s_logger.debug("can not list template for secondary storage host " + ssHost.getId());
 +            if (downloadJobExists) {
 +                dl.setCurrState(volumeHost.getDownloadState());
              }
 -        } 
 -        
 -        return null;
 -    }
 -    
 -    private Map<Long, TemplateInfo> listVolume(HostVO ssHost) {
 -    	ListVolumeCommand cmd = new ListVolumeCommand(ssHost.getStorageUrl());
 -        Answer answer = _agentMgr.sendToSecStorage(ssHost, 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 secondary storage host " + ssHost.getId());
 +            DownloadListener old = null;
 +            synchronized (_listenerVolMap) {
 +                old = _listenerVolMap.put(volumeHost, dl);
              }
 -        } 
 -        
 -        return null;
 -    }
 -    
 -    private Map<String, TemplateInfo> listTemplate(SwiftVO swift) {
 -        if (swift == null) {
 -            return null;
 -        }
 -        ListTemplateCommand cmd = new ListTemplateCommand(swift.toSwiftTO());
 -        Answer answer = _agentMgr.sendToSSVM(null, cmd);
 -        if (answer != null && answer.getResult()) {
 -            ListTemplateAnswer tanswer = (ListTemplateAnswer) answer;
 -            return tanswer.getTemplateInfo();
 -        } else {
 -            if (s_logger.isDebugEnabled()) {
 -                s_logger.debug("can not list template for swift " + swift);
 +            if (old != null) {
 +                old.abandon();
              }
 -        }
 -        return null;
 -    }
 -    
 -    @Override
 -    public void handleVolumeSync(HostVO ssHost) {
 -        if (ssHost == null) {
 -            s_logger.warn("Huh? ssHost is null");
 -            return;
 -        }
 -        long sserverId = ssHost.getId();        
 -        if (!(ssHost.getType() == Host.Type.SecondaryStorage || ssHost.getType() == Host.Type.LocalSecondaryStorage)) {
 -            s_logger.warn("Huh? Agent id " + sserverId + " is not secondary storage host");
 -            return;
 -        }
 -
 -        Map<Long, TemplateInfo> volumeInfos = listVolume(ssHost);
 -        if (volumeInfos == null) {
 -            return;
 -        }
 -        
 -        List<VolumeHostVO> dbVolumes = _volumeHostDao.listBySecStorage(sserverId);
 -        List<VolumeHostVO> toBeDownloaded = new ArrayList<VolumeHostVO>(dbVolumes);
 -        for (VolumeHostVO volumeHost : dbVolumes){
 -        	VolumeVO volume = _volumeDao.findById(volumeHost.getVolumeId());
 -        	//Exists then don't download
 -        	if (volumeInfos.containsKey(volume.getId())){
 -                TemplateInfo volInfo = volumeInfos.remove(volume.getId());
 -                toBeDownloaded.remove(volumeHost);
 -                s_logger.info("Volume Sync found " + volume.getUuid() + " already in the volume host table");
 -                if (volumeHost.getDownloadState() != Status.DOWNLOADED) {
 -                	volumeHost.setErrorString("");
 -                }
 -                if (volInfo.isCorrupted()) {
 -                	volumeHost.setDownloadState(Status.DOWNLOAD_ERROR);
 -                    String msg = "Volume " + volume.getUuid() + " is corrupted on secondary storage ";
 -                    volumeHost.setErrorString(msg);
 -                    s_logger.info("msg");
 -                    if (volumeHost.getDownloadUrl() == null) {
 -                        msg = "Volume (" + volume.getUuid() + ") with install path " + volInfo.getInstallPath() + "is corrupted, please check in secondary storage: " + volumeHost.getHostId();
 -                        s_logger.warn(msg);
 -                    } else {
 -                        toBeDownloaded.add(volumeHost);
 -                    }
 -
 -                } else { // Put them in right status
 -                    volumeHost.setDownloadPercent(100);
 -                    volumeHost.setDownloadState(Status.DOWNLOADED);
 -                    volumeHost.setInstallPath(volInfo.getInstallPath());
 -                    volumeHost.setSize(volInfo.getSize());
 -                    volumeHost.setPhysicalSize(volInfo.getPhysicalSize());
 -                    volumeHost.setLastUpdated(new Date());
 -                    _volumeHostDao.update(volumeHost.getId(), volumeHost);
  
 -                    if (volume.getSize() == 0) {
 -                        // Set volume size in volumes table
 -                        volume.setSize(volInfo.getSize());
 -                        _volumeDao.update(volumeHost.getVolumeId(), volume);
 -                    }
 -
 -                    if (volInfo.getSize() > 0) {
 -                        try {
 -                            String url = _volumeHostDao.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 (volumeHost.getDownloadState() != Status.DOWNLOADED) {
 -                s_logger.info("Volume Sync did not find " + volume.getName() + " ready on server " + sserverId + ", will request download to start/resume shortly");
 -                toBeDownloaded.add(volumeHost);
 -            } 
 -        }
 -        
 -        //Download volumes which haven't been downloaded yet.
 -        if (toBeDownloaded.size() > 0) {
 -            for (VolumeHostVO volumeHost : toBeDownloaded) {
 -                if (volumeHost.getDownloadUrl() == null) { // If url is null we can't initiate the download
 -                    continue;
 -                }                                  
 -                s_logger.debug("Volume " + volumeHost.getVolumeId() + " needs to be downloaded to " + ssHost.getName());
 -                downloadVolumeToStorage(_volumeDao.findById(volumeHost.getVolumeId()), ssHost,  volumeHost.getDownloadUrl(), volumeHost.getChecksum(), volumeHost.getFormat());                
 -            }
 -        }
 -
 -        //Delete volumes which are not present on DB.
 -        for (Long uniqueName : volumeInfos.keySet()) {
 -            TemplateInfo vInfo = volumeInfos.get(uniqueName);
 -            DeleteVolumeCommand dtCommand = new DeleteVolumeCommand(ssHost.getStorageUrl(), vInfo.getInstallPath());
              try {
-                 ep.sendMessageAsyncWithListener(dcmd, dl);
 -	            _agentMgr.sendToSecStorage(ssHost, dtCommand, null);
 -            } catch (AgentUnavailableException e) {
 -                String err = "Failed to delete " + vInfo.getTemplateName() + " on secondary storage " + sserverId + " which isn't in the database";
 -                s_logger.error(err);
 -                return;
++                ep.sendMessageAsync(dcmd, new UploadListener.Callback(ep.getId(), 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);
              }
 -            
 -            String description = "Deleted volume " + vInfo.getTemplateName() + " on secondary storage " + sserverId + " since it isn't in the database";
 -            s_logger.info(description);
          }
      }
 -    
 -	@Override
 -    public void handleTemplateSync(HostVO ssHost) {
 -        if (ssHost == null) {
 -            s_logger.warn("Huh? ssHost is null");
 -            return;
 -        }
 -        long sserverId = ssHost.getId();
 -        long zoneId = ssHost.getDataCenterId();
 -        if (!(ssHost.getType() == Host.Type.SecondaryStorage || ssHost.getType() == Host.Type.LocalSecondaryStorage)) {
 -            s_logger.warn("Huh? Agent id " + sserverId + " is not secondary storage host");
 -            return;
 -        }
  
 -        Map<String, TemplateInfo> templateInfos = listTemplate(ssHost);
 -        if (templateInfos == null) {
 -            return;
 -        }
  
 -        Set<VMTemplateVO> toBeDownloaded = new HashSet<VMTemplateVO>();
 -        List<VMTemplateVO> allTemplates = _templateDao.listAllInZone(zoneId);
 -        List<VMTemplateVO> rtngTmplts = _templateDao.listAllSystemVMTemplates();
 -        List<VMTemplateVO> defaultBuiltin = _templateDao.listDefaultBuiltinTemplates();
  
 -        if (rtngTmplts != null) {
 -            for (VMTemplateVO rtngTmplt : rtngTmplts) {
 -                if (!allTemplates.contains(rtngTmplt)) {
 -                    allTemplates.add(rtngTmplt);
 -                }
 -            }
 -        }
  
 -        if (defaultBuiltin != null) {
 -            for (VMTemplateVO builtinTmplt : defaultBuiltin) {
 -                if (!allTemplates.contains(builtinTmplt)) {
 -                    allTemplates.add(builtinTmplt);
 -                }
 -            }
 +    private Long getMaxTemplateSizeInBytes() {
 +        try {
 +            return Long.parseLong(_configDao.getValue("max.template.iso.size")) * 1024L * 1024L * 1024L;
 +        } catch (NumberFormatException e) {
 +            return null;
          }
 +    }
  
 -        toBeDownloaded.addAll(allTemplates);
 -
 -        for (VMTemplateVO tmplt : allTemplates) {
 -            String uniqueName = tmplt.getUniqueName();
 -            VMTemplateHostVO tmpltHost = _vmTemplateHostDao.findByHostTemplate(sserverId, tmplt.getId());
 -            if (templateInfos.containsKey(uniqueName)) {
 -                TemplateInfo tmpltInfo = templateInfos.remove(uniqueName);
 -                toBeDownloaded.remove(tmplt);
 -                if (tmpltHost != null) {
 -                    s_logger.info("Template Sync found " + tmplt.getName() + " already in the template host table");
 -                    if (tmpltHost.getDownloadState() != Status.DOWNLOADED) {
 -                        tmpltHost.setErrorString("");
 -                    }
 -                    if (tmpltInfo.isCorrupted()) {
 -                        tmpltHost.setDownloadState(Status.DOWNLOAD_ERROR);
 -                        String msg = "Template " + tmplt.getName() + ":" + tmplt.getId() + " is corrupted on secondary storage " + tmpltHost.getId();
 -                        tmpltHost.setErrorString(msg);
 -                        s_logger.info("msg");
 -                        if (tmplt.getUrl() == null) {
 -                            msg = "Private Template (" + tmplt + ") with install path " + tmpltInfo.getInstallPath() + "is corrupted, please check in secondary storage: " + tmpltHost.getHostId();
 -                            s_logger.warn(msg);
 -                        } else {
 -                            toBeDownloaded.add(tmplt);
 -                        }
 -
 -                    } else {
 -                        tmpltHost.setDownloadPercent(100);
 -                        tmpltHost.setDownloadState(Status.DOWNLOADED);
 -                        tmpltHost.setInstallPath(tmpltInfo.getInstallPath());
 -                        tmpltHost.setSize(tmpltInfo.getSize());
 -                        tmpltHost.setPhysicalSize(tmpltInfo.getPhysicalSize());
 -                        tmpltHost.setLastUpdated(new Date());
 -
 -                        // Skipping limit checks for SYSTEM Account and for the templates created from volumes or snapshots
 -                        // which already got checked and incremented during createTemplate API call.
 -                        if (tmpltInfo.getSize() > 0 && tmplt.getAccountId() != Account.ACCOUNT_ID_SYSTEM && tmplt.getUrl() != null) {
 -                            long accountId = tmplt.getAccountId();
 -                            try {
 -                                _resourceLimitMgr.checkResourceLimit(_accountMgr.getAccount(accountId),
 -                                        com.cloud.configuration.Resource.ResourceType.secondary_storage,
 -                                        tmpltInfo.getSize() - UriUtils.getRemoteSize(tmplt.getUrl()));
 -                            } catch (ResourceAllocationException e) {
 -                                s_logger.warn(e.getMessage());
 -                                _alertMgr.sendAlert(_alertMgr.ALERT_TYPE_RESOURCE_LIMIT_EXCEEDED, ssHost.getDataCenterId(),
 -                                        null, e.getMessage(), e.getMessage());
 -                            } finally {
 -                                _resourceLimitMgr.recalculateResourceCount(accountId, _accountMgr.getAccount(accountId).getDomainId(),
 -                                        com.cloud.configuration.Resource.ResourceType.secondary_storage.getOrdinal());
 -                            }
 -                        }
 -                    }
 -                    _vmTemplateHostDao.update(tmpltHost.getId(), tmpltHost);
 -                } else {
 -                    tmpltHost = new VMTemplateHostVO(sserverId, tmplt.getId(), new Date(), 100, Status.DOWNLOADED, null, null, null, tmpltInfo.getInstallPath(), tmplt.getUrl());
 -                    tmpltHost.setSize(tmpltInfo.getSize());
 -                    tmpltHost.setPhysicalSize(tmpltInfo.getPhysicalSize());
 -                    _vmTemplateHostDao.persist(tmpltHost);
 -                    VMTemplateZoneVO tmpltZoneVO = _vmTemplateZoneDao.findByZoneTemplate(zoneId, tmplt.getId());
 -                    if (tmpltZoneVO == null) {
 -                        tmpltZoneVO = new VMTemplateZoneVO(zoneId, tmplt.getId(), new Date());
 -                        _vmTemplateZoneDao.persist(tmpltZoneVO);
 -                    } else {
 -                        tmpltZoneVO.setLastUpdated(new Date());
 -                        _vmTemplateZoneDao.update(tmpltZoneVO.getId(), tmpltZoneVO);
 -                    }
 -
 -                }
 -
 -                continue;
 -            }
 -            if (tmpltHost != null && tmpltHost.getDownloadState() != Status.DOWNLOADED) {
 -                s_logger.info("Template Sync did not find " + tmplt.getName() + " ready on server " + sserverId
 -                        + ", will request download to start/resume shortly");
 -
 -            } else if (tmpltHost == null) {
 -                s_logger.info("Template Sync did not find " + tmplt.getName() + " on the server " + sserverId
 -                        + ", will request download shortly");
 -                VMTemplateHostVO templtHost = new VMTemplateHostVO(sserverId, tmplt.getId(), new Date(), 0, Status.NOT_DOWNLOADED, null, null, null, null, tmplt.getUrl());
 -                _vmTemplateHostDao.persist(templtHost);
 -                VMTemplateZoneVO tmpltZoneVO = _vmTemplateZoneDao.findByZoneTemplate(zoneId, tmplt.getId());
 -                if (tmpltZoneVO == null) {
 -                    tmpltZoneVO = new VMTemplateZoneVO(zoneId, tmplt.getId(), new Date());
 -                    _vmTemplateZoneDao.persist(tmpltZoneVO);
 -                } else {
 -                    tmpltZoneVO.setLastUpdated(new Date());
 -                    _vmTemplateZoneDao.update(tmpltZoneVO.getId(), tmpltZoneVO);
 -                }
 -            }
 -
 +    private Long getMaxVolumeSizeInBytes() {
 +        try {
 +            return Long.parseLong(_configDao.getValue("storage.max.volume.upload.size")) * 1024L * 1024L * 1024L;
 +        } catch (NumberFormatException e) {
 +            return null;
          }
 +    }
  
 -        if (toBeDownloaded.size() > 0) {
 -            /* Only download templates whose hypervirsor type is in the zone */
 -            List<HypervisorType> availHypers = _clusterDao.getAvailableHypervisorInZone(zoneId);
 -            if (availHypers.isEmpty()) {
 -                /*
 -                 * This is for cloudzone, local secondary storage resource
 -                 * started before cluster created
 -                 */
 -                availHypers.add(HypervisorType.KVM);
 -            }
 -            /* Baremetal need not to download any template */
 -            availHypers.remove(HypervisorType.BareMetal);
 -            availHypers.add(HypervisorType.None); // bug 9809: resume ISO
 -                                                  // download.
 -            for (VMTemplateVO tmplt : toBeDownloaded) {
 -                if (tmplt.getUrl() == null) { // If url is null we can't
 -                                              // initiate the download
 -                    continue;
 -                }
 -                // if this is private template, and there is no record for this
 -                // template in this sHost, skip
 -                if (!tmplt.isPublicTemplate() && !tmplt.isFeatured()) {
 -                    VMTemplateHostVO tmpltHost = _vmTemplateHostDao.findByHostTemplate(sserverId, tmplt.getId());
 -                    if (tmpltHost == null) {
 -                        continue;
 -                    }
 -                }
 -                if (availHypers.contains(tmplt.getHypervisorType())) {
 -                    if (_swiftMgr.isSwiftEnabled()) {
 -                        if (_swiftMgr.isTemplateInstalled(tmplt.getId())) {
 -                            continue;
 -                        }
 -                    }
 -                    s_logger.debug("Template " + tmplt.getName() + " needs to be downloaded to " + ssHost.getName());
 -                    downloadTemplateToStorage(tmplt, ssHost);
 -                } else {
 -                    s_logger.info("Skipping download of template " + tmplt.getName() + " since we don't have any "
 -                            + tmplt.getHypervisorType() + " hypervisors");
 -                }
 -            }
 +    private Proxy getHttpProxy() {
 +        if (_proxy == null) {
 +            return null;
          }
 -
 -        for (String uniqueName : templateInfos.keySet()) {
 -            TemplateInfo tInfo = templateInfos.get(uniqueName);
 -            List<UserVmVO> userVmUsingIso = _userVmDao.listByIsoId(tInfo.getId());
 -            //check if there is any Vm using this ISO.
 -            if (userVmUsingIso == null || userVmUsingIso.isEmpty()) {
 -                DeleteTemplateCommand dtCommand = new DeleteTemplateCommand(ssHost.getStorageUrl(), tInfo.getInstallPath());
 -                try {
 -                    _agentMgr.sendToSecStorage(ssHost, dtCommand, null);
 -                } catch (AgentUnavailableException e) {
 -                    String err = "Failed to delete " + tInfo.getTemplateName() + " on secondary storage " + sserverId + " which isn't in the database";
 -                    s_logger.error(err);
 -                    return;
 -                }
 -
 -                String description = "Deleted template " + tInfo.getTemplateName() + " on secondary storage " + sserverId + " since it isn't in the database";
 -                s_logger.info(description);
 -            }
 +        try {
 +            URI uri = new URI(_proxy);
 +            Proxy prx = new Proxy(uri);
 +            return prx;
 +        } catch (URISyntaxException e) {
 +            return null;
          }
      }
  

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/30479293/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java
----------------------------------------------------------------------
diff --cc server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java
index 5d4b43e,3cf9a7e..dd38acf
--- a/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java
+++ b/server/src/com/cloud/storage/secondary/SecondaryStorageManagerImpl.java
@@@ -29,12 -29,8 +29,13 @@@ import javax.ejb.Local
  import javax.inject.Inject;
  import javax.naming.ConfigurationException;
  
 +import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
 +import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager;
 +import org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope;
 +import org.apache.cloudstack.storage.datastore.db.ImageStoreDao;
 +import org.apache.cloudstack.storage.datastore.db.ImageStoreVO;
  import org.apache.log4j.Logger;
+ 
  import com.cloud.agent.AgentManager;
  import com.cloud.agent.api.Answer;
  import com.cloud.agent.api.Command;

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/30479293/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java
----------------------------------------------------------------------
diff --cc server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java
index 572bcab,26aae48..142f58e
--- a/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java
+++ b/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java
@@@ -197,11 -182,10 +198,9 @@@ public class SnapshotManagerImpl extend
      private ResourceTagDao _resourceTagDao;
      @Inject
      private ConfigurationDao _configDao;
- 
 -    @Inject  
 -    private PrimaryDataStoreDao _primaryDataStoreDao;
 +    @Inject
 +    private VMSnapshotDao _vmSnapshotDao;
      String _name;
--
      @Inject TemplateManager templateMgr;
      @Inject VolumeManager volumeMgr;
      @Inject DataStoreManager dataStoreMgr;
@@@ -374,7 -384,7 +373,11 @@@
          String parent = null;
          try {
              for (String backupUuid : BackupUuids) {
++<<<<<<< HEAD
 +                downloadSnapshotFromSwiftCommand cmd = new downloadSnapshotFromSwiftCommand(swift, secStore.getUri(), dcId, accountId, volumeId, parent, backupUuid, _backupsnapshotwait);
++=======
+                 DownloadSnapshotFromSwiftCommand cmd = new DownloadSnapshotFromSwiftCommand(swift, secondaryStoragePoolUrl, dcId, accountId, volumeId, parent, backupUuid, _backupsnapshotwait);
++>>>>>>> master
                  Answer answer = _agentMgr.sendToSSVM(dcId, cmd);
                  if ((answer == null) || !answer.getResult()) {
                      throw new CloudRuntimeException("downloadSnapshotsFromSwift failed ");
@@@ -554,8 -572,9 +557,9 @@@
          String keyword = cmd.getKeyword();
          String snapshotTypeStr = cmd.getSnapshotType();
          String intervalTypeStr = cmd.getIntervalType();
+         String zoneType = cmd.getZoneType();
          Map<String, String> tags = cmd.getTags();
 -        
 +
          Account caller = UserContext.current().getCaller();
          List<Long> permittedAccounts = new ArrayList<Long>();
  
@@@ -583,19 -602,25 +587,25 @@@
          sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
          sb.and("snapshotTypeEQ", sb.entity().getsnapshotType(), SearchCriteria.Op.IN);
          sb.and("snapshotTypeNEQ", sb.entity().getsnapshotType(), SearchCriteria.Op.NEQ);
 -        
 +
          if (tags != null && !tags.isEmpty()) {
-         SearchBuilder<ResourceTagVO> tagSearch = _resourceTagDao.createSearchBuilder();
-         for (int count=0; count < tags.size(); count++) {
-             tagSearch.or().op("key" + String.valueOf(count), tagSearch.entity().getKey(), SearchCriteria.Op.EQ);
-             tagSearch.and("value" + String.valueOf(count), tagSearch.entity().getValue(), SearchCriteria.Op.EQ);
-             tagSearch.cp();
+             SearchBuilder<ResourceTagVO> tagSearch = _resourceTagDao.createSearchBuilder();
+             for (int count=0; count < tags.size(); count++) {
+                 tagSearch.or().op("key" + String.valueOf(count), tagSearch.entity().getKey(), SearchCriteria.Op.EQ);
+                 tagSearch.and("value" + String.valueOf(count), tagSearch.entity().getValue(), SearchCriteria.Op.EQ);
+                 tagSearch.cp();
+             }
+             tagSearch.and("resourceType", tagSearch.entity().getResourceType(), SearchCriteria.Op.EQ);
+             sb.groupBy(sb.entity().getId());
+             sb.join("tagSearch", tagSearch, sb.entity().getId(), tagSearch.entity().getResourceId(), JoinBuilder.JoinType.INNER);
          }
-         tagSearch.and("resourceType", tagSearch.entity().getResourceType(), SearchCriteria.Op.EQ);
-         sb.groupBy(sb.entity().getId());
-         sb.join("tagSearch", tagSearch, sb.entity().getId(), tagSearch.entity().getResourceId(), JoinBuilder.JoinType.INNER);
-     }
  
+         if(zoneType != null) {
+             SearchBuilder<DataCenterVO> zoneSb = _dcDao.createSearchBuilder();
+             zoneSb.and("zoneNetworkType", zoneSb.entity().getNetworkType(), SearchCriteria.Op.EQ);    
+             sb.join("zoneSb", zoneSb, sb.entity().getDataCenterId(), zoneSb.entity().getId(), JoinBuilder.JoinType.INNER);
+         }
+         
          SearchCriteria<SnapshotVO> sc = sb.create();
          _accountMgr.buildACLSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
  


Mime
View raw message