use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class ResourceIPDeallocationTaskService method startDeallocatingIPs.
private void startDeallocatingIPs(ResourceIPDeallocationTaskState state, ResourceIPDeallocationContext ctx, String subTaskLink) {
if (subTaskLink == null) {
createSubTaskCallback(state, ctx, link -> startDeallocatingIPs(state, ctx, link));
return;
}
logFine("Starting deallocate of (%s) IPs for compute resource [%s] using sub task %s", ctx.networkInterfaceToIPAddressMap.size(), ctx.resourceLink, subTaskLink);
List<DeferredResult<Operation>> deallocateOperationDRs = new ArrayList<>();
ctx.networkInterfaceToIPAddressMap.keySet().forEach(networkInterfaceLink -> {
String ipAddressLink = ctx.networkInterfaceToIPAddressMap.get(networkInterfaceLink);
IPAddressAllocationTaskState deallocationTaskState = new IPAddressAllocationTaskState();
deallocationTaskState.serviceTaskCallback = ServiceTaskCallback.create(UriUtils.buildUri(getHost(), subTaskLink));
deallocationTaskState.serviceTaskCallback.onSuccessFinishTask();
// Similar to instance deletes - a failure for one network interface deallocate will fail the task
deallocationTaskState.serviceTaskCallback.onErrorFailTask();
deallocationTaskState.requestType = IPAddressAllocationTaskState.RequestType.DEALLOCATE;
deallocationTaskState.connectedResourceLink = networkInterfaceLink;
deallocationTaskState.ipAddressLinks = new ArrayList<>();
deallocationTaskState.ipAddressLinks.add(ipAddressLink);
deallocationTaskState.tenantLinks = state.tenantLinks;
Operation deallocateNisOperation = Operation.createPost(this, IPAddressAllocationTaskService.FACTORY_LINK).setBody(deallocationTaskState);
DeferredResult<Operation> deallocateOperationDR = sendWithDeferredResult(deallocateNisOperation);
deallocateOperationDRs.add(deallocateOperationDR);
});
DeferredResult.allOf(deallocateOperationDRs).exceptionally(t -> {
String msg = "Failure deallocating IP addresses for a computeResource: [%s]";
logWarning(msg, state.resourceLink, t.getMessage());
failTask(t, msg, state.resourceLink);
return null;
});
}
use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class IPAddressAllocationTaskService method allocateIpInRange.
private DeferredResult<IPAddressAllocationContext> allocateIpInRange(IPAddressState ipAddressState, String connectedResourceLink, IPAddressAllocationContext context) {
// Found a record for the specified IP
if (IPAddressStatus.AVAILABLE.equals(ipAddressState.ipAddressStatus)) {
IPAddressState patchedState = new IPAddressState();
patchedState.ipAddressStatus = IPAddressStatus.ALLOCATED;
patchedState.connectedResourceLink = connectedResourceLink;
return sendWithDeferredResult(Operation.createPatch(this, ipAddressState.documentSelfLink).setBody(patchedState)).thenAccept(oper -> {
logInfo("Allocated IP address %s within range %s to resource %s", ipAddressState.ipAddress, context.subnetRangeState.name, connectedResourceLink);
addIpToContext(context, connectedResourceLink, ipAddressState);
}).thenApply(oper -> context);
} else {
if (connectedResourceLink.equals(ipAddressState.connectedResourceLink)) {
logInfo("IP address '%s' is already allocated to the same resource [%s]", ipAddressState.ipAddress, connectedResourceLink);
addIpToContext(context, connectedResourceLink, ipAddressState);
return DeferredResult.completed(context);
} else {
String errMsg = String.format("IP address '%s' is already allocated to a different resource [%s]", ipAddressState.ipAddress, ipAddressState.connectedResourceLink);
logSevere(errMsg);
return DeferredResult.failed(new IllegalArgumentException(errMsg));
}
}
}
Aggregations