Search in sources :

Example 1 with MemoizingSupplier

use of org.ovirt.engine.core.utils.MemoizingSupplier 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 MemoizingSupplier

use of org.ovirt.engine.core.utils.MemoizingSupplier in project ovirt-engine by oVirt.

the class VmDevicesConverter method parseInterfaces.

private List<Map<String, Object>> parseInterfaces(XmlDocument document, List<VmDevice> devices, Guid vmId, MemoizingSupplier<Map<Map<String, String>, HostDevice>> addressToHostDeviceSupplier) {
    List<VmDevice> dbDevices = filterDevices(devices, VmDeviceGeneralType.INTERFACE);
    Map<Guid, VmDevice> devIdToDbDev = dbDevices.stream().collect(Collectors.toMap(device -> device.getDeviceId(), device -> device));
    List<VmNetworkInterface> dbInterfaces = vmNetworkInterfaceDao.getAllForVm(vmId);
    List<Map<String, Object>> result = new ArrayList<>();
    for (XmlNode node : selectNodes(document, VmDeviceGeneralType.INTERFACE)) {
        String type = parseAttribute(node, TYPE);
        Map<String, Object> dev = new HashMap<>();
        if (VmDeviceType.HOST_DEVICE.getName().equals(type)) {
            dev.put(VdsProperties.HostDev, getHostDeviceName(node, addressToHostDeviceSupplier));
        }
        dev.put(VdsProperties.Type, VmDeviceGeneralType.INTERFACE.getValue());
        dev.put(VdsProperties.Device, type);
        dev.put(VdsProperties.Address, parseAddress(node));
        dev.put(VdsProperties.Alias, parseAlias(node));
        String macAddress = parseMacAddress(node);
        // MAC address is a unique identifier of network interface devices
        VmDevice dbDev = correlate(dev, dbDevices, device -> {
            VmNetworkInterface dbInterface = dbInterfaces.stream().filter(iface -> iface.getMacAddress().equalsIgnoreCase(macAddress)).findFirst().orElse(null);
            return dbInterface != null ? devIdToDbDev.get(dbInterface.getId()) : null;
        });
        if (dbDev == null) {
            log.warn("unmanaged network interface with mac address '{}' is ignored", macAddress);
            continue;
        }
        dev.put(VdsProperties.DeviceId, dbDev.getDeviceId().toString());
        dev.put(VdsProperties.SpecParams, dbDev.getSpecParams());
        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) XmlNode(org.ovirt.engine.core.utils.ovf.xml.XmlNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Guid(org.ovirt.engine.core.compat.Guid) VmDevice(org.ovirt.engine.core.common.businessentities.VmDevice) VmNetworkInterface(org.ovirt.engine.core.common.businessentities.network.VmNetworkInterface) HashMap(java.util.HashMap) DiskLunMap(org.ovirt.engine.core.common.businessentities.storage.DiskLunMap) Map(java.util.Map)

Example 3 with MemoizingSupplier

use of org.ovirt.engine.core.utils.MemoizingSupplier in project ovirt-engine by oVirt.

the class VmDevicesConverter method parseManagedHostDevices.

/**
 * This method processes managed host devices (those that are set by the engine).
 * That means that the device should already exist in the database and can be correlated
 * with one of the devices of the host. Host devices that were designed to be added as
 * unmanaged devices, like mdev devices, are handled separately.
 */
private List<Map<String, Object>> parseManagedHostDevices(XmlDocument document, List<VmDevice> devices, Guid hostId, MemoizingSupplier<Map<Map<String, String>, HostDevice>> addressToHostDeviceSupplier) {
    List<VmDevice> dbDevices = filterDevices(devices, VmDeviceGeneralType.HOSTDEV);
    if (dbDevices.isEmpty()) {
        return Collections.emptyList();
    }
    List<Map<String, Object>> result = new ArrayList<>();
    for (XmlNode node : document.selectNodes("//*/hostdev")) {
        Map<String, String> hostAddress = parseHostAddress(node);
        if (hostAddress == null) {
            continue;
        }
        HostDevice hostDevice = addressToHostDeviceSupplier.get().get(hostAddress);
        if (hostDevice == null) {
            // unmanaged
            continue;
        }
        Map<String, Object> dev = new HashMap<>();
        dev.put(VdsProperties.Address, parseAddress(node));
        dev.put(VdsProperties.Type, VmDeviceGeneralType.HOSTDEV.getValue());
        dev.put(VdsProperties.Alias, parseAlias(node));
        dev.put(VdsProperties.Device, hostDevice.getDeviceName());
        VmDevice dbDev = correlate(dev, dbDevices, device -> dbDevices.stream().filter(d -> d.getDevice().equals(hostDevice.getDeviceName())).findFirst().orElse(null));
        if (dbDev == null) {
            log.warn("VM host device '{}' does not exist in the database, thus ignored", hostDevice.getDeviceName());
            continue;
        }
        dev.put(VdsProperties.DeviceId, dbDev.getDeviceId().toString());
        dev.put(VdsProperties.SpecParams, dbDev.getSpecParams());
        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) XmlNode(org.ovirt.engine.core.utils.ovf.xml.XmlNode) HostDevice(org.ovirt.engine.core.common.businessentities.HostDevice) 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)

Example 4 with MemoizingSupplier

use of org.ovirt.engine.core.utils.MemoizingSupplier in project ovirt-engine by oVirt.

the class VmDevicesConverter method parseUnmanagedHostDevices.

private List<Map<String, Object>> parseUnmanagedHostDevices(XmlDocument document, List<VmDevice> devices, Guid hostId, MemoizingSupplier<Map<Map<String, String>, HostDevice>> addressToHostDeviceSupplier) {
    List<VmDevice> dbDevices = filterDevices(devices, VmDeviceGeneralType.HOSTDEV);
    List<Map<String, Object>> result = new ArrayList<>();
    for (XmlNode node : document.selectNodes("//*/hostdev")) {
        Map<String, String> hostAddress = parseHostAddress(node);
        if (hostAddress == null) {
            continue;
        }
        if (addressToHostDeviceSupplier.get().containsKey(hostAddress)) {
            // managed
            continue;
        }
        Map<String, Object> dev = new HashMap<>();
        dev.put(VdsProperties.Type, VmDeviceGeneralType.HOSTDEV.getValue());
        dev.put(VdsProperties.Address, parseAddress(node));
        dev.put(VdsProperties.Alias, parseAlias(node));
        String deviceType = parseAttribute(node, TYPE);
        dev.put(VdsProperties.Device, deviceType);
        dev.put(VdsProperties.SpecParams, hostAddress);
        VmDevice dbDev = correlate(dev, dbDevices, device -> dbDevices.stream().filter(d -> d.getDevice().equals(deviceType) && Objects.equals(d.getSpecParams(), hostAddress)).findFirst().orElse(null));
        dev.put(VdsProperties.DeviceId, dbDev != null ? dbDev.getDeviceId().toString() : 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)

Aggregations

ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 VmDevice (org.ovirt.engine.core.common.businessentities.VmDevice)4 DiskLunMap (org.ovirt.engine.core.common.businessentities.storage.DiskLunMap)4 MemoizingSupplier (org.ovirt.engine.core.utils.MemoizingSupplier)4 XmlNode (org.ovirt.engine.core.utils.ovf.xml.XmlNode)4 Arrays (java.util.Arrays)3 Collections (java.util.Collections)3 Comparator (java.util.Comparator)3 List (java.util.List)3 Objects (java.util.Objects)3 Optional (java.util.Optional)3 Function (java.util.function.Function)3 Collectors (java.util.stream.Collectors)3 Inject (javax.inject.Inject)3 Singleton (javax.inject.Singleton)3 StringUtils (org.apache.commons.lang.StringUtils)3 HostDevice (org.ovirt.engine.core.common.businessentities.HostDevice)3 UsbControllerModel (org.ovirt.engine.core.common.businessentities.UsbControllerModel)3