Search in sources :

Example 56 with ObjectSpec

use of com.vmware.vim25.ObjectSpec in project cloudstack by apache.

the class VmwareClient method waitForValues.

/**
 * Handle Updates for a single object. waits till expected values of
 * properties to check are reached Destroys the ObjectFilter when done.
 *
 * @param objmor
 *            MOR of the Object to wait for
 * @param filterProps
 *            Properties list to filter
 * @param endWaitProps
 *            Properties list to check for expected values these be
 *            properties of a property in the filter properties list
 * @param expectedVals
 *            values for properties to end the wait
 * @return true indicating expected values were met, and false otherwise
 * @throws RuntimeFaultFaultMsg
 * @throws InvalidPropertyFaultMsg
 * @throws InvalidCollectorVersionFaultMsg
 */
private synchronized Object[] waitForValues(ManagedObjectReference objmor, String[] filterProps, String[] endWaitProps, Object[][] expectedVals) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg, InvalidCollectorVersionFaultMsg {
    // version string is initially null
    String version = "";
    Object[] endVals = new Object[endWaitProps.length];
    Object[] filterVals = new Object[filterProps.length];
    String stateVal = null;
    PropertyFilterSpec spec = new PropertyFilterSpec();
    ObjectSpec oSpec = new ObjectSpec();
    oSpec.setObj(objmor);
    oSpec.setSkip(Boolean.FALSE);
    spec.getObjectSet().add(oSpec);
    PropertySpec pSpec = new PropertySpec();
    pSpec.getPathSet().addAll(Arrays.asList(filterProps));
    pSpec.setType(objmor.getType());
    spec.getPropSet().add(pSpec);
    ManagedObjectReference propertyCollector = getPropCol();
    ManagedObjectReference filterSpecRef = vimPort.createFilter(propertyCollector, spec, true);
    boolean reached = false;
    UpdateSet updateset = null;
    List<PropertyFilterUpdate> filtupary = null;
    List<ObjectUpdate> objupary = null;
    List<PropertyChange> propchgary = null;
    while (!reached) {
        updateset = vimPort.waitForUpdatesEx(propertyCollector, version, new WaitOptions());
        if (updateset == null || updateset.getFilterSet() == null) {
            continue;
        }
        version = updateset.getVersion();
        // Make this code more general purpose when PropCol changes later.
        filtupary = updateset.getFilterSet();
        for (PropertyFilterUpdate filtup : filtupary) {
            objupary = filtup.getObjectSet();
            for (ObjectUpdate objup : objupary) {
                if (objup.getKind() == ObjectUpdateKind.MODIFY || objup.getKind() == ObjectUpdateKind.ENTER || objup.getKind() == ObjectUpdateKind.LEAVE) {
                    propchgary = objup.getChangeSet();
                    for (PropertyChange propchg : propchgary) {
                        updateValues(endWaitProps, endVals, propchg);
                        updateValues(filterProps, filterVals, propchg);
                    }
                }
            }
        }
        Object expctdval = null;
        // Also exit the WaitForUpdates loop if this is the case.
        for (int chgi = 0; chgi < endVals.length && !reached; chgi++) {
            for (int vali = 0; vali < expectedVals[chgi].length && !reached; vali++) {
                expctdval = expectedVals[chgi][vali];
                if (endVals[chgi] == null) {
                // Do nothing
                } else if (endVals[chgi].toString().contains("val: null")) {
                    // Handle JAX-WS De-serialization issue, by parsing nodes
                    Element stateElement = (Element) endVals[chgi];
                    if (stateElement != null && stateElement.getFirstChild() != null) {
                        stateVal = stateElement.getFirstChild().getTextContent();
                        reached = expctdval.toString().equalsIgnoreCase(stateVal) || reached;
                    }
                } else {
                    reached = expctdval.equals(endVals[chgi]) || reached;
                    stateVal = "filtervals";
                }
            }
        }
    }
    // Destroy the filter when we are done.
    vimPort.destroyPropertyFilter(filterSpecRef);
    Object[] retVal = filterVals;
    if (stateVal != null && stateVal.equalsIgnoreCase("success")) {
        retVal = new Object[] { TaskInfoState.SUCCESS, null };
    }
    return retVal;
}
Also used : PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) PropertyFilterUpdate(com.vmware.vim25.PropertyFilterUpdate) PropertyChange(com.vmware.vim25.PropertyChange) Element(org.w3c.dom.Element) WaitOptions(com.vmware.vim25.WaitOptions) ObjectSpec(com.vmware.vim25.ObjectSpec) PropertySpec(com.vmware.vim25.PropertySpec) UpdateSet(com.vmware.vim25.UpdateSet) ObjectUpdate(com.vmware.vim25.ObjectUpdate) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 57 with ObjectSpec

use of com.vmware.vim25.ObjectSpec in project cloudstack by apache.

the class VmwareClient method retrieveMoRefProperties.

private List<ObjectContent> retrieveMoRefProperties(ManagedObjectReference mObj, List<String> props) throws Exception {
    PropertySpec pSpec = new PropertySpec();
    pSpec.setAll(false);
    pSpec.setType(mObj.getType());
    pSpec.getPathSet().addAll(props);
    ObjectSpec oSpec = new ObjectSpec();
    oSpec.setObj(mObj);
    oSpec.setSkip(false);
    PropertyFilterSpec spec = new PropertyFilterSpec();
    spec.getPropSet().add(pSpec);
    spec.getObjectSet().add(oSpec);
    List<PropertyFilterSpec> specArr = new ArrayList<PropertyFilterSpec>();
    specArr.add(spec);
    return vimPort.retrieveProperties(getPropCol(), specArr);
}
Also used : PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) ObjectSpec(com.vmware.vim25.ObjectSpec) PropertySpec(com.vmware.vim25.PropertySpec) ArrayList(java.util.ArrayList)

Example 58 with ObjectSpec

use of com.vmware.vim25.ObjectSpec in project cloudstack by apache.

the class VmwareContext method getHostMorByPath.

public ManagedObjectReference getHostMorByPath(String inventoryPath) throws Exception {
    assert (inventoryPath != null);
    String[] tokens;
    if (inventoryPath.startsWith("/"))
        tokens = inventoryPath.substring(1).split("/");
    else
        tokens = inventoryPath.split("/");
    ManagedObjectReference mor = getRootFolder();
    for (int i = 0; i < tokens.length; i++) {
        String token = tokens[i];
        List<ObjectContent> ocs;
        PropertySpec pSpec = null;
        ObjectSpec oSpec = null;
        if (mor.getType().equalsIgnoreCase("Datacenter")) {
            pSpec = new PropertySpec();
            pSpec.setAll(false);
            pSpec.setType("ManagedEntity");
            pSpec.getPathSet().add("name");
            TraversalSpec dcHostFolderTraversal = new TraversalSpec();
            dcHostFolderTraversal.setType("Datacenter");
            dcHostFolderTraversal.setPath("hostFolder");
            dcHostFolderTraversal.setName("dcHostFolderTraversal");
            oSpec = new ObjectSpec();
            oSpec.setObj(mor);
            oSpec.setSkip(Boolean.TRUE);
            oSpec.getSelectSet().add(dcHostFolderTraversal);
        } else if (mor.getType().equalsIgnoreCase("Folder")) {
            pSpec = new PropertySpec();
            pSpec.setAll(false);
            pSpec.setType("ManagedEntity");
            pSpec.getPathSet().add("name");
            TraversalSpec folderChildrenTraversal = new TraversalSpec();
            folderChildrenTraversal.setType("Folder");
            folderChildrenTraversal.setPath("childEntity");
            folderChildrenTraversal.setName("folderChildrenTraversal");
            oSpec = new ObjectSpec();
            oSpec.setObj(mor);
            oSpec.setSkip(Boolean.TRUE);
            oSpec.getSelectSet().add(folderChildrenTraversal);
        } else if (mor.getType().equalsIgnoreCase("ClusterComputeResource")) {
            pSpec = new PropertySpec();
            pSpec.setType("ManagedEntity");
            pSpec.getPathSet().add("name");
            TraversalSpec clusterHostTraversal = new TraversalSpec();
            clusterHostTraversal.setType("ClusterComputeResource");
            clusterHostTraversal.setPath("host");
            clusterHostTraversal.setName("folderChildrenTraversal");
            oSpec = new ObjectSpec();
            oSpec.setObj(mor);
            oSpec.setSkip(Boolean.TRUE);
            oSpec.getSelectSet().add(clusterHostTraversal);
        } else {
            s_logger.error("Invalid inventory path, path element can only be datacenter and folder");
            return null;
        }
        PropertyFilterSpec pfSpec = new PropertyFilterSpec();
        pfSpec.getPropSet().add(pSpec);
        pfSpec.getObjectSet().add(oSpec);
        List<PropertyFilterSpec> pfSpecArr = new ArrayList<PropertyFilterSpec>();
        pfSpecArr.add(pfSpec);
        ocs = getService().retrieveProperties(getPropertyCollector(), pfSpecArr);
        if (ocs != null && ocs.size() > 0) {
            boolean found = false;
            for (ObjectContent oc : ocs) {
                String name = oc.getPropSet().get(0).getVal().toString();
                if (name.equalsIgnoreCase(token) || name.equalsIgnoreCase("host")) {
                    mor = oc.getObj();
                    found = true;
                    if (name.equalsIgnoreCase("host"))
                        i--;
                    break;
                }
            }
            if (!found) {
                s_logger.error("Path element points to an un-existing inventory entity");
                return null;
            }
        } else {
            s_logger.error("Path element points to an un-existing inventory entity");
            return null;
        }
    }
    return mor;
}
Also used : PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) ArrayList(java.util.ArrayList) ObjectContent(com.vmware.vim25.ObjectContent) ObjectSpec(com.vmware.vim25.ObjectSpec) PropertySpec(com.vmware.vim25.PropertySpec) TraversalSpec(com.vmware.vim25.TraversalSpec) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 59 with ObjectSpec

use of com.vmware.vim25.ObjectSpec in project cloudstack by apache.

the class VMwareUtil method getVms.

public static Map<String, ManagedObjectReference> getVms(VMwareConnection connection) throws Exception {
    Map<String, ManagedObjectReference> nameToVm = new HashMap<>();
    ManagedObjectReference rootFolder = connection.getServiceContent().getRootFolder();
    TraversalSpec tSpec = getVMTraversalSpec();
    PropertySpec propertySpec = new PropertySpec();
    propertySpec.setAll(Boolean.FALSE);
    propertySpec.getPathSet().add("name");
    propertySpec.setType("VirtualMachine");
    ObjectSpec objectSpec = new ObjectSpec();
    objectSpec.setObj(rootFolder);
    objectSpec.setSkip(Boolean.TRUE);
    objectSpec.getSelectSet().add(tSpec);
    PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();
    propertyFilterSpec.getPropSet().add(propertySpec);
    propertyFilterSpec.getObjectSet().add(objectSpec);
    List<PropertyFilterSpec> lstPfs = new ArrayList<>(1);
    lstPfs.add(propertyFilterSpec);
    VimPortType vimPortType = connection.getVimPortType();
    ManagedObjectReference propertyCollector = connection.getServiceContent().getPropertyCollector();
    List<ObjectContent> lstObjectContent = retrievePropertiesAllObjects(lstPfs, vimPortType, propertyCollector);
    if (lstObjectContent != null) {
        for (ObjectContent oc : lstObjectContent) {
            ManagedObjectReference mor = oc.getObj();
            List<DynamicProperty> dps = oc.getPropSet();
            String vmName = null;
            if (dps != null) {
                for (DynamicProperty dp : dps) {
                    vmName = (String) dp.getVal();
                }
            }
            if (vmName != null) {
                nameToVm.put(vmName, mor);
            }
        }
    }
    return nameToVm;
}
Also used : PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) DynamicProperty(com.vmware.vim25.DynamicProperty) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) VimPortType(com.vmware.vim25.VimPortType) ObjectContent(com.vmware.vim25.ObjectContent) ObjectSpec(com.vmware.vim25.ObjectSpec) PropertySpec(com.vmware.vim25.PropertySpec) TraversalSpec(com.vmware.vim25.TraversalSpec) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 60 with ObjectSpec

use of com.vmware.vim25.ObjectSpec in project cloudstack by apache.

the class VMwareUtil method waitForValues.

private static Object[] waitForValues(VMwareConnection connection, ManagedObjectReference morObj, String[] filterProps, String[] endWaitProps, Object[][] expectedVals) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg, InvalidCollectorVersionFaultMsg {
    String version = "";
    Object[] endVals = new Object[endWaitProps.length];
    Object[] filterVals = new Object[filterProps.length];
    PropertyFilterSpec spec = new PropertyFilterSpec();
    ObjectSpec oSpec = new ObjectSpec();
    oSpec.setObj(morObj);
    oSpec.setSkip(Boolean.FALSE);
    spec.getObjectSet().add(oSpec);
    PropertySpec pSpec = new PropertySpec();
    pSpec.getPathSet().addAll(Arrays.asList(filterProps));
    pSpec.setType(morObj.getType());
    spec.getPropSet().add(pSpec);
    ManagedObjectReference propertyCollector = connection.getServiceContent().getPropertyCollector();
    ManagedObjectReference filterSpecRef = connection.getVimPortType().createFilter(propertyCollector, spec, true);
    boolean reached = false;
    UpdateSet updateSet;
    List<PropertyFilterUpdate> lstPropertyFilterUpdates;
    List<ObjectUpdate> lstObjectUpdates;
    List<PropertyChange> lstPropertyChanges;
    while (!reached) {
        updateSet = connection.getVimPortType().waitForUpdates(propertyCollector, version);
        if (updateSet == null || updateSet.getFilterSet() == null) {
            continue;
        }
        version = updateSet.getVersion();
        lstPropertyFilterUpdates = updateSet.getFilterSet();
        for (PropertyFilterUpdate propertyFilterUpdate : lstPropertyFilterUpdates) {
            lstObjectUpdates = propertyFilterUpdate.getObjectSet();
            for (ObjectUpdate objUpdate : lstObjectUpdates) {
                if (objUpdate.getKind() == ObjectUpdateKind.MODIFY || objUpdate.getKind() == ObjectUpdateKind.ENTER || objUpdate.getKind() == ObjectUpdateKind.LEAVE) {
                    lstPropertyChanges = objUpdate.getChangeSet();
                    for (PropertyChange propchg : lstPropertyChanges) {
                        updateValues(endWaitProps, endVals, propchg);
                        updateValues(filterProps, filterVals, propchg);
                    }
                }
            }
        }
        Object expectedValue;
        // Also, exit the WaitForUpdates loop if this is the case.
        for (int chgi = 0; chgi < endVals.length && !reached; chgi++) {
            for (int vali = 0; vali < expectedVals[chgi].length && !reached; vali++) {
                expectedValue = expectedVals[chgi][vali];
                reached = expectedValue.equals(endVals[chgi]) || reached;
            }
        }
    }
    // Destroy the filter when we are done.
    connection.getVimPortType().destroyPropertyFilter(filterSpecRef);
    return filterVals;
}
Also used : PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) PropertyFilterUpdate(com.vmware.vim25.PropertyFilterUpdate) PropertyChange(com.vmware.vim25.PropertyChange) ObjectSpec(com.vmware.vim25.ObjectSpec) PropertySpec(com.vmware.vim25.PropertySpec) UpdateSet(com.vmware.vim25.UpdateSet) ObjectUpdate(com.vmware.vim25.ObjectUpdate) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Aggregations

ObjectSpec (com.vmware.vim25.ObjectSpec)60 PropertyFilterSpec (com.vmware.vim25.PropertyFilterSpec)60 PropertySpec (com.vmware.vim25.PropertySpec)60 TraversalSpec (com.vmware.vim25.TraversalSpec)47 ObjectContent (com.vmware.vim25.ObjectContent)40 ArrayList (java.util.ArrayList)33 DynamicProperty (com.vmware.vim25.DynamicProperty)19 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)11 SelectionSpec (com.vmware.vim25.SelectionSpec)11 Gson (com.google.gson.Gson)10 Pair (com.cloud.utils.Pair)4 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)2 CustomFieldStringValue (com.vmware.vim25.CustomFieldStringValue)2 DVPortgroupConfigInfo (com.vmware.vim25.DVPortgroupConfigInfo)2 ObjectUpdate (com.vmware.vim25.ObjectUpdate)2 PropertyChange (com.vmware.vim25.PropertyChange)2 PropertyFilterUpdate (com.vmware.vim25.PropertyFilterUpdate)2 RetrieveOptions (com.vmware.vim25.RetrieveOptions)2 RetrieveResult (com.vmware.vim25.RetrieveResult)2 UpdateSet (com.vmware.vim25.UpdateSet)2