Search in sources :

Example 41 with ServiceDocument

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);
    }
}
Also used : EndpointAllocationTaskState(com.vmware.photon.controller.model.tasks.EndpointAllocationTaskService.EndpointAllocationTaskState) ServiceDocument(com.vmware.xenon.common.ServiceDocument)

Example 42 with ServiceDocument

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);
}
Also used : ComputeDescriptionService(com.vmware.photon.controller.model.resources.ComputeDescriptionService) EndpointAllocationTaskState(com.vmware.photon.controller.model.tasks.EndpointAllocationTaskService.EndpointAllocationTaskState) ServiceDocument(com.vmware.xenon.common.ServiceDocument) EndpointService(com.vmware.photon.controller.model.resources.EndpointService) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService)

Example 43 with ServiceDocument

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);
    }
}
Also used : ServiceDocument(com.vmware.xenon.common.ServiceDocument) Operation(com.vmware.xenon.common.Operation)

Example 44 with ServiceDocument

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);
    }
}
Also used : TestContext(com.vmware.xenon.common.test.TestContext) Service(com.vmware.xenon.common.Service) StatefulService(com.vmware.xenon.common.StatefulService) FactoryService(com.vmware.xenon.common.FactoryService) ServiceDocument(com.vmware.xenon.common.ServiceDocument) Operation(com.vmware.xenon.common.Operation) URI(java.net.URI) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult)

Example 45 with ServiceDocument

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);
}
Also used : ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) ComputeBootRequest(com.vmware.photon.controller.model.adapterapi.ComputeBootRequest) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) Operation(com.vmware.xenon.common.Operation) ServiceDocument(com.vmware.xenon.common.ServiceDocument) InstanceRequestType(com.vmware.photon.controller.model.adapterapi.ComputeInstanceRequest.InstanceRequestType) BootDevice(com.vmware.photon.controller.model.resources.ComputeService.BootDevice) TaskStage(com.vmware.xenon.common.TaskState.TaskStage) ComputeInstanceRequest(com.vmware.photon.controller.model.adapterapi.ComputeInstanceRequest) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Utils(com.vmware.xenon.common.Utils) UriPaths(com.vmware.photon.controller.model.UriPaths) ClusterUtil(com.vmware.photon.controller.model.util.ClusterUtil) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) InetAddressValidator(org.apache.commons.validator.routines.InetAddressValidator) UriUtils(com.vmware.xenon.common.UriUtils) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) TaskState(com.vmware.xenon.common.TaskState) URI(java.net.URI) TaskService(com.vmware.xenon.services.common.TaskService) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) SubStage(com.vmware.photon.controller.model.tasks.ProvisionComputeTaskService.ProvisionComputeTaskState.SubStage) BootDevice(com.vmware.photon.controller.model.resources.ComputeService.BootDevice) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) ServiceDocument(com.vmware.xenon.common.ServiceDocument) ComputeBootRequest(com.vmware.photon.controller.model.adapterapi.ComputeBootRequest)

Aggregations

ServiceDocument (com.vmware.xenon.common.ServiceDocument)47 Operation (com.vmware.xenon.common.Operation)11 URI (java.net.URI)6 ComputeDescription (com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription)5 ComputeService (com.vmware.photon.controller.model.resources.ComputeService)5 EndpointState (com.vmware.photon.controller.model.resources.EndpointService.EndpointState)5 EndpointAllocationTaskState (com.vmware.photon.controller.model.tasks.EndpointAllocationTaskService.EndpointAllocationTaskState)5 PhotonModelUriUtils.createInventoryUri (com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri)5 CompletionHandler (com.vmware.xenon.common.Operation.CompletionHandler)5 UriUtils (com.vmware.xenon.common.UriUtils)5 List (java.util.List)5 UriPaths (com.vmware.photon.controller.model.UriPaths)4 ServiceTypeCluster (com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster)4 TaskState (com.vmware.xenon.common.TaskState)4 TaskStage (com.vmware.xenon.common.TaskState.TaskStage)4 Utils (com.vmware.xenon.common.Utils)4 TaskService (com.vmware.xenon.services.common.TaskService)4 TimeUnit (java.util.concurrent.TimeUnit)4 ComputeDescriptionService (com.vmware.photon.controller.model.resources.ComputeDescriptionService)3 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)3