Search in sources :

Example 26 with DeferredResult

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

the class IPAddressAllocationTaskService method deallocateIpAddress.

/**
 * De-allocates an IP address, based on IP address resource link.
 *
 * @param state IP Address allocation task state.
 */
private void deallocateIpAddress(IPAddressAllocationTaskState state) {
    IPAddressState addressState = new IPAddressState();
    addressState.connectedResourceLink = state.connectedResourceLink;
    addressState.ipAddressStatus = IPAddressState.IPAddressStatus.RELEASED;
    List<DeferredResult<Operation>> deferredResults = new ArrayList<>();
    for (int i = 0; i < state.ipAddressLinks.size(); i++) {
        String ipAddressResourceLink = state.ipAddressLinks.get(i);
        Operation patchOp = Operation.createPatch(this, ipAddressResourceLink).setBody(addressState);
        deferredResults.add(this.sendWithDeferredResult(patchOp));
    }
    DeferredResult.allOf(deferredResults).thenAccept(dr -> proceedTo(IPAddressAllocationTaskState.SubStage.FINISHED, null)).exceptionally(e -> {
        if (e != null) {
            failTask(e, "Failed to de-allocate IP addresses due to failure %s", e.getMessage());
        }
        return null;
    });
}
Also used : IpHelper(com.vmware.photon.controller.model.util.IpHelper) Service(com.vmware.xenon.common.Service) IPAddressStatus(com.vmware.photon.controller.model.resources.IPAddressService.IPAddressState.IPAddressStatus) IPVersion(com.vmware.photon.controller.model.support.IPVersion) IPAddressState(com.vmware.photon.controller.model.resources.IPAddressService.IPAddressState) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) SubnetValidator(com.vmware.photon.controller.model.util.SubnetValidator) QueryTask(com.vmware.xenon.services.common.QueryTask) STORE_ONLY(com.vmware.xenon.common.ServiceDocumentDescription.PropertyIndexingOption.STORE_ONLY) ServiceDocument(com.vmware.xenon.common.ServiceDocument) LINKS(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption.LINKS) HashMap(java.util.HashMap) PhotonModelUtils(com.vmware.photon.controller.model.resources.util.PhotonModelUtils) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Utils(com.vmware.xenon.common.Utils) UriPaths(com.vmware.photon.controller.model.UriPaths) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) Map(java.util.Map) IPAddressService(com.vmware.photon.controller.model.resources.IPAddressService) SINGLE_ASSIGNMENT(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption.SINGLE_ASSIGNMENT) SERVICE_USE(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption.SERVICE_USE) ServiceErrorResponse(com.vmware.xenon.common.ServiceErrorResponse) Operation(com.vmware.xenon.common.Operation) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) Set(java.util.Set) CompletionException(java.util.concurrent.CompletionException) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) Collectors.toList(java.util.stream.Collectors.toList) OPTIONAL(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption.OPTIONAL) List(java.util.List) ServiceTaskCallbackResponse(com.vmware.photon.controller.model.tasks.ServiceTaskCallback.ServiceTaskCallbackResponse) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) SubnetRangeState(com.vmware.photon.controller.model.resources.SubnetRangeService.SubnetRangeState) Optional(java.util.Optional) ReleaseConstants(com.vmware.photon.controller.model.constants.ReleaseConstants) TaskState(com.vmware.xenon.common.TaskState) ServiceDocumentDescription(com.vmware.xenon.common.ServiceDocumentDescription) TaskService(com.vmware.xenon.services.common.TaskService) Since(com.esotericsoftware.kryo.serializers.VersionFieldSerializer.Since) IPAddressState(com.vmware.photon.controller.model.resources.IPAddressService.IPAddressState) ArrayList(java.util.ArrayList) Operation(com.vmware.xenon.common.Operation) DeferredResult(com.vmware.xenon.common.DeferredResult)

Example 27 with DeferredResult

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

the class IPAddressAllocationTaskService method createNewIpAddressResource.

/**
 * Creates new IP address resource with the specified IP address and moves the task state to completed,
 * once it is done. Then it creates IP Address resource, it first creates it with AVAILABLE state. It then
 * changes the state to ALLOCATED. This is done in two steps, due to concurrency issues. When multiple allocation
 * requests are invoked at the same time, they end up creating single IP Address resource. Only one of them
 * succeeds in their PATCH and the other one will retry the allocation operation.
 *
 * @param ipAddress               IP address to use for the new IP address resource.
 * @param subnetRangeResourceLink Subnet range resource link to use for the new IP address resource.
 * @param connectedResourceLink   Link to the resource this IP is assigned to.
 */
private DeferredResult<IPAddressState> createNewIpAddressResource(String ipAddress, String subnetRangeResourceLink, String connectedResourceLink, IPAddressAllocationContext context) {
    IPAddressState ipAddressState = new IPAddressState();
    ipAddressState.ipAddressStatus = IPAddressState.IPAddressStatus.AVAILABLE;
    ipAddressState.ipAddress = ipAddress;
    ipAddressState.subnetRangeLink = subnetRangeResourceLink;
    ipAddressState.documentSelfLink = generateIPAddressDocumentSelfLink(subnetRangeResourceLink, ipAddress);
    logInfo("Creating IPAddressState with IP %s, subnet %s, for connected resource " + "%s", ipAddress, subnetRangeResourceLink, connectedResourceLink);
    return sendWithDeferredResult(Operation.createPost(this, IPAddressService.FACTORY_LINK).setBody(ipAddressState)).thenApply((out) -> {
        IPAddressState availableIPAddress = out.getBody(IPAddressState.class);
        availableIPAddress.ipAddressStatus = IPAddressState.IPAddressStatus.ALLOCATED;
        availableIPAddress.connectedResourceLink = connectedResourceLink;
        return availableIPAddress;
    }).thenCompose((availableIPAddress) -> (updateExistingIpAddressResource(availableIPAddress, context)));
}
Also used : IpHelper(com.vmware.photon.controller.model.util.IpHelper) Service(com.vmware.xenon.common.Service) IPAddressStatus(com.vmware.photon.controller.model.resources.IPAddressService.IPAddressState.IPAddressStatus) IPVersion(com.vmware.photon.controller.model.support.IPVersion) IPAddressState(com.vmware.photon.controller.model.resources.IPAddressService.IPAddressState) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) SubnetValidator(com.vmware.photon.controller.model.util.SubnetValidator) QueryTask(com.vmware.xenon.services.common.QueryTask) STORE_ONLY(com.vmware.xenon.common.ServiceDocumentDescription.PropertyIndexingOption.STORE_ONLY) ServiceDocument(com.vmware.xenon.common.ServiceDocument) LINKS(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption.LINKS) HashMap(java.util.HashMap) PhotonModelUtils(com.vmware.photon.controller.model.resources.util.PhotonModelUtils) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Utils(com.vmware.xenon.common.Utils) UriPaths(com.vmware.photon.controller.model.UriPaths) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) Map(java.util.Map) IPAddressService(com.vmware.photon.controller.model.resources.IPAddressService) SINGLE_ASSIGNMENT(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption.SINGLE_ASSIGNMENT) SERVICE_USE(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption.SERVICE_USE) ServiceErrorResponse(com.vmware.xenon.common.ServiceErrorResponse) Operation(com.vmware.xenon.common.Operation) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) Set(java.util.Set) CompletionException(java.util.concurrent.CompletionException) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) Collectors.toList(java.util.stream.Collectors.toList) OPTIONAL(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption.OPTIONAL) List(java.util.List) ServiceTaskCallbackResponse(com.vmware.photon.controller.model.tasks.ServiceTaskCallback.ServiceTaskCallbackResponse) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) SubnetRangeState(com.vmware.photon.controller.model.resources.SubnetRangeService.SubnetRangeState) Optional(java.util.Optional) ReleaseConstants(com.vmware.photon.controller.model.constants.ReleaseConstants) TaskState(com.vmware.xenon.common.TaskState) ServiceDocumentDescription(com.vmware.xenon.common.ServiceDocumentDescription) TaskService(com.vmware.xenon.services.common.TaskService) Since(com.esotericsoftware.kryo.serializers.VersionFieldSerializer.Since) IPAddressState(com.vmware.photon.controller.model.resources.IPAddressService.IPAddressState)

Example 28 with DeferredResult

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

the class ImageEnumerationTaskService method deletePublicImagesByEndpointType.

private DeferredResult<SendImageEnumerationAdapterContext> deletePublicImagesByEndpointType(SendImageEnumerationAdapterContext ctx) {
    QueryByPages<ImageState> queryAll = new QueryByPages<ImageState>(getHost(), publicImagesByEndpointTypeQuery(ctx).build(), ImageState.class, null).setQueryTaskTenantLinks(ctx.taskState.tenantLinks);
    queryAll.setMaxPageSize(QueryUtils.DEFAULT_MAX_RESULT_LIMIT);
    queryAll.setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
    final List<DeferredResult<Operation>> deleteDRs = new ArrayList<>();
    queryAll.queryLinks(imageLink -> {
        Operation delOp = Operation.createDelete(UriUtils.buildUri(getHost(), imageLink));
        DeferredResult<Operation> delDR = sendWithDeferredResult(delOp).whenComplete((op, e) -> {
            final String msg = "Deleting '%s' public image state [%s]";
            if (e != null) {
                // Be tolerant on individual images delete
                log(Level.WARNING, () -> String.format(msg + ": FAILED with %s", ctx.taskState.endpointType, imageLink, Utils.toString(e)));
            } else {
                log(Level.FINEST, () -> String.format(msg + ": SUCCESS", ctx.taskState.endpointType, imageLink));
            }
        });
        deleteDRs.add(delDR);
    });
    return DeferredResult.allOf(deleteDRs).thenApply(ignore -> ctx);
}
Also used : ArrayList(java.util.ArrayList) Operation(com.vmware.xenon.common.Operation) ImageState(com.vmware.photon.controller.model.resources.ImageService.ImageState) DeferredResult(com.vmware.xenon.common.DeferredResult)

Example 29 with DeferredResult

use of com.vmware.xenon.common.DeferredResult 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)

Example 30 with DeferredResult

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

the class IPAddressReleaseTaskService method markIPAddressesAsAvailable.

/**
 * Releases IP Addresses and marks them as available.
 * @param ipAddressStates IP Address states
 */
private DeferredResult<List<Operation>> markIPAddressesAsAvailable(List<IPAddressService.IPAddressState> ipAddressStates) {
    IPAddressService.IPAddressState addressState = new IPAddressService.IPAddressState();
    addressState.ipAddressStatus = IPAddressService.IPAddressState.IPAddressStatus.AVAILABLE;
    List<DeferredResult<Operation>> ipAddressOperations = new ArrayList<>();
    for (IPAddressService.IPAddressState ipAddressState : ipAddressStates) {
        Operation patchOp = Operation.createPatch(this, ipAddressState.documentSelfLink).setBody(addressState).setCompletion((o, e) -> {
            if (e != null) {
                logWarning("Failed to mark IP address resource %s as available due to failure %s", ipAddressState.ipAddress, e.getMessage());
            } else {
                logInfo("The IP address %s is made available", ipAddressState.ipAddress);
            }
        });
        ipAddressOperations.add(this.sendWithDeferredResult(patchOp));
    }
    return DeferredResult.allOf(ipAddressOperations);
}
Also used : IPAddressService(com.vmware.photon.controller.model.resources.IPAddressService) ArrayList(java.util.ArrayList) Operation(com.vmware.xenon.common.Operation) DeferredResult(com.vmware.xenon.common.DeferredResult)

Aggregations

DeferredResult (com.vmware.xenon.common.DeferredResult)77 ArrayList (java.util.ArrayList)57 Operation (com.vmware.xenon.common.Operation)52 List (java.util.List)49 Collectors (java.util.stream.Collectors)43 StatelessService (com.vmware.xenon.common.StatelessService)42 Utils (com.vmware.xenon.common.Utils)38 Map (java.util.Map)38 HashMap (java.util.HashMap)37 UriUtils (com.vmware.xenon.common.UriUtils)36 DiskService (com.vmware.photon.controller.model.resources.DiskService)33 PhotonModelUriUtils.createInventoryUri (com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri)33 URI (java.net.URI)33 Collection (java.util.Collection)32 Set (java.util.Set)31 HashSet (java.util.HashSet)30 DiskState (com.vmware.photon.controller.model.resources.DiskService.DiskState)29 QueryTask (com.vmware.xenon.services.common.QueryTask)28 Consumer (java.util.function.Consumer)28 OperationJoin (com.vmware.xenon.common.OperationJoin)26