use of org.ovirt.engine.core.utils.ovf.xml.XmlDocument 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;
}
use of org.ovirt.engine.core.utils.ovf.xml.XmlDocument 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;
}
use of org.ovirt.engine.core.utils.ovf.xml.XmlDocument 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;
}
use of org.ovirt.engine.core.utils.ovf.xml.XmlDocument 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;
}
use of org.ovirt.engine.core.utils.ovf.xml.XmlDocument in project ovirt-engine by oVirt.
the class OvfUtilsTest method testFetchVmDisks.
@Test
public void testFetchVmDisks() throws Exception {
XmlDocument xmlDocument = new XmlDocument(getXmlOvfData());
Set<Guid> disks = ovfUtils.fetchVmDisks(xmlDocument);
assertNotNull("The list of disks should not be null", disks);
assertTrue("The list of disks should not be empty", !disks.isEmpty());
}
Aggregations