Exemple #1
0
  /**
   * Establishes session with the virtual center server.
   *
   * @throws Exception the exception
   */
  private static void connect() throws Exception {

    HostnameVerifier hv =
        new HostnameVerifier() {
          public boolean verify(String urlHostName, SSLSession session) {
            return true;
          }
        };
    trustAllHttpsCertificates();
    HttpsURLConnection.setDefaultHostnameVerifier(hv);

    SVC_INST_REF.setType(SVC_INST_NAME);
    SVC_INST_REF.setValue(SVC_INST_NAME);

    vimService = new VimService();
    vimPort = vimService.getVimPort();
    Map<String, Object> ctxt = ((BindingProvider) vimPort).getRequestContext();

    ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url);
    ctxt.put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true);

    serviceContent = vimPort.retrieveServiceContent(SVC_INST_REF);
    vimPort.login(serviceContent.getSessionManager(), userName, password, null);
    isConnected = true;

    propCollectorRef = serviceContent.getPropertyCollector();
    rootRef = serviceContent.getRootFolder();
    perfManager = serviceContent.getPerfManager();
  }
 /**
  * Power on vm.
  *
  * @param vmMor the vm moref
  * @throws RemoteException the remote exception
  * @throws Exception the exception
  */
 void powerOnVM(ManagedObjectReference vmMor)
     throws RemoteException, RuntimeFaultFaultMsg, InvalidPropertyFaultMsg,
         InvalidCollectorVersionFaultMsg, TaskInProgressFaultMsg, VmConfigFaultFaultMsg,
         InsufficientResourcesFaultFaultMsg, FileFaultFaultMsg, InvalidStateFaultMsg {
   ManagedObjectReference cssTask = vimPort.powerOnVMTask(vmMor, null);
   if (getTaskResultAfterDone(cssTask)) {
     System.out.println("Success: VM started Successfully");
   } else {
     String msg = "Failure: starting [ " + vmMor.getValue() + "] VM";
     throw new RuntimeException(msg);
   }
 }
Exemple #3
0
  /**
   * Establishes session with the virtual center server.
   *
   * @throws Exception the exception
   */
  private static void connect() throws Exception {
    HostnameVerifier hv =
        new HostnameVerifier() {
          public boolean verify(String urlHostName, SSLSession session) {
            return true;
          }
        };
    trustAllHttpsCertificates();
    HttpsURLConnection.setDefaultHostnameVerifier(hv);

    SVC_INST_REF.setType(SVC_INST_NAME);
    SVC_INST_REF.setValue(SVC_INST_NAME);

    vimService = new VimService();
    vimPort = vimService.getVimPort();
    Map<String, Object> ctxt = ((BindingProvider) vimPort).getRequestContext();

    ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url);
    ctxt.put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true);

    serviceContent = vimPort.retrieveServiceContent(SVC_INST_REF);
    boolean isVCApi = checkApiType(serviceContent);
    if (!isVCApi) {
      System.out.println("Virtual Center is not supported");
      System.exit(0);
    }
    headers =
        (Map)
            ((BindingProvider) vimPort)
                .getResponseContext()
                .get(MessageContext.HTTP_RESPONSE_HEADERS);
    vimPort.login(serviceContent.getSessionManager(), userName, password, null);
    isConnected = true;
    rootRef = serviceContent.getRootFolder();
    propCollectorRef = serviceContent.getPropertyCollector();
  }
Exemple #4
0
 /**
  * Retrieve contents for a single object based on the property collector registered with the
  * service.
  *
  * @param collector Property collector registered with service
  * @param mobj Managed Object Reference to get contents for
  * @param properties names of properties of object to retrieve
  * @return retrieved object contents
  */
 private static ObjectContent[] getObjectProperties(
     ManagedObjectReference mobj, String[] properties) throws Exception {
   if (mobj == null) {
     return null;
   }
   PropertyFilterSpec spec = new PropertyFilterSpec();
   spec.getPropSet().add(new PropertySpec());
   if ((properties == null || properties.length == 0)) {
     spec.getPropSet().get(0).setAll(Boolean.TRUE);
   } else {
     spec.getPropSet().get(0).setAll(Boolean.FALSE);
   }
   spec.getPropSet().get(0).setType(mobj.getType());
   spec.getPropSet().get(0).getPathSet().addAll(Arrays.asList(properties));
   spec.getObjectSet().add(new ObjectSpec());
   spec.getObjectSet().get(0).setObj(mobj);
   spec.getObjectSet().get(0).setSkip(Boolean.FALSE);
   List<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>(1);
   listpfs.add(spec);
   List<ObjectContent> listobjcont = retrievePropertiesAllObjects(listpfs);
   return listobjcont.toArray(new ObjectContent[listobjcont.size()]);
 }
Exemple #5
0
  /**
   * Wait for values.
   *
   * @param objmor the object mor
   * @param filterProps the filter props
   * @param endWaitProps the end wait props
   * @param expectedVals the expected vals
   * @return the object[]
   * @throws RemoteException the remote exception
   * @throws Exception the exception
   */
  private static Object[] waitForValues(
      ManagedObjectReference objmor,
      String[] filterProps,
      String[] endWaitProps,
      Object[][] expectedVals)
      throws RemoteException, Exception {
    // version string is initially null
    String version = "";
    Object[] endVals = new Object[endWaitProps.length];
    Object[] filterVals = new Object[filterProps.length];

    PropertyFilterSpec spec = new PropertyFilterSpec();

    spec.getObjectSet().add(new ObjectSpec());

    spec.getObjectSet().get(0).setObj(objmor);

    spec.getPropSet().addAll(Arrays.asList(new PropertySpec[] {new PropertySpec()}));

    spec.getPropSet().get(0).getPathSet().addAll(Arrays.asList(filterProps));

    spec.getPropSet().get(0).setType(objmor.getType());

    spec.getObjectSet().get(0).setSkip(Boolean.FALSE);

    ManagedObjectReference filterSpecRef = vimPort.createFilter(propCollectorRef, spec, true);

    boolean reached = false;

    UpdateSet updateset = null;
    PropertyFilterUpdate[] filtupary = null;
    PropertyFilterUpdate filtup = null;
    ObjectUpdate[] objupary = null;
    ObjectUpdate objup = null;
    PropertyChange[] propchgary = null;
    PropertyChange propchg = null;
    while (!reached) {
      boolean retry = true;
      while (retry) {
        try {
          updateset = vimPort.waitForUpdates(propCollectorRef, version);
          retry = false;
        } catch (SOAPFaultException sfe) {
          printSoapFaultException(sfe);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
      if (updateset != null) {
        version = updateset.getVersion();
      }
      if (updateset == null || updateset.getFilterSet() == null) {
        continue;
      }
      List<PropertyFilterUpdate> listprfup = updateset.getFilterSet();
      filtupary = listprfup.toArray(new PropertyFilterUpdate[listprfup.size()]);
      filtup = null;
      for (int fi = 0; fi < filtupary.length; fi++) {
        filtup = filtupary[fi];
        List<ObjectUpdate> listobjup = filtup.getObjectSet();
        objupary = listobjup.toArray(new ObjectUpdate[listobjup.size()]);
        objup = null;
        propchgary = null;
        for (int oi = 0; oi < objupary.length; oi++) {
          objup = objupary[oi];
          if (objup.getKind() == ObjectUpdateKind.MODIFY
              || objup.getKind() == ObjectUpdateKind.ENTER
              || objup.getKind() == ObjectUpdateKind.LEAVE) {
            List<PropertyChange> listchset = objup.getChangeSet();
            propchgary = listchset.toArray(new PropertyChange[listchset.size()]);
            for (int ci = 0; ci < propchgary.length; ci++) {
              propchg = propchgary[ci];
              updateValues(endWaitProps, endVals, propchg);
              updateValues(filterProps, filterVals, propchg);
            }
          }
        }
      }
      Object expctdval = null;
      // 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++) {
          expctdval = expectedVals[chgi][vali];
          reached = expctdval.equals(endVals[chgi]) || reached;
        }
      }
    }

    // Destroy the filter when we are done.
    vimPort.destroyPropertyFilter(filterSpecRef);
    return filterVals;
  }
 /** Set the managed object reference type, and value to ServiceInstance */
 private static void initSIMORef() {
   SIMO_REF.setType(STR_SERVICE_INSTANCE);
   SIMO_REF.set_value(STR_SERVICE_INSTANCE);
 }