use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class BaseModelTest method getServiceSynchronously.
public <T extends ServiceDocument> T getServiceSynchronously(String serviceUri, Class<T> type) throws Throwable {
AtomicReference<T> responseBody = new AtomicReference<>();
TestContext ctx = this.host.testCreate(1);
Operation getOperation = Operation.createGet(UriUtils.buildUri(this.host, serviceUri)).addPragmaDirective(Operation.PRAGMA_DIRECTIVE_QUEUE_FOR_SERVICE_AVAILABILITY).setCompletion((operation, throwable) -> {
if (throwable != null) {
ctx.failIteration(throwable);
return;
}
responseBody.set(operation.getBody(type));
ctx.completeIteration();
});
this.send(getOperation);
this.testWait(ctx);
return responseBody.get();
}
use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class BaseModelTest method deleteServiceSynchronously.
private <T extends ServiceDocument> void deleteServiceSynchronously(String serviceUri, boolean stopOnly) throws Throwable {
TestContext ctx = this.host.testCreate(1);
Operation deleteOperation = Operation.createDelete(UriUtils.buildUri(this.host, serviceUri)).setCompletion((operation, throwable) -> {
if (throwable != null) {
ctx.failIteration(throwable);
}
ctx.completeIteration();
});
if (stopOnly) {
deleteOperation.addPragmaDirective(Operation.PRAGMA_DIRECTIVE_NO_INDEX_UPDATE);
}
this.send(deleteOperation);
this.testWait(ctx);
}
use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class NetworkInterfaceService method handleGet.
@Override
public void handleGet(Operation get) {
NetworkInterfaceState currentState = getState(get);
boolean doExpand = get.getUri().getQuery() != null && UriUtils.hasODataExpandParamValue(get.getUri());
if (!doExpand) {
get.setBody(currentState).complete();
return;
}
// retrieve the description and include in an augmented version of our
// state.
Operation getDesc = Operation.createGet(this, currentState.networkInterfaceDescriptionLink).setCompletion((o, e) -> {
if (e != null) {
get.fail(e);
return;
}
NetworkInterfaceDescription desc = o.getBody(NetworkInterfaceDescription.class);
NetworkInterfaceStateWithDescription stateWithDesc = NetworkInterfaceStateWithDescription.create(desc, currentState);
get.setBody(stateWithDesc).complete();
});
sendRequest(getDesc);
}
use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class AzureStorageEnumerationAdapterService method disassociateStorageDescription.
/*
* Disassociate local storage accounts and all resources inside them that no longer exist in
* Azure
*
* The logic works by recording a timestamp when enumeration starts. This timestamp is used to
* lookup resources which haven't been touched as part of current enumeration cycle. The other
* data point this method uses is the storage accounts discovered as part of get storage
* accounts call.
*
* A disassociate on a storage description is invoked only if it meets two criteria: -
* Timestamp older
* than current enumeration cycle. - Storage account is not present on Azure.
*/
private void disassociateStorageDescription(StorageEnumContext context, StorageEnumStages next) {
Query.Builder qBuilder = Query.Builder.create().addKindFieldClause(StorageDescription.class).addRangeClause(StorageDescription.FIELD_NAME_UPDATE_TIME_MICROS, QueryTask.NumericRange.createLessThanRange(context.enumerationStartTimeInMicros));
QueryByPages<StorageDescription> queryLocalStates = new QueryByPages<>(getHost(), qBuilder.build(), StorageDescription.class, context.parentCompute.tenantLinks, context.request.endpointLink, context.parentCompute.documentSelfLink).setMaxPageSize(QueryUtils.MAX_RESULT_LIMIT).setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
List<DeferredResult<Operation>> disassociateDRs = new ArrayList<>();
queryLocalStates.queryDocuments(sd -> {
if (context.storageAccountIds.contains(sd.id)) {
return;
}
// Deleting the localResourceState is done by disassociating the
// endpointLink from the localResourceState. If the localResourceState
// isn't associated with any other endpointLink, it should be eventually
// deleted by the groomer task
Operation disassociateOp = PhotonModelUtils.createRemoveEndpointLinksOperation(this, context.request.endpointLink, sd);
if (disassociateOp == null) {
return;
}
// NOTE: The original Op is set with completion that must be executed.
// Since sendWithDeferredResult is used we must manually call it, otherwise it's
// just ignored.
CompletionHandler disassociateOpCompletion = disassociateOp.getCompletion();
DeferredResult<Operation> disassociateDR = sendWithDeferredResult(disassociateOp).whenComplete(disassociateOpCompletion::handle).whenComplete((o, e) -> {
final String message = "Disassociate stale %s state";
if (e != null) {
logWarning(message + ": FAILED with %s", sd.documentSelfLink, Utils.toString(e));
} else {
log(Level.FINEST, message + ": SUCCESS", sd.documentSelfLink);
}
});
disassociateDRs.add(disassociateDR);
}).thenCompose(ignore -> DeferredResult.allOf(disassociateDRs)).whenComplete((r, e) -> {
logFine(() -> "Finished disassociation of storage descriptions for Azure");
context.subStage = next;
handleSubStage(context);
});
}
use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class AzureStorageEnumerationAdapterService method disassociateResourceGroupStates.
/*
* Disassociate local storage containers that no longer exist in Azure
*
* The logic works by recording a timestamp when enumeration starts. This timestamp is used to
* lookup resources which haven't been touched as part of current enumeration cycle. The other
* data point this method uses is the storage accounts discovered as part of get storage
* accounts call.
*
* A disassociate on a resource group state is invoked only if it meets two criteria: -
* Timestamp
* older than current enumeration cycle. - Storage container is not present on Azure.
*/
private void disassociateResourceGroupStates(StorageEnumContext context, StorageEnumStages next) {
Query.Builder qBuilder = newResourceGroupQueryBuilder(context).addRangeClause(ResourceGroupState.FIELD_NAME_UPDATE_TIME_MICROS, QueryTask.NumericRange.createLessThanRange(context.enumerationStartTimeInMicros));
QueryByPages<ResourceGroupState> queryLocalStates = new QueryByPages<>(getHost(), qBuilder.build(), ResourceGroupState.class, context.parentCompute.tenantLinks, null, /* endpointLink */
context.parentCompute.documentSelfLink).setMaxPageSize(QueryUtils.MAX_RESULT_LIMIT).setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
List<DeferredResult<Operation>> ops = new ArrayList<>();
queryLocalStates.queryDocuments(rg -> {
// repository it means nothing has changed about it.
if (context.containerIds.contains(canonizeId(rg.id))) {
return;
}
// Deleting the localResourceState is done by disassociating the
// endpointLink from the localResourceState. If the localResourceState
// isn't associated with any other endpointLink, it should be eventually
// deleted by the groomer task
Operation disassociateOp = PhotonModelUtils.createRemoveEndpointLinksOperation(this, context.request.endpointLink, rg);
if (disassociateOp == null) {
return;
}
// NOTE: The original Op is set with completion that must be executed.
// Since sendWithDeferredResult is used we must manually call it, otherwise it's
// just ignored.
CompletionHandler disassociateOpCompletion = disassociateOp.getCompletion();
DeferredResult<Operation> disassociateDR = sendWithDeferredResult(disassociateOp).whenComplete(disassociateOpCompletion::handle).whenComplete((o, e) -> {
final String message = "Disassociate stale %s state";
if (e != null) {
logWarning(message + ": FAILED with %s", rg.documentSelfLink, Utils.toString(e));
} else {
log(Level.FINEST, message + ": SUCCESS", rg.documentSelfLink);
}
});
ops.add(disassociateDR);
}).thenCompose(r -> DeferredResult.allOf(ops)).whenComplete((r, e) -> {
logFine(() -> "Finished disassociation of storage containers for Azure");
context.subStage = next;
handleSubStage(context);
});
}
Aggregations