Search in sources :

Example 76 with DeferredResult

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

the class ResourceIPDeallocationTaskService method startDeallocatingIPs.

private void startDeallocatingIPs(ResourceIPDeallocationTaskState state, ResourceIPDeallocationContext ctx, String subTaskLink) {
    if (subTaskLink == null) {
        createSubTaskCallback(state, ctx, link -> startDeallocatingIPs(state, ctx, link));
        return;
    }
    logFine("Starting deallocate of (%s) IPs for compute resource [%s] using sub task %s", ctx.networkInterfaceToIPAddressMap.size(), ctx.resourceLink, subTaskLink);
    List<DeferredResult<Operation>> deallocateOperationDRs = new ArrayList<>();
    ctx.networkInterfaceToIPAddressMap.keySet().forEach(networkInterfaceLink -> {
        String ipAddressLink = ctx.networkInterfaceToIPAddressMap.get(networkInterfaceLink);
        IPAddressAllocationTaskState deallocationTaskState = new IPAddressAllocationTaskState();
        deallocationTaskState.serviceTaskCallback = ServiceTaskCallback.create(UriUtils.buildUri(getHost(), subTaskLink));
        deallocationTaskState.serviceTaskCallback.onSuccessFinishTask();
        // Similar to instance deletes - a failure for one network interface deallocate will fail the task
        deallocationTaskState.serviceTaskCallback.onErrorFailTask();
        deallocationTaskState.requestType = IPAddressAllocationTaskState.RequestType.DEALLOCATE;
        deallocationTaskState.connectedResourceLink = networkInterfaceLink;
        deallocationTaskState.ipAddressLinks = new ArrayList<>();
        deallocationTaskState.ipAddressLinks.add(ipAddressLink);
        deallocationTaskState.tenantLinks = state.tenantLinks;
        Operation deallocateNisOperation = Operation.createPost(this, IPAddressAllocationTaskService.FACTORY_LINK).setBody(deallocationTaskState);
        DeferredResult<Operation> deallocateOperationDR = sendWithDeferredResult(deallocateNisOperation);
        deallocateOperationDRs.add(deallocateOperationDR);
    });
    DeferredResult.allOf(deallocateOperationDRs).exceptionally(t -> {
        String msg = "Failure deallocating IP addresses for a computeResource: [%s]";
        logWarning(msg, state.resourceLink, t.getMessage());
        failTask(t, msg, state.resourceLink);
        return null;
    });
}
Also used : IPAddressAllocationTaskState(com.vmware.photon.controller.model.tasks.IPAddressAllocationTaskService.IPAddressAllocationTaskState) ArrayList(java.util.ArrayList) Operation(com.vmware.xenon.common.Operation) DeferredResult(com.vmware.xenon.common.DeferredResult)

Example 77 with DeferredResult

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

the class IPAddressAllocationTaskService method allocateIpInRange.

private DeferredResult<IPAddressAllocationContext> allocateIpInRange(IPAddressState ipAddressState, String connectedResourceLink, IPAddressAllocationContext context) {
    // Found a record for the specified IP
    if (IPAddressStatus.AVAILABLE.equals(ipAddressState.ipAddressStatus)) {
        IPAddressState patchedState = new IPAddressState();
        patchedState.ipAddressStatus = IPAddressStatus.ALLOCATED;
        patchedState.connectedResourceLink = connectedResourceLink;
        return sendWithDeferredResult(Operation.createPatch(this, ipAddressState.documentSelfLink).setBody(patchedState)).thenAccept(oper -> {
            logInfo("Allocated IP address %s within range %s to resource %s", ipAddressState.ipAddress, context.subnetRangeState.name, connectedResourceLink);
            addIpToContext(context, connectedResourceLink, ipAddressState);
        }).thenApply(oper -> context);
    } else {
        if (connectedResourceLink.equals(ipAddressState.connectedResourceLink)) {
            logInfo("IP address '%s' is already allocated to the same resource [%s]", ipAddressState.ipAddress, connectedResourceLink);
            addIpToContext(context, connectedResourceLink, ipAddressState);
            return DeferredResult.completed(context);
        } else {
            String errMsg = String.format("IP address '%s' is already allocated to a different resource [%s]", ipAddressState.ipAddress, ipAddressState.connectedResourceLink);
            logSevere(errMsg);
            return DeferredResult.failed(new IllegalArgumentException(errMsg));
        }
    }
}
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)

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