use of com.vmware.vim25.ManagedObjectReference in project opennms by OpenNMS.
the class VmwareViJavaAccessTest method setUp.
@Before
public void setUp() throws Exception {
// setup required objects
managedObjectReferenceManagedEntity = new ManagedObjectReference();
managedObjectReferenceManagedEntity.setType("ManagedEntity");
managedObjectReferenceManagedEntity.setVal("moIdMe");
managedObjectReferenceVirtualMachine = new ManagedObjectReference();
managedObjectReferenceVirtualMachine.setType("VirtualMachine");
managedObjectReferenceVirtualMachine.setVal("moIdVm");
managedObjectReferenceHostSystem = new ManagedObjectReference();
managedObjectReferenceHostSystem.setType("HostSystem");
managedObjectReferenceHostSystem.setVal("moIdHs");
// setup VmwareViJavaAccess
vmwareViJavaAccess = new VmwareViJavaAccess("hostname", "username", "password") {
@Override
protected void relax() {
}
};
// setup PerformanceManager
mockPerformanceManager = createMock(PerformanceManager.class);
// setup ServiceInstance mock
mockServiceInstance = createMock(ServiceInstance.class);
// setup ServerConnection
mockServerConnection = new ServerConnection(new URL("https://hostname/sdk"), new VimPortType(new WSClient("https://hostname/sdk") {
@Override
protected SSLSocketFactory getTrustAllSocketFactory(boolean ignoreCert) throws RemoteException {
return null;
}
}), mockServiceInstance);
// setup AboutInfo
mockAboutInfo = createMock(AboutInfo.class);
expectNew(ServiceInstance.class, new Class<?>[] { URL.class, String.class, String.class }, new URL("https://hostname/sdk"), "username", "password").andReturn(mockServiceInstance).anyTimes();
expect(mockServiceInstance.getServerConnection()).andReturn(mockServerConnection).anyTimes();
expect(mockServiceInstance.getPerformanceManager()).andReturn(mockPerformanceManager).anyTimes();
expect(mockServiceInstance.getAboutInfo()).andReturn(mockAboutInfo).anyTimes();
managedEntity = new ManagedEntity(null, managedObjectReferenceManagedEntity);
virtualMachine = new VirtualMachine(null, managedObjectReferenceVirtualMachine);
hostSystem = new HostSystem(null, managedObjectReferenceHostSystem);
// setup MorUtil
mockStatic(MorUtil.class);
expect(MorUtil.createExactManagedEntity(mockServerConnection, managedObjectReferenceManagedEntity)).andReturn(managedEntity).anyTimes();
expect(MorUtil.createExactManagedEntity(mockServerConnection, managedObjectReferenceVirtualMachine)).andReturn(virtualMachine).anyTimes();
expect(MorUtil.createExactManagedEntity(mockServerConnection, managedObjectReferenceHostSystem)).andReturn(hostSystem).anyTimes();
// setup about info
expect(mockAboutInfo.getApiVersion()).andReturn("2.x");
expect(mockAboutInfo.getApiVersion()).andReturn("3.x");
expect(mockAboutInfo.getApiVersion()).andReturn("4.x");
expect(mockAboutInfo.getApiVersion()).andReturn("5.x");
expect(mockAboutInfo.getApiVersion()).andReturn("6.x");
expect(mockAboutInfo.getApiVersion()).andReturn("x");
// setup performance data
int refreshRate = 100;
int metricCount = 15;
int instanceModifier = 5;
perfQuerySpec = new PerfQuerySpec();
perfQuerySpec.setEntity(managedEntity.getMOR());
perfQuerySpec.setMaxSample(new Integer(1));
perfQuerySpec.setIntervalId(refreshRate);
perfEntityMetricBases = new PerfEntityMetricBase[metricCount];
perfCounterInfos = new PerfCounterInfo[metricCount];
for (int i = 0; i < metricCount; i++) {
ElementDescription groupInfo = new ElementDescription();
groupInfo.setKey("key" + i);
ElementDescription nameInfo = new ElementDescription();
nameInfo.setKey("name" + i);
perfCounterInfos[i] = new PerfCounterInfo();
perfCounterInfos[i].setKey(i);
perfCounterInfos[i].setGroupInfo(groupInfo);
perfCounterInfos[i].setNameInfo(nameInfo);
perfCounterInfos[i].setRollupType(PerfSummaryType.average);
perfEntityMetricBases[i] = new PerfEntityMetric();
PerfMetricIntSeries[] perfMetricIntSeries;
int instanceCount = (i % instanceModifier) + 1;
perfMetricIntSeries = new PerfMetricIntSeries[instanceCount];
for (int b = 0; b < instanceCount; b++) {
PerfMetricId perfMetricId = new PerfMetricId();
perfMetricId.setCounterId(i);
if (instanceCount == 1) {
perfMetricId.setInstance(null);
} else {
perfMetricId.setInstance("instance" + b);
}
perfMetricIntSeries[b] = new PerfMetricIntSeries();
perfMetricIntSeries[b].setValue(new long[] { (long) 42 });
perfMetricIntSeries[b].setId(perfMetricId);
}
((PerfEntityMetric) perfEntityMetricBases[i]).setValue(perfMetricIntSeries);
}
// setup PerfProviderSummary
mockPerfProviderSummary = createMock(PerfProviderSummary.class);
expect(mockPerformanceManager.queryPerfProviderSummary(managedEntity)).andReturn(mockPerfProviderSummary).anyTimes();
expect(mockPerfProviderSummary.getRefreshRate()).andReturn(refreshRate).anyTimes();
expect(mockPerformanceManager.getPerfCounter()).andReturn(perfCounterInfos).anyTimes();
expect(mockPerformanceManager.queryPerf(anyObject(PerfQuerySpec[].class))).andReturn(perfEntityMetricBases).anyTimes();
// setup network info
HostNetworkInfo hostNetworkInfo = new HostNetworkInfo();
int numberOfVnics = 3;
HostVirtualNic[] hostVirtualNics = new HostVirtualNic[numberOfVnics];
for (int i = 0; i < numberOfVnics; i++) {
HostVirtualNicSpec hostVirtualNicSpec = new HostVirtualNicSpec();
HostIpConfig hostIpConfig = new HostIpConfig();
hostIpConfig.setIpAddress("192.168.1." + (i + 1));
hostVirtualNicSpec.setIp(hostIpConfig);
hostVirtualNics[i] = new HostVirtualNic();
hostVirtualNics[i].setSpec(hostVirtualNicSpec);
}
hostNetworkInfo.setVnic(hostVirtualNics);
HostVirtualNic[] hostVirtualConsoleNics = new HostVirtualNic[numberOfVnics];
for (int i = 0; i < numberOfVnics; i++) {
HostVirtualNicSpec hostVirtualNicSpec = new HostVirtualNicSpec();
HostIpConfig hostIpConfig = new HostIpConfig();
hostIpConfig.setIpAddress("192.168.2." + (i + 1));
hostVirtualNicSpec.setIp(hostIpConfig);
hostVirtualConsoleNics[i] = new HostVirtualNic();
hostVirtualConsoleNics[i].setSpec(hostVirtualNicSpec);
}
hostNetworkInfo.setConsoleVnic(hostVirtualConsoleNics);
HostServiceTicket hostServiceTicket = new HostServiceTicket();
hostServiceTicket.setSessionId("sessionId");
// setup HostSystem
mockHostSystem = createMock(HostSystem.class);
// setup HostNetworkSystem
mockHostNetworkSystem = createMock(HostNetworkSystem.class);
// setup CIMClient
mockCIMClient = createPartialMock(CIMClient.class, "enumerateInstances");
// setup the cim objects
cimObjects = new ArrayList<CIMObject>();
int cimObjectCount = 5;
for (int i = 0; i < cimObjectCount; i++) {
CIMInstance cimInstance = new CIMInstance();
cimInstance.setName("cimInstance" + i);
cimObjects.add(cimInstance);
}
expect(mockHostSystem.getName()).andReturn("mockesxi01.local").anyTimes();
expect(mockHostSystem.getHostNetworkSystem()).andReturn(mockHostNetworkSystem).anyTimes();
expect(mockHostSystem.acquireCimServicesTicket()).andReturn(hostServiceTicket).anyTimes();
expect(mockHostNetworkSystem.getNetworkInfo()).andReturn(hostNetworkInfo).anyTimes();
expectNew(CIMClient.class, new Class<?>[] { CIMNameSpace.class, Principal.class, Object.class }, anyObject(), anyObject(), anyObject()).andReturn(mockCIMClient).anyTimes();
suppress(method(CIMClient.class, "useMPost"));
expect(mockCIMClient.enumerateInstances(new CIMObjectPath("cimClass"))).andReturn(Collections.enumeration(cimObjects)).anyTimes();
}
use of com.vmware.vim25.ManagedObjectReference in project opennms by OpenNMS.
the class VmwareConfigBuilder method lookupMetrics.
private void lookupMetrics(String collectionName, String managedObjectId) throws Exception {
ManagedObjectReference managedObjectReference = new ManagedObjectReference();
managedObjectReference.setType("ManagedEntity");
managedObjectReference.setVal(managedObjectId);
ManagedEntity managedEntity = MorUtil.createExactManagedEntity(serviceInstance.getServerConnection(), managedObjectReference);
int refreshRate = performanceManager.queryPerfProviderSummary(managedEntity).getRefreshRate();
PerfQuerySpec perfQuerySpec = new PerfQuerySpec();
perfQuerySpec.setEntity(managedEntity.getMOR());
perfQuerySpec.setMaxSample(Integer.valueOf(1));
perfQuerySpec.setIntervalId(refreshRate);
PerfEntityMetricBase[] perfEntityMetricBases = performanceManager.queryPerf(new PerfQuerySpec[] { perfQuerySpec });
HashMap<String, TreeSet<VMwareConfigMetric>> groupMap = new HashMap<String, TreeSet<VMwareConfigMetric>>();
HashMap<String, Boolean> multiInstance = new HashMap<String, Boolean>();
if (perfEntityMetricBases != null) {
for (int i = 0; i < perfEntityMetricBases.length; i++) {
PerfMetricSeries[] perfMetricSeries = ((PerfEntityMetric) perfEntityMetricBases[i]).getValue();
for (int j = 0; perfMetricSeries != null && j < perfMetricSeries.length; j++) {
if (perfMetricSeries[j] instanceof PerfMetricIntSeries) {
long[] longs = ((PerfMetricIntSeries) perfMetricSeries[j]).getValue();
if (longs.length == 1) {
PerfCounterInfo perfCounterInfo = perfCounterInfoMap.get(perfMetricSeries[j].getId().getCounterId());
String instanceName = perfMetricSeries[j].getId().getInstance();
String humanReadableName = getHumanReadableName(perfCounterInfo);
String aliasName = getAliasName(perfCounterInfo);
String groupName = perfCounterInfo.getGroupInfo().getKey();
String normalizedGroupName = normalizeGroupName(groupName);
Boolean b = multiInstance.get(getHumanReadableName(perfCounterInfo));
if (b == null) {
b = Boolean.valueOf(instanceName != null && !"".equals(instanceName));
} else {
b = Boolean.valueOf(b.booleanValue() || (instanceName != null && !"".equals(instanceName)));
}
if (!b) {
groupName = "Node";
normalizedGroupName = "Node";
}
if (!groupMap.containsKey(normalizedGroupName)) {
groupMap.put(normalizedGroupName, new TreeSet<VMwareConfigMetric>());
}
TreeSet<VMwareConfigMetric> counterSet = groupMap.get(normalizedGroupName);
multiInstance.put(getHumanReadableName(perfCounterInfo), b);
counterSet.add(new VMwareConfigMetric(perfCounterInfo, humanReadableName, aliasName, b, normalizedGroupName));
}
}
}
}
}
collections.put(collectionName, groupMap);
}
use of com.vmware.vim25.ManagedObjectReference in project CloudStack-archive by CloudStack-extras.
the class VirtualMachineMO method copyAllVmDiskFiles.
public void copyAllVmDiskFiles(DatastoreMO destDsMo, String destDsDir, boolean followDiskChain) throws Exception {
VirtualDevice[] disks = getAllDiskDevice();
DatacenterMO dcMo = getOwnerDatacenter().first();
if (disks != null) {
for (VirtualDevice disk : disks) {
List<Pair<String, ManagedObjectReference>> vmdkFiles = this.getDiskDatastorePathChain((VirtualDisk) disk, followDiskChain);
for (Pair<String, ManagedObjectReference> fileItem : vmdkFiles) {
DatastoreMO srcDsMo = new DatastoreMO(_context, fileItem.second());
DatastoreFile srcFile = new DatastoreFile(fileItem.first());
DatastoreFile destFile = new DatastoreFile(destDsMo.getName(), destDsDir, srcFile.getFileName());
Pair<VmdkFileDescriptor, byte[]> vmdkDescriptor = null;
vmdkDescriptor = getVmdkFileInfo(fileItem.first());
s_logger.info("Copy VM disk file " + srcFile.getPath() + " to " + destFile.getPath());
srcDsMo.copyDatastoreFile(fileItem.first(), dcMo.getMor(), destDsMo.getMor(), destFile.getPath(), dcMo.getMor(), true);
if (vmdkDescriptor != null) {
String vmdkBaseFileName = vmdkDescriptor.first().getBaseFileName();
String baseFilePath = srcFile.getCompanionPath(vmdkBaseFileName);
destFile = new DatastoreFile(destDsMo.getName(), destDsDir, vmdkBaseFileName);
s_logger.info("Copy VM disk file " + baseFilePath + " to " + destFile.getPath());
srcDsMo.copyDatastoreFile(baseFilePath, dcMo.getMor(), destDsMo.getMor(), destFile.getPath(), dcMo.getMor(), true);
}
}
}
}
}
use of com.vmware.vim25.ManagedObjectReference in project CloudStack-archive by CloudStack-extras.
the class VirtualMachineMO method redoRegistration.
public void redoRegistration(ManagedObjectReference morHost) throws Exception {
String vmName = getVmName();
VirtualMachineFileInfo vmFileInfo = getFileInfo();
boolean isTemplate = isTemplate();
HostMO hostMo;
if (morHost != null)
hostMo = new HostMO(getContext(), morHost);
else
hostMo = getRunningHost();
ManagedObjectReference morFolder = getParentMor();
ManagedObjectReference morPool = hostMo.getHyperHostOwnerResourcePool();
_context.getService().unregisterVM(_mor);
ManagedObjectReference morTask = _context.getService().registerVM_Task(morFolder, vmFileInfo.getVmPathName(), vmName, false, morPool, hostMo.getMor());
String result = _context.getServiceUtil().waitForTask(morTask);
if (!result.equalsIgnoreCase("Sucess")) {
throw new Exception("Unable to register template due to " + TaskMO.getTaskFailureInfo(_context, morTask));
} else {
_context.waitForTaskProgressDone(morTask);
if (isTemplate) {
VirtualMachineMO vmNewRegistration = hostMo.findVmOnHyperHost(vmName);
assert (vmNewRegistration != null);
vmNewRegistration.markAsTemplate();
}
}
}
use of com.vmware.vim25.ManagedObjectReference in project CloudStack-archive by CloudStack-extras.
the class VirtualMachineMO method attachIso.
// isoDatastorePath: [datastore name] isoFilePath
public void attachIso(String isoDatastorePath, ManagedObjectReference morDs, boolean connect, boolean connectAtBoot) throws Exception {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - detachIso(). target MOR: " + _mor.get_value() + ", isoDatastorePath: " + isoDatastorePath + ", datastore: " + morDs.get_value() + ", connect: " + connect + ", connectAtBoot: " + connectAtBoot);
assert (isoDatastorePath != null);
assert (morDs != null);
boolean newCdRom = false;
VirtualCdrom cdRom = (VirtualCdrom) getIsoDevice();
if (cdRom == null) {
newCdRom = true;
cdRom = new VirtualCdrom();
cdRom.setControllerKey(getIDEDeviceControllerKey());
int deviceNumber = getNextIDEDeviceNumber();
cdRom.setUnitNumber(deviceNumber);
cdRom.setKey(-deviceNumber);
}
VirtualDeviceConnectInfo cInfo = new VirtualDeviceConnectInfo();
cInfo.setConnected(connect);
cInfo.setStartConnected(connectAtBoot);
cdRom.setConnectable(cInfo);
VirtualCdromIsoBackingInfo backingInfo = new VirtualCdromIsoBackingInfo();
backingInfo.setFileName(isoDatastorePath);
backingInfo.setDatastore(morDs);
cdRom.setBacking(backingInfo);
VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec();
VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1];
VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
deviceConfigSpec.setDevice(cdRom);
if (newCdRom) {
deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.add);
} else {
deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.edit);
}
deviceConfigSpecArray[0] = deviceConfigSpec;
reConfigSpec.setDeviceChange(deviceConfigSpecArray);
ManagedObjectReference morTask = _context.getService().reconfigVM_Task(_mor, reConfigSpec);
String result = _context.getServiceUtil().waitForTask(morTask);
if (!result.equals("sucess")) {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - detachIso() done(failed)");
throw new Exception("Failed to attach ISO due to " + TaskMO.getTaskFailureInfo(_context, morTask));
}
_context.waitForTaskProgressDone(morTask);
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - detachIso() done(successfully)");
}
Aggregations