use of org.ovirt.engine.core.common.businessentities.storage.FullEntityOvfData in project ovirt-engine by oVirt.
the class SnapshotsManager method updateVmFromConfiguration.
/**
* Update the given VM with the (static) data that is contained in the configuration. The {@link VM#getImages()}
* will contain the images that were read from the configuration.
*
* @param vm
* The VM to update.
* @param configuration
* The configuration to update from.
* @return In case of a problem reading the configuration, <code>false</code>. Otherwise, <code>true</code>.
*/
public boolean updateVmFromConfiguration(VM vm, String configuration) {
try {
VmStatic oldVmStatic = vm.getStaticData();
VM tempVM = new VM();
if (vm.getDynamicData() != null) {
tempVM.setDynamicData(vm.getDynamicData());
}
FullEntityOvfData fullEntityOvfData = new FullEntityOvfData(tempVM);
ovfManager.importVm(configuration, tempVM, fullEntityOvfData);
for (DiskImage diskImage : fullEntityOvfData.getDiskImages()) {
DiskImage dbImage = diskImageDao.getSnapshotById(diskImage.getImageId());
if (dbImage != null) {
diskImage.setStorageIds(dbImage.getStorageIds());
}
}
new VMStaticOvfLogHandler(tempVM.getStaticData()).resetDefaults(oldVmStatic);
vm.setStaticData(tempVM.getStaticData());
IconUtils.preserveIcons(vm.getStaticData(), oldVmStatic);
vm.setImages((ArrayList<DiskImage>) fullEntityOvfData.getDiskImages());
vm.setInterfaces(fullEntityOvfData.getInterfaces());
// These fields are not saved in the OVF, so get them from the current VM.
vm.setIsoPath(oldVmStatic.getIsoPath());
vm.setCpuProfileId(oldVmStatic.getCpuProfileId());
vm.setClusterId(oldVmStatic.getClusterId());
// The VM configuration does not hold the vds group Id.
// It is necessary to fetch the vm static from the Db, in order to get this information
VmStatic vmStaticFromDb = vmStaticDao.get(vm.getId());
if (vmStaticFromDb != null) {
Cluster cluster = clusterDao.get(vmStaticFromDb.getClusterId());
if (cluster != null) {
vm.setStoragePoolId(cluster.getStoragePoolId());
vm.setClusterCompatibilityVersion(cluster.getCompatibilityVersion());
vm.setClusterName(cluster.getName());
vm.setClusterCpuName(cluster.getCpuName());
}
}
// if the required dedicated host is invalid -> use current VM dedicated host
if (!vmHandler.validateDedicatedVdsExistOnSameCluster(vm.getStaticData()).isValid()) {
vm.setDedicatedVmForVdsList(oldVmStatic.getDedicatedVmForVdsList());
}
vmHandler.updateMaxMemorySize(vm.getStaticData(), vm.getCompatibilityVersion());
vmHandler.autoSelectResumeBehavior(vm.getStaticData(), vm.getCompatibilityVersion());
validateQuota(vm);
return true;
} catch (OvfReaderException e) {
log.error("Failed to update VM from the configuration '{}': {}", configuration, e.getMessage());
log.debug("Exception", e);
return false;
}
}
use of org.ovirt.engine.core.common.businessentities.storage.FullEntityOvfData in project ovirt-engine by oVirt.
the class SnapshotsManager method generateVmConfiguration.
/**
* Generate a string containing the given VM's configuration.
*
* @param vm
* The VM to generate configuration from.
* @return A String containing the VM configuration.
*/
protected String generateVmConfiguration(VM vm, List<DiskImage> disks, Map<Guid, VmDevice> vmDevices) {
if (vm.getInterfaces() == null || vm.getInterfaces().isEmpty()) {
vm.setInterfaces(vmNetworkInterfaceDao.getAllForVm(vm.getId()));
}
if (StringUtils.isEmpty(vm.getVmtName())) {
VmTemplate t = vmTemplateDao.get(vm.getVmtGuid());
vm.setVmtName(t.getName());
}
if (vmDevices == null) {
vmDeviceUtils.setVmDevices(vm.getStaticData());
} else {
vm.getStaticData().setManagedDeviceMap(vmDevices);
}
if (disks == null) {
disks = DisksFilter.filterImageDisks(diskDao.getAllForVm(vm.getId()), ONLY_SNAPABLE, ONLY_ACTIVE);
disks.addAll(imagesHandler.getCinderLeafImages(diskDao.getAllForVm(vm.getId())));
}
populateDisksWithVmData(disks, vm.getId());
disks.forEach(image -> image.setStorageIds(null));
FullEntityOvfData fullEntityOvfData = new FullEntityOvfData(vm);
fullEntityOvfData.setDiskImages(disks);
return ovfManager.exportVm(vm, fullEntityOvfData, clusterUtils.getCompatibilityVersion(vm));
}
use of org.ovirt.engine.core.common.businessentities.storage.FullEntityOvfData in project ovirt-engine by oVirt.
the class SnapshotsManager method canRestoreVmConfigurationFromSnapshot.
public boolean canRestoreVmConfigurationFromSnapshot(VM vm, Snapshot snapshot, VmInterfaceManager vmInterfaceManager) {
if (snapshot.getVmConfiguration() == null) {
return false;
}
VM tempVM = new VM();
if (vm.getDynamicData() != null) {
tempVM.setDynamicData(vm.getDynamicData());
}
FullEntityOvfData fullEntityOvfData = new FullEntityOvfData(tempVM);
try {
ovfManager.importVm(snapshot.getVmConfiguration(), tempVM, fullEntityOvfData);
} catch (OvfReaderException e) {
log.error("Failed to import VM from the configuration '{}': {}", snapshot.getVmConfiguration(), e.getMessage());
log.debug("Exception", e);
return false;
}
boolean macsInSnapshotAreExpectedToBeAlreadyAllocated = SnapshotType.STATELESS.equals(snapshot.getType());
return canSynchronizeNics(vm, vmInterfaceManager, fullEntityOvfData.getInterfaces(), macsInSnapshotAreExpectedToBeAlreadyAllocated);
}
use of org.ovirt.engine.core.common.businessentities.storage.FullEntityOvfData in project ovirt-engine by oVirt.
the class ProcessOvfUpdateForStoragePoolCommandTest method mockAnswers.
private void mockAnswers() {
doAnswer(invocation -> {
VM vm = (VM) invocation.getArguments()[0];
return vm.getId().toString();
}).when(ovfUpdateProcessHelper).generateVmMetadata(any(), any());
doAnswer(invocation -> {
VmTemplate template = (VmTemplate) ((FullEntityOvfData) invocation.getArguments()[0]).getVmBase();
return template.getId().toString();
}).when(ovfUpdateProcessHelper).generateVmTemplateMetadata(any());
doAnswer(invocation -> {
List<Guid> neededIds = (List<Guid>) invocation.getArguments()[0];
return neededIds.stream().map(id -> vms.get(id)).collect(Collectors.toList());
}).when(vmDao).getVmsByIds(any());
doAnswer(invocation -> {
List<Guid> neededIds = (List<Guid>) invocation.getArguments()[0];
return neededIds.stream().map(id -> templates.get(id)).collect(Collectors.toList());
}).when(vmTemplateDao).getVmTemplatesByIds(any());
doAnswer(invocation -> {
Map<Guid, KeyValuePairCompat<String, List<Guid>>> updateMap = (Map<Guid, KeyValuePairCompat<String, List<Guid>>>) invocation.getArguments()[1];
assertTrue("too many ovfs were sent in one vdsm call", updateMap.size() <= ITEMS_COUNT_PER_UPDATE);
return true;
}).when(ovfUpdateProcessHelper).executeUpdateVmInSpmCommand(any(), any(), any());
doReturn(true).when(ovfUpdateProcessHelper).executeRemoveVmInSpm(any(), any(), any());
doAnswer(invocation -> {
List<Guid> ids = (List<Guid>) invocation.getArguments()[0];
List<Long> values = (List<Long>) invocation.getArguments()[1];
assertFalse("update of ovf version in db shouldn't be called with an empty value list", values.isEmpty());
assertTrue("update of ovf version in db shouldn't be called with more items then MAX_ITEMS_PER_SQL_STATEMENT", values.size() <= StorageConstants.OVF_MAX_ITEMS_PER_SQL_STATEMENT);
assertEquals("the size of the list of ids for update is not the same as the size of the " + "list with the new ovf values", values.size(), ids.size());
Guid[] ids_array = ids.toArray(new Guid[ids.size()]);
Long[] values_array = values.toArray(new Long[values.size()]);
for (int i = 0; i < ids_array.length; i++) {
executedUpdatedOvfGenerationIdsInDb.put(ids_array[i], values_array[i]);
}
return null;
}).when(vmAndTemplatesGenerationsDao).updateOvfGenerations(any(), any(), any());
doAnswer(invocation -> {
StoragePoolStatus desiredStatus = (StoragePoolStatus) invocation.getArguments()[0];
return buildStoragePoolsList().stream().filter(p -> desiredStatus.equals(p.getStatus())).collect(Collectors.toList());
}).when(storagePoolDao).getAllByStatus(any());
doReturn(poolDomainsOvfInfo.values().stream().map(Pair::getSecond).collect(Collectors.toList())).when(storageDomainDao).getAllForStoragePool(any());
doAnswer(invocation -> {
Guid domainId = (Guid) invocation.getArguments()[0];
Pair<List<StorageDomainOvfInfo>, StorageDomain> pair = poolDomainsOvfInfo.get(domainId);
if (pair != null) {
return pair.getFirst();
}
return null;
}).when(storageDomainOvfInfoDao).getAllForDomain(any());
}
use of org.ovirt.engine.core.common.businessentities.storage.FullEntityOvfData in project ovirt-engine by oVirt.
the class RemoveImageCommandTest method testRemoveImageFromSnapshotConfiguration.
@Test
public void testRemoveImageFromSnapshotConfiguration() throws OvfReaderException {
Guid vmId = Guid.newGuid();
VM vm = new VM();
vm.setId(vmId);
vm.setStoragePoolId(Guid.newGuid());
vm.setVmtName(RandomUtils.instance().nextString(10));
vm.setOrigin(OriginType.OVIRT);
vm.setDbGeneration(1L);
Guid vmSnapshotId = Guid.newGuid();
DiskImage disk1 = addTestDisk(vm, vmSnapshotId);
DiskVmElement dve1 = new DiskVmElement(disk1.getId(), vm.getId());
dve1.setDiskInterface(DiskInterface.VirtIO);
disk1.setDiskVmElements(Collections.singletonList(dve1));
DiskImage disk2 = addTestDisk(vm, vmSnapshotId);
DiskVmElement dve2 = new DiskVmElement(disk2.getId(), vm.getId());
dve2.setDiskInterface(DiskInterface.IDE);
disk2.setDiskVmElements(Collections.singletonList(dve2));
mcr.mockConfigValue(ConfigValues.PassDiscardSupported, Version.getLast(), true);
mcr.mockConfigValue(ConfigValues.PassDiscardSupported, Version.ALL.get(0), true);
mcr.mockConfigValue(ConfigValues.MaxNumOfVmSockets, Version.getLast(), 16);
mcr.mockConfigValue(ConfigValues.MaxNumOfVmSockets, Version.ALL.get(0), 16);
mcr.mockConfigValue(ConfigValues.MaxNumOfVmCpus, Version.getLast(), 16);
mcr.mockConfigValue(ConfigValues.MaxNumOfVmCpus, Version.ALL.get(0), 16);
ArrayList<DiskImage> disks = new ArrayList<>(Arrays.asList(disk1, disk2));
FullEntityOvfData fullEntityOvfDataForExport = new FullEntityOvfData(vm);
fullEntityOvfDataForExport.setDiskImages(disks);
String ovf = ovfManager.exportVm(vm, fullEntityOvfDataForExport, Version.getLast());
Snapshot snap = new Snapshot();
snap.setVmConfiguration(ovf);
snap.setId(vmSnapshotId);
doReturn(disk2).when(cmd).getDiskImage();
doReturn(disk2).when(cmd).getImage();
doReturn(disk2.getId()).when(cmd).getImageId();
Snapshot actual = imagesHandler.prepareSnapshotConfigWithAlternateImage(snap, disk2.getImageId(), null, ovfManager);
String actualOvf = actual.getVmConfiguration();
VM emptyVm = new VM();
FullEntityOvfData fullEntityOvfData = new FullEntityOvfData(emptyVm);
ovfManager.importVm(actualOvf, emptyVm, fullEntityOvfData);
assertEquals("Wrong number of disks", 1, fullEntityOvfData.getDiskImages().size());
assertEquals("Wrong disk", disk1, fullEntityOvfData.getDiskImages().get(0));
}
Aggregations