Search in sources :

Example 1 with EditableDeviceOnVmStatusField

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

the class VmHandler method init.

/**
 * Initialize list containers, for identity and permission check. The initialization should be executed
 * before calling ObjectIdentityChecker.
 *
 * @see Backend#initHandlers
 */
@PostConstruct
public void init() {
    Class<?>[] inspectedClassNames = new Class<?>[] { VmBase.class, VM.class, VmStatic.class, VmDynamic.class, VmManagementParametersBase.class };
    updateVmsStatic = new ObjectIdentityChecker(VmHandler.class, Arrays.asList(inspectedClassNames));
    for (Pair<EditableDeviceOnVmStatusField, Field> pair : BaseHandler.extractAnnotatedFields(EditableDeviceOnVmStatusField.class, inspectedClassNames)) {
        updateVmsStatic.addField(Arrays.asList(pair.getFirst().statuses()), pair.getSecond().getName());
    }
    for (Pair<TransientField, Field> pair : BaseHandler.extractAnnotatedFields(TransientField.class, inspectedClassNames)) {
        updateVmsStatic.addTransientFields(pair.getSecond().getName());
    }
    for (Pair<EditableVmField, Field> pair : BaseHandler.extractAnnotatedFields(EditableVmField.class, inspectedClassNames)) {
        EditableVmField annotation = pair.getFirst();
        List<VMStatus> statusList = Arrays.asList(annotation.onStatuses());
        String fieldName = pair.getSecond().getName();
        if (statusList.isEmpty()) {
            updateVmsStatic.addPermittedFields(fieldName);
        } else {
            updateVmsStatic.addField(statusList, fieldName);
            if (!annotation.hotSettableOnStatus().getStates().isEmpty()) {
                updateVmsStatic.addHotsetField(fieldName, annotation.hotSettableOnStatus().getStates());
            }
        }
        if (annotation.onHostedEngine()) {
            updateVmsStatic.addHostedEngineFields(fieldName);
        }
    }
}
Also used : VmBase(org.ovirt.engine.core.common.businessentities.VmBase) 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) VmDynamic(org.ovirt.engine.core.common.businessentities.VmDynamic) TransientField(org.ovirt.engine.core.common.businessentities.TransientField) ObjectIdentityChecker(org.ovirt.engine.core.utils.ObjectIdentityChecker) VM(org.ovirt.engine.core.common.businessentities.VM) VmStatic(org.ovirt.engine.core.common.businessentities.VmStatic) VmManagementParametersBase(org.ovirt.engine.core.common.action.VmManagementParametersBase) EditableDeviceOnVmStatusField(org.ovirt.engine.core.common.businessentities.EditableDeviceOnVmStatusField) EditableVmField(org.ovirt.engine.core.common.businessentities.EditableVmField) VMStatus(org.ovirt.engine.core.common.businessentities.VMStatus) PostConstruct(javax.annotation.PostConstruct)

Example 2 with EditableDeviceOnVmStatusField

use of org.ovirt.engine.core.common.businessentities.EditableDeviceOnVmStatusField 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)

Aggregations

Field (java.lang.reflect.Field)2 VmManagementParametersBase (org.ovirt.engine.core.common.action.VmManagementParametersBase)2 EditableDeviceOnVmStatusField (org.ovirt.engine.core.common.businessentities.EditableDeviceOnVmStatusField)2 EditableVmField (org.ovirt.engine.core.common.businessentities.EditableVmField)2 TransientField (org.ovirt.engine.core.common.businessentities.TransientField)2 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 PostConstruct (javax.annotation.PostConstruct)1 VM (org.ovirt.engine.core.common.businessentities.VM)1 VMStatus (org.ovirt.engine.core.common.businessentities.VMStatus)1 VmBase (org.ovirt.engine.core.common.businessentities.VmBase)1 VmDynamic (org.ovirt.engine.core.common.businessentities.VmDynamic)1 VmStatic (org.ovirt.engine.core.common.businessentities.VmStatic)1 Pair (org.ovirt.engine.core.common.utils.Pair)1 VmDeviceUpdate (org.ovirt.engine.core.common.utils.VmDeviceUpdate)1 ObjectIdentityChecker (org.ovirt.engine.core.utils.ObjectIdentityChecker)1