Search in sources :

Example 46 with ServiceDocumentQueryResult

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

the class VSphereVMSnapshotEnumerationHelper method processSnapshot.

static void processSnapshot(VSphereIncrementalEnumerationService service, VirtualMachineSnapshotTree current, String parentLink, EnumerationProgress enumerationProgress, VmOverlay vm, String vmSelfLink) {
    enumerationProgress.getSnapshotTracker().register();
    QueryTask task = queryForSnapshot(enumerationProgress, current.getId().toString(), vmSelfLink);
    VsphereEnumerationHelper.withTaskResults(service, task, (ServiceDocumentQueryResult result) -> {
        VsphereEnumerationHelper.submitWorkToVSpherePool(service, () -> {
            SnapshotState snapshotState = constructSnapshot(service, current, parentLink, vmSelfLink, enumerationProgress, vm);
            if (result.documentLinks.isEmpty()) {
                VSphereVirtualMachineEnumerationHelper.createSnapshot(service, snapshotState).thenCompose(createdSnapshotState -> trackAndProcessChildSnapshots(service, current, enumerationProgress, vm, vmSelfLink, createdSnapshotState)).whenComplete((ss, e) -> {
                    if (e != null) {
                        service.log(Level.SEVERE, "Creation of snapshot with name {%s} failed.", snapshotState.name);
                    }
                    enumerationProgress.getSnapshotTracker().arrive();
                });
            } else {
                SnapshotState oldState = VsphereEnumerationHelper.convertOnlyResultToDocument(result, SnapshotState.class);
                updateSnapshot(service, enumerationProgress, vm, oldState, snapshotState, current.getId().toString()).thenCompose(updatedSnapshotState -> trackAndProcessChildSnapshots(service, current, enumerationProgress, vm, vmSelfLink, updatedSnapshotState)).whenComplete((ss, e) -> {
                    if (e != null) {
                        service.logSevere("Updating of snapshot with name {%s}, selfLink {%s} failed", snapshotState.name, oldState.documentSelfLink);
                    }
                    enumerationProgress.getSnapshotTracker().arrive();
                });
            }
        });
    });
}
Also used : ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) Operation(com.vmware.xenon.common.Operation) QueryTask(com.vmware.xenon.services.common.QueryTask) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) SnapshotState(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState) Level(java.util.logging.Level) HashSet(java.util.HashSet) List(java.util.List) CollectionUtils(org.apache.commons.collections.CollectionUtils) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) VirtualMachineSnapshotTree(com.vmware.vim25.VirtualMachineSnapshotTree) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) QueryTask(com.vmware.xenon.services.common.QueryTask) SnapshotState(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult)

Example 47 with ServiceDocumentQueryResult

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

the class VsphereFolderEnumerationHelper method processFoundFolder.

static void processFoundFolder(VSphereIncrementalEnumerationService service, EnumerationProgress ctx, FolderOverlay folder, List<FolderOverlay> rootFolders, EnumerationClient client) {
    QueryTask task = queryForFolder(ctx, folder);
    withTaskResults(service, task, (ServiceDocumentQueryResult result) -> {
        try {
            if (result.documentLinks.isEmpty()) {
                createFolder(service, ctx, folder, rootFolders, client);
            } else {
                ResourceGroupState oldDocument = convertOnlyResultToDocument(result, ResourceGroupState.class);
                updateFolder(service, ctx, folder, oldDocument, rootFolders, client, true);
            }
        } catch (Exception e) {
            service.logSevere("Error occurred while processing folder!", e);
            ctx.getFolderTracker().track(folder.getId(), ResourceTracker.ERROR);
        }
    });
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) ResourceGroupState(com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult)

Example 48 with ServiceDocumentQueryResult

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

the class VsphereStoragePolicyEnumerationHelper method updateStorageDescription.

static void updateStorageDescription(VSphereIncrementalEnumerationService service, Stream<Operation> opStream, String spSelfLink, ServiceDocumentQueryResult result) {
    List<Operation> patchOps = new ArrayList<>();
    List<String> originalLinks = new ArrayList<>();
    if (result.documentLinks != null) {
        originalLinks.addAll(result.documentLinks);
    }
    opStream.forEach(op -> {
        StorageDescription storageDescription = op.getBody(StorageDescription.class);
        if (result.documentLinks != null && result.documentLinks.contains(storageDescription.documentSelfLink)) {
            originalLinks.remove(storageDescription.documentSelfLink);
        } else {
            if (storageDescription.groupLinks == null) {
                storageDescription.groupLinks = new HashSet<>();
            }
            storageDescription.groupLinks.add(spSelfLink);
            patchOps.add(Operation.createPatch(PhotonModelUriUtils.createInventoryUri(service.getHost(), storageDescription.documentSelfLink)).setBody(storageDescription));
        }
    });
    // In this case, we need to update the datastore by removing the policy group link
    if (!originalLinks.isEmpty()) {
        originalLinks.stream().forEach(link -> {
            Map<String, Collection<Object>> collectionsToRemove = Collections.singletonMap(ResourceState.FIELD_NAME_GROUP_LINKS, Collections.singletonList(spSelfLink));
            ServiceStateCollectionUpdateRequest updateGroupLinksRequest = ServiceStateCollectionUpdateRequest.create(null, collectionsToRemove);
            patchOps.add(Operation.createPatch(PhotonModelUriUtils.createInventoryUri(service.getHost(), link)).setBody(updateGroupLinksRequest));
        });
    }
    if (!patchOps.isEmpty()) {
        OperationJoin.create(patchOps).setCompletion((ops, exs) -> {
            if (exs != null) {
                service.logFine(() -> String.format("Syncing Storage policy failed %s", Utils.toString(exs)));
            }
        }).sendWith(service);
    }
}
Also used : ComputeProperties(com.vmware.photon.controller.model.ComputeProperties) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) PhotonModelUriUtils(com.vmware.photon.controller.model.util.PhotonModelUriUtils) QueryTask(com.vmware.xenon.services.common.QueryTask) ResourceGroupState(com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState) MatchType(com.vmware.xenon.services.common.QueryTask.QueryTerm.MatchType) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Utils(com.vmware.xenon.common.Utils) ServiceStateCollectionUpdateRequest(com.vmware.xenon.common.ServiceStateCollectionUpdateRequest) PbmProfile(com.vmware.pbm.PbmProfile) Map(java.util.Map) StorageDescription(com.vmware.photon.controller.model.resources.StorageDescriptionService.StorageDescription) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) Collection(java.util.Collection) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) Occurance(com.vmware.xenon.services.common.QueryTask.Query.Occurance) VimNames(com.vmware.photon.controller.model.adapters.vsphere.util.VimNames) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) ResourceGroupService(com.vmware.photon.controller.model.resources.ResourceGroupService) List(java.util.List) Stream(java.util.stream.Stream) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) Collections(java.util.Collections) OperationJoin(com.vmware.xenon.common.OperationJoin) ArrayList(java.util.ArrayList) Collection(java.util.Collection) ServiceStateCollectionUpdateRequest(com.vmware.xenon.common.ServiceStateCollectionUpdateRequest) Operation(com.vmware.xenon.common.Operation) StorageDescription(com.vmware.photon.controller.model.resources.StorageDescriptionService.StorageDescription)

Example 49 with ServiceDocumentQueryResult

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

the class VSphereNetworkEnumerationHelper method processFoundNetwork.

public static void processFoundNetwork(VSphereIncrementalEnumerationService service, EnumerationProgress enumerationProgress, NetworkOverlay net, MoRefKeyedMap<NetworkOverlay> allNetworks) {
    if (net.getParentSwitch() != null) {
        // portgroup: create subnet
        QueryTask task = queryForSubnet(enumerationProgress, net, net.getParentSwitch());
        withTaskResults(service, task, (ServiceDocumentQueryResult result) -> {
            if (result.documentLinks.isEmpty()) {
                createNewSubnet(service, enumerationProgress, net, allNetworks.get(net.getParentSwitch()).getDvsUuid());
            } else {
                SubnetState oldDocument = convertOnlyResultToDocument(result, SubnetState.class);
                updateSubnet(service, oldDocument, enumerationProgress, net, true);
            }
        });
    } else {
        // DVS or opaque network
        QueryTask task = queryForNetwork(enumerationProgress, net);
        withTaskResults(service, task, result -> {
            if (result.documentLinks.isEmpty()) {
                createNewNetwork(service, enumerationProgress, net);
            } else {
                NetworkState oldDocument = convertOnlyResultToDocument(result, NetworkState.class);
                updateNetwork(service, oldDocument, enumerationProgress, net);
            }
        });
    }
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult)

Example 50 with ServiceDocumentQueryResult

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

the class VSphereNetworkEnumerationHelper method handleNetworkChanges.

/**
 * Handles incremental changes on networks.
 *
 * @param service             the incremental service
 * @param networks            the filtered network overlays
 * @param enumerationProgress the context for enumeration.
 * @param client              the enumeration client
 */
public static void handleNetworkChanges(VSphereIncrementalEnumerationService service, MoRefKeyedMap<NetworkOverlay> networks, EnumerationProgress enumerationProgress, EnumerationClient client) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
    enumerationProgress.expectNetworkCount(networks.values().size());
    for (NetworkOverlay netOverlay : networks.values()) {
        // if a network | DVS | DV port group is added.
        if (ObjectUpdateKind.ENTER == netOverlay.getObjectUpdateKind()) {
            // if DV port group is added
            if (netOverlay.getParentSwitch() != null) {
                // check if the parent switch is present as part of this enumeration
                NetworkOverlay parentSwitch = networks.get(netOverlay.getParentSwitch());
                String dvsUUID;
                if (parentSwitch == null) {
                    // retrieve the uuid from vCenter
                    dvsUUID = client.getUUIDForDVS(netOverlay);
                } else {
                    dvsUUID = parentSwitch.getDvsUuid();
                }
                createNewSubnet(service, enumerationProgress, netOverlay, dvsUUID);
            } else {
                createNewNetwork(service, enumerationProgress, netOverlay);
            }
        } else {
            // if DV port group is changed
            if (netOverlay.getId().getType().equals(VimNames.TYPE_PORTGROUP)) {
                ManagedObjectReference parentSwitch = netOverlay.getParentSwitch();
                // if parent is not retrieved, Retrieve it.
                if (null == parentSwitch && ObjectUpdateKind.MODIFY.equals(netOverlay.getObjectUpdateKind())) {
                    parentSwitch = client.getParentSwitchForDVPortGroup(netOverlay.getId());
                }
                QueryTask task = queryForSubnet(enumerationProgress, netOverlay, parentSwitch);
                withTaskResults(service, task, (ServiceDocumentQueryResult result) -> {
                    if (!result.documentLinks.isEmpty()) {
                        SubnetState oldDocument = convertOnlyResultToDocument(result, SubnetState.class);
                        if (ObjectUpdateKind.MODIFY.equals(netOverlay.getObjectUpdateKind())) {
                            updateSubnet(service, oldDocument, enumerationProgress, netOverlay, false);
                        } else {
                            // DV port group has been removed. Remove the subnet state document.
                            deleteNetwork(service, enumerationProgress, netOverlay, oldDocument);
                        }
                    } else {
                        enumerationProgress.getNetworkTracker().track();
                    }
                });
            } else {
                // DVS or Opaque network.
                QueryTask task = queryForNetwork(enumerationProgress, netOverlay);
                VsphereEnumerationHelper.withTaskResults(service, task, result -> {
                    if (!result.documentLinks.isEmpty()) {
                        NetworkState oldDocument = convertOnlyResultToDocument(result, NetworkState.class);
                        if (ObjectUpdateKind.MODIFY.equals(netOverlay.getObjectUpdateKind())) {
                            updateNetwork(service, oldDocument, enumerationProgress, netOverlay);
                        } else {
                            deleteNetwork(service, enumerationProgress, netOverlay, oldDocument);
                        }
                    } else {
                        enumerationProgress.getNetworkTracker().track();
                    }
                });
            }
        }
    }
    try {
        enumerationProgress.getNetworkTracker().await();
    } catch (InterruptedException e) {
        service.logSevere("Interrupted during incremental enumeration for networks!", e);
    }
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Aggregations

ServiceDocumentQueryResult (com.vmware.xenon.common.ServiceDocumentQueryResult)64 Test (org.junit.Test)26 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)25 ArrayList (java.util.ArrayList)15 QueryTask (com.vmware.xenon.services.common.QueryTask)14 EndpointState (com.vmware.photon.controller.model.resources.EndpointService.EndpointState)13 Operation (com.vmware.xenon.common.Operation)13 ComputeDescription (com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription)12 DiskState (com.vmware.photon.controller.model.resources.DiskService.DiskState)12 HashMap (java.util.HashMap)12 BaseModelTest (com.vmware.photon.controller.model.helpers.BaseModelTest)11 ResourcePoolState (com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState)11 URI (java.net.URI)11 Map (java.util.Map)11 List (java.util.List)10 StatsCollectionTaskState (com.vmware.photon.controller.model.tasks.monitoring.StatsCollectionTaskService.StatsCollectionTaskState)8 UriUtils (com.vmware.xenon.common.UriUtils)8 Utils (com.vmware.xenon.common.Utils)8 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)7 ServiceStat (com.vmware.xenon.common.ServiceStats.ServiceStat)7