Search in sources :

Example 81 with Operation

use of com.vmware.xenon.common.Operation in project photon-model by vmware.

the class ProvisionDiskTaskService method validateDiskAndStart.

private void validateDiskAndStart(ProvisionDiskTaskState state, Operation startPost) {
    URI diskUri = UriUtils.buildUri(getHost(), state.diskLink);
    sendRequest(Operation.createGet(createInventoryUri(this.getHost(), diskUri)).setCompletion((o, e) -> {
        if (e != null) {
            logWarning(() -> String.format("Failure retrieving disk state (%s): %s", diskUri, e.toString()));
            o.complete();
            failTask(e);
            return;
        }
        DiskState disk = o.getBody(DiskState.class);
        state.diskAdapterReference = disk.diskAdapterReference;
        startPost.complete();
        if (disk.capacityMBytes < 0) {
            failTask(new IllegalArgumentException("disk capacity is mandatory for a disk"));
            return;
        }
        if (state.taskSubStage == ProvisionDiskTaskState.SubStage.CREATING_DISK && state.diskAdapterReference == null) {
            failTask(new IllegalArgumentException("diskState does not have create service specified"));
            return;
        }
        sendSelfPatch(TaskStage.STARTED, state.taskSubStage, null);
    }));
}
Also used : ProvisionDiskTaskState(com.vmware.photon.controller.model.tasks.ProvisionDiskTaskService.ProvisionDiskTaskState) Operation(com.vmware.xenon.common.Operation) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) ServiceDocument(com.vmware.xenon.common.ServiceDocument) DiskInstanceRequest(com.vmware.photon.controller.model.adapterapi.DiskInstanceRequest) TaskStage(com.vmware.xenon.common.TaskState.TaskStage) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Utils(com.vmware.xenon.common.Utils) UriPaths(com.vmware.photon.controller.model.UriPaths) SubStage(com.vmware.photon.controller.model.tasks.ProvisionDiskTaskService.ProvisionDiskTaskState.SubStage) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) UriUtils(com.vmware.xenon.common.UriUtils) 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) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) URI(java.net.URI)

Example 82 with Operation

use of com.vmware.xenon.common.Operation in project photon-model by vmware.

the class ProvisionDiskTaskService method sendSelfPatch.

private void sendSelfPatch(TaskStage newStage, ProvisionDiskTaskState.SubStage newSubStage, Throwable ex) {
    ProvisionDiskTaskState patchBody = new ProvisionDiskTaskState();
    patchBody.taskInfo = new TaskState();
    patchBody.taskInfo.stage = newStage;
    patchBody.taskSubStage = newSubStage;
    if (ex != null) {
        patchBody.taskInfo.failure = Utils.toServiceErrorResponse(ex);
    }
    Operation patch = Operation.createPatch(createInventoryUri(this.getHost(), getUri())).setBody(patchBody).setCompletion((o, e) -> {
        if (e != null) {
            logWarning(() -> String.format("Self patch failed: %s", com.vmware.xenon.common.Utils.toString(e)));
        }
    });
    sendRequest(patch);
}
Also used : ProvisionDiskTaskState(com.vmware.photon.controller.model.tasks.ProvisionDiskTaskService.ProvisionDiskTaskState) Operation(com.vmware.xenon.common.Operation) ProvisionDiskTaskState(com.vmware.photon.controller.model.tasks.ProvisionDiskTaskService.ProvisionDiskTaskState) TaskState(com.vmware.xenon.common.TaskState)

Example 83 with Operation

use of com.vmware.xenon.common.Operation in project photon-model by vmware.

the class ProvisionDiskTaskService method createSubTask.

public void createSubTask(CompletionHandler c, ProvisionDiskTaskState.SubStage nextStage, ProvisionDiskTaskState currentState) {
    ServiceTaskCallback<SubStage> callback = ServiceTaskCallback.create(UriUtils.buildPublicUri(getHost(), getSelfLink()));
    callback.onSuccessTo(nextStage);
    SubTaskService.SubTaskState<SubStage> subTaskInitState = new SubTaskService.SubTaskState<>();
    subTaskInitState.errorThreshold = 0;
    subTaskInitState.serviceTaskCallback = callback;
    subTaskInitState.tenantLinks = currentState.tenantLinks;
    subTaskInitState.documentExpirationTimeMicros = currentState.documentExpirationTimeMicros;
    Operation startPost = Operation.createPost(createInventoryUri(this.getHost(), SubTaskService.FACTORY_LINK)).setBody(subTaskInitState).setCompletion(c);
    sendRequest(startPost);
}
Also used : SubStage(com.vmware.photon.controller.model.tasks.ProvisionDiskTaskService.ProvisionDiskTaskState.SubStage) Operation(com.vmware.xenon.common.Operation)

Example 84 with Operation

use of com.vmware.xenon.common.Operation in project photon-model by vmware.

the class ProvisionSecurityGroupTaskService method createSubTask.

private void createSubTask(ProvisionSecurityGroupTaskState taskState, Consumer<String> subTaskLinkConsumer) {
    ServiceTaskCallback<SubStage> callback = ServiceTaskCallback.create(UriUtils.buildPublicUri(getHost(), getSelfLink()));
    callback.onSuccessFinishTask();
    SubTaskService.SubTaskState<SubStage> subTaskInitState = new SubTaskService.SubTaskState<>();
    // tell the sub task with what to patch us, on completion
    subTaskInitState.serviceTaskCallback = callback;
    subTaskInitState.completionsRemaining = taskState.securityGroupDescriptionLinks.size();
    subTaskInitState.tenantLinks = taskState.tenantLinks;
    subTaskInitState.documentExpirationTimeMicros = taskState.documentExpirationTimeMicros;
    Operation startPost = Operation.createPost(this, SubTaskService.FACTORY_LINK).setBody(subTaskInitState).setCompletion((o, e) -> {
        if (e != null) {
            logWarning(() -> String.format("Failure creating sub task: %s", Utils.toString(e)));
            sendSelfPatch(TaskState.TaskStage.FAILED, e);
            return;
        }
        SubTaskService.SubTaskState<?> body = o.getBody(SubTaskService.SubTaskState.class);
        subTaskLinkConsumer.accept(body.documentSelfLink);
    });
    sendRequest(startPost);
}
Also used : Operation(com.vmware.xenon.common.Operation)

Example 85 with Operation

use of com.vmware.xenon.common.Operation in project photon-model by vmware.

the class ResourceAllocationTaskService method createDiskResources.

/**
 * Create disks to attach to the compute resource. Use the disk description links to figure out
 * what type of disks to create.
 *
 * @param currentState
 * @param parentLink
 * @param computeResourceId
 */
private void createDiskResources(ResourceAllocationTaskState currentState, ComputeDescription cd, String parentLink, String computeResourceId, String name, List<String> networkLinks) {
    if (cd.diskDescLinks == null || cd.diskDescLinks.isEmpty()) {
        createComputeResource(currentState, cd, parentLink, computeResourceId, name, new ArrayList<>(), networkLinks);
        return;
    }
    DeferredResult<List<String>> result = DeferredResult.allOf(cd.diskDescLinks.stream().map(link -> {
        Operation op = Operation.createGet(this, link);
        return this.sendWithDeferredResult(op, DiskState.class);
    }).map(dr -> dr.thenCompose(d -> {
        String link = d.documentSelfLink;
        // create a new disk based off the template but use a
        // unique ID
        d.id = UUID.randomUUID().toString();
        d.documentSelfLink = null;
        d.tenantLinks = currentState.tenantLinks;
        if (d.customProperties == null) {
            d.customProperties = new HashMap<>();
        }
        d.descriptionLink = link;
        return this.sendWithDeferredResult(Operation.createPost(this, DiskService.FACTORY_LINK).setBody(d), DiskState.class);
    })).map(dsr -> dsr.thenApply(ds -> ds.documentSelfLink)).collect(Collectors.toList()));
    result.whenComplete((diskLinks, e) -> {
        if (e != null) {
            logWarning(() -> String.format("Failure creating disk: %s", e.toString()));
            this.sendFailureSelfPatch(e);
            return;
        }
        // we have created all the disks. Now create the compute host
        // resource
        createComputeResource(currentState, cd, parentLink, computeResourceId, name, diskLinks, networkLinks);
    });
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) ServiceDocument(com.vmware.xenon.common.ServiceDocument) HashMap(java.util.HashMap) ComputeDescriptionService(com.vmware.photon.controller.model.resources.ComputeDescriptionService) ArrayList(java.util.ArrayList) ComputeType(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType) HashSet(java.util.HashSet) Utils(com.vmware.xenon.common.Utils) UriPaths(com.vmware.photon.controller.model.UriPaths) Map(java.util.Map) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) Iterator(java.util.Iterator) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) Collection(java.util.Collection) ResourceDescriptionService(com.vmware.photon.controller.model.resources.ResourceDescriptionService) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) Occurance(com.vmware.xenon.services.common.QueryTask.Query.Occurance) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) TaskStage(com.vmware.xenon.common.TaskState.TaskStage) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) NetworkInterfaceService(com.vmware.photon.controller.model.resources.NetworkInterfaceService) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) TaskState(com.vmware.xenon.common.TaskState) CUSTOM_DISPLAY_NAME(com.vmware.photon.controller.model.ComputeProperties.CUSTOM_DISPLAY_NAME) NetworkInterfaceDescription(com.vmware.photon.controller.model.resources.NetworkInterfaceDescriptionService.NetworkInterfaceDescription) DiskService(com.vmware.photon.controller.model.resources.DiskService) TaskService(com.vmware.xenon.services.common.TaskService) ArrayList(java.util.ArrayList) List(java.util.List) Operation(com.vmware.xenon.common.Operation)

Aggregations

Operation (com.vmware.xenon.common.Operation)391 URI (java.net.URI)142 ArrayList (java.util.ArrayList)132 QueryTask (com.vmware.xenon.services.common.QueryTask)118 List (java.util.List)118 Utils (com.vmware.xenon.common.Utils)111 StatelessService (com.vmware.xenon.common.StatelessService)108 UriUtils (com.vmware.xenon.common.UriUtils)106 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)98 Map (java.util.Map)92 HashMap (java.util.HashMap)90 OperationJoin (com.vmware.xenon.common.OperationJoin)86 Query (com.vmware.xenon.services.common.QueryTask.Query)86 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)82 Collectors (java.util.stream.Collectors)79 HashSet (java.util.HashSet)78 AuthCredentialsServiceState (com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)73 AdapterUtils (com.vmware.photon.controller.model.adapters.util.AdapterUtils)70 DeferredResult (com.vmware.xenon.common.DeferredResult)69 Consumer (java.util.function.Consumer)69