Search in sources :

Example 1 with VmDeviceUpdate

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

the class VmHandler method addDeviceUpdateOnNextRun.

private boolean addDeviceUpdateOnNextRun(Guid vmId, VmDeviceGeneralType generalType, VmDeviceType type, boolean readOnly, String name, Object key, Object value, List<VmDeviceUpdate> updates) {
    if (key != null) {
        VmDeviceGeneralType keyGeneralType = VmDeviceGeneralType.UNKNOWN;
        VmDeviceType keyType = VmDeviceType.UNKNOWN;
        if (key instanceof VmDeviceGeneralType) {
            keyGeneralType = (VmDeviceGeneralType) key;
        } else if (key instanceof VmDeviceType) {
            keyType = (VmDeviceType) key;
        } else if (key instanceof GraphicsType) {
            keyType = ((GraphicsType) key).getCorrespondingDeviceType();
        } else {
            log.warn("addDeviceUpdateOnNextRun: Unsupported map key type: " + key.getClass().getName());
            return false;
        }
        if (keyGeneralType != VmDeviceGeneralType.UNKNOWN) {
            generalType = keyGeneralType;
        }
        if (keyType != VmDeviceType.UNKNOWN) {
            type = keyType;
        }
    }
    // if device type is set to unknown, search by general type only
    // because some devices have more than one type, like sound can be ac97/ich6
    String typeName = type != VmDeviceType.UNKNOWN ? type.getName() : null;
    if (value == null) {
        if (vmDeviceUtils.vmDeviceChanged(vmId, generalType, typeName, false)) {
            updates.add(new VmDeviceUpdate(generalType, type, readOnly, name, false));
        }
    } else if (value instanceof Boolean) {
        if (vmDeviceUtils.vmDeviceChanged(vmId, generalType, typeName, (Boolean) value)) {
            updates.add(new VmDeviceUpdate(generalType, type, readOnly, name, (Boolean) value));
        }
    } else if (value instanceof VmDevice) {
        if (vmDeviceUtils.vmDeviceChanged(vmId, generalType, typeName, (VmDevice) value)) {
            updates.add(new VmDeviceUpdate(generalType, type, readOnly, name, (VmDevice) value));
        }
    } else if (value instanceof VmWatchdog) {
        if (vmDeviceUtils.vmDeviceChanged(vmId, generalType, typeName, ((VmWatchdog) value).getVmDevice())) {
            updates.add(new VmDeviceUpdate(generalType, type, readOnly, name, ((VmWatchdog) value).getVmDevice()));
        }
    } else {
        log.warn("addDeviceUpdateOnNextRun: Unsupported value type: " + value.getClass().getName());
        return false;
    }
    return true;
}
Also used : GraphicsType(org.ovirt.engine.core.common.businessentities.GraphicsType) VmDevice(org.ovirt.engine.core.common.businessentities.VmDevice) VmDeviceUpdate(org.ovirt.engine.core.common.utils.VmDeviceUpdate) VmDeviceType(org.ovirt.engine.core.common.utils.VmDeviceType) VmDeviceGeneralType(org.ovirt.engine.core.common.businessentities.VmDeviceGeneralType) VmWatchdog(org.ovirt.engine.core.common.businessentities.VmWatchdog)

Example 2 with VmDeviceUpdate

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

the class VmHandler method getVmDevicesFieldsToUpdateOnNextRun.

public List<VmDeviceUpdate> getVmDevicesFieldsToUpdateOnNextRun(Guid vmId, VMStatus vmStatus, Object objectWithEditableDeviceFields) {
    List<VmDeviceUpdate> fieldList = new ArrayList<>();
    if (objectWithEditableDeviceFields == null) {
        return fieldList;
    }
    List<Pair<EditableDeviceOnVmStatusField, Field>> pairList = BaseHandler.extractAnnotatedFields(EditableDeviceOnVmStatusField.class, objectWithEditableDeviceFields.getClass());
    for (Pair<EditableDeviceOnVmStatusField, Field> pair : pairList) {
        EditableDeviceOnVmStatusField annotation = pair.getFirst();
        Field field = pair.getSecond();
        field.setAccessible(true);
        if (isUpdateValidForVmDevice(field.getName(), vmStatus)) {
            // field may be updated on the current run, so not including for the next run
            continue;
        }
        try {
            Object value = field.get(objectWithEditableDeviceFields);
            if (value == null) {
            // preserve current configuration
            } else if (value instanceof Boolean) {
                addDeviceUpdateOnNextRun(vmId, annotation, null, value, fieldList);
            } else if (value instanceof VmManagementParametersBase.Optional) {
                VmManagementParametersBase.Optional<?> optional = (VmManagementParametersBase.Optional<?>) value;
                if (optional.isUpdate()) {
                    addDeviceUpdateOnNextRun(vmId, annotation, null, optional.getValue(), fieldList);
                }
            } else if (value instanceof Map) {
                Map<?, ?> map = (Map<?, ?>) value;
                for (Map.Entry<?, ?> entry : map.entrySet()) {
                    boolean success = addDeviceUpdateOnNextRun(vmId, annotation, entry.getKey(), entry.getValue(), fieldList);
                    if (!success) {
                        break;
                    }
                }
            } else {
                log.warn("getVmDevicesFieldsToUpdateOnNextRun: Unsupported field type: " + value.getClass().getName());
            }
        } catch (IllegalAccessException | ClassCastException e) {
            log.warn("getVmDevicesFieldsToUpdateOnNextRun: Reflection error");
            log.debug("Original exception was:", e);
        }
    }
    return fieldList;
}
Also used : VmDeviceUpdate(org.ovirt.engine.core.common.utils.VmDeviceUpdate) ArrayList(java.util.ArrayList) EditableDeviceOnVmStatusField(org.ovirt.engine.core.common.businessentities.EditableDeviceOnVmStatusField) Field(java.lang.reflect.Field) TransientField(org.ovirt.engine.core.common.businessentities.TransientField) EditableVmField(org.ovirt.engine.core.common.businessentities.EditableVmField) EditableDeviceOnVmStatusField(org.ovirt.engine.core.common.businessentities.EditableDeviceOnVmStatusField) VmManagementParametersBase(org.ovirt.engine.core.common.action.VmManagementParametersBase) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) Pair(org.ovirt.engine.core.common.utils.Pair)

Example 3 with VmDeviceUpdate

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

the class GetVmChangedFieldsForNextRunQuery method executeQueryCommand.

@Override
protected void executeQueryCommand() {
    VM srcVm = getParameters().getOriginal();
    VM dstVm = getParameters().getUpdated();
    VmStatic srcStatic = srcVm.getStaticData();
    VmStatic dstStatic = dstVm.getStaticData();
    // Copy creationDate to ignore it, because it is never changed by user.
    // Without this creationDate will always show change in milliseconds,
    // because creationDate is saved without milliseconds in OVF, but
    // with milliseconds in the DB.
    dstStatic.setCreationDate(srcStatic.getCreationDate());
    // so it is not needed to include them into changed fields list.
    if (VmCommonUtils.isCpusToBeHotpluggedOrUnplugged(srcVm, dstVm)) {
        dstStatic.setNumOfSockets(srcStatic.getNumOfSockets());
    }
    if (VmCommonUtils.isMemoryToBeHotplugged(srcVm, dstVm)) {
        dstStatic.setMemSizeMb(srcStatic.getMemSizeMb());
        dstStatic.setMinAllocatedMem(srcStatic.getMinAllocatedMem());
    }
    if (VmCommonUtils.isVmLeaseToBeHotPluggedOrUnplugged(srcVm, dstVm)) {
        dstStatic.setLeaseStorageDomainId(srcStatic.getLeaseStorageDomainId());
    }
    VmPropertiesUtils vmPropertiesUtils = SimpleDependencyInjector.getInstance().get(VmPropertiesUtils.class);
    vmPropertiesUtils.separateCustomPropertiesToUserAndPredefined(srcVm.getCompatibilityVersion(), srcStatic);
    vmPropertiesUtils.separateCustomPropertiesToUserAndPredefined(dstVm.getCompatibilityVersion(), dstStatic);
    Set<String> result = new HashSet<>(vmHandler.getChangedFieldsForStatus(srcStatic, dstStatic, VMStatus.Up));
    for (VmDeviceUpdate device : vmHandler.getVmDevicesFieldsToUpdateOnNextRun(srcVm.getId(), VMStatus.Up, getParameters().getUpdateVmParameters())) {
        if (!device.getName().isEmpty()) {
            result.add(device.getName());
        } else {
            switch(device.getType()) {
                case UNKNOWN:
                case VIRTIO:
                    result.add(device.getGeneralType().name());
                    break;
                default:
                    result.add(device.getType().getName());
                    break;
            }
        }
    }
    setReturnValue(new ArrayList<>(result));
}
Also used : VmDeviceUpdate(org.ovirt.engine.core.common.utils.VmDeviceUpdate) VM(org.ovirt.engine.core.common.businessentities.VM) VmStatic(org.ovirt.engine.core.common.businessentities.VmStatic) VmPropertiesUtils(org.ovirt.engine.core.common.utils.customprop.VmPropertiesUtils) HashSet(java.util.HashSet)

Example 4 with VmDeviceUpdate

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

the class VmDeviceUtils method getVmDevicesForNextRun.

/**
 * Returns a map (device ID to VmDevice) of devices that are relevant for next run.
 *
 * The list of devices is built by examining properties that are annotated with EditableDeviceOnVmStatusField
 * annotation.
 *
 * @param vm the relevant VM
 * @param objectWithEditableDeviceFields object that contains properties which are annotated with
 *                                       EditableDeviceOnVmStatusField (e.g. parameters file)
 * @param vmVideoDeviceTypeforNextRun VM video device type for next run
 * @return a map of device ID to VmDevice
 */
public Map<Guid, VmDevice> getVmDevicesForNextRun(VM vm, Object objectWithEditableDeviceFields, DisplayType vmVideoDeviceTypeforNextRun) {
    setVmDevices(vm.getStaticData());
    Map<Guid, VmDevice> vmManagedDeviceMap = vm.getManagedVmDeviceMap();
    List<VmDeviceUpdate> fieldList = vmHandler.getVmDevicesFieldsToUpdateOnNextRun(vm.getId(), vm.getStatus(), objectWithEditableDeviceFields);
    // Add the enabled devices and remove the disabled ones
    for (VmDeviceUpdate update : fieldList) {
        if (update.isEnable()) {
            VmDevice device;
            if (update.getDevice() == null) {
                device = new VmDevice(new VmDeviceId(Guid.newGuid(), vm.getId()), update.getGeneralType(), update.getType().getName(), "", Collections.emptyMap(), true, true, update.isReadOnly(), "", null, null, null);
            } else {
                device = update.getDevice();
                if (device.getVmId() == null) {
                    device.setVmId(vm.getId());
                }
                if (device.getDeviceId() == null) {
                    if (device.getType() == VmDeviceGeneralType.RNG) {
                        // only one RNG device is allowed per VM
                        VmDevice rng = VmDeviceCommonUtils.findVmDeviceByType(vmManagedDeviceMap, update.getType());
                        device.setDeviceId(rng != null ? rng.getDeviceId() : Guid.newGuid());
                    } else {
                        device.setDeviceId(Guid.newGuid());
                    }
                }
            }
            vmManagedDeviceMap.put(device.getDeviceId(), device);
        } else {
            VmDevice device;
            if (update.getType() != VmDeviceType.UNKNOWN) {
                device = VmDeviceCommonUtils.findVmDeviceByType(vmManagedDeviceMap, update.getType());
            } else {
                device = VmDeviceCommonUtils.findVmDeviceByGeneralType(vmManagedDeviceMap, update.getGeneralType());
            }
            if (device != null) {
                vmManagedDeviceMap.remove(device.getDeviceId());
            }
        }
    }
    // as all other devices will be done
    if (vmVideoDeviceTypeforNextRun == DisplayType.none) {
        vmManagedDeviceMap = vmManagedDeviceMap.entrySet().stream().filter(entry -> !entry.getValue().getType().equals(VmDeviceGeneralType.VIDEO)).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    } else if (vm.getDefaultDisplayType() == DisplayType.none) {
        VmDevice videoDevice = new VmDevice(new VmDeviceId(Guid.newGuid(), vm.getId()), VmDeviceGeneralType.VIDEO, vmVideoDeviceTypeforNextRun.toString(), "", Collections.emptyMap(), true, true, false, "", null, null, null);
        vmManagedDeviceMap.put(videoDevice.getDeviceId(), videoDevice);
    }
    return vmManagedDeviceMap;
}
Also used : VmDevice(org.ovirt.engine.core.common.businessentities.VmDevice) VmDeviceUpdate(org.ovirt.engine.core.common.utils.VmDeviceUpdate) Guid(org.ovirt.engine.core.compat.Guid) VmDeviceId(org.ovirt.engine.core.common.businessentities.VmDeviceId)

Aggregations

VmDeviceUpdate (org.ovirt.engine.core.common.utils.VmDeviceUpdate)4 VmDevice (org.ovirt.engine.core.common.businessentities.VmDevice)2 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 VmManagementParametersBase (org.ovirt.engine.core.common.action.VmManagementParametersBase)1 EditableDeviceOnVmStatusField (org.ovirt.engine.core.common.businessentities.EditableDeviceOnVmStatusField)1 EditableVmField (org.ovirt.engine.core.common.businessentities.EditableVmField)1 GraphicsType (org.ovirt.engine.core.common.businessentities.GraphicsType)1 TransientField (org.ovirt.engine.core.common.businessentities.TransientField)1 VM (org.ovirt.engine.core.common.businessentities.VM)1 VmDeviceGeneralType (org.ovirt.engine.core.common.businessentities.VmDeviceGeneralType)1 VmDeviceId (org.ovirt.engine.core.common.businessentities.VmDeviceId)1 VmStatic (org.ovirt.engine.core.common.businessentities.VmStatic)1 VmWatchdog (org.ovirt.engine.core.common.businessentities.VmWatchdog)1 Pair (org.ovirt.engine.core.common.utils.Pair)1 VmDeviceType (org.ovirt.engine.core.common.utils.VmDeviceType)1 VmPropertiesUtils (org.ovirt.engine.core.common.utils.customprop.VmPropertiesUtils)1