private void handleUpdate(UpdateSet update) throws Exception { ObjectUpdate[] vmUpdates; PropertyFilterUpdate[] pfus = update.getFilterSet(); if (pfus == null) { return; } for (int pfui = 0; pfui < pfus.length; pfui++) { vmUpdates = pfus[pfui].getObjectSet(); if (vmUpdates == null) { continue; } for (ObjectUpdate vmi : vmUpdates) { handleChanges(vmi); } } }
/** * This method executes in a thread managed by {@link #enumerationThreadPool}. * * @param client * @throws Exception */ private void startEnumerationProcess(ComputeStateWithDescription parent, EnumerationClient client) throws Exception { if (parent.description.regionId == null) { // not implemented if no datacenter is provided return; } PropertyFilterSpec spec = client.createResourcesFilterSpec(); try { for (UpdateSet updateSet : client.pollForUpdates(spec)) { processUpdates(updateSet); if (!this.startedEnumProcessesByHost.containsKey(parent.documentSelfLink)) { break; } } } catch (Exception e) { // destroy connection and let global error handler process it further client.close(); throw e; } }
public void run() { while(true) { try { UpdateSet update = pc.waitForUpdates(version); PropertyFilterUpdate[] pfu = update.getFilterSet(); this.setChanged(); this.notifyObservers(pfu); version = update.getVersion(); } catch(NotAuthenticated na) { na.printStackTrace(); break; } catch(Exception e) { e.printStackTrace(); } } }
@Override public UpdateSet next() { try { UpdateSet result = getVimPort().waitForUpdatesEx(this.pc, this.since, this.opts); this.since = result.getVersion(); return result; } catch (Exception e) { destroyCollectorQuietly(this.pc); throw new RuntimeException(e); } }
static void handleUpdate(UpdateSet update) { ArrayList vmUpdates = new ArrayList(); ArrayList hostUpdates = new ArrayList(); PropertyFilterUpdate[] pfus = update.getFilterSet(); for(int i=0; i<pfus.length; i++) { ObjectUpdate[] ous = pfus[i].getObjectSet(); for(int j=0; j<ous.length; ++j) { if(ous[j].getObj().getType().equals("VirtualMachine")) { vmUpdates.add(ous[j]); } else if(ous[j].getObj().getType().equals("HostSystem")) { hostUpdates.add(ous[j]); } } } if(vmUpdates.size() > 0) { System.out.println("Virtual Machine updates:"); for(Iterator vmi = vmUpdates.iterator(); vmi.hasNext();) { handleObjectUpdate((ObjectUpdate)vmi.next()); } } if(hostUpdates.size() > 0) { System.out.println("Host updates:"); for(Iterator vmi = hostUpdates.iterator(); vmi.hasNext();) { handleObjectUpdate((ObjectUpdate)vmi.next()); } } }
void handleUpdate(UpdateSet update) { ObjectUpdate[] vmUpdates; PropertyFilterUpdate[] pfus = update.getFilterSet(); for (int pfui = 0; pfui < pfus.length; pfui++) { System.out.println("Virtual Machine updates:"); vmUpdates = pfus[pfui].getObjectSet(); for (ObjectUpdate vmi : vmUpdates) { System.out.println("Handling object update"); handleObjectUpdate(vmi); } } }
private void processUpdates(UpdateSet updateSet) { // handle PC updates // https://jira-hzn.eng.vmware.com/browse/VCOM-17 }
public Iterable<UpdateSet> pollForUpdates(PropertyFilterSpec spec) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg { ManagedObjectReference pc = createPropertyCollectorWithFilter(spec); return () -> new ObjectUpdateIterator(pc); }
public UpdateSet checkForUpdates(String version) throws InvalidCollectorVersion, RuntimeFault, RemoteException { return getVimService().checkForUpdates(getMOR(), version); }
/** @deprecated as of SDK4.1 */ public UpdateSet waitForUpdates(String version) throws InvalidCollectorVersion, RuntimeFault, RemoteException { return getVimService().waitForUpdates(getMOR(), version); }
/** @since SDK4.1 */ public UpdateSet waitForUpdatesEx(String version, WaitOptions options) throws InvalidCollectorVersion, RuntimeFault, RemoteException { return getVimService().waitForUpdatesEx(getMOR(), version, options); }
public static void main(String [] args) throws Exception { CommandLineParser clp = new CommandLineParser(constructOptions(), args); String urlStr = clp.get_option("url"); String username = clp.get_option("username"); String password = clp.get_option("password"); String vmname = clp.get_option("vmname"); ServiceInstance si = new ServiceInstance(new URL(urlStr), username, password, true); Folder rootFolder = si.getRootFolder(); VirtualMachine vm = (VirtualMachine) new InventoryNavigator(rootFolder).searchManagedEntity("VirtualMachine", vmname); if(vm == null) { System.out.println("Virtual Machine " + vmname + " Not Found"); return; } // "summary.quickStats" is taken out -- API ref says, // "A set of statistics that are typically updated with near real-time regularity. // This data object type does not support notification, for scalability reasons. // Therefore, changes in QuickStats do not generate property collector updates. // To monitor statistics values, use the statistics and alarms modules instead. String[][] typeInfo = { new String[]{"VirtualMachine", "name","runtime"}}; PropertySpec[] pSpecs = PropertyCollectorUtil.buildPropertySpecArray(typeInfo); ObjectSpec[] oSpecs = createObjectSpecs(vm); PropertyFilterSpec pSpec = new PropertyFilterSpec(); pSpec.setPropSet(pSpecs); pSpec.setObjectSet(oSpecs); PropertyCollector pc = si.getPropertyCollector(); PropertyFilter pf = pc.createFilter(pSpec, false); BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); String version = ""; while(true) { UpdateSet update = pc.checkForUpdates(version); if(update != null && update.getFilterSet() != null) { handleUpdate(update); version = update.getVersion(); System.out.println("version is:" + version); } else { System.out.println("No update is present!"); } System.out.println("\nPress <Enter> to check for updates"); System.out.println("Enter 'exit' <Enter> to exit the program"); if(console.readLine().trim().equalsIgnoreCase("exit")) break; } pf.destroyPropertyFilter(); si.getServerConnection().logout(); }
/** * 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 * @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 expectedValues values for properties to end the wait * @return true indicating expected values were met, and false otherwise * @throws RuntimeFaultFaultMsg * @throws InvalidPropertyFaultMsg * @throws InvalidCollectorVersionFaultMsg */ public Object[] wait(ManagedObjectReference objMor, String[] filterProps, String[] endWaitProps, Object[][] expectedValues) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg, InvalidCollectorVersionFaultMsg { String version = Constants.EMPTY; String stateVal = null; Object[] endValues = new Object[endWaitProps.length]; Object[] filterValues = new Object[filterProps.length]; PropertyFilterSpec spec = propertyFilterSpec(objMor, filterProps); ManagedObjectReference filterSpecRef = vimPort.createFilter(serviceContent.getPropertyCollector(), spec, true); boolean reached = false; UpdateSet updateset; while (!reached) { updateset = vimPort.waitForUpdatesEx(serviceContent.getPropertyCollector(), version, new WaitOptions()); int waitForUpdateCounter = 0; if (updateset == null || updateset.getFilterSet() == null) { waitForUpdateCounter++; if (waitForUpdateCounter <= MAX_TRIED_WAIT_FOR_UPDATE_COUNTER) { continue; } break; } version = updateset.getVersion(); for (PropertyFilterUpdate filtup : updateset.getFilterSet()) { for (ObjectUpdate objup : filtup.getObjectSet()) { if (objup.getKind() == ObjectUpdateKind.MODIFY || objup.getKind() == ObjectUpdateKind.ENTER || objup.getKind() == ObjectUpdateKind.LEAVE) { for (PropertyChange propchg : objup.getChangeSet()) { updateValues(endWaitProps, endValues, propchg); updateValues(filterProps, filterValues, propchg); } } } } // Check if the expected values have been reached and exit the loop if done. // Also exit the WaitForUpdates loop if this is the case. for (int chgi = 0; chgi < endValues.length && !reached; chgi++) { for (int vali = 0; vali < expectedValues[chgi].length && !reached; vali++) { Object expctdval = expectedValues[chgi][vali]; if (endValues[chgi].toString().contains(KEY_VALUE_NULL_STRING)) { Element stateElement = (Element) endValues[chgi]; if (stateElement != null && stateElement.getFirstChild() != null) { stateVal = stateElement.getFirstChild().getTextContent(); reached = expctdval.toString().equalsIgnoreCase(stateVal); } } else { expctdval = expectedValues[chgi][vali]; reached = expctdval.equals(endValues[chgi]); stateVal = FILTER_VALUES; } } } } try { vimPort.destroyPropertyFilter(filterSpecRef); } catch (RuntimeFaultFaultMsg e) { throw new RuntimeException(e.getMessage()); } return getObjectState(stateVal, filterValues); }
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; // Check if the expected values have been reached and exit the loop if done. // 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; }