Search in sources :

Example 1 with VmDevice

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

the class VmDevicesConverter method parseDisks.

private List<Map<String, Object>> parseDisks(XmlDocument document, List<VmDevice> devices) {
    List<VmDevice> dbDevices = filterDevices(devices, VmDeviceGeneralType.DISK);
    MemoizingSupplier<Map<Guid, String>> diskToLunSupplier = new MemoizingSupplier<>(() -> diskLunMapDao.getAll().stream().collect(Collectors.toMap(DiskLunMap::getDiskId, DiskLunMap::getLunId)));
    List<Map<String, Object>> result = new ArrayList<>();
    for (XmlNode node : selectNodes(document, VmDeviceGeneralType.DISK)) {
        Map<String, Object> dev = new HashMap<>();
        dev.put(VdsProperties.Type, VmDeviceGeneralType.DISK.getValue());
        String diskType = parseAttribute(node, DEVICE);
        dev.put(VdsProperties.Device, diskType);
        dev.put(VdsProperties.Address, parseAddress(node));
        dev.put(VdsProperties.Alias, parseAlias(node));
        String path = parseDiskPath(node);
        VmDevice dbDev = correlate(dev, dbDevices, device -> findDiskDeviceInDbByPath(dbDevices, diskType, path, diskToLunSupplier));
        if (dbDev == null) {
            log.warn("unmanaged disk with path '{}' is ignored", path);
            continue;
        }
        dbDevices.remove(dbDev);
        dev.put(VdsProperties.ImageId, parseImageIdFromPath(path));
        dev.put(VdsProperties.DeviceId, dbDev.getDeviceId().toString());
        dev.put(VdsProperties.SpecParams, dbDev.getSpecParams());
        List<Map<String, Object>> volumeChain = parseVolumeChain(node);
        if (!volumeChain.isEmpty()) {
            dev.put(VdsProperties.VolumeChain, volumeChain.toArray());
        }
        result.add(dev);
    }
    return result;
}
Also used : XmlNode(org.ovirt.engine.core.utils.ovf.xml.XmlNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) VmDevice(org.ovirt.engine.core.common.businessentities.VmDevice) HashMap(java.util.HashMap) DiskLunMap(org.ovirt.engine.core.common.businessentities.storage.DiskLunMap) Map(java.util.Map) DiskLunMap(org.ovirt.engine.core.common.businessentities.storage.DiskLunMap) MemoizingSupplier(org.ovirt.engine.core.utils.MemoizingSupplier)

Example 2 with VmDevice

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

the class VmDevicesConverter method parseMemories.

private List<Map<String, Object>> parseMemories(XmlDocument document, List<VmDevice> devices) {
    List<VmDevice> dbDevices = filterDevices(devices, VmDeviceGeneralType.MEMORY);
    List<Map<String, Object>> result = new ArrayList<>();
    for (XmlNode node : selectNodes(document, VmDeviceGeneralType.MEMORY)) {
        Map<String, Object> dev = new HashMap<>();
        dev.put(VdsProperties.Type, VmDeviceGeneralType.MEMORY.getValue());
        dev.put(VdsProperties.Device, VmDeviceGeneralType.MEMORY.getValue());
        dev.put(VdsProperties.Address, parseAddress(node));
        dev.put(VdsProperties.Alias, parseAlias(node));
        XmlNode target = node.selectSingleNode("target");
        if (target == null) {
            continue;
        }
        String devNode = target.selectSingleNode(NODE).innerText;
        String devSize = kiloBytesToMegaBytes(target.selectSingleNode(SIZE).innerText);
        VmDevice dbDev = correlate(dev, dbDevices, device -> dbDevices.stream().sorted(// try to match managed devices first
        Comparator.comparing(VmDevice::isManaged).reversed()).filter(d -> d.getDevice().equals(device.get(VdsProperties.Device))).filter(d -> Objects.equals(d.getSpecParams().get(SPEC_PARAM_NODE).toString(), devNode)).filter(d -> Objects.equals(d.getSpecParams().get(SPEC_PARAM_SIZE).toString(), devSize)).findFirst().orElse(null));
        if (dbDev != null) {
            dbDevices.remove(dbDev);
            dev.put(VdsProperties.DeviceId, dbDev.getDeviceId().toString());
            dev.put(VdsProperties.SpecParams, dbDev.getSpecParams());
        } else {
            dev.put(VdsProperties.DeviceId, Guid.newGuid().toString());
            Map<String, Object> specParams = new HashMap<>();
            specParams.put(SPEC_PARAM_NODE, devNode);
            specParams.put(SPEC_PARAM_SIZE, devSize);
            dev.put(VdsProperties.SpecParams, specParams);
        }
        result.add(dev);
    }
    return result;
}
Also used : VmDeviceType(org.ovirt.engine.core.common.utils.VmDeviceType) Arrays(java.util.Arrays) StringUtils(org.apache.commons.lang.StringUtils) DiskLunMapDao(org.ovirt.engine.core.dao.DiskLunMapDao) Guid(org.ovirt.engine.core.compat.Guid) XmlNamespaceManager(org.ovirt.engine.core.utils.ovf.xml.XmlNamespaceManager) UsbControllerModel(org.ovirt.engine.core.common.businessentities.UsbControllerModel) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) XmlAttribute(org.ovirt.engine.core.utils.ovf.xml.XmlAttribute) LibvirtVmXmlBuilder(org.ovirt.engine.core.vdsbroker.builder.vminfo.LibvirtVmXmlBuilder) Singleton(javax.inject.Singleton) Function(java.util.function.Function) DiskImage(org.ovirt.engine.core.common.businessentities.storage.DiskImage) DiskLunMap(org.ovirt.engine.core.common.businessentities.storage.DiskLunMap) ArrayList(java.util.ArrayList) VmDeviceGeneralType(org.ovirt.engine.core.common.businessentities.VmDeviceGeneralType) Inject(javax.inject.Inject) DiskImageDao(org.ovirt.engine.core.dao.DiskImageDao) Map(java.util.Map) VmDevice(org.ovirt.engine.core.common.businessentities.VmDevice) VdsProperties(org.ovirt.engine.core.vdsbroker.vdsbroker.VdsProperties) VmDeviceDao(org.ovirt.engine.core.dao.VmDeviceDao) VmNetworkInterfaceDao(org.ovirt.engine.core.dao.network.VmNetworkInterfaceDao) MemoizingSupplier(org.ovirt.engine.core.utils.MemoizingSupplier) Logger(org.slf4j.Logger) VmNetworkInterface(org.ovirt.engine.core.common.businessentities.network.VmNetworkInterface) XmlNode(org.ovirt.engine.core.utils.ovf.xml.XmlNode) XmlDocument(org.ovirt.engine.core.utils.ovf.xml.XmlDocument) HostDeviceDao(org.ovirt.engine.core.dao.HostDeviceDao) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) HostDevice(org.ovirt.engine.core.common.businessentities.HostDevice) List(java.util.List) SPEC_PARAM_NODE(org.ovirt.engine.core.common.utils.VmDeviceCommonUtils.SPEC_PARAM_NODE) SPEC_PARAM_SIZE(org.ovirt.engine.core.common.utils.VmDeviceCommonUtils.SPEC_PARAM_SIZE) Optional(java.util.Optional) Comparator(java.util.Comparator) Collections(java.util.Collections) XmlNodeList(org.ovirt.engine.core.utils.ovf.xml.XmlNodeList) VmDevice(org.ovirt.engine.core.common.businessentities.VmDevice) XmlNode(org.ovirt.engine.core.utils.ovf.xml.XmlNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) DiskLunMap(org.ovirt.engine.core.common.businessentities.storage.DiskLunMap) Map(java.util.Map)

Example 3 with VmDevice

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

the class VmDevicesConverter method parseChannels.

private List<Map<String, Object>> parseChannels(XmlDocument document, List<VmDevice> devices) {
    List<VmDevice> dbDevices = filterDevices(devices, VmDeviceGeneralType.CHANNEL);
    List<Map<String, Object>> result = new ArrayList<>();
    for (XmlNode node : selectNodes(document, VmDeviceGeneralType.CHANNEL)) {
        String address = parseAddress(node);
        // Ignore channel devices without address
        if (address.isEmpty()) {
            continue;
        }
        Map<String, Object> dev = new HashMap<>();
        dev.put(VdsProperties.Type, VmDeviceGeneralType.CHANNEL.getValue());
        // shouldn't it be VdsProperties.DeviceType?
        dev.put(VdsProperties.Device, parseAttribute(node, TYPE));
        dev.put(VdsProperties.Address, address);
        dev.put(VdsProperties.Alias, parseAlias(node));
        VmDevice dbDev = correlate(dev, dbDevices, device -> dbDevices.stream().filter(d -> d.getDevice().equals(device.get(VdsProperties.Device))).findFirst().orElse(null));
        if (dbDev != null) {
            dbDevices.remove(dbDev);
            dev.put(VdsProperties.DeviceId, dbDev.getDeviceId().toString());
            dev.put(VdsProperties.SpecParams, dbDev.getSpecParams());
        } else {
            dev.put(VdsProperties.DeviceId, Guid.newGuid().toString());
        }
        result.add(dev);
    }
    return result;
}
Also used : VmDeviceType(org.ovirt.engine.core.common.utils.VmDeviceType) Arrays(java.util.Arrays) StringUtils(org.apache.commons.lang.StringUtils) DiskLunMapDao(org.ovirt.engine.core.dao.DiskLunMapDao) Guid(org.ovirt.engine.core.compat.Guid) XmlNamespaceManager(org.ovirt.engine.core.utils.ovf.xml.XmlNamespaceManager) UsbControllerModel(org.ovirt.engine.core.common.businessentities.UsbControllerModel) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) XmlAttribute(org.ovirt.engine.core.utils.ovf.xml.XmlAttribute) LibvirtVmXmlBuilder(org.ovirt.engine.core.vdsbroker.builder.vminfo.LibvirtVmXmlBuilder) Singleton(javax.inject.Singleton) Function(java.util.function.Function) DiskImage(org.ovirt.engine.core.common.businessentities.storage.DiskImage) DiskLunMap(org.ovirt.engine.core.common.businessentities.storage.DiskLunMap) ArrayList(java.util.ArrayList) VmDeviceGeneralType(org.ovirt.engine.core.common.businessentities.VmDeviceGeneralType) Inject(javax.inject.Inject) DiskImageDao(org.ovirt.engine.core.dao.DiskImageDao) Map(java.util.Map) VmDevice(org.ovirt.engine.core.common.businessentities.VmDevice) VdsProperties(org.ovirt.engine.core.vdsbroker.vdsbroker.VdsProperties) VmDeviceDao(org.ovirt.engine.core.dao.VmDeviceDao) VmNetworkInterfaceDao(org.ovirt.engine.core.dao.network.VmNetworkInterfaceDao) MemoizingSupplier(org.ovirt.engine.core.utils.MemoizingSupplier) Logger(org.slf4j.Logger) VmNetworkInterface(org.ovirt.engine.core.common.businessentities.network.VmNetworkInterface) XmlNode(org.ovirt.engine.core.utils.ovf.xml.XmlNode) XmlDocument(org.ovirt.engine.core.utils.ovf.xml.XmlDocument) HostDeviceDao(org.ovirt.engine.core.dao.HostDeviceDao) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) HostDevice(org.ovirt.engine.core.common.businessentities.HostDevice) List(java.util.List) SPEC_PARAM_NODE(org.ovirt.engine.core.common.utils.VmDeviceCommonUtils.SPEC_PARAM_NODE) SPEC_PARAM_SIZE(org.ovirt.engine.core.common.utils.VmDeviceCommonUtils.SPEC_PARAM_SIZE) Optional(java.util.Optional) Comparator(java.util.Comparator) Collections(java.util.Collections) XmlNodeList(org.ovirt.engine.core.utils.ovf.xml.XmlNodeList) VmDevice(org.ovirt.engine.core.common.businessentities.VmDevice) XmlNode(org.ovirt.engine.core.utils.ovf.xml.XmlNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) DiskLunMap(org.ovirt.engine.core.common.businessentities.storage.DiskLunMap) Map(java.util.Map)

Example 4 with VmDevice

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

the class VmDevicesMonitoring method handleRemovedDevices.

/**
 * Handles devices that were removed by libvirt. Unmanaged devices are marked to be removed, managed devices are
 * unplugged - the address is cleared and isPlugged is set to false.
 *
 * @param libvirtDevices list of IDs of devices that were returned by libvirt
 * @param dbDevices list of all devices present in the DB
 */
private void handleRemovedDevices(Change change, Guid vmId, Set<Guid> libvirtDevices, List<VmDevice> dbDevices) {
    for (VmDevice device : dbDevices) {
        if (libvirtDevices.contains(device.getDeviceId())) {
            continue;
        }
        if (deviceWithoutAddress(device)) {
            continue;
        }
        if (device.isManaged()) {
            if (device.isPlugged()) {
                device.setPlugged(Boolean.FALSE);
                device.setAddress("");
                change.addDeviceToUpdate(device);
                log.debug("VM '{}' managed pluggable device was unplugged : '{}'", vmId, device);
            } else if (!devicePluggable(device)) {
                log.error("VM '{}' managed non pluggable device was removed unexpectedly from libvirt: '{}'", vmId, device);
            }
        } else {
            change.addDeviceIdToRemove(device.getId());
            log.debug("VM '{}' unmanaged device was marked for remove : {1}", vmId, device);
        }
    }
}
Also used : VmDevice(org.ovirt.engine.core.common.businessentities.VmDevice)

Example 5 with VmDevice

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

the class VmDevicesMonitoring method processVmDevices.

/**
 * Actually process the VM device update and store individual device additions/updates/removals
 * in the <code>change</code>.
 */
private void processVmDevices(Change change, Map<String, Object> vmInfo) {
    Guid vmId = getVmId(vmInfo);
    Set<Guid> processedDeviceIds = new HashSet<>();
    List<VmDevice> dbDevices = getVmDeviceDao().getVmDeviceByVmId(vmId);
    Map<VmDeviceId, VmDevice> dbDeviceMap = Entities.businessEntitiesById(dbDevices);
    for (Object o : (Object[]) vmInfo.get(VdsProperties.Devices)) {
        Map<String, Object> vdsmDevice = (Map<String, Object>) o;
        if (vdsmDevice.get(VdsProperties.Address) == null) {
            logDeviceInformation(vmId, vdsmDevice);
            continue;
        }
        Guid deviceId = getDeviceId(vdsmDevice);
        VmDevice dbDevice = dbDeviceMap.get(new VmDeviceId(deviceId, vmId));
        if (dbDevice == null) {
            dbDevice = getByDeviceType((String) vdsmDevice.get(VdsProperties.Device), dbDeviceMap);
            deviceId = dbDevice != null ? dbDevice.getDeviceId() : deviceId;
        }
        String logicalName = getDeviceLogicalName(vmInfo, vdsmDevice);
        if (deviceId == null || dbDevice == null) {
            VmDevice newDevice = buildNewVmDevice(vmId, vdsmDevice, logicalName);
            if (newDevice != null) {
                change.addDeviceToAdd(newDevice);
                processedDeviceIds.add(newDevice.getDeviceId());
            }
        } else {
            dbDevice.setPlugged(Boolean.TRUE);
            dbDevice.setAddress(vdsmDevice.get(VdsProperties.Address).toString());
            dbDevice.setAlias(StringUtils.defaultString((String) vdsmDevice.get(VdsProperties.Alias)));
            dbDevice.setLogicalName(logicalName);
            dbDevice.setHostDevice(StringUtils.defaultString((String) vdsmDevice.get(VdsProperties.HostDev)));
            change.addDeviceToUpdate(dbDevice);
            processedDeviceIds.add(deviceId);
        }
    }
    handleRemovedDevices(change, vmId, processedDeviceIds, dbDevices);
}
Also used : VmDevice(org.ovirt.engine.core.common.businessentities.VmDevice) Guid(org.ovirt.engine.core.compat.Guid) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) VmDeviceId(org.ovirt.engine.core.common.businessentities.VmDeviceId) HashSet(java.util.HashSet)

Aggregations

VmDevice (org.ovirt.engine.core.common.businessentities.VmDevice)170 HashMap (java.util.HashMap)59 Guid (org.ovirt.engine.core.compat.Guid)53 VmDeviceId (org.ovirt.engine.core.common.businessentities.VmDeviceId)48 ArrayList (java.util.ArrayList)34 Map (java.util.Map)33 VM (org.ovirt.engine.core.common.businessentities.VM)29 DiskImage (org.ovirt.engine.core.common.businessentities.storage.DiskImage)28 List (java.util.List)26 GraphicsType (org.ovirt.engine.core.common.businessentities.GraphicsType)21 VmDeviceGeneralType (org.ovirt.engine.core.common.businessentities.VmDeviceGeneralType)21 VmDeviceType (org.ovirt.engine.core.common.utils.VmDeviceType)20 Collections (java.util.Collections)19 Test (org.junit.Test)19 Collectors (java.util.stream.Collectors)18 Arrays (java.util.Arrays)17 Optional (java.util.Optional)17 StringUtils (org.apache.commons.lang.StringUtils)17 HostDevice (org.ovirt.engine.core.common.businessentities.HostDevice)17 Inject (javax.inject.Inject)16