Search in sources :

Example 1 with PropertyChange

use of com.vmware.vim25.PropertyChange in project photon-model by vmware.

the class WaitForValues method wait.

/**
 * Handle Updates for a single object. waits till expected values of
 * properties to check are reached Destroys the ObjectFilter when done.
 * The matching properties will be added to the list of properties to fetch.
 *
 * @param moRef       MOR of the Object to wait for
 * @param fetchProps  Properties list to filter
 * @param propsToMatch Properties list to check for expected values these be properties
 *                     of a property in the filter properties list
 * @param propsMatchValues values for properties to end the wait
 * @param maxWaitSeconds how long to wait for the condition to be met, use null to wait forever
 * @return the requested properties or null if expected values not reached within timeout
 * @throws RuntimeFaultFaultMsg
 * @throws InvalidPropertyFaultMsg
 * @throws InvalidCollectorVersionFaultMsg
 */
public Object[] wait(ManagedObjectReference moRef, String[] fetchProps, String[] propsToMatch, Object[][] propsMatchValues, Integer maxWaitSeconds) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg, InvalidCollectorVersionFaultMsg {
    VimPortType vimPort;
    ManagedObjectReference filterSpecRef = null;
    ServiceContent serviceContent;
    try {
        vimPort = this.connection.getVimPort();
        serviceContent = this.connection.getServiceContent();
    } catch (Throwable cause) {
        throw new BaseHelper.HelperException(cause);
    }
    // version string is initially null
    String version = "";
    Object[] endVals = new Object[propsToMatch.length];
    String stateVal = null;
    PropertyFilterSpec spec = propertyFilterSpec(moRef, fetchProps, propsToMatch);
    filterSpecRef = vimPort.createFilter(serviceContent.getPropertyCollector(), spec, true);
    boolean reached = false;
    UpdateSet updateset;
    List<PropertyFilterUpdate> filtupary;
    List<ObjectUpdate> objupary;
    List<PropertyChange> propchgary;
    // override maxWaitSeconds to give the timeout a chance to be hit earlier
    WaitOptions waitOptions = new WaitOptions();
    waitOptions.setMaxWaitSeconds(DEFAULT_MAX_WAIT_SECONDS);
    long timeout = -1;
    if (maxWaitSeconds != null) {
        timeout = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(maxWaitSeconds);
    }
    while (!reached && shouldContinue(timeout)) {
        updateset = vimPort.waitForUpdatesEx(serviceContent.getPropertyCollector(), version, 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) {
                propchgary = objup.getChangeSet();
                for (PropertyChange propchg : propchgary) {
                    updateValues(propsToMatch, endVals, propchg);
                }
            }
        }
        // Also exit the WaitForUpdates loop if this is the case.
        for (int chgi = 0; chgi < endVals.length && !reached; chgi++) {
            for (int vali = 0; vali < propsMatchValues[chgi].length && !reached; vali++) {
                Object expectedValue = propsMatchValues[chgi][vali];
                Object endVal = endVals[chgi];
                if (endVal == null) {
                // Do Nothing
                } else if (endVal.toString().contains("val: null")) {
                    // Due to some issue in JAX-WS De-serialization getting the information from
                    // the nodes
                    Element stateElement = (Element) endVal;
                    if (stateElement.getFirstChild() != null) {
                        stateVal = stateElement.getFirstChild().getTextContent();
                        reached = expectedValue.toString().equalsIgnoreCase(stateVal) || reached;
                    }
                } else {
                    expectedValue = propsMatchValues[chgi][vali];
                    reached = expectedValue.equals(endVal) || reached;
                    stateVal = "filtervals";
                }
            }
        }
    }
    if (!reached) {
        // got here but condition not reached; timeout
        return null;
    }
    Object[] retVal = null;
    // Destroy the filter when we are done.
    try {
        vimPort.destroyPropertyFilter(filterSpecRef);
    } catch (RuntimeFaultFaultMsg e) {
        e.printStackTrace();
    }
    if (stateVal != null) {
        if (stateVal.equalsIgnoreCase("ready")) {
            retVal = new Object[] { HttpNfcLeaseState.READY };
        }
        if (stateVal.equalsIgnoreCase("error")) {
            retVal = new Object[] { HttpNfcLeaseState.ERROR };
        }
        if (stateVal.equals("filtervals")) {
            retVal = fetchFinalValues(moRef, fetchProps);
        }
    } else {
        retVal = new Object[] { HttpNfcLeaseState.ERROR };
    }
    return retVal;
}
Also used : PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) PropertyFilterUpdate(com.vmware.vim25.PropertyFilterUpdate) PropertyChange(com.vmware.vim25.PropertyChange) Element(org.w3c.dom.Element) RuntimeFaultFaultMsg(com.vmware.vim25.RuntimeFaultFaultMsg) WaitOptions(com.vmware.vim25.WaitOptions) VimPortType(com.vmware.vim25.VimPortType) ServiceContent(com.vmware.vim25.ServiceContent) UpdateSet(com.vmware.vim25.UpdateSet) ObjectUpdate(com.vmware.vim25.ObjectUpdate) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 2 with PropertyChange

use of com.vmware.vim25.PropertyChange 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 3 with PropertyChange

use of com.vmware.vim25.PropertyChange 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

ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)3 ObjectUpdate (com.vmware.vim25.ObjectUpdate)3 PropertyChange (com.vmware.vim25.PropertyChange)3 PropertyFilterSpec (com.vmware.vim25.PropertyFilterSpec)3 PropertyFilterUpdate (com.vmware.vim25.PropertyFilterUpdate)3 UpdateSet (com.vmware.vim25.UpdateSet)3 ObjectSpec (com.vmware.vim25.ObjectSpec)2 PropertySpec (com.vmware.vim25.PropertySpec)2 WaitOptions (com.vmware.vim25.WaitOptions)2 Element (org.w3c.dom.Element)2 RuntimeFaultFaultMsg (com.vmware.vim25.RuntimeFaultFaultMsg)1 ServiceContent (com.vmware.vim25.ServiceContent)1 VimPortType (com.vmware.vim25.VimPortType)1