@Override
public PrimaryStorageDownloadAnswer primaryStorageDownload(PrimaryStorageDownloadCommand cmd) {
MockVolumeVO template = findVolumeFromSecondary(cmd.getUrl(), cmd.getSecondaryStorageUrl(),
MockVolumeType.TEMPLATE);
if (template == null) {
return new PrimaryStorageDownloadAnswer("Can't find primary storage");
}
Transaction txn = Transaction.open(Transaction.SIMULATOR_DB);
MockStoragePoolVO primaryStorage = null;
try {
txn.start();
primaryStorage = _mockStoragePoolDao.findByUuid(cmd.getPoolUuid());
txn.commit();
if (primaryStorage == null) {
return new PrimaryStorageDownloadAnswer("Can't find primary storage");
}
} catch (Exception ex) {
txn.rollback();
throw new CloudRuntimeException("Error when finding primary storagee " + cmd.getPoolUuid(), ex);
} finally {
txn.close();
txn = Transaction.open(Transaction.CLOUD_DB);
txn.close();
}
String volumeName = UUID.randomUUID().toString();
MockVolumeVO newVolume = new MockVolumeVO();
newVolume.setName(volumeName);
newVolume.setPath(primaryStorage.getMountPoint() + volumeName);
newVolume.setPoolId(primaryStorage.getId());
newVolume.setSize(template.getSize());
newVolume.setType(MockVolumeType.VOLUME);
txn = Transaction.open(Transaction.SIMULATOR_DB);
try {
txn.start();
_mockVolumeDao.persist(newVolume);
txn.commit();
} catch (Exception ex) {
txn.rollback();
throw new CloudRuntimeException("Error when saving volume " + newVolume, ex);
} finally {
txn.close();
txn = Transaction.open(Transaction.CLOUD_DB);
txn.close();
}
return new PrimaryStorageDownloadAnswer(newVolume.getPath(), newVolume.getSize());
}