use of com.vmware.xenon.common.ServiceDocument in project photon-model by vmware.
the class EndpointServiceTests method testShouldFailOnCreatingDuplicateEndpoint.
public void testShouldFailOnCreatingDuplicateEndpoint(EndpointState ep) throws Throwable {
if (!this.isMock) {
EndpointAllocationTaskState createEndpoint = new EndpointAllocationTaskState();
createEndpoint.endpointState = ep;
createEndpoint.options = this.isMock ? EnumSet.of(TaskOption.IS_MOCK) : null;
createEndpoint.checkForEndpointUniqueness = Boolean.TRUE;
EndpointAllocationTaskState outTask = TestUtils.doPost(this.host, createEndpoint, EndpointAllocationTaskState.class, UriUtils.buildUri(this.host, EndpointAllocationTaskService.FACTORY_LINK));
this.host.waitForFinishedTask(EndpointAllocationTaskState.class, outTask.documentSelfLink);
EndpointAllocationTaskState taskState = getServiceSynchronously(outTask.documentSelfLink, EndpointAllocationTaskState.class);
assertNotNull(taskState);
assertNotNull(taskState.endpointState);
ServiceDocument endpointState = taskState.endpointState;
assertNotNull(endpointState.documentSelfLink);
// check endpoint document was created
getServiceSynchronously(endpointState.documentSelfLink, EndpointState.class);
// Run the task again with the same endpoint
EndpointAllocationTaskState duplicateEndpointTask = TestUtils.doPost(this.host, createEndpoint, EndpointAllocationTaskState.class, UriUtils.buildUri(this.host, EndpointAllocationTaskService.FACTORY_LINK));
EndpointAllocationTaskState failedTask = this.host.waitForFailedTask(EndpointAllocationTaskState.class, duplicateEndpointTask.documentSelfLink);
assertEquals(failedTask.taskInfo.stage, TaskState.TaskStage.FAILED);
}
}
use of com.vmware.xenon.common.ServiceDocument in project photon-model by vmware.
the class EndpointServiceTests method testCreateEndpoint.
public void testCreateEndpoint(EndpointService.EndpointState ep) throws Throwable {
EndpointAllocationTaskState validateEndpoint = new EndpointAllocationTaskState();
validateEndpoint.endpointState = ep;
validateEndpoint.options = this.isMock ? EnumSet.of(TaskOption.IS_MOCK) : null;
EndpointAllocationTaskState outTask = TestUtils.doPost(this.host, validateEndpoint, EndpointAllocationTaskState.class, UriUtils.buildUri(this.host, EndpointAllocationTaskService.FACTORY_LINK));
this.host.waitForFinishedTask(EndpointAllocationTaskState.class, outTask.documentSelfLink);
EndpointAllocationTaskState taskState = getServiceSynchronously(outTask.documentSelfLink, EndpointAllocationTaskState.class);
assertNotNull(taskState);
assertNotNull(taskState.endpointState);
ServiceDocument endpointState = taskState.endpointState;
assertNotNull(endpointState.documentSelfLink);
// check endpoint document was created
EndpointService.EndpointState endpoint = getServiceSynchronously(endpointState.documentSelfLink, EndpointService.EndpointState.class);
assertNotNull(endpoint);
assertNotNull(endpoint.authCredentialsLink);
assertNotNull(endpoint.computeLink);
assertNotNull(endpoint.computeDescriptionLink);
AuthCredentialsService.AuthCredentialsServiceState credentials = getServiceSynchronously(endpoint.authCredentialsLink, AuthCredentialsService.AuthCredentialsServiceState.class);
assertNotNull(credentials);
assertEquals(ep.endpointProperties.get(PRIVATE_KEYID_KEY), credentials.privateKeyId);
assertEquals(ep.endpointProperties.get(PRIVATE_KEY_KEY), credentials.privateKey);
ComputeDescriptionService.ComputeDescription cd = getServiceSynchronously(endpoint.computeDescriptionLink, ComputeDescriptionService.ComputeDescription.class);
assertNotNull(cd);
assertEquals(credentials.documentSelfLink, cd.authCredentialsLink);
assertEquals(this.regionId, cd.regionId);
assertEquals(this.environmentName, cd.environmentName);
ComputeService.ComputeState cs = getServiceSynchronously(endpoint.computeLink, ComputeService.ComputeState.class);
assertNotNull(cs);
assertNotNull(cs.adapterManagementReference);
assertEquals(ComputeDescriptionService.ComputeDescription.ComputeType.ENDPOINT_HOST, cs.type);
assertEquals(this.environmentName, cs.environmentName);
assertEquals(ComputeService.PowerState.ON, cs.powerState);
}
use of com.vmware.xenon.common.ServiceDocument in project photon-model by vmware.
the class TaskUtils method handleSubscriptionNotifications.
/**
* handle subscriptions from multiple services 1. Mark operation as complete 2. If the operation
* is not PUT or POST, return 3. If the task has failed, send failure patch to service 4. Update
* the list of services from which we have received notification 5. Unsubscribe from
* notifications, optionally delete the task that raised the notification 6. If we have received
* the expected number of notifications, patch the next state back
*
* @param service
* Stateful provisioning service
* @param update
* Notification operation
* @param notificationTaskLink
* Self link of the task that raised the notification
* @param opTaskState
* TaskState of the service that raised the notification
* @param expectedNotificationCount
* Expected number of notifications
* @param returnState
* The next state for the StatefulService
* @param finishedTaskLinks
* Set of self links for services from which we have received notification
* @param deleteTask
* flag to delete the service that raised the notification
*/
public static void handleSubscriptionNotifications(StatefulService service, Operation update, String notificationTaskLink, TaskState opTaskState, int expectedNotificationCount, Object returnState, Set<String> finishedTaskLinks, boolean deleteTask) {
int finishedTaskCount;
update.complete();
if ((update.getAction() != Action.PATCH && update.getAction() != Action.PUT)) {
return;
}
// Fail if task was cancelled or has failed
if (TaskState.isCancelled(opTaskState) || TaskState.isFailed(opTaskState)) {
if (update.getBodyRaw() != null) {
sendFailureSelfPatch(service, new IllegalStateException("Operation failed:" + Utils.toJson(update.getBodyRaw())));
} else {
sendFailureSelfPatch(service, new IllegalStateException("Operation failed:" + "Operation cancelled or failed while deleting computes"));
}
if (deleteTask) {
service.sendRequest(Operation.createDelete(service, notificationTaskLink).setBody(new ServiceDocument()));
}
return;
}
// Ignore if task has not finished yet
if (!TaskState.isFinished(opTaskState)) {
return;
}
// Ignore if task has already been seen
synchronized (finishedTaskLinks) {
if (!finishedTaskLinks.add(notificationTaskLink)) {
return;
}
// Retrieve size in synchronized block to prevent racing
finishedTaskCount = finishedTaskLinks.size();
}
Operation deleteOp = Operation.createDelete(service, notificationTaskLink).setReferer(service.getUri()).setCompletion((delOp, delEx) -> {
if (delEx != null) {
service.logWarning(() -> String.format("Stopping subscriber failed" + " %s", Utils.toString(delEx)));
return;
}
});
service.getHost().stopSubscriptionService(deleteOp, UriUtils.buildPublicUri(service.getHost(), update.getUri().getPath()));
if (deleteTask) {
service.sendRequest(Operation.createDelete(service, notificationTaskLink).setBody(new ServiceDocument()));
}
// patch service with return state when we have seen all tasks finish
if (finishedTaskCount == expectedNotificationCount) {
// Patch back the return state
sendPatch(service, returnState);
}
}
use of com.vmware.xenon.common.ServiceDocument in project photon-model by vmware.
the class BaseTestCase method verifyService.
protected void verifyService(URI factoryUri, FactoryService factoryInstance, Class<? extends ServiceDocument> serviceDocumentType, TestServiceDocumentInitialization serviceDocumentInit, TestServiceDocumentAssertion assertion) throws Throwable {
int childCount = 1;
TestContext ctx = testCreate(childCount);
String prefix = "example-";
URI[] childURIs = new URI[childCount];
for (int i = 0; i < childCount; i++) {
ServiceDocument serviceDocument = serviceDocumentInit.create(prefix, i);
final int finalI = i;
// create a ServiceDocument instance.
Operation createPost = createForcedPost(factoryUri).setBody(serviceDocument).setCompletion((o, e) -> {
if (e != null) {
ctx.failIteration(e);
return;
}
ServiceDocument rsp = o.getBody(serviceDocumentType);
childURIs[finalI] = UriUtils.buildUri(this.host, rsp.documentSelfLink);
ctx.completeIteration();
});
this.host.send(createPost);
}
try {
// verify factory and service instance wiring.
factoryInstance.setHost(this.host);
Service serviceInstance = factoryInstance.createServiceInstance();
serviceInstance.setHost(this.host);
assertNotNull(serviceInstance);
ctx.await();
// do GET on all child URIs
Map<URI, ? extends ServiceDocument> childStates = this.host.getServiceState(null, serviceDocumentType, childURIs);
for (ServiceDocument s : childStates.values()) {
assertion.assertState(prefix, s);
}
// verify template GET works on factory
ServiceDocumentQueryResult templateResult = this.host.getServiceState(null, ServiceDocumentQueryResult.class, UriUtils.extendUri(factoryUri, ServiceHost.SERVICE_URI_SUFFIX_TEMPLATE));
assertTrue(templateResult.documentLinks.size() == templateResult.documents.size());
ServiceDocument childTemplate = Utils.fromJson(templateResult.documents.get(templateResult.documentLinks.iterator().next()), serviceDocumentType);
assertTrue(childTemplate.documentDescription != null);
assertTrue(childTemplate.documentDescription.propertyDescriptions != null && childTemplate.documentDescription.propertyDescriptions.size() > 0);
} catch (Throwable t) {
if (t instanceof RuntimeException) {
throw t;
}
throw new RuntimeException(t);
}
}
use of com.vmware.xenon.common.ServiceDocument in project photon-model by vmware.
the class ProvisionComputeTaskService method doSubStageBootHost.
private void doSubStageBootHost(ProvisionComputeTaskState updatedState, BootDevice[] bootDevices, ProvisionComputeTaskState.SubStage nextStage) {
CompletionHandler c = (o, e) -> {
if (e != null) {
failTask(e);
return;
}
ComputeBootRequest br = new ComputeBootRequest();
br.resourceReference = UriUtils.extendUri(ClusterUtil.getClusterUri(getHost(), ServiceTypeCluster.INVENTORY_SERVICE), updatedState.computeLink);
for (BootDevice bootDevice : bootDevices) {
br.bootDeviceOrder.add(bootDevice);
}
ServiceDocument subTask = o.getBody(ServiceDocument.class);
br.taskReference = UriUtils.buildUri(this.getHost(), subTask.documentSelfLink);
br.isMockRequest = updatedState.isMockRequest;
sendHostServiceRequest(br, updatedState.bootAdapterReference);
};
// After setting boot order and rebooting, we want the sub-task
// to patch the main task to the "next" state.
createSubTask(c, nextStage, updatedState);
}
Aggregations