Search in sources :

Example 46 with XmlNode

use of org.ovirt.engine.core.utils.ovf.xml.XmlNode in project ovirt-engine by oVirt.

the class VmDevicesConverter method parseControllers.

private List<Map<String, Object>> parseControllers(XmlDocument document, List<VmDevice> devices) {
    List<VmDevice> dbDevices = filterDevices(devices, VmDeviceGeneralType.CONTROLLER);
    // devices with spec params to appear first
    dbDevices.sort((d1, d2) -> d1.getSpecParams().isEmpty() && !d2.getSpecParams().isEmpty() ? 1 : 0);
    List<Map<String, Object>> result = new ArrayList<>();
    for (XmlNode node : selectNodes(document, VmDeviceGeneralType.CONTROLLER)) {
        String address = parseAddress(node);
        String index = parseAttribute(node, INDEX);
        String model = parseAttribute(node, MODEL);
        Integer ioThreadId = parseIoThreadId(node);
        String devType = "virtio-scsi".equals(model) ? model : parseAttribute(node, TYPE);
        boolean devWithModelNone = model != null ? model.equals(UsbControllerModel.NONE.libvirtName) : false;
        // which is a special case of a device without address that is still marked as plugged
        if (address.isEmpty() && !devWithModelNone) {
            continue;
        }
        Map<String, Object> dev = new HashMap<>();
        dev.put(VdsProperties.Type, VmDeviceGeneralType.CONTROLLER.getValue());
        dev.put(VdsProperties.Device, devType);
        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))).filter(d -> {
            if (d.getSpecParams().isEmpty()) {
                return true;
            }
            if (ioThreadId != null && Objects.equals(d.getSpecParams().get(IO_THREAD_ID), ioThreadId)) {
                return true;
            }
            if (Objects.equals(d.getSpecParams().get(INDEX), index) && Objects.equals(d.getSpecParams().get(MODEL), model)) {
                return true;
            }
            return false;
        }).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<>();
            if (index != null) {
                specParams.put(INDEX, index);
            }
            if (model != null) {
                specParams.put(MODEL, model);
            }
            if (ioThreadId != null) {
                specParams.put(IO_THREAD_ID, ioThreadId);
            }
            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) 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)

Example 47 with XmlNode

use of org.ovirt.engine.core.utils.ovf.xml.XmlNode in project ovirt-engine by oVirt.

the class VmDevicesConverter method parseDiskPath.

private String parseDiskPath(XmlNode node) {
    XmlNode sourceNode = node.selectSingleNode("source");
    if (sourceNode == null) {
        return "";
    }
    XmlAttribute attr = sourceNode.attributes.get("file");
    if (attr != null) {
        return attr.getValue();
    }
    attr = sourceNode.attributes.get("dev");
    if (attr != null) {
        return attr.getValue();
    }
    attr = sourceNode.attributes.get("name");
    if (attr != null) {
        return attr.getValue();
    }
    return "";
}
Also used : XmlNode(org.ovirt.engine.core.utils.ovf.xml.XmlNode) XmlAttribute(org.ovirt.engine.core.utils.ovf.xml.XmlAttribute)

Example 48 with XmlNode

use of org.ovirt.engine.core.utils.ovf.xml.XmlNode in project ovirt-engine by oVirt.

the class VmDevicesConverter method parseRedirs.

private List<Map<String, Object>> parseRedirs(XmlDocument document, List<VmDevice> devices) {
    List<VmDevice> dbDevices = filterDevices(devices, VmDeviceGeneralType.REDIR);
    List<Map<String, Object>> result = new ArrayList<>();
    for (XmlNode node : document.selectNodes("//*/redirdev")) {
        Map<String, Object> dev = new HashMap<>();
        dev.put(VdsProperties.Type, VmDeviceGeneralType.REDIR.getValue());
        dev.put(VdsProperties.Device, parseAttribute(node, TYPE));
        dev.put(VdsProperties.Address, parseAddress(node));
        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 49 with XmlNode

use of org.ovirt.engine.core.utils.ovf.xml.XmlNode 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)

Example 50 with XmlNode

use of org.ovirt.engine.core.utils.ovf.xml.XmlNode in project ovirt-engine by oVirt.

the class VmDevicesConverter method parseHostAddress.

private Map<String, String> parseHostAddress(XmlNode node) {
    XmlNode sourceNode = node.selectSingleNode("source");
    if (sourceNode == null) {
        return null;
    }
    XmlNode addressNode = sourceNode.selectSingleNode("address");
    if (addressNode == null) {
        return null;
    }
    Map<String, String> address = new HashMap<>();
    Arrays.asList("domain", "slot", "bus", "function", "device", "host", "target", "lun").forEach(key -> {
        XmlAttribute attribute = addressNode.attributes.get(key);
        if (attribute != null) {
            String valStr = attribute.getValue();
            boolean hex = valStr.startsWith("0x");
            int val = Integer.parseInt(hex ? valStr.substring(2) : valStr, hex ? 16 : 10);
            address.put(key, String.valueOf(val));
        }
    });
    XmlAttribute uuidAttribute = addressNode.attributes.get("uuid");
    if (uuidAttribute != null) {
        address.put("uuid", uuidAttribute.getValue());
    }
    return address;
}
Also used : XmlNode(org.ovirt.engine.core.utils.ovf.xml.XmlNode) XmlAttribute(org.ovirt.engine.core.utils.ovf.xml.XmlAttribute) HashMap(java.util.HashMap)

Aggregations

XmlNode (org.ovirt.engine.core.utils.ovf.xml.XmlNode)50 XmlNodeList (org.ovirt.engine.core.utils.ovf.xml.XmlNodeList)23 Guid (org.ovirt.engine.core.compat.Guid)19 HashMap (java.util.HashMap)16 VmDevice (org.ovirt.engine.core.common.businessentities.VmDevice)14 ArrayList (java.util.ArrayList)13 XmlAttribute (org.ovirt.engine.core.utils.ovf.xml.XmlAttribute)12 DiskImage (org.ovirt.engine.core.common.businessentities.storage.DiskImage)10 XmlDocument (org.ovirt.engine.core.utils.ovf.xml.XmlDocument)10 Map (java.util.Map)9 VmNetworkInterface (org.ovirt.engine.core.common.businessentities.network.VmNetworkInterface)9 DiskLunMap (org.ovirt.engine.core.common.businessentities.storage.DiskLunMap)9 MemoizingSupplier (org.ovirt.engine.core.utils.MemoizingSupplier)9 XmlNamespaceManager (org.ovirt.engine.core.utils.ovf.xml.XmlNamespaceManager)9 Arrays (java.util.Arrays)8 Collections (java.util.Collections)8 Comparator (java.util.Comparator)8 List (java.util.List)8 Objects (java.util.Objects)8 Optional (java.util.Optional)8