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;
}
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;
}
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));
}
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;
}
Aggregations