Search in sources :

Example 1 with FullEntityOvfData

use of org.ovirt.engine.core.common.businessentities.storage.FullEntityOvfData in project ovirt-engine by oVirt.

the class ImportVmFromConfigurationCommand method initUnregisteredVM.

private void initUnregisteredVM() {
    List<OvfEntityData> ovfEntityDataList = unregisteredOVFDataDao.getByEntityIdAndStorageDomain(getParameters().getContainerId(), getParameters().getStorageDomainId());
    if (!ovfEntityDataList.isEmpty()) {
        try {
            // We should get only one entity, since we fetched the entity with a specific Storage Domain
            ovfEntityData = ovfEntityDataList.get(0);
            FullEntityOvfData fullEntityOvfData = ovfHelper.readVmFromOvf(ovfEntityData.getOvfData());
            VM vmFromConfiguration = fullEntityOvfData.getVm();
            if (Guid.isNullOrEmpty(getParameters().getClusterId())) {
                Cluster cluster = drMappingHelper.getMappedCluster(fullEntityOvfData.getClusterName(), vmFromConfiguration.getId(), getParameters().getClusterMap());
                if (cluster != null) {
                    getParameters().setClusterId(cluster.getId());
                }
            }
            vmFromConfiguration.setClusterId(getParameters().getClusterId());
            getParameters().setVm(vmFromConfiguration);
            getParameters().setDestDomainId(ovfEntityData.getStorageDomainId());
            getParameters().setSourceDomainId(ovfEntityData.getStorageDomainId());
            getParameters().setUserToRoles(fullEntityOvfData.getUserToRoles());
            // For quota, update disks when required
            if (getParameters().getDiskMap() != null) {
                vmFromConfiguration.setDiskMap(getParameters().getDiskMap());
                vmFromConfiguration.setImages(getDiskImageListFromDiskMap(getParameters().getDiskMap()));
            }
            // Note: The VM's OVF does not preserve the username and password for the LUN's connection.
            // Therefore to achieve a simple VM registration the iSCSI storage server should not use
            // credentials, although if the user will use the mapping attribute, one can set the credentials through
            // there.
            drMappingHelper.mapExternalLunDisks(DisksFilter.filterLunDisks(vmFromConfiguration.getDiskMap().values()), getParameters().getExternalLunMap());
            mapEntities(fullEntityOvfData);
        } catch (OvfReaderException e) {
            log.error("Failed to parse a given ovf configuration: {}:\n{}", e.getMessage(), ovfEntityData.getOvfData());
            log.debug("Exception", e);
        }
    }
}
Also used : VM(org.ovirt.engine.core.common.businessentities.VM) OvfEntityData(org.ovirt.engine.core.common.businessentities.OvfEntityData) Cluster(org.ovirt.engine.core.common.businessentities.Cluster) FullEntityOvfData(org.ovirt.engine.core.common.businessentities.storage.FullEntityOvfData) OvfReaderException(org.ovirt.engine.core.utils.ovf.OvfReaderException)

Example 2 with FullEntityOvfData

use of org.ovirt.engine.core.common.businessentities.storage.FullEntityOvfData in project ovirt-engine by oVirt.

the class ImportVmTemplateFromConfigurationCommand method initVmTemplate.

private void initVmTemplate() {
    List<OvfEntityData> ovfEntityList = unregisteredOVFDataDao.getByEntityIdAndStorageDomain(getParameters().getContainerId(), getParameters().getStorageDomainId());
    if (!ovfEntityList.isEmpty()) {
        try {
            // We should get only one entity, since we fetched the entity with a specific Storage Domain
            ovfEntityData = ovfEntityList.get(0);
            FullEntityOvfData fullEntityOvfData = ovfHelper.readVmTemplateFromOvf(ovfEntityData.getOvfData());
            vmTemplateFromConfiguration = fullEntityOvfData.getVmTemplate();
            if (Guid.isNullOrEmpty(getParameters().getClusterId())) {
                mapCluster(fullEntityOvfData);
            }
            vmTemplateFromConfiguration.setClusterId(getParameters().getClusterId());
            setVmTemplate(vmTemplateFromConfiguration);
            setEffectiveCompatibilityVersion(CompatibilityVersionUtils.getEffective(getVmTemplate(), this::getCluster));
            vmHandler.updateMaxMemorySize(getVmTemplate(), getEffectiveCompatibilityVersion());
            getParameters().setVmTemplate(vmTemplateFromConfiguration);
            getParameters().setDestDomainId(ovfEntityData.getStorageDomainId());
            getParameters().setSourceDomainId(ovfEntityData.getStorageDomainId());
            getParameters().setUserToRoles(fullEntityOvfData.getUserToRoles());
            // For quota, update disks when required
            if (getParameters().getDiskTemplateMap() != null) {
                ArrayList imageList = new ArrayList<>(getParameters().getDiskTemplateMap().values());
                vmTemplateFromConfiguration.setDiskList(imageList);
                ensureDomainMap(imageList, getParameters().getDestDomainId());
            }
            if (getParameters().getDomainMap() != null) {
                getParameters().setDbUsers(drMappingHelper.mapDbUsers(fullEntityOvfData.getDbUsers(), getParameters().getDomainMap()));
            } else {
                getParameters().setDbUsers(fullEntityOvfData.getDbUsers());
            }
            if (getParameters().getRoleMap() != null) {
                getParameters().setUserToRoles(drMappingHelper.mapRoles(getParameters().getRoleMap(), getParameters().getUserToRoles()));
            } else {
                getParameters().setUserToRoles(fullEntityOvfData.getUserToRoles());
            }
        } catch (OvfReaderException e) {
            log.error("Failed to parse a given ovf configuration: {}:\n{}", e.getMessage(), ovfEntityData.getOvfData());
            log.debug("Exception", e);
        }
    }
    setClusterId(getParameters().getClusterId());
    if (getCluster() != null) {
        setStoragePoolId(getCluster().getStoragePoolId());
    }
}
Also used : ArrayList(java.util.ArrayList) OvfEntityData(org.ovirt.engine.core.common.businessentities.OvfEntityData) FullEntityOvfData(org.ovirt.engine.core.common.businessentities.storage.FullEntityOvfData) OvfReaderException(org.ovirt.engine.core.utils.ovf.OvfReaderException)

Example 3 with FullEntityOvfData

use of org.ovirt.engine.core.common.businessentities.storage.FullEntityOvfData in project ovirt-engine by oVirt.

the class OvfHelper method readVmFromOvf.

/**
 * parses a given ovf to a vm, initialize all the extra data related to it such as images, interfaces, cluster,
 * LUNS, etc..
 *
 * @return FullEntityOvfData that represents the given ovf data
 */
public FullEntityOvfData readVmFromOvf(String ovf) throws OvfReaderException {
    VM vm = new VM();
    FullEntityOvfData fullEntityOvfData = new FullEntityOvfData(vm);
    ovfManager.importVm(ovf, vm, fullEntityOvfData);
    // add images
    vm.setImages((ArrayList) fullEntityOvfData.getDiskImages());
    // add interfaces
    vm.setInterfaces(fullEntityOvfData.getInterfaces());
    // add disk map
    Map<Guid, List<DiskImage>> images = ImagesHandler.getImagesLeaf(fullEntityOvfData.getDiskImages());
    for (Map.Entry<Guid, List<DiskImage>> entry : images.entrySet()) {
        List<DiskImage> list = entry.getValue();
        vm.getDiskMap().put(entry.getKey(), list.get(list.size() - 1));
    }
    fullEntityOvfData.getLunDisks().forEach(lunDisk -> vm.getDiskMap().put(lunDisk.getId(), lunDisk));
    return fullEntityOvfData;
}
Also used : VM(org.ovirt.engine.core.common.businessentities.VM) ArrayList(java.util.ArrayList) List(java.util.List) Guid(org.ovirt.engine.core.compat.Guid) FullEntityOvfData(org.ovirt.engine.core.common.businessentities.storage.FullEntityOvfData) HashMap(java.util.HashMap) Map(java.util.Map) DiskImage(org.ovirt.engine.core.common.businessentities.storage.DiskImage)

Example 4 with FullEntityOvfData

use of org.ovirt.engine.core.common.businessentities.storage.FullEntityOvfData in project ovirt-engine by oVirt.

the class ProcessOvfUpdateForStoragePoolCommand method populateVmsMetadataForOvfUpdate.

/**
 * Create and returns map contains valid vms metadata
 */
protected Map<Guid, KeyValuePairCompat<String, List<Guid>>> populateVmsMetadataForOvfUpdate(List<Guid> idsToProcess) {
    Map<Guid, KeyValuePairCompat<String, List<Guid>>> vmsAndTemplateMetadata = new HashMap<>();
    List<VM> vms = vmDao.getVmsByIds(idsToProcess);
    for (VM vm : vms) {
        if (VMStatus.ImageLocked != vm.getStatus()) {
            updateVmDisksFromDb(vm);
            if (!verifyImagesStatus(vm.getDiskList())) {
                continue;
            }
            ArrayList<DiskImage> vmImages = ovfUpdateProcessHelper.getVmImagesFromDb(vm);
            if (!verifyImagesStatus(vmImages)) {
                continue;
            }
            vm.setSnapshots(snapshotDao.getAllWithConfiguration(vm.getId()));
            if (!verifySnapshotsStatus(vm.getSnapshots())) {
                continue;
            }
            ovfUpdateProcessHelper.loadVmData(vm);
            Long currentDbGeneration = vmStaticDao.getDbGeneration(vm.getId());
            if (currentDbGeneration == null) {
                log.warn("currentDbGeneration of VM (name: '{}', id: '{}') is null, probably because the VM was deleted during the run of OvfDataUpdater.", vm.getName(), vm.getId());
                continue;
            }
            if (vm.getStaticData().getDbGeneration() == currentDbGeneration) {
                List<LunDisk> lunDisks = DisksFilter.filterLunDisks(vm.getDiskMap().values());
                for (LunDisk lun : lunDisks) {
                    lun.getLun().setLunConnections(new ArrayList<>(storageServerConnectionDao.getAllForLun(lun.getLun().getId())));
                }
                List<AffinityGroup> affinityGroups = affinityGroupDao.getAllAffinityGroupsByVmId(vm.getId());
                List<Label> affinityLabels = labelDao.getAllByEntityIds(Collections.singletonList(vm.getId()));
                Set<DbUser> dbUsers = new HashSet<>(dbUserDao.getAllForVm(vm.getId()));
                FullEntityOvfData fullEntityOvfData = new FullEntityOvfData(vm);
                fullEntityOvfData.setDiskImages(vmImages);
                fullEntityOvfData.setLunDisks(lunDisks);
                fullEntityOvfData.setAffinityGroups(affinityGroups);
                fullEntityOvfData.setAffinityLabels(affinityLabels);
                fullEntityOvfData.setDbUsers(dbUsers);
                ovfHelper.populateUserToRoles(fullEntityOvfData, vm.getId());
                proccessedOvfConfigurationsInfo.add(ovfUpdateProcessHelper.buildMetadataDictionaryForVm(vm, vmsAndTemplateMetadata, fullEntityOvfData));
                proccessedIdsInfo.add(vm.getId());
                proccessedOvfGenerationsInfo.add(vm.getStaticData().getDbGeneration());
                proccessDisksDomains(vm.getDiskList());
            }
        }
    }
    return vmsAndTemplateMetadata;
}
Also used : KeyValuePairCompat(org.ovirt.engine.core.compat.KeyValuePairCompat) HashMap(java.util.HashMap) Label(org.ovirt.engine.core.common.businessentities.Label) Guid(org.ovirt.engine.core.compat.Guid) FullEntityOvfData(org.ovirt.engine.core.common.businessentities.storage.FullEntityOvfData) LunDisk(org.ovirt.engine.core.common.businessentities.storage.LunDisk) AffinityGroup(org.ovirt.engine.core.common.scheduling.AffinityGroup) VM(org.ovirt.engine.core.common.businessentities.VM) DiskImage(org.ovirt.engine.core.common.businessentities.storage.DiskImage) DbUser(org.ovirt.engine.core.common.businessentities.aaa.DbUser) HashSet(java.util.HashSet)

Example 5 with FullEntityOvfData

use of org.ovirt.engine.core.common.businessentities.storage.FullEntityOvfData in project ovirt-engine by oVirt.

the class GetUnregisteredVmTemplatesQueryTest method setUpQueryEntities.

private void setUpQueryEntities() throws OvfReaderException {
    // Set up the expected result
    VmTemplate VmTemplateReturnForOvf = new VmTemplate();
    VmTemplateReturnForOvf.setId(newVmTemplateGuid);
    VmTemplateReturnForOvf.setName("Name");
    String ovfData = new String("OVF data for the first Template");
    OvfEntityData ovfEntityData = new OvfEntityData(VmTemplateReturnForOvf.getId(), VmTemplateReturnForOvf.getName(), VmEntityType.TEMPLATE, null, null, storageDomainId, ovfData, null);
    List<OvfEntityData> expectedResult = new ArrayList<>();
    List<OvfEntityData> expectedResultQuery1 = new ArrayList<>();
    expectedResultQuery1.add(ovfEntityData);
    expectedResult.add(ovfEntityData);
    VmTemplate VmTemplateReturnForOvf2 = new VmTemplate();
    VmTemplateReturnForOvf2.setId(newVmTemplateGuid2);
    VmTemplateReturnForOvf2.setName("Name2");
    String ovfData2 = new String("OVF data for the second Template");
    OvfEntityData ovfEntityData2 = new OvfEntityData(VmTemplateReturnForOvf2.getId(), VmTemplateReturnForOvf2.getName(), VmEntityType.TEMPLATE, null, null, storageDomainId, ovfData2, null);
    expectedResult.add(ovfEntityData2);
    List<OvfEntityData> expectedResultQuery2 = new ArrayList<>();
    expectedResultQuery2.add(ovfEntityData);
    // Mock the Daos
    when(unregisteredOVFDataDaoMock.getAllForStorageDomainByEntityType(storageDomainId, entityType)).thenReturn(expectedResult);
    when(unregisteredOVFDataDaoMock.getByEntityIdAndStorageDomain(newVmTemplateGuid, storageDomainId)).thenReturn(expectedResultQuery1);
    when(unregisteredOVFDataDaoMock.getByEntityIdAndStorageDomain(newVmTemplateGuid2, storageDomainId)).thenReturn(expectedResultQuery2);
    // Mock OVF
    when(ovfHelperMock.readVmTemplateFromOvf(ovfData)).thenReturn(new FullEntityOvfData(VmTemplateReturnForOvf));
    when(ovfHelperMock.readVmTemplateFromOvf(ovfData2)).thenReturn(new FullEntityOvfData(VmTemplateReturnForOvf2));
}
Also used : VmTemplate(org.ovirt.engine.core.common.businessentities.VmTemplate) ArrayList(java.util.ArrayList) OvfEntityData(org.ovirt.engine.core.common.businessentities.OvfEntityData) FullEntityOvfData(org.ovirt.engine.core.common.businessentities.storage.FullEntityOvfData)

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