Search in sources :

Example 6 with ComputeEnumerateResourceRequest

use of com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest in project photon-model by vmware.

the class VSphereIncrementalEnumerationService method handlePatch.

@Override
public void handlePatch(Operation patch) {
    // complete the patch immediately.
    patch.complete();
    logInfo("Received PATCH for incremental enumeration!");
    VSphereIncrementalEnumerationRequest enumerationRequest = patch.getBody(VSphereIncrementalEnumerationRequest.class);
    ComputeEnumerateResourceRequest request = enumerationRequest.request;
    URI parentUri = ComputeService.ComputeStateWithDescription.buildUri(PhotonModelUriUtils.createInventoryUri(getHost(), request.resourceReference));
    logInfo("Creating task manager!");
    TaskManager mgr = new TaskManager(this, request.taskReference, request.resourceLink());
    logInfo(" Requesting GET on compute state with description!.");
    Operation.createGet(parentUri).setCompletion(o -> {
        logInfo("Submitting job to threadpool!");
        VsphereEnumerationHelper.submitWorkToVSpherePool(this, () -> {
            logInfo("Incremental enumeration job started for endpoint %s", enumerationRequest.request.endpointLink);
            ComputeStateWithDescription computeStateWithDesc = o.getBody(ComputeStateWithDescription.class);
            VapiConnection vapiConnection = VapiConnection.createFromVimConnection(this.connection);
            logInfo("Establishing VAPI connection for endpoint %s", enumerationRequest.request.endpointLink);
            try {
                vapiConnection.login();
            } catch (IOException | RpcException rpce) {
                logWarning(() -> String.format("Cannot login into vAPI endpoint: %s", Utils.toString(rpce)));
                mgr.patchTaskToFailure(rpce);
                // self delete service so that full enumeration kicks in next invocation.
                selfDeleteService();
                return;
            }
            try {
                // Get instanceUuid of the vCenter
                AboutInfo vCenter = this.connection.getServiceContent().getAbout();
                for (CollectorDetails collectorDetails : this.collectors) {
                    logInfo("Retrieving  resources incremental data for data center: %s", collectorDetails.datacenter);
                    EnumerationProgress enumerationProgress = new EnumerationProgress(new HashSet<>(), request, computeStateWithDesc, vapiConnection, collectorDetails.datacenter, vCenter.getInstanceUuid());
                    EnumerationClient client = new EnumerationClient(this.connection, computeStateWithDesc, VimUtils.convertStringToMoRef(collectorDetails.datacenter));
                    List<ObjectUpdate> resourcesUpdates = collectResourcesData(collectorDetails);
                    List<ObjectUpdate> vmUpdates = collectVMData(collectorDetails);
                    logInfo("Received resources updates for datacenter: %s : %s", collectorDetails.datacenter, resourcesUpdates.size());
                    logInfo("Received vm updates for datacenter: %s : %s", collectorDetails.datacenter, vmUpdates.size());
                    logInfo("Resources Updates: %s", Utils.toJson(resourcesUpdates));
                    logInfo("VM Updates: %s", Utils.toJson(vmUpdates));
                    if (!resourcesUpdates.isEmpty()) {
                        SegregatedOverlays segregatedOverlays = segregateObjectUpdates(enumerationProgress, resourcesUpdates);
                        this.logInfo("Processing incremental changes for folders for datacenter [%s]!", collectorDetails.datacenter);
                        VsphereFolderEnumerationHelper.handleFolderChanges(this, segregatedOverlays.folders, enumerationProgress, client);
                        logInfo("Processing incremental changes for networks for datacenter [%s]!", collectorDetails.datacenter);
                        VSphereNetworkEnumerationHelper.handleNetworkChanges(this, segregatedOverlays.networks, enumerationProgress, client);
                        logInfo("Processing incremental changes for Datastores for datacenter [%s]!", collectorDetails.datacenter);
                        VsphereDatastoreEnumerationHelper.handleDatastoreChanges(this, segregatedOverlays.datastores, enumerationProgress);
                        logInfo("Processing incremental changes for compute resource for datacenter [%s]!", collectorDetails.datacenter);
                        VsphereComputeResourceEnumerationHelper.handleComputeResourceChanges(this, segregatedOverlays.clusters, enumerationProgress, client);
                        logInfo("Processing incremental changes for host system for datacenter [%s]!", collectorDetails.datacenter);
                        VSphereHostSystemEnumerationHelper.handleHostSystemChanges(this, segregatedOverlays.hosts, enumerationProgress, client);
                        logInfo("Processing incremental changes for resource pool for datacenter [%s]!", collectorDetails.datacenter);
                        VSphereResourcePoolEnumerationHelper.handleResourcePoolChanges(this, segregatedOverlays.resourcePools, enumerationProgress, client);
                    }
                    if (!vmUpdates.isEmpty()) {
                        logInfo("Processing incremental changes for virtual machines for datacenter [%s]!", collectorDetails.datacenter);
                        VSphereVirtualMachineEnumerationHelper.handleVMChanges(this, vmUpdates, enumerationProgress, client);
                    }
                    // sync storage profiles
                    logInfo("Syncing storage profiles for datacenter [%s]!", collectorDetails.datacenter);
                    VsphereStoragePolicyEnumerationHelper.syncStorageProfiles(this, client, enumerationProgress);
                }
                mgr.patchTask(TaskStage.FINISHED);
            } catch (Exception exception) {
                String msg = "Error processing PropertyCollector results during incremental retrieval";
                logWarning(() -> msg + ": " + exception.toString());
                mgr.patchTaskToFailure(exception);
                // self delete service so that full enumeration kicks in next invocation.
                // TODO: This is not complete. We need to enable owner selection on this service.
                selfDeleteService();
                return;
            } finally {
                vapiConnection.close();
            }
        });
    }, mgr).sendWith(this);
}
Also used : Arrays(java.util.Arrays) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) TagCache(com.vmware.photon.controller.model.adapters.vsphere.tagging.TagCache) QueryTask(com.vmware.xenon.services.common.QueryTask) ServiceDocument(com.vmware.xenon.common.ServiceDocument) ResourceGroupState(com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState) UpdateSet(com.vmware.vim25.UpdateSet) PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) Utils(com.vmware.xenon.common.Utils) VsphereEnumerationHelper.convertOnlyResultToDocument(com.vmware.photon.controller.model.adapters.vsphere.VsphereEnumerationHelper.convertOnlyResultToDocument) Map(java.util.Map) Connection(com.vmware.photon.controller.model.adapters.vsphere.util.connection.Connection) URI(java.net.URI) VapiConnection(com.vmware.photon.controller.model.adapters.vsphere.vapi.VapiConnection) SubnetService(com.vmware.photon.controller.model.resources.SubnetService) StatelessService(com.vmware.xenon.common.StatelessService) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) DatacenterLister(com.vmware.photon.controller.model.adapters.vsphere.util.finders.DatacenterLister) Set(java.util.Set) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) Occurance(com.vmware.xenon.services.common.QueryTask.Query.Occurance) Collectors(java.util.stream.Collectors) List(java.util.List) StorageDescriptionService(com.vmware.photon.controller.model.resources.StorageDescriptionService) DeferredResult(com.vmware.xenon.common.DeferredResult) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) VsphereEnumerationHelper.withTaskResults(com.vmware.photon.controller.model.adapters.vsphere.VsphereEnumerationHelper.withTaskResults) AboutInfo(com.vmware.vim25.AboutInfo) DiskService(com.vmware.photon.controller.model.resources.DiskService) MoRefKeyedMap(com.vmware.photon.controller.model.adapters.vsphere.util.MoRefKeyedMap) ComputeProperties(com.vmware.photon.controller.model.ComputeProperties) ResourceCleanRequest(com.vmware.photon.controller.model.adapters.vsphere.VsphereResourceCleanerService.ResourceCleanRequest) PhotonModelUriUtils(com.vmware.photon.controller.model.util.PhotonModelUriUtils) ObjectUpdateKind(com.vmware.vim25.ObjectUpdateKind) HashMap(java.util.HashMap) RuntimeFaultFaultMsg(com.vmware.vim25.RuntimeFaultFaultMsg) RpcException(com.vmware.photon.controller.model.adapters.vsphere.vapi.RpcException) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) HashSet(java.util.HashSet) EnumerationAction(com.vmware.photon.controller.model.adapterapi.EnumerationAction) TYPE_SERVER_DISK(com.vmware.photon.controller.model.adapters.vsphere.util.VimNames.TYPE_SERVER_DISK) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) Element(com.vmware.photon.controller.model.adapters.vsphere.util.finders.Element) Operation(com.vmware.xenon.common.Operation) ObjectUpdateIterator(com.vmware.photon.controller.model.adapters.vsphere.EnumerationClient.ObjectUpdateIterator) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) IOException(java.io.IOException) PropertyFilterUpdate(com.vmware.vim25.PropertyFilterUpdate) VimNames(com.vmware.photon.controller.model.adapters.vsphere.util.VimNames) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) TaskStage(com.vmware.xenon.common.TaskState.TaskStage) NetworkService(com.vmware.photon.controller.model.resources.NetworkService) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) ObjectUpdate(com.vmware.vim25.ObjectUpdate) OperationJoin(com.vmware.xenon.common.OperationJoin) VapiConnection(com.vmware.photon.controller.model.adapters.vsphere.vapi.VapiConnection) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) AboutInfo(com.vmware.vim25.AboutInfo) URI(java.net.URI) RpcException(com.vmware.photon.controller.model.adapters.vsphere.vapi.RpcException) IOException(java.io.IOException) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 7 with ComputeEnumerateResourceRequest

use of com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest in project photon-model by vmware.

the class VSphereNetworkEnumerationHelper method makeSubnetStateFromResults.

private static SubnetState makeSubnetStateFromResults(EnumerationProgress enumerationProgress, NetworkOverlay net) {
    ComputeEnumerateResourceRequest request = enumerationProgress.getRequest();
    SubnetState state = new SubnetState();
    state.id = state.name = net.getName();
    state.endpointLink = enumerationProgress.getRequest().endpointLink;
    AdapterUtils.addToEndpointLinks(state, enumerationProgress.getRequest().endpointLink);
    ManagedObjectReference parentSwitch = net.getParentSwitch();
    state.networkLink = buildStableDvsLink(parentSwitch, request.endpointLink);
    state.regionId = enumerationProgress.getRegionId();
    CustomProperties customProperties = CustomProperties.of(state).put(CustomProperties.MOREF, net.getId()).put(CustomProperties.DATACENTER_SELF_LINK, enumerationProgress.getDcLink()).put(CustomProperties.TYPE, net.getId().getType());
    customProperties.put(DvsProperties.PORT_GROUP_KEY, net.getPortgroupKey());
    return state;
}
Also used : ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 8 with ComputeEnumerateResourceRequest

use of com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest in project photon-model by vmware.

the class VSphereResourcePoolEnumerationHelper method makeResourcePoolFromResults.

private static ComputeState makeResourcePoolFromResults(EnumerationProgress enumerationProgress, ResourcePoolOverlay rp, String selfLink, EnumerationClient client) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
    ComputeEnumerateResourceRequest request = enumerationProgress.getRequest();
    ComputeState state = new ComputeState();
    state.documentSelfLink = selfLink;
    state.name = rp.getName();
    state.id = rp.getId().getValue();
    state.type = ComputeType.VM_HOST;
    state.powerState = PowerState.ON;
    state.endpointLink = request.endpointLink;
    AdapterUtils.addToEndpointLinks(state, request.endpointLink);
    state.regionId = enumerationProgress.getRegionId();
    state.parentLink = enumerationProgress.getRequest().resourceLink();
    state.resourcePoolLink = request.resourcePoolLink;
    state.adapterManagementReference = request.adapterManagementReference;
    ManagedObjectReference owner = rp.getOwner();
    AbstractOverlay ov = enumerationProgress.getOverlay(owner);
    if (ov instanceof ComputeResourceOverlay) {
        ComputeResourceOverlay cr = (ComputeResourceOverlay) ov;
        state.groupLinks = VsphereEnumerationHelper.getConnectedDatastoresAndNetworks(enumerationProgress, cr.getDatastore(), cr.getNetwork(), client);
    } else if (ov instanceof HostSystemOverlay) {
        HostSystemOverlay cr = (HostSystemOverlay) ov;
        state.groupLinks = VsphereEnumerationHelper.getConnectedDatastoresAndNetworks(enumerationProgress, cr.getDatastore(), cr.getNetwork(), client);
    }
    CustomProperties.of(state).put(CustomProperties.MOREF, rp.getId()).put(CustomProperties.DATACENTER_SELF_LINK, enumerationProgress.getDcLink()).put(CustomProperties.TYPE, VimNames.TYPE_RESOURCE_POOL);
    VsphereEnumerationHelper.populateResourceStateWithAdditionalProps(state, enumerationProgress.getVcUuid());
    return state;
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 9 with ComputeEnumerateResourceRequest

use of com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest in project photon-model by vmware.

the class VSphereResourcePoolEnumerationHelper method handleResourcePoolChanges.

public static void handleResourcePoolChanges(VSphereIncrementalEnumerationService service, List<ResourcePoolOverlay> resourcePools, EnumerationProgress enumerationProgress, EnumerationClient client) {
    ComputeEnumerateResourceRequest request = enumerationProgress.getRequest();
    enumerationProgress.expectResourcePoolCount(resourcePools.size());
    for (ResourcePoolOverlay resourcePool : resourcePools) {
        // no need to collect the root resource pool
        if (ObjectUpdateKind.ENTER.equals(resourcePool.getObjectUpdateKind()) && VimNames.TYPE_RESOURCE_POOL.equals(resourcePool.getParent().getType())) {
            String ownerMoRefId = resourcePool.getOwner().getValue();
            QueryTask task = queryForRPOwner(ownerMoRefId, enumerationProgress);
            String selfLink = buildStableResourcePoolLink(resourcePool.getId(), request.endpointLink);
            withTaskResults(service, task, result -> {
                try {
                    if (!result.documentLinks.isEmpty()) {
                        ComputeState ownerDocument = convertOnlyResultToDocument(result, ComputeState.class);
                        createNewResourcePool(service, enumerationProgress, ownerDocument.name, selfLink, resourcePool, client);
                    } else {
                        // This happens for the resource pools within Host. The owner is a ComputeResource and
                        // is not currently enumerated in photon
                        createNewResourcePool(service, enumerationProgress, null, selfLink, resourcePool, client);
                    }
                } catch (Exception e) {
                    enumerationProgress.getResourcePoolTracker().track();
                }
            });
        } else {
            String rpSelfLink = buildStableResourcePoolLink(resourcePool.getId(), request.endpointLink);
            Operation.createGet(PhotonModelUriUtils.createInventoryUri(service.getHost(), rpSelfLink)).setCompletion((o, e) -> {
                try {
                    if (e == null) {
                        ComputeState oldState = o.getBody(ComputeState.class);
                        String existingOwnerName = getOwnerNameFromResourcePoolName(oldState.name);
                        if (ObjectUpdateKind.MODIFY.equals(resourcePool.getObjectUpdateKind())) {
                            updateResourcePool(service, enumerationProgress, existingOwnerName, oldState.documentSelfLink, resourcePool, false, client);
                        } else {
                            Operation.createDelete(PhotonModelUriUtils.createInventoryUri(service.getHost(), rpSelfLink)).setCompletion(trackResourcePool(enumerationProgress, resourcePool)).sendWith(service);
                        }
                    } else {
                        enumerationProgress.getResourcePoolTracker().track();
                    }
                } catch (Exception ex) {
                    enumerationProgress.getResourcePoolTracker().track();
                }
            }).sendWith(service);
        }
    }
    try {
        enumerationProgress.getResourcePoolTracker().await();
    } catch (InterruptedException e) {
        service.logSevere("Interrupted during incremental enumeration for resource pools!", e);
    }
}
Also used : PowerState(com.vmware.photon.controller.model.resources.ComputeService.PowerState) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) PhotonModelUriUtils(com.vmware.photon.controller.model.util.PhotonModelUriUtils) QueryTask(com.vmware.xenon.services.common.QueryTask) ObjectUpdateKind(com.vmware.vim25.ObjectUpdateKind) ComputeDescriptionService(com.vmware.photon.controller.model.resources.ComputeDescriptionService) RuntimeFaultFaultMsg(com.vmware.vim25.RuntimeFaultFaultMsg) ComputeType(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) VsphereEnumerationHelper.convertOnlyResultToDocument(com.vmware.photon.controller.model.adapters.vsphere.VsphereEnumerationHelper.convertOnlyResultToDocument) ServiceNotFoundException(com.vmware.xenon.common.ServiceHost.ServiceNotFoundException) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) VimNames(com.vmware.photon.controller.model.adapters.vsphere.util.VimNames) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) List(java.util.List) UriUtils.buildUriPath(com.vmware.xenon.common.UriUtils.buildUriPath) InvalidPropertyFaultMsg(com.vmware.vim25.InvalidPropertyFaultMsg) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) UriUtils(com.vmware.xenon.common.UriUtils) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) VsphereEnumerationHelper.withTaskResults(com.vmware.photon.controller.model.adapters.vsphere.VsphereEnumerationHelper.withTaskResults) Collections(java.util.Collections) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) QueryTask(com.vmware.xenon.services.common.QueryTask) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) ServiceNotFoundException(com.vmware.xenon.common.ServiceHost.ServiceNotFoundException)

Example 10 with ComputeEnumerateResourceRequest

use of com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest in project photon-model by vmware.

the class VSphereVMSnapshotEnumerationHelper method enumerateSnapshots.

static void enumerateSnapshots(VSphereIncrementalEnumerationService service, EnumerationProgress enumerationProgress, List<VmOverlay> vms) {
    if (CollectionUtils.isEmpty(vms)) {
        return;
    }
    vms.forEach(vm -> {
        // process only those VMs that have snapshots attached.
        if (vm.getRootSnapshotList() != null) {
            ComputeEnumerateResourceRequest request = enumerationProgress.getRequest();
            QueryTask task = VSphereVirtualMachineEnumerationHelper.queryForVm(enumerationProgress, request.resourceLink(), vm.getInstanceUuid(), null);
            VsphereEnumerationHelper.withTaskResults(service, task, result -> {
                ComputeState computeState = VsphereEnumerationHelper.convertOnlyResultToDocument(result, ComputeState.class);
                processSnapshots(service, enumerationProgress, vm, computeState.documentSelfLink);
            });
        }
    });
    enumerationProgress.getSnapshotTracker().arriveAndDeregister();
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) QueryTask(com.vmware.xenon.services.common.QueryTask) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest)

Aggregations

ComputeEnumerateResourceRequest (com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest)29 QueryTask (com.vmware.xenon.services.common.QueryTask)16 Operation (com.vmware.xenon.common.Operation)12 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)11 List (java.util.List)11 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)10 PhotonModelUriUtils (com.vmware.photon.controller.model.util.PhotonModelUriUtils)10 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)10 Utils (com.vmware.xenon.common.Utils)10 ResourceState (com.vmware.photon.controller.model.resources.ResourceState)9 VsphereEnumerationHelper.convertOnlyResultToDocument (com.vmware.photon.controller.model.adapters.vsphere.VsphereEnumerationHelper.convertOnlyResultToDocument)8 VsphereEnumerationHelper.withTaskResults (com.vmware.photon.controller.model.adapters.vsphere.VsphereEnumerationHelper.withTaskResults)8 ObjectUpdateKind (com.vmware.vim25.ObjectUpdateKind)8 CompletionHandler (com.vmware.xenon.common.Operation.CompletionHandler)8 UriUtils (com.vmware.xenon.common.UriUtils)8 Map (java.util.Map)8 EnumerationAction (com.vmware.photon.controller.model.adapterapi.EnumerationAction)7 AdapterUtils (com.vmware.photon.controller.model.adapters.util.AdapterUtils)7 VimNames (com.vmware.photon.controller.model.adapters.vsphere.util.VimNames)7 ResourceGroupState (com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState)7