/** * 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(); }
private static void initRootFolder() { if (ROOT_FOLDER == null) { try { ROOT_FOLDER = SERVICE_CONTENT.getRootFolder(); } catch (Exception e) { e.printStackTrace(); } } }
/** * @param datacenterName The name of the Datacenter * @return ManagedObjectReference to the Datacenter */ private static ManagedObjectReference getDatacenterByName(String datacenterName) { ManagedObjectReference retVal = null; ManagedObjectReference rootFolder = serviceContent.getRootFolder(); ManagedObjectReference propCollector = serviceContent.getPropertyCollector(); try { TraversalSpec tSpec = getDatacenterTraversalSpec(); // Create Property Spec PropertySpec propertySpec = new PropertySpec(); propertySpec.setAll(Boolean.FALSE); propertySpec.getPathSet().add("name"); propertySpec.setType("Datacenter"); // Now create Object Spec ObjectSpec objectSpec = new ObjectSpec(); objectSpec.setObj(rootFolder); objectSpec.setSkip(Boolean.TRUE); objectSpec.getSelectSet().add(tSpec); // Create PropertyFilterSpec using the PropertySpec and ObjectPec // created above. PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec(); propertyFilterSpec.getPropSet().add(propertySpec); propertyFilterSpec.getObjectSet().add(objectSpec); List<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>(1); listpfs.add(propertyFilterSpec); List<ObjectContent> listobjcont = retrievePropertiesAllObjects(listpfs); if (listobjcont != null) { for (ObjectContent oc : listobjcont) { ManagedObjectReference mr = oc.getObj(); String dcnm = null; List<DynamicProperty> dps = oc.getPropSet(); if (dps != null) { // Since there is only one property PropertySpec pathset // this array contains only one value for (DynamicProperty dp : dps) { dcnm = (String) dp.getVal(); } } // This is done outside of the previous for loop to break // out of the loop as soon as the required datacenter is found. if (dcnm != null && dcnm.equals(datacenterName)) { retVal = mr; break; } } } } catch (SOAPFaultException sfe) { printSoapFaultException(sfe); } catch (Exception e) { e.printStackTrace(); } return retVal; }
/** * Retrieves the MOREF of the host. * * @param hostName : * @return */ private static ManagedObjectReference getHostByHostName(String hostName) { ManagedObjectReference retVal = null; ManagedObjectReference rootFolder = serviceContent.getRootFolder(); try { TraversalSpec tSpec = getHostSystemTraversalSpec(); // Create Property Spec PropertySpec propertySpec = new PropertySpec(); propertySpec.setAll(Boolean.FALSE); propertySpec.getPathSet().add("name"); propertySpec.setType("HostSystem"); // Now create Object Spec ObjectSpec objectSpec = new ObjectSpec(); objectSpec.setObj(rootFolder); objectSpec.setSkip(Boolean.TRUE); objectSpec.getSelectSet().add(tSpec); // Create PropertyFilterSpec using the PropertySpec and ObjectPec // created above. PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec(); propertyFilterSpec.getPropSet().add(propertySpec); propertyFilterSpec.getObjectSet().add(objectSpec); List<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>(1); listpfs.add(propertyFilterSpec); List<ObjectContent> listobjcont = retrievePropertiesAllObjects(listpfs); if (listobjcont != null) { for (ObjectContent oc : listobjcont) { ManagedObjectReference mr = oc.getObj(); String hostnm = null; List<DynamicProperty> listDynamicProps = oc.getPropSet(); DynamicProperty[] dps = listDynamicProps.toArray(new DynamicProperty[listDynamicProps.size()]); if (dps != null) { for (DynamicProperty dp : dps) { hostnm = (String) dp.getVal(); } } if (hostnm != null && hostnm.equals(hostName)) { retVal = mr; break; } } } else { System.out.println("The Object Content is Null"); } } catch (SOAPFaultException sfe) { printSoapFaultException(sfe); } catch (Exception e) { e.printStackTrace(); } return retVal; }
/** * 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(); }