Search in sources :

Example 1 with UnPlugNicCommand

use of com.cloud.agent.api.UnPlugNicCommand in project cloudstack by apache.

the class LibvirtComputingResourceTest method testUnPlugNicCommandLibvirtException.

@SuppressWarnings("unchecked")
@Test
public void testUnPlugNicCommandLibvirtException() {
    final NicTO nic = Mockito.mock(NicTO.class);
    final String instanceName = "Test";
    final UnPlugNicCommand command = new UnPlugNicCommand(nic, instanceName);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    try {
        when(libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName())).thenThrow(LibvirtException.class);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, libvirtComputingResource);
    assertFalse(answer.getResult());
    verify(libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
    try {
        verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(command.getVmName());
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
}
Also used : UnsupportedAnswer(com.cloud.agent.api.UnsupportedAnswer) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) LibvirtException(org.libvirt.LibvirtException) NicTO(com.cloud.agent.api.to.NicTO) UnPlugNicCommand(com.cloud.agent.api.UnPlugNicCommand) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with UnPlugNicCommand

use of com.cloud.agent.api.UnPlugNicCommand in project cloudstack by apache.

the class HypervDirectConnectResource method executeRequest.

// TODO: Is it valid to return NULL, or should we throw on error?
@Override
public final Answer executeRequest(final Command cmd) {
    // Set HTTP POST destination URI
    // Using java.net.URI, see
    // http://docs.oracle.com/javase/1.5.0/docs/api/java/net/URI.html
    URI agentUri = null;
    final Class<? extends Command> clazz = cmd.getClass();
    Answer answer = null;
    try {
        final String cmdName = cmd.getClass().getName();
        agentUri = new URI("https", null, _agentIp, _port, "/api/HypervResource/" + cmdName, null, null);
    } catch (final URISyntaxException e) {
        // TODO add proper logging
        final String errMsg = "Could not generate URI for Hyper-V agent";
        s_logger.error(errMsg, e);
        return null;
    }
    if (cmd instanceof NetworkElementCommand) {
        return _vrResource.executeRequest((NetworkElementCommand) cmd);
    }
    if (clazz == CheckSshCommand.class) {
        answer = execute((CheckSshCommand) cmd);
    } else if (cmd instanceof NetworkUsageCommand) {
        answer = execute((NetworkUsageCommand) cmd);
    } else if (clazz == PingTestCommand.class) {
        answer = execute((PingTestCommand) cmd);
    } else if (clazz == PlugNicCommand.class) {
        answer = execute((PlugNicCommand) cmd);
    } else if (clazz == UnPlugNicCommand.class) {
        answer = execute((UnPlugNicCommand) cmd);
    } else if (clazz == CopyCommand.class) {
        answer = execute((CopyCommand) cmd);
    } else {
        if (clazz == StartCommand.class) {
            final VirtualMachineTO vmSpec = ((StartCommand) cmd).getVirtualMachine();
            if (vmSpec.getType() != VirtualMachine.Type.User) {
                if (s_hypervMgr != null) {
                    final String secondary = s_hypervMgr.prepareSecondaryStorageStore(Long.parseLong(_zoneId));
                    if (secondary != null) {
                        ((StartCommand) cmd).setSecondaryStorage(secondary);
                    }
                } else {
                    s_logger.error("Hyperv manager isn't available. Couldn't check and copy the systemvm iso.");
                }
            }
        }
        // Send the cmd to hyperv agent.
        final String ansStr = postHttpRequest(s_gson.toJson(cmd), agentUri);
        if (ansStr == null) {
            return Answer.createUnsupportedCommandAnswer(cmd);
        }
        // Only Answer instances are returned by remote agents.
        // E.g. see Response.getAnswers()
        final Answer[] result = s_gson.fromJson(ansStr, Answer[].class);
        final String logResult = cleanPassword(s_gson.toJson(result));
        s_logger.debug("executeRequest received response " + logResult);
        if (result.length > 0) {
            return result[0];
        }
    }
    return answer;
}
Also used : CheckSshCommand(com.cloud.agent.api.check.CheckSshCommand) StartCommand(com.cloud.agent.api.StartCommand) CopyCommand(org.apache.cloudstack.storage.command.CopyCommand) URISyntaxException(java.net.URISyntaxException) NetworkElementCommand(com.cloud.agent.api.routing.NetworkElementCommand) NetworkUsageCommand(com.cloud.agent.api.NetworkUsageCommand) URI(java.net.URI) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) UnPlugNicCommand(com.cloud.agent.api.UnPlugNicCommand) UnsupportedAnswer(com.cloud.agent.api.UnsupportedAnswer) CheckSshAnswer(com.cloud.agent.api.check.CheckSshAnswer) GetDomRVersionAnswer(com.cloud.agent.api.GetDomRVersionAnswer) CheckS2SVpnConnectionsAnswer(com.cloud.agent.api.CheckS2SVpnConnectionsAnswer) SetPortForwardingRulesAnswer(com.cloud.agent.api.routing.SetPortForwardingRulesAnswer) SetSourceNatAnswer(com.cloud.agent.api.routing.SetSourceNatAnswer) PlugNicAnswer(com.cloud.agent.api.PlugNicAnswer) GetVmConfigAnswer(com.cloud.agent.api.GetVmConfigAnswer) NetworkUsageAnswer(com.cloud.agent.api.NetworkUsageAnswer) Answer(com.cloud.agent.api.Answer) UnPlugNicAnswer(com.cloud.agent.api.UnPlugNicAnswer) SetStaticNatRulesAnswer(com.cloud.agent.api.routing.SetStaticNatRulesAnswer) IpAssocAnswer(com.cloud.agent.api.routing.IpAssocAnswer) SetFirewallRulesAnswer(com.cloud.agent.api.routing.SetFirewallRulesAnswer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) SetStaticRouteAnswer(com.cloud.agent.api.routing.SetStaticRouteAnswer) PlugNicCommand(com.cloud.agent.api.PlugNicCommand) UnPlugNicCommand(com.cloud.agent.api.UnPlugNicCommand) PingTestCommand(com.cloud.agent.api.PingTestCommand)

Example 3 with UnPlugNicCommand

use of com.cloud.agent.api.UnPlugNicCommand in project cloudstack by apache.

the class Ovm3VmSupportTest method unPlugNicTest.

@Test
public void unPlugNicTest() throws ConfigurationException, URISyntaxException {
    hypervisor = support.prepare(configTest.getParams());
    NicTO nic = prepNic(xen.getVmNicMac(), 200, TrafficType.Guest);
    UnPlugNicCommand plug = new UnPlugNicCommand(nic, xen.getVmName());
    Answer ra = hypervisor.executeRequest(plug);
    results.basicBooleanTest(ra.getResult());
}
Also used : Answer(com.cloud.agent.api.Answer) NicTO(com.cloud.agent.api.to.NicTO) UnPlugNicCommand(com.cloud.agent.api.UnPlugNicCommand) Test(org.junit.Test) ConnectionTest(com.cloud.hypervisor.ovm3.objects.ConnectionTest) CloudStackPluginTest(com.cloud.hypervisor.ovm3.objects.CloudStackPluginTest) XenTest(com.cloud.hypervisor.ovm3.objects.XenTest) Ovm3HypervisorResourceTest(com.cloud.hypervisor.ovm3.resources.Ovm3HypervisorResourceTest) Ovm3SupportTest(com.cloud.hypervisor.ovm3.support.Ovm3SupportTest) XmlTestResultTest(com.cloud.hypervisor.ovm3.objects.XmlTestResultTest)

Example 4 with UnPlugNicCommand

use of com.cloud.agent.api.UnPlugNicCommand in project cosmic by MissionCriticalCloud.

the class VirtualMachineManagerImpl method unplugNic.

public boolean unplugNic(final Network network, final NicTO nic, final VirtualMachineTO vm, final ReservationContext context, final DeployDestination dest) throws ConcurrentOperationException, ResourceUnavailableException {
    boolean result = true;
    final VMInstanceVO router = _vmDao.findById(vm.getId());
    if (router.getState() == State.Running) {
        try {
            final Commands cmds = new Commands(Command.OnError.Stop);
            final UnPlugNicCommand unplugNicCmd = new UnPlugNicCommand(nic, vm.getName());
            cmds.addCommand("unplugnic", unplugNicCmd);
            _agentMgr.send(dest.getHost().getId(), cmds);
            final UnPlugNicAnswer unplugNicAnswer = cmds.getAnswer(UnPlugNicAnswer.class);
            if (!(unplugNicAnswer != null && unplugNicAnswer.getResult())) {
                s_logger.warn("Unable to unplug nic from router " + router);
                result = false;
            }
        } catch (final OperationTimedoutException e) {
            throw new AgentUnavailableException("Unable to unplug nic from rotuer " + router + " from network " + network, dest.getHost().getId(), e);
        }
    } else if (router.getState() == State.Stopped || router.getState() == State.Stopping) {
        s_logger.debug("Vm " + router.getInstanceName() + " is in " + router.getState() + ", so not sending unplug nic command to the backend");
    } else {
        s_logger.warn("Unable to apply unplug nic, Vm " + router + " is not in the right state " + router.getState());
        throw new ResourceUnavailableException("Unable to apply unplug nic on the backend," + " vm " + router + " is not in the right state", DataCenter.class, router.getDataCenterId());
    }
    return result;
}
Also used : OperationTimedoutException(com.cloud.exception.OperationTimedoutException) DataCenter(com.cloud.dc.DataCenter) UnPlugNicAnswer(com.cloud.agent.api.UnPlugNicAnswer) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) Commands(com.cloud.agent.manager.Commands) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) UnPlugNicCommand(com.cloud.agent.api.UnPlugNicCommand)

Example 5 with UnPlugNicCommand

use of com.cloud.agent.api.UnPlugNicCommand in project cosmic by MissionCriticalCloud.

the class LibvirtComputingResourceTest method testUnPlugNicCommandNoNics.

@Test
public void testUnPlugNicCommandNoNics() {
    final NicTO nic = Mockito.mock(NicTO.class);
    final String instanceName = "Test";
    final UnPlugNicCommand command = new UnPlugNicCommand(nic, instanceName);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final Connect conn = Mockito.mock(Connect.class);
    final Domain vm = Mockito.mock(Domain.class);
    final List<InterfaceDef> nics = new ArrayList<>();
    final VifDriver vifDriver = Mockito.mock(VifDriver.class);
    final List<VifDriver> drivers = new ArrayList<>();
    drivers.add(vifDriver);
    when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    when(libvirtComputingResource.getInterfaces(conn, command.getVmName())).thenReturn(nics);
    try {
        when(libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName())).thenReturn(conn);
        when(libvirtComputingResource.getDomain(conn, instanceName)).thenReturn(vm);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, libvirtComputingResource);
    assertTrue(answer.getResult());
    verify(libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
    try {
        verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(command.getVmName());
        verify(libvirtComputingResource, times(1)).getDomain(conn, instanceName);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) ArrayList(java.util.ArrayList) UnPlugNicCommand(com.cloud.agent.api.UnPlugNicCommand) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.InterfaceDef) Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) AttachAnswer(com.cloud.storage.command.AttachAnswer) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) Domain(org.libvirt.Domain) NicTO(com.cloud.agent.api.to.NicTO) Test(org.junit.Test)

Aggregations

UnPlugNicCommand (com.cloud.agent.api.UnPlugNicCommand)15 Answer (com.cloud.agent.api.Answer)13 NicTO (com.cloud.agent.api.to.NicTO)10 Test (org.junit.Test)10 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)7 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)6 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)6 LibvirtException (org.libvirt.LibvirtException)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 NetworkUsageCommand (com.cloud.agent.api.NetworkUsageCommand)3 PingTestCommand (com.cloud.agent.api.PingTestCommand)3 PlugNicCommand (com.cloud.agent.api.PlugNicCommand)3 RebootAnswer (com.cloud.agent.api.RebootAnswer)3 ArrayList (java.util.ArrayList)3 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)3 Connect (org.libvirt.Connect)3 Domain (org.libvirt.Domain)3 AttachIsoCommand (com.cloud.agent.api.AttachIsoCommand)2 BackupSnapshotCommand (com.cloud.agent.api.BackupSnapshotCommand)2 CheckHealthCommand (com.cloud.agent.api.CheckHealthCommand)2