use of com.vmware.xenon.common.TaskState.TaskStage in project photon-model by vmware.
the class EndpointAllocationTaskService method validateTransitionAndUpdateState.
private boolean validateTransitionAndUpdateState(Operation patch, EndpointAllocationTaskState body, EndpointAllocationTaskState currentState) {
TaskStage currentStage = currentState.taskInfo.stage;
SubStage currentSubStage = currentState.taskSubStage;
boolean isUpdate = false;
if (body.endpointState != null) {
currentState.endpointState = body.endpointState;
isUpdate = true;
}
if (body.enumerationRequest != null) {
currentState.enumerationRequest = body.enumerationRequest;
isUpdate = true;
}
if (body.createdDocumentLinks != null) {
currentState.createdDocumentLinks = body.createdDocumentLinks;
isUpdate = true;
}
if (body.existingComputeState != null) {
currentState.existingComputeState = body.existingComputeState;
isUpdate = true;
}
if (body.existingComputeDescription != null) {
currentState.existingComputeDescription = body.existingComputeDescription;
isUpdate = true;
}
if (body.accountAlreadyExists != currentState.accountAlreadyExists) {
currentState.accountAlreadyExists = body.accountAlreadyExists;
isUpdate = true;
}
if (body.adapterReference != null) {
currentState.adapterReference = body.adapterReference;
isUpdate = true;
}
if (body.options != null && !body.options.isEmpty()) {
currentState.options = body.options;
isUpdate = true;
}
if (body.checkForEndpointUniqueness != null) {
currentState.checkForEndpointUniqueness = body.checkForEndpointUniqueness;
isUpdate = true;
}
if (body.taskInfo == null || body.taskInfo.stage == null) {
if (isUpdate) {
patch.complete();
return true;
}
patch.fail(new IllegalArgumentException("taskInfo and stage are required"));
return true;
}
if (currentState.taskInfo.stage.ordinal() > body.taskInfo.stage.ordinal()) {
patch.fail(new IllegalArgumentException("stage can not move backwards:" + body.taskInfo.stage));
return true;
}
if (body.taskSubStage != null) {
if (currentSubStage.ordinal() > body.taskSubStage.ordinal()) {
patch.fail(new IllegalArgumentException("subStage can not move backwards:" + body.taskSubStage));
return true;
}
}
if (body.taskInfo.failure != null) {
logWarning(() -> String.format("Referer %s is patching us to failure during subStage %s: %s", patch.getReferer(), currentState.taskSubStage, Utils.toJsonHtml(body.taskInfo.failure)));
currentState.taskInfo.failure = body.taskInfo.failure;
if (SubStage.INVOKE_ADAPTER.equals(currentState.taskSubStage)) {
currentState.taskSubStage = SubStage.ROLLBACK_CREATION;
} else {
currentState.taskInfo.stage = body.taskInfo.stage;
currentState.taskSubStage = SubStage.FAILED;
}
return false;
}
currentState.taskInfo.stage = body.taskInfo.stage;
currentState.taskSubStage = body.taskSubStage;
logFine(() -> String.format("Moving from %s(%s) to %s(%s)", currentSubStage, currentStage, body.taskSubStage, body.taskInfo.stage));
return false;
}
use of com.vmware.xenon.common.TaskState.TaskStage in project photon-model by vmware.
the class ResourceAllocationTaskService method validateTransitionAndUpdateState.
private boolean validateTransitionAndUpdateState(Operation patch, ResourceAllocationTaskState body, ResourceAllocationTaskState currentState) {
TaskStage currentStage = currentState.taskInfo.stage;
SubStage currentSubStage = currentState.taskSubStage;
boolean isUpdate = false;
if (body.parentLinks != null) {
currentState.parentLinks = body.parentLinks;
isUpdate = true;
}
if (body.taskInfo == null || body.taskInfo.stage == null) {
if (isUpdate) {
patch.complete();
return true;
}
patch.fail(new IllegalArgumentException("taskInfo and stage are required"));
return true;
}
if (currentStage.ordinal() > body.taskInfo.stage.ordinal()) {
patch.fail(new IllegalArgumentException("stage can not move backwards:" + body.taskInfo.stage));
return true;
}
if (body.taskInfo.failure != null) {
logWarning(() -> String.format("Referer %s is patching us to failure: %s", patch.getReferer(), Utils.toJsonHtml(body.taskInfo.failure)));
currentState.taskInfo.failure = body.taskInfo.failure;
currentState.taskInfo.stage = body.taskInfo.stage;
currentState.taskSubStage = SubStage.FAILED;
return false;
}
if (TaskState.isFinished(body.taskInfo)) {
currentState.taskInfo.stage = body.taskInfo.stage;
currentState.taskSubStage = SubStage.FINISHED;
return false;
}
if (currentSubStage.ordinal() > body.taskSubStage.ordinal()) {
patch.fail(new IllegalArgumentException("subStage can not move backwards:" + body.taskSubStage));
return true;
}
currentState.taskInfo.stage = body.taskInfo.stage;
currentState.taskSubStage = body.taskSubStage;
logFine(() -> String.format("Moving from %s(%s) to %s(%s)", currentSubStage, currentStage, body.taskSubStage, body.taskInfo.stage));
return false;
}
use of com.vmware.xenon.common.TaskState.TaskStage in project photon-model by vmware.
the class ResourceEnumerationTaskService method updateEndpointState.
private DeferredResult<Operation> updateEndpointState(ResourceEnumerationTaskState currentState) {
TaskStage taskStage = currentState.taskInfo.stage;
String stageName = taskStage.name();
String message = null;
String messageId = null;
if (currentState.taskInfo.failure != null) {
message = currentState.taskInfo.failure.message;
messageId = currentState.taskInfo.failure.messageId;
}
Map<Object, Object> cpToAdd = new HashMap<>();
cpToAdd.put(EP_CP_ENUMERATION_TASK_STATE, stageName);
Collection<String> cpToRemove = new LinkedList<>();
if (message != null && message.length() > 0) {
cpToAdd.put(EP_CP_ENUMERATION_TASK_MESSAGE, message);
} else {
cpToRemove.add(EP_CP_ENUMERATION_TASK_MESSAGE);
}
if (messageId != null && messageId.length() > 0) {
cpToAdd.put(EP_CP_ENUMERATION_TASK_MESSAGE_ID, messageId);
} else {
cpToRemove.add(EP_CP_ENUMERATION_TASK_MESSAGE_ID);
}
Map<String, Map<Object, Object>> entriesToAdd = Collections.singletonMap(ResourceState.FIELD_NAME_CUSTOM_PROPERTIES, cpToAdd);
Map<String, Collection<Object>> keysToRemove = Collections.singletonMap(EndpointState.FIELD_NAME_CUSTOM_PROPERTIES, new HashSet<>(cpToRemove));
ServiceStateMapUpdateRequest mapUpdateRequest = ServiceStateMapUpdateRequest.create(entriesToAdd, keysToRemove);
return sendWithDeferredResult(Operation.createPatch(this, currentState.endpointLink).setBody(mapUpdateRequest)).exceptionally(err -> {
logWarning("Cannot patch custom properties of" + " endpoint '%s' to stageName '%s'. Cause: %s", currentState.endpointLink, stageName, Utils.toString(err));
return null;
});
}
Aggregations