Search in sources :

Example 1 with ManagedObjectReference

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();
}
Also used : HostVirtualNic(com.vmware.vim25.HostVirtualNic) PerfCounterInfo(com.vmware.vim25.PerfCounterInfo) CIMObject(org.sblim.wbem.cim.CIMObject) CIMClient(org.sblim.wbem.client.CIMClient) HostIpConfig(com.vmware.vim25.HostIpConfig) HostVirtualNicSpec(com.vmware.vim25.HostVirtualNicSpec) ServiceInstance(com.vmware.vim25.mo.ServiceInstance) URL(java.net.URL) CIMInstance(org.sblim.wbem.cim.CIMInstance) PerfMetricIntSeries(com.vmware.vim25.PerfMetricIntSeries) PerformanceManager(com.vmware.vim25.mo.PerformanceManager) HostSystem(com.vmware.vim25.mo.HostSystem) HostServiceTicket(com.vmware.vim25.HostServiceTicket) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) VmwareViJavaAccess(org.opennms.protocols.vmware.VmwareViJavaAccess) ElementDescription(com.vmware.vim25.ElementDescription) HostNetworkSystem(com.vmware.vim25.mo.HostNetworkSystem) ManagedEntity(com.vmware.vim25.mo.ManagedEntity) HostNetworkInfo(com.vmware.vim25.HostNetworkInfo) WSClient(com.vmware.vim25.ws.WSClient) PerfMetricId(com.vmware.vim25.PerfMetricId) AboutInfo(com.vmware.vim25.AboutInfo) CIMObjectPath(org.sblim.wbem.cim.CIMObjectPath) ServerConnection(com.vmware.vim25.mo.ServerConnection) VimPortType(com.vmware.vim25.VimPortType) PerfProviderSummary(com.vmware.vim25.PerfProviderSummary) PerfQuerySpec(com.vmware.vim25.PerfQuerySpec) RemoteException(java.rmi.RemoteException) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) VirtualMachine(com.vmware.vim25.mo.VirtualMachine) PerfEntityMetric(com.vmware.vim25.PerfEntityMetric) Before(org.junit.Before)

Example 2 with ManagedObjectReference

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);
}
Also used : ManagedEntity(com.vmware.vim25.mo.ManagedEntity) PerfCounterInfo(com.vmware.vim25.PerfCounterInfo) PerfMetricSeries(com.vmware.vim25.PerfMetricSeries) HashMap(java.util.HashMap) PerfEntityMetricBase(com.vmware.vim25.PerfEntityMetricBase) PerfMetricIntSeries(com.vmware.vim25.PerfMetricIntSeries) TreeSet(java.util.TreeSet) PerfQuerySpec(com.vmware.vim25.PerfQuerySpec) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) PerfEntityMetric(com.vmware.vim25.PerfEntityMetric)

Example 3 with ManagedObjectReference

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);
                }
            }
        }
    }
}
Also used : VirtualDevice(com.vmware.vim25.VirtualDevice) Pair(com.cloud.utils.Pair) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 4 with ManagedObjectReference

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();
        }
    }
}
Also used : VirtualMachineFileInfo(com.vmware.vim25.VirtualMachineFileInfo) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 5 with ManagedObjectReference

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)");
}
Also used : VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) VirtualCdromIsoBackingInfo(com.vmware.vim25.VirtualCdromIsoBackingInfo) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) VirtualCdrom(com.vmware.vim25.VirtualCdrom) VirtualDeviceConnectInfo(com.vmware.vim25.VirtualDeviceConnectInfo) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Aggregations

ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)236 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)62 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)57 Pair (com.cloud.utils.Pair)49 RemoteException (java.rmi.RemoteException)48 ArrayList (java.util.ArrayList)45 UnsupportedEncodingException (java.io.UnsupportedEncodingException)39 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)35 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)32 IOException (java.io.IOException)31 CloudException (com.cloud.exception.CloudException)30 DatastoreMO (com.cloud.hypervisor.vmware.mo.DatastoreMO)30 VirtualMachineConfigSpec (com.vmware.vim25.VirtualMachineConfigSpec)29 ObjectContent (com.vmware.vim25.ObjectContent)28 VirtualDeviceConfigSpec (com.vmware.vim25.VirtualDeviceConfigSpec)27 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)25 ConfigurationException (javax.naming.ConfigurationException)24 InternalErrorException (com.cloud.exception.InternalErrorException)23 VirtualDisk (com.vmware.vim25.VirtualDisk)23 ConnectException (java.net.ConnectException)23