use of com.xensource.xenapi.Network in project cloudstack by apache.
the class CitrixResourceBase method setupvSwitchNetwork.
public synchronized Network setupvSwitchNetwork(final Connection conn) {
try {
if (_host.getVswitchNetwork() == null) {
Network vswitchNw = null;
final Network.Record rec = new Network.Record();
final String nwName = Networks.BroadcastScheme.VSwitch.toString();
final Set<Network> networks = Network.getByNameLabel(conn, nwName);
if (networks.size() == 0) {
rec.nameDescription = "vswitch network for " + nwName;
rec.nameLabel = nwName;
vswitchNw = Network.create(conn, rec);
} else {
vswitchNw = networks.iterator().next();
}
_host.setVswitchNetwork(vswitchNw);
}
return _host.getVswitchNetwork();
} catch (final BadServerResponse e) {
s_logger.error("Failed to setup vswitch network", e);
} catch (final XenAPIException e) {
s_logger.error("Failed to setup vswitch network", e);
} catch (final XmlRpcException e) {
s_logger.error("Failed to setup vswitch network", e);
}
return null;
}
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();
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);
}
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;
}
use of com.xensource.xenapi.Network in project cloudstack by apache.
the class CitrixStopCommandWrapper method execute.
@Override
public Answer execute(final StopCommand command, final CitrixResourceBase citrixResourceBase) {
final String vmName = command.getVmName();
String platformstring = null;
try {
final Connection conn = citrixResourceBase.getConnection();
final Set<VM> vms = VM.getByNameLabel(conn, vmName);
// stop vm which is running on this host or is in halted state
final Iterator<VM> iter = vms.iterator();
while (iter.hasNext()) {
final VM vm = iter.next();
final VM.Record vmr = vm.getRecord(conn);
if (vmr.powerState != VmPowerState.RUNNING) {
continue;
}
if (citrixResourceBase.isRefNull(vmr.residentOn)) {
continue;
}
if (vmr.residentOn.getUuid(conn).equals(citrixResourceBase.getHost().getUuid())) {
continue;
}
iter.remove();
}
if (vms.size() == 0) {
return new StopAnswer(command, "VM does not exist", true);
}
for (final VM vm : vms) {
final VM.Record vmr = vm.getRecord(conn);
platformstring = StringUtils.mapToString(vmr.platform);
if (vmr.isControlDomain) {
final String msg = "Tring to Shutdown control domain";
s_logger.warn(msg);
return new StopAnswer(command, msg, false);
}
if (vmr.powerState == VmPowerState.RUNNING && !citrixResourceBase.isRefNull(vmr.residentOn) && !vmr.residentOn.getUuid(conn).equals(citrixResourceBase.getHost().getUuid())) {
final String msg = "Stop Vm " + vmName + " failed due to this vm is not running on this host: " + citrixResourceBase.getHost().getUuid() + " but host:" + vmr.residentOn.getUuid(conn);
s_logger.warn(msg);
return new StopAnswer(command, msg, platformstring, false);
}
if (command.checkBeforeCleanup() && vmr.powerState == VmPowerState.RUNNING) {
final String msg = "Vm " + vmName + " is running on host and checkBeforeCleanup flag is set, so bailing out";
s_logger.debug(msg);
return new StopAnswer(command, msg, false);
}
s_logger.debug("9. The VM " + vmName + " is in Stopping state");
try {
if (vmr.powerState == VmPowerState.RUNNING) {
/* when stop a vm, set affinity to current xenserver */
vm.setAffinity(conn, vm.getResidentOn(conn));
if (citrixResourceBase.canBridgeFirewall()) {
final String result = citrixResourceBase.callHostPlugin(conn, "vmops", "destroy_network_rules_for_vm", "vmName", command.getVmName());
if (result == null || result.isEmpty() || !Boolean.parseBoolean(result)) {
s_logger.warn("Failed to remove network rules for vm " + command.getVmName());
} else {
s_logger.info("Removed network rules for vm " + command.getVmName());
}
}
citrixResourceBase.shutdownVM(conn, vm, vmName);
}
} catch (final Exception e) {
final String msg = "Catch exception " + e.getClass().getName() + " when stop VM:" + command.getVmName() + " due to " + e.toString();
s_logger.debug(msg);
return new StopAnswer(command, msg, platformstring, false);
} finally {
try {
if (vm.getPowerState(conn) == VmPowerState.HALTED) {
Set<VGPU> vGPUs = null;
// Get updated GPU details
try {
vGPUs = vm.getVGPUs(conn);
} catch (final XenAPIException e2) {
s_logger.debug("VM " + vmName + " does not have GPU support.");
}
if (vGPUs != null && !vGPUs.isEmpty()) {
final HashMap<String, HashMap<String, VgpuTypesInfo>> groupDetails = citrixResourceBase.getGPUGroupDetails(conn);
command.setGpuDevice(new GPUDeviceTO(null, null, groupDetails));
}
final Set<VIF> vifs = vm.getVIFs(conn);
final List<Network> networks = new ArrayList<Network>();
for (final VIF vif : vifs) {
networks.add(vif.getNetwork(conn));
}
vm.destroy(conn);
final SR sr = citrixResourceBase.getISOSRbyVmName(conn, command.getVmName());
citrixResourceBase.removeSR(conn, sr);
// anymore
for (final Network network : networks) {
try {
if (network.getNameLabel(conn).startsWith("VLAN")) {
citrixResourceBase.disableVlanNetwork(conn, network);
}
} catch (final Exception e) {
// network might be destroyed by other host
}
}
return new StopAnswer(command, "Stop VM " + vmName + " Succeed", platformstring, true);
}
} catch (final Exception e) {
final String msg = "VM destroy failed in Stop " + vmName + " Command due to " + e.getMessage();
s_logger.warn(msg, e);
} finally {
s_logger.debug("10. The VM " + vmName + " is in Stopped state");
}
}
}
} catch (final Exception e) {
final String msg = "Stop Vm " + vmName + " fail due to " + e.toString();
s_logger.warn(msg, e);
return new StopAnswer(command, msg, platformstring, false);
}
return new StopAnswer(command, "Stop VM failed", platformstring, false);
}
use of com.xensource.xenapi.Network in project cloudstack by apache.
the class CitrixUnPlugNicCommandWrapper method execute.
@Override
public Answer execute(final UnPlugNicCommand command, final CitrixResourceBase citrixResourceBase) {
final Connection conn = citrixResourceBase.getConnection();
final String vmName = command.getVmName();
try {
final Set<VM> vms = VM.getByNameLabel(conn, vmName);
if (vms == null || vms.isEmpty()) {
return new UnPlugNicAnswer(command, false, "Can not find VM " + vmName);
}
final VM vm = vms.iterator().next();
final NicTO nic = command.getNic();
final String mac = nic.getMac();
final VIF vif = citrixResourceBase.getVifByMac(conn, vm, mac);
if (vif != null) {
vif.unplug(conn);
final Network network = vif.getNetwork(conn);
vif.destroy(conn);
try {
if (network.getNameLabel(conn).startsWith("VLAN")) {
citrixResourceBase.disableVlanNetwork(conn, network);
}
} catch (final Exception e) {
}
}
return new UnPlugNicAnswer(command, true, "success");
} catch (final Exception e) {
final String msg = " UnPlug Nic failed due to " + e.toString();
s_logger.warn(msg, e);
return new UnPlugNicAnswer(command, false, msg);
}
}
Aggregations