Search in sources :

Example 16 with VIF

use of com.xensource.xenapi.VIF 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 17 with VIF

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

the class CitrixResourceBase method getLowestAvailableVIFDeviceNum.

public String getLowestAvailableVIFDeviceNum(final Connection conn, final VM vm) {
    String vmName = "";
    try {
        vmName = vm.getNameLabel(conn);
        final List<Integer> usedDeviceNums = new ArrayList<Integer>();
        final Set<VIF> vifs = vm.getVIFs(conn);
        final Iterator<VIF> vifIter = vifs.iterator();
        while (vifIter.hasNext()) {
            final VIF vif = vifIter.next();
            try {
                final String deviceId = vif.getDevice(conn);
                if (vm.getIsControlDomain(conn) || vif.getCurrentlyAttached(conn)) {
                    usedDeviceNums.add(Integer.valueOf(deviceId));
                } else {
                    s_logger.debug("Found unplugged VIF " + deviceId + " in VM " + vmName + " destroy it");
                    vif.destroy(conn);
                }
            } catch (final NumberFormatException e) {
                final String msg = "Obtained an invalid value for an allocated VIF device number for VM: " + vmName;
                s_logger.debug(msg, e);
                throw new CloudRuntimeException(msg);
            }
        }
        for (Integer i = 0; i < _maxNics; i++) {
            if (!usedDeviceNums.contains(i)) {
                s_logger.debug("Lowest available Vif device number: " + i + " for VM: " + vmName);
                return i.toString();
            }
        }
    } catch (final XmlRpcException e) {
        final String msg = "Caught XmlRpcException: " + e.getMessage();
        s_logger.warn(msg, e);
    } catch (final XenAPIException e) {
        final String msg = "Caught XenAPIException: " + e.toString();
        s_logger.warn(msg, e);
    }
    throw new CloudRuntimeException("Could not find available VIF slot in VM with name: " + vmName);
}
Also used : VIF(com.xensource.xenapi.VIF) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ArrayList(java.util.ArrayList) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Example 18 with VIF

use of com.xensource.xenapi.VIF 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 19 with VIF

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

the class CitrixResourceBase method cleanupNetworkElementCommand.

protected ExecutionResult cleanupNetworkElementCommand(final IpAssocCommand cmd) {
    final Connection conn = getConnection();
    final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
    final String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
    final String lastIp = cmd.getAccessDetail(NetworkElementCommand.NETWORK_PUB_LAST_IP);
    try {
        final IpAddressTO[] ips = cmd.getIpAddresses();
        final int ipsCount = ips.length;
        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());
            Network network = getNetwork(conn, nic);
            // If we are disassociating the last IP address in the VLAN, we
            // need
            // to remove a VIF
            boolean removeVif = false;
            // remove the nic
            if (org.apache.commons.lang.StringUtils.equalsIgnoreCase(lastIp, "true") && !ip.isAdd()) {
                final VIF correctVif = getCorrectVif(conn, router, network);
                // in isolated network eth2 is the default public interface. We don't want to delete it.
                if (correctVif != null && !correctVif.getDevice(conn).equals("2")) {
                    removeVif = true;
                }
            }
            if (removeVif) {
                // Determine the correct VIF on DomR to
                // associate/disassociate the
                // IP address with
                final VIF correctVif = getCorrectVif(conn, router, network);
                if (correctVif != null) {
                    network = correctVif.getNetwork(conn);
                    // Mark this vif to be removed from network usage
                    networkUsage(conn, routerIp, "deleteVif", "eth" + correctVif.getDevice(conn));
                    // Remove the VIF from DomR
                    correctVif.unplug(conn);
                    correctVif.destroy(conn);
                    // Disable the VLAN network if necessary
                    disableVlanNetwork(conn, network);
                }
            }
        }
    } catch (final Exception e) {
        s_logger.debug("Ip Assoc failure on applying one ip due to 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) 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)

Aggregations

VIF (com.xensource.xenapi.VIF)19 VM (com.xensource.xenapi.VM)14 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)12 Connection (com.xensource.xenapi.Connection)12 XenAPIException (com.xensource.xenapi.Types.XenAPIException)12 XmlRpcException (org.apache.xmlrpc.XmlRpcException)11 NicTO (com.cloud.agent.api.to.NicTO)10 Network (com.xensource.xenapi.Network)10 InternalErrorException (com.cloud.exception.InternalErrorException)8 IOException (java.io.IOException)8 MalformedURLException (java.net.MalformedURLException)8 URISyntaxException (java.net.URISyntaxException)8 TimeoutException (java.util.concurrent.TimeoutException)8 ConfigurationException (javax.naming.ConfigurationException)8 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)8 SAXException (org.xml.sax.SAXException)8 ExecutionResult (com.cloud.utils.ExecutionResult)6 URLConnection (java.net.URLConnection)6 HashMap (java.util.HashMap)5 IpAddressTO (com.cloud.agent.api.to.IpAddressTO)4