http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/481f4804/vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareContext.java ---------------------------------------------------------------------- diff --git a/vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareContext.java b/vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareContext.java index 6dd6475..7f9aacb 100755 --- a/vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareContext.java +++ b/vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareContext.java @@ -26,7 +26,6 @@ import java.io.FileOutputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; -import java.net.ConnectException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; @@ -44,15 +43,11 @@ import org.apache.log4j.Logger; import com.cloud.hypervisor.vmware.mo.DatacenterMO; import com.cloud.hypervisor.vmware.mo.DatastoreFile; import com.cloud.utils.ActionDelegate; -import com.vmware.apputils.version.ExtendedAppUtil; -import com.vmware.apputils.vim25.ServiceConnection; -import com.vmware.apputils.vim25.ServiceUtil; import com.vmware.vim25.ManagedObjectReference; import com.vmware.vim25.ObjectContent; import com.vmware.vim25.ObjectSpec; import com.vmware.vim25.PropertyFilterSpec; import com.vmware.vim25.PropertySpec; -import com.vmware.vim25.SelectionSpec; import com.vmware.vim25.ServiceContent; import com.vmware.vim25.TaskInfo; import com.vmware.vim25.TraversalSpec; @@ -63,30 +58,30 @@ public class VmwareContext { private static int MAX_CONNECT_RETRY = 5; private static int CONNECT_RETRY_INTERVAL = 1000; - - private ExtendedAppUtil _appUtil; + + private VmwareClient _vimClient; private String _serverAddress; - + private Map<String, Object> _stockMap = new HashMap<String, Object>(); private int _CHUNKSIZE = 1*1024*1024; // 1M - + static { try { - javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1]; - javax.net.ssl.TrustManager tm = new TrustAllManager(); - trustAllCerts[0] = tm; - javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL"); - sc.init(null, trustAllCerts, null); + javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1]; + javax.net.ssl.TrustManager tm = new TrustAllManager(); + trustAllCerts[0] = tm; + javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL"); + sc.init(null, trustAllCerts, null); javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); } catch (Exception e) { s_logger.error("Unexpected exception ", e); } } - - public VmwareContext(ExtendedAppUtil appUtil, String address) { - assert(appUtil != null) : "Invalid parameter in constructing VmwareContext object"; - - _appUtil = appUtil; + + public VmwareContext(VmwareClient client, String address) { + assert(client != null) : "Invalid parameter in constructing VmwareContext object"; + + _vimClient = client; _serverAddress = address; } @@ -95,132 +90,136 @@ public class VmwareContext { _stockMap.put(name, obj); } } - + public void uregisterStockObject(String name) { synchronized(_stockMap) { _stockMap.remove(name); } } - + @SuppressWarnings("unchecked") public <T> T getStockObject(String name) { synchronized(_stockMap) { return (T)_stockMap.get(name); } } - + public String getServerAddress() { return _serverAddress; } - + + /* public ServiceConnection getServiceConnection() { - return _appUtil.getServiceConnection3(); + return _vimClient.getServiceConnection3(); } - + */ + public VimPortType getService() { - return getServiceConnection().getService(); + return _vimClient.getService(); } - + public ServiceContent getServiceContent() { - return getServiceConnection().getServiceContent(); + return _vimClient.getServiceContent(); } - + + /* public ServiceUtil getServiceUtil() { - return _appUtil.getServiceUtil3(); + return _vimClient.getServiceUtil3(); } - + */ + + public ManagedObjectReference getPropertyCollector(){ + return _vimClient.getPropCol(); + } + public ManagedObjectReference getRootFolder() { - return getServiceContent().getRootFolder(); + return _vimClient.getRootFolder(); } - + + public VmwareClient getVimClient(){ + return _vimClient; + } + + 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]; - ObjectContent[] ocs; + List<ObjectContent> ocs; + PropertySpec pSpec = null; + ObjectSpec oSpec = null; if(mor.getType().equalsIgnoreCase("Datacenter")) { - PropertySpec pSpec = new PropertySpec(); + pSpec = new PropertySpec(); + pSpec.setAll(false); pSpec.setType("ManagedEntity"); - pSpec.setPathSet(new String[] { "name" }); - + pSpec.getPathSet().add("name"); + TraversalSpec dcHostFolderTraversal = new TraversalSpec(); dcHostFolderTraversal.setType("Datacenter"); dcHostFolderTraversal.setPath("hostFolder"); dcHostFolderTraversal.setName("dcHostFolderTraversal"); - ObjectSpec oSpec = new ObjectSpec(); + oSpec = new ObjectSpec(); oSpec.setObj(mor); oSpec.setSkip(Boolean.TRUE); - oSpec.setSelectSet(new SelectionSpec[] { dcHostFolderTraversal }); - - PropertyFilterSpec pfSpec = new PropertyFilterSpec(); - pfSpec.setPropSet(new PropertySpec[] { pSpec }); - pfSpec.setObjectSet(new ObjectSpec[] { oSpec }); - ocs = getService().retrieveProperties( - getServiceContent().getPropertyCollector(), - new PropertyFilterSpec[] { pfSpec }); - + oSpec.getSelectSet().add(dcHostFolderTraversal); + } else if(mor.getType().equalsIgnoreCase("Folder")) { - PropertySpec pSpec = new PropertySpec(); + pSpec = new PropertySpec(); + pSpec.setAll(false); pSpec.setType("ManagedEntity"); - pSpec.setPathSet(new String[] { "name" }); - + pSpec.getPathSet().add("name"); + TraversalSpec folderChildrenTraversal = new TraversalSpec(); folderChildrenTraversal.setType("Folder"); folderChildrenTraversal.setPath("childEntity"); folderChildrenTraversal.setName("folderChildrenTraversal"); - ObjectSpec oSpec = new ObjectSpec(); + oSpec = new ObjectSpec(); oSpec.setObj(mor); oSpec.setSkip(Boolean.TRUE); - oSpec.setSelectSet(new SelectionSpec[] { folderChildrenTraversal }); - - PropertyFilterSpec pfSpec = new PropertyFilterSpec(); - pfSpec.setPropSet(new PropertySpec[] { pSpec }); - pfSpec.setObjectSet(new ObjectSpec[] { oSpec }); - - ocs = getService().retrieveProperties( - getServiceContent().getPropertyCollector(), - new PropertyFilterSpec[] { pfSpec }); + oSpec.getSelectSet().add(folderChildrenTraversal); + + } else if(mor.getType().equalsIgnoreCase("ClusterComputeResource")) { - PropertySpec pSpec = new PropertySpec(); + pSpec = new PropertySpec(); pSpec.setType("ManagedEntity"); - pSpec.setPathSet(new String[] { "name" }); - + pSpec.getPathSet().add("name"); + TraversalSpec clusterHostTraversal = new TraversalSpec(); clusterHostTraversal.setType("ClusterComputeResource"); clusterHostTraversal.setPath("host"); clusterHostTraversal.setName("folderChildrenTraversal"); - ObjectSpec oSpec = new ObjectSpec(); + oSpec = new ObjectSpec(); oSpec.setObj(mor); oSpec.setSkip(Boolean.TRUE); - oSpec.setSelectSet(new SelectionSpec[] { clusterHostTraversal }); - - PropertyFilterSpec pfSpec = new PropertyFilterSpec(); - pfSpec.setPropSet(new PropertySpec[] { pSpec }); - pfSpec.setObjectSet(new ObjectSpec[] { oSpec }); - - ocs = getService().retrieveProperties( - getServiceContent().getPropertyCollector(), - new PropertyFilterSpec[] { pfSpec }); + oSpec.getSelectSet().add(clusterHostTraversal); + } else { s_logger.error("Invalid inventory path, path element can only be datacenter and folder"); return null; } - - if(ocs != null && ocs.length > 0) { + + 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()[0].getVal().toString(); + String name = oc.getPropSet().get(0).getVal().toString(); if(name.equalsIgnoreCase(token) || name.equalsIgnoreCase("host")) { mor = oc.getObj(); found = true; @@ -244,44 +243,44 @@ public class VmwareContext { // path in format of <datacenter name>/<datastore name> public ManagedObjectReference getDatastoreMorByPath(String inventoryPath) throws Exception { assert(inventoryPath != null); - + String[] tokens; if(inventoryPath.startsWith("/")) tokens = inventoryPath.substring(1).split("/"); else tokens = inventoryPath.split("/"); - + if(tokens == null || tokens.length != 2) { s_logger.error("Invalid datastore inventory path. path: " + inventoryPath); return null; } - + DatacenterMO dcMo = new DatacenterMO(this, tokens[0]); if(dcMo.getMor() == null) { s_logger.error("Unable to locate the datacenter specified in path: " + inventoryPath); return null; } - + return dcMo.findDatastore(tokens[1]); } - + public void waitForTaskProgressDone(ManagedObjectReference morTask) throws Exception { while(true) { - TaskInfo tinfo = (TaskInfo)getServiceUtil().getDynamicProperty(morTask, "info"); + TaskInfo tinfo = (TaskInfo)_vimClient.getDynamicProperty(morTask, "info"); Integer progress = tinfo.getProgress(); if(progress == null) break; - + if(progress.intValue() >= 100) break; - + Thread.sleep(1000); } } - + public void getFile(String urlString, String localFileFullName) throws Exception { HttpURLConnection conn = getHTTPConnection(urlString); - + InputStream in = conn.getInputStream(); OutputStream out = new FileOutputStream(new File(localFileFullName)); byte[] buf = new byte[_CHUNKSIZE]; @@ -290,19 +289,19 @@ public class VmwareContext { out.write(buf, 0, len); } in.close(); - out.close(); + out.close(); } - + public void uploadFile(String urlString, String localFileFullName) throws Exception { uploadFile(urlString, new File(localFileFullName)); } - + public void uploadFile(String urlString, File localFile) throws Exception { HttpURLConnection conn = getHTTPConnection(urlString, "PUT"); OutputStream out = null; InputStream in = null; BufferedReader br = null; - + try { out = conn.getOutputStream(); in = new FileInputStream(localFile); @@ -312,7 +311,7 @@ public class VmwareContext { out.write(buf, 0, len); } out.flush(); - + br = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = br.readLine()) != null) { @@ -322,41 +321,41 @@ public class VmwareContext { } finally { if(in != null) in.close(); - + if(out != null) out.close(); - + if(br != null) br.close(); } } - - public void uploadVmdkFile(String httpMethod, String urlString, String localFileName, + + public void uploadVmdkFile(String httpMethod, String urlString, String localFileName, long totalBytesUpdated, ActionDelegate progressUpdater) throws Exception { - + HttpURLConnection conn = getRawHTTPConnection(urlString); - + conn.setDoOutput(true); conn.setUseCaches(false); - - conn.setChunkedStreamingMode(_CHUNKSIZE); - conn.setRequestMethod(httpMethod); - conn.setRequestProperty("Connection", "Keep-Alive"); - conn.setRequestProperty("Content-Type", "application/x-vnd.vmware-streamVmdk"); + + conn.setChunkedStreamingMode(_CHUNKSIZE); + conn.setRequestMethod(httpMethod); + conn.setRequestProperty("Connection", "Keep-Alive"); + conn.setRequestProperty("Content-Type", "application/x-vnd.vmware-streamVmdk"); conn.setRequestProperty("Content-Length", Long.toString(new File(localFileName).length())); connectWithRetry(conn); - + BufferedOutputStream bos = null; BufferedInputStream is = null; try { - bos = new BufferedOutputStream(conn.getOutputStream()); - is = new BufferedInputStream(new FileInputStream(localFileName)); - int bufferSize = _CHUNKSIZE; - byte[] buffer = new byte[bufferSize]; - while (true) { - int bytesRead = is.read(buffer, 0, bufferSize); - if (bytesRead == -1) { - break; + bos = new BufferedOutputStream(conn.getOutputStream()); + is = new BufferedInputStream(new FileInputStream(localFileName)); + int bufferSize = _CHUNKSIZE; + byte[] buffer = new byte[bufferSize]; + while (true) { + int bytesRead = is.read(buffer, 0, bufferSize); + if (bytesRead == -1) { + break; } bos.write(buffer, 0, bytesRead); totalBytesUpdated += bytesRead; @@ -370,54 +369,54 @@ public class VmwareContext { is.close(); if(bos != null) bos.close(); - + conn.disconnect(); } } - - public long downloadVmdkFile(String urlString, String localFileName, + + public long downloadVmdkFile(String urlString, String localFileName, long totalBytesDownloaded, ActionDelegate progressUpdater) throws Exception { HttpURLConnection conn = getRawHTTPConnection(urlString); - + String cookieString = getServiceCookie(); conn.setRequestProperty(org.apache.axis.transport.http.HTTPConstants.HEADER_COOKIE, cookieString); conn.setDoInput(true); - conn.setDoOutput(true); + conn.setDoOutput(true); conn.setAllowUserInteraction(true); connectWithRetry(conn); - long bytesWritten = 0; - InputStream in = null; - OutputStream out = null; + long bytesWritten = 0; + InputStream in = null; + OutputStream out = null; try { - in = conn.getInputStream(); - out = new FileOutputStream(new File(localFileName)); - - byte[] buf = new byte[_CHUNKSIZE]; - int len = 0; - while ((len = in.read(buf)) > 0) { - out.write(buf, 0, len); + in = conn.getInputStream(); + out = new FileOutputStream(new File(localFileName)); + + byte[] buf = new byte[_CHUNKSIZE]; + int len = 0; + while ((len = in.read(buf)) > 0) { + out.write(buf, 0, len); bytesWritten += len; totalBytesDownloaded += len; - + if(progressUpdater != null) progressUpdater.action(new Long(totalBytesDownloaded)); - } + } } finally { if(in != null) in.close(); if(out != null) out.close(); - + conn.disconnect(); } - return bytesWritten; + return bytesWritten; } - + public byte[] getResourceContent(String urlString) throws Exception { HttpURLConnection conn = getHTTPConnection(urlString); InputStream in = conn.getInputStream(); - + ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buf = new byte[_CHUNKSIZE]; int len = 0; @@ -425,10 +424,10 @@ public class VmwareContext { out.write(buf, 0, len); } in.close(); - out.close(); + out.close(); return out.toByteArray(); } - + public void uploadResourceContent(String urlString, byte[] content) throws Exception { // vSphere does not support POST HttpURLConnection conn = getHTTPConnection(urlString, "PUT"); @@ -436,7 +435,7 @@ public class VmwareContext { OutputStream out = conn.getOutputStream(); out.write(content); out.flush(); - + BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { @@ -446,15 +445,15 @@ public class VmwareContext { out.close(); in.close(); } - + /* * Sample content returned by query a datastore directory - * + * * Url for the query * https://vsphere-1.lab.vmops.com/folder/Fedora-clone-test?dcPath=cupertino&dsName=NFS+datastore * * Returned conent from vSphere - * + * <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> @@ -485,7 +484,7 @@ public class VmwareContext { </table> </body> </html> - */ + */ public String[] listDatastoreDirContent(String urlString) throws Exception { List<String> fileList = new ArrayList<String>(); String content = new String(getResourceContent(urlString)); @@ -497,7 +496,7 @@ public class VmwareContext { int beginPos = content.lastIndexOf('>', parsePos -1); if(beginPos < 0) beginPos = 0; - + fileList.add((content.substring(beginPos + 1, parsePos))); parsePos += marker.length(); } else { @@ -511,11 +510,11 @@ public class VmwareContext { DatastoreFile dsFile = new DatastoreFile(fullPath); return composeDatastoreBrowseUrl(dcName, dsFile.getDatastoreName(), dsFile.getRelativePath()); } - + public String composeDatastoreBrowseUrl(String dcName, String datastoreName, String relativePath) { assert(relativePath != null); assert(datastoreName != null); - + StringBuffer sb = new StringBuffer(); sb.append("https://"); sb.append(_serverAddress); @@ -525,23 +524,24 @@ public class VmwareContext { sb.append(URLEncoder.encode(datastoreName)); return sb.toString(); } - + public HttpURLConnection getHTTPConnection(String urlString) throws Exception { return getHTTPConnection(urlString, "GET"); } - + public HttpURLConnection getHTTPConnection(String urlString, String httpMethod) throws Exception { String cookieString = getServiceCookie(); HostnameVerifier hv = new HostnameVerifier() { - public boolean verify(String urlHostName, SSLSession session) { + @Override + public boolean verify(String urlHostName, SSLSession session) { return true; } }; - - HttpsURLConnection.setDefaultHostnameVerifier(hv); - URL url = new URL(urlString); + + HttpsURLConnection.setDefaultHostnameVerifier(hv); + URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); - + conn.setDoInput(true); conn.setDoOutput(true); conn.setAllowUserInteraction(true); @@ -550,16 +550,17 @@ public class VmwareContext { connectWithRetry(conn); return conn; } - + public HttpURLConnection getRawHTTPConnection(String urlString) throws Exception { HostnameVerifier hv = new HostnameVerifier() { - public boolean verify(String urlHostName, SSLSession session) { + @Override + public boolean verify(String urlHostName, SSLSession session) { return true; } }; - - HttpsURLConnection.setDefaultHostnameVerifier(hv); - URL url = new URL(urlString); + + HttpsURLConnection.setDefaultHostnameVerifier(hv); + URL url = new URL(urlString); return (HttpURLConnection)url.openConnection(); } @@ -571,7 +572,7 @@ public class VmwareContext { String cookieString = (String)msgContext.getProperty(org.apache.axis.transport.http.HTTPConstants.HEADER_COOKIE); return cookieString; } - + private static void connectWithRetry(HttpURLConnection conn) throws Exception { boolean connected = false; for(int i = 0; i < MAX_CONNECT_RETRY && !connected; i++) { @@ -581,45 +582,48 @@ public class VmwareContext { s_logger.info("Connected, conn: " + conn.toString() + ", retry: " + i); } catch (Exception e) { s_logger.warn("Unable to connect, conn: " + conn.toString() + ", message: " + e.toString() + ", retry: " + i); - + try { Thread.sleep(CONNECT_RETRY_INTERVAL); } catch(InterruptedException ex) { } } } - + if(!connected) throw new Exception("Unable to connect to " + conn.toString()); } - + public void close() { try { - _appUtil.disConnect(); + _vimClient.disconnect(); } catch(Exception e) { s_logger.warn("Unexpected exception: ", e); } } public static class TrustAllManager implements javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager { - public java.security.cert.X509Certificate[] getAcceptedIssuers() { + @Override + public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } - + public boolean isServerTrusted(java.security.cert.X509Certificate[] certs) { return true; } - + public boolean isClientTrusted(java.security.cert.X509Certificate[] certs) { return true; } - - public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) + + @Override + public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) throws java.security.cert.CertificateException { return; } - - public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) + + @Override + public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) throws java.security.cert.CertificateException { return; }
