Search in sources :

Example 1 with XmlNode

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

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

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

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

the class VmDevicesConverter method parseAddress.

private String parseAddress(XmlNode node) {
    String result = "";
    XmlNode addressNode = node.selectSingleNode("address");
    if (addressNode == null) {
        return "";
    }
    XmlAttribute val = addressNode.attributes.get("type");
    result += String.format("%s=%s", "type", val.getValue());
    val = addressNode.attributes.get("slot");
    if (val != null) {
        result += String.format(", %s=%s", "slot", val.getValue());
    }
    val = addressNode.attributes.get("bus");
    if (val != null) {
        result += String.format(", %s=%s", "bus", val.getValue());
    }
    val = addressNode.attributes.get("domain");
    if (val != null) {
        result += String.format(", %s=%s", "domain", val.getValue());
    }
    val = addressNode.attributes.get("function");
    if (val != null) {
        result += String.format(", %s=%s", "function", val.getValue());
    }
    val = addressNode.attributes.get("controller");
    if (val != null) {
        result += String.format(", %s=%s", "controller", val.getValue());
    }
    val = addressNode.attributes.get("target");
    if (val != null) {
        result += String.format(", %s=%s", "target", val.getValue());
    }
    val = addressNode.attributes.get("unit");
    if (val != null) {
        result += String.format(", %s=%s", "unit", val.getValue());
    }
    val = addressNode.attributes.get("port");
    if (val != null) {
        result += String.format(", %s=%s", "port", val.getValue());
    }
    val = addressNode.attributes.get("multifunction");
    if (val != null) {
        result += String.format(", %s=%s", "multifunction", val.getValue());
    }
    val = addressNode.attributes.get("base");
    if (val != null) {
        result += String.format(", %s=%s", "base", val.getValue());
    }
    return result.isEmpty() ? result : String.format("{%s}", result);
}
Also used : XmlNode(org.ovirt.engine.core.utils.ovf.xml.XmlNode) XmlAttribute(org.ovirt.engine.core.utils.ovf.xml.XmlAttribute)

Example 5 with XmlNode

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

the class VmDevicesConverter method parseDiskMapping.

@SuppressWarnings("unchecked")
private Map<String, Object> parseDiskMapping(XmlNode metadata) throws Exception {
    if (metadata == null) {
        return null;
    }
    XmlNamespaceManager xmlNS = new XmlNamespaceManager();
    xmlNS.addNamespace(LibvirtVmXmlBuilder.OVIRT_VM_PREFIX, LibvirtVmXmlBuilder.OVIRT_VM_URI);
    XmlNode vm = metadata.selectSingleNode("ovirt-vm:vm", xmlNS);
    if (vm == null) {
        return null;
    }
    Map<String, Object> result = new HashMap<>();
    for (XmlNode node : vm.selectNodes("ovirt-vm:device", xmlNS)) {
        if (!VmDeviceGeneralType.DISK.getValue().equals(parseAttribute(node, "devtype"))) {
            continue;
        }
        XmlNode guestNameNode = node.selectSingleNode("ovirt-vm:guestName", xmlNS);
        if (guestNameNode != null) {
            // guest disk mapping is not available for LUNs at the moment
            result.put(node.selectSingleNode("ovirt-vm:imageID", xmlNS).innerText, Collections.singletonMap(VdsProperties.Name, guestNameNode.innerText));
        }
    }
    return result;
}
Also used : XmlNode(org.ovirt.engine.core.utils.ovf.xml.XmlNode) XmlNamespaceManager(org.ovirt.engine.core.utils.ovf.xml.XmlNamespaceManager) 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