Search in sources :

Example 6 with IPAddressState

use of com.vmware.photon.controller.model.resources.IPAddressService.IPAddressState 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 7 with IPAddressState

use of com.vmware.photon.controller.model.resources.IPAddressService.IPAddressState 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 8 with IPAddressState

use of com.vmware.photon.controller.model.resources.IPAddressService.IPAddressState in project photon-model by vmware.

the class IPAddressServiceTest method buildValidStartState.

private static IPAddressState buildValidStartState() {
    IPAddressState ipAddressState = new IPAddressState();
    ipAddressState.id = UUID.randomUUID().toString();
    ipAddressState.name = "ipAddress";
    ipAddressState.ipAddress = "192.130.120.110";
    ipAddressState.ipVersion = IPVersion.IPv4;
    ipAddressState.ipAddressStatus = IPAddressStatus.ALLOCATED;
    ipAddressState.subnetRangeLink = SubnetRangeService.FACTORY_LINK + "/subnet-range-1";
    ipAddressState.tenantLinks = new ArrayList<>();
    ipAddressState.tenantLinks.add("tenant-linkA");
    ipAddressState.connectedResourceLink = ComputeService.FACTORY_LINK + "/machine-1";
    return ipAddressState;
}
Also used : IPAddressState(com.vmware.photon.controller.model.resources.IPAddressService.IPAddressState)

Example 9 with IPAddressState

use of com.vmware.photon.controller.model.resources.IPAddressService.IPAddressState in project photon-model by vmware.

the class IPAddressAllocationTaskService method addResultToState.

private void addResultToState(IPAddressAllocationContext context, IPAddressAllocationTaskState state) {
    state.subnetLink = context.subnetState.documentSelfLink;
    List<String> ctxtSubnetRangeLinks = context.subnetRangeStates.stream().map(s -> s.documentSelfLink).collect(toList());
    state.subnetRangeLinks.addAll(ctxtSubnetRangeLinks);
    for (String connectedResource : context.connectedResourceToAllocatedIpsMap.keySet()) {
        // Multiple IPs per connected resource
        List<IPAddressState> addressStates = context.connectedResourceToAllocatedIpsMap.get(connectedResource);
        List<String> ipAddressSelfLinks = new ArrayList<>();
        for (IPAddressState ipAddressState : addressStates) {
            ipAddressSelfLinks.add(ipAddressState.documentSelfLink);
            state.ipAddresses.add(ipAddressState.ipAddress);
            state.ipAddressLinks.add(ipAddressState.documentSelfLink);
        }
        state.rsrcToAllocatedIpsMap.put(connectedResource, ipAddressSelfLinks);
    }
}
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)

Example 10 with IPAddressState

use of com.vmware.photon.controller.model.resources.IPAddressService.IPAddressState 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

IPAddressState (com.vmware.photon.controller.model.resources.IPAddressService.IPAddressState)10 SubnetRangeState (com.vmware.photon.controller.model.resources.SubnetRangeService.SubnetRangeState)7 SubnetState (com.vmware.photon.controller.model.resources.SubnetService.SubnetState)5 DeferredResult (com.vmware.xenon.common.DeferredResult)5 ArrayList (java.util.ArrayList)5 Since (com.esotericsoftware.kryo.serializers.VersionFieldSerializer.Since)4 UriPaths (com.vmware.photon.controller.model.UriPaths)4 ReleaseConstants (com.vmware.photon.controller.model.constants.ReleaseConstants)4 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)4 IPAddressService (com.vmware.photon.controller.model.resources.IPAddressService)4 IPAddressStatus (com.vmware.photon.controller.model.resources.IPAddressService.IPAddressState.IPAddressStatus)4 PhotonModelUtils (com.vmware.photon.controller.model.resources.util.PhotonModelUtils)4 IPVersion (com.vmware.photon.controller.model.support.IPVersion)4 ServiceTaskCallbackResponse (com.vmware.photon.controller.model.tasks.ServiceTaskCallback.ServiceTaskCallbackResponse)4 ServiceTypeCluster (com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster)4 IpHelper (com.vmware.photon.controller.model.util.IpHelper)4 SubnetValidator (com.vmware.photon.controller.model.util.SubnetValidator)4 Operation (com.vmware.xenon.common.Operation)4 Service (com.vmware.xenon.common.Service)4 ServiceDocument (com.vmware.xenon.common.ServiceDocument)4