@Override
  public void createAsync(
      DataStore dataStore,
      DataObject dataObject,
      AsyncCompletionCallback<CreateCmdResult> callback) {
    String iqn = null;
    String errMsg = null;

    if (dataObject.getType() == DataObjectType.VOLUME) {
      VolumeInfo volumeInfo = (VolumeInfo) dataObject;
      AccountVO account = _accountDao.findById(volumeInfo.getAccountId());
      String sfAccountName = getSfAccountName(account.getUuid(), account.getAccountId());

      long storagePoolId = dataStore.getId();
      SolidFireConnection sfConnection = getSolidFireConnection(storagePoolId);

      if (!sfAccountExists(sfAccountName, sfConnection)) {
        SolidFireUtil.SolidFireAccount sfAccount =
            createSolidFireAccount(sfAccountName, sfConnection);

        updateCsDbWithAccountInfo(account.getId(), sfAccount);
      }

      SolidFireUtil.SolidFireVolume sfVolume = createSolidFireVolume(volumeInfo, sfConnection);

      iqn = sfVolume.getIqn();

      VolumeVO volume = this._volumeDao.findById(volumeInfo.getId());

      volume.set_iScsiName(iqn);
      volume.setFolder(String.valueOf(sfVolume.getId()));
      volume.setPoolType(StoragePoolType.IscsiLUN);
      volume.setPoolId(storagePoolId);

      _volumeDao.update(volume.getId(), volume);

      StoragePoolVO storagePool = _storagePoolDao.findById(dataStore.getId());

      long capacityBytes = storagePool.getCapacityBytes();
      long usedBytes = storagePool.getUsedBytes();

      usedBytes += volumeInfo.getSize();

      storagePool.setUsedBytes(usedBytes > capacityBytes ? capacityBytes : usedBytes);

      _storagePoolDao.update(storagePoolId, storagePool);
    } else {
      errMsg = "Invalid DataObjectType (" + dataObject.getType() + ") passed to createAsync";
    }

    // path = iqn
    // size is pulled from DataObject instance, if errMsg is null
    CreateCmdResult result = new CreateCmdResult(iqn, new Answer(null, errMsg == null, errMsg));

    result.setResult(errMsg);

    callback.complete(result);
  }
  protected Void createTemplateAsyncCallback(
      AsyncCallbackDispatcher<? extends BaseImageStoreDriverImpl, DownloadAnswer> callback,
      CreateContext<CreateCmdResult> context) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("Performing image store createTemplate async callback");
    }
    DownloadAnswer answer = callback.getResult();
    DataObject obj = context.data;
    DataStore store = obj.getDataStore();

    TemplateDataStoreVO tmpltStoreVO =
        _templateStoreDao.findByStoreTemplate(store.getId(), obj.getId());
    if (tmpltStoreVO != null) {
      if (tmpltStoreVO.getDownloadState() == VMTemplateStorageResourceAssoc.Status.DOWNLOADED) {
        if (s_logger.isDebugEnabled()) {
          s_logger.debug(
              "Template is already in DOWNLOADED state, ignore further incoming DownloadAnswer");
        }
        return null;
      }
      TemplateDataStoreVO updateBuilder = _templateStoreDao.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());
      _templateStoreDao.update(tmpltStoreVO.getId(), updateBuilder);
      // update size in vm_template table
      VMTemplateVO tmlptUpdater = _templateDao.createForUpdate();
      tmlptUpdater.setSize(answer.getTemplateSize());
      _templateDao.update(obj.getId(), tmlptUpdater);
    }

    AsyncCompletionCallback<CreateCmdResult> caller = context.getParentCallback();

    if (answer.getDownloadStatus() == VMTemplateStorageResourceAssoc.Status.DOWNLOAD_ERROR
        || answer.getDownloadStatus() == VMTemplateStorageResourceAssoc.Status.ABANDONED
        || answer.getDownloadStatus() == VMTemplateStorageResourceAssoc.Status.UNKNOWN) {
      CreateCmdResult result = new CreateCmdResult(null, null);
      result.setSuccess(false);
      result.setResult(answer.getErrorString());
      caller.complete(result);
    } else if (answer.getDownloadStatus() == VMTemplateStorageResourceAssoc.Status.DOWNLOADED) {
      if (answer.getCheckSum() != null) {
        VMTemplateVO templateDaoBuilder = _templateDao.createForUpdate();
        templateDaoBuilder.setChecksum(answer.getCheckSum());
        _templateDao.update(obj.getId(), templateDaoBuilder);
      }

      CreateCmdResult result = new CreateCmdResult(null, null);
      caller.complete(result);
    }
    return null;
  }
  @Override
  public AsyncCallFuture<TemplateApiResult> copyTemplate(
      TemplateInfo srcTemplate, DataStore destStore) {
    // generate a URL from source template ssvm to download to destination data store
    String url = generateCopyUrl(srcTemplate);
    if (url == null) {
      s_logger.warn(
          "Unable to start/resume copy of template "
              + srcTemplate.getUniqueName()
              + " to "
              + destStore.getName()
              + ", no secondary storage vm in running state in source zone");
      throw new CloudRuntimeException("No secondary VM in running state in source template zone ");
    }

    TemplateObject tmplForCopy =
        (TemplateObject) _templateFactory.getTemplate(srcTemplate, destStore);
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("Setting source template url to " + url);
    }
    tmplForCopy.setUrl(url);

    if (s_logger.isDebugEnabled()) {
      s_logger.debug("Mark template_store_ref entry as Creating");
    }
    AsyncCallFuture<TemplateApiResult> future = new AsyncCallFuture<TemplateApiResult>();
    DataObject templateOnStore = destStore.create(tmplForCopy);
    templateOnStore.processEvent(Event.CreateOnlyRequested);

    if (s_logger.isDebugEnabled()) {
      s_logger.debug("Invoke datastore driver createAsync to create template on destination store");
    }
    try {
      TemplateOpContext<TemplateApiResult> context =
          new TemplateOpContext<TemplateApiResult>(null, (TemplateObject) templateOnStore, future);
      AsyncCallbackDispatcher<TemplateServiceImpl, CreateCmdResult> caller =
          AsyncCallbackDispatcher.create(this);
      caller
          .setCallback(caller.getTarget().copyTemplateCrossZoneCallBack(null, null))
          .setContext(context);
      destStore.getDriver().createAsync(destStore, templateOnStore, caller);
    } catch (CloudRuntimeException ex) {
      // clean up already persisted template_store_ref entry in case of createTemplateCallback is
      // never called
      TemplateDataStoreVO templateStoreVO =
          _vmTemplateStoreDao.findByStoreTemplate(destStore.getId(), srcTemplate.getId());
      if (templateStoreVO != null) {
        TemplateInfo tmplObj = _templateFactory.getTemplate(srcTemplate, destStore);
        tmplObj.processEvent(ObjectInDataStoreStateMachine.Event.OperationFailed);
      }
      TemplateApiResult res = new TemplateApiResult((TemplateObject) templateOnStore);
      res.setResult(ex.getMessage());
      future.complete(res);
    }
    return future;
  }
  private AsyncCallFuture<TemplateApiResult> copyAsync(
      DataObject source, TemplateInfo template, DataStore store) {
    AsyncCallFuture<TemplateApiResult> future = new AsyncCallFuture<TemplateApiResult>();
    DataObject templateOnStore = store.create(template);
    templateOnStore.processEvent(Event.CreateOnlyRequested);

    TemplateOpContext<TemplateApiResult> context =
        new TemplateOpContext<TemplateApiResult>(null, (TemplateObject) templateOnStore, future);
    AsyncCallbackDispatcher<TemplateServiceImpl, CopyCommandResult> caller =
        AsyncCallbackDispatcher.create(this);
    caller.setCallback(caller.getTarget().copyTemplateCallBack(null, null)).setContext(context);
    _motionSrv.copyAsync(source, templateOnStore, caller);
    return future;
  }
  @Override
  public void deleteAsync(
      DataStore dataStore, DataObject dataObject, AsyncCompletionCallback<CommandResult> callback) {
    String errMsg = null;

    if (dataObject.getType() == DataObjectType.VOLUME) {
      VolumeInfo volumeInfo = (VolumeInfo) dataObject;
      AccountVO account = _accountDao.findById(volumeInfo.getAccountId());
      AccountDetailVO accountDetails =
          _accountDetailsDao.findDetail(account.getAccountId(), SolidFireUtil.ACCOUNT_ID);
      long sfAccountId = Long.parseLong(accountDetails.getValue());

      long storagePoolId = dataStore.getId();
      SolidFireConnection sfConnection = getSolidFireConnection(storagePoolId);

      deleteSolidFireVolume(volumeInfo, sfConnection);

      _volumeDao.deleteVolumesByInstance(volumeInfo.getId());

      //            if (!sfAccountHasVolume(sfAccountId, sfConnection)) {
      //                // delete the account from the SolidFire SAN
      //                deleteSolidFireAccount(sfAccountId, sfConnection);
      //
      //                // delete the info in the account_details table
      //                // that's related to the SolidFire account
      //                _accountDetailsDao.deleteDetails(account.getAccountId());
      //            }

      StoragePoolVO storagePool = _storagePoolDao.findById(storagePoolId);

      long usedBytes = storagePool.getUsedBytes();

      usedBytes -= volumeInfo.getSize();

      storagePool.setUsedBytes(usedBytes < 0 ? 0 : usedBytes);

      _storagePoolDao.update(storagePoolId, storagePool);
    } else {
      errMsg = "Invalid DataObjectType (" + dataObject.getType() + ") passed to deleteAsync";
    }

    CommandResult result = new CommandResult();

    result.setResult(errMsg);

    callback.complete(result);
  }
 @Override
 public void deleteAsync(
     DataStore dataStore, DataObject data, AsyncCompletionCallback<CommandResult> callback) {
   CommandResult result = new CommandResult();
   try {
     DeleteCommand cmd = new DeleteCommand(data.getTO());
     EndPoint ep = _epSelector.select(data);
     Answer answer = ep.sendMessage(cmd);
     if (answer != null && !answer.getResult()) {
       result.setResult(answer.getDetails());
     }
   } catch (Exception ex) {
     s_logger.debug("Unable to destoy " + data.getType().toString() + ": " + data.getId(), ex);
     result.setResult(ex.toString());
   }
   callback.complete(result);
 }
 @Override
 public void createAsync(
     DataStore dataStore, DataObject data, AsyncCompletionCallback<CreateCmdResult> callback) {
   CreateContext<CreateCmdResult> context = new CreateContext<CreateCmdResult>(callback, data);
   AsyncCallbackDispatcher<BaseImageStoreDriverImpl, DownloadAnswer> caller =
       AsyncCallbackDispatcher.create(this);
   caller.setContext(context);
   if (data.getType() == DataObjectType.TEMPLATE) {
     caller.setCallback(caller.getTarget().createTemplateAsyncCallback(null, null));
     if (s_logger.isDebugEnabled()) {
       s_logger.debug("Downloading template to data store " + dataStore.getId());
     }
     _downloadMonitor.downloadTemplateToStorage(data, caller);
   } else if (data.getType() == DataObjectType.VOLUME) {
     caller.setCallback(caller.getTarget().createVolumeAsyncCallback(null, null));
     if (s_logger.isDebugEnabled()) {
       s_logger.debug("Downloading volume to data store " + dataStore.getId());
     }
     _downloadMonitor.downloadVolumeToStorage(data, caller);
   }
 }
  @Override
  public void copyAsync(
      DataObject srcData,
      DataObject destData,
      AsyncCompletionCallback<CopyCommandResult> callback) {

    if (srcData.getDataStore().getDriver().canCopy(srcData, destData)) {
      srcData.getDataStore().getDriver().copyAsync(srcData, destData, callback);
      return;
    } else if (destData.getDataStore().getDriver().canCopy(srcData, destData)) {
      destData.getDataStore().getDriver().copyAsync(srcData, destData, callback);
      return;
    }

    for (DataMotionStrategy strategy : strategies) {
      if (strategy.canHandle(srcData, destData)) {
        strategy.copyAsync(srcData, destData, callback);
        return;
      }
    }
    throw new CloudRuntimeException("can't find strategy to move data");
  }
 @Override
 public void downloadTemplateToStorage(
     DataObject template, AsyncCompletionCallback<DownloadAnswer> callback) {
   if (template != null) {
     long templateId = template.getId();
     DataStore store = template.getDataStore();
     if (isTemplateUpdateable(templateId, store.getId())) {
       if (template.getUri() != null) {
         initiateTemplateDownload(template, callback);
       } else {
         s_logger.info("Template url is null, cannot download");
         DownloadAnswer ans = new DownloadAnswer("Template url is null", Status.UNKNOWN);
         callback.complete(ans);
       }
     } else {
       s_logger.info("Template download is already in progress or already downloaded");
       DownloadAnswer ans =
           new DownloadAnswer(
               "Template download is already in progress or already downloaded", Status.UNKNOWN);
       callback.complete(ans);
     }
   }
 }
  @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,
              Status.NOT_DOWNLOADED,
              null,
              null,
              "jobid0000",
              null,
              url,
              checkSum);
      _volumeStoreDao.persist(volumeHost);
    } else if ((volumeHost.getJobId() != null) && (volumeHost.getJobId().length() > 2)) {
      downloadJobExists = true;
    }

    Long maxVolumeSizeInBytes = getMaxVolumeSizeInBytes();
    if (volumeHost != null) {
      start();
      Volume vol = _volumeDao.findById(volume.getId());
      DownloadCommand dcmd =
          new DownloadCommand(
              (VolumeObjectTO) (volume.getTO()), maxVolumeSizeInBytes, checkSum, url, format);
      dcmd.setProxy(getHttpProxy());
      if (downloadJobExists) {
        dcmd = new DownloadProgressCommand(dcmd, volumeHost.getJobId(), RequestType.GET_OR_RESTART);
        dcmd.setResourceType(ResourceType.VOLUME);
      }

      EndPoint ep = _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(ep, store, volume, _timer, this, dcmd, callback);
      ComponentContext.inject(dl); // auto-wired those injected fields in DownloadListener

      if (downloadJobExists) {
        dl.setCurrState(volumeHost.getDownloadState());
      }

      try {
        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);
      }
    }
  }
  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,
              Status.NOT_DOWNLOADED,
              null,
              null,
              "jobid0000",
              null,
              template.getUri());
      vmTemplateStore.setDataStoreRole(store.getRole());
      vmTemplateStore = _vmTemplateStoreDao.persist(vmTemplateStore);
    } else if ((vmTemplateStore.getJobId() != null) && (vmTemplateStore.getJobId().length() > 2)) {
      downloadJobExists = true;
    }

    Long maxTemplateSizeInBytes = getMaxTemplateSizeInBytes();
    if (vmTemplateStore != null) {
      start();
      VirtualMachineTemplate tmpl = _templateDao.findById(template.getId());
      DownloadCommand dcmd =
          new DownloadCommand((TemplateObjectTO) (template.getTO()), maxTemplateSizeInBytes);
      dcmd.setProxy(getHttpProxy());
      if (downloadJobExists) {
        dcmd =
            new DownloadProgressCommand(
                dcmd, vmTemplateStore.getJobId(), RequestType.GET_OR_RESTART);
      }
      if (vmTemplateStore.isCopy()) {
        dcmd.setCreds(TemplateConstants.DEFAULT_HTTP_AUTH_USER, _copyAuthPasswd);
      }
      EndPoint ep = _epSelector.select(template);
      if (ep == null) {
        String errMsg =
            "There is no secondary storage VM for downloading template to image store "
                + store.getName();
        s_logger.warn(errMsg);
        throw new CloudRuntimeException(errMsg);
      }
      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.
        s_logger.info("found existing download job");
        dl.setCurrState(vmTemplateStore.getDownloadState());
      }

      try {
        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);
      }
    }
  }
Пример #12
0
  @Test(priority = -1)
  public void setUp() {
    ComponentContext.initComponentsLifeCycle();

    host = hostDao.findByGuid(this.getHostGuid());
    if (host != null) {
      dcId = host.getDataCenterId();
      clusterId = host.getClusterId();
      podId = host.getPodId();
      imageStore = this.imageStoreDao.findByName(imageStoreName);
    } else {
      // create data center
      DataCenterVO dc =
          new DataCenterVO(
              UUID.randomUUID().toString(),
              "test",
              "8.8.8.8",
              null,
              "10.0.0.1",
              null,
              "10.0.0.1/24",
              null,
              null,
              NetworkType.Basic,
              null,
              null,
              true,
              true,
              null,
              null);
      dc = dcDao.persist(dc);
      dcId = dc.getId();
      // create pod

      HostPodVO pod =
          new HostPodVO(
              UUID.randomUUID().toString(),
              dc.getId(),
              this.getHostGateway(),
              this.getHostCidr(),
              8,
              "test");
      pod = podDao.persist(pod);
      podId = pod.getId();
      // create xen cluster
      ClusterVO cluster = new ClusterVO(dc.getId(), pod.getId(), "devcloud cluster");
      cluster.setHypervisorType(HypervisorType.VMware.toString());
      cluster.setClusterType(ClusterType.ExternalManaged);
      cluster.setManagedState(ManagedState.Managed);
      cluster = clusterDao.persist(cluster);
      clusterId = cluster.getId();

      // setup vcenter
      ClusterDetailsVO clusterDetailVO = new ClusterDetailsVO(cluster.getId(), "url", null);
      this.clusterDetailsDao.persist(clusterDetailVO);
      clusterDetailVO = new ClusterDetailsVO(cluster.getId(), "username", null);
      this.clusterDetailsDao.persist(clusterDetailVO);
      clusterDetailVO = new ClusterDetailsVO(cluster.getId(), "password", null);
      this.clusterDetailsDao.persist(clusterDetailVO);
      // create xen host

      host = new HostVO(this.getHostGuid());
      host.setName("devcloud vmware host");
      host.setType(Host.Type.Routing);
      host.setPrivateIpAddress(this.getHostIp());
      host.setDataCenterId(dc.getId());
      host.setVersion("6.0.1");
      host.setAvailable(true);
      host.setSetup(true);
      host.setPodId(podId);
      host.setLastPinged(0);
      host.setResourceState(ResourceState.Enabled);
      host.setHypervisorType(HypervisorType.VMware);
      host.setClusterId(cluster.getId());

      host = hostDao.persist(host);

      imageStore = new ImageStoreVO();
      imageStore.setName(imageStoreName);
      imageStore.setDataCenterId(dcId);
      imageStore.setProviderName("CloudStack ImageStore Provider");
      imageStore.setRole(DataStoreRole.Image);
      imageStore.setUrl(this.getSecondaryStorage());
      imageStore.setUuid(UUID.randomUUID().toString());
      imageStore.setProtocol("nfs");
      imageStore = imageStoreDao.persist(imageStore);
    }

    image = new VMTemplateVO();
    image.setTemplateType(TemplateType.USER);
    image.setUrl(this.getTemplateUrl());
    image.setUniqueName(UUID.randomUUID().toString());
    image.setName(UUID.randomUUID().toString());
    image.setPublicTemplate(true);
    image.setFeatured(true);
    image.setRequiresHvm(true);
    image.setBits(64);
    image.setFormat(Storage.ImageFormat.VHD);
    image.setEnablePassword(true);
    image.setEnableSshKey(true);
    image.setGuestOSId(1);
    image.setBootable(true);
    image.setPrepopulate(true);
    image.setCrossZones(true);
    image.setExtractable(true);

    image = imageDataDao.persist(image);

    /*
     * TemplateDataStoreVO templateStore = new TemplateDataStoreVO();
     *
     * templateStore.setDataStoreId(imageStore.getId());
     * templateStore.setDownloadPercent(100);
     * templateStore.setDownloadState(Status.DOWNLOADED);
     * templateStore.setDownloadUrl(imageStore.getUrl());
     * templateStore.setInstallPath(this.getImageInstallPath());
     * templateStore.setTemplateId(image.getId());
     * templateStoreDao.persist(templateStore);
     */

    DataStore store = this.dataStoreMgr.getDataStore(imageStore.getId(), DataStoreRole.Image);
    TemplateInfo template = templateFactory.getTemplate(image.getId(), DataStoreRole.Image);
    DataObject templateOnStore = store.create(template);
    TemplateObjectTO to = new TemplateObjectTO();
    to.setPath(this.getImageInstallPath());
    CopyCmdAnswer answer = new CopyCmdAnswer(to);
    templateOnStore.processEvent(Event.CreateOnlyRequested);
    templateOnStore.processEvent(Event.OperationSuccessed, answer);
  }
  @Override
  public AsyncCallFuture<TemplateApiResult> copyTemplate(
      TemplateInfo srcTemplate, DataStore destStore) {
    // for vmware template, we need to check if ova packing is needed, since template created from
    // snapshot does not have .ova file
    // we invoke createEntityExtractURL to trigger ova packing. Ideally, we can directly use
    // extractURL to pass to following createTemplate.
    // Need to understand what is the background to use two different urls for copy and extract.
    if (srcTemplate.getFormat() == ImageFormat.OVA) {
      ImageStoreEntity tmpltStore = (ImageStoreEntity) srcTemplate.getDataStore();
      tmpltStore.createEntityExtractUrl(
          srcTemplate.getInstallPath(), srcTemplate.getFormat(), srcTemplate);
    }
    // generate a URL from source template ssvm to download to destination data store
    String url = generateCopyUrl(srcTemplate);
    if (url == null) {
      s_logger.warn(
          "Unable to start/resume copy of template "
              + srcTemplate.getUniqueName()
              + " to "
              + destStore.getName()
              + ", no secondary storage vm in running state in source zone");
      throw new CloudRuntimeException("No secondary VM in running state in source template zone ");
    }

    TemplateObject tmplForCopy =
        (TemplateObject) _templateFactory.getTemplate(srcTemplate, destStore);
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("Setting source template url to " + url);
    }
    tmplForCopy.setUrl(url);

    if (s_logger.isDebugEnabled()) {
      s_logger.debug("Mark template_store_ref entry as Creating");
    }
    AsyncCallFuture<TemplateApiResult> future = new AsyncCallFuture<TemplateApiResult>();
    DataObject templateOnStore = destStore.create(tmplForCopy);
    templateOnStore.processEvent(Event.CreateOnlyRequested);

    if (s_logger.isDebugEnabled()) {
      s_logger.debug("Invoke datastore driver createAsync to create template on destination store");
    }
    try {
      TemplateOpContext<TemplateApiResult> context =
          new TemplateOpContext<TemplateApiResult>(null, (TemplateObject) templateOnStore, future);
      AsyncCallbackDispatcher<TemplateServiceImpl, CreateCmdResult> caller =
          AsyncCallbackDispatcher.create(this);
      caller
          .setCallback(caller.getTarget().copyTemplateCrossZoneCallBack(null, null))
          .setContext(context);
      destStore.getDriver().createAsync(destStore, templateOnStore, caller);
    } catch (CloudRuntimeException ex) {
      // clean up already persisted template_store_ref entry in case of createTemplateCallback is
      // never called
      TemplateDataStoreVO templateStoreVO =
          _vmTemplateStoreDao.findByStoreTemplate(destStore.getId(), srcTemplate.getId());
      if (templateStoreVO != null) {
        TemplateInfo tmplObj = _templateFactory.getTemplate(srcTemplate, destStore);
        tmplObj.processEvent(ObjectInDataStoreStateMachine.Event.OperationFailed);
      }
      TemplateApiResult res = new TemplateApiResult((TemplateObject) templateOnStore);
      res.setResult(ex.getMessage());
      future.complete(res);
    }
    return future;
  }