Search in sources :

Example 1 with VmHostDevice

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

the class AddVmHostDevicesCommand method executeCommand.

@Override
protected void executeCommand() {
    Set<HostDevice> affectedHostDevices = getAffectedHostDevices();
    Map<String, VmHostDevice> existingDevices = getExistingVmHostDevicesByName();
    List<VmDevice> devicesToAdd = new ArrayList<>();
    List<VmDevice> devicesToUpdate = new ArrayList<>();
    for (HostDevice hostDevice : affectedHostDevices) {
        if (!existingDevices.containsKey(hostDevice.getDeviceName())) {
            VmHostDevice device = new VmHostDevice(getVmId(), hostDevice);
            // if the device was not explicitly intended by the user (only added due to the IOMMU group
            // we mark it as as placeholder
            boolean required = getPrimaryDeviceNames().contains(device.getDevice());
            device.setIommuPlaceholder(!required);
            devicesToAdd.add(device);
        } else {
            VmHostDevice device = new VmHostDevice(existingDevices.get(hostDevice.getDeviceName()));
            // as it is now explicitly requested by the user
            if (getPrimaryDeviceNames().contains(device.getDevice()) && device.isIommuPlaceholder()) {
                device.setIommuPlaceholder(false);
                devicesToUpdate.add(device);
            }
        }
    }
    vmDeviceDao.saveAllInBatch(devicesToAdd);
    vmDeviceDao.updateAllInBatch(devicesToUpdate);
    setSucceeded(true);
}
Also used : VmDevice(org.ovirt.engine.core.common.businessentities.VmDevice) VmHostDevice(org.ovirt.engine.core.common.businessentities.VmHostDevice) HostDevice(org.ovirt.engine.core.common.businessentities.HostDevice) ArrayList(java.util.ArrayList) VmHostDevice(org.ovirt.engine.core.common.businessentities.VmHostDevice)

Example 2 with VmHostDevice

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

the class GetVmHostDevicesQuery method executeQueryCommand.

@Override
protected void executeQueryCommand() {
    Guid vmId = getParameters().getId();
    setReturnValue(vmDeviceDao.getVmDeviceByVmIdAndType(vmId, VmDeviceGeneralType.HOSTDEV).stream().map(VmHostDevice::new).collect(Collectors.toList()));
}
Also used : Guid(org.ovirt.engine.core.compat.Guid) VmHostDevice(org.ovirt.engine.core.common.businessentities.VmHostDevice)

Example 3 with VmHostDevice

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

the class RemoveVmHostDevicesCommand method executeCommand.

@Override
protected void executeCommand() {
    Set<HostDevice> affectedHostDevices = getAffectedHostDevices();
    Map<String, VmHostDevice> existingDevices = getExistingVmHostDevicesByName();
    Map<Integer, List<VmHostDevice>> existingDevicesByIommuGroup = new HashMap<>();
    for (HostDevice hostDevice : affectedHostDevices) {
        boolean shouldRemoveDevice = getPrimaryDeviceNames().contains(hostDevice.getDeviceName());
        boolean deviceExists = existingDevices.containsKey(hostDevice.getDeviceName());
        if (deviceExists) {
            VmHostDevice device = existingDevices.get(hostDevice.getDeviceName());
            existingDevicesByIommuGroup.computeIfAbsent(getIommuGroupKey(hostDevice.getIommuGroup()), k -> new ArrayList<>()).add(device);
            if (shouldRemoveDevice) {
                // first just set the flag that this device is not required
                device.setIommuPlaceholder(true);
            }
        }
    }
    List<VmDevice> devicesToRemove = new ArrayList<>();
    List<VmDevice> devicesToUpdate = new ArrayList<>();
    // no longer needed (placeholder) devices
    for (Map.Entry<Integer, List<VmHostDevice>> group : existingDevicesByIommuGroup.entrySet()) {
        List<VmHostDevice> devices = group.getValue();
        // devices without IOMMU group can be safely removed
        boolean noIommuDeviceGroup = group.getKey() == getIommuGroupKey(null);
        if (noIommuDeviceGroup || allPlaceholder(devices)) {
            // all devices in this group became unnecessary, so remove them
            devicesToRemove.addAll(devices);
        } else {
            // some devices in this group are still required so just update the placeholder flag
            devicesToUpdate.addAll(devices);
        }
    }
    vmDeviceDao.removeAllInBatch(devicesToRemove);
    vmDeviceDao.updateAllInBatch(devicesToUpdate);
    setSucceeded(true);
}
Also used : VmHostDevice(org.ovirt.engine.core.common.businessentities.VmHostDevice) Collection(java.util.Collection) EngineMessage(org.ovirt.engine.core.common.errors.EngineMessage) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) VmHostDevicesParameters(org.ovirt.engine.core.common.action.VmHostDevicesParameters) Inject(javax.inject.Inject) CommandContext(org.ovirt.engine.core.bll.context.CommandContext) HostDevice(org.ovirt.engine.core.common.businessentities.HostDevice) List(java.util.List) Map(java.util.Map) VmDevice(org.ovirt.engine.core.common.businessentities.VmDevice) AuditLogType(org.ovirt.engine.core.common.AuditLogType) VmDeviceDao(org.ovirt.engine.core.dao.VmDeviceDao) VmHostDevice(org.ovirt.engine.core.common.businessentities.VmHostDevice) HostDevice(org.ovirt.engine.core.common.businessentities.HostDevice) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) VmDevice(org.ovirt.engine.core.common.businessentities.VmDevice) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) VmHostDevice(org.ovirt.engine.core.common.businessentities.VmHostDevice)

Aggregations

VmHostDevice (org.ovirt.engine.core.common.businessentities.VmHostDevice)3 ArrayList (java.util.ArrayList)2 HostDevice (org.ovirt.engine.core.common.businessentities.HostDevice)2 VmDevice (org.ovirt.engine.core.common.businessentities.VmDevice)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Inject (javax.inject.Inject)1 CommandContext (org.ovirt.engine.core.bll.context.CommandContext)1 AuditLogType (org.ovirt.engine.core.common.AuditLogType)1 VmHostDevicesParameters (org.ovirt.engine.core.common.action.VmHostDevicesParameters)1 EngineMessage (org.ovirt.engine.core.common.errors.EngineMessage)1 Guid (org.ovirt.engine.core.compat.Guid)1 VmDeviceDao (org.ovirt.engine.core.dao.VmDeviceDao)1