diff --git a/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareContextFactory.java b/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareContextFactory.java index ed607e118d2..3079998198c 100755 --- a/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareContextFactory.java +++ b/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareContextFactory.java @@ -80,8 +80,15 @@ public class VmwareContextFactory { public static VmwareContext getContext(String vCenterAddress, String vCenterUserName, String vCenterPassword) throws Exception { VmwareContext context = s_pool.getContext(vCenterAddress, vCenterUserName); - if(context == null) + if(context == null) { context = create(vCenterAddress, vCenterUserName, vCenterPassword); + } else { + if(!context.validate()) { + s_logger.info("Validation of the context faild. dispose and create a new one"); + context.close(); + context = create(vCenterAddress, vCenterUserName, vCenterPassword); + } + } if(context != null) { context.registerStockObject(VmwareManager.CONTEXT_STOCK_NAME, s_vmwareMgr); diff --git a/plugins/hypervisors/vmware/src/com/cloud/storage/resource/VmwareSecondaryStorageContextFactory.java b/plugins/hypervisors/vmware/src/com/cloud/storage/resource/VmwareSecondaryStorageContextFactory.java index 5365e58e78e..253d6fd3517 100644 --- a/plugins/hypervisors/vmware/src/com/cloud/storage/resource/VmwareSecondaryStorageContextFactory.java +++ b/plugins/hypervisors/vmware/src/com/cloud/storage/resource/VmwareSecondaryStorageContextFactory.java @@ -16,11 +16,15 @@ // under the License. package com.cloud.storage.resource; +import org.apache.log4j.Logger; + import com.cloud.hypervisor.vmware.util.VmwareClient; import com.cloud.hypervisor.vmware.util.VmwareContext; import com.cloud.hypervisor.vmware.util.VmwareContextPool; public class VmwareSecondaryStorageContextFactory { + private static final Logger s_logger = Logger.getLogger(VmwareSecondaryStorageContextFactory.class); + private static volatile int s_seq = 1; private static VmwareContextPool s_pool; @@ -51,6 +55,12 @@ public class VmwareSecondaryStorageContextFactory { VmwareContext context = s_pool.getContext(vCenterAddress, vCenterUserName); if(context == null) { context = create(vCenterAddress, vCenterUserName, vCenterPassword); + } else { + if(!context.validate()) { + s_logger.info("Validation of the context faild. dispose and create a new one"); + context.close(); + context = create(vCenterAddress, vCenterUserName, vCenterPassword); + } } if(context != null) { diff --git a/vmware-base/src/com/cloud/hypervisor/vmware/mo/DatacenterMO.java b/vmware-base/src/com/cloud/hypervisor/vmware/mo/DatacenterMO.java index 1844b6ad37a..03ba2b75db3 100755 --- a/vmware-base/src/com/cloud/hypervisor/vmware/mo/DatacenterMO.java +++ b/vmware-base/src/com/cloud/hypervisor/vmware/mo/DatacenterMO.java @@ -20,6 +20,8 @@ package com.cloud.hypervisor.vmware.mo; import java.util.ArrayList; import java.util.List; +import org.apache.log4j.Logger; + import com.cloud.hypervisor.vmware.util.VmwareContext; import com.cloud.utils.Pair; import com.vmware.vim25.CustomFieldStringValue; @@ -37,6 +39,7 @@ import com.vmware.vim25.VirtualEthernetCardDistributedVirtualPortBackingInfo; import java.util.Arrays; public class DatacenterMO extends BaseMO { + private static final Logger s_logger = Logger.getLogger(DatacenterMO.class); public DatacenterMO(VmwareContext context, ManagedObjectReference morDc) { super(context, morDc); @@ -50,7 +53,9 @@ public class DatacenterMO extends BaseMO { super(context, null); _mor = _context.getVimClient().getDecendentMoRef(_context.getRootFolder(), "Datacenter", dcName); - assert(_mor != null); + if(_mor == null) { + s_logger.error("Unable to locate DC " + dcName); + } } @Override @@ -61,7 +66,6 @@ public class DatacenterMO extends BaseMO { public void registerTemplate(ManagedObjectReference morHost, String datastoreName, String templateName, String templateFileName) throws Exception { - ManagedObjectReference morFolder = (ManagedObjectReference)_context.getVimClient().getDynamicProperty( _mor, "vmFolder"); assert(morFolder != null); diff --git a/vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareClient.java b/vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareClient.java index d4e4256ebde..b20217a5b9f 100644 --- a/vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareClient.java +++ b/vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareClient.java @@ -107,7 +107,6 @@ public class VmwareClient { } private ManagedObjectReference SVC_INST_REF = new ManagedObjectReference(); - private ManagedObjectReference propCollectorRef; private static VimService vimService; private VimPortType vimPort; private String serviceCookie; @@ -151,8 +150,6 @@ public class VmwareClient { vimPort.login(serviceContent.getSessionManager(), userName, password, null); isConnected = true; - - propCollectorRef = serviceContent.getPropertyCollector(); } /** @@ -197,7 +194,7 @@ public class VmwareClient { * @return Service property collector */ public ManagedObjectReference getPropCol() { - return propCollectorRef; + return getServiceContent().getPropertyCollector(); } /** @@ -207,6 +204,43 @@ public class VmwareClient { return getServiceContent().getRootFolder(); } + public boolean validate() { + // + // There is no official API to validate an open vCenter API session. This is hacking way to tell if + // an open vCenter API session is still valid for making calls. + // + // It will give false result if there really does not exist data-center in the inventory, however, I consider + // this really is not possible in production deployment + // + + // Create PropertySpecs + PropertySpec pSpec = new PropertySpec(); + pSpec.setType("Datacenter"); + pSpec.setAll(false); + pSpec.getPathSet().add("name"); + + ObjectSpec oSpec = new ObjectSpec(); + oSpec.setObj(getRootFolder()); + oSpec.setSkip(false); + oSpec.getSelectSet().addAll(constructCompleteTraversalSpec()); + + PropertyFilterSpec spec = new PropertyFilterSpec(); + spec.getPropSet().add(pSpec); + spec.getObjectSet().add(oSpec); + List specArr = new ArrayList(); + specArr.add(spec); + + try { + List ocary = vimPort.retrieveProperties(getPropCol(), specArr); + if(ocary != null && ocary.size() > 0) + return true; + } catch(Exception e) { + return false; + } + + return false; + } + /** * Get the property value of a managed object. * @@ -266,7 +300,7 @@ public class VmwareClient { List specArr = new ArrayList(); specArr.add(spec); - return vimPort.retrieveProperties(propCollectorRef, specArr); + return vimPort.retrieveProperties(getPropCol(), specArr); } /** @@ -334,7 +368,8 @@ public class VmwareClient { pSpec.setType(objmor.getType()); spec.getPropSet().add(pSpec); - ManagedObjectReference filterSpecRef = vimPort.createFilter(propCollectorRef, spec, true); + ManagedObjectReference propertyCollector = this.getPropCol(); + ManagedObjectReference filterSpecRef = vimPort.createFilter(propertyCollector, spec, true); boolean reached = false; @@ -343,7 +378,7 @@ public class VmwareClient { List objupary = null; List propchgary = null; while (!reached) { - updateset = vimPort.waitForUpdates(propCollectorRef, version); + updateset = vimPort.waitForUpdates(propertyCollector, version); if (updateset == null || updateset.getFilterSet() == null) { continue; } @@ -546,7 +581,7 @@ public class VmwareClient { List specArr = new ArrayList(); specArr.add(spec); - List ocary = vimPort.retrieveProperties(propCollectorRef, specArr); + List ocary = vimPort.retrieveProperties(getPropCol(), specArr); if (ocary == null || ocary.size() == 0) { return null; 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 6c3dab7f790..c499576f5f5 100755 --- a/vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareContext.java +++ b/vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareContext.java @@ -102,6 +102,10 @@ public class VmwareContext { if(s_logger.isInfoEnabled()) s_logger.info("New VmwareContext object, current outstanding count: " + getOutstandingContextCount()); } + + public boolean validate() { + return _vimClient.validate(); + } public void registerStockObject(String name, Object obj) { synchronized(_stockMap) {