use of com.vmware.xenon.common.OperationJoin in project photon-model by vmware.
the class AWSComputeDescriptionEnumerationAdapterService method createOrUpdateComputeDescriptions.
/**
* Kicks off the creation of all the identified compute descriptions and creates a join handler to handle the successful
* completion of of those operations. Once all the compute descriptions are successfully created moves the state machine
* to the next stage.
*/
private void createOrUpdateComputeDescriptions(AWSComputeDescriptionCreationServiceContext context, AWSComputeDescCreationStage next) {
if (context.enumerationOperations == null || context.enumerationOperations.size() == 0) {
logFine(() -> "No compute descriptions to be created");
context.creationStage = next;
handleComputeDescriptionCreation(context);
return;
}
OperationJoin.JoinedCompletionHandler joinCompletion = (ox, exc) -> {
if (exc != null) {
logSevere(() -> String.format("Failure creating compute descriptions: %s", Utils.toString(exc)));
finishWithFailure(context, exc.values().iterator().next());
return;
}
logFine(() -> "Successfully created compute descriptions");
context.creationStage = next;
handleComputeDescriptionCreation(context);
};
OperationJoin joinOp = OperationJoin.create(context.enumerationOperations);
joinOp.setCompletion(joinCompletion);
joinOp.sendWith(getHost());
}
use of com.vmware.xenon.common.OperationJoin in project photon-model by vmware.
the class AWSS3StorageEnumerationAdapterService method createDiskStates.
/**
* Creates the disk states that represent the buckets received from AWS during
* enumeration. Fields currently being enumerated for S3 are all immutable on AWS side, hence we only create
* disks and don't patch to them in subsequent except for changes in tagLinks.
*/
private void createDiskStates(S3StorageEnumerationContext aws, S3StorageEnumerationSubStage next) {
// For all the disks to be created, we filter them based on whether we were able to find the correct
// region for the disk using getBucketTaggingConfiguration() call and then map them and create operations.
// Filtering is done to avoid creating disk states with null region (since we don't PATCH region field
// after creating the disk, we need to ensure that disk state is initially created with the correct region).
// kick off the operation using a JOIN
List<DiskState> diskStatesToBeCreated = new ArrayList<>();
aws.bucketsToBeCreated.stream().filter(bucket -> aws.regionsByBucketName.containsKey(bucket.getName())).forEach(bucket -> {
diskStatesToBeCreated.add(mapBucketToDiskState(bucket, aws));
});
diskStatesToBeCreated.forEach(diskState -> aws.enumerationOperations.add(createPostOperation(this, diskState, DiskService.FACTORY_LINK)));
this.logFine(() -> String.format("Creating %d S3 disks", aws.bucketsToBeCreated.size()));
// For those disk states which do not have the tagLink, add the tagLink by PATCHing those states.
if (aws.internalTypeTagSelfLink != null) {
aws.diskStatesToBeUpdatedByBucketName.entrySet().stream().filter(diskMap -> diskMap.getValue().tagLinks == null || !diskMap.getValue().tagLinks.contains(aws.internalTypeTagSelfLink)).forEach(diskMap -> {
Map<String, Collection<Object>> collectionsToAddMap = Collections.singletonMap(DiskState.FIELD_NAME_TAG_LINKS, Collections.singletonList(aws.internalTypeTagSelfLink));
Map<String, Collection<Object>> collectionsToRemoveMap = Collections.singletonMap(DiskState.FIELD_NAME_TAG_LINKS, Collections.emptyList());
ServiceStateCollectionUpdateRequest updateTagLinksRequest = ServiceStateCollectionUpdateRequest.create(collectionsToAddMap, collectionsToRemoveMap);
aws.enumerationOperations.add(Operation.createPatch(this.getHost(), diskMap.getValue().documentSelfLink).setReferer(aws.service.getUri()).setBody(updateTagLinksRequest));
});
}
// update endpointLinks
aws.diskStatesToBeUpdatedByBucketName.entrySet().stream().filter(diskMap -> diskMap.getValue().endpointLinks == null || !diskMap.getValue().endpointLinks.contains(aws.request.original.endpointLink)).forEach(diskMap -> {
Map<String, Collection<Object>> collectionsToAddMap = Collections.singletonMap(DiskState.FIELD_NAME_ENDPOINT_LINKS, Collections.singletonList(aws.request.original.endpointLink));
Map<String, Collection<Object>> collectionsToRemoveMap = Collections.singletonMap(DiskState.FIELD_NAME_ENDPOINT_LINKS, Collections.emptyList());
ServiceStateCollectionUpdateRequest updateEndpointLinksRequest = ServiceStateCollectionUpdateRequest.create(collectionsToAddMap, collectionsToRemoveMap);
aws.enumerationOperations.add(Operation.createPatch(this.getHost(), diskMap.getValue().documentSelfLink).setReferer(aws.service.getUri()).setBody(updateEndpointLinksRequest));
});
OperationJoin.JoinedCompletionHandler joinCompletion = (ox, exc) -> {
if (exc != null) {
this.logSevere(() -> String.format("Error creating/updating disk %s", Utils.toString(exc)));
aws.subStage = S3StorageEnumerationSubStage.DELETE_DISKS;
handleReceivedEnumerationData(aws);
return;
}
ox.entrySet().stream().forEach(operationEntry -> {
aws.diskStatesEnumerated.add(operationEntry.getValue().getBody(DiskState.class));
});
this.logFine(() -> "Successfully created and updated all the disk states.");
aws.subStage = next;
handleReceivedEnumerationData(aws);
};
if (aws.enumerationOperations.isEmpty()) {
aws.subStage = next;
handleReceivedEnumerationData(aws);
return;
}
OperationJoin joinOp = OperationJoin.create(aws.enumerationOperations);
joinOp.setCompletion(joinCompletion);
joinOp.sendWith(this.getHost());
}
use of com.vmware.xenon.common.OperationJoin in project photon-model by vmware.
the class EndpointAdapterUtils method applyChanges.
private static void applyChanges(TaskManager tm, StatelessService service, EndpointState endpoint, Stream<Operation> operations) {
OperationJoin joinOp = OperationJoin.create(operations);
joinOp.setCompletion((ox, exc) -> {
if (exc != null) {
service.logSevere("Error patching endpoint configuration data for %s. %s", endpoint.endpointType, Utils.toString(exc));
tm.patchTaskToFailure(exc.values().iterator().next());
return;
}
service.logFine(() -> String.format("Successfully completed %s endpoint configuration tasks.", endpoint.endpointType));
tm.finishTask();
});
joinOp.sendWith(service);
}
use of com.vmware.xenon.common.OperationJoin in project photon-model by vmware.
the class VSphereAdapterSnapshotService method processNextStepsForDeleteOperation.
private void processNextStepsForDeleteOperation(SnapshotContext context, DeferredResult<SnapshotContext> deferredResult) {
final SnapshotState snapshot = context.snapshotState;
// Update the isCurrent
if (snapshot.isCurrent && snapshot.parentLink != null) {
logInfo("Updating the parent of the snapshot %s to current", snapshot.name);
SnapshotState parentSnapshot = new SnapshotState();
parentSnapshot.isCurrent = Boolean.TRUE;
context.snapshotOperations.add(Operation.createPatch(PhotonModelUriUtils.createInventoryUri(this.getHost(), snapshot.parentLink)).setBody(parentSnapshot).setReferer(getUri()));
}
// Check if the deleted snapshot is the last available snapshot
DeferredResult<Boolean> result = isLastSnapshotForCompute(context);
Operation[] patchComputeOp = new Operation[1];
result.whenComplete((b, e) -> {
if (e != null) {
logSevere(e);
deferredResult.fail(e);
return;
}
if (b) {
ComputeStateWithDescription compute = context.computeDescription;
compute.customProperties.put(ComputeProperties.CUSTOM_PROP_COMPUTE_HAS_SNAPSHOTS, Boolean.FALSE.toString());
// patch compute adding property that it _hasSnapshots
logInfo("Updating the state of compute resource: %s", compute.name);
patchComputeOp[0] = Operation.createPatch(UriUtils.buildUri(getHost(), snapshot.computeLink)).setBody(compute).setReferer(getUri());
context.snapshotOperations.add(patchComputeOp[0]);
}
OperationJoin.JoinedCompletionHandler joinCompletion = (ox, exc) -> {
if (exc != null) {
this.logSevere(() -> String.format("Error updating the snapshot states: %s", Utils.toString(exc)));
deferredResult.fail(new IllegalStateException("Error updating the snapshot states"));
return;
}
deferredResult.complete(context);
};
context.snapshotOperations.add(Operation.createDelete(UriUtils.buildUri(getHost(), snapshot.documentSelfLink)).setReferer(getUri()));
OperationJoin joinOp = OperationJoin.create(context.snapshotOperations);
joinOp.setCompletion(joinCompletion);
joinOp.sendWith(this.getHost());
});
}
use of com.vmware.xenon.common.OperationJoin in project photon-model by vmware.
the class VSphereAdapterSnapshotService method revertSnapshot.
private void revertSnapshot(SnapshotContext context, Connection connection, DeferredResult<SnapshotContext> deferredResult) {
final SnapshotState snapshot = context.snapshotState;
SnapshotState existingSnapshotState = context.existingSnapshotState;
// Physical snapshot processing
ManagedObjectReference snapshotMoref = CustomProperties.of(snapshot).getMoRef(CustomProperties.MOREF);
if (snapshotMoref == null) {
deferredResult.fail(new IllegalStateException(String.format("Cannot find the snapshot %s to revert to", snapshotMoref)));
return;
}
ManagedObjectReference task;
TaskInfo info;
try {
logInfo("Reverting to snapshot with name %s", context.snapshotState.name);
task = connection.getVimPort().revertToSnapshotTask(snapshotMoref, null, false);
info = VimUtils.waitTaskEnd(connection, task);
if (info.getState() != TaskInfoState.SUCCESS) {
VimUtils.rethrow(info.getError());
}
} catch (Exception e) {
logSevere("Reverting to the snapshot %s failed", context.snapshotState.name);
deferredResult.fail(e);
return;
}
// cause concurrency issue
if (snapshot.isCurrent) {
deferredResult.complete(context);
} else {
snapshot.isCurrent = true;
context.snapshotOperations.add(Operation.createPatch(UriUtils.buildUri(getHost(), snapshot.documentSelfLink)).setBody(snapshot).setReferer(getUri()));
if (existingSnapshotState != null) {
existingSnapshotState.isCurrent = false;
context.snapshotOperations.add(Operation.createPatch(UriUtils.buildUri(getHost(), existingSnapshotState.documentSelfLink)).setBody(existingSnapshotState).setReferer(getUri()));
}
OperationJoin.JoinedCompletionHandler joinCompletion = (ox, exc) -> {
if (exc != null) {
this.logSevere(() -> String.format("Error updating the snapshot states: %s", Utils.toString(exc)));
deferredResult.fail(new IllegalStateException("Error updating the snapshot states"));
return;
}
deferredResult.complete(context);
};
OperationJoin joinOp = OperationJoin.create(context.snapshotOperations);
joinOp.setCompletion(joinCompletion);
joinOp.sendWith(this.getHost());
}
}
Aggregations