Search in sources :

Example 1 with ElementDescription

use of com.vmware.vim25.ElementDescription 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", "getSessionProperties");
    // setup the cim objects
    cimObjects = new ArrayList<>();
    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();
    SessionProperties sessionProperties = new SessionProperties();
    expect(mockCIMClient.getSessionProperties()).andReturn(sessionProperties).anyTimes();
}
Also used : HostVirtualNic(com.vmware.vim25.HostVirtualNic) PerfCounterInfo(com.vmware.vim25.PerfCounterInfo) 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) SessionProperties(org.sblim.wbem.util.SessionProperties) 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 ElementDescription

use of com.vmware.vim25.ElementDescription in project cloudstack by apache.

the class VirtualMachineMO method detachIso.

public void detachIso(String isoDatastorePath) throws Exception {
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - detachIso(). target MOR: " + _mor.getValue() + ", isoDatastorePath: " + isoDatastorePath);
    VirtualDevice device = getIsoDevice();
    if (device == null) {
        if (s_logger.isTraceEnabled())
            s_logger.trace("vCenter API trace - detachIso() done(failed)");
        throw new Exception("Unable to find a CDROM device");
    }
    VirtualCdromRemotePassthroughBackingInfo backingInfo = new VirtualCdromRemotePassthroughBackingInfo();
    backingInfo.setDeviceName("");
    device.setBacking(backingInfo);
    VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec();
    //VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1];
    VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
    deviceConfigSpec.setDevice(device);
    deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.EDIT);
    //deviceConfigSpecArray[0] = deviceConfigSpec;
    reConfigSpec.getDeviceChange().add(deviceConfigSpec);
    ManagedObjectReference morTask = _context.getService().reconfigVMTask(_mor, reConfigSpec);
    // Monitor VM questions
    final Boolean[] flags = { false };
    final VirtualMachineMO vmMo = this;
    Future<?> future = MonitorServiceExecutor.submit(new Runnable() {

        @Override
        public void run() {
            s_logger.info("VM Question monitor started...");
            while (!flags[0]) {
                try {
                    VirtualMachineRuntimeInfo runtimeInfo = vmMo.getRuntimeInfo();
                    VirtualMachineQuestionInfo question = runtimeInfo.getQuestion();
                    if (question != null) {
                        if (s_logger.isTraceEnabled()) {
                            s_logger.trace("Question id: " + question.getId());
                            s_logger.trace("Question text: " + question.getText());
                        }
                        if (question.getMessage() != null) {
                            for (VirtualMachineMessage msg : question.getMessage()) {
                                if (s_logger.isTraceEnabled()) {
                                    s_logger.trace("msg id: " + msg.getId());
                                    s_logger.trace("msg text: " + msg.getText());
                                }
                                if ("msg.cdromdisconnect.locked".equalsIgnoreCase(msg.getId())) {
                                    s_logger.info("Found that VM has a pending question that we need to answer programmatically, question id: " + msg.getId() + ", for safe operation we will automatically decline it");
                                    vmMo.answerVM(question.getId(), "1");
                                    break;
                                }
                            }
                        } else if (question.getText() != null) {
                            String text = question.getText();
                            String msgId;
                            String msgText;
                            if (s_logger.isDebugEnabled()) {
                                s_logger.debug("question text : " + text);
                            }
                            String[] tokens = text.split(":");
                            msgId = tokens[0];
                            msgText = tokens[1];
                            if ("msg.cdromdisconnect.locked".equalsIgnoreCase(msgId)) {
                                s_logger.info("Found that VM has a pending question that we need to answer programmatically, question id: " + question.getId() + ". Message id : " + msgId + ". Message text : " + msgText + ", for safe operation we will automatically decline it.");
                                vmMo.answerVM(question.getId(), "1");
                            }
                        }
                        ChoiceOption choice = question.getChoice();
                        if (choice != null) {
                            for (ElementDescription info : choice.getChoiceInfo()) {
                                if (s_logger.isTraceEnabled()) {
                                    s_logger.trace("Choice option key: " + info.getKey());
                                    s_logger.trace("Choice option label: " + info.getLabel());
                                }
                            }
                        }
                    }
                } catch (Throwable e) {
                    s_logger.error("Unexpected exception: ", e);
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    s_logger.debug("[ignored] interupted while handling vm question about iso detach.");
                }
            }
            s_logger.info("VM Question monitor stopped");
        }
    });
    try {
        boolean result = _context.getVimClient().waitForTask(morTask);
        if (!result) {
            if (s_logger.isDebugEnabled())
                s_logger.trace("vCenter API trace - detachIso() done(failed)");
            throw new Exception("Failed to detachIso due to " + TaskMO.getTaskFailureInfo(_context, morTask));
        }
        _context.waitForTaskProgressDone(morTask);
        s_logger.trace("vCenter API trace - detachIso() done(successfully)");
    } finally {
        flags[0] = true;
        future.cancel(true);
    }
}
Also used : VirtualMachineQuestionInfo(com.vmware.vim25.VirtualMachineQuestionInfo) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) VirtualMachineRuntimeInfo(com.vmware.vim25.VirtualMachineRuntimeInfo) VirtualMachineMessage(com.vmware.vim25.VirtualMachineMessage) VirtualDevice(com.vmware.vim25.VirtualDevice) VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) VirtualCdromRemotePassthroughBackingInfo(com.vmware.vim25.VirtualCdromRemotePassthroughBackingInfo) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) ChoiceOption(com.vmware.vim25.ChoiceOption) ElementDescription(com.vmware.vim25.ElementDescription)

Example 3 with ElementDescription

use of com.vmware.vim25.ElementDescription in project cloudstack by apache.

the class VirtualMachineMO method powerOn.

public boolean powerOn() throws Exception {
    if (getResetSafePowerState() == VirtualMachinePowerState.POWERED_ON)
        return true;
    ManagedObjectReference morTask = _context.getService().powerOnVMTask(_mor, null);
    // Monitor VM questions
    final Boolean[] flags = { false };
    final VirtualMachineMO vmMo = this;
    Future<?> future = MonitorServiceExecutor.submit(new Runnable() {

        @Override
        public void run() {
            s_logger.info("VM Question monitor started...");
            while (!flags[0]) {
                try {
                    VirtualMachineRuntimeInfo runtimeInfo = vmMo.getRuntimeInfo();
                    VirtualMachineQuestionInfo question = runtimeInfo.getQuestion();
                    if (question != null) {
                        s_logger.info("Question id: " + question.getId());
                        s_logger.info("Question text: " + question.getText());
                        if (question.getMessage() != null) {
                            for (VirtualMachineMessage msg : question.getMessage()) {
                                if (s_logger.isInfoEnabled()) {
                                    s_logger.info("msg id: " + msg.getId());
                                    s_logger.info("msg text: " + msg.getText());
                                }
                                String logMsg = "Found that VM has a pending question that we need to answer programmatically, question id: " + msg.getId();
                                if ("msg.uuid.altered".equalsIgnoreCase(msg.getId())) {
                                    s_logger.info(logMsg + ", we will automatically answer as 'moved it' to address out of band HA for the VM");
                                    vmMo.answerVM(question.getId(), "1");
                                    break;
                                } else if (msg.getId().equalsIgnoreCase("msg.cpuid.noVHVQuestion")) {
                                    s_logger.info(logMsg + ", automatically answering 'yes'");
                                    vmMo.answerVM(question.getId(), "0");
                                    break;
                                }
                            }
                        }
                        if (s_logger.isTraceEnabled())
                            s_logger.trace("These are the choices we can have just in case");
                        ChoiceOption choice = question.getChoice();
                        if (choice != null) {
                            for (ElementDescription info : choice.getChoiceInfo()) {
                                if (s_logger.isTraceEnabled()) {
                                    s_logger.trace("Choice option key: " + info.getKey());
                                    s_logger.trace("Choice option label: " + info.getLabel());
                                }
                            }
                        }
                    }
                } catch (Throwable e) {
                    s_logger.error("Unexpected exception: ", e);
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    s_logger.debug("[ignored] interupted while dealing with vm questions.");
                }
            }
            s_logger.info("VM Question monitor stopped");
        }
    });
    try {
        boolean result = _context.getVimClient().waitForTask(morTask);
        if (result) {
            _context.waitForTaskProgressDone(morTask);
            return true;
        } else {
            s_logger.error("VMware powerOnVM_Task failed due to " + TaskMO.getTaskFailureInfo(_context, morTask));
        }
    } finally {
        // make sure to let VM question monitor exit
        flags[0] = true;
    }
    return false;
}
Also used : VirtualMachineQuestionInfo(com.vmware.vim25.VirtualMachineQuestionInfo) VirtualMachineRuntimeInfo(com.vmware.vim25.VirtualMachineRuntimeInfo) VirtualMachineMessage(com.vmware.vim25.VirtualMachineMessage) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) ChoiceOption(com.vmware.vim25.ChoiceOption) ElementDescription(com.vmware.vim25.ElementDescription)

Aggregations

ElementDescription (com.vmware.vim25.ElementDescription)3 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)3 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)2 ChoiceOption (com.vmware.vim25.ChoiceOption)2 VirtualMachineMessage (com.vmware.vim25.VirtualMachineMessage)2 VirtualMachineQuestionInfo (com.vmware.vim25.VirtualMachineQuestionInfo)2 VirtualMachineRuntimeInfo (com.vmware.vim25.VirtualMachineRuntimeInfo)2 AboutInfo (com.vmware.vim25.AboutInfo)1 HostIpConfig (com.vmware.vim25.HostIpConfig)1 HostNetworkInfo (com.vmware.vim25.HostNetworkInfo)1 HostServiceTicket (com.vmware.vim25.HostServiceTicket)1 HostVirtualNic (com.vmware.vim25.HostVirtualNic)1 HostVirtualNicSpec (com.vmware.vim25.HostVirtualNicSpec)1 PerfCounterInfo (com.vmware.vim25.PerfCounterInfo)1 PerfEntityMetric (com.vmware.vim25.PerfEntityMetric)1 PerfMetricId (com.vmware.vim25.PerfMetricId)1 PerfMetricIntSeries (com.vmware.vim25.PerfMetricIntSeries)1 PerfProviderSummary (com.vmware.vim25.PerfProviderSummary)1 PerfQuerySpec (com.vmware.vim25.PerfQuerySpec)1 VimPortType (com.vmware.vim25.VimPortType)1