Skip to content

Commit

Permalink
core: validate transfer phase
Browse files Browse the repository at this point in the history
Do not update the phase if the incoming phase is invalid. This only
affects external phase updates, when the user attempts to cancel or
finalize a transfer.
  • Loading branch information
bennyz committed Jul 28, 2022
1 parent 3e0b763 commit 7f9a793
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.ovirt.engine.core.bll.LockMessagesMatchUtil;
import org.ovirt.engine.core.common.businessentities.storage.ImageTransfer;
import org.ovirt.engine.core.common.businessentities.storage.ImageTransferPhase;
import org.ovirt.engine.core.common.errors.EngineMessage;
import org.ovirt.engine.core.common.locks.LockingGroup;
import org.ovirt.engine.core.compat.Guid;
Expand Down Expand Up @@ -50,12 +51,18 @@ public ImageTransfer updateEntity(ImageTransfer updates, Guid commandId, boolean
entity.setId(updates.getId());
}
if (updates.getPhase() != null) {
// TODO: Validate that phase change is valid. For now just log.
log.info("Updating image transfer '{}' phase from '{}' to '{}'",
commandId,
entity.getPhase(),
updates.getPhase());
entity.setPhase(updates.getPhase());
if (!ImageTransferPhase.isValidTransition(entity.getPhase(), updates.getPhase())) {
log.error("Image transfer '{}' transition from phase '{}' to '{}' is invalid",
commandId,
entity.getPhase(),
updates.getPhase());
} else {
log.info("Updating image transfer '{}' phase from '{}' to '{}'",
commandId,
entity.getPhase(),
updates.getPhase());
entity.setPhase(updates.getPhase());
}
}
if (updates.getType() != null) {
entity.setType(updates.getType());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.ovirt.engine.core.common.businessentities.storage;

import java.util.EnumMap;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -25,18 +27,35 @@ public enum ImageTransferPhase implements Identifiable {
private int value;
private String description;
private static final Map<Integer, ImageTransferPhase> valueToPhase = new HashMap<>();
private static final EnumMap<ImageTransferPhase, EnumSet<ImageTransferPhase>> validTransitions = new EnumMap<>(ImageTransferPhase.class);

static {
for (ImageTransferPhase phase : values()) {
valueToPhase.put(phase.getValue(), phase);
}

validTransitions.put(INITIALIZING, EnumSet.of(TRANSFERRING, PAUSED_SYSTEM, CANCELLED_SYSTEM));
validTransitions.put(TRANSFERRING, EnumSet.of(FINALIZING_SUCCESS, PAUSED_SYSTEM, CANCELLED_SYSTEM, CANCELLED_USER, PAUSED_USER));
validTransitions.put(RESUMING, EnumSet.of(TRANSFERRING, PAUSED_SYSTEM, CANCELLED_SYSTEM));
validTransitions.put(PAUSED_SYSTEM, EnumSet.of(RESUMING, CANCELLED_USER, CANCELLED_SYSTEM));
validTransitions.put(PAUSED_USER, EnumSet.of(RESUMING, CANCELLED_USER, CANCELLED_SYSTEM));
validTransitions.put(CANCELLED_USER, EnumSet.of(FINALIZING_CLEANUP));
validTransitions.put(CANCELLED_SYSTEM, EnumSet.of(FINALIZING_FAILURE));
validTransitions.put(FINALIZING_SUCCESS, EnumSet.of(FINALIZING_FAILURE, FINISHED_SUCCESS));
validTransitions.put(FINALIZING_FAILURE, EnumSet.of(FINISHED_FAILURE));
validTransitions.put(FINALIZING_CLEANUP, EnumSet.of(FINISHED_CLEANUP));
validTransitions.put(FINISHED_CLEANUP, EnumSet.of(FINISHED_FAILURE));
}

ImageTransferPhase(int value, String description) {
this.value = value;
this.description = description;
}

public static boolean isValidTransition(ImageTransferPhase from, ImageTransferPhase to) {
return validTransitions.get(from).contains(to);
}

@Override
public int getValue() {
return value;
Expand Down

0 comments on commit 7f9a793

Please sign in to comment.