Search in sources :

Example 1 with XenAPIException

use of com.xensource.xenapi.Types.XenAPIException in project cloudstack by apache.

the class CitrixPvlanSetupCommandWrapper method execute.

@Override
public Answer execute(final PvlanSetupCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    final String primaryPvlan = command.getPrimary();
    final String isolatedPvlan = command.getIsolated();
    final String op = command.getOp();
    final String dhcpName = command.getDhcpName();
    final String dhcpMac = command.getDhcpMac();
    final String dhcpIp = command.getDhcpIp();
    final String vmMac = command.getVmMac();
    final String networkTag = command.getNetworkTag();
    String nwNameLabel = null;
    try {
        final XsLocalNetwork nw = citrixResourceBase.getNativeNetworkForTraffic(conn, TrafficType.Guest, networkTag);
        if (nw == null) {
            s_logger.error("Network is not configured on the backend for pvlan " + primaryPvlan);
            throw new CloudRuntimeException("Network for the backend is not configured correctly for pvlan primary: " + primaryPvlan);
        }
        nwNameLabel = nw.getNetwork().getNameLabel(conn);
    } catch (final XenAPIException e) {
        s_logger.warn("Fail to get network", e);
        return new Answer(command, false, e.toString());
    } catch (final XmlRpcException e) {
        s_logger.warn("Fail to get network", e);
        return new Answer(command, false, e.toString());
    }
    String result = null;
    if (command.getType() == PvlanSetupCommand.Type.DHCP) {
        result = citrixResourceBase.callHostPlugin(conn, "ovs-pvlan", "setup-pvlan-dhcp", "op", op, "nw-label", nwNameLabel, "primary-pvlan", primaryPvlan, "isolated-pvlan", isolatedPvlan, "dhcp-name", dhcpName, "dhcp-ip", dhcpIp, "dhcp-mac", dhcpMac);
        if (result == null || result.isEmpty() || !Boolean.parseBoolean(result)) {
            s_logger.warn("Failed to program pvlan for dhcp server with mac " + dhcpMac);
            return new Answer(command, false, result);
        } else {
            s_logger.info("Programmed pvlan for dhcp server with mac " + dhcpMac);
        }
    } else if (command.getType() == PvlanSetupCommand.Type.VM) {
        result = citrixResourceBase.callHostPlugin(conn, "ovs-pvlan", "setup-pvlan-vm", "op", op, "nw-label", nwNameLabel, "primary-pvlan", primaryPvlan, "isolated-pvlan", isolatedPvlan, "vm-mac", vmMac);
        if (result == null || result.isEmpty() || !Boolean.parseBoolean(result)) {
            s_logger.warn("Failed to program pvlan for vm with mac " + vmMac);
            return new Answer(command, false, result);
        } else {
            s_logger.info("Programmed pvlan for vm with mac " + vmMac);
        }
    }
    return new Answer(command, true, result);
}
Also used : Answer(com.cloud.agent.api.Answer) XsLocalNetwork(com.cloud.hypervisor.xenserver.resource.XsLocalNetwork) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Example 2 with XenAPIException

use of com.xensource.xenapi.Types.XenAPIException in project cloudstack by apache.

the class CitrixReadyCommandWrapper method execute.

@Override
public Answer execute(final ReadyCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    final Long dcId = command.getDataCenterId();
    // Ignore the result of the callHostPlugin. Even if unmounting the
    // snapshots dir fails, let Ready command
    // succeed.
    citrixResourceBase.umountSnapshotDir(conn, dcId);
    citrixResourceBase.setupLinkLocalNetwork(conn);
    // try to destroy CD-ROM device for all system VMs on this host
    try {
        final Host host = Host.getByUuid(conn, citrixResourceBase.getHost().getUuid());
        final Set<VM> vms = host.getResidentVMs(conn);
        for (final VM vm : vms) {
            citrixResourceBase.destroyPatchVbd(conn, vm.getNameLabel(conn));
        }
    } catch (final Exception e) {
    }
    try {
        final boolean result = citrixResourceBase.cleanupHaltedVms(conn);
        if (!result) {
            return new ReadyAnswer(command, "Unable to cleanup halted vms");
        }
    } catch (final XenAPIException e) {
        s_logger.warn("Unable to cleanup halted vms", e);
        return new ReadyAnswer(command, "Unable to cleanup halted vms");
    } catch (final XmlRpcException e) {
        s_logger.warn("Unable to cleanup halted vms", e);
        return new ReadyAnswer(command, "Unable to cleanup halted vms");
    }
    return new ReadyAnswer(command);
}
Also used : VM(com.xensource.xenapi.VM) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) Host(com.xensource.xenapi.Host) XmlRpcException(org.apache.xmlrpc.XmlRpcException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) ReadyAnswer(com.cloud.agent.api.ReadyAnswer) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Example 3 with XenAPIException

use of com.xensource.xenapi.Types.XenAPIException in project cloudstack by apache.

the class NotAValidCommand method testPvlanSetupCommandDhcpFailure.

@Test
public void testPvlanSetupCommandDhcpFailure() {
    final String label = "net";
    final Connection conn = Mockito.mock(Connection.class);
    final XsLocalNetwork network = Mockito.mock(XsLocalNetwork.class);
    final Network network2 = Mockito.mock(Network.class);
    final PvlanSetupCommand lanSetup = PvlanSetupCommand.createDhcpSetup("add", URI.create("http://127.0.0.1"), "tag", "dhcp", "0:0:0:0:0:0", "127.0.0.1");
    final String primaryPvlan = lanSetup.getPrimary();
    final String isolatedPvlan = lanSetup.getIsolated();
    final String op = lanSetup.getOp();
    final String dhcpName = lanSetup.getDhcpName();
    final String dhcpMac = lanSetup.getDhcpMac();
    final String dhcpIp = lanSetup.getDhcpIp();
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    when(citrixResourceBase.getConnection()).thenReturn(conn);
    try {
        when(citrixResourceBase.getNativeNetworkForTraffic(conn, TrafficType.Guest, "tag")).thenReturn(network);
        when(network.getNetwork()).thenReturn(network2);
        when(network2.getNameLabel(conn)).thenReturn(label);
    } catch (final XenAPIException e) {
        fail(e.getMessage());
    } catch (final XmlRpcException e) {
        fail(e.getMessage());
    }
    when(citrixResourceBase.callHostPlugin(conn, "ovs-pvlan", "setup-pvlan-dhcp", "op", op, "nw-label", label, "primary-pvlan", primaryPvlan, "isolated-pvlan", isolatedPvlan, "dhcp-name", dhcpName, "dhcp-ip", dhcpIp, "dhcp-mac", dhcpMac)).thenReturn("false");
    final Answer answer = wrapper.execute(lanSetup, citrixResourceBase);
    verify(citrixResourceBase, times(1)).getConnection();
    assertFalse(answer.getResult());
}
Also used : RebootAnswer(com.cloud.agent.api.RebootAnswer) CreateAnswer(com.cloud.agent.api.storage.CreateAnswer) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) XsLocalNetwork(com.cloud.hypervisor.xenserver.resource.XsLocalNetwork) Network(com.xensource.xenapi.Network) XsLocalNetwork(com.cloud.hypervisor.xenserver.resource.XsLocalNetwork) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) PvlanSetupCommand(com.cloud.agent.api.PvlanSetupCommand) XmlRpcException(org.apache.xmlrpc.XmlRpcException) Test(org.junit.Test)

Example 4 with XenAPIException

use of com.xensource.xenapi.Types.XenAPIException in project cloudstack by apache.

the class NotAValidCommand method testMaintainCommand.

@Test
public void testMaintainCommand() {
    // This test needs further work.
    final String uuid = "befc4dcd-f5c6-4015-8791-3c18622b7c7f";
    final Connection conn = Mockito.mock(Connection.class);
    final XsHost xsHost = Mockito.mock(XsHost.class);
    final XmlRpcClient client = Mockito.mock(XmlRpcClient.class);
    // final Host.Record hr = PowerMockito.mock(Host.Record.class);
    // final Host host = PowerMockito.mock(Host.class);
    final MaintainCommand maintainCommand = new MaintainCommand();
    final Map<String, Object> map = new Hashtable<String, Object>();
    map.put("Value", "Xen");
    final Map<String, Object> spiedMap = spy(map);
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    when(citrixResourceBase.getConnection()).thenReturn(conn);
    when(citrixResourceBase.getHost()).thenReturn(xsHost);
    when(xsHost.getUuid()).thenReturn(uuid);
    when(conn.getSessionReference()).thenReturn("befc4dcd");
    try {
        final Object[] params = { Marshalling.toXMLRPC("befc4dcd"), Marshalling.toXMLRPC(uuid) };
        when(client.execute("host.get_by_uuid", new Object[] { "befc4dcd", uuid })).thenReturn(spiedMap);
        PowerMockito.when(conn, "dispatch", "host.get_by_uuid", params).thenReturn(spiedMap);
    } catch (final Exception e) {
        fail(e.getMessage());
    }
    // try {
    // PowerMockito.mockStatic(Host.class);
    // //BDDMockito.given(Host.getByUuid(conn,
    // xsHost.getUuid())).willReturn(host);
    // PowerMockito.when(Host.getByUuid(conn,
    // xsHost.getUuid())).thenReturn(host);
    // PowerMockito.verifyStatic(times(1));
    // } catch (final BadServerResponse e) {
    // fail(e.getMessage());
    // } catch (final XenAPIException e) {
    // fail(e.getMessage());
    // } catch (final XmlRpcException e) {
    // fail(e.getMessage());
    // }
    //
    // PowerMockito.mockStatic(Types.class);
    // PowerMockito.when(Types.toHostRecord(spiedMap)).thenReturn(hr);
    // PowerMockito.verifyStatic(times(1));
    //
    // try {
    // PowerMockito.mockStatic(Host.Record.class);
    // when(host.getRecord(conn)).thenReturn(hr);
    // verify(host, times(1)).getRecord(conn);
    // } catch (final BadServerResponse e) {
    // fail(e.getMessage());
    // } catch (final XenAPIException e) {
    // fail(e.getMessage());
    // } catch (final XmlRpcException e) {
    // fail(e.getMessage());
    // }
    final Answer answer = wrapper.execute(maintainCommand, citrixResourceBase);
    assertFalse(answer.getResult());
}
Also used : MaintainCommand(com.cloud.agent.api.MaintainCommand) RebootAnswer(com.cloud.agent.api.RebootAnswer) CreateAnswer(com.cloud.agent.api.storage.CreateAnswer) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) XsHost(com.cloud.hypervisor.xenserver.resource.XsHost) XmlRpcClient(org.apache.xmlrpc.client.XmlRpcClient) Hashtable(java.util.Hashtable) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) Test(org.junit.Test)

Example 5 with XenAPIException

use of com.xensource.xenapi.Types.XenAPIException in project cloudstack by apache.

the class NotAValidCommand method testOvsFetchInterfaceCommand.

@Test
public void testOvsFetchInterfaceCommand() {
    final String label = "[abc]";
    final String uuid = "befc4dcd-f5c6-4015-8791-3c18622b7c7f";
    final Connection conn = Mockito.mock(Connection.class);
    final XsLocalNetwork network = Mockito.mock(XsLocalNetwork.class);
    final Network network2 = Mockito.mock(Network.class);
    final PIF pif = Mockito.mock(PIF.class);
    final PIF.Record pifRec = Mockito.mock(PIF.Record.class);
    final XsHost xsHost = Mockito.mock(XsHost.class);
    final OvsFetchInterfaceCommand fetchInterCommand = new OvsFetchInterfaceCommand(label);
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    when(citrixResourceBase.isXcp()).thenReturn(true);
    when(citrixResourceBase.getLabel()).thenReturn("[abc]");
    when(citrixResourceBase.getConnection()).thenReturn(conn);
    when(citrixResourceBase.getHost()).thenReturn(xsHost);
    try {
        when(network.getNetwork()).thenReturn(network2);
        when(network.getPif(conn)).thenReturn(pif);
        when(network.getPif(conn)).thenReturn(pif);
        when(pif.getRecord(conn)).thenReturn(pifRec);
        when(network.getNetwork().getUuid(conn)).thenReturn(uuid);
        when(citrixResourceBase.getNetworkByName(conn, label)).thenReturn(network);
    } catch (final XenAPIException e) {
        fail(e.getMessage());
    } catch (final XmlRpcException e) {
        fail(e.getMessage());
    }
    final Answer answer = wrapper.execute(fetchInterCommand, citrixResourceBase);
    verify(citrixResourceBase, times(1)).getConnection();
    assertTrue(answer.getResult());
}
Also used : RebootAnswer(com.cloud.agent.api.RebootAnswer) CreateAnswer(com.cloud.agent.api.storage.CreateAnswer) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) XsHost(com.cloud.hypervisor.xenserver.resource.XsHost) XsLocalNetwork(com.cloud.hypervisor.xenserver.resource.XsLocalNetwork) OvsFetchInterfaceCommand(com.cloud.agent.api.OvsFetchInterfaceCommand) Network(com.xensource.xenapi.Network) XsLocalNetwork(com.cloud.hypervisor.xenserver.resource.XsLocalNetwork) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) PIF(com.xensource.xenapi.PIF) XmlRpcException(org.apache.xmlrpc.XmlRpcException) Test(org.junit.Test)

Aggregations

XenAPIException (com.xensource.xenapi.Types.XenAPIException)101 XmlRpcException (org.apache.xmlrpc.XmlRpcException)93 Connection (com.xensource.xenapi.Connection)56 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)53 Answer (com.cloud.agent.api.Answer)33 InternalErrorException (com.cloud.exception.InternalErrorException)31 SR (com.xensource.xenapi.SR)30 Host (com.xensource.xenapi.Host)29 Network (com.xensource.xenapi.Network)27 BadServerResponse (com.xensource.xenapi.Types.BadServerResponse)26 VDI (com.xensource.xenapi.VDI)25 IOException (java.io.IOException)25 HashMap (java.util.HashMap)25 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)25 Test (org.junit.Test)25 ConfigurationException (javax.naming.ConfigurationException)22 XsLocalNetwork (com.cloud.hypervisor.xenserver.resource.XsLocalNetwork)21 MalformedURLException (java.net.MalformedURLException)21 URISyntaxException (java.net.URISyntaxException)21 TimeoutException (java.util.concurrent.TimeoutException)21