use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method collectRegions.
private void collectRegions(EnumerationContext ctx, ComputeEnumerationSubStages next) {
Operation getEndpoint = Operation.createGet(this, ctx.request.endpointLink);
DeferredResult<EndpointState> getEndpointDR = sendWithDeferredResult(getEndpoint, EndpointState.class);
getEndpointDR.thenCompose(endpoint -> {
Operation getRegions = Operation.createPost(this, AzureRegionEnumerationAdapterService.SELF_LINK).setBody(endpoint);
return sendWithDeferredResult(getRegions, RegionEnumerationResponse.class);
}).whenComplete((regionsResponse, e) -> {
if (e != null) {
logWarning("Resource enumeration failed at stage %s with exception %s", ctx.stage, Utils.toString(e));
handleError(ctx, e);
} else {
ctx.regions.putAll(regionsResponse.regions.stream().collect(Collectors.toMap(r -> r.regionId, r -> r)));
ctx.regionIds.addAll(ctx.regions.keySet());
ctx.subStage = next;
handleSubStage(ctx);
}
});
}
use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method doCreateUpdateNics.
/**
* Manages creating and updating Network Interfaces resources based on network interfaces
* associated with virtual machines.
*/
private DeferredResult<List<NetworkInterfaceState>> doCreateUpdateNics(EnumerationContext ctx, Map<String, String> subnetPerNicId, List<Pair<NetworkInterfaceInner, String>> remoteNics) {
Map<String, Pair<NetworkInterfaceInner, String>> remoteStates = remoteNics.stream().filter(p -> p.getLeft() != null).collect(Collectors.toMap(p -> p.getLeft().id(), p -> p));
Query.Builder qBuilder = Query.Builder.create().addKindFieldClause(NetworkInterfaceState.class).addInClause(NetworkInterfaceState.FIELD_NAME_ID, remoteStates.keySet());
QueryByPages<NetworkInterfaceState> queryLocalStates = new QueryByPages<>(getHost(), qBuilder.build(), NetworkInterfaceState.class, ctx.parentCompute.tenantLinks, // endpointLink
null, ctx.parentCompute.documentSelfLink).setMaxPageSize(QueryUtils.MAX_RESULT_LIMIT).setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
return queryLocalStates.collectDocuments(Collectors.toList()).thenCompose(localNics -> requestCreateUpdateNic(localNics, remoteStates, ctx, subnetPerNicId, remoteNics));
}
use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method createNetworkInterfaceStates.
/**
* Create network interface states for each VM
*/
private void createNetworkInterfaceStates(EnumerationContext ctx, ComputeEnumerationSubStages next) {
Consumer<Throwable> failure = e -> {
logWarning("Failure getting Azure network interface states [endpointLink:%s] [Exception:%s]", ctx.request.endpointLink, e.getMessage());
handleError(ctx, e);
};
PhotonModelUtils.runInExecutor(this.executorService, () -> {
Azure azureClient = ctx.azureSdkClients.getAzureClient();
NetworkInterfacesInner netOps = azureClient.networkInterfaces().inner();
List<DeferredResult<Pair<NetworkInterfaceInner, String>>> remoteNics = ctx.virtualMachines.values().stream().filter(vm -> vm.networkProfile() != null && !vm.networkProfile().networkInterfaces().isEmpty()).flatMap(vm -> vm.networkProfile().networkInterfaces().stream().map(nic -> Pair.of(nic, vm.id()))).map(pair -> loadRemoteNic(pair, netOps)).collect(Collectors.toList());
DeferredResult.allOf(remoteNics).thenCompose(rnics -> loadSubnets(ctx, rnics).thenCompose(subnetPerNicId -> doCreateUpdateNics(ctx, subnetPerNicId, rnics))).whenComplete(thenHandleSubStage(ctx, next));
}, failure);
}
use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method loadSubnets.
private DeferredResult<Map<String, String>> loadSubnets(EnumerationContext ctx, List<Pair<NetworkInterfaceInner, String>> remoteNics) {
Map<String, List<Pair<NetworkInterfaceInner, String>>> nicsPerSubnet = remoteNics.stream().filter(p -> p.getLeft() != null && p.getLeft().ipConfigurations() != null && !p.getLeft().ipConfigurations().isEmpty() && p.getLeft().ipConfigurations().get(0).subnet() != null).collect(java.util.stream.Collectors.groupingBy(p -> p.getLeft().ipConfigurations().get(0).subnet().id()));
Query.Builder qBuilder = Query.Builder.create().addKindFieldClause(SubnetState.class).addInClause(NetworkInterfaceState.FIELD_NAME_ID, nicsPerSubnet.keySet().stream().collect(Collectors.toList()));
QueryByPages<SubnetState> queryLocalStates = new QueryByPages<>(getHost(), qBuilder.build(), SubnetState.class, ctx.parentCompute.tenantLinks, // endpointLink
null, ctx.parentCompute.documentSelfLink).setMaxPageSize(QueryUtils.MAX_RESULT_LIMIT).setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
Map<String, String> subnetLinkPerNicId = new HashMap<>();
return queryLocalStates.queryDocuments(subnet -> nicsPerSubnet.get(subnet.id).forEach(p -> subnetLinkPerNicId.put(p.getLeft().id(), subnet.documentSelfLink))).thenApply(ignore -> subnetLinkPerNicId);
}
use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method disassociateNics.
/**
* Deletes stale network interface states that are deleted from the remote.
*/
private DeferredResult<List<Operation>> disassociateNics(EnumerationContext ctx, List<String> remoteNicIds, List<NetworkInterfaceState> allLocalNics) {
List<DeferredResult<Operation>> updateOps = new ArrayList<>();
allLocalNics.stream().filter(localNic -> !remoteNicIds.contains(localNic.id)).forEach(localNic -> {
Operation upOp = PhotonModelUtils.createRemoveEndpointLinksOperation(this, ctx.request.endpointLink, localNic);
if (upOp != null) {
CompletionHandler completionHandler = upOp.getCompletion();
upOp.setCompletion(null);
updateOps.add(sendWithDeferredResult(upOp).whenComplete(completionHandler::handle));
}
});
return DeferredResult.allOf(updateOps);
}
Aggregations