Search in sources :

Example 6 with Network

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

the class CitrixResourceBase method setupLinkLocalNetwork.

public void setupLinkLocalNetwork(final Connection conn) {
    try {
        final Network.Record rec = new Network.Record();
        final Set<Network> networks = Network.getByNameLabel(conn, _linkLocalPrivateNetworkName);
        Network linkLocal = null;
        if (networks.size() == 0) {
            rec.nameDescription = "link local network used by system vms";
            rec.nameLabel = _linkLocalPrivateNetworkName;
            final Map<String, String> configs = new HashMap<String, String>();
            configs.put("ip_begin", NetUtils.getLinkLocalGateway());
            configs.put("ip_end", NetUtils.getLinkLocalIpEnd());
            configs.put("netmask", NetUtils.getLinkLocalNetMask());
            configs.put("vswitch-disable-in-band", "true");
            rec.otherConfig = configs;
            linkLocal = Network.create(conn, rec);
        } else {
            linkLocal = networks.iterator().next();
            if (!linkLocal.getOtherConfig(conn).containsKey("vswitch-disable-in-band")) {
                linkLocal.addToOtherConfig(conn, "vswitch-disable-in-band", "true");
            }
        }
        /* Make sure there is a physical bridge on this network */
        VIF dom0vif = null;
        final Pair<VM, VM.Record> vm = getControlDomain(conn);
        final VM dom0 = vm.first();
        final Set<VIF> vifs = dom0.getVIFs(conn);
        if (vifs.size() != 0) {
            for (final VIF vif : vifs) {
                final Map<String, String> otherConfig = vif.getOtherConfig(conn);
                if (otherConfig != null) {
                    final String nameLabel = otherConfig.get("nameLabel");
                    if (nameLabel != null && nameLabel.equalsIgnoreCase("link_local_network_vif")) {
                        dom0vif = vif;
                    }
                }
            }
        }
        /* create temp VIF0 */
        if (dom0vif == null) {
            s_logger.debug("Can't find a vif on dom0 for link local, creating a new one");
            final VIF.Record vifr = new VIF.Record();
            vifr.VM = dom0;
            vifr.device = getLowestAvailableVIFDeviceNum(conn, dom0);
            if (vifr.device == null) {
                s_logger.debug("Failed to create link local network, no vif available");
                return;
            }
            final Map<String, String> config = new HashMap<String, String>();
            config.put("nameLabel", "link_local_network_vif");
            vifr.otherConfig = config;
            vifr.MAC = "FE:FF:FF:FF:FF:FF";
            vifr.network = linkLocal;
            vifr.lockingMode = Types.VifLockingMode.NETWORK_DEFAULT;
            dom0vif = VIF.create(conn, vifr);
            plugDom0Vif(conn, dom0vif);
        } else {
            s_logger.debug("already have a vif on dom0 for link local network");
            if (!dom0vif.getCurrentlyAttached(conn)) {
                plugDom0Vif(conn, dom0vif);
            }
        }
        final String brName = linkLocal.getBridge(conn);
        callHostPlugin(conn, "vmops", "setLinkLocalIP", "brName", brName);
        _host.setLinkLocalNetwork(linkLocal.getUuid(conn));
    } catch (final XenAPIException e) {
        s_logger.warn("Unable to create local link network", e);
        throw new CloudRuntimeException("Unable to create local link network due to " + e.toString(), e);
    } catch (final XmlRpcException e) {
        s_logger.warn("Unable to create local link network", e);
        throw new CloudRuntimeException("Unable to create local link network due to " + e.toString(), e);
    }
}
Also used : HashMap(java.util.HashMap) XenAPIException(com.xensource.xenapi.Types.XenAPIException) VIF(com.xensource.xenapi.VIF) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.xensource.xenapi.Network) VM(com.xensource.xenapi.VM) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Example 7 with Network

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

the class CitrixResourceBase method getCorrectVif.

protected VIF getCorrectVif(final Connection conn, final VM router, final IpAddressTO ip) throws XmlRpcException, XenAPIException {
    final NicTO nic = new NicTO();
    nic.setType(ip.getTrafficType());
    nic.setName(ip.getNetworkName());
    if (ip.getBroadcastUri() == null) {
        nic.setBroadcastType(BroadcastDomainType.Native);
    } else {
        final URI uri = BroadcastDomainType.fromString(ip.getBroadcastUri());
        nic.setBroadcastType(BroadcastDomainType.getSchemeValue(uri));
        nic.setBroadcastUri(uri);
    }
    final Network network = getNetwork(conn, nic);
    // Determine the correct VIF on DomR to associate/disassociate the
    // IP address with
    final Set<VIF> routerVIFs = router.getVIFs(conn);
    for (final VIF vif : routerVIFs) {
        final Network vifNetwork = vif.getNetwork(conn);
        if (vifNetwork.getUuid(conn).equals(network.getUuid(conn))) {
            return vif;
        }
    }
    return null;
}
Also used : VIF(com.xensource.xenapi.VIF) Network(com.xensource.xenapi.Network) URI(java.net.URI) NicTO(com.cloud.agent.api.to.NicTO)

Example 8 with Network

use of com.xensource.xenapi.Network 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 9 with Network

use of com.xensource.xenapi.Network 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)

Example 10 with Network

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

the class CitrixSetupCommandWrapper method execute.

@Override
public Answer execute(final SetupCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    try {
        final Map<Pool, Pool.Record> poolRecs = Pool.getAllRecords(conn);
        if (poolRecs.size() != 1) {
            throw new CloudRuntimeException("There are " + poolRecs.size() + " pool for host :" + citrixResourceBase.getHost().getUuid());
        }
        final Host master = poolRecs.values().iterator().next().master;
        citrixResourceBase.setupServer(conn, master);
        final Host host = Host.getByUuid(conn, citrixResourceBase.getHost().getUuid());
        citrixResourceBase.setupServer(conn, host);
        if (!citrixResourceBase.setIptables(conn)) {
            s_logger.warn("set xenserver Iptable failed");
            return null;
        }
        if (citrixResourceBase.isSecurityGroupEnabled()) {
            final boolean canBridgeFirewall = citrixResourceBase.canBridgeFirewall(conn);
            citrixResourceBase.setCanBridgeFirewall(canBridgeFirewall);
            if (!canBridgeFirewall) {
                final String msg = "Failed to configure brige firewall";
                s_logger.warn(msg);
                s_logger.warn("Check host " + citrixResourceBase.getHost().getIp() + " for CSP is installed or not and check network mode for bridge");
                return new SetupAnswer(command, msg);
            }
        }
        final boolean r = citrixResourceBase.launchHeartBeat(conn);
        if (!r) {
            return null;
        }
        citrixResourceBase.cleanupTemplateSR(conn);
        try {
            if (command.useMultipath()) {
                // the config value is set to true
                host.addToOtherConfig(conn, "multipathing", "true");
                host.addToOtherConfig(conn, "multipathhandle", "dmp");
            }
        } catch (final Types.MapDuplicateKey e) {
            s_logger.debug("multipath is already set");
        }
        if (command.needSetup()) {
            final String result = citrixResourceBase.callHostPlugin(conn, "vmops", "setup_iscsi", "uuid", citrixResourceBase.getHost().getUuid());
            if (!result.contains("> DONE <")) {
                s_logger.warn("Unable to setup iscsi: " + result);
                return new SetupAnswer(command, result);
            }
            Pair<PIF, PIF.Record> mgmtPif = null;
            final Set<PIF> hostPifs = host.getPIFs(conn);
            for (final PIF pif : hostPifs) {
                final PIF.Record rec = pif.getRecord(conn);
                if (rec.management) {
                    if (rec.VLAN != null && rec.VLAN != -1) {
                        final String msg = new StringBuilder("Unsupported configuration.  Management network is on a VLAN.  host=").append(citrixResourceBase.getHost().getUuid()).append("; pif=").append(rec.uuid).append("; vlan=").append(rec.VLAN).toString();
                        s_logger.warn(msg);
                        return new SetupAnswer(command, msg);
                    }
                    if (s_logger.isDebugEnabled()) {
                        s_logger.debug("Management network is on pif=" + rec.uuid);
                    }
                    mgmtPif = new Pair<PIF, PIF.Record>(pif, rec);
                    break;
                }
            }
            if (mgmtPif == null) {
                final String msg = "Unable to find management network for " + citrixResourceBase.getHost().getUuid();
                s_logger.warn(msg);
                return new SetupAnswer(command, msg);
            }
            final Map<Network, Network.Record> networks = Network.getAllRecords(conn);
            if (networks == null) {
                final String msg = "Unable to setup as there are no networks in the host: " + citrixResourceBase.getHost().getUuid();
                s_logger.warn(msg);
                return new SetupAnswer(command, msg);
            }
            for (final Network.Record network : networks.values()) {
                if (network.nameLabel.equals("cloud-private")) {
                    for (final PIF pif : network.PIFs) {
                        final PIF.Record pr = pif.getRecord(conn);
                        if (citrixResourceBase.getHost().getUuid().equals(pr.host.getUuid(conn))) {
                            if (s_logger.isDebugEnabled()) {
                                s_logger.debug("Found a network called cloud-private. host=" + citrixResourceBase.getHost().getUuid() + ";  Network=" + network.uuid + "; pif=" + pr.uuid);
                            }
                            if (pr.VLAN != null && pr.VLAN != -1) {
                                final String msg = new StringBuilder("Unsupported configuration.  Network cloud-private is on a VLAN.  Network=").append(network.uuid).append(" ; pif=").append(pr.uuid).toString();
                                s_logger.warn(msg);
                                return new SetupAnswer(command, msg);
                            }
                            if (!pr.management && pr.bondMasterOf != null && pr.bondMasterOf.size() > 0) {
                                if (pr.bondMasterOf.size() > 1) {
                                    final String msg = new StringBuilder("Unsupported configuration.  Network cloud-private has more than one bond.  Network=").append(network.uuid).append("; pif=").append(pr.uuid).toString();
                                    s_logger.warn(msg);
                                    return new SetupAnswer(command, msg);
                                }
                                final Bond bond = pr.bondMasterOf.iterator().next();
                                final Set<PIF> slaves = bond.getSlaves(conn);
                                for (final PIF slave : slaves) {
                                    final PIF.Record spr = slave.getRecord(conn);
                                    if (spr.management) {
                                        if (!citrixResourceBase.transferManagementNetwork(conn, host, slave, spr, pif)) {
                                            final String msg = new StringBuilder("Unable to transfer management network.  slave=" + spr.uuid + "; master=" + pr.uuid + "; host=" + citrixResourceBase.getHost().getUuid()).toString();
                                            s_logger.warn(msg);
                                            return new SetupAnswer(command, msg);
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return new SetupAnswer(command, false);
    } catch (final XmlRpcException e) {
        s_logger.warn("Unable to setup", e);
        return new SetupAnswer(command, e.getMessage());
    } catch (final XenAPIException e) {
        s_logger.warn("Unable to setup", e);
        return new SetupAnswer(command, e.getMessage());
    } catch (final Exception e) {
        s_logger.warn("Unable to setup", e);
        return new SetupAnswer(command, e.getMessage());
    }
}
Also used : Types(com.xensource.xenapi.Types) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) Host(com.xensource.xenapi.Host) PIF(com.xensource.xenapi.PIF) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) SetupAnswer(com.cloud.agent.api.SetupAnswer) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.xensource.xenapi.Network) Pool(com.xensource.xenapi.Pool) Bond(com.xensource.xenapi.Bond) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

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