use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.
the class MockStorageManagerImpl method ManageSnapshot.
@Override
public ManageSnapshotAnswer ManageSnapshot(ManageSnapshotCommand cmd) {
String volPath = cmd.getVolumePath();
MockVolumeVO volume = null;
MockStoragePoolVO storagePool = null;
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
try {
txn.start();
volume = _mockVolumeDao.findByStoragePathAndType(volPath);
if (volume == null) {
return new ManageSnapshotAnswer(cmd, false, "Can't find the volume");
}
storagePool = _mockStoragePoolDao.findById(volume.getPoolId());
if (storagePool == null) {
return new ManageSnapshotAnswer(cmd, false, "Can't find the storage pooll");
}
txn.commit();
} catch (Exception ex) {
txn.rollback();
throw new CloudRuntimeException("Unable to perform snapshot", ex);
} finally {
txn.close();
txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
txn.close();
}
String mountPoint = storagePool.getMountPoint();
MockVolumeVO snapshot = new MockVolumeVO();
snapshot.setName(cmd.getSnapshotName());
snapshot.setPath(mountPoint + cmd.getSnapshotName());
snapshot.setSize(volume.getSize());
snapshot.setPoolId(storagePool.getId());
snapshot.setType(MockVolumeType.SNAPSHOT);
snapshot.setStatus(Status.DOWNLOADED);
txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
try {
txn.start();
snapshot = _mockVolumeDao.persist(snapshot);
txn.commit();
} catch (Exception ex) {
txn.rollback();
throw new CloudRuntimeException("Error when saving snapshot " + snapshot, ex);
} finally {
txn.close();
txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
txn.close();
}
return new ManageSnapshotAnswer(cmd, snapshot.getId(), snapshot.getPath(), true, "");
}
use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.
the class MockStorageManagerImpl method SecStorageSetup.
@Override
public Answer SecStorageSetup(SecStorageSetupCommand cmd) {
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
MockSecStorageVO storage = null;
try {
txn.start();
storage = _mockSecStorageDao.findByUrl(cmd.getSecUrl());
if (storage == null) {
return new Answer(cmd, false, "can't find the storage");
}
txn.commit();
} catch (Exception ex) {
txn.rollback();
throw new CloudRuntimeException("Error when setting up sec storage" + cmd.getSecUrl(), ex);
} finally {
txn.close();
txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
txn.close();
}
return new SecStorageSetupAnswer(storage.getMountPoint());
}
use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.
the class MockStorageManagerImpl method CreatePrivateTemplateFromVolume.
@Override
public CreatePrivateTemplateAnswer CreatePrivateTemplateFromVolume(CreatePrivateTemplateFromVolumeCommand cmd) {
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
MockVolumeVO volume = null;
MockSecStorageVO sec = null;
try {
txn.start();
volume = _mockVolumeDao.findByStoragePathAndType(cmd.getVolumePath());
if (volume == null) {
return new CreatePrivateTemplateAnswer(cmd, false, "cant' find volume" + cmd.getVolumePath());
}
sec = _mockSecStorageDao.findByUrl(cmd.getSecondaryStorageUrl());
if (sec == null) {
return new CreatePrivateTemplateAnswer(cmd, false, "can't find secondary storage");
}
txn.commit();
} catch (Exception ex) {
txn.rollback();
throw new CloudRuntimeException("Error when creating private template from volume");
} finally {
txn.close();
txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
txn.close();
}
MockVolumeVO template = new MockVolumeVO();
String uuid = UUID.randomUUID().toString();
template.setName(uuid);
template.setPath(sec.getMountPoint() + uuid);
template.setPoolId(sec.getId());
template.setSize(volume.getSize());
template.setStatus(Status.DOWNLOADED);
template.setType(MockVolumeType.TEMPLATE);
txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
try {
txn.start();
template = _mockVolumeDao.persist(template);
txn.commit();
} catch (Exception ex) {
txn.rollback();
throw new CloudRuntimeException("Encountered " + ex.getMessage() + " when persisting template " + template.getName(), ex);
} finally {
txn.close();
txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
txn.close();
}
return new CreatePrivateTemplateAnswer(cmd, true, "", template.getName(), template.getSize(), template.getSize(), template.getName(), ImageFormat.QCOW2);
}
use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.
the class MockStorageManagerImpl method ListVolumes.
@Override
public Answer ListVolumes(ListVolumeCommand cmd) {
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
MockSecStorageVO storage = null;
try {
txn.start();
storage = _mockSecStorageDao.findByUrl(cmd.getSecUrl());
if (storage == null) {
return new Answer(cmd, false, "Failed to get secondary storage");
}
txn.commit();
} catch (Exception ex) {
txn.rollback();
throw new CloudRuntimeException("Error when finding sec storage " + cmd.getSecUrl(), ex);
} finally {
txn.close();
txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
txn.close();
}
txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
try {
txn.start();
List<MockVolumeVO> volumes = _mockVolumeDao.findByStorageIdAndType(storage.getId(), MockVolumeType.VOLUME);
Map<Long, TemplateProp> templateInfos = new HashMap<Long, TemplateProp>();
for (MockVolumeVO volume : volumes) {
templateInfos.put(volume.getId(), new TemplateProp(volume.getName(), volume.getPath().replaceAll(storage.getMountPoint(), ""), volume.getSize(), volume.getSize(), true, false));
}
txn.commit();
return new ListVolumeAnswer(cmd.getSecUrl(), templateInfos);
} catch (Exception ex) {
txn.rollback();
throw new CloudRuntimeException("Error when finding template on sec storage " + storage.getId(), ex);
} finally {
txn.close();
txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
txn.close();
}
}
use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.
the class MockStorageManagerImpl method ComputeChecksum.
@Override
public Answer ComputeChecksum(ComputeChecksumCommand cmd) {
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
try {
txn.start();
MockVolumeVO volume = _mockVolumeDao.findByName(cmd.getTemplatePath());
if (volume == null) {
return new Answer(cmd, false, "cant' find volume:" + cmd.getTemplatePath());
}
String md5 = null;
try {
MessageDigest md = MessageDigest.getInstance("md5");
md5 = String.format("%032x", new BigInteger(1, md.digest(cmd.getTemplatePath().getBytes())));
} catch (NoSuchAlgorithmException e) {
s_logger.debug("failed to gernerate md5:" + e.toString());
}
txn.commit();
return new Answer(cmd, true, md5);
} finally {
txn.close();
txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
txn.close();
}
}
Aggregations