Search in sources :

Example 11 with FullEntityOvfData

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;
    }
}
Also used : VM(org.ovirt.engine.core.common.businessentities.VM) VMStaticOvfLogHandler(org.ovirt.engine.core.utils.ovf.VMStaticOvfLogHandler) Cluster(org.ovirt.engine.core.common.businessentities.Cluster) VmStatic(org.ovirt.engine.core.common.businessentities.VmStatic) FullEntityOvfData(org.ovirt.engine.core.common.businessentities.storage.FullEntityOvfData) DiskImage(org.ovirt.engine.core.common.businessentities.storage.DiskImage) OvfReaderException(org.ovirt.engine.core.utils.ovf.OvfReaderException)

Example 12 with FullEntityOvfData

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));
}
Also used : VmTemplate(org.ovirt.engine.core.common.businessentities.VmTemplate) FullEntityOvfData(org.ovirt.engine.core.common.businessentities.storage.FullEntityOvfData)

Example 13 with FullEntityOvfData

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);
}
Also used : VM(org.ovirt.engine.core.common.businessentities.VM) FullEntityOvfData(org.ovirt.engine.core.common.businessentities.storage.FullEntityOvfData) OvfReaderException(org.ovirt.engine.core.utils.ovf.OvfReaderException)

Example 14 with FullEntityOvfData

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());
}
Also used : ClusterDao(org.ovirt.engine.core.dao.ClusterDao) SnapshotDao(org.ovirt.engine.core.dao.SnapshotDao) VmTemplateDao(org.ovirt.engine.core.dao.VmTemplateDao) VmStaticDao(org.ovirt.engine.core.dao.VmStaticDao) KeyValuePairCompat(org.ovirt.engine.core.compat.KeyValuePairCompat) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) Spy(org.mockito.Spy) Snapshot(org.ovirt.engine.core.common.businessentities.Snapshot) StorageDomainOvfInfoDao(org.ovirt.engine.core.dao.StorageDomainOvfInfoDao) StoragePoolDao(org.ovirt.engine.core.dao.StoragePoolDao) ClassRule(org.junit.ClassRule) Mockito.doReturn(org.mockito.Mockito.doReturn) MockConfigRule.mockConfig(org.ovirt.engine.core.utils.MockConfigRule.mockConfig) Collection(java.util.Collection) VmTemplate(org.ovirt.engine.core.common.businessentities.VmTemplate) StorageConstants(org.ovirt.engine.core.common.constants.StorageConstants) Set(java.util.Set) Mockito.doNothing(org.mockito.Mockito.doNothing) Collectors(java.util.stream.Collectors) DbUser(org.ovirt.engine.core.common.businessentities.aaa.DbUser) VmDao(org.ovirt.engine.core.dao.VmDao) ProcessOvfUpdateParameters(org.ovirt.engine.core.common.action.ProcessOvfUpdateParameters) DbUserDao(org.ovirt.engine.core.dao.DbUserDao) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) VmAndTemplatesGenerationsDao(org.ovirt.engine.core.dao.VmAndTemplatesGenerationsDao) LabelDao(org.ovirt.engine.core.dao.LabelDao) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) MockConfigRule(org.ovirt.engine.core.utils.MockConfigRule) StoragePool(org.ovirt.engine.core.common.businessentities.StoragePool) Mock(org.mockito.Mock) Guid(org.ovirt.engine.core.compat.Guid) StorageDomain(org.ovirt.engine.core.common.businessentities.StorageDomain) StorageDomainOvfInfo(org.ovirt.engine.core.common.businessentities.StorageDomainOvfInfo) HashMap(java.util.HashMap) StorageDomainStatus(org.ovirt.engine.core.common.businessentities.StorageDomainStatus) DiskImage(org.ovirt.engine.core.common.businessentities.storage.DiskImage) ArrayList(java.util.ArrayList) ImageStatus(org.ovirt.engine.core.common.businessentities.storage.ImageStatus) CollectionUtils(org.apache.commons.collections.CollectionUtils) AffinityGroupDao(org.ovirt.engine.core.dao.scheduling.AffinityGroupDao) StorageDomainOvfInfoStatus(org.ovirt.engine.core.common.businessentities.StorageDomainOvfInfoStatus) LinkedList(java.util.LinkedList) VmStatic(org.ovirt.engine.core.common.businessentities.VmStatic) Pair(org.ovirt.engine.core.common.utils.Pair) Before(org.junit.Before) InjectMocks(org.mockito.InjectMocks) StoragePoolStatus(org.ovirt.engine.core.common.businessentities.StoragePoolStatus) FullEntityOvfData(org.ovirt.engine.core.common.businessentities.storage.FullEntityOvfData) VmTemplateStatus(org.ovirt.engine.core.common.businessentities.VmTemplateStatus) Assert.assertTrue(org.junit.Assert.assertTrue) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) ConfigValues(org.ovirt.engine.core.common.config.ConfigValues) PermissionDao(org.ovirt.engine.core.dao.PermissionDao) BaseCommandTest(org.ovirt.engine.core.bll.BaseCommandTest) Mockito.verify(org.mockito.Mockito.verify) Mockito.never(org.mockito.Mockito.never) VM(org.ovirt.engine.core.common.businessentities.VM) StorageDomainDao(org.ovirt.engine.core.dao.StorageDomainDao) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) VMStatus(org.ovirt.engine.core.common.businessentities.VMStatus) StoragePoolIsoMap(org.ovirt.engine.core.common.businessentities.StoragePoolIsoMap) KeyValuePairCompat(org.ovirt.engine.core.compat.KeyValuePairCompat) StoragePoolStatus(org.ovirt.engine.core.common.businessentities.StoragePoolStatus) Guid(org.ovirt.engine.core.compat.Guid) StorageDomain(org.ovirt.engine.core.common.businessentities.StorageDomain) VmTemplate(org.ovirt.engine.core.common.businessentities.VmTemplate) VM(org.ovirt.engine.core.common.businessentities.VM) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Map(java.util.Map) HashMap(java.util.HashMap) StoragePoolIsoMap(org.ovirt.engine.core.common.businessentities.StoragePoolIsoMap) Pair(org.ovirt.engine.core.common.utils.Pair)

Example 15 with FullEntityOvfData

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));
}
Also used : Snapshot(org.ovirt.engine.core.common.businessentities.Snapshot) VM(org.ovirt.engine.core.common.businessentities.VM) ArrayList(java.util.ArrayList) DiskVmElement(org.ovirt.engine.core.common.businessentities.storage.DiskVmElement) Guid(org.ovirt.engine.core.compat.Guid) FullEntityOvfData(org.ovirt.engine.core.common.businessentities.storage.FullEntityOvfData) DiskImage(org.ovirt.engine.core.common.businessentities.storage.DiskImage) Test(org.junit.Test) BaseCommandTest(org.ovirt.engine.core.bll.BaseCommandTest)

Aggregations

FullEntityOvfData (org.ovirt.engine.core.common.businessentities.storage.FullEntityOvfData)27 VM (org.ovirt.engine.core.common.businessentities.VM)17 DiskImage (org.ovirt.engine.core.common.businessentities.storage.DiskImage)13 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)8 Guid (org.ovirt.engine.core.compat.Guid)7 VmTemplate (org.ovirt.engine.core.common.businessentities.VmTemplate)6 HashMap (java.util.HashMap)5 OvfReaderException (org.ovirt.engine.core.utils.ovf.OvfReaderException)5 OvfEntityData (org.ovirt.engine.core.common.businessentities.OvfEntityData)4 DbUser (org.ovirt.engine.core.common.businessentities.aaa.DbUser)4 KeyValuePairCompat (org.ovirt.engine.core.compat.KeyValuePairCompat)4 HashSet (java.util.HashSet)3 List (java.util.List)2 Map (java.util.Map)2 Before (org.junit.Before)2 BaseCommandTest (org.ovirt.engine.core.bll.BaseCommandTest)2 Cluster (org.ovirt.engine.core.common.businessentities.Cluster)2 Label (org.ovirt.engine.core.common.businessentities.Label)2 Snapshot (org.ovirt.engine.core.common.businessentities.Snapshot)2