Search in sources :

Example 61 with Network

use of com.xensource.xenapi.Network 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();
        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 (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, true);
                }
            }
        }
    } 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 : VIF(com.xensource.xenapi.VIF) IpAddressTO(com.cloud.agent.api.to.IpAddressTO) VM(com.xensource.xenapi.VM) Network(com.xensource.xenapi.Network) 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) NicTO(com.cloud.agent.api.to.NicTO)

Example 62 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 63 with Network

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

the class CitrixResourceBase method getNetworkByName.

/**
 * getNetworkByName() retrieves what the server thinks is the actual network
 * used by the XenServer host. This method should always be used to talk to
 * retrieve a network by the name. The reason is because of the problems in
 * using the name label as the way to find the Network.
 *
 * To see how we are working around these problems, take a look at
 * enableVlanNetwork(). The following description assumes you have looked at
 * the description on that method.
 *
 * In order to understand this, we have to see what type of networks are
 * within a XenServer that's under CloudStack control.
 *
 * - Native Networks: these are networks that are untagged on the XenServer
 * and are used to crate VLAN networks on. These are created by the user and
 * is assumed to be one per cluster. - VLAN Networks: these are dynamically
 * created by CloudStack and can have problems with duplicated names. -
 * LinkLocal Networks: these are dynamically created by CloudStack and can
 * also have problems with duplicated names but these don't have actual
 * PIFs.
 *
 * In order to speed to retrieval of a network, we do the following: - We
 * retrieve by the name. If only one network is retrieved, we assume we
 * retrieved the right network. - If more than one network is retrieved, we
 * check to see which one has the pif for the local host and use that. - If
 * a pif is not found, then we look at the tags and find the one with the
 * lowest timestamp. (See enableVlanNetwork())
 *
 * @param conn
 *            Xapi connection
 * @param name
 *            name of the network
 * @return XsNic an object that contains network, network record, pif, and
 *         pif record.
 * @throws XenAPIException
 * @throws XmlRpcException
 *
 * @see CitrixResourceBase#enableVlanNetwork
 */
public XsLocalNetwork getNetworkByName(final Connection conn, final String name) throws XenAPIException, XmlRpcException {
    final Set<Network> networks = Network.getByNameLabel(conn, name);
    if (networks.size() == 1) {
        return new XsLocalNetwork(this, networks.iterator().next(), null, null, null);
    }
    if (networks.size() == 0) {
        return null;
    }
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Found more than one network with the name " + name);
    }
    Network earliestNetwork = null;
    Network.Record earliestNetworkRecord = null;
    long earliestTimestamp = Long.MAX_VALUE;
    int earliestRandom = Integer.MAX_VALUE;
    for (final Network network : networks) {
        final XsLocalNetwork nic = new XsLocalNetwork(this, network);
        if (nic.getPif(conn) != null) {
            return nic;
        }
        final Network.Record record = network.getRecord(conn);
        if (record.tags != null) {
            for (final String tag : record.tags) {
                final Pair<Long, Integer> stamp = parseTimestamp(tag);
                if (stamp == null) {
                    continue;
                }
                if (stamp.first() < earliestTimestamp || stamp.first() == earliestTimestamp && stamp.second() < earliestRandom) {
                    earliestTimestamp = stamp.first();
                    earliestRandom = stamp.second();
                    earliestNetwork = network;
                    earliestNetworkRecord = record;
                }
            }
        }
    }
    return earliestNetwork != null ? new XsLocalNetwork(this, earliestNetwork, earliestNetworkRecord, null, null) : null;
}
Also used : Network(com.xensource.xenapi.Network)

Example 64 with Network

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

the class NotAValidCommand method testOvsDestroyTunnelCommandSuccess.

@Test
public void testOvsDestroyTunnelCommandSuccess() {
    final String bridge = "tunnel";
    final Connection conn = Mockito.mock(Connection.class);
    final Network network = Mockito.mock(Network.class);
    final OvsDestroyTunnelCommand destroyTunnel = new OvsDestroyTunnelCommand(1l, "net01", "port11");
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    when(citrixResourceBase.getConnection()).thenReturn(conn);
    try {
        when(citrixResourceBase.findOrCreateTunnelNetwork(conn, destroyTunnel.getBridgeName())).thenReturn(network);
        when(network.getBridge(conn)).thenReturn(bridge);
        when(citrixResourceBase.callHostPlugin(conn, "ovstunnel", "destroy_tunnel", "bridge", bridge, "in_port", destroyTunnel.getInPortName())).thenReturn("SUCCESS");
    } 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(destroyTunnel, 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) BadServerResponse(com.xensource.xenapi.Types.BadServerResponse) Network(com.xensource.xenapi.Network) XsLocalNetwork(com.cloud.hypervisor.xenserver.resource.XsLocalNetwork) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) OvsDestroyTunnelCommand(com.cloud.agent.api.OvsDestroyTunnelCommand) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 65 with Network

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

the class NotAValidCommand method testOvsVpcPhysicalTopologyConfigCommand.

@Test
public void testOvsVpcPhysicalTopologyConfigCommand() {
    final String bridge = "gre";
    final Connection conn = Mockito.mock(Connection.class);
    final Network network = Mockito.mock(Network.class);
    final OvsVpcPhysicalTopologyConfigCommand.Host[] hosts = new OvsVpcPhysicalTopologyConfigCommand.Host[0];
    final OvsVpcPhysicalTopologyConfigCommand.Tier[] tiers = new OvsVpcPhysicalTopologyConfigCommand.Tier[0];
    final OvsVpcPhysicalTopologyConfigCommand.Vm[] vms = new OvsVpcPhysicalTopologyConfigCommand.Vm[0];
    final OvsVpcPhysicalTopologyConfigCommand physicalTopology = new OvsVpcPhysicalTopologyConfigCommand(hosts, tiers, vms, "10.0.0.1/24");
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    when(citrixResourceBase.getConnection()).thenReturn(conn);
    try {
        when(citrixResourceBase.findOrCreateTunnelNetwork(conn, physicalTopology.getBridgeName())).thenReturn(network);
        when(network.getBridge(conn)).thenReturn(bridge);
        when(citrixResourceBase.callHostPlugin(conn, "ovstunnel", "configure_ovs_bridge_for_network_topology", "bridge", bridge, "config", physicalTopology.getVpcConfigInJson(), "host-id", ((Long) physicalTopology.getHostId()).toString(), "seq-no", Long.toString(1))).thenReturn("SUCCESS");
    } 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(physicalTopology, citrixResourceBase);
    verify(citrixResourceBase, times(1)).getConnection();
    assertFalse(answer.getResult());
}
Also used : OvsVpcPhysicalTopologyConfigCommand(com.cloud.agent.api.OvsVpcPhysicalTopologyConfigCommand) BadServerResponse(com.xensource.xenapi.Types.BadServerResponse) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XsHost(com.cloud.hypervisor.xenserver.resource.XsHost) Host(com.xensource.xenapi.Host) 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) Network(com.xensource.xenapi.Network) XsLocalNetwork(com.cloud.hypervisor.xenserver.resource.XsLocalNetwork) XmlRpcException(org.apache.xmlrpc.XmlRpcException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Network (com.xensource.xenapi.Network)69 Connection (com.xensource.xenapi.Connection)50 XenAPIException (com.xensource.xenapi.Types.XenAPIException)45 XmlRpcException (org.apache.xmlrpc.XmlRpcException)41 Answer (com.cloud.agent.api.Answer)31 XsLocalNetwork (com.cloud.hypervisor.xenserver.resource.XsLocalNetwork)30 Test (org.junit.Test)26 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)23 RebootAnswer (com.cloud.agent.api.RebootAnswer)20 CreateAnswer (com.cloud.agent.api.storage.CreateAnswer)20 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)20 NicTO (com.cloud.agent.api.to.NicTO)18 VIF (com.xensource.xenapi.VIF)17 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)16 HashMap (java.util.HashMap)15 SR (com.xensource.xenapi.SR)14 BadServerResponse (com.xensource.xenapi.Types.BadServerResponse)14 VM (com.xensource.xenapi.VM)14 XsHost (com.cloud.hypervisor.xenserver.resource.XsHost)13 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)12