Search in sources :

Example 31 with Network

use of com.xensource.xenapi.Network in project cloudstack by apache.

the class CitrixResourceBase method prepareNetworkElementCommand.

protected ExecutionResult prepareNetworkElementCommand(final IpAssocCommand cmd) {
    final Connection conn = getConnection();
    final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
    final String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
    try {
        final IpAddressTO[] ips = cmd.getIpAddresses();
        for (final IpAddressTO ip : ips) {
            final VM router = getVM(conn, routerName);
            final NicTO nic = new NicTO();
            nic.setMac(ip.getVifMacAddress());
            nic.setType(ip.getTrafficType());
            if (ip.getBroadcastUri() == null) {
                nic.setBroadcastType(BroadcastDomainType.Native);
            } else {
                final URI uri = BroadcastDomainType.fromString(ip.getBroadcastUri());
                nic.setBroadcastType(BroadcastDomainType.getSchemeValue(uri));
                nic.setBroadcastUri(uri);
            }
            nic.setDeviceId(0);
            nic.setNetworkRateMbps(ip.getNetworkRate());
            nic.setName(ip.getNetworkName());
            final Network network = getNetwork(conn, nic);
            // Determine the correct VIF on DomR to associate/disassociate
            // the
            // IP address with
            VIF correctVif = getCorrectVif(conn, router, network);
            // If we are associating an IP address and DomR doesn't have a
            // VIF
            // for the specified vlan ID, we need to add a VIF
            // If we are disassociating the last IP address in the VLAN, we
            // need
            // to remove a VIF
            boolean addVif = false;
            if (ip.isAdd() && correctVif == null) {
                addVif = true;
            }
            if (addVif) {
                // Add a new VIF to DomR
                final String vifDeviceNum = getLowestAvailableVIFDeviceNum(conn, router);
                if (vifDeviceNum == null) {
                    throw new InternalErrorException("There were no more available slots for a new VIF on router: " + router.getNameLabel(conn));
                }
                nic.setDeviceId(Integer.parseInt(vifDeviceNum));
                correctVif = createVif(conn, routerName, router, null, nic);
                correctVif.plug(conn);
                // Add iptables rule for network usage
                networkUsage(conn, routerIp, "addVif", "eth" + correctVif.getDevice(conn));
            }
            if (ip.isAdd() && correctVif == null) {
                throw new InternalErrorException("Failed to find DomR VIF to associate/disassociate IP with.");
            }
            if (correctVif != null) {
                ip.setNicDevId(Integer.valueOf(correctVif.getDevice(conn)));
                ip.setNewNic(addVif);
            }
        }
    } catch (final InternalErrorException e) {
        s_logger.error("Ip Assoc failure on applying one ip due to exception:  ", e);
        return new ExecutionResult(false, e.getMessage());
    } catch (final Exception e) {
        return new ExecutionResult(false, e.getMessage());
    }
    return new ExecutionResult(true, null);
}
Also used : IpAddressTO(com.cloud.agent.api.to.IpAddressTO) Connection(com.xensource.xenapi.Connection) URLConnection(java.net.URLConnection) ExecutionResult(com.cloud.utils.ExecutionResult) InternalErrorException(com.cloud.exception.InternalErrorException) URI(java.net.URI) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) SAXException(org.xml.sax.SAXException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) VIF(com.xensource.xenapi.VIF) VM(com.xensource.xenapi.VM) Network(com.xensource.xenapi.Network) NicTO(com.cloud.agent.api.to.NicTO)

Example 32 with Network

use of com.xensource.xenapi.Network in project cloudstack by apache.

the class XenServer610WrapperTest method testMigrateWithStorageReceiveCommand.

@Test
public void testMigrateWithStorageReceiveCommand() {
    final String vmName = "small";
    final String uuid = "206b21a7-c6ec-40e2-b5e2-f861b9612f04";
    final Connection conn = Mockito.mock(Connection.class);
    final VirtualMachineTO vmSpec = Mockito.mock(VirtualMachineTO.class);
    final VolumeTO vol1 = Mockito.mock(VolumeTO.class);
    final VolumeTO vol2 = Mockito.mock(VolumeTO.class);
    final StorageFilerTO storage1 = Mockito.mock(StorageFilerTO.class);
    final StorageFilerTO storage2 = Mockito.mock(StorageFilerTO.class);
    final List<Pair<VolumeTO, String>> volumeToFiler = new ArrayList<>();
    volumeToFiler.add(new Pair<>(vol1, storage1.getPath()));
    volumeToFiler.add(new Pair<>(vol2, storage2.getPath()));
    final NicTO nicTO1 = Mockito.mock(NicTO.class);
    final NicTO nicTO2 = Mockito.mock(NicTO.class);
    final NicTO nicTO3 = Mockito.mock(NicTO.class);
    final NicTO[] nicTOs = { nicTO1, nicTO2, nicTO3 };
    final XsLocalNetwork nativeNetworkForTraffic = Mockito.mock(XsLocalNetwork.class);
    final Network network = Mockito.mock(Network.class);
    final XsHost xsHost = Mockito.mock(XsHost.class);
    final Network nw1 = Mockito.mock(Network.class);
    final Network nw2 = Mockito.mock(Network.class);
    final Network nw3 = Mockito.mock(Network.class);
    final SR sr1 = Mockito.mock(SR.class);
    final SR sr2 = Mockito.mock(SR.class);
    final MigrateWithStorageReceiveCommand migrateStorageCommand = new MigrateWithStorageReceiveCommand(vmSpec, volumeToFiler);
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    when(xenServer610Resource.getConnection()).thenReturn(conn);
    when(vmSpec.getName()).thenReturn(vmName);
    when(vmSpec.getNics()).thenReturn(nicTOs);
    when(storage1.getUuid()).thenReturn(uuid);
    when(storage2.getUuid()).thenReturn(uuid);
    when(xenServer610Resource.getStorageRepository(conn, storage1.getUuid())).thenReturn(sr1);
    when(xenServer610Resource.getStorageRepository(conn, storage2.getUuid())).thenReturn(sr2);
    try {
        when(xenServer610Resource.getNetwork(conn, nicTO1)).thenReturn(nw1);
        when(xenServer610Resource.getNetwork(conn, nicTO2)).thenReturn(nw2);
        when(xenServer610Resource.getNetwork(conn, nicTO3)).thenReturn(nw3);
        when(xenServer610Resource.getNativeNetworkForTraffic(conn, TrafficType.Storage, null)).thenReturn(nativeNetworkForTraffic);
        when(nativeNetworkForTraffic.getNetwork()).thenReturn(network);
        when(xenServer610Resource.getHost()).thenReturn(xsHost);
        when(xsHost.getUuid()).thenReturn(uuid);
    } catch (final XenAPIException e) {
        fail(e.getMessage());
    } catch (final XmlRpcException e) {
        fail(e.getMessage());
    }
    final Answer answer = wrapper.execute(migrateStorageCommand, xenServer610Resource);
    verify(xenServer610Resource, times(1)).getConnection();
    try {
        verify(xenServer610Resource, times(1)).getNetwork(conn, nicTO1);
        verify(xenServer610Resource, times(1)).getNetwork(conn, nicTO2);
        verify(xenServer610Resource, times(1)).getNetwork(conn, nicTO3);
        verify(xenServer610Resource, times(1)).getNativeNetworkForTraffic(conn, TrafficType.Storage, null);
        verify(nativeNetworkForTraffic, times(1)).getNetwork();
        verify(xenServer610Resource, times(1)).getHost();
        verify(xsHost, times(1)).getUuid();
    } catch (final XenAPIException e) {
        fail(e.getMessage());
    } catch (final XmlRpcException e) {
        fail(e.getMessage());
    }
    assertFalse(answer.getResult());
}
Also used : XsHost(com.cloud.hypervisor.xenserver.resource.XsHost) Connection(com.xensource.xenapi.Connection) ArrayList(java.util.ArrayList) XenAPIException(com.xensource.xenapi.Types.XenAPIException) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) MigrateWithStorageReceiveCommand(com.cloud.agent.api.MigrateWithStorageReceiveCommand) Answer(com.cloud.agent.api.Answer) VolumeTO(com.cloud.agent.api.to.VolumeTO) XsLocalNetwork(com.cloud.hypervisor.xenserver.resource.XsLocalNetwork) Network(com.xensource.xenapi.Network) XsLocalNetwork(com.cloud.hypervisor.xenserver.resource.XsLocalNetwork) XmlRpcException(org.apache.xmlrpc.XmlRpcException) Pair(com.cloud.utils.Pair) NicTO(com.cloud.agent.api.to.NicTO) SR(com.xensource.xenapi.SR) Test(org.junit.Test)

Example 33 with Network

use of com.xensource.xenapi.Network in project cloudstack by apache.

the class XenServer610WrapperTest method testMigrateWithStorageCommand.

@Test
public void testMigrateWithStorageCommand() {
    final String vmName = "small";
    final String uuid = "206b21a7-c6ec-40e2-b5e2-f861b9612f04";
    final String path = "/";
    final Connection conn = Mockito.mock(Connection.class);
    final VirtualMachineTO vmSpec = Mockito.mock(VirtualMachineTO.class);
    final VolumeTO vol1 = Mockito.mock(VolumeTO.class);
    final VolumeTO vol2 = Mockito.mock(VolumeTO.class);
    final StorageFilerTO storage1 = Mockito.mock(StorageFilerTO.class);
    final StorageFilerTO storage2 = Mockito.mock(StorageFilerTO.class);
    final Map<VolumeTO, StorageFilerTO> volumeToFiler = new HashMap<VolumeTO, StorageFilerTO>();
    volumeToFiler.put(vol1, storage1);
    volumeToFiler.put(vol2, storage2);
    final NicTO nicTO1 = Mockito.mock(NicTO.class);
    final NicTO nicTO2 = Mockito.mock(NicTO.class);
    final NicTO nicTO3 = Mockito.mock(NicTO.class);
    final NicTO[] nicTOs = { nicTO1, nicTO2, nicTO3 };
    final XsLocalNetwork nativeNetworkForTraffic = Mockito.mock(XsLocalNetwork.class);
    final Network networkForSm = Mockito.mock(Network.class);
    final XsHost xsHost = Mockito.mock(XsHost.class);
    final SR sr1 = Mockito.mock(SR.class);
    final SR sr2 = Mockito.mock(SR.class);
    final VDI vdi1 = Mockito.mock(VDI.class);
    final VDI vdi2 = Mockito.mock(VDI.class);
    final MigrateWithStorageCommand migrateStorageCommand = new MigrateWithStorageCommand(vmSpec, volumeToFiler);
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    when(xenServer610Resource.getConnection()).thenReturn(conn);
    when(vmSpec.getName()).thenReturn(vmName);
    when(vmSpec.getNics()).thenReturn(nicTOs);
    when(storage1.getUuid()).thenReturn(uuid);
    when(storage2.getUuid()).thenReturn(uuid);
    when(vol1.getPath()).thenReturn(path);
    when(vol2.getPath()).thenReturn(path);
    when(xenServer610Resource.getStorageRepository(conn, storage1.getUuid())).thenReturn(sr1);
    when(xenServer610Resource.getStorageRepository(conn, storage2.getUuid())).thenReturn(sr2);
    when(xenServer610Resource.getVDIbyUuid(conn, storage1.getPath())).thenReturn(vdi1);
    when(xenServer610Resource.getVDIbyUuid(conn, storage2.getPath())).thenReturn(vdi2);
    try {
        when(xenServer610Resource.getNativeNetworkForTraffic(conn, TrafficType.Storage, null)).thenReturn(nativeNetworkForTraffic);
        when(nativeNetworkForTraffic.getNetwork()).thenReturn(networkForSm);
        when(xenServer610Resource.getHost()).thenReturn(xsHost);
        when(xsHost.getUuid()).thenReturn(uuid);
    } catch (final XenAPIException e) {
        fail(e.getMessage());
    } catch (final XmlRpcException e) {
        fail(e.getMessage());
    }
    final Answer answer = wrapper.execute(migrateStorageCommand, xenServer610Resource);
    verify(xenServer610Resource, times(1)).getConnection();
    try {
        verify(xenServer610Resource, times(1)).prepareISO(conn, vmName, null, null);
        verify(xenServer610Resource, times(1)).getNetwork(conn, nicTO1);
        verify(xenServer610Resource, times(1)).getNetwork(conn, nicTO2);
        verify(xenServer610Resource, times(1)).getNetwork(conn, nicTO3);
        verify(xenServer610Resource, times(1)).getNativeNetworkForTraffic(conn, TrafficType.Storage, null);
        verify(nativeNetworkForTraffic, times(1)).getNetwork();
        verify(xenServer610Resource, times(1)).getHost();
        verify(xsHost, times(1)).getUuid();
    } catch (final XenAPIException e) {
        fail(e.getMessage());
    } catch (final XmlRpcException e) {
        fail(e.getMessage());
    }
    assertFalse(answer.getResult());
}
Also used : XsHost(com.cloud.hypervisor.xenserver.resource.XsHost) MigrateWithStorageCommand(com.cloud.agent.api.MigrateWithStorageCommand) HashMap(java.util.HashMap) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) Answer(com.cloud.agent.api.Answer) VolumeTO(com.cloud.agent.api.to.VolumeTO) XsLocalNetwork(com.cloud.hypervisor.xenserver.resource.XsLocalNetwork) Network(com.xensource.xenapi.Network) XsLocalNetwork(com.cloud.hypervisor.xenserver.resource.XsLocalNetwork) VDI(com.xensource.xenapi.VDI) XmlRpcException(org.apache.xmlrpc.XmlRpcException) NicTO(com.cloud.agent.api.to.NicTO) SR(com.xensource.xenapi.SR) Test(org.junit.Test)

Example 34 with Network

use of com.xensource.xenapi.Network in project cloudstack by apache.

the class XenServer610WrapperTest method testMigrateWithStorageSendCommand.

@Test
public void testMigrateWithStorageSendCommand() {
    final String vmName = "small";
    final String path = "/";
    final String mac = "3c:15:c2:c4:4f:18";
    final Connection conn = Mockito.mock(Connection.class);
    final VirtualMachineTO vmSpec = Mockito.mock(VirtualMachineTO.class);
    final VolumeTO volume1 = Mockito.mock(VolumeTO.class);
    final VolumeTO volume2 = Mockito.mock(VolumeTO.class);
    final SR sr1 = Mockito.mock(SR.class);
    final SR sr2 = Mockito.mock(SR.class);
    final VDI vdi1 = Mockito.mock(VDI.class);
    final VDI vdi2 = Mockito.mock(VDI.class);
    final NicTO nic1 = Mockito.mock(NicTO.class);
    final NicTO nic2 = Mockito.mock(NicTO.class);
    final Network network1 = Mockito.mock(Network.class);
    final Network network2 = Mockito.mock(Network.class);
    final List<Pair<VolumeTO, Object>> volumeToSr = new ArrayList<Pair<VolumeTO, Object>>();
    volumeToSr.add(new Pair<VolumeTO, Object>(volume1, sr1));
    volumeToSr.add(new Pair<VolumeTO, Object>(volume2, sr2));
    final List<Pair<NicTO, Object>> nicToNetwork = new ArrayList<Pair<NicTO, Object>>();
    nicToNetwork.add(new Pair<NicTO, Object>(nic1, network1));
    nicToNetwork.add(new Pair<NicTO, Object>(nic2, network2));
    final Map<String, String> token = new HashMap<String, String>();
    final VIF vif1 = Mockito.mock(VIF.class);
    final VIF vif2 = Mockito.mock(VIF.class);
    final MigrateWithStorageSendCommand migrateStorageCommand = new MigrateWithStorageSendCommand(vmSpec, volumeToSr, nicToNetwork, token);
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    when(xenServer610Resource.getConnection()).thenReturn(conn);
    when(vmSpec.getName()).thenReturn(vmName);
    when(volume1.getPath()).thenReturn(path);
    when(volume2.getPath()).thenReturn(path);
    when(nic1.getMac()).thenReturn(mac);
    when(nic2.getMac()).thenReturn(mac);
    when(xenServer610Resource.getVDIbyUuid(conn, volume1.getPath())).thenReturn(vdi1);
    when(xenServer610Resource.getVDIbyUuid(conn, volume2.getPath())).thenReturn(vdi2);
    try {
        when(xenServer610Resource.getVifByMac(conn, null, nic1.getMac())).thenReturn(vif1);
        when(xenServer610Resource.getVifByMac(conn, null, nic2.getMac())).thenReturn(vif2);
    } catch (final XenAPIException e) {
        fail(e.getMessage());
    } catch (final XmlRpcException e) {
        fail(e.getMessage());
    }
    final Answer answer = wrapper.execute(migrateStorageCommand, xenServer610Resource);
    verify(xenServer610Resource, times(1)).getConnection();
    try {
        verify(xenServer610Resource, times(2)).getVDIbyUuid(conn, volume1.getPath());
        verify(xenServer610Resource, times(2)).getVifByMac(conn, null, nic1.getMac());
    } catch (final XenAPIException e) {
        fail(e.getMessage());
    } catch (final XmlRpcException e) {
        fail(e.getMessage());
    }
    assertFalse(answer.getResult());
}
Also used : HashMap(java.util.HashMap) Connection(com.xensource.xenapi.Connection) ArrayList(java.util.ArrayList) XenAPIException(com.xensource.xenapi.Types.XenAPIException) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) Answer(com.cloud.agent.api.Answer) VolumeTO(com.cloud.agent.api.to.VolumeTO) VIF(com.xensource.xenapi.VIF) Network(com.xensource.xenapi.Network) XsLocalNetwork(com.cloud.hypervisor.xenserver.resource.XsLocalNetwork) VDI(com.xensource.xenapi.VDI) MigrateWithStorageSendCommand(com.cloud.agent.api.MigrateWithStorageSendCommand) XmlRpcException(org.apache.xmlrpc.XmlRpcException) SR(com.xensource.xenapi.SR) NicTO(com.cloud.agent.api.to.NicTO) Pair(com.cloud.utils.Pair) Test(org.junit.Test)

Example 35 with Network

use of com.xensource.xenapi.Network in project cloudstack by apache.

the class NotAValidCommand method testPvlanSetupCommandDhcpSuccess.

@Test
public void testPvlanSetupCommandDhcpSuccess() {
    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("true");
    final Answer answer = wrapper.execute(lanSetup, 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) 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)

Aggregations

Network (com.xensource.xenapi.Network)46 Connection (com.xensource.xenapi.Connection)35 XenAPIException (com.xensource.xenapi.Types.XenAPIException)32 XmlRpcException (org.apache.xmlrpc.XmlRpcException)30 Answer (com.cloud.agent.api.Answer)24 XsLocalNetwork (com.cloud.hypervisor.xenserver.resource.XsLocalNetwork)21 Test (org.junit.Test)19 RebootAnswer (com.cloud.agent.api.RebootAnswer)16 CreateAnswer (com.cloud.agent.api.storage.CreateAnswer)16 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)16 BadServerResponse (com.xensource.xenapi.Types.BadServerResponse)14 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)13 NicTO (com.cloud.agent.api.to.NicTO)10 VIF (com.xensource.xenapi.VIF)10 VM (com.xensource.xenapi.VM)8 HashMap (java.util.HashMap)8 XsHost (com.cloud.hypervisor.xenserver.resource.XsHost)7 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)6 Host (com.xensource.xenapi.Host)6 PIF (com.xensource.xenapi.PIF)6